1 /*
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
5 * of code.
6 */
7
8 #ifdef SNAMES
9 #define matcher smatcher
10 #define fast sfast
11 #define slow sslow
12 #define dissect sdissect
13 #define backref sbackref
14 #define step sstep
15 #define print sprint
16 #define at sat
17 #define match smat
18 #endif
19 #ifdef LNAMES
20 #define matcher lmatcher
21 #define fast lfast
22 #define slow lslow
23 #define dissect ldissect
24 #define backref lbackref
25 #define step lstep
26 #define print lprint
27 #define at lat
28 #define match lmat
29 #endif
30
31 /* another structure passed up and down to avoid zillions of parameters */
32 struct match {
33 struct re_guts *g;
34 int eflags;
35 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] */
41 STATEVARS;
42 states st; /* current states */
43 states fresh; /* states for a fresh start */
44 states tmp; /* temporary */
45 states empty; /* empty set of states */
46 };
47
48 /* #include "engine.ih" // why generate this */
49 /* forward references */
50 static int matcher(register struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
51 static char *dissect(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
52 static char *backref(register struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev);
53 static char *fast(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
54 static char *slow(register struct match *m, char *start, char *stop, sopno startst, sopno stopst);
55 static states step(register struct re_guts *g, sopno start, sopno stop, register states bef, int ch, register states aft);
56 #define BOL (OUT+1)
57 #define EOL (BOL+1)
58 #define BOLEOL (BOL+2)
59 #define NOTHING (BOL+3)
60 #define BOW (BOL+4)
61 #define EOW (BOL+5)
62 #define CODEMAX (BOL+5) /* highest code used */
63 #define NONCHAR(c) ((c) > CHAR_MAX)
64 #define NNONCHAR (CODEMAX-CHAR_MAX)
65 #ifdef REDEBUG
66 static void print(struct match *m, char *caption, states st, int ch, FILE *d);
67 #endif
68 #ifdef REDEBUG
69 static void at(struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst);
70 #endif
71 #ifdef REDEBUG
72 static char *pchar(int ch);
73 #endif
74
75 #ifdef REDEBUG
76 #define SP(t, s, c) print(m, t, s, c, stdout)
77 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
78 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
79 #else
80 #define SP(t, s, c) /* nothing */
81 #define AT(t, p1, p2, s1, s2) /* nothing */
82 #define NOTE(s) /* nothing */
83 #endif
84
85 /*
86 - matcher - the actual matching engine
87 == static int matcher(register struct re_guts *g, char *string, \
88 == size_t nmatch, regmatch_t pmatch[], int eflags);
89 */
90 static int /* 0 success, REG_NOMATCH failure */
matcher(g,string,nmatch,pmatch,eflags)91 matcher(g, string, nmatch, pmatch, eflags)
92 register struct re_guts *g;
93 char *string;
94 size_t nmatch;
95 regmatch_t pmatch[];
96 int eflags;
97 {
98 register char *endp;
99 register int i;
100 struct match mv;
101 register struct match *m = &mv;
102 register char *dp;
103 const register sopno gf = g->firststate+1; /* +1 for OEND */
104 const register sopno gl = g->laststate;
105 char *start;
106 char *stop;
107
108 /* simplify the situation where possible */
109 if (g->cflags®_NOSUB)
110 nmatch = 0;
111 if (eflags®_STARTEND) {
112 start = string + pmatch[0].rm_so;
113 stop = string + pmatch[0].rm_eo;
114 } else {
115 start = string;
116 stop = start + strlen(start);
117 }
118 if (stop < start)
119 return(REG_INVARG);
120
121 /* prescreening; this does wonders for this rather slow code */
122 if (g->must != NULL) {
123 for (dp = start; dp < stop; dp++)
124 if (*dp == g->must[0] && stop - dp >= g->mlen &&
125 memcmp(dp, g->must, (size_t)g->mlen) == 0)
126 break;
127 if (dp == stop) /* we didn't find g->must */
128 return(REG_NOMATCH);
129 }
130
131 /* match struct setup */
132 m->g = g;
133 m->eflags = eflags;
134 m->pmatch = NULL;
135 m->lastpos = NULL;
136 m->offp = string;
137 m->beginp = start;
138 m->endp = stop;
139 STATESETUP(m, 4);
140 SETUP(m->st);
141 SETUP(m->fresh);
142 SETUP(m->tmp);
143 SETUP(m->empty);
144 CLEAR(m->empty);
145
146 /* this loop does only one repetition except for backrefs */
147 for (;;) {
148 endp = fast(m, start, stop, gf, gl);
149 if (endp == NULL) { /* a miss */
150 STATETEARDOWN(m);
151 return(REG_NOMATCH);
152 }
153 if (nmatch == 0 && !g->backrefs)
154 break; /* no further info needed */
155
156 /* where? */
157 assert(m->coldp != NULL);
158 for (;;) {
159 NOTE("finding start");
160 endp = slow(m, m->coldp, stop, gf, gl);
161 if (endp != NULL)
162 break;
163 assert(m->coldp < m->endp);
164 m->coldp++;
165 }
166 if (nmatch == 1 && !g->backrefs)
167 break; /* no further info needed */
168
169 /* oh my, he wants the subexpressions... */
170 if (m->pmatch == NULL)
171 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
172 sizeof(regmatch_t));
173 if (m->pmatch == NULL) {
174 STATETEARDOWN(m);
175 return(REG_ESPACE);
176 }
177 for (i = 1; i <= m->g->nsub; i++)
178 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
179 if (!g->backrefs && !(m->eflags®_BACKR)) {
180 NOTE("dissecting");
181 dp = dissect(m, m->coldp, endp, gf, gl);
182 } else {
183 if (g->nplus > 0 && m->lastpos == NULL)
184 m->lastpos = (char **)malloc((g->nplus+1) *
185 sizeof(char *));
186 if (g->nplus > 0 && m->lastpos == NULL) {
187 free(m->pmatch);
188 STATETEARDOWN(m);
189 return(REG_ESPACE);
190 }
191 NOTE("backref dissect");
192 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
193 }
194 if (dp != NULL)
195 break;
196
197 /* uh-oh... we couldn't find a subexpression-level match */
198 assert(g->backrefs); /* must be back references doing it */
199 assert(g->nplus == 0 || m->lastpos != NULL);
200 for (;;) {
201 if (dp != NULL || endp <= m->coldp)
202 break; /* defeat */
203 NOTE("backoff");
204 endp = slow(m, m->coldp, endp-1, gf, gl);
205 if (endp == NULL)
206 break; /* defeat */
207 /* try it on a shorter possibility */
208 #ifndef NDEBUG
209 for (i = 1; i <= m->g->nsub; i++) {
210 assert(m->pmatch[i].rm_so == -1);
211 assert(m->pmatch[i].rm_eo == -1);
212 }
213 #endif
214 NOTE("backoff dissect");
215 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
216 }
217 assert(dp == NULL || dp == endp);
218 if (dp != NULL) /* found a shorter one */
219 break;
220
221 /* despite initial appearances, there is no match here */
222 NOTE("false alarm");
223 start = m->coldp + 1; /* recycle starting later */
224 assert(start <= stop);
225 }
226
227 /* fill in the details if requested */
228 if (nmatch > 0) {
229 pmatch[0].rm_so = m->coldp - m->offp;
230 pmatch[0].rm_eo = endp - m->offp;
231 }
232 if (nmatch > 1) {
233 assert(m->pmatch != NULL);
234 for (i = 1; i < nmatch; i++)
235 if (i <= m->g->nsub)
236 pmatch[i] = m->pmatch[i];
237 else {
238 pmatch[i].rm_so = -1;
239 pmatch[i].rm_eo = -1;
240 }
241 }
242
243 if (m->pmatch != NULL)
244 free((char *)m->pmatch);
245 if (m->lastpos != NULL)
246 free((char *)m->lastpos);
247 STATETEARDOWN(m);
248 return(0);
249 }
250
251 /*
252 - dissect - figure out what matched what, no back references
253 == static char *dissect(register struct match *m, char *start, \
254 == char *stop, sopno startst, sopno stopst);
255 */
256 static char * /* == stop (success) always */
dissect(m,start,stop,startst,stopst)257 dissect(m, start, stop, startst, stopst)
258 register struct match *m;
259 char *start;
260 char *stop;
261 sopno startst;
262 sopno stopst;
263 {
264 register int i;
265 register sopno ss; /* start sop of current subRE */
266 register sopno es; /* end sop of current subRE */
267 register char *sp; /* start of string matched by it */
268 register char *stp; /* string matched by it cannot pass here */
269 register char *rest; /* start of rest of string */
270 register char *tail; /* string unmatched by rest of RE */
271 register sopno ssub; /* start sop of subsubRE */
272 register sopno esub; /* end sop of subsubRE */
273 register char *ssp; /* start of string matched by subsubRE */
274 register char *sep; /* end of string matched by subsubRE */
275 register char *oldssp; /* previous ssp */
276 register char *dp;
277
278 AT("diss", start, stop, startst, stopst);
279 sp = start;
280 for (ss = startst; ss < stopst; ss = es) {
281 /* identify end of subRE */
282 es = ss;
283 switch (OP(m->g->strip[es])) {
284 case OPLUS_:
285 case OQUEST_:
286 es += OPND(m->g->strip[es]);
287 break;
288 case OCH_:
289 while (OP(m->g->strip[es]) != O_CH)
290 es += OPND(m->g->strip[es]);
291 break;
292 }
293 es++;
294
295 /* figure out what it matched */
296 switch (OP(m->g->strip[ss])) {
297 case OEND:
298 assert(nope);
299 break;
300 case OCHAR:
301 sp++;
302 break;
303 case OBOL:
304 case OEOL:
305 case OBOW:
306 case OEOW:
307 break;
308 case OANY:
309 case OANYOF:
310 sp++;
311 break;
312 case OBACK_:
313 case O_BACK:
314 assert(nope);
315 break;
316 /* cases where length of match is hard to find */
317 case OQUEST_:
318 stp = stop;
319 for (;;) {
320 /* how long could this one be? */
321 rest = slow(m, sp, stp, ss, es);
322 assert(rest != NULL); /* it did match */
323 /* could the rest match the rest? */
324 tail = slow(m, rest, stop, es, stopst);
325 if (tail == stop)
326 break; /* yes! */
327 /* no -- try a shorter match for this one */
328 stp = rest - 1;
329 assert(stp >= sp); /* it did work */
330 }
331 ssub = ss + 1;
332 esub = es - 1;
333 /* did innards match? */
334 if (slow(m, sp, rest, ssub, esub) != NULL) {
335 dp = dissect(m, sp, rest, ssub, esub);
336 assert(dp == rest);
337 } else /* no */
338 assert(sp == rest);
339 sp = rest;
340 break;
341 case OPLUS_:
342 stp = stop;
343 for (;;) {
344 /* how long could this one be? */
345 rest = slow(m, sp, stp, ss, es);
346 assert(rest != NULL); /* it did match */
347 /* could the rest match the rest? */
348 tail = slow(m, rest, stop, es, stopst);
349 if (tail == stop)
350 break; /* yes! */
351 /* no -- try a shorter match for this one */
352 stp = rest - 1;
353 assert(stp >= sp); /* it did work */
354 }
355 ssub = ss + 1;
356 esub = es - 1;
357 ssp = sp;
358 oldssp = ssp;
359 for (;;) { /* find last match of innards */
360 sep = slow(m, ssp, rest, ssub, esub);
361 if (sep == NULL || sep == ssp)
362 break; /* failed or matched null */
363 oldssp = ssp; /* on to next try */
364 ssp = sep;
365 }
366 if (sep == NULL) {
367 /* last successful match */
368 sep = ssp;
369 ssp = oldssp;
370 }
371 assert(sep == rest); /* must exhaust substring */
372 assert(slow(m, ssp, sep, ssub, esub) == rest);
373 dp = dissect(m, ssp, sep, ssub, esub);
374 assert(dp == sep);
375 sp = rest;
376 break;
377 case OCH_:
378 stp = stop;
379 for (;;) {
380 /* how long could this one be? */
381 rest = slow(m, sp, stp, ss, es);
382 assert(rest != NULL); /* it did match */
383 /* could the rest match the rest? */
384 tail = slow(m, rest, stop, es, stopst);
385 if (tail == stop)
386 break; /* yes! */
387 /* no -- try a shorter match for this one */
388 stp = rest - 1;
389 assert(stp >= sp); /* it did work */
390 }
391 ssub = ss + 1;
392 esub = ss + OPND(m->g->strip[ss]) - 1;
393 assert(OP(m->g->strip[esub]) == OOR1);
394 for (;;) { /* find first matching branch */
395 if (slow(m, sp, rest, ssub, esub) == rest)
396 break; /* it matched all of it */
397 /* that one missed, try next one */
398 assert(OP(m->g->strip[esub]) == OOR1);
399 esub++;
400 assert(OP(m->g->strip[esub]) == OOR2);
401 ssub = esub + 1;
402 esub += OPND(m->g->strip[esub]);
403 if (OP(m->g->strip[esub]) == OOR2)
404 esub--;
405 else
406 assert(OP(m->g->strip[esub]) == O_CH);
407 }
408 dp = dissect(m, sp, rest, ssub, esub);
409 assert(dp == rest);
410 sp = rest;
411 break;
412 case O_PLUS:
413 case O_QUEST:
414 case OOR1:
415 case OOR2:
416 case O_CH:
417 assert(nope);
418 break;
419 case OLPAREN:
420 i = OPND(m->g->strip[ss]);
421 assert(0 < i && i <= m->g->nsub);
422 m->pmatch[i].rm_so = sp - m->offp;
423 break;
424 case ORPAREN:
425 i = OPND(m->g->strip[ss]);
426 assert(0 < i && i <= m->g->nsub);
427 m->pmatch[i].rm_eo = sp - m->offp;
428 break;
429 default: /* uh oh */
430 assert(nope);
431 break;
432 }
433 }
434
435 assert(sp == stop);
436 return(sp);
437 }
438
439 /*
440 - backref - figure out what matched what, figuring in back references
441 == static char *backref(register struct match *m, char *start, \
442 == char *stop, sopno startst, sopno stopst, sopno lev);
443 */
444 static char * /* == stop (success) or NULL (failure) */
backref(m,start,stop,startst,stopst,lev)445 backref(m, start, stop, startst, stopst, lev)
446 register struct match *m;
447 char *start;
448 char *stop;
449 sopno startst;
450 sopno stopst;
451 sopno lev; /* PLUS nesting level */
452 {
453 register int i;
454 register sopno ss; /* start sop of current subRE */
455 register char *sp; /* start of string matched by it */
456 register sopno ssub; /* start sop of subsubRE */
457 register sopno esub; /* end sop of subsubRE */
458 register char *ssp; /* start of string matched by subsubRE */
459 register char *dp;
460 register size_t len;
461 register int hard;
462 register sop s;
463 register regoff_t offsave;
464 register cset *cs;
465
466 AT("back", start, stop, startst, stopst);
467 sp = start;
468
469 /* get as far as we can with easy stuff */
470 hard = 0;
471 for (ss = startst; !hard && ss < stopst; ss++)
472 switch (OP(s = m->g->strip[ss])) {
473 case OCHAR:
474 if (sp == stop || *sp++ != (char)OPND(s))
475 return(NULL);
476 break;
477 case OANY:
478 if (sp == stop)
479 return(NULL);
480 sp++;
481 break;
482 case OANYOF:
483 cs = &m->g->sets[OPND(s)];
484 if (sp == stop || !CHIN(cs, *sp++))
485 return(NULL);
486 break;
487 case OBOL:
488 if ( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
489 (sp < m->endp && *(sp-1) == '\n' &&
490 (m->g->cflags®_NEWLINE)) )
491 { /* yes */ }
492 else
493 return(NULL);
494 break;
495 case OEOL:
496 if ( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
497 (sp < m->endp && *sp == '\n' &&
498 (m->g->cflags®_NEWLINE)) )
499 { /* yes */ }
500 else
501 return(NULL);
502 break;
503 case OBOW:
504 if (( (sp == m->beginp && !(m->eflags®_NOTBOL)) ||
505 (sp < m->endp && *(sp-1) == '\n' &&
506 (m->g->cflags®_NEWLINE)) ||
507 (sp > m->beginp &&
508 !ISWORD(*(sp-1))) ) &&
509 (sp < m->endp && ISWORD(*sp)) )
510 { /* yes */ }
511 else
512 return(NULL);
513 break;
514 case OEOW:
515 if (( (sp == m->endp && !(m->eflags®_NOTEOL)) ||
516 (sp < m->endp && *sp == '\n' &&
517 (m->g->cflags®_NEWLINE)) ||
518 (sp < m->endp && !ISWORD(*sp)) ) &&
519 (sp > m->beginp && ISWORD(*(sp-1))) )
520 { /* yes */ }
521 else
522 return(NULL);
523 break;
524 case O_QUEST:
525 break;
526 case OOR1: /* matches null but needs to skip */
527 ss++;
528 s = m->g->strip[ss];
529 do {
530 assert(OP(s) == OOR2);
531 ss += OPND(s);
532 } while (OP(s = m->g->strip[ss]) != O_CH);
533 /* note that the ss++ gets us past the O_CH */
534 break;
535 default: /* have to make a choice */
536 hard = 1;
537 break;
538 }
539 if (!hard) { /* that was it! */
540 if (sp != stop)
541 return(NULL);
542 return(sp);
543 }
544 ss--; /* adjust for the for's final increment */
545
546 /* the hard stuff */
547 AT("hard", sp, stop, ss, stopst);
548 s = m->g->strip[ss];
549 switch (OP(s)) {
550 case OBACK_: /* the vilest depths */
551 i = OPND(s);
552 assert(0 < i && i <= m->g->nsub);
553 if (m->pmatch[i].rm_eo == -1)
554 return(NULL);
555 assert(m->pmatch[i].rm_so != -1);
556 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
557 assert(stop - m->beginp >= len);
558 if (sp > stop - len)
559 return(NULL); /* not enough left to match */
560 ssp = m->offp + m->pmatch[i].rm_so;
561 if (memcmp(sp, ssp, len) != 0)
562 return(NULL);
563 while (m->g->strip[ss] != SOP(O_BACK, i))
564 ss++;
565 return(backref(m, sp+len, stop, ss+1, stopst, lev));
566 break;
567 case OQUEST_: /* to null or not */
568 dp = backref(m, sp, stop, ss+1, stopst, lev);
569 if (dp != NULL)
570 return(dp); /* not */
571 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
572 break;
573 case OPLUS_:
574 assert(m->lastpos != NULL);
575 assert(lev+1 <= m->g->nplus);
576 m->lastpos[lev+1] = sp;
577 return(backref(m, sp, stop, ss+1, stopst, lev+1));
578 break;
579 case O_PLUS:
580 if (sp == m->lastpos[lev]) /* last pass matched null */
581 return(backref(m, sp, stop, ss+1, stopst, lev-1));
582 /* try another pass */
583 m->lastpos[lev] = sp;
584 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
585 if (dp == NULL)
586 return(backref(m, sp, stop, ss+1, stopst, lev-1));
587 else
588 return(dp);
589 break;
590 case OCH_: /* find the right one, if any */
591 ssub = ss + 1;
592 esub = ss + OPND(s) - 1;
593 assert(OP(m->g->strip[esub]) == OOR1);
594 for (;;) { /* find first matching branch */
595 dp = backref(m, sp, stop, ssub, esub, lev);
596 if (dp != NULL)
597 return(dp);
598 /* that one missed, try next one */
599 if (OP(m->g->strip[esub]) == O_CH)
600 return(NULL); /* there is none */
601 esub++;
602 assert(OP(m->g->strip[esub]) == OOR2);
603 ssub = esub + 1;
604 esub += OPND(m->g->strip[esub]);
605 if (OP(m->g->strip[esub]) == OOR2)
606 esub--;
607 else
608 assert(OP(m->g->strip[esub]) == O_CH);
609 }
610 break;
611 case OLPAREN: /* must undo assignment if rest fails */
612 i = OPND(s);
613 assert(0 < i && i <= m->g->nsub);
614 offsave = m->pmatch[i].rm_so;
615 m->pmatch[i].rm_so = sp - m->offp;
616 dp = backref(m, sp, stop, ss+1, stopst, lev);
617 if (dp != NULL)
618 return(dp);
619 m->pmatch[i].rm_so = offsave;
620 return(NULL);
621 break;
622 case ORPAREN: /* must undo assignment if rest fails */
623 i = OPND(s);
624 assert(0 < i && i <= m->g->nsub);
625 offsave = m->pmatch[i].rm_eo;
626 m->pmatch[i].rm_eo = sp - m->offp;
627 dp = backref(m, sp, stop, ss+1, stopst, lev);
628 if (dp != NULL)
629 return(dp);
630 m->pmatch[i].rm_eo = offsave;
631 return(NULL);
632 break;
633 default: /* uh oh */
634 assert(nope);
635 break;
636 }
637
638 /* "can't happen" */
639 assert(nope);
640 /* NOTREACHED */
641 return((char *)NULL); /* dummy */
642 }
643
644 /*
645 - fast - step through the string at top speed
646 == static char *fast(register struct match *m, char *start, \
647 == char *stop, sopno startst, sopno stopst);
648 */
649 static char * /* where tentative match ended, or NULL */
fast(m,start,stop,startst,stopst)650 fast(m, start, stop, startst, stopst)
651 register struct match *m;
652 char *start;
653 char *stop;
654 sopno startst;
655 sopno stopst;
656 {
657 register states st = m->st;
658 register states fresh = m->fresh;
659 register states tmp = m->tmp;
660 register char *p = start;
661 register int c = (start == m->beginp) ? OUT : *(start-1);
662 register int lastc; /* previous c */
663 register int flagch;
664 register int i;
665 register char *coldp; /* last p after which no match was underway */
666
667 CLEAR(st);
668 SET1(st, startst);
669 st = step(m->g, startst, stopst, st, NOTHING, st);
670 ASSIGN(fresh, st);
671 SP("start", st, *p);
672 coldp = NULL;
673 for (;;) {
674 /* next character */
675 lastc = c;
676 c = (p == m->endp) ? OUT : *p;
677 if (EQ(st, fresh))
678 coldp = p;
679
680 /* is there an EOL and/or BOL between lastc and c? */
681 flagch = '\0';
682 i = 0;
683 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
684 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
685 flagch = BOL;
686 i = m->g->nbol;
687 }
688 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
689 (c == OUT && !(m->eflags®_NOTEOL)) ) {
690 flagch = (flagch == BOL) ? BOLEOL : EOL;
691 i += m->g->neol;
692 }
693 if (i != 0) {
694 for (; i > 0; i--)
695 st = step(m->g, startst, stopst, st, flagch, st);
696 SP("boleol", st, c);
697 }
698
699 /* how about a word boundary? */
700 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
701 (c != OUT && ISWORD(c)) ) {
702 flagch = BOW;
703 }
704 if ( (lastc != OUT && ISWORD(lastc)) &&
705 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
706 flagch = EOW;
707 }
708 if (flagch == BOW || flagch == EOW) {
709 st = step(m->g, startst, stopst, st, flagch, st);
710 SP("boweow", st, c);
711 }
712
713 /* are we done? */
714 if (ISSET(st, stopst) || p == stop)
715 break; /* NOTE BREAK OUT */
716
717 /* no, we must deal with this character */
718 ASSIGN(tmp, st);
719 ASSIGN(st, fresh);
720 assert(c != OUT);
721 st = step(m->g, startst, stopst, tmp, c, st);
722 SP("aft", st, c);
723 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
724 p++;
725 }
726
727 assert(coldp != NULL);
728 m->coldp = coldp;
729 if (ISSET(st, stopst))
730 return(p+1);
731 else
732 return(NULL);
733 }
734
735 /*
736 - slow - step through the string more deliberately
737 == static char *slow(register struct match *m, char *start, \
738 == char *stop, sopno startst, sopno stopst);
739 */
740 static char * /* where it ended */
slow(m,start,stop,startst,stopst)741 slow(m, start, stop, startst, stopst)
742 register struct match *m;
743 char *start;
744 char *stop;
745 sopno startst;
746 sopno stopst;
747 {
748 register states st = m->st;
749 register states empty = m->empty;
750 register states tmp = m->tmp;
751 register char *p = start;
752 register int c = (start == m->beginp) ? OUT : *(start-1);
753 register int lastc; /* previous c */
754 register int flagch;
755 register int i;
756 register char *matchp; /* last p at which a match ended */
757
758 AT("slow", start, stop, startst, stopst);
759 CLEAR(st);
760 SET1(st, startst);
761 SP("sstart", st, *p);
762 st = step(m->g, startst, stopst, st, NOTHING, st);
763 matchp = NULL;
764 for (;;) {
765 /* next character */
766 lastc = c;
767 c = (p == m->endp) ? OUT : *p;
768
769 /* is there an EOL and/or BOL between lastc and c? */
770 flagch = '\0';
771 i = 0;
772 if ( (lastc == '\n' && m->g->cflags®_NEWLINE) ||
773 (lastc == OUT && !(m->eflags®_NOTBOL)) ) {
774 flagch = BOL;
775 i = m->g->nbol;
776 }
777 if ( (c == '\n' && m->g->cflags®_NEWLINE) ||
778 (c == OUT && !(m->eflags®_NOTEOL)) ) {
779 flagch = (flagch == BOL) ? BOLEOL : EOL;
780 i += m->g->neol;
781 }
782 if (i != 0) {
783 for (; i > 0; i--)
784 st = step(m->g, startst, stopst, st, flagch, st);
785 SP("sboleol", st, c);
786 }
787
788 /* how about a word boundary? */
789 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
790 (c != OUT && ISWORD(c)) ) {
791 flagch = BOW;
792 }
793 if ( (lastc != OUT && ISWORD(lastc)) &&
794 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
795 flagch = EOW;
796 }
797 if (flagch == BOW || flagch == EOW) {
798 st = step(m->g, startst, stopst, st, flagch, st);
799 SP("sboweow", st, c);
800 }
801
802 /* are we done? */
803 if (ISSET(st, stopst))
804 matchp = p;
805 if (EQ(st, empty) || p == stop)
806 break; /* NOTE BREAK OUT */
807
808 /* no, we must deal with this character */
809 ASSIGN(tmp, st);
810 ASSIGN(st, empty);
811 assert(c != OUT);
812 st = step(m->g, startst, stopst, tmp, c, st);
813 SP("saft", st, c);
814 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
815 p++;
816 }
817
818 return(matchp);
819 }
820
821
822 /*
823 - step - map set of states reachable before char to set reachable after
824 == static states step(register struct re_guts *g, sopno start, sopno stop, \
825 == register states bef, int ch, register states aft);
826 == #define BOL (OUT+1)
827 == #define EOL (BOL+1)
828 == #define BOLEOL (BOL+2)
829 == #define NOTHING (BOL+3)
830 == #define BOW (BOL+4)
831 == #define EOW (BOL+5)
832 == #define CODEMAX (BOL+5) // highest code used
833 == #define NONCHAR(c) ((c) > CHAR_MAX)
834 == #define NNONCHAR (CODEMAX-CHAR_MAX)
835 */
836 static states
step(g,start,stop,bef,ch,aft)837 step(g, start, stop, bef, ch, aft)
838 register struct re_guts *g;
839 sopno start; /* start state within strip */
840 sopno stop; /* state after stop state within strip */
841 register states bef; /* states reachable before */
842 int ch; /* character or NONCHAR code */
843 register states aft; /* states already known reachable after */
844 {
845 register cset *cs;
846 register sop s;
847 register sopno pc;
848 register onestate here; /* note, macros know this name */
849 register sopno look;
850 register long i;
851
852 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
853 s = g->strip[pc];
854 switch (OP(s)) {
855 case OEND:
856 assert(pc == stop-1);
857 break;
858 case OCHAR:
859 /* only characters can match */
860 assert(!NONCHAR(ch) || ch != (char)OPND(s));
861 if (ch == (char)OPND(s))
862 FWD(aft, bef, 1);
863 break;
864 case OBOL:
865 if (ch == BOL || ch == BOLEOL)
866 FWD(aft, bef, 1);
867 break;
868 case OEOL:
869 if (ch == EOL || ch == BOLEOL)
870 FWD(aft, bef, 1);
871 break;
872 case OBOW:
873 if (ch == BOW)
874 FWD(aft, bef, 1);
875 break;
876 case OEOW:
877 if (ch == EOW)
878 FWD(aft, bef, 1);
879 break;
880 case OANY:
881 if (!NONCHAR(ch))
882 FWD(aft, bef, 1);
883 break;
884 case OANYOF:
885 cs = &g->sets[OPND(s)];
886 if (!NONCHAR(ch) && CHIN(cs, ch))
887 FWD(aft, bef, 1);
888 break;
889 case OBACK_: /* ignored here */
890 case O_BACK:
891 FWD(aft, aft, 1);
892 break;
893 case OPLUS_: /* forward, this is just an empty */
894 FWD(aft, aft, 1);
895 break;
896 case O_PLUS: /* both forward and back */
897 FWD(aft, aft, 1);
898 i = ISSETBACK(aft, OPND(s));
899 BACK(aft, aft, OPND(s));
900 if (!i && ISSETBACK(aft, OPND(s))) {
901 /* oho, must reconsider loop body */
902 pc -= OPND(s) + 1;
903 INIT(here, pc);
904 }
905 break;
906 case OQUEST_: /* two branches, both forward */
907 FWD(aft, aft, 1);
908 FWD(aft, aft, OPND(s));
909 break;
910 case O_QUEST: /* just an empty */
911 FWD(aft, aft, 1);
912 break;
913 case OLPAREN: /* not significant here */
914 case ORPAREN:
915 FWD(aft, aft, 1);
916 break;
917 case OCH_: /* mark the first two branches */
918 FWD(aft, aft, 1);
919 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
920 FWD(aft, aft, OPND(s));
921 break;
922 case OOR1: /* done a branch, find the O_CH */
923 if (ISSTATEIN(aft, here)) {
924 for (look = 1;
925 OP(s = g->strip[pc+look]) != O_CH;
926 look += OPND(s))
927 assert(OP(s) == OOR2);
928 FWD(aft, aft, look);
929 }
930 break;
931 case OOR2: /* propagate OCH_'s marking */
932 FWD(aft, aft, 1);
933 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
934 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
935 FWD(aft, aft, OPND(s));
936 }
937 break;
938 case O_CH: /* just empty */
939 FWD(aft, aft, 1);
940 break;
941 default: /* ooooops... */
942 assert(nope);
943 break;
944 }
945 }
946
947 return(aft);
948 }
949
950 #ifdef REDEBUG
951 /*
952 - print - print a set of states
953 == #ifdef REDEBUG
954 == static void print(struct match *m, char *caption, states st, \
955 == int ch, FILE *d);
956 == #endif
957 */
958 static void
print(m,caption,st,ch,d)959 print(m, caption, st, ch, d)
960 struct match *m;
961 char *caption;
962 states st;
963 int ch;
964 FILE *d;
965 {
966 register struct re_guts *g = m->g;
967 register int i;
968 register int first = 1;
969
970 if (!(m->eflags®_TRACE))
971 return;
972
973 fprintf(d, "%s", caption);
974 if (ch != '\0')
975 fprintf(d, " %s", pchar(ch));
976 for (i = 0; i < g->nstates; i++)
977 if (ISSET(st, i)) {
978 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
979 first = 0;
980 }
981 fprintf(d, "\n");
982 }
983
984 /*
985 - at - print current situation
986 == #ifdef REDEBUG
987 == static void at(struct match *m, char *title, char *start, char *stop, \
988 == sopno startst, sopno stopst);
989 == #endif
990 */
991 static void
at(m,title,start,stop,startst,stopst)992 at(m, title, start, stop, startst, stopst)
993 struct match *m;
994 char *title;
995 char *start;
996 char *stop;
997 sopno startst;
998 sopno stopst;
999 {
1000 if (!(m->eflags®_TRACE))
1001 return;
1002
1003 printf("%s %s-", title, pchar(*start));
1004 printf("%s ", pchar(*stop));
1005 printf("%ld-%ld\n", (long)startst, (long)stopst);
1006 }
1007
1008 #ifndef PCHARDONE
1009 #define PCHARDONE /* never again */
1010 /*
1011 - pchar - make a character printable
1012 == #ifdef REDEBUG
1013 == static char *pchar(int ch);
1014 == #endif
1015 *
1016 * Is this identical to regchar() over in debug.c? Well, yes. But a
1017 * duplicate here avoids having a debugging-capable regexec.o tied to
1018 * a matching debug.o, and this is convenient. It all disappears in
1019 * the non-debug compilation anyway, so it doesn't matter much.
1020 */
1021 static char * /* -> representation */
pchar(ch)1022 pchar(ch)
1023 int ch;
1024 {
1025 static char pbuf[10];
1026
1027 if (isprint(ch) || ch == ' ')
1028 sprintf(pbuf, "%c", ch);
1029 else
1030 sprintf(pbuf, "\\%o", ch);
1031 return(pbuf);
1032 }
1033 #endif
1034 #endif
1035
1036 #undef matcher
1037 #undef fast
1038 #undef slow
1039 #undef dissect
1040 #undef backref
1041 #undef step
1042 #undef print
1043 #undef at
1044 #undef match
1045