17 int copts = REG_EXTENDED;
19 regoff_t startoff = 0;
24 extern void regprint();
27 - main - do the simple case, hand off to regress() for regression
47 while ((c = getopt(argc, argv, "c:e:S:E:x")) != -1)
49 case 'c': /* compile options */
50 copts = options('c', optarg);
52 case 'e': /* execute options */
53 eopts = options('e', optarg);
55 case 'S': /* start offset */
56 startoff = (regoff_t)atoi(optarg);
58 case 'E': /* end offset */
59 endoff = (regoff_t)atoi(optarg);
61 case 'x': /* Debugging. */
70 fprintf(stderr, "usage: %s ", progname);
71 fprintf(stderr, "[-c copt][-C][-d] [re]\n");
80 err = regcomp(&re, argv[optind++], copts);
82 len = regerror(err, &re, erbuf, sizeof(erbuf));
83 fprintf(stderr, "error %s, %d/%d `%s'\n",
84 eprint(err), len, sizeof(erbuf), erbuf);
87 regprint(&re, stdout);
94 if (eopts®_STARTEND) {
95 subs[0].rm_so = startoff;
96 subs[0].rm_eo = strlen(argv[optind]) - endoff;
98 err = regexec(&re, argv[optind], (size_t)NS, subs, eopts);
100 len = regerror(err, &re, erbuf, sizeof(erbuf));
101 fprintf(stderr, "error %s, %d/%d `%s'\n",
102 eprint(err), len, sizeof(erbuf), erbuf);
105 if (!(copts®_NOSUB)) {
106 len = (int)(subs[0].rm_eo - subs[0].rm_so);
107 if (subs[0].rm_so != -1) {
109 printf("match `%.*s'\n", len,
110 argv[optind] + subs[0].rm_so);
112 printf("match `'@%.1s\n",
113 argv[optind] + subs[0].rm_so);
115 for (i = 1; i < NS; i++)
116 if (subs[i].rm_so != -1)
117 printf("(%d) `%.*s'\n", i,
118 (int)(subs[i].rm_eo - subs[i].rm_so),
119 argv[optind] + subs[i].rm_so);
125 - regress - main loop of regression test
126 == void regress(FILE *in);
139 char *badpat = "invalid regular expression";
141 char *bpname = "REG_BADPAT";
144 while (fgets(inbuf, sizeof(inbuf), in) != NULL) {
146 if (inbuf[0] == '#' || inbuf[0] == '\n')
147 continue; /* NOTE CONTINUE */
148 inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */
150 fprintf(stdout, "%d:\n", line);
151 nf = split(inbuf, f, MAXF, "\t\t");
153 fprintf(stderr, "bad input, line %d\n", line);
156 for (i = 0; i < nf; i++)
157 if (strcmp(f[i], "\"\"") == 0)
163 try(f[0], f[1], f[2], f[3], f[4], options('c', f[1]));
164 if (opt('&', f[1])) /* try with either type of RE */
165 try(f[0], f[1], f[2], f[3], f[4],
166 options('c', f[1]) &~ REG_EXTENDED);
169 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
170 if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
171 fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n",
175 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, (size_t)SHORT);
176 if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' ||
177 ne != strlen(badpat)+1) {
178 fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n",
179 erbuf, SHORT-1, badpat);
182 ne = regerror(REG_ITOA|REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
183 if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) {
184 fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n",
189 ne = regerror(REG_ATOI, &re, erbuf, sizeof(erbuf));
190 if (atoi(erbuf) != (int)REG_BADPAT) {
191 fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n",
192 erbuf, (long)REG_BADPAT);
194 } else if (ne != strlen(erbuf)+1) {
195 fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
196 erbuf, (long)REG_BADPAT);
202 - try - try it, and report on problems
203 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
206 try(f0, f1, f2, f3, f4, opts)
212 int opts; /* may not match f1 */
216 regmatch_t subs[NSUBS];
218 char *should[NSHOULD];
223 char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
230 re.re_endp = (opts®_PEND) ? f0copy + strlen(f0copy) : NULL;
232 err = regcomp(&re, f0copy, opts);
233 if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
234 /* unexpected error or wrong error */
235 len = regerror(err, &re, erbuf, sizeof(erbuf));
236 fprintf(stderr, "%d: %s error %s, %d/%d `%s'\n",
237 line, type, eprint(err), len,
238 sizeof(erbuf), erbuf);
240 } else if (err == 0 && opt('C', f1)) {
241 /* unexpected success */
242 fprintf(stderr, "%d: %s should have given REG_%s\n",
245 err = 1; /* so we won't try regexec */
256 if (options('e', f1)®_STARTEND) {
257 if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL)
258 fprintf(stderr, "%d: bad STARTEND syntax\n", line);
259 subs[0].rm_so = strchr(f2, '(') - f2 + 1;
260 subs[0].rm_eo = strchr(f2, ')') - f2;
262 err = regexec(&re, f2copy, NSUBS, subs, options('e', f1));
264 if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) {
265 /* unexpected error or wrong error */
266 len = regerror(err, &re, erbuf, sizeof(erbuf));
267 fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n",
268 line, type, eprint(err), len,
269 sizeof(erbuf), erbuf);
271 } else if (err != 0) {
272 /* nothing more to check */
273 } else if (f3 == NULL) {
274 /* unexpected success */
275 fprintf(stderr, "%d: %s exec should have failed\n",
278 err = 1; /* just on principle */
279 } else if (opts®_NOSUB) {
280 /* nothing more to check */
281 } else if ((grump = check(f2, subs[0], f3)) != NULL) {
282 fprintf(stderr, "%d: %s %s\n", line, type, grump);
287 if (err != 0 || f4 == NULL) {
292 for (i = 1; i < NSHOULD; i++)
294 nshould = split(f4, should+1, NSHOULD-1, ",");
299 for (i = 1; i < NSUBS; i++) {
300 grump = check(f2, subs[i], should[i]);
302 fprintf(stderr, "%d: %s $%d %s\n", line,
313 - options - pick options out of a regression-test string
314 == int options(int type, char *s);
318 int type; /* 'c' compile, 'e' exec */
322 int o = (type == 'c') ? copts : eopts;
323 char *legal = (type == 'c') ? "bisnmp" : "^$#tl";
325 for (p = s; *p != '\0'; p++)
326 if (strchr(legal, *p) != NULL)
356 case 't': /* trace */
359 case 'l': /* force long representation */
362 case 'r': /* force backref use */
370 - opt - is a particular option in a regression string?
371 == int opt(int c, char *s);
378 return(strchr(s, c) != NULL);
382 - fixstr - transform magic characters in strings
383 == void fixstr(char *p);
392 for (; *p != '\0'; p++)
404 - check - check a substring match
405 == char *check(char *str, regmatch_t sub, char *should);
407 char * /* NULL or complaint */
408 check(str, sub, should)
416 static char grump[500];
419 if (should != NULL && strcmp(should, "-") == 0)
421 if (should != NULL && should[0] == '@') {
426 /* check rm_so and rm_eo for consistency */
427 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
428 (sub.rm_so != -1 && sub.rm_eo == -1) ||
429 (sub.rm_so != -1 && sub.rm_so < 0) ||
430 (sub.rm_eo != -1 && sub.rm_eo < 0) ) {
431 sprintf(grump, "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 sprintf(grump, "start %ld end %ld, past end of string",
445 (long)sub.rm_so, (long)sub.rm_eo);
449 len = (int)(sub.rm_eo - sub.rm_so);
450 shlen = (int)strlen(should);
453 /* check for not supposed to match */
454 if (should == NULL) {
455 sprintf(grump, "matched `%.*s'", len, p);
459 /* check for wrong match */
460 if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) {
461 sprintf(grump, "matched `%.*s' instead", len, p);
467 /* check null match in right place */
472 shlen = 1; /* force check for end-of-string */
473 if (strncmp(p, at, shlen) != 0) {
474 sprintf(grump, "matched null at `%.20s'", p);
481 - eprint - convert error number to name
482 == static char *eprint(int err);
488 static char epbuf[100];
491 len = regerror(REG_ITOA|err, (regex_t *)NULL, epbuf, sizeof(epbuf));
492 assert(len <= sizeof(epbuf));
497 - efind - convert error name to number
498 == static int efind(char *name);
504 static char efbuf[100];
508 sprintf(efbuf, "REG_%s", name);
509 assert(strlen(efbuf) < sizeof(efbuf));
511 (void) regerror(REG_ATOI, &re, efbuf, sizeof(efbuf));