xref: /titanic_50/usr/src/lib/libshell/common/tests/options.sh (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31unset HISTFILE
32
33if	[[ $( ${SHELL-ksh} -s hello<<-\!
34		print $1
35		!
36	    ) != hello ]]
37then	err_exit "${SHELL-ksh} -s not working"
38fi
39x=$(
40	set -e
41	false && print bad
42	print good
43)
44if	[[ $x != good ]]
45then	err_exit 'sh -e not workuing'
46fi
47[[ $($SHELL -D -c 'print hi; print $"hello"') == '"hello"' ]] || err_exit 'ksh -D not working'
48
49tmp=/tmp/ksh$$
50mkdir $tmp
51rc=$tmp/.kshrc
52print $'PS1=""\nfunction env_hit\n{\n\tprint OK\n}' > $rc
53
54export ENV='${nosysrc}'$rc
55if	[[ -o privileged ]]
56then
57	[[ $(print env_hit | $SHELL 2>/dev/null) == "OK" ]] &&
58		err_exit 'privileged nointeractive shell reads $ENV file'
59	[[ $(print env_hit | $SHELL -E 2>/dev/null) == "OK" ]] &&
60		err_exit 'privileged -E reads $ENV file'
61	[[ $(print env_hit | $SHELL +E 2>/dev/null) == "OK" ]] &&
62		err_exit 'privileged +E reads $ENV file'
63	[[ $(print env_hit | $SHELL --rc 2>/dev/null) == "OK" ]] &&
64		err_exit 'privileged --rc reads $ENV file'
65	[[ $(print env_hit | $SHELL --norc 2>/dev/null) == "OK" ]] &&
66		err_exit 'privileged --norc reads $ENV file'
67else
68	[[ $(print env_hit | $SHELL 2>/dev/null) == "OK" ]] &&
69		err_exit 'nointeractive shell reads $ENV file'
70	[[ $(print env_hit | $SHELL -E 2>/dev/null) == "OK" ]] ||
71		err_exit '-E ignores $ENV file'
72	[[ $(print env_hit | $SHELL +E 2>/dev/null) == "OK" ]] &&
73		err_exit '+E reads $ENV file'
74	[[ $(print env_hit | $SHELL --rc 2>/dev/null) == "OK" ]] ||
75		err_exit '--rc ignores $ENV file'
76	[[ $(print env_hit | $SHELL --norc 2>/dev/null) == "OK" ]] &&
77		err_exit '--norc reads $ENV file'
78	[[ $(print env_hit | $SHELL -i 2>/dev/null) == "OK" ]] ||
79		err_exit '-i ignores $ENV file'
80fi
81
82export ENV=
83if	[[ -o privileged ]]
84then
85	[[ $(print env_hit | HOME=$tmp $SHELL 2>/dev/null) == "OK" ]] &&
86		err_exit 'privileged nointeractive shell reads $HOME/.kshrc file'
87	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>/dev/null) == "OK" ]] &&
88		err_exit 'privileged -E ignores empty $ENV'
89	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>/dev/null) == "OK" ]] &&
90		err_exit 'privileged +E reads $HOME/.kshrc file'
91	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>/dev/null) == "OK" ]] &&
92		err_exit 'privileged --rc ignores empty $ENV'
93	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>/dev/null) == "OK" ]] &&
94		err_exit 'privileged --norc reads $HOME/.kshrc file'
95else
96	[[ $(print env_hit | HOME=$tmp $SHELL 2>/dev/null) == "OK" ]] &&
97		err_exit 'nointeractive shell reads $HOME/.kshrc file'
98	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>/dev/null) == "OK" ]] &&
99		err_exit '-E ignores empty $ENV'
100	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>/dev/null) == "OK" ]] &&
101		err_exit '+E reads $HOME/.kshrc file'
102	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>/dev/null) == "OK" ]] &&
103		err_exit '--rc ignores empty $ENV'
104	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>/dev/null) == "OK" ]] &&
105		err_exit '--norc reads $HOME/.kshrc file'
106fi
107
108unset ENV
109if	[[ -o privileged ]]
110then
111	[[ $(print env_hit | HOME=$tmp $SHELL 2>/dev/null) == "OK" ]] &&
112		err_exit 'privileged nointeractive shell reads $HOME/.kshrc file'
113	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>/dev/null) == "OK" ]] &&
114		err_exit 'privileged -E reads $HOME/.kshrc file'
115	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>/dev/null) == "OK" ]] &&
116		err_exit 'privileged +E reads $HOME/.kshrc file'
117	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>/dev/null) == "OK" ]] &&
118		err_exit 'privileged --rc reads $HOME/.kshrc file'
119	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>/dev/null) == "OK" ]] &&
120		err_exit 'privileged --norc reads $HOME/.kshrc file'
121else
122	[[ $(print env_hit | HOME=$tmp $SHELL 2>/dev/null) == "OK" ]] &&
123		err_exit 'nointeractive shell reads $HOME/.kshrc file'
124	[[ $(print env_hit | HOME=$tmp $SHELL -E 2>/dev/null) == "OK" ]] ||
125		err_exit '-E ignores $HOME/.kshrc file'
126	[[ $(print env_hit | HOME=$tmp $SHELL +E 2>/dev/null) == "OK" ]] &&
127		err_exit '+E reads $HOME/.kshrc file'
128	[[ $(print env_hit | HOME=$tmp $SHELL --rc 2>/dev/null) == "OK" ]] ||
129		err_exit '--rc ignores $HOME/.kshrc file'
130	[[ $(print env_hit | HOME=$tmp $SHELL --norc 2>/dev/null) == "OK" ]] &&
131		err_exit '--norc reads $HOME/.kshrc file'
132fi
133
134rm -rf $tmp
135
136if	command set -G 2> /dev/null
137then	mkdir /tmp/ksh$$
138	cd /tmp/ksh$$
139	mkdir bar foo
140	> bar.c  > bam.c
141	> bar/foo.c > bar/bam.c
142	> foo/bam.c
143	set -- **.c
144	expected='bam.c bar.c'
145	[[ $* == $expected ]] ||
146		err_exit "-G **.c failed -- expected '$expected', got '$*'"
147	set -- **
148	expected='bam.c bar bar.c bar/bam.c bar/foo.c foo foo/bam.c'
149	[[ $* == $expected ]] ||
150		err_exit "-G ** failed -- expected '$expected', got '$*'"
151	set -- **/*.c
152	expected='bam.c bar.c bar/bam.c bar/foo.c foo/bam.c'
153	[[ $* == $expected ]] ||
154		err_exit "-G **/*.c failed -- expected '$expected', got '$*'"
155	set -- **/bam.c
156	expected='bam.c bar/bam.c foo/bam.c'
157	[[ $* == $expected ]] ||
158		err_exit "-G **/bam.c failed -- expected '$expected', got '$*'"
159	cd ~-
160	rm -rf /tmp/ksh$$
161fi
162
163mkdir /tmp/ksh$$
164cd /tmp/ksh$$
165t="<$$>.profile.<$$>"
166echo "echo '$t'" > .profile
167cp $SHELL ./-ksh
168if	[[ -o privileged ]]
169then
170	[[ $(HOME=$PWD $SHELL -l </dev/null 2>/dev/null) == *$t* ]] &&
171		err_exit 'privileged -l reads .profile'
172	[[ $(HOME=$PWD $SHELL --login </dev/null 2>/dev/null) == *$t* ]] &&
173		err_exit 'privileged --login reads .profile'
174	[[ $(HOME=$PWD $SHELL --login-shell </dev/null 2>/dev/null) == *$t* ]] &&
175		err_exit 'privileged --login-shell reads .profile'
176	[[ $(HOME=$PWD $SHELL --login_shell </dev/null 2>/dev/null) == *$t* ]] &&
177		err_exit 'privileged --login_shell reads .profile'
178	[[ $(HOME=$PWD exec -a -ksh $SHELL </dev/null 2>/dev/null) == *$t* ]]  &&
179		err_exit 'privileged exec -a -ksh ksh reads .profile'
180	[[ $(HOME=$PWD ./-ksh -i </dev/null 2>/dev/null) == *$t* ]] &&
181		err_exit 'privileged ./-ksh reads .profile'
182	[[ $(HOME=$PWD ./-ksh -ip </dev/null 2>/dev/null) == *$t* ]] &&
183		err_exit 'privileged ./-ksh -p reads .profile'
184else
185	[[ $(HOME=$PWD $SHELL -l </dev/null 2>/dev/null) == *$t* ]] ||
186		err_exit '-l ignores .profile'
187	[[ $(HOME=$PWD $SHELL --login </dev/null 2>/dev/null) == *$t* ]] ||
188		err_exit '--login ignores .profile'
189	[[ $(HOME=$PWD $SHELL --login-shell </dev/null 2>/dev/null) == *$t* ]] ||
190		err_exit '--login-shell ignores .profile'
191	[[ $(HOME=$PWD $SHELL --login_shell </dev/null 2>/dev/null) == *$t* ]] ||
192		err_exit '--login_shell ignores .profile'
193	[[ $(HOME=$PWD exec -a -ksh $SHELL </dev/null 2>/dev/null) == *$t* ]]  ||
194		err_exit 'exec -a -ksh ksh ignores .profile'
195	[[ $(HOME=$PWD ./-ksh -i </dev/null 2>/dev/null) == *$t* ]] ||
196		err_exit './-ksh ignores .profile'
197	[[ $(HOME=$PWD ./-ksh -ip </dev/null 2>/dev/null) == *$t* ]] &&
198		err_exit './-ksh -p does not ignore .profile'
199fi
200cd ~-
201rm -rf /tmp/ksh$$
202
203
204# { exec interactive login_shell restricted xtrace } in the following test
205
206for opt in \
207	allexport all-export all_export \
208	bgnice bg-nice bg_nice \
209	clobber emacs \
210	errexit err-exit err_exit \
211	glob \
212	globstar glob-star glob_star \
213	gmacs \
214	ignoreeof ignore-eof ignore_eof \
215	keyword log markdirs monitor notify \
216	pipefail pipe-fail pipe_fail \
217	trackall track-all track_all \
218	unset verbose vi \
219	viraw vi-raw vi_raw
220do	old=$opt
221	if [[ ! -o $opt ]]
222	then	old=no$opt
223	fi
224
225	set --$opt || err_exit "set --$opt failed"
226	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
227	[[ -o no$opt ]] && err_exit "[[ -o no$opt ]] failed"
228	[[ -o no-$opt ]] && err_exit "[[ -o no-$opt ]] failed"
229	[[ -o no_$opt ]] && err_exit "[[ -o no_$opt ]] failed"
230	[[ -o ?$opt ]] || err_exit "[[ -o ?$opt ]] failed"
231	[[ -o ?no$opt ]] || err_exit "[[ -o ?no$opt ]] failed"
232	[[ -o ?no-$opt ]] || err_exit "[[ -o ?no-$opt ]] failed"
233	[[ -o ?no_$opt ]] || err_exit "[[ -o ?no_$opt ]] failed"
234
235	set --no$opt || err_exit "set --no$opt failed"
236	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
237	[[ -o $opt ]] && err_exit "[[ -o $opt ]] failed"
238
239	set --no-$opt || err_exit "set --no-$opt failed"
240	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
241	[[ -o $opt ]] && err_exit "[[ -o $opt ]] failed"
242
243	set --no_$opt || err_exit "set --no_$opt failed"
244	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
245	[[ -o $opt ]] && err_exit "[[ -o $opt ]] failed"
246
247	set -o $opt || err_exit "set -o $opt failed"
248	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
249	set -o $opt=1 || err_exit "set -o $opt=1 failed"
250	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
251	set -o no$opt=0 || err_exit "set -o no$opt=0 failed"
252	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
253	set --$opt=1 || err_exit "set --$opt=1 failed"
254	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
255	set --no$opt=0 || err_exit "set --no$opt=0 failed"
256	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
257
258	set -o no$opt || err_exit "set -o no$opt failed"
259	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
260	set -o $opt=0 || err_exit "set -o $opt=0 failed"
261	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
262	set -o no$opt=1 || err_exit "set -o no$opt=1 failed"
263	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
264	set --$opt=0 || err_exit "set --$opt=0 failed"
265	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
266	set --no$opt=1 || err_exit "set --no$opt=1 failed"
267	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
268
269	set -o no-$opt || err_exit "set -o no-$opt failed"
270	[[ -o no-$opt ]] || err_exit "[[ -o no-$opt ]] failed"
271
272	set -o no_$opt || err_exit "set -o no_$opt failed"
273	[[ -o no_$opt ]] || err_exit "[[ -o no_$opt ]] failed"
274
275	set +o $opt || err_exit "set +o $opt failed"
276	[[ -o no$opt ]] || err_exit "[[ -o no$opt ]] failed"
277
278	set +o no$opt || err_exit "set +o no$opt failed"
279	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
280
281	set +o no-$opt || err_exit "set +o no-$opt failed"
282	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
283
284	set +o no_$opt || err_exit "set +o no_$opt failed"
285	[[ -o $opt ]] || err_exit "[[ -o $opt ]] failed"
286
287	set --$old
288done
289
290for opt in \
291	exec interactive login_shell login-shell logi privileged \
292	rc restricted xtrace
293do	[[ -o $opt ]]
294	y=$?
295	[[ -o no$opt ]]
296	n=$?
297	case $y$n in
298	10|01)	;;
299	*)	err_exit "[[ -o $opt ]] == [[ -o no$opt ]]" ;;
300	esac
301done
302
303for opt in \
304	foo foo-bar foo_bar
305do	if	[[ -o ?$opt ]]
306	then	err_exit "[[ -o ?$opt ]] should fail"
307	fi
308	if	[[ -o ?no$opt ]]
309	then	err_exit "[[ -o ?no$opt ]] should fail"
310	fi
311done
312
313[[ $(set +o) == $(set --state) ]] || err_exit "set --state different from set +o"
314set -- $(set --state)
315[[ $1 == set && $2 == --default ]] || err_exit "set --state failed -- expected 'set --default *', got '$1 $2 *'"
316shift
317restore=$*
318shift
319off=
320for opt
321do	case $opt in
322	--not*)	opt=${opt/--/--no} ;;
323	--no*)	opt=${opt/--no/--} ;;
324	--*)	opt=${opt/--/--no} ;;
325	esac
326	off="$off $opt"
327done
328set $off
329state=$(set --state)
330default=$(set --default --state)
331[[ $state == $default ]] || err_exit "set --state for default options failed: expected '$default', got '$state'"
332set $restore
333state=$(set --state)
334[[ $state == "set $restore" ]] || err_exit "set --state after restore failed: expected 'set $restore', got '$state'"
335
336false | true | true   || err_exit 'pipe not exiting exit value of last element'
337true | true | false   && err_exit 'pipe not exiting false'
338set -o pipefail
339false | true | true    && err_exit 'pipe with first not failing with pipefail'
340true | false | true    && err_exit 'pipe middle not failing with pipefail'
341true | true | false    && err_exit 'pipe last not failing with pipefail'
342print hi | (sleep 1;/bin/cat) > /dev/null || err_exit 'pipeline fails with pipefail'
343(
344	set -o pipefail
345	false | true
346	(( $? )) || err_exit 'pipe not failing in subshell with pipefail'
347) | wc >/dev/null
348$SHELL -c 'set -o pipefail; false | $(whence -p true);' && err_exit 'pipefail not returning failure with sh -c'
349[[ $( set -o pipefail
350	pipe() { date | cat > /dev/null ;}
351	print $'1\n2' |
352	while	read i
353	do 	if	pipe /tmp
354		then	{ print enter $i; sleep 2; print leave $i; } &
355		fi
356	done
357	wait) == $'enter 1\nenter 2'* ]] || err_exit  '& job delayed by pipefail'
358$SHELL -c '[[ $- == *c* ]]' || err_exit  'option c not in $-'
359trap 'rm -f /tmp/.profile' EXIT
360> /tmp/.profile
361for i in  i l r s D E a b e f h k n r t u v  x B C G H
362do	HOME=/tmp ENV= $SHELL -$i  2> /dev/null <<- ++EOF++ || err_exit "option $i not in \$-"
363	[[ \$- == *$i* ]]  ||   exit 1
364	++EOF++
365done
366letters=ilrabefhknuvxBCGE
367integer j=0
368for i in  interactive login restricted allexport notify errexit \
369	noglob  trackall keyword noexec nounset verbose xtrace braceexpand \
370	noclobber globstar rc
371do	HOME=/tmp ENV= $SHELL   -o $i  2> /dev/null <<- ++EOF++ || err_exit "option $i not equivalent to ${letters:j:1}"
372	[[ \$- == *${letters:j:1}* ]]  ||   exit 1
373	++EOF++
374	((j++))
375done
376exit $((Errors))
377