14887Schin########################################################################
24887Schin#                                                                      #
34887Schin#               This software is part of the ast package               #
4*8462SApril.Chin@Sun.COM#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
54887Schin#                      and is licensed under the                       #
64887Schin#                  Common Public License, Version 1.0                  #
7*8462SApril.Chin@Sun.COM#                    by AT&T Intellectual Property                     #
84887Schin#                                                                      #
94887Schin#                A copy of the License is available at                 #
104887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
114887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
124887Schin#                                                                      #
134887Schin#              Information and Software Systems Research               #
144887Schin#                            AT&T Research                             #
154887Schin#                           Florham Park NJ                            #
164887Schin#                                                                      #
174887Schin#                  David Korn <dgk@research.att.com>                   #
184887Schin#                                                                      #
194887Schin########################################################################
204887Schinfunction err_exit
214887Schin{
224887Schin	print -u2 -n "\t"
234887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
244887Schin	let Errors+=1
254887Schin}
264887Schinalias err_exit='err_exit $LINENO'
274887SchinCommand=${0##*/}
284887Schininteger Errors=0
29*8462SApril.Chin@Sun.COM
304887Schinmkdir /tmp/ksh$$
314887Schincd /tmp/ksh$$
324887Schintrap "PATH=$PATH; cd /; rm -rf /tmp/ksh$$" EXIT
33*8462SApril.Chin@Sun.COMtype /xxxxxx > out1 2> out2
34*8462SApril.Chin@Sun.COM[[ -s out1 ]] && err_exit 'type should not write on stdout for not found case'
35*8462SApril.Chin@Sun.COM[[ -s out2 ]] || err_exit 'type should write on stderr for not found case'
36*8462SApril.Chin@Sun.COMmkdir dir1 dir2
37*8462SApril.Chin@Sun.COMcat  > dir1/foobar << '+++'
38*8462SApril.Chin@Sun.COMfoobar() { print foobar1;}
39*8462SApril.Chin@Sun.COMfunction dir1 { print dir1;}
40*8462SApril.Chin@Sun.COM+++
41*8462SApril.Chin@Sun.COMcat  > dir2/foobar << '+++'
42*8462SApril.Chin@Sun.COMfoobar() { print foobar2;}
43*8462SApril.Chin@Sun.COMfunction dir2 { print dir2;}
44*8462SApril.Chin@Sun.COM+++
45*8462SApril.Chin@Sun.COMchmod +x dir[12]/foobar
46*8462SApril.Chin@Sun.COMp=$PATH
47*8462SApril.Chin@Sun.COMFPATH=$PWD/dir1
48*8462SApril.Chin@Sun.COMPATH=$FPATH:$p
49*8462SApril.Chin@Sun.COM[[ $( foobar) == foobar1 ]] || err_exit 'foobar should output foobar1'
50*8462SApril.Chin@Sun.COMFPATH=$PWD/dir2
51*8462SApril.Chin@Sun.COMPATH=$FPATH:$p
52*8462SApril.Chin@Sun.COM[[ $(foobar) == foobar2 ]] || err_exit 'foobar should output foobar2'
53*8462SApril.Chin@Sun.COMFPATH=$PWD/dir1
54*8462SApril.Chin@Sun.COMPATH=$FPATH:$p
55*8462SApril.Chin@Sun.COM[[ $(foobar) == foobar1 ]] || err_exit 'foobar should output foobar1 again'
56*8462SApril.Chin@Sun.COMFPATH=$PWD/dir2
57*8462SApril.Chin@Sun.COMPATH=$FPATH:$p
58*8462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${}'
59*8462SApril.Chin@Sun.COM[[ ${ dir2;} == dir2 ]] || err_exit 'should be dir2'
60*8462SApril.Chin@Sun.COM[[ ${ dir1;} == dir1 ]] 2> /dev/null &&  err_exit 'should not be be dir1'
61*8462SApril.Chin@Sun.COMFPATH=$PWD/dir1
62*8462SApril.Chin@Sun.COMPATH=$FPATH:$p
63*8462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar1 ]] || err_exit 'foobar should output foobar1 with ${}'
64*8462SApril.Chin@Sun.COM[[ ${ dir1;} == dir1 ]] || err_exit 'should be dir1'
65*8462SApril.Chin@Sun.COM[[ ${ dir2;} == dir2 ]] 2> /dev/null &&  err_exit 'should not be be dir2'
66*8462SApril.Chin@Sun.COMFPATH=$PWD/dir2
67*8462SApril.Chin@Sun.COMPATH=$FPATH:$p
68*8462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${} again'
69*8462SApril.Chin@Sun.COMPATH=$p
704887Schin(PATH="/bin")
714887Schin[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell'
724887Schincat > bug1 <<- \EOF
734887Schin	print print ok > /tmp/ok$$
744887Schin	/bin/chmod 755 /tmp/ok$$
754887Schin	trap 'cd /; rm -f /tmp/ok$$' EXIT
764887Schin	function a
774887Schin	{
784887Schin	        typeset -x PATH=/tmp
794887Schin	        ok$$
804887Schin	}
814887Schin	path=$PATH
824887Schin	unset PATH
834887Schin	a
844887Schin	PATH=$path
854887Schin}
864887SchinEOF
874887Schin[[ $($SHELL ./bug1  2> /dev/null) == ok ]]  || err_exit "PATH in function not working"
884887Schincat > bug1 <<- \EOF
894887Schin	function lock_unlock
904887Schin	{
914887Schin	typeset PATH=/usr/bin
924887Schin	typeset -x PATH=''
934887Schin	}
944887Schin
954887Schin	PATH=/usr/bin
964887Schin	: $(PATH=/usr/bin getconf PATH)
974887Schin	typeset -ft lock_unlock
984887Schin	lock_unlock
994887SchinEOF
1004887Schin($SHELL ./bug1)  2> /dev/null || err_exit "path_delete bug"
1014887Schinmkdir tdir$$
1024887Schinif	$SHELL tdir$$ > /dev/null 2>&1
1034887Schinthen	err_exit 'not an error to run ksh on a directory'
1044887Schinfi
1054887Schin
1064887Schinprint 'print hi' > ls
1074887Schinif	[[ $($SHELL ls 2> /dev/null) != hi ]]
1084887Schinthen	err_exit "$SHELL name not executing version in current directory"
1094887Schinfi
1104887Schinif	[[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]]
1114887Schinthen	err_exit 'PATH export in command substitution not working'
1124887Schinfi
1134887Schinpwd=$PWD
1144887Schin# get rid of leading and trailing : and trailing :.
1154887SchinPATH=${PATH%.}
1164887SchinPATH=${PATH%:}
1174887SchinPATH=${PATH#.}
1184887SchinPATH=${PATH#:}
1194887Schinpath=$PATH
1204887Schinvar=$(whence date)
1214887Schindir=$(basename "$var")
1224887Schinfor i in 1 2 3 4 5 6 7 8 9 0
1234887Schindo	if	! whence notfound$i 2> /dev/null
1244887Schin	then	cmd=notfound$i
1254887Schin		break
1264887Schin	fi
1274887Schindone
1284887Schinprint 'print hello' > date
1294887Schinchmod +x date
1304887Schinprint 'print notfound' >  $cmd
1314887Schinchmod +x "$cmd"
1324887Schin> foo
1334887Schinchmod 755 foo
1344887Schinfor PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD
1354887Schindo
1364887Schin#	print path=$PATH $(whence date)
1374887Schin#	print path=$PATH $(whence "$cmd")
1384887Schin		date
1394887Schin		"$cmd"
1404887Schindone > /dev/null 2>&1
1414887Schinbuiltin -d date 2> /dev/null
1424887Schinif	[[ $(PATH=:/usr/bin; date) != 'hello' ]]
1434887Schinthen	err_exit "leading : in path not working"
1444887Schinfi
1454887Schin(
1464887Schin	PATH=$PWD:
1474887Schin	builtin chmod
1484887Schin	print 'print cannot execute' > noexec
1494887Schin	chmod 644 noexec
1504887Schin	if	[[ ! -x noexec ]]
1514887Schin	then	noexec > /dev/null 2>&1
1524887Schin	else	exit 126
1534887Schin	fi
1544887Schin)
1554887Schinstatus=$?
1564887Schin[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected"
1574887Schinbuiltin -d rm 2> /dev/null
1584887Schinrm=$(whence rm)
1594887Schind=$(dirname "$rm")
1604887Schinunset FPATH
1614887SchinPATH=/dev/null
1624887Schinif	date > /dev/null 2>&1
1634887Schinthen	err_exit 'programs in . should not be found'
1644887Schinfi
1654887Schin[[ $(whence ./foo) != "$PWD/"./foo ]] && err_exit 'whence ./foo not working'
1664887Schin[[ $(whence "$PWD/foo") != "$PWD/foo" ]] && err_exit 'whence $PWD/foo not working'
1674887Schin[[ $(whence ./xxxxx) ]] && err_exit 'whence ./xxxx not working'
1684887SchinPATH=$d:
1694887Schincp "$rm" kshrm$$
1704887Schinif	[[ $(whence kshrm$$) != $PWD/kshrm$$  ]]
1714887Schinthen	err_exit 'trailing : in pathname not working'
1724887Schinfi
1734887Schincp "$rm" rm
1744887SchinPATH=:$d
1754887Schinif	[[ $(whence rm) != $PWD/rm ]]
1764887Schinthen	err_exit 'leading : in pathname not working'
1774887Schinfi
1784887SchinPATH=$d: whence rm > /dev/null
1794887Schinif	[[ $(whence rm) != $PWD/rm ]]
1804887Schinthen	err_exit 'pathname not restored after scoping'
1814887Schinfi
1824887Schinmkdir bin
1834887Schinprint 'print ok' > bin/tst
1844887Schinchmod +x bin/tst
1854887Schinif	[[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]]
1864887Schinthen	err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo'
1874887Schinfi
1884887Schincd /
1894887Schinif	whence ls > /dev/null
1904887Schinthen	PATH=
1914887Schin	if	[[ $(whence rm) ]]
1924887Schin	then	err_exit 'setting PATH to Null not working'
1934887Schin	fi
1944887Schin	unset PATH
1954887Schin	if	[[ $(whence rm) != /*rm ]]
1964887Schin	then	err_exit 'unsetting path  not working'
1974887Schin	fi
1984887Schinfi
1994887SchinPATH=/dev:/tmp/ksh$$
2004887Schinx=$(whence rm)
2014887Schintypeset foo=$(PATH=/xyz:/abc :)
2024887Schiny=$(whence rm)
2034887Schin[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution'
2044887Schinwhence getconf > /dev/null  &&  err_exit 'getconf should not be found'
2054887Schinbuiltin /bin/getconf
2064887SchinPATH=/bin
2074887SchinPATH=$(getconf PATH)
2084887Schinx=$(whence ls)
2094887SchinPATH=.:$PWD:${x%/ls}
2104887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path'
2114887SchinPATH=$PWD:.:${x%/ls}
2124887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path'
2134887Schincd   "${x%/ls}"
2144887Schin[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname'
2154887Schinstatus=$($SHELL -c $'trap \'print $?\' EXIT;/a/b/c/d/e 2> /dev/null')
2164887Schin[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127"
2174887Schinstatus=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null')
2184887Schin[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126"
2194887Schinstatus=$($SHELL -c $'trap \'print $?\' ERR;/a/b/c/d/e 2> /dev/null')
2204887Schin[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127"
2214887Schinstatus=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null')
2224887Schin[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126"
223*8462SApril.Chin@Sun.COM
224*8462SApril.Chin@Sun.COM# universe via PATH
225*8462SApril.Chin@Sun.COM
226*8462SApril.Chin@Sun.COMbuiltin getconf
227*8462SApril.Chin@Sun.COMgetconf UNIVERSE - att # override sticky default 'UNIVERSE = foo'
228*8462SApril.Chin@Sun.COM
229*8462SApril.Chin@Sun.COM[[ $(PATH=/usr/ucb/bin:/usr/bin echo -n ucb) == 'ucb' ]] || err_exit "ucb universe echo ignores -n option"
230*8462SApril.Chin@Sun.COM[[ $(PATH=/usr/xpg/bin:/usr/bin echo -n att) == '-n att' ]] || err_exit "att universe echo does not ignore -n option"
231*8462SApril.Chin@Sun.COM
232*8462SApril.Chin@Sun.COMPATH=$path
233*8462SApril.Chin@Sun.COM
234*8462SApril.Chin@Sun.COMscr=/tmp/ksh$$/foo
235*8462SApril.Chin@Sun.COMexp=126
236*8462SApril.Chin@Sun.COM
237*8462SApril.Chin@Sun.COM: > $scr
238*8462SApril.Chin@Sun.COMchmod a=x $scr
239*8462SApril.Chin@Sun.COM{ got=$($scr; print $?); } 2>/dev/null
240*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "unreadable empty script should fail -- expected $exp, got $got"
241*8462SApril.Chin@Sun.COM{ got=$(command $scr; print $?); } 2>/dev/null
242*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "command of unreadable empty script should fail -- expected $exp, got $got"
243*8462SApril.Chin@Sun.COM[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable empty script in [[ ... ]] should fail -- expected $exp"
244*8462SApril.Chin@Sun.COM[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable empty script in [[ ... ]] should fail -- expected $exp"
245*8462SApril.Chin@Sun.COMgot=$($SHELL -c "$scr; print \$?" 2>/dev/null)
246*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable empty script should fail -- expected $exp, got" $got
247*8462SApril.Chin@Sun.COMgot=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
248*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable empty script should fail -- expected $exp, got" $got
249*8462SApril.Chin@Sun.COM
250*8462SApril.Chin@Sun.COMrm -f $scr
251*8462SApril.Chin@Sun.COMprint : > $scr
252*8462SApril.Chin@Sun.COMchmod a=x $scr
253*8462SApril.Chin@Sun.COM{ got=$($scr; print $?); } 2>/dev/null
254*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "unreadable non-empty script should fail -- expected $exp, got $got"
255*8462SApril.Chin@Sun.COM{ got=$(command $scr; print $?); } 2>/dev/null
256*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "command of unreadable non-empty script should fail -- expected $exp, got $got"
257*8462SApril.Chin@Sun.COM[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
258*8462SApril.Chin@Sun.COM[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
259*8462SApril.Chin@Sun.COMgot=$($SHELL -c "$scr; print \$?" 2>/dev/null)
260*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable non-empty script should fail -- expected $exp, got" $got
261*8462SApril.Chin@Sun.COMgot=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
262*8462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable non-empty script should fail -- expected $exp, got" $got
263*8462SApril.Chin@Sun.COM
2644887Schinexit $((Errors))
265