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