home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rex22emx.zip / demo / shell.rexx < prev    next >
OS/2 REXX Batch file  |  2000-04-20  |  5KB  |  186 lines

  1. /* small shell program. */
  2. trace off
  3. signal on halt
  4. signal on syntax
  5. /*
  6. address command
  7. */
  8. $home=value("HOME",,"ENVIRONMENT")
  9. $startup=home'/.shellrc'
  10. address command 'test -f' $startup
  11. if $rc=0 then address unix 'rxstack < '$startup
  12. $alias.=""
  13. $noargs.=0
  14. /*
  15. hostname='/bin/hostname'()
  16. */
  17. trace o
  18. $hostname='hostname'()
  19. $rc=0
  20. $n=1
  21. $t=0
  22. $hist=20
  23. $do_level=0
  24. $select_level=0
  25. $if_level=0
  26. $cmd=''
  27. $last_nest = ''
  28. $first_nest = ''
  29. $nested = 0
  30. restart:
  31. do $i=$n
  32.    $qu=queued()
  33.    if $qu=0 then do
  34.       /*
  35.        * If nothing queued, then we are interactive, so display the
  36.        * prompt.
  37.        */
  38.        if $nested = 0 then do
  39.           $r="Ready"
  40.           if $rc\=0 then $r=$r "("$rc")"
  41.           if $t>0 then $r=$r"; T="format($t,,2)
  42.           say $r
  43.           call charout ,$hostname":"$i">"
  44.           end
  45.        else do
  46.           $prompt = '>'Copies(' ',Length($hostname)+Length($i)+(2*($do_level+$select_level+$if_level)))
  47.           call charout ,$prompt
  48.           end
  49.           
  50.    end
  51.    $t=0
  52.    /*
  53.     * Get the next command from the user
  54.     */
  55.    parse pull $command 1 $word $@ 1 $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
  56.    $echo=left($word,1)\=='@'
  57.    if \$echo then parse var $command 2 $command 2 $word $@ 2,
  58.       $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
  59.    /*
  60.     * Echo the command with the prompt.
  61.     */
  62.    if $qu>0 & $echo then say $hostname":"$i">"$command
  63.    if $alias.$word\=="" then
  64.       /*
  65.        * Get aliases...
  66.        */
  67.       if $noargs.$word then $command=$alias.$word
  68.       else $command=$alias.$word $@
  69.    if $command="??" then do
  70.       /*
  71.        * Show the history...
  72.        */
  73.       do $l=max(1,$i-$hist) to $i-1
  74.          $idx = $l//$hist
  75.          say right($l,4)':'$line.$idx
  76.       end
  77.       $i=$i-1
  78.       iterate $i
  79.    end
  80.    if left($command,1)="!" then do
  81.       /*
  82.        * Check for a specific history entry...
  83.        */
  84.       $what=substr($command,2)
  85.       if \datatype($what,'W') then do
  86.          do $l=$i-1 to max(1,$i-$hist) by -1
  87.             $idx = $l//$hist
  88.             if abbrev($line.$idx,$what,1) then leave $l
  89.          end
  90.          $what=$l
  91.       end
  92.       if $what<$i-$hist|$what<1|$what>=$i then do
  93.          say "Event not found"
  94.          $i=$i-1
  95.          iterate $i
  96.       end
  97.       $idx = $what//$hist
  98.       $command=$line.$idx
  99.       say $command
  100.    end
  101.    $srcline = sourceline()
  102.    $idx = $i//$hist
  103.    $line.$idx=$command
  104.    /*
  105.     * Determine the command entered...
  106.     */
  107.    parse var $command $c $t1 $t2 1 . $tt
  108.    $c=translate($c)
  109.    select
  110.       when $c='EXIT' then do
  111.          say 'Exiting rexx shell'
  112.          if $t1="" then exit
  113.          else exit $t1
  114.       end
  115.       when $c='SETENV' then $rc=value($t1,$t2,"ENVIRONMENT")
  116.       when $c = 'FRED' then interpret 'do' $tt
  117.       when $c='SAY' then do
  118.          if $nested then $cmd = $cmd $c $tt ';'
  119.          else interpret 'say' $tt
  120.       end
  121.       when $c='DO' then do
  122.          $nested = 1
  123.          $do_level = $do_level + 1
  124.          if $do_level \= 0 Then $cmd = $cmd $c $tt ';'
  125.          else $cmd = $cmd $tt ';'
  126.          $last_nest = 'DO'
  127.          if $first_nest = '' Then $first_nest = 'DO'
  128.       end
  129.       when $c='END' then do
  130.          select
  131.             when $last_nest = 'DO' then $do_level = $do_level - 1
  132.             when $select_nest = 'SELECT' then $select_level = $select_level - 1
  133.             otherwise nop
  134.          end
  135.          $cmd = $cmd $c
  136.          if ($do_level + $select_level + $if_level) = 0 then do
  137.             say $first_nest $cmd
  138.             select
  139.                when $first_nest = 'DO' then interpret 'do' $cmd
  140.                when $first_nest = 'SELECT' then interpret 'select' $cmd
  141.                otherwise nop
  142.             end
  143.             $first_nest = ''
  144.             $cmd = ''
  145.          end
  146.          else $cmd = $cmd $c $tt ';'
  147.       end
  148.       when $c=';' then do
  149.          call time 'R'
  150.          interpret $tt
  151.          $t=time('E')
  152.       end 
  153.       when $c='CD' then do
  154.          $rc=$chdir($tt)
  155.          if $rc\=0 then say $tt':' ioerr($rc)
  156.       end
  157.       when $c='ALIAS' then do
  158.          if $t2="" then say $alias.$t1
  159.          else do
  160.             $alias.$t1=$t2
  161.             if left($2,1)==';' then $noargs.$t1=1
  162.          end
  163.       end
  164.       otherwise
  165.          call time 'R'
  166.          $command
  167.          $t=time('E')
  168.    end
  169.    if $do_level = 0 & $select_level = 0 & $if_level = 0 then $nested = 0
  170.    else $nested = 1
  171. end
  172.  
  173. halt:
  174. signal on halt
  175. say "Interrupted!"
  176. $n=$i
  177. if sigl>$srcline then $n=$n+1
  178. signal restart
  179.  
  180. syntax:
  181. signal on syntax
  182. say right(sigl,5) "+++" errortext($rc)
  183. $n=$i
  184. if sigl>$srcline then $n=$n+1
  185. signal restart
  186.