1*4887Schin########################################################################
2*4887Schin#                                                                      #
3*4887Schin#               This software is part of the ast package               #
4*4887Schin#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5*4887Schin#                      and is licensed under the                       #
6*4887Schin#                  Common Public License, Version 1.0                  #
7*4887Schin#                      by AT&T Knowledge Ventures                      #
8*4887Schin#                                                                      #
9*4887Schin#                A copy of the License is available at                 #
10*4887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11*4887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12*4887Schin#                                                                      #
13*4887Schin#              Information and Software Systems Research               #
14*4887Schin#                            AT&T Research                             #
15*4887Schin#                           Florham Park NJ                            #
16*4887Schin#                                                                      #
17*4887Schin#                  David Korn <dgk@research.att.com>                   #
18*4887Schin#                                                                      #
19*4887Schin########################################################################
20*4887Schinfunction err_exit
21*4887Schin{
22*4887Schin	print -u2 -n "\t"
23*4887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
24*4887Schin	let Errors+=1
25*4887Schin}
26*4887Schinalias err_exit='err_exit $LINENO'
27*4887Schin
28*4887SchinCommand=${0##*/}
29*4887Schininteger Errors=0
30*4887Schinif	[[ $( ${SHELL-ksh} -s hello<<-\!
31*4887Schin		print $1
32*4887Schin		!
33*4887Schin	    ) != hello ]]
34*4887Schinthen	err_exit "${SHELL-ksh} -s not working"
35*4887Schinfi
36*4887Schinx=$(
37*4887Schin	set -e
38*4887Schin	false && print bad
39*4887Schin	print good
40*4887Schin)
41*4887Schinif	[[ $x != good ]]
42*4887Schinthen	err_exit 'sh -e not workuing'
43*4887Schinfi
44*4887Schin[[ $($SHELL -D -c 'print hi; print $"hello"') == '"hello"' ]] || err_exit 'ksh -D not working'
45*4887Schin
46*4887Schintmp=/tmp/ksh$$
47*4887Schinmkdir $tmp
48*4887Schinrc=$tmp/.kshrc
49*4887Schinprint $'function env_hit\n{\n\tprint OK\n}' > $rc
50*4887Schin
51*4887Schinexport ENV=$rc
52*4887Schinif	[[ -o privileged ]]
53*4887Schinthen
54*4887Schin	[[ $(print env_hit | $SHELL 2>&1) == "OK" ]] &&
55*4887Schin		err_exit 'privileged nointeractive shell reads $ENV file'
56*4887Schin	[[ $(print env_hit | $SHELL -E 2>&1) == "OK" ]] &&
57*4887Schin		err_exit 'privileged -E reads $ENV file'
58*4887Schin	[[ $(print env_hit | $SHELL +E 2>&1) == "OK" ]] &&
59*4887Schin		err_exit 'privileged +E reads $ENV file'
60*4887Schin	[[ $(print env_hit | $SHELL --rc 2>&1) == "OK" ]] &&
61*4887Schin		err_exit 'privileged --rc reads $ENV file'
62*4887Schin	[[ $(print env_hit | $SHELL --norc 2>&1) == "OK" ]] &&
63*4887Schin		err_exit 'privileged --norc reads $ENV file'
64*4887Schinelse
65*4887Schin	[[ $(print env_hit | $SHELL 2>&1) == "OK" ]] &&
66*4887Schin		err_exit 'nointeractive shell reads $ENV file'
67*4887Schin	[[ $(print env_hit | $SHELL -E 2>&1) == "OK" ]] ||
68*4887Schin		err_exit '-E ignores $ENV file'
69*4887Schin	[[ $(print env_hit | $SHELL +E 2>&1) == "OK" ]] &&
70*4887Schin		err_exit '+E reads $ENV file'
71*4887Schin	[[ $(print env_hit | $SHELL --rc 2>&1) == "OK" ]] ||
72*4887Schin		err_exit '--rc ignores $ENV file'
73*4887Schin	[[ $(print env_hit | $SHELL --norc 2>&1) == "OK" ]] &&
74*4887Schin		err_exit '--norc reads $ENV file'
75*4887Schinfi
76*4887Schin
77*4887Schinexport ENV=
78*4887Schinif	[[ -o privileged ]]
79*4887Schinthen
80*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL 2>&1) == "OK" ]] &&
81*4887Schin		err_exit 'privileged nointeractive shell reads $HOME/.kshrc file'
82*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>&1) == "OK" ]] &&
83*4887Schin		err_exit 'privileged -E ignores empty $ENV'
84*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>&1) == "OK" ]] &&
85*4887Schin		err_exit 'privileged +E reads $HOME/.kshrc file'
86*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>&1) == "OK" ]] &&
87*4887Schin		err_exit 'privileged --rc ignores empty $ENV'
88*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>&1) == "OK" ]] &&
89*4887Schin		err_exit 'privileged --norc reads $HOME/.kshrc file'
90*4887Schinelse
91*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL 2>&1) == "OK" ]] &&
92*4887Schin		err_exit 'nointeractive shell reads $HOME/.kshrc file'
93*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>&1) == "OK" ]] &&
94*4887Schin		err_exit '-E ignores empty $ENV'
95*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>&1) == "OK" ]] &&
96*4887Schin		err_exit '+E reads $HOME/.kshrc file'
97*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>&1) == "OK" ]] &&
98*4887Schin		err_exit '--rc ignores empty $ENV'
99*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>&1) == "OK" ]] &&
100*4887Schin		err_exit '--norc reads $HOME/.kshrc file'
101*4887Schinfi
102*4887Schin
103*4887Schinunset ENV
104*4887Schinif	[[ -o privileged ]]
105*4887Schinthen
106*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL 2>&1) == "OK" ]] &&
107*4887Schin		err_exit 'privileged nointeractive shell reads $HOME/.kshrc file'
108*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>&1) == "OK" ]] &&
109*4887Schin		err_exit 'privileged -E reads $HOME/.kshrc file'
110*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>&1) == "OK" ]] &&
111*4887Schin		err_exit 'privileged +E reads $HOME/.kshrc file'
112*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>&1) == "OK" ]] &&
113*4887Schin		err_exit 'privileged --rc reads $HOME/.kshrc file'
114*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>&1) == "OK" ]] &&
115*4887Schin		err_exit 'privileged --norc reads $HOME/.kshrc file'
116*4887Schinelse
117*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL 2>&1) == "OK" ]] &&
118*4887Schin		err_exit 'nointeractive shell reads $HOME/.kshrc file'
119*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>&1) == "OK" ]] ||
120*4887Schin		err_exit '-E ignores $HOME/.kshrc file'
121*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>&1) == "OK" ]] &&
122*4887Schin		err_exit '+E reads $HOME/.kshrc file'
123*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>&1) == "OK" ]] ||
124*4887Schin		err_exit '--rc ignores $HOME/.kshrc file'
125*4887Schin	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>&1) == "OK" ]] &&
126*4887Schin		err_exit '--norc reads $HOME/.kshrc file'
127*4887Schinfi
128*4887Schin
129*4887Schinrm -rf $tmp
130*4887Schin
131*4887Schinif	command set -G 2> /dev/null
132*4887Schinthen	mkdir /tmp/ksh$$
133*4887Schin	cd /tmp/ksh$$
134*4887Schin	mkdir bar foo
135*4887Schin	> bar.c  > bam.c
136*4887Schin	> bar/foo.c > bar/bam.c
137*4887Schin	> foo/bam.c
138*4887Schin	set -- **.c
139*4887Schin	expected='bam.c bar.c'
140*4887Schin	[[ $* == $expected ]] ||
141*4887Schin		err_exit "-G **.c failed -- expected '$expected', got '$*'"
142*4887Schin	set -- **
143*4887Schin	expected='bam.c bar bar.c bar/bam.c bar/foo.c foo foo/bam.c'
144*4887Schin	[[ $* == $expected ]] ||
145*4887Schin		err_exit "-G ** failed -- expected '$expected', got '$*'"
146*4887Schin	set -- **/*.c
147*4887Schin	expected='bam.c bar.c bar/bam.c bar/foo.c foo/bam.c'
148*4887Schin	[[ $* == $expected ]] ||
149*4887Schin		err_exit "-G **/*.c failed -- expected '$expected', got '$*'"
150*4887Schin	set -- **/bam.c
151*4887Schin	expected='bam.c bar/bam.c foo/bam.c'
152*4887Schin	[[ $* == $expected ]] ||
153*4887Schin		err_exit "-G **/bam.c failed -- expected '$expected', got '$*'"
154*4887Schin	cd ~-
155*4887Schin	rm -rf /tmp/ksh$$
156*4887Schinfi
157*4887Schin
158*4887Schinmkdir /tmp/ksh$$
159*4887Schincd /tmp/ksh$$
160*4887Schint="<$$>.profile.<$$>"
161*4887Schinecho "echo '$t'" > .profile
162*4887Schincp $SHELL ./-ksh
163*4887Schinif	[[ -o privileged ]]
164*4887Schinthen
165*4887Schin	[[ $(HOME=$PWD $SHELL -l </dev/null 2>&1) == *$t* ]] &&
166*4887Schin		err_exit 'privileged -l reads .profile'
167*4887Schin	[[ $(HOME=$PWD $SHELL --login </dev/null 2>&1) == *$t* ]] &&
168*4887Schin		err_exit 'privileged --login reads .profile'
169*4887Schin	[[ $(HOME=$PWD $SHELL --login-shell </dev/null 2>&1) == *$t* ]] &&
170*4887Schin		err_exit 'privileged --login-shell reads .profile'
171*4887Schin	[[ $(HOME=$PWD $SHELL --login_shell </dev/null 2>&1) == *$t* ]] &&
172*4887Schin		err_exit 'privileged --login_shell reads .profile'
173*4887Schin	[[ $(HOME=$PWD exec -a -ksh $SHELL </dev/null 2>&1) == *$t* ]]  &&
174*4887Schin		err_exit 'privileged exec -a -ksh ksh reads .profile'
175*4887Schin	[[ $(HOME=$PWD ./-ksh -i </dev/null 2>&1) == *$t* ]] &&
176*4887Schin		err_exit 'privileged ./-ksh reads .profile'
177*4887Schin	[[ $(HOME=$PWD ./-ksh -ip </dev/null 2>&1) == *$t* ]] &&
178*4887Schin		err_exit 'privileged ./-ksh -p reads .profile'
179*4887Schinelse
180*4887Schin	[[ $(HOME=$PWD $SHELL -l </dev/null 2>&1) == *$t* ]] ||
181*4887Schin		err_exit '-l ignores .profile'
182*4887Schin	[[ $(HOME=$PWD $SHELL --login </dev/null 2>&1) == *$t* ]] ||
183*4887Schin		err_exit '--login ignores .profile'
184*4887Schin	[[ $(HOME=$PWD $SHELL --login-shell </dev/null 2>&1) == *$t* ]] ||
185*4887Schin		err_exit '--login-shell ignores .profile'
186*4887Schin	[[ $(HOME=$PWD $SHELL --login_shell </dev/null 2>&1) == *$t* ]] ||
187*4887Schin		err_exit '--login_shell ignores .profile'
188*4887Schin	[[ $(HOME=$PWD exec -a -ksh $SHELL </dev/null 2>&1) == *$t* ]]  ||
189*4887Schin		err_exit 'exec -a -ksh ksh ignores .profile'
190*4887Schin	[[ $(HOME=$PWD ./-ksh -i </dev/null 2>&1) == *$t* ]] ||
191*4887Schin		err_exit './-ksh ignores .profile'
192*4887Schin	[[ $(HOME=$PWD ./-ksh -ip </dev/null 2>&1) == *$t* ]] &&
193*4887Schin		err_exit './-ksh -p does not ignore .profile'
194*4887Schinfi
195*4887Schincd ~-
196*4887Schinrm -rf /tmp/ksh$$
197*4887Schin
198*4887Schin
199*4887Schin# { exec interactive login_shell restricted xtrace } in the following test
200*4887Schin
201*4887Schinfor opt in \
202*4887Schin	allexport all-export all_export \
203*4887Schin	bgnice bg-nice bg_nice \
204*4887Schin	clobber emacs \
205*4887Schin	errexit err-exit err_exit \
206*4887Schin	glob \
207*4887Schin	globstar glob-star glob_star \
208*4887Schin	gmacs \
209*4887Schin	ignoreeof ignore-eof ignore_eof \
210*4887Schin	keyword log markdirs monitor notify \
211*4887Schin	pipefail pipe-fail pipe_fail \
212*4887Schin	trackall track-all track_all \
213*4887Schin	unset verbose vi \
214*4887Schin	viraw vi-raw vi_raw
215*4887Schindo	old=$opt
216*4887Schin	if [[ ! -o $opt ]]
217*4887Schin	then	old=no$opt
218*4887Schin	fi
219*4887Schin
220*4887Schin	set --$opt || err_exit "set --$opt failed"
221*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
222*4887Schin	[[ -o no$opt ]] && err_exit "[[ -o no$opt ]] failed"
223*4887Schin	[[ -o no-$opt ]] && err_exit "[[ -o no-$opt ]] failed"
224*4887Schin	[[ -o no_$opt ]] && err_exit "[[ -o no_$opt ]] failed"
225*4887Schin	[[ -o ?$opt ]] || err_exit "[[ -o ?$opt ]] failed"
226*4887Schin	[[ -o ?no$opt ]] || err_exit "[[ -o ?no$opt ]] failed"
227*4887Schin	[[ -o ?no-$opt ]] || err_exit "[[ -o ?no-$opt ]] failed"
228*4887Schin	[[ -o ?no_$opt ]] || err_exit "[[ -o ?no_$opt ]] failed"
229*4887Schin
230*4887Schin	set --no$opt || err_exit "set --no$opt failed"
231*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
232*4887Schin	[[ -o $opt ]] && err_exit "[[ -o $opt ]] failed"
233*4887Schin
234*4887Schin	set --no-$opt || err_exit "set --no-$opt failed"
235*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
236*4887Schin	[[ -o $opt ]] && err_exit "[[ -o $opt ]] failed"
237*4887Schin
238*4887Schin	set --no_$opt || err_exit "set --no_$opt failed"
239*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
240*4887Schin	[[ -o $opt ]] && err_exit "[[ -o $opt ]] failed"
241*4887Schin
242*4887Schin	set -o $opt || err_exit "set -o $opt failed"
243*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
244*4887Schin	set -o $opt=1 || err_exit "set -o $opt=1 failed"
245*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
246*4887Schin	set -o no$opt=0 || err_exit "set -o no$opt=0 failed"
247*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
248*4887Schin	set --$opt=1 || err_exit "set --$opt=1 failed"
249*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
250*4887Schin	set --no$opt=0 || err_exit "set --no$opt=0 failed"
251*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
252*4887Schin
253*4887Schin	set -o no$opt || err_exit "set -o no$opt failed"
254*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
255*4887Schin	set -o $opt=0 || err_exit "set -o $opt=0 failed"
256*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
257*4887Schin	set -o no$opt=1 || err_exit "set -o no$opt=1 failed"
258*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
259*4887Schin	set --$opt=0 || err_exit "set --$opt=0 failed"
260*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
261*4887Schin	set --no$opt=1 || err_exit "set --no$opt=1 failed"
262*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
263*4887Schin
264*4887Schin	set -o no-$opt || err_exit "set -o no-$opt failed"
265*4887Schin	[[ -o no-$opt ]] || err_exit "[[ -o no-$opt ]] failed"
266*4887Schin
267*4887Schin	set -o no_$opt || err_exit "set -o no_$opt failed"
268*4887Schin	[[ -o no_$opt ]] || err_exit "[[ -o no_$opt ]] failed"
269*4887Schin
270*4887Schin	set +o $opt || err_exit "set +o $opt failed"
271*4887Schin	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
272*4887Schin
273*4887Schin	set +o no$opt || err_exit "set +o no$opt failed"
274*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
275*4887Schin
276*4887Schin	set +o no-$opt || err_exit "set +o no-$opt failed"
277*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
278*4887Schin
279*4887Schin	set +o no_$opt || err_exit "set +o no_$opt failed"
280*4887Schin	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
281*4887Schin
282*4887Schin	set --$old
283*4887Schindone
284*4887Schin
285*4887Schinfor opt in \
286*4887Schin	exec interactive login_shell login-shell logi privileged \
287*4887Schin	rc restricted xtrace
288*4887Schindo	[[ -o $opt ]]
289*4887Schin	y=$?
290*4887Schin	[[ -o no$opt ]]
291*4887Schin	n=$?
292*4887Schin	case $y$n in
293*4887Schin	10|01)	;;
294*4887Schin	*)	err_exit "[[ -o $opt ]] == [[ -o no$opt ]]" ;;
295*4887Schin	esac
296*4887Schindone
297*4887Schin
298*4887Schinfor opt in \
299*4887Schin	foo foo-bar foo_bar
300*4887Schindo	if	[[ -o ?$opt ]]
301*4887Schin	then	err_exit "[[ -o ?$opt ]] should fail"
302*4887Schin	fi
303*4887Schin	if	[[ -o ?no$opt ]]
304*4887Schin	then	err_exit "[[ -o ?no$opt ]] should fail"
305*4887Schin	fi
306*4887Schindone
307*4887Schinfalse | true | true   || err_exit 'pipe not exiting exit value of last element'
308*4887Schintrue | true | false   && err_exit 'pipe not exiting false'
309*4887Schinset -o pipefail
310*4887Schinfalse | true | true    && err_exit 'pipe with first not failing with pipefail'
311*4887Schintrue | false | true    && err_exit 'pipe middle not failing with pipefail'
312*4887Schintrue | true | false    && err_exit 'pipe last not failing with pipefail'
313*4887Schinexit $((Errors))
314