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