xref: /freebsd/lib/libc/regex/regcomp.c (revision 54a648d1e5d7baa2e89f141baa263d1e7df70838)
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  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  *
3358f0484fSRodney W. Grimes  *	@(#)regcomp.c	8.5 (Berkeley) 3/20/94
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3758f0484fSRodney W. Grimes static char sccsid[] = "@(#)regcomp.c	8.5 (Berkeley) 3/20/94";
3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
39333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
40333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <sys/types.h>
4358f0484fSRodney W. Grimes #include <stdio.h>
4458f0484fSRodney W. Grimes #include <string.h>
4558f0484fSRodney W. Grimes #include <ctype.h>
4658f0484fSRodney W. Grimes #include <limits.h>
4758f0484fSRodney W. Grimes #include <stdlib.h>
4858f0484fSRodney W. Grimes #include <regex.h>
494e8c80e9SStefan Farfeleder #include <runetype.h>
50e5996857STim J. Robbins #include <wchar.h>
51e5996857STim J. Robbins #include <wctype.h>
5258f0484fSRodney W. Grimes 
53c61cea72SAndrey A. Chernov #include "collate.h"
54c61cea72SAndrey A. Chernov 
5558f0484fSRodney W. Grimes #include "utils.h"
5658f0484fSRodney W. Grimes #include "regex2.h"
5758f0484fSRodney W. Grimes 
5858f0484fSRodney W. Grimes #include "cname.h"
5958f0484fSRodney W. Grimes 
6058f0484fSRodney W. Grimes /*
6158f0484fSRodney W. Grimes  * parse structure, passed up and down to avoid global variables and
6258f0484fSRodney W. Grimes  * other clumsinesses
6358f0484fSRodney W. Grimes  */
6458f0484fSRodney W. Grimes struct parse {
6558f0484fSRodney W. Grimes 	char *next;		/* next character in RE */
6658f0484fSRodney W. Grimes 	char *end;		/* end of string (-> NUL normally) */
6758f0484fSRodney W. Grimes 	int error;		/* has an error been seen? */
6858f0484fSRodney W. Grimes 	sop *strip;		/* malloced strip */
6958f0484fSRodney W. Grimes 	sopno ssize;		/* malloced strip size (allocated) */
7058f0484fSRodney W. Grimes 	sopno slen;		/* malloced strip length (used) */
7158f0484fSRodney W. Grimes 	int ncsalloc;		/* number of csets allocated */
7258f0484fSRodney W. Grimes 	struct re_guts *g;
7358f0484fSRodney W. Grimes #	define	NPAREN	10	/* we need to remember () 1-9 for back refs */
7458f0484fSRodney W. Grimes 	sopno pbegin[NPAREN];	/* -> ( ([0] unused) */
7558f0484fSRodney W. Grimes 	sopno pend[NPAREN];	/* -> ) ([0] unused) */
7658f0484fSRodney W. Grimes };
7758f0484fSRodney W. Grimes 
7858f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */
7958f0484fSRodney W. Grimes #ifdef __cplusplus
8058f0484fSRodney W. Grimes extern "C" {
8158f0484fSRodney W. Grimes #endif
8258f0484fSRodney W. Grimes 
8358f0484fSRodney W. Grimes /* === regcomp.c === */
84e5996857STim J. Robbins static void p_ere(struct parse *p, wint_t stop);
85c05ac53bSDavid E. O'Brien static void p_ere_exp(struct parse *p);
86c05ac53bSDavid E. O'Brien static void p_str(struct parse *p);
87e5996857STim J. Robbins static void p_bre(struct parse *p, wint_t end1, wint_t end2);
88c05ac53bSDavid E. O'Brien static int p_simp_re(struct parse *p, int starordinary);
89c05ac53bSDavid E. O'Brien static int p_count(struct parse *p);
90c05ac53bSDavid E. O'Brien static void p_bracket(struct parse *p);
91c05ac53bSDavid E. O'Brien static void p_b_term(struct parse *p, cset *cs);
92c05ac53bSDavid E. O'Brien static void p_b_cclass(struct parse *p, cset *cs);
93c05ac53bSDavid E. O'Brien static void p_b_eclass(struct parse *p, cset *cs);
94e5996857STim J. Robbins static wint_t p_b_symbol(struct parse *p);
95e5996857STim J. Robbins static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
96e5996857STim J. Robbins static wint_t othercase(wint_t ch);
97e5996857STim J. Robbins static void bothcases(struct parse *p, wint_t ch);
98e5996857STim J. Robbins static void ordinary(struct parse *p, wint_t ch);
99c05ac53bSDavid E. O'Brien static void nonnewline(struct parse *p);
100c05ac53bSDavid E. O'Brien static void repeat(struct parse *p, sopno start, int from, int to);
101c05ac53bSDavid E. O'Brien static int seterr(struct parse *p, int e);
102c05ac53bSDavid E. O'Brien static cset *allocset(struct parse *p);
103c05ac53bSDavid E. O'Brien static void freeset(struct parse *p, cset *cs);
104e5996857STim J. Robbins static void CHadd(struct parse *p, cset *cs, wint_t ch);
105e5996857STim J. Robbins static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max);
106e5996857STim J. Robbins static void CHaddtype(struct parse *p, cset *cs, wctype_t wct);
107e5996857STim J. Robbins static wint_t singleton(cset *cs);
108c05ac53bSDavid E. O'Brien static sopno dupl(struct parse *p, sopno start, sopno finish);
109c05ac53bSDavid E. O'Brien static void doemit(struct parse *p, sop op, size_t opnd);
110c05ac53bSDavid E. O'Brien static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
111c05ac53bSDavid E. O'Brien static void dofwd(struct parse *p, sopno pos, sop value);
112c05ac53bSDavid E. O'Brien static void enlarge(struct parse *p, sopno size);
113c05ac53bSDavid E. O'Brien static void stripsnug(struct parse *p, struct re_guts *g);
114c05ac53bSDavid E. O'Brien static void findmust(struct parse *p, struct re_guts *g);
11580f36366STim J. Robbins static int altoffset(sop *scan, int offset);
116c05ac53bSDavid E. O'Brien static void computejumps(struct parse *p, struct re_guts *g);
117c05ac53bSDavid E. O'Brien static void computematchjumps(struct parse *p, struct re_guts *g);
118c05ac53bSDavid E. O'Brien static sopno pluscount(struct parse *p, struct re_guts *g);
119e5996857STim J. Robbins static wint_t wgetnext(struct parse *p);
12058f0484fSRodney W. Grimes 
12158f0484fSRodney W. Grimes #ifdef __cplusplus
12258f0484fSRodney W. Grimes }
12358f0484fSRodney W. Grimes #endif
12458f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */
12558f0484fSRodney W. Grimes 
12658f0484fSRodney W. Grimes static char nuls[10];		/* place to point scanner in event of error */
12758f0484fSRodney W. Grimes 
12858f0484fSRodney W. Grimes /*
12958f0484fSRodney W. Grimes  * macros for use with parse structure
13058f0484fSRodney W. Grimes  * BEWARE:  these know that the parse structure is named `p' !!!
13158f0484fSRodney W. Grimes  */
13258f0484fSRodney W. Grimes #define	PEEK()	(*p->next)
13358f0484fSRodney W. Grimes #define	PEEK2()	(*(p->next+1))
13458f0484fSRodney W. Grimes #define	MORE()	(p->next < p->end)
13558f0484fSRodney W. Grimes #define	MORE2()	(p->next+1 < p->end)
13658f0484fSRodney W. Grimes #define	SEE(c)	(MORE() && PEEK() == (c))
13758f0484fSRodney W. Grimes #define	SEETWO(a, b)	(MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
13858f0484fSRodney W. Grimes #define	EAT(c)	((SEE(c)) ? (NEXT(), 1) : 0)
13958f0484fSRodney W. Grimes #define	EATTWO(a, b)	((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
14058f0484fSRodney W. Grimes #define	NEXT()	(p->next++)
14158f0484fSRodney W. Grimes #define	NEXT2()	(p->next += 2)
14258f0484fSRodney W. Grimes #define	NEXTn(n)	(p->next += (n))
14358f0484fSRodney W. Grimes #define	GETNEXT()	(*p->next++)
144e5996857STim J. Robbins #define	WGETNEXT()	wgetnext(p)
14558f0484fSRodney W. Grimes #define	SETERROR(e)	seterr(p, (e))
14658f0484fSRodney W. Grimes #define	REQUIRE(co, e)	((co) || SETERROR(e))
14758f0484fSRodney W. Grimes #define	MUSTSEE(c, e)	(REQUIRE(MORE() && PEEK() == (c), e))
14858f0484fSRodney W. Grimes #define	MUSTEAT(c, e)	(REQUIRE(MORE() && GETNEXT() == (c), e))
14958f0484fSRodney W. Grimes #define	MUSTNOTSEE(c, e)	(REQUIRE(!MORE() || PEEK() != (c), e))
15058f0484fSRodney W. Grimes #define	EMIT(op, sopnd)	doemit(p, (sop)(op), (size_t)(sopnd))
15158f0484fSRodney W. Grimes #define	INSERT(op, pos)	doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
15258f0484fSRodney W. Grimes #define	AHEAD(pos)		dofwd(p, pos, HERE()-(pos))
15358f0484fSRodney W. Grimes #define	ASTERN(sop, pos)	EMIT(sop, HERE()-pos)
15458f0484fSRodney W. Grimes #define	HERE()		(p->slen)
15558f0484fSRodney W. Grimes #define	THERE()		(p->slen - 1)
15658f0484fSRodney W. Grimes #define	THERETHERE()	(p->slen - 2)
15758f0484fSRodney W. Grimes #define	DROP(n)	(p->slen -= (n))
15858f0484fSRodney W. Grimes 
15958f0484fSRodney W. Grimes #ifndef NDEBUG
16058f0484fSRodney W. Grimes static int never = 0;		/* for use in asserts; shuts lint up */
16158f0484fSRodney W. Grimes #else
16258f0484fSRodney W. Grimes #define	never	0		/* some <assert.h>s have bugs too */
16358f0484fSRodney W. Grimes #endif
16458f0484fSRodney W. Grimes 
1656049d9f0SDaniel C. Sobral /* Macro used by computejump()/computematchjump() */
1666049d9f0SDaniel C. Sobral #define MIN(a,b)	((a)<(b)?(a):(b))
1676049d9f0SDaniel C. Sobral 
16858f0484fSRodney W. Grimes /*
16958f0484fSRodney W. Grimes  - regcomp - interface for parser and compilation
17058f0484fSRodney W. Grimes  = extern int regcomp(regex_t *, const char *, int);
17158f0484fSRodney W. Grimes  = #define	REG_BASIC	0000
17258f0484fSRodney W. Grimes  = #define	REG_EXTENDED	0001
17358f0484fSRodney W. Grimes  = #define	REG_ICASE	0002
17458f0484fSRodney W. Grimes  = #define	REG_NOSUB	0004
17558f0484fSRodney W. Grimes  = #define	REG_NEWLINE	0010
17658f0484fSRodney W. Grimes  = #define	REG_NOSPEC	0020
17758f0484fSRodney W. Grimes  = #define	REG_PEND	0040
17858f0484fSRodney W. Grimes  = #define	REG_DUMP	0200
17958f0484fSRodney W. Grimes  */
18058f0484fSRodney W. Grimes int				/* 0 success, otherwise REG_something */
18154a648d1SXin LI regcomp(regex_t * __restrict preg,
18254a648d1SXin LI 	const char * __restrict pattern,
18354a648d1SXin LI 	int cflags)
18458f0484fSRodney W. Grimes {
18558f0484fSRodney W. Grimes 	struct parse pa;
1868fb3f3f6SDavid E. O'Brien 	struct re_guts *g;
1878fb3f3f6SDavid E. O'Brien 	struct parse *p = &pa;
1888fb3f3f6SDavid E. O'Brien 	int i;
1898fb3f3f6SDavid E. O'Brien 	size_t len;
19058f0484fSRodney W. Grimes #ifdef REDEBUG
19158f0484fSRodney W. Grimes #	define	GOODFLAGS(f)	(f)
19258f0484fSRodney W. Grimes #else
19358f0484fSRodney W. Grimes #	define	GOODFLAGS(f)	((f)&~REG_DUMP)
19458f0484fSRodney W. Grimes #endif
19558f0484fSRodney W. Grimes 
19658f0484fSRodney W. Grimes 	cflags = GOODFLAGS(cflags);
19758f0484fSRodney W. Grimes 	if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
19858f0484fSRodney W. Grimes 		return(REG_INVARG);
19958f0484fSRodney W. Grimes 
20058f0484fSRodney W. Grimes 	if (cflags&REG_PEND) {
20158f0484fSRodney W. Grimes 		if (preg->re_endp < pattern)
20258f0484fSRodney W. Grimes 			return(REG_INVARG);
20358f0484fSRodney W. Grimes 		len = preg->re_endp - pattern;
20458f0484fSRodney W. Grimes 	} else
20558f0484fSRodney W. Grimes 		len = strlen((char *)pattern);
20658f0484fSRodney W. Grimes 
20758f0484fSRodney W. Grimes 	/* do the mallocs early so failure handling is easy */
20880f36366STim J. Robbins 	g = (struct re_guts *)malloc(sizeof(struct re_guts));
20958f0484fSRodney W. Grimes 	if (g == NULL)
21058f0484fSRodney W. Grimes 		return(REG_ESPACE);
21158f0484fSRodney W. Grimes 	p->ssize = len/(size_t)2*(size_t)3 + (size_t)1;	/* ugh */
21258f0484fSRodney W. Grimes 	p->strip = (sop *)malloc(p->ssize * sizeof(sop));
21358f0484fSRodney W. Grimes 	p->slen = 0;
21458f0484fSRodney W. Grimes 	if (p->strip == NULL) {
21558f0484fSRodney W. Grimes 		free((char *)g);
21658f0484fSRodney W. Grimes 		return(REG_ESPACE);
21758f0484fSRodney W. Grimes 	}
21858f0484fSRodney W. Grimes 
21958f0484fSRodney W. Grimes 	/* set things up */
22058f0484fSRodney W. Grimes 	p->g = g;
22158f0484fSRodney W. Grimes 	p->next = (char *)pattern;	/* convenience; we do not modify it */
22258f0484fSRodney W. Grimes 	p->end = p->next + len;
22358f0484fSRodney W. Grimes 	p->error = 0;
22458f0484fSRodney W. Grimes 	p->ncsalloc = 0;
22558f0484fSRodney W. Grimes 	for (i = 0; i < NPAREN; i++) {
22658f0484fSRodney W. Grimes 		p->pbegin[i] = 0;
22758f0484fSRodney W. Grimes 		p->pend[i] = 0;
22858f0484fSRodney W. Grimes 	}
22958f0484fSRodney W. Grimes 	g->sets = NULL;
23058f0484fSRodney W. Grimes 	g->ncsets = 0;
23158f0484fSRodney W. Grimes 	g->cflags = cflags;
23258f0484fSRodney W. Grimes 	g->iflags = 0;
23358f0484fSRodney W. Grimes 	g->nbol = 0;
23458f0484fSRodney W. Grimes 	g->neol = 0;
23558f0484fSRodney W. Grimes 	g->must = NULL;
236e6a886d8SDaniel C. Sobral 	g->moffset = -1;
2376b709b74SDaniel C. Sobral 	g->charjump = NULL;
2386b709b74SDaniel C. Sobral 	g->matchjump = NULL;
23958f0484fSRodney W. Grimes 	g->mlen = 0;
24058f0484fSRodney W. Grimes 	g->nsub = 0;
24158f0484fSRodney W. Grimes 	g->backrefs = 0;
24258f0484fSRodney W. Grimes 
24358f0484fSRodney W. Grimes 	/* do it */
24458f0484fSRodney W. Grimes 	EMIT(OEND, 0);
24558f0484fSRodney W. Grimes 	g->firststate = THERE();
24658f0484fSRodney W. Grimes 	if (cflags&REG_EXTENDED)
24758f0484fSRodney W. Grimes 		p_ere(p, OUT);
24858f0484fSRodney W. Grimes 	else if (cflags&REG_NOSPEC)
24958f0484fSRodney W. Grimes 		p_str(p);
25058f0484fSRodney W. Grimes 	else
25158f0484fSRodney W. Grimes 		p_bre(p, OUT, OUT);
25258f0484fSRodney W. Grimes 	EMIT(OEND, 0);
25358f0484fSRodney W. Grimes 	g->laststate = THERE();
25458f0484fSRodney W. Grimes 
25558f0484fSRodney W. Grimes 	/* tidy up loose ends and fill things in */
25658f0484fSRodney W. Grimes 	stripsnug(p, g);
25758f0484fSRodney W. Grimes 	findmust(p, g);
2586049d9f0SDaniel C. Sobral 	/* only use Boyer-Moore algorithm if the pattern is bigger
2596049d9f0SDaniel C. Sobral 	 * than three characters
2606049d9f0SDaniel C. Sobral 	 */
2616049d9f0SDaniel C. Sobral 	if(g->mlen > 3) {
2626049d9f0SDaniel C. Sobral 		computejumps(p, g);
2636049d9f0SDaniel C. Sobral 		computematchjumps(p, g);
2644d41cae8SDaniel C. Sobral 		if(g->matchjump == NULL && g->charjump != NULL) {
2656049d9f0SDaniel C. Sobral 			free(g->charjump);
2666049d9f0SDaniel C. Sobral 			g->charjump = NULL;
2676049d9f0SDaniel C. Sobral 		}
2686049d9f0SDaniel C. Sobral 	}
26958f0484fSRodney W. Grimes 	g->nplus = pluscount(p, g);
27058f0484fSRodney W. Grimes 	g->magic = MAGIC2;
27158f0484fSRodney W. Grimes 	preg->re_nsub = g->nsub;
27258f0484fSRodney W. Grimes 	preg->re_g = g;
27358f0484fSRodney W. Grimes 	preg->re_magic = MAGIC1;
27458f0484fSRodney W. Grimes #ifndef REDEBUG
27558f0484fSRodney W. Grimes 	/* not debugging, so can't rely on the assert() in regexec() */
27658f0484fSRodney W. Grimes 	if (g->iflags&BAD)
27758f0484fSRodney W. Grimes 		SETERROR(REG_ASSERT);
27858f0484fSRodney W. Grimes #endif
27958f0484fSRodney W. Grimes 
28058f0484fSRodney W. Grimes 	/* win or lose, we're done */
28158f0484fSRodney W. Grimes 	if (p->error != 0)	/* lose */
28258f0484fSRodney W. Grimes 		regfree(preg);
28358f0484fSRodney W. Grimes 	return(p->error);
28458f0484fSRodney W. Grimes }
28558f0484fSRodney W. Grimes 
28658f0484fSRodney W. Grimes /*
28758f0484fSRodney W. Grimes  - p_ere - ERE parser top level, concatenation and alternation
2888fb3f3f6SDavid E. O'Brien  == static void p_ere(struct parse *p, int stop);
28958f0484fSRodney W. Grimes  */
29058f0484fSRodney W. Grimes static void
29154a648d1SXin LI p_ere(struct parse *p,
29254a648d1SXin LI 	int stop)		/* character this ERE should end at */
29358f0484fSRodney W. Grimes {
2948fb3f3f6SDavid E. O'Brien 	char c;
2958fb3f3f6SDavid E. O'Brien 	sopno prevback;
2968fb3f3f6SDavid E. O'Brien 	sopno prevfwd;
2978fb3f3f6SDavid E. O'Brien 	sopno conc;
2988fb3f3f6SDavid E. O'Brien 	int first = 1;		/* is this the first alternative? */
29958f0484fSRodney W. Grimes 
30058f0484fSRodney W. Grimes 	for (;;) {
30158f0484fSRodney W. Grimes 		/* do a bunch of concatenated expressions */
30258f0484fSRodney W. Grimes 		conc = HERE();
30358f0484fSRodney W. Grimes 		while (MORE() && (c = PEEK()) != '|' && c != stop)
30458f0484fSRodney W. Grimes 			p_ere_exp(p);
30551295a4dSJordan K. Hubbard 		(void)REQUIRE(HERE() != conc, REG_EMPTY);	/* require nonempty */
30658f0484fSRodney W. Grimes 
30758f0484fSRodney W. Grimes 		if (!EAT('|'))
30858f0484fSRodney W. Grimes 			break;		/* NOTE BREAK OUT */
30958f0484fSRodney W. Grimes 
31058f0484fSRodney W. Grimes 		if (first) {
31158f0484fSRodney W. Grimes 			INSERT(OCH_, conc);	/* offset is wrong */
31258f0484fSRodney W. Grimes 			prevfwd = conc;
31358f0484fSRodney W. Grimes 			prevback = conc;
31458f0484fSRodney W. Grimes 			first = 0;
31558f0484fSRodney W. Grimes 		}
31658f0484fSRodney W. Grimes 		ASTERN(OOR1, prevback);
31758f0484fSRodney W. Grimes 		prevback = THERE();
31858f0484fSRodney W. Grimes 		AHEAD(prevfwd);			/* fix previous offset */
31958f0484fSRodney W. Grimes 		prevfwd = HERE();
32058f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset is very wrong */
32158f0484fSRodney W. Grimes 	}
32258f0484fSRodney W. Grimes 
32358f0484fSRodney W. Grimes 	if (!first) {		/* tail-end fixups */
32458f0484fSRodney W. Grimes 		AHEAD(prevfwd);
32558f0484fSRodney W. Grimes 		ASTERN(O_CH, prevback);
32658f0484fSRodney W. Grimes 	}
32758f0484fSRodney W. Grimes 
32858f0484fSRodney W. Grimes 	assert(!MORE() || SEE(stop));
32958f0484fSRodney W. Grimes }
33058f0484fSRodney W. Grimes 
33158f0484fSRodney W. Grimes /*
33258f0484fSRodney W. Grimes  - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
3338fb3f3f6SDavid E. O'Brien  == static void p_ere_exp(struct parse *p);
33458f0484fSRodney W. Grimes  */
33558f0484fSRodney W. Grimes static void
33654a648d1SXin LI p_ere_exp(struct parse *p)
33758f0484fSRodney W. Grimes {
3388fb3f3f6SDavid E. O'Brien 	char c;
339e5996857STim J. Robbins 	wint_t wc;
3408fb3f3f6SDavid E. O'Brien 	sopno pos;
3418fb3f3f6SDavid E. O'Brien 	int count;
3428fb3f3f6SDavid E. O'Brien 	int count2;
3438fb3f3f6SDavid E. O'Brien 	sopno subno;
34458f0484fSRodney W. Grimes 	int wascaret = 0;
34558f0484fSRodney W. Grimes 
34658f0484fSRodney W. Grimes 	assert(MORE());		/* caller should have ensured this */
34758f0484fSRodney W. Grimes 	c = GETNEXT();
34858f0484fSRodney W. Grimes 
34958f0484fSRodney W. Grimes 	pos = HERE();
35058f0484fSRodney W. Grimes 	switch (c) {
35158f0484fSRodney W. Grimes 	case '(':
35251295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EPAREN);
35358f0484fSRodney W. Grimes 		p->g->nsub++;
35458f0484fSRodney W. Grimes 		subno = p->g->nsub;
35558f0484fSRodney W. Grimes 		if (subno < NPAREN)
35658f0484fSRodney W. Grimes 			p->pbegin[subno] = HERE();
35758f0484fSRodney W. Grimes 		EMIT(OLPAREN, subno);
35858f0484fSRodney W. Grimes 		if (!SEE(')'))
35958f0484fSRodney W. Grimes 			p_ere(p, ')');
36058f0484fSRodney W. Grimes 		if (subno < NPAREN) {
36158f0484fSRodney W. Grimes 			p->pend[subno] = HERE();
36258f0484fSRodney W. Grimes 			assert(p->pend[subno] != 0);
36358f0484fSRodney W. Grimes 		}
36458f0484fSRodney W. Grimes 		EMIT(ORPAREN, subno);
36551295a4dSJordan K. Hubbard 		(void)MUSTEAT(')', REG_EPAREN);
36658f0484fSRodney W. Grimes 		break;
36758f0484fSRodney W. Grimes #ifndef POSIX_MISTAKE
36858f0484fSRodney W. Grimes 	case ')':		/* happens only if no current unmatched ( */
36958f0484fSRodney W. Grimes 		/*
37058f0484fSRodney W. Grimes 		 * You may ask, why the ifndef?  Because I didn't notice
37158f0484fSRodney W. Grimes 		 * this until slightly too late for 1003.2, and none of the
37258f0484fSRodney W. Grimes 		 * other 1003.2 regular-expression reviewers noticed it at
37358f0484fSRodney W. Grimes 		 * all.  So an unmatched ) is legal POSIX, at least until
37458f0484fSRodney W. Grimes 		 * we can get it fixed.
37558f0484fSRodney W. Grimes 		 */
37658f0484fSRodney W. Grimes 		SETERROR(REG_EPAREN);
37758f0484fSRodney W. Grimes 		break;
37858f0484fSRodney W. Grimes #endif
37958f0484fSRodney W. Grimes 	case '^':
38058f0484fSRodney W. Grimes 		EMIT(OBOL, 0);
38158f0484fSRodney W. Grimes 		p->g->iflags |= USEBOL;
38258f0484fSRodney W. Grimes 		p->g->nbol++;
38358f0484fSRodney W. Grimes 		wascaret = 1;
38458f0484fSRodney W. Grimes 		break;
38558f0484fSRodney W. Grimes 	case '$':
38658f0484fSRodney W. Grimes 		EMIT(OEOL, 0);
38758f0484fSRodney W. Grimes 		p->g->iflags |= USEEOL;
38858f0484fSRodney W. Grimes 		p->g->neol++;
38958f0484fSRodney W. Grimes 		break;
39058f0484fSRodney W. Grimes 	case '|':
39158f0484fSRodney W. Grimes 		SETERROR(REG_EMPTY);
39258f0484fSRodney W. Grimes 		break;
39358f0484fSRodney W. Grimes 	case '*':
39458f0484fSRodney W. Grimes 	case '+':
39558f0484fSRodney W. Grimes 	case '?':
39658f0484fSRodney W. Grimes 		SETERROR(REG_BADRPT);
39758f0484fSRodney W. Grimes 		break;
39858f0484fSRodney W. Grimes 	case '.':
39958f0484fSRodney W. Grimes 		if (p->g->cflags&REG_NEWLINE)
40058f0484fSRodney W. Grimes 			nonnewline(p);
40158f0484fSRodney W. Grimes 		else
40258f0484fSRodney W. Grimes 			EMIT(OANY, 0);
40358f0484fSRodney W. Grimes 		break;
40458f0484fSRodney W. Grimes 	case '[':
40558f0484fSRodney W. Grimes 		p_bracket(p);
40658f0484fSRodney W. Grimes 		break;
40758f0484fSRodney W. Grimes 	case '\\':
40851295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EESCAPE);
409e5996857STim J. Robbins 		wc = WGETNEXT();
410e5996857STim J. Robbins 		ordinary(p, wc);
41158f0484fSRodney W. Grimes 		break;
41258f0484fSRodney W. Grimes 	case '{':		/* okay as ordinary except if digit follows */
41389b86d02SAndrey A. Chernov 		(void)REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
41458f0484fSRodney W. Grimes 		/* FALLTHROUGH */
41558f0484fSRodney W. Grimes 	default:
416e5996857STim J. Robbins 		p->next--;
417e5996857STim J. Robbins 		wc = WGETNEXT();
418e5996857STim J. Robbins 		ordinary(p, wc);
41958f0484fSRodney W. Grimes 		break;
42058f0484fSRodney W. Grimes 	}
42158f0484fSRodney W. Grimes 
42258f0484fSRodney W. Grimes 	if (!MORE())
42358f0484fSRodney W. Grimes 		return;
42458f0484fSRodney W. Grimes 	c = PEEK();
42558f0484fSRodney W. Grimes 	/* we call { a repetition if followed by a digit */
42658f0484fSRodney W. Grimes 	if (!( c == '*' || c == '+' || c == '?' ||
42789b86d02SAndrey A. Chernov 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
42858f0484fSRodney W. Grimes 		return;		/* no repetition, we're done */
42958f0484fSRodney W. Grimes 	NEXT();
43058f0484fSRodney W. Grimes 
43151295a4dSJordan K. Hubbard 	(void)REQUIRE(!wascaret, REG_BADRPT);
43258f0484fSRodney W. Grimes 	switch (c) {
43358f0484fSRodney W. Grimes 	case '*':	/* implemented as +? */
43458f0484fSRodney W. Grimes 		/* this case does not require the (y|) trick, noKLUDGE */
43558f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
43658f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
43758f0484fSRodney W. Grimes 		INSERT(OQUEST_, pos);
43858f0484fSRodney W. Grimes 		ASTERN(O_QUEST, pos);
43958f0484fSRodney W. Grimes 		break;
44058f0484fSRodney W. Grimes 	case '+':
44158f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
44258f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
44358f0484fSRodney W. Grimes 		break;
44458f0484fSRodney W. Grimes 	case '?':
44558f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
44658f0484fSRodney W. Grimes 		INSERT(OCH_, pos);		/* offset slightly wrong */
44758f0484fSRodney W. Grimes 		ASTERN(OOR1, pos);		/* this one's right */
44858f0484fSRodney W. Grimes 		AHEAD(pos);			/* fix the OCH_ */
44958f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset very wrong... */
45058f0484fSRodney W. Grimes 		AHEAD(THERE());			/* ...so fix it */
45158f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
45258f0484fSRodney W. Grimes 		break;
45358f0484fSRodney W. Grimes 	case '{':
45458f0484fSRodney W. Grimes 		count = p_count(p);
45558f0484fSRodney W. Grimes 		if (EAT(',')) {
45689b86d02SAndrey A. Chernov 			if (isdigit((uch)PEEK())) {
45758f0484fSRodney W. Grimes 				count2 = p_count(p);
45851295a4dSJordan K. Hubbard 				(void)REQUIRE(count <= count2, REG_BADBR);
45958f0484fSRodney W. Grimes 			} else		/* single number with comma */
46058f0484fSRodney W. Grimes 				count2 = INFINITY;
46158f0484fSRodney W. Grimes 		} else		/* just a single number */
46258f0484fSRodney W. Grimes 			count2 = count;
46358f0484fSRodney W. Grimes 		repeat(p, pos, count, count2);
46458f0484fSRodney W. Grimes 		if (!EAT('}')) {	/* error heuristics */
46558f0484fSRodney W. Grimes 			while (MORE() && PEEK() != '}')
46658f0484fSRodney W. Grimes 				NEXT();
46751295a4dSJordan K. Hubbard 			(void)REQUIRE(MORE(), REG_EBRACE);
46858f0484fSRodney W. Grimes 			SETERROR(REG_BADBR);
46958f0484fSRodney W. Grimes 		}
47058f0484fSRodney W. Grimes 		break;
47158f0484fSRodney W. Grimes 	}
47258f0484fSRodney W. Grimes 
47358f0484fSRodney W. Grimes 	if (!MORE())
47458f0484fSRodney W. Grimes 		return;
47558f0484fSRodney W. Grimes 	c = PEEK();
47658f0484fSRodney W. Grimes 	if (!( c == '*' || c == '+' || c == '?' ||
47789b86d02SAndrey A. Chernov 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
47858f0484fSRodney W. Grimes 		return;
47958f0484fSRodney W. Grimes 	SETERROR(REG_BADRPT);
48058f0484fSRodney W. Grimes }
48158f0484fSRodney W. Grimes 
48258f0484fSRodney W. Grimes /*
48358f0484fSRodney W. Grimes  - p_str - string (no metacharacters) "parser"
4848fb3f3f6SDavid E. O'Brien  == static void p_str(struct parse *p);
48558f0484fSRodney W. Grimes  */
48658f0484fSRodney W. Grimes static void
48754a648d1SXin LI p_str(struct parse *p)
48858f0484fSRodney W. Grimes {
48951295a4dSJordan K. Hubbard 	(void)REQUIRE(MORE(), REG_EMPTY);
49058f0484fSRodney W. Grimes 	while (MORE())
491e5996857STim J. Robbins 		ordinary(p, WGETNEXT());
49258f0484fSRodney W. Grimes }
49358f0484fSRodney W. Grimes 
49458f0484fSRodney W. Grimes /*
49558f0484fSRodney W. Grimes  - p_bre - BRE parser top level, anchoring and concatenation
4968fb3f3f6SDavid E. O'Brien  == static void p_bre(struct parse *p, int end1, \
4978fb3f3f6SDavid E. O'Brien  ==	int end2);
49858f0484fSRodney W. Grimes  * Giving end1 as OUT essentially eliminates the end1/end2 check.
49958f0484fSRodney W. Grimes  *
50058f0484fSRodney W. Grimes  * This implementation is a bit of a kludge, in that a trailing $ is first
50180f36366STim J. Robbins  * taken as an ordinary character and then revised to be an anchor.
50258f0484fSRodney W. Grimes  * The amount of lookahead needed to avoid this kludge is excessive.
50358f0484fSRodney W. Grimes  */
50458f0484fSRodney W. Grimes static void
50554a648d1SXin LI p_bre(struct parse *p,
50654a648d1SXin LI 	int end1,		/* first terminating character */
50754a648d1SXin LI 	int end2)		/* second terminating character */
50858f0484fSRodney W. Grimes {
5098fb3f3f6SDavid E. O'Brien 	sopno start = HERE();
5108fb3f3f6SDavid E. O'Brien 	int first = 1;			/* first subexpression? */
5118fb3f3f6SDavid E. O'Brien 	int wasdollar = 0;
51258f0484fSRodney W. Grimes 
51358f0484fSRodney W. Grimes 	if (EAT('^')) {
51458f0484fSRodney W. Grimes 		EMIT(OBOL, 0);
51558f0484fSRodney W. Grimes 		p->g->iflags |= USEBOL;
51658f0484fSRodney W. Grimes 		p->g->nbol++;
51758f0484fSRodney W. Grimes 	}
51858f0484fSRodney W. Grimes 	while (MORE() && !SEETWO(end1, end2)) {
51958f0484fSRodney W. Grimes 		wasdollar = p_simp_re(p, first);
52058f0484fSRodney W. Grimes 		first = 0;
52158f0484fSRodney W. Grimes 	}
52258f0484fSRodney W. Grimes 	if (wasdollar) {	/* oops, that was a trailing anchor */
52358f0484fSRodney W. Grimes 		DROP(1);
52458f0484fSRodney W. Grimes 		EMIT(OEOL, 0);
52558f0484fSRodney W. Grimes 		p->g->iflags |= USEEOL;
52658f0484fSRodney W. Grimes 		p->g->neol++;
52758f0484fSRodney W. Grimes 	}
52858f0484fSRodney W. Grimes 
52951295a4dSJordan K. Hubbard 	(void)REQUIRE(HERE() != start, REG_EMPTY);	/* require nonempty */
53058f0484fSRodney W. Grimes }
53158f0484fSRodney W. Grimes 
53258f0484fSRodney W. Grimes /*
53358f0484fSRodney W. Grimes  - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
5348fb3f3f6SDavid E. O'Brien  == static int p_simp_re(struct parse *p, int starordinary);
53558f0484fSRodney W. Grimes  */
53658f0484fSRodney W. Grimes static int			/* was the simple RE an unbackslashed $? */
53754a648d1SXin LI p_simp_re(struct parse *p,
53854a648d1SXin LI 	int starordinary)	/* is a leading * an ordinary character? */
53958f0484fSRodney W. Grimes {
5408fb3f3f6SDavid E. O'Brien 	int c;
5418fb3f3f6SDavid E. O'Brien 	int count;
5428fb3f3f6SDavid E. O'Brien 	int count2;
5438fb3f3f6SDavid E. O'Brien 	sopno pos;
5448fb3f3f6SDavid E. O'Brien 	int i;
545e5996857STim J. Robbins 	wint_t wc;
5468fb3f3f6SDavid E. O'Brien 	sopno subno;
54758f0484fSRodney W. Grimes #	define	BACKSL	(1<<CHAR_BIT)
54858f0484fSRodney W. Grimes 
54958f0484fSRodney W. Grimes 	pos = HERE();		/* repetion op, if any, covers from here */
55058f0484fSRodney W. Grimes 
55158f0484fSRodney W. Grimes 	assert(MORE());		/* caller should have ensured this */
55258f0484fSRodney W. Grimes 	c = GETNEXT();
55358f0484fSRodney W. Grimes 	if (c == '\\') {
55451295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EESCAPE);
55589b86d02SAndrey A. Chernov 		c = BACKSL | GETNEXT();
55658f0484fSRodney W. Grimes 	}
55758f0484fSRodney W. Grimes 	switch (c) {
55858f0484fSRodney W. Grimes 	case '.':
55958f0484fSRodney W. Grimes 		if (p->g->cflags&REG_NEWLINE)
56058f0484fSRodney W. Grimes 			nonnewline(p);
56158f0484fSRodney W. Grimes 		else
56258f0484fSRodney W. Grimes 			EMIT(OANY, 0);
56358f0484fSRodney W. Grimes 		break;
56458f0484fSRodney W. Grimes 	case '[':
56558f0484fSRodney W. Grimes 		p_bracket(p);
56658f0484fSRodney W. Grimes 		break;
56758f0484fSRodney W. Grimes 	case BACKSL|'{':
56858f0484fSRodney W. Grimes 		SETERROR(REG_BADRPT);
56958f0484fSRodney W. Grimes 		break;
57058f0484fSRodney W. Grimes 	case BACKSL|'(':
57158f0484fSRodney W. Grimes 		p->g->nsub++;
57258f0484fSRodney W. Grimes 		subno = p->g->nsub;
57358f0484fSRodney W. Grimes 		if (subno < NPAREN)
57458f0484fSRodney W. Grimes 			p->pbegin[subno] = HERE();
57558f0484fSRodney W. Grimes 		EMIT(OLPAREN, subno);
57658f0484fSRodney W. Grimes 		/* the MORE here is an error heuristic */
57758f0484fSRodney W. Grimes 		if (MORE() && !SEETWO('\\', ')'))
57858f0484fSRodney W. Grimes 			p_bre(p, '\\', ')');
57958f0484fSRodney W. Grimes 		if (subno < NPAREN) {
58058f0484fSRodney W. Grimes 			p->pend[subno] = HERE();
58158f0484fSRodney W. Grimes 			assert(p->pend[subno] != 0);
58258f0484fSRodney W. Grimes 		}
58358f0484fSRodney W. Grimes 		EMIT(ORPAREN, subno);
58451295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
58558f0484fSRodney W. Grimes 		break;
58658f0484fSRodney W. Grimes 	case BACKSL|')':	/* should not get here -- must be user */
58758f0484fSRodney W. Grimes 	case BACKSL|'}':
58858f0484fSRodney W. Grimes 		SETERROR(REG_EPAREN);
58958f0484fSRodney W. Grimes 		break;
59058f0484fSRodney W. Grimes 	case BACKSL|'1':
59158f0484fSRodney W. Grimes 	case BACKSL|'2':
59258f0484fSRodney W. Grimes 	case BACKSL|'3':
59358f0484fSRodney W. Grimes 	case BACKSL|'4':
59458f0484fSRodney W. Grimes 	case BACKSL|'5':
59558f0484fSRodney W. Grimes 	case BACKSL|'6':
59658f0484fSRodney W. Grimes 	case BACKSL|'7':
59758f0484fSRodney W. Grimes 	case BACKSL|'8':
59858f0484fSRodney W. Grimes 	case BACKSL|'9':
59958f0484fSRodney W. Grimes 		i = (c&~BACKSL) - '0';
60058f0484fSRodney W. Grimes 		assert(i < NPAREN);
60158f0484fSRodney W. Grimes 		if (p->pend[i] != 0) {
60258f0484fSRodney W. Grimes 			assert(i <= p->g->nsub);
60358f0484fSRodney W. Grimes 			EMIT(OBACK_, i);
60458f0484fSRodney W. Grimes 			assert(p->pbegin[i] != 0);
60558f0484fSRodney W. Grimes 			assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
60658f0484fSRodney W. Grimes 			assert(OP(p->strip[p->pend[i]]) == ORPAREN);
60758f0484fSRodney W. Grimes 			(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
60858f0484fSRodney W. Grimes 			EMIT(O_BACK, i);
60958f0484fSRodney W. Grimes 		} else
61058f0484fSRodney W. Grimes 			SETERROR(REG_ESUBREG);
61158f0484fSRodney W. Grimes 		p->g->backrefs = 1;
61258f0484fSRodney W. Grimes 		break;
61358f0484fSRodney W. Grimes 	case '*':
61451295a4dSJordan K. Hubbard 		(void)REQUIRE(starordinary, REG_BADRPT);
61558f0484fSRodney W. Grimes 		/* FALLTHROUGH */
61658f0484fSRodney W. Grimes 	default:
617e5996857STim J. Robbins 		p->next--;
618e5996857STim J. Robbins 		wc = WGETNEXT();
619e5996857STim J. Robbins 		ordinary(p, wc);
62058f0484fSRodney W. Grimes 		break;
62158f0484fSRodney W. Grimes 	}
62258f0484fSRodney W. Grimes 
62358f0484fSRodney W. Grimes 	if (EAT('*')) {		/* implemented as +? */
62458f0484fSRodney W. Grimes 		/* this case does not require the (y|) trick, noKLUDGE */
62558f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
62658f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
62758f0484fSRodney W. Grimes 		INSERT(OQUEST_, pos);
62858f0484fSRodney W. Grimes 		ASTERN(O_QUEST, pos);
62958f0484fSRodney W. Grimes 	} else if (EATTWO('\\', '{')) {
63058f0484fSRodney W. Grimes 		count = p_count(p);
63158f0484fSRodney W. Grimes 		if (EAT(',')) {
63289b86d02SAndrey A. Chernov 			if (MORE() && isdigit((uch)PEEK())) {
63358f0484fSRodney W. Grimes 				count2 = p_count(p);
63451295a4dSJordan K. Hubbard 				(void)REQUIRE(count <= count2, REG_BADBR);
63558f0484fSRodney W. Grimes 			} else		/* single number with comma */
63658f0484fSRodney W. Grimes 				count2 = INFINITY;
63758f0484fSRodney W. Grimes 		} else		/* just a single number */
63858f0484fSRodney W. Grimes 			count2 = count;
63958f0484fSRodney W. Grimes 		repeat(p, pos, count, count2);
64058f0484fSRodney W. Grimes 		if (!EATTWO('\\', '}')) {	/* error heuristics */
64158f0484fSRodney W. Grimes 			while (MORE() && !SEETWO('\\', '}'))
64258f0484fSRodney W. Grimes 				NEXT();
64351295a4dSJordan K. Hubbard 			(void)REQUIRE(MORE(), REG_EBRACE);
64458f0484fSRodney W. Grimes 			SETERROR(REG_BADBR);
64558f0484fSRodney W. Grimes 		}
64689b86d02SAndrey A. Chernov 	} else if (c == '$')     /* $ (but not \$) ends it */
64758f0484fSRodney W. Grimes 		return(1);
64858f0484fSRodney W. Grimes 
64958f0484fSRodney W. Grimes 	return(0);
65058f0484fSRodney W. Grimes }
65158f0484fSRodney W. Grimes 
65258f0484fSRodney W. Grimes /*
65358f0484fSRodney W. Grimes  - p_count - parse a repetition count
6548fb3f3f6SDavid E. O'Brien  == static int p_count(struct parse *p);
65558f0484fSRodney W. Grimes  */
65658f0484fSRodney W. Grimes static int			/* the value */
65754a648d1SXin LI p_count(struct parse *p)
65858f0484fSRodney W. Grimes {
6598fb3f3f6SDavid E. O'Brien 	int count = 0;
6608fb3f3f6SDavid E. O'Brien 	int ndigits = 0;
66158f0484fSRodney W. Grimes 
66289b86d02SAndrey A. Chernov 	while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
66358f0484fSRodney W. Grimes 		count = count*10 + (GETNEXT() - '0');
66458f0484fSRodney W. Grimes 		ndigits++;
66558f0484fSRodney W. Grimes 	}
66658f0484fSRodney W. Grimes 
66751295a4dSJordan K. Hubbard 	(void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
66858f0484fSRodney W. Grimes 	return(count);
66958f0484fSRodney W. Grimes }
67058f0484fSRodney W. Grimes 
67158f0484fSRodney W. Grimes /*
67258f0484fSRodney W. Grimes  - p_bracket - parse a bracketed character list
6738fb3f3f6SDavid E. O'Brien  == static void p_bracket(struct parse *p);
67458f0484fSRodney W. Grimes  */
67558f0484fSRodney W. Grimes static void
67654a648d1SXin LI p_bracket(struct parse *p)
67758f0484fSRodney W. Grimes {
678e5996857STim J. Robbins 	cset *cs;
679e5996857STim J. Robbins 	wint_t ch;
68058f0484fSRodney W. Grimes 
68158f0484fSRodney W. Grimes 	/* Dept of Truly Sickening Special-Case Kludges */
68258f0484fSRodney W. Grimes 	if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
68358f0484fSRodney W. Grimes 		EMIT(OBOW, 0);
68458f0484fSRodney W. Grimes 		NEXTn(6);
68558f0484fSRodney W. Grimes 		return;
68658f0484fSRodney W. Grimes 	}
68758f0484fSRodney W. Grimes 	if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
68858f0484fSRodney W. Grimes 		EMIT(OEOW, 0);
68958f0484fSRodney W. Grimes 		NEXTn(6);
69058f0484fSRodney W. Grimes 		return;
69158f0484fSRodney W. Grimes 	}
69258f0484fSRodney W. Grimes 
693e5996857STim J. Robbins 	if ((cs = allocset(p)) == NULL)
694e5996857STim J. Robbins 		return;
695e5996857STim J. Robbins 
696e5996857STim J. Robbins 	if (p->g->cflags&REG_ICASE)
697e5996857STim J. Robbins 		cs->icase = 1;
69858f0484fSRodney W. Grimes 	if (EAT('^'))
699e5996857STim J. Robbins 		cs->invert = 1;
70058f0484fSRodney W. Grimes 	if (EAT(']'))
701e5996857STim J. Robbins 		CHadd(p, cs, ']');
70258f0484fSRodney W. Grimes 	else if (EAT('-'))
703e5996857STim J. Robbins 		CHadd(p, cs, '-');
70458f0484fSRodney W. Grimes 	while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
70558f0484fSRodney W. Grimes 		p_b_term(p, cs);
70658f0484fSRodney W. Grimes 	if (EAT('-'))
707e5996857STim J. Robbins 		CHadd(p, cs, '-');
70851295a4dSJordan K. Hubbard 	(void)MUSTEAT(']', REG_EBRACK);
70958f0484fSRodney W. Grimes 
71058f0484fSRodney W. Grimes 	if (p->error != 0)	/* don't mess things up further */
71158f0484fSRodney W. Grimes 		return;
71258f0484fSRodney W. Grimes 
713e5996857STim J. Robbins 	if (cs->invert && p->g->cflags&REG_NEWLINE)
714e5996857STim J. Robbins 		cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
71558f0484fSRodney W. Grimes 
716e5996857STim J. Robbins 	if ((ch = singleton(cs)) != OUT) {	/* optimize singleton sets */
717e5996857STim J. Robbins 		ordinary(p, ch);
71858f0484fSRodney W. Grimes 		freeset(p, cs);
71958f0484fSRodney W. Grimes 	} else
720e5996857STim J. Robbins 		EMIT(OANYOF, (int)(cs - p->g->sets));
72158f0484fSRodney W. Grimes }
72258f0484fSRodney W. Grimes 
72358f0484fSRodney W. Grimes /*
72458f0484fSRodney W. Grimes  - p_b_term - parse one term of a bracketed character list
7258fb3f3f6SDavid E. O'Brien  == static void p_b_term(struct parse *p, cset *cs);
72658f0484fSRodney W. Grimes  */
72758f0484fSRodney W. Grimes static void
72854a648d1SXin LI p_b_term(struct parse *p, cset *cs)
72958f0484fSRodney W. Grimes {
7308fb3f3f6SDavid E. O'Brien 	char c;
731e5996857STim J. Robbins 	wint_t start, finish;
732e5996857STim J. Robbins 	wint_t i;
73358f0484fSRodney W. Grimes 
73458f0484fSRodney W. Grimes 	/* classify what we've got */
73558f0484fSRodney W. Grimes 	switch ((MORE()) ? PEEK() : '\0') {
73658f0484fSRodney W. Grimes 	case '[':
73758f0484fSRodney W. Grimes 		c = (MORE2()) ? PEEK2() : '\0';
73858f0484fSRodney W. Grimes 		break;
73958f0484fSRodney W. Grimes 	case '-':
74058f0484fSRodney W. Grimes 		SETERROR(REG_ERANGE);
74158f0484fSRodney W. Grimes 		return;			/* NOTE RETURN */
74258f0484fSRodney W. Grimes 		break;
74358f0484fSRodney W. Grimes 	default:
74458f0484fSRodney W. Grimes 		c = '\0';
74558f0484fSRodney W. Grimes 		break;
74658f0484fSRodney W. Grimes 	}
74758f0484fSRodney W. Grimes 
74858f0484fSRodney W. Grimes 	switch (c) {
74958f0484fSRodney W. Grimes 	case ':':		/* character class */
75058f0484fSRodney W. Grimes 		NEXT2();
75151295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
75258f0484fSRodney W. Grimes 		c = PEEK();
75351295a4dSJordan K. Hubbard 		(void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
75458f0484fSRodney W. Grimes 		p_b_cclass(p, cs);
75551295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
75651295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
75758f0484fSRodney W. Grimes 		break;
75858f0484fSRodney W. Grimes 	case '=':		/* equivalence class */
75958f0484fSRodney W. Grimes 		NEXT2();
76051295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
76158f0484fSRodney W. Grimes 		c = PEEK();
76251295a4dSJordan K. Hubbard 		(void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
76358f0484fSRodney W. Grimes 		p_b_eclass(p, cs);
76451295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
76551295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
76658f0484fSRodney W. Grimes 		break;
76758f0484fSRodney W. Grimes 	default:		/* symbol, ordinary character, or range */
76858f0484fSRodney W. Grimes 		start = p_b_symbol(p);
76958f0484fSRodney W. Grimes 		if (SEE('-') && MORE2() && PEEK2() != ']') {
77058f0484fSRodney W. Grimes 			/* range */
77158f0484fSRodney W. Grimes 			NEXT();
77258f0484fSRodney W. Grimes 			if (EAT('-'))
77358f0484fSRodney W. Grimes 				finish = '-';
77458f0484fSRodney W. Grimes 			else
77558f0484fSRodney W. Grimes 				finish = p_b_symbol(p);
77658f0484fSRodney W. Grimes 		} else
77758f0484fSRodney W. Grimes 			finish = start;
7785c551438SAndrey A. Chernov 		if (start == finish)
779e5996857STim J. Robbins 			CHadd(p, cs, start);
7805c551438SAndrey A. Chernov 		else {
781b5a6eb18SAndrey A. Chernov 			if (__collate_load_error) {
782b5a6eb18SAndrey A. Chernov 				(void)REQUIRE((uch)start <= (uch)finish, REG_ERANGE);
783e5996857STim J. Robbins 				CHaddrange(p, cs, start, finish);
784b5a6eb18SAndrey A. Chernov 			} else {
785c61cea72SAndrey A. Chernov 				(void)REQUIRE(__collate_range_cmp(start, finish) <= 0, REG_ERANGE);
786e5996857STim J. Robbins 				for (i = 0; i <= UCHAR_MAX; i++) {
787c61cea72SAndrey A. Chernov 					if (   __collate_range_cmp(start, i) <= 0
788c61cea72SAndrey A. Chernov 					    && __collate_range_cmp(i, finish) <= 0
7895c551438SAndrey A. Chernov 					   )
790e5996857STim J. Robbins 						CHadd(p, cs, i);
7915c551438SAndrey A. Chernov 				}
7925c551438SAndrey A. Chernov 			}
793b5a6eb18SAndrey A. Chernov 		}
79458f0484fSRodney W. Grimes 		break;
79558f0484fSRodney W. Grimes 	}
79658f0484fSRodney W. Grimes }
79758f0484fSRodney W. Grimes 
79858f0484fSRodney W. Grimes /*
79958f0484fSRodney W. Grimes  - p_b_cclass - parse a character-class name and deal with it
8008fb3f3f6SDavid E. O'Brien  == static void p_b_cclass(struct parse *p, cset *cs);
80158f0484fSRodney W. Grimes  */
80258f0484fSRodney W. Grimes static void
80354a648d1SXin LI p_b_cclass(struct parse *p, cset *cs)
80458f0484fSRodney W. Grimes {
8058fb3f3f6SDavid E. O'Brien 	char *sp = p->next;
8068fb3f3f6SDavid E. O'Brien 	size_t len;
807e5996857STim J. Robbins 	wctype_t wct;
808e5996857STim J. Robbins 	char clname[16];
80958f0484fSRodney W. Grimes 
810b5363c4aSAndrey A. Chernov 	while (MORE() && isalpha((uch)PEEK()))
81158f0484fSRodney W. Grimes 		NEXT();
81258f0484fSRodney W. Grimes 	len = p->next - sp;
813e5996857STim J. Robbins 	if (len >= sizeof(clname) - 1) {
81458f0484fSRodney W. Grimes 		SETERROR(REG_ECTYPE);
81558f0484fSRodney W. Grimes 		return;
81658f0484fSRodney W. Grimes 	}
817e5996857STim J. Robbins 	memcpy(clname, sp, len);
818e5996857STim J. Robbins 	clname[len] = '\0';
819e5996857STim J. Robbins 	if ((wct = wctype(clname)) == 0) {
820e5996857STim J. Robbins 		SETERROR(REG_ECTYPE);
821e5996857STim J. Robbins 		return;
822b5363c4aSAndrey A. Chernov 	}
823e5996857STim J. Robbins 	CHaddtype(p, cs, wct);
82458f0484fSRodney W. Grimes }
82558f0484fSRodney W. Grimes 
82658f0484fSRodney W. Grimes /*
82758f0484fSRodney W. Grimes  - p_b_eclass - parse an equivalence-class name and deal with it
8288fb3f3f6SDavid E. O'Brien  == static void p_b_eclass(struct parse *p, cset *cs);
82958f0484fSRodney W. Grimes  *
83058f0484fSRodney W. Grimes  * This implementation is incomplete. xxx
83158f0484fSRodney W. Grimes  */
83258f0484fSRodney W. Grimes static void
83354a648d1SXin LI p_b_eclass(struct parse *p, cset *cs)
83458f0484fSRodney W. Grimes {
835e5996857STim J. Robbins 	wint_t c;
83658f0484fSRodney W. Grimes 
83758f0484fSRodney W. Grimes 	c = p_b_coll_elem(p, '=');
838e5996857STim J. Robbins 	CHadd(p, cs, c);
83958f0484fSRodney W. Grimes }
84058f0484fSRodney W. Grimes 
84158f0484fSRodney W. Grimes /*
84258f0484fSRodney W. Grimes  - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
8438fb3f3f6SDavid E. O'Brien  == static char p_b_symbol(struct parse *p);
84458f0484fSRodney W. Grimes  */
845e5996857STim J. Robbins static wint_t			/* value of symbol */
84654a648d1SXin LI p_b_symbol(struct parse *p)
84758f0484fSRodney W. Grimes {
848e5996857STim J. Robbins 	wint_t value;
84958f0484fSRodney W. Grimes 
85051295a4dSJordan K. Hubbard 	(void)REQUIRE(MORE(), REG_EBRACK);
85158f0484fSRodney W. Grimes 	if (!EATTWO('[', '.'))
852e5996857STim J. Robbins 		return(WGETNEXT());
85358f0484fSRodney W. Grimes 
85458f0484fSRodney W. Grimes 	/* collating symbol */
85558f0484fSRodney W. Grimes 	value = p_b_coll_elem(p, '.');
85651295a4dSJordan K. Hubbard 	(void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
85758f0484fSRodney W. Grimes 	return(value);
85858f0484fSRodney W. Grimes }
85958f0484fSRodney W. Grimes 
86058f0484fSRodney W. Grimes /*
86158f0484fSRodney W. Grimes  - p_b_coll_elem - parse a collating-element name and look it up
8628fb3f3f6SDavid E. O'Brien  == static char p_b_coll_elem(struct parse *p, int endc);
86358f0484fSRodney W. Grimes  */
864e5996857STim J. Robbins static wint_t			/* value of collating element */
86554a648d1SXin LI p_b_coll_elem(struct parse *p,
86654a648d1SXin LI 	wint_t endc)		/* name ended by endc,']' */
86758f0484fSRodney W. Grimes {
8688fb3f3f6SDavid E. O'Brien 	char *sp = p->next;
8698fb3f3f6SDavid E. O'Brien 	struct cname *cp;
8708fb3f3f6SDavid E. O'Brien 	int len;
871e5996857STim J. Robbins 	mbstate_t mbs;
872e5996857STim J. Robbins 	wchar_t wc;
873e5996857STim J. Robbins 	size_t clen;
87458f0484fSRodney W. Grimes 
87558f0484fSRodney W. Grimes 	while (MORE() && !SEETWO(endc, ']'))
87658f0484fSRodney W. Grimes 		NEXT();
87758f0484fSRodney W. Grimes 	if (!MORE()) {
87858f0484fSRodney W. Grimes 		SETERROR(REG_EBRACK);
87958f0484fSRodney W. Grimes 		return(0);
88058f0484fSRodney W. Grimes 	}
88158f0484fSRodney W. Grimes 	len = p->next - sp;
88258f0484fSRodney W. Grimes 	for (cp = cnames; cp->name != NULL; cp++)
88358f0484fSRodney W. Grimes 		if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
88458f0484fSRodney W. Grimes 			return(cp->code);	/* known name */
885e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
886e5996857STim J. Robbins 	if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
887e5996857STim J. Robbins 		return (wc);			/* single character */
888e5996857STim J. Robbins 	else if (clen == (size_t)-1 || clen == (size_t)-2)
889e5996857STim J. Robbins 		SETERROR(REG_ILLSEQ);
890e5996857STim J. Robbins 	else
89158f0484fSRodney W. Grimes 		SETERROR(REG_ECOLLATE);		/* neither */
89258f0484fSRodney W. Grimes 	return(0);
89358f0484fSRodney W. Grimes }
89458f0484fSRodney W. Grimes 
89558f0484fSRodney W. Grimes /*
89658f0484fSRodney W. Grimes  - othercase - return the case counterpart of an alphabetic
89758f0484fSRodney W. Grimes  == static char othercase(int ch);
89858f0484fSRodney W. Grimes  */
899e5996857STim J. Robbins static wint_t			/* if no counterpart, return ch */
90054a648d1SXin LI othercase(wint_t ch)
90158f0484fSRodney W. Grimes {
902e5996857STim J. Robbins 	assert(iswalpha(ch));
903e5996857STim J. Robbins 	if (iswupper(ch))
904e5996857STim J. Robbins 		return(towlower(ch));
905e5996857STim J. Robbins 	else if (iswlower(ch))
906e5996857STim J. Robbins 		return(towupper(ch));
90758f0484fSRodney W. Grimes 	else			/* peculiar, but could happen */
90858f0484fSRodney W. Grimes 		return(ch);
90958f0484fSRodney W. Grimes }
91058f0484fSRodney W. Grimes 
91158f0484fSRodney W. Grimes /*
91258f0484fSRodney W. Grimes  - bothcases - emit a dualcase version of a two-case character
9138fb3f3f6SDavid E. O'Brien  == static void bothcases(struct parse *p, int ch);
91458f0484fSRodney W. Grimes  *
91558f0484fSRodney W. Grimes  * Boy, is this implementation ever a kludge...
91658f0484fSRodney W. Grimes  */
91758f0484fSRodney W. Grimes static void
91854a648d1SXin LI bothcases(struct parse *p, wint_t ch)
91958f0484fSRodney W. Grimes {
9208fb3f3f6SDavid E. O'Brien 	char *oldnext = p->next;
9218fb3f3f6SDavid E. O'Brien 	char *oldend = p->end;
922e5996857STim J. Robbins 	char bracket[3 + MB_LEN_MAX];
923e5996857STim J. Robbins 	size_t n;
924e5996857STim J. Robbins 	mbstate_t mbs;
92558f0484fSRodney W. Grimes 
92658f0484fSRodney W. Grimes 	assert(othercase(ch) != ch);	/* p_bracket() would recurse */
92758f0484fSRodney W. Grimes 	p->next = bracket;
928e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
929e5996857STim J. Robbins 	n = wcrtomb(bracket, ch, &mbs);
930e5996857STim J. Robbins 	assert(n != (size_t)-1);
931e5996857STim J. Robbins 	bracket[n] = ']';
932e5996857STim J. Robbins 	bracket[n + 1] = '\0';
933e5996857STim J. Robbins 	p->end = bracket+n+1;
93458f0484fSRodney W. Grimes 	p_bracket(p);
935e5996857STim J. Robbins 	assert(p->next == p->end);
93658f0484fSRodney W. Grimes 	p->next = oldnext;
93758f0484fSRodney W. Grimes 	p->end = oldend;
93858f0484fSRodney W. Grimes }
93958f0484fSRodney W. Grimes 
94058f0484fSRodney W. Grimes /*
94158f0484fSRodney W. Grimes  - ordinary - emit an ordinary character
9428fb3f3f6SDavid E. O'Brien  == static void ordinary(struct parse *p, int ch);
94358f0484fSRodney W. Grimes  */
94458f0484fSRodney W. Grimes static void
94554a648d1SXin LI ordinary(struct parse *p, wint_t ch)
94658f0484fSRodney W. Grimes {
947e5996857STim J. Robbins 	cset *cs;
94858f0484fSRodney W. Grimes 
949e5996857STim J. Robbins 	if ((p->g->cflags&REG_ICASE) && iswalpha(ch) && othercase(ch) != ch)
95058f0484fSRodney W. Grimes 		bothcases(p, ch);
951e5996857STim J. Robbins 	else if ((ch & OPDMASK) == ch)
952e5996857STim J. Robbins 		EMIT(OCHAR, ch);
953e5996857STim J. Robbins 	else {
954e5996857STim J. Robbins 		/*
955e5996857STim J. Robbins 		 * Kludge: character is too big to fit into an OCHAR operand.
956e5996857STim J. Robbins 		 * Emit a singleton set.
957e5996857STim J. Robbins 		 */
958e5996857STim J. Robbins 		if ((cs = allocset(p)) == NULL)
959e5996857STim J. Robbins 			return;
960e5996857STim J. Robbins 		CHadd(p, cs, ch);
961e5996857STim J. Robbins 		EMIT(OANYOF, (int)(cs - p->g->sets));
962e5996857STim J. Robbins 	}
96358f0484fSRodney W. Grimes }
96458f0484fSRodney W. Grimes 
96558f0484fSRodney W. Grimes /*
96658f0484fSRodney W. Grimes  - nonnewline - emit REG_NEWLINE version of OANY
9678fb3f3f6SDavid E. O'Brien  == static void nonnewline(struct parse *p);
96858f0484fSRodney W. Grimes  *
96958f0484fSRodney W. Grimes  * Boy, is this implementation ever a kludge...
97058f0484fSRodney W. Grimes  */
97158f0484fSRodney W. Grimes static void
97254a648d1SXin LI nonnewline(struct parse *p)
97358f0484fSRodney W. Grimes {
9748fb3f3f6SDavid E. O'Brien 	char *oldnext = p->next;
9758fb3f3f6SDavid E. O'Brien 	char *oldend = p->end;
97658f0484fSRodney W. Grimes 	char bracket[4];
97758f0484fSRodney W. Grimes 
97858f0484fSRodney W. Grimes 	p->next = bracket;
97958f0484fSRodney W. Grimes 	p->end = bracket+3;
98058f0484fSRodney W. Grimes 	bracket[0] = '^';
98158f0484fSRodney W. Grimes 	bracket[1] = '\n';
98258f0484fSRodney W. Grimes 	bracket[2] = ']';
98358f0484fSRodney W. Grimes 	bracket[3] = '\0';
98458f0484fSRodney W. Grimes 	p_bracket(p);
98558f0484fSRodney W. Grimes 	assert(p->next == bracket+3);
98658f0484fSRodney W. Grimes 	p->next = oldnext;
98758f0484fSRodney W. Grimes 	p->end = oldend;
98858f0484fSRodney W. Grimes }
98958f0484fSRodney W. Grimes 
99058f0484fSRodney W. Grimes /*
99158f0484fSRodney W. Grimes  - repeat - generate code for a bounded repetition, recursively if needed
9928fb3f3f6SDavid E. O'Brien  == static void repeat(struct parse *p, sopno start, int from, int to);
99358f0484fSRodney W. Grimes  */
99458f0484fSRodney W. Grimes static void
99554a648d1SXin LI repeat(struct parse *p,
99654a648d1SXin LI 	sopno start,		/* operand from here to end of strip */
99754a648d1SXin LI 	int from,		/* repeated from this number */
99854a648d1SXin LI 	int to)			/* to this number of times (maybe INFINITY) */
99958f0484fSRodney W. Grimes {
10008fb3f3f6SDavid E. O'Brien 	sopno finish = HERE();
100158f0484fSRodney W. Grimes #	define	N	2
100258f0484fSRodney W. Grimes #	define	INF	3
100358f0484fSRodney W. Grimes #	define	REP(f, t)	((f)*8 + (t))
100458f0484fSRodney W. Grimes #	define	MAP(n)	(((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
10058fb3f3f6SDavid E. O'Brien 	sopno copy;
100658f0484fSRodney W. Grimes 
100758f0484fSRodney W. Grimes 	if (p->error != 0)	/* head off possible runaway recursion */
100858f0484fSRodney W. Grimes 		return;
100958f0484fSRodney W. Grimes 
101058f0484fSRodney W. Grimes 	assert(from <= to);
101158f0484fSRodney W. Grimes 
101258f0484fSRodney W. Grimes 	switch (REP(MAP(from), MAP(to))) {
101358f0484fSRodney W. Grimes 	case REP(0, 0):			/* must be user doing this */
101458f0484fSRodney W. Grimes 		DROP(finish-start);	/* drop the operand */
101558f0484fSRodney W. Grimes 		break;
101658f0484fSRodney W. Grimes 	case REP(0, 1):			/* as x{1,1}? */
101758f0484fSRodney W. Grimes 	case REP(0, N):			/* as x{1,n}? */
101858f0484fSRodney W. Grimes 	case REP(0, INF):		/* as x{1,}? */
101958f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
102058f0484fSRodney W. Grimes 		INSERT(OCH_, start);		/* offset is wrong... */
102158f0484fSRodney W. Grimes 		repeat(p, start+1, 1, to);
102258f0484fSRodney W. Grimes 		ASTERN(OOR1, start);
102358f0484fSRodney W. Grimes 		AHEAD(start);			/* ... fix it */
102458f0484fSRodney W. Grimes 		EMIT(OOR2, 0);
102558f0484fSRodney W. Grimes 		AHEAD(THERE());
102658f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
102758f0484fSRodney W. Grimes 		break;
102858f0484fSRodney W. Grimes 	case REP(1, 1):			/* trivial case */
102958f0484fSRodney W. Grimes 		/* done */
103058f0484fSRodney W. Grimes 		break;
103158f0484fSRodney W. Grimes 	case REP(1, N):			/* as x?x{1,n-1} */
103258f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
103358f0484fSRodney W. Grimes 		INSERT(OCH_, start);
103458f0484fSRodney W. Grimes 		ASTERN(OOR1, start);
103558f0484fSRodney W. Grimes 		AHEAD(start);
103658f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset very wrong... */
103758f0484fSRodney W. Grimes 		AHEAD(THERE());			/* ...so fix it */
103858f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
103958f0484fSRodney W. Grimes 		copy = dupl(p, start+1, finish+1);
104058f0484fSRodney W. Grimes 		assert(copy == finish+4);
104158f0484fSRodney W. Grimes 		repeat(p, copy, 1, to-1);
104258f0484fSRodney W. Grimes 		break;
104358f0484fSRodney W. Grimes 	case REP(1, INF):		/* as x+ */
104458f0484fSRodney W. Grimes 		INSERT(OPLUS_, start);
104558f0484fSRodney W. Grimes 		ASTERN(O_PLUS, start);
104658f0484fSRodney W. Grimes 		break;
104758f0484fSRodney W. Grimes 	case REP(N, N):			/* as xx{m-1,n-1} */
104858f0484fSRodney W. Grimes 		copy = dupl(p, start, finish);
104958f0484fSRodney W. Grimes 		repeat(p, copy, from-1, to-1);
105058f0484fSRodney W. Grimes 		break;
105158f0484fSRodney W. Grimes 	case REP(N, INF):		/* as xx{n-1,INF} */
105258f0484fSRodney W. Grimes 		copy = dupl(p, start, finish);
105358f0484fSRodney W. Grimes 		repeat(p, copy, from-1, to);
105458f0484fSRodney W. Grimes 		break;
105558f0484fSRodney W. Grimes 	default:			/* "can't happen" */
105658f0484fSRodney W. Grimes 		SETERROR(REG_ASSERT);	/* just in case */
105758f0484fSRodney W. Grimes 		break;
105858f0484fSRodney W. Grimes 	}
105958f0484fSRodney W. Grimes }
106058f0484fSRodney W. Grimes 
106158f0484fSRodney W. Grimes /*
1062e5996857STim J. Robbins  - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1063e5996857STim J. Robbins  - character from the parse struct, signals a REG_ILLSEQ error if the
1064e5996857STim J. Robbins  - character can't be converted. Returns the number of bytes consumed.
1065e5996857STim J. Robbins  */
1066e5996857STim J. Robbins static wint_t
106754a648d1SXin LI wgetnext(struct parse *p)
1068e5996857STim J. Robbins {
1069e5996857STim J. Robbins 	mbstate_t mbs;
1070e5996857STim J. Robbins 	wchar_t wc;
1071e5996857STim J. Robbins 	size_t n;
1072e5996857STim J. Robbins 
1073e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1074e5996857STim J. Robbins 	n = mbrtowc(&wc, p->next, p->end - p->next, &mbs);
1075e5996857STim J. Robbins 	if (n == (size_t)-1 || n == (size_t)-2) {
1076e5996857STim J. Robbins 		SETERROR(REG_ILLSEQ);
1077e5996857STim J. Robbins 		return (0);
1078e5996857STim J. Robbins 	}
1079e5996857STim J. Robbins 	if (n == 0)
1080e5996857STim J. Robbins 		n = 1;
1081e5996857STim J. Robbins 	p->next += n;
1082e5996857STim J. Robbins 	return (wc);
1083e5996857STim J. Robbins }
1084e5996857STim J. Robbins 
1085e5996857STim J. Robbins /*
108658f0484fSRodney W. Grimes  - seterr - set an error condition
10878fb3f3f6SDavid E. O'Brien  == static int seterr(struct parse *p, int e);
108858f0484fSRodney W. Grimes  */
108958f0484fSRodney W. Grimes static int			/* useless but makes type checking happy */
109054a648d1SXin LI seterr(struct parse *p, int e)
109158f0484fSRodney W. Grimes {
109258f0484fSRodney W. Grimes 	if (p->error == 0)	/* keep earliest error condition */
109358f0484fSRodney W. Grimes 		p->error = e;
109458f0484fSRodney W. Grimes 	p->next = nuls;		/* try to bring things to a halt */
109558f0484fSRodney W. Grimes 	p->end = nuls;
109658f0484fSRodney W. Grimes 	return(0);		/* make the return value well-defined */
109758f0484fSRodney W. Grimes }
109858f0484fSRodney W. Grimes 
109958f0484fSRodney W. Grimes /*
110058f0484fSRodney W. Grimes  - allocset - allocate a set of characters for []
11018fb3f3f6SDavid E. O'Brien  == static cset *allocset(struct parse *p);
110258f0484fSRodney W. Grimes  */
110358f0484fSRodney W. Grimes static cset *
110454a648d1SXin LI allocset(struct parse *p)
110558f0484fSRodney W. Grimes {
1106e5996857STim J. Robbins 	cset *cs, *ncs;
110758f0484fSRodney W. Grimes 
1108e5996857STim J. Robbins 	ncs = realloc(p->g->sets, (p->g->ncsets + 1) * sizeof(*ncs));
1109e5996857STim J. Robbins 	if (ncs == NULL) {
111058f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
1111e5996857STim J. Robbins 		return (NULL);
111258f0484fSRodney W. Grimes 	}
1113e5996857STim J. Robbins 	p->g->sets = ncs;
1114e5996857STim J. Robbins 	cs = &p->g->sets[p->g->ncsets++];
1115e5996857STim J. Robbins 	memset(cs, 0, sizeof(*cs));
111658f0484fSRodney W. Grimes 
111758f0484fSRodney W. Grimes 	return(cs);
111858f0484fSRodney W. Grimes }
111958f0484fSRodney W. Grimes 
112058f0484fSRodney W. Grimes /*
112158f0484fSRodney W. Grimes  - freeset - free a now-unused set
11228fb3f3f6SDavid E. O'Brien  == static void freeset(struct parse *p, cset *cs);
112358f0484fSRodney W. Grimes  */
112458f0484fSRodney W. Grimes static void
112554a648d1SXin LI freeset(struct parse *p, cset *cs)
112658f0484fSRodney W. Grimes {
11278fb3f3f6SDavid E. O'Brien 	cset *top = &p->g->sets[p->g->ncsets];
112858f0484fSRodney W. Grimes 
1129e5996857STim J. Robbins 	free(cs->wides);
1130e5996857STim J. Robbins 	free(cs->ranges);
1131e5996857STim J. Robbins 	free(cs->types);
1132e5996857STim J. Robbins 	memset(cs, 0, sizeof(*cs));
113358f0484fSRodney W. Grimes 	if (cs == top-1)	/* recover only the easy case */
113458f0484fSRodney W. Grimes 		p->g->ncsets--;
113558f0484fSRodney W. Grimes }
113658f0484fSRodney W. Grimes 
113758f0484fSRodney W. Grimes /*
1138e5996857STim J. Robbins  - singleton - Determine whether a set contains only one character,
1139e5996857STim J. Robbins  - returning it if so, otherwise returning OUT.
114058f0484fSRodney W. Grimes  */
1141e5996857STim J. Robbins static wint_t
114254a648d1SXin LI singleton(cset *cs)
114358f0484fSRodney W. Grimes {
1144e5996857STim J. Robbins 	wint_t i, s, n;
114558f0484fSRodney W. Grimes 
1146e5996857STim J. Robbins 	for (i = n = 0; i < NC; i++)
1147e5996857STim J. Robbins 		if (CHIN(cs, i)) {
114858f0484fSRodney W. Grimes 			n++;
1149e5996857STim J. Robbins 			s = i;
1150e5996857STim J. Robbins 		}
1151e5996857STim J. Robbins 	if (n == 1)
1152e5996857STim J. Robbins 		return (s);
1153e5996857STim J. Robbins 	if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 &&
1154e5996857STim J. Robbins 	    cs->icase == 0)
1155e5996857STim J. Robbins 		return (cs->wides[0]);
1156e5996857STim J. Robbins 	/* Don't bother handling the other cases. */
1157e5996857STim J. Robbins 	return (OUT);
1158e5996857STim J. Robbins }
1159e5996857STim J. Robbins 
1160e5996857STim J. Robbins /*
1161e5996857STim J. Robbins  - CHadd - add character to character set.
1162e5996857STim J. Robbins  */
1163e5996857STim J. Robbins static void
116454a648d1SXin LI CHadd(struct parse *p, cset *cs, wint_t ch)
1165e5996857STim J. Robbins {
116633176f17STim J. Robbins 	wint_t nch, *newwides;
1167e5996857STim J. Robbins 	assert(ch >= 0);
116833176f17STim J. Robbins 	if (ch < NC)
1169e5996857STim J. Robbins 		cs->bmp[ch >> 3] |= 1 << (ch & 7);
117033176f17STim J. Robbins 	else {
1171e5996857STim J. Robbins 		newwides = realloc(cs->wides, (cs->nwides + 1) *
1172e5996857STim J. Robbins 		    sizeof(*cs->wides));
1173e5996857STim J. Robbins 		if (newwides == NULL) {
1174e5996857STim J. Robbins 			SETERROR(REG_ESPACE);
1175e5996857STim J. Robbins 			return;
1176e5996857STim J. Robbins 		}
1177e5996857STim J. Robbins 		cs->wides = newwides;
1178e5996857STim J. Robbins 		cs->wides[cs->nwides++] = ch;
1179e5996857STim J. Robbins 	}
118033176f17STim J. Robbins 	if (cs->icase) {
118133176f17STim J. Robbins 		if ((nch = towlower(ch)) < NC)
118233176f17STim J. Robbins 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
118333176f17STim J. Robbins 		if ((nch = towupper(ch)) < NC)
118433176f17STim J. Robbins 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
118533176f17STim J. Robbins 	}
1186e5996857STim J. Robbins }
1187e5996857STim J. Robbins 
1188e5996857STim J. Robbins /*
1189e5996857STim J. Robbins  - CHaddrange - add all characters in the range [min,max] to a character set.
1190e5996857STim J. Robbins  */
1191e5996857STim J. Robbins static void
119254a648d1SXin LI CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max)
1193e5996857STim J. Robbins {
1194e5996857STim J. Robbins 	crange *newranges;
1195e5996857STim J. Robbins 
1196e5996857STim J. Robbins 	for (; min < NC && min <= max; min++)
1197e5996857STim J. Robbins 		CHadd(p, cs, min);
1198e5996857STim J. Robbins 	if (min >= max)
1199e5996857STim J. Robbins 		return;
1200e5996857STim J. Robbins 	newranges = realloc(cs->ranges, (cs->nranges + 1) *
1201e5996857STim J. Robbins 	    sizeof(*cs->ranges));
1202e5996857STim J. Robbins 	if (newranges == NULL) {
1203e5996857STim J. Robbins 		SETERROR(REG_ESPACE);
1204e5996857STim J. Robbins 		return;
1205e5996857STim J. Robbins 	}
1206e5996857STim J. Robbins 	cs->ranges = newranges;
1207e5996857STim J. Robbins 	cs->ranges[cs->nranges].min = min;
1208e5996857STim J. Robbins 	cs->ranges[cs->nranges].min = max;
1209e5996857STim J. Robbins 	cs->nranges++;
1210e5996857STim J. Robbins }
1211e5996857STim J. Robbins 
1212e5996857STim J. Robbins /*
1213e5996857STim J. Robbins  - CHaddtype - add all characters of a certain type to a character set.
1214e5996857STim J. Robbins  */
1215e5996857STim J. Robbins static void
121654a648d1SXin LI CHaddtype(struct parse *p, cset *cs, wctype_t wct)
1217e5996857STim J. Robbins {
1218e5996857STim J. Robbins 	wint_t i;
1219e5996857STim J. Robbins 	wctype_t *newtypes;
1220e5996857STim J. Robbins 
122133176f17STim J. Robbins 	for (i = 0; i < NC; i++)
1222e5996857STim J. Robbins 		if (iswctype(i, wct))
1223e5996857STim J. Robbins 			CHadd(p, cs, i);
1224e5996857STim J. Robbins 	newtypes = realloc(cs->types, (cs->ntypes + 1) *
1225e5996857STim J. Robbins 	    sizeof(*cs->types));
1226e5996857STim J. Robbins 	if (newtypes == NULL) {
1227e5996857STim J. Robbins 		SETERROR(REG_ESPACE);
1228e5996857STim J. Robbins 		return;
1229e5996857STim J. Robbins 	}
1230e5996857STim J. Robbins 	cs->types = newtypes;
1231e5996857STim J. Robbins 	cs->types[cs->ntypes++] = wct;
123258f0484fSRodney W. Grimes }
123358f0484fSRodney W. Grimes 
123458f0484fSRodney W. Grimes /*
123558f0484fSRodney W. Grimes  - dupl - emit a duplicate of a bunch of sops
12368fb3f3f6SDavid E. O'Brien  == static sopno dupl(struct parse *p, sopno start, sopno finish);
123758f0484fSRodney W. Grimes  */
123858f0484fSRodney W. Grimes static sopno			/* start of duplicate */
123954a648d1SXin LI dupl(struct parse *p,
124054a648d1SXin LI 	sopno start,		/* from here */
124154a648d1SXin LI 	sopno finish)		/* to this less one */
124258f0484fSRodney W. Grimes {
12438fb3f3f6SDavid E. O'Brien 	sopno ret = HERE();
12448fb3f3f6SDavid E. O'Brien 	sopno len = finish - start;
124558f0484fSRodney W. Grimes 
124658f0484fSRodney W. Grimes 	assert(finish >= start);
124758f0484fSRodney W. Grimes 	if (len == 0)
124858f0484fSRodney W. Grimes 		return(ret);
124958f0484fSRodney W. Grimes 	enlarge(p, p->ssize + len);	/* this many unexpected additions */
125058f0484fSRodney W. Grimes 	assert(p->ssize >= p->slen + len);
125158f0484fSRodney W. Grimes 	(void) memcpy((char *)(p->strip + p->slen),
125258f0484fSRodney W. Grimes 		(char *)(p->strip + start), (size_t)len*sizeof(sop));
125358f0484fSRodney W. Grimes 	p->slen += len;
125458f0484fSRodney W. Grimes 	return(ret);
125558f0484fSRodney W. Grimes }
125658f0484fSRodney W. Grimes 
125758f0484fSRodney W. Grimes /*
125858f0484fSRodney W. Grimes  - doemit - emit a strip operator
12598fb3f3f6SDavid E. O'Brien  == static void doemit(struct parse *p, sop op, size_t opnd);
126058f0484fSRodney W. Grimes  *
126158f0484fSRodney W. Grimes  * It might seem better to implement this as a macro with a function as
126258f0484fSRodney W. Grimes  * hard-case backup, but it's just too big and messy unless there are
126358f0484fSRodney W. Grimes  * some changes to the data structures.  Maybe later.
126458f0484fSRodney W. Grimes  */
126558f0484fSRodney W. Grimes static void
126654a648d1SXin LI doemit(struct parse *p, sop op, size_t opnd)
126758f0484fSRodney W. Grimes {
126858f0484fSRodney W. Grimes 	/* avoid making error situations worse */
126958f0484fSRodney W. Grimes 	if (p->error != 0)
127058f0484fSRodney W. Grimes 		return;
127158f0484fSRodney W. Grimes 
127258f0484fSRodney W. Grimes 	/* deal with oversize operands ("can't happen", more or less) */
127358f0484fSRodney W. Grimes 	assert(opnd < 1<<OPSHIFT);
127458f0484fSRodney W. Grimes 
127558f0484fSRodney W. Grimes 	/* deal with undersized strip */
127658f0484fSRodney W. Grimes 	if (p->slen >= p->ssize)
127758f0484fSRodney W. Grimes 		enlarge(p, (p->ssize+1) / 2 * 3);	/* +50% */
127858f0484fSRodney W. Grimes 	assert(p->slen < p->ssize);
127958f0484fSRodney W. Grimes 
128058f0484fSRodney W. Grimes 	/* finally, it's all reduced to the easy case */
128158f0484fSRodney W. Grimes 	p->strip[p->slen++] = SOP(op, opnd);
128258f0484fSRodney W. Grimes }
128358f0484fSRodney W. Grimes 
128458f0484fSRodney W. Grimes /*
128558f0484fSRodney W. Grimes  - doinsert - insert a sop into the strip
12868fb3f3f6SDavid E. O'Brien  == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
128758f0484fSRodney W. Grimes  */
128858f0484fSRodney W. Grimes static void
128954a648d1SXin LI doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
129058f0484fSRodney W. Grimes {
12918fb3f3f6SDavid E. O'Brien 	sopno sn;
12928fb3f3f6SDavid E. O'Brien 	sop s;
12938fb3f3f6SDavid E. O'Brien 	int i;
129458f0484fSRodney W. Grimes 
129558f0484fSRodney W. Grimes 	/* avoid making error situations worse */
129658f0484fSRodney W. Grimes 	if (p->error != 0)
129758f0484fSRodney W. Grimes 		return;
129858f0484fSRodney W. Grimes 
129958f0484fSRodney W. Grimes 	sn = HERE();
130058f0484fSRodney W. Grimes 	EMIT(op, opnd);		/* do checks, ensure space */
130158f0484fSRodney W. Grimes 	assert(HERE() == sn+1);
130258f0484fSRodney W. Grimes 	s = p->strip[sn];
130358f0484fSRodney W. Grimes 
130458f0484fSRodney W. Grimes 	/* adjust paren pointers */
130558f0484fSRodney W. Grimes 	assert(pos > 0);
130658f0484fSRodney W. Grimes 	for (i = 1; i < NPAREN; i++) {
130758f0484fSRodney W. Grimes 		if (p->pbegin[i] >= pos) {
130858f0484fSRodney W. Grimes 			p->pbegin[i]++;
130958f0484fSRodney W. Grimes 		}
131058f0484fSRodney W. Grimes 		if (p->pend[i] >= pos) {
131158f0484fSRodney W. Grimes 			p->pend[i]++;
131258f0484fSRodney W. Grimes 		}
131358f0484fSRodney W. Grimes 	}
131458f0484fSRodney W. Grimes 
131558f0484fSRodney W. Grimes 	memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
131658f0484fSRodney W. Grimes 						(HERE()-pos-1)*sizeof(sop));
131758f0484fSRodney W. Grimes 	p->strip[pos] = s;
131858f0484fSRodney W. Grimes }
131958f0484fSRodney W. Grimes 
132058f0484fSRodney W. Grimes /*
132158f0484fSRodney W. Grimes  - dofwd - complete a forward reference
13228fb3f3f6SDavid E. O'Brien  == static void dofwd(struct parse *p, sopno pos, sop value);
132358f0484fSRodney W. Grimes  */
132458f0484fSRodney W. Grimes static void
132554a648d1SXin LI dofwd(struct parse *p, sopno pos, sop value)
132658f0484fSRodney W. Grimes {
132758f0484fSRodney W. Grimes 	/* avoid making error situations worse */
132858f0484fSRodney W. Grimes 	if (p->error != 0)
132958f0484fSRodney W. Grimes 		return;
133058f0484fSRodney W. Grimes 
133158f0484fSRodney W. Grimes 	assert(value < 1<<OPSHIFT);
133258f0484fSRodney W. Grimes 	p->strip[pos] = OP(p->strip[pos]) | value;
133358f0484fSRodney W. Grimes }
133458f0484fSRodney W. Grimes 
133558f0484fSRodney W. Grimes /*
133658f0484fSRodney W. Grimes  - enlarge - enlarge the strip
13378fb3f3f6SDavid E. O'Brien  == static void enlarge(struct parse *p, sopno size);
133858f0484fSRodney W. Grimes  */
133958f0484fSRodney W. Grimes static void
134054a648d1SXin LI enlarge(struct parse *p, sopno size)
134158f0484fSRodney W. Grimes {
13428fb3f3f6SDavid E. O'Brien 	sop *sp;
134358f0484fSRodney W. Grimes 
134458f0484fSRodney W. Grimes 	if (p->ssize >= size)
134558f0484fSRodney W. Grimes 		return;
134658f0484fSRodney W. Grimes 
134758f0484fSRodney W. Grimes 	sp = (sop *)realloc(p->strip, size*sizeof(sop));
134858f0484fSRodney W. Grimes 	if (sp == NULL) {
134958f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
135058f0484fSRodney W. Grimes 		return;
135158f0484fSRodney W. Grimes 	}
135258f0484fSRodney W. Grimes 	p->strip = sp;
135358f0484fSRodney W. Grimes 	p->ssize = size;
135458f0484fSRodney W. Grimes }
135558f0484fSRodney W. Grimes 
135658f0484fSRodney W. Grimes /*
135758f0484fSRodney W. Grimes  - stripsnug - compact the strip
13588fb3f3f6SDavid E. O'Brien  == static void stripsnug(struct parse *p, struct re_guts *g);
135958f0484fSRodney W. Grimes  */
136058f0484fSRodney W. Grimes static void
136154a648d1SXin LI stripsnug(struct parse *p, struct re_guts *g)
136258f0484fSRodney W. Grimes {
136358f0484fSRodney W. Grimes 	g->nstates = p->slen;
136458f0484fSRodney W. Grimes 	g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
136558f0484fSRodney W. Grimes 	if (g->strip == NULL) {
136658f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
136758f0484fSRodney W. Grimes 		g->strip = p->strip;
136858f0484fSRodney W. Grimes 	}
136958f0484fSRodney W. Grimes }
137058f0484fSRodney W. Grimes 
137158f0484fSRodney W. Grimes /*
137258f0484fSRodney W. Grimes  - findmust - fill in must and mlen with longest mandatory literal string
13738fb3f3f6SDavid E. O'Brien  == static void findmust(struct parse *p, struct re_guts *g);
137458f0484fSRodney W. Grimes  *
137558f0484fSRodney W. Grimes  * This algorithm could do fancy things like analyzing the operands of |
137658f0484fSRodney W. Grimes  * for common subsequences.  Someday.  This code is simple and finds most
137758f0484fSRodney W. Grimes  * of the interesting cases.
137858f0484fSRodney W. Grimes  *
137958f0484fSRodney W. Grimes  * Note that must and mlen got initialized during setup.
138058f0484fSRodney W. Grimes  */
138158f0484fSRodney W. Grimes static void
138254a648d1SXin LI findmust(struct parse *p, struct re_guts *g)
138358f0484fSRodney W. Grimes {
13848fb3f3f6SDavid E. O'Brien 	sop *scan;
138558f0484fSRodney W. Grimes 	sop *start;
13868fb3f3f6SDavid E. O'Brien 	sop *newstart;
13878fb3f3f6SDavid E. O'Brien 	sopno newlen;
13888fb3f3f6SDavid E. O'Brien 	sop s;
13898fb3f3f6SDavid E. O'Brien 	char *cp;
1390e6a886d8SDaniel C. Sobral 	int offset;
1391e5996857STim J. Robbins 	char buf[MB_LEN_MAX];
1392e5996857STim J. Robbins 	size_t clen;
1393e5996857STim J. Robbins 	mbstate_t mbs;
139458f0484fSRodney W. Grimes 
139558f0484fSRodney W. Grimes 	/* avoid making error situations worse */
139658f0484fSRodney W. Grimes 	if (p->error != 0)
139758f0484fSRodney W. Grimes 		return;
139858f0484fSRodney W. Grimes 
1399e5996857STim J. Robbins 	/*
1400e5996857STim J. Robbins 	 * It's not generally safe to do a ``char'' substring search on
1401e5996857STim J. Robbins 	 * multibyte character strings, but it's safe for at least
1402e5996857STim J. Robbins 	 * UTF-8 (see RFC 3629).
1403e5996857STim J. Robbins 	 */
1404e5996857STim J. Robbins 	if (MB_CUR_MAX > 1 &&
1405e5996857STim J. Robbins 	    strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
1406e5996857STim J. Robbins 		return;
1407e5996857STim J. Robbins 
140858f0484fSRodney W. Grimes 	/* find the longest OCHAR sequence in strip */
140958f0484fSRodney W. Grimes 	newlen = 0;
1410e6a886d8SDaniel C. Sobral 	offset = 0;
1411e6a886d8SDaniel C. Sobral 	g->moffset = 0;
141258f0484fSRodney W. Grimes 	scan = g->strip + 1;
141358f0484fSRodney W. Grimes 	do {
141458f0484fSRodney W. Grimes 		s = *scan++;
141558f0484fSRodney W. Grimes 		switch (OP(s)) {
141658f0484fSRodney W. Grimes 		case OCHAR:		/* sequence member */
1417e5996857STim J. Robbins 			if (newlen == 0) {		/* new sequence */
1418e5996857STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
141958f0484fSRodney W. Grimes 				newstart = scan - 1;
1420e5996857STim J. Robbins 			}
1421e5996857STim J. Robbins 			clen = wcrtomb(buf, OPND(s), &mbs);
1422e5996857STim J. Robbins 			if (clen == (size_t)-1)
1423e5996857STim J. Robbins 				goto toohard;
1424e5996857STim J. Robbins 			newlen += clen;
142558f0484fSRodney W. Grimes 			break;
142658f0484fSRodney W. Grimes 		case OPLUS_:		/* things that don't break one */
142758f0484fSRodney W. Grimes 		case OLPAREN:
142858f0484fSRodney W. Grimes 		case ORPAREN:
142958f0484fSRodney W. Grimes 			break;
143058f0484fSRodney W. Grimes 		case OQUEST_:		/* things that must be skipped */
143158f0484fSRodney W. Grimes 		case OCH_:
143280f36366STim J. Robbins 			offset = altoffset(scan, offset);
143358f0484fSRodney W. Grimes 			scan--;
143458f0484fSRodney W. Grimes 			do {
143558f0484fSRodney W. Grimes 				scan += OPND(s);
143658f0484fSRodney W. Grimes 				s = *scan;
143758f0484fSRodney W. Grimes 				/* assert() interferes w debug printouts */
143858f0484fSRodney W. Grimes 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
143958f0484fSRodney W. Grimes 							OP(s) != OOR2) {
144058f0484fSRodney W. Grimes 					g->iflags |= BAD;
144158f0484fSRodney W. Grimes 					return;
144258f0484fSRodney W. Grimes 				}
144358f0484fSRodney W. Grimes 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
14447fed38d0SPhilippe Charnier 			/* FALLTHROUGH */
1445e6a886d8SDaniel C. Sobral 		case OBOW:		/* things that break a sequence */
1446e6a886d8SDaniel C. Sobral 		case OEOW:
1447e6a886d8SDaniel C. Sobral 		case OBOL:
1448e6a886d8SDaniel C. Sobral 		case OEOL:
1449e6a886d8SDaniel C. Sobral 		case O_QUEST:
1450e6a886d8SDaniel C. Sobral 		case O_CH:
1451e6a886d8SDaniel C. Sobral 		case OEND:
145258f0484fSRodney W. Grimes 			if (newlen > g->mlen) {		/* ends one */
145358f0484fSRodney W. Grimes 				start = newstart;
145458f0484fSRodney W. Grimes 				g->mlen = newlen;
1455e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1456e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1457e6a886d8SDaniel C. Sobral 					offset = newlen;
1458e6a886d8SDaniel C. Sobral 				} else
1459e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1460e6a886d8SDaniel C. Sobral 			} else {
1461e6a886d8SDaniel C. Sobral 				if (offset > -1)
1462e6a886d8SDaniel C. Sobral 					offset += newlen;
146358f0484fSRodney W. Grimes 			}
146458f0484fSRodney W. Grimes 			newlen = 0;
146558f0484fSRodney W. Grimes 			break;
1466e6a886d8SDaniel C. Sobral 		case OANY:
1467e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1468e6a886d8SDaniel C. Sobral 				start = newstart;
1469e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1470e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1471e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1472e6a886d8SDaniel C. Sobral 					offset = newlen;
1473e6a886d8SDaniel C. Sobral 				} else
1474e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1475e6a886d8SDaniel C. Sobral 			} else {
1476e6a886d8SDaniel C. Sobral 				if (offset > -1)
1477e6a886d8SDaniel C. Sobral 					offset += newlen;
1478e6a886d8SDaniel C. Sobral 			}
1479e6a886d8SDaniel C. Sobral 			if (offset > -1)
1480e6a886d8SDaniel C. Sobral 				offset++;
1481e6a886d8SDaniel C. Sobral 			newlen = 0;
1482e6a886d8SDaniel C. Sobral 			break;
1483e6a886d8SDaniel C. Sobral 		case OANYOF:		/* may or may not invalidate offset */
1484e6a886d8SDaniel C. Sobral 			/* First, everything as OANY */
1485e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1486e6a886d8SDaniel C. Sobral 				start = newstart;
1487e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1488e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1489e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1490e6a886d8SDaniel C. Sobral 					offset = newlen;
1491e6a886d8SDaniel C. Sobral 				} else
1492e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1493e6a886d8SDaniel C. Sobral 			} else {
1494e6a886d8SDaniel C. Sobral 				if (offset > -1)
1495e6a886d8SDaniel C. Sobral 					offset += newlen;
1496e6a886d8SDaniel C. Sobral 			}
1497e6a886d8SDaniel C. Sobral 			if (offset > -1)
1498e6a886d8SDaniel C. Sobral 				offset++;
1499e6a886d8SDaniel C. Sobral 			newlen = 0;
1500e6a886d8SDaniel C. Sobral 			break;
1501e5996857STim J. Robbins 		toohard:
1502e6a886d8SDaniel C. Sobral 		default:
1503e6a886d8SDaniel C. Sobral 			/* Anything here makes it impossible or too hard
1504e6a886d8SDaniel C. Sobral 			 * to calculate the offset -- so we give up;
1505e6a886d8SDaniel C. Sobral 			 * save the last known good offset, in case the
1506e6a886d8SDaniel C. Sobral 			 * must sequence doesn't occur later.
1507e6a886d8SDaniel C. Sobral 			 */
1508e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1509e6a886d8SDaniel C. Sobral 				start = newstart;
1510e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1511e6a886d8SDaniel C. Sobral 				if (offset > -1)
1512e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1513e6a886d8SDaniel C. Sobral 				else
1514e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1515e6a886d8SDaniel C. Sobral 			}
1516e6a886d8SDaniel C. Sobral 			offset = -1;
1517e6a886d8SDaniel C. Sobral 			newlen = 0;
1518e6a886d8SDaniel C. Sobral 			break;
151958f0484fSRodney W. Grimes 		}
152058f0484fSRodney W. Grimes 	} while (OP(s) != OEND);
152158f0484fSRodney W. Grimes 
1522e6a886d8SDaniel C. Sobral 	if (g->mlen == 0) {		/* there isn't one */
1523e6a886d8SDaniel C. Sobral 		g->moffset = -1;
152458f0484fSRodney W. Grimes 		return;
1525e6a886d8SDaniel C. Sobral 	}
152658f0484fSRodney W. Grimes 
152758f0484fSRodney W. Grimes 	/* turn it into a character string */
152858f0484fSRodney W. Grimes 	g->must = malloc((size_t)g->mlen + 1);
152958f0484fSRodney W. Grimes 	if (g->must == NULL) {		/* argh; just forget it */
153058f0484fSRodney W. Grimes 		g->mlen = 0;
1531e6a886d8SDaniel C. Sobral 		g->moffset = -1;
153258f0484fSRodney W. Grimes 		return;
153358f0484fSRodney W. Grimes 	}
153458f0484fSRodney W. Grimes 	cp = g->must;
153558f0484fSRodney W. Grimes 	scan = start;
1536e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1537e5996857STim J. Robbins 	while (cp < g->must + g->mlen) {
153858f0484fSRodney W. Grimes 		while (OP(s = *scan++) != OCHAR)
153958f0484fSRodney W. Grimes 			continue;
1540e5996857STim J. Robbins 		clen = wcrtomb(cp, OPND(s), &mbs);
1541e5996857STim J. Robbins 		assert(clen != (size_t)-1);
1542e5996857STim J. Robbins 		cp += clen;
154358f0484fSRodney W. Grimes 	}
154458f0484fSRodney W. Grimes 	assert(cp == g->must + g->mlen);
154558f0484fSRodney W. Grimes 	*cp++ = '\0';		/* just on general principles */
154658f0484fSRodney W. Grimes }
154758f0484fSRodney W. Grimes 
154858f0484fSRodney W. Grimes /*
1549e6a886d8SDaniel C. Sobral  - altoffset - choose biggest offset among multiple choices
155080f36366STim J. Robbins  == static int altoffset(sop *scan, int offset);
1551e6a886d8SDaniel C. Sobral  *
1552e6a886d8SDaniel C. Sobral  * Compute, recursively if necessary, the largest offset among multiple
1553e6a886d8SDaniel C. Sobral  * re paths.
1554e6a886d8SDaniel C. Sobral  */
1555e6a886d8SDaniel C. Sobral static int
155654a648d1SXin LI altoffset(sop *scan, int offset)
1557e6a886d8SDaniel C. Sobral {
1558e6a886d8SDaniel C. Sobral 	int largest;
1559e6a886d8SDaniel C. Sobral 	int try;
1560e6a886d8SDaniel C. Sobral 	sop s;
1561e6a886d8SDaniel C. Sobral 
1562e6a886d8SDaniel C. Sobral 	/* If we gave up already on offsets, return */
1563e6a886d8SDaniel C. Sobral 	if (offset == -1)
1564e6a886d8SDaniel C. Sobral 		return -1;
1565e6a886d8SDaniel C. Sobral 
1566e6a886d8SDaniel C. Sobral 	largest = 0;
1567e6a886d8SDaniel C. Sobral 	try = 0;
1568e6a886d8SDaniel C. Sobral 	s = *scan++;
1569e6a886d8SDaniel C. Sobral 	while (OP(s) != O_QUEST && OP(s) != O_CH) {
1570e6a886d8SDaniel C. Sobral 		switch (OP(s)) {
1571e6a886d8SDaniel C. Sobral 		case OOR1:
1572e6a886d8SDaniel C. Sobral 			if (try > largest)
1573e6a886d8SDaniel C. Sobral 				largest = try;
1574e6a886d8SDaniel C. Sobral 			try = 0;
1575e6a886d8SDaniel C. Sobral 			break;
1576e6a886d8SDaniel C. Sobral 		case OQUEST_:
1577e6a886d8SDaniel C. Sobral 		case OCH_:
157880f36366STim J. Robbins 			try = altoffset(scan, try);
1579e6a886d8SDaniel C. Sobral 			if (try == -1)
1580e6a886d8SDaniel C. Sobral 				return -1;
1581e6a886d8SDaniel C. Sobral 			scan--;
1582e6a886d8SDaniel C. Sobral 			do {
1583e6a886d8SDaniel C. Sobral 				scan += OPND(s);
1584e6a886d8SDaniel C. Sobral 				s = *scan;
1585e6a886d8SDaniel C. Sobral 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
1586e6a886d8SDaniel C. Sobral 							OP(s) != OOR2)
1587e6a886d8SDaniel C. Sobral 					return -1;
1588e6a886d8SDaniel C. Sobral 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
15898f9e434fSDaniel C. Sobral 			/* We must skip to the next position, or we'll
15908f9e434fSDaniel C. Sobral 			 * leave altoffset() too early.
15918f9e434fSDaniel C. Sobral 			 */
15928f9e434fSDaniel C. Sobral 			scan++;
1593e6a886d8SDaniel C. Sobral 			break;
1594e6a886d8SDaniel C. Sobral 		case OANYOF:
1595e6a886d8SDaniel C. Sobral 		case OCHAR:
1596e6a886d8SDaniel C. Sobral 		case OANY:
1597e6a886d8SDaniel C. Sobral 			try++;
1598e6a886d8SDaniel C. Sobral 		case OBOW:
1599e6a886d8SDaniel C. Sobral 		case OEOW:
1600e6a886d8SDaniel C. Sobral 		case OLPAREN:
1601e6a886d8SDaniel C. Sobral 		case ORPAREN:
1602e6a886d8SDaniel C. Sobral 		case OOR2:
1603e6a886d8SDaniel C. Sobral 			break;
1604e6a886d8SDaniel C. Sobral 		default:
1605e6a886d8SDaniel C. Sobral 			try = -1;
1606e6a886d8SDaniel C. Sobral 			break;
1607e6a886d8SDaniel C. Sobral 		}
1608e6a886d8SDaniel C. Sobral 		if (try == -1)
1609e6a886d8SDaniel C. Sobral 			return -1;
1610e6a886d8SDaniel C. Sobral 		s = *scan++;
1611e6a886d8SDaniel C. Sobral 	}
1612e6a886d8SDaniel C. Sobral 
1613e6a886d8SDaniel C. Sobral 	if (try > largest)
1614e6a886d8SDaniel C. Sobral 		largest = try;
1615e6a886d8SDaniel C. Sobral 
1616e6a886d8SDaniel C. Sobral 	return largest+offset;
1617e6a886d8SDaniel C. Sobral }
1618e6a886d8SDaniel C. Sobral 
1619e6a886d8SDaniel C. Sobral /*
16206049d9f0SDaniel C. Sobral  - computejumps - compute char jumps for BM scan
16218fb3f3f6SDavid E. O'Brien  == static void computejumps(struct parse *p, struct re_guts *g);
16226049d9f0SDaniel C. Sobral  *
16236049d9f0SDaniel C. Sobral  * This algorithm assumes g->must exists and is has size greater than
16246049d9f0SDaniel C. Sobral  * zero. It's based on the algorithm found on Computer Algorithms by
16256049d9f0SDaniel C. Sobral  * Sara Baase.
16266049d9f0SDaniel C. Sobral  *
16276049d9f0SDaniel C. Sobral  * A char jump is the number of characters one needs to jump based on
16286049d9f0SDaniel C. Sobral  * the value of the character from the text that was mismatched.
16296049d9f0SDaniel C. Sobral  */
16306049d9f0SDaniel C. Sobral static void
163154a648d1SXin LI computejumps(struct parse *p, struct re_guts *g)
16326049d9f0SDaniel C. Sobral {
16336049d9f0SDaniel C. Sobral 	int ch;
16346049d9f0SDaniel C. Sobral 	int mindex;
16356049d9f0SDaniel C. Sobral 
16366049d9f0SDaniel C. Sobral 	/* Avoid making errors worse */
16376049d9f0SDaniel C. Sobral 	if (p->error != 0)
16386049d9f0SDaniel C. Sobral 		return;
16396049d9f0SDaniel C. Sobral 
1640517bffcaSDaniel C. Sobral 	g->charjump = (int*) malloc((NC + 1) * sizeof(int));
16416049d9f0SDaniel C. Sobral 	if (g->charjump == NULL)	/* Not a fatal error */
16426049d9f0SDaniel C. Sobral 		return;
1643c5e125bbSDaniel C. Sobral 	/* Adjust for signed chars, if necessary */
1644c5e125bbSDaniel C. Sobral 	g->charjump = &g->charjump[-(CHAR_MIN)];
16456049d9f0SDaniel C. Sobral 
16466049d9f0SDaniel C. Sobral 	/* If the character does not exist in the pattern, the jump
16476049d9f0SDaniel C. Sobral 	 * is equal to the number of characters in the pattern.
16486049d9f0SDaniel C. Sobral 	 */
1649c5e125bbSDaniel C. Sobral 	for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
16506049d9f0SDaniel C. Sobral 		g->charjump[ch] = g->mlen;
16516049d9f0SDaniel C. Sobral 
16526049d9f0SDaniel C. Sobral 	/* If the character does exist, compute the jump that would
16536049d9f0SDaniel C. Sobral 	 * take us to the last character in the pattern equal to it
16546049d9f0SDaniel C. Sobral 	 * (notice that we match right to left, so that last character
16556049d9f0SDaniel C. Sobral 	 * is the first one that would be matched).
16566049d9f0SDaniel C. Sobral 	 */
16576049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex < g->mlen; mindex++)
1658e0554a53SJacques Vidrine 		g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1;
16596049d9f0SDaniel C. Sobral }
16606049d9f0SDaniel C. Sobral 
16616049d9f0SDaniel C. Sobral /*
16626049d9f0SDaniel C. Sobral  - computematchjumps - compute match jumps for BM scan
16638fb3f3f6SDavid E. O'Brien  == static void computematchjumps(struct parse *p, struct re_guts *g);
16646049d9f0SDaniel C. Sobral  *
16656049d9f0SDaniel C. Sobral  * This algorithm assumes g->must exists and is has size greater than
16666049d9f0SDaniel C. Sobral  * zero. It's based on the algorithm found on Computer Algorithms by
16676049d9f0SDaniel C. Sobral  * Sara Baase.
16686049d9f0SDaniel C. Sobral  *
16696049d9f0SDaniel C. Sobral  * A match jump is the number of characters one needs to advance based
16706049d9f0SDaniel C. Sobral  * on the already-matched suffix.
16716049d9f0SDaniel C. Sobral  * Notice that all values here are minus (g->mlen-1), because of the way
16726049d9f0SDaniel C. Sobral  * the search algorithm works.
16736049d9f0SDaniel C. Sobral  */
16746049d9f0SDaniel C. Sobral static void
167554a648d1SXin LI computematchjumps(struct parse *p, struct re_guts *g)
16766049d9f0SDaniel C. Sobral {
16776049d9f0SDaniel C. Sobral 	int mindex;		/* General "must" iterator */
16786049d9f0SDaniel C. Sobral 	int suffix;		/* Keeps track of matching suffix */
16796049d9f0SDaniel C. Sobral 	int ssuffix;		/* Keeps track of suffixes' suffix */
16806049d9f0SDaniel C. Sobral 	int* pmatches;		/* pmatches[k] points to the next i
16816049d9f0SDaniel C. Sobral 				 * such that i+1...mlen is a substring
16826049d9f0SDaniel C. Sobral 				 * of k+1...k+mlen-i-1
16836049d9f0SDaniel C. Sobral 				 */
16846049d9f0SDaniel C. Sobral 
16856049d9f0SDaniel C. Sobral 	/* Avoid making errors worse */
16866049d9f0SDaniel C. Sobral 	if (p->error != 0)
16876049d9f0SDaniel C. Sobral 		return;
16886049d9f0SDaniel C. Sobral 
1689517bffcaSDaniel C. Sobral 	pmatches = (int*) malloc(g->mlen * sizeof(unsigned int));
16906049d9f0SDaniel C. Sobral 	if (pmatches == NULL) {
16916049d9f0SDaniel C. Sobral 		g->matchjump = NULL;
16926049d9f0SDaniel C. Sobral 		return;
16936049d9f0SDaniel C. Sobral 	}
16946049d9f0SDaniel C. Sobral 
1695517bffcaSDaniel C. Sobral 	g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int));
16966049d9f0SDaniel C. Sobral 	if (g->matchjump == NULL)	/* Not a fatal error */
16976049d9f0SDaniel C. Sobral 		return;
16986049d9f0SDaniel C. Sobral 
16996049d9f0SDaniel C. Sobral 	/* Set maximum possible jump for each character in the pattern */
17006049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex < g->mlen; mindex++)
17016049d9f0SDaniel C. Sobral 		g->matchjump[mindex] = 2*g->mlen - mindex - 1;
17026049d9f0SDaniel C. Sobral 
17036049d9f0SDaniel C. Sobral 	/* Compute pmatches[] */
17046049d9f0SDaniel C. Sobral 	for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
17056049d9f0SDaniel C. Sobral 	    mindex--, suffix--) {
17066049d9f0SDaniel C. Sobral 		pmatches[mindex] = suffix;
17076049d9f0SDaniel C. Sobral 
17086049d9f0SDaniel C. Sobral 		/* If a mismatch is found, interrupting the substring,
17096049d9f0SDaniel C. Sobral 		 * compute the matchjump for that position. If no
17106049d9f0SDaniel C. Sobral 		 * mismatch is found, then a text substring mismatched
17116049d9f0SDaniel C. Sobral 		 * against the suffix will also mismatch against the
17126049d9f0SDaniel C. Sobral 		 * substring.
17136049d9f0SDaniel C. Sobral 		 */
17146049d9f0SDaniel C. Sobral 		while (suffix < g->mlen
17156049d9f0SDaniel C. Sobral 		    && g->must[mindex] != g->must[suffix]) {
17166049d9f0SDaniel C. Sobral 			g->matchjump[suffix] = MIN(g->matchjump[suffix],
17176049d9f0SDaniel C. Sobral 			    g->mlen - mindex - 1);
17186049d9f0SDaniel C. Sobral 			suffix = pmatches[suffix];
17196049d9f0SDaniel C. Sobral 		}
17206049d9f0SDaniel C. Sobral 	}
17216049d9f0SDaniel C. Sobral 
17226049d9f0SDaniel C. Sobral 	/* Compute the matchjump up to the last substring found to jump
17236049d9f0SDaniel C. Sobral 	 * to the beginning of the largest must pattern prefix matching
17246049d9f0SDaniel C. Sobral 	 * it's own suffix.
17256049d9f0SDaniel C. Sobral 	 */
17266049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex <= suffix; mindex++)
17276049d9f0SDaniel C. Sobral 		g->matchjump[mindex] = MIN(g->matchjump[mindex],
17286049d9f0SDaniel C. Sobral 		    g->mlen + suffix - mindex);
17296049d9f0SDaniel C. Sobral 
17306049d9f0SDaniel C. Sobral         ssuffix = pmatches[suffix];
17316049d9f0SDaniel C. Sobral         while (suffix < g->mlen) {
17329868274bSDaniel C. Sobral                 while (suffix <= ssuffix && suffix < g->mlen) {
17336049d9f0SDaniel C. Sobral                         g->matchjump[suffix] = MIN(g->matchjump[suffix],
17346049d9f0SDaniel C. Sobral 			    g->mlen + ssuffix - suffix);
17356049d9f0SDaniel C. Sobral                         suffix++;
17366049d9f0SDaniel C. Sobral                 }
17378e2f75b8SDaniel C. Sobral 		if (suffix < g->mlen)
17386049d9f0SDaniel C. Sobral                 	ssuffix = pmatches[ssuffix];
17396049d9f0SDaniel C. Sobral         }
17406049d9f0SDaniel C. Sobral 
17416049d9f0SDaniel C. Sobral 	free(pmatches);
17426049d9f0SDaniel C. Sobral }
17436049d9f0SDaniel C. Sobral 
17446049d9f0SDaniel C. Sobral /*
174558f0484fSRodney W. Grimes  - pluscount - count + nesting
17468fb3f3f6SDavid E. O'Brien  == static sopno pluscount(struct parse *p, struct re_guts *g);
174758f0484fSRodney W. Grimes  */
174858f0484fSRodney W. Grimes static sopno			/* nesting depth */
174954a648d1SXin LI pluscount(struct parse *p, struct re_guts *g)
175058f0484fSRodney W. Grimes {
17518fb3f3f6SDavid E. O'Brien 	sop *scan;
17528fb3f3f6SDavid E. O'Brien 	sop s;
17538fb3f3f6SDavid E. O'Brien 	sopno plusnest = 0;
17548fb3f3f6SDavid E. O'Brien 	sopno maxnest = 0;
175558f0484fSRodney W. Grimes 
175658f0484fSRodney W. Grimes 	if (p->error != 0)
175758f0484fSRodney W. Grimes 		return(0);	/* there may not be an OEND */
175858f0484fSRodney W. Grimes 
175958f0484fSRodney W. Grimes 	scan = g->strip + 1;
176058f0484fSRodney W. Grimes 	do {
176158f0484fSRodney W. Grimes 		s = *scan++;
176258f0484fSRodney W. Grimes 		switch (OP(s)) {
176358f0484fSRodney W. Grimes 		case OPLUS_:
176458f0484fSRodney W. Grimes 			plusnest++;
176558f0484fSRodney W. Grimes 			break;
176658f0484fSRodney W. Grimes 		case O_PLUS:
176758f0484fSRodney W. Grimes 			if (plusnest > maxnest)
176858f0484fSRodney W. Grimes 				maxnest = plusnest;
176958f0484fSRodney W. Grimes 			plusnest--;
177058f0484fSRodney W. Grimes 			break;
177158f0484fSRodney W. Grimes 		}
177258f0484fSRodney W. Grimes 	} while (OP(s) != OEND);
177358f0484fSRodney W. Grimes 	if (plusnest != 0)
177458f0484fSRodney W. Grimes 		g->iflags |= BAD;
177558f0484fSRodney W. Grimes 	return(maxnest);
177658f0484fSRodney W. Grimes }
1777