1#!/bin/bash
2
3if [[ -z "$AWK" || -z "$WORKDIR" ]]; then
4    printf '$AWK and $WORKDIR must be set\n' >&2
5    exit 1
6fi
7
8TEMPLS=$WORKDIR/test.temp.ls
9TEMPSH=$WORKDIR/test.temp.sh
10TEMPOUT=$WORKDIR/test.temp.out
11
12TEMP0=$WORKDIR/test.temp.0
13TEMP1=$WORKDIR/test.temp.1
14TEMP2=$WORKDIR/test.temp.2
15
16RESULT=0
17
18fail() {
19	echo "$1" >&2
20	RESULT=1
21}
22
23echo T.errmsg: check some error messages
24
25ls > $TEMPLS
26$AWK '
27$1 ~ /^#/ {
28	next
29}
30{	pat = $0
31	prog = ""
32	while (getline x > 0 && x != "")
33		prog = prog "\n" x
34	print sprintf("\n%s '"'"'%s'"'"' < '$TEMPLS' >> /dev/null 2> '$TEMPOUT'",
35		ENVIRON["AWK"], prog)
36	print sprintf("grep '"'"'%s'"'"' '$TEMPOUT' >> /dev/null || fail '"'"'BAD: %s'"'"' failed", pat, pat)
37}
38' > $TEMPSH <<\!!!!
39illegal primary in regular expression
40/(/
41
42illegal break, continue, next or nextfile from BEGIN
43BEGIN { nextfile }
44
45illegal break, continue, next or nextfile from END
46END { nextfile }
47
48nextfile is illegal inside a function
49function foo() { nextfile }
50
51duplicate argument
52function f(i,j,i) { return i }
53
54nonterminated character class
55/[[/
56
57nonterminated character class
58/[]/
59
60nonterminated character class
61/[\
62
63nonterminated character class
64BEGIN { s = "[x"; if (1 ~ s) print "foo"}
65
66syntax error in regular expression
67BEGIN { if ("x" ~ /$^/) print "ugh" }
68
69syntax error in regular expression
70/((.)/
71
72division by zero
73BEGIN { print 1/0 }
74
75division by zero in /=
76BEGIN { x = 1; print x /= 0 }
77
78division by zero in %=
79BEGIN { x = 1; print x %= 0 }
80
81division by zero in mod
82BEGIN { print 1%0 }
83
84can.t read value.* array name.
85BEGIN { x[1] = 0; split("a b c", y, x) }
86
87can.t read value.* function
88function f(){}; {split($0, x, f)}
89
90can.t assign.* a function
91function f(){}; {f = split($0, x)}
92
93can.t assign to x; it.s an array name.
94{x = split($0, x)}
95
96is a function, not an array
97function f(){}; {split($0, f)}
98
99function f called with 1 args, uses only 0
100BEGIN { f(f) }
101function f() { print "x" }
102
103can.t use function f as argument in f
104BEGIN { f(f) }
105function f() { print "x" }
106
107x is an array, not a function
108{ split($0, x) }; function x() {}
109
110illegal nested function
111function x() { function g() {} }
112
113return not in function
114{ return }
115
116break illegal outside
117{ break }
118
119continue illegal outside
120{ continue }
121
122non-terminated string
123{ print "abc
124}
125
126illegal field $(foo)
127BEGIN { print $"foo" }
128
129next is illegal inside a function
130BEGIN { f() }
131function f() { next }
132
133not enough args in printf(%s)
134BEGIN { printf("%s") }
135
136weird printf conversion
137BEGIN { printf("%z", "foo")}
138
139function f has .* arguments, limit .*
140function f(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,
141	c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,
142	e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10) {}
143BEGIN { f(123) }
144
145bailing out
146])}
147
148bailing out
149{ print }}
150
151bailing out
152{ print }}}
153
154bailing out
155]
156
157bailing out
158[
159
160bailing out
161a & b
162
163extra )
164{ x = 1) }
165
166illegal statement
167{ print ))}
168
169illegal statement
170{{ print }
171
172illegal statement
173{{{ print }
174
175illegal .*next.* from BEGIN
176BEGIN { next }
177
178illegal .*next.* from END
179END {	next; print NR }
180
181can.t open file /etc/passwd
182BEGIN { print "abc" >"/etc/passwd" }
183
184you can.t define function f more than once
185function f() { print 1 }
186function f() { print 2 }
187
188function mp called with 1 args, uses only 0
189function mp(){ cnt++;}
190BEGIN {	mp(xx) }
191
192index.*doesn.t permit regular expressions
193BEGIN { index("abc", /a/) }
194
195log argument out of domain
196BEGIN { print log(-1) }
197
198exp result out of range
199BEGIN {print exp(1000)}
200
201null file name in print or getline
202BEGIN { print >foo }
203
204function has too many arguments
205BEGIN { length("abc", "def") }
206
207calling undefined function foo
208BEGIN { foo() }
209
210cannot delete SYMTAB or its elements
211BEGIN { delete SYMTAB }
212
213cannot delete SYMTAB or its elements
214BEGIN { delete SYMTAB["OFS"] }
215
216# I am pretty certain that this test is _meant_ to fail,
217# to allow checking whether anything is happening. Uncomment
218# below to check whether the script then fails:
219#
220# this should print a BAD message
221# BEGIN { print }
222!!!!
223
224
225echo '	running tests in test.temp.sh'
226source $TEMPSH
227
228[[ $? -eq 139 ]] && fail 'BAD: someone dropped core' 1>&2
229
230echo xxx > $TEMP0
231$AWK '{print x}' x='a
232b' $TEMP0 > $TEMP1 2> $TEMP2
233grep 'newline in string' $TEMP2 >/dev/null || fail 'BAD: T.errmsg newline in string'
234
235$AWK -safe 'BEGIN{"date" | getline}' > $TEMP0 2> $TEMP2
236grep 'cmd | getline is unsafe' $TEMP2 >/dev/null || fail 'BAD: T.errmsg cmd|getline unsafe'
237
238$AWK -safe 'BEGIN{print >"'$TEMP0'"}' > $TEMP0 2> $TEMP2
239grep 'print > is unsafe' $TEMP2 >/dev/null || fail 'BAD: T.errmsg print > unsafe'
240
241$AWK -safe 'BEGIN{print >> "'$TEMP0'"}' > $TEMP0 2> $TEMP2
242grep 'print >> is unsafe' $TEMP2 >/dev/null || fail 'BAD: T.errmsg print >> unsafe'
243
244$AWK -safe 'BEGIN{print | "'$TEMP0'"}' > $TEMP0 2> $TEMP2
245grep 'print | is unsafe' $TEMP2 >/dev/null || fail 'BAD: T.errmsg print | unsafe'
246
247$AWK -safe 'BEGIN {system("date")}' > $TEMP0 2> $TEMP2
248grep 'system is unsafe' $TEMP2 >/dev/null || fail 'BAD: T.errmsg system unsafe'
249
250exit $RESULT
251