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