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