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