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>
37 __FBSDID("$FreeBSD: src/lib/libc/regex/engine.c,v 1.21 2007/05/25 12:44:58 delphij Exp $");
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 #define matcher mmatcher
72 #define dissect mdissect
73 #define backref mbackref
80 /* another structure passed up and down to avoid zillions of parameters */
84 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
85 const char *offp
; /* offsets work from here */
86 const char *beginp
; /* start of string -- virtual NUL precedes */
87 const char *endp
; /* end of string -- virtual NUL here */
88 const char *coldp
; /* can be no match starting before here */
89 const char **lastpos
; /* [nplus+1] */
91 states st
; /* current states */
92 states fresh
; /* states for a fresh start */
93 states tmp
; /* temporary */
94 states empty
; /* empty set of states */
95 mbstate_t mbs
; /* multibyte conversion state */
98 /* ========= begin header generated by ./mkh ========= */
103 /* === engine.c === */
104 static int matcher(struct re_guts
*g
, const char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
);
105 static const char *dissect(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
106 static const char *backref(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
, sopno lev
, int);
107 static const char *fast(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
108 static const char *slow(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
109 static states
step(struct re_guts
*g
, sopno start
, sopno stop
, states bef
, wint_t ch
, states aft
);
110 #define MAX_RECURSION 100
113 #define BOLEOL (BOL-2)
114 #define NOTHING (BOL-3)
117 #define BADCHAR (BOL-6)
118 #define NONCHAR(c) ((c) <= OUT)
120 static void print(struct match
*m
, const char *caption
, states st
, int ch
, FILE *d
);
123 static void at(struct match
*m
, const char *title
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
126 static const char *pchar(int ch
);
132 /* ========= end header generated by ./mkh ========= */
135 #define SP(t, s, c) print(m, t, s, c, stdout)
136 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
137 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
139 #define SP(t, s, c) /* nothing */
140 #define AT(t, p1, p2, s1, s2) /* nothing */
141 #define NOTE(s) /* nothing */
145 - matcher - the actual matching engine
146 == static int matcher(struct re_guts *g, const char *string, \
147 == size_t nmatch, regmatch_t pmatch[], int eflags);
149 static int /* 0 success, REG_NOMATCH failure */
150 matcher(struct re_guts
*g
,
159 struct match
*m
= &mv
;
161 const sopno gf
= g
->firststate
+1; /* +1 for OEND */
162 const sopno gl
= g
->laststate
;
165 /* Boyer-Moore algorithms variables */
168 const char *mustfirst
;
169 const char *mustlast
;
173 /* simplify the situation where possible */
174 if (g
->cflags
®_NOSUB
)
176 if (eflags
®_STARTEND
) {
177 start
= string
+ pmatch
[0].rm_so
;
178 stop
= string
+ pmatch
[0].rm_eo
;
181 stop
= start
+ strlen(start
);
186 /* prescreening; this does wonders for this rather slow code */
187 if (g
->must
!= NULL
) {
188 if (g
->charjump
!= NULL
&& g
->matchjump
!= NULL
) {
190 mustlast
= g
->must
+ g
->mlen
- 1;
191 charjump
= g
->charjump
;
192 matchjump
= g
->matchjump
;
194 for (dp
= start
+g
->mlen
-1; dp
< stop
;) {
195 /* Fast skip non-matches */
196 while (dp
< stop
&& charjump
[(int)*dp
])
197 dp
+= charjump
[(int)*dp
];
203 /* We depend on not being used for
204 * for strings of length 1
206 while (*--dp
== *--pp
&& pp
!= mustfirst
);
211 /* Jump to next possible match */
212 mj
= matchjump
[pp
- mustfirst
];
213 cj
= charjump
[(int)*dp
];
214 dp
+= (cj
< mj
? mj
: cj
);
220 for (dp
= start
; dp
< stop
; dp
++)
221 if (*dp
== g
->must
[0] &&
222 stop
- dp
>= g
->mlen
&&
223 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
225 if (dp
== stop
) /* we didn't find g->must */
230 /* match struct setup */
246 /* Adjust start according to moffset, to speed things up */
248 start
= ((dp
- g
->moffset
) < start
) ? start
: dp
- g
->moffset
;
250 /* this loop does only one repetition except for backrefs */
252 endp
= fast(m
, start
, stop
, gf
, gl
);
253 if (endp
== NULL
) { /* a miss */
254 if (m
->pmatch
!= NULL
)
255 free((char *)m
->pmatch
);
256 if (m
->lastpos
!= NULL
)
257 free((char *)m
->lastpos
);
261 if (nmatch
== 0 && !g
->backrefs
)
262 break; /* no further info needed */
265 assert(m
->coldp
!= NULL
);
267 NOTE("finding start");
268 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
271 assert(m
->coldp
< m
->endp
);
272 m
->coldp
+= XMBRTOWC(NULL
, m
->coldp
,
273 m
->endp
- m
->coldp
, &m
->mbs
, 0);
275 if (nmatch
== 1 && !g
->backrefs
)
276 break; /* no further info needed */
278 /* oh my, he wants the subexpressions... */
279 if (m
->pmatch
== NULL
)
280 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
282 if (m
->pmatch
== NULL
) {
286 for (i
= 1; i
<= m
->g
->nsub
; i
++)
287 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
288 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
290 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
292 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
293 m
->lastpos
= malloc((g
->nplus
+1) *
294 sizeof(const char *));
295 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
300 NOTE("backref dissect");
301 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0, 0);
306 /* uh-oh... we couldn't find a subexpression-level match */
307 assert(g
->backrefs
); /* must be back references doing it */
308 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
310 if (dp
!= NULL
|| endp
<= m
->coldp
)
313 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
316 /* try it on a shorter possibility */
318 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
319 assert(m
->pmatch
[i
].rm_so
== -1);
320 assert(m
->pmatch
[i
].rm_eo
== -1);
323 NOTE("backoff dissect");
324 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0, 0);
326 assert(dp
== NULL
|| dp
== endp
);
327 if (dp
!= NULL
) /* found a shorter one */
330 /* despite initial appearances, there is no match here */
332 /* recycle starting later */
333 start
= m
->coldp
+ XMBRTOWC(NULL
, m
->coldp
,
334 stop
- m
->coldp
, &m
->mbs
, 0);
335 assert(start
<= stop
);
338 /* fill in the details if requested */
340 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
341 pmatch
[0].rm_eo
= endp
- m
->offp
;
344 assert(m
->pmatch
!= NULL
);
345 for (i
= 1; i
< nmatch
; i
++)
347 pmatch
[i
] = m
->pmatch
[i
];
349 pmatch
[i
].rm_so
= -1;
350 pmatch
[i
].rm_eo
= -1;
354 if (m
->pmatch
!= NULL
)
355 free((char *)m
->pmatch
);
356 if (m
->lastpos
!= NULL
)
357 free((char *)m
->lastpos
);
363 - dissect - figure out what matched what, no back references
364 == static const char *dissect(struct match *m, const char *start, \
365 == const char *stop, sopno startst, sopno stopst);
367 static const char * /* == stop (success) always */
368 dissect(struct match
*m
,
375 sopno ss
; /* start sop of current subRE */
376 sopno es
; /* end sop of current subRE */
377 const char *sp
; /* start of string matched by it */
378 const char *stp
; /* string matched by it cannot pass here */
379 const char *rest
; /* start of rest of string */
380 const char *tail
; /* string unmatched by rest of RE */
381 sopno ssub
; /* start sop of subsubRE */
382 sopno esub
; /* end sop of subsubRE */
383 const char *ssp
; /* start of string matched by subsubRE */
384 const char *sep
; /* end of string matched by subsubRE */
385 const char *oldssp
; /* previous ssp */
388 AT("diss", start
, stop
, startst
, stopst
);
390 for (ss
= startst
; ss
< stopst
; ss
= es
) {
391 /* identify end of subRE */
393 switch (OP(m
->g
->strip
[es
])) {
396 es
+= OPND(m
->g
->strip
[es
]);
399 while (OP(m
->g
->strip
[es
]) != O_CH
)
400 es
+= OPND(m
->g
->strip
[es
]);
405 /* figure out what it matched */
406 switch (OP(m
->g
->strip
[ss
])) {
411 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0);
420 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0);
426 /* cases where length of match is hard to find */
430 /* how long could this one be? */
431 rest
= slow(m
, sp
, stp
, ss
, es
);
432 assert(rest
!= NULL
); /* it did match */
433 /* could the rest match the rest? */
434 tail
= slow(m
, rest
, stop
, es
, stopst
);
437 /* no -- try a shorter match for this one */
439 assert(stp
>= sp
); /* it did work */
443 /* did innards match? */
444 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
445 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
454 /* how long could this one be? */
455 rest
= slow(m
, sp
, stp
, ss
, es
);
456 assert(rest
!= NULL
); /* it did match */
457 /* could the rest match the rest? */
458 tail
= slow(m
, rest
, stop
, es
, stopst
);
461 /* no -- try a shorter match for this one */
463 assert(stp
>= sp
); /* it did work */
469 for (;;) { /* find last match of innards */
470 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
471 if (sep
== NULL
|| sep
== ssp
)
472 break; /* failed or matched null */
473 oldssp
= ssp
; /* on to next try */
477 /* last successful match */
481 assert(sep
== rest
); /* must exhaust substring */
482 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
483 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
490 /* how long could this one be? */
491 rest
= slow(m
, sp
, stp
, ss
, es
);
492 assert(rest
!= NULL
); /* it did match */
493 /* could the rest match the rest? */
494 tail
= slow(m
, rest
, stop
, es
, stopst
);
497 /* no -- try a shorter match for this one */
499 assert(stp
>= sp
); /* it did work */
502 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
503 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
504 for (;;) { /* find first matching branch */
505 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
506 break; /* it matched all of it */
507 /* that one missed, try next one */
508 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
510 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
512 esub
+= OPND(m
->g
->strip
[esub
]);
513 if (OP(m
->g
->strip
[esub
]) == OOR2
)
516 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
518 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
530 i
= OPND(m
->g
->strip
[ss
]);
531 assert(0 < i
&& i
<= m
->g
->nsub
);
532 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
535 i
= OPND(m
->g
->strip
[ss
]);
536 assert(0 < i
&& i
<= m
->g
->nsub
);
537 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
550 - backref - figure out what matched what, figuring in back references
551 == static const char *backref(struct match *m, const char *start, \
552 == const char *stop, sopno startst, sopno stopst, sopno lev);
554 static const char * /* == stop (success) or NULL (failure) */
555 backref(struct match
*m
,
560 sopno lev
, /* PLUS nesting level */
564 sopno ss
; /* start sop of current subRE */
565 const char *sp
; /* start of string matched by it */
566 sopno ssub
; /* start sop of subsubRE */
567 sopno esub
; /* end sop of subsubRE */
568 const char *ssp
; /* start of string matched by subsubRE */
577 AT("back", start
, stop
, startst
, stopst
);
580 /* get as far as we can with easy stuff */
582 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
583 switch (OP(s
= m
->g
->strip
[ss
])) {
587 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
);
594 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
);
601 cs
= &m
->g
->sets
[OPND(s
)];
602 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
);
603 if (wc
== BADCHAR
|| !CHIN(cs
, wc
))
607 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
608 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
609 (m
->g
->cflags
®_NEWLINE
)) )
615 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
616 (sp
< m
->endp
&& *sp
== '\n' &&
617 (m
->g
->cflags
®_NEWLINE
)) )
623 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
624 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
625 (m
->g
->cflags
®_NEWLINE
)) ||
627 !ISWORD(*(sp
-1))) ) &&
628 (sp
< m
->endp
&& ISWORD(*sp
)) )
634 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
635 (sp
< m
->endp
&& *sp
== '\n' &&
636 (m
->g
->cflags
®_NEWLINE
)) ||
637 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
638 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
645 case OOR1
: /* matches null but needs to skip */
649 assert(OP(s
) == OOR2
);
651 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
652 /* note that the ss++ gets us past the O_CH */
654 default: /* have to make a choice */
658 if (!hard
) { /* that was it! */
663 ss
--; /* adjust for the for's final increment */
666 AT("hard", sp
, stop
, ss
, stopst
);
669 case OBACK_
: /* the vilest depths */
671 assert(0 < i
&& i
<= m
->g
->nsub
);
672 if (m
->pmatch
[i
].rm_eo
== -1)
674 assert(m
->pmatch
[i
].rm_so
!= -1);
675 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
676 if (len
== 0 && rec
++ > MAX_RECURSION
)
678 assert(stop
- m
->beginp
>= len
);
680 return(NULL
); /* not enough left to match */
681 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
682 if (memcmp(sp
, ssp
, len
) != 0)
684 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
686 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
, rec
));
688 case OQUEST_
: /* to null or not */
689 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
691 return(dp
); /* not */
692 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
, rec
));
695 assert(m
->lastpos
!= NULL
);
696 assert(lev
+1 <= m
->g
->nplus
);
697 m
->lastpos
[lev
+1] = sp
;
698 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1, rec
));
701 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
702 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1, rec
));
703 /* try another pass */
704 m
->lastpos
[lev
] = sp
;
705 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
, rec
);
707 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1, rec
));
711 case OCH_
: /* find the right one, if any */
713 esub
= ss
+ OPND(s
) - 1;
714 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
715 for (;;) { /* find first matching branch */
716 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
, rec
);
719 /* that one missed, try next one */
720 if (OP(m
->g
->strip
[esub
]) == O_CH
)
721 return(NULL
); /* there is none */
723 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
725 esub
+= OPND(m
->g
->strip
[esub
]);
726 if (OP(m
->g
->strip
[esub
]) == OOR2
)
729 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
732 case OLPAREN
: /* must undo assignment if rest fails */
734 assert(0 < i
&& i
<= m
->g
->nsub
);
735 offsave
= m
->pmatch
[i
].rm_so
;
736 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
737 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
740 m
->pmatch
[i
].rm_so
= offsave
;
743 case ORPAREN
: /* must undo assignment if rest fails */
745 assert(0 < i
&& i
<= m
->g
->nsub
);
746 offsave
= m
->pmatch
[i
].rm_eo
;
747 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
748 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
751 m
->pmatch
[i
].rm_eo
= offsave
;
762 return "shut up gcc";
766 - fast - step through the string at top speed
767 == static const char *fast(struct match *m, const char *start, \
768 == const char *stop, sopno startst, sopno stopst);
770 static const char * /* where tentative match ended, or NULL */
771 fast( struct match
*m
,
778 states fresh
= m
->fresh
;
780 const char *p
= start
;
782 wint_t lastc
; /* previous c */
785 const char *coldp
; /* last p after which no match was underway */
790 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
794 if (start
== m
->beginp
)
798 * XXX Wrong if the previous character was multi-byte.
799 * Newline never is (in encodings supported by FreeBSD),
800 * so this only breaks the ISWORD tests below.
802 c
= (uch
)*(start
- 1);
811 clen
= XMBRTOWC(&c
, p
, m
->endp
- p
, &m
->mbs
, BADCHAR
);
815 /* is there an EOL and/or BOL between lastc and c? */
818 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
819 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
823 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
824 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
825 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
830 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
834 /* how about a word boundary? */
835 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
836 (c
!= OUT
&& ISWORD(c
)) ) {
839 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
840 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
843 if (flagch
== BOW
|| flagch
== EOW
) {
844 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
849 if (ISSET(st
, stopst
) || p
== stop
|| clen
> stop
- p
)
850 break; /* NOTE BREAK OUT */
852 /* no, we must deal with this character */
856 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
858 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
862 assert(coldp
!= NULL
);
864 if (ISSET(st
, stopst
))
865 return(p
+XMBRTOWC(NULL
, p
, stop
- p
, &m
->mbs
, 0));
871 - slow - step through the string more deliberately
872 == static const char *slow(struct match *m, const char *start, \
873 == const char *stop, sopno startst, sopno stopst);
875 static const char * /* where it ended */
876 slow( struct match
*m
,
883 states empty
= m
->empty
;
885 const char *p
= start
;
887 wint_t lastc
; /* previous c */
890 const char *matchp
; /* last p at which a match ended */
893 AT("slow", start
, stop
, startst
, stopst
);
896 SP("sstart", st
, *p
);
897 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
899 if (start
== m
->beginp
)
903 * XXX Wrong if the previous character was multi-byte.
904 * Newline never is (in encodings supported by FreeBSD),
905 * so this only breaks the ISWORD tests below.
907 c
= (uch
)*(start
- 1);
916 clen
= XMBRTOWC(&c
, p
, m
->endp
- p
, &m
->mbs
, BADCHAR
);
918 /* is there an EOL and/or BOL between lastc and c? */
921 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
922 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
926 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
927 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
928 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
933 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
934 SP("sboleol", st
, c
);
937 /* how about a word boundary? */
938 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
939 (c
!= OUT
&& ISWORD(c
)) ) {
942 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
943 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
946 if (flagch
== BOW
|| flagch
== EOW
) {
947 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
948 SP("sboweow", st
, c
);
952 if (ISSET(st
, stopst
))
954 if (EQ(st
, empty
) || p
== stop
|| clen
> stop
- p
)
955 break; /* NOTE BREAK OUT */
957 /* no, we must deal with this character */
961 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
963 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
972 - step - map set of states reachable before char to set reachable after
973 == static states step(struct re_guts *g, sopno start, sopno stop, \
974 == states bef, int ch, states aft);
975 == #define BOL (OUT-1)
976 == #define EOL (BOL-1)
977 == #define BOLEOL (BOL-2)
978 == #define NOTHING (BOL-3)
979 == #define BOW (BOL-4)
980 == #define EOW (BOL-5)
981 == #define BADCHAR (BOL-6)
982 == #define NONCHAR(c) ((c) <= OUT)
985 step(struct re_guts
*g
,
986 sopno start
, /* start state within strip */
987 sopno stop
, /* state after stop state within strip */
988 states bef
, /* states reachable before */
989 wint_t ch
, /* character or NONCHAR code */
990 states aft
) /* states already known reachable after */
995 onestate here
; /* note, macros know this name */
999 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
1003 assert(pc
== stop
-1);
1006 /* only characters can match */
1007 assert(!NONCHAR(ch
) || ch
!= OPND(s
));
1012 if (ch
== BOL
|| ch
== BOLEOL
)
1016 if (ch
== EOL
|| ch
== BOLEOL
)
1032 cs
= &g
->sets
[OPND(s
)];
1033 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
1036 case OBACK_
: /* ignored here */
1040 case OPLUS_
: /* forward, this is just an empty */
1043 case O_PLUS
: /* both forward and back */
1045 i
= ISSETBACK(aft
, OPND(s
));
1046 BACK(aft
, aft
, OPND(s
));
1047 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
1048 /* oho, must reconsider loop body */
1053 case OQUEST_
: /* two branches, both forward */
1055 FWD(aft
, aft
, OPND(s
));
1057 case O_QUEST
: /* just an empty */
1060 case OLPAREN
: /* not significant here */
1064 case OCH_
: /* mark the first two branches */
1066 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1067 FWD(aft
, aft
, OPND(s
));
1069 case OOR1
: /* done a branch, find the O_CH */
1070 if (ISSTATEIN(aft
, here
)) {
1072 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
1074 assert(OP(s
) == OOR2
);
1075 FWD(aft
, aft
, look
);
1078 case OOR2
: /* propagate OCH_'s marking */
1080 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
1081 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1082 FWD(aft
, aft
, OPND(s
));
1085 case O_CH
: /* just empty */
1088 default: /* ooooops... */
1099 - print - print a set of states
1101 == static void print(struct match *m, const char *caption, states st, \
1102 == int ch, FILE *d);
1106 print(struct match
*m
,
1107 const char *caption
,
1112 struct re_guts
*g
= m
->g
;
1116 if (!(m
->eflags
®_TRACE
))
1119 fprintf(d
, "%s", caption
);
1121 fprintf(d
, " %s", pchar(ch
));
1122 for (i
= 0; i
< g
->nstates
; i
++)
1124 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1131 - at - print current situation
1133 == static void at(struct match *m, const char *title, const char *start, \
1134 == const char *stop, sopno startst, sopno stopst);
1138 at( struct match
*m
,
1145 if (!(m
->eflags
®_TRACE
))
1148 printf("%s %s-", title
, pchar(*start
));
1149 printf("%s ", pchar(*stop
));
1150 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1154 #define PCHARDONE /* never again */
1156 - pchar - make a character printable
1158 == static const char *pchar(int ch);
1161 * Is this identical to regchar() over in debug.c? Well, yes. But a
1162 * duplicate here avoids having a debugging-capable regexec.o tied to
1163 * a matching debug.o, and this is convenient. It all disappears in
1164 * the non-debug compilation anyway, so it doesn't matter much.
1166 static const char * /* -> representation */
1169 static char pbuf
[10];
1171 if (isprint((uch
)ch
) || ch
== ' ')
1172 sprintf(pbuf
, "%c", ch
);
1174 sprintf(pbuf
, "\\%o", ch
);