2 * This code is derived from OpenBSD's libc/regex, original license follows:
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
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
58 #define matcher lmatcher
61 #define dissect ldissect
62 #define backref lbackref
70 /* another structure passed up and down to avoid zillions of parameters */
74 llvm_regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
75 char *offp; /* offsets work from here */
76 char *beginp; /* start of string -- virtual NUL precedes */
77 char *endp; /* end of string -- virtual NUL here */
78 char *coldp; /* can be no match starting before here */
79 char **lastpos; /* [nplus+1] */
81 states st; /* current states */
82 states fresh; /* states for a fresh start */
83 states tmp; /* temporary */
84 states empty; /* empty set of states */
87 static int matcher(struct re_guts *, char *, size_t, llvm_regmatch_t[], int);
88 static char *dissect(struct match *, char *, char *, sopno, sopno);
89 static char *backref(struct match *, char *, char *, sopno, sopno, sopno, int);
90 static char *fast(struct match *, char *, char *, sopno, sopno);
91 static char *slow(struct match *, char *, char *, sopno, sopno);
92 static states step(struct re_guts *, sopno, sopno, states, int, states);
93 #define MAX_RECURSION 100
96 #define BOLEOL (BOL+2)
97 #define NOTHING (BOL+3)
100 #define CODEMAX (BOL+5) /* highest code used */
101 #define NONCHAR(c) ((c) > CHAR_MAX)
102 #define NNONCHAR (CODEMAX-CHAR_MAX)
104 static void print(struct match *, char *, states, int, FILE *);
107 static void at(struct match *, char *, char *, char *, sopno, sopno);
110 static char *pchar(int);
114 #define SP(t, s, c) print(m, t, s, c, stdout)
115 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
116 #define NOTE(str) { if (m->eflags®_TRACE) (void)printf("=%s\n", (str)); }
119 #define SP(t, s, c) /* nothing */
120 #define AT(t, p1, p2, s1, s2) /* nothing */
121 #define NOTE(s) /* nothing */
125 - matcher - the actual matching engine
127 static int /* 0 success, REG_NOMATCH failure */
128 matcher(struct re_guts *g, char *string, size_t nmatch, llvm_regmatch_t pmatch[],
134 struct match *m = &mv;
136 const sopno gf = g->firststate+1; /* +1 for OEND */
137 const sopno gl = g->laststate;
141 /* simplify the situation where possible */
142 if (g->cflags®_NOSUB)
144 if (eflags®_STARTEND) {
145 start = string + pmatch[0].rm_so;
146 stop = string + pmatch[0].rm_eo;
149 stop = start + strlen(start);
154 /* prescreening; this does wonders for this rather slow code */
155 if (g->must != NULL) {
156 for (dp = start; dp < stop; dp++)
157 if (*dp == g->must[0] && stop - dp >= g->mlen &&
158 memcmp(dp, g->must, (size_t)g->mlen) == 0)
160 if (dp == stop) /* we didn't find g->must */
164 /* match struct setup */
179 /* this loop does only one repetition except for backrefs */
181 endp = fast(m, start, stop, gf, gl);
182 if (endp == NULL) { /* a miss */
188 if (nmatch == 0 && !g->backrefs)
189 break; /* no further info needed */
192 assert(m->coldp != NULL);
194 NOTE("finding start");
195 endp = slow(m, m->coldp, stop, gf, gl);
198 assert(m->coldp < m->endp);
201 if (nmatch == 1 && !g->backrefs)
202 break; /* no further info needed */
204 /* oh my, he wants the subexpressions... */
205 if (m->pmatch == NULL)
206 m->pmatch = (llvm_regmatch_t *)malloc((m->g->nsub + 1) *
207 sizeof(llvm_regmatch_t));
208 if (m->pmatch == NULL) {
212 for (i = 1; i <= m->g->nsub; i++)
213 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
214 if (!g->backrefs && !(m->eflags®_BACKR)) {
216 dp = dissect(m, m->coldp, endp, gf, gl);
218 if (g->nplus > 0 && m->lastpos == NULL)
219 m->lastpos = (char **)malloc((g->nplus+1) *
221 if (g->nplus > 0 && m->lastpos == NULL) {
226 NOTE("backref dissect");
227 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
232 /* uh-oh... we couldn't find a subexpression-level match */
233 assert(g->backrefs); /* must be back references doing it */
234 assert(g->nplus == 0 || m->lastpos != NULL);
236 if (dp != NULL || endp <= m->coldp)
239 endp = slow(m, m->coldp, endp-1, gf, gl);
242 /* try it on a shorter possibility */
244 for (i = 1; i <= m->g->nsub; i++) {
245 assert(m->pmatch[i].rm_so == -1);
246 assert(m->pmatch[i].rm_eo == -1);
249 NOTE("backoff dissect");
250 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
252 assert(dp == NULL || dp == endp);
253 if (dp != NULL) /* found a shorter one */
256 /* despite initial appearances, there is no match here */
258 if (m->coldp == stop)
260 start = m->coldp + 1; /* recycle starting later */
263 /* fill in the details if requested */
265 pmatch[0].rm_so = m->coldp - m->offp;
266 pmatch[0].rm_eo = endp - m->offp;
269 assert(m->pmatch != NULL);
270 for (i = 1; i < nmatch; i++)
272 pmatch[i] = m->pmatch[i];
274 pmatch[i].rm_so = -1;
275 pmatch[i].rm_eo = -1;
279 if (m->pmatch != NULL)
280 free((char *)m->pmatch);
281 if (m->lastpos != NULL)
282 free((char *)m->lastpos);
288 - dissect - figure out what matched what, no back references
290 static char * /* == stop (success) always */
291 dissect(struct match *m, char *start, char *stop, sopno startst, sopno stopst)
294 sopno ss; /* start sop of current subRE */
295 sopno es; /* end sop of current subRE */
296 char *sp; /* start of string matched by it */
297 char *stp; /* string matched by it cannot pass here */
298 char *rest; /* start of rest of string */
299 char *tail; /* string unmatched by rest of RE */
300 sopno ssub; /* start sop of subsubRE */
301 sopno esub; /* end sop of subsubRE */
302 char *ssp; /* start of string matched by subsubRE */
303 char *sep; /* end of string matched by subsubRE */
304 char *oldssp; /* previous ssp */
307 AT("diss", start, stop, startst, stopst);
309 for (ss = startst; ss < stopst; ss = es) {
310 /* identify end of subRE */
312 switch (OP(m->g->strip[es])) {
315 es += OPND(m->g->strip[es]);
318 while (OP(m->g->strip[es]) != O_CH)
319 es += OPND(m->g->strip[es]);
324 /* figure out what it matched */
325 switch (OP(m->g->strip[ss])) {
345 /* cases where length of match is hard to find */
349 /* how long could this one be? */
350 rest = slow(m, sp, stp, ss, es);
351 assert(rest != NULL); /* it did match */
352 /* could the rest match the rest? */
353 tail = slow(m, rest, stop, es, stopst);
356 /* no -- try a shorter match for this one */
358 assert(stp >= sp); /* it did work */
362 /* did innards match? */
363 if (slow(m, sp, rest, ssub, esub) != NULL) {
364 dp = dissect(m, sp, rest, ssub, esub);
373 /* how long could this one be? */
374 rest = slow(m, sp, stp, ss, es);
375 assert(rest != NULL); /* it did match */
376 /* could the rest match the rest? */
377 tail = slow(m, rest, stop, es, stopst);
380 /* no -- try a shorter match for this one */
382 assert(stp >= sp); /* it did work */
388 for (;;) { /* find last match of innards */
389 sep = slow(m, ssp, rest, ssub, esub);
390 if (sep == NULL || sep == ssp)
391 break; /* failed or matched null */
392 oldssp = ssp; /* on to next try */
396 /* last successful match */
400 assert(sep == rest); /* must exhaust substring */
401 assert(slow(m, ssp, sep, ssub, esub) == rest);
402 dp = dissect(m, ssp, sep, ssub, esub);
409 /* how long could this one be? */
410 rest = slow(m, sp, stp, ss, es);
411 assert(rest != NULL); /* it did match */
412 /* could the rest match the rest? */
413 tail = slow(m, rest, stop, es, stopst);
416 /* no -- try a shorter match for this one */
418 assert(stp >= sp); /* it did work */
421 esub = ss + OPND(m->g->strip[ss]) - 1;
422 assert(OP(m->g->strip[esub]) == OOR1);
423 for (;;) { /* find first matching branch */
424 if (slow(m, sp, rest, ssub, esub) == rest)
425 break; /* it matched all of it */
426 /* that one missed, try next one */
427 assert(OP(m->g->strip[esub]) == OOR1);
429 assert(OP(m->g->strip[esub]) == OOR2);
431 esub += OPND(m->g->strip[esub]);
432 if (OP(m->g->strip[esub]) == OOR2)
435 assert(OP(m->g->strip[esub]) == O_CH);
437 dp = dissect(m, sp, rest, ssub, esub);
449 i = OPND(m->g->strip[ss]);
450 assert(0 < i && i <= m->g->nsub);
451 m->pmatch[i].rm_so = sp - m->offp;
454 i = OPND(m->g->strip[ss]);
455 assert(0 < i && i <= m->g->nsub);
456 m->pmatch[i].rm_eo = sp - m->offp;
469 - backref - figure out what matched what, figuring in back references
471 static char * /* == stop (success) or NULL (failure) */
472 backref(struct match *m, char *start, char *stop, sopno startst, sopno stopst,
473 sopno lev, int rec) /* PLUS nesting level */
476 sopno ss; /* start sop of current subRE */
477 char *sp; /* start of string matched by it */
478 sopno ssub; /* start sop of subsubRE */
479 sopno esub; /* end sop of subsubRE */
480 char *ssp; /* start of string matched by subsubRE */
485 llvm_regoff_t offsave;
488 AT("back", start, stop, startst, stopst);
491 /* get as far as we can with easy stuff */
493 for (ss = startst; !hard && ss < stopst; ss++)
494 switch (OP(s = m->g->strip[ss])) {
496 if (sp == stop || *sp++ != (char)OPND(s))
505 cs = &m->g->sets[OPND(s)];
506 if (sp == stop || !CHIN(cs, *sp++))
510 if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
511 (sp < m->endp && *(sp-1) == '\n' &&
512 (m->g->cflags®_NEWLINE)) )
518 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
519 (sp < m->endp && *sp == '\n' &&
520 (m->g->cflags®_NEWLINE)) )
526 if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
527 (sp < m->endp && *(sp-1) == '\n' &&
528 (m->g->cflags®_NEWLINE)) ||
530 !ISWORD(*(sp-1))) ) &&
531 (sp < m->endp && ISWORD(*sp)) )
537 if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
538 (sp < m->endp && *sp == '\n' &&
539 (m->g->cflags®_NEWLINE)) ||
540 (sp < m->endp && !ISWORD(*sp)) ) &&
541 (sp > m->beginp && ISWORD(*(sp-1))) )
548 case OOR1: /* matches null but needs to skip */
552 assert(OP(s) == OOR2);
554 } while (OP(s = m->g->strip[ss]) != O_CH);
555 /* note that the ss++ gets us past the O_CH */
557 default: /* have to make a choice */
561 if (!hard) { /* that was it! */
566 ss--; /* adjust for the for's final increment */
569 AT("hard", sp, stop, ss, stopst);
572 case OBACK_: /* the vilest depths */
574 assert(0 < i && i <= m->g->nsub);
575 if (m->pmatch[i].rm_eo == -1)
577 assert(m->pmatch[i].rm_so != -1);
578 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
579 if (len == 0 && rec++ > MAX_RECURSION)
581 assert(stop - m->beginp >= len);
583 return(NULL); /* not enough left to match */
584 ssp = m->offp + m->pmatch[i].rm_so;
585 if (memcmp(sp, ssp, len) != 0)
587 while (m->g->strip[ss] != SOP(O_BACK, i))
589 return(backref(m, sp+len, stop, ss+1, stopst, lev, rec));
591 case OQUEST_: /* to null or not */
592 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
594 return(dp); /* not */
595 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev, rec));
598 assert(m->lastpos != NULL);
599 assert(lev+1 <= m->g->nplus);
600 m->lastpos[lev+1] = sp;
601 return(backref(m, sp, stop, ss+1, stopst, lev+1, rec));
604 if (sp == m->lastpos[lev]) /* last pass matched null */
605 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
606 /* try another pass */
607 m->lastpos[lev] = sp;
608 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev, rec);
610 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
614 case OCH_: /* find the right one, if any */
616 esub = ss + OPND(s) - 1;
617 assert(OP(m->g->strip[esub]) == OOR1);
618 for (;;) { /* find first matching branch */
619 dp = backref(m, sp, stop, ssub, esub, lev, rec);
622 /* that one missed, try next one */
623 if (OP(m->g->strip[esub]) == O_CH)
624 return(NULL); /* there is none */
626 assert(OP(m->g->strip[esub]) == OOR2);
628 esub += OPND(m->g->strip[esub]);
629 if (OP(m->g->strip[esub]) == OOR2)
632 assert(OP(m->g->strip[esub]) == O_CH);
635 case OLPAREN: /* must undo assignment if rest fails */
637 assert(0 < i && i <= m->g->nsub);
638 offsave = m->pmatch[i].rm_so;
639 m->pmatch[i].rm_so = sp - m->offp;
640 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
643 m->pmatch[i].rm_so = offsave;
646 case ORPAREN: /* must undo assignment if rest fails */
648 assert(0 < i && i <= m->g->nsub);
649 offsave = m->pmatch[i].rm_eo;
650 m->pmatch[i].rm_eo = sp - m->offp;
651 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
654 m->pmatch[i].rm_eo = offsave;
669 - fast - step through the string at top speed
671 static char * /* where tentative match ended, or NULL */
672 fast(struct match *m, char *start, char *stop, sopno startst, sopno stopst)
675 states fresh = m->fresh;
678 int c = (start == m->beginp) ? OUT : *(start-1);
679 int lastc; /* previous c */
682 char *coldp; /* last p after which no match was underway */
686 st = step(m->g, startst, stopst, st, NOTHING, st);
693 c = (p == m->endp) ? OUT : *p;
697 /* is there an EOL and/or BOL between lastc and c? */
700 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
701 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
705 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
706 (c == OUT && !(m->eflags®_NOTEOL)) ) {
707 flagch = (flagch == BOL) ? BOLEOL : EOL;
712 st = step(m->g, startst, stopst, st, flagch, st);
716 /* how about a word boundary? */
717 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
718 (c != OUT && ISWORD(c)) ) {
721 if ( (lastc != OUT && ISWORD(lastc)) &&
722 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
725 if (flagch == BOW || flagch == EOW) {
726 st = step(m->g, startst, stopst, st, flagch, st);
731 if (ISSET(st, stopst) || p == stop)
732 break; /* NOTE BREAK OUT */
734 /* no, we must deal with this character */
738 st = step(m->g, startst, stopst, tmp, c, st);
740 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
744 assert(coldp != NULL);
746 if (ISSET(st, stopst))
753 - slow - step through the string more deliberately
755 static char * /* where it ended */
756 slow(struct match *m, char *start, char *stop, sopno startst, sopno stopst)
759 states empty = m->empty;
762 int c = (start == m->beginp) ? OUT : *(start-1);
763 int lastc; /* previous c */
766 char *matchp; /* last p at which a match ended */
768 AT("slow", start, stop, startst, stopst);
771 SP("sstart", st, *p);
772 st = step(m->g, startst, stopst, st, NOTHING, st);
777 c = (p == m->endp) ? OUT : *p;
779 /* is there an EOL and/or BOL between lastc and c? */
782 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
783 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
787 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
788 (c == OUT && !(m->eflags®_NOTEOL)) ) {
789 flagch = (flagch == BOL) ? BOLEOL : EOL;
794 st = step(m->g, startst, stopst, st, flagch, st);
795 SP("sboleol", st, c);
798 /* how about a word boundary? */
799 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
800 (c != OUT && ISWORD(c)) ) {
803 if ( (lastc != OUT && ISWORD(lastc)) &&
804 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
807 if (flagch == BOW || flagch == EOW) {
808 st = step(m->g, startst, stopst, st, flagch, st);
809 SP("sboweow", st, c);
813 if (ISSET(st, stopst))
815 if (EQ(st, empty) || p == stop)
816 break; /* NOTE BREAK OUT */
818 /* no, we must deal with this character */
822 st = step(m->g, startst, stopst, tmp, c, st);
824 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
833 - step - map set of states reachable before char to set reachable after
836 step(struct re_guts *g,
837 sopno start, /* start state within strip */
838 sopno stop, /* state after stop state within strip */
839 states bef, /* states reachable before */
840 int ch, /* character or NONCHAR code */
841 states aft) /* states already known reachable after */
846 onestate here; /* note, macros know this name */
850 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
854 assert(pc == stop-1);
857 /* only characters can match */
858 assert(!NONCHAR(ch) || ch != (char)OPND(s));
859 if (ch == (char)OPND(s))
863 if (ch == BOL || ch == BOLEOL)
867 if (ch == EOL || ch == BOLEOL)
883 cs = &g->sets[OPND(s)];
884 if (!NONCHAR(ch) && CHIN(cs, ch))
887 case OBACK_: /* ignored here */
891 case OPLUS_: /* forward, this is just an empty */
894 case O_PLUS: /* both forward and back */
896 i = ISSETBACK(aft, OPND(s));
897 BACK(aft, aft, OPND(s));
898 if (!i && ISSETBACK(aft, OPND(s))) {
899 /* oho, must reconsider loop body */
904 case OQUEST_: /* two branches, both forward */
906 FWD(aft, aft, OPND(s));
908 case O_QUEST: /* just an empty */
911 case OLPAREN: /* not significant here */
915 case OCH_: /* mark the first two branches */
917 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
918 FWD(aft, aft, OPND(s));
920 case OOR1: /* done a branch, find the O_CH */
921 if (ISSTATEIN(aft, here)) {
923 OP(s = g->strip[pc+look]) != O_CH;
925 assert(OP(s) == OOR2);
929 case OOR2: /* propagate OCH_'s marking */
931 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
932 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
933 FWD(aft, aft, OPND(s));
936 case O_CH: /* just empty */
939 default: /* ooooops... */
950 - print - print a set of states
953 print(struct match *m, char *caption, states st, int ch, FILE *d)
955 struct re_guts *g = m->g;
959 if (!(m->eflags®_TRACE))
962 (void)fprintf(d, "%s", caption);
964 (void)fprintf(d, " %s", pchar(ch));
965 for (i = 0; i < g->nstates; i++)
967 (void)fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
970 (void)fprintf(d, "\n");
974 - at - print current situation
977 at(struct match *m, char *title, char *start, char *stop, sopno startst,
980 if (!(m->eflags®_TRACE))
983 (void)printf("%s %s-", title, pchar(*start));
984 (void)printf("%s ", pchar(*stop));
985 (void)printf("%ld-%ld\n", (long)startst, (long)stopst);
989 #define PCHARDONE /* never again */
991 - pchar - make a character printable
993 * Is this identical to regchar() over in debug.c? Well, yes. But a
994 * duplicate here avoids having a debugging-capable regexec.o tied to
995 * a matching debug.o, and this is convenient. It all disappears in
996 * the non-debug compilation anyway, so it doesn't matter much.
998 static char * /* -> representation */
1001 static char pbuf[10];
1003 if (isprint(ch) || ch == ' ')
1004 (void)snprintf(pbuf, sizeof pbuf, "%c", ch);
1006 (void)snprintf(pbuf, sizeof pbuf, "\\%o", ch);