]>
git.saurik.com Git - apple/libc.git/blob - regex/FreeBSD/engine.c
77592de177e563e02e22ea068bf01112b091fa54
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
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/regex/engine.c,v 1.13 2003/02/16 17:29:10 nectar 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(g
, string
, nmatch
, pmatch
, eflags
)
152 struct match
*m
= &mv
;
154 const sopno gf
= g
->firststate
+1; /* +1 for OEND */
155 const sopno gl
= g
->laststate
;
158 /* Boyer-Moore algorithms variables */
166 /* simplify the situation where possible */
167 if (g
->cflags
®_NOSUB
)
169 if (eflags
®_STARTEND
) {
170 start
= string
+ pmatch
[0].rm_so
;
171 stop
= string
+ pmatch
[0].rm_eo
;
174 stop
= start
+ strlen(start
);
179 /* prescreening; this does wonders for this rather slow code */
180 if (g
->must
!= NULL
) {
181 if (g
->charjump
!= NULL
&& g
->matchjump
!= NULL
) {
183 mustlast
= g
->must
+ g
->mlen
- 1;
184 charjump
= g
->charjump
;
185 matchjump
= g
->matchjump
;
187 for (dp
= start
+g
->mlen
-1; dp
< stop
;) {
188 /* Fast skip non-matches */
189 while (dp
< stop
&& charjump
[(int)*dp
])
190 dp
+= charjump
[(int)*dp
];
196 /* We depend on not being used for
197 * for strings of length 1
199 while (*--dp
== *--pp
&& pp
!= mustfirst
);
204 /* Jump to next possible match */
205 mj
= matchjump
[pp
- mustfirst
];
206 cj
= charjump
[(int)*dp
];
207 dp
+= (cj
< mj
? mj
: cj
);
213 for (dp
= start
; dp
< stop
; dp
++)
214 if (*dp
== g
->must
[0] &&
215 stop
- dp
>= g
->mlen
&&
216 memcmp(dp
, g
->must
, (size_t)g
->mlen
) == 0)
218 if (dp
== stop
) /* we didn't find g->must */
223 /* match struct setup */
238 /* Adjust start according to moffset, to speed things up */
240 start
= ((dp
- g
->moffset
) < start
) ? start
: dp
- g
->moffset
;
242 /* this loop does only one repetition except for backrefs */
244 endp
= fast(m
, start
, stop
, gf
, gl
);
245 if (endp
== NULL
) { /* a miss */
249 if (nmatch
== 0 && !g
->backrefs
)
250 break; /* no further info needed */
253 assert(m
->coldp
!= NULL
);
255 NOTE("finding start");
256 endp
= slow(m
, m
->coldp
, stop
, gf
, gl
);
259 assert(m
->coldp
< m
->endp
);
262 if (nmatch
== 1 && !g
->backrefs
)
263 break; /* no further info needed */
265 /* oh my, he wants the subexpressions... */
266 if (m
->pmatch
== NULL
)
267 m
->pmatch
= (regmatch_t
*)malloc((m
->g
->nsub
+ 1) *
269 if (m
->pmatch
== NULL
) {
273 for (i
= 1; i
<= m
->g
->nsub
; i
++)
274 m
->pmatch
[i
].rm_so
= m
->pmatch
[i
].rm_eo
= -1;
275 if (!g
->backrefs
&& !(m
->eflags
®_BACKR
)) {
277 dp
= dissect(m
, m
->coldp
, endp
, gf
, gl
);
279 if (g
->nplus
> 0 && m
->lastpos
== NULL
)
280 m
->lastpos
= (char **)malloc((g
->nplus
+1) *
282 if (g
->nplus
> 0 && m
->lastpos
== NULL
) {
287 NOTE("backref dissect");
288 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
293 /* uh-oh... we couldn't find a subexpression-level match */
294 assert(g
->backrefs
); /* must be back references doing it */
295 assert(g
->nplus
== 0 || m
->lastpos
!= NULL
);
297 if (dp
!= NULL
|| endp
<= m
->coldp
)
300 endp
= slow(m
, m
->coldp
, endp
-1, gf
, gl
);
303 /* try it on a shorter possibility */
305 for (i
= 1; i
<= m
->g
->nsub
; i
++) {
306 assert(m
->pmatch
[i
].rm_so
== -1);
307 assert(m
->pmatch
[i
].rm_eo
== -1);
310 NOTE("backoff dissect");
311 dp
= backref(m
, m
->coldp
, endp
, gf
, gl
, (sopno
)0);
313 assert(dp
== NULL
|| dp
== endp
);
314 if (dp
!= NULL
) /* found a shorter one */
317 /* despite initial appearances, there is no match here */
319 start
= m
->coldp
+ 1; /* recycle starting later */
320 assert(start
<= stop
);
323 /* fill in the details if requested */
325 pmatch
[0].rm_so
= m
->coldp
- m
->offp
;
326 pmatch
[0].rm_eo
= endp
- m
->offp
;
329 assert(m
->pmatch
!= NULL
);
330 for (i
= 1; i
< nmatch
; i
++)
332 pmatch
[i
] = m
->pmatch
[i
];
334 pmatch
[i
].rm_so
= -1;
335 pmatch
[i
].rm_eo
= -1;
339 if (m
->pmatch
!= NULL
)
340 free((char *)m
->pmatch
);
341 if (m
->lastpos
!= NULL
)
342 free((char *)m
->lastpos
);
348 - dissect - figure out what matched what, no back references
349 == static char *dissect(struct match *m, char *start, \
350 == char *stop, sopno startst, sopno stopst);
352 static char * /* == stop (success) always */
353 dissect(m
, start
, stop
, startst
, stopst
)
361 sopno ss
; /* start sop of current subRE */
362 sopno es
; /* end sop of current subRE */
363 char *sp
; /* start of string matched by it */
364 char *stp
; /* string matched by it cannot pass here */
365 char *rest
; /* start of rest of string */
366 char *tail
; /* string unmatched by rest of RE */
367 sopno ssub
; /* start sop of subsubRE */
368 sopno esub
; /* end sop of subsubRE */
369 char *ssp
; /* start of string matched by subsubRE */
370 char *sep
; /* end of string matched by subsubRE */
371 char *oldssp
; /* previous ssp */
374 AT("diss", start
, stop
, startst
, stopst
);
376 for (ss
= startst
; ss
< stopst
; ss
= es
) {
377 /* identify end of subRE */
379 switch (OP(m
->g
->strip
[es
])) {
382 es
+= OPND(m
->g
->strip
[es
]);
385 while (OP(m
->g
->strip
[es
]) != O_CH
)
386 es
+= OPND(m
->g
->strip
[es
]);
391 /* figure out what it matched */
392 switch (OP(m
->g
->strip
[ss
])) {
412 /* cases where length of match is hard to find */
416 /* how long could this one be? */
417 rest
= slow(m
, sp
, stp
, ss
, es
);
418 assert(rest
!= NULL
); /* it did match */
419 /* could the rest match the rest? */
420 tail
= slow(m
, rest
, stop
, es
, stopst
);
423 /* no -- try a shorter match for this one */
425 assert(stp
>= sp
); /* it did work */
429 /* did innards match? */
430 if (slow(m
, sp
, rest
, ssub
, esub
) != NULL
) {
431 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
440 /* how long could this one be? */
441 rest
= slow(m
, sp
, stp
, ss
, es
);
442 assert(rest
!= NULL
); /* it did match */
443 /* could the rest match the rest? */
444 tail
= slow(m
, rest
, stop
, es
, stopst
);
447 /* no -- try a shorter match for this one */
449 assert(stp
>= sp
); /* it did work */
455 for (;;) { /* find last match of innards */
456 sep
= slow(m
, ssp
, rest
, ssub
, esub
);
457 if (sep
== NULL
|| sep
== ssp
)
458 break; /* failed or matched null */
459 oldssp
= ssp
; /* on to next try */
463 /* last successful match */
467 assert(sep
== rest
); /* must exhaust substring */
468 assert(slow(m
, ssp
, sep
, ssub
, esub
) == rest
);
469 dp
= dissect(m
, ssp
, sep
, ssub
, esub
);
476 /* how long could this one be? */
477 rest
= slow(m
, sp
, stp
, ss
, es
);
478 assert(rest
!= NULL
); /* it did match */
479 /* could the rest match the rest? */
480 tail
= slow(m
, rest
, stop
, es
, stopst
);
483 /* no -- try a shorter match for this one */
485 assert(stp
>= sp
); /* it did work */
488 esub
= ss
+ OPND(m
->g
->strip
[ss
]) - 1;
489 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
490 for (;;) { /* find first matching branch */
491 if (slow(m
, sp
, rest
, ssub
, esub
) == rest
)
492 break; /* it matched all of it */
493 /* that one missed, try next one */
494 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
496 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
498 esub
+= OPND(m
->g
->strip
[esub
]);
499 if (OP(m
->g
->strip
[esub
]) == OOR2
)
502 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
504 dp
= dissect(m
, sp
, rest
, ssub
, esub
);
516 i
= OPND(m
->g
->strip
[ss
]);
517 assert(0 < i
&& i
<= m
->g
->nsub
);
518 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
521 i
= OPND(m
->g
->strip
[ss
]);
522 assert(0 < i
&& i
<= m
->g
->nsub
);
523 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
536 - backref - figure out what matched what, figuring in back references
537 == static char *backref(struct match *m, char *start, \
538 == char *stop, sopno startst, sopno stopst, sopno lev);
540 static char * /* == stop (success) or NULL (failure) */
541 backref(m
, start
, stop
, startst
, stopst
, lev
)
547 sopno lev
; /* PLUS nesting level */
550 sopno ss
; /* start sop of current subRE */
551 char *sp
; /* start of string matched by it */
552 sopno ssub
; /* start sop of subsubRE */
553 sopno esub
; /* end sop of subsubRE */
554 char *ssp
; /* start of string matched by subsubRE */
562 AT("back", start
, stop
, startst
, stopst
);
565 /* get as far as we can with easy stuff */
567 for (ss
= startst
; !hard
&& ss
< stopst
; ss
++)
568 switch (OP(s
= m
->g
->strip
[ss
])) {
570 if (sp
== stop
|| *sp
++ != (char)OPND(s
))
579 cs
= &m
->g
->sets
[OPND(s
)];
580 if (sp
== stop
|| !CHIN(cs
, *sp
++))
584 if ( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
585 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
586 (m
->g
->cflags
®_NEWLINE
)) )
592 if ( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
593 (sp
< m
->endp
&& *sp
== '\n' &&
594 (m
->g
->cflags
®_NEWLINE
)) )
600 if (( (sp
== m
->beginp
&& !(m
->eflags
®_NOTBOL
)) ||
601 (sp
< m
->endp
&& *(sp
-1) == '\n' &&
602 (m
->g
->cflags
®_NEWLINE
)) ||
604 !ISWORD(*(sp
-1))) ) &&
605 (sp
< m
->endp
&& ISWORD(*sp
)) )
611 if (( (sp
== m
->endp
&& !(m
->eflags
®_NOTEOL
)) ||
612 (sp
< m
->endp
&& *sp
== '\n' &&
613 (m
->g
->cflags
®_NEWLINE
)) ||
614 (sp
< m
->endp
&& !ISWORD(*sp
)) ) &&
615 (sp
> m
->beginp
&& ISWORD(*(sp
-1))) )
622 case OOR1
: /* matches null but needs to skip */
626 assert(OP(s
) == OOR2
);
628 } while (OP(s
= m
->g
->strip
[ss
]) != O_CH
);
629 /* note that the ss++ gets us past the O_CH */
631 default: /* have to make a choice */
635 if (!hard
) { /* that was it! */
640 ss
--; /* adjust for the for's final increment */
643 AT("hard", sp
, stop
, ss
, stopst
);
646 case OBACK_
: /* the vilest depths */
648 assert(0 < i
&& i
<= m
->g
->nsub
);
649 if (m
->pmatch
[i
].rm_eo
== -1)
651 assert(m
->pmatch
[i
].rm_so
!= -1);
652 len
= m
->pmatch
[i
].rm_eo
- m
->pmatch
[i
].rm_so
;
653 assert(stop
- m
->beginp
>= len
);
655 return(NULL
); /* not enough left to match */
656 ssp
= m
->offp
+ m
->pmatch
[i
].rm_so
;
657 if (memcmp(sp
, ssp
, len
) != 0)
659 while (m
->g
->strip
[ss
] != SOP(O_BACK
, i
))
661 return(backref(m
, sp
+len
, stop
, ss
+1, stopst
, lev
));
663 case OQUEST_
: /* to null or not */
664 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
666 return(dp
); /* not */
667 return(backref(m
, sp
, stop
, ss
+OPND(s
)+1, stopst
, lev
));
670 assert(m
->lastpos
!= NULL
);
671 assert(lev
+1 <= m
->g
->nplus
);
672 m
->lastpos
[lev
+1] = sp
;
673 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
+1));
676 if (sp
== m
->lastpos
[lev
]) /* last pass matched null */
677 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
678 /* try another pass */
679 m
->lastpos
[lev
] = sp
;
680 dp
= backref(m
, sp
, stop
, ss
-OPND(s
)+1, stopst
, lev
);
682 return(backref(m
, sp
, stop
, ss
+1, stopst
, lev
-1));
686 case OCH_
: /* find the right one, if any */
688 esub
= ss
+ OPND(s
) - 1;
689 assert(OP(m
->g
->strip
[esub
]) == OOR1
);
690 for (;;) { /* find first matching branch */
691 dp
= backref(m
, sp
, stop
, ssub
, esub
, lev
);
694 /* that one missed, try next one */
695 if (OP(m
->g
->strip
[esub
]) == O_CH
)
696 return(NULL
); /* there is none */
698 assert(OP(m
->g
->strip
[esub
]) == OOR2
);
700 esub
+= OPND(m
->g
->strip
[esub
]);
701 if (OP(m
->g
->strip
[esub
]) == OOR2
)
704 assert(OP(m
->g
->strip
[esub
]) == O_CH
);
707 case OLPAREN
: /* must undo assignment if rest fails */
709 assert(0 < i
&& i
<= m
->g
->nsub
);
710 offsave
= m
->pmatch
[i
].rm_so
;
711 m
->pmatch
[i
].rm_so
= sp
- m
->offp
;
712 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
715 m
->pmatch
[i
].rm_so
= offsave
;
718 case ORPAREN
: /* must undo assignment if rest fails */
720 assert(0 < i
&& i
<= m
->g
->nsub
);
721 offsave
= m
->pmatch
[i
].rm_eo
;
722 m
->pmatch
[i
].rm_eo
= sp
- m
->offp
;
723 dp
= backref(m
, sp
, stop
, ss
+1, stopst
, lev
);
726 m
->pmatch
[i
].rm_eo
= offsave
;
737 return "shut up gcc";
741 - fast - step through the string at top speed
742 == static char *fast(struct match *m, char *start, \
743 == char *stop, sopno startst, sopno stopst);
745 static char * /* where tentative match ended, or NULL */
746 fast(m
, start
, stop
, startst
, stopst
)
754 states fresh
= m
->fresh
;
757 int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
758 int lastc
; /* previous c */
761 char *coldp
; /* last p after which no match was underway */
765 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
772 c
= (p
== m
->endp
) ? OUT
: *p
;
776 /* is there an EOL and/or BOL between lastc and c? */
779 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
780 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
784 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
785 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
786 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
791 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
795 /* how about a word boundary? */
796 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
797 (c
!= OUT
&& ISWORD(c
)) ) {
800 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
801 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
804 if (flagch
== BOW
|| flagch
== EOW
) {
805 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
810 if (ISSET(st
, stopst
) || p
== stop
)
811 break; /* NOTE BREAK OUT */
813 /* no, we must deal with this character */
817 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
819 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
823 assert(coldp
!= NULL
);
825 if (ISSET(st
, stopst
))
832 - slow - step through the string more deliberately
833 == static char *slow(struct match *m, char *start, \
834 == char *stop, sopno startst, sopno stopst);
836 static char * /* where it ended */
837 slow(m
, start
, stop
, startst
, stopst
)
845 states empty
= m
->empty
;
848 int c
= (start
== m
->beginp
) ? OUT
: *(start
-1);
849 int lastc
; /* previous c */
852 char *matchp
; /* last p at which a match ended */
854 AT("slow", start
, stop
, startst
, stopst
);
857 SP("sstart", st
, *p
);
858 st
= step(m
->g
, startst
, stopst
, st
, NOTHING
, st
);
863 c
= (p
== m
->endp
) ? OUT
: *p
;
865 /* is there an EOL and/or BOL between lastc and c? */
868 if ( (lastc
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
869 (lastc
== OUT
&& !(m
->eflags
®_NOTBOL
)) ) {
873 if ( (c
== '\n' && m
->g
->cflags
®_NEWLINE
) ||
874 (c
== OUT
&& !(m
->eflags
®_NOTEOL
)) ) {
875 flagch
= (flagch
== BOL
) ? BOLEOL
: EOL
;
880 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
881 SP("sboleol", st
, c
);
884 /* how about a word boundary? */
885 if ( (flagch
== BOL
|| (lastc
!= OUT
&& !ISWORD(lastc
))) &&
886 (c
!= OUT
&& ISWORD(c
)) ) {
889 if ( (lastc
!= OUT
&& ISWORD(lastc
)) &&
890 (flagch
== EOL
|| (c
!= OUT
&& !ISWORD(c
))) ) {
893 if (flagch
== BOW
|| flagch
== EOW
) {
894 st
= step(m
->g
, startst
, stopst
, st
, flagch
, st
);
895 SP("sboweow", st
, c
);
899 if (ISSET(st
, stopst
))
901 if (EQ(st
, empty
) || p
== stop
)
902 break; /* NOTE BREAK OUT */
904 /* no, we must deal with this character */
908 st
= step(m
->g
, startst
, stopst
, tmp
, c
, st
);
910 assert(EQ(step(m
->g
, startst
, stopst
, st
, NOTHING
, st
), st
));
919 - step - map set of states reachable before char to set reachable after
920 == static states step(struct re_guts *g, sopno start, sopno stop, \
921 == states bef, int ch, states aft);
922 == #define BOL (OUT+1)
923 == #define EOL (BOL+1)
924 == #define BOLEOL (BOL+2)
925 == #define NOTHING (BOL+3)
926 == #define BOW (BOL+4)
927 == #define EOW (BOL+5)
928 == #define CODEMAX (BOL+5) // highest code used
929 == #define NONCHAR(c) ((c) > CHAR_MAX)
930 == #define NNONCHAR (CODEMAX-CHAR_MAX)
933 step(g
, start
, stop
, bef
, ch
, aft
)
935 sopno start
; /* start state within strip */
936 sopno stop
; /* state after stop state within strip */
937 states bef
; /* states reachable before */
938 int ch
; /* character or NONCHAR code */
939 states aft
; /* states already known reachable after */
944 onestate here
; /* note, macros know this name */
948 for (pc
= start
, INIT(here
, pc
); pc
!= stop
; pc
++, INC(here
)) {
952 assert(pc
== stop
-1);
955 /* only characters can match */
956 assert(!NONCHAR(ch
) || ch
!= (char)OPND(s
));
957 if (ch
== (char)OPND(s
))
961 if (ch
== BOL
|| ch
== BOLEOL
)
965 if (ch
== EOL
|| ch
== BOLEOL
)
981 cs
= &g
->sets
[OPND(s
)];
982 if (!NONCHAR(ch
) && CHIN(cs
, ch
))
985 case OBACK_
: /* ignored here */
989 case OPLUS_
: /* forward, this is just an empty */
992 case O_PLUS
: /* both forward and back */
994 i
= ISSETBACK(aft
, OPND(s
));
995 BACK(aft
, aft
, OPND(s
));
996 if (!i
&& ISSETBACK(aft
, OPND(s
))) {
997 /* oho, must reconsider loop body */
1002 case OQUEST_
: /* two branches, both forward */
1004 FWD(aft
, aft
, OPND(s
));
1006 case O_QUEST
: /* just an empty */
1009 case OLPAREN
: /* not significant here */
1013 case OCH_
: /* mark the first two branches */
1015 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1016 FWD(aft
, aft
, OPND(s
));
1018 case OOR1
: /* done a branch, find the O_CH */
1019 if (ISSTATEIN(aft
, here
)) {
1021 OP(s
= g
->strip
[pc
+look
]) != O_CH
;
1023 assert(OP(s
) == OOR2
);
1024 FWD(aft
, aft
, look
);
1027 case OOR2
: /* propagate OCH_'s marking */
1029 if (OP(g
->strip
[pc
+OPND(s
)]) != O_CH
) {
1030 assert(OP(g
->strip
[pc
+OPND(s
)]) == OOR2
);
1031 FWD(aft
, aft
, OPND(s
));
1034 case O_CH
: /* just empty */
1037 default: /* ooooops... */
1048 - print - print a set of states
1050 == static void print(struct match *m, char *caption, states st, \
1051 == int ch, FILE *d);
1055 print(m
, caption
, st
, ch
, d
)
1062 struct re_guts
*g
= m
->g
;
1066 if (!(m
->eflags
®_TRACE
))
1069 fprintf(d
, "%s", caption
);
1071 fprintf(d
, " %s", pchar(ch
));
1072 for (i
= 0; i
< g
->nstates
; i
++)
1074 fprintf(d
, "%s%d", (first
) ? "\t" : ", ", i
);
1081 - at - print current situation
1083 == static void at(struct match *m, char *title, char *start, char *stop, \
1084 == sopno startst, sopno stopst);
1088 at(m
, title
, start
, stop
, startst
, stopst
)
1096 if (!(m
->eflags
®_TRACE
))
1099 printf("%s %s-", title
, pchar(*start
));
1100 printf("%s ", pchar(*stop
));
1101 printf("%ld-%ld\n", (long)startst
, (long)stopst
);
1105 #define PCHARDONE /* never again */
1107 - pchar - make a character printable
1109 == static char *pchar(int ch);
1112 * Is this identical to regchar() over in debug.c? Well, yes. But a
1113 * duplicate here avoids having a debugging-capable regexec.o tied to
1114 * a matching debug.o, and this is convenient. It all disappears in
1115 * the non-debug compilation anyway, so it doesn't matter much.
1117 static char * /* -> representation */
1121 static char pbuf
[10];
1123 if (isprint((uch
)ch
) || ch
== ' ')
1124 sprintf(pbuf
, "%c", ch
);
1126 sprintf(pbuf
, "\\%o", ch
);