Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / posix / engine.c
blobcda7afa897808e64ce9f74ed6a3cac16d4b9ef44
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.
6 * This code is derived from software contributed to Berkeley by
7 * Henry Spencer.
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.
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.
33 * @(#)engine.c 8.5 (Berkeley) 3/20/94
36 #include <sys/cdefs.h>
37 #include <_ansi.h>
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.
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
69 /* another structure passed up and down to avoid zillions of parameters */
70 struct match {
71 struct re_guts *g;
72 int eflags;
73 regmatch_t *pmatch; /* [nsub+1] (0 element unused) */
74 char *offp; /* offsets work from here */
75 char *beginp; /* start of string -- virtual NUL precedes */
76 char *endp; /* end of string -- virtual NUL here */
77 char *coldp; /* can be no match starting before here */
78 char **lastpos; /* [nplus+1] */
79 STATEVARS;
80 states st; /* current states */
81 states fresh; /* states for a fresh start */
82 states tmp; /* temporary */
83 states empty; /* empty set of states */
86 /* ========= begin header generated by ./mkh ========= */
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
91 /* === engine.c === */
92 static int matcher(struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags);
93 static char *dissect(struct match *m, char *start, char *stop, sopno startst, sopno stopst);
94 static char *backref(struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev);
95 static char *fast(struct match *m, char *start, char *stop, sopno startst, sopno stopst);
96 static char *slow(struct match *m, char *start, char *stop, sopno startst, sopno stopst);
97 static states step(struct re_guts *g, sopno start, sopno stop, states bef, int ch, states aft);
98 #define BOL (OUT+1)
99 #define EOL (BOL+1)
100 #define BOLEOL (BOL+2)
101 #define NOTHING (BOL+3)
102 #define BOW (BOL+4)
103 #define EOW (BOL+5)
104 #define CODEMAX (BOL+5) /* highest code used */
105 #define NONCHAR(c) ((c) > CHAR_MAX)
106 #define NNONCHAR (CODEMAX-CHAR_MAX)
107 #ifdef REDEBUG
108 static void print(struct match *m, char *caption, states st, int ch, FILE *d);
109 #endif
110 #ifdef REDEBUG
111 static void at(struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst);
112 #endif
113 #ifdef REDEBUG
114 static char *pchar(int ch);
115 #endif
117 #ifdef __cplusplus
119 #endif
120 /* ========= end header generated by ./mkh ========= */
122 #ifdef REDEBUG
123 #define SP(t, s, c) print(m, t, s, c, stdout)
124 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
125 #define NOTE(str) { if (m->eflags&REG_TRACE) printf("=%s\n", (str)); }
126 #else
127 #define SP(t, s, c) /* nothing */
128 #define AT(t, p1, p2, s1, s2) /* nothing */
129 #define NOTE(s) /* nothing */
130 #endif
133 - matcher - the actual matching engine
134 == static int matcher(struct re_guts *g, char *string, \
135 == size_t nmatch, regmatch_t pmatch[], int eflags);
137 static int /* 0 success, REG_NOMATCH failure */
138 matcher(
139 struct re_guts *g,
140 char *string,
141 size_t nmatch,
142 regmatch_t pmatch[],
143 int eflags
146 char *endp;
147 int i;
148 struct match mv;
149 struct match *m = &mv;
150 char *dp = NULL;
151 const sopno gf = g->firststate+1; /* +1 for OEND */
152 const sopno gl = g->laststate;
153 char *start;
154 char *stop;
155 /* Boyer-Moore algorithms variables */
156 char *pp;
157 int cj, mj;
158 char *mustfirst;
159 char *mustlast;
160 int *matchjump;
161 int *charjump;
163 /* simplify the situation where possible */
164 if (g->cflags&REG_NOSUB)
165 nmatch = 0;
166 if (eflags&REG_STARTEND) {
167 start = string + pmatch[0].rm_so;
168 stop = string + pmatch[0].rm_eo;
169 } else {
170 start = string;
171 stop = start + strlen(start);
173 if (stop < start)
174 return(REG_INVARG);
176 /* prescreening; this does wonders for this rather slow code */
177 if (g->must != NULL) {
178 if (g->charjump != NULL && g->matchjump != NULL) {
179 mustfirst = g->must;
180 mustlast = g->must + g->mlen - 1;
181 charjump = g->charjump;
182 matchjump = g->matchjump;
183 pp = mustlast;
184 for (dp = start+g->mlen-1; dp < stop;) {
185 /* Fast skip non-matches */
186 while (dp < stop && charjump[(unsigned char) *dp])
187 dp += charjump[(unsigned char) *dp];
189 if (dp >= stop)
190 break;
192 /* Greedy matcher */
193 /* We depend on not being used for
194 * for strings of length 1
196 while (*--dp == *--pp && pp != mustfirst);
198 if (*dp == *pp)
199 break;
201 /* Jump to next possible match */
202 mj = matchjump[pp - mustfirst];
203 cj = charjump[(unsigned char) *dp];
204 dp += (cj < mj ? mj : cj);
205 pp = mustlast;
207 if (pp != mustfirst)
208 return(REG_NOMATCH);
209 } else {
210 for (dp = start; dp < stop; dp++)
211 if (*dp == g->must[0] &&
212 stop - dp >= g->mlen &&
213 memcmp(dp, g->must, (size_t)g->mlen) == 0)
214 break;
215 if (dp == stop) /* we didn't find g->must */
216 return(REG_NOMATCH);
220 /* match struct setup */
221 m->g = g;
222 m->eflags = eflags;
223 m->pmatch = NULL;
224 m->lastpos = NULL;
225 m->offp = string;
226 m->beginp = start;
227 m->endp = stop;
228 STATESETUP(m, 4);
229 SETUP(m->st);
230 SETUP(m->fresh);
231 SETUP(m->tmp);
232 SETUP(m->empty);
233 CLEAR(m->empty);
235 /* Adjust start according to moffset, to speed things up */
236 if (g->moffset > -1)
237 start = ((dp - g->moffset) < start) ? start : dp - g->moffset;
239 /* this loop does only one repetition except for backrefs */
240 for (;;) {
241 endp = fast(m, start, stop, gf, gl);
242 if (endp == NULL) { /* a miss */
243 STATETEARDOWN(m);
244 return(REG_NOMATCH);
246 if (nmatch == 0 && !g->backrefs)
247 break; /* no further info needed */
249 /* where? */
250 assert(m->coldp != NULL);
251 for (;;) {
252 NOTE("finding start");
253 endp = slow(m, m->coldp, stop, gf, gl);
254 if (endp != NULL)
255 break;
256 assert(m->coldp < m->endp);
257 m->coldp++;
259 if (nmatch == 1 && !g->backrefs)
260 break; /* no further info needed */
262 /* oh my, he wants the subexpressions... */
263 if (m->pmatch == NULL)
264 m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
265 sizeof(regmatch_t));
266 if (m->pmatch == NULL) {
267 STATETEARDOWN(m);
268 return(REG_ESPACE);
270 for (i = 1; i <= m->g->nsub; i++)
271 m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
272 if (!g->backrefs && !(m->eflags&REG_BACKR)) {
273 NOTE("dissecting");
274 dp = dissect(m, m->coldp, endp, gf, gl);
275 } else {
276 if (g->nplus > 0 && m->lastpos == NULL)
277 m->lastpos = (char **)malloc((g->nplus+1) *
278 sizeof(char *));
279 if (g->nplus > 0 && m->lastpos == NULL) {
280 free(m->pmatch);
281 STATETEARDOWN(m);
282 return(REG_ESPACE);
284 NOTE("backref dissect");
285 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
287 if (dp != NULL)
288 break;
290 /* uh-oh... we couldn't find a subexpression-level match */
291 assert(g->backrefs); /* must be back references doing it */
292 assert(g->nplus == 0 || m->lastpos != NULL);
293 for (;;) {
294 if (dp != NULL || endp <= m->coldp)
295 break; /* defeat */
296 NOTE("backoff");
297 endp = slow(m, m->coldp, endp-1, gf, gl);
298 if (endp == NULL)
299 break; /* defeat */
300 /* try it on a shorter possibility */
301 #ifndef NDEBUG
302 for (i = 1; i <= m->g->nsub; i++) {
303 assert(m->pmatch[i].rm_so == -1);
304 assert(m->pmatch[i].rm_eo == -1);
306 #endif
307 NOTE("backoff dissect");
308 dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
310 assert(dp == NULL || dp == endp);
311 if (dp != NULL) /* found a shorter one */
312 break;
314 /* despite initial appearances, there is no match here */
315 NOTE("false alarm");
316 start = m->coldp + 1; /* recycle starting later */
317 assert(start <= stop);
320 /* fill in the details if requested */
321 if (nmatch > 0) {
322 pmatch[0].rm_so = m->coldp - m->offp;
323 pmatch[0].rm_eo = endp - m->offp;
325 if (nmatch > 1) {
326 assert(m->pmatch != NULL);
327 for (i = 1; i < nmatch; i++)
328 if (i <= m->g->nsub)
329 pmatch[i] = m->pmatch[i];
330 else {
331 pmatch[i].rm_so = -1;
332 pmatch[i].rm_eo = -1;
336 if (m->pmatch != NULL)
337 free((char *)m->pmatch);
338 if (m->lastpos != NULL)
339 free((char *)m->lastpos);
340 STATETEARDOWN(m);
341 return(0);
345 - dissect - figure out what matched what, no back references
346 == static char *dissect(struct match *m, char *start, \
347 == char *stop, sopno startst, sopno stopst);
349 static char * /* == stop (success) always */
350 dissect(
351 struct match *m,
352 char *start,
353 char *stop,
354 sopno startst,
355 sopno stopst
358 int i;
359 sopno ss; /* start sop of current subRE */
360 sopno es; /* end sop of current subRE */
361 char *sp; /* start of string matched by it */
362 char *stp; /* string matched by it cannot pass here */
363 char *rest; /* start of rest of string */
364 char *tail; /* string unmatched by rest of RE */
365 sopno ssub; /* start sop of subsubRE */
366 sopno esub; /* end sop of subsubRE */
367 char *ssp; /* start of string matched by subsubRE */
368 char *sep; /* end of string matched by subsubRE */
369 char *oldssp; /* previous ssp */
370 #if __GNUC_PREREQ (4, 6)
371 /* dp is only used for assertion testing which, for some reason, is not
372 recognized as usage. */
373 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
374 #endif
375 char *dp;
377 AT("diss", start, stop, startst, stopst);
378 sp = start;
379 for (ss = startst; ss < stopst; ss = es) {
380 /* identify end of subRE */
381 es = ss;
382 switch (OP(m->g->strip[es])) {
383 case OPLUS_:
384 case OQUEST_:
385 es += OPND(m->g->strip[es]);
386 break;
387 case OCH_:
388 while (OP(m->g->strip[es]) != O_CH)
389 es += OPND(m->g->strip[es]);
390 break;
392 es++;
394 /* figure out what it matched */
395 switch (OP(m->g->strip[ss])) {
396 case OEND:
397 assert(nope);
398 break;
399 case OCHAR:
400 sp++;
401 break;
402 case OBOL:
403 case OEOL:
404 case OBOW:
405 case OEOW:
406 break;
407 case OANY:
408 case OANYOF:
409 sp++;
410 break;
411 case OBACK_:
412 case O_BACK:
413 assert(nope);
414 break;
415 /* cases where length of match is hard to find */
416 case OQUEST_:
417 stp = stop;
418 for (;;) {
419 /* how long could this one be? */
420 rest = slow(m, sp, stp, ss, es);
421 assert(rest != NULL); /* it did match */
422 /* could the rest match the rest? */
423 tail = slow(m, rest, stop, es, stopst);
424 if (tail == stop)
425 break; /* yes! */
426 /* no -- try a shorter match for this one */
427 stp = rest - 1;
428 assert(stp >= sp); /* it did work */
430 ssub = ss + 1;
431 esub = es - 1;
432 /* did innards match? */
433 if (slow(m, sp, rest, ssub, esub) != NULL) {
434 dp = dissect(m, sp, rest, ssub, esub);
435 assert(dp == rest);
436 } else /* no */
437 assert(sp == rest);
438 sp = rest;
439 break;
440 case OPLUS_:
441 stp = stop;
442 for (;;) {
443 /* how long could this one be? */
444 rest = slow(m, sp, stp, ss, es);
445 assert(rest != NULL); /* it did match */
446 /* could the rest match the rest? */
447 tail = slow(m, rest, stop, es, stopst);
448 if (tail == stop)
449 break; /* yes! */
450 /* no -- try a shorter match for this one */
451 stp = rest - 1;
452 assert(stp >= sp); /* it did work */
454 ssub = ss + 1;
455 esub = es - 1;
456 ssp = sp;
457 oldssp = ssp;
458 for (;;) { /* find last match of innards */
459 sep = slow(m, ssp, rest, ssub, esub);
460 if (sep == NULL || sep == ssp)
461 break; /* failed or matched null */
462 oldssp = ssp; /* on to next try */
463 ssp = sep;
465 if (sep == NULL) {
466 /* last successful match */
467 sep = ssp;
468 ssp = oldssp;
470 assert(sep == rest); /* must exhaust substring */
471 assert(slow(m, ssp, sep, ssub, esub) == rest);
472 dp = dissect(m, ssp, sep, ssub, esub);
473 assert(dp == sep);
474 sp = rest;
475 break;
476 case OCH_:
477 stp = stop;
478 for (;;) {
479 /* how long could this one be? */
480 rest = slow(m, sp, stp, ss, es);
481 assert(rest != NULL); /* it did match */
482 /* could the rest match the rest? */
483 tail = slow(m, rest, stop, es, stopst);
484 if (tail == stop)
485 break; /* yes! */
486 /* no -- try a shorter match for this one */
487 stp = rest - 1;
488 assert(stp >= sp); /* it did work */
490 ssub = ss + 1;
491 esub = ss + OPND(m->g->strip[ss]) - 1;
492 assert(OP(m->g->strip[esub]) == OOR1);
493 for (;;) { /* find first matching branch */
494 if (slow(m, sp, rest, ssub, esub) == rest)
495 break; /* it matched all of it */
496 /* that one missed, try next one */
497 assert(OP(m->g->strip[esub]) == OOR1);
498 esub++;
499 assert(OP(m->g->strip[esub]) == OOR2);
500 ssub = esub + 1;
501 esub += OPND(m->g->strip[esub]);
502 if (OP(m->g->strip[esub]) == OOR2)
503 esub--;
504 else
505 assert(OP(m->g->strip[esub]) == O_CH);
507 dp = dissect(m, sp, rest, ssub, esub);
508 assert(dp == rest);
509 sp = rest;
510 break;
511 case O_PLUS:
512 case O_QUEST:
513 case OOR1:
514 case OOR2:
515 case O_CH:
516 assert(nope);
517 break;
518 case OLPAREN:
519 i = OPND(m->g->strip[ss]);
520 assert(0 < i && i <= m->g->nsub);
521 m->pmatch[i].rm_so = sp - m->offp;
522 break;
523 case ORPAREN:
524 i = OPND(m->g->strip[ss]);
525 assert(0 < i && i <= m->g->nsub);
526 m->pmatch[i].rm_eo = sp - m->offp;
527 break;
528 default: /* uh oh */
529 assert(nope);
530 break;
534 assert(sp == stop);
535 return(sp);
539 - backref - figure out what matched what, figuring in back references
540 == static char *backref(struct match *m, char *start, \
541 == char *stop, sopno startst, sopno stopst, sopno lev);
543 static char * /* == stop (success) or NULL (failure) */
544 backref(
545 struct match *m,
546 char *start,
547 char *stop,
548 sopno startst,
549 sopno stopst,
550 sopno lev /* PLUS nesting level */
553 int i;
554 sopno ss; /* start sop of current subRE */
555 char *sp; /* start of string matched by it */
556 sopno ssub; /* start sop of subsubRE */
557 sopno esub; /* end sop of subsubRE */
558 char *ssp; /* start of string matched by subsubRE */
559 char *dp;
560 size_t len;
561 int hard;
562 sop s;
563 regoff_t offsave;
564 cset *cs;
566 AT("back", start, stop, startst, stopst);
567 sp = start;
569 /* get as far as we can with easy stuff */
570 hard = 0;
571 for (ss = startst; !hard && ss < stopst; ss++)
572 switch (OP(s = m->g->strip[ss])) {
573 case OCHAR:
574 if (sp == stop || *sp++ != (char)OPND(s))
575 return(NULL);
576 break;
577 case OANY:
578 if (sp == stop)
579 return(NULL);
580 sp++;
581 break;
582 case OANYOF:
583 cs = &m->g->sets[OPND(s)];
584 if (sp == stop || !CHIN(cs, *sp++))
585 return(NULL);
586 break;
587 case OBOL:
588 if ( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
589 (sp < m->endp && *(sp-1) == '\n' &&
590 (m->g->cflags&REG_NEWLINE)) )
591 { /* yes */ }
592 else
593 return(NULL);
594 break;
595 case OEOL:
596 if ( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
597 (sp < m->endp && *sp == '\n' &&
598 (m->g->cflags&REG_NEWLINE)) )
599 { /* yes */ }
600 else
601 return(NULL);
602 break;
603 case OBOW:
604 if (( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
605 (sp < m->endp && *(sp-1) == '\n' &&
606 (m->g->cflags&REG_NEWLINE)) ||
607 (sp > m->beginp &&
608 !ISWORD(*(sp-1))) ) &&
609 (sp < m->endp && ISWORD(*sp)) )
610 { /* yes */ }
611 else
612 return(NULL);
613 break;
614 case OEOW:
615 if (( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
616 (sp < m->endp && *sp == '\n' &&
617 (m->g->cflags&REG_NEWLINE)) ||
618 (sp < m->endp && !ISWORD(*sp)) ) &&
619 (sp > m->beginp && ISWORD(*(sp-1))) )
620 { /* yes */ }
621 else
622 return(NULL);
623 break;
624 case O_QUEST:
625 break;
626 case OOR1: /* matches null but needs to skip */
627 ss++;
628 s = m->g->strip[ss];
629 do {
630 assert(OP(s) == OOR2);
631 ss += OPND(s);
632 } while (OP(s = m->g->strip[ss]) != O_CH);
633 /* note that the ss++ gets us past the O_CH */
634 break;
635 default: /* have to make a choice */
636 hard = 1;
637 break;
639 if (!hard) { /* that was it! */
640 if (sp != stop)
641 return(NULL);
642 return(sp);
644 ss--; /* adjust for the for's final increment */
646 /* the hard stuff */
647 AT("hard", sp, stop, ss, stopst);
648 s = m->g->strip[ss];
649 switch (OP(s)) {
650 case OBACK_: /* the vilest depths */
651 i = OPND(s);
652 assert(0 < i && i <= m->g->nsub);
653 if (m->pmatch[i].rm_eo == -1)
654 return(NULL);
655 assert(m->pmatch[i].rm_so != -1);
656 len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
657 assert(stop - m->beginp >= len);
658 if (sp > stop - len)
659 return(NULL); /* not enough left to match */
660 ssp = m->offp + m->pmatch[i].rm_so;
661 if (memcmp(sp, ssp, len) != 0)
662 return(NULL);
663 while (m->g->strip[ss] != SOP(O_BACK, i))
664 ss++;
665 return(backref(m, sp+len, stop, ss+1, stopst, lev));
666 break;
667 case OQUEST_: /* to null or not */
668 dp = backref(m, sp, stop, ss+1, stopst, lev);
669 if (dp != NULL)
670 return(dp); /* not */
671 return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
672 break;
673 case OPLUS_:
674 assert(m->lastpos != NULL);
675 assert(lev+1 <= m->g->nplus);
676 m->lastpos[lev+1] = sp;
677 return(backref(m, sp, stop, ss+1, stopst, lev+1));
678 break;
679 case O_PLUS:
680 if (sp == m->lastpos[lev]) /* last pass matched null */
681 return(backref(m, sp, stop, ss+1, stopst, lev-1));
682 /* try another pass */
683 m->lastpos[lev] = sp;
684 dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
685 if (dp == NULL)
686 return(backref(m, sp, stop, ss+1, stopst, lev-1));
687 else
688 return(dp);
689 break;
690 case OCH_: /* find the right one, if any */
691 ssub = ss + 1;
692 esub = ss + OPND(s) - 1;
693 assert(OP(m->g->strip[esub]) == OOR1);
694 for (;;) { /* find first matching branch */
695 dp = backref(m, sp, stop, ssub, esub, lev);
696 if (dp != NULL)
697 return(dp);
698 /* that one missed, try next one */
699 if (OP(m->g->strip[esub]) == O_CH)
700 return(NULL); /* there is none */
701 esub++;
702 assert(OP(m->g->strip[esub]) == OOR2);
703 ssub = esub + 1;
704 esub += OPND(m->g->strip[esub]);
705 if (OP(m->g->strip[esub]) == OOR2)
706 esub--;
707 else
708 assert(OP(m->g->strip[esub]) == O_CH);
710 break;
711 case OLPAREN: /* must undo assignment if rest fails */
712 i = OPND(s);
713 assert(0 < i && i <= m->g->nsub);
714 offsave = m->pmatch[i].rm_so;
715 m->pmatch[i].rm_so = sp - m->offp;
716 dp = backref(m, sp, stop, ss+1, stopst, lev);
717 if (dp != NULL)
718 return(dp);
719 m->pmatch[i].rm_so = offsave;
720 return(NULL);
721 break;
722 case ORPAREN: /* must undo assignment if rest fails */
723 i = OPND(s);
724 assert(0 < i && i <= m->g->nsub);
725 offsave = m->pmatch[i].rm_eo;
726 m->pmatch[i].rm_eo = sp - m->offp;
727 dp = backref(m, sp, stop, ss+1, stopst, lev);
728 if (dp != NULL)
729 return(dp);
730 m->pmatch[i].rm_eo = offsave;
731 return(NULL);
732 break;
733 default: /* uh oh */
734 assert(nope);
735 break;
738 /* "can't happen" */
739 assert(nope);
740 /* NOTREACHED */
741 return "shut up gcc";
745 - fast - step through the string at top speed
746 == static char *fast(struct match *m, char *start, \
747 == char *stop, sopno startst, sopno stopst);
749 static char * /* where tentative match ended, or NULL */
750 fast(
751 struct match *m,
752 char *start,
753 char *stop,
754 sopno startst,
755 sopno stopst
758 states st = m->st;
759 states fresh = m->fresh;
760 states tmp = m->tmp;
761 char *p = start;
762 int c = (start == m->beginp) ? OUT : *(start-1);
763 int lastc; /* previous c */
764 int flagch;
765 int i;
766 char *coldp; /* last p after which no match was underway */
768 CLEAR(st);
769 SET1(st, startst);
770 st = step(m->g, startst, stopst, st, NOTHING, st);
771 ASSIGN(fresh, st);
772 SP("start", st, *p);
773 coldp = NULL;
774 for (;;) {
775 /* next character */
776 lastc = c;
777 c = (p == m->endp) ? OUT : *p;
778 if (EQ(st, fresh))
779 coldp = p;
781 /* is there an EOL and/or BOL between lastc and c? */
782 flagch = '\0';
783 i = 0;
784 if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
785 (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
786 flagch = BOL;
787 i = m->g->nbol;
789 if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
790 (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
791 flagch = (flagch == BOL) ? BOLEOL : EOL;
792 i += m->g->neol;
794 if (i != 0) {
795 for (; i > 0; i--)
796 st = step(m->g, startst, stopst, st, flagch, st);
797 SP("boleol", st, c);
800 /* how about a word boundary? */
801 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
802 (c != OUT && ISWORD(c)) ) {
803 flagch = BOW;
805 if ( (lastc != OUT && ISWORD(lastc)) &&
806 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
807 flagch = EOW;
809 if (flagch == BOW || flagch == EOW) {
810 st = step(m->g, startst, stopst, st, flagch, st);
811 SP("boweow", st, c);
814 /* are we done? */
815 if (ISSET(st, stopst) || p == stop)
816 break; /* NOTE BREAK OUT */
818 /* no, we must deal with this character */
819 ASSIGN(tmp, st);
820 ASSIGN(st, fresh);
821 assert(c != OUT);
822 st = step(m->g, startst, stopst, tmp, c, st);
823 SP("aft", st, c);
824 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
825 p++;
828 assert(coldp != NULL);
829 m->coldp = coldp;
830 if (ISSET(st, stopst))
831 return(p+1);
832 else
833 return(NULL);
837 - slow - step through the string more deliberately
838 == static char *slow(struct match *m, char *start, \
839 == char *stop, sopno startst, sopno stopst);
841 static char * /* where it ended */
842 slow(
843 struct match *m,
844 char *start,
845 char *stop,
846 sopno startst,
847 sopno stopst
850 states st = m->st;
851 states empty = m->empty;
852 states tmp = m->tmp;
853 char *p = start;
854 int c = (start == m->beginp) ? OUT : *(start-1);
855 int lastc; /* previous c */
856 int flagch;
857 int i;
858 char *matchp; /* last p at which a match ended */
860 AT("slow", start, stop, startst, stopst);
861 CLEAR(st);
862 SET1(st, startst);
863 SP("sstart", st, *p);
864 st = step(m->g, startst, stopst, st, NOTHING, st);
865 matchp = NULL;
866 for (;;) {
867 /* next character */
868 lastc = c;
869 c = (p == m->endp) ? OUT : *p;
871 /* is there an EOL and/or BOL between lastc and c? */
872 flagch = '\0';
873 i = 0;
874 if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
875 (lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
876 flagch = BOL;
877 i = m->g->nbol;
879 if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
880 (c == OUT && !(m->eflags&REG_NOTEOL)) ) {
881 flagch = (flagch == BOL) ? BOLEOL : EOL;
882 i += m->g->neol;
884 if (i != 0) {
885 for (; i > 0; i--)
886 st = step(m->g, startst, stopst, st, flagch, st);
887 SP("sboleol", st, c);
890 /* how about a word boundary? */
891 if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
892 (c != OUT && ISWORD(c)) ) {
893 flagch = BOW;
895 if ( (lastc != OUT && ISWORD(lastc)) &&
896 (flagch == EOL || (c != OUT && !ISWORD(c))) ) {
897 flagch = EOW;
899 if (flagch == BOW || flagch == EOW) {
900 st = step(m->g, startst, stopst, st, flagch, st);
901 SP("sboweow", st, c);
904 /* are we done? */
905 if (ISSET(st, stopst))
906 matchp = p;
907 if (EQ(st, empty) || p == stop)
908 break; /* NOTE BREAK OUT */
910 /* no, we must deal with this character */
911 ASSIGN(tmp, st);
912 ASSIGN(st, empty);
913 assert(c != OUT);
914 st = step(m->g, startst, stopst, tmp, c, st);
915 SP("saft", st, c);
916 assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
917 p++;
920 return(matchp);
925 - step - map set of states reachable before char to set reachable after
926 == static states step(struct re_guts *g, sopno start, sopno stop, \
927 == states bef, int ch, states aft);
928 == #define BOL (OUT+1)
929 == #define EOL (BOL+1)
930 == #define BOLEOL (BOL+2)
931 == #define NOTHING (BOL+3)
932 == #define BOW (BOL+4)
933 == #define EOW (BOL+5)
934 == #define CODEMAX (BOL+5) // highest code used
935 == #define NONCHAR(c) ((c) > CHAR_MAX)
936 == #define NNONCHAR (CODEMAX-CHAR_MAX)
938 static states
939 step(
940 struct re_guts *g,
941 sopno start, /* start state within strip */
942 sopno stop, /* state after stop state within strip */
943 states bef, /* states reachable before */
944 int ch, /* character or NONCHAR code */
945 states aft /* states already known reachable after */
948 cset *cs;
949 sop s;
950 sopno pc;
951 onestate here; /* note, macros know this name */
952 sopno look;
953 int i;
955 for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
956 s = g->strip[pc];
957 switch (OP(s)) {
958 case OEND:
959 assert(pc == stop-1);
960 break;
961 case OCHAR:
962 /* only characters can match */
963 assert(!NONCHAR(ch) || ch != (char)OPND(s));
964 if (ch == (char)OPND(s))
965 FWD(aft, bef, 1);
966 break;
967 case OBOL:
968 if (ch == BOL || ch == BOLEOL)
969 FWD(aft, bef, 1);
970 break;
971 case OEOL:
972 if (ch == EOL || ch == BOLEOL)
973 FWD(aft, bef, 1);
974 break;
975 case OBOW:
976 if (ch == BOW)
977 FWD(aft, bef, 1);
978 break;
979 case OEOW:
980 if (ch == EOW)
981 FWD(aft, bef, 1);
982 break;
983 case OANY:
984 if (!NONCHAR(ch))
985 FWD(aft, bef, 1);
986 break;
987 case OANYOF:
988 cs = &g->sets[OPND(s)];
989 if (!NONCHAR(ch) && CHIN(cs, ch))
990 FWD(aft, bef, 1);
991 break;
992 case OBACK_: /* ignored here */
993 case O_BACK:
994 FWD(aft, aft, 1);
995 break;
996 case OPLUS_: /* forward, this is just an empty */
997 FWD(aft, aft, 1);
998 break;
999 case O_PLUS: /* both forward and back */
1000 FWD(aft, aft, 1);
1001 i = ISSETBACK(aft, OPND(s));
1002 BACK(aft, aft, OPND(s));
1003 if (!i && ISSETBACK(aft, OPND(s))) {
1004 /* oho, must reconsider loop body */
1005 pc -= OPND(s) + 1;
1006 INIT(here, pc);
1008 break;
1009 case OQUEST_: /* two branches, both forward */
1010 FWD(aft, aft, 1);
1011 FWD(aft, aft, OPND(s));
1012 break;
1013 case O_QUEST: /* just an empty */
1014 FWD(aft, aft, 1);
1015 break;
1016 case OLPAREN: /* not significant here */
1017 case ORPAREN:
1018 FWD(aft, aft, 1);
1019 break;
1020 case OCH_: /* mark the first two branches */
1021 FWD(aft, aft, 1);
1022 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1023 FWD(aft, aft, OPND(s));
1024 break;
1025 case OOR1: /* done a branch, find the O_CH */
1026 if (ISSTATEIN(aft, here)) {
1027 for (look = 1;
1028 OP(s = g->strip[pc+look]) != O_CH;
1029 look += OPND(s))
1030 assert(OP(s) == OOR2);
1031 FWD(aft, aft, look);
1033 break;
1034 case OOR2: /* propagate OCH_'s marking */
1035 FWD(aft, aft, 1);
1036 if (OP(g->strip[pc+OPND(s)]) != O_CH) {
1037 assert(OP(g->strip[pc+OPND(s)]) == OOR2);
1038 FWD(aft, aft, OPND(s));
1040 break;
1041 case O_CH: /* just empty */
1042 FWD(aft, aft, 1);
1043 break;
1044 default: /* ooooops... */
1045 assert(nope);
1046 break;
1050 return(aft);
1053 #ifdef REDEBUG
1055 - print - print a set of states
1056 == #ifdef REDEBUG
1057 == static void print(struct match *m, char *caption, states st, \
1058 == int ch, FILE *d);
1059 == #endif
1061 static void
1062 print(m, caption, st, ch, d)
1063 struct match *m;
1064 char *caption;
1065 states st;
1066 int ch;
1067 FILE *d;
1069 struct re_guts *g = m->g;
1070 int i;
1071 int first = 1;
1073 if (!(m->eflags&REG_TRACE))
1074 return;
1076 fprintf(d, "%s", caption);
1077 if (ch != '\0')
1078 fprintf(d, " %s", pchar(ch));
1079 for (i = 0; i < g->nstates; i++)
1080 if (ISSET(st, i)) {
1081 fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
1082 first = 0;
1084 fprintf(d, "\n");
1088 - at - print current situation
1089 == #ifdef REDEBUG
1090 == static void at(struct match *m, char *title, char *start, char *stop, \
1091 == sopno startst, sopno stopst);
1092 == #endif
1094 static void
1095 at(m, title, start, stop, startst, stopst)
1096 struct match *m;
1097 char *title;
1098 char *start;
1099 char *stop;
1100 sopno startst;
1101 sopno stopst;
1103 if (!(m->eflags&REG_TRACE))
1104 return;
1106 printf("%s %s-", title, pchar(*start));
1107 printf("%s ", pchar(*stop));
1108 printf("%ld-%ld\n", (long)startst, (long)stopst);
1111 #ifndef PCHARDONE
1112 #define PCHARDONE /* never again */
1114 - pchar - make a character printable
1115 == #ifdef REDEBUG
1116 == static char *pchar(int ch);
1117 == #endif
1119 * Is this identical to regchar() over in debug.c? Well, yes. But a
1120 * duplicate here avoids having a debugging-capable regexec.o tied to
1121 * a matching debug.o, and this is convenient. It all disappears in
1122 * the non-debug compilation anyway, so it doesn't matter much.
1124 static char * /* -> representation */
1125 pchar(ch)
1126 int ch;
1128 static char pbuf[10];
1130 if (isprint((uch)ch) || ch == ' ')
1131 sprintf(pbuf, "%c", ch);
1132 else
1133 sprintf(pbuf, "\\%o", ch);
1134 return(pbuf);
1136 #endif
1137 #endif
1139 #undef matcher
1140 #undef fast
1141 #undef slow
1142 #undef dissect
1143 #undef backref
1144 #undef step
1145 #undef print
1146 #undef at
1147 #undef match