home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 18 REXX
/
18-REXX.zip
/
rex22emx.zip
/
demo
/
shell.rexx
< prev
next >
Wrap
OS/2 REXX Batch file
|
2000-04-20
|
5KB
|
186 lines
/* small shell program. */
trace off
signal on halt
signal on syntax
/*
address command
*/
$home=value("HOME",,"ENVIRONMENT")
$startup=home'/.shellrc'
address command 'test -f' $startup
if $rc=0 then address unix 'rxstack < '$startup
$alias.=""
$noargs.=0
/*
hostname='/bin/hostname'()
*/
trace o
$hostname='hostname'()
$rc=0
$n=1
$t=0
$hist=20
$do_level=0
$select_level=0
$if_level=0
$cmd=''
$last_nest = ''
$first_nest = ''
$nested = 0
restart:
do $i=$n
$qu=queued()
if $qu=0 then do
/*
* If nothing queued, then we are interactive, so display the
* prompt.
*/
if $nested = 0 then do
$r="Ready"
if $rc\=0 then $r=$r "("$rc")"
if $t>0 then $r=$r"; T="format($t,,2)
say $r
call charout ,$hostname":"$i">"
end
else do
$prompt = '>'Copies(' ',Length($hostname)+Length($i)+(2*($do_level+$select_level+$if_level)))
call charout ,$prompt
end
end
$t=0
/*
* Get the next command from the user
*/
parse pull $command 1 $word $@ 1 $0 $1 $2 $3 $4 $5 $6 $7 $8 $9
$echo=left($word,1)\=='@'
if \$echo then parse var $command 2 $command 2 $word $@ 2,
$0 $1 $2 $3 $4 $5 $6 $7 $8 $9
/*
* Echo the command with the prompt.
*/
if $qu>0 & $echo then say $hostname":"$i">"$command
if $alias.$word\=="" then
/*
* Get aliases...
*/
if $noargs.$word then $command=$alias.$word
else $command=$alias.$word $@
if $command="??" then do
/*
* Show the history...
*/
do $l=max(1,$i-$hist) to $i-1
$idx = $l//$hist
say right($l,4)':'$line.$idx
end
$i=$i-1
iterate $i
end
if left($command,1)="!" then do
/*
* Check for a specific history entry...
*/
$what=substr($command,2)
if \datatype($what,'W') then do
do $l=$i-1 to max(1,$i-$hist) by -1
$idx = $l//$hist
if abbrev($line.$idx,$what,1) then leave $l
end
$what=$l
end
if $what<$i-$hist|$what<1|$what>=$i then do
say "Event not found"
$i=$i-1
iterate $i
end
$idx = $what//$hist
$command=$line.$idx
say $command
end
$srcline = sourceline()
$idx = $i//$hist
$line.$idx=$command
/*
* Determine the command entered...
*/
parse var $command $c $t1 $t2 1 . $tt
$c=translate($c)
select
when $c='EXIT' then do
say 'Exiting rexx shell'
if $t1="" then exit
else exit $t1
end
when $c='SETENV' then $rc=value($t1,$t2,"ENVIRONMENT")
when $c = 'FRED' then interpret 'do' $tt
when $c='SAY' then do
if $nested then $cmd = $cmd $c $tt ';'
else interpret 'say' $tt
end
when $c='DO' then do
$nested = 1
$do_level = $do_level + 1
if $do_level \= 0 Then $cmd = $cmd $c $tt ';'
else $cmd = $cmd $tt ';'
$last_nest = 'DO'
if $first_nest = '' Then $first_nest = 'DO'
end
when $c='END' then do
select
when $last_nest = 'DO' then $do_level = $do_level - 1
when $select_nest = 'SELECT' then $select_level = $select_level - 1
otherwise nop
end
$cmd = $cmd $c
if ($do_level + $select_level + $if_level) = 0 then do
say $first_nest $cmd
select
when $first_nest = 'DO' then interpret 'do' $cmd
when $first_nest = 'SELECT' then interpret 'select' $cmd
otherwise nop
end
$first_nest = ''
$cmd = ''
end
else $cmd = $cmd $c $tt ';'
end
when $c=';' then do
call time 'R'
interpret $tt
$t=time('E')
end
when $c='CD' then do
$rc=$chdir($tt)
if $rc\=0 then say $tt':' ioerr($rc)
end
when $c='ALIAS' then do
if $t2="" then say $alias.$t1
else do
$alias.$t1=$t2
if left($2,1)==';' then $noargs.$t1=1
end
end
otherwise
call time 'R'
$command
$t=time('E')
end
if $do_level = 0 & $select_level = 0 & $if_level = 0 then $nested = 0
else $nested = 1
end
halt:
signal on halt
say "Interrupted!"
$n=$i
if sigl>$srcline then $n=$n+1
signal restart
syntax:
signal on syntax
say right(sigl,5) "+++" errortext($rc)
$n=$i
if sigl>$srcline then $n=$n+1
signal restart