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 const char *offp; /* offsets work from here */
76 const char *beginp; /* start of string -- virtual NUL precedes */
77 const char *endp; /* end of string -- virtual NUL here */
78 const char *coldp; /* can be no match starting before here */
79 const 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 *, const char *, size_t,
88 llvm_regmatch_t[], int);
89 static const char *dissect(struct match *, const char *, const char *, sopno,
91 static const char *backref(struct match *, const char *, const char *, sopno,
93 static const char *fast(struct match *, const char *, const char *, sopno, sopno);
94 static const char *slow(struct match *, const char *, const char *, sopno, sopno);
95 static states step(struct re_guts *, sopno, sopno, states, int, states);
96 #define MAX_RECURSION 100
99 #define BOLEOL (BOL+2)
100 #define NOTHING (BOL+3)
103 #define CODEMAX (BOL+5) /* highest code used */
104 #define NONCHAR(c) ((c) > CHAR_MAX)
105 #define NNONCHAR (CODEMAX-CHAR_MAX)
107 static void print(struct match *, char *, states, int, FILE *);
110 static void at(struct match *, char *, char *, char *, sopno, sopno);
113 static char *pchar(int);
117 #define SP(t, s, c) print(m, t, s, c, stdout)
118 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
119 #define NOTE(str) { if (m->eflags®_TRACE) (void)printf("=%s\n", (str)); }
122 #define SP(t, s, c) /* nothing */
123 #define AT(t, p1, p2, s1, s2) /* nothing */
124 #define NOTE(s) /* nothing */
128 - matcher - the actual matching engine
130 static int /* 0 success, REG_NOMATCH failure */
131 matcher(struct re_guts *g, const char *string, size_t nmatch,
132 llvm_regmatch_t pmatch[],
138 struct match *m = &mv;
140 const sopno gf = g->firststate+1; /* +1 for OEND */
141 const sopno gl = g->laststate;
145 /* simplify the situation where possible */
146 if (g->cflags®_NOSUB)
148 if (eflags®_STARTEND) {
149 start = string + pmatch[0].rm_so;
150 stop = string + pmatch[0].rm_eo;
153 stop = start + strlen(start);
158 /* prescreening; this does wonders for this rather slow code */
159 if (g->must != NULL) {
160 for (dp = start; dp < stop; dp++)
161 if (*dp == g->must[0] && stop - dp >= g->mlen &&
162 memcmp(dp, g->must, (size_t)g->mlen) == 0)
164 if (dp == stop) /* we didn't find g->must */
168 /* match struct setup */
183 /* this loop does only one repetition except for backrefs */
185 endp = fast(m, start, stop, gf, gl);
186 if (endp == NULL) { /* a miss */
188 free((void*)m->lastpos);
192 if (nmatch == 0 && !g->backrefs)
193 break; /* no further info needed */
196 assert(m->coldp != NULL);
198 NOTE("finding start");
199 endp = slow(m, m->coldp, stop, gf, gl);
202 assert(m->coldp < m->endp);
205 if (nmatch == 1 && !g->backrefs)
206 break; /* no further info needed */
208 /* oh my, he wants the subexpressions... */
209 if (m->pmatch == NULL)
210 m->pmatch = (llvm_regmatch_t *)malloc((m->g->nsub + 1) *
211 sizeof(llvm_regmatch_t));
212 if (m->pmatch == NULL) {
216 for (i = 1; i <= m->g->nsub; i++)
217 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
218 if (!g->backrefs && !(m->eflags®_BACKR)) {
220 dp = dissect(m, m->coldp, endp, gf, gl);
222 if (g->nplus > 0 && m->lastpos == NULL)
223 m->lastpos = (const char **)malloc((g->nplus+1) *
225 if (g->nplus > 0 && m->lastpos == NULL) {
230 NOTE("backref dissect");
231 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
236 /* uh-oh... we couldn't find a subexpression-level match */
237 assert(g->backrefs); /* must be back references doing it */
238 assert(g->nplus == 0 || m->lastpos != NULL);
240 if (dp != NULL || endp <= m->coldp)
243 endp = slow(m, m->coldp, endp-1, gf, gl);
246 /* try it on a shorter possibility */
248 for (i = 1; i <= m->g->nsub; i++) {
249 assert(m->pmatch[i].rm_so == -1);
250 assert(m->pmatch[i].rm_eo == -1);
253 NOTE("backoff dissect");
254 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
256 assert(dp == NULL || dp == endp);
257 if (dp != NULL) /* found a shorter one */
260 /* despite initial appearances, there is no match here */
262 if (m->coldp == stop)
264 start = m->coldp + 1; /* recycle starting later */
267 /* fill in the details if requested */
269 pmatch[0].rm_so = m->coldp - m->offp;
270 pmatch[0].rm_eo = endp - m->offp;
273 assert(m->pmatch != NULL);
274 for (i = 1; i < nmatch; i++)
276 pmatch[i] = m->pmatch[i];
278 pmatch[i].rm_so = -1;
279 pmatch[i].rm_eo = -1;
283 if (m->pmatch != NULL)
284 free((char *)m->pmatch);
285 if (m->lastpos != NULL)
286 free((char *)m->lastpos);
292 - dissect - figure out what matched what, no back references
294 static const char * /* == stop (success) always */
295 dissect(struct match *m, const char *start, const char *stop, sopno startst,
299 sopno ss; /* start sop of current subRE */
300 sopno es; /* end sop of current subRE */
301 const char *sp; /* start of string matched by it */
302 const char *stp; /* string matched by it cannot pass here */
303 const char *rest; /* start of rest of string */
304 const char *tail; /* string unmatched by rest of RE */
305 sopno ssub; /* start sop of subsubRE */
306 sopno esub; /* end sop of subsubRE */
307 const char *ssp; /* start of string matched by subsubRE */
308 const char *sep; /* end of string matched by subsubRE */
309 const char *oldssp; /* previous ssp */
311 AT("diss", start, stop, startst, stopst);
313 for (ss = startst; ss < stopst; ss = es) {
314 /* identify end of subRE */
316 switch (OP(m->g->strip[es])) {
319 es += OPND(m->g->strip[es]);
322 while (OP(m->g->strip[es]) != O_CH)
323 es += OPND(m->g->strip[es]);
328 /* figure out what it matched */
329 switch (OP(m->g->strip[ss])) {
349 /* cases where length of match is hard to find */
353 /* how long could this one be? */
354 rest = slow(m, sp, stp, ss, es);
355 assert(rest != NULL); /* it did match */
356 /* could the rest match the rest? */
357 tail = slow(m, rest, stop, es, stopst);
360 /* no -- try a shorter match for this one */
362 assert(stp >= sp); /* it did work */
366 /* did innards match? */
367 if (slow(m, sp, rest, ssub, esub) != NULL) {
368 const char *dp = dissect(m, sp, rest, ssub, esub);
369 (void)dp; /* avoid warning if assertions off */
378 /* how long could this one be? */
379 rest = slow(m, sp, stp, ss, es);
380 assert(rest != NULL); /* it did match */
381 /* could the rest match the rest? */
382 tail = slow(m, rest, stop, es, stopst);
385 /* no -- try a shorter match for this one */
387 assert(stp >= sp); /* it did work */
393 for (;;) { /* find last match of innards */
394 sep = slow(m, ssp, rest, ssub, esub);
395 if (sep == NULL || sep == ssp)
396 break; /* failed or matched null */
397 oldssp = ssp; /* on to next try */
401 /* last successful match */
405 assert(sep == rest); /* must exhaust substring */
406 assert(slow(m, ssp, sep, ssub, esub) == rest);
408 const char *dp = dissect(m, ssp, sep, ssub, esub);
409 (void)dp; /* avoid warning if assertions off */
417 /* how long could this one be? */
418 rest = slow(m, sp, stp, ss, es);
419 assert(rest != NULL); /* it did match */
420 /* could the rest match the rest? */
421 tail = slow(m, rest, stop, es, stopst);
424 /* no -- try a shorter match for this one */
426 assert(stp >= sp); /* it did work */
429 esub = ss + OPND(m->g->strip[ss]) - 1;
430 assert(OP(m->g->strip[esub]) == OOR1);
431 for (;;) { /* find first matching branch */
432 if (slow(m, sp, rest, ssub, esub) == rest)
433 break; /* it matched all of it */
434 /* that one missed, try next one */
435 assert(OP(m->g->strip[esub]) == OOR1);
437 assert(OP(m->g->strip[esub]) == OOR2);
439 esub += OPND(m->g->strip[esub]);
440 if (OP(m->g->strip[esub]) == OOR2)
443 assert(OP(m->g->strip[esub]) == O_CH);
446 const char *dp = dissect(m, sp, rest, ssub, esub);
447 (void)dp; /* avoid warning if assertions off */
460 i = OPND(m->g->strip[ss]);
461 assert(0 < i && i <= m->g->nsub);
462 m->pmatch[i].rm_so = sp - m->offp;
465 i = OPND(m->g->strip[ss]);
466 assert(0 < i && i <= m->g->nsub);
467 m->pmatch[i].rm_eo = sp - m->offp;
480 - backref - figure out what matched what, figuring in back references
482 static const char * /* == stop (success) or NULL (failure) */
483 backref(struct match *m, const char *start, const char *stop, sopno startst,
484 sopno stopst, sopno lev, int rec) /* PLUS nesting level */
487 sopno ss; /* start sop of current subRE */
488 const char *sp; /* start of string matched by it */
489 sopno ssub; /* start sop of subsubRE */
490 sopno esub; /* end sop of subsubRE */
491 const char *ssp; /* start of string matched by subsubRE */
496 llvm_regoff_t offsave;
499 AT("back", start, stop, startst, stopst);
502 /* get as far as we can with easy stuff */
504 for (ss = startst; !hard && ss < stopst; ss++)
505 switch (OP(s = m->g->strip[ss])) {
507 if (sp == stop || *sp++ != (char)OPND(s))
516 cs = &m->g->sets[OPND(s)];
517 if (sp == stop || !CHIN(cs, *sp++))
521 if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
522 (sp < m->endp && *(sp-1) == '\n' &&
523 (m->g->cflags®_NEWLINE)) )
529 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
530 (sp < m->endp && *sp == '\n' &&
531 (m->g->cflags®_NEWLINE)) )
537 if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
538 (sp < m->endp && *(sp-1) == '\n' &&
539 (m->g->cflags®_NEWLINE)) ||
541 !ISWORD(*(sp-1))) ) &&
542 (sp < m->endp && ISWORD(*sp)) )
548 if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
549 (sp < m->endp && *sp == '\n' &&
550 (m->g->cflags®_NEWLINE)) ||
551 (sp < m->endp && !ISWORD(*sp)) ) &&
552 (sp > m->beginp && ISWORD(*(sp-1))) )
559 case OOR1: /* matches null but needs to skip */
563 assert(OP(s) == OOR2);
565 } while (OP(s = m->g->strip[ss]) != O_CH);
566 /* note that the ss++ gets us past the O_CH */
568 default: /* have to make a choice */
572 if (!hard) { /* that was it! */
577 ss--; /* adjust for the for's final increment */
580 AT("hard", sp, stop, ss, stopst);
583 case OBACK_: /* the vilest depths */
585 assert(0 < i && i <= m->g->nsub);
586 if (m->pmatch[i].rm_eo == -1)
588 assert(m->pmatch[i].rm_so != -1);
589 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
590 if (len == 0 && rec++ > MAX_RECURSION)
592 assert(stop - m->beginp >= len);
594 return(NULL); /* not enough left to match */
595 ssp = m->offp + m->pmatch[i].rm_so;
596 if (memcmp(sp, ssp, len) != 0)
598 while (m->g->strip[ss] != SOP(O_BACK, i))
600 return(backref(m, sp+len, stop, ss+1, stopst, lev, rec));
602 case OQUEST_: /* to null or not */
603 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
605 return(dp); /* not */
606 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev, rec));
609 assert(m->lastpos != NULL);
610 assert(lev+1 <= m->g->nplus);
611 m->lastpos[lev+1] = sp;
612 return(backref(m, sp, stop, ss+1, stopst, lev+1, rec));
615 if (sp == m->lastpos[lev]) /* last pass matched null */
616 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
617 /* try another pass */
618 m->lastpos[lev] = sp;
619 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev, rec);
621 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
625 case OCH_: /* find the right one, if any */
627 esub = ss + OPND(s) - 1;
628 assert(OP(m->g->strip[esub]) == OOR1);
629 for (;;) { /* find first matching branch */
630 dp = backref(m, sp, stop, ssub, esub, lev, rec);
633 /* that one missed, try next one */
634 if (OP(m->g->strip[esub]) == O_CH)
635 return(NULL); /* there is none */
637 assert(OP(m->g->strip[esub]) == OOR2);
639 esub += OPND(m->g->strip[esub]);
640 if (OP(m->g->strip[esub]) == OOR2)
643 assert(OP(m->g->strip[esub]) == O_CH);
646 case OLPAREN: /* must undo assignment if rest fails */
648 assert(0 < i && i <= m->g->nsub);
649 offsave = m->pmatch[i].rm_so;
650 m->pmatch[i].rm_so = sp - m->offp;
651 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
654 m->pmatch[i].rm_so = offsave;
657 case ORPAREN: /* must undo assignment if rest fails */
659 assert(0 < i && i <= m->g->nsub);
660 offsave = m->pmatch[i].rm_eo;
661 m->pmatch[i].rm_eo = sp - m->offp;
662 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
665 m->pmatch[i].rm_eo = offsave;
680 - fast - step through the string at top speed
682 static const char * /* where tentative match ended, or NULL */
683 fast(struct match *m, const char *start, const char *stop, sopno startst,
687 states fresh = m->fresh;
689 const char *p = start;
690 int c = (start == m->beginp) ? OUT : *(start-1);
691 int lastc; /* previous c */
694 const char *coldp; /* last p after which no match was underway */
698 st = step(m->g, startst, stopst, st, NOTHING, st);
705 c = (p == m->endp) ? OUT : *p;
709 /* is there an EOL and/or BOL between lastc and c? */
712 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
713 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
717 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
718 (c == OUT && !(m->eflags®_NOTEOL)) ) {
719 flagch = (flagch == BOL) ? BOLEOL : EOL;
724 st = step(m->g, startst, stopst, st, flagch, st);
728 /* how about a word boundary? */
729 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
730 (c != OUT && ISWORD(c)) ) {
733 if ( (lastc != OUT && ISWORD(lastc)) &&
734 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
737 if (flagch == BOW || flagch == EOW) {
738 st = step(m->g, startst, stopst, st, flagch, st);
743 if (ISSET(st, stopst) || p == stop)
744 break; /* NOTE BREAK OUT */
746 /* no, we must deal with this character */
750 st = step(m->g, startst, stopst, tmp, c, st);
752 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
756 assert(coldp != NULL);
758 if (ISSET(st, stopst))
765 - slow - step through the string more deliberately
767 static const char * /* where it ended */
768 slow(struct match *m, const char *start, const char *stop, sopno startst,
772 states empty = m->empty;
774 const char *p = start;
775 int c = (start == m->beginp) ? OUT : *(start-1);
776 int lastc; /* previous c */
779 const char *matchp; /* last p at which a match ended */
781 AT("slow", start, stop, startst, stopst);
784 SP("sstart", st, *p);
785 st = step(m->g, startst, stopst, st, NOTHING, st);
790 c = (p == m->endp) ? OUT : *p;
792 /* is there an EOL and/or BOL between lastc and c? */
795 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
796 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
800 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
801 (c == OUT && !(m->eflags®_NOTEOL)) ) {
802 flagch = (flagch == BOL) ? BOLEOL : EOL;
807 st = step(m->g, startst, stopst, st, flagch, st);
808 SP("sboleol", st, c);
811 /* how about a word boundary? */
812 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
813 (c != OUT && ISWORD(c)) ) {
816 if ( (lastc != OUT && ISWORD(lastc)) &&
817 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
820 if (flagch == BOW || flagch == EOW) {
821 st = step(m->g, startst, stopst, st, flagch, st);
822 SP("sboweow", st, c);
826 if (ISSET(st, stopst))
828 if (EQ(st, empty) || p == stop)
829 break; /* NOTE BREAK OUT */
831 /* no, we must deal with this character */
835 st = step(m->g, startst, stopst, tmp, c, st);
837 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
846 - step - map set of states reachable before char to set reachable after
849 step(struct re_guts *g,
850 sopno start, /* start state within strip */
851 sopno stop, /* state after stop state within strip */
852 states bef, /* states reachable before */
853 int ch, /* character or NONCHAR code */
854 states aft) /* states already known reachable after */
859 onestate here; /* note, macros know this name */
863 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
867 assert(pc == stop-1);
870 /* only characters can match */
871 assert(!NONCHAR(ch) || ch != (char)OPND(s));
872 if (ch == (char)OPND(s))
876 if (ch == BOL || ch == BOLEOL)
880 if (ch == EOL || ch == BOLEOL)
896 cs = &g->sets[OPND(s)];
897 if (!NONCHAR(ch) && CHIN(cs, ch))
900 case OBACK_: /* ignored here */
904 case OPLUS_: /* forward, this is just an empty */
907 case O_PLUS: /* both forward and back */
909 i = ISSETBACK(aft, OPND(s));
910 BACK(aft, aft, OPND(s));
911 if (!i && ISSETBACK(aft, OPND(s))) {
912 /* oho, must reconsider loop body */
917 case OQUEST_: /* two branches, both forward */
919 FWD(aft, aft, OPND(s));
921 case O_QUEST: /* just an empty */
924 case OLPAREN: /* not significant here */
928 case OCH_: /* mark the first two branches */
930 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
931 FWD(aft, aft, OPND(s));
933 case OOR1: /* done a branch, find the O_CH */
934 if (ISSTATEIN(aft, here)) {
936 OP(s = g->strip[pc+look]) != O_CH;
938 assert(OP(s) == OOR2);
942 case OOR2: /* propagate OCH_'s marking */
944 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
945 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
946 FWD(aft, aft, OPND(s));
949 case O_CH: /* just empty */
952 default: /* ooooops... */
963 - print - print a set of states
966 print(struct match *m, char *caption, states st, int ch, FILE *d)
968 struct re_guts *g = m->g;
972 if (!(m->eflags®_TRACE))
975 (void)fprintf(d, "%s", caption);
977 (void)fprintf(d, " %s", pchar(ch));
978 for (i = 0; i < g->nstates; i++)
980 (void)fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
983 (void)fprintf(d, "\n");
987 - at - print current situation
990 at(struct match *m, char *title, char *start, char *stop, sopno startst,
993 if (!(m->eflags®_TRACE))
996 (void)printf("%s %s-", title, pchar(*start));
997 (void)printf("%s ", pchar(*stop));
998 (void)printf("%ld-%ld\n", (long)startst, (long)stopst);
1002 #define PCHARDONE /* never again */
1004 - pchar - make a character printable
1006 * Is this identical to regchar() over in debug.c? Well, yes. But a
1007 * duplicate here avoids having a debugging-capable regexec.o tied to
1008 * a matching debug.o, and this is convenient. It all disappears in
1009 * the non-debug compilation anyway, so it doesn't matter much.
1011 static char * /* -> representation */
1014 static char pbuf[10];
1016 if (isprint(ch) || ch == ' ')
1017 (void)snprintf(pbuf, sizeof pbuf, "%c", ch);
1019 (void)snprintf(pbuf, sizeof pbuf, "\\%o", ch);