xref: /freebsd/lib/libc/regex/engine.c (revision 6049d9f0ebdd55b8018e51d20fd42c899c5bcf54)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
358f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994
458f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
558f0484fSRodney W. Grimes  *
658f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
758f0484fSRodney W. Grimes  * Henry Spencer.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1858f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1958f0484fSRodney W. Grimes  *	This product includes software developed by the University of
2058f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  *
3758f0484fSRodney W. Grimes  *	@(#)engine.c	8.5 (Berkeley) 3/20/94
386049d9f0SDaniel C. Sobral  *
396049d9f0SDaniel C. Sobral  * $FreeBSD$
4058f0484fSRodney W. Grimes  */
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes /*
4358f0484fSRodney W. Grimes  * The matching engine and friends.  This file is #included by regexec.c
4458f0484fSRodney W. Grimes  * after suitable #defines of a variety of macros used herein, so that
4558f0484fSRodney W. Grimes  * different state representations can be used without duplicating masses
4658f0484fSRodney W. Grimes  * of code.
4758f0484fSRodney W. Grimes  */
4858f0484fSRodney W. Grimes 
4958f0484fSRodney W. Grimes #ifdef SNAMES
5058f0484fSRodney W. Grimes #define	matcher	smatcher
5158f0484fSRodney W. Grimes #define	fast	sfast
5258f0484fSRodney W. Grimes #define	slow	sslow
5358f0484fSRodney W. Grimes #define	dissect	sdissect
5458f0484fSRodney W. Grimes #define	backref	sbackref
5558f0484fSRodney W. Grimes #define	step	sstep
5658f0484fSRodney W. Grimes #define	print	sprint
5758f0484fSRodney W. Grimes #define	at	sat
5858f0484fSRodney W. Grimes #define	match	smat
5958f0484fSRodney W. Grimes #endif
6058f0484fSRodney W. Grimes #ifdef LNAMES
6158f0484fSRodney W. Grimes #define	matcher	lmatcher
6258f0484fSRodney W. Grimes #define	fast	lfast
6358f0484fSRodney W. Grimes #define	slow	lslow
6458f0484fSRodney W. Grimes #define	dissect	ldissect
6558f0484fSRodney W. Grimes #define	backref	lbackref
6658f0484fSRodney W. Grimes #define	step	lstep
6758f0484fSRodney W. Grimes #define	print	lprint
6858f0484fSRodney W. Grimes #define	at	lat
6958f0484fSRodney W. Grimes #define	match	lmat
7058f0484fSRodney W. Grimes #endif
7158f0484fSRodney W. Grimes 
7258f0484fSRodney W. Grimes /* another structure passed up and down to avoid zillions of parameters */
7358f0484fSRodney W. Grimes struct match {
7458f0484fSRodney W. Grimes 	struct re_guts *g;
7558f0484fSRodney W. Grimes 	int eflags;
7658f0484fSRodney W. Grimes 	regmatch_t *pmatch;	/* [nsub+1] (0 element unused) */
7758f0484fSRodney W. Grimes 	char *offp;		/* offsets work from here */
7858f0484fSRodney W. Grimes 	char *beginp;		/* start of string -- virtual NUL precedes */
7958f0484fSRodney W. Grimes 	char *endp;		/* end of string -- virtual NUL here */
8058f0484fSRodney W. Grimes 	char *coldp;		/* can be no match starting before here */
8158f0484fSRodney W. Grimes 	char **lastpos;		/* [nplus+1] */
8258f0484fSRodney W. Grimes 	STATEVARS;
8358f0484fSRodney W. Grimes 	states st;		/* current states */
8458f0484fSRodney W. Grimes 	states fresh;		/* states for a fresh start */
8558f0484fSRodney W. Grimes 	states tmp;		/* temporary */
8658f0484fSRodney W. Grimes 	states empty;		/* empty set of states */
8758f0484fSRodney W. Grimes };
8858f0484fSRodney W. Grimes 
8958f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */
9058f0484fSRodney W. Grimes #ifdef __cplusplus
9158f0484fSRodney W. Grimes extern "C" {
9258f0484fSRodney W. Grimes #endif
9358f0484fSRodney W. Grimes 
9458f0484fSRodney W. Grimes /* === engine.c === */
9558f0484fSRodney W. Grimes static int matcher __P((struct re_guts *g, char *string, size_t nmatch, regmatch_t pmatch[], int eflags));
9658f0484fSRodney W. Grimes static char *dissect __P((struct match *m, char *start, char *stop, sopno startst, sopno stopst));
9758f0484fSRodney W. Grimes static char *backref __P((struct match *m, char *start, char *stop, sopno startst, sopno stopst, sopno lev));
9858f0484fSRodney W. Grimes static char *fast __P((struct match *m, char *start, char *stop, sopno startst, sopno stopst));
9958f0484fSRodney W. Grimes static char *slow __P((struct match *m, char *start, char *stop, sopno startst, sopno stopst));
10058f0484fSRodney W. Grimes static states step __P((struct re_guts *g, sopno start, sopno stop, states bef, int ch, states aft));
10158f0484fSRodney W. Grimes #define	BOL	(OUT+1)
10258f0484fSRodney W. Grimes #define	EOL	(BOL+1)
10358f0484fSRodney W. Grimes #define	BOLEOL	(BOL+2)
10458f0484fSRodney W. Grimes #define	NOTHING	(BOL+3)
10558f0484fSRodney W. Grimes #define	BOW	(BOL+4)
10658f0484fSRodney W. Grimes #define	EOW	(BOL+5)
10758f0484fSRodney W. Grimes #define	CODEMAX	(BOL+5)		/* highest code used */
10858f0484fSRodney W. Grimes #define	NONCHAR(c)	((c) > CHAR_MAX)
10958f0484fSRodney W. Grimes #define	NNONCHAR	(CODEMAX-CHAR_MAX)
11058f0484fSRodney W. Grimes #ifdef REDEBUG
11158f0484fSRodney W. Grimes static void print __P((struct match *m, char *caption, states st, int ch, FILE *d));
11258f0484fSRodney W. Grimes #endif
11358f0484fSRodney W. Grimes #ifdef REDEBUG
11458f0484fSRodney W. Grimes static void at __P((struct match *m, char *title, char *start, char *stop, sopno startst, sopno stopst));
11558f0484fSRodney W. Grimes #endif
11658f0484fSRodney W. Grimes #ifdef REDEBUG
11758f0484fSRodney W. Grimes static char *pchar __P((int ch));
11858f0484fSRodney W. Grimes #endif
11958f0484fSRodney W. Grimes 
12058f0484fSRodney W. Grimes #ifdef __cplusplus
12158f0484fSRodney W. Grimes }
12258f0484fSRodney W. Grimes #endif
12358f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes #ifdef REDEBUG
12658f0484fSRodney W. Grimes #define	SP(t, s, c)	print(m, t, s, c, stdout)
12758f0484fSRodney W. Grimes #define	AT(t, p1, p2, s1, s2)	at(m, t, p1, p2, s1, s2)
12858f0484fSRodney W. Grimes #define	NOTE(str)	{ if (m->eflags&REG_TRACE) printf("=%s\n", (str)); }
12958f0484fSRodney W. Grimes #else
13058f0484fSRodney W. Grimes #define	SP(t, s, c)	/* nothing */
13158f0484fSRodney W. Grimes #define	AT(t, p1, p2, s1, s2)	/* nothing */
13258f0484fSRodney W. Grimes #define	NOTE(s)	/* nothing */
13358f0484fSRodney W. Grimes #endif
13458f0484fSRodney W. Grimes 
13558f0484fSRodney W. Grimes /*
13658f0484fSRodney W. Grimes  - matcher - the actual matching engine
13758f0484fSRodney W. Grimes  == static int matcher(register struct re_guts *g, char *string, \
13858f0484fSRodney W. Grimes  ==	size_t nmatch, regmatch_t pmatch[], int eflags);
13958f0484fSRodney W. Grimes  */
14058f0484fSRodney W. Grimes static int			/* 0 success, REG_NOMATCH failure */
14158f0484fSRodney W. Grimes matcher(g, string, nmatch, pmatch, eflags)
14258f0484fSRodney W. Grimes register struct re_guts *g;
14358f0484fSRodney W. Grimes char *string;
14458f0484fSRodney W. Grimes size_t nmatch;
14558f0484fSRodney W. Grimes regmatch_t pmatch[];
14658f0484fSRodney W. Grimes int eflags;
14758f0484fSRodney W. Grimes {
14858f0484fSRodney W. Grimes 	register char *endp;
14958f0484fSRodney W. Grimes 	register int i;
15058f0484fSRodney W. Grimes 	struct match mv;
15158f0484fSRodney W. Grimes 	register struct match *m = &mv;
15258f0484fSRodney W. Grimes 	register char *dp;
15351295a4dSJordan K. Hubbard 	register const sopno gf = g->firststate+1;	/* +1 for OEND */
15451295a4dSJordan K. Hubbard 	register const sopno gl = g->laststate;
15558f0484fSRodney W. Grimes 	char *start;
15658f0484fSRodney W. Grimes 	char *stop;
1576049d9f0SDaniel C. Sobral 	/* Boyer-Moore algorithms variables */
1586049d9f0SDaniel C. Sobral 	register unsigned char *pp;
1596049d9f0SDaniel C. Sobral 	int cj, mj;
1606049d9f0SDaniel C. Sobral 	register unsigned char *mustfirst;
1616049d9f0SDaniel C. Sobral 	register unsigned char *mustlast;
1626049d9f0SDaniel C. Sobral 	register int mustlen;
1636049d9f0SDaniel C. Sobral 	register int *matchjump;
1646049d9f0SDaniel C. Sobral 	register int *charjump;
1656049d9f0SDaniel C. Sobral 	register unsigned char *bmp;
1666049d9f0SDaniel C. Sobral 	register unsigned char *bmps;
16758f0484fSRodney W. Grimes 
16858f0484fSRodney W. Grimes 	/* simplify the situation where possible */
16958f0484fSRodney W. Grimes 	if (g->cflags&REG_NOSUB)
17058f0484fSRodney W. Grimes 		nmatch = 0;
17158f0484fSRodney W. Grimes 	if (eflags&REG_STARTEND) {
17258f0484fSRodney W. Grimes 		start = string + pmatch[0].rm_so;
17358f0484fSRodney W. Grimes 		stop = string + pmatch[0].rm_eo;
17458f0484fSRodney W. Grimes 	} else {
17558f0484fSRodney W. Grimes 		start = string;
17658f0484fSRodney W. Grimes 		stop = start + strlen(start);
17758f0484fSRodney W. Grimes 	}
17858f0484fSRodney W. Grimes 	if (stop < start)
17958f0484fSRodney W. Grimes 		return(REG_INVARG);
18058f0484fSRodney W. Grimes 
18158f0484fSRodney W. Grimes 	/* prescreening; this does wonders for this rather slow code */
18258f0484fSRodney W. Grimes 	if (g->must != NULL) {
1836049d9f0SDaniel C. Sobral 		if(g->charjump != NULL && g->matchjump != NULL) {
1846049d9f0SDaniel C. Sobral 			mustlen = -g->mlen;
1856049d9f0SDaniel C. Sobral 			mustfirst = g->must;
1866049d9f0SDaniel C. Sobral 			mustlast = g->must + g->mlen - 1;
1876049d9f0SDaniel C. Sobral 			charjump = g->charjump;
1886049d9f0SDaniel C. Sobral 			matchjump = g->matchjump;
1896049d9f0SDaniel C. Sobral 			bmps = stop;
1906049d9f0SDaniel C. Sobral 			pp = mustlast;
1916049d9f0SDaniel C. Sobral 			for(bmp = start+g->mlen-1; bmp < bmps;) {
1926049d9f0SDaniel C. Sobral 				/* Fast skip non-matches */
1936049d9f0SDaniel C. Sobral 				while(bmp < bmps && charjump[*bmp])
1946049d9f0SDaniel C. Sobral 					bmp += charjump[*bmp];
1956049d9f0SDaniel C. Sobral 
1966049d9f0SDaniel C. Sobral 				if(bmp >= bmps)
1976049d9f0SDaniel C. Sobral 					break;
1986049d9f0SDaniel C. Sobral 
1996049d9f0SDaniel C. Sobral 				/* Greedy matcher */
2006049d9f0SDaniel C. Sobral 				/* We depend on not being used for
2016049d9f0SDaniel C. Sobral 				 * for strings of length 1
2026049d9f0SDaniel C. Sobral 				 */
2036049d9f0SDaniel C. Sobral 				while(*--bmp == *--pp && pp != mustfirst);
2046049d9f0SDaniel C. Sobral 
2056049d9f0SDaniel C. Sobral 				if(*bmp == *pp)
2066049d9f0SDaniel C. Sobral 					break;
2076049d9f0SDaniel C. Sobral 
2086049d9f0SDaniel C. Sobral 				/* Jump to next possible match */
2096049d9f0SDaniel C. Sobral 				mj = matchjump[pp - mustfirst];
2106049d9f0SDaniel C. Sobral 				cj = charjump[*bmp];
2116049d9f0SDaniel C. Sobral 				bmp += (cj < mj ? mj : cj);
2126049d9f0SDaniel C. Sobral 				pp = mustlast;
2136049d9f0SDaniel C. Sobral 			}
2146049d9f0SDaniel C. Sobral 			if(pp != mustfirst)
2156049d9f0SDaniel C. Sobral 				return(REG_NOMATCH);
2166049d9f0SDaniel C. Sobral 		} else {
21758f0484fSRodney W. Grimes 			for (dp = start; dp < stop; dp++)
2186049d9f0SDaniel C. Sobral 				if (*dp == g->must[0] &&
2196049d9f0SDaniel C. Sobral 				    stop - dp >= g->mlen &&
22058f0484fSRodney W. Grimes 				    memcmp(dp, g->must, (size_t)g->mlen) == 0)
22158f0484fSRodney W. Grimes 					break;
22258f0484fSRodney W. Grimes 			if (dp == stop)		/* we didn't find g->must */
22358f0484fSRodney W. Grimes 				return(REG_NOMATCH);
22458f0484fSRodney W. Grimes 		}
2256049d9f0SDaniel C. Sobral 	}
22658f0484fSRodney W. Grimes 
22758f0484fSRodney W. Grimes 	/* match struct setup */
22858f0484fSRodney W. Grimes 	m->g = g;
22958f0484fSRodney W. Grimes 	m->eflags = eflags;
23058f0484fSRodney W. Grimes 	m->pmatch = NULL;
23158f0484fSRodney W. Grimes 	m->lastpos = NULL;
23258f0484fSRodney W. Grimes 	m->offp = string;
23358f0484fSRodney W. Grimes 	m->beginp = start;
23458f0484fSRodney W. Grimes 	m->endp = stop;
23558f0484fSRodney W. Grimes 	STATESETUP(m, 4);
23658f0484fSRodney W. Grimes 	SETUP(m->st);
23758f0484fSRodney W. Grimes 	SETUP(m->fresh);
23858f0484fSRodney W. Grimes 	SETUP(m->tmp);
23958f0484fSRodney W. Grimes 	SETUP(m->empty);
24058f0484fSRodney W. Grimes 	CLEAR(m->empty);
24158f0484fSRodney W. Grimes 
24258f0484fSRodney W. Grimes 	/* this loop does only one repetition except for backrefs */
24358f0484fSRodney W. Grimes 	for (;;) {
24458f0484fSRodney W. Grimes 		endp = fast(m, start, stop, gf, gl);
24558f0484fSRodney W. Grimes 		if (endp == NULL) {		/* a miss */
24658f0484fSRodney W. Grimes 			STATETEARDOWN(m);
24758f0484fSRodney W. Grimes 			return(REG_NOMATCH);
24858f0484fSRodney W. Grimes 		}
24958f0484fSRodney W. Grimes 		if (nmatch == 0 && !g->backrefs)
25058f0484fSRodney W. Grimes 			break;		/* no further info needed */
25158f0484fSRodney W. Grimes 
25258f0484fSRodney W. Grimes 		/* where? */
25358f0484fSRodney W. Grimes 		assert(m->coldp != NULL);
25458f0484fSRodney W. Grimes 		for (;;) {
25558f0484fSRodney W. Grimes 			NOTE("finding start");
25658f0484fSRodney W. Grimes 			endp = slow(m, m->coldp, stop, gf, gl);
25758f0484fSRodney W. Grimes 			if (endp != NULL)
25858f0484fSRodney W. Grimes 				break;
25958f0484fSRodney W. Grimes 			assert(m->coldp < m->endp);
26058f0484fSRodney W. Grimes 			m->coldp++;
26158f0484fSRodney W. Grimes 		}
26258f0484fSRodney W. Grimes 		if (nmatch == 1 && !g->backrefs)
26358f0484fSRodney W. Grimes 			break;		/* no further info needed */
26458f0484fSRodney W. Grimes 
26558f0484fSRodney W. Grimes 		/* oh my, he wants the subexpressions... */
26658f0484fSRodney W. Grimes 		if (m->pmatch == NULL)
26758f0484fSRodney W. Grimes 			m->pmatch = (regmatch_t *)malloc((m->g->nsub + 1) *
26858f0484fSRodney W. Grimes 							sizeof(regmatch_t));
26958f0484fSRodney W. Grimes 		if (m->pmatch == NULL) {
27058f0484fSRodney W. Grimes 			STATETEARDOWN(m);
27158f0484fSRodney W. Grimes 			return(REG_ESPACE);
27258f0484fSRodney W. Grimes 		}
27358f0484fSRodney W. Grimes 		for (i = 1; i <= m->g->nsub; i++)
27458f0484fSRodney W. Grimes 			m->pmatch[i].rm_so = m->pmatch[i].rm_eo = -1;
27558f0484fSRodney W. Grimes 		if (!g->backrefs && !(m->eflags&REG_BACKR)) {
27658f0484fSRodney W. Grimes 			NOTE("dissecting");
27758f0484fSRodney W. Grimes 			dp = dissect(m, m->coldp, endp, gf, gl);
27858f0484fSRodney W. Grimes 		} else {
27958f0484fSRodney W. Grimes 			if (g->nplus > 0 && m->lastpos == NULL)
28058f0484fSRodney W. Grimes 				m->lastpos = (char **)malloc((g->nplus+1) *
28158f0484fSRodney W. Grimes 							sizeof(char *));
28258f0484fSRodney W. Grimes 			if (g->nplus > 0 && m->lastpos == NULL) {
28358f0484fSRodney W. Grimes 				free(m->pmatch);
28458f0484fSRodney W. Grimes 				STATETEARDOWN(m);
28558f0484fSRodney W. Grimes 				return(REG_ESPACE);
28658f0484fSRodney W. Grimes 			}
28758f0484fSRodney W. Grimes 			NOTE("backref dissect");
28858f0484fSRodney W. Grimes 			dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
28958f0484fSRodney W. Grimes 		}
29058f0484fSRodney W. Grimes 		if (dp != NULL)
29158f0484fSRodney W. Grimes 			break;
29258f0484fSRodney W. Grimes 
29358f0484fSRodney W. Grimes 		/* uh-oh... we couldn't find a subexpression-level match */
29458f0484fSRodney W. Grimes 		assert(g->backrefs);	/* must be back references doing it */
29558f0484fSRodney W. Grimes 		assert(g->nplus == 0 || m->lastpos != NULL);
29658f0484fSRodney W. Grimes 		for (;;) {
29758f0484fSRodney W. Grimes 			if (dp != NULL || endp <= m->coldp)
29858f0484fSRodney W. Grimes 				break;		/* defeat */
29958f0484fSRodney W. Grimes 			NOTE("backoff");
30058f0484fSRodney W. Grimes 			endp = slow(m, m->coldp, endp-1, gf, gl);
30158f0484fSRodney W. Grimes 			if (endp == NULL)
30258f0484fSRodney W. Grimes 				break;		/* defeat */
30358f0484fSRodney W. Grimes 			/* try it on a shorter possibility */
30458f0484fSRodney W. Grimes #ifndef NDEBUG
30558f0484fSRodney W. Grimes 			for (i = 1; i <= m->g->nsub; i++) {
30658f0484fSRodney W. Grimes 				assert(m->pmatch[i].rm_so == -1);
30758f0484fSRodney W. Grimes 				assert(m->pmatch[i].rm_eo == -1);
30858f0484fSRodney W. Grimes 			}
30958f0484fSRodney W. Grimes #endif
31058f0484fSRodney W. Grimes 			NOTE("backoff dissect");
31158f0484fSRodney W. Grimes 			dp = backref(m, m->coldp, endp, gf, gl, (sopno)0);
31258f0484fSRodney W. Grimes 		}
31358f0484fSRodney W. Grimes 		assert(dp == NULL || dp == endp);
31458f0484fSRodney W. Grimes 		if (dp != NULL)		/* found a shorter one */
31558f0484fSRodney W. Grimes 			break;
31658f0484fSRodney W. Grimes 
31758f0484fSRodney W. Grimes 		/* despite initial appearances, there is no match here */
31858f0484fSRodney W. Grimes 		NOTE("false alarm");
31958f0484fSRodney W. Grimes 		start = m->coldp + 1;	/* recycle starting later */
32058f0484fSRodney W. Grimes 		assert(start <= stop);
32158f0484fSRodney W. Grimes 	}
32258f0484fSRodney W. Grimes 
32358f0484fSRodney W. Grimes 	/* fill in the details if requested */
32458f0484fSRodney W. Grimes 	if (nmatch > 0) {
32558f0484fSRodney W. Grimes 		pmatch[0].rm_so = m->coldp - m->offp;
32658f0484fSRodney W. Grimes 		pmatch[0].rm_eo = endp - m->offp;
32758f0484fSRodney W. Grimes 	}
32858f0484fSRodney W. Grimes 	if (nmatch > 1) {
32958f0484fSRodney W. Grimes 		assert(m->pmatch != NULL);
33058f0484fSRodney W. Grimes 		for (i = 1; i < nmatch; i++)
33158f0484fSRodney W. Grimes 			if (i <= m->g->nsub)
33258f0484fSRodney W. Grimes 				pmatch[i] = m->pmatch[i];
33358f0484fSRodney W. Grimes 			else {
33458f0484fSRodney W. Grimes 				pmatch[i].rm_so = -1;
33558f0484fSRodney W. Grimes 				pmatch[i].rm_eo = -1;
33658f0484fSRodney W. Grimes 			}
33758f0484fSRodney W. Grimes 	}
33858f0484fSRodney W. Grimes 
33958f0484fSRodney W. Grimes 	if (m->pmatch != NULL)
34058f0484fSRodney W. Grimes 		free((char *)m->pmatch);
34158f0484fSRodney W. Grimes 	if (m->lastpos != NULL)
34258f0484fSRodney W. Grimes 		free((char *)m->lastpos);
34358f0484fSRodney W. Grimes 	STATETEARDOWN(m);
34458f0484fSRodney W. Grimes 	return(0);
34558f0484fSRodney W. Grimes }
34658f0484fSRodney W. Grimes 
34758f0484fSRodney W. Grimes /*
34858f0484fSRodney W. Grimes  - dissect - figure out what matched what, no back references
34958f0484fSRodney W. Grimes  == static char *dissect(register struct match *m, char *start, \
35058f0484fSRodney W. Grimes  ==	char *stop, sopno startst, sopno stopst);
35158f0484fSRodney W. Grimes  */
35258f0484fSRodney W. Grimes static char *			/* == stop (success) always */
35358f0484fSRodney W. Grimes dissect(m, start, stop, startst, stopst)
35458f0484fSRodney W. Grimes register struct match *m;
35558f0484fSRodney W. Grimes char *start;
35658f0484fSRodney W. Grimes char *stop;
35758f0484fSRodney W. Grimes sopno startst;
35858f0484fSRodney W. Grimes sopno stopst;
35958f0484fSRodney W. Grimes {
36058f0484fSRodney W. Grimes 	register int i;
36158f0484fSRodney W. Grimes 	register sopno ss;	/* start sop of current subRE */
36258f0484fSRodney W. Grimes 	register sopno es;	/* end sop of current subRE */
36358f0484fSRodney W. Grimes 	register char *sp;	/* start of string matched by it */
36458f0484fSRodney W. Grimes 	register char *stp;	/* string matched by it cannot pass here */
36558f0484fSRodney W. Grimes 	register char *rest;	/* start of rest of string */
36658f0484fSRodney W. Grimes 	register char *tail;	/* string unmatched by rest of RE */
36758f0484fSRodney W. Grimes 	register sopno ssub;	/* start sop of subsubRE */
36858f0484fSRodney W. Grimes 	register sopno esub;	/* end sop of subsubRE */
36958f0484fSRodney W. Grimes 	register char *ssp;	/* start of string matched by subsubRE */
37058f0484fSRodney W. Grimes 	register char *sep;	/* end of string matched by subsubRE */
37158f0484fSRodney W. Grimes 	register char *oldssp;	/* previous ssp */
37258f0484fSRodney W. Grimes 	register char *dp;
37358f0484fSRodney W. Grimes 
37458f0484fSRodney W. Grimes 	AT("diss", start, stop, startst, stopst);
37558f0484fSRodney W. Grimes 	sp = start;
37658f0484fSRodney W. Grimes 	for (ss = startst; ss < stopst; ss = es) {
37758f0484fSRodney W. Grimes 		/* identify end of subRE */
37858f0484fSRodney W. Grimes 		es = ss;
37958f0484fSRodney W. Grimes 		switch (OP(m->g->strip[es])) {
38058f0484fSRodney W. Grimes 		case OPLUS_:
38158f0484fSRodney W. Grimes 		case OQUEST_:
38258f0484fSRodney W. Grimes 			es += OPND(m->g->strip[es]);
38358f0484fSRodney W. Grimes 			break;
38458f0484fSRodney W. Grimes 		case OCH_:
38558f0484fSRodney W. Grimes 			while (OP(m->g->strip[es]) != O_CH)
38658f0484fSRodney W. Grimes 				es += OPND(m->g->strip[es]);
38758f0484fSRodney W. Grimes 			break;
38858f0484fSRodney W. Grimes 		}
38958f0484fSRodney W. Grimes 		es++;
39058f0484fSRodney W. Grimes 
39158f0484fSRodney W. Grimes 		/* figure out what it matched */
39258f0484fSRodney W. Grimes 		switch (OP(m->g->strip[ss])) {
39358f0484fSRodney W. Grimes 		case OEND:
39458f0484fSRodney W. Grimes 			assert(nope);
39558f0484fSRodney W. Grimes 			break;
39658f0484fSRodney W. Grimes 		case OCHAR:
39758f0484fSRodney W. Grimes 			sp++;
39858f0484fSRodney W. Grimes 			break;
39958f0484fSRodney W. Grimes 		case OBOL:
40058f0484fSRodney W. Grimes 		case OEOL:
40158f0484fSRodney W. Grimes 		case OBOW:
40258f0484fSRodney W. Grimes 		case OEOW:
40358f0484fSRodney W. Grimes 			break;
40458f0484fSRodney W. Grimes 		case OANY:
40558f0484fSRodney W. Grimes 		case OANYOF:
40658f0484fSRodney W. Grimes 			sp++;
40758f0484fSRodney W. Grimes 			break;
40858f0484fSRodney W. Grimes 		case OBACK_:
40958f0484fSRodney W. Grimes 		case O_BACK:
41058f0484fSRodney W. Grimes 			assert(nope);
41158f0484fSRodney W. Grimes 			break;
41258f0484fSRodney W. Grimes 		/* cases where length of match is hard to find */
41358f0484fSRodney W. Grimes 		case OQUEST_:
41458f0484fSRodney W. Grimes 			stp = stop;
41558f0484fSRodney W. Grimes 			for (;;) {
41658f0484fSRodney W. Grimes 				/* how long could this one be? */
41758f0484fSRodney W. Grimes 				rest = slow(m, sp, stp, ss, es);
41858f0484fSRodney W. Grimes 				assert(rest != NULL);	/* it did match */
41958f0484fSRodney W. Grimes 				/* could the rest match the rest? */
42058f0484fSRodney W. Grimes 				tail = slow(m, rest, stop, es, stopst);
42158f0484fSRodney W. Grimes 				if (tail == stop)
42258f0484fSRodney W. Grimes 					break;		/* yes! */
42358f0484fSRodney W. Grimes 				/* no -- try a shorter match for this one */
42458f0484fSRodney W. Grimes 				stp = rest - 1;
42558f0484fSRodney W. Grimes 				assert(stp >= sp);	/* it did work */
42658f0484fSRodney W. Grimes 			}
42758f0484fSRodney W. Grimes 			ssub = ss + 1;
42858f0484fSRodney W. Grimes 			esub = es - 1;
42958f0484fSRodney W. Grimes 			/* did innards match? */
43058f0484fSRodney W. Grimes 			if (slow(m, sp, rest, ssub, esub) != NULL) {
43158f0484fSRodney W. Grimes 				dp = dissect(m, sp, rest, ssub, esub);
43258f0484fSRodney W. Grimes 				assert(dp == rest);
43358f0484fSRodney W. Grimes 			} else		/* no */
43458f0484fSRodney W. Grimes 				assert(sp == rest);
43558f0484fSRodney W. Grimes 			sp = rest;
43658f0484fSRodney W. Grimes 			break;
43758f0484fSRodney W. Grimes 		case OPLUS_:
43858f0484fSRodney W. Grimes 			stp = stop;
43958f0484fSRodney W. Grimes 			for (;;) {
44058f0484fSRodney W. Grimes 				/* how long could this one be? */
44158f0484fSRodney W. Grimes 				rest = slow(m, sp, stp, ss, es);
44258f0484fSRodney W. Grimes 				assert(rest != NULL);	/* it did match */
44358f0484fSRodney W. Grimes 				/* could the rest match the rest? */
44458f0484fSRodney W. Grimes 				tail = slow(m, rest, stop, es, stopst);
44558f0484fSRodney W. Grimes 				if (tail == stop)
44658f0484fSRodney W. Grimes 					break;		/* yes! */
44758f0484fSRodney W. Grimes 				/* no -- try a shorter match for this one */
44858f0484fSRodney W. Grimes 				stp = rest - 1;
44958f0484fSRodney W. Grimes 				assert(stp >= sp);	/* it did work */
45058f0484fSRodney W. Grimes 			}
45158f0484fSRodney W. Grimes 			ssub = ss + 1;
45258f0484fSRodney W. Grimes 			esub = es - 1;
45358f0484fSRodney W. Grimes 			ssp = sp;
45458f0484fSRodney W. Grimes 			oldssp = ssp;
45558f0484fSRodney W. Grimes 			for (;;) {	/* find last match of innards */
45658f0484fSRodney W. Grimes 				sep = slow(m, ssp, rest, ssub, esub);
45758f0484fSRodney W. Grimes 				if (sep == NULL || sep == ssp)
45858f0484fSRodney W. Grimes 					break;	/* failed or matched null */
45958f0484fSRodney W. Grimes 				oldssp = ssp;	/* on to next try */
46058f0484fSRodney W. Grimes 				ssp = sep;
46158f0484fSRodney W. Grimes 			}
46258f0484fSRodney W. Grimes 			if (sep == NULL) {
46358f0484fSRodney W. Grimes 				/* last successful match */
46458f0484fSRodney W. Grimes 				sep = ssp;
46558f0484fSRodney W. Grimes 				ssp = oldssp;
46658f0484fSRodney W. Grimes 			}
46758f0484fSRodney W. Grimes 			assert(sep == rest);	/* must exhaust substring */
46858f0484fSRodney W. Grimes 			assert(slow(m, ssp, sep, ssub, esub) == rest);
46958f0484fSRodney W. Grimes 			dp = dissect(m, ssp, sep, ssub, esub);
47058f0484fSRodney W. Grimes 			assert(dp == sep);
47158f0484fSRodney W. Grimes 			sp = rest;
47258f0484fSRodney W. Grimes 			break;
47358f0484fSRodney W. Grimes 		case OCH_:
47458f0484fSRodney W. Grimes 			stp = stop;
47558f0484fSRodney W. Grimes 			for (;;) {
47658f0484fSRodney W. Grimes 				/* how long could this one be? */
47758f0484fSRodney W. Grimes 				rest = slow(m, sp, stp, ss, es);
47858f0484fSRodney W. Grimes 				assert(rest != NULL);	/* it did match */
47958f0484fSRodney W. Grimes 				/* could the rest match the rest? */
48058f0484fSRodney W. Grimes 				tail = slow(m, rest, stop, es, stopst);
48158f0484fSRodney W. Grimes 				if (tail == stop)
48258f0484fSRodney W. Grimes 					break;		/* yes! */
48358f0484fSRodney W. Grimes 				/* no -- try a shorter match for this one */
48458f0484fSRodney W. Grimes 				stp = rest - 1;
48558f0484fSRodney W. Grimes 				assert(stp >= sp);	/* it did work */
48658f0484fSRodney W. Grimes 			}
48758f0484fSRodney W. Grimes 			ssub = ss + 1;
48858f0484fSRodney W. Grimes 			esub = ss + OPND(m->g->strip[ss]) - 1;
48958f0484fSRodney W. Grimes 			assert(OP(m->g->strip[esub]) == OOR1);
49058f0484fSRodney W. Grimes 			for (;;) {	/* find first matching branch */
49158f0484fSRodney W. Grimes 				if (slow(m, sp, rest, ssub, esub) == rest)
49258f0484fSRodney W. Grimes 					break;	/* it matched all of it */
49358f0484fSRodney W. Grimes 				/* that one missed, try next one */
49458f0484fSRodney W. Grimes 				assert(OP(m->g->strip[esub]) == OOR1);
49558f0484fSRodney W. Grimes 				esub++;
49658f0484fSRodney W. Grimes 				assert(OP(m->g->strip[esub]) == OOR2);
49758f0484fSRodney W. Grimes 				ssub = esub + 1;
49858f0484fSRodney W. Grimes 				esub += OPND(m->g->strip[esub]);
49958f0484fSRodney W. Grimes 				if (OP(m->g->strip[esub]) == OOR2)
50058f0484fSRodney W. Grimes 					esub--;
50158f0484fSRodney W. Grimes 				else
50258f0484fSRodney W. Grimes 					assert(OP(m->g->strip[esub]) == O_CH);
50358f0484fSRodney W. Grimes 			}
50458f0484fSRodney W. Grimes 			dp = dissect(m, sp, rest, ssub, esub);
50558f0484fSRodney W. Grimes 			assert(dp == rest);
50658f0484fSRodney W. Grimes 			sp = rest;
50758f0484fSRodney W. Grimes 			break;
50858f0484fSRodney W. Grimes 		case O_PLUS:
50958f0484fSRodney W. Grimes 		case O_QUEST:
51058f0484fSRodney W. Grimes 		case OOR1:
51158f0484fSRodney W. Grimes 		case OOR2:
51258f0484fSRodney W. Grimes 		case O_CH:
51358f0484fSRodney W. Grimes 			assert(nope);
51458f0484fSRodney W. Grimes 			break;
51558f0484fSRodney W. Grimes 		case OLPAREN:
51658f0484fSRodney W. Grimes 			i = OPND(m->g->strip[ss]);
51758f0484fSRodney W. Grimes 			assert(0 < i && i <= m->g->nsub);
51858f0484fSRodney W. Grimes 			m->pmatch[i].rm_so = sp - m->offp;
51958f0484fSRodney W. Grimes 			break;
52058f0484fSRodney W. Grimes 		case ORPAREN:
52158f0484fSRodney W. Grimes 			i = OPND(m->g->strip[ss]);
52258f0484fSRodney W. Grimes 			assert(0 < i && i <= m->g->nsub);
52358f0484fSRodney W. Grimes 			m->pmatch[i].rm_eo = sp - m->offp;
52458f0484fSRodney W. Grimes 			break;
52558f0484fSRodney W. Grimes 		default:		/* uh oh */
52658f0484fSRodney W. Grimes 			assert(nope);
52758f0484fSRodney W. Grimes 			break;
52858f0484fSRodney W. Grimes 		}
52958f0484fSRodney W. Grimes 	}
53058f0484fSRodney W. Grimes 
53158f0484fSRodney W. Grimes 	assert(sp == stop);
53258f0484fSRodney W. Grimes 	return(sp);
53358f0484fSRodney W. Grimes }
53458f0484fSRodney W. Grimes 
53558f0484fSRodney W. Grimes /*
53658f0484fSRodney W. Grimes  - backref - figure out what matched what, figuring in back references
53758f0484fSRodney W. Grimes  == static char *backref(register struct match *m, char *start, \
53858f0484fSRodney W. Grimes  ==	char *stop, sopno startst, sopno stopst, sopno lev);
53958f0484fSRodney W. Grimes  */
54058f0484fSRodney W. Grimes static char *			/* == stop (success) or NULL (failure) */
54158f0484fSRodney W. Grimes backref(m, start, stop, startst, stopst, lev)
54258f0484fSRodney W. Grimes register struct match *m;
54358f0484fSRodney W. Grimes char *start;
54458f0484fSRodney W. Grimes char *stop;
54558f0484fSRodney W. Grimes sopno startst;
54658f0484fSRodney W. Grimes sopno stopst;
54758f0484fSRodney W. Grimes sopno lev;			/* PLUS nesting level */
54858f0484fSRodney W. Grimes {
54958f0484fSRodney W. Grimes 	register int i;
55058f0484fSRodney W. Grimes 	register sopno ss;	/* start sop of current subRE */
55158f0484fSRodney W. Grimes 	register char *sp;	/* start of string matched by it */
55258f0484fSRodney W. Grimes 	register sopno ssub;	/* start sop of subsubRE */
55358f0484fSRodney W. Grimes 	register sopno esub;	/* end sop of subsubRE */
55458f0484fSRodney W. Grimes 	register char *ssp;	/* start of string matched by subsubRE */
55558f0484fSRodney W. Grimes 	register char *dp;
55658f0484fSRodney W. Grimes 	register size_t len;
55758f0484fSRodney W. Grimes 	register int hard;
55858f0484fSRodney W. Grimes 	register sop s;
55958f0484fSRodney W. Grimes 	register regoff_t offsave;
56058f0484fSRodney W. Grimes 	register cset *cs;
56158f0484fSRodney W. Grimes 
56258f0484fSRodney W. Grimes 	AT("back", start, stop, startst, stopst);
56358f0484fSRodney W. Grimes 	sp = start;
56458f0484fSRodney W. Grimes 
56558f0484fSRodney W. Grimes 	/* get as far as we can with easy stuff */
56658f0484fSRodney W. Grimes 	hard = 0;
56758f0484fSRodney W. Grimes 	for (ss = startst; !hard && ss < stopst; ss++)
56858f0484fSRodney W. Grimes 		switch (OP(s = m->g->strip[ss])) {
56958f0484fSRodney W. Grimes 		case OCHAR:
57058f0484fSRodney W. Grimes 			if (sp == stop || *sp++ != (char)OPND(s))
57158f0484fSRodney W. Grimes 				return(NULL);
57258f0484fSRodney W. Grimes 			break;
57358f0484fSRodney W. Grimes 		case OANY:
57458f0484fSRodney W. Grimes 			if (sp == stop)
57558f0484fSRodney W. Grimes 				return(NULL);
57658f0484fSRodney W. Grimes 			sp++;
57758f0484fSRodney W. Grimes 			break;
57858f0484fSRodney W. Grimes 		case OANYOF:
57958f0484fSRodney W. Grimes 			cs = &m->g->sets[OPND(s)];
58058f0484fSRodney W. Grimes 			if (sp == stop || !CHIN(cs, *sp++))
58158f0484fSRodney W. Grimes 				return(NULL);
58258f0484fSRodney W. Grimes 			break;
58358f0484fSRodney W. Grimes 		case OBOL:
58458f0484fSRodney W. Grimes 			if ( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
58558f0484fSRodney W. Grimes 					(sp < m->endp && *(sp-1) == '\n' &&
58658f0484fSRodney W. Grimes 						(m->g->cflags&REG_NEWLINE)) )
58758f0484fSRodney W. Grimes 				{ /* yes */ }
58858f0484fSRodney W. Grimes 			else
58958f0484fSRodney W. Grimes 				return(NULL);
59058f0484fSRodney W. Grimes 			break;
59158f0484fSRodney W. Grimes 		case OEOL:
59258f0484fSRodney W. Grimes 			if ( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
59358f0484fSRodney W. Grimes 					(sp < m->endp && *sp == '\n' &&
59458f0484fSRodney W. Grimes 						(m->g->cflags&REG_NEWLINE)) )
59558f0484fSRodney W. Grimes 				{ /* yes */ }
59658f0484fSRodney W. Grimes 			else
59758f0484fSRodney W. Grimes 				return(NULL);
59858f0484fSRodney W. Grimes 			break;
59958f0484fSRodney W. Grimes 		case OBOW:
60058f0484fSRodney W. Grimes 			if (( (sp == m->beginp && !(m->eflags&REG_NOTBOL)) ||
60158f0484fSRodney W. Grimes 					(sp < m->endp && *(sp-1) == '\n' &&
60258f0484fSRodney W. Grimes 						(m->g->cflags&REG_NEWLINE)) ||
60358f0484fSRodney W. Grimes 					(sp > m->beginp &&
60458f0484fSRodney W. Grimes 							!ISWORD(*(sp-1))) ) &&
60558f0484fSRodney W. Grimes 					(sp < m->endp && ISWORD(*sp)) )
60658f0484fSRodney W. Grimes 				{ /* yes */ }
60758f0484fSRodney W. Grimes 			else
60858f0484fSRodney W. Grimes 				return(NULL);
60958f0484fSRodney W. Grimes 			break;
61058f0484fSRodney W. Grimes 		case OEOW:
61158f0484fSRodney W. Grimes 			if (( (sp == m->endp && !(m->eflags&REG_NOTEOL)) ||
61258f0484fSRodney W. Grimes 					(sp < m->endp && *sp == '\n' &&
61358f0484fSRodney W. Grimes 						(m->g->cflags&REG_NEWLINE)) ||
61458f0484fSRodney W. Grimes 					(sp < m->endp && !ISWORD(*sp)) ) &&
61558f0484fSRodney W. Grimes 					(sp > m->beginp && ISWORD(*(sp-1))) )
61658f0484fSRodney W. Grimes 				{ /* yes */ }
61758f0484fSRodney W. Grimes 			else
61858f0484fSRodney W. Grimes 				return(NULL);
61958f0484fSRodney W. Grimes 			break;
62058f0484fSRodney W. Grimes 		case O_QUEST:
62158f0484fSRodney W. Grimes 			break;
62258f0484fSRodney W. Grimes 		case OOR1:	/* matches null but needs to skip */
62358f0484fSRodney W. Grimes 			ss++;
62458f0484fSRodney W. Grimes 			s = m->g->strip[ss];
62558f0484fSRodney W. Grimes 			do {
62658f0484fSRodney W. Grimes 				assert(OP(s) == OOR2);
62758f0484fSRodney W. Grimes 				ss += OPND(s);
62858f0484fSRodney W. Grimes 			} while (OP(s = m->g->strip[ss]) != O_CH);
62958f0484fSRodney W. Grimes 			/* note that the ss++ gets us past the O_CH */
63058f0484fSRodney W. Grimes 			break;
63158f0484fSRodney W. Grimes 		default:	/* have to make a choice */
63258f0484fSRodney W. Grimes 			hard = 1;
63358f0484fSRodney W. Grimes 			break;
63458f0484fSRodney W. Grimes 		}
63558f0484fSRodney W. Grimes 	if (!hard) {		/* that was it! */
63658f0484fSRodney W. Grimes 		if (sp != stop)
63758f0484fSRodney W. Grimes 			return(NULL);
63858f0484fSRodney W. Grimes 		return(sp);
63958f0484fSRodney W. Grimes 	}
64058f0484fSRodney W. Grimes 	ss--;			/* adjust for the for's final increment */
64158f0484fSRodney W. Grimes 
64258f0484fSRodney W. Grimes 	/* the hard stuff */
64358f0484fSRodney W. Grimes 	AT("hard", sp, stop, ss, stopst);
64458f0484fSRodney W. Grimes 	s = m->g->strip[ss];
64558f0484fSRodney W. Grimes 	switch (OP(s)) {
64658f0484fSRodney W. Grimes 	case OBACK_:		/* the vilest depths */
64758f0484fSRodney W. Grimes 		i = OPND(s);
64858f0484fSRodney W. Grimes 		assert(0 < i && i <= m->g->nsub);
64958f0484fSRodney W. Grimes 		if (m->pmatch[i].rm_eo == -1)
65058f0484fSRodney W. Grimes 			return(NULL);
65158f0484fSRodney W. Grimes 		assert(m->pmatch[i].rm_so != -1);
65258f0484fSRodney W. Grimes 		len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so;
65358f0484fSRodney W. Grimes 		assert(stop - m->beginp >= len);
65458f0484fSRodney W. Grimes 		if (sp > stop - len)
65558f0484fSRodney W. Grimes 			return(NULL);	/* not enough left to match */
65658f0484fSRodney W. Grimes 		ssp = m->offp + m->pmatch[i].rm_so;
65758f0484fSRodney W. Grimes 		if (memcmp(sp, ssp, len) != 0)
65858f0484fSRodney W. Grimes 			return(NULL);
65958f0484fSRodney W. Grimes 		while (m->g->strip[ss] != SOP(O_BACK, i))
66058f0484fSRodney W. Grimes 			ss++;
66158f0484fSRodney W. Grimes 		return(backref(m, sp+len, stop, ss+1, stopst, lev));
66258f0484fSRodney W. Grimes 		break;
66358f0484fSRodney W. Grimes 	case OQUEST_:		/* to null or not */
66458f0484fSRodney W. Grimes 		dp = backref(m, sp, stop, ss+1, stopst, lev);
66558f0484fSRodney W. Grimes 		if (dp != NULL)
66658f0484fSRodney W. Grimes 			return(dp);	/* not */
66758f0484fSRodney W. Grimes 		return(backref(m, sp, stop, ss+OPND(s)+1, stopst, lev));
66858f0484fSRodney W. Grimes 		break;
66958f0484fSRodney W. Grimes 	case OPLUS_:
67058f0484fSRodney W. Grimes 		assert(m->lastpos != NULL);
67158f0484fSRodney W. Grimes 		assert(lev+1 <= m->g->nplus);
67258f0484fSRodney W. Grimes 		m->lastpos[lev+1] = sp;
67358f0484fSRodney W. Grimes 		return(backref(m, sp, stop, ss+1, stopst, lev+1));
67458f0484fSRodney W. Grimes 		break;
67558f0484fSRodney W. Grimes 	case O_PLUS:
67658f0484fSRodney W. Grimes 		if (sp == m->lastpos[lev])	/* last pass matched null */
67758f0484fSRodney W. Grimes 			return(backref(m, sp, stop, ss+1, stopst, lev-1));
67858f0484fSRodney W. Grimes 		/* try another pass */
67958f0484fSRodney W. Grimes 		m->lastpos[lev] = sp;
68058f0484fSRodney W. Grimes 		dp = backref(m, sp, stop, ss-OPND(s)+1, stopst, lev);
68158f0484fSRodney W. Grimes 		if (dp == NULL)
68258f0484fSRodney W. Grimes 			return(backref(m, sp, stop, ss+1, stopst, lev-1));
68358f0484fSRodney W. Grimes 		else
68458f0484fSRodney W. Grimes 			return(dp);
68558f0484fSRodney W. Grimes 		break;
68658f0484fSRodney W. Grimes 	case OCH_:		/* find the right one, if any */
68758f0484fSRodney W. Grimes 		ssub = ss + 1;
68858f0484fSRodney W. Grimes 		esub = ss + OPND(s) - 1;
68958f0484fSRodney W. Grimes 		assert(OP(m->g->strip[esub]) == OOR1);
69058f0484fSRodney W. Grimes 		for (;;) {	/* find first matching branch */
69158f0484fSRodney W. Grimes 			dp = backref(m, sp, stop, ssub, esub, lev);
69258f0484fSRodney W. Grimes 			if (dp != NULL)
69358f0484fSRodney W. Grimes 				return(dp);
69458f0484fSRodney W. Grimes 			/* that one missed, try next one */
69558f0484fSRodney W. Grimes 			if (OP(m->g->strip[esub]) == O_CH)
69658f0484fSRodney W. Grimes 				return(NULL);	/* there is none */
69758f0484fSRodney W. Grimes 			esub++;
69858f0484fSRodney W. Grimes 			assert(OP(m->g->strip[esub]) == OOR2);
69958f0484fSRodney W. Grimes 			ssub = esub + 1;
70058f0484fSRodney W. Grimes 			esub += OPND(m->g->strip[esub]);
70158f0484fSRodney W. Grimes 			if (OP(m->g->strip[esub]) == OOR2)
70258f0484fSRodney W. Grimes 				esub--;
70358f0484fSRodney W. Grimes 			else
70458f0484fSRodney W. Grimes 				assert(OP(m->g->strip[esub]) == O_CH);
70558f0484fSRodney W. Grimes 		}
70658f0484fSRodney W. Grimes 		break;
70758f0484fSRodney W. Grimes 	case OLPAREN:		/* must undo assignment if rest fails */
70858f0484fSRodney W. Grimes 		i = OPND(s);
70958f0484fSRodney W. Grimes 		assert(0 < i && i <= m->g->nsub);
71058f0484fSRodney W. Grimes 		offsave = m->pmatch[i].rm_so;
71158f0484fSRodney W. Grimes 		m->pmatch[i].rm_so = sp - m->offp;
71258f0484fSRodney W. Grimes 		dp = backref(m, sp, stop, ss+1, stopst, lev);
71358f0484fSRodney W. Grimes 		if (dp != NULL)
71458f0484fSRodney W. Grimes 			return(dp);
71558f0484fSRodney W. Grimes 		m->pmatch[i].rm_so = offsave;
71658f0484fSRodney W. Grimes 		return(NULL);
71758f0484fSRodney W. Grimes 		break;
71858f0484fSRodney W. Grimes 	case ORPAREN:		/* must undo assignment if rest fails */
71958f0484fSRodney W. Grimes 		i = OPND(s);
72058f0484fSRodney W. Grimes 		assert(0 < i && i <= m->g->nsub);
72158f0484fSRodney W. Grimes 		offsave = m->pmatch[i].rm_eo;
72258f0484fSRodney W. Grimes 		m->pmatch[i].rm_eo = sp - m->offp;
72358f0484fSRodney W. Grimes 		dp = backref(m, sp, stop, ss+1, stopst, lev);
72458f0484fSRodney W. Grimes 		if (dp != NULL)
72558f0484fSRodney W. Grimes 			return(dp);
72658f0484fSRodney W. Grimes 		m->pmatch[i].rm_eo = offsave;
72758f0484fSRodney W. Grimes 		return(NULL);
72858f0484fSRodney W. Grimes 		break;
72958f0484fSRodney W. Grimes 	default:		/* uh oh */
73058f0484fSRodney W. Grimes 		assert(nope);
73158f0484fSRodney W. Grimes 		break;
73258f0484fSRodney W. Grimes 	}
73358f0484fSRodney W. Grimes 
73458f0484fSRodney W. Grimes 	/* "can't happen" */
73558f0484fSRodney W. Grimes 	assert(nope);
73658f0484fSRodney W. Grimes 	/* NOTREACHED */
73716252f11SPoul-Henning Kamp 	return "shut up gcc";
73858f0484fSRodney W. Grimes }
73958f0484fSRodney W. Grimes 
74058f0484fSRodney W. Grimes /*
74158f0484fSRodney W. Grimes  - fast - step through the string at top speed
74258f0484fSRodney W. Grimes  == static char *fast(register struct match *m, char *start, \
74358f0484fSRodney W. Grimes  ==	char *stop, sopno startst, sopno stopst);
74458f0484fSRodney W. Grimes  */
74558f0484fSRodney W. Grimes static char *			/* where tentative match ended, or NULL */
74658f0484fSRodney W. Grimes fast(m, start, stop, startst, stopst)
74758f0484fSRodney W. Grimes register struct match *m;
74858f0484fSRodney W. Grimes char *start;
74958f0484fSRodney W. Grimes char *stop;
75058f0484fSRodney W. Grimes sopno startst;
75158f0484fSRodney W. Grimes sopno stopst;
75258f0484fSRodney W. Grimes {
75358f0484fSRodney W. Grimes 	register states st = m->st;
75458f0484fSRodney W. Grimes 	register states fresh = m->fresh;
75558f0484fSRodney W. Grimes 	register states tmp = m->tmp;
75658f0484fSRodney W. Grimes 	register char *p = start;
75758f0484fSRodney W. Grimes 	register int c = (start == m->beginp) ? OUT : *(start-1);
75858f0484fSRodney W. Grimes 	register int lastc;	/* previous c */
75958f0484fSRodney W. Grimes 	register int flagch;
76058f0484fSRodney W. Grimes 	register int i;
76158f0484fSRodney W. Grimes 	register char *coldp;	/* last p after which no match was underway */
76258f0484fSRodney W. Grimes 
76358f0484fSRodney W. Grimes 	CLEAR(st);
76458f0484fSRodney W. Grimes 	SET1(st, startst);
76558f0484fSRodney W. Grimes 	st = step(m->g, startst, stopst, st, NOTHING, st);
76658f0484fSRodney W. Grimes 	ASSIGN(fresh, st);
76758f0484fSRodney W. Grimes 	SP("start", st, *p);
76858f0484fSRodney W. Grimes 	coldp = NULL;
76958f0484fSRodney W. Grimes 	for (;;) {
77058f0484fSRodney W. Grimes 		/* next character */
77158f0484fSRodney W. Grimes 		lastc = c;
77258f0484fSRodney W. Grimes 		c = (p == m->endp) ? OUT : *p;
77358f0484fSRodney W. Grimes 		if (EQ(st, fresh))
77458f0484fSRodney W. Grimes 			coldp = p;
77558f0484fSRodney W. Grimes 
77658f0484fSRodney W. Grimes 		/* is there an EOL and/or BOL between lastc and c? */
77758f0484fSRodney W. Grimes 		flagch = '\0';
77858f0484fSRodney W. Grimes 		i = 0;
77958f0484fSRodney W. Grimes 		if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
78058f0484fSRodney W. Grimes 				(lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
78158f0484fSRodney W. Grimes 			flagch = BOL;
78258f0484fSRodney W. Grimes 			i = m->g->nbol;
78358f0484fSRodney W. Grimes 		}
78458f0484fSRodney W. Grimes 		if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
78558f0484fSRodney W. Grimes 				(c == OUT && !(m->eflags&REG_NOTEOL)) ) {
78658f0484fSRodney W. Grimes 			flagch = (flagch == BOL) ? BOLEOL : EOL;
78758f0484fSRodney W. Grimes 			i += m->g->neol;
78858f0484fSRodney W. Grimes 		}
78958f0484fSRodney W. Grimes 		if (i != 0) {
79058f0484fSRodney W. Grimes 			for (; i > 0; i--)
79158f0484fSRodney W. Grimes 				st = step(m->g, startst, stopst, st, flagch, st);
79258f0484fSRodney W. Grimes 			SP("boleol", st, c);
79358f0484fSRodney W. Grimes 		}
79458f0484fSRodney W. Grimes 
79558f0484fSRodney W. Grimes 		/* how about a word boundary? */
79658f0484fSRodney W. Grimes 		if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
79758f0484fSRodney W. Grimes 					(c != OUT && ISWORD(c)) ) {
79858f0484fSRodney W. Grimes 			flagch = BOW;
79958f0484fSRodney W. Grimes 		}
80058f0484fSRodney W. Grimes 		if ( (lastc != OUT && ISWORD(lastc)) &&
80158f0484fSRodney W. Grimes 				(flagch == EOL || (c != OUT && !ISWORD(c))) ) {
80258f0484fSRodney W. Grimes 			flagch = EOW;
80358f0484fSRodney W. Grimes 		}
80458f0484fSRodney W. Grimes 		if (flagch == BOW || flagch == EOW) {
80558f0484fSRodney W. Grimes 			st = step(m->g, startst, stopst, st, flagch, st);
80658f0484fSRodney W. Grimes 			SP("boweow", st, c);
80758f0484fSRodney W. Grimes 		}
80858f0484fSRodney W. Grimes 
80958f0484fSRodney W. Grimes 		/* are we done? */
81058f0484fSRodney W. Grimes 		if (ISSET(st, stopst) || p == stop)
81158f0484fSRodney W. Grimes 			break;		/* NOTE BREAK OUT */
81258f0484fSRodney W. Grimes 
81358f0484fSRodney W. Grimes 		/* no, we must deal with this character */
81458f0484fSRodney W. Grimes 		ASSIGN(tmp, st);
81558f0484fSRodney W. Grimes 		ASSIGN(st, fresh);
81658f0484fSRodney W. Grimes 		assert(c != OUT);
81758f0484fSRodney W. Grimes 		st = step(m->g, startst, stopst, tmp, c, st);
81858f0484fSRodney W. Grimes 		SP("aft", st, c);
81958f0484fSRodney W. Grimes 		assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
82058f0484fSRodney W. Grimes 		p++;
82158f0484fSRodney W. Grimes 	}
82258f0484fSRodney W. Grimes 
82358f0484fSRodney W. Grimes 	assert(coldp != NULL);
82458f0484fSRodney W. Grimes 	m->coldp = coldp;
82558f0484fSRodney W. Grimes 	if (ISSET(st, stopst))
82658f0484fSRodney W. Grimes 		return(p+1);
82758f0484fSRodney W. Grimes 	else
82858f0484fSRodney W. Grimes 		return(NULL);
82958f0484fSRodney W. Grimes }
83058f0484fSRodney W. Grimes 
83158f0484fSRodney W. Grimes /*
83258f0484fSRodney W. Grimes  - slow - step through the string more deliberately
83358f0484fSRodney W. Grimes  == static char *slow(register struct match *m, char *start, \
83458f0484fSRodney W. Grimes  ==	char *stop, sopno startst, sopno stopst);
83558f0484fSRodney W. Grimes  */
83658f0484fSRodney W. Grimes static char *			/* where it ended */
83758f0484fSRodney W. Grimes slow(m, start, stop, startst, stopst)
83858f0484fSRodney W. Grimes register struct match *m;
83958f0484fSRodney W. Grimes char *start;
84058f0484fSRodney W. Grimes char *stop;
84158f0484fSRodney W. Grimes sopno startst;
84258f0484fSRodney W. Grimes sopno stopst;
84358f0484fSRodney W. Grimes {
84458f0484fSRodney W. Grimes 	register states st = m->st;
84558f0484fSRodney W. Grimes 	register states empty = m->empty;
84658f0484fSRodney W. Grimes 	register states tmp = m->tmp;
84758f0484fSRodney W. Grimes 	register char *p = start;
84858f0484fSRodney W. Grimes 	register int c = (start == m->beginp) ? OUT : *(start-1);
84958f0484fSRodney W. Grimes 	register int lastc;	/* previous c */
85058f0484fSRodney W. Grimes 	register int flagch;
85158f0484fSRodney W. Grimes 	register int i;
85258f0484fSRodney W. Grimes 	register char *matchp;	/* last p at which a match ended */
85358f0484fSRodney W. Grimes 
85458f0484fSRodney W. Grimes 	AT("slow", start, stop, startst, stopst);
85558f0484fSRodney W. Grimes 	CLEAR(st);
85658f0484fSRodney W. Grimes 	SET1(st, startst);
85758f0484fSRodney W. Grimes 	SP("sstart", st, *p);
85858f0484fSRodney W. Grimes 	st = step(m->g, startst, stopst, st, NOTHING, st);
85958f0484fSRodney W. Grimes 	matchp = NULL;
86058f0484fSRodney W. Grimes 	for (;;) {
86158f0484fSRodney W. Grimes 		/* next character */
86258f0484fSRodney W. Grimes 		lastc = c;
86358f0484fSRodney W. Grimes 		c = (p == m->endp) ? OUT : *p;
86458f0484fSRodney W. Grimes 
86558f0484fSRodney W. Grimes 		/* is there an EOL and/or BOL between lastc and c? */
86658f0484fSRodney W. Grimes 		flagch = '\0';
86758f0484fSRodney W. Grimes 		i = 0;
86858f0484fSRodney W. Grimes 		if ( (lastc == '\n' && m->g->cflags&REG_NEWLINE) ||
86958f0484fSRodney W. Grimes 				(lastc == OUT && !(m->eflags&REG_NOTBOL)) ) {
87058f0484fSRodney W. Grimes 			flagch = BOL;
87158f0484fSRodney W. Grimes 			i = m->g->nbol;
87258f0484fSRodney W. Grimes 		}
87358f0484fSRodney W. Grimes 		if ( (c == '\n' && m->g->cflags&REG_NEWLINE) ||
87458f0484fSRodney W. Grimes 				(c == OUT && !(m->eflags&REG_NOTEOL)) ) {
87558f0484fSRodney W. Grimes 			flagch = (flagch == BOL) ? BOLEOL : EOL;
87658f0484fSRodney W. Grimes 			i += m->g->neol;
87758f0484fSRodney W. Grimes 		}
87858f0484fSRodney W. Grimes 		if (i != 0) {
87958f0484fSRodney W. Grimes 			for (; i > 0; i--)
88058f0484fSRodney W. Grimes 				st = step(m->g, startst, stopst, st, flagch, st);
88158f0484fSRodney W. Grimes 			SP("sboleol", st, c);
88258f0484fSRodney W. Grimes 		}
88358f0484fSRodney W. Grimes 
88458f0484fSRodney W. Grimes 		/* how about a word boundary? */
88558f0484fSRodney W. Grimes 		if ( (flagch == BOL || (lastc != OUT && !ISWORD(lastc))) &&
88658f0484fSRodney W. Grimes 					(c != OUT && ISWORD(c)) ) {
88758f0484fSRodney W. Grimes 			flagch = BOW;
88858f0484fSRodney W. Grimes 		}
88958f0484fSRodney W. Grimes 		if ( (lastc != OUT && ISWORD(lastc)) &&
89058f0484fSRodney W. Grimes 				(flagch == EOL || (c != OUT && !ISWORD(c))) ) {
89158f0484fSRodney W. Grimes 			flagch = EOW;
89258f0484fSRodney W. Grimes 		}
89358f0484fSRodney W. Grimes 		if (flagch == BOW || flagch == EOW) {
89458f0484fSRodney W. Grimes 			st = step(m->g, startst, stopst, st, flagch, st);
89558f0484fSRodney W. Grimes 			SP("sboweow", st, c);
89658f0484fSRodney W. Grimes 		}
89758f0484fSRodney W. Grimes 
89858f0484fSRodney W. Grimes 		/* are we done? */
89958f0484fSRodney W. Grimes 		if (ISSET(st, stopst))
90058f0484fSRodney W. Grimes 			matchp = p;
90158f0484fSRodney W. Grimes 		if (EQ(st, empty) || p == stop)
90258f0484fSRodney W. Grimes 			break;		/* NOTE BREAK OUT */
90358f0484fSRodney W. Grimes 
90458f0484fSRodney W. Grimes 		/* no, we must deal with this character */
90558f0484fSRodney W. Grimes 		ASSIGN(tmp, st);
90658f0484fSRodney W. Grimes 		ASSIGN(st, empty);
90758f0484fSRodney W. Grimes 		assert(c != OUT);
90858f0484fSRodney W. Grimes 		st = step(m->g, startst, stopst, tmp, c, st);
90958f0484fSRodney W. Grimes 		SP("saft", st, c);
91058f0484fSRodney W. Grimes 		assert(EQ(step(m->g, startst, stopst, st, NOTHING, st), st));
91158f0484fSRodney W. Grimes 		p++;
91258f0484fSRodney W. Grimes 	}
91358f0484fSRodney W. Grimes 
91458f0484fSRodney W. Grimes 	return(matchp);
91558f0484fSRodney W. Grimes }
91658f0484fSRodney W. Grimes 
91758f0484fSRodney W. Grimes 
91858f0484fSRodney W. Grimes /*
91958f0484fSRodney W. Grimes  - step - map set of states reachable before char to set reachable after
92058f0484fSRodney W. Grimes  == static states step(register struct re_guts *g, sopno start, sopno stop, \
92158f0484fSRodney W. Grimes  ==	register states bef, int ch, register states aft);
92258f0484fSRodney W. Grimes  == #define	BOL	(OUT+1)
92358f0484fSRodney W. Grimes  == #define	EOL	(BOL+1)
92458f0484fSRodney W. Grimes  == #define	BOLEOL	(BOL+2)
92558f0484fSRodney W. Grimes  == #define	NOTHING	(BOL+3)
92658f0484fSRodney W. Grimes  == #define	BOW	(BOL+4)
92758f0484fSRodney W. Grimes  == #define	EOW	(BOL+5)
92858f0484fSRodney W. Grimes  == #define	CODEMAX	(BOL+5)		// highest code used
92958f0484fSRodney W. Grimes  == #define	NONCHAR(c)	((c) > CHAR_MAX)
93058f0484fSRodney W. Grimes  == #define	NNONCHAR	(CODEMAX-CHAR_MAX)
93158f0484fSRodney W. Grimes  */
93258f0484fSRodney W. Grimes static states
93358f0484fSRodney W. Grimes step(g, start, stop, bef, ch, aft)
93458f0484fSRodney W. Grimes register struct re_guts *g;
93558f0484fSRodney W. Grimes sopno start;			/* start state within strip */
93658f0484fSRodney W. Grimes sopno stop;			/* state after stop state within strip */
93758f0484fSRodney W. Grimes register states bef;		/* states reachable before */
93858f0484fSRodney W. Grimes int ch;				/* character or NONCHAR code */
93958f0484fSRodney W. Grimes register states aft;		/* states already known reachable after */
94058f0484fSRodney W. Grimes {
94158f0484fSRodney W. Grimes 	register cset *cs;
94258f0484fSRodney W. Grimes 	register sop s;
94358f0484fSRodney W. Grimes 	register sopno pc;
94458f0484fSRodney W. Grimes 	register onestate here;		/* note, macros know this name */
94558f0484fSRodney W. Grimes 	register sopno look;
94658f0484fSRodney W. Grimes 	register int i;
94758f0484fSRodney W. Grimes 
94858f0484fSRodney W. Grimes 	for (pc = start, INIT(here, pc); pc != stop; pc++, INC(here)) {
94958f0484fSRodney W. Grimes 		s = g->strip[pc];
95058f0484fSRodney W. Grimes 		switch (OP(s)) {
95158f0484fSRodney W. Grimes 		case OEND:
95258f0484fSRodney W. Grimes 			assert(pc == stop-1);
95358f0484fSRodney W. Grimes 			break;
95458f0484fSRodney W. Grimes 		case OCHAR:
95558f0484fSRodney W. Grimes 			/* only characters can match */
95658f0484fSRodney W. Grimes 			assert(!NONCHAR(ch) || ch != (char)OPND(s));
95758f0484fSRodney W. Grimes 			if (ch == (char)OPND(s))
95858f0484fSRodney W. Grimes 				FWD(aft, bef, 1);
95958f0484fSRodney W. Grimes 			break;
96058f0484fSRodney W. Grimes 		case OBOL:
96158f0484fSRodney W. Grimes 			if (ch == BOL || ch == BOLEOL)
96258f0484fSRodney W. Grimes 				FWD(aft, bef, 1);
96358f0484fSRodney W. Grimes 			break;
96458f0484fSRodney W. Grimes 		case OEOL:
96558f0484fSRodney W. Grimes 			if (ch == EOL || ch == BOLEOL)
96658f0484fSRodney W. Grimes 				FWD(aft, bef, 1);
96758f0484fSRodney W. Grimes 			break;
96858f0484fSRodney W. Grimes 		case OBOW:
96958f0484fSRodney W. Grimes 			if (ch == BOW)
97058f0484fSRodney W. Grimes 				FWD(aft, bef, 1);
97158f0484fSRodney W. Grimes 			break;
97258f0484fSRodney W. Grimes 		case OEOW:
97358f0484fSRodney W. Grimes 			if (ch == EOW)
97458f0484fSRodney W. Grimes 				FWD(aft, bef, 1);
97558f0484fSRodney W. Grimes 			break;
97658f0484fSRodney W. Grimes 		case OANY:
97758f0484fSRodney W. Grimes 			if (!NONCHAR(ch))
97858f0484fSRodney W. Grimes 				FWD(aft, bef, 1);
97958f0484fSRodney W. Grimes 			break;
98058f0484fSRodney W. Grimes 		case OANYOF:
98158f0484fSRodney W. Grimes 			cs = &g->sets[OPND(s)];
98258f0484fSRodney W. Grimes 			if (!NONCHAR(ch) && CHIN(cs, ch))
98358f0484fSRodney W. Grimes 				FWD(aft, bef, 1);
98458f0484fSRodney W. Grimes 			break;
98558f0484fSRodney W. Grimes 		case OBACK_:		/* ignored here */
98658f0484fSRodney W. Grimes 		case O_BACK:
98758f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
98858f0484fSRodney W. Grimes 			break;
98958f0484fSRodney W. Grimes 		case OPLUS_:		/* forward, this is just an empty */
99058f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
99158f0484fSRodney W. Grimes 			break;
99258f0484fSRodney W. Grimes 		case O_PLUS:		/* both forward and back */
99358f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
99458f0484fSRodney W. Grimes 			i = ISSETBACK(aft, OPND(s));
99558f0484fSRodney W. Grimes 			BACK(aft, aft, OPND(s));
99658f0484fSRodney W. Grimes 			if (!i && ISSETBACK(aft, OPND(s))) {
99758f0484fSRodney W. Grimes 				/* oho, must reconsider loop body */
99858f0484fSRodney W. Grimes 				pc -= OPND(s) + 1;
99958f0484fSRodney W. Grimes 				INIT(here, pc);
100058f0484fSRodney W. Grimes 			}
100158f0484fSRodney W. Grimes 			break;
100258f0484fSRodney W. Grimes 		case OQUEST_:		/* two branches, both forward */
100358f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
100458f0484fSRodney W. Grimes 			FWD(aft, aft, OPND(s));
100558f0484fSRodney W. Grimes 			break;
100658f0484fSRodney W. Grimes 		case O_QUEST:		/* just an empty */
100758f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
100858f0484fSRodney W. Grimes 			break;
100958f0484fSRodney W. Grimes 		case OLPAREN:		/* not significant here */
101058f0484fSRodney W. Grimes 		case ORPAREN:
101158f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
101258f0484fSRodney W. Grimes 			break;
101358f0484fSRodney W. Grimes 		case OCH_:		/* mark the first two branches */
101458f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
101558f0484fSRodney W. Grimes 			assert(OP(g->strip[pc+OPND(s)]) == OOR2);
101658f0484fSRodney W. Grimes 			FWD(aft, aft, OPND(s));
101758f0484fSRodney W. Grimes 			break;
101858f0484fSRodney W. Grimes 		case OOR1:		/* done a branch, find the O_CH */
101958f0484fSRodney W. Grimes 			if (ISSTATEIN(aft, here)) {
102058f0484fSRodney W. Grimes 				for (look = 1;
102158f0484fSRodney W. Grimes 						OP(s = g->strip[pc+look]) != O_CH;
102258f0484fSRodney W. Grimes 						look += OPND(s))
102358f0484fSRodney W. Grimes 					assert(OP(s) == OOR2);
102458f0484fSRodney W. Grimes 				FWD(aft, aft, look);
102558f0484fSRodney W. Grimes 			}
102658f0484fSRodney W. Grimes 			break;
102758f0484fSRodney W. Grimes 		case OOR2:		/* propagate OCH_'s marking */
102858f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
102958f0484fSRodney W. Grimes 			if (OP(g->strip[pc+OPND(s)]) != O_CH) {
103058f0484fSRodney W. Grimes 				assert(OP(g->strip[pc+OPND(s)]) == OOR2);
103158f0484fSRodney W. Grimes 				FWD(aft, aft, OPND(s));
103258f0484fSRodney W. Grimes 			}
103358f0484fSRodney W. Grimes 			break;
103458f0484fSRodney W. Grimes 		case O_CH:		/* just empty */
103558f0484fSRodney W. Grimes 			FWD(aft, aft, 1);
103658f0484fSRodney W. Grimes 			break;
103758f0484fSRodney W. Grimes 		default:		/* ooooops... */
103858f0484fSRodney W. Grimes 			assert(nope);
103958f0484fSRodney W. Grimes 			break;
104058f0484fSRodney W. Grimes 		}
104158f0484fSRodney W. Grimes 	}
104258f0484fSRodney W. Grimes 
104358f0484fSRodney W. Grimes 	return(aft);
104458f0484fSRodney W. Grimes }
104558f0484fSRodney W. Grimes 
104658f0484fSRodney W. Grimes #ifdef REDEBUG
104758f0484fSRodney W. Grimes /*
104858f0484fSRodney W. Grimes  - print - print a set of states
104958f0484fSRodney W. Grimes  == #ifdef REDEBUG
105058f0484fSRodney W. Grimes  == static void print(struct match *m, char *caption, states st, \
105158f0484fSRodney W. Grimes  ==	int ch, FILE *d);
105258f0484fSRodney W. Grimes  == #endif
105358f0484fSRodney W. Grimes  */
105458f0484fSRodney W. Grimes static void
105558f0484fSRodney W. Grimes print(m, caption, st, ch, d)
105658f0484fSRodney W. Grimes struct match *m;
105758f0484fSRodney W. Grimes char *caption;
105858f0484fSRodney W. Grimes states st;
105958f0484fSRodney W. Grimes int ch;
106058f0484fSRodney W. Grimes FILE *d;
106158f0484fSRodney W. Grimes {
106258f0484fSRodney W. Grimes 	register struct re_guts *g = m->g;
106358f0484fSRodney W. Grimes 	register int i;
106458f0484fSRodney W. Grimes 	register int first = 1;
106558f0484fSRodney W. Grimes 
106658f0484fSRodney W. Grimes 	if (!(m->eflags&REG_TRACE))
106758f0484fSRodney W. Grimes 		return;
106858f0484fSRodney W. Grimes 
106958f0484fSRodney W. Grimes 	fprintf(d, "%s", caption);
107058f0484fSRodney W. Grimes 	if (ch != '\0')
107158f0484fSRodney W. Grimes 		fprintf(d, " %s", pchar(ch));
107258f0484fSRodney W. Grimes 	for (i = 0; i < g->nstates; i++)
107358f0484fSRodney W. Grimes 		if (ISSET(st, i)) {
107458f0484fSRodney W. Grimes 			fprintf(d, "%s%d", (first) ? "\t" : ", ", i);
107558f0484fSRodney W. Grimes 			first = 0;
107658f0484fSRodney W. Grimes 		}
107758f0484fSRodney W. Grimes 	fprintf(d, "\n");
107858f0484fSRodney W. Grimes }
107958f0484fSRodney W. Grimes 
108058f0484fSRodney W. Grimes /*
108158f0484fSRodney W. Grimes  - at - print current situation
108258f0484fSRodney W. Grimes  == #ifdef REDEBUG
108358f0484fSRodney W. Grimes  == static void at(struct match *m, char *title, char *start, char *stop, \
108458f0484fSRodney W. Grimes  ==						sopno startst, sopno stopst);
108558f0484fSRodney W. Grimes  == #endif
108658f0484fSRodney W. Grimes  */
108758f0484fSRodney W. Grimes static void
108858f0484fSRodney W. Grimes at(m, title, start, stop, startst, stopst)
108958f0484fSRodney W. Grimes struct match *m;
109058f0484fSRodney W. Grimes char *title;
109158f0484fSRodney W. Grimes char *start;
109258f0484fSRodney W. Grimes char *stop;
109358f0484fSRodney W. Grimes sopno startst;
109458f0484fSRodney W. Grimes sopno stopst;
109558f0484fSRodney W. Grimes {
109658f0484fSRodney W. Grimes 	if (!(m->eflags&REG_TRACE))
109758f0484fSRodney W. Grimes 		return;
109858f0484fSRodney W. Grimes 
109958f0484fSRodney W. Grimes 	printf("%s %s-", title, pchar(*start));
110058f0484fSRodney W. Grimes 	printf("%s ", pchar(*stop));
110158f0484fSRodney W. Grimes 	printf("%ld-%ld\n", (long)startst, (long)stopst);
110258f0484fSRodney W. Grimes }
110358f0484fSRodney W. Grimes 
110458f0484fSRodney W. Grimes #ifndef PCHARDONE
110558f0484fSRodney W. Grimes #define	PCHARDONE	/* never again */
110658f0484fSRodney W. Grimes /*
110758f0484fSRodney W. Grimes  - pchar - make a character printable
110858f0484fSRodney W. Grimes  == #ifdef REDEBUG
110958f0484fSRodney W. Grimes  == static char *pchar(int ch);
111058f0484fSRodney W. Grimes  == #endif
111158f0484fSRodney W. Grimes  *
111258f0484fSRodney W. Grimes  * Is this identical to regchar() over in debug.c?  Well, yes.  But a
111358f0484fSRodney W. Grimes  * duplicate here avoids having a debugging-capable regexec.o tied to
111458f0484fSRodney W. Grimes  * a matching debug.o, and this is convenient.  It all disappears in
111558f0484fSRodney W. Grimes  * the non-debug compilation anyway, so it doesn't matter much.
111658f0484fSRodney W. Grimes  */
111758f0484fSRodney W. Grimes static char *			/* -> representation */
111858f0484fSRodney W. Grimes pchar(ch)
111958f0484fSRodney W. Grimes int ch;
112058f0484fSRodney W. Grimes {
112158f0484fSRodney W. Grimes 	static char pbuf[10];
112258f0484fSRodney W. Grimes 
1123b5363c4aSAndrey A. Chernov 	if (isprint((uch)ch) || ch == ' ')
112458f0484fSRodney W. Grimes 		sprintf(pbuf, "%c", ch);
112558f0484fSRodney W. Grimes 	else
112658f0484fSRodney W. Grimes 		sprintf(pbuf, "\\%o", ch);
112758f0484fSRodney W. Grimes 	return(pbuf);
112858f0484fSRodney W. Grimes }
112958f0484fSRodney W. Grimes #endif
113058f0484fSRodney W. Grimes #endif
113158f0484fSRodney W. Grimes 
113258f0484fSRodney W. Grimes #undef	matcher
113358f0484fSRodney W. Grimes #undef	fast
113458f0484fSRodney W. Grimes #undef	slow
113558f0484fSRodney W. Grimes #undef	dissect
113658f0484fSRodney W. Grimes #undef	backref
113758f0484fSRodney W. Grimes #undef	step
113858f0484fSRodney W. Grimes #undef	print
113958f0484fSRodney W. Grimes #undef	at
114058f0484fSRodney W. Grimes #undef	match
1141