summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/regex/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/regex/main.c')
-rw-r--r--src/regress/lib/libc/regex/main.c516
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 4501e86b87..0000000000
--- a/src/regress/lib/libc/regex/main.c
+++ /dev/null
@@ -1,516 +0,0 @@
1/* $OpenBSD: main.c,v 1.12 2022/12/04 23:50:46 cheloha 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
14char *progname;
15int debug = 0;
16int line = 0;
17int status = 0;
18
19int copts = REG_EXTENDED;
20int eopts = 0;
21regoff_t startoff = 0;
22regoff_t endoff = 0;
23
24
25extern int split(char *, char *[], int, char *);
26extern void regprint(regex_t *, FILE *);
27
28/*
29 - main - do the simple case, hand off to regress() for regression
30 */
31int
32main(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
45 progname = argv[0];
46
47 while ((c = getopt(argc, argv, "c:E:e:S:x")) != -1)
48 switch (c) {
49 case 'c': /* compile options */
50 copts = options('c', optarg);
51 break;
52 case 'E': /* end offset */
53 endoff = (regoff_t)atoi(optarg);
54 break;
55 case 'e': /* execute options */
56 eopts = options('e', optarg);
57 break;
58 case 'S': /* start offset */
59 startoff = (regoff_t)atoi(optarg);
60 break;
61 case 'x': /* Debugging. */
62 debug++;
63 break;
64 default:
65 errflg++;
66 break;
67 }
68 if (errflg) {
69 fprintf(stderr, "usage: %s ", progname);
70 fprintf(stderr, "[-x] [-c copt] [-E endoff] [-e eopt] [-S startoff] [re]\n");
71 exit(2);
72 }
73
74 if (optind >= argc) {
75 regress(stdin);
76 exit(status);
77 }
78
79 err = regcomp(&re, argv[optind++], copts);
80 if (err) {
81 len = regerror(err, &re, erbuf, sizeof(erbuf));
82 fprintf(stderr, "error %s, %zu/%zu `%s'\n",
83 eprint(err), len, sizeof(erbuf), erbuf);
84 exit(status);
85 }
86 regprint(&re, stdout);
87
88 if (optind >= argc) {
89 regfree(&re);
90 exit(status);
91 }
92
93 if (eopts&REG_STARTEND) {
94 subs[0].rm_so = startoff;
95 subs[0].rm_eo = strlen(argv[optind]) - endoff;
96 }
97 err = regexec(&re, argv[optind], (size_t)NS, subs, eopts);
98 if (err) {
99 len = regerror(err, &re, erbuf, sizeof(erbuf));
100 fprintf(stderr, "error %s, %zu/%zu `%s'\n",
101 eprint(err), len, sizeof(erbuf), erbuf);
102 exit(status);
103 }
104 if (!(copts&REG_NOSUB)) {
105 len = (size_t)(subs[0].rm_eo - subs[0].rm_so);
106 if (subs[0].rm_so != -1) {
107 if (len != 0)
108 printf("match `%.*s'\n", (int)len,
109 argv[optind] + subs[0].rm_so);
110 else
111 printf("match `'@%.1s\n",
112 argv[optind] + subs[0].rm_so);
113 }
114 for (i = 1; i < NS; i++)
115 if (subs[i].rm_so != -1)
116 printf("(%d) `%.*s'\n", i,
117 (int)(subs[i].rm_eo - subs[i].rm_so),
118 argv[optind] + subs[i].rm_so);
119 }
120 exit(status);
121}
122
123/*
124 - regress - main loop of regression test
125 == void regress(FILE *in);
126 */
127void
128regress(in)
129FILE *in;
130{
131 char inbuf[1000];
132# define MAXF 10
133 char *f[MAXF];
134 int nf;
135 int i;
136 char erbuf[100];
137 size_t ne;
138 char *badpat = "invalid regular expression";
139# define SHORT 10
140 char *bpname = "REG_BADPAT";
141 regex_t re;
142
143 while (fgets(inbuf, sizeof(inbuf), in) != NULL) {
144 line++;
145 if (inbuf[0] == '#' || inbuf[0] == '\n')
146 continue; /* NOTE CONTINUE */
147 inbuf[strcspn(inbuf, "\n")] = '\0'; /* get rid of stupid \n */
148 if (debug)
149 fprintf(stdout, "%d:\n", line);
150 nf = split(inbuf, f, MAXF, "\t\t");
151 if (nf < 3) {
152 fprintf(stderr, "bad input, line %d\n", line);
153 exit(1);
154 }
155 for (i = 0; i < nf; i++)
156 if (strcmp(f[i], "\"\"") == 0)
157 f[i] = "";
158 if (nf <= 3)
159 f[3] = NULL;
160 if (nf <= 4)
161 f[4] = NULL;
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);
166 }
167
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",
171 erbuf, badpat);
172 status = 1;
173 }
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);
179 status = 1;
180 }
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",
184 erbuf, bpname);
185 status = 1;
186 }
187 re.re_endp = bpname;
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);
192 status = 1;
193 } else if (ne != strlen(erbuf)+1) {
194 fprintf(stderr, "end: regerror() ATOI test len(`%s') = %ld\n",
195 erbuf, (long)REG_BADPAT);
196 status = 1;
197 }
198}
199
200/*
201 - try - try it, and report on problems
202 == void try(char *f0, char *f1, char *f2, char *f3, char *f4, int opts);
203 */
204void
205try(f0, f1, f2, f3, f4, opts)
206char *f0;
207char *f1;
208char *f2;
209char *f3;
210char *f4;
211int opts; /* may not match f1 */
212{
213 regex_t re;
214# define NSUBS 10
215 regmatch_t subs[NSUBS];
216# define NSHOULD 15
217 char *should[NSHOULD];
218 int nshould;
219 char erbuf[100];
220 int err;
221 int len;
222 char *type = (opts & REG_EXTENDED) ? "ERE" : "BRE";
223 register int i;
224 char *grump;
225 char f0copy[1000];
226 char f2copy[1000];
227
228 strlcpy(f0copy, f0, sizeof f0copy);
229 re.re_endp = (opts&REG_PEND) ? f0copy + strlen(f0copy) : NULL;
230 fixstr(f0copy);
231 err = regcomp(&re, f0copy, opts);
232 if (err != 0 && (!opt('C', f1) || err != efind(f2))) {
233 /* unexpected error or wrong error */
234 len = regerror(err, &re, erbuf, sizeof(erbuf));
235 fprintf(stderr, "%d: %s error %s, %d/%zu `%s'\n",
236 line, type, eprint(err), len,
237 sizeof(erbuf), erbuf);
238 status = 1;
239 } else if (err == 0 && opt('C', f1)) {
240 /* unexpected success */
241 fprintf(stderr, "%d: %s should have given REG_%s\n",
242 line, type, f2);
243 status = 1;
244 err = 1; /* so we won't try regexec */
245 }
246
247 if (err != 0) {
248 regfree(&re);
249 return;
250 }
251
252 strlcpy(f2copy, f2, sizeof f2copy);
253 fixstr(f2copy);
254
255 if (options('e', f1)&REG_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;
260 /* the preceding character is relevant with REG_NOTBOL */
261 f2copy[subs[0].rm_so - 1] = subs[0].rm_so > 1 ?
262 f2copy[subs[0].rm_so - 2] : 'X';
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&REG_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 */
318int
319options(type, s)
320int type; /* 'c' compile, 'e' exec */
321char *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 */
375int /* predicate */
376opt(c, s)
377int c;
378char *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 */
387void
388fixstr(p)
389register 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 */
409char * /* NULL or complaint */
410check(str, sub, should)
411char *str;
412regmatch_t sub;
413char *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 p = str + sub.rm_so;
455
456 /* check for not supposed to match */
457 if (should == NULL) {
458 snprintf(grump, sizeof grump, "matched `%.*s'", len, p);
459 return(grump);
460 }
461
462 /* check for wrong match */
463 shlen = (int)strlen(should);
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 */
488static char *
489eprint(err)
490int 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 */
504static int
505efind(name)
506char *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}