######################################################################## # # # This software is part of the ast package # # Copyright (c) 1982-2010 AT&T Intellectual Property # # and is licensed under the # # Common Public License, Version 1.0 # # by AT&T Intellectual Property # # # # A copy of the License is available at # # http://www.opensource.org/licenses/cpl1.0.txt # # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) # # # # Information and Software Systems Research # # AT&T Research # # Florham Park NJ # # # # David Korn # # # ######################################################################## proc err_exit { print -u2 -r $'\t'"${Command}[$1] ${@:2}" ((errors++)) } alias err_exit='err_exit $LINENO' setvar Command = ${0##*/} integer aware=0 contrary=0 errors=0 ignorant=0 setvar tmp = $(mktemp -dt) || do { err_exit mktemp -dt failed; exit 1; } trap "cd /; rm -rf $tmp" EXIT proc test_glob { typeset lineno expected drop arg got sep op val add del setvar lineno = "$1" shift if [[ $1 == --* ]] { setvar del = ${1#--} shift } if [[ $1 == ++* ]] { setvar add = ${1#++} shift } setvar expected = "$1" shift if (( contrary )) { if [[ $expected == " "* ]] { setvar expected = ${expected#" "} setvar expected = ""$expected "" } if [[ $expected == *" "* ]] { setvar expected = ${expected/" "/" "} } }for arg in @ARGV { setvar got = ""$got$sep<$arg>"" setvar sep = "" "" } if (( ignorant && aware )) { if [[ $del ]] { setvar got = ""<$del> $got"" } if [[ $add ]] { setvar expected = ""<$add> $expected"" } } if [[ $got != "$expected" ]] { 'err_exit' $lineno "glob -- expected '$expected', got '$got'" } } alias test_glob='test_glob $LINENO' proc test_case { typeset lineno expected subject pattern got setvar lineno = "$1", expected = "$2", subject = "$3", pattern = "$4" eval " case $subject in $pattern) got='' ;; *) got='' ;; esac " if [[ $got != "$expected" ]] { 'err_exit' $lineno "case $subject in $pattern) -- expected '$expected', got '$got'" } } alias test_case='test_case $LINENO' unset undefined export LC_COLLATE=C cd $tmp || do { err_exit "cd $tmp failed"; exit 1; } touch B b set -- * match $ifsjoin(ARGV) { with 'b B' setvar contrary = '1' with b|B setvar ignorant = '1' } set -- $(/bin/sh -c 'echo [a-c]') match $ifsjoin(ARGV) { with B setvar aware = '1' } rm -rf * touch a b c d abc abd abe bb bcd ca cb dd de Beware mkdir bdir test_glob ' ' a* X* test_glob ' ' 'a'* if shell { set --nullglob } 2>/dev/null { set --nullglob test_glob ' ' a* X* set --nonullglob } if shell { set --failglob } 2>/dev/null { set --failglob mkdir tmp touch tmp/l1 tmp/l2 tmp/l3 test_glob '' tmp/l[12] tmp/*4 tmp/*3 test_glob '' tmp/l[12] tmp/*4 tmp/*3 rm -r tmp set --nofailglob } test_glob '' b*/ test_glob '<*>' '*' test_glob '' 'a*' test_glob '' a'*' test_glob ' <*q*>' c* a'*' *q* test_glob '<**>' "*"* test_glob '<**>' '*'* test_glob '<\.\./*/>' "\.\./*/" test_glob '' 's/\..*//' test_glob '' "/^root:/{s/^[!:]*:[!:]*:\([!:]*\).*"'$'"/\1/" test_glob ' ' [a-c]b* test_glob ++Beware '
' [a-y]*[!c] test_glob ' ' a*[!c] touch a-b aXb test_glob ' ' a[X-]b touch .x .y test_glob --Beware '
' [!a-c]* if mkdir a'*'b 2>/dev/null { touch a'*'b/ooo test_glob '' a'*'b/* test_glob '' a'*'?/* test_case '' '!7' '*\!*' test_case '' 'r.*' '*.\*' test_glob '' a[b]c test_glob '' a["b"]c test_glob '' a['b']c test_glob '' a?c test_case '' 'abc' 'a"b"c' test_case '' 'abc' 'a*c' test_case '' 'abc' '"a?c"' test_case '' 'abc' 'a\*c' test_case '' 'abc' 'a\[b]c' test_case '' '"$undefined"' '""' test_case '' 'abc' 'a["\b"]c' rm -rf mkdir a'*'b } mkdir man mkdir man/man1 touch man/man1/sh.1 test_glob '' */man*/sh.* test_glob '' $(echo */man*/sh.*) test_glob '' $(echo */man*/sh.*) test_case '' 'abc' 'a***c' test_case '' 'abc' 'a*****?c' test_case '' 'abc' '?*****??' test_case '' 'abc' '*****??' test_case '' 'abc' '*****??c' test_case '' 'abc' '?*****?c' test_case '' 'abc' '?***?****c' test_case '' 'abc' '?***?****?' test_case '' 'abc' '?***?****' test_case '' 'abc' '*******c' test_case '' 'abc' '*******?' test_case '' 'abcdecdhjk' 'a*cd**?**??k' test_case '' 'abcdecdhjk' 'a**?**cd**?**??k' test_case '' 'abcdecdhjk' 'a**?**cd**?**??k***' test_case '' 'abcdecdhjk' 'a**?**cd**?**??***k' test_case '' 'abcdecdhjk' 'a**?**cd**?**??***k**' test_case '' 'abcdecdhjk' 'a****c**?**??*****' test_case '' "'-'" '[-abc]' test_case '' "'-'" '[abc-]' test_case '' "'\\'" '\\' test_case '' "'\\'" '[\\]' test_case '' "'\\'" "'\\'" test_case '' "'['" '[[]' test_case '' '[' '[[]' test_case '' "'['" '[' test_case '' '[' '[' test_case '' "'[abc'" "'['*" test_case '' "'[abc'" '[*' test_case '' '[abc' "'['*" test_case '' '[abc' '[*' test_case '' 'abd' "a[b/c]d" test_case '' 'a/d' "a[b/c]d" test_case '' 'acd' "a[b/c]d" test_case '' "']'" '[]]' test_case '' "'-'" '[]-]' test_case '' 'p' '[a-\z]' test_case '' '"/tmp"' '[/\\]*' test_case '' 'abc' '??**********?****?' test_case '' 'abc' '??**********?****c' test_case '' 'abc' '?************c****?****' test_case '' 'abc' '*c*?**' test_case '' 'abc' 'a*****c*?**' test_case '' 'abc' 'a********???*******' test_case '' "'a'" '[]' test_case '' 'a' '[]' test_case '' "'['" '[abc' test_case '' '[' '[abc' test_glob ++Beware ' ' b* test_glob ' ' [bB]* if shell { set --nocaseglob } 2>/dev/null { set --nocaseglob test_glob ' ' b* test_glob ' ' [b]* test_glob ' ' [bB]* set --nonocaseglob } if shell { set -f } 2>/dev/null { set -f test_glob '<*>' * set +f } if shell { set --noglob } 2>/dev/null { set --noglob test_glob '<*>' * set --glob } setvar FIGNORE = ''.*|*'' test_glob '<*>' * setvar FIGNORE = ''.*|*c|*e|?'' test_glob '
' * setvar FIGNORE = ''.*|*b|*d|?'' test_glob ' ' * setvar FIGNORE = '' test_glob '' */man*/sh.* unset FIGNORE test_glob '
' ?? test_glob '' */man*/sh.* setvar GLOBIGNORE = ''.*:*'' set -- * if [[ $1 == '*' ]] { setvar GLOBIGNORE = ''.*:*c:*e:?'' test_glob '<>' * setvar GLOBIGNORE = ''.*:*b:*d:?'' test_glob '<>' * unset GLOBIGNORE test_glob '<>' * test_glob '' */man*/sh.* setvar GLOBIGNORE = '' test_glob '' */man*/sh.* } unset GLOBIGNORE proc test_sub { setvar x = "'${subject'$2'}"' eval g=$x if [[ "$g" != "$3" ]] { 'err_exit' $1 subject="'$subject' $x failed, expected '$3', got '$g'" } } alias test_sub='test_sub $LINENO' set --noglob --nobraceexpand setvar subject = ''A regular expressions test'' test_sub '/e/#' 'A r#gular expressions test' test_sub '//e/#' 'A r#gular #xpr#ssions t#st' test_sub '/[^e]/#' '# regular expressions test' test_sub '//[^e]/#' '###e######e###e########e##' test_sub '/+(e)/#' 'A r#gular expressions test' test_sub '//+(e)/#' 'A r#gular #xpr#ssions t#st' test_sub '/@-(e)/#' 'A r#gular expressions test' test_sub '//@-(e)/#' 'A r#gular #xpr#ssions t#st' test_sub '/?(e)/#' '#A regular expressions test' test_sub '//?(e)/#' '#A# #r#g#u#l#a#r# #x#p#r#s#s#i#o#n#s# #t#s#t#' test_sub '/*(e)/#' '#A regular expressions test' test_sub '//*(e)/#' '#A# #r#g#u#l#a#r# #x#p#r#s#s#i#o#n#s# #t#s#t#' test_sub '//@(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st' test_sub '//@-(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st' test_sub '//+(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st' test_sub '//+-(e)/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st' test_sub '//@(+(e))/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st' test_sub '//@(+-(e))/[\1]' 'A r[e]gular [e]xpr[e]ssions t[e]st' test_sub '//-(e)/#' 'A regular expressions test' test_sub '//--(e)/#' 'A regular expressions test' test_sub '//?(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '//{0,1}(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '//*(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '//{0,}(e)/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '//@(?(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '//@({0,1}(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '//@(*(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '//@({0,}(e))/[\1]' '[]A[] []r[e]g[]u[]l[]a[]r[] [e]x[]p[]r[e]s[]s[]i[]o[]n[]s[] []t[e]s[]t[]' test_sub '/?-(e)/#' '#A regular expressions test' test_sub '/@(?-(e))/[\1]' '[]A regular expressions test' test_sub '/!(e)/#' '#' test_sub '//!(e)/#' '#' test_sub '/@(!(e))/[\1]' '[A regular expressions test]' test_sub '//@(!(e))/[\1]' '[A regular expressions test]' setvar subject = ''e'' test_sub '/!(e)/#' '#e' test_sub '//!(e)/#' '#e#' test_sub '/!(e)/[\1]' '[]e' test_sub '//!(e)/[\1]' '[]e[]' test_sub '/@(!(e))/[\1]' '[]e' test_sub '//@(!(e))/[\1]' '[]e[]' setvar subject = ''a'' test_sub '/@(!(a))/[\1]' '[]a' test_sub '//@(!(a))/[\1]' '[]a[]' setvar subject = ''aha'' test_sub '/@(!(a))/[\1]' '[aha]' test_sub '//@(!(a))/[\1]' '[aha]' test_sub '/@(!(aha))/[\1]' '[ah]a' test_sub '//@(!(aha))/[\1]' '[ah][a]' exit $errors