2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Henry Spencer of the University of Toronto.
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.4 (Berkeley) 3/19/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 RCHAR_T
*offp
; /* offsets work from here */
76 RCHAR_T
*beginp
; /* start of string -- virtual NUL precedes */
77 RCHAR_T
*endp
; /* end of string -- virtual NUL here */
78 RCHAR_T
*coldp
; /* can be no match starting before here */
79 RCHAR_T
**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
, RCHAR_T
*string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
));
94 static RCHAR_T
*dissect
__P((struct match
*m
, RCHAR_T
*start
, RCHAR_T
*stop
, sopno startst
, sopno stopst
));
95 static RCHAR_T
*backref
__P((struct match
*m
, RCHAR_T
*start
, RCHAR_T
*stop
, sopno startst
, sopno stopst
, sopno lev
));
96 static RCHAR_T
*fast
__P((struct match
*m
, RCHAR_T
*start
, RCHAR_T
*stop
, sopno startst
, sopno stopst
));
97 static RCHAR_T
*slow
__P((struct match
*m
, RCHAR_T
*start
, RCHAR_T
*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) > RCHAR_T_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, RCHAR_T *string, \
136 == size_t nmatch, regmatch_t pmatch[], int eflags);
138 static int /* 0 success, REG_NOMATCH failure */
139 matcher(g
, string
, nmatch
, pmatch
, eflags
)
140 register struct re_guts
*g
;
146 register RCHAR_T
*endp
;
149 register struct match
*m
= &mv
;
150 register RCHAR_T
*dp
;
151 const register sopno gf
= g
->firststate
+1; /* +1 for OEND */
152 const register sopno gl
= g
->laststate
;
156 /* simplify the situation where possible */
157 if (g
->cflags
®_NOSUB
)
159 if (eflags
®_STARTEND
) {
160 start
= string
+ pmatch
[0].rm_so
;
161 stop
= string
+ pmatch
[0].rm_eo
;
164 stop
= start
+ STRLEN(start
);
169 /* prescreening; this does wonders for this rather slow code */
170 if (g
->must
!= NULL
) {
171 for (dp
= start
; dp
< stop
; dp
++)
172 if (*dp
== g
->must
[0] && stop
- dp
>= g
->mlen
&&
173 MEMCMP(dp
, g
->must
, (size_t)g
->mlen
) == 0)
175 if (dp
== stop
) /* we didn't find g->must */
179 /* match struct setup */
194 /* this loop does only one repetition except for backrefs */
196 endp
= fast(m
, start
, stop
, gf
, gl
);
197 if (endp
== NULL
) { /* a miss */
201 if (nmatch
== 0 && !g
->backrefs
)
202 break; /* no further info needed */
205 assert(m
->coldp
!= NULL
);
207 NOTE("finding start");
208 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
211 assert(m
->coldp
< m
->endp
);
214 if (nmatch
== 1 && !g
->backrefs
)
215 break; /* no further info needed */
217 /* oh my, he wants the subexpressions... */
218 if (m
->pmatch
== NULL
)
219 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
221 if (m
->pmatch
== NULL
) {
225 for (i
= 1; i
<= m
->g
->nsub
; i
++)
226 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
227 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
229 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
231 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
232 m
->lastpos
= (RCHAR_T
**)malloc((g
->nplus
+1) *
234 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
239 NOTE("backref dissect");
240 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
245 /* uh-oh... we couldn't find a subexpression-level match */
246 assert(g
->backrefs
); /* must be back references doing it */
247 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
249 if (dp
!= NULL
|| endp
<= m
->coldp
)
252 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
255 /* try it on a shorter possibility */
257 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
258 assert(m
->pmatch
[i
].rm_so
== -1);
259 assert(m
->pmatch
[i
].rm_eo
== -1);
262 NOTE("backoff dissect");
263 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
265 assert(dp
== NULL
|| dp
== endp
);
266 if (dp
!= NULL
) /* found a shorter one */
269 /* despite initial appearances, there is no match here */
271 start
= m
->coldp
+ 1; /* recycle starting later */
272 assert(start
<= stop
);
275 /* fill in the details if requested */
277 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
278 pmatch
[0].rm_eo
= endp
- m
->offp
;
281 assert(m
->pmatch
!= NULL
);
282 for (i
= 1; i
< nmatch
; i
++)
284 pmatch
[i
] = m
->pmatch
[i
];
286 pmatch
[i
].rm_so
= -1;
287 pmatch
[i
].rm_eo
= -1;
291 if (m
->pmatch
!= NULL
)
292 free((char *)m
->pmatch
);
293 if (m
->lastpos
!= NULL
)
294 free((char *)m
->lastpos
);
300 - dissect - figure out what matched what, no back references
301 == static RCHAR_T *dissect(register struct match *m, RCHAR_T *start, \
302 == RCHAR_T *stop, sopno startst, sopno stopst);
304 static RCHAR_T
* /* == stop (success) always */
305 dissect(m
, start
, stop
, startst
, stopst
)
306 register struct match
*m
;
313 register sopno ss
; /* start sop of current subRE */
314 register sopno es
; /* end sop of current subRE */
315 register RCHAR_T
*sp
; /* start of string matched by it */
316 register RCHAR_T
*stp
; /* string matched by it cannot pass here */
317 register RCHAR_T
*rest
; /* start of rest of string */
318 register RCHAR_T
*tail
; /* string unmatched by rest of RE */
319 register sopno ssub
; /* start sop of subsubRE */
320 register sopno esub
; /* end sop of subsubRE */
321 register RCHAR_T
*ssp
; /* start of string matched by subsubRE */
322 register RCHAR_T
*sep
; /* end of string matched by subsubRE */
323 register RCHAR_T
*oldssp
; /* previous ssp */
324 register RCHAR_T
*dp
;
326 AT("diss", start
, stop
, startst
, stopst
);
328 for (ss
= startst
; ss
< stopst
; ss
= es
) {
329 /* identify end of subRE */
331 switch (OP(m
->g
->strip
[es
])) {
334 es
+= OPND(m
->g
->strip
[es
]);
337 while (OP(m
->g
->strip
[es
]) != O_CH
)
338 es
+= OPND(m
->g
->strip
[es
]);
343 /* figure out what it matched */
344 switch (OP(m
->g
->strip
[ss
])) {
364 /* cases where length of match is hard to find */
368 /* how long could this one be? */
369 rest
= slow(m
, sp
, stp
, ss
, es
);
370 assert(rest
!= NULL
); /* it did match */
371 /* could the rest match the rest? */
372 tail
= slow(m
, rest
, stop
, es
, stopst
);
375 /* no -- try a shorter match for this one */
377 assert(stp
>= sp
); /* it did work */
381 /* did innards match? */
382 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
383 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
392 /* how long could this one be? */
393 rest
= slow(m
, sp
, stp
, ss
, es
);
394 assert(rest
!= NULL
); /* it did match */
395 /* could the rest match the rest? */
396 tail
= slow(m
, rest
, stop
, es
, stopst
);
399 /* no -- try a shorter match for this one */
401 assert(stp
>= sp
); /* it did work */
407 for (;;) { /* find last match of innards */
408 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
409 if (sep
== NULL
|| sep
== ssp
)
410 break; /* failed or matched null */
411 oldssp
= ssp
; /* on to next try */
415 /* last successful match */
419 assert(sep
== rest
); /* must exhaust substring */
420 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
421 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
428 /* how long could this one be? */
429 rest
= slow(m
, sp
, stp
, ss
, es
);
430 assert(rest
!= NULL
); /* it did match */
431 /* could the rest match the rest? */
432 tail
= slow(m
, rest
, stop
, es
, stopst
);
435 /* no -- try a shorter match for this one */
437 assert(stp
>= sp
); /* it did work */
440 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
441 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
442 for (;;) { /* find first matching branch */
443 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
444 break; /* it matched all of it */
445 /* that one missed, try next one */
446 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
448 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
450 esub
+= OPND(m
->g
->strip
[esub
]);
451 if (OP(m
->g
->strip
[esub
]) == OOR2
)
454 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
456 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
468 i
= OPND(m
->g
->strip
[ss
]);
469 assert(0 < i
&& i
<= m
->g
->nsub
);
470 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
473 i
= OPND(m
->g
->strip
[ss
]);
474 assert(0 < i
&& i
<= m
->g
->nsub
);
475 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
488 - backref - figure out what matched what, figuring in back references
489 == static RCHAR_T *backref(register struct match *m, RCHAR_T *start, \
490 == RCHAR_T *stop, sopno startst, sopno stopst, sopno lev);
492 static RCHAR_T
* /* == stop (success) or NULL (failure) */
493 backref(m
, start
, stop
, startst
, stopst
, lev
)
494 register struct match
*m
;
499 sopno lev
; /* PLUS nesting level */
502 register sopno ss
; /* start sop of current subRE */
503 register RCHAR_T
*sp
; /* start of string matched by it */
504 register sopno ssub
; /* start sop of subsubRE */
505 register sopno esub
; /* end sop of subsubRE */
506 register RCHAR_T
*ssp
; /* start of string matched by subsubRE */
507 register RCHAR_T
*dp
;
511 register regoff_t offsave
;
514 AT("back", start
, stop
, startst
, stopst
);
517 /* get as far as we can with easy stuff */
519 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
520 switch (OP(s
= m
->g
->strip
[ss
])) {
522 if (sp
== stop
|| *sp
++ != (RCHAR_T
)OPND(s
))
531 cs
= &m
->g
->sets
[OPND(s
)];
532 if (sp
== stop
|| !CHIN(cs
, *sp
++))
536 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
537 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
538 (m
->g
->cflags
®_NEWLINE
)) )
544 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
545 (sp
< m
->endp
&& *sp
== '\n' &&
546 (m
->g
->cflags
®_NEWLINE
)) )
552 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
553 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
554 (m
->g
->cflags
®_NEWLINE
)) ||
556 !ISWORD(*(sp
-1))) ) &&
557 (sp
< m
->endp
&& ISWORD(*sp
)) )
563 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
564 (sp
< m
->endp
&& *sp
== '\n' &&
565 (m
->g
->cflags
®_NEWLINE
)) ||
566 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
567 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
574 case OOR1
: /* matches null but needs to skip */
578 assert(OP(s
) == OOR2
);
580 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
581 /* note that the ss++ gets us past the O_CH */
583 default: /* have to make a choice */
587 if (!hard
) { /* that was it! */
592 ss
--; /* adjust for the for's final increment */
595 AT("hard", sp
, stop
, ss
, stopst
);
598 case OBACK_
: /* the vilest depths */
600 assert(0 < i
&& i
<= m
->g
->nsub
);
601 if (m
->pmatch
[i
].rm_eo
== -1)
603 assert(m
->pmatch
[i
].rm_so
!= -1);
604 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
605 assert(stop
- m
->beginp
>= len
);
607 return(NULL
); /* not enough left to match */
608 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
609 if (memcmp(sp
, ssp
, len
) != 0)
611 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
613 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
615 case OQUEST_
: /* to null or not */
616 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
618 return(dp
); /* not */
619 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
622 assert(m
->lastpos
!= NULL
);
623 assert(lev
+1 <= m
->g
->nplus
);
624 m
->lastpos
[lev
+1] = sp
;
625 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1));
628 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
629 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
630 /* try another pass */
631 m
->lastpos
[lev
] = sp
;
632 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
);
634 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
638 case OCH_
: /* find the right one, if any */
640 esub
= ss
+ OPND(s
) - 1;
641 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
642 for (;;) { /* find first matching branch */
643 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
);
646 /* that one missed, try next one */
647 if (OP(m
->g
->strip
[esub
]) == O_CH
)
648 return(NULL
); /* there is none */
650 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
652 esub
+= OPND(m
->g
->strip
[esub
]);
653 if (OP(m
->g
->strip
[esub
]) == OOR2
)
656 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
659 case OLPAREN
: /* must undo assignment if rest fails */
661 assert(0 < i
&& i
<= m
->g
->nsub
);
662 offsave
= m
->pmatch
[i
].rm_so
;
663 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
664 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
667 m
->pmatch
[i
].rm_so
= offsave
;
670 case ORPAREN
: /* must undo assignment if rest fails */
672 assert(0 < i
&& i
<= m
->g
->nsub
);
673 offsave
= m
->pmatch
[i
].rm_eo
;
674 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
675 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
678 m
->pmatch
[i
].rm_eo
= offsave
;
692 - fast - step through the string at top speed
693 == static RCHAR_T *fast(register struct match *m, RCHAR_T *start, \
694 == RCHAR_T *stop, sopno startst, sopno stopst);
696 static RCHAR_T
* /* where tentative match ended, or NULL */
697 fast(m
, start
, stop
, startst
, stopst
)
698 register struct match
*m
;
704 register states st
= m
->st
;
705 register states fresh
= m
->fresh
;
706 register states tmp
= m
->tmp
;
707 register RCHAR_T
*p
= start
;
708 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
709 register int lastc
; /* previous c */
712 register RCHAR_T
*coldp
; /* last p after which no match was underway */
716 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
723 c
= (p
== m
->endp
) ? OUT
: *p
;
727 /* is there an EOL and/or BOL between lastc and c? */
730 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
731 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
735 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
736 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
737 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
742 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
746 /* how about a word boundary? */
747 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
748 (c
!= OUT
&& ISWORD(c
)) ) {
751 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
752 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
755 if (flagch
== BOW
|| flagch
== EOW
) {
756 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
761 if (ISSET(st
, stopst
) || p
== stop
)
762 break; /* NOTE BREAK OUT */
764 /* no, we must deal with this character */
768 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
770 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
774 assert(coldp
!= NULL
);
776 if (ISSET(st
, stopst
))
783 - slow - step through the string more deliberately
784 == static RCHAR_T *slow(register struct match *m, RCHAR_T *start, \
785 == RCHAR_T *stop, sopno startst, sopno stopst);
787 static RCHAR_T
* /* where it ended */
788 slow(m
, start
, stop
, startst
, stopst
)
789 register struct match
*m
;
795 register states st
= m
->st
;
796 register states empty
= m
->empty
;
797 register states tmp
= m
->tmp
;
798 register RCHAR_T
*p
= start
;
799 register int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
800 register int lastc
; /* previous c */
803 register RCHAR_T
*matchp
; /* last p at which a match ended */
805 AT("slow", start
, stop
, startst
, stopst
);
808 SP("sstart", st
, *p
);
809 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
814 c
= (p
== m
->endp
) ? OUT
: *p
;
816 /* is there an EOL and/or BOL between lastc and c? */
819 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
820 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
824 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
825 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
826 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
831 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
832 SP("sboleol", st
, c
);
835 /* how about a word boundary? */
836 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
837 (c
!= OUT
&& ISWORD(c
)) ) {
840 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
841 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
844 if (flagch
== BOW
|| flagch
== EOW
) {
845 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
846 SP("sboweow", st
, c
);
850 if (ISSET(st
, stopst
))
852 if (EQ(st
, empty
) || p
== stop
)
853 break; /* NOTE BREAK OUT */
855 /* no, we must deal with this character */
859 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
861 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
870 - step - map set of states reachable before char to set reachable after
871 == static states step(register struct re_guts *g, sopno start, sopno stop, \
872 == register states bef, int ch, register states aft);
873 == #define BOL (OUT+1)
874 == #define EOL (BOL+1)
875 == #define BOLEOL (BOL+2)
876 == #define NOTHING (BOL+3)
877 == #define BOW (BOL+4)
878 == #define EOW (BOL+5)
879 == #define CODEMAX (BOL+5) // highest code used
880 == #define NONCHAR(c) ((c) > CHAR_MAX)
881 == #define NNONCHAR (CODEMAX-CHAR_MAX)
884 step(g
, start
, stop
, bef
, ch
, aft
)
885 register struct re_guts
*g
;
886 sopno start
; /* start state within strip */
887 sopno stop
; /* state after stop state within strip */
888 register states bef
; /* states reachable before */
889 int ch
; /* character or NONCHAR code */
890 register states aft
; /* states already known reachable after */
895 register onestate here
; /* note, macros know this name */
899 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
903 assert(pc
== stop
-1);
906 /* only characters can match */
907 assert(!NONCHAR(ch
) || ch
!= (RCHAR_T
)OPND(s
));
908 if (ch
== (RCHAR_T
)OPND(s
))
912 if (ch
== BOL
|| ch
== BOLEOL
)
916 if (ch
== EOL
|| ch
== BOLEOL
)
932 cs
= &g
->sets
[OPND(s
)];
933 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
936 case OBACK_
: /* ignored here */
940 case OPLUS_
: /* forward, this is just an empty */
943 case O_PLUS
: /* both forward and back */
945 i
= ISSETBACK(aft
, OPND(s
));
946 BACK(aft
, aft
, OPND(s
));
947 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
948 /* oho, must reconsider loop body */
953 case OQUEST_
: /* two branches, both forward */
955 FWD(aft
, aft
, OPND(s
));
957 case O_QUEST
: /* just an empty */
960 case OLPAREN
: /* not significant here */
964 case OCH_
: /* mark the first two branches */
966 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
967 FWD(aft
, aft
, OPND(s
));
969 case OOR1
: /* done a branch, find the O_CH */
970 if (ISSTATEIN(aft
, here
)) {
972 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
974 assert(OP(s
) == OOR2
);
978 case OOR2
: /* propagate OCH_'s marking */
980 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
981 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
982 FWD(aft
, aft
, OPND(s
));
985 case O_CH
: /* just empty */
988 default: /* ooooops... */
999 - print - print a set of states
1001 == static void print(struct match *m, char *caption, states st, \
1002 == int ch, FILE *d);
1006 print(m
, caption
, st
, ch
, d
)
1013 register struct re_guts
*g
= m
->g
;
1015 register int first
= 1;
1017 if (!(m
->eflags
®_TRACE
))
1020 fprintf(d
, "%s", caption
);
1022 fprintf(d
, " %s", pchar(ch
));
1023 for (i
= 0; i
< g
->nstates
; i
++)
1025 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1032 - at - print current situation
1034 == static void at(struct match *m, char *title, char *start, char *stop, \
1035 == sopno startst, sopno stopst);
1039 at(m
, title
, start
, stop
, startst
, stopst
)
1047 if (!(m
->eflags
®_TRACE
))
1050 printf("%s %s-", title
, pchar(*start
));
1051 printf("%s ", pchar(*stop
));
1052 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1056 #define PCHARDONE /* never again */
1058 - pchar - make a character printable
1060 == static char *pchar(int ch);
1063 * Is this identical to regchar() over in debug.c? Well, yes. But a
1064 * duplicate here avoids having a debugging-capable regexec.o tied to
1065 * a matching debug.o, and this is convenient. It all disappears in
1066 * the non-debug compilation anyway, so it doesn't matter much.
1068 static char * /* -> representation */
1072 static char pbuf
[10];
1074 if (isprint(ch
) || ch
== ' ')
1075 sprintf(pbuf
, "%c", ch
);
1077 sprintf(pbuf
, "\\%o", ch
);