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