xref: /titanic_50/usr/src/lib/libshell/common/tests/heredoc.sh (revision 0a1278f26ea4b7c8c0285d4f2d6c5b680904aa01)
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
34f=$tmp/here1
35g=$tmp/here2
36cat > $f <<!
37hello world
38!
39if	[[ $(<$f) != 'hello world' ]]
40then	err_exit "'hello world' here doc not working"
41fi
42cat > $g <<\!
43hello world
44!
45cmp $f $g 2> /dev/null || err_exit "'hello world' quoted here doc not working"
46cat > $g <<- !
47	hello world
48!
49cmp $f $g 2> /dev/null || err_exit "'hello world' tabbed here doc not working"
50cat > $g <<- \!
51	hello world
52!
53cmp $f $g 2> /dev/null || err_exit "'hello world' quoted tabbed here doc not working"
54x=hello
55cat > $g <<!
56$x world
57!
58cmp $f $g 2> /dev/null || err_exit "'$x world' here doc not working"
59cat > $g <<!
60$(print hello) world
61!
62cmp $f $g 2> /dev/null || err_exit "'$(print hello) world' here doc not working"
63cat > $f <<\!!
64!@#$%%^^&*()_+~"::~;'`<>?/.,{}[]
65!!
66if	[[ $(<$f) != '!@#$%%^^&*()_+~"::~;'\''`<>?/.,{}[]' ]]
67then	err_exit "'hello world' here doc not working"
68fi
69cat > $g <<!!
70!@#\$%%^^&*()_+~"::~;'\`<>?/.,{}[]
71!!
72cmp $f $g 2> /dev/null || err_exit "unquoted here doc not working"
73exec 3<<!
74	foo
75!
76if	[[ $(<&3) != '	foo' ]]
77then	err_exit "leading tabs stripped with <<!"
78fi
79$SHELL -c "
80eval `echo 'cat <<x'` "|| err_exit "eval `echo 'cat <<x'` core dumps"
81cat > /dev/null <<EOF # comments should not cause core dumps
82abc
83EOF
84cat >$g << :
85:
86:
87cmp /dev/null $g 2> /dev/null || err_exit "empty here doc not working"
88x=$(print $( cat <<HUP
89hello
90HUP
91)
92)
93if	[[ $x != hello ]]
94then	err_exit "here doc inside command sub not working"
95fi
96y=$(cat <<!
97${x:+${x}}
98!
99)
100if	[[ $y != "${x:+${x}}" ]]
101then	err_exit '${x:+${x}} not working in here document'
102fi
103$SHELL -c '
104x=0
105while (( x < 100 ))
106do	((x = x+1))
107	cat << EOF
108EOF
109done
110' 2> /dev/null  || err_exit '100 empty here docs fails'
111{
112	print 'builtin -d cat
113	cat <<- EOF'
114	for ((i=0; i < 100; i++))
115	do print XXXXXXXXXXXXXXXXXXXX
116	done
117	print ' XXX$(date)XXXX
118	EOF'
119} > $f
120chmod +x "$f"
121$SHELL "$f" > /dev/null  || err_exit "large here-doc with command substitution fails"
122x=$(/bin/cat <<!
123$0
124!
125)
126[[ "$x" == "$0" ]] || err_exit '$0 not correct inside here documents'
127$SHELL -c 'x=$(
128cat << EOF
129EOF)' 2> /dev/null || err_exit 'here-doc cannot be terminated by )'
130if	[[ $( IFS=:;cat <<-!
131			$IFS$(print hi)$IFS
132		!) != :hi: ]]
133then	err_exit '$IFS unset by command substitution in here docs'
134fi
135if	x=$($SHELL -c 'cat <<< "hello world"' 2> /dev/null)
136then	[[ $x == 'hello world' ]] || err_exit '<<< documents not working'
137	x=$($SHELL -c 'v="hello  world";cat <<< $v' 2> /dev/null)
138	[[ $x == 'hello  world' ]] || err_exit '<<< documents with $x not working'
139	x=$($SHELL -c 'v="hello  world";cat <<< "$v"' 2> /dev/null)
140	[[ $x == 'hello  world' ]] || err_exit '<<< documents with $x not working'
141else	err_exit '<<< syntax not supported'
142fi
143if	[[ $(cat << EOF #testing
144#abc
145abc
146EOF) != $'#abc\nabc' ]]
147then	err_exit 'comments not preserved in here-documents'
148fi
149cat  > "$f" <<- '!!!!'
150	builtin cat
151	: << EOF
152	$PWD
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	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
166	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
167	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
168	EOF
169	command exec 3>&- 4>&- 5>&- 6>&- 7>&- 8>&- 9>&-
170	x=abc
171	cat << EOF
172	$x
173	EOF
174!!!!
175chmod 755 "$f"
176if	[[ $($SHELL  "$f") != abc ]]
177then	err_exit	'here document descritor was closed'
178fi
179cat  > "$f" <<- '!!!!'
180	exec 0<&-
181	foobar()
182	{
183		/bin/cat <<- !
184		foobar
185		!
186	}
187	: << EOF
188	$PWD
189	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
190	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
191	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
192	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
193	xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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exit	'here document with stdin closed failed'
211fi
212printf $'cat   <<# \\!!!\n\thello\n\t\tworld\n!!!' > $f
213[[ $($SHELL "$f") == $'hello\n\tworld' ]] || err_exit "<<# not working for quoted here documents"
214printf $'w=world;cat   <<# !!!\n\thello\n\t\t$w\n!!!' > $f
215[[ $($SHELL "$f") == $'hello\n\tworld' ]] || err_exit "<<# not working for non-quoted here documents"
216[[ $( $SHELL  <<- \++++
217	S=( typeset a )
218	function S.a.get
219	{
220	     .sh.value=$__a
221	}
222	__a=1234
223	cat <<-EOF
224	${S.a}
225	EOF
226++++
227) == 1234 ]]  2> /dev/null || err_exit 'here document with get discipline failed'
228[[ $($SHELL -c 'g(){ print ok;}; cat <<- EOF
229	${ g;}
230	EOF
231	' 2> /dev/null) == ok ]] || err_exit '${ command;} not working in heredoc'
232script=$f
233{
234for ((i=0; i < 406; i++))
235do	print ': 23456789012345678'
236done
237print : 123456789123
238cat <<- \EOF
239eval "$(
240	{ cat                                 ; } <<MARKER
241	  print  hello
242	MARKER
243)"
244EOF
245} > $script
246chmod +x $script
247[[ $($SHELL $script) == hello ]] 2> /dev/null || err_exit 'heredoc embeded in command substitution fails at buffer boundary'
248
249got=$( cat << EOF
250\
251abc
252EOF)
253[[ $got == abc ]] || err_exit 'line continuation at start of buffer not working'
254
255exit $((Errors))
256