14 int copts = REG_EXTENDED;
16 regoff_t startoff = 0;
21 extern void regprint();
24 - main - do the simple case, hand off to regress() for regression
44 while ((c = getopt(argc, argv, "c:e:S:E:x")) != EOF)
46 case 'c': /* compile options */
47 copts = options('c', optarg);
49 case 'e': /* execute options */
50 eopts = options('e', optarg);
52 case 'S': /* start offset */
53 startoff = (regoff_t)atoi(optarg);
55 case 'E': /* end offset */
56 endoff = (regoff_t)atoi(optarg);
58 case 'x': /* Debugging. */
67 fprintf(stderr, "usage: %s ", progname);
68 fprintf(stderr, "[-c copt][-C][-d] [re]\n");
77 err = regcomp(&re, argv[optind++], copts);
79 len = regerror(err, &re, erbuf, sizeof(erbuf));
80 fprintf(stderr, "error %s, %d/%d `%s'\n",
81 eprint(err), len, sizeof(erbuf), erbuf);
84 regprint(&re, stdout);
91 if (eopts®_STARTEND) {
92 subs[0].rm_so = startoff;
93 subs[0].rm_eo = strlen(argv[optind]) - endoff;
95 err = regexec(&re, argv[optind], (size_t)NS, subs, eopts);
97 len = regerror(err, &re, erbuf, sizeof(erbuf));
98 fprintf(stderr, "error %s, %d/%d `%s'\n",
99 eprint(err), len, sizeof(erbuf), erbuf);
102 if (!(copts®_NOSUB)) {
103 len = (int)(subs[0].rm_eo - subs[0].rm_so);
104 if (subs[0].rm_so != -1) {
106 printf("match `%.*s'\n", len,
107 argv[optind] + subs[0].rm_so);
109 printf("match `'@%.1s\n",
110 argv[optind] + subs[0].rm_so);
112 for (i = 1; i < NS; i++)
113 if (subs[i].rm_so != -1)
114 printf("(%d) `%.*s'\n", i,
115 (int)(subs[i].rm_eo - subs[i].rm_so),
116 argv[optind] + subs[i].rm_so);
122 - regress - main loop of regression test
123 == void regress(FILE *in);
136 char *badpat = "invalid regular expression";
138 char *bpname = "REG_BADPAT";
141 while (fgets(inbuf, sizeof(inbuf), in) != NULL) {
143 if (inbuf[0] == '#' || inbuf[0] == '\n')
144 continue; /* NOTE CONTINUE */
145 inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */
147 fprintf(stdout, "%d:\n", line);
148 nf = split(inbuf, f, MAXF, "\t\t");
150 fprintf(stderr, "bad input, line %d\n", line);
153 for (i = 0; i < nf; i++)
154 if (strcmp(f[i], "\"\"") == 0)
160 try(f[0], f[1], f[2], f[3], f[4], options('c', f[1]));
161 if (opt('&', f[1])) /* try with either type of RE */
162 try(f[0], f[1], f[2], f[3], f[4],
163 options('c', f[1]) &~ REG_EXTENDED);
166 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
167 if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
168 fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n",
172 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, (size_t)SHORT);
173 if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' ||
174 ne != strlen(badpat)+1) {
175 fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n",
176 erbuf, SHORT-1, badpat);
179 ne = regerror(REG_ITOA|REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
180 if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) {
181 fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n",
186 ne = regerror(REG_ATOI, &re, erbuf, sizeof(erbuf));
187 if (atoi(erbuf) != (int)REG_BADPAT) {
188 fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n",
189 erbuf, (long)REG_BADPAT);
191 } else if (ne != strlen(erbuf)+1) {
192 fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
193 erbuf, (long)REG_BADPAT);
199 - try - try it, and report on problems
200 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
203 try(f0, f1, f2, f3, f4, opts)
209 int opts; /* may not match f1 */
213 regmatch_t subs[NSUBS];
215 char *should[NSHOULD];
220 char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
226 strncpy(f0copy, f0, sizeof(f0copy)-1);
227 f0copy[sizeof(f0copy)-1] = '\0';
228 re.re_endp = (opts®_PEND) ? f0copy + strlen(f0copy) : NULL;
230 err = regcomp(&re, f0copy, opts);
231 if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
232 /* unexpected error or wrong error */
233 len = regerror(err, &re, erbuf, sizeof(erbuf));
234 fprintf(stderr, "%d: %s error %s, %d/%d `%s'\n",
235 line, type, eprint(err), len,
236 sizeof(erbuf), erbuf);
238 } else if (err == 0 && opt('C', f1)) {
239 /* unexpected success */
240 fprintf(stderr, "%d: %s should have given REG_%s\n",
243 err = 1; /* so we won't try regexec */
251 strncpy(f2copy, f2, sizeof(f2copy)-1);
252 f2copy[sizeof(f2copy)-1] = '\0';
255 if (options('e', f1)®_STARTEND) {
256 if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL)
257 fprintf(stderr, "%d: bad STARTEND syntax\n", line);
258 subs[0].rm_so = strchr(f2, '(') - f2 + 1;
259 subs[0].rm_eo = strchr(f2, ')') - f2;
261 err = regexec(&re, f2copy, NSUBS, subs, options('e', f1));
263 if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) {
264 /* unexpected error or wrong error */
265 len = regerror(err, &re, erbuf, sizeof(erbuf));
266 fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n",
267 line, type, eprint(err), len,
268 sizeof(erbuf), erbuf);
270 } else if (err != 0) {
271 /* nothing more to check */
272 } else if (f3 == NULL) {
273 /* unexpected success */
274 fprintf(stderr, "%d: %s exec should have failed\n",
277 err = 1; /* just on principle */
278 } else if (opts®_NOSUB) {
279 /* nothing more to check */
280 } else if ((grump = check(f2, subs[0], f3)) != NULL) {
281 fprintf(stderr, "%d: %s %s\n", line, type, grump);
286 if (err != 0 || f4 == NULL) {
291 for (i = 1; i < NSHOULD; i++)
293 nshould = split(f4, should+1, NSHOULD-1, ",");
298 for (i = 1; i < NSUBS; i++) {
299 grump = check(f2, subs[i], should[i]);
301 fprintf(stderr, "%d: %s $%d %s\n", line,
312 - options - pick options out of a regression-test string
313 == int options(int type, char *s);
317 int type; /* 'c' compile, 'e' exec */
321 register int o = (type == 'c') ? copts : eopts;
322 register char *legal = (type == 'c') ? "bisnmp" : "^$#tl";
324 for (p = s; *p != '\0'; p++)
325 if (strchr(legal, *p) != NULL)
355 case 't': /* trace */
358 case 'l': /* force long representation */
361 case 'r': /* force backref use */
369 - opt - is a particular option in a regression string?
370 == int opt(int c, char *s);
377 return(strchr(s, c) != NULL);
381 - fixstr - transform magic characters in strings
382 == void fixstr(register char *p);
391 for (; *p != '\0'; p++)
403 - check - check a substring match
404 == char *check(char *str, regmatch_t sub, char *should);
406 char * /* NULL or complaint */
407 check(str, sub, should)
415 static char grump[500];
416 register char *at = NULL;
418 if (should != NULL && strcmp(should, "-") == 0)
420 if (should != NULL && should[0] == '@') {
425 /* check rm_so and rm_eo for consistency */
426 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
427 (sub.rm_so != -1 && sub.rm_eo == -1) ||
428 (sub.rm_so != -1 && sub.rm_so < 0) ||
429 (sub.rm_eo != -1 && sub.rm_eo < 0) ) {
430 snprintf(grump, sizeof(grump),
431 "start %ld end %ld", (long)sub.rm_so,
436 /* check for no match */
437 if (sub.rm_so == -1 && should == NULL)
440 return("did not match");
442 /* check for in range */
443 if (sub.rm_eo > strlen(str)) {
444 snprintf(grump, sizeof(grump),
445 "start %ld end %ld, past end of string",
446 (long)sub.rm_so, (long)sub.rm_eo);
450 len = (int)(sub.rm_eo - sub.rm_so);
451 shlen = (int)strlen(should);
454 /* check for not supposed to match */
455 if (should == NULL) {
456 snprintf(grump, sizeof(grump), "matched `%.*s'", len, p);
460 /* check for wrong match */
461 if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) {
462 snprintf(grump, sizeof(grump),
463 "matched `%.*s' instead", len, p);
469 /* check null match in right place */
474 shlen = 1; /* force check for end-of-string */
475 if (strncmp(p, at, shlen) != 0) {
476 snprintf(grump, sizeof(grump), "matched null at `%.20s'", p);
483 - eprint - convert error number to name
484 == static char *eprint(int err);
490 static char epbuf[100];
493 len = regerror(REG_ITOA|err, (regex_t *)NULL, epbuf, sizeof(epbuf));
494 assert(len <= sizeof(epbuf));
499 - efind - convert error name to number
500 == static int efind(char *name);
506 static char efbuf[100];
510 snprintf(efbuf, sizeof(efbuf), "REG_%s", name);
511 assert(strlen(efbuf) < sizeof(efbuf));
513 (void) regerror(REG_ATOI, &re, efbuf, sizeof(efbuf));