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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)engine.c 8.5 (Berkeley) 3/20/94
39 * $FreeBSD: src/lib/libc/regex/engine.c,v 1.5.8.1 2000/07/31 06:30:37 dcs Exp $
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
50 #define matcher smatcher
53 #define dissect sdissect
54 #define backref sbackref
61 #define matcher lmatcher
64 #define dissect ldissect
65 #define backref lbackref
72 /* another structure passed up and down to avoid zillions of parameters */
76 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
77 char *offp; /* offsets work from here */
78 char *beginp; /* start of string -- virtual NUL precedes */
79 char *endp; /* end of string -- virtual NUL here */
80 char *coldp; /* can be no match starting before here */
81 char **lastpos; /* [nplus+1] */
83 states st; /* current states */
84 states fresh; /* states for a fresh start */
85 states tmp; /* temporary */
86 states empty; /* empty set of states */
89 /* ========= begin header generated by ./mkh ========= */
94 /* === engine.c === */
95 static int matcher (struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
96 static char *dissect (struct match *m, char *start, char *stop, sopno startst, sopno stopst);
97 static char *backref (struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev);
98 static char *fast (struct match *m, char *start, char *stop, sopno startst, sopno stopst);
99 static char *slow (struct match *m, char *start, char *stop, sopno startst, sopno stopst);
100 static states step (struct re_guts *g, sopno start, sopno stop, states bef, int ch, states aft);
103 #define BOLEOL (BOL+2)
104 #define NOTHING (BOL+3)
107 #define CODEMAX (BOL+5) /* highest code used */
108 #define NONCHAR(c) ((c) > CHAR_MAX)
109 #define NNONCHAR (CODEMAX-CHAR_MAX)
111 static void print (struct match *m, char *caption, states st, int ch, FILE *d);
114 static void at __P((struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst));
117 static char *pchar __P((int ch));
123 /* ========= end header generated by ./mkh ========= */
126 #define SP(t, s, c) print(m, t, s, c, stdout)
127 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
128 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
130 #define SP(t, s, c) /* nothing */
131 #define AT(t, p1, p2, s1, s2) /* nothing */
132 #define NOTE(s) /* nothing */
136 - matcher - the actual matching engine
137 == static int matcher(register struct re_guts *g, char *string, \
138 == size_t nmatch, regmatch_t pmatch[], int eflags);
140 static int /* 0 success, REG_NOMATCH failure */
141 matcher(g, string, nmatch, pmatch, eflags)
142 register struct re_guts *g;
151 register struct match *m = &mv;
153 register const sopno gf = g->firststate+1; /* +1 for OEND */
154 register const sopno gl = g->laststate;
157 /* Boyer-Moore algorithms variables */
160 register char *mustfirst;
161 register char *mustlast;
162 register int *matchjump;
163 register int *charjump;
165 /* simplify the situation where possible */
166 if (g->cflags®_NOSUB)
168 if (eflags®_STARTEND) {
169 start = string + pmatch[0].rm_so;
170 stop = string + pmatch[0].rm_eo;
173 stop = start + strlen(start);
178 /* prescreening; this does wonders for this rather slow code */
179 if (g->must != NULL) {
180 if (g->charjump != NULL && g->matchjump != NULL) {
182 mustlast = g->must + g->mlen - 1;
183 charjump = g->charjump;
184 matchjump = g->matchjump;
186 for (dp = start+g->mlen-1; dp < stop;) {
187 /* Fast skip non-matches */
188 while (dp < stop && charjump[*dp])
195 /* We depend on not being used for
196 * for strings of length 1
198 while (*--dp == *--pp && pp != mustfirst);
203 /* Jump to next possible match */
204 mj = matchjump[pp - mustfirst];
206 dp += (cj < mj ? mj : cj);
212 for (dp = start; dp < stop; dp++)
213 if (*dp == g->must[0] &&
214 stop - dp >= g->mlen &&
215 memcmp(dp, g->must, (size_t)g->mlen) == 0)
217 if (dp == stop) /* we didn't find g->must */
222 /* match struct setup */
237 /* Adjust start according to moffset, to speed things up */
239 start = ((dp - g->moffset) < start) ? start : dp - g->moffset;
241 /* this loop does only one repetition except for backrefs */
243 endp = fast(m, start, stop, gf, gl);
244 if (endp == NULL) { /* a miss */
248 if (nmatch == 0 && !g->backrefs)
249 break; /* no further info needed */
252 assert(m->coldp != NULL);
254 NOTE("finding start");
255 endp = slow(m, m->coldp, stop, gf, gl);
258 assert(m->coldp < m->endp);
261 if (nmatch == 1 && !g->backrefs)
262 break; /* no further info needed */
264 /* oh my, he wants the subexpressions... */
265 if (m->pmatch == NULL)
266 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
268 if (m->pmatch == NULL) {
272 for (i = 1; i <= m->g->nsub; i++)
273 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
274 if (!g->backrefs && !(m->eflags®_BACKR)) {
276 dp = dissect(m, m->coldp, endp, gf, gl);
278 if (g->nplus > 0 && m->lastpos == NULL)
279 m->lastpos = (char **)malloc((g->nplus+1) *
281 if (g->nplus > 0 && m->lastpos == NULL) {
286 NOTE("backref dissect");
287 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
292 /* uh-oh... we couldn't find a subexpression-level match */
293 assert(g->backrefs); /* must be back references doing it */
294 assert(g->nplus == 0 || m->lastpos != NULL);
296 if (dp != NULL || endp <= m->coldp)
299 endp = slow(m, m->coldp, endp-1, gf, gl);
302 /* try it on a shorter possibility */
304 for (i = 1; i <= m->g->nsub; i++) {
305 assert(m->pmatch[i].rm_so == -1);
306 assert(m->pmatch[i].rm_eo == -1);
309 NOTE("backoff dissect");
310 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
312 assert(dp == NULL || dp == endp);
313 if (dp != NULL) /* found a shorter one */
316 /* despite initial appearances, there is no match here */
318 start = m->coldp + 1; /* recycle starting later */
319 assert(start <= stop);
322 /* fill in the details if requested */
324 pmatch[0].rm_so = m->coldp - m->offp;
325 pmatch[0].rm_eo = endp - m->offp;
328 assert(m->pmatch != NULL);
329 for (i = 1; i < nmatch; i++)
331 pmatch[i] = m->pmatch[i];
333 pmatch[i].rm_so = -1;
334 pmatch[i].rm_eo = -1;
338 if (m->pmatch != NULL)
339 free((char *)m->pmatch);
340 if (m->lastpos != NULL)
341 free((char *)m->lastpos);
347 - dissect - figure out what matched what, no back references
348 == static char *dissect(register struct match *m, char *start, \
349 == char *stop, sopno startst, sopno stopst);
351 static char * /* == stop (success) always */
352 dissect(m, start, stop, startst, stopst)
353 register struct match *m;
360 register sopno ss; /* start sop of current subRE */
361 register sopno es; /* end sop of current subRE */
362 register char *sp; /* start of string matched by it */
363 register char *stp; /* string matched by it cannot pass here */
364 register char *rest; /* start of rest of string */
365 register char *tail; /* string unmatched by rest of RE */
366 register sopno ssub; /* start sop of subsubRE */
367 register sopno esub; /* end sop of subsubRE */
368 register char *ssp; /* start of string matched by subsubRE */
369 register char *sep; /* end of string matched by subsubRE */
370 register char *oldssp; /* previous ssp */
373 AT("diss", start, stop, startst, stopst);
375 for (ss = startst; ss < stopst; ss = es) {
376 /* identify end of subRE */
378 switch (OP(m->g->strip[es])) {
381 es += OPND(m->g->strip[es]);
384 while (OP(m->g->strip[es]) != O_CH)
385 es += OPND(m->g->strip[es]);
390 /* figure out what it matched */
391 switch (OP(m->g->strip[ss])) {
411 /* cases where length of match is hard to find */
415 /* how long could this one be? */
416 rest = slow(m, sp, stp, ss, es);
417 assert(rest != NULL); /* it did match */
418 /* could the rest match the rest? */
419 tail = slow(m, rest, stop, es, stopst);
422 /* no -- try a shorter match for this one */
424 assert(stp >= sp); /* it did work */
428 /* did innards match? */
429 if (slow(m, sp, rest, ssub, esub) != NULL) {
430 dp = dissect(m, sp, rest, ssub, esub);
439 /* how long could this one be? */
440 rest = slow(m, sp, stp, ss, es);
441 assert(rest != NULL); /* it did match */
442 /* could the rest match the rest? */
443 tail = slow(m, rest, stop, es, stopst);
446 /* no -- try a shorter match for this one */
448 assert(stp >= sp); /* it did work */
454 for (;;) { /* find last match of innards */
455 sep = slow(m, ssp, rest, ssub, esub);
456 if (sep == NULL || sep == ssp)
457 break; /* failed or matched null */
458 oldssp = ssp; /* on to next try */
462 /* last successful match */
466 assert(sep == rest); /* must exhaust substring */
467 assert(slow(m, ssp, sep, ssub, esub) == rest);
468 dp = dissect(m, ssp, sep, ssub, esub);
475 /* how long could this one be? */
476 rest = slow(m, sp, stp, ss, es);
477 assert(rest != NULL); /* it did match */
478 /* could the rest match the rest? */
479 tail = slow(m, rest, stop, es, stopst);
482 /* no -- try a shorter match for this one */
484 assert(stp >= sp); /* it did work */
487 esub = ss + OPND(m->g->strip[ss]) - 1;
488 assert(OP(m->g->strip[esub]) == OOR1);
489 for (;;) { /* find first matching branch */
490 if (slow(m, sp, rest, ssub, esub) == rest)
491 break; /* it matched all of it */
492 /* that one missed, try next one */
493 assert(OP(m->g->strip[esub]) == OOR1);
495 assert(OP(m->g->strip[esub]) == OOR2);
497 esub += OPND(m->g->strip[esub]);
498 if (OP(m->g->strip[esub]) == OOR2)
501 assert(OP(m->g->strip[esub]) == O_CH);
503 dp = dissect(m, sp, rest, ssub, esub);
515 i = OPND(m->g->strip[ss]);
516 assert(0 < i && i <= m->g->nsub);
517 m->pmatch[i].rm_so = sp - m->offp;
520 i = OPND(m->g->strip[ss]);
521 assert(0 < i && i <= m->g->nsub);
522 m->pmatch[i].rm_eo = sp - m->offp;
535 - backref - figure out what matched what, figuring in back references
536 == static char *backref(register struct match *m, char *start, \
537 == char *stop, sopno startst, sopno stopst, sopno lev);
539 static char * /* == stop (success) or NULL (failure) */
540 backref(m, start, stop, startst, stopst, lev)
541 register struct match *m;
546 sopno lev; /* PLUS nesting level */
549 register sopno ss; /* start sop of current subRE */
550 register char *sp; /* start of string matched by it */
551 register sopno ssub; /* start sop of subsubRE */
552 register sopno esub; /* end sop of subsubRE */
553 register char *ssp; /* start of string matched by subsubRE */
558 register regoff_t offsave;
561 AT("back", start, stop, startst, stopst);
564 /* get as far as we can with easy stuff */
566 for (ss = startst; !hard && ss < stopst; ss++)
567 switch (OP(s = m->g->strip[ss])) {
569 if (sp == stop || *sp++ != (char)OPND(s))
578 cs = &m->g->sets[OPND(s)];
579 if (sp == stop || !CHIN(cs, *sp++))
583 if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
584 (sp < m->endp && *(sp-1) == '\n' &&
585 (m->g->cflags®_NEWLINE)) )
591 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
592 (sp < m->endp && *sp == '\n' &&
593 (m->g->cflags®_NEWLINE)) )
599 if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
600 (sp < m->endp && *(sp-1) == '\n' &&
601 (m->g->cflags®_NEWLINE)) ||
603 !ISWORD(*(sp-1))) ) &&
604 (sp < m->endp && ISWORD(*sp)) )
610 if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
611 (sp < m->endp && *sp == '\n' &&
612 (m->g->cflags®_NEWLINE)) ||
613 (sp < m->endp && !ISWORD(*sp)) ) &&
614 (sp > m->beginp && ISWORD(*(sp-1))) )
621 case OOR1: /* matches null but needs to skip */
625 assert(OP(s) == OOR2);
627 } while (OP(s = m->g->strip[ss]) != O_CH);
628 /* note that the ss++ gets us past the O_CH */
630 default: /* have to make a choice */
634 if (!hard) { /* that was it! */
639 ss--; /* adjust for the for's final increment */
642 AT("hard", sp, stop, ss, stopst);
645 case OBACK_: /* the vilest depths */
647 assert(0 < i && i <= m->g->nsub);
648 if (m->pmatch[i].rm_eo == -1)
650 assert(m->pmatch[i].rm_so != -1);
651 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
652 assert(stop - m->beginp >= len);
654 return(NULL); /* not enough left to match */
655 ssp = m->offp + m->pmatch[i].rm_so;
656 if (memcmp(sp, ssp, len) != 0)
658 while (m->g->strip[ss] != SOP(O_BACK, i))
660 return(backref(m, sp+len, stop, ss+1, stopst, lev));
662 case OQUEST_: /* to null or not */
663 dp = backref(m, sp, stop, ss+1, stopst, lev);
665 return(dp); /* not */
666 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
669 assert(m->lastpos != NULL);
670 assert(lev+1 <= m->g->nplus);
671 m->lastpos[lev+1] = sp;
672 return(backref(m, sp, stop, ss+1, stopst, lev+1));
675 if (sp == m->lastpos[lev]) /* last pass matched null */
676 return(backref(m, sp, stop, ss+1, stopst, lev-1));
677 /* try another pass */
678 m->lastpos[lev] = sp;
679 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
681 return(backref(m, sp, stop, ss+1, stopst, lev-1));
685 case OCH_: /* find the right one, if any */
687 esub = ss + OPND(s) - 1;
688 assert(OP(m->g->strip[esub]) == OOR1);
689 for (;;) { /* find first matching branch */
690 dp = backref(m, sp, stop, ssub, esub, lev);
693 /* that one missed, try next one */
694 if (OP(m->g->strip[esub]) == O_CH)
695 return(NULL); /* there is none */
697 assert(OP(m->g->strip[esub]) == OOR2);
699 esub += OPND(m->g->strip[esub]);
700 if (OP(m->g->strip[esub]) == OOR2)
703 assert(OP(m->g->strip[esub]) == O_CH);
706 case OLPAREN: /* must undo assignment if rest fails */
708 assert(0 < i && i <= m->g->nsub);
709 offsave = m->pmatch[i].rm_so;
710 m->pmatch[i].rm_so = sp - m->offp;
711 dp = backref(m, sp, stop, ss+1, stopst, lev);
714 m->pmatch[i].rm_so = offsave;
717 case ORPAREN: /* must undo assignment if rest fails */
719 assert(0 < i && i <= m->g->nsub);
720 offsave = m->pmatch[i].rm_eo;
721 m->pmatch[i].rm_eo = sp - m->offp;
722 dp = backref(m, sp, stop, ss+1, stopst, lev);
725 m->pmatch[i].rm_eo = offsave;
736 return "shut up gcc";
740 - fast - step through the string at top speed
741 == static char *fast(register struct match *m, char *start, \
742 == char *stop, sopno startst, sopno stopst);
744 static char * /* where tentative match ended, or NULL */
745 fast(m, start, stop, startst, stopst)
746 register struct match *m;
752 register states st = m->st;
753 register states fresh = m->fresh;
754 register states tmp = m->tmp;
755 register char *p = start;
756 register int c = (start == m->beginp) ? OUT : *(start-1);
757 register int lastc; /* previous c */
760 register char *coldp; /* last p after which no match was underway */
764 st = step(m->g, startst, stopst, st, NOTHING, st);
771 c = (p == m->endp) ? OUT : *p;
775 /* is there an EOL and/or BOL between lastc and c? */
778 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
779 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
783 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
784 (c == OUT && !(m->eflags®_NOTEOL)) ) {
785 flagch = (flagch == BOL) ? BOLEOL : EOL;
790 st = step(m->g, startst, stopst, st, flagch, st);
794 /* how about a word boundary? */
795 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
796 (c != OUT && ISWORD(c)) ) {
799 if ( (lastc != OUT && ISWORD(lastc)) &&
800 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
803 if (flagch == BOW || flagch == EOW) {
804 st = step(m->g, startst, stopst, st, flagch, st);
809 if (ISSET(st, stopst) || p == stop)
810 break; /* NOTE BREAK OUT */
812 /* no, we must deal with this character */
816 st = step(m->g, startst, stopst, tmp, c, st);
818 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
822 assert(coldp != NULL);
824 if (ISSET(st, stopst))
831 - slow - step through the string more deliberately
832 == static char *slow(register struct match *m, char *start, \
833 == char *stop, sopno startst, sopno stopst);
835 static char * /* where it ended */
836 slow(m, start, stop, startst, stopst)
837 register struct match *m;
843 register states st = m->st;
844 register states empty = m->empty;
845 register states tmp = m->tmp;
846 register char *p = start;
847 register int c = (start == m->beginp) ? OUT : *(start-1);
848 register int lastc; /* previous c */
851 register char *matchp; /* last p at which a match ended */
853 AT("slow", start, stop, startst, stopst);
856 SP("sstart", st, *p);
857 st = step(m->g, startst, stopst, st, NOTHING, st);
862 c = (p == m->endp) ? OUT : *p;
864 /* is there an EOL and/or BOL between lastc and c? */
867 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
868 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
872 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
873 (c == OUT && !(m->eflags®_NOTEOL)) ) {
874 flagch = (flagch == BOL) ? BOLEOL : EOL;
879 st = step(m->g, startst, stopst, st, flagch, st);
880 SP("sboleol", st, c);
883 /* how about a word boundary? */
884 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
885 (c != OUT && ISWORD(c)) ) {
888 if ( (lastc != OUT && ISWORD(lastc)) &&
889 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
892 if (flagch == BOW || flagch == EOW) {
893 st = step(m->g, startst, stopst, st, flagch, st);
894 SP("sboweow", st, c);
898 if (ISSET(st, stopst))
900 if (EQ(st, empty) || p == stop)
901 break; /* NOTE BREAK OUT */
903 /* no, we must deal with this character */
907 st = step(m->g, startst, stopst, tmp, c, st);
909 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
918 - step - map set of states reachable before char to set reachable after
919 == static states step(register struct re_guts *g, sopno start, sopno stop, \
920 == register states bef, int ch, register states aft);
921 == #define BOL (OUT+1)
922 == #define EOL (BOL+1)
923 == #define BOLEOL (BOL+2)
924 == #define NOTHING (BOL+3)
925 == #define BOW (BOL+4)
926 == #define EOW (BOL+5)
927 == #define CODEMAX (BOL+5) // highest code used
928 == #define NONCHAR(c) ((c) > CHAR_MAX)
929 == #define NNONCHAR (CODEMAX-CHAR_MAX)
932 step(g, start, stop, bef, ch, aft)
933 register struct re_guts *g;
934 sopno start; /* start state within strip */
935 sopno stop; /* state after stop state within strip */
936 register states bef; /* states reachable before */
937 int ch; /* character or NONCHAR code */
938 register states aft; /* states already known reachable after */
943 register onestate here; /* note, macros know this name */
947 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
951 assert(pc == stop-1);
954 /* only characters can match */
955 assert(!NONCHAR(ch) || ch != (char)OPND(s));
956 if (ch == (char)OPND(s))
960 if (ch == BOL || ch == BOLEOL)
964 if (ch == EOL || ch == BOLEOL)
980 cs = &g->sets[OPND(s)];
981 if (!NONCHAR(ch) && CHIN(cs, ch))
984 case OBACK_: /* ignored here */
988 case OPLUS_: /* forward, this is just an empty */
991 case O_PLUS: /* both forward and back */
993 i = ISSETBACK(aft, OPND(s));
994 BACK(aft, aft, OPND(s));
995 if (!i && ISSETBACK(aft, OPND(s))) {
996 /* oho, must reconsider loop body */
1001 case OQUEST_: /* two branches, both forward */
1003 FWD(aft, aft, OPND(s));
1005 case O_QUEST: /* just an empty */
1008 case OLPAREN: /* not significant here */
1012 case OCH_: /* mark the first two branches */
1014 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1015 FWD(aft, aft, OPND(s));
1017 case OOR1: /* done a branch, find the O_CH */
1018 if (ISSTATEIN(aft, here)) {
1020 OP(s = g->strip[pc+look]) != O_CH;
1022 assert(OP(s) == OOR2);
1023 FWD(aft, aft, look);
1026 case OOR2: /* propagate OCH_'s marking */
1028 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
1029 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1030 FWD(aft, aft, OPND(s));
1033 case O_CH: /* just empty */
1036 default: /* ooooops... */
1047 - print - print a set of states
1049 == static void print(struct match *m, char *caption, states st, \
1050 == int ch, FILE *d);
1054 print(m, caption, st, ch, d)
1061 register struct re_guts *g = m->g;
1063 register int first = 1;
1065 if (!(m->eflags®_TRACE))
1068 fprintf(d, "%s", caption);
1070 fprintf(d, " %s", pchar(ch));
1071 for (i = 0; i < g->nstates; i++)
1073 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
1080 - at - print current situation
1082 == static void at(struct match *m, char *title, char *start, char *stop, \
1083 == sopno startst, sopno stopst);
1087 at(m, title, start, stop, startst, stopst)
1095 if (!(m->eflags®_TRACE))
1098 printf("%s %s-", title, pchar(*start));
1099 printf("%s ", pchar(*stop));
1100 printf("%ld-%ld\n", (long)startst, (long)stopst);
1104 #define PCHARDONE /* never again */
1106 - pchar - make a character printable
1108 == static char *pchar(int ch);
1111 * Is this identical to regchar() over in debug.c? Well, yes. But a
1112 * duplicate here avoids having a debugging-capable regexec.o tied to
1113 * a matching debug.o, and this is convenient. It all disappears in
1114 * the non-debug compilation anyway, so it doesn't matter much.
1116 static char * /* -> representation */
1120 static char pbuf[10];
1122 if (isprint((uch)ch) || ch == ' ')
1123 sprintf(pbuf, "%c", ch);
1125 sprintf(pbuf, "\\%o", ch);