1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2011 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
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########################################################################
20# test the behavior of return and exit with functions
21
22function err_exit
23{
24	print -u2 -n "\t"
25	print -u2 -r ${Command}[$1]: "${@:2}"
26	let Errors+=1
27}
28alias err_exit='err_exit $LINENO'
29
30Command=${0##*/}
31integer Errors=0
32
33tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
34trap "cd /; rm -rf $tmp" EXIT
35
36unset HISTFILE
37
38foo=NOVAL bar=NOVAL
39file=$tmp/test
40function foo
41{
42	typeset foo=NOEXIT
43	trap "foo=EXIT;rm -f $file" EXIT
44	> $file
45	if	(( $1 == 0 ))
46	then	return $2
47	elif	(( $1 == 1 ))
48	then	exit $2
49	else	bar "$@"
50	fi
51}
52
53function bar
54{
55	typeset bar=NOEXIT
56	trap 'bar=EXIT' EXIT
57	if	(( $1 == 2 ))
58	then	return $2
59	elif	(( $1 == 3 ))
60	then	exit $2
61	fi
62}
63
64function funcheck
65{
66	[[ $foo == EXIT ]] || err_exit "foo "$@" : exit trap not set"
67	if	[[ -f $file ]]
68	then	rm -r $file
69		err_exit "foo $@: doesn't remove $file"
70	fi
71	foo=NOVAL bar=NOVAL
72}
73
74(exit 0) || err_exit "exit 0 is not zero"
75(return 0) || err_exit "return 0 is not zero"
76(exit) || err_exit "default exit value is not zero"
77(return) || err_exit "default return value is not zero"
78(exit 35)
79ret=$?
80if	(( $ret != 35 ))
81then	err_exit "exit 35 is $ret not 35"
82fi
83(return 35)
84ret=$?
85if	(( $ret != 35 ))
86then	err_exit "return 35 is $ret not 35"
87fi
88
89foo 0 0 || err_exit "foo 0 0: incorrect return"
90funcheck 0 0
91foo 0 3
92ret=$?
93if	(( $ret != 3 ))
94then	err_exit "foo 0 3: return is $ret not 3"
95fi
96funcheck 0 3
97foo 2 0 || err_exit "foo 2 0: incorrect return"
98[[ $bar == EXIT ]] || err_exit "foo 2 0: bar exit trap not set"
99funcheck 2 0
100foo 2 3
101ret=$?
102if	(( $ret != 3 ))
103then	err_exit "foo 2 3: return is $ret not 3"
104fi
105[[ $bar == EXIT ]] || err_exit "foo 2 3: bar exit trap not set"
106funcheck 2 3
107(foo 3 3)
108ret=$?
109if	(( $ret != 3 ))
110then	err_exit "foo 3 3: return is $ret not 3"
111fi
112foo=EXIT
113funcheck 3 3
114cat > $file <<!
115return 3
116exit 4
117!
118( . $file )
119ret=$?
120if	(( $ret != 3 ))
121then	err_exit "return in dot script is $ret should be 3"
122fi
123chmod 755 $file
124(  $file )
125ret=$?
126if	(( $ret != 3 ))
127then	err_exit "return in script is $ret should be 3"
128fi
129cat > $file <<!
130: line 1
131# next line should fail and cause an exit
132: > /
133exit 4
134!
135( . $file ; exit 5 ) 2> /dev/null
136ret=$?
137if	(( $ret != 1 ))
138then	err_exit "error in dot script is $ret should be 1"
139fi
140(  $file; exit 5 ) 2> /dev/null
141ret=$?
142if	(( $ret != 5 ))
143then	err_exit "error in script is $ret should be 5"
144fi
145cat > $file <<\!
146print -r -- "$0"
147!
148x=$( . $file)
149if	[[ $x != $0 ]]
150then	err_exit "\$0 in a dot script is $x. Should be $0"
151fi
152x=$($SHELL -i --norc 2> /dev/null <<\!
153typeset -i x=1/0
154print hello
155!
156)
157if	[[ $x != hello ]]
158then	err_exit "interactive shell terminates with error in bltin"
159fi
160x=$( set -e
161	false
162	print bad
163	)
164if	[[ $x != '' ]]
165then	err_exit "set -e doesn't terminate script on error"
166fi
167x=$( set -e
168	trap 'exit 0' EXIT
169	false
170	print bad
171	)
172if	(( $? != 0 ))
173then	err_exit "exit 0 in trap should doesn't set exit value to 0"
174fi
175$SHELL <<\!
176trap 'exit 8' EXIT
177exit 1
178!
179if	(( $? != 8 ))
180then	err_exit "exit 8 in trap should set exit value to 8"
181fi
182
183exit $((Errors<125?Errors:125))
184