2
* The matching engine and friends. This file is #included by regexec.c
3
* after suitable #defines of a variety of macros used herein, so that
4
* different state representations can be used without duplicating masses
9
#define matcher smatcher
12
#define dissect sdissect
13
#define backref sbackref
20
#define matcher lmatcher
23
#define dissect ldissect
24
#define backref lbackref
31
/* another structure passed up and down to avoid zillions of parameters */
35
my_regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
36
char *offp; /* offsets work from here */
37
char *beginp; /* start of string -- virtual NUL precedes */
38
char *endp; /* end of string -- virtual NUL here */
39
char *coldp; /* can be no match starting before here */
40
char **lastpos; /* [nplus+1] */
42
states st; /* current states */
43
states fresh; /* states for a fresh start */
44
states tmp; /* temporary */
45
states empty; /* empty set of states */
51
#define SP(t, s, c) print(m, t, s, c, stdout)
52
#define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
53
#define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
55
#define SP(t, s, c) /* nothing */
56
#define AT(t, p1, p2, s1, s2) /* nothing */
57
#define NOTE(s) /* nothing */
61
- matcher - the actual matching engine
62
== static int matcher(register struct re_guts *g, char *string, \
63
== size_t nmatch, regmatch_t pmatch[], int eflags);
65
static int /* 0 success, REG_NOMATCH failure */
66
matcher(charset,g, str, nmatch, pmatch, eflags)
67
CHARSET_INFO *charset;
68
register struct re_guts *g;
71
my_regmatch_t pmatch[];
77
register struct match *m = &mv;
79
register const sopno gf = g->firststate+1; /* +1 for OEND */
80
register const sopno gl = g->laststate;
84
/* simplify the situation where possible */
85
if (g->cflags®_NOSUB)
87
if (eflags®_STARTEND) {
88
start = str + pmatch[0].rm_so;
89
stop = str + pmatch[0].rm_eo;
92
stop = start + strlen(start);
97
/* prescreening; this does wonders for this rather slow code */
98
if (g->must != NULL) {
99
for (dp = start; dp < stop; dp++)
100
if (*dp == g->must[0] && stop - dp >= g->mlen &&
101
memcmp(dp, g->must, (size_t)g->mlen) == 0)
103
if (dp == stop) /* we didn't find g->must */
107
/* match struct setup */
122
/* this loop does only one repetition except for backrefs */
124
endp = fast(charset, m, start, stop, gf, gl);
125
if (endp == NULL) { /* a miss */
126
if (m->pmatch != NULL)
127
free((char *)m->pmatch);
128
if (m->lastpos != NULL)
129
free((char *)m->lastpos);
133
if (nmatch == 0 && !g->backrefs)
134
break; /* no further info needed */
137
assert(m->coldp != NULL);
139
NOTE("finding start");
140
endp = slow(charset, m, m->coldp, stop, gf, gl);
143
assert(m->coldp < m->endp);
146
if (nmatch == 1 && !g->backrefs)
147
break; /* no further info needed */
149
/* oh my, he wants the subexpressions... */
150
if (m->pmatch == NULL)
151
m->pmatch = (my_regmatch_t *)malloc((m->g->nsub + 1) *
152
sizeof(my_regmatch_t));
153
if (m->pmatch == NULL) {
154
if (m->lastpos != NULL)
155
free((char *)m->lastpos);
159
for (i = 1; i <= m->g->nsub; i++)
160
m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
161
if (!g->backrefs && !(m->eflags®_BACKR)) {
163
dp = dissect(charset, m, m->coldp, endp, gf, gl);
165
if (g->nplus > 0 && m->lastpos == NULL)
166
m->lastpos = (char **)malloc((g->nplus+1) *
168
if (g->nplus > 0 && m->lastpos == NULL) {
173
NOTE("backref dissect");
174
dp = backref(charset, m, m->coldp, endp, gf, gl, (sopno)0);
179
/* uh-oh... we couldn't find a subexpression-level match */
180
assert(g->backrefs); /* must be back references doing it */
181
assert(g->nplus == 0 || m->lastpos != NULL);
183
if (dp != NULL || endp <= m->coldp)
186
endp = slow(charset, m, m->coldp, endp-1, gf, gl);
189
/* try it on a shorter possibility */
191
for (i = 1; i <= m->g->nsub; i++) {
192
assert(m->pmatch[i].rm_so == -1);
193
assert(m->pmatch[i].rm_eo == -1);
196
NOTE("backoff dissect");
197
dp = backref(charset, m, m->coldp, endp, gf, gl, (sopno)0);
199
assert(dp == NULL || dp == endp);
200
if (dp != NULL) /* found a shorter one */
203
/* despite initial appearances, there is no match here */
205
start = m->coldp + 1; /* recycle starting later */
206
assert(start <= stop);
209
/* fill in the details if requested */
211
pmatch[0].rm_so = m->coldp - m->offp;
212
pmatch[0].rm_eo = endp - m->offp;
215
assert(m->pmatch != NULL);
216
for (i = 1; i < nmatch; i++)
218
pmatch[i] = m->pmatch[i];
220
pmatch[i].rm_so = -1;
221
pmatch[i].rm_eo = -1;
225
if (m->pmatch != NULL)
226
free((char *)m->pmatch);
227
if (m->lastpos != NULL)
228
free((char *)m->lastpos);
234
- dissect - figure out what matched what, no back references
235
== static char *dissect(register struct match *m, char *start, \
236
== char *stop, sopno startst, sopno stopst);
238
static char * /* == stop (success) always */
239
dissect(charset, m, start, stop, startst, stopst)
240
CHARSET_INFO *charset;
241
register struct match *m;
248
register sopno ss; /* start sop of current subRE */
249
register sopno es; /* end sop of current subRE */
250
register char *sp; /* start of string matched by it */
251
register char *stp; /* string matched by it cannot pass here */
252
register char *rest; /* start of rest of string */
253
register char *tail; /* string unmatched by rest of RE */
254
register sopno ssub; /* start sop of subsubRE */
255
register sopno esub; /* end sop of subsubRE */
256
register char *ssp; /* start of string matched by subsubRE */
257
register char *sep; /* end of string matched by subsubRE */
258
register char *oldssp; /* previous ssp */
259
register char *dp; /* used in debug mode to check asserts */
261
AT("diss", start, stop, startst, stopst);
263
for (ss = startst; ss < stopst; ss = es) {
264
/* identify end of subRE */
266
switch (OP(m->g->strip[es])) {
269
es += OPND(m->g->strip[es]);
272
while (OP(m->g->strip[es]) != O_CH)
273
es += OPND(m->g->strip[es]);
278
/* figure out what it matched */
279
switch (OP(m->g->strip[ss])) {
299
/* cases where length of match is hard to find */
303
/* how long could this one be? */
304
rest = slow(charset, m, sp, stp, ss, es);
305
assert(rest != NULL); /* it did match */
306
/* could the rest match the rest? */
307
tail = slow(charset, m, rest, stop, es, stopst);
310
/* no -- try a shorter match for this one */
312
assert(stp >= sp); /* it did work */
316
/* did innards match? */
317
if (slow(charset, m, sp, rest, ssub, esub) != NULL) {
318
dp = dissect(charset, m, sp, rest, ssub, esub);
327
/* how long could this one be? */
328
rest = slow(charset, m, sp, stp, ss, es);
329
assert(rest != NULL); /* it did match */
330
/* could the rest match the rest? */
331
tail = slow(charset, m, rest, stop, es, stopst);
334
/* no -- try a shorter match for this one */
336
assert(stp >= sp); /* it did work */
342
for (;;) { /* find last match of innards */
343
sep = slow(charset, m, ssp, rest, ssub, esub);
344
if (sep == NULL || sep == ssp)
345
break; /* failed or matched null */
346
oldssp = ssp; /* on to next try */
350
/* last successful match */
354
assert(sep == rest); /* must exhaust substring */
355
assert(slow(charset, m, ssp, sep, ssub, esub) == rest);
356
dp = dissect(charset, m, ssp, sep, ssub, esub);
363
/* how long could this one be? */
364
rest = slow(charset, m, sp, stp, ss, es);
365
assert(rest != NULL); /* it did match */
366
/* could the rest match the rest? */
367
tail = slow(charset, m, rest, stop, es, stopst);
370
/* no -- try a shorter match for this one */
372
assert(stp >= sp); /* it did work */
375
esub = ss + OPND(m->g->strip[ss]) - 1;
376
assert(OP(m->g->strip[esub]) == OOR1);
377
for (;;) { /* find first matching branch */
378
if (slow(charset, m, sp, rest, ssub, esub) == rest)
379
break; /* it matched all of it */
380
/* that one missed, try next one */
381
assert(OP(m->g->strip[esub]) == OOR1);
383
assert(OP(m->g->strip[esub]) == OOR2);
385
esub += OPND(m->g->strip[esub]);
386
if (OP(m->g->strip[esub]) == OOR2)
389
assert(OP(m->g->strip[esub]) == O_CH);
391
dp = dissect(charset, m, sp, rest, ssub, esub);
403
i = OPND(m->g->strip[ss]);
404
assert(0 < i && i <= m->g->nsub);
405
m->pmatch[i].rm_so = sp - m->offp;
408
i = OPND(m->g->strip[ss]);
409
assert(0 < i && i <= m->g->nsub);
410
m->pmatch[i].rm_eo = sp - m->offp;
423
- backref - figure out what matched what, figuring in back references
424
== static char *backref(register struct match *m, char *start, \
425
== char *stop, sopno startst, sopno stopst, sopno lev);
427
static char * /* == stop (success) or NULL (failure) */
428
backref(charset,m, start, stop, startst, stopst, lev)
429
CHARSET_INFO *charset;
430
register struct match *m;
435
sopno lev; /* PLUS nesting level */
438
register sopno ss; /* start sop of current subRE */
439
register char *sp; /* start of string matched by it */
440
register sopno ssub; /* start sop of subsubRE */
441
register sopno esub; /* end sop of subsubRE */
442
register char *ssp; /* start of string matched by subsubRE */
447
register regoff_t offsave;
450
AT("back", start, stop, startst, stopst);
453
/* get as far as we can with easy stuff */
455
for (ss = startst; !hard && ss < stopst; ss++)
456
switch (OP(s = m->g->strip[ss])) {
458
if (sp == stop || *sp++ != (char)OPND(s))
467
cs = &m->g->sets[OPND(s)];
468
if (sp == stop || !CHIN(cs, *sp++))
472
if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
473
(sp < m->endp && *(sp-1) == '\n' &&
474
(m->g->cflags®_NEWLINE)) )
480
if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
481
(sp < m->endp && *sp == '\n' &&
482
(m->g->cflags®_NEWLINE)) )
488
if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
489
(sp < m->endp && *(sp-1) == '\n' &&
490
(m->g->cflags®_NEWLINE)) ||
492
!ISWORD(charset,*(sp-1))) ) &&
493
(sp < m->endp && ISWORD(charset,*sp)) )
499
if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
500
(sp < m->endp && *sp == '\n' &&
501
(m->g->cflags®_NEWLINE)) ||
502
(sp < m->endp && !ISWORD(charset,*sp)) ) &&
503
(sp > m->beginp && ISWORD(charset,*(sp-1))) )
510
case OOR1: /* matches null but needs to skip */
514
assert(OP(s) == OOR2);
516
} while (OP(s = m->g->strip[ss]) != O_CH);
517
/* note that the ss++ gets us past the O_CH */
519
default: /* have to make a choice */
523
if (!hard) { /* that was it! */
528
ss--; /* adjust for the for's final increment */
531
AT("hard", sp, stop, ss, stopst);
534
case OBACK_: /* the vilest depths */
536
assert(0 < i && i <= m->g->nsub);
537
if (m->pmatch[i].rm_eo == -1)
539
assert(m->pmatch[i].rm_so != -1);
540
len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
541
assert((size_t) (stop - m->beginp) >= len);
543
return(NULL); /* not enough left to match */
544
ssp = m->offp + m->pmatch[i].rm_so;
545
if (memcmp(sp, ssp, len) != 0)
547
while (m->g->strip[ss] != SOP(O_BACK, i))
549
return(backref(charset, m, sp+len, stop, ss+1, stopst, lev));
551
case OQUEST_: /* to null or not */
552
dp = backref(charset, m, sp, stop, ss+1, stopst, lev);
554
return(dp); /* not */
555
return(backref(charset, m, sp, stop, ss+OPND(s)+1, stopst, lev));
558
assert(m->lastpos != NULL);
559
assert(lev+1 <= m->g->nplus);
560
m->lastpos[lev+1] = sp;
561
return(backref(charset, m, sp, stop, ss+1, stopst, lev+1));
564
if (sp == m->lastpos[lev]) /* last pass matched null */
565
return(backref(charset, m, sp, stop, ss+1, stopst, lev-1));
566
/* try another pass */
567
m->lastpos[lev] = sp;
568
dp = backref(charset, m, sp, stop, ss-OPND(s)+1, stopst, lev);
570
return(backref(charset, m, sp, stop, ss+1, stopst, lev-1));
574
case OCH_: /* find the right one, if any */
576
esub = ss + OPND(s) - 1;
577
assert(OP(m->g->strip[esub]) == OOR1);
578
for (;;) { /* find first matching branch */
579
dp = backref(charset, m, sp, stop, ssub, esub, lev);
582
/* that one missed, try next one */
583
if (OP(m->g->strip[esub]) == O_CH)
584
return(NULL); /* there is none */
586
assert(OP(m->g->strip[esub]) == OOR2);
588
esub += OPND(m->g->strip[esub]);
589
if (OP(m->g->strip[esub]) == OOR2)
592
assert(OP(m->g->strip[esub]) == O_CH);
595
case OLPAREN: /* must undo assignment if rest fails */
597
assert(0 < i && i <= m->g->nsub);
598
offsave = m->pmatch[i].rm_so;
599
m->pmatch[i].rm_so = sp - m->offp;
600
dp = backref(charset, m, sp, stop, ss+1, stopst, lev);
603
m->pmatch[i].rm_so = offsave;
606
case ORPAREN: /* must undo assignment if rest fails */
608
assert(0 < i && i <= m->g->nsub);
609
offsave = m->pmatch[i].rm_eo;
610
m->pmatch[i].rm_eo = sp - m->offp;
611
dp = backref(charset, m, sp, stop, ss+1, stopst, lev);
614
m->pmatch[i].rm_eo = offsave;
625
return 0; /* Keep gcc happy */
629
- fast - step through the string at top speed
630
== static char *fast(register struct match *m, char *start, \
631
== char *stop, sopno startst, sopno stopst);
633
static char * /* where tentative match ended, or NULL */
634
fast(charset, m, start, stop, startst, stopst)
635
CHARSET_INFO *charset;
636
register struct match *m;
642
register states st = m->st;
643
register states fresh = m->fresh;
644
register states tmp = m->tmp;
645
register char *p = start;
646
register int c = (start == m->beginp) ? OUT : *(start-1);
647
register int lastc; /* previous c */
650
register char *coldp; /* last p after which no match was underway */
654
st = step(m->g, startst, stopst, st, NOTHING, st);
661
c = (p == m->endp) ? OUT : *p;
665
/* is there an EOL and/or BOL between lastc and c? */
668
if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
669
(lastc == OUT && !(m->eflags®_NOTBOL)) ) {
673
if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
674
(c == OUT && !(m->eflags®_NOTEOL)) ) {
675
flagch = (flagch == BOL) ? BOLEOL : EOL;
680
st = step(m->g, startst, stopst, st, flagch, st);
684
/* how about a word boundary? */
685
if ( (flagch == BOL || (lastc != OUT && !ISWORD(charset,lastc))) &&
686
(c != OUT && ISWORD(charset,c)) ) {
689
if ( (lastc != OUT && ISWORD(charset,lastc)) &&
690
(flagch == EOL || (c != OUT && !ISWORD(charset,c))) ) {
693
if (flagch == BOW || flagch == EOW) {
694
st = step(m->g, startst, stopst, st, flagch, st);
699
if (ISSET(st, stopst) || p == stop)
700
break; /* NOTE BREAK OUT */
702
/* no, we must deal with this character */
706
st = step(m->g, startst, stopst, tmp, c, st);
708
assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
712
assert(coldp != NULL);
714
if (ISSET(st, stopst))
721
- slow - step through the string more deliberately
722
== static char *slow(register struct match *m, char *start, \
723
== char *stop, sopno startst, sopno stopst);
725
static char * /* where it ended */
726
slow(charset, m, start, stop, startst, stopst)
727
CHARSET_INFO *charset;
728
register struct match *m;
734
register states st = m->st;
735
register states empty = m->empty;
736
register states tmp = m->tmp;
737
register char *p = start;
738
register int c = (start == m->beginp) ? OUT : *(start-1);
739
register int lastc; /* previous c */
742
register char *matchp; /* last p at which a match ended */
744
AT("slow", start, stop, startst, stopst);
747
SP("sstart", st, *p);
748
st = step(m->g, startst, stopst, st, NOTHING, st);
753
c = (p == m->endp) ? OUT : *p;
755
/* is there an EOL and/or BOL between lastc and c? */
758
if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
759
(lastc == OUT && !(m->eflags®_NOTBOL)) ) {
763
if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
764
(c == OUT && !(m->eflags®_NOTEOL)) ) {
765
flagch = (flagch == BOL) ? BOLEOL : EOL;
770
st = step(m->g, startst, stopst, st, flagch, st);
771
SP("sboleol", st, c);
774
/* how about a word boundary? */
775
if ( (flagch == BOL || (lastc != OUT && !ISWORD(charset,lastc))) &&
776
(c != OUT && ISWORD(charset,c)) ) {
779
if ( (lastc != OUT && ISWORD(charset,lastc)) &&
780
(flagch == EOL || (c != OUT && !ISWORD(charset,c))) ) {
783
if (flagch == BOW || flagch == EOW) {
784
st = step(m->g, startst, stopst, st, flagch, st);
785
SP("sboweow", st, c);
789
if (ISSET(st, stopst))
791
if (EQ(st, empty) || p == stop)
792
break; /* NOTE BREAK OUT */
794
/* no, we must deal with this character */
798
st = step(m->g, startst, stopst, tmp, c, st);
800
assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
809
- step - map set of states reachable before char to set reachable after
810
== static states step(register struct re_guts *g, sopno start, sopno stop, \
811
== register states bef, int ch, register states aft);
812
== #define BOL (OUT+1)
813
== #define EOL (BOL+1)
814
== #define BOLEOL (BOL+2)
815
== #define NOTHING (BOL+3)
816
== #define BOW (BOL+4)
817
== #define EOW (BOL+5)
818
== #define CODEMAX (BOL+5) // highest code used
819
== #define NONCHAR(c) ((c) > CHAR_MAX)
820
== #define NNONCHAR (CODEMAX-CHAR_MAX)
823
step(g, start, stop, bef, ch, aft)
824
register struct re_guts *g;
825
sopno start; /* start state within strip */
826
sopno stop; /* state after stop state within strip */
827
register states bef; /* states reachable before */
828
int ch; /* character or NONCHAR code */
829
register states aft; /* states already known reachable after */
834
register onestate here; /* note, macros know this name */
836
register onestate i; /* Changed from int by Monty */
838
for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
842
assert(pc == stop-1);
845
/* only characters can match */
846
assert(!NONCHAR(ch) || ch != (char)OPND(s));
847
if (ch == (char)OPND(s))
851
if (ch == BOL || ch == BOLEOL)
855
if (ch == EOL || ch == BOLEOL)
871
cs = &g->sets[OPND(s)];
872
if (!NONCHAR(ch) && CHIN(cs, ch))
875
case OBACK_: /* ignored here */
879
case OPLUS_: /* forward, this is just an empty */
882
case O_PLUS: /* both forward and back */
884
i = ISSETBACK(aft, OPND(s));
885
BACK(aft, aft, OPND(s));
886
if (!i && ISSETBACK(aft, OPND(s))) {
887
/* oho, must reconsider loop body */
892
case OQUEST_: /* two branches, both forward */
894
FWD(aft, aft, OPND(s));
896
case O_QUEST: /* just an empty */
899
case OLPAREN: /* not significant here */
903
case OCH_: /* mark the first two branches */
905
assert(OP(g->strip[pc+OPND(s)]) == OOR2);
906
FWD(aft, aft, OPND(s));
908
case OOR1: /* done a branch, find the O_CH */
909
if (ISSTATEIN(aft, here)) {
911
OP(s = g->strip[pc+look]) != O_CH;
913
assert(OP(s) == OOR2);
917
case OOR2: /* propagate OCH_'s marking */
919
if (OP(g->strip[pc+OPND(s)]) != O_CH) {
920
assert(OP(g->strip[pc+OPND(s)]) == OOR2);
921
FWD(aft, aft, OPND(s));
924
case O_CH: /* just empty */
927
default: /* ooooops... */
938
- print - print a set of states
940
== static void print(struct match *m, char *caption, states st, \
945
print(m, caption, st, ch, d)
952
register struct re_guts *g = m->g;
954
register int first = 1;
957
if (!(m->eflags®_TRACE))
960
fprintf(d, "%s", caption);
962
fprintf(d, " %s", printchar(ch,buf));
963
for (i = 0; i < g->nstates; i++)
965
fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
972
- at - print current situation
974
== static void at(struct match *m, char *title, char *start, char *stop, \
975
== sopno startst, sopno stopst);
979
at(m, title, start, stop, startst, stopst)
988
if (!(m->eflags®_TRACE))
991
printf("%s %s-", title, printchar(*start,buf));
992
printf("%s ", printchar(*stop,buf));
993
printf("%ld-%ld\n", (long)startst, (long)stopst,buf);
997
#define PCHARDONE /* never again */
999
- printchar - make a character printable
1001
== static char *printchar(int ch);
1004
* Is this identical to regchar() over in debug.c? Well, yes. But a
1005
* duplicate here avoids having a debugging-capable regexec.o tied to
1006
* a matching debug.o, and this is convenient. It all disappears in
1007
* the non-debug compilation anyway, so it doesn't matter much.
1009
static char * /* -> representation */
1014
if (isprint(ch) || ch == ' ')
1015
sprintf(pbuf, "%c", ch);
1017
sprintf(pbuf, "\\%o", ch);