xref: /titanic_50/usr/src/lib/libshell/common/tests/builtins.sh (revision b819cea2f73f98c5662230cc9affc8cc84f77fcf)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2010 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
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34# test shell builtin commands
35builtin getconf
36: ${foo=bar} || err_exit ": failed"
37[[ $foo = bar ]] || err_exit ": side effects failed"
38set -- - foobar
39[[ $# = 2 && $1 = - && $2 = foobar ]] || err_exit "set -- - foobar failed"
40set -- -x foobar
41[[ $# = 2 && $1 = -x && $2 = foobar ]] || err_exit "set -- -x foobar failed"
42getopts :x: foo || err_exit "getopts :x: returns false"
43[[ $foo = x && $OPTARG = foobar ]] || err_exit "getopts :x: failed"
44OPTIND=1
45getopts :r:s var -r
46if	[[ $var != : || $OPTARG != r ]]
47then	err_exit "'getopts :r:s var -r' not working"
48fi
49OPTIND=1
50getopts :d#u OPT -d 16177
51if	[[ $OPT != d || $OPTARG != 16177 ]]
52then	err_exit "'getopts :d#u OPT=d OPTARG=16177' failed -- OPT=$OPT OPTARG=$OPTARG"
53fi
54OPTIND=1
55while getopts 'ab' option -a -b
56do	[[ $OPTIND == $((OPTIND)) ]] || err_exit "OPTIND optimization bug"
57done
58
59USAGE=$'[-][S:server?Operate on the specified \asubservice\a:]:[subservice:=pmserver]
60    {
61        [p:pmserver]
62        [r:repserver]
63        [11:notifyd]
64    }'
65set pmser p rep r notifyd -11
66while	(( $# > 1 ))
67do	OPTIND=1
68	getopts "$USAGE" OPT -S $1
69	[[ $OPT == S && $OPTARG == $2 ]] || err_exit "OPT=$OPT OPTARG=$OPTARG -- expected OPT=S OPTARG=$2"
70	shift 2
71done
72
73false ${foo=bar} &&  err_exit "false failed"
74read <<!
75hello world
76!
77[[ $REPLY = 'hello world' ]] || err_exit "read builtin failed"
78print x:y | IFS=: read a b
79if	[[ $a != x ]]
80then	err_exit "IFS=: read ... not working"
81fi
82read <<!
83hello \
84world
85!
86[[ $REPLY = 'hello world' ]] || err_exit "read continuation failed"
87read -d x <<!
88hello worldxfoobar
89!
90[[ $REPLY = 'hello world' ]] || err_exit "read builtin failed"
91read <<\!
92hello \
93	world \
94
95!
96[[ $REPLY == 'hello 	world' ]] || err_exit "read continuation2 failed"
97print "one\ntwo" | { read line
98	print $line | /bin/cat > /dev/null
99	read line
100}
101read <<\!
102\
103a\
104\
105\
106b
107!
108if	[[ $REPLY != ab ]]
109then	err_exit "read multiple continuation failed"
110fi
111if	[[ $line != two ]]
112then	err_exit "read from pipeline failed"
113fi
114line=two
115read line < /dev/null
116if	[[ $line != "" ]]
117then	err_exit "read from /dev/null failed"
118fi
119if	[[ $(print -R -) != - ]]
120then	err_exit "print -R not working correctly"
121fi
122if	[[ $(print -- -) != - ]]
123then	err_exit "print -- not working correctly"
124fi
125print -f "hello%nbar\n" size > /dev/null
126if	((	size != 5 ))
127then	err_exit "%n format of printf not working"
128fi
129print -n -u2 2>&1-
130[[ -w /dev/fd/1 ]] || err_exit "2<&1- with built-ins has side effects"
131x=$0
132if	[[ $(eval 'print $0') != $x ]]
133then	err_exit '$0 not correct for eval'
134fi
135$SHELL -c 'read x <<< hello' 2> /dev/null || err_exit 'syntax <<< not recognized'
136($SHELL -c 'read x[1] <<< hello') 2> /dev/null || err_exit 'read x[1] not working'
137unset x
138readonly x
139set -- $(readonly)
140if      [[ " $@ " != *" x "* ]]
141then    err_exit 'unset readonly variables are not displayed'
142fi
143if	[[ $(	for i in foo bar
144		do	print $i
145			continue 10
146		done
147	    ) != $'foo\nbar' ]]
148then	err_exit 'continue breaks out of loop'
149fi
150(continue bad 2>/dev/null && err_exit 'continue bad should return an error')
151(break bad 2>/dev/null && err_exit 'break bad should return an error')
152(continue 0 2>/dev/null && err_exit 'continue 0 should return an error')
153(break 0 2>/dev/null && err_exit 'break 0 should return an error')
154breakfun() { break;}
155continuefun() { continue;}
156for fun in break continue
157do	if	[[ $(	for i in foo
158			do	${fun}fun
159				print $i
160			done
161		) != foo ]]
162	then	err_exit "$fun call in ${fun}fun breaks out of for loop"
163	fi
164done
165if	[[ $(print -f "%b" "\a\n\v\b\r\f\E\03\\oo") != $'\a\n\v\b\r\f\E\03\\oo' ]]
166then	err_exit 'print -f "%b" not working'
167fi
168if	[[ $(print -f "%P" "[^x].*b\$") != '*[!x]*b' ]]
169then	err_exit 'print -f "%P" not working'
170fi
171if	[[ $(abc: for i in foo bar;do print $i;break abc;done) != foo ]]
172then	err_exit 'break labels not working'
173fi
174if	[[ $(command -v if)	!= if ]]
175then	err_exit	'command -v not working'
176fi
177read -r var <<\!
178
179!
180if	[[ $var != "" ]]
181then	err_exit "read -r of blank line not working"
182fi
183mkdir -p $tmp/a/b/c 2>/dev/null || err_exit  "mkdir -p failed"
184$SHELL -c "cd $tmp/a/b; cd c" 2>/dev/null || err_exit "initial script relative cd fails"
185
186trap 'print TERM' TERM
187exp=$'trap -- \'print TERM\' TERM\ntrap -- \'cd /; rm -rf '$tmp$'\' EXIT'
188got=$(trap)
189[[ $got == $exp ]] || err_exit "\$(trap) failed -- expected \"$exp\", got \"$got\""
190exp='print TERM'
191got=$(trap -p TERM)
192[[ $got == $exp ]] || err_exit "\$(trap -p TERM) failed -- expected \"$exp\", got \"$got\""
193
194[[ $($SHELL -c 'trap "print ok" SIGTERM; kill -s SIGTERM $$' 2> /dev/null) == ok ]] || err_exit 'SIGTERM not recognized'
195[[ $($SHELL -c 'trap "print ok" sigterm; kill -s sigterm $$' 2> /dev/null) == ok ]] || err_exit 'SIGTERM not recognized'
196[[ $($SHELL -c '( trap "" TERM);kill $$;print bad' == bad) ]] 2> /dev/null && err_exit 'trap ignored in subshell causes it to be ignored by parent'
197${SHELL} -c 'kill -1 -$$' 2> /dev/null
198[[ $(kill -l $?) == HUP ]] || err_exit 'kill -1 -pid not working'
199${SHELL} -c 'kill -1 -$$' 2> /dev/null
200[[ $(kill -l $?) == HUP ]] || err_exit 'kill -n1 -pid not working'
201${SHELL} -c 'kill -s HUP -$$' 2> /dev/null
202[[ $(kill -l $?) == HUP ]] || err_exit 'kill -HUP -pid not working'
203n=123
204typeset -A base
205base[o]=8#
206base[x]=16#
207base[X]=16#
208for i in d i o u x X
209do	if	(( $(( ${base[$i]}$(printf "%$i" $n) )) != n  ))
210	then	err_exit "printf %$i not working"
211	fi
212done
213if	[[ $( trap 'print done' EXIT) != done ]]
214then	err_exit 'trap on EXIT not working'
215fi
216if	[[ $( trap 'print done' EXIT; trap - EXIT) == done ]]
217then	err_exit 'trap on EXIT not being cleared'
218fi
219if	[[ $(type test) != 'test is a shell builtin' ]]
220then	err_exit 'whence -v test not a builtin'
221fi
222builtin -d test
223if	[[ $(type test) == *builtin* ]]
224then	err_exit 'whence -v test after builtin -d incorrect'
225fi
226typeset -Z3 percent=$(printf '%o\n' "'%'")
227forrmat=\\${percent}s
228if      [[ $(printf "$forrmat") != %s ]]
229then    err_exit "printf $forrmat not working"
230fi
231if	(( $(printf 'x\0y' | wc -c) != 3 ))
232then	err_exit 'printf \0 not working'
233fi
234if	[[ $(printf "%bx%s\n" 'f\to\cbar') != $'f\to' ]]
235then	err_exit 'printf %bx%s\n  not working'
236fi
237alpha=abcdefghijklmnop
238if	[[ $(printf "%10.*s\n" 5 $alpha) != '     abcde' ]]
239then	err_exit 'printf %10.%s\n  not working'
240fi
241float x2=.0000625
242if	[[ $(printf "%10.5E\n" x2) != 6.25000E-05 ]]
243then	err_exit 'printf "%10.5E" not normalizing correctly'
244fi
245x2=.000000001
246if	[[ $(printf "%g\n" x2 2>/dev/null) != 1e-09 ]]
247then	err_exit 'printf "%g" not working correctly'
248fi
249#FIXME#($SHELL read -s foobar <<\!
250#FIXME#testing
251#FIXME#!
252#FIXME#) 2> /dev/null || err_exit ksh read -s var fails
253if	[[ $(printf +3 2>/dev/null) !=   +3 ]]
254then	err_exit 'printf is not processing formats beginning with + correctly'
255fi
256if	printf "%d %d\n" 123bad 78 >/dev/null 2>/dev/null
257then	err_exit "printf not exiting non-zero with conversion errors"
258fi
259if	[[ $(trap --version 2> /dev/null;print done) != done ]]
260then	err_exit 'trap builtin terminating after --version'
261fi
262if	[[ $(set --version 2> /dev/null;print done) != done ]]
263then	err_exit 'set builtin terminating after --veresion'
264fi
265unset -f foobar
266function foobar
267{
268	print 'hello world'
269}
270OPTIND=1
271if	[[ $(getopts  $'[+?X\ffoobar\fX]' v --man 2>&1) != *'Xhello world'X* ]]
272then	err_exit '\f...\f not working in getopts usage strings'
273fi
274if	[[ $(printf '%H\n' $'<>"& \'\tabc') != '&lt;&gt;&quot;&amp;&nbsp;&apos;&#9;abc' ]]
275then	err_exit 'printf %H not working'
276fi
277if	[[ $(printf '%R %R %R %R\n' 'a.b' '*.c' '^'  '!(*.*)') != '^a\.b$ \.c$ ^\^$ ^(.*\..*)!$' ]]
278then	err_exit 'printf %R not working'
279fi
280if	[[ $(printf '%..:c\n' abc) != a:b:c ]]
281then	err_exit "printf '%..:c' not working"
282fi
283if	[[ $(printf '%..*c\n' : abc) != a:b:c ]]
284then	err_exit "printf '%..*c' not working"
285fi
286if	[[ $(printf '%..:s\n' abc def ) != abc:def ]]
287then	err_exit "printf '%..:s' not working"
288fi
289if	[[ $(printf '%..*s\n' : abc def) != abc:def ]]
290then	err_exit "printf '%..*s' not working"
291fi
292[[ $(printf '%q\n') == '' ]] || err_exit 'printf "%q" with missing arguments'
293# we won't get hit by the one second boundary twice, right?
294[[ $(printf '%T\n' now) == "$(date)" ]] ||
295[[ $(printf '%T\n' now) == "$(date)" ]] ||
296err_exit 'printf "%T" now'
297behead()
298{
299	read line
300	left=$(cat)
301}
302print $'line1\nline2' | behead
303if	[[ $left != line2 ]]
304then	err_exit "read reading ahead on a pipe"
305fi
306read -n1 y <<!
307abc
308!
309exp=a
310if      [[ $y != $exp ]]
311then    err_exit "read -n1 failed -- expected '$exp', got '$y'"
312fi
313print -n $'{ read -r line;print $line;}\nhello' > $tmp/script
314chmod 755 $tmp/script
315if	[[ $($SHELL < $tmp/script) != hello ]]
316then	err_exit 'read of incomplete line not working correctly'
317fi
318set -f
319set -- *
320if      [[ $1 != '*' ]]
321then    err_exit 'set -f not working'
322fi
323unset pid1 pid2
324false &
325pid1=$!
326pid2=$(
327	wait $pid1
328	(( $? == 127 )) || err_exit "job known to subshell"
329	print $!
330)
331wait $pid1
332(( $? == 1 )) || err_exit "wait not saving exit value"
333wait $pid2
334(( $? == 127 )) || err_exit "subshell job known to parent"
335env=
336v=$(getconf LIBPATH)
337for v in ${v//,/ }
338do	v=${v#*:}
339	v=${v%%:*}
340	eval [[ \$$v ]] && env="$env $v=\"\$$v\""
341done
342if	[[ $(foo=bar; eval foo=\$foo $env exec -c \$SHELL -c \'print \$foo\') != bar ]]
343then	err_exit '"name=value exec -c ..." not working'
344fi
345$SHELL -c 'OPTIND=-1000000; getopts a opt -a' 2> /dev/null
346[[ $? == 1 ]] || err_exit 'getopts with negative OPTIND not working'
347getopts 'n#num' opt  -n 3
348[[ $OPTARG == 3 ]] || err_exit 'getopts with numerical arguments failed'
349if	[[ $($SHELL -c $'printf \'%2$s %1$s\n\' world hello') != 'hello world' ]]
350then	err_exit 'printf %2$s %1$s not working'
351fi
352val=$(( 'C' ))
353set -- \
354	"'C"	$val	0	\
355	"'C'"	$val	0	\
356	'"C'	$val	0	\
357	'"C"'	$val	0	\
358	"'CX"	$val	1	\
359	"'CX'"	$val	1	\
360	"'C'X"	$val	1	\
361	'"CX'	$val	1	\
362	'"CX"'	$val	1	\
363	'"C"X'	$val	1
364while (( $# >= 3 ))
365do	arg=$1 val=$2 code=$3
366	shift 3
367	for fmt in '%d' '%g'
368	do	out=$(printf "$fmt" "$arg" 2>/dev/null)
369		err=$(printf "$fmt" "$arg" 2>&1 >/dev/null)
370		printf "$fmt" "$arg" >/dev/null 2>&1
371		ret=$?
372		[[ $out == $val ]] || err_exit "printf $fmt $arg failed -- expected '$val', got '$out'"
373		if	(( $code ))
374		then	[[ $err ]] || err_exit "printf $fmt $arg failed, error message expected"
375		else	[[ $err ]] && err_exit "$err: printf $fmt $arg failed, error message not expected -- got '$err'"
376		fi
377		(( $ret == $code )) || err_exit "printf $fmt $arg failed -- expected exit code $code, got $ret"
378	done
379done
380((n=0))
381((n++)); ARGC[$n]=1 ARGV[$n]=""
382((n++)); ARGC[$n]=2 ARGV[$n]="-a"
383((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2"
384((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2 x"
385((n++)); ARGC[$n]=4 ARGV[$n]="-a -v 2 x y"
386for ((i=1; i<=n; i++))
387do	set -- ${ARGV[$i]}
388	OPTIND=0
389	while	getopts -a tst "av:" OPT
390	do	:
391	done
392	if	[[ $OPTIND != ${ARGC[$i]} ]]
393	then	err_exit "\$OPTIND after getopts loop incorrect -- expected ${ARGC[$i]}, got $OPTIND"
394	fi
395done
396options=ab:c
397optarg=foo
398set -- -a -b $optarg -c bar
399while	getopts $options opt
400do	case $opt in
401	a|c)	[[ $OPTARG ]] && err_exit "getopts $options \$OPTARG for flag $opt failed, expected \"\", got \"$OPTARG\"" ;;
402	b)	[[ $OPTARG == $optarg ]] || err_exit "getopts $options \$OPTARG failed -- \"$optarg\" expected, got \"$OPTARG\"" ;;
403	*)	err_exit "getopts $options failed -- got flag $opt" ;;
404	esac
405done
406
407unset a
408{ read -N3 a; read -N1 b;}  <<!
409abcdefg
410!
411exp=abc
412[[ $a == $exp ]] || err_exit "read -N3 here-document failed -- expected '$exp', got '$a'"
413exp=d
414[[ $b == $exp ]] || err_exit "read -N1 here-document failed -- expected '$exp', got '$b'"
415read -n3 a <<!
416abcdefg
417!
418exp=abc
419[[ $a == $exp ]] || err_exit "read -n3 here-document failed -- expected '$exp', got '$a'"
420#(print -n a;sleep 1; print -n bcde) | { read -N3 a; read -N1 b;}
421#[[ $a == $exp ]] || err_exit "read -N3 from pipe failed -- expected '$exp', got '$a'"
422#exp=d
423#[[ $b == $exp ]] || err_exit "read -N1 from pipe failed -- expected '$exp', got '$b'"
424#(print -n a;sleep 1; print -n bcde) | read -n3 a
425#exp=a
426#[[ $a == $exp ]] || err_exit "read -n3 from pipe failed -- expected '$exp', got '$a'"
427#rm -f $tmp/fifo
428#if	mkfifo $tmp/fifo 2> /dev/null
429#then	(print -n a; sleep 1;print -n bcde)  > $tmp/fifo &
430#	{
431#	read -u5 -n3 -t2 a || err_exit 'read -n3 from fifo timedout'
432#	read -u5 -n1 -t2 b || err_exit 'read -n1 from fifo timedout'
433#	} 5< $tmp/fifo
434#	exp=a
435#	[[ $a == $exp ]] || err_exit "read -n3 from fifo failed -- expected '$exp', got '$a'"
436#	rm -f $tmp/fifo
437#	mkfifo $tmp/fifo 2> /dev/null
438#	(print -n a; sleep 1;print -n bcde) > $tmp/fifo &
439#	{
440#	read -u5 -N3 -t2 a || err_exit 'read -N3 from fifo timed out'
441#	read -u5 -N1 -t2 b || err_exit 'read -N1 from fifo timedout'
442#	} 5< $tmp/fifo
443#	exp=abc
444#	[[ $a == $exp ]] || err_exit "read -N3 from fifo failed -- expected '$exp', got '$a'"
445#	exp=d
446#	[[ $b == $exp ]] || err_exit "read -N1 from fifo failed -- expected '$exp', got '$b'"
447#fi
448#rm -f $tmp/fifo
449
450function longline
451{
452	integer i
453	for((i=0; i < $1; i++))
454	do	print argument$i
455	done
456}
457# test command -x option
458integer sum=0 n=10000
459if	! ${SHELL:-ksh} -c 'print $#' count $(longline $n) > /dev/null  2>&1
460then	for i in $(command command -x ${SHELL:-ksh} -c 'print $#;[[ $1 != argument0 ]]' count $(longline $n) 2> /dev/null)
461	do	((sum += $i))
462	done
463	(( sum == n )) || err_exit "command -x processed only $sum arguments"
464	command -p command -x ${SHELL:-ksh} -c 'print $#;[[ $1 == argument0 ]]' count $(longline $n) > /dev/null  2>&1
465	[[ $? != 1 ]] && err_exit 'incorrect exit status for command -x'
466fi
467# test command -x option with extra arguments
468integer sum=0 n=10000
469if      ! ${SHELL:-ksh} -c 'print $#' count $(longline $n) > /dev/null  2>&1
470then    for i in $(command command -x ${SHELL:-ksh} -c 'print $#;[[ $1 != argument0 ]]' count $(longline $n) one two three) #2> /dev/null)
471	do      ((sum += $i))
472	done
473	(( sum  > n )) || err_exit "command -x processed only $sum arguments"
474	(( (sum-n)%3==0 )) || err_exit "command -x processed only $sum arguments"
475	(( sum == n+3)) && err_exit "command -x processed only $sum arguments"
476	command -p command -x ${SHELL:-ksh} -c 'print $#;[[ $1 == argument0 ]]' count $(longline $n) > /dev/null  2>&1
477	[[ $? != 1 ]] && err_exit 'incorrect exit status for command -x'
478fi
479# test for debug trap
480[[ $(typeset -i i=0
481	trap 'print $i' DEBUG
482	while (( i <2))
483	do	(( i++))
484	done) == $'0\n0\n1\n1\n2' ]]  || err_exit  "DEBUG trap not working"
485getconf UNIVERSE - ucb
486[[ $($SHELL -c 'echo -3') == -3 ]] || err_exit "echo -3 not working in ucb universe"
487typeset -F3 start_x=SECONDS total_t delay=0.02
488typeset reps=50 leeway=5
489#sleep $(( 2 * leeway * reps * delay )) |
490#for (( i=0 ; i < reps ; i++ ))
491#do	read -N1 -t $delay
492#done
493#(( total_t = SECONDS - start_x ))
494#if	(( total_t > leeway * reps * delay ))
495#then	err_exit "read -t in pipe taking $total_t secs - $(( reps * delay )) minimum - too long"
496#elif	(( total_t < reps * delay ))
497#then	err_exit "read -t in pipe taking $total_t secs - $(( reps * delay )) minimum - too fast"
498#fi
499#$SHELL -c 'sleep $(printf "%a" .95)' 2> /dev/null || err_exit "sleep doesn't except %a format constants"
500#$SHELL -c 'test \( ! -e \)' 2> /dev/null ; [[ $? == 1 ]] || err_exit 'test \( ! -e \) not working'
501[[ $(ulimit) == "$(ulimit -fS)" ]] || err_exit 'ulimit is not the same as ulimit -fS'
502tmpfile=$tmp/file.2
503print $'\nprint -r -- "${.sh.file} ${LINENO} ${.sh.lineno}"' > $tmpfile
504[[ $( . "$tmpfile") == "$tmpfile 2 1" ]] || err_exit 'dot command not working'
505print -r -- "'xxx" > $tmpfile
506[[ $($SHELL -c ". $tmpfile"$'\n print ok' 2> /dev/null) == ok ]] || err_exit 'syntax error in dot command affects next command'
507
508#float sec=$SECONDS del=4
509#exec 3>&2 2>/dev/null
510#$SHELL -c "( sleep 1; kill -ALRM \$\$ ) & sleep $del" 2> /dev/null
511#exitval=$?
512#(( sec = SECONDS - sec ))
513#exec 2>&3-
514#(( exitval )) && err_exit "sleep doesn't exit 0 with ALRM interupt"
515#(( sec > (del - 1) )) || err_exit "ALRM signal causes sleep to terminate prematurely -- expected 3 sec, got $sec"
516typeset -r z=3
517y=5
518for i in 123 z  %x a.b.c
519do	( unset $i)  2>/dev/null && err_exit "unset $i should fail"
520done
521a=()
522for i in y y  y[8] t[abc] y.d a.b  a
523do	unset $i ||  print -u2  "err_exit unset $i should not fail"
524done
525[[ $($SHELL -c 'y=3; unset 123 y;print $?$y') == 1 ]] 2> /dev/null ||  err_exit 'y is not getting unset with unset 123 y'
526[[ $($SHELL -c 'trap foo TERM; (trap;(trap) )') == 'trap -- foo TERM' ]] || err_exit 'traps not getting reset when subshell is last process'
527
528exit $((Errors))
529