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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * @(#)engine.c 8.5 (Berkeley) 3/20/94
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
48 #define matcher smatcher
51 #define dissect sdissect
52 #define backref sbackref
59 #define matcher lmatcher
62 #define dissect ldissect
63 #define backref lbackref
70 /* another structure passed up and down to avoid zillions of parameters */
74 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
75 char *offp
; /* offsets work from here */
76 char *beginp
; /* start of string -- virtual NUL precedes */
77 char *endp
; /* end of string -- virtual NUL here */
78 char *coldp
; /* can be no match starting before here */
79 char **lastpos
; /* [nplus+1] */
81 states st
; /* current states */
82 states fresh
; /* states for a fresh start */
83 states tmp
; /* temporary */
84 states empty
; /* empty set of states */
87 /* ========= begin header generated by ./mkh ========= */
92 /* === engine.c === */
93 static int matcher
__P((struct re_guts
*g
, char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
));
94 static char *dissect
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
95 static char *backref
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
, sopno lev
));
96 static char *fast
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
97 static char *slow
__P((struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
));
98 static states step
__P((struct re_guts
*g
, sopno start
, sopno stop
, states bef
, int ch
, states aft
));
101 #define BOLEOL (BOL+2)
102 #define NOTHING (BOL+3)
105 #define CODEMAX (BOL+5) /* highest code used */
106 #define NONCHAR(c) ((c) > CHAR_MAX)
107 #define NNONCHAR (CODEMAX-CHAR_MAX)
109 static void print
__P((struct match
*m
, char *caption
, states st
, int ch
, FILE *d
));
112 static void at
__P((struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
, sopno stopst
));
115 static char *pchar
__P((int ch
));
121 /* ========= end header generated by ./mkh ========= */
124 #define SP(t, s, c) print(m, t, s, c, stdout)
125 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
126 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
128 #define SP(t, s, c) /* nothing */
129 #define AT(t, p1, p2, s1, s2) /* nothing */
130 #define NOTE(s) /* nothing */
134 - matcher - the actual matching engine
135 == static int matcher(register struct re_guts *g, char *string, \
136 == size_t nmatch, regmatch_t pmatch[], int eflags);
138 static int /* 0 success, REG_NOMATCH failure */
139 matcher(struct re_guts
*g
, char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
)
144 register struct match
*m
= &mv
;
146 const register sopno gf
= g
->firststate
+1; /* +1 for OEND */
147 const register sopno gl
= g
->laststate
;
151 /* simplify the situation where possible */
152 if (g
->cflags
®_NOSUB
)
154 if (eflags
®_STARTEND
) {
155 start
= string
+ pmatch
[0].rm_so
;
156 stop
= string
+ pmatch
[0].rm_eo
;
159 stop
= start
+ strlen(start
);
164 /* prescreening; this does wonders for this rather slow code */
165 if (g
->must
!= NULL
) {
166 for (dp
= start
; dp
< stop
; dp
++)
167 if (*dp
== g
->must
[0] && stop
- dp
>= g
->mlen
&&
168 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
170 if (dp
== stop
) /* we didn't find g->must */
174 /* match struct setup */
189 /* this loop does only one repetition except for backrefs */
191 endp
= fast(m
, start
, stop
, gf
, gl
);
192 if (endp
== NULL
) { /* a miss */
196 if (nmatch
== 0 && !g
->backrefs
)
197 break; /* no further info needed */
200 assert(m
->coldp
!= NULL
);
202 NOTE("finding start");
203 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
206 assert(m
->coldp
< m
->endp
);
209 if (nmatch
== 1 && !g
->backrefs
)
210 break; /* no further info needed */
212 /* oh my, he wants the subexpressions... */
213 if (m
->pmatch
== NULL
)
214 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
216 if (m
->pmatch
== NULL
) {
220 for (i
= 1; i
<= m
->g
->nsub
; i
++)
221 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
222 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
224 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
226 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
227 m
->lastpos
= (char **)malloc((g
->nplus
+1) *
229 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
234 NOTE("backref dissect");
235 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
240 /* uh-oh... we couldn't find a subexpression-level match */
241 assert(g
->backrefs
); /* must be back references doing it */
242 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
244 if (dp
!= NULL
|| endp
<= m
->coldp
)
247 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
250 /* try it on a shorter possibility */
252 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
253 assert(m
->pmatch
[i
].rm_so
== -1);
254 assert(m
->pmatch
[i
].rm_eo
== -1);
257 NOTE("backoff dissect");
258 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
260 assert(dp
== NULL
|| dp
== endp
);
261 if (dp
!= NULL
) /* found a shorter one */
264 /* despite initial appearances, there is no match here */
266 start
= m
->coldp
+ 1; /* recycle starting later */
267 assert(start
<= stop
);
270 /* fill in the details if requested */
272 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
273 pmatch
[0].rm_eo
= endp
- m
->offp
;
276 assert(m
->pmatch
!= NULL
);
277 for (i
= 1; i
< nmatch
; i
++)
279 pmatch
[i
] = m
->pmatch
[i
];
281 pmatch
[i
].rm_so
= -1;
282 pmatch
[i
].rm_eo
= -1;
286 if (m
->pmatch
!= NULL
)
287 free((char *)m
->pmatch
);
288 if (m
->lastpos
!= NULL
)
289 free((char *)m
->lastpos
);
295 - dissect - figure out what matched what, no back references
296 == static char *dissect(register struct match *m, char *start, \
297 == char *stop, sopno startst, sopno stopst);
299 static char * /* == stop (success) always */
300 dissect(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
)
303 register sopno ss
; /* start sop of current subRE */
304 register sopno es
; /* end sop of current subRE */
305 register char *sp
; /* start of string matched by it */
306 register char *stp
; /* string matched by it cannot pass here */
307 register char *rest
; /* start of rest of string */
308 register char *tail
; /* string unmatched by rest of RE */
309 register sopno ssub
; /* start sop of subsubRE */
310 register sopno esub
; /* end sop of subsubRE */
311 register char *ssp
; /* start of string matched by subsubRE */
312 register char *sep
; /* end of string matched by subsubRE */
313 register char *oldssp
; /* previous ssp */
316 AT("diss", start
, stop
, startst
, stopst
);
318 for (ss
= startst
; ss
< stopst
; ss
= es
) {
319 /* identify end of subRE */
321 switch (OP(m
->g
->strip
[es
])) {
324 es
+= OPND(m
->g
->strip
[es
]);
327 while (OP(m
->g
->strip
[es
]) != O_CH
)
328 es
+= OPND(m
->g
->strip
[es
]);
333 /* figure out what it matched */
334 switch (OP(m
->g
->strip
[ss
])) {
354 /* cases where length of match is hard to find */
358 /* how long could this one be? */
359 rest
= slow(m
, sp
, stp
, ss
, es
);
360 assert(rest
!= NULL
); /* it did match */
361 /* could the rest match the rest? */
362 tail
= slow(m
, rest
, stop
, es
, stopst
);
365 /* no -- try a shorter match for this one */
367 assert(stp
>= sp
); /* it did work */
371 /* did innards match? */
372 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
373 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
382 /* how long could this one be? */
383 rest
= slow(m
, sp
, stp
, ss
, es
);
384 assert(rest
!= NULL
); /* it did match */
385 /* could the rest match the rest? */
386 tail
= slow(m
, rest
, stop
, es
, stopst
);
389 /* no -- try a shorter match for this one */
391 assert(stp
>= sp
); /* it did work */
397 for (;;) { /* find last match of innards */
398 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
399 if (sep
== NULL
|| sep
== ssp
)
400 break; /* failed or matched null */
401 oldssp
= ssp
; /* on to next try */
405 /* last successful match */
409 assert(sep
== rest
); /* must exhaust substring */
410 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
411 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
418 /* how long could this one be? */
419 rest
= slow(m
, sp
, stp
, ss
, es
);
420 assert(rest
!= NULL
); /* it did match */
421 /* could the rest match the rest? */
422 tail
= slow(m
, rest
, stop
, es
, stopst
);
425 /* no -- try a shorter match for this one */
427 assert(stp
>= sp
); /* it did work */
430 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
431 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
432 for (;;) { /* find first matching branch */
433 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
434 break; /* it matched all of it */
435 /* that one missed, try next one */
436 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
438 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
440 esub
+= OPND(m
->g
->strip
[esub
]);
441 if (OP(m
->g
->strip
[esub
]) == OOR2
)
444 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
446 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
458 i
= OPND(m
->g
->strip
[ss
]);
459 assert(0 < i
&& i
<= m
->g
->nsub
);
460 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
463 i
= OPND(m
->g
->strip
[ss
]);
464 assert(0 < i
&& i
<= m
->g
->nsub
);
465 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
478 - backref - figure out what matched what, figuring in back references
479 == static char *backref(register struct match *m, char *start, \
480 == char *stop, sopno startst, sopno stopst, sopno lev);
482 static char * /* == stop (success) or NULL (failure) */
483 backref(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
, sopno lev
)
486 register sopno ss
; /* start sop of current subRE */
487 register char *sp
; /* start of string matched by it */
488 register sopno ssub
; /* start sop of subsubRE */
489 register sopno esub
; /* end sop of subsubRE */
490 register char *ssp
; /* start of string matched by subsubRE */
495 register regoff_t offsave
;
498 AT("back", start
, stop
, startst
, stopst
);
501 /* get as far as we can with easy stuff */
503 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
504 switch (OP(s
= m
->g
->strip
[ss
])) {
506 if (sp
== stop
|| *sp
++ != (char)OPND(s
))
515 cs
= &m
->g
->sets
[OPND(s
)];
516 if (sp
== stop
|| !CHIN(cs
, *sp
++))
520 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
521 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
522 (m
->g
->cflags
®_NEWLINE
)) )
528 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
529 (sp
< m
->endp
&& *sp
== '\n' &&
530 (m
->g
->cflags
®_NEWLINE
)) )
536 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
537 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
538 (m
->g
->cflags
®_NEWLINE
)) ||
540 !ISWORD(*(sp
-1))) ) &&
541 (sp
< m
->endp
&& ISWORD(*sp
)) )
547 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
548 (sp
< m
->endp
&& *sp
== '\n' &&
549 (m
->g
->cflags
®_NEWLINE
)) ||
550 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
551 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
558 case OOR1
: /* matches null but needs to skip */
562 assert(OP(s
) == OOR2
);
564 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
565 /* note that the ss++ gets us past the O_CH */
567 default: /* have to make a choice */
571 if (!hard
) { /* that was it! */
576 ss
--; /* adjust for the for's final increment */
579 AT("hard", sp
, stop
, ss
, stopst
);
582 case OBACK_
: /* the vilest depths */
584 assert(0 < i
&& i
<= m
->g
->nsub
);
585 if (m
->pmatch
[i
].rm_eo
== -1)
587 assert(m
->pmatch
[i
].rm_so
!= -1);
588 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
589 assert(stop
- m
->beginp
>= len
);
591 return(NULL
); /* not enough left to match */
592 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
593 if (memcmp(sp
, ssp
, len
) != 0)
595 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
597 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
599 case OQUEST_
: /* to null or not */
600 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
602 return(dp
); /* not */
603 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
606 assert(m
->lastpos
!= NULL
);
607 assert(lev
+1 <= m
->g
->nplus
);
608 m
->lastpos
[lev
+1] = sp
;
609 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1));
612 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
613 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
614 /* try another pass */
615 m
->lastpos
[lev
] = sp
;
616 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
);
618 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
622 case OCH_
: /* find the right one, if any */
624 esub
= ss
+ OPND(s
) - 1;
625 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
626 for (;;) { /* find first matching branch */
627 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
);
630 /* that one missed, try next one */
631 if (OP(m
->g
->strip
[esub
]) == O_CH
)
632 return(NULL
); /* there is none */
634 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
636 esub
+= OPND(m
->g
->strip
[esub
]);
637 if (OP(m
->g
->strip
[esub
]) == OOR2
)
640 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
643 case OLPAREN
: /* must undo assignment if rest fails */
645 assert(0 < i
&& i
<= m
->g
->nsub
);
646 offsave
= m
->pmatch
[i
].rm_so
;
647 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
648 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
651 m
->pmatch
[i
].rm_so
= offsave
;
654 case ORPAREN
: /* must undo assignment if rest fails */
656 assert(0 < i
&& i
<= m
->g
->nsub
);
657 offsave
= m
->pmatch
[i
].rm_eo
;
658 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
659 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
662 m
->pmatch
[i
].rm_eo
= offsave
;
677 - fast - step through the string at top speed
678 == static char *fast(register struct match *m, char *start, \
679 == char *stop, sopno startst, sopno stopst);
681 static char * /* where tentative match ended, or NULL */
682 fast(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
)
684 register states st
= m
->st
;
685 register states fresh
= m
->fresh
;
686 register states tmp
= m
->tmp
;
687 register char *p
= start
;
688 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
689 register int lastc
; /* previous c */
692 register char *coldp
; /* last p after which no match was underway */
696 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
703 c
= (p
== m
->endp
) ? OUT
: *p
;
707 /* is there an EOL and/or BOL between lastc and c? */
710 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
711 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
715 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
716 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
717 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
722 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
726 /* how about a word boundary? */
727 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
728 (c
!= OUT
&& ISWORD(c
)) ) {
731 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
732 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
735 if (flagch
== BOW
|| flagch
== EOW
) {
736 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
741 if (ISSET(st
, stopst
) || p
== stop
)
742 break; /* NOTE BREAK OUT */
744 /* no, we must deal with this character */
748 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
750 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
754 assert(coldp
!= NULL
);
756 if (ISSET(st
, stopst
))
763 - slow - step through the string more deliberately
764 == static char *slow(register struct match *m, char *start, \
765 == char *stop, sopno startst, sopno stopst);
767 static char * /* where it ended */
768 slow(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
)
770 register states st
= m
->st
;
771 register states empty
= m
->empty
;
772 register states tmp
= m
->tmp
;
773 register char *p
= start
;
774 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
775 register int lastc
; /* previous c */
778 register char *matchp
; /* last p at which a match ended */
780 AT("slow", start
, stop
, startst
, stopst
);
783 SP("sstart", st
, *p
);
784 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
789 c
= (p
== m
->endp
) ? OUT
: *p
;
791 /* is there an EOL and/or BOL between lastc and c? */
794 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
795 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
799 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
800 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
801 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
806 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
807 SP("sboleol", st
, c
);
810 /* how about a word boundary? */
811 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
812 (c
!= OUT
&& ISWORD(c
)) ) {
815 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
816 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
819 if (flagch
== BOW
|| flagch
== EOW
) {
820 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
821 SP("sboweow", st
, c
);
825 if (ISSET(st
, stopst
))
827 if (EQ(st
, empty
) || p
== stop
)
828 break; /* NOTE BREAK OUT */
830 /* no, we must deal with this character */
834 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
836 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
845 - step - map set of states reachable before char to set reachable after
846 == static states step(register struct re_guts *g, sopno start, sopno stop, \
847 == register states bef, int ch, register states aft);
848 == #define BOL (OUT+1)
849 == #define EOL (BOL+1)
850 == #define BOLEOL (BOL+2)
851 == #define NOTHING (BOL+3)
852 == #define BOW (BOL+4)
853 == #define EOW (BOL+5)
854 == #define CODEMAX (BOL+5) // highest code used
855 == #define NONCHAR(c) ((c) > CHAR_MAX)
856 == #define NNONCHAR (CODEMAX-CHAR_MAX)
859 step(struct re_guts
*g
,
860 sopno start
, /* start state within strip */
861 sopno stop
, /* state after stop state within strip */
862 states bef
, /* states reachable before */
863 int ch
, /* character or NONCHAR code */
864 states aft
) /* states already known reachable after */
869 register onestate here
; /* note, macros know this name */
873 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
877 assert(pc
== stop
-1);
880 /* only characters can match */
881 assert(!NONCHAR(ch
) || ch
!= (char)OPND(s
));
882 if (ch
== (char)OPND(s
))
886 if (ch
== BOL
|| ch
== BOLEOL
)
890 if (ch
== EOL
|| ch
== BOLEOL
)
906 cs
= &g
->sets
[OPND(s
)];
907 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
910 case OBACK_
: /* ignored here */
914 case OPLUS_
: /* forward, this is just an empty */
917 case O_PLUS
: /* both forward and back */
919 i
= ISSETBACK(aft
, OPND(s
));
920 BACK(aft
, aft
, OPND(s
));
921 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
922 /* oho, must reconsider loop body */
927 case OQUEST_
: /* two branches, both forward */
929 FWD(aft
, aft
, OPND(s
));
931 case O_QUEST
: /* just an empty */
934 case OLPAREN
: /* not significant here */
938 case OCH_
: /* mark the first two branches */
940 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
941 FWD(aft
, aft
, OPND(s
));
943 case OOR1
: /* done a branch, find the O_CH */
944 if (ISSTATEIN(aft
, here
)) {
946 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
948 assert(OP(s
) == OOR2
);
952 case OOR2
: /* propagate OCH_'s marking */
954 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
955 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
956 FWD(aft
, aft
, OPND(s
));
959 case O_CH
: /* just empty */
962 default: /* ooooops... */
973 - print - print a set of states
975 == static void print(struct match *m, char *caption, states st, \
980 print(struct match
*m
, char *caption
, states st
, int ch
, FILE *d
)
982 register struct re_guts
*g
= m
->g
;
984 register int first
= 1;
986 if (!(m
->eflags
®_TRACE
))
989 fprintf(d
, "%s", caption
);
991 fprintf(d
, " %s", pchar(ch
));
992 for (i
= 0; i
< g
->nstates
; i
++)
994 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1001 - at - print current situation
1003 == static void at(struct match *m, char *title, char *start, char *stop, \
1004 == sopno startst, sopno stopst);
1008 at(struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
, sopno stopst
)
1010 if (!(m
->eflags
®_TRACE
))
1013 printf("%s %s-", title
, pchar(*start
));
1014 printf("%s ", pchar(*stop
));
1015 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1019 #define PCHARDONE /* never again */
1021 - pchar - make a character printable
1023 == static char *pchar(int ch);
1026 * Is this identical to regchar() over in debug.c? Well, yes. But a
1027 * duplicate here avoids having a debugging-capable regexec.o tied to
1028 * a matching debug.o, and this is convenient. It all disappears in
1029 * the non-debug compilation anyway, so it doesn't matter much.
1031 static char * /* -> representation */
1034 static char pbuf
[10];
1036 if (isprint(ch
) || ch
== ' ')
1037 sprintf(pbuf
, "%c", ch
);
1039 sprintf(pbuf
, "\\%o", ch
);