engine.c revision 1.8
1/* $OpenBSD: engine.c,v 1.8 2004/03/30 20:36:07 millert Exp $ */ 2 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 * This code is derived from software contributed to Berkeley by 9 * Henry Spencer. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)engine.c 8.5 (Berkeley) 3/20/94 36 */ 37 38#if defined(SNAMES) && defined(LIBC_SCCS) && !defined(lint) 39static char enginercsid[] = "$OpenBSD: engine.c,v 1.8 2004/03/30 20:36:07 millert Exp $"; 40#endif /* SNAMES and LIBC_SCCS and not lint */ 41 42/* 43 * The matching engine and friends. This file is #included by regexec.c 44 * after suitable #defines of a variety of macros used herein, so that 45 * different state representations can be used without duplicating masses 46 * of code. 47 */ 48 49#ifdef SNAMES 50#define matcher smatcher 51#define fast sfast 52#define slow sslow 53#define dissect sdissect 54#define backref sbackref 55#define step sstep 56#define print sprint 57#define at sat 58#define match smat 59#define nope snope 60#endif 61#ifdef LNAMES 62#define matcher lmatcher 63#define fast lfast 64#define slow lslow 65#define dissect ldissect 66#define backref lbackref 67#define step lstep 68#define print lprint 69#define at lat 70#define match lmat 71#define nope lnope 72#endif 73 74/* another structure passed up and down to avoid zillions of parameters */ 75struct match { 76 struct re_guts *g; 77 int eflags; 78 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */ 79 char *offp; /* offsets work from here */ 80 char *beginp; /* start of string -- virtual NUL precedes */ 81 char *endp; /* end of string -- virtual NUL here */ 82 char *coldp; /* can be no match starting before here */ 83 char **lastpos; /* [nplus+1] */ 84 STATEVARS; 85 states st; /* current states */ 86 states fresh; /* states for a fresh start */ 87 states tmp; /* temporary */ 88 states empty; /* empty set of states */ 89}; 90 91/* ========= begin header generated by ./mkh ========= */ 92#ifdef __cplusplus 93extern "C" { 94#endif 95 96/* === engine.c === */ 97static int matcher(struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags); 98static char *dissect(struct match *m, char *start, char *stop, sopno startst, sopno stopst); 99static char *backref(struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev); 100static char *fast(struct match *m, char *start, char *stop, sopno startst, sopno stopst); 101static char *slow(struct match *m, char *start, char *stop, sopno startst, sopno stopst); 102static states step(struct re_guts *g, sopno start, sopno stop, states bef, int ch, states aft); 103#define BOL (OUT+1) 104#define EOL (BOL+1) 105#define BOLEOL (BOL+2) 106#define NOTHING (BOL+3) 107#define BOW (BOL+4) 108#define EOW (BOL+5) 109#define CODEMAX (BOL+5) /* highest code used */ 110#define NONCHAR(c) ((c) > CHAR_MAX) 111#define NNONCHAR (CODEMAX-CHAR_MAX) 112#ifdef REDEBUG 113static void print(struct match *m, char *caption, states st, int ch, FILE *d); 114#endif 115#ifdef REDEBUG 116static void at(struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst); 117#endif 118#ifdef REDEBUG 119static char *pchar(int ch); 120#endif 121 122#ifdef __cplusplus 123} 124#endif 125/* ========= end header generated by ./mkh ========= */ 126 127#ifdef REDEBUG 128#define SP(t, s, c) print(m, t, s, c, stdout) 129#define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2) 130#define NOTE(str) { if (m->eflags®_TRACE) (void)printf("=%s\n", (str)); } 131static int nope = 0; 132#else 133#define SP(t, s, c) /* nothing */ 134#define AT(t, p1, p2, s1, s2) /* nothing */ 135#define NOTE(s) /* nothing */ 136#endif 137 138/* 139 - matcher - the actual matching engine 140 == static int matcher(register struct re_guts *g, char *string, \ 141 == size_t nmatch, regmatch_t pmatch[], int eflags); 142 */ 143static int /* 0 success, REG_NOMATCH failure */ 144matcher(g, string, nmatch, pmatch, eflags) 145register struct re_guts *g; 146char *string; 147size_t nmatch; 148regmatch_t pmatch[]; 149int eflags; 150{ 151 register char *endp; 152 register int i; 153 struct match mv; 154 register struct match *m = &mv; 155 register char *dp; 156 const register sopno gf = g->firststate+1; /* +1 for OEND */ 157 const register sopno gl = g->laststate; 158 char *start; 159 char *stop; 160 161 /* simplify the situation where possible */ 162 if (g->cflags®_NOSUB) 163 nmatch = 0; 164 if (eflags®_STARTEND) { 165 start = string + pmatch[0].rm_so; 166 stop = string + pmatch[0].rm_eo; 167 } else { 168 start = string; 169 stop = start + strlen(start); 170 } 171 if (stop < start) 172 return(REG_INVARG); 173 174 /* prescreening; this does wonders for this rather slow code */ 175 if (g->must != NULL) { 176 for (dp = start; dp < stop; dp++) 177 if (*dp == g->must[0] && stop - dp >= g->mlen && 178 memcmp(dp, g->must, (size_t)g->mlen) == 0) 179 break; 180 if (dp == stop) /* we didn't find g->must */ 181 return(REG_NOMATCH); 182 } 183 184 /* match struct setup */ 185 m->g = g; 186 m->eflags = eflags; 187 m->pmatch = NULL; 188 m->lastpos = NULL; 189 m->offp = string; 190 m->beginp = start; 191 m->endp = stop; 192 STATESETUP(m, 4); 193 SETUP(m->st); 194 SETUP(m->fresh); 195 SETUP(m->tmp); 196 SETUP(m->empty); 197 CLEAR(m->empty); 198 199 /* this loop does only one repetition except for backrefs */ 200 for (;;) { 201 endp = fast(m, start, stop, gf, gl); 202 if (endp == NULL) { /* a miss */ 203 STATETEARDOWN(m); 204 return(REG_NOMATCH); 205 } 206 if (nmatch == 0 && !g->backrefs) 207 break; /* no further info needed */ 208 209 /* where? */ 210 assert(m->coldp != NULL); 211 for (;;) { 212 NOTE("finding start"); 213 endp = slow(m, m->coldp, stop, gf, gl); 214 if (endp != NULL) 215 break; 216 assert(m->coldp < m->endp); 217 m->coldp++; 218 } 219 if (nmatch == 1 && !g->backrefs) 220 break; /* no further info needed */ 221 222 /* oh my, he wants the subexpressions... */ 223 if (m->pmatch == NULL) 224 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) * 225 sizeof(regmatch_t)); 226 if (m->pmatch == NULL) { 227 STATETEARDOWN(m); 228 return(REG_ESPACE); 229 } 230 for (i = 1; i <= m->g->nsub; i++) 231 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1; 232 if (!g->backrefs && !(m->eflags®_BACKR)) { 233 NOTE("dissecting"); 234 dp = dissect(m, m->coldp, endp, gf, gl); 235 } else { 236 if (g->nplus > 0 && m->lastpos == NULL) 237 m->lastpos = (char **)malloc((g->nplus+1) * 238 sizeof(char *)); 239 if (g->nplus > 0 && m->lastpos == NULL) { 240 free(m->pmatch); 241 STATETEARDOWN(m); 242 return(REG_ESPACE); 243 } 244 NOTE("backref dissect"); 245 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0); 246 } 247 if (dp != NULL) 248 break; 249 250 /* uh-oh... we couldn't find a subexpression-level match */ 251 assert(g->backrefs); /* must be back references doing it */ 252 assert(g->nplus == 0 || m->lastpos != NULL); 253 for (;;) { 254 if (dp != NULL || endp <= m->coldp) 255 break; /* defeat */ 256 NOTE("backoff"); 257 endp = slow(m, m->coldp, endp-1, gf, gl); 258 if (endp == NULL) 259 break; /* defeat */ 260 /* try it on a shorter possibility */ 261#ifndef NDEBUG 262 for (i = 1; i <= m->g->nsub; i++) { 263 assert(m->pmatch[i].rm_so == -1); 264 assert(m->pmatch[i].rm_eo == -1); 265 } 266#endif 267 NOTE("backoff dissect"); 268 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0); 269 } 270 assert(dp == NULL || dp == endp); 271 if (dp != NULL) /* found a shorter one */ 272 break; 273 274 /* despite initial appearances, there is no match here */ 275 NOTE("false alarm"); 276 start = m->coldp + 1; /* recycle starting later */ 277 assert(start <= stop); 278 } 279 280 /* fill in the details if requested */ 281 if (nmatch > 0) { 282 pmatch[0].rm_so = m->coldp - m->offp; 283 pmatch[0].rm_eo = endp - m->offp; 284 } 285 if (nmatch > 1) { 286 assert(m->pmatch != NULL); 287 for (i = 1; i < nmatch; i++) 288 if (i <= m->g->nsub) 289 pmatch[i] = m->pmatch[i]; 290 else { 291 pmatch[i].rm_so = -1; 292 pmatch[i].rm_eo = -1; 293 } 294 } 295 296 if (m->pmatch != NULL) 297 free((char *)m->pmatch); 298 if (m->lastpos != NULL) 299 free((char *)m->lastpos); 300 STATETEARDOWN(m); 301 return(0); 302} 303 304/* 305 - dissect - figure out what matched what, no back references 306 == static char *dissect(register struct match *m, char *start, \ 307 == char *stop, sopno startst, sopno stopst); 308 */ 309static char * /* == stop (success) always */ 310dissect(m, start, stop, startst, stopst) 311register struct match *m; 312char *start; 313char *stop; 314sopno startst; 315sopno stopst; 316{ 317 register int i; 318 register sopno ss; /* start sop of current subRE */ 319 register sopno es; /* end sop of current subRE */ 320 register char *sp; /* start of string matched by it */ 321 register char *stp; /* string matched by it cannot pass here */ 322 register char *rest; /* start of rest of string */ 323 register char *tail; /* string unmatched by rest of RE */ 324 register sopno ssub; /* start sop of subsubRE */ 325 register sopno esub; /* end sop of subsubRE */ 326 register char *ssp; /* start of string matched by subsubRE */ 327 register char *sep; /* end of string matched by subsubRE */ 328 register char *oldssp; /* previous ssp */ 329 register char *dp; 330 331 AT("diss", start, stop, startst, stopst); 332 sp = start; 333 for (ss = startst; ss < stopst; ss = es) { 334 /* identify end of subRE */ 335 es = ss; 336 switch (OP(m->g->strip[es])) { 337 case OPLUS_: 338 case OQUEST_: 339 es += OPND(m->g->strip[es]); 340 break; 341 case OCH_: 342 while (OP(m->g->strip[es]) != O_CH) 343 es += OPND(m->g->strip[es]); 344 break; 345 } 346 es++; 347 348 /* figure out what it matched */ 349 switch (OP(m->g->strip[ss])) { 350 case OEND: 351 assert(nope); 352 break; 353 case OCHAR: 354 sp++; 355 break; 356 case OBOL: 357 case OEOL: 358 case OBOW: 359 case OEOW: 360 break; 361 case OANY: 362 case OANYOF: 363 sp++; 364 break; 365 case OBACK_: 366 case O_BACK: 367 assert(nope); 368 break; 369 /* cases where length of match is hard to find */ 370 case OQUEST_: 371 stp = stop; 372 for (;;) { 373 /* how long could this one be? */ 374 rest = slow(m, sp, stp, ss, es); 375 assert(rest != NULL); /* it did match */ 376 /* could the rest match the rest? */ 377 tail = slow(m, rest, stop, es, stopst); 378 if (tail == stop) 379 break; /* yes! */ 380 /* no -- try a shorter match for this one */ 381 stp = rest - 1; 382 assert(stp >= sp); /* it did work */ 383 } 384 ssub = ss + 1; 385 esub = es - 1; 386 /* did innards match? */ 387 if (slow(m, sp, rest, ssub, esub) != NULL) { 388 dp = dissect(m, sp, rest, ssub, esub); 389 assert(dp == rest); 390 } else /* no */ 391 assert(sp == rest); 392 sp = rest; 393 break; 394 case OPLUS_: 395 stp = stop; 396 for (;;) { 397 /* how long could this one be? */ 398 rest = slow(m, sp, stp, ss, es); 399 assert(rest != NULL); /* it did match */ 400 /* could the rest match the rest? */ 401 tail = slow(m, rest, stop, es, stopst); 402 if (tail == stop) 403 break; /* yes! */ 404 /* no -- try a shorter match for this one */ 405 stp = rest - 1; 406 assert(stp >= sp); /* it did work */ 407 } 408 ssub = ss + 1; 409 esub = es - 1; 410 ssp = sp; 411 oldssp = ssp; 412 for (;;) { /* find last match of innards */ 413 sep = slow(m, ssp, rest, ssub, esub); 414 if (sep == NULL || sep == ssp) 415 break; /* failed or matched null */ 416 oldssp = ssp; /* on to next try */ 417 ssp = sep; 418 } 419 if (sep == NULL) { 420 /* last successful match */ 421 sep = ssp; 422 ssp = oldssp; 423 } 424 assert(sep == rest); /* must exhaust substring */ 425 assert(slow(m, ssp, sep, ssub, esub) == rest); 426 dp = dissect(m, ssp, sep, ssub, esub); 427 assert(dp == sep); 428 sp = rest; 429 break; 430 case OCH_: 431 stp = stop; 432 for (;;) { 433 /* how long could this one be? */ 434 rest = slow(m, sp, stp, ss, es); 435 assert(rest != NULL); /* it did match */ 436 /* could the rest match the rest? */ 437 tail = slow(m, rest, stop, es, stopst); 438 if (tail == stop) 439 break; /* yes! */ 440 /* no -- try a shorter match for this one */ 441 stp = rest - 1; 442 assert(stp >= sp); /* it did work */ 443 } 444 ssub = ss + 1; 445 esub = ss + OPND(m->g->strip[ss]) - 1; 446 assert(OP(m->g->strip[esub]) == OOR1); 447 for (;;) { /* find first matching branch */ 448 if (slow(m, sp, rest, ssub, esub) == rest) 449 break; /* it matched all of it */ 450 /* that one missed, try next one */ 451 assert(OP(m->g->strip[esub]) == OOR1); 452 esub++; 453 assert(OP(m->g->strip[esub]) == OOR2); 454 ssub = esub + 1; 455 esub += OPND(m->g->strip[esub]); 456 if (OP(m->g->strip[esub]) == OOR2) 457 esub--; 458 else 459 assert(OP(m->g->strip[esub]) == O_CH); 460 } 461 dp = dissect(m, sp, rest, ssub, esub); 462 assert(dp == rest); 463 sp = rest; 464 break; 465 case O_PLUS: 466 case O_QUEST: 467 case OOR1: 468 case OOR2: 469 case O_CH: 470 assert(nope); 471 break; 472 case OLPAREN: 473 i = OPND(m->g->strip[ss]); 474 assert(0 < i && i <= m->g->nsub); 475 m->pmatch[i].rm_so = sp - m->offp; 476 break; 477 case ORPAREN: 478 i = OPND(m->g->strip[ss]); 479 assert(0 < i && i <= m->g->nsub); 480 m->pmatch[i].rm_eo = sp - m->offp; 481 break; 482 default: /* uh oh */ 483 assert(nope); 484 break; 485 } 486 } 487 488 assert(sp == stop); 489 return(sp); 490} 491 492/* 493 - backref - figure out what matched what, figuring in back references 494 == static char *backref(register struct match *m, char *start, \ 495 == char *stop, sopno startst, sopno stopst, sopno lev); 496 */ 497static char * /* == stop (success) or NULL (failure) */ 498backref(m, start, stop, startst, stopst, lev) 499register struct match *m; 500char *start; 501char *stop; 502sopno startst; 503sopno stopst; 504sopno lev; /* PLUS nesting level */ 505{ 506 register int i; 507 register sopno ss; /* start sop of current subRE */ 508 register char *sp; /* start of string matched by it */ 509 register sopno ssub; /* start sop of subsubRE */ 510 register sopno esub; /* end sop of subsubRE */ 511 register char *ssp; /* start of string matched by subsubRE */ 512 register char *dp; 513 register size_t len; 514 register int hard; 515 register sop s; 516 register regoff_t offsave; 517 register cset *cs; 518 519 AT("back", start, stop, startst, stopst); 520 sp = start; 521 522 /* get as far as we can with easy stuff */ 523 hard = 0; 524 for (ss = startst; !hard && ss < stopst; ss++) 525 switch (OP(s = m->g->strip[ss])) { 526 case OCHAR: 527 if (sp == stop || *sp++ != (char)OPND(s)) 528 return(NULL); 529 break; 530 case OANY: 531 if (sp == stop) 532 return(NULL); 533 sp++; 534 break; 535 case OANYOF: 536 cs = &m->g->sets[OPND(s)]; 537 if (sp == stop || !CHIN(cs, *sp++)) 538 return(NULL); 539 break; 540 case OBOL: 541 if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) || 542 (sp < m->endp && *(sp-1) == '\n' && 543 (m->g->cflags®_NEWLINE)) ) 544 { /* yes */ } 545 else 546 return(NULL); 547 break; 548 case OEOL: 549 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) || 550 (sp < m->endp && *sp == '\n' && 551 (m->g->cflags®_NEWLINE)) ) 552 { /* yes */ } 553 else 554 return(NULL); 555 break; 556 case OBOW: 557 if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) || 558 (sp < m->endp && *(sp-1) == '\n' && 559 (m->g->cflags®_NEWLINE)) || 560 (sp > m->beginp && 561 !ISWORD(*(sp-1))) ) && 562 (sp < m->endp && ISWORD(*sp)) ) 563 { /* yes */ } 564 else 565 return(NULL); 566 break; 567 case OEOW: 568 if (( (sp == m->endp && !(m->eflags®_NOTEOL)) || 569 (sp < m->endp && *sp == '\n' && 570 (m->g->cflags®_NEWLINE)) || 571 (sp < m->endp && !ISWORD(*sp)) ) && 572 (sp > m->beginp && ISWORD(*(sp-1))) ) 573 { /* yes */ } 574 else 575 return(NULL); 576 break; 577 case O_QUEST: 578 break; 579 case OOR1: /* matches null but needs to skip */ 580 ss++; 581 s = m->g->strip[ss]; 582 do { 583 assert(OP(s) == OOR2); 584 ss += OPND(s); 585 } while (OP(s = m->g->strip[ss]) != O_CH); 586 /* note that the ss++ gets us past the O_CH */ 587 break; 588 default: /* have to make a choice */ 589 hard = 1; 590 break; 591 } 592 if (!hard) { /* that was it! */ 593 if (sp != stop) 594 return(NULL); 595 return(sp); 596 } 597 ss--; /* adjust for the for's final increment */ 598 599 /* the hard stuff */ 600 AT("hard", sp, stop, ss, stopst); 601 s = m->g->strip[ss]; 602 switch (OP(s)) { 603 case OBACK_: /* the vilest depths */ 604 i = OPND(s); 605 assert(0 < i && i <= m->g->nsub); 606 if (m->pmatch[i].rm_eo == -1) 607 return(NULL); 608 assert(m->pmatch[i].rm_so != -1); 609 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so; 610 assert(stop - m->beginp >= len); 611 if (sp > stop - len) 612 return(NULL); /* not enough left to match */ 613 ssp = m->offp + m->pmatch[i].rm_so; 614 if (memcmp(sp, ssp, len) != 0) 615 return(NULL); 616 while (m->g->strip[ss] != SOP(O_BACK, i)) 617 ss++; 618 return(backref(m, sp+len, stop, ss+1, stopst, lev)); 619 break; 620 case OQUEST_: /* to null or not */ 621 dp = backref(m, sp, stop, ss+1, stopst, lev); 622 if (dp != NULL) 623 return(dp); /* not */ 624 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev)); 625 break; 626 case OPLUS_: 627 assert(m->lastpos != NULL); 628 assert(lev+1 <= m->g->nplus); 629 m->lastpos[lev+1] = sp; 630 return(backref(m, sp, stop, ss+1, stopst, lev+1)); 631 break; 632 case O_PLUS: 633 if (sp == m->lastpos[lev]) /* last pass matched null */ 634 return(backref(m, sp, stop, ss+1, stopst, lev-1)); 635 /* try another pass */ 636 m->lastpos[lev] = sp; 637 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev); 638 if (dp == NULL) 639 return(backref(m, sp, stop, ss+1, stopst, lev-1)); 640 else 641 return(dp); 642 break; 643 case OCH_: /* find the right one, if any */ 644 ssub = ss + 1; 645 esub = ss + OPND(s) - 1; 646 assert(OP(m->g->strip[esub]) == OOR1); 647 for (;;) { /* find first matching branch */ 648 dp = backref(m, sp, stop, ssub, esub, lev); 649 if (dp != NULL) 650 return(dp); 651 /* that one missed, try next one */ 652 if (OP(m->g->strip[esub]) == O_CH) 653 return(NULL); /* there is none */ 654 esub++; 655 assert(OP(m->g->strip[esub]) == OOR2); 656 ssub = esub + 1; 657 esub += OPND(m->g->strip[esub]); 658 if (OP(m->g->strip[esub]) == OOR2) 659 esub--; 660 else 661 assert(OP(m->g->strip[esub]) == O_CH); 662 } 663 break; 664 case OLPAREN: /* must undo assignment if rest fails */ 665 i = OPND(s); 666 assert(0 < i && i <= m->g->nsub); 667 offsave = m->pmatch[i].rm_so; 668 m->pmatch[i].rm_so = sp - m->offp; 669 dp = backref(m, sp, stop, ss+1, stopst, lev); 670 if (dp != NULL) 671 return(dp); 672 m->pmatch[i].rm_so = offsave; 673 return(NULL); 674 break; 675 case ORPAREN: /* must undo assignment if rest fails */ 676 i = OPND(s); 677 assert(0 < i && i <= m->g->nsub); 678 offsave = m->pmatch[i].rm_eo; 679 m->pmatch[i].rm_eo = sp - m->offp; 680 dp = backref(m, sp, stop, ss+1, stopst, lev); 681 if (dp != NULL) 682 return(dp); 683 m->pmatch[i].rm_eo = offsave; 684 return(NULL); 685 break; 686 default: /* uh oh */ 687 assert(nope); 688 break; 689 } 690 691 /* "can't happen" */ 692 assert(nope); 693 /* NOTREACHED */ 694} 695 696/* 697 - fast - step through the string at top speed 698 == static char *fast(register struct match *m, char *start, \ 699 == char *stop, sopno startst, sopno stopst); 700 */ 701static char * /* where tentative match ended, or NULL */ 702fast(m, start, stop, startst, stopst) 703register struct match *m; 704char *start; 705char *stop; 706sopno startst; 707sopno stopst; 708{ 709 register states st = m->st; 710 register states fresh = m->fresh; 711 register states tmp = m->tmp; 712 register char *p = start; 713 register int c = (start == m->beginp) ? OUT : *(start-1); 714 register int lastc; /* previous c */ 715 register int flagch; 716 register int i; 717 register char *coldp; /* last p after which no match was underway */ 718 719 CLEAR(st); 720 SET1(st, startst); 721 st = step(m->g, startst, stopst, st, NOTHING, st); 722 ASSIGN(fresh, st); 723 SP("start", st, *p); 724 coldp = NULL; 725 for (;;) { 726 /* next character */ 727 lastc = c; 728 c = (p == m->endp) ? OUT : *p; 729 if (EQ(st, fresh)) 730 coldp = p; 731 732 /* is there an EOL and/or BOL between lastc and c? */ 733 flagch = '\0'; 734 i = 0; 735 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || 736 (lastc == OUT && !(m->eflags®_NOTBOL)) ) { 737 flagch = BOL; 738 i = m->g->nbol; 739 } 740 if ( (c == '\n' && m->g->cflags®_NEWLINE) || 741 (c == OUT && !(m->eflags®_NOTEOL)) ) { 742 flagch = (flagch == BOL) ? BOLEOL : EOL; 743 i += m->g->neol; 744 } 745 if (i != 0) { 746 for (; i > 0; i--) 747 st = step(m->g, startst, stopst, st, flagch, st); 748 SP("boleol", st, c); 749 } 750 751 /* how about a word boundary? */ 752 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) && 753 (c != OUT && ISWORD(c)) ) { 754 flagch = BOW; 755 } 756 if ( (lastc != OUT && ISWORD(lastc)) && 757 (flagch == EOL || (c != OUT && !ISWORD(c))) ) { 758 flagch = EOW; 759 } 760 if (flagch == BOW || flagch == EOW) { 761 st = step(m->g, startst, stopst, st, flagch, st); 762 SP("boweow", st, c); 763 } 764 765 /* are we done? */ 766 if (ISSET(st, stopst) || p == stop) 767 break; /* NOTE BREAK OUT */ 768 769 /* no, we must deal with this character */ 770 ASSIGN(tmp, st); 771 ASSIGN(st, fresh); 772 assert(c != OUT); 773 st = step(m->g, startst, stopst, tmp, c, st); 774 SP("aft", st, c); 775 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); 776 p++; 777 } 778 779 assert(coldp != NULL); 780 m->coldp = coldp; 781 if (ISSET(st, stopst)) 782 return(p+1); 783 else 784 return(NULL); 785} 786 787/* 788 - slow - step through the string more deliberately 789 == static char *slow(register struct match *m, char *start, \ 790 == char *stop, sopno startst, sopno stopst); 791 */ 792static char * /* where it ended */ 793slow(m, start, stop, startst, stopst) 794register struct match *m; 795char *start; 796char *stop; 797sopno startst; 798sopno stopst; 799{ 800 register states st = m->st; 801 register states empty = m->empty; 802 register states tmp = m->tmp; 803 register char *p = start; 804 register int c = (start == m->beginp) ? OUT : *(start-1); 805 register int lastc; /* previous c */ 806 register int flagch; 807 register int i; 808 register char *matchp; /* last p at which a match ended */ 809 810 AT("slow", start, stop, startst, stopst); 811 CLEAR(st); 812 SET1(st, startst); 813 SP("sstart", st, *p); 814 st = step(m->g, startst, stopst, st, NOTHING, st); 815 matchp = NULL; 816 for (;;) { 817 /* next character */ 818 lastc = c; 819 c = (p == m->endp) ? OUT : *p; 820 821 /* is there an EOL and/or BOL between lastc and c? */ 822 flagch = '\0'; 823 i = 0; 824 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) || 825 (lastc == OUT && !(m->eflags®_NOTBOL)) ) { 826 flagch = BOL; 827 i = m->g->nbol; 828 } 829 if ( (c == '\n' && m->g->cflags®_NEWLINE) || 830 (c == OUT && !(m->eflags®_NOTEOL)) ) { 831 flagch = (flagch == BOL) ? BOLEOL : EOL; 832 i += m->g->neol; 833 } 834 if (i != 0) { 835 for (; i > 0; i--) 836 st = step(m->g, startst, stopst, st, flagch, st); 837 SP("sboleol", st, c); 838 } 839 840 /* how about a word boundary? */ 841 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) && 842 (c != OUT && ISWORD(c)) ) { 843 flagch = BOW; 844 } 845 if ( (lastc != OUT && ISWORD(lastc)) && 846 (flagch == EOL || (c != OUT && !ISWORD(c))) ) { 847 flagch = EOW; 848 } 849 if (flagch == BOW || flagch == EOW) { 850 st = step(m->g, startst, stopst, st, flagch, st); 851 SP("sboweow", st, c); 852 } 853 854 /* are we done? */ 855 if (ISSET(st, stopst)) 856 matchp = p; 857 if (EQ(st, empty) || p == stop) 858 break; /* NOTE BREAK OUT */ 859 860 /* no, we must deal with this character */ 861 ASSIGN(tmp, st); 862 ASSIGN(st, empty); 863 assert(c != OUT); 864 st = step(m->g, startst, stopst, tmp, c, st); 865 SP("saft", st, c); 866 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st)); 867 p++; 868 } 869 870 return(matchp); 871} 872 873 874/* 875 - step - map set of states reachable before char to set reachable after 876 == static states step(register struct re_guts *g, sopno start, sopno stop, \ 877 == register states bef, int ch, register states aft); 878 == #define BOL (OUT+1) 879 == #define EOL (BOL+1) 880 == #define BOLEOL (BOL+2) 881 == #define NOTHING (BOL+3) 882 == #define BOW (BOL+4) 883 == #define EOW (BOL+5) 884 == #define CODEMAX (BOL+5) // highest code used 885 == #define NONCHAR(c) ((c) > CHAR_MAX) 886 == #define NNONCHAR (CODEMAX-CHAR_MAX) 887 */ 888static states 889step(g, start, stop, bef, ch, aft) 890register struct re_guts *g; 891sopno start; /* start state within strip */ 892sopno stop; /* state after stop state within strip */ 893register states bef; /* states reachable before */ 894int ch; /* character or NONCHAR code */ 895register states aft; /* states already known reachable after */ 896{ 897 register cset *cs; 898 register sop s; 899 register sopno pc; 900 register onestate here; /* note, macros know this name */ 901 register sopno look; 902 register int i; 903 904 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) { 905 s = g->strip[pc]; 906 switch (OP(s)) { 907 case OEND: 908 assert(pc == stop-1); 909 break; 910 case OCHAR: 911 /* only characters can match */ 912 assert(!NONCHAR(ch) || ch != (char)OPND(s)); 913 if (ch == (char)OPND(s)) 914 FWD(aft, bef, 1); 915 break; 916 case OBOL: 917 if (ch == BOL || ch == BOLEOL) 918 FWD(aft, bef, 1); 919 break; 920 case OEOL: 921 if (ch == EOL || ch == BOLEOL) 922 FWD(aft, bef, 1); 923 break; 924 case OBOW: 925 if (ch == BOW) 926 FWD(aft, bef, 1); 927 break; 928 case OEOW: 929 if (ch == EOW) 930 FWD(aft, bef, 1); 931 break; 932 case OANY: 933 if (!NONCHAR(ch)) 934 FWD(aft, bef, 1); 935 break; 936 case OANYOF: 937 cs = &g->sets[OPND(s)]; 938 if (!NONCHAR(ch) && CHIN(cs, ch)) 939 FWD(aft, bef, 1); 940 break; 941 case OBACK_: /* ignored here */ 942 case O_BACK: 943 FWD(aft, aft, 1); 944 break; 945 case OPLUS_: /* forward, this is just an empty */ 946 FWD(aft, aft, 1); 947 break; 948 case O_PLUS: /* both forward and back */ 949 FWD(aft, aft, 1); 950 i = ISSETBACK(aft, OPND(s)); 951 BACK(aft, aft, OPND(s)); 952 if (!i && ISSETBACK(aft, OPND(s))) { 953 /* oho, must reconsider loop body */ 954 pc -= OPND(s) + 1; 955 INIT(here, pc); 956 } 957 break; 958 case OQUEST_: /* two branches, both forward */ 959 FWD(aft, aft, 1); 960 FWD(aft, aft, OPND(s)); 961 break; 962 case O_QUEST: /* just an empty */ 963 FWD(aft, aft, 1); 964 break; 965 case OLPAREN: /* not significant here */ 966 case ORPAREN: 967 FWD(aft, aft, 1); 968 break; 969 case OCH_: /* mark the first two branches */ 970 FWD(aft, aft, 1); 971 assert(OP(g->strip[pc+OPND(s)]) == OOR2); 972 FWD(aft, aft, OPND(s)); 973 break; 974 case OOR1: /* done a branch, find the O_CH */ 975 if (ISSTATEIN(aft, here)) { 976 for (look = 1; 977 OP(s = g->strip[pc+look]) != O_CH; 978 look += OPND(s)) 979 assert(OP(s) == OOR2); 980 FWD(aft, aft, look); 981 } 982 break; 983 case OOR2: /* propagate OCH_'s marking */ 984 FWD(aft, aft, 1); 985 if (OP(g->strip[pc+OPND(s)]) != O_CH) { 986 assert(OP(g->strip[pc+OPND(s)]) == OOR2); 987 FWD(aft, aft, OPND(s)); 988 } 989 break; 990 case O_CH: /* just empty */ 991 FWD(aft, aft, 1); 992 break; 993 default: /* ooooops... */ 994 assert(nope); 995 break; 996 } 997 } 998 999 return(aft); 1000} 1001 1002#ifdef REDEBUG 1003/* 1004 - print - print a set of states 1005 == #ifdef REDEBUG 1006 == static void print(struct match *m, char *caption, states st, \ 1007 == int ch, FILE *d); 1008 == #endif 1009 */ 1010static void 1011print(m, caption, st, ch, d) 1012struct match *m; 1013char *caption; 1014states st; 1015int ch; 1016FILE *d; 1017{ 1018 register struct re_guts *g = m->g; 1019 register int i; 1020 register int first = 1; 1021 1022 if (!(m->eflags®_TRACE)) 1023 return; 1024 1025 (void)fprintf(d, "%s", caption); 1026 if (ch != '\0') 1027 (void)fprintf(d, " %s", pchar(ch)); 1028 for (i = 0; i < g->nstates; i++) 1029 if (ISSET(st, i)) { 1030 (void)fprintf(d, "%s%d", (first) ? "\t" : ", ", i); 1031 first = 0; 1032 } 1033 (void)fprintf(d, "\n"); 1034} 1035 1036/* 1037 - at - print current situation 1038 == #ifdef REDEBUG 1039 == static void at(struct match *m, char *title, char *start, char *stop, \ 1040 == sopno startst, sopno stopst); 1041 == #endif 1042 */ 1043static void 1044at(m, title, start, stop, startst, stopst) 1045struct match *m; 1046char *title; 1047char *start; 1048char *stop; 1049sopno startst; 1050sopno stopst; 1051{ 1052 if (!(m->eflags®_TRACE)) 1053 return; 1054 1055 (void)printf("%s %s-", title, pchar(*start)); 1056 (void)printf("%s ", pchar(*stop)); 1057 (void)printf("%ld-%ld\n", (long)startst, (long)stopst); 1058} 1059 1060#ifndef PCHARDONE 1061#define PCHARDONE /* never again */ 1062/* 1063 - pchar - make a character printable 1064 == #ifdef REDEBUG 1065 == static char *pchar(int ch); 1066 == #endif 1067 * 1068 * Is this identical to regchar() over in debug.c? Well, yes. But a 1069 * duplicate here avoids having a debugging-capable regexec.o tied to 1070 * a matching debug.o, and this is convenient. It all disappears in 1071 * the non-debug compilation anyway, so it doesn't matter much. 1072 */ 1073static char * /* -> representation */ 1074pchar(ch) 1075int ch; 1076{ 1077 static char pbuf[10]; 1078 1079 if (isprint(ch) || ch == ' ') 1080 (void)snprintf(pbuf, sizeof pbuf, "%c", ch); 1081 else 1082 (void)snprintf(pbuf, sizeof pbuf, "\\%o", ch); 1083 return(pbuf); 1084} 1085#endif 1086#endif 1087 1088#undef matcher 1089#undef fast 1090#undef slow 1091#undef dissect 1092#undef backref 1093#undef step 1094#undef print 1095#undef at 1096#undef match 1097#undef nope 1098