14
int copts = REG_EXTENDED;
16
regoff_t startoff = 0;
20
extern int split(char *string, char *fields[], int nfields, char *sep);
21
extern void regprint(my_regex_t *r, FILE *d);
24
- main - do the simple case, hand off to regress() for regression
26
int main(int argc, char **argv)
30
my_regmatch_t subs[NS];
40
while ((c = getopt(argc, argv, "c:e:S:E:x")) != EOF)
42
case 'c': /* compile options */
43
copts = options('c', optarg);
45
case 'e': /* execute options */
46
eopts = options('e', optarg);
48
case 'S': /* start offset */
49
startoff = (regoff_t)atoi(optarg);
51
case 'E': /* end offset */
52
endoff = (regoff_t)atoi(optarg);
54
case 'x': /* Debugging. */
63
fprintf(stderr, "usage: %s ", progname);
64
fprintf(stderr, "[-c copt][-C][-d] [re]\n");
73
err = my_regcomp(&re, argv[optind++], copts, &my_charset_latin1);
75
len = my_regerror(err, &re, erbuf, sizeof(erbuf));
76
fprintf(stderr, "error %s, %d/%d `%s'\n",
77
eprint(err), (int) len, (int) sizeof(erbuf), erbuf);
80
regprint(&re, stdout);
87
if (eopts®_STARTEND) {
88
subs[0].rm_so = startoff;
89
subs[0].rm_eo = strlen(argv[optind]) - endoff;
91
err = my_regexec(&re, argv[optind], (size_t)NS, subs, eopts);
93
len = my_regerror(err, &re, erbuf, sizeof(erbuf));
94
fprintf(stderr, "error %s, %d/%d `%s'\n",
95
eprint(err), (int) len, (int) sizeof(erbuf), erbuf);
98
if (!(copts®_NOSUB)) {
99
len = (int)(subs[0].rm_eo - subs[0].rm_so);
100
if (subs[0].rm_so != -1) {
102
printf("match `%.*s'\n", (int)len,
103
argv[optind] + subs[0].rm_so);
105
printf("match `'@%.1s\n",
106
argv[optind] + subs[0].rm_so);
108
for (i = 1; i < NS; i++)
109
if (subs[i].rm_so != -1)
110
printf("(%d) `%.*s'\n", i,
111
(int)(subs[i].rm_eo - subs[i].rm_so),
112
argv[optind] + subs[i].rm_so);
118
- regress - main loop of regression test
119
== void regress(FILE *in);
132
const char *badpat = "invalid regular expression";
134
const char *bpname = "REG_BADPAT";
137
while (fgets(inbuf, sizeof(inbuf), in) != NULL) {
139
if (inbuf[0] == '#' || inbuf[0] == '\n')
140
continue; /* NOTE CONTINUE */
141
inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */
143
fprintf(stdout, "%d:\n", line);
144
nf = split(inbuf, f, MAXF, "\t\t");
146
fprintf(stderr, "bad input, line %d\n", line);
149
for (i = 0; i < nf; i++)
150
if (strcmp(f[i], "\"\"") == 0)
156
rx_try(f[0], f[1], f[2], f[3], f[4], options('c', f[1]));
157
if (opt('&', f[1])) /* try with either type of RE */
158
rx_try(f[0], f[1], f[2], f[3], f[4],
159
options('c', f[1]) &~ REG_EXTENDED);
162
ne = my_regerror(REG_BADPAT, (my_regex_t *)NULL, erbuf, sizeof(erbuf));
163
if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
164
fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n",
168
ne = my_regerror(REG_BADPAT, (my_regex_t *)NULL, erbuf, (size_t)SHORT);
169
if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' ||
170
ne != strlen(badpat)+1) {
171
fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n",
172
erbuf, SHORT-1, badpat);
175
ne = my_regerror(REG_ITOA|REG_BADPAT, (my_regex_t *)NULL, erbuf, sizeof(erbuf));
176
if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) {
177
fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n",
182
ne = my_regerror(REG_ATOI, &re, erbuf, sizeof(erbuf));
183
if (atoi(erbuf) != (int)REG_BADPAT) {
184
fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n",
185
erbuf, (long)REG_BADPAT);
187
} else if (ne != strlen(erbuf)+1) {
188
fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
189
erbuf, (long)REG_BADPAT);
195
- rx_try - try it, and report on problems
196
== void rx_try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
199
rx_try(f0, f1, f2, f3, f4, opts)
205
int opts; /* may not match f1 */
209
my_regmatch_t subs[NSUBS];
211
char *should[NSHOULD];
216
const char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
223
re.re_endp = (opts®_PEND) ? f0copy + strlen(f0copy) : NULL;
225
err = my_regcomp(&re, f0copy, opts, &my_charset_latin1);
226
if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
227
/* unexpected error or wrong error */
228
len = my_regerror(err, &re, erbuf, sizeof(erbuf));
229
fprintf(stderr, "%d: %s error %s, %d/%d `%s'\n",
230
line, type, eprint(err), len,
231
(int) sizeof(erbuf), erbuf);
233
} else if (err == 0 && opt('C', f1)) {
234
/* unexpected success */
235
fprintf(stderr, "%d: %s should have given REG_%s\n",
238
err = 1; /* so we won't try regexec */
249
if (options('e', f1)®_STARTEND) {
250
if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL)
251
fprintf(stderr, "%d: bad STARTEND syntax\n", line);
252
subs[0].rm_so = strchr(f2, '(') - f2 + 1;
253
subs[0].rm_eo = strchr(f2, ')') - f2;
255
err = my_regexec(&re, f2copy, NSUBS, subs, options('e', f1));
257
if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) {
258
/* unexpected error or wrong error */
259
len = my_regerror(err, &re, erbuf, sizeof(erbuf));
260
fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n",
261
line, type, eprint(err), len,
262
(int) sizeof(erbuf), erbuf);
264
} else if (err != 0) {
265
/* nothing more to check */
266
} else if (f3 == NULL) {
267
/* unexpected success */
268
fprintf(stderr, "%d: %s exec should have failed\n",
271
err = 1; /* just on principle */
272
} else if (opts®_NOSUB) {
273
/* nothing more to check */
274
} else if ((grump = check(f2, subs[0], f3)) != NULL) {
275
fprintf(stderr, "%d: %s %s\n", line, type, grump);
280
if (err != 0 || f4 == NULL) {
285
for (i = 1; i < NSHOULD; i++)
287
nshould = split(f4, should+1, NSHOULD-1, ",");
290
should[1] = (char*) "";
292
for (i = 1; i < NSUBS; i++) {
293
grump = check(f2, subs[i], should[i]);
295
fprintf(stderr, "%d: %s $%d %s\n", line,
306
- options - pick options out of a regression-test string
307
== int options(int type, char *s);
311
int type; /* 'c' compile, 'e' exec */
315
register int o = (type == 'c') ? copts : eopts;
316
register const char *legal = (type == 'c') ? "bisnmp" : "^$#tl";
318
for (p = s; *p != '\0'; p++)
319
if (strchr(legal, *p) != NULL)
349
case 't': /* trace */
352
case 'l': /* force long representation */
355
case 'r': /* force backref use */
363
- opt - is a particular option in a regression string?
364
== int opt(int c, char *s);
371
return(strchr(s, c) != NULL);
375
- fixstr - transform magic characters in strings
376
== void fixstr(register char *p);
385
for (; *p != '\0'; p++)
397
- check - check a substring match
398
== char *check(char *str, regmatch_t sub, char *should);
400
char * /* NULL or complaint */
401
check(str, sub, should)
409
static char grump[500];
410
register char *at = NULL;
412
if (should != NULL && strcmp(should, "-") == 0)
414
if (should != NULL && should[0] == '@') {
419
/* check rm_so and rm_eo for consistency */
420
if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
421
(sub.rm_so != -1 && sub.rm_eo == -1) ||
422
(sub.rm_so != -1 && sub.rm_so < 0) ||
423
(sub.rm_eo != -1 && sub.rm_eo < 0) ) {
424
sprintf(grump, "start %ld end %ld", (long)sub.rm_so,
429
/* check for no match */
430
if (sub.rm_so == -1 && should == NULL)
433
return((char*) "did not match");
435
/* check for in range */
436
if ((int) sub.rm_eo > (int) strlen(str)) {
437
sprintf(grump, "start %ld end %ld, past end of string",
438
(long)sub.rm_so, (long)sub.rm_eo);
442
len = (int)(sub.rm_eo - sub.rm_so);
443
shlen = (int)strlen(should);
446
/* check for not supposed to match */
447
if (should == NULL) {
448
sprintf(grump, "matched `%.*s'", len, p);
452
/* check for wrong match */
453
if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) {
454
sprintf(grump, "matched `%.*s' instead", len, p);
460
/* check null match in right place */
465
shlen = 1; /* force check for end-of-string */
466
if (strncmp(p, at, shlen) != 0) {
467
sprintf(grump, "matched null at `%.20s'", p);
474
- eprint - convert error number to name
475
== static char *eprint(int err);
481
static char epbuf[100];
484
len = my_regerror(REG_ITOA|err, (my_regex_t *)NULL, epbuf, sizeof(epbuf));
485
assert(len <= sizeof(epbuf));
490
- efind - convert error name to number
491
== static int efind(char *name);
497
static char efbuf[100];
500
sprintf(efbuf, "REG_%s", name);
501
assert(strlen(efbuf) < sizeof(efbuf));
503
(void) my_regerror(REG_ATOI, &re, efbuf, sizeof(efbuf));