xref: /openbsd-src/regress/bin/ksh/heredoc.t (revision 7a45d967763dfef3375840838510612b182a27d4)
1*7a45d967Smillertname: heredoc-1
2*7a45d967Smillertdescription:
3*7a45d967Smillert	Check ordering/content of redundent here documents.
4*7a45d967Smillertstdin:
5*7a45d967Smillert	cat << EOF1 << EOF2
6*7a45d967Smillert	hi
7*7a45d967Smillert	EOF1
8*7a45d967Smillert	there
9*7a45d967Smillert	EOF2
10*7a45d967Smillertexpected-stdout:
11*7a45d967Smillert	there
12*7a45d967Smillert---
13*7a45d967Smillert
14*7a45d967Smillertname: heredoc-2
15*7a45d967Smillertdescription:
16*7a45d967Smillert	Check quoted here-doc is protected.
17*7a45d967Smillertstdin:
18*7a45d967Smillert	a=foo
19*7a45d967Smillert	cat << 'EOF'
20*7a45d967Smillert	hi\
21*7a45d967Smillert	there$a
22*7a45d967Smillert	stuff
23*7a45d967Smillert	EO\
24*7a45d967Smillert	F
25*7a45d967Smillert	EOF
26*7a45d967Smillertexpected-stdout:
27*7a45d967Smillert	hi\
28*7a45d967Smillert	there$a
29*7a45d967Smillert	stuff
30*7a45d967Smillert	EO\
31*7a45d967Smillert	F
32*7a45d967Smillert---
33*7a45d967Smillert
34*7a45d967Smillertname: heredoc-3
35*7a45d967Smillertdescription:
36*7a45d967Smillert	Check that newline isn't needed after heredoc-delimiter marker.
37*7a45d967Smillertstdin: !
38*7a45d967Smillert	cat << EOF
39*7a45d967Smillert	hi
40*7a45d967Smillert	there
41*7a45d967Smillert	EOF
42*7a45d967Smillertexpected-stdout:
43*7a45d967Smillert	hi
44*7a45d967Smillert	there
45*7a45d967Smillert---
46*7a45d967Smillert
47*7a45d967Smillertname: heredoc-4
48*7a45d967Smillertdescription:
49*7a45d967Smillert	Check that an error occurs if the heredoc-delimiter is missing.
50*7a45d967Smillertstdin: !
51*7a45d967Smillert	cat << EOF
52*7a45d967Smillert	hi
53*7a45d967Smillert	there
54*7a45d967Smillertexpected-exit: e > 0
55*7a45d967Smillertexpected-stderr-pattern: /.*/
56*7a45d967Smillert---
57*7a45d967Smillert
58*7a45d967Smillertname: heredoc-5
59*7a45d967Smillertdescription:
60*7a45d967Smillert	Check that backslash quotes a $, ` and \ and kills a \newline
61*7a45d967Smillertstdin:
62*7a45d967Smillert	a=BAD
63*7a45d967Smillert	b=ok
64*7a45d967Smillert	cat << EOF
65*7a45d967Smillert	h\${a}i
66*7a45d967Smillert	h\\${b}i
67*7a45d967Smillert	th\`echo not-run\`ere
68*7a45d967Smillert	th\\`echo is-run`ere
69*7a45d967Smillert	fol\\ks
70*7a45d967Smillert	more\\
71*7a45d967Smillert	last \
72*7a45d967Smillert	line
73*7a45d967Smillert	EOF
74*7a45d967Smillertexpected-stdout:
75*7a45d967Smillert	h${a}i
76*7a45d967Smillert	h\oki
77*7a45d967Smillert	th`echo not-run`ere
78*7a45d967Smillert	th\is-runere
79*7a45d967Smillert	fol\ks
80*7a45d967Smillert	more\
81*7a45d967Smillert	last line
82*7a45d967Smillert---
83*7a45d967Smillert
84*7a45d967Smillertname: heredoc-6
85*7a45d967Smillertdescription:
86*7a45d967Smillert	Check that \newline in initial here-delim word doesn't imply
87*7a45d967Smillert	a quoted here-doc.
88*7a45d967Smillertstdin:
89*7a45d967Smillert	a=i
90*7a45d967Smillert	cat << EO\
91*7a45d967Smillert	F
92*7a45d967Smillert	h$a
93*7a45d967Smillert	there
94*7a45d967Smillert	EOF
95*7a45d967Smillertexpected-stdout:
96*7a45d967Smillert	hi
97*7a45d967Smillert	there
98*7a45d967Smillert---
99*7a45d967Smillert
100*7a45d967Smillertname: heredoc-7
101*7a45d967Smillertdescription:
102*7a45d967Smillert	Check that double quoted $ expressions in here delimiters are
103*7a45d967Smillert	not expanded and match the delimiter.
104*7a45d967Smillert	POSIX says only quote removal is applied to the delimiter.
105*7a45d967Smillertstdin:
106*7a45d967Smillert	a=b
107*7a45d967Smillert	cat << "E$a"
108*7a45d967Smillert	hi
109*7a45d967Smillert	h$a
110*7a45d967Smillert	hb
111*7a45d967Smillert	E$a
112*7a45d967Smillert	echo done
113*7a45d967Smillertexpected-stdout:
114*7a45d967Smillert	hi
115*7a45d967Smillert	h$a
116*7a45d967Smillert	hb
117*7a45d967Smillert	done
118*7a45d967Smillert---
119*7a45d967Smillert
120*7a45d967Smillertname: heredoc-8
121*7a45d967Smillertdescription:
122*7a45d967Smillert	Check that double quoted escaped $ expressions in here
123*7a45d967Smillert	delimiters are not expanded and match the delimiter.
124*7a45d967Smillert	POSIX says only quote removal is applied to the delimiter
125*7a45d967Smillert	(\ counts as a quote).
126*7a45d967Smillertstdin:
127*7a45d967Smillert	a=b
128*7a45d967Smillert	cat << "E\$a"
129*7a45d967Smillert	hi
130*7a45d967Smillert	h$a
131*7a45d967Smillert	h\$a
132*7a45d967Smillert	hb
133*7a45d967Smillert	h\b
134*7a45d967Smillert	E$a
135*7a45d967Smillert	echo done
136*7a45d967Smillertexpected-stdout:
137*7a45d967Smillert	hi
138*7a45d967Smillert	h$a
139*7a45d967Smillert	h\$a
140*7a45d967Smillert	hb
141*7a45d967Smillert	h\b
142*7a45d967Smillert	done
143*7a45d967Smillert---
144*7a45d967Smillert
145*7a45d967Smillertname: heredoc-tmpfile-1
146*7a45d967Smillertdescription:
147*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
148*7a45d967Smillert	Heredoc in simple command.
149*7a45d967Smillertstdin:
150*7a45d967Smillert	TMPDIR=$PWD
151*7a45d967Smillert	eval '
152*7a45d967Smillert		cat <<- EOF
153*7a45d967Smillert		hi
154*7a45d967Smillert		EOF
155*7a45d967Smillert		for i in a b ; do
156*7a45d967Smillert			cat <<- EOF
157*7a45d967Smillert			more
158*7a45d967Smillert			EOF
159*7a45d967Smillert		done
160*7a45d967Smillert	    ' &
161*7a45d967Smillert	sleep 1
162*7a45d967Smillert	echo Left overs: *
163*7a45d967Smillertexpected-stdout:
164*7a45d967Smillert	hi
165*7a45d967Smillert	more
166*7a45d967Smillert	more
167*7a45d967Smillert	Left overs: *
168*7a45d967Smillert---
169*7a45d967Smillert
170*7a45d967Smillertname: heredoc-tmpfile-2
171*7a45d967Smillertdescription:
172*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
173*7a45d967Smillert	Heredoc in function, multiple calls to function.
174*7a45d967Smillertstdin:
175*7a45d967Smillert	TMPDIR=$PWD
176*7a45d967Smillert	eval '
177*7a45d967Smillert		foo() {
178*7a45d967Smillert			cat <<- EOF
179*7a45d967Smillert			hi
180*7a45d967Smillert			EOF
181*7a45d967Smillert		}
182*7a45d967Smillert		foo
183*7a45d967Smillert		foo
184*7a45d967Smillert	    ' &
185*7a45d967Smillert	sleep 1
186*7a45d967Smillert	echo Left overs: *
187*7a45d967Smillertexpected-stdout:
188*7a45d967Smillert	hi
189*7a45d967Smillert	hi
190*7a45d967Smillert	Left overs: *
191*7a45d967Smillert---
192*7a45d967Smillert
193*7a45d967Smillertname: heredoc-tmpfile-3
194*7a45d967Smillertdescription:
195*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
196*7a45d967Smillert	Heredoc in function in loop, multiple calls to function.
197*7a45d967Smillertstdin:
198*7a45d967Smillert	TMPDIR=$PWD
199*7a45d967Smillert	eval '
200*7a45d967Smillert		foo() {
201*7a45d967Smillert			cat <<- EOF
202*7a45d967Smillert			hi
203*7a45d967Smillert			EOF
204*7a45d967Smillert		}
205*7a45d967Smillert		for i in a b; do
206*7a45d967Smillert			foo
207*7a45d967Smillert			foo() {
208*7a45d967Smillert				cat <<- EOF
209*7a45d967Smillert				folks $i
210*7a45d967Smillert				EOF
211*7a45d967Smillert			}
212*7a45d967Smillert		done
213*7a45d967Smillert		foo
214*7a45d967Smillert	    ' &
215*7a45d967Smillert	sleep 1
216*7a45d967Smillert	echo Left overs: *
217*7a45d967Smillertexpected-stdout:
218*7a45d967Smillert	hi
219*7a45d967Smillert	folks b
220*7a45d967Smillert	folks b
221*7a45d967Smillert	Left overs: *
222*7a45d967Smillert---
223*7a45d967Smillert
224*7a45d967Smillertname: heredoc-tmpfile-4
225*7a45d967Smillertdescription:
226*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
227*7a45d967Smillert	Backgrounded simple command with here doc
228*7a45d967Smillertstdin:
229*7a45d967Smillert	TMPDIR=$PWD
230*7a45d967Smillert	eval '
231*7a45d967Smillert		cat <<- EOF &
232*7a45d967Smillert		hi
233*7a45d967Smillert		EOF
234*7a45d967Smillert	    ' &
235*7a45d967Smillert	sleep 1
236*7a45d967Smillert	echo Left overs: *
237*7a45d967Smillertexpected-stdout:
238*7a45d967Smillert	hi
239*7a45d967Smillert	Left overs: *
240*7a45d967Smillert---
241*7a45d967Smillert
242*7a45d967Smillertname: heredoc-tmpfile-5
243*7a45d967Smillertdescription:
244*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
245*7a45d967Smillert	Backgrounded subshell command with here doc
246*7a45d967Smillertstdin:
247*7a45d967Smillert	TMPDIR=$PWD
248*7a45d967Smillert	eval '
249*7a45d967Smillert	      (
250*7a45d967Smillert		sleep 1	# so parent exits
251*7a45d967Smillert		echo A
252*7a45d967Smillert		cat <<- EOF
253*7a45d967Smillert		hi
254*7a45d967Smillert		EOF
255*7a45d967Smillert		echo B
256*7a45d967Smillert	      ) &
257*7a45d967Smillert	    ' &
258*7a45d967Smillert	sleep 2
259*7a45d967Smillert	echo Left overs: *
260*7a45d967Smillertexpected-stdout:
261*7a45d967Smillert	A
262*7a45d967Smillert	hi
263*7a45d967Smillert	B
264*7a45d967Smillert	Left overs: *
265*7a45d967Smillert---
266*7a45d967Smillert
267*7a45d967Smillertname: heredoc-tmpfile-6
268*7a45d967Smillertdescription:
269*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
270*7a45d967Smillert	Heredoc in pipeline.
271*7a45d967Smillertstdin:
272*7a45d967Smillert	TMPDIR=$PWD
273*7a45d967Smillert	eval '
274*7a45d967Smillert		cat <<- EOF | sed "s/hi/HI/"
275*7a45d967Smillert		hi
276*7a45d967Smillert		EOF
277*7a45d967Smillert	    ' &
278*7a45d967Smillert	sleep 1
279*7a45d967Smillert	echo Left overs: *
280*7a45d967Smillertexpected-stdout:
281*7a45d967Smillert	HI
282*7a45d967Smillert	Left overs: *
283*7a45d967Smillert---
284*7a45d967Smillert
285*7a45d967Smillertname: heredoc-tmpfile-7
286*7a45d967Smillertdescription:
287*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
288*7a45d967Smillert	Heredoc in backgrounded pipeline.
289*7a45d967Smillertstdin:
290*7a45d967Smillert	TMPDIR=$PWD
291*7a45d967Smillert	eval '
292*7a45d967Smillert		cat <<- EOF | sed 's/hi/HI/' &
293*7a45d967Smillert		hi
294*7a45d967Smillert		EOF
295*7a45d967Smillert	    ' &
296*7a45d967Smillert	sleep 1
297*7a45d967Smillert	echo Left overs: *
298*7a45d967Smillertexpected-stdout:
299*7a45d967Smillert	HI
300*7a45d967Smillert	Left overs: *
301*7a45d967Smillert---
302*7a45d967Smillert
303*7a45d967Smillertname: heredoc-tmpfile-8
304*7a45d967Smillertdescription:
305*7a45d967Smillert	Check that heredoc temp files aren't removed too soon or too late.
306*7a45d967Smillert	Heredoc in function, backgrounded call to function.
307*7a45d967Smillertstdin:
308*7a45d967Smillert	TMPDIR=$PWD
309*7a45d967Smillert	# Background eval so main shell doesn't do parsing
310*7a45d967Smillert	eval '
311*7a45d967Smillert		foo() {
312*7a45d967Smillert			cat <<- EOF
313*7a45d967Smillert			hi
314*7a45d967Smillert			EOF
315*7a45d967Smillert		}
316*7a45d967Smillert		foo
317*7a45d967Smillert		# sleep so eval can die
318*7a45d967Smillert		(sleep 1; foo) &
319*7a45d967Smillert		(sleep 1; foo) &
320*7a45d967Smillert		foo
321*7a45d967Smillert	    ' &
322*7a45d967Smillert	sleep 2
323*7a45d967Smillert	echo Left overs: *
324*7a45d967Smillertexpected-stdout:
325*7a45d967Smillert	hi
326*7a45d967Smillert	hi
327*7a45d967Smillert	hi
328*7a45d967Smillert	hi
329*7a45d967Smillert	Left overs: *
330*7a45d967Smillert---
331*7a45d967Smillert
332