xref: /freebsd/bin/sh/tests/builtins/trap2.0 (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1# $FreeBSD$
2# This is really a test for outqstr(), which is readily accessible via trap.
3
4runtest()
5{
6	teststring=$1
7	trap -- "$teststring" USR1
8	traps=$(trap)
9	if [ "$teststring" != "-" ] && [ -z "$traps" ]; then
10		# One possible reading of POSIX requires the above to return an
11		# empty string because backquote commands are executed in a
12		# subshell and subshells shall reset traps. However, an example
13		# in the normative description of the trap builtin shows the
14		# same usage as here, it is useful and our /bin/sh allows it.
15		echo '$(trap) is broken'
16		exit 1
17	fi
18	trap - USR1
19	eval "$traps"
20	traps2=$(trap)
21	if [ "$traps" != "$traps2" ]; then
22		echo "Mismatch for '$teststring'"
23		exit 1
24	fi
25}
26
27runtest 'echo'
28runtest 'echo hi'
29runtest "'echo' 'hi'"
30runtest '"echo" $PATH'
31runtest '\echo "$PATH"'
32runtest ' 0'
33runtest '0 '
34runtest ' 1'
35runtest '1 '
36i=1
37while [ $i -le 127 ]; do
38	c=$(printf \\"$(printf %o $i)")
39	if [ $i -lt 48 ] || [ $i -gt 57 ]; then
40		runtest "$c"
41	fi
42	runtest " $c$c"
43	runtest "a$c"
44	i=$((i+1))
45done
46IFS=,
47runtest ' '
48runtest ','
49unset IFS
50runtest ' '
51
52exit 0
53