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