home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 18 REXX
/
18-REXX.zip
/
yes2rexx.zip
/
yes.cmd
Wrap
OS/2 REXX Batch file
|
2002-04-09
|
6KB
|
116 lines
/* OS/2 REXX implementation of the UNIX yes "filter" optionally */
/* using any "local" Yes / No / Abort / Retry / Ignore character: */
call trace 'O' /* disable any SET RXTRACE=ON */
signal on novalue name TRAP ; signal on syntax name TRAP
signal on failure name TRAP ; signal on halt name TRAP
call UTIL 'SysSleep' ; call UTIL 'SysGetMessage'
YES = strip( arg( 1 )) /* stripped arg = output line */
if pos( left( YES, 1 ), '-/' ) > 0 then do /* "-" or "/": */
GOT = translate( substr( YES, 2, 1 )) /* option char */
YES = strip( substr( YES, 3 )) /* white space */
select
when GOT = '-' then nop /* -- = IGNORE */
when YES <> '' then exit TELL( YES ) /* if bad arg. */
when \ datatype( GOT, 'w' ) then exit TELL( GOT )
when 1 > GOT | GOT > 5 then exit TELL( GOT )
otherwise YES = left( word( SysGetMessage( 0 ), GOT ), 1 )
end /* last word is no character */
end
else GOT = '' /* no option, default YES 'y' */
if abbrev( YES, '"' ) & lastpos( '"', YES ) = length( YES )
then YES = substr( YES, 2, length( YES ) - 2 )
if YES = '' & GOT = '' then YES = 'y'
signal on notready name DONE /* NOTREADY: pipe broken => exit */
do while lineout( /**/, YES ) = 0 ; call SysSleep 1 ; end
DONE: exit 0
TELL: procedure /* unknown option or -?, -h, -u */
parse source . . YES ; L = x2c( 0D0A )
M = L || L || 'usage:' YES ' | prog'
M = M || L || 'or :' YES ' text | prog'
M = M || L || 'or :' YES '-- text | prog'
M = M || L || 'or :' YES '-n | prog'
M = M || L || 'The 1st form sends yes-lines to a program.'
M = M || L || 'The 2nd form allows to specify which text'
M = M || L || 'is sent instead of "y", e.g. Y, n, NO, ...'
M = M || L || 'The 3rd form is used either to send texts'
M = M || L || 'starting with "-" or to send empty lines.'
M = M || L || 'Finally -n (n = 1..5) sends word 1..5 of'
M = M || L || 'SysGetMessage(0): Y N Abort Retry Ignore,'
M = M || L || '= on this system:' SysGetMessage( 0 )
if wordpos( arg( 1 ), '? h H u U' ) = 0
then return TRAP( 'unknown option -' arg( 1 ) || M )
say M ; return 1 /* TRAP is ugly but save: STDERR */
UTIL: procedure /* load necessary RexxUtil entry */
if RxFuncQuery( arg( 1 )) then
if RxFuncAdd( arg( 1 ), 'RexxUtil', arg( 1 )) then
exit TRAP( "can't add RexxUtil" arg( 1 ))
return 0
TRAP: /* select REXX exception handler */
call trace 'O' ; trace N /* don't trace interactive */
parse source TRAP /* source on separate line */
TRAP = x2c( 0D ) || right( '+++', 10 ) TRAP || x2c( 0D0A )
TRAP = TRAP || right( '+++', 10 ) /* = standard trace prefix */
TRAP = TRAP condition( 'c' ) 'trap:' condition( 'd' )
select
when wordpos( condition( 'c' ), 'ERROR FAILURE' ) > 0 then do
if condition( 'd' ) > '' /* need an additional line */
then TRAP = TRAP || x2c( 0D0A ) || right( '+++', 10 )
TRAP = TRAP '(RC' rc || ')' /* any system error codes */
if condition( 'c' ) = 'FAILURE' then rc = -3
end
when wordpos( condition( 'c' ), 'HALT SYNTAX' ) > 0 then do
if condition( 'c' ) = 'HALT' then rc = 4
if condition( 'd' ) > '' & condition( 'd' ) <> rc then do
if condition( 'd' ) <> errortext( rc ) then do
TRAP = TRAP || x2c( 0D0A ) || right( '+++', 10 )
TRAP = TRAP errortext( rc )
end /* future condition( 'd' ) */
end /* may use errortext( rc ) */
else TRAP = TRAP errortext( rc )
rc = -rc /* rc < 0: REXX error code */
end
when condition( 'c' ) = 'NOVALUE' then rc = -2 /* dubious */
when condition( 'c' ) = 'NOTREADY' then rc = -1 /* dubious */
otherwise /* force non-zero whole rc */
if datatype( value( 'RC' ), 'W' ) = 0 then rc = 1
if condition() = '' then TRAP = TRAP arg( 1 )
end /* direct: TRAP( message ) */
TRAP = TRAP || x2c( 0D0A ) || format( sigl, 6 )
signal on syntax name TRAP.SIGL /* throw syntax error 3... */
if 0 < sigl & sigl <= sourceline() /* if no handle for source */
then TRAP = TRAP '*-*' strip( sourceline( sigl ))
else TRAP = TRAP '+++ (source line unavailable)'
TRAP.SIGL: /* ...catch syntax error 3 */
if abbrev( right( TRAP, 2 + 6 ), x2c( 0D0A )) then do
TRAP = TRAP '+++ (source line unreadable)' ; rc = -rc
end
select
when 1 then do /* in pipes STDERR: output */
parse version TRAP.REXX . . /* REXX/Personal: \dev\con */
signal on syntax name TRAP.FAIL
if TRAP.REXX = 'REXXSAA' /* fails if no more handle */
then call lineout 'STDERR' , TRAP
else call lineout '\dev\con', TRAP
end
when 0 then do /* OS/2 PM: RxMessageBox() */
signal on syntax name TRAP.FAIL
call RxMessageBox , /* fails if not in PMREXX */
translate( TRAP, ' ', x2c( 0D )), , 'CANCEL', 'WARNING'
end /* replace any CR by blank */
otherwise say TRAP ; trace ?L /* interactive Label trace */
end
if condition() = 'SIGNAL' then signal TRAP.EXIT
TRAP.CALL: return rc /* continue after CALL ON */
TRAP.FAIL: say TRAP ; rc = 0 - rc /* force TRAP error output */
TRAP.EXIT: exit rc /* exit for any SIGNAL ON */