xref: /aosp_15_r20/external/one-true-awk/testdir/T.misc (revision 9a7741de182b2776d7b30d6355f2585c0780a51b)
1*9a7741deSElliott Hughes#!/bin/sh
2*9a7741deSElliott Hughes
3*9a7741deSElliott Hughesecho T.misc: miscellaneous buglets now watched for
4*9a7741deSElliott Hughes
5*9a7741deSElliott Hughesawk=${awk-../a.out}
6*9a7741deSElliott Hughes
7*9a7741deSElliott Hughesrm -f core
8*9a7741deSElliott Hughes
9*9a7741deSElliott Hughesecho 'The big brown over the lazy doe
10*9a7741deSElliott HughesThe big brown over the lazy dog
11*9a7741deSElliott Hughesx
12*9a7741deSElliott HughesThe big brown over the lazy dog' >foo
13*9a7741deSElliott Hughesecho 'failed
14*9a7741deSElliott Hughessucceeded
15*9a7741deSElliott Hughesfailed
16*9a7741deSElliott Hughessucceeded' >foo1
17*9a7741deSElliott Hughes$awk '{ if (match($0, /^The big brown over the lazy dog/) == 0) {
18*9a7741deSElliott Hughes		printf("failed\n")
19*9a7741deSElliott Hughes	} else {
20*9a7741deSElliott Hughes		printf("succeeded\n")
21*9a7741deSElliott Hughes	}
22*9a7741deSElliott Hughes} ' foo >foo2
23*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc ghosh RE bug'
24*9a7741deSElliott Hughes
25*9a7741deSElliott Hughesecho '123
26*9a7741deSElliott Hughes1234567890
27*9a7741deSElliott Hughes12345678901' >foo
28*9a7741deSElliott Hughesecho '12345678901' >foo1
29*9a7741deSElliott Hughes$awk 'length($0) > 10' foo >foo2
30*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc last number bug'
31*9a7741deSElliott Hughes
32*9a7741deSElliott Hughes# check some \ sequences in strings (ascii)
33*9a7741deSElliott Hughesecho HIJKL >foo1
34*9a7741deSElliott Hughesecho foo | $awk '{ print "H\x49\x4a\x4BL" }' >foo2
35*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc hex string cvt'
36*9a7741deSElliott Hughes
37*9a7741deSElliott Hughesecho 012x45 >foo1
38*9a7741deSElliott Hughes$awk 'BEGIN { print "0\061\62x\0645" }' >foo2
39*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc oct string cvt'
40*9a7741deSElliott Hughes
41*9a7741deSElliott Hughes# $i++ means ($i)++
42*9a7741deSElliott Hughesecho 3 5 | $awk '{ i = 1; print $i++ ; print $1, i }' >foo1
43*9a7741deSElliott Hughesecho '3
44*9a7741deSElliott Hughes4 1' >foo2
45*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc bad field increment'
46*9a7741deSElliott Hughes
47*9a7741deSElliott Hughes# makes sure that fields are recomputed even if self-assignment
48*9a7741deSElliott Hughes# take into account that subtracting from NF now rebuilds the record
49*9a7741deSElliott Hughesecho 'a b c
50*9a7741deSElliott Hughess p q r
51*9a7741deSElliott Hughesx y z' >foo
52*9a7741deSElliott Hughesecho 'a
53*9a7741deSElliott Hughess p
54*9a7741deSElliott Hughesx' >foo1
55*9a7741deSElliott Hughes$awk '{ NF -= 2; $1 = $1; print }' <foo >foo2
56*9a7741deSElliott Hughesdiff foo1 foo2 || echo 1>&2 "BAD: T.misc bad field self-assignment"
57*9a7741deSElliott Hughes
58*9a7741deSElliott Hughesecho '1
59*9a7741deSElliott Hughes1' >foo1
60*9a7741deSElliott Hughes$awk 'BEGIN {x = 1; print x; x = x; print x}' >foo2
61*9a7741deSElliott Hughesdiff foo1 foo2 || echo 1>&2 "BAD: T.misc bad self-assignment"
62*9a7741deSElliott Hughes
63*9a7741deSElliott Hughesecho 573109312 | $awk '{print $1*4}' >foo1
64*9a7741deSElliott Hughesecho 2292437248 >foo2
65*9a7741deSElliott Hughesdiff foo1 foo2 || echo 1>&2 "BAD: T.misc bad overflow"
66*9a7741deSElliott Hughes
67*9a7741deSElliott Hughes# note that there are 8-bit characters in the echo
68*9a7741deSElliott Hughes# some shells will probably screw this up.
69*9a7741deSElliott Hughesecho '#
70*9a7741deSElliott Hughescode € 1
71*9a7741deSElliott Hughescode € 2' |
72*9a7741deSElliott Hughes$awk '/^#/' >foo1
73*9a7741deSElliott Hughesecho '#' >foo2
74*9a7741deSElliott Hughesdiff foo1 foo2 || echo 1>&2 "BAD: T.misc bad match of 8-bit char"
75*9a7741deSElliott Hughes
76*9a7741deSElliott Hughesecho hello |
77*9a7741deSElliott Hughes$awk 'BEGIN	{ FILENAME = "/etc/passwd" }
78*9a7741deSElliott Hughes	{ print $0 }' >/dev/null
79*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc /etc/passwd dropped core"; fi
80*9a7741deSElliott Hughes
81*9a7741deSElliott Hughesecho hello |
82*9a7741deSElliott Hughes$awk '  function foo(foo) {
83*9a7741deSElliott Hughes                foo = 1
84*9a7741deSElliott Hughes                foo()
85*9a7741deSElliott Hughes        }
86*9a7741deSElliott Hughes	{ foo(bar) }
87*9a7741deSElliott Hughes' >/dev/null 2>&1
88*9a7741deSElliott Hughesif test -r core; then
89*9a7741deSElliott Hughes	echo 1>&2 "BAD: T.misc function foo(foo) dropped core"
90*9a7741deSElliott Hughes	rm -f core
91*9a7741deSElliott Hughesfi
92*9a7741deSElliott Hughes
93*9a7741deSElliott Hughesecho '2
94*9a7741deSElliott Hughes10' |
95*9a7741deSElliott Hughes$awk '{ x[NR] = $0 }	# test whether $0 is NUM as well as STR
96*9a7741deSElliott HughesEND { if (x[1] > x[2]) print "BAD: T.misc: $0 is not NUM" }'
97*9a7741deSElliott Hughes
98*9a7741deSElliott Hughes
99*9a7741deSElliott Hughes$awk 'BEGIN {
100*9a7741deSElliott Hughes	npad = substr("alexander" "           ",1,15)
101*9a7741deSElliott Hughes	print npad
102*9a7741deSElliott Hughes	}' >foo
103*9a7741deSElliott Hughesgrep '\\' foo && echo 1>&2 "BAD: T.misc alexander fails"
104*9a7741deSElliott Hughes
105*9a7741deSElliott Hughes# This should give an error about function arguments
106*9a7741deSElliott Hughes$awk '
107*9a7741deSElliott Hughesfunction foo(x) { print "x is" x }
108*9a7741deSElliott HughesBEGIN { foo(foo) }
109*9a7741deSElliott Hughes' 2>foo
110*9a7741deSElliott Hughesgrep "can't use function foo" foo >/dev/null || echo 1>&2 "BAD: T.misc fcn args"
111*9a7741deSElliott Hughes
112*9a7741deSElliott Hughes
113*9a7741deSElliott Hughes# gawk defref test; should give error about undefined function
114*9a7741deSElliott Hughes$awk 'BEGIN { foo() }' 2>foo
115*9a7741deSElliott Hughesgrep "calling undefined function foo" foo >/dev/null || echo 1>&2 "BAD: T.misc undefined function"
116*9a7741deSElliott Hughes
117*9a7741deSElliott Hughes
118*9a7741deSElliott Hughes# gawk arrayparm test; should give error about function
119*9a7741deSElliott Hughes$awk '
120*9a7741deSElliott HughesBEGIN {
121*9a7741deSElliott Hughes    foo[1]=1;
122*9a7741deSElliott Hughes    foo[2]=2;
123*9a7741deSElliott Hughes    bug1(foo);
124*9a7741deSElliott Hughes}
125*9a7741deSElliott Hughesfunction bug1(i) {
126*9a7741deSElliott Hughes    for (i in foo) {
127*9a7741deSElliott Hughes	bug2(i);
128*9a7741deSElliott Hughes	delete foo[i];
129*9a7741deSElliott Hughes	print i,1,bot[1];
130*9a7741deSElliott Hughes    }
131*9a7741deSElliott Hughes}
132*9a7741deSElliott Hughesfunction bug2(arg) {
133*9a7741deSElliott Hughes    bot[arg]=arg;
134*9a7741deSElliott Hughes}
135*9a7741deSElliott Hughes' 2>foo
136*9a7741deSElliott Hughesgrep "can.t assign to foo" foo >/dev/null || echo 1>&2 "BAD: T.misc foo bug"
137*9a7741deSElliott Hughes
138*9a7741deSElliott Hughes
139*9a7741deSElliott Hughes# This should be a syntax error
140*9a7741deSElliott Hughes$awk '
141*9a7741deSElliott Hughes!x = y
142*9a7741deSElliott Hughes' 2>foo
143*9a7741deSElliott Hughesgrep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error !x=y fails"
144*9a7741deSElliott Hughes
145*9a7741deSElliott Hughes# This should print bbb
146*9a7741deSElliott Hughes$awk '
147*9a7741deSElliott HughesBEGIN { up[1] = "a"
148*9a7741deSElliott Hughes	for (i in up) gsub("a", "A", x)
149*9a7741deSElliott Hughes	print x x "bbb"
150*9a7741deSElliott Hughes	exit
151*9a7741deSElliott Hughes      }
152*9a7741deSElliott Hughes' >foo
153*9a7741deSElliott Hughesgrep bbb foo >/dev/null || echo 1>&2 "BAD: T.misc gsub failed"
154*9a7741deSElliott Hughes
155*9a7741deSElliott Hughesecho yes |
156*9a7741deSElliott Hughes$awk '
157*9a7741deSElliott HughesBEGIN {
158*9a7741deSElliott Hughes	printf "push return" >"/dev/null"
159*9a7741deSElliott Hughes	getline ans <"/dev/null"
160*9a7741deSElliott Hughes} '
161*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc getline ans dropped core"; fi
162*9a7741deSElliott Hughes
163*9a7741deSElliott Hughes$awk 'BEGIN { unireghf() }
164*9a7741deSElliott Hughesfunction unireghf(hfeed) { hfeed[1] = 0 }'
165*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc unireghf dropped core"; fi
166*9a7741deSElliott Hughes
167*9a7741deSElliott Hughesecho x | $awk '/[/]/' 2>foo
168*9a7741deSElliott Hughesgrep 'nonterminated character class' foo >/dev/null || error 'BAD: T.misc nonterminated fails'
169*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc nonterminated dropped core"; fi
170*9a7741deSElliott Hughes
171*9a7741deSElliott Hughes$awk '
172*9a7741deSElliott Hughesfunction f() { return 12345 }
173*9a7741deSElliott HughesBEGIN { printf "<%s>\n", f() }
174*9a7741deSElliott Hughes' >foo
175*9a7741deSElliott Hughesgrep '<12345>' foo >/dev/null || echo 'BAD: T.misc <12345> fails'
176*9a7741deSElliott Hughes
177*9a7741deSElliott Hughesecho 'abc
178*9a7741deSElliott Hughesdef
179*9a7741deSElliott Hughes
180*9a7741deSElliott Hughesghi
181*9a7741deSElliott Hughesjkl' >foo
182*9a7741deSElliott Hughes$awk '
183*9a7741deSElliott HughesBEGIN {	RS = ""
184*9a7741deSElliott Hughes	while (getline <"foo")
185*9a7741deSElliott Hughes		print
186*9a7741deSElliott Hughes}' >foo1
187*9a7741deSElliott Hughes$awk 'END {print NR}' foo1 | grep 4 >/dev/null || echo 'BAD: T.misc abcdef fails'
188*9a7741deSElliott Hughes
189*9a7741deSElliott Hughes# Test for RS regex matching an empty record at EOF
190*9a7741deSElliott Hughesecho a | $awk 1 RS='a\n' > foo1
191*9a7741deSElliott Hughescat << \EOF > foo2
192*9a7741deSElliott Hughes
193*9a7741deSElliott HughesEOF
194*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.misc RS regex matching an empty record at EOF fails'
195*9a7741deSElliott Hughes
196*9a7741deSElliott Hughes# Test for RS regex being reapplied
197*9a7741deSElliott Hughesecho aaa1a2a | $awk 1 RS='^a' >foo1
198*9a7741deSElliott Hughescat << \EOF > foo2
199*9a7741deSElliott Hughes
200*9a7741deSElliott Hughesaa1a2a
201*9a7741deSElliott Hughes
202*9a7741deSElliott HughesEOF
203*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.misc ^regex reapplied fails'
204*9a7741deSElliott Hughes
205*9a7741deSElliott Hughes# ^-anchored RS matching should be active at the start of each input file
206*9a7741deSElliott Hughestee foo1 foo2 >foo3 << \EOF
207*9a7741deSElliott Hughesaaa
208*9a7741deSElliott HughesEOF
209*9a7741deSElliott Hughes$awk 1 RS='^a' foo1 foo2 foo3 >foo4
210*9a7741deSElliott Hughescat << \EOF > foo5
211*9a7741deSElliott Hughes
212*9a7741deSElliott Hughesaa
213*9a7741deSElliott Hughes
214*9a7741deSElliott Hughes
215*9a7741deSElliott Hughesaa
216*9a7741deSElliott Hughes
217*9a7741deSElliott Hughes
218*9a7741deSElliott Hughesaa
219*9a7741deSElliott Hughes
220*9a7741deSElliott HughesEOF
221*9a7741deSElliott Hughesdiff foo4 foo5 || echo 'BAD: T.misc ^RS matches the start of every input file fails'
222*9a7741deSElliott Hughes
223*9a7741deSElliott Hughes# The following should not produce a warning about changing a constant
224*9a7741deSElliott Hughes# nor about a curdled tempcell list
225*9a7741deSElliott Hughes$awk 'function f(x) { x = 2 }
226*9a7741deSElliott HughesBEGIN { f(1) }' >foo
227*9a7741deSElliott Hughesgrep '^' foo && echo 'BAD: test constant change fails'
228*9a7741deSElliott Hughes
229*9a7741deSElliott Hughes# The following should not produce a warning about a curdled tempcell list
230*9a7741deSElliott Hughes$awk 'function f(x) { x }
231*9a7741deSElliott HughesBEGIN { f(1) }' >foo
232*9a7741deSElliott Hughesgrep '^' foo && echo 'BAD: test tempcell list fails'
233*9a7741deSElliott Hughes
234*9a7741deSElliott Hughes$awk 'BEGIN { print 9, a=10, 11; print a; exit }' >foo1
235*9a7741deSElliott Hughesecho '9 10 11
236*9a7741deSElliott Hughes10' >foo2
237*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.misc (embedded expression)'
238*9a7741deSElliott Hughes
239*9a7741deSElliott Hughesecho "abc defgh ijkl" | $awk '
240*9a7741deSElliott Hughes  { $1 = ""; line = $0; print line; print $0; $0 = line; print $0 }' >foo1
241*9a7741deSElliott Hughesecho " defgh ijkl
242*9a7741deSElliott Hughes defgh ijkl
243*9a7741deSElliott Hughes defgh ijkl" >foo2
244*9a7741deSElliott Hughesdiff foo1 foo2 || echo 'BAD: T.misc (assignment to $0)'
245*9a7741deSElliott Hughes
246*9a7741deSElliott Hughes$awk '
247*9a7741deSElliott Hughesfunction min(a, b)
248*9a7741deSElliott Hughes{
249*9a7741deSElliott Hughes	if (a < b)
250*9a7741deSElliott Hughes		return a
251*9a7741deSElliott Hughes	else
252*9a7741deSElliott Hughes		return b
253*9a7741deSElliott Hughes}
254*9a7741deSElliott HughesBEGIN { exit }
255*9a7741deSElliott Hughes'
256*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc function min dropped core"; fi
257*9a7741deSElliott Hughes
258*9a7741deSElliott Hughes# The following should not give a syntax error message:
259*9a7741deSElliott Hughes$awk '
260*9a7741deSElliott Hughesfunction expand(chart) {
261*9a7741deSElliott Hughes	getline chart < "CHAR.ticks"
262*9a7741deSElliott Hughes}
263*9a7741deSElliott Hughes' >foo
264*9a7741deSElliott Hughesgrep '^' foo >/dev/null && echo 'BAD: T.misc expand error'
265*9a7741deSElliott Hughes
266*9a7741deSElliott Hughes$awk 'BEGIN { print 1e40 }' >/dev/null
267*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc 1E40 dropped core"; fi
268*9a7741deSElliott Hughes
269*9a7741deSElliott Hughes# The following syntax error should not dump core:
270*9a7741deSElliott Hughes$awk '
271*9a7741deSElliott Hughes$NF==3	{first=1}
272*9a7741deSElliott Hughes$NF==2 && first==0 && (abs($1-o1)>120||abs($2-o2)>120)	{print $0}
273*9a7741deSElliott Hughes$NF==2	{o1=%1; o2=$2; first=0}
274*9a7741deSElliott Hughes' 2>/dev/null
275*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc first/abs dropped core"; fi
276*9a7741deSElliott Hughes
277*9a7741deSElliott Hughes# The following syntax error should not dump core:
278*9a7741deSElliott Hughes$awk '{ n = split($1, address, !); print address[1] }' 2>foo
279*9a7741deSElliott Hughesgrep 'illegal statement' foo >/dev/null || echo 'BAD: T.misc split error'
280*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc split! dropped core"; fi
281*9a7741deSElliott Hughes
282*9a7741deSElliott Hughes# The following should cause a syntax error message
283*9a7741deSElliott Hughes$awk 'BEGIN {"hello"}' 2>foo
284*9a7741deSElliott Hughesgrep 'illegal statement' foo >/dev/null || echo 'BAD: T.misc hello error'
285*9a7741deSElliott Hughes
286*9a7741deSElliott Hughes# The following should give a syntax error message:
287*9a7741deSElliott Hughes$awk '
288*9a7741deSElliott Hughesfunction pile(c,     r) {
289*9a7741deSElliott Hughes	r = ++pile[c]
290*9a7741deSElliott Hughes}
291*9a7741deSElliott Hughes
292*9a7741deSElliott Hughes{ pile($1) }
293*9a7741deSElliott Hughes' 2>foo
294*9a7741deSElliott Hughesgrep 'context is' foo >/dev/null || echo 'BAD: T.misc pile error'
295*9a7741deSElliott Hughes
296*9a7741deSElliott Hughes# This should complain about missing atan2 argument:
297*9a7741deSElliott Hughes$awk 'BEGIN { atan2(1) }' 2>foo
298*9a7741deSElliott Hughesgrep 'requires two arg' foo >/dev/null || echo 'BAD: T.misc atan2 error'
299*9a7741deSElliott Hughes
300*9a7741deSElliott Hughes# This should not core dump:
301*9a7741deSElliott Hughes$awk 'BEGIN { f() }
302*9a7741deSElliott Hughesfunction f(A) { delete A[1] }
303*9a7741deSElliott Hughes'
304*9a7741deSElliott Hughesif test -r core; then echo 1>&2 "BAD: T.misc delete dropped core"; fi
305*9a7741deSElliott Hughes
306*9a7741deSElliott Hughes# nasty one:  should not be able to overwrite constants
307*9a7741deSElliott Hughes$awk 'BEGIN { gsub(/ana/,"anda","banana")
308*9a7741deSElliott Hughes		printf "the monkey ate a %s\n", "banana" }
309*9a7741deSElliott Hughes' >/dev/null 2>foo
310*9a7741deSElliott Hughesgrep 'syntax error' foo >/dev/null || echo 'BAD: T.misc gsub banana error'
311*9a7741deSElliott Hughes
312*9a7741deSElliott Hughes# nasty one:  should not be able to overwrite constants
313*9a7741deSElliott Hughes$awk 'BEGIN { sub(/ana/,"anda","banana")
314*9a7741deSElliott Hughes		printf "the monkey ate a %s\n", "banana" }
315*9a7741deSElliott Hughes' >/dev/null 2>foo
316*9a7741deSElliott Hughesgrep 'syntax error' foo >/dev/null || echo 'BAD: T.misc sub banana error'
317*9a7741deSElliott Hughes
318*9a7741deSElliott Hughes# line numbers used to double-count comments
319*9a7741deSElliott Hughes$awk '#
320*9a7741deSElliott Hughes#
321*9a7741deSElliott Hughes#
322*9a7741deSElliott Hughes/x
323*9a7741deSElliott Hughes' >/dev/null 2>foo
324*9a7741deSElliott Hughesgrep 'line [45]' foo >/dev/null || echo 'BAD: T.misc lineno'
325*9a7741deSElliott Hughes
326*9a7741deSElliott Hughesecho 'x
327*9a7741deSElliott Hughes\y' >foo1
328*9a7741deSElliott Hughes$awk 'BEGIN { print "x\f\r\b\v\a\\y" }' >foo2
329*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc weird chars'
330*9a7741deSElliott Hughes
331*9a7741deSElliott Hughesecho 0 >foo1
332*9a7741deSElliott Hughes$awk '	BEGIN { exit }
333*9a7741deSElliott Hughes	{ print }
334*9a7741deSElliott Hughes	END { print NR }' >foo2
335*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc BEGIN exit'
336*9a7741deSElliott Hughes
337*9a7741deSElliott Hughesecho 1 >foo1
338*9a7741deSElliott Hughes$awk '	{ exit }
339*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
340*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit'
341*9a7741deSElliott Hughes
342*9a7741deSElliott Hughesecho 1 >foo1
343*9a7741deSElliott Hughes$awk '	{i = 1; while (i <= NF) {if (i == NF) exit; i++ } }
344*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
345*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 2'
346*9a7741deSElliott Hughes
347*9a7741deSElliott Hughesecho 1 >foo1
348*9a7741deSElliott Hughes$awk '	function f() {
349*9a7741deSElliott Hughes		i = 1; while (i <= NF) {if (i == NF) return NR; i++ }
350*9a7741deSElliott Hughes	}
351*9a7741deSElliott Hughes	{ if (f() == 1) exit }
352*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
353*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc while return'
354*9a7741deSElliott Hughes
355*9a7741deSElliott Hughesecho 1 >foo1
356*9a7741deSElliott Hughes$awk '	function f() {
357*9a7741deSElliott Hughes		split("a b c", arr)
358*9a7741deSElliott Hughes		for (i in arr) {if (i == 3) return NR; i++ }
359*9a7741deSElliott Hughes	}
360*9a7741deSElliott Hughes	{ if (f() == 1) exit }
361*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
362*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc while return'
363*9a7741deSElliott Hughes
364*9a7741deSElliott Hughesecho 1 >foo1
365*9a7741deSElliott Hughes$awk '	{i = 1; do { if (i == NF) exit; i++ } while (i <= NF) }
366*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
367*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 3'
368*9a7741deSElliott Hughes
369*9a7741deSElliott Hughesecho 1 >foo1
370*9a7741deSElliott Hughes$awk '	function f() {
371*9a7741deSElliott Hughes		i = 1; do { if (i == NF) return NR; i++ } while (i <= NF)
372*9a7741deSElliott Hughes	}
373*9a7741deSElliott Hughes	{ if (f() == 1) exit }
374*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
375*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc do return'
376*9a7741deSElliott Hughes
377*9a7741deSElliott Hughesecho 1 >foo1
378*9a7741deSElliott Hughes$awk '	{i = 1; do { if (i == NF) break; i++ } while (i <= NF); exit }
379*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
380*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 4'
381*9a7741deSElliott Hughes
382*9a7741deSElliott Hughesecho 1 >foo1
383*9a7741deSElliott Hughes$awk '	{ n = split($0, x)
384*9a7741deSElliott Hughes	  for (i in x) {
385*9a7741deSElliott Hughes	 	if (i == 1)
386*9a7741deSElliott Hughes			exit } }
387*9a7741deSElliott Hughes	END { print NR }' /etc/passwd >foo2
388*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc immmediate exit 5'
389*9a7741deSElliott Hughes
390*9a7741deSElliott Hughesecho XXXXXXXX >foo1
391*9a7741deSElliott Hughes$awk 'BEGIN { s = "ab\fc\rd\be"
392*9a7741deSElliott Hughes	t = s; 	gsub("[" s "]", "X", t); print t }' >foo2
393*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc weird escapes in char class'
394*9a7741deSElliott Hughes
395*9a7741deSElliott Hughes$awk '{}' /etc/passwd glop/glop >foo 2>foo2
396*9a7741deSElliott Hughesgrep "can't open.*glop" foo2 >/dev/null || echo "BAD: T.misc can't open"
397*9a7741deSElliott Hughes
398*9a7741deSElliott Hughesecho '
399*9a7741deSElliott Hughes
400*9a7741deSElliott Hughes
401*9a7741deSElliott Hughesa
402*9a7741deSElliott Hughesaa
403*9a7741deSElliott Hughes
404*9a7741deSElliott Hughesb
405*9a7741deSElliott Hughes
406*9a7741deSElliott Hughes
407*9a7741deSElliott Hughesc
408*9a7741deSElliott Hughes
409*9a7741deSElliott Hughes' >foo
410*9a7741deSElliott Hughesecho 3 >foo1
411*9a7741deSElliott Hughes$awk 'BEGIN { RS = "" }; END { print NR }' foo >foo2
412*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc RS botch'
413*9a7741deSElliott Hughes
414*9a7741deSElliott Hughes$awk 'BEGIN \
415*9a7741deSElliott Hughes	{
416*9a7741deSElliott Hughes		print "hello, world"
417*9a7741deSElliott Hughes	}
418*9a7741deSElliott Hughes}}}' >foo1 2>foo2
419*9a7741deSElliott Hughesgrep 'source line 4' foo2 >/dev/null 2>&1 || echo 'BAD: T.misc continuation line number'
420*9a7741deSElliott Hughes
421*9a7741deSElliott Hughes
422*9a7741deSElliott Hughesecho 111 222 333 >foo
423*9a7741deSElliott Hughes$awk '{ f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] }' foo >foo2
424*9a7741deSElliott Hughesecho 111 111 222 2 2 >foo1
425*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc $f[1]++'
426*9a7741deSElliott Hughes
427*9a7741deSElliott Hughes
428*9a7741deSElliott Hughes# These should be syntax errors
429*9a7741deSElliott Hughes$awk . 2>foo
430*9a7741deSElliott Hughesgrep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error . fails"
431*9a7741deSElliott Hughes
432*9a7741deSElliott Hughes$awk .. 2>foo
433*9a7741deSElliott Hughesgrep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error .. fails"
434*9a7741deSElliott Hughes
435*9a7741deSElliott Hughes$awk .E. 2>foo
436*9a7741deSElliott Hughesgrep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error .E. fails"
437*9a7741deSElliott Hughes
438*9a7741deSElliott Hughes$awk .++. 2>foo
439*9a7741deSElliott Hughesgrep "syntax error" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error .++. fails"
440*9a7741deSElliott Hughes
441*9a7741deSElliott Hughes
442*9a7741deSElliott Hughes
443*9a7741deSElliott Hughes# These should be syntax errors
444*9a7741deSElliott Hughes$awk '$' 2>foo
445*9a7741deSElliott Hughesgrep "unexpected" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error $ fails"
446*9a7741deSElliott Hughes
447*9a7741deSElliott Hughes$awk '{print $' 2>foo
448*9a7741deSElliott Hughesgrep "unexpected" foo >/dev/null || echo 1>&2 "BAD: T.misc syntax error $2 fails"
449*9a7741deSElliott Hughes
450*9a7741deSElliott Hughes$awk '"' 2>foo
451*9a7741deSElliott Hughesgrep "non-terminated" foo >/dev/null || echo 1>&2 "BAD: T.misc bare quote fails"
452*9a7741deSElliott Hughes
453*9a7741deSElliott Hughes
454*9a7741deSElliott Hughes# %c of 0 is explicit null byte
455*9a7741deSElliott Hughes
456*9a7741deSElliott Hughes./echo '3' >foo1
457*9a7741deSElliott Hughes$awk 'BEGIN {printf("%c%c\n", 0, 0) }' | wc | $awk '{print $3}' >foo2
458*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc null byte'
459*9a7741deSElliott Hughes
460*9a7741deSElliott Hughes# non-terminated RE
461*9a7741deSElliott Hughes
462*9a7741deSElliott Hughes$awk /xyz >foo 2>&1
463*9a7741deSElliott Hughesgrep "non-terminated" foo >/dev/null || echo 1>&2 "BAD: T.misc non-terminated RE"
464*9a7741deSElliott Hughes
465*9a7741deSElliott Hughes# next several were infinite loops, found by brian tsang.
466*9a7741deSElliott Hughes# this is his example:
467*9a7741deSElliott Hughes
468*9a7741deSElliott Hughes$awk 'BEGIN {
469*9a7741deSElliott Hughes    switch (substr("x",1,1)) {
470*9a7741deSElliott Hughes    case /ask.com/:
471*9a7741deSElliott Hughes	break
472*9a7741deSElliott Hughes    case "google":
473*9a7741deSElliott Hughes	break
474*9a7741deSElliott Hughes    }
475*9a7741deSElliott Hughes}' >foo 2>&1
476*9a7741deSElliott Hughesgrep "illegal statement" foo >/dev/null || echo 1>&2 "BAD: T.misc looping syntax error 1"
477*9a7741deSElliott Hughes
478*9a7741deSElliott Hughes$awk 'BEGIN { s { c /./ } }' >foo 2>&1
479*9a7741deSElliott Hughesgrep "illegal statement" foo >/dev/null || echo 1>&2 "BAD: T.misc looping syntax error 2"
480*9a7741deSElliott Hughes
481*9a7741deSElliott Hughes$awk 'BEGIN { s { c /../ } }' >foo 2>&1
482*9a7741deSElliott Hughesgrep "illegal statement" foo >/dev/null || echo 1>&2 "BAD: T.misc looping syntax error 3"
483*9a7741deSElliott Hughes
484*9a7741deSElliott Hughes$awk 'BEGIN {printf "%2$s %1$s\n", "a", "b"}' >foo 2>&1
485*9a7741deSElliott Hughesgrep "'$' not permitted in awk formats" foo >/dev/null || echo 1>&2 "BAD: T.misc '$' not permitted in formats"
486*9a7741deSElliott Hughes
487*9a7741deSElliott Hughesecho 'a
488*9a7741deSElliott Hughesb c
489*9a7741deSElliott Hughesde fg hi' >foo0
490*9a7741deSElliott Hughes$awk 'END { print NF, $0 }' foo0 >foo1
491*9a7741deSElliott Hughesawk '{ print NF, $0 }' foo0| tail -1 >foo2
492*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc END must preserve $0'
493*9a7741deSElliott Hughes
494*9a7741deSElliott Hughesecho 'fg hi' >foo0
495*9a7741deSElliott Hughes$awk 'END { print NF, $0 }' foo0 >foo1
496*9a7741deSElliott Hughesawk '{ print NF, $0 }' foo0| tail -1 >foo2
497*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc END must preserve $0'
498*9a7741deSElliott Hughes
499*9a7741deSElliott Hughesecho '' >foo0
500*9a7741deSElliott Hughes$awk 'END { print NF, $0 }' foo0 >foo1
501*9a7741deSElliott Hughesawk '{ print NF, $0 }' foo0| tail -1 >foo2
502*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc END must preserve $0'
503*9a7741deSElliott Hughes
504*9a7741deSElliott Hughes# Check for nonzero exit status on I/O error.
505*9a7741deSElliott Hughesecho 'E 2' >foo1
506*9a7741deSElliott Hughes(trap '' PIPE; "$awk" 'BEGIN { print "hi"; }' 2>/dev/null; echo "E $?" >foo2) | :
507*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc exit status on I/O error'
508*9a7741deSElliott Hughes
509*9a7741deSElliott Hughes# Check for clobbering of the lexer's regular expression buffer.
510*9a7741deSElliott Hughes# If the output is "a1" instead of "1b", /b/ clobbered /a/.
511*9a7741deSElliott Hughesecho 1b >foo1
512*9a7741deSElliott Hughesecho ab | $awk '{ sub(/a/, "b" ~ /b/); print }' >foo2
513*9a7741deSElliott Hughescmp -s foo1 foo2 || echo 'BAD: T.misc lexer regex buffer clobbered'
514*9a7741deSElliott Hughes
515*9a7741deSElliott Hughes# Check handling of octal \OOO and hex \xHH esc. seqs. in strings.
516*9a7741deSElliott Hughesecho 'hello888
517*9a7741deSElliott Hugheshello
518*9a7741deSElliott Hugheshello
519*9a7741deSElliott HugheshelloxGOO
520*9a7741deSElliott Hugheshello
521*9a7741deSElliott Hughes0A' > foo1
522*9a7741deSElliott Hughes$awk 'BEGIN { print "hello\888" }'   > foo2
523*9a7741deSElliott Hughes$awk 'BEGIN { print "hello\x000A" }' >> foo2
524*9a7741deSElliott Hughes$awk 'BEGIN { printf "hello\x0A" }'  >> foo2
525*9a7741deSElliott Hughes$awk 'BEGIN { print "hello\xGOO" }'  >> foo2
526*9a7741deSElliott Hughes$awk 'BEGIN { print "hello\x0A0A" }' >> foo2
527cmp -s foo1 foo2 || echo '�BAD: T.misc escape sequences in strings mishandled'
528