2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)engine.c 8.5 (Berkeley) 3/20/94
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
44 * The matching engine and friends. This file is #included by regexec.c
45 * after suitable #defines of a variety of macros used herein, so that
46 * different state representations can be used without duplicating masses
51 #define stepback sstepback
52 #define matcher smatcher
54 #define dissect sdissect
55 #define backref sbackref
62 #define stepback lstepback
63 #define matcher lmatcher
65 #define dissect ldissect
66 #define backref lbackref
73 #define stepback mstepback
74 #define matcher mmatcher
76 #define dissect mdissect
77 #define backref mbackref
84 /* another structure passed up and down to avoid zillions of parameters */
88 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
89 const char *offp; /* offsets work from here */
90 const char *beginp; /* start of string -- virtual NUL precedes */
91 const char *endp; /* end of string -- virtual NUL here */
92 const char *coldp; /* can be no match starting before here */
93 const char **lastpos; /* [nplus+1] */
95 states st; /* current states */
96 states fresh; /* states for a fresh start */
97 states tmp; /* temporary */
98 states empty; /* empty set of states */
99 mbstate_t mbs; /* multibyte conversion state */
102 /* ========= begin header generated by ./mkh ========= */
107 /* === engine.c === */
108 static int matcher(struct re_guts *g, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
109 static const char *dissect(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
110 static const char *backref(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst, sopno lev, int);
111 static const char *walk(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst, bool fast);
112 static states step(struct re_guts *g, sopno start, sopno stop, states bef, wint_t ch, states aft, int sflags);
113 #define MAX_RECURSION 100
116 #define BOLEOL (BOL-2)
117 #define NOTHING (BOL-3)
120 #define BADCHAR (BOL-6)
121 #define NONCHAR(c) ((c) <= OUT)
127 static void print(struct match *m, const char *caption, states st, int ch, FILE *d);
130 static void at(struct match *m, const char *title, const char *start, const char *stop, sopno startst, sopno stopst);
133 static const char *pchar(int ch);
139 /* ========= end header generated by ./mkh ========= */
142 #define SP(t, s, c) print(m, t, s, c, stdout)
143 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
144 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
146 #define SP(t, s, c) /* nothing */
147 #define AT(t, p1, p2, s1, s2) /* nothing */
148 #define NOTE(s) /* nothing */
152 * Given a multibyte string pointed to by start, step back nchar characters
153 * from current position pointed to by cur.
156 stepback(const char *start, const char *cur, int nchar)
164 return ((cur - nchar) > start ? cur - nchar : NULL);
167 for (wc = nchar; wc > 0; wc--) {
168 for (mbc = 1; mbc <= MB_CUR_MAX; mbc++) {
169 if ((ret - mbc) < start)
171 memset(&mbs, 0, sizeof(mbs));
172 clen = mbrtowc(NULL, ret - mbc, mbc, &mbs);
173 if (clen != (size_t)-1 && clen != (size_t)-2)
176 if (mbc > MB_CUR_MAX)
185 - matcher - the actual matching engine
186 == static int matcher(struct re_guts *g, const char *string, \
187 == size_t nmatch, regmatch_t pmatch[], int eflags);
189 static int /* 0 success, REG_NOMATCH failure */
190 matcher(struct re_guts *g,
199 struct match *m = &mv;
200 const char *dp = NULL;
201 const sopno gf = g->firststate+1; /* +1 for OEND */
202 const sopno gl = g->laststate;
205 /* Boyer-Moore algorithms variables */
208 const char *mustfirst;
209 const char *mustlast;
213 /* simplify the situation where possible */
214 if (g->cflags®_NOSUB)
216 if (eflags®_STARTEND) {
217 start = string + pmatch[0].rm_so;
218 stop = string + pmatch[0].rm_eo;
221 stop = start + strlen(start);
226 /* prescreening; this does wonders for this rather slow code */
227 if (g->must != NULL) {
228 if (g->charjump != NULL && g->matchjump != NULL) {
230 mustlast = g->must + g->mlen - 1;
231 charjump = g->charjump;
232 matchjump = g->matchjump;
234 for (dp = start+g->mlen-1; dp < stop;) {
235 /* Fast skip non-matches */
236 while (dp < stop && charjump[(int)*dp])
237 dp += charjump[(int)*dp];
243 /* We depend on not being used for
244 * for strings of length 1
246 while (*--dp == *--pp && pp != mustfirst);
251 /* Jump to next possible match */
252 mj = matchjump[pp - mustfirst];
253 cj = charjump[(int)*dp];
254 dp += (cj < mj ? mj : cj);
260 for (dp = start; dp < stop; dp++)
261 if (*dp == g->must[0] &&
262 stop - dp >= g->mlen &&
263 memcmp(dp, g->must, (size_t)g->mlen) == 0)
265 if (dp == stop) /* we didn't find g->must */
270 /* match struct setup */
286 /* Adjust start according to moffset, to speed things up */
287 if (dp != NULL && g->moffset > -1) {
290 nstart = stepback(start, dp, g->moffset);
295 SP("mloop", m->st, *start);
297 /* this loop does only one repetition except for backrefs */
299 endp = walk(m, start, stop, gf, gl, true);
300 if (endp == NULL) { /* a miss */
301 if (m->pmatch != NULL)
302 free((char *)m->pmatch);
303 if (m->lastpos != NULL)
304 free((char *)m->lastpos);
308 if (nmatch == 0 && !g->backrefs)
309 break; /* no further info needed */
312 assert(m->coldp != NULL);
314 NOTE("finding start");
315 endp = walk(m, m->coldp, stop, gf, gl, false);
318 assert(m->coldp < m->endp);
319 m->coldp += XMBRTOWC(NULL, m->coldp,
320 m->endp - m->coldp, &m->mbs, 0);
322 if (nmatch == 1 && !g->backrefs)
323 break; /* no further info needed */
325 /* oh my, he wants the subexpressions... */
326 if (m->pmatch == NULL)
327 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
329 if (m->pmatch == NULL) {
333 for (i = 1; i <= m->g->nsub; i++)
334 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
335 if (!g->backrefs && !(m->eflags®_BACKR)) {
337 dp = dissect(m, m->coldp, endp, gf, gl);
339 if (g->nplus > 0 && m->lastpos == NULL)
340 m->lastpos = malloc((g->nplus+1) *
341 sizeof(const char *));
342 if (g->nplus > 0 && m->lastpos == NULL) {
347 NOTE("backref dissect");
348 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
353 /* uh-oh... we couldn't find a subexpression-level match */
354 assert(g->backrefs); /* must be back references doing it */
355 assert(g->nplus == 0 || m->lastpos != NULL);
357 if (dp != NULL || endp <= m->coldp)
360 endp = walk(m, m->coldp, endp-1, gf, gl, false);
363 /* try it on a shorter possibility */
365 for (i = 1; i <= m->g->nsub; i++) {
366 assert(m->pmatch[i].rm_so == -1);
367 assert(m->pmatch[i].rm_eo == -1);
370 NOTE("backoff dissect");
371 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
373 assert(dp == NULL || dp == endp);
374 if (dp != NULL) /* found a shorter one */
377 /* despite initial appearances, there is no match here */
379 /* recycle starting later */
380 start = m->coldp + XMBRTOWC(NULL, m->coldp,
381 stop - m->coldp, &m->mbs, 0);
382 assert(start <= stop);
385 /* fill in the details if requested */
387 pmatch[0].rm_so = m->coldp - m->offp;
388 pmatch[0].rm_eo = endp - m->offp;
391 assert(m->pmatch != NULL);
392 for (i = 1; i < nmatch; i++)
394 pmatch[i] = m->pmatch[i];
396 pmatch[i].rm_so = -1;
397 pmatch[i].rm_eo = -1;
401 if (m->pmatch != NULL)
402 free((char *)m->pmatch);
403 if (m->lastpos != NULL)
404 free((char *)m->lastpos);
410 - dissect - figure out what matched what, no back references
411 == static const char *dissect(struct match *m, const char *start, \
412 == const char *stop, sopno startst, sopno stopst);
414 static const char * /* == stop (success) always */
415 dissect(struct match *m,
422 sopno ss; /* start sop of current subRE */
423 sopno es; /* end sop of current subRE */
424 const char *sp; /* start of string matched by it */
425 const char *stp; /* string matched by it cannot pass here */
426 const char *rest; /* start of rest of string */
427 const char *tail; /* string unmatched by rest of RE */
428 sopno ssub; /* start sop of subsubRE */
429 sopno esub; /* end sop of subsubRE */
430 const char *ssp; /* start of string matched by subsubRE */
431 const char *sep; /* end of string matched by subsubRE */
432 const char *oldssp; /* previous ssp */
433 const char *dp __unused;
435 AT("diss", start, stop, startst, stopst);
437 for (ss = startst; ss < stopst; ss = es) {
438 /* identify end of subRE */
440 switch (OP(m->g->strip[es])) {
443 es += OPND(m->g->strip[es]);
446 while (OP(m->g->strip[es]) != (sop)O_CH)
447 es += OPND(m->g->strip[es]);
452 /* figure out what it matched */
453 switch (OP(m->g->strip[ss])) {
458 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0);
469 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0);
475 /* cases where length of match is hard to find */
479 /* how long could this one be? */
480 rest = walk(m, sp, stp, ss, es, false);
481 assert(rest != NULL); /* it did match */
482 /* could the rest match the rest? */
483 tail = walk(m, rest, stop, es, stopst, false);
486 /* no -- try a shorter match for this one */
488 assert(stp >= sp); /* it did work */
492 /* did innards match? */
493 if (walk(m, sp, rest, ssub, esub, false) != NULL) {
494 dp = dissect(m, sp, rest, ssub, esub);
503 /* how long could this one be? */
504 rest = walk(m, sp, stp, ss, es, false);
505 assert(rest != NULL); /* it did match */
506 /* could the rest match the rest? */
507 tail = walk(m, rest, stop, es, stopst, false);
510 /* no -- try a shorter match for this one */
512 assert(stp >= sp); /* it did work */
518 for (;;) { /* find last match of innards */
519 sep = walk(m, ssp, rest, ssub, esub, false);
520 if (sep == NULL || sep == ssp)
521 break; /* failed or matched null */
522 oldssp = ssp; /* on to next try */
526 /* last successful match */
530 assert(sep == rest); /* must exhaust substring */
531 assert(walk(m, ssp, sep, ssub, esub, false) == rest);
532 dp = dissect(m, ssp, sep, ssub, esub);
539 /* how long could this one be? */
540 rest = walk(m, sp, stp, ss, es, false);
541 assert(rest != NULL); /* it did match */
542 /* could the rest match the rest? */
543 tail = walk(m, rest, stop, es, stopst, false);
546 /* no -- try a shorter match for this one */
548 assert(stp >= sp); /* it did work */
551 esub = ss + OPND(m->g->strip[ss]) - 1;
552 assert(OP(m->g->strip[esub]) == OOR1);
553 for (;;) { /* find first matching branch */
554 if (walk(m, sp, rest, ssub, esub, false) == rest)
555 break; /* it matched all of it */
556 /* that one missed, try next one */
557 assert(OP(m->g->strip[esub]) == OOR1);
559 assert(OP(m->g->strip[esub]) == OOR2);
561 esub += OPND(m->g->strip[esub]);
562 if (OP(m->g->strip[esub]) == (sop)OOR2)
565 assert(OP(m->g->strip[esub]) == O_CH);
567 dp = dissect(m, sp, rest, ssub, esub);
579 i = OPND(m->g->strip[ss]);
580 assert(0 < i && i <= m->g->nsub);
581 m->pmatch[i].rm_so = sp - m->offp;
584 i = OPND(m->g->strip[ss]);
585 assert(0 < i && i <= m->g->nsub);
586 m->pmatch[i].rm_eo = sp - m->offp;
598 #define ISBOW(m, sp) \
599 (sp < m->endp && ISWORD(*sp) && \
600 ((sp == m->beginp && !(m->eflags®_NOTBOL)) || \
601 (sp > m->offp && !ISWORD(*(sp-1)))))
602 #define ISEOW(m, sp) \
603 (((sp == m->endp && !(m->eflags®_NOTEOL)) || \
604 (sp < m->endp && *sp == '\n' && \
605 (m->g->cflags®_NEWLINE)) || \
606 (sp < m->endp && !ISWORD(*sp)) ) && \
607 (sp > m->beginp && ISWORD(*(sp-1)))) \
610 - backref - figure out what matched what, figuring in back references
611 == static const char *backref(struct match *m, const char *start, \
612 == const char *stop, sopno startst, sopno stopst, sopno lev);
614 static const char * /* == stop (success) or NULL (failure) */
615 backref(struct match *m,
620 sopno lev, /* PLUS nesting level */
624 sopno ss; /* start sop of current subRE */
625 const char *sp; /* start of string matched by it */
626 sopno ssub; /* start sop of subsubRE */
627 sopno esub; /* end sop of subsubRE */
628 const char *ssp; /* start of string matched by subsubRE */
637 AT("back", start, stop, startst, stopst);
640 /* get as far as we can with easy stuff */
642 for (ss = startst; !hard && ss < stopst; ss++)
643 switch (OP(s = m->g->strip[ss])) {
647 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
654 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
661 cs = &m->g->sets[OPND(s)];
662 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR);
663 if (wc == BADCHAR || !CHIN(cs, wc))
667 if (sp == m->beginp && (m->eflags & REG_NOTBOL) == 0)
673 if (sp == m->endp && (m->eflags & REG_NOTEOL) == 0)
679 if ((sp == m->beginp && !(m->eflags®_NOTBOL)) ||
680 (sp > m->offp && sp < m->endp &&
681 *(sp-1) == '\n' && (m->g->cflags®_NEWLINE)))
687 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
688 (sp < m->endp && *sp == '\n' &&
689 (m->g->cflags®_NEWLINE)) )
708 case OOR1: /* matches null but needs to skip */
712 assert(OP(s) == OOR2);
714 } while (OP(s = m->g->strip[ss]) != (sop)O_CH);
715 /* note that the ss++ gets us past the O_CH */
717 default: /* have to make a choice */
721 if (!hard) { /* that was it! */
726 ss--; /* adjust for the for's final increment */
729 AT("hard", sp, stop, ss, stopst);
732 case OBACK_: /* the vilest depths */
734 assert(0 < i && i <= m->g->nsub);
735 if (m->pmatch[i].rm_eo == -1)
737 assert(m->pmatch[i].rm_so != -1);
738 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
739 if (len == 0 && rec++ > MAX_RECURSION)
741 assert(stop - m->beginp >= len);
743 return(NULL); /* not enough left to match */
744 ssp = m->offp + m->pmatch[i].rm_so;
745 if (memcmp(sp, ssp, len) != 0)
747 while (m->g->strip[ss] != (sop)SOP(O_BACK, i))
749 return(backref(m, sp+len, stop, ss+1, stopst, lev, rec));
750 case OQUEST_: /* to null or not */
751 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
753 return(dp); /* not */
754 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev, rec));
756 assert(m->lastpos != NULL);
757 assert(lev+1 <= m->g->nplus);
758 m->lastpos[lev+1] = sp;
759 return(backref(m, sp, stop, ss+1, stopst, lev+1, rec));
761 if (sp == m->lastpos[lev]) /* last pass matched null */
762 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
763 /* try another pass */
764 m->lastpos[lev] = sp;
765 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev, rec);
767 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
770 case OCH_: /* find the right one, if any */
772 esub = ss + OPND(s) - 1;
773 assert(OP(m->g->strip[esub]) == OOR1);
774 for (;;) { /* find first matching branch */
775 dp = backref(m, sp, stop, ssub, esub, lev, rec);
778 /* that one missed, try next one */
779 if (OP(m->g->strip[esub]) == (sop)O_CH)
780 return(NULL); /* there is none */
782 assert(OP(m->g->strip[esub]) == (sop)OOR2);
784 esub += OPND(m->g->strip[esub]);
785 if (OP(m->g->strip[esub]) == (sop)OOR2)
788 assert(OP(m->g->strip[esub]) == O_CH);
792 case OLPAREN: /* must undo assignment if rest fails */
794 assert(0 < i && i <= m->g->nsub);
795 offsave = m->pmatch[i].rm_so;
796 m->pmatch[i].rm_so = sp - m->offp;
797 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
800 m->pmatch[i].rm_so = offsave;
802 case ORPAREN: /* must undo assignment if rest fails */
804 assert(0 < i && i <= m->g->nsub);
805 offsave = m->pmatch[i].rm_eo;
806 m->pmatch[i].rm_eo = sp - m->offp;
807 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
810 m->pmatch[i].rm_eo = offsave;
820 return "shut up gcc";
824 - walk - step through the string either quickly or slowly
825 == static const char *walk(struct match *m, const char *start, \
826 == const char *stop, sopno startst, sopno stopst, bool fast);
828 static const char * /* where it ended, or NULL */
829 walk(struct match *m, const char *start, const char *stop, sopno startst,
830 sopno stopst, bool fast)
833 states fresh = m->fresh;
834 states empty = m->empty;
836 const char *p = start;
838 wint_t lastc; /* previous c */
841 const char *matchp; /* last p at which a match ended */
845 AT("slow", start, stop, startst, stopst);
848 SP("sstart", st, *p);
849 st = step(m->g, startst, stopst, st, NOTHING, st, sflags);
853 if (start == m->offp || (start == m->beginp && !(m->eflags®_NOTBOL)))
857 * XXX Wrong if the previous character was multi-byte.
858 * Newline never is (in encodings supported by FreeBSD),
859 * so this only breaks the ISWORD tests below.
861 c = (uch)*(start - 1);
871 clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR);
873 if (fast && EQ(st, fresh))
876 /* is there an EOL and/or BOL between lastc and c? */
879 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
880 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
884 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
885 (c == OUT && !(m->eflags®_NOTEOL)) ) {
886 flagch = (flagch == BOL) ? BOLEOL : EOL;
889 if (lastc == OUT && (m->eflags & REG_NOTBOL) == 0) {
891 /* Step one more for BOS. */
894 if (c == OUT && (m->eflags & REG_NOTEOL) == 0) {
896 /* Step one more for EOS. */
901 st = step(m->g, startst, stopst, st, flagch, st,
903 SP("sboleol", st, c);
906 /* how about a word boundary? */
907 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
908 (c != OUT && ISWORD(c)) ) {
911 if ( (lastc != OUT && ISWORD(lastc)) &&
912 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
915 if (flagch == BOW || flagch == EOW) {
916 st = step(m->g, startst, stopst, st, flagch, st, sflags);
917 SP("sboweow", st, c);
921 if (ISSET(st, stopst)) {
927 if (EQ(st, empty) || p == stop || clen > (size_t)(stop - p))
928 break; /* NOTE BREAK OUT */
930 /* no, we must deal with this character */
937 st = step(m->g, startst, stopst, tmp, c, st, sflags);
939 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st, sflags),
945 assert(matchp != NULL);
947 if (ISSET(st, stopst))
948 return (p + XMBRTOWC(NULL, p, stop - p, &m->mbs, 0));
956 - step - map set of states reachable before char to set reachable after
957 == static states step(struct re_guts *g, sopno start, sopno stop, \
958 == states bef, int ch, states aft);
959 == #define BOL (OUT-1)
960 == #define EOL (BOL-1)
961 == #define BOLEOL (BOL-2)
962 == #define NOTHING (BOL-3)
963 == #define BOW (BOL-4)
964 == #define EOW (BOL-5)
965 == #define BADCHAR (BOL-6)
966 == #define NONCHAR(c) ((c) <= OUT)
969 step(struct re_guts *g,
970 sopno start, /* start state within strip */
971 sopno stop, /* state after stop state within strip */
972 states bef, /* states reachable before */
973 wint_t ch, /* character or NONCHAR code */
974 states aft, /* states already known reachable after */
975 int sflags) /* state flags */
980 onestate here; /* note, macros know this name */
984 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
988 assert(pc == stop-1);
991 /* only characters can match */
992 assert(!NONCHAR(ch) || ch != OPND(s));
997 if ((ch == BOL || ch == BOLEOL) && (sflags & SBOS) != 0)
1001 if ((ch == EOL || ch == BOLEOL) && (sflags & SEOS) != 0)
1005 if (ch == BOL || ch == BOLEOL)
1009 if (ch == EOL || ch == BOLEOL)
1025 cs = &g->sets[OPND(s)];
1026 if (!NONCHAR(ch) && CHIN(cs, ch))
1029 case OBACK_: /* ignored here */
1033 case OPLUS_: /* forward, this is just an empty */
1036 case O_PLUS: /* both forward and back */
1038 i = ISSETBACK(aft, OPND(s));
1039 BACK(aft, aft, OPND(s));
1040 if (!i && ISSETBACK(aft, OPND(s))) {
1041 /* oho, must reconsider loop body */
1046 case OQUEST_: /* two branches, both forward */
1048 FWD(aft, aft, OPND(s));
1050 case O_QUEST: /* just an empty */
1053 case OLPAREN: /* not significant here */
1057 case OCH_: /* mark the first two branches */
1059 assert(OP(g->strip[pc+OPND(s)]) == (sop)OOR2);
1060 FWD(aft, aft, OPND(s));
1062 case OOR1: /* done a branch, find the O_CH */
1063 if (ISSTATEIN(aft, here)) {
1065 OP(s = g->strip[pc+look]) != (sop)O_CH;
1067 assert(OP(s) == (sop)OOR2);
1068 FWD(aft, aft, look + 1);
1071 case OOR2: /* propagate OCH_'s marking */
1073 if (OP(g->strip[pc+OPND(s)]) != (sop)O_CH) {
1074 assert(OP(g->strip[pc+OPND(s)]) == (sop)OOR2);
1075 FWD(aft, aft, OPND(s));
1078 case O_CH: /* just empty */
1081 default: /* ooooops... */
1092 - print - print a set of states
1094 == static void print(struct match *m, const char *caption, states st, \
1095 == int ch, FILE *d);
1099 print(struct match *m,
1100 const char *caption,
1105 struct re_guts *g = m->g;
1109 if (!(m->eflags®_TRACE))
1112 fprintf(d, "%s", caption);
1114 fprintf(d, " %s", pchar(ch));
1115 for (i = 0; i < g->nstates; i++)
1117 fprintf(d, "%s%lu", (first) ? "\t" : ", ", i);
1124 - at - print current situation
1126 == static void at(struct match *m, const char *title, const char *start, \
1127 == const char *stop, sopno startst, sopno stopst);
1131 at( struct match *m,
1138 if (!(m->eflags®_TRACE))
1141 printf("%s %s-", title, pchar(*start));
1142 printf("%s ", pchar(*stop));
1143 printf("%ld-%ld\n", (long)startst, (long)stopst);
1147 #define PCHARDONE /* never again */
1149 - pchar - make a character printable
1151 == static const char *pchar(int ch);
1154 * Is this identical to regchar() over in debug.c? Well, yes. But a
1155 * duplicate here avoids having a debugging-capable regexec.o tied to
1156 * a matching debug.o, and this is convenient. It all disappears in
1157 * the non-debug compilation anyway, so it doesn't matter much.
1159 static const char * /* -> representation */
1162 static char pbuf[10];
1164 if (isprint((uch)ch) || ch == ' ')
1165 sprintf(pbuf, "%c", ch);
1167 sprintf(pbuf, "\\%o", ch);