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