home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / yes2rexx.zip / yes.cmd
OS/2 REXX Batch file  |  2002-04-09  |  6KB  |  116 lines

  1. /* OS/2 REXX implementation of the UNIX yes "filter" optionally   */
  2. /* using any "local" Yes / No / Abort / Retry / Ignore character: */
  3.  
  4.    call trace 'O'                   /* disable any SET RXTRACE=ON */
  5.    signal on novalue  name TRAP  ;  signal on syntax name TRAP
  6.    signal on failure  name TRAP  ;  signal on halt   name TRAP
  7.    call UTIL 'SysSleep'          ;  call UTIL 'SysGetMessage'
  8.  
  9.    YES = strip( arg( 1 ))           /* stripped arg = output line */
  10.  
  11.    if pos( left( YES, 1 ), '-/' ) > 0 then do      /* "-" or "/": */
  12.       GOT = translate( substr( YES, 2, 1 ))        /* option char */
  13.       YES = strip( substr( YES, 3 ))               /* white space */
  14.       select
  15.          when GOT = '-' then nop                   /* -- = IGNORE */
  16.          when YES <> '' then exit TELL( YES )      /* if bad arg. */
  17.          when \ datatype( GOT, 'w' ) then exit TELL( GOT )
  18.          when 1 > GOT | GOT > 5      then exit TELL( GOT )
  19.          otherwise YES = left( word( SysGetMessage( 0 ), GOT ), 1 )
  20.       end                           /* last word is no character  */
  21.    end
  22.    else GOT = ''                    /* no option, default YES 'y' */
  23.  
  24.    if abbrev( YES, '"' ) & lastpos( '"', YES ) = length( YES )
  25.       then YES = substr( YES, 2, length( YES ) - 2 )
  26.    if YES = '' & GOT = '' then YES = 'y'
  27.  
  28.    signal on notready name DONE  /* NOTREADY: pipe broken => exit */
  29.    do while lineout( /**/, YES ) = 0   ;  call SysSleep 1   ;  end
  30. DONE: exit 0
  31.  
  32. TELL: procedure                  /* unknown option or -?, -h, -u  */
  33.    parse source . . YES ;  L = x2c( 0D0A )
  34.    M = L || L || 'usage:' YES '        | prog'
  35.    M = M || L || 'or   :' YES '   text | prog'
  36.    M = M || L || 'or   :' YES '-- text | prog'
  37.    M = M || L || 'or   :' YES '-n      | prog'
  38.    M = M || L || 'The 1st form sends yes-lines to a program.'
  39.    M = M || L || 'The 2nd form allows to specify which text'
  40.    M = M || L || 'is sent instead of "y", e.g. Y, n, NO, ...'
  41.    M = M || L || 'The 3rd form is used either to send texts'
  42.    M = M || L || 'starting with "-" or to send empty lines.'
  43.    M = M || L || 'Finally -n (n = 1..5) sends word 1..5 of'
  44.    M = M || L || 'SysGetMessage(0): Y N Abort Retry Ignore,'
  45.    M = M || L || '= on this system:' SysGetMessage( 0 )
  46.    if wordpos( arg( 1 ), '? h H u U' ) = 0
  47.       then return TRAP( 'unknown option -' arg( 1 ) || M )
  48.    say M ;  return 1             /* TRAP is ugly but save: STDERR */
  49.  
  50. UTIL: procedure                  /* load necessary RexxUtil entry */
  51.    if RxFuncQuery(  arg( 1 )) then
  52.       if RxFuncAdd( arg( 1 ), 'RexxUtil', arg( 1 )) then
  53.          exit TRAP( "can't add RexxUtil"  arg( 1 ))
  54.    return 0
  55.  
  56. TRAP:                            /* select REXX exception handler */
  57.    call trace 'O' ;  trace N           /* don't trace interactive */
  58.    parse source TRAP                   /* source on separate line */
  59.    TRAP = x2c( 0D ) || right( '+++', 10 ) TRAP || x2c( 0D0A )
  60.    TRAP = TRAP || right( '+++', 10 )   /* = standard trace prefix */
  61.    TRAP = TRAP condition( 'c' ) 'trap:' condition( 'd' )
  62.    select
  63.       when wordpos( condition( 'c' ), 'ERROR FAILURE' ) > 0 then do
  64.          if condition( 'd' ) > ''      /* need an additional line */
  65.             then TRAP = TRAP || x2c( 0D0A ) || right( '+++', 10 )
  66.          TRAP = TRAP '(RC' rc || ')'   /* any system error codes  */
  67.          if condition( 'c' ) = 'FAILURE' then rc = -3
  68.       end
  69.       when wordpos( condition( 'c' ), 'HALT SYNTAX'   ) > 0 then do
  70.          if condition( 'c' ) = 'HALT' then rc = 4
  71.          if condition( 'd' ) > '' & condition( 'd' ) <> rc then do
  72.             if condition( 'd' ) <> errortext( rc ) then do
  73.                TRAP = TRAP || x2c( 0D0A ) || right( '+++', 10 )
  74.                TRAP = TRAP errortext( rc )
  75.             end                        /* future condition( 'd' ) */
  76.          end                           /* may use errortext( rc ) */
  77.          else  TRAP = TRAP errortext( rc )
  78.          rc = -rc                      /* rc < 0: REXX error code */
  79.       end
  80.       when condition( 'c' ) = 'NOVALUE'  then rc = -2 /* dubious  */
  81.       when condition( 'c' ) = 'NOTREADY' then rc = -1 /* dubious  */
  82.       otherwise                        /* force non-zero whole rc */
  83.          if datatype( value( 'RC' ), 'W' ) = 0 then rc = 1
  84.          if condition() = '' then TRAP = TRAP arg( 1 )
  85.    end                                 /* direct: TRAP( message ) */
  86.  
  87.    TRAP = TRAP || x2c( 0D0A ) || format( sigl, 6 )
  88.    signal on syntax name TRAP.SIGL     /* throw syntax error 3... */
  89.    if 0 < sigl & sigl <= sourceline()  /* if no handle for source */
  90.       then TRAP = TRAP '*-*' strip( sourceline( sigl ))
  91.       else TRAP = TRAP '+++ (source line unavailable)'
  92. TRAP.SIGL:                             /* ...catch syntax error 3 */
  93.    if abbrev( right( TRAP, 2 + 6 ), x2c( 0D0A )) then do
  94.       TRAP = TRAP '+++ (source line unreadable)'   ;  rc = -rc
  95.    end
  96.    select
  97.       when 1 then do                   /* in pipes STDERR: output */
  98.          parse version TRAP.REXX . .   /* REXX/Personal: \dev\con */
  99.          signal on syntax name TRAP.FAIL
  100.          if TRAP.REXX = 'REXXSAA'      /* fails if no more handle */
  101.             then call lineout 'STDERR'  , TRAP
  102.             else call lineout '\dev\con', TRAP
  103.       end
  104.       when 0 then do                   /* OS/2 PM: RxMessageBox() */
  105.          signal on syntax name TRAP.FAIL
  106.          call RxMessageBox ,           /* fails if not in PMREXX  */
  107.             translate( TRAP, ' ', x2c( 0D )), , 'CANCEL', 'WARNING'
  108.       end                              /* replace any CR by blank */
  109.       otherwise   say TRAP ; trace ?L  /* interactive Label trace */
  110.    end
  111.  
  112.    if condition() = 'SIGNAL' then signal TRAP.EXIT
  113. TRAP.CALL:  return rc                  /* continue after CALL ON  */
  114. TRAP.FAIL:  say TRAP ;  rc = 0 - rc    /* force TRAP error output */
  115. TRAP.EXIT:  exit   rc                  /* exit for any SIGNAL ON  */
  116.