home *** CD-ROM | disk | FTP | other *** search
- # trip.rc -- take a tour of rc
- # Invoke as "path-to-new-rc < trip.rc"
-
- rc=$0
- echo tripping $rc
-
- fn fail {
- echo >[1=2] trip took a wrong turn: $*
- rm -f $tmp
- exit 1
- }
- fn expect {
- echo >[1=2] -n expect $^*^': '
- }
- fn submatch {
- if (!~ $#* 3)
- fail incorrect invocation of submatch
- prompt=$nl if (!~ `` $nl {$rc -ic $1>[2=1]} $2)
- fail $3
- }
- fn sigexit sigint sigquit sigsegv
- fn sigexit {
- echo trip complete
- }
- tmp=/tmp/trip.$pid
- rm -f $tmp
- nl='
- '
-
- #
- # rc -c
- #
-
- if ($rc -c >[2]/dev/null) fail 'rc -c didn''t report a bad exit status'
- x=`{$rc -c 'echo $0 $2 $#*' a b c d e f}
- if (false) { # WARNING: this differs from sh
- if (!~ $x(1) a) fail rc -c reports '$0' incorrectly as $x(1)
- if (!~ $x(2) c) fail rc -c reports '$2' incorrectly as $x(2)
- if (!~ $x(3) 5) fail rc -c reports '$#' incorrectly as $x(3)
- } else {
- if (!~ $x(1) $rc) fail rc -c reports '$0' incorrectly as $x(1)
- if (!~ $x(2) b) fail rc -c reports '$2' incorrectly as $x(2)
- if (!~ $x(3) 6) fail rc -c reports '$#' incorrectly as $x(3)
- }
-
- #
- # umask
- #
-
- umask 0
- > $tmp
- x=`{ls -l $tmp}
- if (!~ $x(1) '-rw-rw-rw-') fail umask 0 produced incorrect result: $x(1)
- rm -f $tmp
- umask 027
- > $tmp
- y=`{ls -l $tmp}
- if (!~ $y(1) '-rw-r-----') fail umask 027 produced incorrect file: $y(1)
- rm -f $tmp
- if (!~ `umask 027) fail umask reported bad value: `umask
-
- submatch 'umask bad' 'bad umask' 'bad umask'
- submatch 'umask -027' 'bad umask' 'bad umask'
- submatch 'umask 999999' 'bad umask' 'bad umask'
- submatch 'umask hi there' 'too many arguments to umask' 'umask arg count'
-
- if (!~ `umask 027) fail bad umask changed umask value to `umask
-
- #
- # redirections
- #
-
- fn bytes { for (i) x=`{wc -c $i} echo $x(1) }
- echo foo > foo > bar
- if (!~ `{bytes foo} 0) fail double redirection created non-empty empty file
- if (!~ `{bytes bar} 4) fail double redirection created wrong sized file: `{bytes bar}
- rm -f foo bar
- echo -n >1 >[2]2 >[1=2] foo
- x = `` '' {cat 1}
- if (!~ $#x 0) fail dup created non-empty empty file: `` '' {cat 1}
- if (!~ `` '' {cat 2} foo) fail dup put wrong contents in file : `` '' {cat 2}
- rm -f 1 2
-
- expect error from cat, closing stdin
- cat >[0=]
-
- submatch 'cat>(1 2 3)' 'multi-word filename in redirection' 'redirection error'
- submatch 'cat>()' 'null filename in redirection' 'redirection error'
-
- #
- # blow the input stack
- #
-
- if (!~ hi `{
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval eval eval \
- eval eval eval eval eval eval eval eval eval eval eval echo hi
- })
- fail huge eval
-
- #
- # heredocs and herestrings
- #
-
- bigfile=/tmp/big.$pid
- od $rc | sed 5000q > $bigfile
- abc=(this is a)
- x=()
- result='this is a heredoc
- this is an heredoc
- '
- if (!~ `` '' {<<[5] EOF cat <[0=5]} $result) fail unquoted heredoc
- $abc heredoc$x
- $abc^n $x^here$x^doc
- EOF
- {if (!~ `` $nl cat ' ') fail quoted heredoc} << ' '
-
-
-
- <<<[9] ``''{cat $bigfile} \
- {
- if(!~ ``''{cat <[0=9]}``'' cat)fail large herestrings
- } < \
- $bigfile
-
- rm -f $bigfile
-
- if (!~ `{cat<<eof
- $$
- eof
- } '$')
- fail quoting '$' in heredoc
-
- submatch 'cat<<eof' 'heredoc incomplete' 'incomplete heredoc'
- submatch 'cat<<eof
- ' 'heredoc incomplete' 'incomplete heredoc'
-
- submatch 'cat<<(eof eof)' 'eof-marker not a single literal word' 'bad heredoc marker'
-
- #
- # lexical analysis
- #
-
- expect warning