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.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 * 3. 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.
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
33 * $FreeBSD: head/lib/libc/regex/engine.c 279104 2015-02-21 15:02:27Z pfg $
35 * @(#)engine.c 8.5 (Berkeley) 3/20/94
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
47 #define matcher smatcher
50 #define dissect sdissect
51 #define backref sbackref
58 #define matcher lmatcher
61 #define dissect ldissect
62 #define backref lbackref
69 #define matcher mmatcher
72 #define dissect mdissect
73 #define backref mbackref
80 /* another structure passed up and down to avoid zillions of parameters */
84 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
85 const char *offp; /* offsets work from here */
86 const char *beginp; /* start of string -- virtual NUL precedes */
87 const char *endp; /* end of string -- virtual NUL here */
88 const char *coldp; /* can be no match starting before here */
89 const char **lastpos; /* [nplus+1] */
91 states st; /* current states */
92 states fresh; /* states for a fresh start */
93 states tmp; /* temporary */
94 states empty; /* empty set of states */
95 mbstate_t mbs; /* multibyte conversion state */
98 /* ========= begin header generated by ./mkh ========= */
103 /* === engine.c === */
104 static int matcher(struct re_guts *g, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
105 static const char *dissect(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
106 static const char *backref(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst, sopno lev, int);
107 static const char *fast(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
108 static const char *slow(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
109 static states step(struct re_guts *g, sopno start, sopno stop, states bef, wint_t ch, states aft);
110 #define MAX_RECURSION 100
113 #define BOLEOL (BOL-2)
114 #define NOTHING (BOL-3)
117 #define BADCHAR (BOL-6)
118 #define NONCHAR(c) ((c) <= OUT)
120 static void print(struct match *m, const char *caption, states st, int ch, FILE *d);
123 static void at(struct match *m, const char *title, const char *start, const char *stop, sopno startst, sopno stopst);
126 static const char *pchar(int ch);
132 /* ========= end header generated by ./mkh ========= */
135 #define SP(t, s, c) print(m, t, s, c, stdout)
136 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
137 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
139 #define SP(t, s, c) /* nothing */
140 #define AT(t, p1, p2, s1, s2) /* nothing */
141 #define NOTE(s) /* nothing */
145 - matcher - the actual matching engine
146 == static int matcher(struct re_guts *g, const char *string, \
147 == size_t nmatch, regmatch_t pmatch[], int eflags);
149 static int /* 0 success, REG_NOMATCH failure */
150 matcher(struct re_guts *g,
159 struct match *m = &mv;
160 const char *dp = NULL;
161 const sopno gf = g->firststate+1; /* +1 for OEND */
162 const sopno gl = g->laststate;
165 /* Boyer-Moore algorithms variables */
168 const char *mustfirst;
169 const char *mustlast;
173 /* simplify the situation where possible */
174 if (g->cflags®_NOSUB)
176 if (eflags®_STARTEND) {
177 start = string + pmatch[0].rm_so;
178 stop = string + pmatch[0].rm_eo;
181 stop = start + strlen(start);
186 /* prescreening; this does wonders for this rather slow code */
187 if (g->must != NULL) {
188 if (g->charjump != NULL && g->matchjump != NULL) {
190 mustlast = g->must + g->mlen - 1;
191 charjump = g->charjump;
192 matchjump = g->matchjump;
194 for (dp = start+g->mlen-1; dp < stop;) {
195 /* Fast skip non-matches */
196 while (dp < stop && charjump[(int)*dp])
197 dp += charjump[(int)*dp];
203 /* We depend on not being used for
204 * for strings of length 1
206 while (*--dp == *--pp && pp != mustfirst);
211 /* Jump to next possible match */
212 mj = matchjump[pp - mustfirst];
213 cj = charjump[(int)*dp];
214 dp += (cj < mj ? mj : cj);
220 for (dp = start; dp < stop; dp++)
221 if (*dp == g->must[0] &&
222 stop - dp >= g->mlen &&
223 memcmp(dp, g->must, (size_t)g->mlen) == 0)
225 if (dp == stop) /* we didn't find g->must */
230 /* match struct setup */
246 /* Adjust start according to moffset, to speed things up */
247 if (dp != NULL && g->moffset > -1)
248 start = ((dp - g->moffset) < start) ? start : dp - g->moffset;
250 SP("mloop", m->st, *start);
252 /* this loop does only one repetition except for backrefs */
254 endp = fast(m, start, stop, gf, gl);
255 if (endp == NULL) { /* a miss */
256 if (m->pmatch != NULL)
257 free((char *)m->pmatch);
258 if (m->lastpos != NULL)
259 free((char *)m->lastpos);
263 if (nmatch == 0 && !g->backrefs)
264 break; /* no further info needed */
267 assert(m->coldp != NULL);
269 NOTE("finding start");
270 endp = slow(m, m->coldp, stop, gf, gl);
273 assert(m->coldp < m->endp);
274 m->coldp += XMBRTOWC(NULL, m->coldp,
275 m->endp - m->coldp, &m->mbs, 0);
277 if (nmatch == 1 && !g->backrefs)
278 break; /* no further info needed */
280 /* oh my, he wants the subexpressions... */
281 if (m->pmatch == NULL)
282 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
284 if (m->pmatch == NULL) {
288 for (i = 1; i <= m->g->nsub; i++)
289 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
290 if (!g->backrefs && !(m->eflags®_BACKR)) {
292 dp = dissect(m, m->coldp, endp, gf, gl);
294 if (g->nplus > 0 && m->lastpos == NULL)
295 m->lastpos = malloc((g->nplus+1) *
296 sizeof(const char *));
297 if (g->nplus > 0 && m->lastpos == NULL) {
302 NOTE("backref dissect");
303 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
308 /* uh-oh... we couldn't find a subexpression-level match */
309 assert(g->backrefs); /* must be back references doing it */
310 assert(g->nplus == 0 || m->lastpos != NULL);
312 if (dp != NULL || endp <= m->coldp)
315 endp = slow(m, m->coldp, endp-1, gf, gl);
318 /* try it on a shorter possibility */
320 for (i = 1; i <= m->g->nsub; i++) {
321 assert(m->pmatch[i].rm_so == -1);
322 assert(m->pmatch[i].rm_eo == -1);
325 NOTE("backoff dissect");
326 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
328 assert(dp == NULL || dp == endp);
329 if (dp != NULL) /* found a shorter one */
332 /* despite initial appearances, there is no match here */
334 /* recycle starting later */
335 start = m->coldp + XMBRTOWC(NULL, m->coldp,
336 stop - m->coldp, &m->mbs, 0);
337 assert(start <= stop);
340 /* fill in the details if requested */
342 pmatch[0].rm_so = m->coldp - m->offp;
343 pmatch[0].rm_eo = endp - m->offp;
346 assert(m->pmatch != NULL);
347 for (i = 1; i < nmatch; i++)
349 pmatch[i] = m->pmatch[i];
351 pmatch[i].rm_so = -1;
352 pmatch[i].rm_eo = -1;
356 if (m->pmatch != NULL)
357 free((char *)m->pmatch);
358 if (m->lastpos != NULL)
359 free((char *)m->lastpos);
365 - dissect - figure out what matched what, no back references
366 == static const char *dissect(struct match *m, const char *start, \
367 == const char *stop, sopno startst, sopno stopst);
369 static const char * /* == stop (success) always */
370 dissect(struct match *m,
377 sopno ss; /* start sop of current subRE */
378 sopno es; /* end sop of current subRE */
379 const char *sp; /* start of string matched by it */
380 const char *stp; /* string matched by it cannot pass here */
381 const char *rest; /* start of rest of string */
382 const char *tail; /* string unmatched by rest of RE */
383 sopno ssub; /* start sop of subsubRE */
384 sopno esub; /* end sop of subsubRE */
385 const char *ssp; /* start of string matched by subsubRE */
386 const char *sep; /* end of string matched by subsubRE */
387 const char *oldssp; /* previous ssp */
388 const char *dp __unused; /* actually used for assert checks */
390 AT("diss", start, stop, startst, stopst);
392 for (ss = startst; ss < stopst; ss = es) {
393 /* identify end of subRE */
395 switch (OP(m->g->strip[es])) {
398 es += OPND(m->g->strip[es]);
401 while (OP(m->g->strip[es]) != O_CH)
402 es += OPND(m->g->strip[es]);
407 /* figure out what it matched */
408 switch (OP(m->g->strip[ss])) {
413 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0);
422 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0);
428 /* cases where length of match is hard to find */
432 /* how long could this one be? */
433 rest = slow(m, sp, stp, ss, es);
434 assert(rest != NULL); /* it did match */
435 /* could the rest match the rest? */
436 tail = slow(m, rest, stop, es, stopst);
439 /* no -- try a shorter match for this one */
441 assert(stp >= sp); /* it did work */
445 /* did innards match? */
446 if (slow(m, sp, rest, ssub, esub) != NULL) {
447 dp = dissect(m, sp, rest, ssub, esub);
456 /* how long could this one be? */
457 rest = slow(m, sp, stp, ss, es);
458 assert(rest != NULL); /* it did match */
459 /* could the rest match the rest? */
460 tail = slow(m, rest, stop, es, stopst);
463 /* no -- try a shorter match for this one */
465 assert(stp >= sp); /* it did work */
471 for (;;) { /* find last match of innards */
472 sep = slow(m, ssp, rest, ssub, esub);
473 if (sep == NULL || sep == ssp)
474 break; /* failed or matched null */
475 oldssp = ssp; /* on to next try */
479 /* last successful match */
483 assert(sep == rest); /* must exhaust substring */
484 assert(slow(m, ssp, sep, ssub, esub) == rest);
485 dp = dissect(m, ssp, sep, ssub, esub);
492 /* how long could this one be? */
493 rest = slow(m, sp, stp, ss, es);
494 assert(rest != NULL); /* it did match */
495 /* could the rest match the rest? */
496 tail = slow(m, rest, stop, es, stopst);
499 /* no -- try a shorter match for this one */
501 assert(stp >= sp); /* it did work */
504 esub = ss + OPND(m->g->strip[ss]) - 1;
505 assert(OP(m->g->strip[esub]) == OOR1);
506 for (;;) { /* find first matching branch */
507 if (slow(m, sp, rest, ssub, esub) == rest)
508 break; /* it matched all of it */
509 /* that one missed, try next one */
510 assert(OP(m->g->strip[esub]) == OOR1);
512 assert(OP(m->g->strip[esub]) == OOR2);
514 esub += OPND(m->g->strip[esub]);
515 if (OP(m->g->strip[esub]) == OOR2)
518 assert(OP(m->g->strip[esub]) == O_CH);
520 dp = dissect(m, sp, rest, ssub, esub);
532 i = OPND(m->g->strip[ss]);
533 assert(0 < i && i <= m->g->nsub);
534 m->pmatch[i].rm_so = sp - m->offp;
537 i = OPND(m->g->strip[ss]);
538 assert(0 < i && i <= m->g->nsub);
539 m->pmatch[i].rm_eo = sp - m->offp;
552 - backref - figure out what matched what, figuring in back references
553 == static const char *backref(struct match *m, const char *start, \
554 == const char *stop, sopno startst, sopno stopst, sopno lev);
556 static const char * /* == stop (success) or NULL (failure) */
557 backref(struct match *m,
562 sopno lev, /* PLUS nesting level */
566 sopno ss; /* start sop of current subRE */
567 const char *sp; /* start of string matched by it */
568 sopno ssub; /* start sop of subsubRE */
569 sopno esub; /* end sop of subsubRE */
570 const char *ssp; /* start of string matched by subsubRE */
579 AT("back", start, stop, startst, stopst);
582 /* get as far as we can with easy stuff */
584 for (ss = startst; !hard && ss < stopst; ss++)
585 switch (OP(s = m->g->strip[ss])) {
589 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
596 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
603 cs = &m->g->sets[OPND(s)];
604 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
605 if (wc == BADCHAR || !CHIN(cs, wc))
609 if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
610 (sp < m->endp && *(sp-1) == '\n' &&
611 (m->g->cflags®_NEWLINE)) )
617 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
618 (sp < m->endp && *sp == '\n' &&
619 (m->g->cflags®_NEWLINE)) )
625 if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
626 (sp < m->endp && *(sp-1) == '\n' &&
627 (m->g->cflags®_NEWLINE)) ||
629 !ISWORD(*(sp-1))) ) &&
630 (sp < m->endp && ISWORD(*sp)) )
636 if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
637 (sp < m->endp && *sp == '\n' &&
638 (m->g->cflags®_NEWLINE)) ||
639 (sp < m->endp && !ISWORD(*sp)) ) &&
640 (sp > m->beginp && ISWORD(*(sp-1))) )
647 case OOR1: /* matches null but needs to skip */
651 assert(OP(s) == OOR2);
653 } while (OP(s = m->g->strip[ss]) != O_CH);
654 /* note that the ss++ gets us past the O_CH */
656 default: /* have to make a choice */
660 if (!hard) { /* that was it! */
665 ss--; /* adjust for the for's final increment */
668 AT("hard", sp, stop, ss, stopst);
671 case OBACK_: /* the vilest depths */
673 assert(0 < i && i <= m->g->nsub);
674 if (m->pmatch[i].rm_eo == -1)
676 assert(m->pmatch[i].rm_so != -1);
677 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
678 if (len == 0 && rec++ > MAX_RECURSION)
680 assert(stop - m->beginp >= len);
682 return(NULL); /* not enough left to match */
683 ssp = m->offp + m->pmatch[i].rm_so;
684 if (memcmp(sp, ssp, len) != 0)
686 while (m->g->strip[ss] != SOP(O_BACK, i))
688 return(backref(m, sp+len, stop, ss+1, stopst, lev, rec));
689 case OQUEST_: /* to null or not */
690 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
692 return(dp); /* not */
693 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev, rec));
695 assert(m->lastpos != NULL);
696 assert(lev+1 <= m->g->nplus);
697 m->lastpos[lev+1] = sp;
698 return(backref(m, sp, stop, ss+1, stopst, lev+1, rec));
700 if (sp == m->lastpos[lev]) /* last pass matched null */
701 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
702 /* try another pass */
703 m->lastpos[lev] = sp;
704 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev, rec);
706 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
709 case OCH_: /* find the right one, if any */
711 esub = ss + OPND(s) - 1;
712 assert(OP(m->g->strip[esub]) == OOR1);
713 for (;;) { /* find first matching branch */
714 dp = backref(m, sp, stop, ssub, esub, lev, rec);
717 /* that one missed, try next one */
718 if (OP(m->g->strip[esub]) == O_CH)
719 return(NULL); /* there is none */
721 assert(OP(m->g->strip[esub]) == OOR2);
723 esub += OPND(m->g->strip[esub]);
724 if (OP(m->g->strip[esub]) == OOR2)
727 assert(OP(m->g->strip[esub]) == O_CH);
731 case OLPAREN: /* must undo assignment if rest fails */
733 assert(0 < i && i <= m->g->nsub);
734 offsave = m->pmatch[i].rm_so;
735 m->pmatch[i].rm_so = sp - m->offp;
736 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
739 m->pmatch[i].rm_so = offsave;
741 case ORPAREN: /* must undo assignment if rest fails */
743 assert(0 < i && i <= m->g->nsub);
744 offsave = m->pmatch[i].rm_eo;
745 m->pmatch[i].rm_eo = sp - m->offp;
746 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
749 m->pmatch[i].rm_eo = offsave;
759 return "shut up gcc";
763 - fast - step through the string at top speed
764 == static const char *fast(struct match *m, const char *start, \
765 == const char *stop, sopno startst, sopno stopst);
767 static const char * /* where tentative match ended, or NULL */
768 fast( struct match *m,
775 states fresh = m->fresh;
777 const char *p = start;
779 wint_t lastc; /* previous c */
782 const char *coldp; /* last p after which no match was underway */
788 st = step(m->g, startst, stopst, st, NOTHING, st);
792 if (start == m->beginp)
796 * XXX Wrong if the previous character was multi-byte.
797 * Newline never is (in supported encodings),
798 * so this only breaks the ISWORD tests below.
800 c = (uch)*(start - 1);
809 clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR);
813 /* is there an EOL and/or BOL between lastc and c? */
816 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
817 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
821 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
822 (c == OUT && !(m->eflags®_NOTEOL)) ) {
823 flagch = (flagch == BOL) ? BOLEOL : EOL;
828 st = step(m->g, startst, stopst, st, flagch, st);
832 /* how about a word boundary? */
833 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
834 (c != OUT && ISWORD(c)) ) {
837 if ( (lastc != OUT && ISWORD(lastc)) &&
838 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
841 if (flagch == BOW || flagch == EOW) {
842 st = step(m->g, startst, stopst, st, flagch, st);
847 if (ISSET(st, stopst) || p == stop || clen > stop - p)
848 break; /* NOTE BREAK OUT */
850 /* no, we must deal with this character */
854 st = step(m->g, startst, stopst, tmp, c, st);
856 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
860 assert(coldp != NULL);
862 if (ISSET(st, stopst))
863 return(p+XMBRTOWC(NULL, p, stop - p, &m->mbs, 0));
869 - slow - step through the string more deliberately
870 == static const char *slow(struct match *m, const char *start, \
871 == const char *stop, sopno startst, sopno stopst);
873 static const char * /* where it ended */
874 slow( struct match *m,
881 states empty = m->empty;
883 const char *p = start;
885 wint_t lastc; /* previous c */
888 const char *matchp; /* last p at which a match ended */
891 AT("slow", start, stop, startst, stopst);
894 SP("sstart", st, *p);
895 st = step(m->g, startst, stopst, st, NOTHING, st);
897 if (start == m->beginp)
901 * XXX Wrong if the previous character was multi-byte.
902 * Newline never is (in supported encodings),
903 * so this only breaks the ISWORD tests below.
905 c = (uch)*(start - 1);
914 clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR);
916 /* is there an EOL and/or BOL between lastc and c? */
919 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
920 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
924 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
925 (c == OUT && !(m->eflags®_NOTEOL)) ) {
926 flagch = (flagch == BOL) ? BOLEOL : EOL;
931 st = step(m->g, startst, stopst, st, flagch, st);
932 SP("sboleol", st, c);
935 /* how about a word boundary? */
936 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
937 (c != OUT && ISWORD(c)) ) {
940 if ( (lastc != OUT && ISWORD(lastc)) &&
941 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
944 if (flagch == BOW || flagch == EOW) {
945 st = step(m->g, startst, stopst, st, flagch, st);
946 SP("sboweow", st, c);
950 if (ISSET(st, stopst))
952 if (EQ(st, empty) || p == stop || clen > stop - p)
953 break; /* NOTE BREAK OUT */
955 /* no, we must deal with this character */
959 st = step(m->g, startst, stopst, tmp, c, st);
961 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
970 - step - map set of states reachable before char to set reachable after
971 == static states step(struct re_guts *g, sopno start, sopno stop, \
972 == states bef, int ch, states aft);
973 == #define BOL (OUT-1)
974 == #define EOL (BOL-1)
975 == #define BOLEOL (BOL-2)
976 == #define NOTHING (BOL-3)
977 == #define BOW (BOL-4)
978 == #define EOW (BOL-5)
979 == #define BADCHAR (BOL-6)
980 == #define NONCHAR(c) ((c) <= OUT)
983 step(struct re_guts *g,
984 sopno start, /* start state within strip */
985 sopno stop, /* state after stop state within strip */
986 states bef, /* states reachable before */
987 wint_t ch, /* character or NONCHAR code */
988 states aft) /* states already known reachable after */
993 onestate here; /* note, macros know this name */
997 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
1001 assert(pc == stop-1);
1004 /* only characters can match */
1005 assert(!NONCHAR(ch) || ch != OPND(s));
1010 if (ch == BOL || ch == BOLEOL)
1014 if (ch == EOL || ch == BOLEOL)
1030 cs = &g->sets[OPND(s)];
1031 if (!NONCHAR(ch) && CHIN(cs, ch))
1034 case OBACK_: /* ignored here */
1038 case OPLUS_: /* forward, this is just an empty */
1041 case O_PLUS: /* both forward and back */
1043 i = ISSETBACK(aft, OPND(s));
1044 BACK(aft, aft, OPND(s));
1045 if (!i && ISSETBACK(aft, OPND(s))) {
1046 /* oho, must reconsider loop body */
1051 case OQUEST_: /* two branches, both forward */
1053 FWD(aft, aft, OPND(s));
1055 case O_QUEST: /* just an empty */
1058 case OLPAREN: /* not significant here */
1062 case OCH_: /* mark the first two branches */
1064 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1065 FWD(aft, aft, OPND(s));
1067 case OOR1: /* done a branch, find the O_CH */
1068 if (ISSTATEIN(aft, here)) {
1070 OP(s = g->strip[pc+look]) != O_CH;
1072 assert(OP(s) == OOR2);
1073 FWD(aft, aft, look + 1);
1076 case OOR2: /* propagate OCH_'s marking */
1078 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
1079 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1080 FWD(aft, aft, OPND(s));
1083 case O_CH: /* just empty */
1086 default: /* ooooops... */
1097 - print - print a set of states
1099 == static void print(struct match *m, const char *caption, states st, \
1100 == int ch, FILE *d);
1104 print(struct match *m,
1105 const char *caption,
1110 struct re_guts *g = m->g;
1114 if (!(m->eflags®_TRACE))
1117 fprintf(d, "%s", caption);
1119 fprintf(d, " %s", pchar(ch));
1120 for (i = 0; i < g->nstates; i++)
1122 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
1129 - at - print current situation
1131 == static void at(struct match *m, const char *title, const char *start, \
1132 == const char *stop, sopno startst, sopno stopst);
1136 at( struct match *m,
1143 if (!(m->eflags®_TRACE))
1146 printf("%s %s-", title, pchar(*start));
1147 printf("%s ", pchar(*stop));
1148 printf("%ld-%ld\n", (long)startst, (long)stopst);
1152 #define PCHARDONE /* never again */
1154 - pchar - make a character printable
1156 == static const char *pchar(int ch);
1159 * Is this identical to regchar() over in debug.c? Well, yes. But a
1160 * duplicate here avoids having a debugging-capable regexec.o tied to
1161 * a matching debug.o, and this is convenient. It all disappears in
1162 * the non-debug compilation anyway, so it doesn't matter much.
1164 static const char * /* -> representation */
1167 static char pbuf[10];
1169 if (isprint((uch)ch) || ch == ' ')
1170 sprintf(pbuf, "%c", ch);
1172 sprintf(pbuf, "\\%o", ch);