1 /* $FreeBSD$ 2 */ 3 #include <stdio.h> 4 #include <string.h> 5 #include <sys/types.h> 6 #include <regex.h> 7 #include <assert.h> 8 9 #include "main.ih" 10 11 char *progname; 12 int debug = 0; 13 int line = 0; 14 int status = 0; 15 16 int copts = REG_EXTENDED; 17 int eopts = 0; 18 regoff_t startoff = 0; 19 regoff_t endoff = 0; 20 21 22 extern int split(); 23 extern void regprint(); 24 25 /* 26 - main - do the simple case, hand off to regress() for regression 27 */ 28 main(argc, argv) 29 int argc; 30 char *argv[]; 31 { 32 regex_t re; 33 # define NS 10 34 regmatch_t subs[NS]; 35 char erbuf[100]; 36 int err; 37 size_t len; 38 int c; 39 int errflg = 0; 40 register int i; 41 extern int optind; 42 extern char *optarg; 43 44 progname = argv[0]; 45 46 while ((c = getopt(argc, argv, "c:e:S:E:x")) != EOF) 47 switch (c) { 48 case 'c': /* compile options */ 49 copts = options('c', optarg); 50 break; 51 case 'e': /* execute options */ 52 eopts = options('e', optarg); 53 break; 54 case 'S': /* start offset */ 55 startoff = (regoff_t)atoi(optarg); 56 break; 57 case 'E': /* end offset */ 58 endoff = (regoff_t)atoi(optarg); 59 break; 60 case 'x': /* Debugging. */ 61 debug++; 62 break; 63 case '?': 64 default: 65 errflg++; 66 break; 67 } 68 if (errflg) { 69 fprintf(stderr, "usage: %s ", progname); 70 fprintf(stderr, "[-c copt][-C][-d] [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, %d/%d `%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®_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, %d/%d `%s'\n", 101 eprint(err), len, sizeof(erbuf), erbuf); 102 exit(status); 103 } 104 if (!(copts®_NOSUB)) { 105 len = (int)(subs[0].rm_eo - subs[0].rm_so); 106 if (subs[0].rm_so != -1) { 107 if (len != 0) 108 printf("match `%.*s'\n", 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 */ 127 void 128 regress(in) 129 FILE *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[strlen(inbuf)-1] = '\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 */ 204 void 205 try(f0, f1, f2, f3, f4, opts) 206 char *f0; 207 char *f1; 208 char *f2; 209 char *f3; 210 char *f4; 211 int 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 strcpy(f0copy, f0); 229 re.re_endp = (opts®_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/%d `%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 strcpy(f2copy, f2); 253 fixstr(f2copy); 254 255 if (options('e', f1)®_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 } 261 err = regexec(&re, f2copy, NSUBS, subs, options('e', f1)); 262 263 if (err != 0 && (f3 != NULL || err != REG_NOMATCH)) { 264 /* unexpected error or wrong error */ 265 len = regerror(err, &re, erbuf, sizeof(erbuf)); 266 fprintf(stderr, "%d: %s exec error %s, %d/%d `%s'\n", 267 line, type, eprint(err), len, 268 sizeof(erbuf), erbuf); 269 status = 1; 270 } else if (err != 0) { 271 /* nothing more to check */ 272 } else if (f3 == NULL) { 273 /* unexpected success */ 274 fprintf(stderr, "%d: %s exec should have failed\n", 275 line, type); 276 status = 1; 277 err = 1; /* just on principle */ 278 } else if (opts®_NOSUB) { 279 /* nothing more to check */ 280 } else if ((grump = check(f2, subs[0], f3)) != NULL) { 281 fprintf(stderr, "%d: %s %s\n", line, type, grump); 282 status = 1; 283 err = 1; 284 } 285 286 if (err != 0 || f4 == NULL) { 287 regfree(&re); 288 return; 289 } 290 291 for (i = 1; i < NSHOULD; i++) 292 should[i] = NULL; 293 nshould = split(f4, should+1, NSHOULD-1, ","); 294 if (nshould == 0) { 295 nshould = 1; 296 should[1] = ""; 297 } 298 for (i = 1; i < NSUBS; i++) { 299 grump = check(f2, subs[i], should[i]); 300 if (grump != NULL) { 301 fprintf(stderr, "%d: %s $%d %s\n", line, 302 type, i, grump); 303 status = 1; 304 err = 1; 305 } 306 } 307 308 regfree(&re); 309 } 310 311 /* 312 - options - pick options out of a regression-test string 313 == int options(int type, char *s); 314 */ 315 int 316 options(type, s) 317 int type; /* 'c' compile, 'e' exec */ 318 char *s; 319 { 320 register char *p; 321 register int o = (type == 'c') ? copts : eopts; 322 register char *legal = (type == 'c') ? "bisnmp" : "^$#tl"; 323 324 for (p = s; *p != '\0'; p++) 325 if (strchr(legal, *p) != NULL) 326 switch (*p) { 327 case 'b': 328 o &= ~REG_EXTENDED; 329 break; 330 case 'i': 331 o |= REG_ICASE; 332 break; 333 case 's': 334 o |= REG_NOSUB; 335 break; 336 case 'n': 337 o |= REG_NEWLINE; 338 break; 339 case 'm': 340 o &= ~REG_EXTENDED; 341 o |= REG_NOSPEC; 342 break; 343 case 'p': 344 o |= REG_PEND; 345 break; 346 case '^': 347 o |= REG_NOTBOL; 348 break; 349 case '$': 350 o |= REG_NOTEOL; 351 break; 352 case '#': 353 o |= REG_STARTEND; 354 break; 355 case 't': /* trace */ 356 o |= REG_TRACE; 357 break; 358 case 'l': /* force long representation */ 359 o |= REG_LARGE; 360 break; 361 case 'r': /* force backref use */ 362 o |= REG_BACKR; 363 break; 364 } 365 return(o); 366 } 367 368 /* 369 - opt - is a particular option in a regression string? 370 == int opt(int c, char *s); 371 */ 372 int /* predicate */ 373 opt(c, s) 374 int c; 375 char *s; 376 { 377 return(strchr(s, c) != NULL); 378 } 379 380 /* 381 - fixstr - transform magic characters in strings 382 == void fixstr(register char *p); 383 */ 384 void 385 fixstr(p) 386 register char *p; 387 { 388 if (p == NULL) 389 return; 390 391 for (; *p != '\0'; p++) 392 if (*p == 'N') 393 *p = '\n'; 394 else if (*p == 'T') 395 *p = '\t'; 396 else if (*p == 'S') 397 *p = ' '; 398 else if (*p == 'Z') 399 *p = '\0'; 400 } 401 402 /* 403 - check - check a substring match 404 == char *check(char *str, regmatch_t sub, char *should); 405 */ 406 char * /* NULL or complaint */ 407 check(str, sub, should) 408 char *str; 409 regmatch_t sub; 410 char *should; 411 { 412 register int len; 413 register int shlen; 414 register char *p; 415 static char grump[500]; 416 register char *at = NULL; 417 418 if (should != NULL && strcmp(should, "-") == 0) 419 should = NULL; 420 if (should != NULL && should[0] == '@') { 421 at = should + 1; 422 should = ""; 423 } 424 425 /* check rm_so and rm_eo for consistency */ 426 if (sub.rm_so > sub.rm_eo || (sub.rm_so == -1 && sub.rm_eo != -1) || 427 (sub.rm_so != -1 && sub.rm_eo == -1) || 428 (sub.rm_so != -1 && sub.rm_so < 0) || 429 (sub.rm_eo != -1 && sub.rm_eo < 0) ) { 430 sprintf(grump, "start %ld end %ld", (long)sub.rm_so, 431 (long)sub.rm_eo); 432 return(grump); 433 } 434 435 /* check for no match */ 436 if (sub.rm_so == -1 && should == NULL) 437 return(NULL); 438 if (sub.rm_so == -1) 439 return("did not match"); 440 441 /* check for in range */ 442 if (sub.rm_eo > strlen(str)) { 443 sprintf(grump, "start %ld end %ld, past end of string", 444 (long)sub.rm_so, (long)sub.rm_eo); 445 return(grump); 446 } 447 448 len = (int)(sub.rm_eo - sub.rm_so); 449 shlen = (int)strlen(should); 450 p = str + sub.rm_so; 451 452 /* check for not supposed to match */ 453 if (should == NULL) { 454 sprintf(grump, "matched `%.*s'", len, p); 455 return(grump); 456 } 457 458 /* check for wrong match */ 459 if (len != shlen || strncmp(p, should, (size_t)shlen) != 0) { 460 sprintf(grump, "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 sprintf(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 sprintf(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 } 513