| 1 |
#!/bin/sh |
| 2 |
# $MawkId: mawkerrs,v 1.21 2024/12/11 00:43:22 tom Exp $ |
| 3 |
############################################################################### |
| 4 |
# copyright 2024, Thomas E. Dickey |
| 5 |
# |
| 6 |
# This is a source file for mawk, an implementation of |
| 7 |
# the AWK programming language. |
| 8 |
# |
| 9 |
# Mawk is distributed without warranty under the terms of |
| 10 |
# the GNU General Public License, version 2, 1991. |
| 11 |
############################################################################### |
| 12 |
|
| 13 |
: "${FGREP:=grep -F}" |
| 14 |
|
| 15 |
MYTEMP=`mktemp -d 2>/dev/null` |
| 16 |
if [ -z "$MYTEMP" ] |
| 17 |
then |
| 18 |
MYTEMP=${TMPDIR-/tmp}/mawktest$$ |
| 19 |
fi |
| 20 |
mkdir -p "$MYTEMP" || exit |
| 21 |
trap 'echo mawk_errs failed ; rm -rf "$MYTEMP"; exit 1' 0 |
| 22 |
|
| 23 |
STDOUT=$MYTEMP/mawk-out |
| 24 |
STDERR=$MYTEMP/mawk-err |
| 25 |
|
| 26 |
Fail() { |
| 27 |
echo "?? fail $*" |
| 28 |
FAIL=`expr "$FAIL" + 1` |
| 29 |
ERRS=`expr "$ERRS" + 1` |
| 30 |
} |
| 31 |
|
| 32 |
Begin() { |
| 33 |
echo |
| 34 |
echo "$*" |
| 35 |
FAIL=0 |
| 36 |
} |
| 37 |
|
| 38 |
Finish() { |
| 39 |
if test $FAIL = 1 |
| 40 |
then |
| 41 |
echo "$* had one failure" |
| 42 |
elif test $FAIL != 0 |
| 43 |
then |
| 44 |
echo "$* had $FAIL failures" |
| 45 |
else |
| 46 |
echo "$* OK" |
| 47 |
fi |
| 48 |
} |
| 49 |
|
| 50 |
Summary() { |
| 51 |
if test $ERRS = 1 |
| 52 |
then |
| 53 |
echo "$* had one failure" |
| 54 |
elif test $ERRS != 0 |
| 55 |
then |
| 56 |
echo "$* had $ERRS failures" |
| 57 |
else |
| 58 |
echo "$* OK" |
| 59 |
fi |
| 60 |
} |
| 61 |
|
| 62 |
ERRS=0 |
| 63 |
|
| 64 |
if [ -f ../mawk ] |
| 65 |
then |
| 66 |
PROG="${AWK:-../mawk}" |
| 67 |
else |
| 68 |
PROG="${AWK:-mawk}" |
| 69 |
fi |
| 70 |
|
| 71 |
####### init.c: |
| 72 |
|
| 73 |
$PROG -Wv >$MYTEMP/version |
| 74 |
$PROG -Wh >$MYTEMP/usage |
| 75 |
|
| 76 |
Begin "test for no parameters" |
| 77 |
$PROG 2>&1 | cmp -s - "$MYTEMP"/usage || Fail "no parameters" |
| 78 |
Finish "test for no parameters" |
| 79 |
|
| 80 |
Begin "test for only -" |
| 81 |
printf '' >$STDERR |
| 82 |
$PROG - 2>&1 | cmp -s - "$STDERR" || Fail "only -" |
| 83 |
Finish "test for only -" |
| 84 |
|
| 85 |
Begin "test for stray -" |
| 86 |
printf '' >$STDERR |
| 87 |
$PROG -f cclass.awk < $STDERR >$STDOUT |
| 88 |
cat $STDERR | $PROG -f cclass.awk - 2>&1 | cmp -s - "$STDOUT" || Fail "stray -" |
| 89 |
Finish "test for stray -" |
| 90 |
|
| 91 |
#?# errmsg(0, "ambiguous -W value: \"%.*s\" (%s vs %s)", |
| 92 |
#?$PROG -Wr 0 'BEGIN{}' |
| 93 |
|
| 94 |
# errmsg(0, "deprecated option, use -W posix"); |
| 95 |
Begin "test for deprecated option" |
| 96 |
echo 'mawk: deprecated option, use -W posix' >$STDERR |
| 97 |
$PROG -Wposix_space 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "deprecated option" |
| 98 |
Finish "test for deprecated option" |
| 99 |
|
| 100 |
# errmsg(0, "ambiguous long option: \"--%.*s\" (--%s vs --%s)", |
| 101 |
Begin "test for ambiguous long option" |
| 102 |
echo 'mawk: ambiguous long option: "--r" (--random vs --re-interval)' >$STDERR |
| 103 |
$PROG --r 0 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "ambiguous long option" |
| 104 |
Finish "test for ambiguous long option" |
| 105 |
|
| 106 |
# errmsg(0, "invalid numeric option: \"%s\"", source); |
| 107 |
Begin "test for invalid numeric option" |
| 108 |
echo 'mawk: invalid numeric option: "oops"' >$STDERR |
| 109 |
$PROG -Wra oops 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "invalid numeric option" |
| 110 |
Finish "test for invalid numeric option" |
| 111 |
|
| 112 |
# errmsg(0, "-W exec is incompatible with -f"); |
| 113 |
Begin "test for incompatible option" |
| 114 |
echo 'mawk: -W exec is incompatible with -f' >$STDERR |
| 115 |
date | $PROG -f cclass.awk -W exec=cclass.awk 2>&1 | cmp -s - "$STDERR" || Fail "incompatible option" |
| 116 |
Finish "test for incompatible option" |
| 117 |
|
| 118 |
Begin "test if -W exec works" |
| 119 |
echo 'mawk: -W exec is incompatible with -f' >$STDERR |
| 120 |
date | $PROG -W exec=cclass.awk >/dev/null 2>/dev/null || Fail "-W exec works" |
| 121 |
Finish "test if -W exec works" |
| 122 |
|
| 123 |
Begin "test if -W exec requires value" |
| 124 |
echo 'mawk: -W exec is incompatible with -f' >$STDERR |
| 125 |
date | $PROG -W exec >/dev/null 2>/dev/null || Fail "-W exec needs option value" |
| 126 |
Finish "test if -W exec requires value" |
| 127 |
|
| 128 |
# errmsg(0, "vacuous option: -W \"%s\"", option); |
| 129 |
Begin "test for vacuous option" |
| 130 |
echo 'mawk: vacuous option: -W "oops"' >$STDERR |
| 131 |
$PROG -Woops 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "vacuous option" |
| 132 |
Finish "test for vacuous option" |
| 133 |
|
| 134 |
# errmsg(0, "missing value for -W \"%.*s\"", length, option); |
| 135 |
Begin "test for missing value for -W option" |
| 136 |
echo 'mawk: missing value for -W "sprintf"' >$STDERR |
| 137 |
$PROG -Wsprintf 2>&1 | cmp -s - "$STDERR" || Fail "missing value for -W option" |
| 138 |
Finish "test for missing value for -W option" |
| 139 |
|
| 140 |
Begin "test for given value for -W sprintf option" |
| 141 |
printf '' > $STDERR |
| 142 |
$PROG -Wsprintf=10000 -Wsprintf=12000 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "given value for -W sprintf option" |
| 143 |
Finish "test for given value for -W sprintf option" |
| 144 |
|
| 145 |
# errmsg(0, "unexpected option value \"%s\"", option); |
| 146 |
Begin "test for unexpected option value" |
| 147 |
echo 'mawk: vacuous option: -W "lint=2"' >$STDERR |
| 148 |
echo 'mawk: unexpected option value "lint=2"' >>$STDERR |
| 149 |
$PROG -Wlint=2 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "unexpected option value" |
| 150 |
Finish "test for unexpected option value" |
| 151 |
|
| 152 |
# errmsg(0, "not an option: %s", s); |
| 153 |
Begin "test for not an option" |
| 154 |
echo 'mawk: not an option: -z' >$STDERR |
| 155 |
$PROG -z 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "not an option" |
| 156 |
Finish "test for not an option" |
| 157 |
|
| 158 |
# errmsg(0, "ignored option: %s", arg); |
| 159 |
Begin "test for warning about long option" |
| 160 |
echo 'mawk: ignored option: --version' >$STDERR |
| 161 |
echo 'mawk: not an option: --version' >>$STDERR |
| 162 |
MAWK_LONG_OPTIONS=warn $PROG --version 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "warning on long option" |
| 163 |
Finish "test for warning about long option" |
| 164 |
|
| 165 |
Begin "test for error on long option" |
| 166 |
echo 'mawk: not an option: --version' >$STDERR |
| 167 |
MAWK_LONG_OPTIONS=error $PROG --version 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "error on long option" |
| 168 |
Finish "test for error on long option" |
| 169 |
|
| 170 |
Begin "test for allowing long option" |
| 171 |
MAWK_LONG_OPTIONS=allow $PROG --version 'BEGIN{}' 2>&1 | cmp -s - "$MYTEMP"/version || Fail "allowing long option" |
| 172 |
Finish "test for allowing long option" |
| 173 |
|
| 174 |
# errmsg(0, "option %s lacks argument", curArg); |
| 175 |
Begin "test for option lacks argument" |
| 176 |
echo 'mawk: option -W lacks argument' >$STDERR |
| 177 |
$PROG -W 2>&1 | cmp -s - "$STDERR" || Fail "option lacks argument" |
| 178 |
Finish "test for option lacks argument" |
| 179 |
|
| 180 |
# errmsg(0, "improper assignment: -v %s", optArg); |
| 181 |
Begin "test for improper assignment" |
| 182 |
echo 'mawk: improper assignment: -v FOO' >$STDERR |
| 183 |
$PROG -vFOO 'BEGIN{}' 2>&1 | cmp -s - "$STDERR" || Fail "improper assignment" |
| 184 |
Finish "test for improper assignment" |
| 185 |
|
| 186 |
######## parse.y: |
| 187 |
Begin "test for syntax error" |
| 188 |
echo 'mawk: line 1: syntax error at or near if' >$STDERR |
| 189 |
$PROG -Wd 'BEGIN{ foo = length(if); print foo; }' 2>&1 | cmp -s - "$STDERR" || Fail "syntax error" |
| 190 |
Finish "test for syntax error" |
| 191 |
|
| 192 |
Begin "test for missing right-parenthesis" |
| 193 |
echo 'mawk: line 1: missing ) near ;' >$STDERR |
| 194 |
$PROG -Wd 'BEGIN{ foo = (1 + 0; print foo; }' 2>&1 | cmp -s - "$STDERR" || Fail "missing right parenthesis" |
| 195 |
Finish "test for missing right-parenthesis" |
| 196 |
|
| 197 |
Begin "test for missing right-parenthesis (file)" |
| 198 |
echo "mawk: $MYTEMP/script: line 1: missing ) near ;" >$STDERR |
| 199 |
echo 'BEGIN{ foo = (1 + 0; print foo; }' >$MYTEMP/script |
| 200 |
$PROG -Wd -f $MYTEMP/script 2>&1 | cmp -s - "$STDERR" || Fail "missing right parenthesis (file)" |
| 201 |
Finish "test for missing right-parenthesis (file)" |
| 202 |
|
| 203 |
Begin "test for missing right-brace" |
| 204 |
echo 'mawk: line 2: missing } near end of file' >$STDERR |
| 205 |
$PROG -Wd 'BEGIN{ printf "?\n"' 2>&1 | cmp -s - "$STDERR" || Fail "missing right brace" |
| 206 |
Finish "test for missing right-brace" |
| 207 |
|
| 208 |
Begin "test for enough arguments of index()" |
| 209 |
echo 'mawk: line 1: not enough arguments in call to index: 1 (need 2)' >$STDERR |
| 210 |
$PROG -Wd 'BEGIN{ foo = index("?"); }' 2>&1 | cmp -s - "$STDERR" || Fail "enough arguments for index()" |
| 211 |
Finish "test for enough arguments of index()" |
| 212 |
|
| 213 |
Begin "test for too many arguments of index()" |
| 214 |
echo 'mawk: line 1: too many arguments in call to index: 4 (maximum 2)' >$STDERR |
| 215 |
$PROG -Wd 'BEGIN{ foo = index("?",1,2,3); }' 2>&1 | cmp -s - "$STDERR" || Fail "too many arguments for index()" |
| 216 |
Finish "test for too many arguments of index()" |
| 217 |
|
| 218 |
Begin "test for too many arguments of print()" |
| 219 |
echo 'mawk: line 1: too many arguments in call to print: 256 (maximum 255)' >$STDERR |
| 220 |
$PROG -Wd 'BEGIN{ print 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ; }' 2>&1 | cmp -s - "$STDERR" || Fail "too many arguments for print()" |
| 221 |
Finish "test for too many arguments of print()" |
| 222 |
|
| 223 |
Begin "test for return outside function" |
| 224 |
echo 'mawk: line 1: return outside function body' >$STDERR |
| 225 |
$PROG -Wd 'BEGIN{ return ; }' 2>&1 | cmp -s - "$STDERR" || Fail "return outside function" |
| 226 |
Finish "test for return outside function" |
| 227 |
|
| 228 |
Begin "test for improper use of next" |
| 229 |
echo 'mawk: line 1: improper use of next' >$STDERR |
| 230 |
$PROG -Wd 'function foo() { next; }BEGIN{ }' 2>&1 | cmp -s - "$STDERR" || Fail "improper use of next" |
| 231 |
Finish "test for improper use of next" |
| 232 |
|
| 233 |
Begin "test for improper use of nextfile" |
| 234 |
echo 'mawk: line 1: improper use of nextfile' >$STDERR |
| 235 |
$PROG -Wd 'function foo() { nextfile; }BEGIN{ }' 2>&1 | cmp -s - "$STDERR" || Fail "improper use of nextfile" |
| 236 |
Finish "test for improper use of nextfile" |
| 237 |
|
| 238 |
Begin "test for redefinition of function" |
| 239 |
echo 'mawk: line 1: redefinition of foo' >$STDERR |
| 240 |
$PROG -Wd 'function foo() { print; }function foo(){ printf "?"; }BEGIN{ }' 2>&1 | cmp -s - "$STDERR" || Fail "redefinition of function" |
| 241 |
Finish "test for redefinition of function" |
| 242 |
|
| 243 |
Begin "test for illegal reference to variable" |
| 244 |
echo 'mawk: line 1: illegal reference to variable bar' >$STDERR |
| 245 |
$PROG -Wd 'function foo() { bar = 1; }function bar(){ }BEGIN{ print(); }' 2>&1 | cmp -s - "$STDERR" || Fail "illegal reference to variable" |
| 246 |
Finish "test for illegal reference to variable" |
| 247 |
|
| 248 |
Begin "test for illegal reference to variable NR" |
| 249 |
echo 'mawk: line 1: illegal reference to variable NR' >$STDERR |
| 250 |
$PROG -Wd '{ NR[1] = "alpha"; }' 2>&1 | cmp -s - "$STDERR" || Fail "illegal reference to array ENVIRON" |
| 251 |
Finish "test for illegal reference to variable NR" |
| 252 |
|
| 253 |
Begin "test for illegal reference to array" |
| 254 |
echo 'mawk: line 1: illegal reference to array A' >$STDERR |
| 255 |
$PROG -Wd 'function foo() { A[1] = 1; }function A(){ }BEGIN{ print(); }' 2>&1 | cmp -s - "$STDERR" || Fail "illegal reference to array" |
| 256 |
Finish "test for illegal reference to array" |
| 257 |
|
| 258 |
Begin "test for illegal reference to array ENVIRON" |
| 259 |
echo 'mawk: line 1: illegal reference to array ENVIRON' >$STDERR |
| 260 |
$PROG -Wd '{ ENVIRON = "alpha"; }' 2>&1 | cmp -s - "$STDERR" || Fail "illegal reference to array ENVIRON" |
| 261 |
Finish "test for illegal reference to array ENVIRON" |
| 262 |
|
| 263 |
Begin "test for reference to undefined function" |
| 264 |
echo 'mawk: line 2: function f never defined' >$STDERR |
| 265 |
$PROG -Wd '{ f(z); }' 2>&1 | cmp -s - "$STDERR" || Fail "illegal reference to undefined function" |
| 266 |
Finish "test for reference to undefined function" |
| 267 |
|
| 268 |
######## print.c |
| 269 |
|
| 270 |
Begin "test sprintf buffer-overflow" |
| 271 |
echo 'mawk: program limit exceeded: sprintf buffer size=8192' >$STDERR |
| 272 |
echo ' FILENAME="" FNR=0 NR=0' >>$STDERR |
| 273 |
$PROG 'BEGIN{ foo = sprintf("%10000s\n", "x"); print length(foo); }' 2>&1 | cmp -s - "$STDERR" || Fail "sprintf buffer-overflow" |
| 274 |
Finish "test sprintf buffer-overflow" |
| 275 |
|
| 276 |
######## scan.c |
| 277 |
|
| 278 |
Begin "test for unexpected character" |
| 279 |
echo 'mawk: 1: unexpected character 0x7f' >$STDERR |
| 280 |
$PROG -Wd 'BEGIN{ printf "?" }' 2>&1 | cmp -s - "$STDERR" || Fail "unexpected character" |
| 281 |
Finish "test for unexpected character" |
| 282 |
|
| 283 |
Begin "test for runaway string constant" |
| 284 |
echo 'mawk: line 1: runaway string constant " } ...' >$STDERR |
| 285 |
$PROG -Wd 'BEGIN{ printf "?"?" }' 2>&1 | cmp -s - "$STDERR" || Fail "runaway string constant" |
| 286 |
Finish "test for runaway string constant" |
| 287 |
|
| 288 |
Begin "test for runaway regular expression" |
| 289 |
echo 'mawk: line 1: runaway regular expression /xxx{ print ...' >$STDERR |
| 290 |
$PROG -Wd '/xxx{ print }' 2>&1 | cmp -s - "$STDERR" || Fail "runaway regular expression" |
| 291 |
Finish "test for runaway regular expression" |
| 292 |
|
| 293 |
Begin "test for extra right-brace" |
| 294 |
echo "mawk: line 1: extra '}'" >$STDERR |
| 295 |
echo 'mawk: line 1: syntax error at or near end of line' >>$STDERR |
| 296 |
$PROG -Wd 'BEGIN{ printf "?\n" } }' 2>&1 | cmp -s - "$STDERR" || Fail "extra right brace" |
| 297 |
Finish "test for extra right-brace" |
| 298 |
|
| 299 |
echo |
| 300 |
echo "##################################################" |
| 301 |
Summary "tested $PROG" |
| 302 |
echo "##################################################" |
| 303 |
|
| 304 |
trap 0 |
| 305 |
rm -rf "$MYTEMP" |
| 306 |
exit $ERRS |
| 307 |
# vile: ts=4 sw=4 |