]> git.saurik.com Git - apple/libc.git/blob - regex/engine.c
Libc-763.12.tar.gz
[apple/libc.git] / regex / engine.c
1 /*-
2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Henry Spencer.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)engine.c 8.5 (Berkeley) 3/20/94
34 */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/regex/engine.c,v 1.23 2009/09/16 06:32:23 dds Exp $");
38
39 /*
40 * The matching engine and friends. This file is #included by regexec.c
41 * after suitable #defines of a variety of macros used herein, so that
42 * different state representations can be used without duplicating masses
43 * of code.
44 */
45
46 #ifdef SNAMES
47 #define matcher smatcher
48 #define fast sfast
49 #define slow sslow
50 #define dissect sdissect
51 #define backref sbackref
52 #define step sstep
53 #define print sprint
54 #define at sat
55 #define match smat
56 #endif
57 #ifdef LNAMES
58 #define matcher lmatcher
59 #define fast lfast
60 #define slow lslow
61 #define dissect ldissect
62 #define backref lbackref
63 #define step lstep
64 #define print lprint
65 #define at lat
66 #define match lmat
67 #endif
68 #ifdef MNAMES
69 #define matcher mmatcher
70 #define fast mfast
71 #define slow mslow
72 #define dissect mdissect
73 #define backref mbackref
74 #define step mstep
75 #define print mprint
76 #define at mat
77 #define match mmat
78 #endif
79
80 /* another structure passed up and down to avoid zillions of parameters */
81 struct match {
82 struct re_guts *g;
83 int eflags;
84 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
85 const char *offp; /* offsets work from here */
86 const char *beginp; /* start of string -- virtual NUL precedes */
87 const char *endp; /* end of string -- virtual NUL here */
88 const char *coldp; /* can be no match starting before here */
89 const char **lastpos; /* [nplus+1] */
90 STATEVARS;
91 states st; /* current states */
92 states fresh; /* states for a fresh start */
93 states tmp; /* temporary */
94 states empty; /* empty set of states */
95 mbstate_t mbs; /* multibyte conversion state */
96 };
97
98 /* ========= begin header generated by ./mkh ========= */
99 #ifdef __cplusplus
100 extern "C" {
101 #endif
102
103 /* === engine.c === */
104 static int matcher(struct re_guts *g, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
105 static const char *dissect(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
106 static const char *backref(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst, sopno lev, int);
107 static const char *fast(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
108 static const char *slow(struct match *m, const char *start, const char *stop, sopno startst, sopno stopst);
109 static states step(struct re_guts *g, sopno start, sopno stop, states bef, wint_t ch, states aft);
110 #define MAX_RECURSION 100
111 #define BOL (OUT-1)
112 #define EOL (BOL-1)
113 #define BOLEOL (BOL-2)
114 #define NOTHING (BOL-3)
115 #define BOW (BOL-4)
116 #define EOW (BOL-5)
117 #define BADCHAR (BOL-6)
118 #define NONCHAR(c) ((c) <= OUT)
119 #ifdef REDEBUG
120 static void print(struct match *m, const char *caption, states st, int ch, FILE *d);
121 #endif
122 #ifdef REDEBUG
123 static void at(struct match *m, const char *title, const char *start, const char *stop, sopno startst, sopno stopst);
124 #endif
125 #ifdef REDEBUG
126 static const char *pchar(int ch);
127 #endif
128
129 #ifdef __cplusplus
130 }
131 #endif
132 /* ========= end header generated by ./mkh ========= */
133
134 #ifdef REDEBUG
135 #define SP(t, s, c) print(m, t, s, c, stdout)
136 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
137 #define NOTE(str) { if (m->eflags&REG_TRACE) printf("=%s\n", (str)); }
138 #else
139 #define SP(t, s, c) /* nothing */
140 #define AT(t, p1, p2, s1, s2) /* nothing */
141 #define NOTE(s) /* nothing */
142 #endif
143
144 /*
145 - matcher - the actual matching engine
146 == static int matcher(struct re_guts *g, const char *string, \
147 == size_t nmatch, regmatch_t pmatch[], int eflags);
148 */
149 static int /* 0 success, REG_NOMATCH failure */
150 matcher(struct re_guts *g,
151 const char *string,
152 size_t nmatch,
153 regmatch_t pmatch[],
154 int eflags)
155 {
156 const char *endp;
157 int i;
158 struct match mv;
159 struct match *m = &mv;
160 const char *dp;
161 const sopno gf = g->firststate+1; /* +1 for OEND */
162 const sopno gl = g->laststate;
163 const char *start;
164 const char *stop;
165 /* Boyer-Moore algorithms variables */
166 const char *pp;
167 int cj, mj;
168 const char *mustfirst;
169 const char *mustlast;
170 int *matchjump;
171 int *charjump;
172
173 /* simplify the situation where possible */
174 if (g->cflags&REG_NOSUB)
175 nmatch = 0;
176 if (eflags&REG_STARTEND) {
177 start = string + pmatch[0].rm_so;
178 stop = string + pmatch[0].rm_eo;
179 } else {
180 start = string;
181 stop = start + strlen(start);
182 }
183 if (stop < start)
184 return(REG_INVARG);
185
186 /* prescreening; this does wonders for this rather slow code */
187 if (g->must != NULL) {
188 if (g->charjump != NULL && g->matchjump != NULL) {
189 mustfirst = g->must;
190 mustlast = g->must + g->mlen - 1;
191 charjump = g->charjump;
192 matchjump = g->matchjump;
193 pp = mustlast;
194 for (dp = start+g->mlen-1; dp < stop;) {
195 /* Fast skip non-matches */
196 while (dp < stop && charjump[(int)*dp])
197 dp += charjump[(int)*dp];
198
199 if (dp >= stop)
200 break;
201
202 /* Greedy matcher */
203 /* We depend on not being used for
204 * for strings of length 1
205 */
206 while (*--dp == *--pp && pp != mustfirst);
207
208 if (*dp == *pp)
209 break;
210
211 /* Jump to next possible match */
212 mj = matchjump[pp - mustfirst];
213 cj = charjump[(int)*dp];
214 dp += (cj < mj ? mj : cj);
215 pp = mustlast;
216 }
217 if (pp != mustfirst)
218 return(REG_NOMATCH);
219 } else {
220 for (dp = start; dp < stop; dp++)
221 if (*dp == g->must[0] &&
222 stop - dp >= g->mlen &&
223 memcmp(dp, g->must, (size_t)g->mlen) == 0)
224 break;
225 if (dp == stop) /* we didn't find g->must */
226 return(REG_NOMATCH);
227 }
228 }
229
230 /* match struct setup */
231 m->g = g;
232 m->eflags = eflags;
233 m->pmatch = NULL;
234 m->lastpos = NULL;
235 m->offp = string;
236 m->beginp = start;
237 m->endp = stop;
238 STATESETUP(m, 4);
239 SETUP(m->st);
240 SETUP(m->fresh);
241 SETUP(m->tmp);
242 SETUP(m->empty);
243 CLEAR(m->empty);
244 ZAPSTATE(&m->mbs);
245
246 /* Adjust start according to moffset, to speed things up */
247 if (g->moffset > -1)
248 start = ((dp - g->moffset) < start) ? start : dp - g->moffset;
249
250 SP("mloop", m->st, *start);
251
252 /* this loop does only one repetition except for backrefs */
253 for (;;) {
254 endp = fast(m, start, stop, gf, gl);
255 if (endp == NULL) { /* a miss */
256 if (m->pmatch != NULL)
257 free((char *)m->pmatch);
258 if (m->lastpos != NULL)
259 free((char *)m->lastpos);
260 STATETEARDOWN(m);
261 return(REG_NOMATCH);
262 }
263 if (nmatch == 0 && !g->backrefs)
264 break; /* no further info needed */
265
266 /* where? */
267 assert(m->coldp != NULL);
268 for (;;) {
269 NOTE("finding start");
270 endp = slow(m, m->coldp, stop, gf, gl);
271 if (endp != NULL)
272 break;
273 assert(m->coldp < m->endp);
274 m->coldp += XMBRTOWC(NULL, m->coldp,
275 m->endp - m->coldp, &m->mbs, 0, g->loc);
276 }
277 if (nmatch == 1 && !g->backrefs)
278 break; /* no further info needed */
279
280 /* oh my, he wants the subexpressions... */
281 if (m->pmatch == NULL)
282 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
283 sizeof(regmatch_t));
284 if (m->pmatch == NULL) {
285 STATETEARDOWN(m);
286 return(REG_ESPACE);
287 }
288 for (i = 1; i <= m->g->nsub; i++)
289 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
290 if (!g->backrefs && !(m->eflags&REG_BACKR)) {
291 NOTE("dissecting");
292 dp = dissect(m, m->coldp, endp, gf, gl);
293 } else {
294 if (g->nplus > 0 && m->lastpos == NULL)
295 m->lastpos = malloc((g->nplus+1) *
296 sizeof(const char *));
297 if (g->nplus > 0 && m->lastpos == NULL) {
298 free(m->pmatch);
299 STATETEARDOWN(m);
300 return(REG_ESPACE);
301 }
302 NOTE("backref dissect");
303 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
304 }
305 if (dp != NULL)
306 break;
307
308 /* uh-oh... we couldn't find a subexpression-level match */
309 assert(g->backrefs); /* must be back references doing it */
310 assert(g->nplus == 0 || m->lastpos != NULL);
311 for (;;) {
312 if (dp != NULL || endp <= m->coldp)
313 break; /* defeat */
314 NOTE("backoff");
315 endp = slow(m, m->coldp, endp-1, gf, gl);
316 if (endp == NULL)
317 break; /* defeat */
318 /* try it on a shorter possibility */
319 #ifndef NDEBUG
320 for (i = 1; i <= m->g->nsub; i++) {
321 assert(m->pmatch[i].rm_so == -1);
322 assert(m->pmatch[i].rm_eo == -1);
323 }
324 #endif
325 NOTE("backoff dissect");
326 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0, 0);
327 }
328 assert(dp == NULL || dp == endp);
329 if (dp != NULL) /* found a shorter one */
330 break;
331
332 /* despite initial appearances, there is no match here */
333 NOTE("false alarm");
334 /* recycle starting later */
335 start = m->coldp + XMBRTOWC(NULL, m->coldp,
336 stop - m->coldp, &m->mbs, 0, g->loc);
337 assert(start <= stop);
338 }
339
340 /* fill in the details if requested */
341 if (nmatch > 0) {
342 pmatch[0].rm_so = m->coldp - m->offp;
343 pmatch[0].rm_eo = endp - m->offp;
344 }
345 if (nmatch > 1) {
346 assert(m->pmatch != NULL);
347 for (i = 1; i < nmatch; i++)
348 if (i <= m->g->nsub)
349 pmatch[i] = m->pmatch[i];
350 else {
351 pmatch[i].rm_so = -1;
352 pmatch[i].rm_eo = -1;
353 }
354 }
355
356 if (m->pmatch != NULL)
357 free((char *)m->pmatch);
358 if (m->lastpos != NULL)
359 free((char *)m->lastpos);
360 STATETEARDOWN(m);
361 return(0);
362 }
363
364 /*
365 - dissect - figure out what matched what, no back references
366 == static const char *dissect(struct match *m, const char *start, \
367 == const char *stop, sopno startst, sopno stopst);
368 */
369 static const char * /* == stop (success) always */
370 dissect(struct match *m,
371 const char *start,
372 const char *stop,
373 sopno startst,
374 sopno stopst)
375 {
376 int i;
377 sopno ss; /* start sop of current subRE */
378 sopno es; /* end sop of current subRE */
379 const char *sp; /* start of string matched by it */
380 const char *stp; /* string matched by it cannot pass here */
381 const char *rest; /* start of rest of string */
382 const char *tail; /* string unmatched by rest of RE */
383 sopno ssub; /* start sop of subsubRE */
384 sopno esub; /* end sop of subsubRE */
385 const char *ssp; /* start of string matched by subsubRE */
386 const char *sep; /* end of string matched by subsubRE */
387 const char *oldssp; /* previous ssp */
388 const char *dp;
389
390 AT("diss", start, stop, startst, stopst);
391 sp = start;
392 for (ss = startst; ss < stopst; ss = es) {
393 /* identify end of subRE */
394 es = ss;
395 switch (OP(m->g->strip[es])) {
396 case OPLUS_:
397 case OQUEST_:
398 es += OPND(m->g->strip[es]);
399 break;
400 case OCH_:
401 while (OP(m->g->strip[es]) != O_CH)
402 es += OPND(m->g->strip[es]);
403 break;
404 }
405 es++;
406
407 /* figure out what it matched */
408 switch (OP(m->g->strip[ss])) {
409 case OEND:
410 assert(nope);
411 break;
412 case OCHAR:
413 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0, m->g->loc);
414 break;
415 case OBOL:
416 case OEOL:
417 case OBOW:
418 case OEOW:
419 break;
420 case OANY:
421 case OANYOF:
422 sp += XMBRTOWC(NULL, sp, stop - start, &m->mbs, 0, m->g->loc);
423 break;
424 case OBACK_:
425 case O_BACK:
426 assert(nope);
427 break;
428 /* cases where length of match is hard to find */
429 case OQUEST_:
430 stp = stop;
431 for (;;) {
432 /* how long could this one be? */
433 rest = slow(m, sp, stp, ss, es);
434 assert(rest != NULL); /* it did match */
435 /* could the rest match the rest? */
436 tail = slow(m, rest, stop, es, stopst);
437 if (tail == stop)
438 break; /* yes! */
439 /* no -- try a shorter match for this one */
440 stp = rest - 1;
441 assert(stp >= sp); /* it did work */
442 }
443 ssub = ss + 1;
444 esub = es - 1;
445 /* did innards match? */
446 if (slow(m, sp, rest, ssub, esub) != NULL) {
447 dp = dissect(m, sp, rest, ssub, esub);
448 assert(dp == rest);
449 } else /* no */
450 assert(sp == rest);
451 sp = rest;
452 break;
453 case OPLUS_:
454 stp = stop;
455 for (;;) {
456 /* how long could this one be? */
457 rest = slow(m, sp, stp, ss, es);
458 assert(rest != NULL); /* it did match */
459 /* could the rest match the rest? */
460 tail = slow(m, rest, stop, es, stopst);
461 if (tail == stop)
462 break; /* yes! */
463 /* no -- try a shorter match for this one */
464 stp = rest - 1;
465 assert(stp >= sp); /* it did work */
466 }
467 ssub = ss + 1;
468 esub = es - 1;
469 ssp = sp;
470 oldssp = ssp;
471 for (;;) { /* find last match of innards */
472 sep = slow(m, ssp, rest, ssub, esub);
473 if (sep == NULL || sep == ssp)
474 break; /* failed or matched null */
475 oldssp = ssp; /* on to next try */
476 ssp = sep;
477 }
478 if (sep == NULL) {
479 /* last successful match */
480 sep = ssp;
481 ssp = oldssp;
482 }
483 else if (tail==rest) {
484 /* Fix for test expr 105 */
485 ssp = oldssp;
486 }
487 assert(sep == rest); /* must exhaust substring */
488 assert(slow(m, ssp, sep, ssub, esub) == rest);
489 dp = dissect(m, ssp, sep, ssub, esub);
490 assert(dp == sep);
491 sp = rest;
492 break;
493 case OCH_:
494 stp = stop;
495 for (;;) {
496 /* how long could this one be? */
497 rest = slow(m, sp, stp, ss, es);
498 assert(rest != NULL); /* it did match */
499 /* could the rest match the rest? */
500 tail = slow(m, rest, stop, es, stopst);
501 if (tail == stop)
502 break; /* yes! */
503 /* no -- try a shorter match for this one */
504 stp = rest - 1;
505 assert(stp >= sp); /* it did work */
506 }
507 ssub = ss + 1;
508 esub = ss + OPND(m->g->strip[ss]) - 1;
509 assert(OP(m->g->strip[esub]) == OOR1);
510 for (;;) { /* find first matching branch */
511 if (slow(m, sp, rest, ssub, esub) == rest)
512 break; /* it matched all of it */
513 /* that one missed, try next one */
514 assert(OP(m->g->strip[esub]) == OOR1);
515 esub++;
516 assert(OP(m->g->strip[esub]) == OOR2);
517 ssub = esub + 1;
518 esub += OPND(m->g->strip[esub]);
519 if (OP(m->g->strip[esub]) == OOR2)
520 esub--;
521 else
522 assert(OP(m->g->strip[esub]) == O_CH);
523 }
524 dp = dissect(m, sp, rest, ssub, esub);
525 assert(dp == rest);
526 sp = rest;
527 break;
528 case O_PLUS:
529 case O_QUEST:
530 case OOR1:
531 case OOR2:
532 case O_CH:
533 assert(nope);
534 break;
535 case OLPAREN:
536 i = OPND(m->g->strip[ss]);
537 assert(0 < i && i <= m->g->nsub);
538 m->pmatch[i].rm_so = sp - m->offp;
539 /* fix for T.regcomp 43: don't remember previous
540 subexpression matches beyond the current one (i) */
541 i++;
542 while (i<= m->g->nsub) {
543 m->pmatch[i].rm_so = -1;
544 m->pmatch[i].rm_eo = -1;
545 i++;
546 }
547 break;
548 case ORPAREN:
549 i = OPND(m->g->strip[ss]);
550 assert(0 < i && i <= m->g->nsub);
551 m->pmatch[i].rm_eo = sp - m->offp;
552 break;
553 default: /* uh oh */
554 assert(nope);
555 break;
556 }
557 }
558
559 assert(sp == stop);
560 return(sp);
561 }
562
563 /*
564 - backref - figure out what matched what, figuring in back references
565 == static const char *backref(struct match *m, const char *start, \
566 == const char *stop, sopno startst, sopno stopst, sopno lev);
567 */
568 static const char * /* == stop (success) or NULL (failure) */
569 backref(struct match *m,
570 const char *start,
571 const char *stop,
572 sopno startst,
573 sopno stopst,
574 sopno lev, /* PLUS nesting level */
575 int rec)
576 {
577 int i;
578 sopno ss; /* start sop of current subRE */
579 const char *sp; /* start of string matched by it */
580 sopno ssub; /* start sop of subsubRE */
581 sopno esub; /* end sop of subsubRE */
582 const char *ssp; /* start of string matched by subsubRE */
583 const char *dp;
584 size_t len;
585 int hard;
586 sop s;
587 regoff_t offsave;
588 cset *cs;
589 wint_t wc;
590
591 AT("back", start, stop, startst, stopst);
592 sp = start;
593
594 /* get as far as we can with easy stuff */
595 hard = 0;
596 for (ss = startst; !hard && ss < stopst; ss++)
597 switch (OP(s = m->g->strip[ss])) {
598 case OCHAR:
599 if (sp == stop)
600 return(NULL);
601 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR, m->g->loc);
602 if (wc != OPND(s))
603 return(NULL);
604 break;
605 case OANY:
606 if (sp == stop)
607 return(NULL);
608 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR, m->g->loc);
609 if (wc == BADCHAR)
610 return (NULL);
611 break;
612 case OANYOF:
613 if (sp == stop)
614 return (NULL);
615 cs = &m->g->sets[OPND(s)];
616 sp += XMBRTOWC(&wc, sp, stop - sp, &m->mbs, BADCHAR, m->g->loc);
617 if (wc == BADCHAR || !CHIN(cs, wc, m->g->loc))
618 return(NULL);
619 break;
620 case OBOL:
621 if ( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
622 (sp < m->endp && *(sp-1) == '\n' &&
623 (m->g->cflags&REG_NEWLINE)) )
624 { /* yes */ }
625 else
626 return(NULL);
627 break;
628 case OEOL:
629 if ( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
630 (sp < m->endp && *sp == '\n' &&
631 (m->g->cflags&REG_NEWLINE)) )
632 { /* yes */ }
633 else
634 return(NULL);
635 break;
636 case OBOW:
637 if (( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
638 (sp < m->endp && *(sp-1) == '\n' &&
639 (m->g->cflags&REG_NEWLINE)) ||
640 (sp > m->beginp &&
641 !ISWORD(*(sp-1), m->g->loc)) ) &&
642 (sp < m->endp && ISWORD(*sp, m->g->loc)) )
643 { /* yes */ }
644 else
645 return(NULL);
646 break;
647 case OEOW:
648 if (( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
649 (sp < m->endp && *sp == '\n' &&
650 (m->g->cflags&REG_NEWLINE)) ||
651 (sp < m->endp && !ISWORD(*sp, m->g->loc)) ) &&
652 (sp > m->beginp && ISWORD(*(sp-1), m->g->loc)) )
653 { /* yes */ }
654 else
655 return(NULL);
656 break;
657 case O_QUEST:
658 break;
659 case OOR1: /* matches null but needs to skip */
660 ss++;
661 s = m->g->strip[ss];
662 do {
663 assert(OP(s) == OOR2);
664 ss += OPND(s);
665 } while (OP(s = m->g->strip[ss]) != O_CH);
666 /* note that the ss++ gets us past the O_CH */
667 break;
668 default: /* have to make a choice */
669 hard = 1;
670 break;
671 }
672 if (!hard) { /* that was it! */
673 if (sp != stop)
674 return(NULL);
675 return(sp);
676 }
677 ss--; /* adjust for the for's final increment */
678
679 /* the hard stuff */
680 AT("hard", sp, stop, ss, stopst);
681 s = m->g->strip[ss];
682 switch (OP(s)) {
683 case OBACK_: /* the vilest depths */
684 i = OPND(s);
685 assert(0 < i && i <= m->g->nsub);
686 if (m->pmatch[i].rm_eo == -1)
687 return(NULL);
688 assert(m->pmatch[i].rm_so != -1);
689 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
690 if (len == 0 && rec++ > MAX_RECURSION)
691 return(NULL);
692 assert(stop - m->beginp >= len);
693 if (sp > stop - len)
694 return(NULL); /* not enough left to match */
695 ssp = m->offp + m->pmatch[i].rm_so;
696 if (memcmp(sp, ssp, len) != 0)
697 return(NULL);
698 while (m->g->strip[ss] != SOP(O_BACK, i))
699 ss++;
700 return(backref(m, sp+len, stop, ss+1, stopst, lev, rec));
701 break;
702 case OQUEST_: /* to null or not */
703 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
704 if (dp != NULL)
705 return(dp); /* not */
706 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev, rec));
707 break;
708 case OPLUS_:
709 assert(m->lastpos != NULL);
710 assert(lev+1 <= m->g->nplus);
711 m->lastpos[lev+1] = sp;
712 return(backref(m, sp, stop, ss+1, stopst, lev+1, rec));
713 break;
714 case O_PLUS:
715 if (sp == m->lastpos[lev]) /* last pass matched null */
716 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
717 /* try another pass */
718 m->lastpos[lev] = sp;
719 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev, rec);
720 if (dp == NULL)
721 return(backref(m, sp, stop, ss+1, stopst, lev-1, rec));
722 else
723 return(dp);
724 break;
725 case OCH_: /* find the right one, if any */
726 ssub = ss + 1;
727 esub = ss + OPND(s) - 1;
728 assert(OP(m->g->strip[esub]) == OOR1);
729 for (;;) { /* find first matching branch */
730 dp = backref(m, sp, stop, ssub, esub, lev, rec);
731 if (dp != NULL)
732 return(dp);
733 /* that one missed, try next one */
734 if (OP(m->g->strip[esub]) == O_CH)
735 return(NULL); /* there is none */
736 esub++;
737 assert(OP(m->g->strip[esub]) == OOR2);
738 ssub = esub + 1;
739 esub += OPND(m->g->strip[esub]);
740 if (OP(m->g->strip[esub]) == OOR2)
741 esub--;
742 else
743 assert(OP(m->g->strip[esub]) == O_CH);
744 }
745 break;
746 case OLPAREN: /* must undo assignment if rest fails */
747 i = OPND(s);
748 assert(0 < i && i <= m->g->nsub);
749 offsave = m->pmatch[i].rm_so;
750 m->pmatch[i].rm_so = sp - m->offp;
751 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
752 if (dp != NULL)
753 return(dp);
754 m->pmatch[i].rm_so = offsave;
755 return(NULL);
756 break;
757 case ORPAREN: /* must undo assignment if rest fails */
758 i = OPND(s);
759 assert(0 < i && i <= m->g->nsub);
760 offsave = m->pmatch[i].rm_eo;
761 m->pmatch[i].rm_eo = sp - m->offp;
762 dp = backref(m, sp, stop, ss+1, stopst, lev, rec);
763 if (dp != NULL)
764 return(dp);
765 m->pmatch[i].rm_eo = offsave;
766 return(NULL);
767 break;
768 default: /* uh oh */
769 assert(nope);
770 break;
771 }
772
773 /* "can't happen" */
774 assert(nope);
775 /* NOTREACHED */
776 return "shut up gcc";
777 }
778
779 /*
780 - fast - step through the string at top speed
781 == static const char *fast(struct match *m, const char *start, \
782 == const char *stop, sopno startst, sopno stopst);
783 */
784 static const char * /* where tentative match ended, or NULL */
785 fast( struct match *m,
786 const char *start,
787 const char *stop,
788 sopno startst,
789 sopno stopst)
790 {
791 states st = m->st;
792 states fresh = m->fresh;
793 states tmp = m->tmp;
794 const char *p = start;
795 wint_t c;
796 wint_t lastc; /* previous c */
797 wint_t flagch;
798 int i;
799 const char *coldp; /* last p after which no match was underway */
800 size_t clen;
801
802 CLEAR(st);
803 SET1(st, startst);
804 SP("fast", st, *p);
805 st = step(m->g, startst, stopst, st, NOTHING, st);
806 ASSIGN(fresh, st);
807 SP("start", st, *p);
808 coldp = NULL;
809 if (start == m->beginp)
810 c = OUT;
811 else {
812 /*
813 * XXX Wrong if the previous character was multi-byte.
814 * Newline never is (in encodings supported by FreeBSD),
815 * so this only breaks the ISWORD tests below.
816 */
817 c = (uch)*(start - 1);
818 }
819 for (;;) {
820 /* next character */
821 lastc = c;
822 if (p == m->endp) {
823 clen = 0;
824 c = OUT;
825 } else
826 clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR, m->g->loc);
827 if (EQ(st, fresh))
828 coldp = p;
829
830 /* is there an EOL and/or BOL between lastc and c? */
831 flagch = '\0';
832 i = 0;
833 if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
834 (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
835 flagch = BOL;
836 i = m->g->nbol;
837 }
838 if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
839 (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
840 flagch = (flagch == BOL) ? BOLEOL : EOL;
841 i += m->g->neol;
842 }
843 if (i != 0) {
844 for (; i > 0; i--)
845 st = step(m->g, startst, stopst, st, flagch, st);
846 SP("boleol", st, c);
847 }
848
849 /* how about a word boundary? */
850 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc, m->g->loc))) &&
851 (c != OUT && ISWORD(c, m->g->loc)) ) {
852 flagch = BOW;
853 }
854 if ( (lastc != OUT && ISWORD(lastc, m->g->loc)) &&
855 (flagch == EOL || (c != OUT && !ISWORD(c, m->g->loc))) ) {
856 flagch = EOW;
857 }
858 if (flagch == BOW || flagch == EOW) {
859 st = step(m->g, startst, stopst, st, flagch, st);
860 SP("boweow", st, c);
861 }
862
863 /* are we done? */
864 if (ISSET(st, stopst) || p == stop || clen > stop - p)
865 break; /* NOTE BREAK OUT */
866
867 /* no, we must deal with this character */
868 ASSIGN(tmp, st);
869 ASSIGN(st, fresh);
870 assert(c != OUT);
871 st = step(m->g, startst, stopst, tmp, c, st);
872 SP("aft", st, c);
873 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
874 p += clen;
875 }
876
877 assert(coldp != NULL);
878 m->coldp = coldp;
879 if (ISSET(st, stopst))
880 return(p+XMBRTOWC(NULL, p, stop - p, &m->mbs, 0, m->g->loc));
881 else
882 return(NULL);
883 }
884
885 /*
886 - slow - step through the string more deliberately
887 == static const char *slow(struct match *m, const char *start, \
888 == const char *stop, sopno startst, sopno stopst);
889 */
890 static const char * /* where it ended */
891 slow( struct match *m,
892 const char *start,
893 const char *stop,
894 sopno startst,
895 sopno stopst)
896 {
897 states st = m->st;
898 states empty = m->empty;
899 states tmp = m->tmp;
900 const char *p = start;
901 wint_t c;
902 wint_t lastc; /* previous c */
903 wint_t flagch;
904 int i;
905 const char *matchp; /* last p at which a match ended */
906 size_t clen;
907
908 AT("slow", start, stop, startst, stopst);
909 CLEAR(st);
910 SET1(st, startst);
911 SP("sstart", st, *p);
912 st = step(m->g, startst, stopst, st, NOTHING, st);
913 matchp = NULL;
914 if (start == m->beginp)
915 c = OUT;
916 else {
917 /*
918 * XXX Wrong if the previous character was multi-byte.
919 * Newline never is (in encodings supported by FreeBSD),
920 * so this only breaks the ISWORD tests below.
921 */
922 c = (uch)*(start - 1);
923 }
924 for (;;) {
925 /* next character */
926 lastc = c;
927 if (p == m->endp) {
928 c = OUT;
929 clen = 0;
930 } else
931 clen = XMBRTOWC(&c, p, m->endp - p, &m->mbs, BADCHAR, m->g->loc);
932
933 /* is there an EOL and/or BOL between lastc and c? */
934 flagch = '\0';
935 i = 0;
936 if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
937 (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
938 flagch = BOL;
939 i = m->g->nbol;
940 }
941 if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
942 (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
943 flagch = (flagch == BOL) ? BOLEOL : EOL;
944 i += m->g->neol;
945 }
946 if (i != 0) {
947 for (; i > 0; i--)
948 st = step(m->g, startst, stopst, st, flagch, st);
949 SP("sboleol", st, c);
950 }
951
952 /* how about a word boundary? */
953 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc, m->g->loc))) &&
954 (c != OUT && ISWORD(c, m->g->loc)) ) {
955 flagch = BOW;
956 }
957 if ( (lastc != OUT && ISWORD(lastc, m->g->loc)) &&
958 (flagch == EOL || (c != OUT && !ISWORD(c, m->g->loc))) ) {
959 flagch = EOW;
960 }
961 if (flagch == BOW || flagch == EOW) {
962 st = step(m->g, startst, stopst, st, flagch, st);
963 SP("sboweow", st, c);
964 }
965
966 /* are we done? */
967 if (ISSET(st, stopst))
968 matchp = p;
969 if (EQ(st, empty) || p == stop || clen > stop - p)
970 break; /* NOTE BREAK OUT */
971
972 /* no, we must deal with this character */
973 ASSIGN(tmp, st);
974 ASSIGN(st, empty);
975 assert(c != OUT);
976 st = step(m->g, startst, stopst, tmp, c, st);
977 SP("saft", st, c);
978 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
979 p += clen;
980 }
981
982 return(matchp);
983 }
984
985
986 /*
987 - step - map set of states reachable before char to set reachable after
988 == static states step(struct re_guts *g, sopno start, sopno stop, \
989 == states bef, int ch, states aft);
990 == #define BOL (OUT-1)
991 == #define EOL (BOL-1)
992 == #define BOLEOL (BOL-2)
993 == #define NOTHING (BOL-3)
994 == #define BOW (BOL-4)
995 == #define EOW (BOL-5)
996 == #define BADCHAR (BOL-6)
997 == #define NONCHAR(c) ((c) <= OUT)
998 */
999 static states
1000 step(struct re_guts *g,
1001 sopno start, /* start state within strip */
1002 sopno stop, /* state after stop state within strip */
1003 states bef, /* states reachable before */
1004 wint_t ch, /* character or NONCHAR code */
1005 states aft) /* states already known reachable after */
1006 {
1007 cset *cs;
1008 sop s;
1009 sopno pc;
1010 onestate here; /* note, macros know this name */
1011 sopno look;
1012 int i;
1013
1014 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
1015 s = g->strip[pc];
1016 switch (OP(s)) {
1017 case OEND:
1018 assert(pc == stop-1);
1019 break;
1020 case OCHAR:
1021 /* only characters can match */
1022 assert(!NONCHAR(ch) || ch != OPND(s));
1023 if (ch == OPND(s))
1024 FWD(aft, bef, 1);
1025 break;
1026 case OBOL:
1027 if (ch == BOL || ch == BOLEOL)
1028 FWD(aft, bef, 1);
1029 break;
1030 case OEOL:
1031 if (ch == EOL || ch == BOLEOL)
1032 FWD(aft, bef, 1);
1033 break;
1034 case OBOW:
1035 if (ch == BOW)
1036 FWD(aft, bef, 1);
1037 break;
1038 case OEOW:
1039 if (ch == EOW)
1040 FWD(aft, bef, 1);
1041 break;
1042 case OANY:
1043 if (!NONCHAR(ch))
1044 FWD(aft, bef, 1);
1045 break;
1046 case OANYOF:
1047 cs = &g->sets[OPND(s)];
1048 if (!NONCHAR(ch) && CHIN(cs, ch, g->loc))
1049 FWD(aft, bef, 1);
1050 break;
1051 case OBACK_: /* ignored here */
1052 case O_BACK:
1053 FWD(aft, aft, 1);
1054 break;
1055 case OPLUS_: /* forward, this is just an empty */
1056 FWD(aft, aft, 1);
1057 break;
1058 case O_PLUS: /* both forward and back */
1059 FWD(aft, aft, 1);
1060 i = ISSETBACK(aft, OPND(s));
1061 BACK(aft, aft, OPND(s));
1062 if (!i && ISSETBACK(aft, OPND(s))) {
1063 /* oho, must reconsider loop body */
1064 pc -= OPND(s) + 1;
1065 INIT(here, pc);
1066 }
1067 break;
1068 case OQUEST_: /* two branches, both forward */
1069 FWD(aft, aft, 1);
1070 FWD(aft, aft, OPND(s));
1071 break;
1072 case O_QUEST: /* just an empty */
1073 FWD(aft, aft, 1);
1074 break;
1075 case OLPAREN: /* not significant here */
1076 case ORPAREN:
1077 FWD(aft, aft, 1);
1078 break;
1079 case OCH_: /* mark the first two branches */
1080 FWD(aft, aft, 1);
1081 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1082 FWD(aft, aft, OPND(s));
1083 break;
1084 case OOR1: /* done a branch, find the O_CH */
1085 if (ISSTATEIN(aft, here)) {
1086 for (look = 1;
1087 OP(s = g->strip[pc+look]) != O_CH;
1088 look += OPND(s))
1089 assert(OP(s) == OOR2);
1090 FWD(aft, aft, look + 1);
1091 }
1092 break;
1093 case OOR2: /* propagate OCH_'s marking */
1094 FWD(aft, aft, 1);
1095 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
1096 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1097 FWD(aft, aft, OPND(s));
1098 }
1099 break;
1100 case O_CH: /* just empty */
1101 FWD(aft, aft, 1);
1102 break;
1103 default: /* ooooops... */
1104 assert(nope);
1105 break;
1106 }
1107 }
1108
1109 return(aft);
1110 }
1111
1112 #ifdef REDEBUG
1113 /*
1114 - print - print a set of states
1115 == #ifdef REDEBUG
1116 == static void print(struct match *m, const char *caption, states st, \
1117 == int ch, FILE *d);
1118 == #endif
1119 */
1120 static void
1121 print(struct match *m,
1122 const char *caption,
1123 states st,
1124 int ch,
1125 FILE *d)
1126 {
1127 struct re_guts *g = m->g;
1128 int i;
1129 int first = 1;
1130
1131 if (!(m->eflags&REG_TRACE))
1132 return;
1133
1134 fprintf(d, "%s", caption);
1135 if (ch != '\0')
1136 fprintf(d, " %s", pchar(ch));
1137 for (i = 0; i < g->nstates; i++)
1138 if (ISSET(st, i)) {
1139 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
1140 first = 0;
1141 }
1142 fprintf(d, "\n");
1143 }
1144
1145 /*
1146 - at - print current situation
1147 == #ifdef REDEBUG
1148 == static void at(struct match *m, const char *title, const char *start, \
1149 == const char *stop, sopno startst, sopno stopst);
1150 == #endif
1151 */
1152 static void
1153 at( struct match *m,
1154 const char *title,
1155 const char *start,
1156 const char *stop,
1157 sopno startst,
1158 sopno stopst)
1159 {
1160 if (!(m->eflags&REG_TRACE))
1161 return;
1162
1163 printf("%s %s-", title, pchar(*start));
1164 printf("%s ", pchar(*stop));
1165 printf("%ld-%ld\n", (long)startst, (long)stopst);
1166 }
1167
1168 #ifndef PCHARDONE
1169 #define PCHARDONE /* never again */
1170 /*
1171 - pchar - make a character printable
1172 == #ifdef REDEBUG
1173 == static const char *pchar(int ch);
1174 == #endif
1175 *
1176 * Is this identical to regchar() over in debug.c? Well, yes. But a
1177 * duplicate here avoids having a debugging-capable regexec.o tied to
1178 * a matching debug.o, and this is convenient. It all disappears in
1179 * the non-debug compilation anyway, so it doesn't matter much.
1180 */
1181 static const char * /* -> representation */
1182 pchar(int ch)
1183 {
1184 static char pbuf[10];
1185
1186 if (isprint((uch)ch) || ch == ' ')
1187 sprintf(pbuf, "%c", ch);
1188 else
1189 sprintf(pbuf, "\\%o", ch);
1190 return(pbuf);
1191 }
1192 #endif
1193 #endif
1194
1195 #undef matcher
1196 #undef fast
1197 #undef slow
1198 #undef dissect
1199 #undef backref
1200 #undef step
1201 #undef print
1202 #undef at
1203 #undef match