1# redir-y.tst: yash-specific test of redirections
2
3exec 3>&- 4>&- 5>&- 6>&- 7>&- 8>&- 9>&-
4
5echo in0 >in0
6echo in1 >in1
7echo 'in*' >'in*'
8
9echo PS1= >yashrc
10
11test_oE -e 0 \
12    'pathname expansion in redirection operand (non-POSIX, interactive), success'\
13    -i +m --rcfile=yashrc
14cat <i*0
15cat <"i"'n'\*
16HOME='in*'
17cat <~ # result of tilde expansion is not subject to pathname expansion
18__IN__
19in0
20in*
21in*
22__OUT__
23
24test_O -d -e 2 \
25    'pathname expansion in redirection operand (non-POSIX, interactive), failure' \
26    -i +m --rcfile=yashrc
27cat <in* # more than one matching pathname
28__IN__
29
30(
31posix="true"
32export ENV=yashrc
33
34test_oE -e 0 'pathname expansion in redirection operand (POSIX, interactive)' \
35    -i +m
36cat <i*0
37cat <in* # multiple matches: the pattern is left intact
38__IN__
39in0
40in*
41__OUT__
42
43)
44
45test_o -e 0 'redirection to special built-in affects current environment'
46unset x
47: < ${x=/dev/null}
48echo $x
49__IN__
50/dev/null
51__OUT__
52
53test_o -e 0 'redirection to external command affects current environment'
54unset x
55"$TESTEE" -c : < ${x=/dev/null}
56echo $x
57__IN__
58/dev/null
59__OUT__
60
61test_o -e 0 'redirection to grouping affects current environment'
62unset x
63{ :; } < ${x=/dev/null}
64echo $x
65__IN__
66/dev/null
67__OUT__
68
69test_o -e 0 'redirection to subshell affects current environment'
70unset x
71(:) < ${x=/dev/null}
72echo $x
73__IN__
74/dev/null
75__OUT__
76
77test_OE -e 0 'input duplication of unwritable file descriptor'
783>/dev/null <&3
79__IN__
80
81test_OE -e 0 'output duplication of unreadable file descriptor'
823</dev/null >&3
83__IN__
84
85test_oE -e 0 'tilde expansion not performed in here-document operand'
86HOME=/home
87cat <<~
88/home
89~
90__IN__
91/home
92__OUT__
93
94test_oE -e 0 'parameter expansion not performed in here-document operand'
95foo=FOO
96cat <<$foo
97FOO
98$foo
99__IN__
100FOO
101__OUT__
102
103test_oE -e 0 'command substitution not performed in here-document operand'
104cat <<$(echo foo)`echo bar`
105foobar
106$(echo foo)`echo bar`
107__IN__
108foobar
109__OUT__
110
111test_oE -e 0 'arithmetic expansion not performed in here-document operand'
112cat <<$((1+1))
113foo
114$((1+1))
115__IN__
116foo
117__OUT__
118
119test_oE -e 0 'complex expansion in here-document' -s 1 '"  "' 3
120IFS=-
121printf '[%s]' ${1+"$@"}; echo
122printf '[%s]' "${1+"$@"}"; echo
123cat <<END
124[$*]
125[$@]
126[${1+"$*"}]
127[${1+"$@"}]
128END
129
130echo =====
131
132IFS==
133cat <<END
134[$*]
135[$@]
136[${1+"$*"}]
137[${1+"$@"}]
138END
139__IN__
140[1]["  "][3]
141[1]["  "][3]
142[1-"  "-3]
143[1-"  "-3]
144[1-"  "-3]
145[1-"  "-3]
146=====
147[1="  "=3]
148[1="  "=3]
149[1="  "=3]
150[1="  "=3]
151__OUT__
152
153test_oE -e 0 'complex expansion with backslashes in here-document' -s 1 '\' 3
154IFS='\'
155cat <<END
156[$*]
157[$@]
158[${1+"$*"}]
159[${1+"$@"}]
160END
161__IN__
162[1\\\3]
163[1\\\3]
164[1\\\3]
165[1\\\3]
166__OUT__
167
168test_oE -e 0 'line-continued end-of-here-document indicator (unquoted)' -e
169cat <<echo
170ec\
171ho
172echo
173__IN__
174echo
175__OUT__
176# See also the test 'no quote removal with quoted here-document delimiter'
177# in redir-p.tst
178
179# This is not required by POSIX, but many other shells behave this way.
180test_oE -e 0 'end-of-here-document indicator just before EOF (unquoted)' \
181    -c 'cat <<END
182foo
183END'
184__IN__
185foo
186__OUT__
187
188test_oE -e 0 'end-of-here-document indicator just before EOF (quoted)' \
189    -c 'cat <<\END
190foo
191END'
192__IN__
193foo
194__OUT__
195
196test_oE -e 0 'here-document is expanded in current shell' -e
197unset a
198cat <<END
199${a=foo}
200END
201echo $a
202__IN__
203foo
204foo
205__OUT__
206
207test_oE -e 0 'duplicating input to the same file descriptor'
208echo foo | cat <&0
209__IN__
210foo
211__OUT__
212
213test_oE -e 0 'duplicating output to the same file descriptor'
214echo foo >&1
215__IN__
216foo
217__OUT__
218
219# Test various file descriptor combinations to ensure the shell correctly
220# "dup"s the pipe file descriptors after opening a pipe.
221test_oE 'pipe redirection'
222exec 4>>|3; echo 4-3 >&4; exec 4>&-; cat <&3; exec 3<&-
223exec 4>>|6; echo 4-6 >&4; exec 4>&-; cat <&6; exec 6<&-
224exec 5>>|3; echo 5-3 >&5; exec 5>&-; cat <&3; exec 3<&-
225exec 5>>|6; echo 5-6 >&5; exec 5>&-; cat <&6; exec 6<&-
226exec 5>>|4; echo 5-4 >&5; exec 5>&-; cat <&4; exec 4<&-
227exec 3>>|6; echo 3-6 >&3; exec 3>&-; cat <&6; exec 6<&-
228exec 3>>|4; echo 3-4 >&3; exec 3>&-; cat <&4; exec 4<&-
229exec 9>&1
230exec  >>|0; echo 1-0    ; exec  >&9; cat <&0;
231__IN__
2324-3
2334-6
2345-3
2355-6
2365-4
2373-6
2383-4
2391-0
240__OUT__
241
242test_oE 'using pipe redirection in subshell'
243exec 3>&1
244{
245    while read -r i && [ "$i" -lt 5 ]; do
246	echo "$i" >&3
247	echo "$((i+1))"
248    done | {
249	echo 0
250	cat -u
251    }
252} >>|0
253__IN__
2540
2551
2562
2573
2584
259__OUT__
260
261test_oE -e 0 'input process redirection' -e
262cat <(echo foo)
263cat <(echo bar)-
264__IN__
265foo
266bar
267__OUT__
268
269test_oE -e 0 'output process redirection' -e
270echo >(read i && echo $((i+1)))99 | cat
271# "cat" ensures the output is flushed before the shell exits
272__IN__
273100
274__OUT__
275
276test_oE -e 0 'process redirection is run in subshell' -e
277i=0
278echo >(read i && echo $((i+1))) 99 | cat
279# "cat" ensures the output is flushed before reaching this line
280echo $i
281__IN__
282100
2830
284__OUT__
285
286test_x -e 0 'exit status of process redirection is ignored'
287<(false) >(false)
288__IN__
289
290test_oE -e 0 'empty here-string'
291cat <<<""
292__IN__
293
294__OUT__
295
296test_oE -e 0 'tilde expansion in here-string'
297HOME=/home
298cat <<<~/foo
299__IN__
300/home/foo
301__OUT__
302
303test_oE -e 0 'double quotation and parameter expansion in here-string'
304foo=FOOOO
305cat <<<"${foo%OO}"
306__IN__
307FOO
308__OUT__
309
310test_oE -e 0 'multi-line here-string'
311cat <<<'1
3122'
313__IN__
3141
3152
316__OUT__
317
318test_oE -e 0 'space after here-string operator'
319cat <<< foo
320__IN__
321foo
322__OUT__
323
324test_oE -e 0 'here-string with non-default file descriptor'
325cat 3<<<foo <&3
326__IN__
327foo
328__OUT__
329
330test_OE -e 0 'IO_NUMBER can be redirection operand'
331> 1> 2< 2>>3 <<4
3324
333__IN__
334
335(
336posix="true"
337
338test_Oe -e 2 'IO_NUMBER as redirection operand (filename)'
339# Here the token "12" is an IO_NUMBER token for the second redirection, and
340# hence cannot be the operand of the first.
341> 12> 34
342__IN__
343syntax error: put a space between `2' and `>' for disambiguation
344__ERR__
345#'
346#`
347#'
348#`
349
350test_Oe -e 2 'IO_NUMBER as redirection operand (here-document)'
351# Here the token "01" is an IO_NUMBER token for the second redirection, and
352# hence cannot be the operand of the first.
353<< 01<< 23
35401
35523
356__IN__
357syntax error: put a space between `1' and `<' for disambiguation
358syntax error: here-document content for <<01 is missing
359syntax error: here-document content for <<23 is missing
360__ERR__
361#'
362#`
363#'
364#`
365)
366
367test_Oe -e 2 'missing target for <'
368<
369__IN__
370syntax error: the redirection target is missing
371__ERR__
372
373test_Oe -e 2 'missing target for <>'
374<>
375__IN__
376syntax error: the redirection target is missing
377__ERR__
378
379test_Oe -e 2 'missing target for <& (EOF)'
380<&
381__IN__
382syntax error: the redirection target is missing
383__ERR__
384
385test_Oe -e 2 'missing target for <& (line continuation and comment)'
386<&\
387    #
388__IN__
389syntax error: the redirection target is missing
390__ERR__
391
392test_Oe -e 2 'missing target for >'
393>
394__IN__
395syntax error: the redirection target is missing
396__ERR__
397
398test_Oe -e 2 'missing target for >|'
399>|
400__IN__
401syntax error: the redirection target is missing
402__ERR__
403
404test_Oe -e 2 'missing target for >&'
405>&
406__IN__
407syntax error: the redirection target is missing
408__ERR__
409
410test_Oe -e 2 'missing target for >>'
411>>
412__IN__
413syntax error: the redirection target is missing
414__ERR__
415
416test_Oe -e 2 'missing target for >>|'
417>>|
418__IN__
419syntax error: the redirection target is missing
420__ERR__
421
422test_Oe -e 2 'missing target for <<<'
423<<<
424__IN__
425syntax error: the redirection target is missing
426__ERR__
427
428test_Oe -e 2 'missing delimiter after <<'
429<<
430__IN__
431syntax error: the end-of-here-document indicator is missing
432__ERR__
433
434test_Oe -e 2 'missing delimiter after <<-'
435<<-
436__IN__
437syntax error: the end-of-here-document indicator is missing
438__ERR__
439
440test_Oe -e 2 'newline in end-of-here-document indicator'
441<<'
442'
443__IN__
444syntax error: the end-of-here-document indicator contains a newline
445__ERR__
446
447test_Oe -e 2 'unclosed here-document (unquoted)'
448cat <<END
449foo
450__IN__
451syntax error: the here-document content is not closed by `END'
452__ERR__
453#'
454#`
455: <<END
456END
457
458test_Oe -e 2 'missing newline and here-document delimiter (unquoted)' \
459    -c 'cat <<END'
460__IN__
461yash -c:1: syntax error: here-document content for <<END is missing
462__ERR__
463: <<END
464END
465
466test_Oe -e 2 'unclosed here-document (quoted)'
467cat <<\END
468foo
469__IN__
470syntax error: the here-document content is not closed by `END'
471__ERR__
472: <<END
473END
474
475test_Oe -e 2 'missing newline and here-document delimiter (quoted)' \
476    -c 'cat <<\END'
477__IN__
478yash -c:1: syntax error: here-document content for <<\END is missing
479__ERR__
480: <<END
481END
482
483test_O -d -e 2 'space between < and ( in process redirection'
484< (:)
485__IN__
486
487test_Oe -e 2 'unclosed input process redirection'
488echo not printed <(
489__IN__
490syntax error: unclosed process redirection
491__ERR__
492
493test_Oe -e 2 'unclosed output process redirection'
494echo not printed >(
495__IN__
496syntax error: unclosed process redirection
497__ERR__
498
499(
500posix="true"
501
502test_Oe -e 2 'no process redirection <() in POSIX mode'
503<()
504__IN__
505syntax error: process redirection is not supported in the POSIXly-correct mode
506__ERR__
507
508test_Oe -e 2 'no process redirection >() in POSIX mode'
509>()
510__IN__
511syntax error: process redirection is not supported in the POSIXly-correct mode
512__ERR__
513
514test_Oe -e 2 'no pipe redirection in POSIX mode'
515>>|0
516__IN__
517syntax error: pipe redirection is not supported in the POSIXly-correct mode
518__ERR__
519
520test_Oe -e 2 'no here-string in POSIX mode'
521<<<foo
522__IN__
523syntax error: here-string is not supported in the POSIXly-correct mode
524__ERR__
525
526test_Oe -e 2 'keyword after redirection in compound command (POSIX)'
527{ { echo not printed; } >/dev/null }
528__IN__
529syntax error: unexpected word after redirection
530syntax error: (maybe you missed `;'?)
531__ERR__
532#'
533#`
534#)
535
536test_Oe -e 2 'keyword after redirection on subshell (POSIX)'
537{ (echo not printed) >/dev/null }
538__IN__
539syntax error: unexpected word after redirection
540syntax error: (maybe you missed `;'?)
541__ERR__
542#'
543#`
544#)
545
546# Some relevant tests in grouping-y.tst.
547
548)
549
550test_OE -e 0 'keyword after redirection in compound command (non-POSIX)'
551{ { echo not printed; } >/dev/null }
552{ ( echo not printed  ) >/dev/null }
553__IN__
554
555# vim: set ft=sh ts=8 sts=4 sw=4 noet:
556