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 * 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
33 * @(#)engine.c 8.5 (Berkeley) 3/20/94
36 #include <sys/cdefs.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
47 #define matcher smatcher
50 #define dissect sdissect
51 #define backref sbackref
58 #define matcher lmatcher
61 #define dissect ldissect
62 #define backref lbackref
69 /* another structure passed up and down to avoid zillions of parameters */
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] */
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 ========= */
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
);
100 #define BOLEOL (BOL+2)
101 #define NOTHING (BOL+3)
104 #define CODEMAX (BOL+5) /* highest code used */
105 #define NONCHAR(c) ((c) > CHAR_MAX)
106 #define NNONCHAR (CODEMAX-CHAR_MAX)
108 static void print(struct match
*m
, char *caption
, states st
, int ch
, FILE *d
);
111 static void at(struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
, sopno stopst
);
114 static char *pchar(int ch
);
120 /* ========= end header generated by ./mkh ========= */
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®_TRACE) printf("=%s\n", (str)); }
127 #define SP(t, s, c) /* nothing */
128 #define AT(t, p1, p2, s1, s2) /* nothing */
129 #define NOTE(s) /* nothing */
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 */
149 struct match
*m
= &mv
;
151 const sopno gf
= g
->firststate
+1; /* +1 for OEND */
152 const sopno gl
= g
->laststate
;
155 /* Boyer-Moore algorithms variables */
163 /* simplify the situation where possible */
164 if (g
->cflags
®_NOSUB
)
166 if (eflags
®_STARTEND
) {
167 start
= string
+ pmatch
[0].rm_so
;
168 stop
= string
+ pmatch
[0].rm_eo
;
171 stop
= start
+ strlen(start
);
176 /* prescreening; this does wonders for this rather slow code */
177 if (g
->must
!= NULL
) {
178 if (g
->charjump
!= NULL
&& g
->matchjump
!= NULL
) {
180 mustlast
= g
->must
+ g
->mlen
- 1;
181 charjump
= g
->charjump
;
182 matchjump
= g
->matchjump
;
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
];
193 /* We depend on not being used for
194 * for strings of length 1
196 while (*--dp
== *--pp
&& pp
!= mustfirst
);
201 /* Jump to next possible match */
202 mj
= matchjump
[pp
- mustfirst
];
203 cj
= charjump
[(unsigned char) *dp
];
204 dp
+= (cj
< mj
? mj
: cj
);
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)
215 if (dp
== stop
) /* we didn't find g->must */
220 /* match struct setup */
235 /* Adjust start according to moffset, to speed things up */
237 start
= ((dp
- g
->moffset
) < start
) ? start
: dp
- g
->moffset
;
239 /* this loop does only one repetition except for backrefs */
241 endp
= fast(m
, start
, stop
, gf
, gl
);
242 if (endp
== NULL
) { /* a miss */
246 if (nmatch
== 0 && !g
->backrefs
)
247 break; /* no further info needed */
250 assert(m
->coldp
!= NULL
);
252 NOTE("finding start");
253 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
256 assert(m
->coldp
< m
->endp
);
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) *
266 if (m
->pmatch
== NULL
) {
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
®_BACKR
)) {
274 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
276 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
277 m
->lastpos
= (char **)malloc((g
->nplus
+1) *
279 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
284 NOTE("backref dissect");
285 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
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
);
294 if (dp
!= NULL
|| endp
<= m
->coldp
)
297 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
300 /* try it on a shorter possibility */
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);
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 */
314 /* despite initial appearances, there is no match here */
316 start
= m
->coldp
+ 1; /* recycle starting later */
317 assert(start
<= stop
);
320 /* fill in the details if requested */
322 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
323 pmatch
[0].rm_eo
= endp
- m
->offp
;
326 assert(m
->pmatch
!= NULL
);
327 for (i
= 1; i
< nmatch
; i
++)
329 pmatch
[i
] = m
->pmatch
[i
];
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
);
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 */
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"
377 AT("diss", start
, stop
, startst
, stopst
);
379 for (ss
= startst
; ss
< stopst
; ss
= es
) {
380 /* identify end of subRE */
382 switch (OP(m
->g
->strip
[es
])) {
385 es
+= OPND(m
->g
->strip
[es
]);
388 while (OP(m
->g
->strip
[es
]) != O_CH
)
389 es
+= OPND(m
->g
->strip
[es
]);
394 /* figure out what it matched */
395 switch (OP(m
->g
->strip
[ss
])) {
415 /* cases where length of match is hard to find */
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
);
426 /* no -- try a shorter match for this one */
428 assert(stp
>= sp
); /* it did work */
432 /* did innards match? */
433 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
434 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
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
);
450 /* no -- try a shorter match for this one */
452 assert(stp
>= sp
); /* it did work */
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 */
466 /* last successful match */
470 assert(sep
== rest
); /* must exhaust substring */
471 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
472 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
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
);
486 /* no -- try a shorter match for this one */
488 assert(stp
>= sp
); /* it did work */
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
);
499 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
501 esub
+= OPND(m
->g
->strip
[esub
]);
502 if (OP(m
->g
->strip
[esub
]) == OOR2
)
505 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
507 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
519 i
= OPND(m
->g
->strip
[ss
]);
520 assert(0 < i
&& i
<= m
->g
->nsub
);
521 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
524 i
= OPND(m
->g
->strip
[ss
]);
525 assert(0 < i
&& i
<= m
->g
->nsub
);
526 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
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) */
550 sopno lev
/* PLUS nesting level */
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 */
566 AT("back", start
, stop
, startst
, stopst
);
569 /* get as far as we can with easy stuff */
571 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
572 switch (OP(s
= m
->g
->strip
[ss
])) {
574 if (sp
== stop
|| *sp
++ != (char)OPND(s
))
583 cs
= &m
->g
->sets
[OPND(s
)];
584 if (sp
== stop
|| !CHIN(cs
, *sp
++))
588 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
589 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
590 (m
->g
->cflags
®_NEWLINE
)) )
596 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
597 (sp
< m
->endp
&& *sp
== '\n' &&
598 (m
->g
->cflags
®_NEWLINE
)) )
604 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
605 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
606 (m
->g
->cflags
®_NEWLINE
)) ||
608 !ISWORD(*(sp
-1))) ) &&
609 (sp
< m
->endp
&& ISWORD(*sp
)) )
615 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
616 (sp
< m
->endp
&& *sp
== '\n' &&
617 (m
->g
->cflags
®_NEWLINE
)) ||
618 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
619 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
626 case OOR1
: /* matches null but needs to skip */
630 assert(OP(s
) == OOR2
);
632 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
633 /* note that the ss++ gets us past the O_CH */
635 default: /* have to make a choice */
639 if (!hard
) { /* that was it! */
644 ss
--; /* adjust for the for's final increment */
647 AT("hard", sp
, stop
, ss
, stopst
);
650 case OBACK_
: /* the vilest depths */
652 assert(0 < i
&& i
<= m
->g
->nsub
);
653 if (m
->pmatch
[i
].rm_eo
== -1)
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
);
659 return(NULL
); /* not enough left to match */
660 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
661 if (memcmp(sp
, ssp
, len
) != 0)
663 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
665 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
667 case OQUEST_
: /* to null or not */
668 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
670 return(dp
); /* not */
671 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
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));
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
);
686 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
690 case OCH_
: /* find the right one, if any */
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
);
698 /* that one missed, try next one */
699 if (OP(m
->g
->strip
[esub
]) == O_CH
)
700 return(NULL
); /* there is none */
702 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
704 esub
+= OPND(m
->g
->strip
[esub
]);
705 if (OP(m
->g
->strip
[esub
]) == OOR2
)
708 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
711 case OLPAREN
: /* must undo assignment if rest fails */
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
);
719 m
->pmatch
[i
].rm_so
= offsave
;
722 case ORPAREN
: /* must undo assignment if rest fails */
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
);
730 m
->pmatch
[i
].rm_eo
= offsave
;
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 */
759 states fresh
= m
->fresh
;
762 int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
763 int lastc
; /* previous c */
766 char *coldp
; /* last p after which no match was underway */
770 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
777 c
= (p
== m
->endp
) ? OUT
: *p
;
781 /* is there an EOL and/or BOL between lastc and c? */
784 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
785 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
789 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
790 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
791 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
796 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
800 /* how about a word boundary? */
801 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
802 (c
!= OUT
&& ISWORD(c
)) ) {
805 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
806 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
809 if (flagch
== BOW
|| flagch
== EOW
) {
810 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
815 if (ISSET(st
, stopst
) || p
== stop
)
816 break; /* NOTE BREAK OUT */
818 /* no, we must deal with this character */
822 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
824 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
828 assert(coldp
!= NULL
);
830 if (ISSET(st
, stopst
))
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 */
851 states empty
= m
->empty
;
854 int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
855 int lastc
; /* previous c */
858 char *matchp
; /* last p at which a match ended */
860 AT("slow", start
, stop
, startst
, stopst
);
863 SP("sstart", st
, *p
);
864 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
869 c
= (p
== m
->endp
) ? OUT
: *p
;
871 /* is there an EOL and/or BOL between lastc and c? */
874 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
875 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
879 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
880 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
881 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
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
)) ) {
895 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
896 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
899 if (flagch
== BOW
|| flagch
== EOW
) {
900 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
901 SP("sboweow", st
, c
);
905 if (ISSET(st
, stopst
))
907 if (EQ(st
, empty
) || p
== stop
)
908 break; /* NOTE BREAK OUT */
910 /* no, we must deal with this character */
914 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
916 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
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)
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 */
951 onestate here
; /* note, macros know this name */
955 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
959 assert(pc
== stop
-1);
962 /* only characters can match */
963 assert(!NONCHAR(ch
) || ch
!= (char)OPND(s
));
964 if (ch
== (char)OPND(s
))
968 if (ch
== BOL
|| ch
== BOLEOL
)
972 if (ch
== EOL
|| ch
== BOLEOL
)
988 cs
= &g
->sets
[OPND(s
)];
989 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
992 case OBACK_
: /* ignored here */
996 case OPLUS_
: /* forward, this is just an empty */
999 case O_PLUS
: /* both forward and back */
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 */
1009 case OQUEST_
: /* two branches, both forward */
1011 FWD(aft
, aft
, OPND(s
));
1013 case O_QUEST
: /* just an empty */
1016 case OLPAREN
: /* not significant here */
1020 case OCH_
: /* mark the first two branches */
1022 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1023 FWD(aft
, aft
, OPND(s
));
1025 case OOR1
: /* done a branch, find the O_CH */
1026 if (ISSTATEIN(aft
, here
)) {
1028 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
1030 assert(OP(s
) == OOR2
);
1031 FWD(aft
, aft
, look
);
1034 case OOR2
: /* propagate OCH_'s marking */
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
));
1041 case O_CH
: /* just empty */
1044 default: /* ooooops... */
1055 - print - print a set of states
1057 == static void print(struct match *m, char *caption, states st, \
1058 == int ch, FILE *d);
1062 print(m
, caption
, st
, ch
, d
)
1069 struct re_guts
*g
= m
->g
;
1073 if (!(m
->eflags
®_TRACE
))
1076 fprintf(d
, "%s", caption
);
1078 fprintf(d
, " %s", pchar(ch
));
1079 for (i
= 0; i
< g
->nstates
; i
++)
1081 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1088 - at - print current situation
1090 == static void at(struct match *m, char *title, char *start, char *stop, \
1091 == sopno startst, sopno stopst);
1095 at(m
, title
, start
, stop
, startst
, stopst
)
1103 if (!(m
->eflags
®_TRACE
))
1106 printf("%s %s-", title
, pchar(*start
));
1107 printf("%s ", pchar(*stop
));
1108 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1112 #define PCHARDONE /* never again */
1114 - pchar - make a character printable
1116 == static char *pchar(int ch);
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 */
1128 static char pbuf
[10];
1130 if (isprint((uch
)ch
) || ch
== ' ')
1131 sprintf(pbuf
, "%c", ch
);
1133 sprintf(pbuf
, "\\%o", ch
);