11 int copts = REG_EXTENDED;
13 regoff_t startoff = 0;
18 extern void regprint();
21 - main - do the simple case, hand off to regress() for regression
41 while ((c = getopt(argc, argv, "c:e:S:E:x")) != EOF)
43 case 'c': /* compile options */
44 copts = options('c', optarg);
46 case 'e': /* execute options */
47 eopts = options('e', optarg);
49 case 'S': /* start offset */
50 startoff = (regoff_t)atoi(optarg);
52 case 'E': /* end offset */
53 endoff = (regoff_t)atoi(optarg);
55 case 'x': /* Debugging. */
64 fprintf(stderr, "usage: %s ", progname);
65 fprintf(stderr, "[-c copt][-C][-d] [re]\n");
74 err = regcomp(&re, argv[optind++], copts);
76 len = regerror(err, &re, erbuf, SIZEOF(erbuf));
77 fprintf(stderr, "error %s, %d/%d `%s'\n",
78 eprint(err), len, SIZEOF(erbuf), erbuf);
81 regprint(&re, stdout);
88 if (eopts®_STARTEND) {
89 subs[0].rm_so = startoff;
90 subs[0].rm_eo = strlen(argv[optind]) - endoff;
92 err = regexec(&re, argv[optind], (size_t)NS, subs, eopts);
94 len = regerror(err, &re, erbuf, SIZEOF(erbuf));
95 fprintf(stderr, "error %s, %d/%d `%s'\n",
96 eprint(err), len, SIZEOF(erbuf), erbuf);
99 if (!(copts®_NOSUB)) {
100 len = (int)(subs[0].rm_eo - subs[0].rm_so);
101 if (subs[0].rm_so != -1) {
103 printf("match `%.*s'\n", len,
104 argv[optind] + subs[0].rm_so);
106 printf("match `'@%.1s\n",
107 argv[optind] + subs[0].rm_so);
109 for (i = 1; i < NS; i++)
110 if (subs[i].rm_so != -1)
111 printf("(%d) `%.*s'\n", i,
112 (int)(subs[i].rm_eo - subs[i].rm_so),
113 argv[optind] + subs[i].rm_so);
119 - regress - main loop of regression test
120 == void regress(FILE *in);
133 char *badpat = "invalid regular expression";
135 char *bpname = "REG_BADPAT";
138 while (fgets(inbuf, (int)sizeof(inbuf), in) != NULL) {
140 if (inbuf[0] == '#' || inbuf[0] == '\n')
141 continue; /* NOTE CONTINUE */
142 inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */
144 fprintf(stdout, "%d:\n", line);
145 nf = split(inbuf, f, MAXF, "\t\t");
147 fprintf(stderr, "bad input, line %d\n", line);
150 for (i = 0; i < nf; i++)
151 if (strcmp(f[i], "\"\"") == 0)
157 try(f[0], f[1], f[2], f[3], f[4], options('c', f[1]));
158 if (opt('&', f[1])) /* try with either type of RE */
159 try(f[0], f[1], f[2], f[3], f[4],
160 options('c', f[1]) &~ REG_EXTENDED);
163 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, SIZEOF(erbuf));
164 if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
165 fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n",
169 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, (size_t)SHORT);
170 if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' ||
171 ne != strlen(badpat)+1) {
172 fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n",
173 erbuf, SHORT-1, badpat);
176 ne = regerror(REG_ITOA|REG_BADPAT, (regex_t *)NULL, erbuf, SIZEOF(erbuf));
177 if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) {
178 fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n",
183 ne = regerror(REG_ATOI, &re, erbuf, SIZEOF(erbuf));
184 if (atoi(erbuf) != (int)REG_BADPAT) {
185 fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n",
186 erbuf, (long)REG_BADPAT);
188 } else if (ne != strlen(erbuf)+1) {
189 fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
190 erbuf, (long)REG_BADPAT);
196 - try - try it, and report on problems
197 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
200 try(f0, f1, f2, f3, f4, opts)
206 int opts; /* may not match f1 */
210 regmatch_t subs[NSUBS];
212 char *should[NSHOULD];
217 char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
223 strncpy(f0copy, f0, SIZEOF(f0copy)-1);
224 f0copy[SIZEOF(f0copy)-1] = '\0';
225 re.re_endp = (opts®_PEND) ? f0copy + strlen(f0copy) : NULL;
227 err = regcomp(&re, f0copy, opts);
228 if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
229 /* unexpected error or wrong error */
230 len = regerror(err, &re, erbuf, SIZEOF(erbuf));
231 fprintf(stderr, "%d: %s error %s, %d/%d `%s'\n",
232 line, type, eprint(err), len,
233 SIZEOF(erbuf), erbuf);
235 } else if (err == 0 && opt('C', f1)) {
236 /* unexpected success */
237 fprintf(stderr, "%d: %s should have given REG_%s\n",
240 err = 1; /* so we won't try regexec */
248 strncpy(f2copy, f2, SIZEOF(f2copy)-1);
249 f2copy[SIZEOF(f2copy)-1] = '\0';
252 if (options('e', f1)®_STARTEND) {
253 if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL)
254 fprintf(stderr, "%d: bad STARTEND syntax\n", line);
255 subs[0].rm_so = strchr(f2, '(') - f2 + 1;
256 subs[0].rm_eo = strchr(f2, ')') - f2;
258 err = regexec(&re, f2copy, NSUBS, subs, options('e', f1));
260 if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) {
261 /* unexpected error or wrong error */
262 len = regerror(err, &re, erbuf, SIZEOF(erbuf));
263 fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n",
264 line, type, eprint(err), len,
265 SIZEOF(erbuf), erbuf);
267 } else if (err != 0) {
268 /* nothing more to check */
269 } else if (f3 == NULL) {
270 /* unexpected success */
271 fprintf(stderr, "%d: %s exec should have failed\n",
274 err = 1; /* just on principle */
275 } else if (opts®_NOSUB) {
276 /* nothing more to check */
277 } else if ((grump = check(f2, subs[0], f3)) != NULL) {
278 fprintf(stderr, "%d: %s %s\n", line, type, grump);
283 if (err != 0 || f4 == NULL) {
288 for (i = 1; i < NSHOULD; i++)
290 nshould = split(f4, should+1, NSHOULD-1, ",");
295 for (i = 1; i < NSUBS; i++) {
296 grump = check(f2, subs[i], should[i]);
298 fprintf(stderr, "%d: %s $%d %s\n", line,
309 - options - pick options out of a regression-test string
310 == int options(int type, char *s);
314 int type; /* 'c' compile, 'e' exec */
318 register int o = (type == 'c') ? copts : eopts;
319 register char *legal = (type == 'c') ? "bisnmp" : "^$#tl";
321 for (p = s; *p != '\0'; p++)
322 if (strchr(legal, *p) != NULL)
352 case 't': /* trace */
355 case 'l': /* force long representation */
358 case 'r': /* force backref use */
366 - opt - is a particular option in a regression string?
367 == int opt(int c, char *s);
374 return(strchr(s, c) != NULL);
378 - fixstr - transform magic characters in strings
379 == void fixstr(register char *p);
388 for (; *p != '\0'; p++)
400 - check - check a substring match
401 == char *check(char *str, regmatch_t sub, char *should);
403 char * /* NULL or complaint */
404 check(str, sub, should)
412 static char grump[500];
413 register char *at = NULL;
415 if (should != NULL && strcmp(should, "-") == 0)
417 if (should != NULL && should[0] == '@') {
422 /* check rm_so and rm_eo for consistency */
423 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
424 (sub.rm_so != -1 && sub.rm_eo == -1) ||
425 (sub.rm_so != -1 && sub.rm_so < 0) ||
426 (sub.rm_eo != -1 && sub.rm_eo < 0) ) {
427 snprintf(grump, SIZEOF(grump),
428 "start %ld end %ld", (long)sub.rm_so,
433 /* check for no match */
434 if (sub.rm_so == -1 && should == NULL)
437 return("did not match");
439 /* check for in range */
440 if (sub.rm_eo > strlen(str)) {
441 snprintf(grump, SIZEOF(grump),
442 "start %ld end %ld, past end of string",
443 (long)sub.rm_so, (long)sub.rm_eo);
447 len = (int)(sub.rm_eo - sub.rm_so);
448 shlen = (int)strlen(should);
451 /* check for not supposed to match */
452 if (should == NULL) {
453 snprintf(grump, SIZEOF(grump), "matched `%.*s'", len, p);
457 /* check for wrong match */
458 if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) {
459 snprintf(grump, SIZEOF(grump),
460 "matched `%.*s' instead", len, p);
466 /* check null match in right place */
471 shlen = 1; /* force check for end-of-string */
472 if (strncmp(p, at, shlen) != 0) {
473 snprintf(grump, SIZEOF(grump), "matched null at `%.20s'", p);
480 - eprint - convert error number to name
481 == static char *eprint(int err);
487 static char epbuf[100];
490 len = regerror(REG_ITOA|err, (regex_t *)NULL, epbuf, SIZEOF(epbuf));
491 assert(len <= SIZEOF(epbuf));
496 - efind - convert error name to number
497 == static int efind(char *name);
503 static char efbuf[100];
507 snprintf(efbuf, SIZEOF(efbuf), "REG_%s", name);
508 assert(strlen(efbuf) < SIZEOF(efbuf));
510 (void) regerror(REG_ATOI, &re, efbuf, SIZEOF(efbuf));