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