xref: /titanic_44/usr/src/lib/libshell/common/tests/heredoc.sh (revision a776e90690d98f629ab57f6493ff5949af9d65bd)
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
30f=/tmp/here1$$
31g=/tmp/here2$$
32trap "rm -f $f $g" EXIT
33cat > $f <<!
34hello world
35!
36if	[[ $(<$f) != 'hello world' ]]
37then	err_exit "'hello world' here doc not working"
38fi
39cat > $g <<\!
40hello world
41!
42cmp $f $g 2> /dev/null || err_exit "'hello world' quoted here doc not working"
43cat > $g <<- !
44	hello world
45!
46cmp $f $g 2> /dev/null || err_exit "'hello world' tabbed here doc not working"
47cat > $g <<- \!
48	hello world
49!
50cmp $f $g 2> /dev/null || err_exit "'hello world' quoted tabbed here doc not working"
51x=hello
52cat > $g <<!
53$x world
54!
55cmp $f $g 2> /dev/null || err_exit "'$x world' here doc not working"
56cat > $g <<!
57$(print hello) world
58!
59cmp $f $g 2> /dev/null || err_exit "'$(print hello) world' here doc not working"
60cat > $f <<\!!
61!@#$%%^^&*()_+~"::~;'`<>?/.,{}[]
62!!
63if	[[ $(<$f) != '!@#$%%^^&*()_+~"::~;'\''`<>?/.,{}[]' ]]
64then	err_exit "'hello world' here doc not working"
65fi
66cat > $g <<!!
67!@#\$%%^^&*()_+~"::~;'\`<>?/.,{}[]
68!!
69cmp $f $g 2> /dev/null || err_exit "unquoted here doc not working"
70exec 3<<!
71	foo
72!
73if	[[ $(<&3) != '	foo' ]]
74then	err_exit "leading tabs stripped with <<!"
75fi
76$SHELL -c "
77eval `echo 'cat <<x'` "|| err_exit "eval `echo 'cat <<x'` core dumps"
78cat > /dev/null <<EOF # comments should not cause core dumps
79abc
80EOF
81cat >$g << :
82:
83:
84cmp /dev/null $g 2> /dev/null || err_exit "empty here doc not working"
85x=$(print $( cat <<HUP
86hello
87HUP
88)
89)
90if	[[ $x != hello ]]
91then	err_exit "here doc inside command sub not working"
92fi
93y=$(cat <<!
94${x:+${x}}
95!
96)
97if	[[ $y != "${x:+${x}}" ]]
98then	err_exit '${x:+${x}} not working in here document'
99fi
100$SHELL -c '
101x=0
102while (( x < 100 ))
103do	((x = x+1))
104	cat << EOF
105EOF
106done
107' 2> /dev/null  || err_exit '100 empty here docs fails'
108{
109	print 'builtin -d cat
110	cat <<- EOF'
111	for ((i=0; i < 100; i++))
112	do print XXXXXXXXXXXXXXXXXXXX
113	done
114	print ' XXX$(date)XXXX
115	EOF'
116} > $f
117chmod +x "$f"
118$SHELL "$f" > /dev/null  || err_exit "large here-doc with command substitution fails"
119x=$(/bin/cat <<!
120$0
121!
122)
123[[ "$x" == "$0" ]] || err_exit '$0 not correct inside here documents'
124$SHELL -c 'x=$(
125cat << EOF
126EOF)' 2> /dev/null || err_exit 'here-doc cannot be terminated by )'
127if	[[ $( IFS=:;cat <<-!
128			$IFS$(print hi)$IFS
129		!) != :hi: ]]
130then	err_exit '$IFS unset by command substitution in here docs'
131fi
132if	x=$($SHELL -c 'cat <<< "hello world"' 2> /dev/null)
133then	[[ $x == 'hello world' ]] || err_exit '<<< documents not working'
134	x=$($SHELL -c 'v="hello  world";cat <<< $v' 2> /dev/null)
135	[[ $x == 'hello  world' ]] || err_exit '<<< documents with $x not working'
136	x=$($SHELL -c 'v="hello  world";cat <<< "$v"' 2> /dev/null)
137	[[ $x == 'hello  world' ]] || err_exit '<<< documents with $x not working'
138else	err_exit '<<< syntax not supported'
139fi
140if	[[ $(cat << EOF #testing
141#abc
142abc
143EOF) != $'#abc\nabc' ]]
144then	err_exit 'comments not preserved in here-documents'
145fi
146cat  > "$f" <<- '!!!!' 
147	builtin cat
148	: << EOF
149	$PWD
150	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
151	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
152	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
153	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
154	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
155	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
156	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
157	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
158	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
159	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
160	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
161	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
162	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
163	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
164	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
165	EOF
166	command exec 3>&- 4>&- 5>&- 6>&- 7>&- 8>&- 9>&-
167	x=abc
168	cat << EOF
169	$x
170	EOF
171!!!!
172chmod 755 "$f"
173if	[[ $($SHELL  "$f") != abc ]]
174then	err_exit	'here document descritor was closed'
175fi
176cat  > "$f" <<- '!!!!'
177	exec 0<&-
178	foobar()
179	{
180		/bin/cat <<- !
181		foobar
182		!
183	}
184	: << EOF
185	$PWD
186	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
187	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
188	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
189	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
190	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
191	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
192	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
193	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
194	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
195	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
196	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
197	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
198	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
199	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
200	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
201	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
202	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
203	EOF
204	print -r -- "$(foobar)"
205!!!!
206if	[[ $($SHELL  "$f") != foobar ]]
207then	err_exit	'here document with stdin closed failed'
208fi
209printf $'cat   <<# \\!!!\n\thello\n\t\tworld\n!!!' > $f
210[[ $($SHELL "$f") == $'hello\n\tworld' ]] || err_exit "<<# not working for quoted here documents"
211printf $'w=world;cat   <<# !!!\n\thello\n\t\t$w\n!!!' > $f
212[[ $($SHELL "$f") == $'hello\n\tworld' ]] || err_exit "<<# not working for non-quoted here documents"
213exit $((Errors))
214