1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
#!/usr/local/bin/bash
#
# Copyright (c) 2002, Stooges & Cueless CO., All rights reserved.
# Copyright (c) 2026, Robin Haberkorn
#
# Module:
# @(#)regtest_terex.sh 1.0 (Stooges & Clueless) 04/xx/02
# Purpose:
# @(#)Perform regression test to Henry Spencer's RE libary.
# Author:
# Walter Waldo
# History:
# 04/xx/02 (ww) Version 1.0
# 06/xx/26 (rh) Adaptions for terex
#
#set -x
H=$HOME
me=`basename $0`
rgsrc=regtest_terex.c
rgbin=regtest_terex
datsrc=regtest_data.c
datbin=regtest_data
CC=gcc
cleanup_and_exit()
{
rm -f $rgsrc $rgbin $datsrc $datbin
exit 1
}
usage ()
{
test X$1 != X && printf "$me: $1\n"
cat << EOF
$me: Perform regression test to Henry Spencer's RE libary.
Usage: $me [-h]
Options:
-h This stuff.
EOF
test X$1 != X && exit 1 || exit 0
}
. feedback_defs.sh
while getopts h FLAG; do
case $FLAG in
h) usage
;;
?) quit "Unknown option"
;;
esac
done
. regtest_util.sh
trap "trap '' 0; cleanup_and_exit 1" HUP INT QUIT PIPE TERM
trap "cleanup_and_exit $?" 0
printf "\nTesting Henry Spencer's REs\n\n"
#-----------------------------------
# Invocation:
# $rgbin <number_of_groups> <RE> <string2scan>
#
cat<<-EOF>$rgsrc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "regalone.h"
#include "regex.h"
size_t hexescapes2bin(unsigned char *t, char *src, size_t mxlen)
{
char *s, *xs;
size_t len;
s = xs = src;
len = 0;
while ( (s = strstr(s, "\\\x")) )
{
int cbin;
sscanf(&s[2], "%2x", &cbin);
memcpy(&t[len], xs, (size_t ) (s-xs));
len += (size_t ) (s-xs);
t[len++] = cbin;
s += 4;
xs = s;
}
strcpy((char *)&t[len], xs);
len += strlen(xs);
return len;
}
int main(int argc, char *argv[])
{
unsigned char re[1024*4], dat[1024*8];
size_t relen, datlen;
regex_t cre;
regmatch_t pmatch[100];
int nmatch, rc, ch;
int cflags = REG_ADVANCED, eflags = 0;
char buf[1024*2];
//memset(&cre, '\0', sizeof(cre));
while ((ch = getopt(argc, argv, "iraA")) != -1) {
switch (ch) {
case 'i': cflags |= REG_ICASE; break;
case 'r': cflags |= REG_RAW; break;
case 'a': cflags |= REG_BOSONLY; break;
case 'A': eflags |= REG_ANCHORED; break;
}
}
argc -= optind;
argv += optind;
nmatch = atoi(argv[0]);
if (!nmatch) cflags |= REG_NOSUB;
relen = hexescapes2bin(re, argv[1], sizeof(re)/sizeof(char));
datlen = hexescapes2bin(dat, argv[2], sizeof(dat)/sizeof(char));
rc = tere_comp(&cre, re, relen, cflags);
if ( rc != REG_OKAY )
{
regerror(rc, &cre, buf, sizeof(buf));
fprintf(stderr, "Compile error. %s\n", buf);
return 1;
}
if ( nmatch >= 0 && cre.re_nsub != nmatch )
{
fprintf(stderr,
"Mismatch on number of group patterns. "
"Expected %d, compiled %zu\n",
nmatch, cre.re_nsub);
return 1;
}
rc = tere_exec(&cre, dat, datlen, NULL, 100, pmatch, eflags);
if ( rc != REG_OKAY )
{
tere_error(rc, &cre, buf, sizeof(buf));
fprintf(stderr, "Execution error. %s\n", buf);
return 1;
}
if ( cre.re_nsub )
{
int i;
buf[0] = '\0';
for ( i=1; i<cre.re_nsub+1 && pmatch[i].rm_so>=0; i++ )
sprintf(&buf[strlen(buf)], "%s%.*s",
i>1 ? ":" : "",
(int)(pmatch[i].rm_eo-pmatch[i].rm_so),
argv[2]+pmatch[i].rm_so);
printf("%s\n", buf);
}
tere_free(&cre);
return 0;
}
EOF
PATH=.:$PATH
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH
$CC -Wall -g -O0 -I. -I$H/inc -L. -lterex -o $rgbin $rgsrc
#-----------------------------------
msg="Simple match"
resp=`$rgbin 0 "clavo" "Pablito clavo un clavito" 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 "clavo" "Pablito clavo un clavito" 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="yyyy-mm-dd between 1900-01-01 and 2099-12-31"
resp=`$rgbin 0 \
"(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])" \
"1960-10-12" 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 \
"(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])" \
"1960-10-12" 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="yyyy-mm-dd out of 1900-01-01 and 2099-12-31"
resp=`$rgbin 0 \
"(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])" \
"El arzobispo 1960-14-12 de Constantinopla" 2>&1`
if echo "$resp"|grep "failed to match">/dev/null;
then f_ok "$msg"; else f_no "$msg" "$resp"; fi
resp=`$rgbin -r 0 \
"(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])" \
"El arzobispo 1960-14-12 de Constantinopla" 2>&1`
if echo "$resp"|grep "failed to match">/dev/null;
then f_ok "$msg (raw)"; else f_no "$msg (raw)" "$resp"; fi
#-----------------------------------
msg="1..999"
resp=`$rgbin 0 "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$" "432" 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$" "432" 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="Bad 1..999"
resp=`$rgbin 0 "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$" " 4321" 2>&1`
if echo "$resp"|grep "failed to match">/dev/null;
then f_ok "$msg"; else f_no "$msg" "$resp"; fi
resp=`$rgbin -r 0 "^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$" " 4321" 2>&1`
if echo "$resp"|grep "failed to match">/dev/null;
then f_ok "$msg (raw)"; else f_no "$msg (raw)" "$resp"; fi
#-----------------------------------
msg="Quantifier: One to three words between 'word1' and 'word2'"
resp=`$rgbin 0 \
"word1\W+(?:\w+\W+){1,3}?word2" \
"word1 clavo un clavito word2" 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 \
"word1\W+(?:\w+\W+){1,3}?word2" \
"word1 clavo un clavito word2" 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="Pathological: a?^6a^15 against aaaaaaaaaaaaaaaaaa..."
resp=`$rgbin 0 \
"a?a?a?a?a?aaaaaaaaaaaaaaa" \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 \
"a?a?a?a?a?aaaaaaaaaaaaaaa" \
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="Pathological: (a|aa)*b against aaaaaaaaaaaaaaaaaa...b"
resp=`$rgbin 0 \
"(a|aa)*b" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 \
"(a|aa)*b" "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
cat<<-EOF>$datsrc
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef WIN32
# include <process.h>
# define getpid _getpid
#elif defined(unix) || defined(__unix__)
# include <unistd.h>
#else
# error unknown platform
#endif
char nums[] = "0123456789";
char alph[] = "abcdefghijklmnopqrstuvwxyz";
int main(int argc, char *argv[])
{
char dat[16], *arr;
int arrsz, datsz, i;
if ( isdigit(argv[1][0]) )
{ arr = nums; arrsz = sizeof(nums)-1; }
else if ( isalpha(argv[1][0]) )
{ arr = alph; arrsz = sizeof(alph)-1; }
srand(getpid());
datsz = rand()%13+1;
for ( i=0; i<datsz; i++ ) dat[i] = arr[ rand()%arrsz ];
dat[datsz] = '\0';
printf("%s\n", dat);
return 0;
}
EOF
$CC -o $datbin $datsrc
#-----------------------------------
i=0
totre="[a-zA-Z]+"
totdat=`$datbin a`
while test $i -lt 5; do
num=`$datbin 0`
alph=`$datbin a`
totre=$totre"([0-9]+)[a-zA-Z]+"
totdat=$totdat$num$alph
test $i -eq 0 && expectedresp=$num || expectedresp=$expectedresp:$num
i=`expr $i + 1`
done
msg="5 group patterns taken with bracket ranges"
resp=`$rgbin 5 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 5 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
i=0
totre="[a-zA-Z]+"
totdat=`$datbin a`
while test $i -lt 10; do
num=`$datbin 0`
alph=`$datbin a`
totre=$totre"([0-9]+)[a-zA-Z]+"
totdat=$totdat$num$alph
test $i -eq 0 && expectedresp=$num || expectedresp=$expectedresp:$num
i=`expr $i + 1`
done
msg="10 group patterns taken with bracket ranges"
resp=`$rgbin 10 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 10 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
i=0
totre="[a-zA-Z]+"
totdat=`$datbin a`
while test $i -lt 99; do
num=`$datbin 0`
alph=`$datbin a`
totre=$totre"([0-9]+)[a-zA-Z]+"
totdat=$totdat$num$alph
test $i -eq 0 && expectedresp=$num || expectedresp=$expectedresp:$num
i=`expr $i + 1`
done
msg="99 group patterns taken with bracket ranges"
resp=`$rgbin 99 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 99 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
i=0
totre="[[:alpha:]]+"
totdat=`$datbin a`
while test $i -lt 99; do
num=`$datbin 0`
alph=`$datbin a`
totre=$totre"([[:digit:]]+)[[:alpha:]]+"
totdat=$totdat$num$alph
test $i -eq 0 && expectedresp=$num || expectedresp=$expectedresp:$num
i=`expr $i + 1`
done
msg="99 group patterns taken with character classes"
resp=`$rgbin 99 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 99 "$totre" "$totdat" 2>&1`
test "$resp" = "$expectedresp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="Binary data"
resp=`$rgbin 0 "clavo" $'Pablito\01clavo un clavito' 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 "clavo" $'Pablito\01clavo un clavito' 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="Binary RE and data"
resp=`$rgbin 0 $'cl\xFFavo' $'Pablito\x01cl\xFFavo un clavito' 2>&1`
test -z "$resp" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 0 $'cl\xFFavo' $'Pablito\x01cl\xFFavo un clavito' 2>&1`
test -z "$resp" && f_ok "$msg (raw)" || f_no "$msg (raw)" "$resp"
#-----------------------------------
msg="One group pattern with case-insensitive matching"
resp=`$rgbin 1 "(?i)(clavo)" "Pablito ClAvO un clavito" 2>&1`
test "$resp" = "ClAvO" && f_ok "$msg" || f_no "$msg" "$resp"
resp=`$rgbin -r 1 "(?i)(clavo)" "Pablito ClAvO un clavito" 2>&1`
test "$resp" = "ClAvO" && f_ok "$msg (raw)" || f_no "$msg" "$resp (raw)"
#-----------------------------------
resp=`$rgbin -r 1 $'([\x01\x5F\xFF]+)' $'ABC\x5F' 2>&1`
msg="Raw character class"
test "$resp" = $'\x5F' && f_ok "$msg" || f_no "$msg" "$resp"
#-----------------------------------
resp=`$rgbin 1 '([[:alpha:]]+)' 'абвгд' 2>&1`
msg="Unicode character class"
test "$resp" = "абвгд" && f_ok "$msg" || f_no "$msg" "$resp"
#-----------------------------------
resp=`$rgbin -A 0 'ABC' 'blablaABC' 2>&1`
msg="Anchored search (failure)"
if echo "$resp"|grep "failed to match">/dev/null;
then f_ok "$msg"; else f_no "$msg" "$resp"; fi
resp=`$rgbin -A 1 '(ABC)' 'ABCblablaABC' 2>&1`
msg="Anchored search (success)"
test "$resp" = "ABC" && f_ok "$msg" || f_no "$msg" "$resp"
#-----------------------------------
|