21 int copts = REG_EXTENDED;
23 regoff_t startoff = 0;
28 - main - do the simple case, hand off to regress() for regression
31 main(int argc, char **argv)
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, %zu/%zu `%s'\n",
84 eprint(err), len, sizeof(erbuf), erbuf);
87 regprint(&re, stdout);
94 if ((eopts & REG_STARTEND) != 0) {
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, %zu/%zu `%s'\n",
102 eprint(err), len, sizeof(erbuf), erbuf);
105 if ((copts & REG_NOSUB) == 0) {
106 len = (int)(subs[0].rm_eo - subs[0].rm_so);
107 if (subs[0].rm_so != -1) {
109 printf("match `%.*s'\n", (int)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);
138 char *badpat = "invalid regular expression";
140 char *bpname = "REG_BADPAT";
143 while (fgets(inbuf, sizeof(inbuf), in) != NULL) {
145 if (inbuf[0] == '#' || inbuf[0] == '\n')
146 continue; /* NOTE CONTINUE */
147 inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */
149 fprintf(stdout, "%d:\n", line);
150 nf = split(inbuf, f, MAXF, "\t\t");
152 fprintf(stderr, "bad input, line %d\n", line);
155 for (i = 0; i < nf; i++)
156 if (strcmp(f[i], "\"\"") == 0)
162 try(f[0], f[1], f[2], f[3], f[4], options('c', f[1]));
163 if (opt('&', f[1])) /* try with either type of RE */
164 try(f[0], f[1], f[2], f[3], f[4],
165 options('c', f[1]) &~ REG_EXTENDED);
168 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
169 if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
170 fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n",
174 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, (size_t)SHORT);
175 if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' ||
176 ne != strlen(badpat)+1) {
177 fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n",
178 erbuf, SHORT-1, badpat);
181 ne = regerror(REG_ITOA|REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
182 if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname) + 1) {
183 fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n",
188 ne = regerror(REG_ATOI, &re, erbuf, sizeof(erbuf));
189 if (atoi(erbuf) != (int)REG_BADPAT) {
190 fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n",
191 erbuf, (long)REG_BADPAT);
193 } else if (ne != strlen(erbuf) + 1) {
194 fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
195 erbuf, (long)REG_BADPAT);
201 - try - try it, and report on problems
202 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
203 - opts: may not match f1
206 try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts)
210 regmatch_t subs[NSUBS];
212 char *should[NSHOULD];
217 char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
222 re.re_endp = (opts®_PEND) ? f0copy + strlen(f0copy) : NULL;
224 err = regcomp(&re, f0copy, opts);
225 if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
226 /* unexpected error or wrong error */
227 len = regerror(err, &re, erbuf, sizeof(erbuf));
228 fprintf(stderr, "%d: %s error %s, %zu/%zu `%s'\n",
229 line, type, eprint(err), len, sizeof(erbuf), erbuf);
231 } else if (err == 0 && opt('C', f1)) {
232 /* unexpected success */
233 fprintf(stderr, "%d: %s should have given REG_%s\n",
236 err = 1; /* so we won't try regexec */
247 if (options('e', f1)®_STARTEND) {
248 if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL)
249 fprintf(stderr, "%d: bad STARTEND syntax\n", line);
250 subs[0].rm_so = strchr(f2, '(') - f2 + 1;
251 subs[0].rm_eo = strchr(f2, ')') - f2;
253 err = regexec(&re, f2copy, NSUBS, subs, options('e', f1));
255 if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) {
256 /* unexpected error or wrong error */
257 len = regerror(err, &re, erbuf, sizeof(erbuf));
258 fprintf(stderr, "%d: %s exec error %s, %zu/%zu `%s'\n",
259 line, type, eprint(err), len, sizeof(erbuf), erbuf);
261 } else if (err != 0) {
262 /* nothing more to check */
263 } else if (f3 == NULL) {
264 /* unexpected success */
265 fprintf(stderr, "%d: %s exec should have failed\n",
268 err = 1; /* just on principle */
269 } else if (opts®_NOSUB) {
270 /* nothing more to check */
271 } else if ((grump = check(f2, subs[0], f3)) != NULL) {
272 fprintf(stderr, "%d: %s %s\n", line, type, grump);
277 if (err != 0 || f4 == NULL) {
282 for (i = 1; i < NSHOULD; i++)
284 nshould = split(f4, should+1, NSHOULD-1, ",");
289 for (i = 1; i < NSUBS; i++) {
290 grump = check(f2, subs[i], should[i]);
292 fprintf(stderr, "%d: %s $%d %s\n", line,
303 - options - pick options out of a regression-test string
304 - type: 'c' - compile, 'e' - exec
305 == int options(int type, char *s);
308 options(int type, char *s)
311 int o = (type == 'c') ? copts : eopts;
312 char *legal = (type == 'c') ? "bisnmp" : "^$#tl";
314 for (p = s; *p != '\0'; p++)
315 if (strchr(legal, *p) != NULL)
345 case 't': /* trace */
348 case 'l': /* force long representation */
351 case 'r': /* force backref use */
359 - opt - is a particular option in a regression string?
360 == int opt(int c, char *s);
365 return(strchr(s, c) != NULL);
369 - fixstr - transform magic characters in strings
370 == void fixstr(char *p);
378 for (; *p != '\0'; p++)
390 - check - check a substring match
391 == char *check(char *str, regmatch_t sub, char *should);
393 char * /* NULL or complaint */
394 check(char *str, regmatch_t sub, char *should)
399 static char grump[500];
402 if (should != NULL && strcmp(should, "-") == 0)
404 if (should != NULL && should[0] == '@') {
409 /* check rm_so and rm_eo for consistency */
410 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
411 (sub.rm_so != -1 && sub.rm_eo == -1) ||
412 (sub.rm_so != -1 && sub.rm_so < 0) ||
413 (sub.rm_eo != -1 && sub.rm_eo < 0) ) {
414 sprintf(grump, "start %ld end %ld", (long)sub.rm_so,
419 /* check for no match */
420 if (sub.rm_so == -1 && should == NULL)
423 return("did not match");
425 /* check for in range */
426 if (sub.rm_eo > strlen(str)) {
427 sprintf(grump, "start %ld end %ld, past end of string",
428 (long)sub.rm_so, (long)sub.rm_eo);
432 len = (int)(sub.rm_eo - sub.rm_so);
433 shlen = (int)strlen(should);
436 /* check for not supposed to match */
437 if (should == NULL) {
438 sprintf(grump, "matched `%.*s'", len, p);
442 /* check for wrong match */
443 if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) {
444 sprintf(grump, "matched `%.*s' instead", len, p);
450 /* check null match in right place */
455 shlen = 1; /* force check for end-of-string */
456 if (strncmp(p, at, shlen) != 0) {
457 sprintf(grump, "matched null at `%.20s'", p);
464 - eprint - convert error number to name
465 == static char *eprint(int err);
470 static char epbuf[100];
473 len = regerror(REG_ITOA|err, (regex_t *)NULL, epbuf, sizeof(epbuf));
474 assert(len <= sizeof(epbuf));
479 - efind - convert error name to number
480 == static int efind(char *name);
485 static char efbuf[100];
489 sprintf(efbuf, "REG_%s", name);
490 assert(strlen(efbuf) < sizeof(efbuf));
492 (void) regerror(REG_ATOI, &re, efbuf, sizeof(efbuf));