1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1992, 1993, 1994 Henry Spencer. 5 * Copyright (c) 1992, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Copyright (c) 2011 The FreeBSD Foundation 9 * All rights reserved. 10 * Portions of this software were developed by David Chisnall 11 * under sponsorship from the FreeBSD Foundation. 12 * 13 * This code is derived from software contributed to Berkeley by 14 * Henry Spencer. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94 41 */ 42 43 #if defined(LIBC_SCCS) && !defined(lint) 44 static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94"; 45 #endif /* LIBC_SCCS and not lint */ 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include <sys/types.h> 50 #include <stdio.h> 51 #include <string.h> 52 #include <ctype.h> 53 #include <limits.h> 54 #include <stdlib.h> 55 #include <regex.h> 56 #include <stdbool.h> 57 #include <wchar.h> 58 #include <wctype.h> 59 60 #ifndef LIBREGEX 61 #include "collate.h" 62 #endif 63 64 #include "utils.h" 65 #include "regex2.h" 66 67 #include "cname.h" 68 69 /* 70 * Branching context, used to keep track of branch state for all of the branch- 71 * aware functions. In addition to keeping track of branch positions for the 72 * p_branch_* functions, we use this to simplify some clumsiness in BREs for 73 * detection of whether ^ is acting as an anchor or being used erroneously and 74 * also for whether we're in a sub-expression or not. 75 */ 76 struct branchc { 77 sopno start; 78 sopno back; 79 sopno fwd; 80 81 int nbranch; 82 int nchain; 83 bool outer; 84 bool terminate; 85 }; 86 87 /* 88 * parse structure, passed up and down to avoid global variables and 89 * other clumsinesses 90 */ 91 struct parse { 92 const char *next; /* next character in RE */ 93 const char *end; /* end of string (-> NUL normally) */ 94 int error; /* has an error been seen? */ 95 int gnuext; 96 sop *strip; /* malloced strip */ 97 sopno ssize; /* malloced strip size (allocated) */ 98 sopno slen; /* malloced strip length (used) */ 99 int ncsalloc; /* number of csets allocated */ 100 struct re_guts *g; 101 # define NPAREN 10 /* we need to remember () 1-9 for back refs */ 102 sopno pbegin[NPAREN]; /* -> ( ([0] unused) */ 103 sopno pend[NPAREN]; /* -> ) ([0] unused) */ 104 bool allowbranch; /* can this expression branch? */ 105 bool bre; /* convenience; is this a BRE? */ 106 int pflags; /* other parsing flags -- legacy escapes? */ 107 bool (*parse_expr)(struct parse *, struct branchc *); 108 void (*pre_parse)(struct parse *, struct branchc *); 109 void (*post_parse)(struct parse *, struct branchc *); 110 }; 111 112 #define PFLAG_LEGACY_ESC 0x00000001 113 114 /* ========= begin header generated by ./mkh ========= */ 115 #ifdef __cplusplus 116 extern "C" { 117 #endif 118 119 /* === regcomp.c === */ 120 static bool p_ere_exp(struct parse *p, struct branchc *bc); 121 static void p_str(struct parse *p); 122 static int p_branch_eat_delim(struct parse *p, struct branchc *bc); 123 static void p_branch_ins_offset(struct parse *p, struct branchc *bc); 124 static void p_branch_fix_tail(struct parse *p, struct branchc *bc); 125 static bool p_branch_empty(struct parse *p, struct branchc *bc); 126 static bool p_branch_do(struct parse *p, struct branchc *bc); 127 static void p_bre_pre_parse(struct parse *p, struct branchc *bc); 128 static void p_bre_post_parse(struct parse *p, struct branchc *bc); 129 static void p_re(struct parse *p, int end1, int end2); 130 static bool p_simp_re(struct parse *p, struct branchc *bc); 131 static int p_count(struct parse *p); 132 static void p_bracket(struct parse *p); 133 static int p_range_cmp(wchar_t c1, wchar_t c2); 134 static void p_b_term(struct parse *p, cset *cs); 135 static int p_b_pseudoclass(struct parse *p, char c); 136 static void p_b_cclass(struct parse *p, cset *cs); 137 static void p_b_cclass_named(struct parse *p, cset *cs, const char[]); 138 static void p_b_eclass(struct parse *p, cset *cs); 139 static wint_t p_b_symbol(struct parse *p); 140 static wint_t p_b_coll_elem(struct parse *p, wint_t endc); 141 static bool may_escape(struct parse *p, const wint_t ch); 142 static wint_t othercase(wint_t ch); 143 static void bothcases(struct parse *p, wint_t ch); 144 static void ordinary(struct parse *p, wint_t ch); 145 static void nonnewline(struct parse *p); 146 static void repeat(struct parse *p, sopno start, int from, int to); 147 static int seterr(struct parse *p, int e); 148 static cset *allocset(struct parse *p); 149 static void freeset(struct parse *p, cset *cs); 150 static void CHadd(struct parse *p, cset *cs, wint_t ch); 151 static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max); 152 static void CHaddtype(struct parse *p, cset *cs, wctype_t wct); 153 static wint_t singleton(cset *cs); 154 static sopno dupl(struct parse *p, sopno start, sopno finish); 155 static void doemit(struct parse *p, sop op, size_t opnd); 156 static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 157 static void dofwd(struct parse *p, sopno pos, sop value); 158 static int enlarge(struct parse *p, sopno size); 159 static void stripsnug(struct parse *p, struct re_guts *g); 160 static void findmust(struct parse *p, struct re_guts *g); 161 static int altoffset(sop *scan, int offset); 162 static void computejumps(struct parse *p, struct re_guts *g); 163 static void computematchjumps(struct parse *p, struct re_guts *g); 164 static sopno pluscount(struct parse *p, struct re_guts *g); 165 static wint_t wgetnext(struct parse *p); 166 167 #ifdef __cplusplus 168 } 169 #endif 170 /* ========= end header generated by ./mkh ========= */ 171 172 static char nuls[10]; /* place to point scanner in event of error */ 173 174 /* 175 * macros for use with parse structure 176 * BEWARE: these know that the parse structure is named `p' !!! 177 */ 178 #define PEEK() (*p->next) 179 #define PEEK2() (*(p->next+1)) 180 #define MORE() (p->next < p->end) 181 #define MORE2() (p->next+1 < p->end) 182 #define SEE(c) (MORE() && PEEK() == (c)) 183 #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b)) 184 #define SEESPEC(a) (p->bre ? SEETWO('\\', a) : SEE(a)) 185 #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0) 186 #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0) 187 #define EATSPEC(a) (p->bre ? EATTWO('\\', a) : EAT(a)) 188 #define NEXT() (p->next++) 189 #define NEXT2() (p->next += 2) 190 #define NEXTn(n) (p->next += (n)) 191 #define GETNEXT() (*p->next++) 192 #define WGETNEXT() wgetnext(p) 193 #define SETERROR(e) seterr(p, (e)) 194 #define REQUIRE(co, e) ((co) || SETERROR(e)) 195 #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e)) 196 #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e)) 197 #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e)) 198 #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd)) 199 #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos) 200 #define AHEAD(pos) dofwd(p, pos, HERE()-(pos)) 201 #define ASTERN(sop, pos) EMIT(sop, HERE()-pos) 202 #define HERE() (p->slen) 203 #define THERE() (p->slen - 1) 204 #define THERETHERE() (p->slen - 2) 205 #define DROP(n) (p->slen -= (n)) 206 207 /* Macro used by computejump()/computematchjump() */ 208 #define MIN(a,b) ((a)<(b)?(a):(b)) 209 210 static int /* 0 success, otherwise REG_something */ 211 regcomp_internal(regex_t * __restrict preg, 212 const char * __restrict pattern, 213 int cflags, int pflags) 214 { 215 struct parse pa; 216 struct re_guts *g; 217 struct parse *p = &pa; 218 int i; 219 size_t len; 220 size_t maxlen; 221 #ifdef REDEBUG 222 # define GOODFLAGS(f) (f) 223 #else 224 # define GOODFLAGS(f) ((f)&~REG_DUMP) 225 #endif 226 227 cflags = GOODFLAGS(cflags); 228 if ((cflags®_EXTENDED) && (cflags®_NOSPEC)) 229 return(REG_INVARG); 230 231 if (cflags®_PEND) { 232 if (preg->re_endp < pattern) 233 return(REG_INVARG); 234 len = preg->re_endp - pattern; 235 } else 236 len = strlen(pattern); 237 238 /* do the mallocs early so failure handling is easy */ 239 g = (struct re_guts *)malloc(sizeof(struct re_guts)); 240 if (g == NULL) 241 return(REG_ESPACE); 242 /* 243 * Limit the pattern space to avoid a 32-bit overflow on buffer 244 * extension. Also avoid any signed overflow in case of conversion 245 * so make the real limit based on a 31-bit overflow. 246 * 247 * Likely not applicable on 64-bit systems but handle the case 248 * generically (who are we to stop people from using ~715MB+ 249 * patterns?). 250 */ 251 maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3; 252 if (len >= maxlen) { 253 free((char *)g); 254 return(REG_ESPACE); 255 } 256 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */ 257 assert(p->ssize >= len); 258 259 p->strip = (sop *)malloc(p->ssize * sizeof(sop)); 260 p->slen = 0; 261 if (p->strip == NULL) { 262 free((char *)g); 263 return(REG_ESPACE); 264 } 265 266 /* set things up */ 267 p->g = g; 268 p->next = pattern; /* convenience; we do not modify it */ 269 p->end = p->next + len; 270 p->error = 0; 271 p->ncsalloc = 0; 272 p->pflags = pflags; 273 for (i = 0; i < NPAREN; i++) { 274 p->pbegin[i] = 0; 275 p->pend[i] = 0; 276 } 277 #ifdef LIBREGEX 278 if (cflags®_POSIX) { 279 p->gnuext = false; 280 p->allowbranch = (cflags & REG_EXTENDED) != 0; 281 } else 282 p->gnuext = p->allowbranch = true; 283 #else 284 p->gnuext = false; 285 p->allowbranch = (cflags & REG_EXTENDED) != 0; 286 #endif 287 if (cflags & REG_EXTENDED) { 288 p->bre = false; 289 p->parse_expr = p_ere_exp; 290 p->pre_parse = NULL; 291 p->post_parse = NULL; 292 } else { 293 p->bre = true; 294 p->parse_expr = p_simp_re; 295 p->pre_parse = p_bre_pre_parse; 296 p->post_parse = p_bre_post_parse; 297 } 298 g->sets = NULL; 299 g->ncsets = 0; 300 g->cflags = cflags; 301 g->iflags = 0; 302 g->nbol = 0; 303 g->neol = 0; 304 g->must = NULL; 305 g->moffset = -1; 306 g->charjump = NULL; 307 g->matchjump = NULL; 308 g->mlen = 0; 309 g->nsub = 0; 310 g->backrefs = 0; 311 312 /* do it */ 313 EMIT(OEND, 0); 314 g->firststate = THERE(); 315 if (cflags & REG_NOSPEC) 316 p_str(p); 317 else 318 p_re(p, OUT, OUT); 319 EMIT(OEND, 0); 320 g->laststate = THERE(); 321 322 /* tidy up loose ends and fill things in */ 323 stripsnug(p, g); 324 findmust(p, g); 325 /* only use Boyer-Moore algorithm if the pattern is bigger 326 * than three characters 327 */ 328 if(g->mlen > 3) { 329 computejumps(p, g); 330 computematchjumps(p, g); 331 if(g->matchjump == NULL && g->charjump != NULL) { 332 free(g->charjump); 333 g->charjump = NULL; 334 } 335 } 336 g->nplus = pluscount(p, g); 337 g->magic = MAGIC2; 338 preg->re_nsub = g->nsub; 339 preg->re_g = g; 340 preg->re_magic = MAGIC1; 341 #ifndef REDEBUG 342 /* not debugging, so can't rely on the assert() in regexec() */ 343 if (g->iflags&BAD) 344 SETERROR(REG_ASSERT); 345 #endif 346 347 /* win or lose, we're done */ 348 if (p->error != 0) /* lose */ 349 regfree(preg); 350 return(p->error); 351 } 352 353 /* 354 - regcomp - interface for parser and compilation 355 = extern int regcomp(regex_t *, const char *, int); 356 = #define REG_BASIC 0000 357 = #define REG_EXTENDED 0001 358 = #define REG_ICASE 0002 359 = #define REG_NOSUB 0004 360 = #define REG_NEWLINE 0010 361 = #define REG_NOSPEC 0020 362 = #define REG_PEND 0040 363 = #define REG_DUMP 0200 364 */ 365 int /* 0 success, otherwise REG_something */ 366 regcomp(regex_t * __restrict preg, 367 const char * __restrict pattern, 368 int cflags) 369 { 370 371 return (regcomp_internal(preg, pattern, cflags, 0)); 372 } 373 374 #ifndef LIBREGEX 375 /* 376 * Legacy interface that requires more lax escaping behavior. 377 */ 378 int 379 freebsd12_regcomp(regex_t * __restrict preg, 380 const char * __restrict pattern, 381 int cflags, int pflags) 382 { 383 384 return (regcomp_internal(preg, pattern, cflags, PFLAG_LEGACY_ESC)); 385 } 386 387 __sym_compat(regcomp, freebsd12_regcomp, FBSD_1.0); 388 #endif /* !LIBREGEX */ 389 390 /* 391 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op, 392 - return whether we should terminate or not 393 == static bool p_ere_exp(struct parse *p); 394 */ 395 static bool 396 p_ere_exp(struct parse *p, struct branchc *bc) 397 { 398 char c; 399 wint_t wc; 400 sopno pos; 401 int count; 402 int count2; 403 #ifdef LIBREGEX 404 int i; 405 int handled; 406 #endif 407 sopno subno; 408 int wascaret = 0; 409 410 (void)bc; 411 assert(MORE()); /* caller should have ensured this */ 412 c = GETNEXT(); 413 414 #ifdef LIBREGEX 415 handled = 0; 416 #endif 417 pos = HERE(); 418 switch (c) { 419 case '(': 420 (void)REQUIRE(MORE(), REG_EPAREN); 421 p->g->nsub++; 422 subno = p->g->nsub; 423 if (subno < NPAREN) 424 p->pbegin[subno] = HERE(); 425 EMIT(OLPAREN, subno); 426 if (!SEE(')')) 427 p_re(p, ')', IGN); 428 if (subno < NPAREN) { 429 p->pend[subno] = HERE(); 430 assert(p->pend[subno] != 0); 431 } 432 EMIT(ORPAREN, subno); 433 (void)MUSTEAT(')', REG_EPAREN); 434 break; 435 #ifndef POSIX_MISTAKE 436 case ')': /* happens only if no current unmatched ( */ 437 /* 438 * You may ask, why the ifndef? Because I didn't notice 439 * this until slightly too late for 1003.2, and none of the 440 * other 1003.2 regular-expression reviewers noticed it at 441 * all. So an unmatched ) is legal POSIX, at least until 442 * we can get it fixed. 443 */ 444 SETERROR(REG_EPAREN); 445 break; 446 #endif 447 case '^': 448 EMIT(OBOL, 0); 449 p->g->iflags |= USEBOL; 450 p->g->nbol++; 451 wascaret = 1; 452 break; 453 case '$': 454 EMIT(OEOL, 0); 455 p->g->iflags |= USEEOL; 456 p->g->neol++; 457 break; 458 case '|': 459 SETERROR(REG_EMPTY); 460 break; 461 case '*': 462 case '+': 463 case '?': 464 case '{': 465 SETERROR(REG_BADRPT); 466 break; 467 case '.': 468 if (p->g->cflags®_NEWLINE) 469 nonnewline(p); 470 else 471 EMIT(OANY, 0); 472 break; 473 case '[': 474 p_bracket(p); 475 break; 476 case '\\': 477 (void)REQUIRE(MORE(), REG_EESCAPE); 478 wc = WGETNEXT(); 479 #ifdef LIBREGEX 480 if (p->gnuext) { 481 handled = 1; 482 switch (wc) { 483 case '`': 484 EMIT(OBOS, 0); 485 break; 486 case '\'': 487 EMIT(OEOS, 0); 488 break; 489 case 'W': 490 case 'w': 491 case 'S': 492 case 's': 493 p_b_pseudoclass(p, wc); 494 break; 495 case '1': 496 case '2': 497 case '3': 498 case '4': 499 case '5': 500 case '6': 501 case '7': 502 case '8': 503 case '9': 504 i = wc - '0'; 505 assert(i < NPAREN); 506 if (p->pend[i] != 0) { 507 assert(i <= p->g->nsub); 508 EMIT(OBACK_, i); 509 assert(p->pbegin[i] != 0); 510 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN); 511 assert(OP(p->strip[p->pend[i]]) == ORPAREN); 512 (void) dupl(p, p->pbegin[i]+1, p->pend[i]); 513 EMIT(O_BACK, i); 514 } else 515 SETERROR(REG_ESUBREG); 516 p->g->backrefs = 1; 517 break; 518 default: 519 handled = 0; 520 } 521 /* Don't proceed to the POSIX bits if we've already handled it */ 522 if (handled) 523 break; 524 } 525 #endif 526 switch (wc) { 527 case '<': 528 EMIT(OBOW, 0); 529 break; 530 case '>': 531 EMIT(OEOW, 0); 532 break; 533 default: 534 if (may_escape(p, wc)) 535 ordinary(p, wc); 536 else 537 SETERROR(REG_EESCAPE); 538 break; 539 } 540 break; 541 default: 542 if (p->error != 0) 543 return (false); 544 p->next--; 545 wc = WGETNEXT(); 546 ordinary(p, wc); 547 break; 548 } 549 550 if (!MORE()) 551 return (false); 552 c = PEEK(); 553 /* we call { a repetition if followed by a digit */ 554 if (!( c == '*' || c == '+' || c == '?' || c == '{')) 555 return (false); /* no repetition, we're done */ 556 else if (c == '{') 557 (void)REQUIRE(MORE2() && \ 558 (isdigit((uch)PEEK2()) || PEEK2() == ','), REG_BADRPT); 559 NEXT(); 560 561 (void)REQUIRE(!wascaret, REG_BADRPT); 562 switch (c) { 563 case '*': /* implemented as +? */ 564 /* this case does not require the (y|) trick, noKLUDGE */ 565 INSERT(OPLUS_, pos); 566 ASTERN(O_PLUS, pos); 567 INSERT(OQUEST_, pos); 568 ASTERN(O_QUEST, pos); 569 break; 570 case '+': 571 INSERT(OPLUS_, pos); 572 ASTERN(O_PLUS, pos); 573 break; 574 case '?': 575 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 576 INSERT(OCH_, pos); /* offset slightly wrong */ 577 ASTERN(OOR1, pos); /* this one's right */ 578 AHEAD(pos); /* fix the OCH_ */ 579 EMIT(OOR2, 0); /* offset very wrong... */ 580 AHEAD(THERE()); /* ...so fix it */ 581 ASTERN(O_CH, THERETHERE()); 582 break; 583 case '{': 584 count = p_count(p); 585 if (EAT(',')) { 586 if (isdigit((uch)PEEK())) { 587 count2 = p_count(p); 588 (void)REQUIRE(count <= count2, REG_BADBR); 589 } else /* single number with comma */ 590 count2 = INFINITY; 591 } else /* just a single number */ 592 count2 = count; 593 repeat(p, pos, count, count2); 594 if (!EAT('}')) { /* error heuristics */ 595 while (MORE() && PEEK() != '}') 596 NEXT(); 597 (void)REQUIRE(MORE(), REG_EBRACE); 598 SETERROR(REG_BADBR); 599 } 600 break; 601 } 602 603 if (!MORE()) 604 return (false); 605 c = PEEK(); 606 if (!( c == '*' || c == '+' || c == '?' || 607 (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) ) 608 return (false); 609 SETERROR(REG_BADRPT); 610 return (false); 611 } 612 613 /* 614 - p_str - string (no metacharacters) "parser" 615 == static void p_str(struct parse *p); 616 */ 617 static void 618 p_str(struct parse *p) 619 { 620 (void)REQUIRE(MORE(), REG_EMPTY); 621 while (MORE()) 622 ordinary(p, WGETNEXT()); 623 } 624 625 /* 626 * Eat consecutive branch delimiters for the kind of expression that we are 627 * parsing, return the number of delimiters that we ate. 628 */ 629 static int 630 p_branch_eat_delim(struct parse *p, struct branchc *bc) 631 { 632 int nskip; 633 634 (void)bc; 635 nskip = 0; 636 while (EATSPEC('|')) 637 ++nskip; 638 return (nskip); 639 } 640 641 /* 642 * Insert necessary branch book-keeping operations. This emits a 643 * bogus 'next' offset, since we still have more to parse 644 */ 645 static void 646 p_branch_ins_offset(struct parse *p, struct branchc *bc) 647 { 648 649 if (bc->nbranch == 0) { 650 INSERT(OCH_, bc->start); /* offset is wrong */ 651 bc->fwd = bc->start; 652 bc->back = bc->start; 653 } 654 655 ASTERN(OOR1, bc->back); 656 bc->back = THERE(); 657 AHEAD(bc->fwd); /* fix previous offset */ 658 bc->fwd = HERE(); 659 EMIT(OOR2, 0); /* offset is very wrong */ 660 ++bc->nbranch; 661 } 662 663 /* 664 * Fix the offset of the tail branch, if we actually had any branches. 665 * This is to correct the bogus placeholder offset that we use. 666 */ 667 static void 668 p_branch_fix_tail(struct parse *p, struct branchc *bc) 669 { 670 671 /* Fix bogus offset at the tail if we actually have branches */ 672 if (bc->nbranch > 0) { 673 AHEAD(bc->fwd); 674 ASTERN(O_CH, bc->back); 675 } 676 } 677 678 /* 679 * Signal to the parser that an empty branch has been encountered; this will, 680 * in the future, be used to allow for more permissive behavior with empty 681 * branches. The return value should indicate whether parsing may continue 682 * or not. 683 */ 684 static bool 685 p_branch_empty(struct parse *p, struct branchc *bc) 686 { 687 688 #if defined(LIBREGEX) && defined(NOTYET) 689 if (bc->outer) 690 p->g->iflags |= EMPTBR; 691 return (true); 692 #else 693 (void)bc; 694 SETERROR(REG_EMPTY); 695 return (false); 696 #endif 697 } 698 699 /* 700 * Take care of any branching requirements. This includes inserting the 701 * appropriate branching instructions as well as eating all of the branch 702 * delimiters until we either run out of pattern or need to parse more pattern. 703 */ 704 static bool 705 p_branch_do(struct parse *p, struct branchc *bc) 706 { 707 int ate = 0; 708 709 ate = p_branch_eat_delim(p, bc); 710 if (ate == 0) 711 return (false); 712 else if ((ate > 1 || (bc->outer && !MORE())) && !p_branch_empty(p, bc)) 713 /* 714 * Halt parsing only if we have an empty branch and p_branch_empty 715 * indicates that we must not continue. In the future, this will not 716 * necessarily be an error. 717 */ 718 return (false); 719 p_branch_ins_offset(p, bc); 720 721 return (true); 722 } 723 724 static void 725 p_bre_pre_parse(struct parse *p, struct branchc *bc) 726 { 727 728 (void) bc; 729 /* 730 * Does not move cleanly into expression parser because of 731 * ordinary interpration of * at the beginning position of 732 * an expression. 733 */ 734 if (EAT('^')) { 735 EMIT(OBOL, 0); 736 p->g->iflags |= USEBOL; 737 p->g->nbol++; 738 } 739 } 740 741 static void 742 p_bre_post_parse(struct parse *p, struct branchc *bc) 743 { 744 745 /* Expression is terminating due to EOL token */ 746 if (bc->terminate) { 747 DROP(1); 748 EMIT(OEOL, 0); 749 p->g->iflags |= USEEOL; 750 p->g->neol++; 751 } 752 } 753 754 /* 755 - p_re - Top level parser, concatenation and BRE anchoring 756 == static void p_re(struct parse *p, int end1, int end2); 757 * Giving end1 as OUT essentially eliminates the end1/end2 check. 758 * 759 * This implementation is a bit of a kludge, in that a trailing $ is first 760 * taken as an ordinary character and then revised to be an anchor. 761 * The amount of lookahead needed to avoid this kludge is excessive. 762 */ 763 static void 764 p_re(struct parse *p, 765 int end1, /* first terminating character */ 766 int end2) /* second terminating character; ignored for EREs */ 767 { 768 struct branchc bc; 769 770 bc.nbranch = 0; 771 if (end1 == OUT && end2 == OUT) 772 bc.outer = true; 773 else 774 bc.outer = false; 775 #define SEEEND() (!p->bre ? SEE(end1) : SEETWO(end1, end2)) 776 for (;;) { 777 bc.start = HERE(); 778 bc.nchain = 0; 779 bc.terminate = false; 780 if (p->pre_parse != NULL) 781 p->pre_parse(p, &bc); 782 while (MORE() && (!p->allowbranch || !SEESPEC('|')) && !SEEEND()) { 783 bc.terminate = p->parse_expr(p, &bc); 784 ++bc.nchain; 785 } 786 if (p->post_parse != NULL) 787 p->post_parse(p, &bc); 788 (void) REQUIRE(p->gnuext || HERE() != bc.start, REG_EMPTY); 789 #ifdef LIBREGEX 790 if (HERE() == bc.start && !p_branch_empty(p, &bc)) 791 break; 792 #endif 793 if (!p->allowbranch) 794 break; 795 /* 796 * p_branch_do's return value indicates whether we should 797 * continue parsing or not. This is both for correctness and 798 * a slight optimization, because it will check if we've 799 * encountered an empty branch or the end of the string 800 * immediately following a branch delimiter. 801 */ 802 if (!p_branch_do(p, &bc)) 803 break; 804 } 805 #undef SEE_END 806 if (p->allowbranch) 807 p_branch_fix_tail(p, &bc); 808 assert(!MORE() || SEE(end1)); 809 } 810 811 /* 812 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition 813 == static bool p_simp_re(struct parse *p, struct branchc *bc); 814 */ 815 static bool /* was the simple RE an unbackslashed $? */ 816 p_simp_re(struct parse *p, struct branchc *bc) 817 { 818 int c; 819 int cc; /* convenient/control character */ 820 int count; 821 int count2; 822 sopno pos; 823 bool handled; 824 int i; 825 wint_t wc; 826 sopno subno; 827 # define BACKSL (1<<CHAR_BIT) 828 829 pos = HERE(); /* repetition op, if any, covers from here */ 830 handled = false; 831 832 assert(MORE()); /* caller should have ensured this */ 833 c = GETNEXT(); 834 if (c == '\\') { 835 (void)REQUIRE(MORE(), REG_EESCAPE); 836 cc = GETNEXT(); 837 c = BACKSL | cc; 838 #ifdef LIBREGEX 839 if (p->gnuext) { 840 handled = true; 841 switch (c) { 842 case BACKSL|'`': 843 EMIT(OBOS, 0); 844 break; 845 case BACKSL|'\'': 846 EMIT(OEOS, 0); 847 break; 848 case BACKSL|'W': 849 case BACKSL|'w': 850 case BACKSL|'S': 851 case BACKSL|'s': 852 p_b_pseudoclass(p, cc); 853 break; 854 default: 855 handled = false; 856 } 857 } 858 #endif 859 } 860 if (!handled) { 861 switch (c) { 862 case '.': 863 if (p->g->cflags®_NEWLINE) 864 nonnewline(p); 865 else 866 EMIT(OANY, 0); 867 break; 868 case '[': 869 p_bracket(p); 870 break; 871 case BACKSL|'<': 872 EMIT(OBOW, 0); 873 break; 874 case BACKSL|'>': 875 EMIT(OEOW, 0); 876 break; 877 case BACKSL|'{': 878 SETERROR(REG_BADRPT); 879 break; 880 case BACKSL|'(': 881 p->g->nsub++; 882 subno = p->g->nsub; 883 if (subno < NPAREN) 884 p->pbegin[subno] = HERE(); 885 EMIT(OLPAREN, subno); 886 /* the MORE here is an error heuristic */ 887 if (MORE() && !SEETWO('\\', ')')) 888 p_re(p, '\\', ')'); 889 if (subno < NPAREN) { 890 p->pend[subno] = HERE(); 891 assert(p->pend[subno] != 0); 892 } 893 EMIT(ORPAREN, subno); 894 (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN); 895 break; 896 case BACKSL|')': /* should not get here -- must be user */ 897 SETERROR(REG_EPAREN); 898 break; 899 case BACKSL|'1': 900 case BACKSL|'2': 901 case BACKSL|'3': 902 case BACKSL|'4': 903 case BACKSL|'5': 904 case BACKSL|'6': 905 case BACKSL|'7': 906 case BACKSL|'8': 907 case BACKSL|'9': 908 i = (c&~BACKSL) - '0'; 909 assert(i < NPAREN); 910 if (p->pend[i] != 0) { 911 assert(i <= p->g->nsub); 912 EMIT(OBACK_, i); 913 assert(p->pbegin[i] != 0); 914 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN); 915 assert(OP(p->strip[p->pend[i]]) == ORPAREN); 916 (void) dupl(p, p->pbegin[i]+1, p->pend[i]); 917 EMIT(O_BACK, i); 918 } else 919 SETERROR(REG_ESUBREG); 920 p->g->backrefs = 1; 921 break; 922 case '*': 923 /* 924 * Ordinary if used as the first character beyond BOL anchor of 925 * a (sub-)expression, counts as a bad repetition operator if it 926 * appears otherwise. 927 */ 928 (void)REQUIRE(bc->nchain == 0, REG_BADRPT); 929 /* FALLTHROUGH */ 930 default: 931 if (p->error != 0) 932 return (false); /* Definitely not $... */ 933 p->next--; 934 wc = WGETNEXT(); 935 if ((c & BACKSL) == 0 || may_escape(p, wc)) 936 ordinary(p, wc); 937 else 938 SETERROR(REG_EESCAPE); 939 break; 940 } 941 } 942 943 if (EAT('*')) { /* implemented as +? */ 944 /* this case does not require the (y|) trick, noKLUDGE */ 945 INSERT(OPLUS_, pos); 946 ASTERN(O_PLUS, pos); 947 INSERT(OQUEST_, pos); 948 ASTERN(O_QUEST, pos); 949 #ifdef LIBREGEX 950 } else if (p->gnuext && EATTWO('\\', '?')) { 951 INSERT(OQUEST_, pos); 952 ASTERN(O_QUEST, pos); 953 } else if (p->gnuext && EATTWO('\\', '+')) { 954 INSERT(OPLUS_, pos); 955 ASTERN(O_PLUS, pos); 956 #endif 957 } else if (EATTWO('\\', '{')) { 958 count = p_count(p); 959 if (EAT(',')) { 960 if (MORE() && isdigit((uch)PEEK())) { 961 count2 = p_count(p); 962 (void)REQUIRE(count <= count2, REG_BADBR); 963 } else /* single number with comma */ 964 count2 = INFINITY; 965 } else /* just a single number */ 966 count2 = count; 967 repeat(p, pos, count, count2); 968 if (!EATTWO('\\', '}')) { /* error heuristics */ 969 while (MORE() && !SEETWO('\\', '}')) 970 NEXT(); 971 (void)REQUIRE(MORE(), REG_EBRACE); 972 SETERROR(REG_BADBR); 973 } 974 } else if (c == '$') /* $ (but not \$) ends it */ 975 return (true); 976 977 return (false); 978 } 979 980 /* 981 - p_count - parse a repetition count 982 == static int p_count(struct parse *p); 983 */ 984 static int /* the value */ 985 p_count(struct parse *p) 986 { 987 int count = 0; 988 int ndigits = 0; 989 990 while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) { 991 count = count*10 + (GETNEXT() - '0'); 992 ndigits++; 993 } 994 995 (void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR); 996 return(count); 997 } 998 999 /* 1000 - p_bracket - parse a bracketed character list 1001 == static void p_bracket(struct parse *p); 1002 */ 1003 static void 1004 p_bracket(struct parse *p) 1005 { 1006 cset *cs; 1007 wint_t ch; 1008 1009 /* Dept of Truly Sickening Special-Case Kludges */ 1010 if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) { 1011 EMIT(OBOW, 0); 1012 NEXTn(6); 1013 return; 1014 } 1015 if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) { 1016 EMIT(OEOW, 0); 1017 NEXTn(6); 1018 return; 1019 } 1020 1021 if ((cs = allocset(p)) == NULL) 1022 return; 1023 1024 if (p->g->cflags®_ICASE) 1025 cs->icase = 1; 1026 if (EAT('^')) 1027 cs->invert = 1; 1028 if (EAT(']')) 1029 CHadd(p, cs, ']'); 1030 else if (EAT('-')) 1031 CHadd(p, cs, '-'); 1032 while (MORE() && PEEK() != ']' && !SEETWO('-', ']')) 1033 p_b_term(p, cs); 1034 if (EAT('-')) 1035 CHadd(p, cs, '-'); 1036 (void)MUSTEAT(']', REG_EBRACK); 1037 1038 if (p->error != 0) /* don't mess things up further */ 1039 return; 1040 1041 if (cs->invert && p->g->cflags®_NEWLINE) 1042 cs->bmp['\n' >> 3] |= 1 << ('\n' & 7); 1043 1044 if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */ 1045 ordinary(p, ch); 1046 freeset(p, cs); 1047 } else 1048 EMIT(OANYOF, (int)(cs - p->g->sets)); 1049 } 1050 1051 static int 1052 p_range_cmp(wchar_t c1, wchar_t c2) 1053 { 1054 #ifndef LIBREGEX 1055 return __wcollate_range_cmp(c1, c2); 1056 #else 1057 /* Copied from libc/collate __wcollate_range_cmp */ 1058 wchar_t s1[2], s2[2]; 1059 1060 s1[0] = c1; 1061 s1[1] = L'\0'; 1062 s2[0] = c2; 1063 s2[1] = L'\0'; 1064 return (wcscoll(s1, s2)); 1065 #endif 1066 } 1067 1068 /* 1069 - p_b_term - parse one term of a bracketed character list 1070 == static void p_b_term(struct parse *p, cset *cs); 1071 */ 1072 static void 1073 p_b_term(struct parse *p, cset *cs) 1074 { 1075 char c; 1076 wint_t start, finish; 1077 wint_t i; 1078 #ifndef LIBREGEX 1079 struct xlocale_collate *table = 1080 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; 1081 #endif 1082 /* classify what we've got */ 1083 switch ((MORE()) ? PEEK() : '\0') { 1084 case '[': 1085 c = (MORE2()) ? PEEK2() : '\0'; 1086 break; 1087 case '-': 1088 SETERROR(REG_ERANGE); 1089 return; /* NOTE RETURN */ 1090 default: 1091 c = '\0'; 1092 break; 1093 } 1094 1095 switch (c) { 1096 case ':': /* character class */ 1097 NEXT2(); 1098 (void)REQUIRE(MORE(), REG_EBRACK); 1099 c = PEEK(); 1100 (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE); 1101 p_b_cclass(p, cs); 1102 (void)REQUIRE(MORE(), REG_EBRACK); 1103 (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE); 1104 break; 1105 case '=': /* equivalence class */ 1106 NEXT2(); 1107 (void)REQUIRE(MORE(), REG_EBRACK); 1108 c = PEEK(); 1109 (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE); 1110 p_b_eclass(p, cs); 1111 (void)REQUIRE(MORE(), REG_EBRACK); 1112 (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE); 1113 break; 1114 default: /* symbol, ordinary character, or range */ 1115 start = p_b_symbol(p); 1116 if (SEE('-') && MORE2() && PEEK2() != ']') { 1117 /* range */ 1118 NEXT(); 1119 if (EAT('-')) 1120 finish = '-'; 1121 else 1122 finish = p_b_symbol(p); 1123 } else 1124 finish = start; 1125 if (start == finish) 1126 CHadd(p, cs, start); 1127 else { 1128 #ifndef LIBREGEX 1129 if (table->__collate_load_error || MB_CUR_MAX > 1) { 1130 #else 1131 if (MB_CUR_MAX > 1) { 1132 #endif 1133 (void)REQUIRE(start <= finish, REG_ERANGE); 1134 CHaddrange(p, cs, start, finish); 1135 } else { 1136 (void)REQUIRE(p_range_cmp(start, finish) <= 0, REG_ERANGE); 1137 for (i = 0; i <= UCHAR_MAX; i++) { 1138 if (p_range_cmp(start, i) <= 0 && 1139 p_range_cmp(i, finish) <= 0 ) 1140 CHadd(p, cs, i); 1141 } 1142 } 1143 } 1144 break; 1145 } 1146 } 1147 1148 /* 1149 - p_b_pseudoclass - parse a pseudo-class (\w, \W, \s, \S) 1150 == static int p_b_pseudoclass(struct parse *p, char c) 1151 */ 1152 static int 1153 p_b_pseudoclass(struct parse *p, char c) { 1154 cset *cs; 1155 1156 if ((cs = allocset(p)) == NULL) 1157 return(0); 1158 1159 if (p->g->cflags®_ICASE) 1160 cs->icase = 1; 1161 1162 switch (c) { 1163 case 'W': 1164 cs->invert = 1; 1165 /* PASSTHROUGH */ 1166 case 'w': 1167 p_b_cclass_named(p, cs, "alnum"); 1168 break; 1169 case 'S': 1170 cs->invert = 1; 1171 /* PASSTHROUGH */ 1172 case 's': 1173 p_b_cclass_named(p, cs, "space"); 1174 break; 1175 default: 1176 return(0); 1177 } 1178 1179 EMIT(OANYOF, (int)(cs - p->g->sets)); 1180 return(1); 1181 } 1182 1183 /* 1184 - p_b_cclass - parse a character-class name and deal with it 1185 == static void p_b_cclass(struct parse *p, cset *cs); 1186 */ 1187 static void 1188 p_b_cclass(struct parse *p, cset *cs) 1189 { 1190 const char *sp = p->next; 1191 size_t len; 1192 char clname[16]; 1193 1194 while (MORE() && isalpha((uch)PEEK())) 1195 NEXT(); 1196 len = p->next - sp; 1197 if (len >= sizeof(clname) - 1) { 1198 SETERROR(REG_ECTYPE); 1199 return; 1200 } 1201 memcpy(clname, sp, len); 1202 clname[len] = '\0'; 1203 1204 p_b_cclass_named(p, cs, clname); 1205 } 1206 /* 1207 - p_b_cclass_named - deal with a named character class 1208 == static void p_b_cclass_named(struct parse *p, cset *cs, const char []); 1209 */ 1210 static void 1211 p_b_cclass_named(struct parse *p, cset *cs, const char clname[]) { 1212 wctype_t wct; 1213 1214 if ((wct = wctype(clname)) == 0) { 1215 SETERROR(REG_ECTYPE); 1216 return; 1217 } 1218 CHaddtype(p, cs, wct); 1219 } 1220 1221 /* 1222 - p_b_eclass - parse an equivalence-class name and deal with it 1223 == static void p_b_eclass(struct parse *p, cset *cs); 1224 * 1225 * This implementation is incomplete. xxx 1226 */ 1227 static void 1228 p_b_eclass(struct parse *p, cset *cs) 1229 { 1230 wint_t c; 1231 1232 c = p_b_coll_elem(p, '='); 1233 CHadd(p, cs, c); 1234 } 1235 1236 /* 1237 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol 1238 == static wint_t p_b_symbol(struct parse *p); 1239 */ 1240 static wint_t /* value of symbol */ 1241 p_b_symbol(struct parse *p) 1242 { 1243 wint_t value; 1244 1245 (void)REQUIRE(MORE(), REG_EBRACK); 1246 if (!EATTWO('[', '.')) 1247 return(WGETNEXT()); 1248 1249 /* collating symbol */ 1250 value = p_b_coll_elem(p, '.'); 1251 (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE); 1252 return(value); 1253 } 1254 1255 /* 1256 - p_b_coll_elem - parse a collating-element name and look it up 1257 == static wint_t p_b_coll_elem(struct parse *p, wint_t endc); 1258 */ 1259 static wint_t /* value of collating element */ 1260 p_b_coll_elem(struct parse *p, 1261 wint_t endc) /* name ended by endc,']' */ 1262 { 1263 const char *sp = p->next; 1264 struct cname *cp; 1265 mbstate_t mbs; 1266 wchar_t wc; 1267 size_t clen, len; 1268 1269 while (MORE() && !SEETWO(endc, ']')) 1270 NEXT(); 1271 if (!MORE()) { 1272 SETERROR(REG_EBRACK); 1273 return(0); 1274 } 1275 len = p->next - sp; 1276 for (cp = cnames; cp->name != NULL; cp++) 1277 if (strncmp(cp->name, sp, len) == 0 && strlen(cp->name) == len) 1278 return(cp->code); /* known name */ 1279 memset(&mbs, 0, sizeof(mbs)); 1280 if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len) 1281 return (wc); /* single character */ 1282 else if (clen == (size_t)-1 || clen == (size_t)-2) 1283 SETERROR(REG_ILLSEQ); 1284 else 1285 SETERROR(REG_ECOLLATE); /* neither */ 1286 return(0); 1287 } 1288 1289 /* 1290 - may_escape - determine whether 'ch' is escape-able in the current context 1291 == static int may_escape(struct parse *p, const wint_t ch) 1292 */ 1293 static bool 1294 may_escape(struct parse *p, const wint_t ch) 1295 { 1296 1297 if ((p->pflags & PFLAG_LEGACY_ESC) != 0) 1298 return (true); 1299 if (isalpha(ch) || ch == '\'' || ch == '`') 1300 return (false); 1301 return (true); 1302 #ifdef NOTYET 1303 /* 1304 * Build a whitelist of characters that may be escaped to produce an 1305 * ordinary in the current context. This assumes that these have not 1306 * been otherwise interpreted as a special character. Escaping an 1307 * ordinary character yields undefined results according to 1308 * IEEE 1003.1-2008. Some extensions (notably, some GNU extensions) take 1309 * advantage of this and use escaped ordinary characters to provide 1310 * special meaning, e.g. \b, \B, \w, \W, \s, \S. 1311 */ 1312 switch(ch) { 1313 case '|': 1314 case '+': 1315 case '?': 1316 /* The above characters may not be escaped in BREs */ 1317 if (!(p->g->cflags®_EXTENDED)) 1318 return (false); 1319 /* Fallthrough */ 1320 case '(': 1321 case ')': 1322 case '{': 1323 case '}': 1324 case '.': 1325 case '[': 1326 case ']': 1327 case '\\': 1328 case '*': 1329 case '^': 1330 case '$': 1331 return (true); 1332 default: 1333 return (false); 1334 } 1335 #endif 1336 } 1337 1338 /* 1339 - othercase - return the case counterpart of an alphabetic 1340 == static wint_t othercase(wint_t ch); 1341 */ 1342 static wint_t /* if no counterpart, return ch */ 1343 othercase(wint_t ch) 1344 { 1345 assert(iswalpha(ch)); 1346 if (iswupper(ch)) 1347 return(towlower(ch)); 1348 else if (iswlower(ch)) 1349 return(towupper(ch)); 1350 else /* peculiar, but could happen */ 1351 return(ch); 1352 } 1353 1354 /* 1355 - bothcases - emit a dualcase version of a two-case character 1356 == static void bothcases(struct parse *p, wint_t ch); 1357 * 1358 * Boy, is this implementation ever a kludge... 1359 */ 1360 static void 1361 bothcases(struct parse *p, wint_t ch) 1362 { 1363 const char *oldnext = p->next; 1364 const char *oldend = p->end; 1365 char bracket[3 + MB_LEN_MAX]; 1366 size_t n; 1367 mbstate_t mbs; 1368 1369 assert(othercase(ch) != ch); /* p_bracket() would recurse */ 1370 p->next = bracket; 1371 memset(&mbs, 0, sizeof(mbs)); 1372 n = wcrtomb(bracket, ch, &mbs); 1373 assert(n != (size_t)-1); 1374 bracket[n] = ']'; 1375 bracket[n + 1] = '\0'; 1376 p->end = bracket+n+1; 1377 p_bracket(p); 1378 assert(p->next == p->end); 1379 p->next = oldnext; 1380 p->end = oldend; 1381 } 1382 1383 /* 1384 - ordinary - emit an ordinary character 1385 == static void ordinary(struct parse *p, wint_t ch); 1386 */ 1387 static void 1388 ordinary(struct parse *p, wint_t ch) 1389 { 1390 cset *cs; 1391 1392 if ((p->g->cflags®_ICASE) && iswalpha(ch) && othercase(ch) != ch) 1393 bothcases(p, ch); 1394 else if ((ch & OPDMASK) == ch) 1395 EMIT(OCHAR, ch); 1396 else { 1397 /* 1398 * Kludge: character is too big to fit into an OCHAR operand. 1399 * Emit a singleton set. 1400 */ 1401 if ((cs = allocset(p)) == NULL) 1402 return; 1403 CHadd(p, cs, ch); 1404 EMIT(OANYOF, (int)(cs - p->g->sets)); 1405 } 1406 } 1407 1408 /* 1409 - nonnewline - emit REG_NEWLINE version of OANY 1410 == static void nonnewline(struct parse *p); 1411 * 1412 * Boy, is this implementation ever a kludge... 1413 */ 1414 static void 1415 nonnewline(struct parse *p) 1416 { 1417 const char *oldnext = p->next; 1418 const char *oldend = p->end; 1419 char bracket[4]; 1420 1421 p->next = bracket; 1422 p->end = bracket+3; 1423 bracket[0] = '^'; 1424 bracket[1] = '\n'; 1425 bracket[2] = ']'; 1426 bracket[3] = '\0'; 1427 p_bracket(p); 1428 assert(p->next == bracket+3); 1429 p->next = oldnext; 1430 p->end = oldend; 1431 } 1432 1433 /* 1434 - repeat - generate code for a bounded repetition, recursively if needed 1435 == static void repeat(struct parse *p, sopno start, int from, int to); 1436 */ 1437 static void 1438 repeat(struct parse *p, 1439 sopno start, /* operand from here to end of strip */ 1440 int from, /* repeated from this number */ 1441 int to) /* to this number of times (maybe INFINITY) */ 1442 { 1443 sopno finish = HERE(); 1444 # define N 2 1445 # define INF 3 1446 # define REP(f, t) ((f)*8 + (t)) 1447 # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N) 1448 sopno copy; 1449 1450 if (p->error != 0) /* head off possible runaway recursion */ 1451 return; 1452 1453 assert(from <= to); 1454 1455 switch (REP(MAP(from), MAP(to))) { 1456 case REP(0, 0): /* must be user doing this */ 1457 DROP(finish-start); /* drop the operand */ 1458 break; 1459 case REP(0, 1): /* as x{1,1}? */ 1460 case REP(0, N): /* as x{1,n}? */ 1461 case REP(0, INF): /* as x{1,}? */ 1462 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 1463 INSERT(OCH_, start); /* offset is wrong... */ 1464 repeat(p, start+1, 1, to); 1465 ASTERN(OOR1, start); 1466 AHEAD(start); /* ... fix it */ 1467 EMIT(OOR2, 0); 1468 AHEAD(THERE()); 1469 ASTERN(O_CH, THERETHERE()); 1470 break; 1471 case REP(1, 1): /* trivial case */ 1472 /* done */ 1473 break; 1474 case REP(1, N): /* as x?x{1,n-1} */ 1475 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 1476 INSERT(OCH_, start); 1477 ASTERN(OOR1, start); 1478 AHEAD(start); 1479 EMIT(OOR2, 0); /* offset very wrong... */ 1480 AHEAD(THERE()); /* ...so fix it */ 1481 ASTERN(O_CH, THERETHERE()); 1482 copy = dupl(p, start+1, finish+1); 1483 assert(copy == finish+4); 1484 repeat(p, copy, 1, to-1); 1485 break; 1486 case REP(1, INF): /* as x+ */ 1487 INSERT(OPLUS_, start); 1488 ASTERN(O_PLUS, start); 1489 break; 1490 case REP(N, N): /* as xx{m-1,n-1} */ 1491 copy = dupl(p, start, finish); 1492 repeat(p, copy, from-1, to-1); 1493 break; 1494 case REP(N, INF): /* as xx{n-1,INF} */ 1495 copy = dupl(p, start, finish); 1496 repeat(p, copy, from-1, to); 1497 break; 1498 default: /* "can't happen" */ 1499 SETERROR(REG_ASSERT); /* just in case */ 1500 break; 1501 } 1502 } 1503 1504 /* 1505 - wgetnext - helper function for WGETNEXT() macro. Gets the next wide 1506 - character from the parse struct, signals a REG_ILLSEQ error if the 1507 - character can't be converted. Returns the number of bytes consumed. 1508 */ 1509 static wint_t 1510 wgetnext(struct parse *p) 1511 { 1512 mbstate_t mbs; 1513 wchar_t wc; 1514 size_t n; 1515 1516 memset(&mbs, 0, sizeof(mbs)); 1517 n = mbrtowc(&wc, p->next, p->end - p->next, &mbs); 1518 if (n == (size_t)-1 || n == (size_t)-2) { 1519 SETERROR(REG_ILLSEQ); 1520 return (0); 1521 } 1522 if (n == 0) 1523 n = 1; 1524 p->next += n; 1525 return (wc); 1526 } 1527 1528 /* 1529 - seterr - set an error condition 1530 == static int seterr(struct parse *p, int e); 1531 */ 1532 static int /* useless but makes type checking happy */ 1533 seterr(struct parse *p, int e) 1534 { 1535 if (p->error == 0) /* keep earliest error condition */ 1536 p->error = e; 1537 p->next = nuls; /* try to bring things to a halt */ 1538 p->end = nuls; 1539 return(0); /* make the return value well-defined */ 1540 } 1541 1542 /* 1543 - allocset - allocate a set of characters for [] 1544 == static cset *allocset(struct parse *p); 1545 */ 1546 static cset * 1547 allocset(struct parse *p) 1548 { 1549 cset *cs, *ncs; 1550 1551 ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs)); 1552 if (ncs == NULL) { 1553 SETERROR(REG_ESPACE); 1554 return (NULL); 1555 } 1556 p->g->sets = ncs; 1557 cs = &p->g->sets[p->g->ncsets++]; 1558 memset(cs, 0, sizeof(*cs)); 1559 1560 return(cs); 1561 } 1562 1563 /* 1564 - freeset - free a now-unused set 1565 == static void freeset(struct parse *p, cset *cs); 1566 */ 1567 static void 1568 freeset(struct parse *p, cset *cs) 1569 { 1570 cset *top = &p->g->sets[p->g->ncsets]; 1571 1572 free(cs->wides); 1573 free(cs->ranges); 1574 free(cs->types); 1575 memset(cs, 0, sizeof(*cs)); 1576 if (cs == top-1) /* recover only the easy case */ 1577 p->g->ncsets--; 1578 } 1579 1580 /* 1581 - singleton - Determine whether a set contains only one character, 1582 - returning it if so, otherwise returning OUT. 1583 */ 1584 static wint_t 1585 singleton(cset *cs) 1586 { 1587 wint_t i, s, n; 1588 1589 for (i = n = 0; i < NC; i++) 1590 if (CHIN(cs, i)) { 1591 n++; 1592 s = i; 1593 } 1594 if (n == 1) 1595 return (s); 1596 if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 && 1597 cs->icase == 0) 1598 return (cs->wides[0]); 1599 /* Don't bother handling the other cases. */ 1600 return (OUT); 1601 } 1602 1603 /* 1604 - CHadd - add character to character set. 1605 */ 1606 static void 1607 CHadd(struct parse *p, cset *cs, wint_t ch) 1608 { 1609 wint_t nch, *newwides; 1610 assert(ch >= 0); 1611 if (ch < NC) 1612 cs->bmp[ch >> 3] |= 1 << (ch & 7); 1613 else { 1614 newwides = reallocarray(cs->wides, cs->nwides + 1, 1615 sizeof(*cs->wides)); 1616 if (newwides == NULL) { 1617 SETERROR(REG_ESPACE); 1618 return; 1619 } 1620 cs->wides = newwides; 1621 cs->wides[cs->nwides++] = ch; 1622 } 1623 if (cs->icase) { 1624 if ((nch = towlower(ch)) < NC) 1625 cs->bmp[nch >> 3] |= 1 << (nch & 7); 1626 if ((nch = towupper(ch)) < NC) 1627 cs->bmp[nch >> 3] |= 1 << (nch & 7); 1628 } 1629 } 1630 1631 /* 1632 - CHaddrange - add all characters in the range [min,max] to a character set. 1633 */ 1634 static void 1635 CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max) 1636 { 1637 crange *newranges; 1638 1639 for (; min < NC && min <= max; min++) 1640 CHadd(p, cs, min); 1641 if (min >= max) 1642 return; 1643 newranges = reallocarray(cs->ranges, cs->nranges + 1, 1644 sizeof(*cs->ranges)); 1645 if (newranges == NULL) { 1646 SETERROR(REG_ESPACE); 1647 return; 1648 } 1649 cs->ranges = newranges; 1650 cs->ranges[cs->nranges].min = min; 1651 cs->ranges[cs->nranges].max = max; 1652 cs->nranges++; 1653 } 1654 1655 /* 1656 - CHaddtype - add all characters of a certain type to a character set. 1657 */ 1658 static void 1659 CHaddtype(struct parse *p, cset *cs, wctype_t wct) 1660 { 1661 wint_t i; 1662 wctype_t *newtypes; 1663 1664 for (i = 0; i < NC; i++) 1665 if (iswctype(i, wct)) 1666 CHadd(p, cs, i); 1667 newtypes = reallocarray(cs->types, cs->ntypes + 1, 1668 sizeof(*cs->types)); 1669 if (newtypes == NULL) { 1670 SETERROR(REG_ESPACE); 1671 return; 1672 } 1673 cs->types = newtypes; 1674 cs->types[cs->ntypes++] = wct; 1675 } 1676 1677 /* 1678 - dupl - emit a duplicate of a bunch of sops 1679 == static sopno dupl(struct parse *p, sopno start, sopno finish); 1680 */ 1681 static sopno /* start of duplicate */ 1682 dupl(struct parse *p, 1683 sopno start, /* from here */ 1684 sopno finish) /* to this less one */ 1685 { 1686 sopno ret = HERE(); 1687 sopno len = finish - start; 1688 1689 assert(finish >= start); 1690 if (len == 0) 1691 return(ret); 1692 if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */ 1693 return(ret); 1694 (void) memcpy((char *)(p->strip + p->slen), 1695 (char *)(p->strip + start), (size_t)len*sizeof(sop)); 1696 p->slen += len; 1697 return(ret); 1698 } 1699 1700 /* 1701 - doemit - emit a strip operator 1702 == static void doemit(struct parse *p, sop op, size_t opnd); 1703 * 1704 * It might seem better to implement this as a macro with a function as 1705 * hard-case backup, but it's just too big and messy unless there are 1706 * some changes to the data structures. Maybe later. 1707 */ 1708 static void 1709 doemit(struct parse *p, sop op, size_t opnd) 1710 { 1711 /* avoid making error situations worse */ 1712 if (p->error != 0) 1713 return; 1714 1715 /* deal with oversize operands ("can't happen", more or less) */ 1716 assert(opnd < 1<<OPSHIFT); 1717 1718 /* deal with undersized strip */ 1719 if (p->slen >= p->ssize) 1720 if (!enlarge(p, (p->ssize+1) / 2 * 3)) /* +50% */ 1721 return; 1722 1723 /* finally, it's all reduced to the easy case */ 1724 p->strip[p->slen++] = SOP(op, opnd); 1725 } 1726 1727 /* 1728 - doinsert - insert a sop into the strip 1729 == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 1730 */ 1731 static void 1732 doinsert(struct parse *p, sop op, size_t opnd, sopno pos) 1733 { 1734 sopno sn; 1735 sop s; 1736 int i; 1737 1738 /* avoid making error situations worse */ 1739 if (p->error != 0) 1740 return; 1741 1742 sn = HERE(); 1743 EMIT(op, opnd); /* do checks, ensure space */ 1744 assert(HERE() == sn+1); 1745 s = p->strip[sn]; 1746 1747 /* adjust paren pointers */ 1748 assert(pos > 0); 1749 for (i = 1; i < NPAREN; i++) { 1750 if (p->pbegin[i] >= pos) { 1751 p->pbegin[i]++; 1752 } 1753 if (p->pend[i] >= pos) { 1754 p->pend[i]++; 1755 } 1756 } 1757 1758 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos], 1759 (HERE()-pos-1)*sizeof(sop)); 1760 p->strip[pos] = s; 1761 } 1762 1763 /* 1764 - dofwd - complete a forward reference 1765 == static void dofwd(struct parse *p, sopno pos, sop value); 1766 */ 1767 static void 1768 dofwd(struct parse *p, sopno pos, sop value) 1769 { 1770 /* avoid making error situations worse */ 1771 if (p->error != 0) 1772 return; 1773 1774 assert(value < 1<<OPSHIFT); 1775 p->strip[pos] = OP(p->strip[pos]) | value; 1776 } 1777 1778 /* 1779 - enlarge - enlarge the strip 1780 == static int enlarge(struct parse *p, sopno size); 1781 */ 1782 static int 1783 enlarge(struct parse *p, sopno size) 1784 { 1785 sop *sp; 1786 1787 if (p->ssize >= size) 1788 return 1; 1789 1790 sp = reallocarray(p->strip, size, sizeof(sop)); 1791 if (sp == NULL) { 1792 SETERROR(REG_ESPACE); 1793 return 0; 1794 } 1795 p->strip = sp; 1796 p->ssize = size; 1797 return 1; 1798 } 1799 1800 /* 1801 - stripsnug - compact the strip 1802 == static void stripsnug(struct parse *p, struct re_guts *g); 1803 */ 1804 static void 1805 stripsnug(struct parse *p, struct re_guts *g) 1806 { 1807 g->nstates = p->slen; 1808 g->strip = reallocarray((char *)p->strip, p->slen, sizeof(sop)); 1809 if (g->strip == NULL) { 1810 SETERROR(REG_ESPACE); 1811 g->strip = p->strip; 1812 } 1813 } 1814 1815 /* 1816 - findmust - fill in must and mlen with longest mandatory literal string 1817 == static void findmust(struct parse *p, struct re_guts *g); 1818 * 1819 * This algorithm could do fancy things like analyzing the operands of | 1820 * for common subsequences. Someday. This code is simple and finds most 1821 * of the interesting cases. 1822 * 1823 * Note that must and mlen got initialized during setup. 1824 */ 1825 static void 1826 findmust(struct parse *p, struct re_guts *g) 1827 { 1828 sop *scan; 1829 sop *start = NULL; 1830 sop *newstart = NULL; 1831 sopno newlen; 1832 sop s; 1833 char *cp; 1834 int offset; 1835 char buf[MB_LEN_MAX]; 1836 size_t clen; 1837 mbstate_t mbs; 1838 1839 /* avoid making error situations worse */ 1840 if (p->error != 0) 1841 return; 1842 1843 /* 1844 * It's not generally safe to do a ``char'' substring search on 1845 * multibyte character strings, but it's safe for at least 1846 * UTF-8 (see RFC 3629). 1847 */ 1848 if (MB_CUR_MAX > 1 && 1849 strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0) 1850 return; 1851 1852 /* find the longest OCHAR sequence in strip */ 1853 newlen = 0; 1854 offset = 0; 1855 g->moffset = 0; 1856 scan = g->strip + 1; 1857 do { 1858 s = *scan++; 1859 switch (OP(s)) { 1860 case OCHAR: /* sequence member */ 1861 if (newlen == 0) { /* new sequence */ 1862 memset(&mbs, 0, sizeof(mbs)); 1863 newstart = scan - 1; 1864 } 1865 clen = wcrtomb(buf, OPND(s), &mbs); 1866 if (clen == (size_t)-1) 1867 goto toohard; 1868 newlen += clen; 1869 break; 1870 case OPLUS_: /* things that don't break one */ 1871 case OLPAREN: 1872 case ORPAREN: 1873 break; 1874 case OQUEST_: /* things that must be skipped */ 1875 case OCH_: 1876 offset = altoffset(scan, offset); 1877 scan--; 1878 do { 1879 scan += OPND(s); 1880 s = *scan; 1881 /* assert() interferes w debug printouts */ 1882 if (OP(s) != (sop)O_QUEST && 1883 OP(s) != (sop)O_CH && OP(s) != (sop)OOR2) { 1884 g->iflags |= BAD; 1885 return; 1886 } 1887 } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH); 1888 /* FALLTHROUGH */ 1889 case OBOW: /* things that break a sequence */ 1890 case OEOW: 1891 case OBOL: 1892 case OEOL: 1893 case OBOS: 1894 case OEOS: 1895 case O_QUEST: 1896 case O_CH: 1897 case OEND: 1898 if (newlen > (sopno)g->mlen) { /* ends one */ 1899 start = newstart; 1900 g->mlen = newlen; 1901 if (offset > -1) { 1902 g->moffset += offset; 1903 offset = newlen; 1904 } else 1905 g->moffset = offset; 1906 } else { 1907 if (offset > -1) 1908 offset += newlen; 1909 } 1910 newlen = 0; 1911 break; 1912 case OANY: 1913 if (newlen > (sopno)g->mlen) { /* ends one */ 1914 start = newstart; 1915 g->mlen = newlen; 1916 if (offset > -1) { 1917 g->moffset += offset; 1918 offset = newlen; 1919 } else 1920 g->moffset = offset; 1921 } else { 1922 if (offset > -1) 1923 offset += newlen; 1924 } 1925 if (offset > -1) 1926 offset++; 1927 newlen = 0; 1928 break; 1929 case OANYOF: /* may or may not invalidate offset */ 1930 /* First, everything as OANY */ 1931 if (newlen > (sopno)g->mlen) { /* ends one */ 1932 start = newstart; 1933 g->mlen = newlen; 1934 if (offset > -1) { 1935 g->moffset += offset; 1936 offset = newlen; 1937 } else 1938 g->moffset = offset; 1939 } else { 1940 if (offset > -1) 1941 offset += newlen; 1942 } 1943 if (offset > -1) 1944 offset++; 1945 newlen = 0; 1946 break; 1947 toohard: 1948 default: 1949 /* Anything here makes it impossible or too hard 1950 * to calculate the offset -- so we give up; 1951 * save the last known good offset, in case the 1952 * must sequence doesn't occur later. 1953 */ 1954 if (newlen > (sopno)g->mlen) { /* ends one */ 1955 start = newstart; 1956 g->mlen = newlen; 1957 if (offset > -1) 1958 g->moffset += offset; 1959 else 1960 g->moffset = offset; 1961 } 1962 offset = -1; 1963 newlen = 0; 1964 break; 1965 } 1966 } while (OP(s) != OEND); 1967 1968 if (g->mlen == 0) { /* there isn't one */ 1969 g->moffset = -1; 1970 return; 1971 } 1972 1973 /* turn it into a character string */ 1974 g->must = malloc((size_t)g->mlen + 1); 1975 if (g->must == NULL) { /* argh; just forget it */ 1976 g->mlen = 0; 1977 g->moffset = -1; 1978 return; 1979 } 1980 cp = g->must; 1981 scan = start; 1982 memset(&mbs, 0, sizeof(mbs)); 1983 while (cp < g->must + g->mlen) { 1984 while (OP(s = *scan++) != OCHAR) 1985 continue; 1986 clen = wcrtomb(cp, OPND(s), &mbs); 1987 assert(clen != (size_t)-1); 1988 cp += clen; 1989 } 1990 assert(cp == g->must + g->mlen); 1991 *cp++ = '\0'; /* just on general principles */ 1992 } 1993 1994 /* 1995 - altoffset - choose biggest offset among multiple choices 1996 == static int altoffset(sop *scan, int offset); 1997 * 1998 * Compute, recursively if necessary, the largest offset among multiple 1999 * re paths. 2000 */ 2001 static int 2002 altoffset(sop *scan, int offset) 2003 { 2004 int largest; 2005 int try; 2006 sop s; 2007 2008 /* If we gave up already on offsets, return */ 2009 if (offset == -1) 2010 return -1; 2011 2012 largest = 0; 2013 try = 0; 2014 s = *scan++; 2015 while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH) { 2016 switch (OP(s)) { 2017 case OOR1: 2018 if (try > largest) 2019 largest = try; 2020 try = 0; 2021 break; 2022 case OQUEST_: 2023 case OCH_: 2024 try = altoffset(scan, try); 2025 if (try == -1) 2026 return -1; 2027 scan--; 2028 do { 2029 scan += OPND(s); 2030 s = *scan; 2031 if (OP(s) != (sop)O_QUEST && 2032 OP(s) != (sop)O_CH && OP(s) != (sop)OOR2) 2033 return -1; 2034 } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH); 2035 /* We must skip to the next position, or we'll 2036 * leave altoffset() too early. 2037 */ 2038 scan++; 2039 break; 2040 case OANYOF: 2041 case OCHAR: 2042 case OANY: 2043 try++; 2044 case OBOW: 2045 case OEOW: 2046 case OLPAREN: 2047 case ORPAREN: 2048 case OOR2: 2049 break; 2050 default: 2051 try = -1; 2052 break; 2053 } 2054 if (try == -1) 2055 return -1; 2056 s = *scan++; 2057 } 2058 2059 if (try > largest) 2060 largest = try; 2061 2062 return largest+offset; 2063 } 2064 2065 /* 2066 - computejumps - compute char jumps for BM scan 2067 == static void computejumps(struct parse *p, struct re_guts *g); 2068 * 2069 * This algorithm assumes g->must exists and is has size greater than 2070 * zero. It's based on the algorithm found on Computer Algorithms by 2071 * Sara Baase. 2072 * 2073 * A char jump is the number of characters one needs to jump based on 2074 * the value of the character from the text that was mismatched. 2075 */ 2076 static void 2077 computejumps(struct parse *p, struct re_guts *g) 2078 { 2079 int ch; 2080 int mindex; 2081 2082 /* Avoid making errors worse */ 2083 if (p->error != 0) 2084 return; 2085 2086 g->charjump = (int *)malloc((NC_MAX + 1) * sizeof(int)); 2087 if (g->charjump == NULL) /* Not a fatal error */ 2088 return; 2089 /* Adjust for signed chars, if necessary */ 2090 g->charjump = &g->charjump[-(CHAR_MIN)]; 2091 2092 /* If the character does not exist in the pattern, the jump 2093 * is equal to the number of characters in the pattern. 2094 */ 2095 for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++) 2096 g->charjump[ch] = g->mlen; 2097 2098 /* If the character does exist, compute the jump that would 2099 * take us to the last character in the pattern equal to it 2100 * (notice that we match right to left, so that last character 2101 * is the first one that would be matched). 2102 */ 2103 for (mindex = 0; mindex < g->mlen; mindex++) 2104 g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1; 2105 } 2106 2107 /* 2108 - computematchjumps - compute match jumps for BM scan 2109 == static void computematchjumps(struct parse *p, struct re_guts *g); 2110 * 2111 * This algorithm assumes g->must exists and is has size greater than 2112 * zero. It's based on the algorithm found on Computer Algorithms by 2113 * Sara Baase. 2114 * 2115 * A match jump is the number of characters one needs to advance based 2116 * on the already-matched suffix. 2117 * Notice that all values here are minus (g->mlen-1), because of the way 2118 * the search algorithm works. 2119 */ 2120 static void 2121 computematchjumps(struct parse *p, struct re_guts *g) 2122 { 2123 int mindex; /* General "must" iterator */ 2124 int suffix; /* Keeps track of matching suffix */ 2125 int ssuffix; /* Keeps track of suffixes' suffix */ 2126 int* pmatches; /* pmatches[k] points to the next i 2127 * such that i+1...mlen is a substring 2128 * of k+1...k+mlen-i-1 2129 */ 2130 2131 /* Avoid making errors worse */ 2132 if (p->error != 0) 2133 return; 2134 2135 pmatches = (int*) malloc(g->mlen * sizeof(int)); 2136 if (pmatches == NULL) { 2137 g->matchjump = NULL; 2138 return; 2139 } 2140 2141 g->matchjump = (int*) malloc(g->mlen * sizeof(int)); 2142 if (g->matchjump == NULL) { /* Not a fatal error */ 2143 free(pmatches); 2144 return; 2145 } 2146 2147 /* Set maximum possible jump for each character in the pattern */ 2148 for (mindex = 0; mindex < g->mlen; mindex++) 2149 g->matchjump[mindex] = 2*g->mlen - mindex - 1; 2150 2151 /* Compute pmatches[] */ 2152 for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0; 2153 mindex--, suffix--) { 2154 pmatches[mindex] = suffix; 2155 2156 /* If a mismatch is found, interrupting the substring, 2157 * compute the matchjump for that position. If no 2158 * mismatch is found, then a text substring mismatched 2159 * against the suffix will also mismatch against the 2160 * substring. 2161 */ 2162 while (suffix < g->mlen 2163 && g->must[mindex] != g->must[suffix]) { 2164 g->matchjump[suffix] = MIN(g->matchjump[suffix], 2165 g->mlen - mindex - 1); 2166 suffix = pmatches[suffix]; 2167 } 2168 } 2169 2170 /* Compute the matchjump up to the last substring found to jump 2171 * to the beginning of the largest must pattern prefix matching 2172 * it's own suffix. 2173 */ 2174 for (mindex = 0; mindex <= suffix; mindex++) 2175 g->matchjump[mindex] = MIN(g->matchjump[mindex], 2176 g->mlen + suffix - mindex); 2177 2178 ssuffix = pmatches[suffix]; 2179 while (suffix < g->mlen) { 2180 while (suffix <= ssuffix && suffix < g->mlen) { 2181 g->matchjump[suffix] = MIN(g->matchjump[suffix], 2182 g->mlen + ssuffix - suffix); 2183 suffix++; 2184 } 2185 if (suffix < g->mlen) 2186 ssuffix = pmatches[ssuffix]; 2187 } 2188 2189 free(pmatches); 2190 } 2191 2192 /* 2193 - pluscount - count + nesting 2194 == static sopno pluscount(struct parse *p, struct re_guts *g); 2195 */ 2196 static sopno /* nesting depth */ 2197 pluscount(struct parse *p, struct re_guts *g) 2198 { 2199 sop *scan; 2200 sop s; 2201 sopno plusnest = 0; 2202 sopno maxnest = 0; 2203 2204 if (p->error != 0) 2205 return(0); /* there may not be an OEND */ 2206 2207 scan = g->strip + 1; 2208 do { 2209 s = *scan++; 2210 switch (OP(s)) { 2211 case OPLUS_: 2212 plusnest++; 2213 break; 2214 case O_PLUS: 2215 if (plusnest > maxnest) 2216 maxnest = plusnest; 2217 plusnest--; 2218 break; 2219 } 2220 } while (OP(s) != OEND); 2221 if (plusnest != 0) 2222 g->iflags |= BAD; 2223 return(maxnest); 2224 } 2225