2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)engine.c 8.5 (Berkeley) 3/20/94
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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 stepback sstepback
52 #define matcher smatcher
54 #define dissect sdissect
55 #define backref sbackref
62 #define stepback lstepback
63 #define matcher lmatcher
65 #define dissect ldissect
66 #define backref lbackref
73 #define stepback mstepback
74 #define matcher mmatcher
76 #define dissect mdissect
77 #define backref mbackref
84 /* another structure passed up and down to avoid zillions of parameters */
88 regmatch_t
*pmatch
; /* [nsub+1] (0 element unused) */
89 const char *offp
; /* offsets work from here */
90 const char *beginp
; /* start of string -- virtual NUL precedes */
91 const char *endp
; /* end of string -- virtual NUL here */
92 const char *coldp
; /* can be no match starting before here */
93 const char **lastpos
; /* [nplus+1] */
95 states st
; /* current states */
96 states fresh
; /* states for a fresh start */
97 states tmp
; /* temporary */
98 states empty
; /* empty set of states */
99 mbstate_t mbs
; /* multibyte conversion state */
102 /* ========= begin header generated by ./mkh ========= */
107 /* === engine.c === */
108 static int matcher(struct re_guts
*g
, const char *string
, size_t nmatch
, regmatch_t pmatch
[], int eflags
);
109 static const char *dissect(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
110 static const char *backref(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
, sopno lev
, int);
111 static const char *walk(struct match
*m
, const char *start
, const char *stop
, sopno startst
, sopno stopst
, bool fast
);
112 static states
step(struct re_guts
*g
, sopno start
, sopno stop
, states bef
, wint_t ch
, states aft
, int sflags
);
113 #define MAX_RECURSION 100
116 #define BOLEOL (BOL-2)
117 #define NOTHING (BOL-3)
120 #define BADCHAR (BOL-6)
121 #define NWBND (BOL-7)
122 #define NONCHAR(c) ((int)(c) <= OUT)
128 static void print(struct match
*m
, const char *caption
, states st
, int ch
, FILE *d
);
131 static void at(struct match
*m
, const char *title
, const char *start
, const char *stop
, sopno startst
, sopno stopst
);
134 static const char *pchar(int ch
);
140 /* ========= end header generated by ./mkh ========= */
143 #define SP(t, s, c) print(m, t, s, c, stdout)
144 #define AT(t, p1, p2, s1, s2) at(m, t, p1, p2, s1, s2)
145 #define NOTE(str) { if (m->eflags®_TRACE) printf("=%s\n", (str)); }
147 #define SP(t, s, c) /* nothing */
148 #define AT(t, p1, p2, s1, s2) /* nothing */
149 #define NOTE(s) /* nothing */
153 * Given a multibyte string pointed to by start, step back nchar characters
154 * from current position pointed to by cur.
157 stepback(const char *start
, const char *cur
, int nchar
)
165 return ((cur
- nchar
) > start
? cur
- nchar
: NULL
);
168 for (wc
= nchar
; wc
> 0; wc
--) {
169 for (mbc
= 1; mbc
<= MB_CUR_MAX
; mbc
++) {
170 if ((ret
- mbc
) < start
)
172 memset(&mbs
, 0, sizeof(mbs
));
173 clen
= mbrtowi(NULL
, ret
- mbc
, mbc
, &mbs
);
174 if (clen
!= (size_t)-1 && clen
!= (size_t)-2)
177 if (mbc
> MB_CUR_MAX
)
186 - matcher - the actual matching engine
187 == static int matcher(struct re_guts *g, const char *string, \
188 == size_t nmatch, regmatch_t pmatch[], int eflags);
190 static int /* 0 success, REG_NOMATCH failure */
191 matcher(struct re_guts
*g
,
200 struct match
*m
= &mv
;
201 const char *dp
= NULL
;
202 const sopno gf
= g
->firststate
+1; /* +1 for OEND */
203 const sopno gl
= g
->laststate
;
206 /* Boyer-Moore algorithms variables */
209 const char *mustfirst
;
210 const char *mustlast
;
214 /* simplify the situation where possible */
215 if (g
->cflags
®_NOSUB
)
217 if (eflags
®_STARTEND
) {
218 start
= string
+ pmatch
[0].rm_so
;
219 stop
= string
+ pmatch
[0].rm_eo
;
222 stop
= start
+ strlen(start
);
227 /* prescreening; this does wonders for this rather slow code */
228 if (g
->must
!= NULL
) {
229 if (g
->charjump
!= NULL
&& g
->matchjump
!= NULL
) {
231 mustlast
= g
->must
+ g
->mlen
- 1;
232 charjump
= g
->charjump
;
233 matchjump
= g
->matchjump
;
235 for (dp
= start
+g
->mlen
-1; dp
< stop
;) {
236 /* Fast skip non-matches */
237 while (dp
< stop
&& charjump
[(int)*dp
])
238 dp
+= charjump
[(int)*dp
];
244 /* We depend on not being used for
245 * for strings of length 1
247 while (*--dp
== *--pp
&& pp
!= mustfirst
);
252 /* Jump to next possible match */
253 mj
= matchjump
[pp
- mustfirst
];
254 cj
= charjump
[(int)*dp
];
255 dp
+= (cj
< mj
? mj
: cj
);
261 for (dp
= start
; dp
< stop
; dp
++)
262 if (*dp
== g
->must
[0] &&
263 stop
- dp
>= g
->mlen
&&
264 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
266 if (dp
== stop
) /* we didn't find g->must */
271 /* match struct setup */
287 /* Adjust start according to moffset, to speed things up */
288 if (dp
!= NULL
&& g
->moffset
> -1) {
291 nstart
= stepback(start
, dp
, g
->moffset
);
296 SP("mloop", m
->st
, *start
);
298 /* this loop does only one repetition except for backrefs */
300 endp
= walk(m
, start
, stop
, gf
, gl
, true);
301 if (endp
== NULL
) { /* a miss */
302 if (m
->pmatch
!= NULL
)
303 free((char *)m
->pmatch
);
304 if (m
->lastpos
!= NULL
)
305 free((char *)m
->lastpos
);
309 if (nmatch
== 0 && !g
->backrefs
)
310 break; /* no further info needed */
313 assert(m
->coldp
!= NULL
);
315 NOTE("finding start");
316 endp
= walk(m
, m
->coldp
, stop
, gf
, gl
, false);
319 assert(m
->coldp
< m
->endp
);
320 m
->coldp
+= XMBRTOWC(NULL
, m
->coldp
,
321 m
->endp
- m
->coldp
, &m
->mbs
, 0);
323 if (nmatch
== 1 && !g
->backrefs
)
324 break; /* no further info needed */
326 /* oh my, he wants the subexpressions... */
327 if (m
->pmatch
== NULL
)
328 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
330 if (m
->pmatch
== NULL
) {
334 for (i
= 1; i
<= m
->g
->nsub
; i
++)
335 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
336 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
338 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
340 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
341 m
->lastpos
= malloc((g
->nplus
+1) *
342 sizeof(const char *));
343 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
348 NOTE("backref dissect");
349 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0, 0);
354 /* uh-oh... we couldn't find a subexpression-level match */
355 assert(g
->backrefs
); /* must be back references doing it */
356 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
358 if (dp
!= NULL
|| endp
<= m
->coldp
)
361 endp
= walk(m
, m
->coldp
, endp
-1, gf
, gl
, false);
364 /* try it on a shorter possibility */
366 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
367 assert(m
->pmatch
[i
].rm_so
== -1);
368 assert(m
->pmatch
[i
].rm_eo
== -1);
371 NOTE("backoff dissect");
372 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0, 0);
374 assert(dp
== NULL
|| dp
== endp
);
375 if (dp
!= NULL
) /* found a shorter one */
378 /* despite initial appearances, there is no match here */
380 /* recycle starting later */
381 start
= m
->coldp
+ XMBRTOWC(NULL
, m
->coldp
,
382 stop
- m
->coldp
, &m
->mbs
, 0);
383 assert(start
<= stop
);
386 /* fill in the details if requested */
388 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
389 pmatch
[0].rm_eo
= endp
- m
->offp
;
392 assert(m
->pmatch
!= NULL
);
393 for (i
= 1; i
< nmatch
; i
++)
395 pmatch
[i
] = m
->pmatch
[i
];
397 pmatch
[i
].rm_so
= -1;
398 pmatch
[i
].rm_eo
= -1;
402 if (m
->pmatch
!= NULL
)
403 free((char *)m
->pmatch
);
404 if (m
->lastpos
!= NULL
)
405 free((char *)m
->lastpos
);
411 - dissect - figure out what matched what, no back references
412 == static const char *dissect(struct match *m, const char *start, \
413 == const char *stop, sopno startst, sopno stopst);
415 static const char * /* == stop (success) always */
416 dissect(struct match
*m
,
423 sopno ss
; /* start sop of current subRE */
424 sopno es
; /* end sop of current subRE */
425 const char *sp
; /* start of string matched by it */
426 const char *stp
; /* string matched by it cannot pass here */
427 const char *rest
; /* start of rest of string */
428 const char *tail
; /* string unmatched by rest of RE */
429 sopno ssub
; /* start sop of subsubRE */
430 sopno esub
; /* end sop of subsubRE */
431 const char *ssp
; /* start of string matched by subsubRE */
432 const char *sep
; /* end of string matched by subsubRE */
433 const char *oldssp
; /* previous ssp */
434 const char *dp __unused
;
436 AT("diss", start
, stop
, startst
, stopst
);
438 for (ss
= startst
; ss
< stopst
; ss
= es
) {
439 /* identify end of subRE */
441 switch (OP(m
->g
->strip
[es
])) {
444 es
+= OPND(m
->g
->strip
[es
]);
447 while (OP(m
->g
->strip
[es
]) != (sop
)O_CH
)
448 es
+= OPND(m
->g
->strip
[es
]);
453 /* figure out what it matched */
454 switch (OP(m
->g
->strip
[ss
])) {
459 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0);
472 sp
+= XMBRTOWC(NULL
, sp
, stop
- start
, &m
->mbs
, 0);
478 /* cases where length of match is hard to find */
482 /* how long could this one be? */
483 rest
= walk(m
, sp
, stp
, ss
, es
, false);
484 assert(rest
!= NULL
); /* it did match */
485 /* could the rest match the rest? */
486 tail
= walk(m
, rest
, stop
, es
, stopst
, false);
489 /* no -- try a shorter match for this one */
491 assert(stp
>= sp
); /* it did work */
495 /* did innards match? */
496 if (walk(m
, sp
, rest
, ssub
, esub
, false) != NULL
) {
497 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
506 /* how long could this one be? */
507 rest
= walk(m
, sp
, stp
, ss
, es
, false);
508 assert(rest
!= NULL
); /* it did match */
509 /* could the rest match the rest? */
510 tail
= walk(m
, rest
, stop
, es
, stopst
, false);
513 /* no -- try a shorter match for this one */
515 assert(stp
>= sp
); /* it did work */
521 for (;;) { /* find last match of innards */
522 sep
= walk(m
, ssp
, rest
, ssub
, esub
, false);
523 if (sep
== NULL
|| sep
== ssp
)
524 break; /* failed or matched null */
525 oldssp
= ssp
; /* on to next try */
529 /* last successful match */
533 assert(sep
== rest
); /* must exhaust substring */
534 assert(walk(m
, ssp
, sep
, ssub
, esub
, false) == rest
);
535 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
542 /* how long could this one be? */
543 rest
= walk(m
, sp
, stp
, ss
, es
, false);
544 assert(rest
!= NULL
); /* it did match */
545 /* could the rest match the rest? */
546 tail
= walk(m
, rest
, stop
, es
, stopst
, false);
549 /* no -- try a shorter match for this one */
551 assert(stp
>= sp
); /* it did work */
554 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
555 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
556 for (;;) { /* find first matching branch */
557 if (walk(m
, sp
, rest
, ssub
, esub
, false) == rest
)
558 break; /* it matched all of it */
559 /* that one missed, try next one */
560 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
562 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
564 esub
+= OPND(m
->g
->strip
[esub
]);
565 if (OP(m
->g
->strip
[esub
]) == (sop
)OOR2
)
568 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
570 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
582 i
= OPND(m
->g
->strip
[ss
]);
583 assert(0 < i
&& i
<= m
->g
->nsub
);
584 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
587 i
= OPND(m
->g
->strip
[ss
]);
588 assert(0 < i
&& i
<= m
->g
->nsub
);
589 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
601 #define ISBOW(m, sp) \
602 (sp < m->endp && ISWORD(*sp) && \
603 ((sp == m->beginp && !(m->eflags®_NOTBOL)) || \
604 (sp > m->offp && !ISWORD(*(sp-1)))))
605 #define ISEOW(m, sp) \
606 (((sp == m->endp && !(m->eflags®_NOTEOL)) || \
607 (sp < m->endp && *sp == '\n' && \
608 (m->g->cflags®_NEWLINE)) || \
609 (sp < m->endp && !ISWORD(*sp)) ) && \
610 (sp > m->beginp && ISWORD(*(sp-1)))) \
613 - backref - figure out what matched what, figuring in back references
614 == static const char *backref(struct match *m, const char *start, \
615 == const char *stop, sopno startst, sopno stopst, sopno lev);
617 static const char * /* == stop (success) or NULL (failure) */
618 backref(struct match
*m
,
623 sopno lev
, /* PLUS nesting level */
627 sopno ss
; /* start sop of current subRE */
628 const char *sp
; /* start of string matched by it */
629 sopno ssub
; /* start sop of subsubRE */
630 sopno esub
; /* end sop of subsubRE */
631 const char *ssp
; /* start of string matched by subsubRE */
640 AT("back", start
, stop
, startst
, stopst
);
643 /* get as far as we can with easy stuff */
645 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
646 switch (OP(s
= m
->g
->strip
[ss
])) {
650 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
);
657 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
);
664 cs
= &m
->g
->sets
[OPND(s
)];
665 sp
+= XMBRTOWC(&wc
, sp
, stop
- sp
, &m
->mbs
, BADCHAR
);
666 if (wc
== BADCHAR
|| !CHIN(cs
, wc
))
670 if (sp
== m
->beginp
&& (m
->eflags
& REG_NOTBOL
) == 0)
676 if (sp
== m
->endp
&& (m
->eflags
& REG_NOTEOL
) == 0)
682 if ((sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
683 (sp
> m
->offp
&& sp
< m
->endp
&&
684 *(sp
-1) == '\n' && (m
->g
->cflags
®_NEWLINE
)))
690 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
691 (sp
< m
->endp
&& *sp
== '\n' &&
692 (m
->g
->cflags
®_NEWLINE
)) )
698 if (ISBOW(m
, sp
) || ISEOW(m
, sp
))
704 if (((sp
== m
->beginp
) && !ISWORD(*sp
)) ||
705 (sp
== m
->endp
&& !ISWORD(*(sp
- 1))))
706 { /* yes, beginning/end of subject */ }
707 else if (ISWORD(*(sp
- 1)) == ISWORD(*sp
))
708 { /* yes, beginning/end of subject */ }
726 case OOR1
: /* matches null but needs to skip */
730 assert(OP(s
) == OOR2
);
732 } while (OP(s
= m
->g
->strip
[ss
]) != (sop
)O_CH
);
733 /* note that the ss++ gets us past the O_CH */
735 default: /* have to make a choice */
739 if (!hard
) { /* that was it! */
744 ss
--; /* adjust for the for's final increment */
747 AT("hard", sp
, stop
, ss
, stopst
);
750 case OBACK_
: /* the vilest depths */
752 assert(0 < i
&& i
<= m
->g
->nsub
);
753 if (m
->pmatch
[i
].rm_eo
== -1)
755 assert(m
->pmatch
[i
].rm_so
!= -1);
756 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
757 if (len
== 0 && rec
++ > MAX_RECURSION
)
759 assert(stop
- m
->beginp
>= len
);
761 return(NULL
); /* not enough left to match */
762 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
763 if (memcmp(sp
, ssp
, len
) != 0)
765 while (m
->g
->strip
[ss
] != (sop
)SOP(O_BACK
, i
))
767 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
, rec
));
768 case OQUEST_
: /* to null or not */
769 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
771 return(dp
); /* not */
772 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
, rec
));
774 assert(m
->lastpos
!= NULL
);
775 assert(lev
+1 <= m
->g
->nplus
);
776 m
->lastpos
[lev
+1] = sp
;
777 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1, rec
));
779 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
780 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1, rec
));
781 /* try another pass */
782 m
->lastpos
[lev
] = sp
;
783 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
, rec
);
785 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1, rec
));
788 case OCH_
: /* find the right one, if any */
790 esub
= ss
+ OPND(s
) - 1;
791 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
792 for (;;) { /* find first matching branch */
793 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
, rec
);
796 /* that one missed, try next one */
797 if (OP(m
->g
->strip
[esub
]) == (sop
)O_CH
)
798 return(NULL
); /* there is none */
800 assert(OP(m
->g
->strip
[esub
]) == (sop
)OOR2
);
802 esub
+= OPND(m
->g
->strip
[esub
]);
803 if (OP(m
->g
->strip
[esub
]) == (sop
)OOR2
)
806 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
810 case OLPAREN
: /* must undo assignment if rest fails */
812 assert(0 < i
&& i
<= m
->g
->nsub
);
813 offsave
= m
->pmatch
[i
].rm_so
;
814 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
815 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
818 m
->pmatch
[i
].rm_so
= offsave
;
820 case ORPAREN
: /* must undo assignment if rest fails */
822 assert(0 < i
&& i
<= m
->g
->nsub
);
823 offsave
= m
->pmatch
[i
].rm_eo
;
824 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
825 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
, rec
);
828 m
->pmatch
[i
].rm_eo
= offsave
;
838 return "shut up gcc";
842 - walk - step through the string either quickly or slowly
843 == static const char *walk(struct match *m, const char *start, \
844 == const char *stop, sopno startst, sopno stopst, bool fast);
846 static const char * /* where it ended, or NULL */
847 walk(struct match
*m
, const char *start
, const char *stop
, sopno startst
,
848 sopno stopst
, bool fast
)
851 states fresh
= m
->fresh
;
852 states empty
= m
->empty
;
854 const char *p
= start
;
856 wint_t lastc
; /* previous c */
859 const char *matchp
; /* last p at which a match ended */
863 AT("slow", start
, stop
, startst
, stopst
);
866 SP("sstart", st
, *p
);
867 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
, sflags
);
871 if (start
== m
->offp
|| (start
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)))
875 * XXX Wrong if the previous character was multi-byte.
876 * Newline never is (in encodings supported by FreeBSD),
877 * so this only breaks the ISWORD tests below.
879 c
= (uch
)*(start
- 1);
889 clen
= XMBRTOWC(&c
, p
, m
->endp
- p
, &m
->mbs
, BADCHAR
);
891 if (fast
&& EQ(st
, fresh
))
894 /* is there an EOL and/or BOL between lastc and c? */
897 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
898 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
902 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
903 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
904 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
907 if (lastc
== OUT
&& (m
->eflags
& REG_NOTBOL
) == 0) {
909 /* Step one more for BOS. */
912 if (c
== OUT
&& (m
->eflags
& REG_NOTEOL
) == 0) {
914 /* Step one more for EOS. */
919 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
,
921 SP("sboleol", st
, c
);
924 /* how about a word boundary? */
925 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
926 (c
!= OUT
&& ISWORD(c
)) ) {
929 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
930 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
933 if (flagch
== BOW
|| flagch
== EOW
) {
934 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
, sflags
);
935 SP("sboweow", st
, c
);
937 if (lastc
!= OUT
&& c
!= OUT
&&
938 ISWORD(lastc
) == ISWORD(c
)) {
940 } else if ((lastc
== OUT
&& !ISWORD(c
)) ||
941 (c
== OUT
&& !ISWORD(lastc
))) {
944 if (flagch
== NWBND
) {
945 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
, sflags
);
950 if (ISSET(st
, stopst
)) {
956 if (EQ(st
, empty
) || p
== stop
|| clen
> (size_t)(stop
- p
))
957 break; /* NOTE BREAK OUT */
959 /* no, we must deal with this character */
966 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
, sflags
);
968 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
, sflags
),
974 assert(matchp
!= NULL
);
976 if (ISSET(st
, stopst
))
977 return (p
+ XMBRTOWC(NULL
, p
, stop
- p
, &m
->mbs
, 0));
985 - step - map set of states reachable before char to set reachable after
986 == static states step(struct re_guts *g, sopno start, sopno stop, \
987 == states bef, int ch, states aft);
988 == #define BOL (OUT-1)
989 == #define EOL (BOL-1)
990 == #define BOLEOL (BOL-2)
991 == #define NOTHING (BOL-3)
992 == #define BOW (BOL-4)
993 == #define EOW (BOL-5)
994 == #define BADCHAR (BOL-6)
995 == #define NONCHAR(c) ((c) <= OUT)
998 step(struct re_guts
*g
,
999 sopno start
, /* start state within strip */
1000 sopno stop
, /* state after stop state within strip */
1001 states bef
, /* states reachable before */
1002 wint_t ch
, /* character or NONCHAR code */
1003 states aft
, /* states already known reachable after */
1004 int sflags
) /* state flags */
1009 onestate here
; /* note, macros know this name */
1013 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
1017 assert(pc
== stop
-1);
1020 /* only characters can match */
1021 assert(!NONCHAR(ch
) || ch
!= OPND(s
));
1026 if ((ch
== BOL
|| ch
== BOLEOL
) && (sflags
& SBOS
) != 0)
1030 if ((ch
== EOL
|| ch
== BOLEOL
) && (sflags
& SEOS
) != 0)
1034 if (ch
== BOL
|| ch
== BOLEOL
)
1038 if (ch
== EOL
|| ch
== BOLEOL
)
1050 if (ch
== BOW
|| ch
== EOW
)
1062 cs
= &g
->sets
[OPND(s
)];
1063 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
1066 case OBACK_
: /* ignored here */
1070 case OPLUS_
: /* forward, this is just an empty */
1073 case O_PLUS
: /* both forward and back */
1075 i
= ISSETBACK(aft
, OPND(s
));
1076 BACK(aft
, aft
, OPND(s
));
1077 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
1078 /* oho, must reconsider loop body */
1083 case OQUEST_
: /* two branches, both forward */
1085 FWD(aft
, aft
, OPND(s
));
1087 case O_QUEST
: /* just an empty */
1090 case OLPAREN
: /* not significant here */
1094 case OCH_
: /* mark the first two branches */
1096 assert(OP(g
->strip
[pc
+OPND(s
)]) == (sop
)OOR2
);
1097 FWD(aft
, aft
, OPND(s
));
1099 case OOR1
: /* done a branch, find the O_CH */
1100 if (ISSTATEIN(aft
, here
)) {
1102 OP(s
= g
->strip
[pc
+look
]) != (sop
)O_CH
;
1104 assert(OP(s
) == (sop
)OOR2
);
1105 FWD(aft
, aft
, look
+ 1);
1108 case OOR2
: /* propagate OCH_'s marking */
1110 if (OP(g
->strip
[pc
+OPND(s
)]) != (sop
)O_CH
) {
1111 assert(OP(g
->strip
[pc
+OPND(s
)]) == (sop
)OOR2
);
1112 FWD(aft
, aft
, OPND(s
));
1115 case O_CH
: /* just empty */
1118 default: /* ooooops... */
1129 - print - print a set of states
1131 == static void print(struct match *m, const char *caption, states st, \
1132 == int ch, FILE *d);
1136 print(struct match
*m
,
1137 const char *caption
,
1142 struct re_guts
*g
= m
->g
;
1146 if (!(m
->eflags
®_TRACE
))
1149 fprintf(d
, "%s", caption
);
1151 fprintf(d
, " %s", pchar(ch
));
1152 for (i
= 0; i
< g
->nstates
; i
++)
1154 fprintf(d
, "%s%lu", (first
) ? "\t" : ", ", i
);
1161 - at - print current situation
1163 == static void at(struct match *m, const char *title, const char *start, \
1164 == const char *stop, sopno startst, sopno stopst);
1168 at( struct match
*m
,
1175 if (!(m
->eflags
®_TRACE
))
1178 printf("%s %s-", title
, pchar(*start
));
1179 printf("%s ", pchar(*stop
));
1180 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1184 #define PCHARDONE /* never again */
1186 - pchar - make a character printable
1188 == static const char *pchar(int ch);
1191 * Is this identical to regchar() over in debug.c? Well, yes. But a
1192 * duplicate here avoids having a debugging-capable regexec.o tied to
1193 * a matching debug.o, and this is convenient. It all disappears in
1194 * the non-debug compilation anyway, so it doesn't matter much.
1196 static const char * /* -> representation */
1199 static char pbuf
[10];
1201 if (isprint((uch
)ch
) || ch
== ' ')
1202 sprintf(pbuf
, "%c", ch
);
1204 sprintf(pbuf
, "\\%o", ch
);