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
39 * $FreeBSD: src/lib/libc/regex/engine.c,v 1.5.8.1 2000/07/31 06:30:37 dcs Exp $
40 * $DragonFly: src/lib/libc/regex/engine.c,v 1.7 2005/11/20 09:18:37 swildner Exp $
44 * The matching engine and friends. This file is #included by regexec.c
45 * after suitable #defines of a variety of macros used herein, so that
46 * different state representations can be used without duplicating masses
51 #define matcher smatcher
54 #define dissect sdissect
55 #define backref sbackref
62 #define matcher lmatcher
65 #define dissect ldissect
66 #define backref lbackref
73 /* another structure passed up and down to avoid zillions of parameters */
77 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
78 char *offp
; /* offsets work from here */
79 char *beginp
; /* start of string -- virtual NUL precedes */
80 char *endp
; /* end of string -- virtual NUL here */
81 char *coldp
; /* can be no match starting before here */
82 char **lastpos
; /* [nplus+1] */
84 states st
; /* current states */
85 states fresh
; /* states for a fresh start */
86 states tmp
; /* temporary */
87 states empty
; /* empty set of states */
90 /* ========= begin header generated by ./mkh ========= */
95 /* === engine.c === */
96 static int matcher(struct re_guts
*g
, char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
);
97 static char *dissect(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
);
98 static char *backref(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
, sopno lev
);
99 static char *fast(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
);
100 static char *slow(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
);
101 static states
step(struct re_guts
*g
, sopno start
, sopno stop
, states bef
, int ch
, states aft
);
104 #define BOLEOL (BOL+2)
105 #define NOTHING (BOL+3)
108 #define CODEMAX (BOL+5) /* highest code used */
109 #define NONCHAR(c) ((c) > CHAR_MAX)
110 #define NNONCHAR (CODEMAX-CHAR_MAX)
112 static void print(struct match
*m
, char *caption
, states st
, int ch
, FILE *d
);
115 static void at(struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
, sopno stopst
);
118 static char *pchar(int ch
);
124 /* ========= end header generated by ./mkh ========= */
127 #define SP(t, s, c) print(m, t, s, c, stdout)
128 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
129 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
131 #define SP(t, s, c) /* nothing */
132 #define AT(t, p1, p2, s1, s2) /* nothing */
133 #define NOTE(s) /* nothing */
137 - matcher - the actual matching engine
138 == static int matcher(struct re_guts *g, char *string, \
139 == size_t nmatch, regmatch_t pmatch[], int eflags);
141 static int /* 0 success, REG_NOMATCH failure */
142 matcher(struct re_guts
*g
, char *string
, size_t nmatch
, regmatch_t pmatch
[],
148 struct match
*m
= &mv
;
150 const sopno gf
= g
->firststate
+1; /* +1 for OEND */
151 const sopno gl
= g
->laststate
;
154 /* Boyer-Moore algorithms variables */
164 /* simplify the situation where possible */
165 if (g
->cflags
®_NOSUB
)
167 if (eflags
®_STARTEND
) {
168 start
= string
+ pmatch
[0].rm_so
;
169 stop
= string
+ pmatch
[0].rm_eo
;
172 stop
= start
+ strlen(start
);
177 /* prescreening; this does wonders for this rather slow code */
178 if (g
->must
!= NULL
) {
179 if (g
->charjump
!= NULL
&& g
->matchjump
!= NULL
) {
181 mustlast
= g
->must
+ g
->mlen
- 1;
182 charjump
= g
->charjump
;
183 matchjump
= g
->matchjump
;
185 for (dp
= start
+g
->mlen
-1; dp
< stop
;) {
186 /* Fast skip non-matches */
187 while (dp
< stop
&& charjump
[*dp
])
194 /* We depend on not being used for
195 * for strings of length 1
197 while (*--dp
== *--pp
&& pp
!= mustfirst
);
202 /* Jump to next possible match */
203 mj
= matchjump
[pp
- mustfirst
];
205 dp
+= (cj
< mj
? mj
: cj
);
211 for (dp
= start
; dp
< stop
; dp
++)
212 if (*dp
== g
->must
[0] &&
213 stop
- dp
>= g
->mlen
&&
214 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
216 if (dp
== stop
) /* we didn't find g->must */
221 /* match struct setup */
236 /* Adjust start according to moffset, to speed things up */
238 start
= ((dp
- g
->moffset
) < start
) ? start
: dp
- g
->moffset
;
240 /* this loop does only one repetition except for backrefs */
242 endp
= fast(m
, start
, stop
, gf
, gl
);
243 if (endp
== NULL
) { /* a miss */
244 if (m
->pmatch
!= NULL
)
245 free((char *)m
->pmatch
);
246 if (m
->lastpos
!= NULL
)
247 free((char *)m
->lastpos
);
251 if (nmatch
== 0 && !g
->backrefs
)
252 break; /* no further info needed */
255 assert(m
->coldp
!= NULL
);
257 NOTE("finding start");
258 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
261 assert(m
->coldp
< m
->endp
);
264 if (nmatch
== 1 && !g
->backrefs
)
265 break; /* no further info needed */
267 /* oh my, he wants the subexpressions... */
268 if (m
->pmatch
== NULL
)
269 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
271 if (m
->pmatch
== NULL
) {
275 for (i
= 1; i
<= m
->g
->nsub
; i
++)
276 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
277 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
279 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
281 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
282 m
->lastpos
= (char **)malloc((g
->nplus
+1) *
284 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
289 NOTE("backref dissect");
290 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
295 /* uh-oh... we couldn't find a subexpression-level match */
296 assert(g
->backrefs
); /* must be back references doing it */
297 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
299 if (dp
!= NULL
|| endp
<= m
->coldp
)
302 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
305 /* try it on a shorter possibility */
307 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
308 assert(m
->pmatch
[i
].rm_so
== -1);
309 assert(m
->pmatch
[i
].rm_eo
== -1);
312 NOTE("backoff dissect");
313 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
315 assert(dp
== NULL
|| dp
== endp
);
316 if (dp
!= NULL
) /* found a shorter one */
319 /* despite initial appearances, there is no match here */
321 start
= m
->coldp
+ 1; /* recycle starting later */
322 assert(start
<= stop
);
325 /* fill in the details if requested */
327 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
328 pmatch
[0].rm_eo
= endp
- m
->offp
;
331 assert(m
->pmatch
!= NULL
);
332 for (i
= 1; i
< nmatch
; i
++)
334 pmatch
[i
] = m
->pmatch
[i
];
336 pmatch
[i
].rm_so
= -1;
337 pmatch
[i
].rm_eo
= -1;
341 if (m
->pmatch
!= NULL
)
342 free((char *)m
->pmatch
);
343 if (m
->lastpos
!= NULL
)
344 free((char *)m
->lastpos
);
350 - dissect - figure out what matched what, no back references
351 == static char *dissect(struct match *m, char *start, \
352 == char *stop, sopno startst, sopno stopst);
354 static char * /* == stop (success) always */
355 dissect(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
)
358 sopno ss
; /* start sop of current subRE */
359 sopno es
; /* end sop of current subRE */
360 char *sp
; /* start of string matched by it */
361 char *stp
; /* string matched by it cannot pass here */
362 char *rest
; /* start of rest of string */
363 char *tail
; /* string unmatched by rest of RE */
364 sopno ssub
; /* start sop of subsubRE */
365 sopno esub
; /* end sop of subsubRE */
366 char *ssp
; /* start of string matched by subsubRE */
367 char *sep
; /* end of string matched by subsubRE */
368 char *oldssp
; /* previous ssp */
371 AT("diss", start
, stop
, startst
, stopst
);
373 for (ss
= startst
; ss
< stopst
; ss
= es
) {
374 /* identify end of subRE */
376 switch (OP(m
->g
->strip
[es
])) {
379 es
+= OPND(m
->g
->strip
[es
]);
382 while (OP(m
->g
->strip
[es
]) != O_CH
)
383 es
+= OPND(m
->g
->strip
[es
]);
388 /* figure out what it matched */
389 switch (OP(m
->g
->strip
[ss
])) {
409 /* cases where length of match is hard to find */
413 /* how long could this one be? */
414 rest
= slow(m
, sp
, stp
, ss
, es
);
415 assert(rest
!= NULL
); /* it did match */
416 /* could the rest match the rest? */
417 tail
= slow(m
, rest
, stop
, es
, stopst
);
420 /* no -- try a shorter match for this one */
422 assert(stp
>= sp
); /* it did work */
426 /* did innards match? */
427 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
428 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
437 /* how long could this one be? */
438 rest
= slow(m
, sp
, stp
, ss
, es
);
439 assert(rest
!= NULL
); /* it did match */
440 /* could the rest match the rest? */
441 tail
= slow(m
, rest
, stop
, es
, stopst
);
444 /* no -- try a shorter match for this one */
446 assert(stp
>= sp
); /* it did work */
452 for (;;) { /* find last match of innards */
453 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
454 if (sep
== NULL
|| sep
== ssp
)
455 break; /* failed or matched null */
456 oldssp
= ssp
; /* on to next try */
460 /* last successful match */
464 assert(sep
== rest
); /* must exhaust substring */
465 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
466 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
473 /* how long could this one be? */
474 rest
= slow(m
, sp
, stp
, ss
, es
);
475 assert(rest
!= NULL
); /* it did match */
476 /* could the rest match the rest? */
477 tail
= slow(m
, rest
, stop
, es
, stopst
);
480 /* no -- try a shorter match for this one */
482 assert(stp
>= sp
); /* it did work */
485 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
486 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
487 for (;;) { /* find first matching branch */
488 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
489 break; /* it matched all of it */
490 /* that one missed, try next one */
491 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
493 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
495 esub
+= OPND(m
->g
->strip
[esub
]);
496 if (OP(m
->g
->strip
[esub
]) == OOR2
)
499 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
501 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
513 i
= OPND(m
->g
->strip
[ss
]);
514 assert(0 < i
&& i
<= m
->g
->nsub
);
515 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
518 i
= OPND(m
->g
->strip
[ss
]);
519 assert(0 < i
&& i
<= m
->g
->nsub
);
520 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
533 - backref - figure out what matched what, figuring in back references
534 == static char *backref(struct match *m, char *start, \
535 == char *stop, sopno startst, sopno stopst, sopno lev);
537 static char * /* == stop (success) or NULL (failure) */
538 backref(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
,
539 sopno lev
) /* PLUS nesting level */
542 sopno ss
; /* start sop of current subRE */
543 char *sp
; /* start of string matched by it */
544 sopno ssub
; /* start sop of subsubRE */
545 sopno esub
; /* end sop of subsubRE */
546 char *ssp
; /* start of string matched by subsubRE */
554 AT("back", start
, stop
, startst
, stopst
);
557 /* get as far as we can with easy stuff */
559 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
560 switch (OP(s
= m
->g
->strip
[ss
])) {
562 if (sp
== stop
|| *sp
++ != (char)OPND(s
))
571 cs
= &m
->g
->sets
[OPND(s
)];
572 if (sp
== stop
|| !CHIN(cs
, *sp
++))
576 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
577 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
578 (m
->g
->cflags
®_NEWLINE
)) )
584 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
585 (sp
< m
->endp
&& *sp
== '\n' &&
586 (m
->g
->cflags
®_NEWLINE
)) )
592 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
593 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
594 (m
->g
->cflags
®_NEWLINE
)) ||
596 !ISWORD(*(sp
-1))) ) &&
597 (sp
< m
->endp
&& ISWORD(*sp
)) )
603 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
604 (sp
< m
->endp
&& *sp
== '\n' &&
605 (m
->g
->cflags
®_NEWLINE
)) ||
606 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
607 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
614 case OOR1
: /* matches null but needs to skip */
618 assert(OP(s
) == OOR2
);
620 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
621 /* note that the ss++ gets us past the O_CH */
623 default: /* have to make a choice */
627 if (!hard
) { /* that was it! */
632 ss
--; /* adjust for the for's final increment */
635 AT("hard", sp
, stop
, ss
, stopst
);
638 case OBACK_
: /* the vilest depths */
640 assert(0 < i
&& i
<= m
->g
->nsub
);
641 if (m
->pmatch
[i
].rm_eo
== -1)
643 assert(m
->pmatch
[i
].rm_so
!= -1);
644 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
645 assert(stop
- m
->beginp
>= len
);
647 return(NULL
); /* not enough left to match */
648 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
649 if (memcmp(sp
, ssp
, len
) != 0)
651 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
653 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
655 case OQUEST_
: /* to null or not */
656 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
658 return(dp
); /* not */
659 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
662 assert(m
->lastpos
!= NULL
);
663 assert(lev
+1 <= m
->g
->nplus
);
664 m
->lastpos
[lev
+1] = sp
;
665 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1));
668 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
669 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
670 /* try another pass */
671 m
->lastpos
[lev
] = sp
;
672 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
);
674 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
678 case OCH_
: /* find the right one, if any */
680 esub
= ss
+ OPND(s
) - 1;
681 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
682 for (;;) { /* find first matching branch */
683 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
);
686 /* that one missed, try next one */
687 if (OP(m
->g
->strip
[esub
]) == O_CH
)
688 return(NULL
); /* there is none */
690 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
692 esub
+= OPND(m
->g
->strip
[esub
]);
693 if (OP(m
->g
->strip
[esub
]) == OOR2
)
696 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
699 case OLPAREN
: /* must undo assignment if rest fails */
701 assert(0 < i
&& i
<= m
->g
->nsub
);
702 offsave
= m
->pmatch
[i
].rm_so
;
703 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
704 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
707 m
->pmatch
[i
].rm_so
= offsave
;
710 case ORPAREN
: /* must undo assignment if rest fails */
712 assert(0 < i
&& i
<= m
->g
->nsub
);
713 offsave
= m
->pmatch
[i
].rm_eo
;
714 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
715 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
718 m
->pmatch
[i
].rm_eo
= offsave
;
729 return "shut up gcc";
733 - fast - step through the string at top speed
734 == static char *fast(struct match *m, char *start, \
735 == char *stop, sopno startst, sopno stopst);
737 static char * /* where tentative match ended, or NULL */
738 fast(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
)
741 states fresh
= m
->fresh
;
744 int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
745 int lastc
; /* previous c */
748 char *coldp
; /* last p after which no match was underway */
752 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
759 c
= (p
== m
->endp
) ? OUT
: *p
;
763 /* is there an EOL and/or BOL between lastc and c? */
766 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
767 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
771 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
772 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
773 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
778 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
782 /* how about a word boundary? */
783 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
784 (c
!= OUT
&& ISWORD(c
)) ) {
787 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
788 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
791 if (flagch
== BOW
|| flagch
== EOW
) {
792 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
797 if (ISSET(st
, stopst
) || p
== stop
)
798 break; /* NOTE BREAK OUT */
800 /* no, we must deal with this character */
804 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
806 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
810 assert(coldp
!= NULL
);
812 if (ISSET(st
, stopst
))
819 - slow - step through the string more deliberately
820 == static char *slow(struct match *m, char *start, \
821 == char *stop, sopno startst, sopno stopst);
823 static char * /* where it ended */
824 slow(struct match
*m
, char *start
, char *stop
, sopno startst
, sopno stopst
)
827 states empty
= m
->empty
;
830 int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
831 int lastc
; /* previous c */
834 char *matchp
; /* last p at which a match ended */
836 AT("slow", start
, stop
, startst
, stopst
);
839 SP("sstart", st
, *p
);
840 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
845 c
= (p
== m
->endp
) ? OUT
: *p
;
847 /* is there an EOL and/or BOL between lastc and c? */
850 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
851 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
855 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
856 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
857 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
862 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
863 SP("sboleol", st
, c
);
866 /* how about a word boundary? */
867 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
868 (c
!= OUT
&& ISWORD(c
)) ) {
871 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
872 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
875 if (flagch
== BOW
|| flagch
== EOW
) {
876 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
877 SP("sboweow", st
, c
);
881 if (ISSET(st
, stopst
))
883 if (EQ(st
, empty
) || p
== stop
)
884 break; /* NOTE BREAK OUT */
886 /* no, we must deal with this character */
890 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
892 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
901 - step - map set of states reachable before char to set reachable after
902 == static states step(struct re_guts *g, sopno start, sopno stop, \
903 == states bef, int ch, states aft);
904 == #define BOL (OUT+1)
905 == #define EOL (BOL+1)
906 == #define BOLEOL (BOL+2)
907 == #define NOTHING (BOL+3)
908 == #define BOW (BOL+4)
909 == #define EOW (BOL+5)
910 == #define CODEMAX (BOL+5) // highest code used
911 == #define NONCHAR(c) ((c) > CHAR_MAX)
912 == #define NNONCHAR (CODEMAX-CHAR_MAX)
915 step(struct re_guts
*g
,
916 sopno start
, /* start state within strip */
917 sopno stop
, /* state after stop state within strip */
918 states bef
, /* states reachable before */
919 int ch
, /* character or NONCHAR code */
920 states aft
) /* states already known reachable after */
925 onestate here
; /* note, macros know this name */
929 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
933 assert(pc
== stop
-1);
936 /* only characters can match */
937 assert(!NONCHAR(ch
) || ch
!= (char)OPND(s
));
938 if (ch
== (char)OPND(s
))
942 if (ch
== BOL
|| ch
== BOLEOL
)
946 if (ch
== EOL
|| ch
== BOLEOL
)
962 cs
= &g
->sets
[OPND(s
)];
963 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
966 case OBACK_
: /* ignored here */
970 case OPLUS_
: /* forward, this is just an empty */
973 case O_PLUS
: /* both forward and back */
975 i
= ISSETBACK(aft
, OPND(s
));
976 BACK(aft
, aft
, OPND(s
));
977 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
978 /* oho, must reconsider loop body */
983 case OQUEST_
: /* two branches, both forward */
985 FWD(aft
, aft
, OPND(s
));
987 case O_QUEST
: /* just an empty */
990 case OLPAREN
: /* not significant here */
994 case OCH_
: /* mark the first two branches */
996 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
997 FWD(aft
, aft
, OPND(s
));
999 case OOR1
: /* done a branch, find the O_CH */
1000 if (ISSTATEIN(aft
, here
)) {
1002 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
1004 assert(OP(s
) == OOR2
);
1005 FWD(aft
, aft
, look
);
1008 case OOR2
: /* propagate OCH_'s marking */
1010 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
1011 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1012 FWD(aft
, aft
, OPND(s
));
1015 case O_CH
: /* just empty */
1018 default: /* ooooops... */
1029 - print - print a set of states
1031 == static void print(struct match *m, char *caption, states st, \
1032 == int ch, FILE *d);
1036 print(struct match
*m
, char *caption
, states st
, int ch
, FILE *d
)
1038 struct re_guts
*g
= m
->g
;
1042 if (!(m
->eflags
®_TRACE
))
1045 fprintf(d
, "%s", caption
);
1047 fprintf(d
, " %s", pchar(ch
));
1048 for (i
= 0; i
< g
->nstates
; i
++)
1050 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1057 - at - print current situation
1059 == static void at(struct match *m, char *title, char *start, char *stop, \
1060 == sopno startst, sopno stopst);
1064 at(struct match
*m
, char *title
, char *start
, char *stop
, sopno startst
,
1067 if (!(m
->eflags
®_TRACE
))
1070 printf("%s %s-", title
, pchar(*start
));
1071 printf("%s ", pchar(*stop
));
1072 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1076 #define PCHARDONE /* never again */
1078 - pchar - make a character printable
1080 == static char *pchar(int ch);
1083 * Is this identical to regchar() over in debug.c? Well, yes. But a
1084 * duplicate here avoids having a debugging-capable regexec.o tied to
1085 * a matching debug.o, and this is convenient. It all disappears in
1086 * the non-debug compilation anyway, so it doesn't matter much.
1088 static char * /* -> representation */
1091 static char pbuf
[10];
1093 if (isprint((uch
)ch
) || ch
== ' ')
1094 sprintf(pbuf
, "%c", ch
);
1096 sprintf(pbuf
, "\\%o", ch
);