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