No empty .Rs/.Re
[netbsd-mini2440.git] / regress / lib / libc / regex / main.c
blobec180620471e26534afea84fbbf42caf0eb90258
1 /* $NetBSD: main.c,v 1.7 2006/05/23 21:52:55 jnemeth Exp $ */
3 /*-
4 * Copyright (c) 1993 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <regex.h>
34 #include <assert.h>
36 #include "main.ih"
38 char *progname;
39 int debug = 0;
40 int line = 0;
41 int status = 0;
43 int copts = REG_EXTENDED;
44 int eopts = 0;
45 regoff_t startoff = 0;
46 regoff_t endoff = 0;
49 extern int split();
50 extern void regprint();
53 - main - do the simple case, hand off to regress() for regression
55 main(argc, argv)
56 int argc;
57 char *argv[];
59 regex_t re;
60 # define NS 10
61 regmatch_t subs[NS];
62 char erbuf[100];
63 int err;
64 size_t len;
65 int c;
66 int errflg = 0;
67 int i;
68 extern int optind;
69 extern char *optarg;
71 progname = argv[0];
73 while ((c = getopt(argc, argv, "c:e:S:E:x")) != -1)
74 switch (c) {
75 case 'c': /* compile options */
76 copts = options('c', optarg);
77 break;
78 case 'e': /* execute options */
79 eopts = options('e', optarg);
80 break;
81 case 'S': /* start offset */
82 startoff = (regoff_t)atoi(optarg);
83 break;
84 case 'E': /* end offset */
85 endoff = (regoff_t)atoi(optarg);
86 break;
87 case 'x': /* Debugging. */
88 debug++;
89 break;
90 case '?':
91 default:
92 errflg++;
93 break;
95 if (errflg) {
96 fprintf(stderr, "usage: %s ", progname);
97 fprintf(stderr, "[-c copt][-C][-d] [re]\n");
98 exit(2);
101 if (optind >= argc) {
102 regress(stdin);
103 exit(status);
106 err = regcomp(&re, argv[optind++], copts);
107 if (err) {
108 len = regerror(err, &re, erbuf, sizeof(erbuf));
109 fprintf(stderr, "error %s, %d/%d `%s'\n",
110 eprint(err), len, sizeof(erbuf), erbuf);
111 exit(status);
113 regprint(&re, stdout);
115 if (optind >= argc) {
116 regfree(&re);
117 exit(status);
120 if (eopts&REG_STARTEND) {
121 subs[0].rm_so = startoff;
122 subs[0].rm_eo = strlen(argv[optind]) - endoff;
124 err = regexec(&re, argv[optind], (size_t)NS, subs, eopts);
125 if (err) {
126 len = regerror(err, &re, erbuf, sizeof(erbuf));
127 fprintf(stderr, "error %s, %d/%d `%s'\n",
128 eprint(err), len, sizeof(erbuf), erbuf);
129 exit(status);
131 if (!(copts&REG_NOSUB)) {
132 len = (int)(subs[0].rm_eo - subs[0].rm_so);
133 if (subs[0].rm_so != -1) {
134 if (len != 0)
135 printf("match `%.*s'\n", len,
136 argv[optind] + subs[0].rm_so);
137 else
138 printf("match `'@%.1s\n",
139 argv[optind] + subs[0].rm_so);
141 for (i = 1; i < NS; i++)
142 if (subs[i].rm_so != -1)
143 printf("(%d) `%.*s'\n", i,
144 (int)(subs[i].rm_eo - subs[i].rm_so),
145 argv[optind] + subs[i].rm_so);
147 exit(status);
151 - regress - main loop of regression test
152 == void regress(FILE *in);
154 void
155 regress(in)
156 FILE *in;
158 char inbuf[1000];
159 # define MAXF 10
160 char *f[MAXF];
161 int nf;
162 int i;
163 char erbuf[100];
164 size_t ne;
165 char *badpat = "invalid regular expression";
166 # define SHORT 10
167 char *bpname = "REG_BADPAT";
168 regex_t re;
170 while (fgets(inbuf, sizeof(inbuf), in) != NULL) {
171 line++;
172 if (inbuf[0] == '#' || inbuf[0] == '\n')
173 continue; /* NOTE CONTINUE */
174 inbuf[strlen(inbuf)-1] = '\0'; /* get rid of stupid \n */
175 if (debug)
176 fprintf(stdout, "%d:\n", line);
177 nf = split(inbuf, f, MAXF, "\t\t");
178 if (nf < 3) {
179 fprintf(stderr, "bad input, line %d\n", line);
180 exit(1);
182 for (i = 0; i < nf; i++)
183 if (strcmp(f[i], "\"\"") == 0)
184 f[i] = "";
185 if (nf <= 3)
186 f[3] = NULL;
187 if (nf <= 4)
188 f[4] = NULL;
189 try(f[0], f[1], f[2], f[3], f[4], options('c', f[1]));
190 if (opt('&', f[1])) /* try with either type of RE */
191 try(f[0], f[1], f[2], f[3], f[4],
192 options('c', f[1]) &~ REG_EXTENDED);
195 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
196 if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) {
197 fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n",
198 erbuf, badpat);
199 status = 1;
201 ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, (size_t)SHORT);
202 if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' ||
203 ne != strlen(badpat)+1) {
204 fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n",
205 erbuf, SHORT-1, badpat);
206 status = 1;
208 ne = regerror(REG_ITOA|REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf));
209 if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) {
210 fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n",
211 erbuf, bpname);
212 status = 1;
214 re.re_endp = bpname;
215 ne = regerror(REG_ATOI, &re, erbuf, sizeof(erbuf));
216 if (atoi(erbuf) != (int)REG_BADPAT) {
217 fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n",
218 erbuf, (long)REG_BADPAT);
219 status = 1;
220 } else if (ne != strlen(erbuf)+1) {
221 fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
222 erbuf, (long)REG_BADPAT);
223 status = 1;
228 - try - try it, and report on problems
229 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
231 void
232 try(f0, f1, f2, f3, f4, opts)
233 char *f0;
234 char *f1;
235 char *f2;
236 char *f3;
237 char *f4;
238 int opts; /* may not match f1 */
240 regex_t re;
241 # define NSUBS 10
242 regmatch_t subs[NSUBS];
243 # define NSHOULD 15
244 char *should[NSHOULD];
245 int nshould;
246 char erbuf[100];
247 int err;
248 int len;
249 char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
250 int i;
251 char *grump;
252 char f0copy[1000];
253 char f2copy[1000];
255 strcpy(f0copy, f0);
256 re.re_endp = (opts&REG_PEND) ? f0copy + strlen(f0copy) : NULL;
257 fixstr(f0copy);
258 err = regcomp(&re, f0copy, opts);
259 if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
260 /* unexpected error or wrong error */
261 len = regerror(err, &re, erbuf, sizeof(erbuf));
262 fprintf(stderr, "%d: %s error %s, %d/%d `%s'\n",
263 line, type, eprint(err), len,
264 sizeof(erbuf), erbuf);
265 status = 1;
266 } else if (err == 0 && opt('C', f1)) {
267 /* unexpected success */
268 fprintf(stderr, "%d: %s should have given REG_%s\n",
269 line, type, f2);
270 status = 1;
271 err = 1; /* so we won't try regexec */
274 if (err != 0) {
275 regfree(&re);
276 return;
279 strcpy(f2copy, f2);
280 fixstr(f2copy);
282 if (options('e', f1)&REG_STARTEND) {
283 if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL)
284 fprintf(stderr, "%d: bad STARTEND syntax\n", line);
285 subs[0].rm_so = strchr(f2, '(') - f2 + 1;
286 subs[0].rm_eo = strchr(f2, ')') - f2;
288 err = regexec(&re, f2copy, NSUBS, subs, options('e', f1));
290 if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) {
291 /* unexpected error or wrong error */
292 len = regerror(err, &re, erbuf, sizeof(erbuf));
293 fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n",
294 line, type, eprint(err), len,
295 sizeof(erbuf), erbuf);
296 status = 1;
297 } else if (err != 0) {
298 /* nothing more to check */
299 } else if (f3 == NULL) {
300 /* unexpected success */
301 fprintf(stderr, "%d: %s exec should have failed\n",
302 line, type);
303 status = 1;
304 err = 1; /* just on principle */
305 } else if (opts&REG_NOSUB) {
306 /* nothing more to check */
307 } else if ((grump = check(f2, subs[0], f3)) != NULL) {
308 fprintf(stderr, "%d: %s %s\n", line, type, grump);
309 status = 1;
310 err = 1;
313 if (err != 0 || f4 == NULL) {
314 regfree(&re);
315 return;
318 for (i = 1; i < NSHOULD; i++)
319 should[i] = NULL;
320 nshould = split(f4, should+1, NSHOULD-1, ",");
321 if (nshould == 0) {
322 nshould = 1;
323 should[1] = "";
325 for (i = 1; i < NSUBS; i++) {
326 grump = check(f2, subs[i], should[i]);
327 if (grump != NULL) {
328 fprintf(stderr, "%d: %s $%d %s\n", line,
329 type, i, grump);
330 status = 1;
331 err = 1;
335 regfree(&re);
339 - options - pick options out of a regression-test string
340 == int options(int type, char *s);
343 options(type, s)
344 int type; /* 'c' compile, 'e' exec */
345 char *s;
347 char *p;
348 int o = (type == 'c') ? copts : eopts;
349 char *legal = (type == 'c') ? "bisnmp" : "^$#tl";
351 for (p = s; *p != '\0'; p++)
352 if (strchr(legal, *p) != NULL)
353 switch (*p) {
354 case 'b':
355 o &= ~REG_EXTENDED;
356 break;
357 case 'i':
358 o |= REG_ICASE;
359 break;
360 case 's':
361 o |= REG_NOSUB;
362 break;
363 case 'n':
364 o |= REG_NEWLINE;
365 break;
366 case 'm':
367 o &= ~REG_EXTENDED;
368 o |= REG_NOSPEC;
369 break;
370 case 'p':
371 o |= REG_PEND;
372 break;
373 case '^':
374 o |= REG_NOTBOL;
375 break;
376 case '$':
377 o |= REG_NOTEOL;
378 break;
379 case '#':
380 o |= REG_STARTEND;
381 break;
382 case 't': /* trace */
383 o |= REG_TRACE;
384 break;
385 case 'l': /* force long representation */
386 o |= REG_LARGE;
387 break;
388 case 'r': /* force backref use */
389 o |= REG_BACKR;
390 break;
392 return(o);
396 - opt - is a particular option in a regression string?
397 == int opt(int c, char *s);
399 int /* predicate */
400 opt(c, s)
401 int c;
402 char *s;
404 return(strchr(s, c) != NULL);
408 - fixstr - transform magic characters in strings
409 == void fixstr(char *p);
411 void
412 fixstr(p)
413 char *p;
415 if (p == NULL)
416 return;
418 for (; *p != '\0'; p++)
419 if (*p == 'N')
420 *p = '\n';
421 else if (*p == 'T')
422 *p = '\t';
423 else if (*p == 'S')
424 *p = ' ';
425 else if (*p == 'Z')
426 *p = '\0';
430 - check - check a substring match
431 == char *check(char *str, regmatch_t sub, char *should);
433 char * /* NULL or complaint */
434 check(str, sub, should)
435 char *str;
436 regmatch_t sub;
437 char *should;
439 int len;
440 int shlen;
441 char *p;
442 static char grump[500];
443 char *at = NULL;
445 if (should != NULL && strcmp(should, "-") == 0)
446 should = NULL;
447 if (should != NULL && should[0] == '@') {
448 at = should + 1;
449 should = "";
452 /* check rm_so and rm_eo for consistency */
453 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) ||
454 (sub.rm_so != -1 && sub.rm_eo == -1) ||
455 (sub.rm_so != -1 && sub.rm_so < 0) ||
456 (sub.rm_eo != -1 && sub.rm_eo < 0) ) {
457 sprintf(grump, "start %ld end %ld", (long)sub.rm_so,
458 (long)sub.rm_eo);
459 return(grump);
462 /* check for no match */
463 if (sub.rm_so == -1 && should == NULL)
464 return(NULL);
465 if (sub.rm_so == -1)
466 return("did not match");
468 /* check for in range */
469 if (sub.rm_eo > strlen(str)) {
470 sprintf(grump, "start %ld end %ld, past end of string",
471 (long)sub.rm_so, (long)sub.rm_eo);
472 return(grump);
475 len = (int)(sub.rm_eo - sub.rm_so);
476 p = str + sub.rm_so;
478 /* check for not supposed to match */
479 if (should == NULL) {
480 sprintf(grump, "matched `%.*s'", len, p);
481 return(grump);
484 /* check for wrong match */
485 shlen = (int)strlen(should);
486 if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) {
487 sprintf(grump, "matched `%.*s' instead", len, p);
488 return(grump);
490 if (shlen > 0)
491 return(NULL);
493 /* check null match in right place */
494 if (at == NULL)
495 return(NULL);
496 shlen = strlen(at);
497 if (shlen == 0)
498 shlen = 1; /* force check for end-of-string */
499 if (strncmp(p, at, shlen) != 0) {
500 sprintf(grump, "matched null at `%.20s'", p);
501 return(grump);
503 return(NULL);
507 - eprint - convert error number to name
508 == static char *eprint(int err);
510 static char *
511 eprint(err)
512 int err;
514 static char epbuf[100];
515 size_t len;
517 len = regerror(REG_ITOA|err, (regex_t *)NULL, epbuf, sizeof(epbuf));
518 assert(len <= sizeof(epbuf));
519 return(epbuf);
523 - efind - convert error name to number
524 == static int efind(char *name);
526 static int
527 efind(name)
528 char *name;
530 static char efbuf[100];
531 size_t n;
532 regex_t re;
534 sprintf(efbuf, "REG_%s", name);
535 assert(strlen(efbuf) < sizeof(efbuf));
536 re.re_endp = efbuf;
537 (void) regerror(REG_ATOI, &re, efbuf, sizeof(efbuf));
538 return(atoi(efbuf));