diff options
Diffstat (limited to 'src/regress/lib/libc/regex/main.c')
-rw-r--r-- | src/regress/lib/libc/regex/main.c | 516 |
1 files changed, 0 insertions, 516 deletions
diff --git a/src/regress/lib/libc/regex/main.c b/src/regress/lib/libc/regex/main.c deleted file mode 100644 index e0ed86f5e8..0000000000 --- a/src/regress/lib/libc/regex/main.c +++ /dev/null | |||
@@ -1,516 +0,0 @@ | |||
1 | /* $OpenBSD: main.c,v 1.7 2007/09/12 19:32:35 otto Exp $ */ | ||
2 | /* $NetBSD: main.c,v 1.2 1995/04/20 22:39:51 cgd Exp $ */ | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <string.h> | ||
7 | #include <sys/types.h> | ||
8 | #include <regex.h> | ||
9 | #include <assert.h> | ||
10 | #include <unistd.h> | ||
11 | |||
12 | #include "main.ih" | ||
13 | |||
14 | char *progname; | ||
15 | int debug = 0; | ||
16 | int line = 0; | ||
17 | int status = 0; | ||
18 | |||
19 | int copts = REG_EXTENDED; | ||
20 | int eopts = 0; | ||
21 | regoff_t startoff = 0; | ||
22 | regoff_t endoff = 0; | ||
23 | |||
24 | |||
25 | extern int split(char *, char *[], int, char *); | ||
26 | extern void regprint(regex_t *, FILE *); | ||
27 | |||
28 | /* | ||
29 | - main - do the simple case, hand off to regress() for regression | ||
30 | */ | ||
31 | int | ||
32 | main(int argc, char *argv[]) | ||
33 | |||
34 | { | ||
35 | regex_t re; | ||
36 | # define NS 10 | ||
37 | regmatch_t subs[NS]; | ||
38 | char erbuf[100]; | ||
39 | int err; | ||
40 | size_t len; | ||
41 | int c; | ||
42 | int errflg = 0; | ||
43 | register int i; | ||
44 | extern int optind; | ||
45 | extern char *optarg; | ||
46 | |||
47 | progname = argv[0]; | ||
48 | |||
49 | while ((c = getopt(argc, argv, "c:e:S:E:x")) != -1) | ||
50 | switch (c) { | ||
51 | case 'c': /* compile options */ | ||
52 | copts = options('c', optarg); | ||
53 | break; | ||
54 | case 'e': /* execute options */ | ||
55 | eopts = options('e', optarg); | ||
56 | break; | ||
57 | case 'S': /* start offset */ | ||
58 | startoff = (regoff_t)atoi(optarg); | ||
59 | break; | ||
60 | case 'E': /* end offset */ | ||
61 | endoff = (regoff_t)atoi(optarg); | ||
62 | break; | ||
63 | case 'x': /* Debugging. */ | ||
64 | debug++; | ||
65 | break; | ||
66 | case '?': | ||
67 | default: | ||
68 | errflg++; | ||
69 | break; | ||
70 | } | ||
71 | if (errflg) { | ||
72 | fprintf(stderr, "usage: %s ", progname); | ||
73 | fprintf(stderr, "[-c copt][-C][-d] [re]\n"); | ||
74 | exit(2); | ||
75 | } | ||
76 | |||
77 | if (optind >= argc) { | ||
78 | regress(stdin); | ||
79 | exit(status); | ||
80 | } | ||
81 | |||
82 | err = regcomp(&re, argv[optind++], copts); | ||
83 | if (err) { | ||
84 | len = regerror(err, &re, erbuf, sizeof(erbuf)); | ||
85 | fprintf(stderr, "error %s, %zu/%zu `%s'\n", | ||
86 | eprint(err), len, sizeof(erbuf), erbuf); | ||
87 | exit(status); | ||
88 | } | ||
89 | regprint(&re, stdout); | ||
90 | |||
91 | if (optind >= argc) { | ||
92 | regfree(&re); | ||
93 | exit(status); | ||
94 | } | ||
95 | |||
96 | if (eopts®_STARTEND) { | ||
97 | subs[0].rm_so = startoff; | ||
98 | subs[0].rm_eo = strlen(argv[optind]) - endoff; | ||
99 | } | ||
100 | err = regexec(&re, argv[optind], (size_t)NS, subs, eopts); | ||
101 | if (err) { | ||
102 | len = regerror(err, &re, erbuf, sizeof(erbuf)); | ||
103 | fprintf(stderr, "error %s, %zu/%zu `%s'\n", | ||
104 | eprint(err), len, sizeof(erbuf), erbuf); | ||
105 | exit(status); | ||
106 | } | ||
107 | if (!(copts®_NOSUB)) { | ||
108 | len = (size_t)(subs[0].rm_eo - subs[0].rm_so); | ||
109 | if (subs[0].rm_so != -1) { | ||
110 | if (len != 0) | ||
111 | printf("match `%.*s'\n", (int)len, | ||
112 | argv[optind] + subs[0].rm_so); | ||
113 | else | ||
114 | printf("match `'@%.1s\n", | ||
115 | argv[optind] + subs[0].rm_so); | ||
116 | } | ||
117 | for (i = 1; i < NS; i++) | ||
118 | if (subs[i].rm_so != -1) | ||
119 | printf("(%d) `%.*s'\n", i, | ||
120 | (int)(subs[i].rm_eo - subs[i].rm_so), | ||
121 | argv[optind] + subs[i].rm_so); | ||
122 | } | ||
123 | exit(status); | ||
124 | } | ||
125 | |||
126 | /* | ||
127 | - regress - main loop of regression test | ||
128 | == void regress(FILE *in); | ||
129 | */ | ||
130 | void | ||
131 | regress(in) | ||
132 | FILE *in; | ||
133 | { | ||
134 | char inbuf[1000]; | ||
135 | # define MAXF 10 | ||
136 | char *f[MAXF]; | ||
137 | int nf; | ||
138 | int i; | ||
139 | char erbuf[100]; | ||
140 | size_t ne; | ||
141 | char *badpat = "invalid regular expression"; | ||
142 | # define SHORT 10 | ||
143 | char *bpname = "REG_BADPAT"; | ||
144 | regex_t re; | ||
145 | |||
146 | while (fgets(inbuf, sizeof(inbuf), in) != NULL) { | ||
147 | line++; | ||
148 | if (inbuf[0] == '#' || inbuf[0] == '\n') | ||
149 | continue; /* NOTE CONTINUE */ | ||
150 | inbuf[strcspn(inbuf, "\n")] = '\0'; /* get rid of stupid \n */ | ||
151 | if (debug) | ||
152 | fprintf(stdout, "%d:\n", line); | ||
153 | nf = split(inbuf, f, MAXF, "\t\t"); | ||
154 | if (nf < 3) { | ||
155 | fprintf(stderr, "bad input, line %d\n", line); | ||
156 | exit(1); | ||
157 | } | ||
158 | for (i = 0; i < nf; i++) | ||
159 | if (strcmp(f[i], "\"\"") == 0) | ||
160 | f[i] = ""; | ||
161 | if (nf <= 3) | ||
162 | f[3] = NULL; | ||
163 | if (nf <= 4) | ||
164 | f[4] = NULL; | ||
165 | try(f[0], f[1], f[2], f[3], f[4], options('c', f[1])); | ||
166 | if (opt('&', f[1])) /* try with either type of RE */ | ||
167 | try(f[0], f[1], f[2], f[3], f[4], | ||
168 | options('c', f[1]) &~ REG_EXTENDED); | ||
169 | } | ||
170 | |||
171 | ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf)); | ||
172 | if (strcmp(erbuf, badpat) != 0 || ne != strlen(badpat)+1) { | ||
173 | fprintf(stderr, "end: regerror() test gave `%s' not `%s'\n", | ||
174 | erbuf, badpat); | ||
175 | status = 1; | ||
176 | } | ||
177 | ne = regerror(REG_BADPAT, (regex_t *)NULL, erbuf, (size_t)SHORT); | ||
178 | if (strncmp(erbuf, badpat, SHORT-1) != 0 || erbuf[SHORT-1] != '\0' || | ||
179 | ne != strlen(badpat)+1) { | ||
180 | fprintf(stderr, "end: regerror() short test gave `%s' not `%.*s'\n", | ||
181 | erbuf, SHORT-1, badpat); | ||
182 | status = 1; | ||
183 | } | ||
184 | ne = regerror(REG_ITOA|REG_BADPAT, (regex_t *)NULL, erbuf, sizeof(erbuf)); | ||
185 | if (strcmp(erbuf, bpname) != 0 || ne != strlen(bpname)+1) { | ||
186 | fprintf(stderr, "end: regerror() ITOA test gave `%s' not `%s'\n", | ||
187 | erbuf, bpname); | ||
188 | status = 1; | ||
189 | } | ||
190 | re.re_endp = bpname; | ||
191 | ne = regerror(REG_ATOI, &re, erbuf, sizeof(erbuf)); | ||
192 | if (atoi(erbuf) != (int)REG_BADPAT) { | ||
193 | fprintf(stderr, "end: regerror() ATOI test gave `%s' not `%ld'\n", | ||
194 | erbuf, (long)REG_BADPAT); | ||
195 | status = 1; | ||
196 | } else if (ne != strlen(erbuf)+1) { | ||
197 | fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n", | ||
198 | erbuf, (long)REG_BADPAT); | ||
199 | status = 1; | ||
200 | } | ||
201 | } | ||
202 | |||
203 | /* | ||
204 | - try - try it, and report on problems | ||
205 | == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts); | ||
206 | */ | ||
207 | void | ||
208 | try(f0, f1, f2, f3, f4, opts) | ||
209 | char *f0; | ||
210 | char *f1; | ||
211 | char *f2; | ||
212 | char *f3; | ||
213 | char *f4; | ||
214 | int opts; /* may not match f1 */ | ||
215 | { | ||
216 | regex_t re; | ||
217 | # define NSUBS 10 | ||
218 | regmatch_t subs[NSUBS]; | ||
219 | # define NSHOULD 15 | ||
220 | char *should[NSHOULD]; | ||
221 | int nshould; | ||
222 | char erbuf[100]; | ||
223 | int err; | ||
224 | int len; | ||
225 | char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE"; | ||
226 | register int i; | ||
227 | char *grump; | ||
228 | char f0copy[1000]; | ||
229 | char f2copy[1000]; | ||
230 | |||
231 | strlcpy(f0copy, f0, sizeof f0copy); | ||
232 | re.re_endp = (opts®_PEND) ? f0copy + strlen(f0copy) : NULL; | ||
233 | fixstr(f0copy); | ||
234 | err = regcomp(&re, f0copy, opts); | ||
235 | if (err != 0 && (!opt('C', f1) || err != efind(f2))) { | ||
236 | /* unexpected error or wrong error */ | ||
237 | len = regerror(err, &re, erbuf, sizeof(erbuf)); | ||
238 | fprintf(stderr, "%d: %s error %s, %d/%zu `%s'\n", | ||
239 | line, type, eprint(err), len, | ||
240 | sizeof(erbuf), erbuf); | ||
241 | status = 1; | ||
242 | } else if (err == 0 && opt('C', f1)) { | ||
243 | /* unexpected success */ | ||
244 | fprintf(stderr, "%d: %s should have given REG_%s\n", | ||
245 | line, type, f2); | ||
246 | status = 1; | ||
247 | err = 1; /* so we won't try regexec */ | ||
248 | } | ||
249 | |||
250 | if (err != 0) { | ||
251 | regfree(&re); | ||
252 | return; | ||
253 | } | ||
254 | |||
255 | strlcpy(f2copy, f2, sizeof f2copy); | ||
256 | fixstr(f2copy); | ||
257 | |||
258 | if (options('e', f1)®_STARTEND) { | ||
259 | if (strchr(f2, '(') == NULL || strchr(f2, ')') == NULL) | ||
260 | fprintf(stderr, "%d: bad STARTEND syntax\n", line); | ||
261 | subs[0].rm_so = strchr(f2, '(') - f2 + 1; | ||
262 | subs[0].rm_eo = strchr(f2, ')') - f2; | ||
263 | } | ||
264 | err = regexec(&re, f2copy, NSUBS, subs, options('e', f1)); | ||
265 | |||
266 | if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) { | ||
267 | /* unexpected error or wrong error */ | ||
268 | len = regerror(err, &re, erbuf, sizeof(erbuf)); | ||
269 | fprintf(stderr, "%d: %s exec error %s, %d/%zu `%s'\n", | ||
270 | line, type, eprint(err), len, | ||
271 | sizeof(erbuf), erbuf); | ||
272 | status = 1; | ||
273 | } else if (err != 0) { | ||
274 | /* nothing more to check */ | ||
275 | } else if (f3 == NULL) { | ||
276 | /* unexpected success */ | ||
277 | fprintf(stderr, "%d: %s exec should have failed\n", | ||
278 | line, type); | ||
279 | status = 1; | ||
280 | err = 1; /* just on principle */ | ||
281 | } else if (opts®_NOSUB) { | ||
282 | /* nothing more to check */ | ||
283 | } else if ((grump = check(f2, subs[0], f3)) != NULL) { | ||
284 | fprintf(stderr, "%d: %s %s\n", line, type, grump); | ||
285 | status = 1; | ||
286 | err = 1; | ||
287 | } | ||
288 | |||
289 | if (err != 0 || f4 == NULL) { | ||
290 | regfree(&re); | ||
291 | return; | ||
292 | } | ||
293 | |||
294 | for (i = 1; i < NSHOULD; i++) | ||
295 | should[i] = NULL; | ||
296 | nshould = split(f4, should+1, NSHOULD-1, ","); | ||
297 | if (nshould == 0) { | ||
298 | nshould = 1; | ||
299 | should[1] = ""; | ||
300 | } | ||
301 | for (i = 1; i < NSUBS; i++) { | ||
302 | grump = check(f2, subs[i], should[i]); | ||
303 | if (grump != NULL) { | ||
304 | fprintf(stderr, "%d: %s $%d %s\n", line, | ||
305 | type, i, grump); | ||
306 | status = 1; | ||
307 | err = 1; | ||
308 | } | ||
309 | } | ||
310 | |||
311 | regfree(&re); | ||
312 | } | ||
313 | |||
314 | /* | ||
315 | - options - pick options out of a regression-test string | ||
316 | == int options(int type, char *s); | ||
317 | */ | ||
318 | int | ||
319 | options(type, s) | ||
320 | int type; /* 'c' compile, 'e' exec */ | ||
321 | char *s; | ||
322 | { | ||
323 | register char *p; | ||
324 | register int o = (type == 'c') ? copts : eopts; | ||
325 | register char *legal = (type == 'c') ? "bisnmp" : "^$#tl"; | ||
326 | |||
327 | for (p = s; *p != '\0'; p++) | ||
328 | if (strchr(legal, *p) != NULL) | ||
329 | switch (*p) { | ||
330 | case 'b': | ||
331 | o &= ~REG_EXTENDED; | ||
332 | break; | ||
333 | case 'i': | ||
334 | o |= REG_ICASE; | ||
335 | break; | ||
336 | case 's': | ||
337 | o |= REG_NOSUB; | ||
338 | break; | ||
339 | case 'n': | ||
340 | o |= REG_NEWLINE; | ||
341 | break; | ||
342 | case 'm': | ||
343 | o &= ~REG_EXTENDED; | ||
344 | o |= REG_NOSPEC; | ||
345 | break; | ||
346 | case 'p': | ||
347 | o |= REG_PEND; | ||
348 | break; | ||
349 | case '^': | ||
350 | o |= REG_NOTBOL; | ||
351 | break; | ||
352 | case '$': | ||
353 | o |= REG_NOTEOL; | ||
354 | break; | ||
355 | case '#': | ||
356 | o |= REG_STARTEND; | ||
357 | break; | ||
358 | case 't': /* trace */ | ||
359 | o |= REG_TRACE; | ||
360 | break; | ||
361 | case 'l': /* force long representation */ | ||
362 | o |= REG_LARGE; | ||
363 | break; | ||
364 | case 'r': /* force backref use */ | ||
365 | o |= REG_BACKR; | ||
366 | break; | ||
367 | } | ||
368 | return(o); | ||
369 | } | ||
370 | |||
371 | /* | ||
372 | - opt - is a particular option in a regression string? | ||
373 | == int opt(int c, char *s); | ||
374 | */ | ||
375 | int /* predicate */ | ||
376 | opt(c, s) | ||
377 | int c; | ||
378 | char *s; | ||
379 | { | ||
380 | return(strchr(s, c) != NULL); | ||
381 | } | ||
382 | |||
383 | /* | ||
384 | - fixstr - transform magic characters in strings | ||
385 | == void fixstr(register char *p); | ||
386 | */ | ||
387 | void | ||
388 | fixstr(p) | ||
389 | register char *p; | ||
390 | { | ||
391 | if (p == NULL) | ||
392 | return; | ||
393 | |||
394 | for (; *p != '\0'; p++) | ||
395 | if (*p == 'N') | ||
396 | *p = '\n'; | ||
397 | else if (*p == 'T') | ||
398 | *p = '\t'; | ||
399 | else if (*p == 'S') | ||
400 | *p = ' '; | ||
401 | else if (*p == 'Z') | ||
402 | *p = '\0'; | ||
403 | } | ||
404 | |||
405 | /* | ||
406 | - check - check a substring match | ||
407 | == char *check(char *str, regmatch_t sub, char *should); | ||
408 | */ | ||
409 | char * /* NULL or complaint */ | ||
410 | check(str, sub, should) | ||
411 | char *str; | ||
412 | regmatch_t sub; | ||
413 | char *should; | ||
414 | { | ||
415 | register int len; | ||
416 | register int shlen; | ||
417 | register char *p; | ||
418 | static char grump[500]; | ||
419 | register char *at = NULL; | ||
420 | |||
421 | if (should != NULL && strcmp(should, "-") == 0) | ||
422 | should = NULL; | ||
423 | if (should != NULL && should[0] == '@') { | ||
424 | at = should + 1; | ||
425 | should = ""; | ||
426 | } | ||
427 | |||
428 | /* check rm_so and rm_eo for consistency */ | ||
429 | if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) || | ||
430 | (sub.rm_so != -1 && sub.rm_eo == -1) || | ||
431 | (sub.rm_so != -1 && sub.rm_so < 0) || | ||
432 | (sub.rm_eo != -1 && sub.rm_eo < 0) ) { | ||
433 | snprintf(grump, sizeof grump, | ||
434 | "start %ld end %ld", (long)sub.rm_so, | ||
435 | (long)sub.rm_eo); | ||
436 | return(grump); | ||
437 | } | ||
438 | |||
439 | /* check for no match */ | ||
440 | if (sub.rm_so == -1 && should == NULL) | ||
441 | return(NULL); | ||
442 | if (sub.rm_so == -1) | ||
443 | return("did not match"); | ||
444 | |||
445 | /* check for in range */ | ||
446 | if (sub.rm_eo > strlen(str)) { | ||
447 | snprintf(grump, sizeof grump, | ||
448 | "start %ld end %ld, past end of string", | ||
449 | (long)sub.rm_so, (long)sub.rm_eo); | ||
450 | return(grump); | ||
451 | } | ||
452 | |||
453 | len = (int)(sub.rm_eo - sub.rm_so); | ||
454 | shlen = (int)strlen(should); | ||
455 | p = str + sub.rm_so; | ||
456 | |||
457 | /* check for not supposed to match */ | ||
458 | if (should == NULL) { | ||
459 | snprintf(grump, sizeof grump, "matched `%.*s'", len, p); | ||
460 | return(grump); | ||
461 | } | ||
462 | |||
463 | /* check for wrong match */ | ||
464 | if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) { | ||
465 | snprintf(grump, sizeof grump, "matched `%.*s' instead", len, p); | ||
466 | return(grump); | ||
467 | } | ||
468 | if (shlen > 0) | ||
469 | return(NULL); | ||
470 | |||
471 | /* check null match in right place */ | ||
472 | if (at == NULL) | ||
473 | return(NULL); | ||
474 | shlen = strlen(at); | ||
475 | if (shlen == 0) | ||
476 | shlen = 1; /* force check for end-of-string */ | ||
477 | if (strncmp(p, at, shlen) != 0) { | ||
478 | snprintf(grump, sizeof grump, "matched null at `%.20s'", p); | ||
479 | return(grump); | ||
480 | } | ||
481 | return(NULL); | ||
482 | } | ||
483 | |||
484 | /* | ||
485 | - eprint - convert error number to name | ||
486 | == static char *eprint(int err); | ||
487 | */ | ||
488 | static char * | ||
489 | eprint(err) | ||
490 | int err; | ||
491 | { | ||
492 | static char epbuf[100]; | ||
493 | size_t len; | ||
494 | |||
495 | len = regerror(REG_ITOA|err, (regex_t *)NULL, epbuf, sizeof(epbuf)); | ||
496 | assert(len <= sizeof(epbuf)); | ||
497 | return(epbuf); | ||
498 | } | ||
499 | |||
500 | /* | ||
501 | - efind - convert error name to number | ||
502 | == static int efind(char *name); | ||
503 | */ | ||
504 | static int | ||
505 | efind(name) | ||
506 | char *name; | ||
507 | { | ||
508 | static char efbuf[100]; | ||
509 | regex_t re; | ||
510 | |||
511 | snprintf(efbuf, sizeof efbuf, "REG_%s", name); | ||
512 | assert(strlen(efbuf) < sizeof(efbuf)); | ||
513 | re.re_endp = efbuf; | ||
514 | (void) regerror(REG_ATOI, &re, efbuf, sizeof(efbuf)); | ||
515 | return(atoi(efbuf)); | ||
516 | } | ||