1e6d6c189SCody Peter Mello#!/bin/bash
2e6d6c189SCody Peter Mello
3e98dc02aSDan McDonald# Copyright 2019 Joyent, Inc.
4e98dc02aSDan McDonald
5e6d6c189SCody Peter Melloif [[ -z "$AWK" || -z "$WORKDIR" ]]; then
6e6d6c189SCody Peter Mello    printf '$AWK and $WORKDIR must be set\n' >&2
7e6d6c189SCody Peter Mello    exit 1
8e6d6c189SCody Peter Mellofi
9e6d6c189SCody Peter Mello
10e6d6c189SCody Peter MelloTEMP0=$WORKDIR/test.temp.0
11e6d6c189SCody Peter MelloTEMP1=$WORKDIR/test.temp.1
12e6d6c189SCody Peter MelloTEMP2=$WORKDIR/test.temp.2
13e6d6c189SCody Peter MelloTEMP3=$WORKDIR/test.temp.3
14e6d6c189SCody Peter Mello
15e6d6c189SCody Peter MelloRESULT=0
16e6d6c189SCody Peter Mello
17e6d6c189SCody Peter Mellofail() {
18e6d6c189SCody Peter Mello	echo "$1" >&2
19e6d6c189SCody Peter Mello	RESULT=1
20e6d6c189SCody Peter Mello}
21e6d6c189SCody Peter Mello
22e6d6c189SCody Peter Melloecho T.misc: miscellaneous buglets now watched for
23e6d6c189SCody Peter Mello
24e6d6c189SCody Peter Mellorm -f core
25e6d6c189SCody Peter Mello
26e6d6c189SCody Peter Melloecho 'The big brown over the lazy doe
27e6d6c189SCody Peter MelloThe big brown over the lazy dog
28e6d6c189SCody Peter Mellox
29e6d6c189SCody Peter MelloThe big brown over the lazy dog' > $TEMP0
30e6d6c189SCody Peter Melloecho 'failed
31e6d6c189SCody Peter Mellosucceeded
32e6d6c189SCody Peter Mellofailed
33e6d6c189SCody Peter Mellosucceeded' > $TEMP1
34e6d6c189SCody Peter Mello$AWK '{ if (match($0, /^The big brown over the lazy dog/) == 0) {
35e6d6c189SCody Peter Mello		printf("failed\n")
36e6d6c189SCody Peter Mello	} else {
37e6d6c189SCody Peter Mello		printf("succeeded\n")
38e6d6c189SCody Peter Mello	}
39e6d6c189SCody Peter Mello} ' $TEMP0 > $TEMP2
40e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc ghosh RE bug'
41e6d6c189SCody Peter Mello
42e6d6c189SCody Peter Melloecho '123
43e6d6c189SCody Peter Mello1234567890
44e6d6c189SCody Peter Mello12345678901' > $TEMP0
45e6d6c189SCody Peter Melloecho '12345678901' > $TEMP1
46e6d6c189SCody Peter Mello$AWK 'length($0) > 10' $TEMP0 > $TEMP2
47e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc last number bug'
48e6d6c189SCody Peter Mello
49e6d6c189SCody Peter Mello# check some \ sequences in strings (ascii)
50e6d6c189SCody Peter Melloecho HIJKL > $TEMP1
51e6d6c189SCody Peter Melloecho $TEMP0 | $AWK '{ print "H\x49\x4a\x4BL" }' > $TEMP2
52e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc hex string cvt'
53e6d6c189SCody Peter Mello
54e6d6c189SCody Peter Melloecho 012x45 > $TEMP1
55e6d6c189SCody Peter Mello$AWK 'BEGIN { print "0\061\62x\0645" }' > $TEMP2
56e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc oct string cvt'
57e6d6c189SCody Peter Mello
58e6d6c189SCody Peter Mello# $i++ means ($i)++
59e6d6c189SCody Peter Melloecho 3 5 | $AWK '{ i = 1; print $i++ ; print $1, i }' > $TEMP1
60e6d6c189SCody Peter Melloecho '3
61e6d6c189SCody Peter Mello4 1' > $TEMP2
62e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc bad field increment'
63e6d6c189SCody Peter Mello
64e6d6c189SCody Peter Mello# makes sure that fields are recomputed even if self-assignment
653ee4fc2aSCody Peter Mello# take into account that subtracting from NF now rebuilds the record
66e6d6c189SCody Peter Melloecho 'a b c
67e6d6c189SCody Peter Mellos p q r
683ee4fc2aSCody Peter Mellox y z' > $TEMP0
69e6d6c189SCody Peter Melloecho 'a
70e6d6c189SCody Peter Mellos p
71e6d6c189SCody Peter Mellox' > $TEMP1
72e6d6c189SCody Peter Mello$AWK '{ NF -= 2; $1 = $1; print }' < $TEMP0 > $TEMP2
73e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail "BAD: T.misc bad field self-assignment"
74e6d6c189SCody Peter Mello
75e6d6c189SCody Peter Melloecho '1
76e6d6c189SCody Peter Mello1' > $TEMP1
77e6d6c189SCody Peter Mello$AWK 'BEGIN {x = 1; print x; x = x; print x}' > $TEMP2
78e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail "BAD: T.misc bad self-assignment"
79e6d6c189SCody Peter Mello
80e6d6c189SCody Peter Melloecho 573109312 | $AWK '{print $1*4}' > $TEMP1
81e6d6c189SCody Peter Melloecho 2292437248 > $TEMP2
82e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail "BAD: T.misc bad overflow"
83e6d6c189SCody Peter Mello
84e6d6c189SCody Peter Mello# note that there are 8-bit characters in the echo
85e6d6c189SCody Peter Mello# some shells will probably screw this up.
86e6d6c189SCody Peter Melloecho '#
87e6d6c189SCody Peter Mellocode € 1
88e6d6c189SCody Peter Mellocode € 2' |
89e6d6c189SCody Peter Mello$AWK '/^#/' > $TEMP1
90e6d6c189SCody Peter Melloecho '#' > $TEMP2
91e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail "BAD: T.misc bad match of 8-bit char"
92e6d6c189SCody Peter Mello
93e6d6c189SCody Peter Melloecho hello |
94e6d6c189SCody Peter Mello$AWK 'BEGIN	{ FILENAME = "/etc/passwd" }
95e6d6c189SCody Peter Mello	{ print $0 }' >/dev/null
96e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc /etc/passwd dropped core"; fi
97e6d6c189SCody Peter Mello
983ee4fc2aSCody Peter Melloecho hello |
993ee4fc2aSCody Peter Mello$AWK '  function foo(foo) {
1003ee4fc2aSCody Peter Mello                foo = 1
1013ee4fc2aSCody Peter Mello                foo()
1023ee4fc2aSCody Peter Mello        }
1033ee4fc2aSCody Peter Mello	{ foo(bar) }
1043ee4fc2aSCody Peter Mello' >/dev/null 2>&1
1053ee4fc2aSCody Peter Melloif [[ $? -eq 139 ]]; then
1063ee4fc2aSCody Peter Mello	fail "BAD: T.misc function foo(foo) dropped core"
1073ee4fc2aSCody Peter Mello	rm -f core
1083ee4fc2aSCody Peter Mellofi
1093ee4fc2aSCody Peter Mello
110e6d6c189SCody Peter Melloecho '2
111e6d6c189SCody Peter Mello10' |
112e6d6c189SCody Peter Mello$AWK '{ x[NR] = $0 }	# test whether $0 is NUM as well as STR
113e6d6c189SCody Peter MelloEND { if (x[1] > x[2]) print "BAD: T.misc: $0 is not NUM" }'
114e6d6c189SCody Peter Mello
115e6d6c189SCody Peter Mello
116e6d6c189SCody Peter Mello$AWK 'BEGIN {
117e6d6c189SCody Peter Mello	npad = substr("alexander" "           ",1,15)
118e6d6c189SCody Peter Mello	print npad
119e6d6c189SCody Peter Mello	}' > $TEMP0
120e6d6c189SCody Peter Mellogrep '\\' $TEMP0 && fail "BAD: T.misc alexander fails"
121e6d6c189SCody Peter Mello
122e6d6c189SCody Peter Mello# This should give an error about function arguments
123e6d6c189SCody Peter Mello$AWK '
124e6d6c189SCody Peter Mellofunction foo(x) { print "x is" x }
125e6d6c189SCody Peter MelloBEGIN { foo(foo) }
126e6d6c189SCody Peter Mello' 2> $TEMP0
127e6d6c189SCody Peter Mellogrep "can't use function foo" $TEMP0 >/dev/null || fail "BAD: T.misc fcn args"
128e6d6c189SCody Peter Mello
129e6d6c189SCody Peter Mello
130e6d6c189SCody Peter Mello# gawk defref test; should give error about undefined function
131e6d6c189SCody Peter Mello$AWK 'BEGIN { foo() }' 2> $TEMP0
132e6d6c189SCody Peter Mellogrep "calling undefined function foo" $TEMP0 >/dev/null || fail "BAD: T.misc undefined function"
133e6d6c189SCody Peter Mello
134e6d6c189SCody Peter Mello
135e98dc02aSDan McDonald# gawk arrayparm test; should give error about function
136e6d6c189SCody Peter Mello$AWK '
137e6d6c189SCody Peter MelloBEGIN {
138e6d6c189SCody Peter Mello    foo[1]=1;
139e6d6c189SCody Peter Mello    foo[2]=2;
140e6d6c189SCody Peter Mello    bug1(foo);
141e6d6c189SCody Peter Mello}
142e6d6c189SCody Peter Mellofunction bug1(i) {
143e6d6c189SCody Peter Mello    for (i in foo) {
144e6d6c189SCody Peter Mello	bug2(i);
145e6d6c189SCody Peter Mello	delete foo[i];
146e6d6c189SCody Peter Mello	print i,1,bot[1];
147e6d6c189SCody Peter Mello    }
148e6d6c189SCody Peter Mello}
149e6d6c189SCody Peter Mellofunction bug2(arg) {
150e6d6c189SCody Peter Mello    bot[arg]=arg;
151e6d6c189SCody Peter Mello}
152e6d6c189SCody Peter Mello' 2> $TEMP0
153e6d6c189SCody Peter Mellogrep "can.t assign to foo" $TEMP0 >/dev/null || fail "BAD: T.misc foo bug"
154e6d6c189SCody Peter Mello
155e6d6c189SCody Peter Mello
1563ee4fc2aSCody Peter Mello# This should be a syntax error
1573ee4fc2aSCody Peter Mello$AWK '
1583ee4fc2aSCody Peter Mello!x = y
1593ee4fc2aSCody Peter Mello' 2> $TEMP0
1603ee4fc2aSCody Peter Mellogrep "syntax error" $TEMP0 >/dev/null || fail "BAD: T.misc syntax error !x=y fails"
1613ee4fc2aSCody Peter Mello
162e6d6c189SCody Peter Mello# This should print bbb
163e6d6c189SCody Peter Mello$AWK '
164e6d6c189SCody Peter MelloBEGIN { up[1] = "a"
165e6d6c189SCody Peter Mello	for (i in up) gsub("a", "A", x)
166e6d6c189SCody Peter Mello	print x x "bbb"
167e6d6c189SCody Peter Mello	exit
168e6d6c189SCody Peter Mello      }
169e6d6c189SCody Peter Mello' > $TEMP0
170e6d6c189SCody Peter Mellogrep bbb $TEMP0 >/dev/null || fail "BAD: T.misc gsub failed"
171e6d6c189SCody Peter Mello
172e6d6c189SCody Peter Melloecho yes |
173e6d6c189SCody Peter Mello$AWK '
174e6d6c189SCody Peter MelloBEGIN {
175e6d6c189SCody Peter Mello	printf "push return" >"/dev/null"
176e6d6c189SCody Peter Mello	getline ans <"/dev/null"
177e6d6c189SCody Peter Mello} '
178e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc getline ans dropped core"; fi
179e6d6c189SCody Peter Mello
180e6d6c189SCody Peter Mello$AWK 'BEGIN { unireghf() }
181e6d6c189SCody Peter Mellofunction unireghf(hfeed) { hfeed[1] = 0 }'
182e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc unireghf dropped core"; fi
183e6d6c189SCody Peter Mello
184e6d6c189SCody Peter Melloecho x | $AWK '/[/]/' 2> $TEMP0
185e6d6c189SCody Peter Mellogrep 'nonterminated character class' $TEMP0 >/dev/null || error 'BAD: T.misc nonterminated fails'
186e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc nonterminated dropped core"; fi
187e6d6c189SCody Peter Mello
188e6d6c189SCody Peter Mello$AWK '
189e6d6c189SCody Peter Mellofunction f() { return 12345 }
190e6d6c189SCody Peter MelloBEGIN { printf "<%s>\n", f() }
191e6d6c189SCody Peter Mello' > $TEMP0
192e6d6c189SCody Peter Mellogrep '<12345>' $TEMP0 >/dev/null || fail 'BAD: T.misc <12345> fails'
193e6d6c189SCody Peter Mello
194e6d6c189SCody Peter Melloecho 'abc
195e6d6c189SCody Peter Mellodef
196e6d6c189SCody Peter Mello
197e6d6c189SCody Peter Melloghi
198e6d6c189SCody Peter Mellojkl' > $TEMP0
199e6d6c189SCody Peter Mello$AWK '
200e6d6c189SCody Peter MelloBEGIN {	RS = ""
201e6d6c189SCody Peter Mello	while (getline <"'$TEMP0'")
202e6d6c189SCody Peter Mello		print
203e6d6c189SCody Peter Mello}' > $TEMP1
204e6d6c189SCody Peter Mello$AWK 'END {print NR}' $TEMP1 | grep 4 >/dev/null || fail 'BAD: T.misc abcdef fails'
205e6d6c189SCody Peter Mello
206e6d6c189SCody Peter Mello
207e6d6c189SCody Peter Mello# The following should not produce a warning about changing a constant
208e6d6c189SCody Peter Mello# nor about a curdled tempcell list
209e6d6c189SCody Peter Mello$AWK 'function f(x) { x = 2 }
210e6d6c189SCody Peter MelloBEGIN { f(1) }' > $TEMP0
211e6d6c189SCody Peter Mellogrep '^' $TEMP0 && fail 'BAD: test constant change fails'
212e6d6c189SCody Peter Mello
213e6d6c189SCody Peter Mello# The following should not produce a warning about a curdled tempcell list
214e6d6c189SCody Peter Mello$AWK 'function f(x) { x }
215e6d6c189SCody Peter MelloBEGIN { f(1) }' > $TEMP0
216e6d6c189SCody Peter Mellogrep '^' $TEMP0 && fail 'BAD: test tempcell list fails'
217e6d6c189SCody Peter Mello
218e6d6c189SCody Peter Mello$AWK 'BEGIN { print 9, a=10, 11; print a; exit }' > $TEMP1
219e6d6c189SCody Peter Melloecho '9 10 11
220e6d6c189SCody Peter Mello10' > $TEMP2
221e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.misc (embedded expression)'
222e6d6c189SCody Peter Mello
223e6d6c189SCody Peter Melloecho "abc defgh ijkl" | $AWK '
224e6d6c189SCody Peter Mello  { $1 = ""; line = $0; print line; print $0; $0 = line; print $0 }' > $TEMP1
225e6d6c189SCody Peter Melloecho " defgh ijkl
226e6d6c189SCody Peter Mello defgh ijkl
227e6d6c189SCody Peter Mello defgh ijkl" > $TEMP2
228e6d6c189SCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.misc (assignment to $0)'
229e6d6c189SCody Peter Mello
230e6d6c189SCody Peter Mello$AWK '
231e6d6c189SCody Peter Mellofunction min(a, b)
232e6d6c189SCody Peter Mello{
233e6d6c189SCody Peter Mello	if (a < b)
234e6d6c189SCody Peter Mello		return a
235e6d6c189SCody Peter Mello	else
236e6d6c189SCody Peter Mello		return b
237e6d6c189SCody Peter Mello}
238e6d6c189SCody Peter MelloBEGIN { exit }
239e6d6c189SCody Peter Mello'
240e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc function min dropped core"; fi
241e6d6c189SCody Peter Mello
242e6d6c189SCody Peter Mello# The following should not give a syntax error message:
243e6d6c189SCody Peter Mello$AWK '
244e6d6c189SCody Peter Mellofunction expand(chart) {
245e6d6c189SCody Peter Mello	getline chart < "CHAR.ticks"
246e6d6c189SCody Peter Mello}
247e6d6c189SCody Peter Mello' > $TEMP0
248e6d6c189SCody Peter Mellogrep '^' $TEMP0 >/dev/null && fail 'BAD: T.misc expand error'
249e6d6c189SCody Peter Mello
250e6d6c189SCody Peter Mello$AWK 'BEGIN { print 1e40 }' >/dev/null
251e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc 1E40 dropped core"; fi
252e6d6c189SCody Peter Mello
253e6d6c189SCody Peter Mello# The following syntax error should not dump core:
254e6d6c189SCody Peter Mello$AWK '
255e6d6c189SCody Peter Mello$NF==3	{first=1}
256e6d6c189SCody Peter Mello$NF==2 && first==0 && (abs($1-o1)>120||abs($2-o2)>120)	{print $0}
257e6d6c189SCody Peter Mello$NF==2	{o1=%1; o2=$2; first=0}
258e6d6c189SCody Peter Mello' 2>/dev/null
259e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc first/abs dropped core"; fi
260e6d6c189SCody Peter Mello
261e6d6c189SCody Peter Mello# The following syntax error should not dump core:
262e6d6c189SCody Peter Mello$AWK '{ n = split($1, address, !); print address[1] }' 2> $TEMP0
263e6d6c189SCody Peter Mellogrep 'illegal statement' $TEMP0 >/dev/null || fail 'BAD: T.misc split error'
264e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc split! dropped core"; fi
265e6d6c189SCody Peter Mello
266e6d6c189SCody Peter Mello# The following should cause a syntax error message
267e6d6c189SCody Peter Mello$AWK 'BEGIN {"hello"}' 2> $TEMP0
268e6d6c189SCody Peter Mellogrep 'illegal statement' $TEMP0 >/dev/null || fail 'BAD: T.misc hello error'
269e6d6c189SCody Peter Mello
270e6d6c189SCody Peter Mello# The following should give a syntax error message:
271e6d6c189SCody Peter Mello$AWK '
272e6d6c189SCody Peter Mellofunction pile(c,     r) {
273e6d6c189SCody Peter Mello	r = ++pile[c]
274e6d6c189SCody Peter Mello}
275e6d6c189SCody Peter Mello
276e6d6c189SCody Peter Mello{ pile($1) }
277e6d6c189SCody Peter Mello' 2> $TEMP0
278e6d6c189SCody Peter Mellogrep 'context is' $TEMP0 >/dev/null || fail 'BAD: T.misc pile error'
279e6d6c189SCody Peter Mello
280e6d6c189SCody Peter Mello# This should complain about missing atan2 argument:
281e6d6c189SCody Peter Mello$AWK 'BEGIN { atan2(1) }' 2> $TEMP0
282e6d6c189SCody Peter Mellogrep 'requires two arg' $TEMP0 >/dev/null || fail 'BAD: T.misc atan2 error'
283e6d6c189SCody Peter Mello
284e6d6c189SCody Peter Mello# This should not core dump:
285e6d6c189SCody Peter Mello$AWK 'BEGIN { f() }
286e6d6c189SCody Peter Mellofunction f(A) { delete A[1] }
287e6d6c189SCody Peter Mello'
288e6d6c189SCody Peter Melloif [[ $? -eq 139 ]]; then fail "BAD: T.misc delete dropped core"; fi
289e6d6c189SCody Peter Mello
290e6d6c189SCody Peter Mello# nasty one:  should not be able to overwrite constants
291e6d6c189SCody Peter Mello$AWK 'BEGIN { gsub(/ana/,"anda","banana")
292e6d6c189SCody Peter Mello		printf "the monkey ate a %s\n", "banana" }
293e6d6c189SCody Peter Mello' >/dev/null 2> $TEMP0
294e6d6c189SCody Peter Mellogrep 'syntax error' $TEMP0 >/dev/null || fail 'BAD: T.misc gsub banana error'
295e6d6c189SCody Peter Mello
296e6d6c189SCody Peter Mello# nasty one:  should not be able to overwrite constants
297e6d6c189SCody Peter Mello$AWK 'BEGIN { sub(/ana/,"anda","banana")
298e6d6c189SCody Peter Mello		printf "the monkey ate a %s\n", "banana" }
299e6d6c189SCody Peter Mello' >/dev/null 2> $TEMP0
300e6d6c189SCody Peter Mellogrep 'syntax error' $TEMP0 >/dev/null || fail 'BAD: T.misc sub banana error'
301e6d6c189SCody Peter Mello
302e6d6c189SCody Peter Mello# line numbers used to double-count comments
303e6d6c189SCody Peter Mello$AWK '#
304e6d6c189SCody Peter Mello#
305e6d6c189SCody Peter Mello#
306e6d6c189SCody Peter Mello/x
307e6d6c189SCody Peter Mello' >/dev/null 2> $TEMP0
308e6d6c189SCody Peter Mellogrep 'line [45]' $TEMP0 >/dev/null || fail 'BAD: T.misc lineno'
309e6d6c189SCody Peter Mello
310*8becd264SBill Sommerfeldecho 'x\r\y' > $TEMP1
311e6d6c189SCody Peter Mello$AWK 'BEGIN { print "x\f\r\b\v\a\\y" }' > $TEMP2
312e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc weird chars'
313e6d6c189SCody Peter Mello
314e6d6c189SCody Peter Melloecho 0 > $TEMP1
315e6d6c189SCody Peter Mello$AWK '	BEGIN { exit }
316e6d6c189SCody Peter Mello	{ print }
317e6d6c189SCody Peter Mello	END { print NR }' > $TEMP2
318e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc BEGIN exit'
319e6d6c189SCody Peter Mello
320e6d6c189SCody Peter Melloecho 1 > $TEMP1
321e6d6c189SCody Peter Mello$AWK '	{ exit }
322e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
323e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc immmediate exit'
324e6d6c189SCody Peter Mello
325e6d6c189SCody Peter Melloecho 1 > $TEMP1
326e6d6c189SCody Peter Mello$AWK '	{i = 1; while (i <= NF) {if (i == NF) exit; i++ } }
327e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
328e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc immmediate exit 2'
329e6d6c189SCody Peter Mello
330e6d6c189SCody Peter Melloecho 1 > $TEMP1
331e6d6c189SCody Peter Mello$AWK '	function f() {
332e6d6c189SCody Peter Mello		i = 1; while (i <= NF) {if (i == NF) return NR; i++ }
333e6d6c189SCody Peter Mello	}
334e6d6c189SCody Peter Mello	{ if (f() == 1) exit }
335e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
336e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc while return'
337e6d6c189SCody Peter Mello
338e6d6c189SCody Peter Melloecho 1 > $TEMP1
339e6d6c189SCody Peter Mello$AWK '	function f() {
340e6d6c189SCody Peter Mello		split("a b c", arr)
341e6d6c189SCody Peter Mello		for (i in arr) {if (i == 3) return NR; i++ }
342e6d6c189SCody Peter Mello	}
343e6d6c189SCody Peter Mello	{ if (f() == 1) exit }
344e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
345e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc while return'
346e6d6c189SCody Peter Mello
347e6d6c189SCody Peter Melloecho 1 > $TEMP1
348e6d6c189SCody Peter Mello$AWK '	{i = 1; do { if (i == NF) exit; i++ } while (i <= NF) }
349e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
350e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc immmediate exit 3'
351e6d6c189SCody Peter Mello
352e6d6c189SCody Peter Melloecho 1 > $TEMP1
353e6d6c189SCody Peter Mello$AWK '	function f() {
354e6d6c189SCody Peter Mello		i = 1; do { if (i == NF) return NR; i++ } while (i <= NF)
355e6d6c189SCody Peter Mello	}
356e6d6c189SCody Peter Mello	{ if (f() == 1) exit }
357e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
358e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc do return'
359e6d6c189SCody Peter Mello
360e6d6c189SCody Peter Melloecho 1 > $TEMP1
361e6d6c189SCody Peter Mello$AWK '	{i = 1; do { if (i == NF) break; i++ } while (i <= NF); exit }
362e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
363e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc immmediate exit 4'
364e6d6c189SCody Peter Mello
365e6d6c189SCody Peter Melloecho 1 > $TEMP1
366e6d6c189SCody Peter Mello$AWK '	{ n = split($0, x)
367e6d6c189SCody Peter Mello	  for (i in x) {
368e98dc02aSDan McDonald		if (i == 1)
369e6d6c189SCody Peter Mello			exit } }
370e6d6c189SCody Peter Mello	END { print NR }' /etc/passwd > $TEMP2
371e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc immmediate exit 5'
372e6d6c189SCody Peter Mello
373e6d6c189SCody Peter Melloecho XXXXXXXX > $TEMP1
374e6d6c189SCody Peter Mello$AWK 'BEGIN { s = "ab\fc\rd\be"
375e98dc02aSDan McDonald	t = s;	gsub("[" s "]", "X", t); print t }' > $TEMP2
376e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc weird escapes in char class'
377e6d6c189SCody Peter Mello
378e6d6c189SCody Peter Mello$AWK '{}' /etc/passwd glop/glop > $TEMP0 2> $TEMP2
379e6d6c189SCody Peter Mellogrep "can't open.*glop" $TEMP2 >/dev/null || fail "BAD: T.misc can't open"
380e6d6c189SCody Peter Mello
381e6d6c189SCody Peter Melloecho '
382e6d6c189SCody Peter Mello
383e6d6c189SCody Peter Mello
384e6d6c189SCody Peter Melloa
385e6d6c189SCody Peter Melloaa
386e6d6c189SCody Peter Mello
387e6d6c189SCody Peter Mellob
388e6d6c189SCody Peter Mello
389e6d6c189SCody Peter Mello
390e6d6c189SCody Peter Melloc
391e6d6c189SCody Peter Mello
392e6d6c189SCody Peter Mello' > $TEMP0
393e6d6c189SCody Peter Melloecho 3 > $TEMP1
394e6d6c189SCody Peter Mello$AWK 'BEGIN { RS = "" }; END { print NR }' $TEMP0 > $TEMP2
395e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc RS botch'
396e6d6c189SCody Peter Mello
397e6d6c189SCody Peter Mello$AWK 'BEGIN \
398e6d6c189SCody Peter Mello	{
399e6d6c189SCody Peter Mello		print "hello, world"
400e6d6c189SCody Peter Mello	}
401e6d6c189SCody Peter Mello}}}' > $TEMP1 2> $TEMP2
402e6d6c189SCody Peter Mellogrep 'source line 5' $TEMP2 >/dev/null 2>&1 || fail 'BAD: T.misc continuation line number'
403e6d6c189SCody Peter Mello
404e6d6c189SCody Peter Mello
405e6d6c189SCody Peter Melloecho 111 222 333 > $TEMP0
406e6d6c189SCody Peter Mello$AWK '{ f[1]=1; f[2]=2; print $f[1], $f[1]++, $f[2], f[1], f[2] }' $TEMP0 > $TEMP2
407e6d6c189SCody Peter Melloecho 111 111 222 2 2 > $TEMP1
408e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc $f[1]++'
409e6d6c189SCody Peter Mello
410e6d6c189SCody Peter Mello
411e6d6c189SCody Peter Mello# These should be syntax errors
412e6d6c189SCody Peter Mello$AWK . 2> $TEMP0
413e6d6c189SCody Peter Mellogrep "syntax error" $TEMP0 >/dev/null || fail "BAD: T.misc syntax error . fails"
414e6d6c189SCody Peter Mello
415e6d6c189SCody Peter Mello$AWK .. 2> $TEMP0
416e6d6c189SCody Peter Mellogrep "syntax error" $TEMP0 >/dev/null || fail "BAD: T.misc syntax error .. fails"
417e6d6c189SCody Peter Mello
418e6d6c189SCody Peter Mello$AWK .E. 2> $TEMP0
419e6d6c189SCody Peter Mellogrep "syntax error" $TEMP0 >/dev/null || fail "BAD: T.misc syntax error .E. fails"
420e6d6c189SCody Peter Mello
421e6d6c189SCody Peter Mello$AWK .++. 2> $TEMP0
422e6d6c189SCody Peter Mellogrep "syntax error" $TEMP0 >/dev/null || fail "BAD: T.misc syntax error .++. fails"
423e6d6c189SCody Peter Mello
424e6d6c189SCody Peter Mello
4253ee4fc2aSCody Peter Mello
4263ee4fc2aSCody Peter Mello# These should be syntax errors
4273ee4fc2aSCody Peter Mello$AWK '$' 2> $TEMP0
4283ee4fc2aSCody Peter Mellogrep "unexpected" $TEMP0 >/dev/null || fail "BAD: T.misc syntax error $ fails"
4293ee4fc2aSCody Peter Mello
4303ee4fc2aSCody Peter Mello$AWK '{print $' 2> $TEMP0
4313ee4fc2aSCody Peter Mellogrep "unexpected" $TEMP0 >/dev/null || fail "BAD: T.misc syntax error \$2 fails"
4323ee4fc2aSCody Peter Mello
4333ee4fc2aSCody Peter Mello$AWK '"' 2> $TEMP0
4343ee4fc2aSCody Peter Mellogrep "non-terminated" $TEMP0 >/dev/null || fail "BAD: T.misc bare quote fails"
4353ee4fc2aSCody Peter Mello
4363ee4fc2aSCody Peter Mello
4373ee4fc2aSCody Peter Mello# %c of 0 is explicit null byte
4383ee4fc2aSCody Peter Mello
4393ee4fc2aSCody Peter Melloecho '3' > $TEMP1
4403ee4fc2aSCody Peter Mello$AWK 'BEGIN {printf("%c%c\n", 0, 0) }' | wc | $AWK '{print $3}' > $TEMP2
4413ee4fc2aSCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc null byte'
4423ee4fc2aSCody Peter Mello
4433ee4fc2aSCody Peter Mello# non-terminated RE
4443ee4fc2aSCody Peter Mello
4453ee4fc2aSCody Peter Mello$AWK /xyz > $TEMP0 2>&1
4463ee4fc2aSCody Peter Mellogrep "non-terminated" $TEMP0 >/dev/null || fail "BAD: T.misc non-terminated RE"
4473ee4fc2aSCody Peter Mello
448e6d6c189SCody Peter Mello# next several were infinite loops, found by brian tsang.
449e6d6c189SCody Peter Mello# this is his example:
450e6d6c189SCody Peter Mello
451e6d6c189SCody Peter Mello$AWK 'BEGIN {
452e6d6c189SCody Peter Mello    switch (substr("x",1,1)) {
453e6d6c189SCody Peter Mello    case /ask.com/:
454e6d6c189SCody Peter Mello	break
455e6d6c189SCody Peter Mello    case "google":
456e6d6c189SCody Peter Mello	break
457e6d6c189SCody Peter Mello    }
458e6d6c189SCody Peter Mello}' > $TEMP0 2>&1
459e6d6c189SCody Peter Mellogrep "illegal statement" $TEMP0 >/dev/null || fail "BAD: T.misc looping syntax error 1"
460e6d6c189SCody Peter Mello
461e6d6c189SCody Peter Mello$AWK 'BEGIN { s { c /./ } }' > $TEMP0 2>&1
462e6d6c189SCody Peter Mellogrep "illegal statement" $TEMP0 >/dev/null || fail "BAD: T.misc looping syntax error 2"
463e6d6c189SCody Peter Mello
464e6d6c189SCody Peter Mello$AWK 'BEGIN { s { c /../ } }' > $TEMP0 2>&1
465e6d6c189SCody Peter Mellogrep "illegal statement" $TEMP0 >/dev/null || fail "BAD: T.misc looping syntax error 3"
466e6d6c189SCody Peter Mello
4673ee4fc2aSCody Peter Mello$AWK 'BEGIN {printf "%2$s %1$s\n", "a", "b"}' >$TEMP0 2>&1
4683ee4fc2aSCody Peter Mellogrep "'$' not permitted in awk formats" $TEMP0 >/dev/null || fail "BAD: T.misc '$' not permitted in formats"
4693ee4fc2aSCody Peter Mello
4703ee4fc2aSCody Peter Melloecho 'a
4713ee4fc2aSCody Peter Mellob c
4723ee4fc2aSCody Peter Mellode fg hi' > $TEMP0
4733ee4fc2aSCody Peter Mello$AWK 'END { print NF, $0 }' $TEMP0 > $TEMP1
4743ee4fc2aSCody Peter Melloawk '{ print NF, $0 }' $TEMP0| tail -1 > $TEMP2
4753ee4fc2aSCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc END must preserve $0'
4763ee4fc2aSCody Peter Mello
4773ee4fc2aSCody Peter Melloecho 'fg hi' > $TEMP0
4783ee4fc2aSCody Peter Mello$AWK 'END { print NF, $0 }' $TEMP0 > $TEMP1
4793ee4fc2aSCody Peter Melloawk '{ print NF, $0 }' $TEMP0| tail -1 > $TEMP2
4803ee4fc2aSCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc END must preserve $0'
4813ee4fc2aSCody Peter Mello
482e6d6c189SCody Peter Melloecho '' > $TEMP0
483e6d6c189SCody Peter Mello$AWK 'END { print NF, $0 }' $TEMP0 > $TEMP1
484e6d6c189SCody Peter Melloawk '{ print NF, $0 }' $TEMP0| tail -1 > $TEMP2
485e6d6c189SCody Peter Mellocmp -s $TEMP1 $TEMP2 || fail 'BAD: T.misc END must preserve $0'
486e6d6c189SCody Peter Mello
4873ee4fc2aSCody Peter Mello
488*8becd264SBill SommerfeldLC_ALL= LC_NUMERIC=de_DE.UTF-8 $AWK 'BEGIN {
4893ee4fc2aSCody Peter Mello	"echo 1,200" | getline;
4903ee4fc2aSCody Peter Mello	if ($1 == 1.2) {
4913ee4fc2aSCody Peter Mello		printf "good ";
4923ee4fc2aSCody Peter Mello	} else {
4933ee4fc2aSCody Peter Mello		printf "bad ";
4943ee4fc2aSCody Peter Mello	}
4953ee4fc2aSCody Peter Mello	n = 2.3;
4963ee4fc2aSCody Peter Mello	print ($1 + 0.1), (n + 0.1);
4973ee4fc2aSCody Peter Mello}' > $TEMP1
4983ee4fc2aSCody Peter Melloecho 'good 1,3 2,4' > $TEMP2
4993ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.misc LC_NUMERIC should change radix'
5003ee4fc2aSCody Peter Mello
5013ee4fc2aSCody Peter Mello$AWK 'function foo(q) {
5023ee4fc2aSCody Peter Mello	return (q = q);
5033ee4fc2aSCody Peter Mello}
5043ee4fc2aSCody Peter MelloBEGIN { print foo("h"); }' > $TEMP1
5053ee4fc2aSCody Peter Melloecho 'h' > $TEMP2
5063ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.misc return tempcell'
5073ee4fc2aSCody Peter Mello
5083ee4fc2aSCody Peter Mello$AWK -v RECSIZE=8192 'BEGIN {
5093ee4fc2aSCody Peter Mello	for (c = 0; c < 3; c++) {
5103ee4fc2aSCody Peter Mello		a = (RECSIZE % 2 > 0 ? "5" : "55");
5113ee4fc2aSCody Peter Mello		while (length(a) < RECSIZE + c) {
5123ee4fc2aSCody Peter Mello			a = a " 5";
5133ee4fc2aSCody Peter Mello		}
5143ee4fc2aSCody Peter Mello		$0 = a;
5153ee4fc2aSCody Peter Mello		print $2;
5163ee4fc2aSCody Peter Mello	}
5173ee4fc2aSCody Peter Mello}' > $TEMP1
5183ee4fc2aSCody Peter Melloprintf '5\n5\n5\n' > $TEMP2
5193ee4fc2aSCody Peter Mellodiff $TEMP1 $TEMP2 || fail 'BAD: T.misc initial fields overflow'
5203ee4fc2aSCody Peter Mello
521e6d6c189SCody Peter Melloexit $RESULT
522