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 * 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.
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 * @(#)engine.c 8.5 (Berkeley) 3/20/94
34 * $FreeBSD: src/lib/libc/regex/engine.c,v 1.21 2007/05/25 12:44:58 delphij Exp $
35 * $DragonFly: src/lib/libc/regex/engine.c,v 1.7 2005/11/20 09:18:37 swildner Exp $
39 * The matching engine and friends. This file is #included by regexec.c
40 * after suitable #defines of a variety of macros used herein, so that
41 * different state representations can be used without duplicating masses
46 #define matcher smatcher
49 #define dissect sdissect
50 #define backref sbackref
57 #define matcher lmatcher
60 #define dissect ldissect
61 #define backref lbackref
68 #define matcher mmatcher
71 #define dissect mdissect
72 #define backref mbackref
79 /* another structure passed up and down to avoid zillions of parameters */
83 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
84 const char *offp; /* offsets work from here */
85 const char *beginp; /* start of string -- virtual NUL precedes */
86 const char *endp; /* end of string -- virtual NUL here */
87 const char *coldp; /* can be no match starting before here */
88 const char **lastpos; /* [nplus+1] */
90 states st; /* current states */
91 states fresh; /* states for a fresh start */
92 states tmp; /* temporary */
93 states empty; /* empty set of states */
94 mbstate_t mbs; /* multibyte conversion state */
97 /* ========= begin header generated by ./mkh ========= */
102 /* === engine.c === */
103 static int matcher(struct re_guts *g, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
104 static const char *dissect(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
105 static const char *backref(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst, sopno lev, int);
106 static const char *fast(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
107 static const char *slow(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
108 static states step(struct re_guts *g, sopno start, sopno stop, states bef, wint_t ch, states aft);
109 #define MAX_RECURSION 100
112 #define BOLEOL (BOL-2)
113 #define NOTHING (BOL-3)
116 #define BADCHAR (BOL-6)
117 #define NONCHAR(c) ((c) <= OUT)
119 static void print(struct match *m, const char *caption, states st, int ch, FILE *d);
122 static void at(struct match *m, const char *title, const char *start, const char *stop, sopno startst, sopno stopst);
125 static const char *pchar(int ch);
131 /* ========= end header generated by ./mkh ========= */
134 #define SP(t, s, c) print(m, t, s, c, stdout)
135 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
136 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
138 #define SP(t, s, c) /* nothing */
139 #define AT(t, p1, p2, s1, s2) /* nothing */
140 #define NOTE(s) /* nothing */
144 - matcher - the actual matching engine
145 == static int matcher(struct re_guts *g, const char *string, \
146 == size_t nmatch, regmatch_t pmatch[], int eflags);
148 static int /* 0 success, REG_NOMATCH failure */
149 matcher(struct re_guts *g,
158 struct match *m = &mv;
160 const sopno gf = g->firststate+1; /* +1 for OEND */
161 const sopno gl = g->laststate;
164 /* Boyer-Moore algorithms variables */
167 const char *mustfirst;
168 const char *mustlast;
172 /* simplify the situation where possible */
173 if (g->cflags®_NOSUB)
175 if (eflags®_STARTEND) {
176 start = string + pmatch[0].rm_so;
177 stop = string + pmatch[0].rm_eo;
180 stop = start + strlen(start);
185 /* prescreening; this does wonders for this rather slow code */
186 if (g->must != NULL) {
187 if (g->charjump != NULL && g->matchjump != NULL) {
189 mustlast = g->must + g->mlen - 1;
190 charjump = g->charjump;
191 matchjump = g->matchjump;
193 for (dp = start+g->mlen-1; dp < stop;) {
194 /* Fast skip non-matches */
195 while (dp < stop && charjump[(int)*dp])
196 dp += charjump[(int)*dp];
202 /* We depend on not being used for
203 * for strings of length 1
205 while (*--dp == *--pp && pp != mustfirst);
210 /* Jump to next possible match */
211 mj = matchjump[pp - mustfirst];
212 cj = charjump[(int)*dp];
213 dp += (cj < mj ? mj : cj);
219 for (dp = start; dp < stop; dp++)
220 if (*dp == g->must[0] &&
221 stop - dp >= g->mlen &&
222 memcmp(dp, g->must, (size_t)g->mlen) == 0)
224 if (dp == stop) /* we didn't find g->must */
229 /* match struct setup */
245 /* Adjust start according to moffset, to speed things up */
247 start = ((dp - g->moffset) < start) ? start : dp - g->moffset;
249 /* this loop does only one repetition except for backrefs */
251 endp = fast(m, start, stop, gf, gl);
252 if (endp == NULL) { /* a miss */
253 if (m->pmatch != NULL)
254 free((char *)m->pmatch);
255 if (m->lastpos != NULL)
256 free((char *)m->lastpos);
260 if (nmatch == 0 && !g->backrefs)
261 break; /* no further info needed */
264 assert(m->coldp != NULL);
266 NOTE("finding start");
267 endp = slow(m, m->coldp, stop, gf, gl);
270 assert(m->coldp < m->endp);
271 m->coldp += XMBRTOWC(NULL, m->coldp,
272 m->endp - m->coldp, &m->mbs, 0);
274 if (nmatch == 1 && !g->backrefs)
275 break; /* no further info needed */
277 /* oh my, he wants the subexpressions... */
278 if (m->pmatch == NULL)
279 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
281 if (m->pmatch == NULL) {
285 for (i = 1; i <= m->g->nsub; i++)
286 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
287 if (!g->backrefs && !(m->eflags®_BACKR)) {
289 dp = dissect(m, m->coldp, endp, gf, gl);
291 if (g->nplus > 0 && m->lastpos == NULL)
292 m->lastpos = malloc((g->nplus+1) *
293 sizeof(const char *));
294 if (g->nplus > 0 && m->lastpos == NULL) {
299 NOTE("backref dissect");
300 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
305 /* uh-oh... we couldn't find a subexpression-level match */
306 assert(g->backrefs); /* must be back references doing it */
307 assert(g->nplus == 0 || m->lastpos != NULL);
309 if (dp != NULL || endp <= m->coldp)
312 endp = slow(m, m->coldp, endp-1, gf, gl);
315 /* try it on a shorter possibility */
317 for (i = 1; i <= m->g->nsub; i++) {
318 assert(m->pmatch[i].rm_so == -1);
319 assert(m->pmatch[i].rm_eo == -1);
322 NOTE("backoff dissect");
323 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
325 assert(dp == NULL || dp == endp);
326 if (dp != NULL) /* found a shorter one */
329 /* despite initial appearances, there is no match here */
331 /* recycle starting later */
332 start = m->coldp + XMBRTOWC(NULL, m->coldp,
333 stop - m->coldp, &m->mbs, 0);
334 assert(start <= stop);
337 /* fill in the details if requested */
339 pmatch[0].rm_so = m->coldp - m->offp;
340 pmatch[0].rm_eo = endp - m->offp;
343 assert(m->pmatch != NULL);
344 for (i = 1; i < nmatch; i++)
346 pmatch[i] = m->pmatch[i];
348 pmatch[i].rm_so = -1;
349 pmatch[i].rm_eo = -1;
353 if (m->pmatch != NULL)
354 free((char *)m->pmatch);
355 if (m->lastpos != NULL)
356 free((char *)m->lastpos);
362 - dissect - figure out what matched what, no back references
363 == static const char *dissect(struct match *m, const char *start, \
364 == const char *stop, sopno startst, sopno stopst);
366 static const char * /* == stop (success) always */
367 dissect(struct match *m,
374 sopno ss; /* start sop of current subRE */
375 sopno es; /* end sop of current subRE */
376 const char *sp; /* start of string matched by it */
377 const char *stp; /* string matched by it cannot pass here */
378 const char *rest; /* start of rest of string */
379 const char *tail; /* string unmatched by rest of RE */
380 sopno ssub; /* start sop of subsubRE */
381 sopno esub; /* end sop of subsubRE */
382 const char *ssp; /* start of string matched by subsubRE */
383 const char *sep; /* end of string matched by subsubRE */
384 const char *oldssp; /* previous ssp */
387 AT("diss", start, stop, startst, stopst);
389 for (ss = startst; ss < stopst; ss = es) {
390 /* identify end of subRE */
392 switch (OP(m->g->strip[es])) {
395 es += OPND(m->g->strip[es]);
398 while (OP(m->g->strip[es]) != O_CH)
399 es += OPND(m->g->strip[es]);
404 /* figure out what it matched */
405 switch (OP(m->g->strip[ss])) {
410 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0);
419 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0);
425 /* cases where length of match is hard to find */
429 /* how long could this one be? */
430 rest = slow(m, sp, stp, ss, es);
431 assert(rest != NULL); /* it did match */
432 /* could the rest match the rest? */
433 tail = slow(m, rest, stop, es, stopst);
436 /* no -- try a shorter match for this one */
438 assert(stp >= sp); /* it did work */
442 /* did innards match? */
443 if (slow(m, sp, rest, ssub, esub) != NULL) {
444 dp = dissect(m, sp, rest, ssub, esub);
453 /* how long could this one be? */
454 rest = slow(m, sp, stp, ss, es);
455 assert(rest != NULL); /* it did match */
456 /* could the rest match the rest? */
457 tail = slow(m, rest, stop, es, stopst);
460 /* no -- try a shorter match for this one */
462 assert(stp >= sp); /* it did work */
468 for (;;) { /* find last match of innards */
469 sep = slow(m, ssp, rest, ssub, esub);
470 if (sep == NULL || sep == ssp)
471 break; /* failed or matched null */
472 oldssp = ssp; /* on to next try */
476 /* last successful match */
480 assert(sep == rest); /* must exhaust substring */
481 assert(slow(m, ssp, sep, ssub, esub) == rest);
482 dp = dissect(m, ssp, sep, ssub, esub);
489 /* how long could this one be? */
490 rest = slow(m, sp, stp, ss, es);
491 assert(rest != NULL); /* it did match */
492 /* could the rest match the rest? */
493 tail = slow(m, rest, stop, es, stopst);
496 /* no -- try a shorter match for this one */
498 assert(stp >= sp); /* it did work */
501 esub = ss + OPND(m->g->strip[ss]) - 1;
502 assert(OP(m->g->strip[esub]) == OOR1);
503 for (;;) { /* find first matching branch */
504 if (slow(m, sp, rest, ssub, esub) == rest)
505 break; /* it matched all of it */
506 /* that one missed, try next one */
507 assert(OP(m->g->strip[esub]) == OOR1);
509 assert(OP(m->g->strip[esub]) == OOR2);
511 esub += OPND(m->g->strip[esub]);
512 if (OP(m->g->strip[esub]) == OOR2)
515 assert(OP(m->g->strip[esub]) == O_CH);
517 dp = dissect(m, sp, rest, ssub, esub);
529 i = OPND(m->g->strip[ss]);
530 assert(0 < i && i <= m->g->nsub);
531 m->pmatch[i].rm_so = sp - m->offp;
534 i = OPND(m->g->strip[ss]);
535 assert(0 < i && i <= m->g->nsub);
536 m->pmatch[i].rm_eo = sp - m->offp;
549 - backref - figure out what matched what, figuring in back references
550 == static const char *backref(struct match *m, const char *start, \
551 == const char *stop, sopno startst, sopno stopst, sopno lev);
553 static const char * /* == stop (success) or NULL (failure) */
554 backref(struct match *m,
559 sopno lev, /* PLUS nesting level */
563 sopno ss; /* start sop of current subRE */
564 const char *sp; /* start of string matched by it */
565 sopno ssub; /* start sop of subsubRE */
566 sopno esub; /* end sop of subsubRE */
567 const char *ssp; /* start of string matched by subsubRE */
576 AT("back", start, stop, startst, stopst);
579 /* get as far as we can with easy stuff */
581 for (ss = startst; !hard && ss < stopst; ss++)
582 switch (OP(s = m->g->strip[ss])) {
586 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
593 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
600 cs = &m->g->sets[OPND(s)];
601 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
602 if (wc == BADCHAR || !CHIN(cs, wc))
606 if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
607 (sp < m->endp && *(sp-1) == '\n' &&
608 (m->g->cflags®_NEWLINE)) )
614 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
615 (sp < m->endp && *sp == '\n' &&
616 (m->g->cflags®_NEWLINE)) )
622 if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
623 (sp < m->endp && *(sp-1) == '\n' &&
624 (m->g->cflags®_NEWLINE)) ||
626 !ISWORD(*(sp-1))) ) &&
627 (sp < m->endp && ISWORD(*sp)) )
633 if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
634 (sp < m->endp && *sp == '\n' &&
635 (m->g->cflags®_NEWLINE)) ||
636 (sp < m->endp && !ISWORD(*sp)) ) &&
637 (sp > m->beginp && ISWORD(*(sp-1))) )
644 case OOR1: /* matches null but needs to skip */
648 assert(OP(s) == OOR2);
650 } while (OP(s = m->g->strip[ss]) != O_CH);
651 /* note that the ss++ gets us past the O_CH */
653 default: /* have to make a choice */
657 if (!hard) { /* that was it! */
662 ss--; /* adjust for the for's final increment */
665 AT("hard", sp, stop, ss, stopst);
668 case OBACK_: /* the vilest depths */
670 assert(0 < i && i <= m->g->nsub);
671 if (m->pmatch[i].rm_eo == -1)
673 assert(m->pmatch[i].rm_so != -1);
674 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
675 if (len == 0 && rec++ > MAX_RECURSION)
677 assert(stop - m->beginp >= len);
679 return(NULL); /* not enough left to match */
680 ssp = m->offp + m->pmatch[i].rm_so;
681 if (memcmp(sp, ssp, len) != 0)
683 while (m->g->strip[ss] != SOP(O_BACK, i))
685 return(backref(m, sp+len, stop, ss+1, stopst, lev, rec));
687 case OQUEST_: /* to null or not */
688 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
690 return(dp); /* not */
691 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev, rec));
694 assert(m->lastpos != NULL);
695 assert(lev+1 <= m->g->nplus);
696 m->lastpos[lev+1] = sp;
697 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));
710 case OCH_: /* find the right one, if any */
712 esub = ss + OPND(s) - 1;
713 assert(OP(m->g->strip[esub]) == OOR1);
714 for (;;) { /* find first matching branch */
715 dp = backref(m, sp, stop, ssub, esub, lev, rec);
718 /* that one missed, try next one */
719 if (OP(m->g->strip[esub]) == O_CH)
720 return(NULL); /* there is none */
722 assert(OP(m->g->strip[esub]) == OOR2);
724 esub += OPND(m->g->strip[esub]);
725 if (OP(m->g->strip[esub]) == OOR2)
728 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;
742 case ORPAREN: /* must undo assignment if rest fails */
744 assert(0 < i && i <= m->g->nsub);
745 offsave = m->pmatch[i].rm_eo;
746 m->pmatch[i].rm_eo = sp - m->offp;
747 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
750 m->pmatch[i].rm_eo = offsave;
761 return "shut up gcc";
765 - fast - step through the string at top speed
766 == static const char *fast(struct match *m, const char *start, \
767 == const char *stop, sopno startst, sopno stopst);
769 static const char * /* where tentative match ended, or NULL */
770 fast( struct match *m,
777 states fresh = m->fresh;
779 const char *p = start;
781 wint_t lastc; /* previous c */
784 const char *coldp; /* last p after which no match was underway */
789 st = step(m->g, startst, stopst, st, NOTHING, st);
793 if (start == m->beginp)
797 * XXX Wrong if the previous character was multi-byte.
798 * Newline never is (in supported encodings),
799 * so this only breaks the ISWORD tests below.
801 c = (uch)*(start - 1);
810 clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR);
814 /* is there an EOL and/or BOL between lastc and c? */
817 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
818 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
822 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
823 (c == OUT && !(m->eflags®_NOTEOL)) ) {
824 flagch = (flagch == BOL) ? BOLEOL : EOL;
829 st = step(m->g, startst, stopst, st, flagch, st);
833 /* how about a word boundary? */
834 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
835 (c != OUT && ISWORD(c)) ) {
838 if ( (lastc != OUT && ISWORD(lastc)) &&
839 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
842 if (flagch == BOW || flagch == EOW) {
843 st = step(m->g, startst, stopst, st, flagch, st);
848 if (ISSET(st, stopst) || p == stop || clen > stop - p)
849 break; /* NOTE BREAK OUT */
851 /* no, we must deal with this character */
855 st = step(m->g, startst, stopst, tmp, c, st);
857 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
861 assert(coldp != NULL);
863 if (ISSET(st, stopst))
864 return(p+XMBRTOWC(NULL, p, stop - p, &m->mbs, 0));
870 - slow - step through the string more deliberately
871 == static const char *slow(struct match *m, const char *start, \
872 == const char *stop, sopno startst, sopno stopst);
874 static const char * /* where it ended */
875 slow( struct match *m,
882 states empty = m->empty;
884 const char *p = start;
886 wint_t lastc; /* previous c */
889 const char *matchp; /* last p at which a match ended */
892 AT("slow", start, stop, startst, stopst);
895 SP("sstart", st, *p);
896 st = step(m->g, startst, stopst, st, NOTHING, st);
898 if (start == m->beginp)
902 * XXX Wrong if the previous character was multi-byte.
903 * Newline never is (in supported encodings),
904 * so this only breaks the ISWORD tests below.
906 c = (uch)*(start - 1);
915 clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR);
917 /* is there an EOL and/or BOL between lastc and c? */
920 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
921 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
925 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
926 (c == OUT && !(m->eflags®_NOTEOL)) ) {
927 flagch = (flagch == BOL) ? BOLEOL : EOL;
932 st = step(m->g, startst, stopst, st, flagch, st);
933 SP("sboleol", st, c);
936 /* how about a word boundary? */
937 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
938 (c != OUT && ISWORD(c)) ) {
941 if ( (lastc != OUT && ISWORD(lastc)) &&
942 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
945 if (flagch == BOW || flagch == EOW) {
946 st = step(m->g, startst, stopst, st, flagch, st);
947 SP("sboweow", st, c);
951 if (ISSET(st, stopst))
953 if (EQ(st, empty) || p == stop || clen > stop - p)
954 break; /* NOTE BREAK OUT */
956 /* no, we must deal with this character */
960 st = step(m->g, startst, stopst, tmp, c, st);
962 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
971 - step - map set of states reachable before char to set reachable after
972 == static states step(struct re_guts *g, sopno start, sopno stop, \
973 == states bef, int ch, states aft);
974 == #define BOL (OUT-1)
975 == #define EOL (BOL-1)
976 == #define BOLEOL (BOL-2)
977 == #define NOTHING (BOL-3)
978 == #define BOW (BOL-4)
979 == #define EOW (BOL-5)
980 == #define BADCHAR (BOL-6)
981 == #define NONCHAR(c) ((c) <= OUT)
984 step(struct re_guts *g,
985 sopno start, /* start state within strip */
986 sopno stop, /* state after stop state within strip */
987 states bef, /* states reachable before */
988 wint_t ch, /* character or NONCHAR code */
989 states aft) /* states already known reachable after */
994 onestate here; /* note, macros know this name */
998 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
1002 assert(pc == stop-1);
1005 /* only characters can match */
1006 assert(!NONCHAR(ch) || ch != OPND(s));
1011 if (ch == BOL || ch == BOLEOL)
1015 if (ch == EOL || ch == BOLEOL)
1031 cs = &g->sets[OPND(s)];
1032 if (!NONCHAR(ch) && CHIN(cs, ch))
1035 case OBACK_: /* ignored here */
1039 case OPLUS_: /* forward, this is just an empty */
1042 case O_PLUS: /* both forward and back */
1044 i = ISSETBACK(aft, OPND(s));
1045 BACK(aft, aft, OPND(s));
1046 if (!i && ISSETBACK(aft, OPND(s))) {
1047 /* oho, must reconsider loop body */
1052 case OQUEST_: /* two branches, both forward */
1054 FWD(aft, aft, OPND(s));
1056 case O_QUEST: /* just an empty */
1059 case OLPAREN: /* not significant here */
1063 case OCH_: /* mark the first two branches */
1065 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1066 FWD(aft, aft, OPND(s));
1068 case OOR1: /* done a branch, find the O_CH */
1069 if (ISSTATEIN(aft, here)) {
1071 OP(s = g->strip[pc+look]) != O_CH;
1073 assert(OP(s) == OOR2);
1074 FWD(aft, aft, look);
1077 case OOR2: /* propagate OCH_'s marking */
1079 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
1080 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1081 FWD(aft, aft, OPND(s));
1084 case O_CH: /* just empty */
1087 default: /* ooooops... */
1098 - print - print a set of states
1100 == static void print(struct match *m, const char *caption, states st, \
1101 == int ch, FILE *d);
1105 print(struct match *m,
1106 const char *caption,
1111 struct re_guts *g = m->g;
1115 if (!(m->eflags®_TRACE))
1118 fprintf(d, "%s", caption);
1120 fprintf(d, " %s", pchar(ch));
1121 for (i = 0; i < g->nstates; i++)
1123 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
1130 - at - print current situation
1132 == static void at(struct match *m, const char *title, const char *start, \
1133 == const char *stop, sopno startst, sopno stopst);
1137 at( struct match *m,
1144 if (!(m->eflags®_TRACE))
1147 printf("%s %s-", title, pchar(*start));
1148 printf("%s ", pchar(*stop));
1149 printf("%ld-%ld\n", (long)startst, (long)stopst);
1153 #define PCHARDONE /* never again */
1155 - pchar - make a character printable
1157 == static const char *pchar(int ch);
1160 * Is this identical to regchar() over in debug.c? Well, yes. But a
1161 * duplicate here avoids having a debugging-capable regexec.o tied to
1162 * a matching debug.o, and this is convenient. It all disappears in
1163 * the non-debug compilation anyway, so it doesn't matter much.
1165 static const char * /* -> representation */
1168 static char pbuf[10];
1170 if (isprint((uch)ch) || ch == ' ')
1171 sprintf(pbuf, "%c", ch);
1173 sprintf(pbuf, "\\%o", ch);