xref: /freebsd/lib/libc/regex/regcomp.c (revision 4e8c80e977270a75e4f11720651fca4eb6b24c78)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
358f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994
458f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
558f0484fSRodney W. Grimes  *
658f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
758f0484fSRodney W. Grimes  * Henry Spencer.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1858f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1958f0484fSRodney W. Grimes  *	This product includes software developed by the University of
2058f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  *
3758f0484fSRodney W. Grimes  *	@(#)regcomp.c	8.5 (Berkeley) 3/20/94
3858f0484fSRodney W. Grimes  */
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
4158f0484fSRodney W. Grimes static char sccsid[] = "@(#)regcomp.c	8.5 (Berkeley) 3/20/94";
4258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
43333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
44333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes #include <sys/types.h>
4758f0484fSRodney W. Grimes #include <stdio.h>
4858f0484fSRodney W. Grimes #include <string.h>
4958f0484fSRodney W. Grimes #include <ctype.h>
5058f0484fSRodney W. Grimes #include <limits.h>
5158f0484fSRodney W. Grimes #include <stdlib.h>
5258f0484fSRodney W. Grimes #include <regex.h>
534e8c80e9SStefan Farfeleder #include <runetype.h>
54e5996857STim J. Robbins #include <wchar.h>
55e5996857STim J. Robbins #include <wctype.h>
5658f0484fSRodney W. Grimes 
57c61cea72SAndrey A. Chernov #include "collate.h"
58c61cea72SAndrey A. Chernov 
5958f0484fSRodney W. Grimes #include "utils.h"
6058f0484fSRodney W. Grimes #include "regex2.h"
6158f0484fSRodney W. Grimes 
6258f0484fSRodney W. Grimes #include "cname.h"
6358f0484fSRodney W. Grimes 
6458f0484fSRodney W. Grimes /*
6558f0484fSRodney W. Grimes  * parse structure, passed up and down to avoid global variables and
6658f0484fSRodney W. Grimes  * other clumsinesses
6758f0484fSRodney W. Grimes  */
6858f0484fSRodney W. Grimes struct parse {
6958f0484fSRodney W. Grimes 	char *next;		/* next character in RE */
7058f0484fSRodney W. Grimes 	char *end;		/* end of string (-> NUL normally) */
7158f0484fSRodney W. Grimes 	int error;		/* has an error been seen? */
7258f0484fSRodney W. Grimes 	sop *strip;		/* malloced strip */
7358f0484fSRodney W. Grimes 	sopno ssize;		/* malloced strip size (allocated) */
7458f0484fSRodney W. Grimes 	sopno slen;		/* malloced strip length (used) */
7558f0484fSRodney W. Grimes 	int ncsalloc;		/* number of csets allocated */
7658f0484fSRodney W. Grimes 	struct re_guts *g;
7758f0484fSRodney W. Grimes #	define	NPAREN	10	/* we need to remember () 1-9 for back refs */
7858f0484fSRodney W. Grimes 	sopno pbegin[NPAREN];	/* -> ( ([0] unused) */
7958f0484fSRodney W. Grimes 	sopno pend[NPAREN];	/* -> ) ([0] unused) */
8058f0484fSRodney W. Grimes };
8158f0484fSRodney W. Grimes 
8258f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */
8358f0484fSRodney W. Grimes #ifdef __cplusplus
8458f0484fSRodney W. Grimes extern "C" {
8558f0484fSRodney W. Grimes #endif
8658f0484fSRodney W. Grimes 
8758f0484fSRodney W. Grimes /* === regcomp.c === */
88e5996857STim J. Robbins static void p_ere(struct parse *p, wint_t stop);
89c05ac53bSDavid E. O'Brien static void p_ere_exp(struct parse *p);
90c05ac53bSDavid E. O'Brien static void p_str(struct parse *p);
91e5996857STim J. Robbins static void p_bre(struct parse *p, wint_t end1, wint_t end2);
92c05ac53bSDavid E. O'Brien static int p_simp_re(struct parse *p, int starordinary);
93c05ac53bSDavid E. O'Brien static int p_count(struct parse *p);
94c05ac53bSDavid E. O'Brien static void p_bracket(struct parse *p);
95c05ac53bSDavid E. O'Brien static void p_b_term(struct parse *p, cset *cs);
96c05ac53bSDavid E. O'Brien static void p_b_cclass(struct parse *p, cset *cs);
97c05ac53bSDavid E. O'Brien static void p_b_eclass(struct parse *p, cset *cs);
98e5996857STim J. Robbins static wint_t p_b_symbol(struct parse *p);
99e5996857STim J. Robbins static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
100e5996857STim J. Robbins static wint_t othercase(wint_t ch);
101e5996857STim J. Robbins static void bothcases(struct parse *p, wint_t ch);
102e5996857STim J. Robbins static void ordinary(struct parse *p, wint_t ch);
103c05ac53bSDavid E. O'Brien static void nonnewline(struct parse *p);
104c05ac53bSDavid E. O'Brien static void repeat(struct parse *p, sopno start, int from, int to);
105c05ac53bSDavid E. O'Brien static int seterr(struct parse *p, int e);
106c05ac53bSDavid E. O'Brien static cset *allocset(struct parse *p);
107c05ac53bSDavid E. O'Brien static void freeset(struct parse *p, cset *cs);
108e5996857STim J. Robbins static void CHadd(struct parse *p, cset *cs, wint_t ch);
109e5996857STim J. Robbins static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max);
110e5996857STim J. Robbins static void CHaddtype(struct parse *p, cset *cs, wctype_t wct);
111e5996857STim J. Robbins static wint_t singleton(cset *cs);
112c05ac53bSDavid E. O'Brien static sopno dupl(struct parse *p, sopno start, sopno finish);
113c05ac53bSDavid E. O'Brien static void doemit(struct parse *p, sop op, size_t opnd);
114c05ac53bSDavid E. O'Brien static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
115c05ac53bSDavid E. O'Brien static void dofwd(struct parse *p, sopno pos, sop value);
116c05ac53bSDavid E. O'Brien static void enlarge(struct parse *p, sopno size);
117c05ac53bSDavid E. O'Brien static void stripsnug(struct parse *p, struct re_guts *g);
118c05ac53bSDavid E. O'Brien static void findmust(struct parse *p, struct re_guts *g);
11980f36366STim J. Robbins static int altoffset(sop *scan, int offset);
120c05ac53bSDavid E. O'Brien static void computejumps(struct parse *p, struct re_guts *g);
121c05ac53bSDavid E. O'Brien static void computematchjumps(struct parse *p, struct re_guts *g);
122c05ac53bSDavid E. O'Brien static sopno pluscount(struct parse *p, struct re_guts *g);
123e5996857STim J. Robbins static wint_t wgetnext(struct parse *p);
12458f0484fSRodney W. Grimes 
12558f0484fSRodney W. Grimes #ifdef __cplusplus
12658f0484fSRodney W. Grimes }
12758f0484fSRodney W. Grimes #endif
12858f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */
12958f0484fSRodney W. Grimes 
13058f0484fSRodney W. Grimes static char nuls[10];		/* place to point scanner in event of error */
13158f0484fSRodney W. Grimes 
13258f0484fSRodney W. Grimes /*
13358f0484fSRodney W. Grimes  * macros for use with parse structure
13458f0484fSRodney W. Grimes  * BEWARE:  these know that the parse structure is named `p' !!!
13558f0484fSRodney W. Grimes  */
13658f0484fSRodney W. Grimes #define	PEEK()	(*p->next)
13758f0484fSRodney W. Grimes #define	PEEK2()	(*(p->next+1))
13858f0484fSRodney W. Grimes #define	MORE()	(p->next < p->end)
13958f0484fSRodney W. Grimes #define	MORE2()	(p->next+1 < p->end)
14058f0484fSRodney W. Grimes #define	SEE(c)	(MORE() && PEEK() == (c))
14158f0484fSRodney W. Grimes #define	SEETWO(a, b)	(MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
14258f0484fSRodney W. Grimes #define	EAT(c)	((SEE(c)) ? (NEXT(), 1) : 0)
14358f0484fSRodney W. Grimes #define	EATTWO(a, b)	((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
14458f0484fSRodney W. Grimes #define	NEXT()	(p->next++)
14558f0484fSRodney W. Grimes #define	NEXT2()	(p->next += 2)
14658f0484fSRodney W. Grimes #define	NEXTn(n)	(p->next += (n))
14758f0484fSRodney W. Grimes #define	GETNEXT()	(*p->next++)
148e5996857STim J. Robbins #define	WGETNEXT()	wgetnext(p)
14958f0484fSRodney W. Grimes #define	SETERROR(e)	seterr(p, (e))
15058f0484fSRodney W. Grimes #define	REQUIRE(co, e)	((co) || SETERROR(e))
15158f0484fSRodney W. Grimes #define	MUSTSEE(c, e)	(REQUIRE(MORE() && PEEK() == (c), e))
15258f0484fSRodney W. Grimes #define	MUSTEAT(c, e)	(REQUIRE(MORE() && GETNEXT() == (c), e))
15358f0484fSRodney W. Grimes #define	MUSTNOTSEE(c, e)	(REQUIRE(!MORE() || PEEK() != (c), e))
15458f0484fSRodney W. Grimes #define	EMIT(op, sopnd)	doemit(p, (sop)(op), (size_t)(sopnd))
15558f0484fSRodney W. Grimes #define	INSERT(op, pos)	doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
15658f0484fSRodney W. Grimes #define	AHEAD(pos)		dofwd(p, pos, HERE()-(pos))
15758f0484fSRodney W. Grimes #define	ASTERN(sop, pos)	EMIT(sop, HERE()-pos)
15858f0484fSRodney W. Grimes #define	HERE()		(p->slen)
15958f0484fSRodney W. Grimes #define	THERE()		(p->slen - 1)
16058f0484fSRodney W. Grimes #define	THERETHERE()	(p->slen - 2)
16158f0484fSRodney W. Grimes #define	DROP(n)	(p->slen -= (n))
16258f0484fSRodney W. Grimes 
16358f0484fSRodney W. Grimes #ifndef NDEBUG
16458f0484fSRodney W. Grimes static int never = 0;		/* for use in asserts; shuts lint up */
16558f0484fSRodney W. Grimes #else
16658f0484fSRodney W. Grimes #define	never	0		/* some <assert.h>s have bugs too */
16758f0484fSRodney W. Grimes #endif
16858f0484fSRodney W. Grimes 
1696049d9f0SDaniel C. Sobral /* Macro used by computejump()/computematchjump() */
1706049d9f0SDaniel C. Sobral #define MIN(a,b)	((a)<(b)?(a):(b))
1716049d9f0SDaniel C. Sobral 
17258f0484fSRodney W. Grimes /*
17358f0484fSRodney W. Grimes  - regcomp - interface for parser and compilation
17458f0484fSRodney W. Grimes  = extern int regcomp(regex_t *, const char *, int);
17558f0484fSRodney W. Grimes  = #define	REG_BASIC	0000
17658f0484fSRodney W. Grimes  = #define	REG_EXTENDED	0001
17758f0484fSRodney W. Grimes  = #define	REG_ICASE	0002
17858f0484fSRodney W. Grimes  = #define	REG_NOSUB	0004
17958f0484fSRodney W. Grimes  = #define	REG_NEWLINE	0010
18058f0484fSRodney W. Grimes  = #define	REG_NOSPEC	0020
18158f0484fSRodney W. Grimes  = #define	REG_PEND	0040
18258f0484fSRodney W. Grimes  = #define	REG_DUMP	0200
18358f0484fSRodney W. Grimes  */
18458f0484fSRodney W. Grimes int				/* 0 success, otherwise REG_something */
18558f0484fSRodney W. Grimes regcomp(preg, pattern, cflags)
1864047df8dSMike Barcroft regex_t * __restrict preg;
1874047df8dSMike Barcroft const char * __restrict pattern;
18858f0484fSRodney W. Grimes int cflags;
18958f0484fSRodney W. Grimes {
19058f0484fSRodney W. Grimes 	struct parse pa;
1918fb3f3f6SDavid E. O'Brien 	struct re_guts *g;
1928fb3f3f6SDavid E. O'Brien 	struct parse *p = &pa;
1938fb3f3f6SDavid E. O'Brien 	int i;
1948fb3f3f6SDavid E. O'Brien 	size_t len;
19558f0484fSRodney W. Grimes #ifdef REDEBUG
19658f0484fSRodney W. Grimes #	define	GOODFLAGS(f)	(f)
19758f0484fSRodney W. Grimes #else
19858f0484fSRodney W. Grimes #	define	GOODFLAGS(f)	((f)&~REG_DUMP)
19958f0484fSRodney W. Grimes #endif
20058f0484fSRodney W. Grimes 
20158f0484fSRodney W. Grimes 	cflags = GOODFLAGS(cflags);
20258f0484fSRodney W. Grimes 	if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
20358f0484fSRodney W. Grimes 		return(REG_INVARG);
20458f0484fSRodney W. Grimes 
20558f0484fSRodney W. Grimes 	if (cflags&REG_PEND) {
20658f0484fSRodney W. Grimes 		if (preg->re_endp < pattern)
20758f0484fSRodney W. Grimes 			return(REG_INVARG);
20858f0484fSRodney W. Grimes 		len = preg->re_endp - pattern;
20958f0484fSRodney W. Grimes 	} else
21058f0484fSRodney W. Grimes 		len = strlen((char *)pattern);
21158f0484fSRodney W. Grimes 
21258f0484fSRodney W. Grimes 	/* do the mallocs early so failure handling is easy */
21380f36366STim J. Robbins 	g = (struct re_guts *)malloc(sizeof(struct re_guts));
21458f0484fSRodney W. Grimes 	if (g == NULL)
21558f0484fSRodney W. Grimes 		return(REG_ESPACE);
21658f0484fSRodney W. Grimes 	p->ssize = len/(size_t)2*(size_t)3 + (size_t)1;	/* ugh */
21758f0484fSRodney W. Grimes 	p->strip = (sop *)malloc(p->ssize * sizeof(sop));
21858f0484fSRodney W. Grimes 	p->slen = 0;
21958f0484fSRodney W. Grimes 	if (p->strip == NULL) {
22058f0484fSRodney W. Grimes 		free((char *)g);
22158f0484fSRodney W. Grimes 		return(REG_ESPACE);
22258f0484fSRodney W. Grimes 	}
22358f0484fSRodney W. Grimes 
22458f0484fSRodney W. Grimes 	/* set things up */
22558f0484fSRodney W. Grimes 	p->g = g;
22658f0484fSRodney W. Grimes 	p->next = (char *)pattern;	/* convenience; we do not modify it */
22758f0484fSRodney W. Grimes 	p->end = p->next + len;
22858f0484fSRodney W. Grimes 	p->error = 0;
22958f0484fSRodney W. Grimes 	p->ncsalloc = 0;
23058f0484fSRodney W. Grimes 	for (i = 0; i < NPAREN; i++) {
23158f0484fSRodney W. Grimes 		p->pbegin[i] = 0;
23258f0484fSRodney W. Grimes 		p->pend[i] = 0;
23358f0484fSRodney W. Grimes 	}
23458f0484fSRodney W. Grimes 	g->sets = NULL;
23558f0484fSRodney W. Grimes 	g->ncsets = 0;
23658f0484fSRodney W. Grimes 	g->cflags = cflags;
23758f0484fSRodney W. Grimes 	g->iflags = 0;
23858f0484fSRodney W. Grimes 	g->nbol = 0;
23958f0484fSRodney W. Grimes 	g->neol = 0;
24058f0484fSRodney W. Grimes 	g->must = NULL;
241e6a886d8SDaniel C. Sobral 	g->moffset = -1;
2426b709b74SDaniel C. Sobral 	g->charjump = NULL;
2436b709b74SDaniel C. Sobral 	g->matchjump = NULL;
24458f0484fSRodney W. Grimes 	g->mlen = 0;
24558f0484fSRodney W. Grimes 	g->nsub = 0;
24658f0484fSRodney W. Grimes 	g->backrefs = 0;
24758f0484fSRodney W. Grimes 
24858f0484fSRodney W. Grimes 	/* do it */
24958f0484fSRodney W. Grimes 	EMIT(OEND, 0);
25058f0484fSRodney W. Grimes 	g->firststate = THERE();
25158f0484fSRodney W. Grimes 	if (cflags&REG_EXTENDED)
25258f0484fSRodney W. Grimes 		p_ere(p, OUT);
25358f0484fSRodney W. Grimes 	else if (cflags&REG_NOSPEC)
25458f0484fSRodney W. Grimes 		p_str(p);
25558f0484fSRodney W. Grimes 	else
25658f0484fSRodney W. Grimes 		p_bre(p, OUT, OUT);
25758f0484fSRodney W. Grimes 	EMIT(OEND, 0);
25858f0484fSRodney W. Grimes 	g->laststate = THERE();
25958f0484fSRodney W. Grimes 
26058f0484fSRodney W. Grimes 	/* tidy up loose ends and fill things in */
26158f0484fSRodney W. Grimes 	stripsnug(p, g);
26258f0484fSRodney W. Grimes 	findmust(p, g);
2636049d9f0SDaniel C. Sobral 	/* only use Boyer-Moore algorithm if the pattern is bigger
2646049d9f0SDaniel C. Sobral 	 * than three characters
2656049d9f0SDaniel C. Sobral 	 */
2666049d9f0SDaniel C. Sobral 	if(g->mlen > 3) {
2676049d9f0SDaniel C. Sobral 		computejumps(p, g);
2686049d9f0SDaniel C. Sobral 		computematchjumps(p, g);
2694d41cae8SDaniel C. Sobral 		if(g->matchjump == NULL && g->charjump != NULL) {
2706049d9f0SDaniel C. Sobral 			free(g->charjump);
2716049d9f0SDaniel C. Sobral 			g->charjump = NULL;
2726049d9f0SDaniel C. Sobral 		}
2736049d9f0SDaniel C. Sobral 	}
27458f0484fSRodney W. Grimes 	g->nplus = pluscount(p, g);
27558f0484fSRodney W. Grimes 	g->magic = MAGIC2;
27658f0484fSRodney W. Grimes 	preg->re_nsub = g->nsub;
27758f0484fSRodney W. Grimes 	preg->re_g = g;
27858f0484fSRodney W. Grimes 	preg->re_magic = MAGIC1;
27958f0484fSRodney W. Grimes #ifndef REDEBUG
28058f0484fSRodney W. Grimes 	/* not debugging, so can't rely on the assert() in regexec() */
28158f0484fSRodney W. Grimes 	if (g->iflags&BAD)
28258f0484fSRodney W. Grimes 		SETERROR(REG_ASSERT);
28358f0484fSRodney W. Grimes #endif
28458f0484fSRodney W. Grimes 
28558f0484fSRodney W. Grimes 	/* win or lose, we're done */
28658f0484fSRodney W. Grimes 	if (p->error != 0)	/* lose */
28758f0484fSRodney W. Grimes 		regfree(preg);
28858f0484fSRodney W. Grimes 	return(p->error);
28958f0484fSRodney W. Grimes }
29058f0484fSRodney W. Grimes 
29158f0484fSRodney W. Grimes /*
29258f0484fSRodney W. Grimes  - p_ere - ERE parser top level, concatenation and alternation
2938fb3f3f6SDavid E. O'Brien  == static void p_ere(struct parse *p, int stop);
29458f0484fSRodney W. Grimes  */
29558f0484fSRodney W. Grimes static void
29658f0484fSRodney W. Grimes p_ere(p, stop)
2978fb3f3f6SDavid E. O'Brien struct parse *p;
29858f0484fSRodney W. Grimes int stop;			/* character this ERE should end at */
29958f0484fSRodney W. Grimes {
3008fb3f3f6SDavid E. O'Brien 	char c;
3018fb3f3f6SDavid E. O'Brien 	sopno prevback;
3028fb3f3f6SDavid E. O'Brien 	sopno prevfwd;
3038fb3f3f6SDavid E. O'Brien 	sopno conc;
3048fb3f3f6SDavid E. O'Brien 	int first = 1;		/* is this the first alternative? */
30558f0484fSRodney W. Grimes 
30658f0484fSRodney W. Grimes 	for (;;) {
30758f0484fSRodney W. Grimes 		/* do a bunch of concatenated expressions */
30858f0484fSRodney W. Grimes 		conc = HERE();
30958f0484fSRodney W. Grimes 		while (MORE() && (c = PEEK()) != '|' && c != stop)
31058f0484fSRodney W. Grimes 			p_ere_exp(p);
31151295a4dSJordan K. Hubbard 		(void)REQUIRE(HERE() != conc, REG_EMPTY);	/* require nonempty */
31258f0484fSRodney W. Grimes 
31358f0484fSRodney W. Grimes 		if (!EAT('|'))
31458f0484fSRodney W. Grimes 			break;		/* NOTE BREAK OUT */
31558f0484fSRodney W. Grimes 
31658f0484fSRodney W. Grimes 		if (first) {
31758f0484fSRodney W. Grimes 			INSERT(OCH_, conc);	/* offset is wrong */
31858f0484fSRodney W. Grimes 			prevfwd = conc;
31958f0484fSRodney W. Grimes 			prevback = conc;
32058f0484fSRodney W. Grimes 			first = 0;
32158f0484fSRodney W. Grimes 		}
32258f0484fSRodney W. Grimes 		ASTERN(OOR1, prevback);
32358f0484fSRodney W. Grimes 		prevback = THERE();
32458f0484fSRodney W. Grimes 		AHEAD(prevfwd);			/* fix previous offset */
32558f0484fSRodney W. Grimes 		prevfwd = HERE();
32658f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset is very wrong */
32758f0484fSRodney W. Grimes 	}
32858f0484fSRodney W. Grimes 
32958f0484fSRodney W. Grimes 	if (!first) {		/* tail-end fixups */
33058f0484fSRodney W. Grimes 		AHEAD(prevfwd);
33158f0484fSRodney W. Grimes 		ASTERN(O_CH, prevback);
33258f0484fSRodney W. Grimes 	}
33358f0484fSRodney W. Grimes 
33458f0484fSRodney W. Grimes 	assert(!MORE() || SEE(stop));
33558f0484fSRodney W. Grimes }
33658f0484fSRodney W. Grimes 
33758f0484fSRodney W. Grimes /*
33858f0484fSRodney W. Grimes  - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
3398fb3f3f6SDavid E. O'Brien  == static void p_ere_exp(struct parse *p);
34058f0484fSRodney W. Grimes  */
34158f0484fSRodney W. Grimes static void
34258f0484fSRodney W. Grimes p_ere_exp(p)
3438fb3f3f6SDavid E. O'Brien struct parse *p;
34458f0484fSRodney W. Grimes {
3458fb3f3f6SDavid E. O'Brien 	char c;
346e5996857STim J. Robbins 	wint_t wc;
3478fb3f3f6SDavid E. O'Brien 	sopno pos;
3488fb3f3f6SDavid E. O'Brien 	int count;
3498fb3f3f6SDavid E. O'Brien 	int count2;
3508fb3f3f6SDavid E. O'Brien 	sopno subno;
35158f0484fSRodney W. Grimes 	int wascaret = 0;
35258f0484fSRodney W. Grimes 
35358f0484fSRodney W. Grimes 	assert(MORE());		/* caller should have ensured this */
35458f0484fSRodney W. Grimes 	c = GETNEXT();
35558f0484fSRodney W. Grimes 
35658f0484fSRodney W. Grimes 	pos = HERE();
35758f0484fSRodney W. Grimes 	switch (c) {
35858f0484fSRodney W. Grimes 	case '(':
35951295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EPAREN);
36058f0484fSRodney W. Grimes 		p->g->nsub++;
36158f0484fSRodney W. Grimes 		subno = p->g->nsub;
36258f0484fSRodney W. Grimes 		if (subno < NPAREN)
36358f0484fSRodney W. Grimes 			p->pbegin[subno] = HERE();
36458f0484fSRodney W. Grimes 		EMIT(OLPAREN, subno);
36558f0484fSRodney W. Grimes 		if (!SEE(')'))
36658f0484fSRodney W. Grimes 			p_ere(p, ')');
36758f0484fSRodney W. Grimes 		if (subno < NPAREN) {
36858f0484fSRodney W. Grimes 			p->pend[subno] = HERE();
36958f0484fSRodney W. Grimes 			assert(p->pend[subno] != 0);
37058f0484fSRodney W. Grimes 		}
37158f0484fSRodney W. Grimes 		EMIT(ORPAREN, subno);
37251295a4dSJordan K. Hubbard 		(void)MUSTEAT(')', REG_EPAREN);
37358f0484fSRodney W. Grimes 		break;
37458f0484fSRodney W. Grimes #ifndef POSIX_MISTAKE
37558f0484fSRodney W. Grimes 	case ')':		/* happens only if no current unmatched ( */
37658f0484fSRodney W. Grimes 		/*
37758f0484fSRodney W. Grimes 		 * You may ask, why the ifndef?  Because I didn't notice
37858f0484fSRodney W. Grimes 		 * this until slightly too late for 1003.2, and none of the
37958f0484fSRodney W. Grimes 		 * other 1003.2 regular-expression reviewers noticed it at
38058f0484fSRodney W. Grimes 		 * all.  So an unmatched ) is legal POSIX, at least until
38158f0484fSRodney W. Grimes 		 * we can get it fixed.
38258f0484fSRodney W. Grimes 		 */
38358f0484fSRodney W. Grimes 		SETERROR(REG_EPAREN);
38458f0484fSRodney W. Grimes 		break;
38558f0484fSRodney W. Grimes #endif
38658f0484fSRodney W. Grimes 	case '^':
38758f0484fSRodney W. Grimes 		EMIT(OBOL, 0);
38858f0484fSRodney W. Grimes 		p->g->iflags |= USEBOL;
38958f0484fSRodney W. Grimes 		p->g->nbol++;
39058f0484fSRodney W. Grimes 		wascaret = 1;
39158f0484fSRodney W. Grimes 		break;
39258f0484fSRodney W. Grimes 	case '$':
39358f0484fSRodney W. Grimes 		EMIT(OEOL, 0);
39458f0484fSRodney W. Grimes 		p->g->iflags |= USEEOL;
39558f0484fSRodney W. Grimes 		p->g->neol++;
39658f0484fSRodney W. Grimes 		break;
39758f0484fSRodney W. Grimes 	case '|':
39858f0484fSRodney W. Grimes 		SETERROR(REG_EMPTY);
39958f0484fSRodney W. Grimes 		break;
40058f0484fSRodney W. Grimes 	case '*':
40158f0484fSRodney W. Grimes 	case '+':
40258f0484fSRodney W. Grimes 	case '?':
40358f0484fSRodney W. Grimes 		SETERROR(REG_BADRPT);
40458f0484fSRodney W. Grimes 		break;
40558f0484fSRodney W. Grimes 	case '.':
40658f0484fSRodney W. Grimes 		if (p->g->cflags&REG_NEWLINE)
40758f0484fSRodney W. Grimes 			nonnewline(p);
40858f0484fSRodney W. Grimes 		else
40958f0484fSRodney W. Grimes 			EMIT(OANY, 0);
41058f0484fSRodney W. Grimes 		break;
41158f0484fSRodney W. Grimes 	case '[':
41258f0484fSRodney W. Grimes 		p_bracket(p);
41358f0484fSRodney W. Grimes 		break;
41458f0484fSRodney W. Grimes 	case '\\':
41551295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EESCAPE);
416e5996857STim J. Robbins 		wc = WGETNEXT();
417e5996857STim J. Robbins 		ordinary(p, wc);
41858f0484fSRodney W. Grimes 		break;
41958f0484fSRodney W. Grimes 	case '{':		/* okay as ordinary except if digit follows */
42089b86d02SAndrey A. Chernov 		(void)REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
42158f0484fSRodney W. Grimes 		/* FALLTHROUGH */
42258f0484fSRodney W. Grimes 	default:
423e5996857STim J. Robbins 		p->next--;
424e5996857STim J. Robbins 		wc = WGETNEXT();
425e5996857STim J. Robbins 		ordinary(p, wc);
42658f0484fSRodney W. Grimes 		break;
42758f0484fSRodney W. Grimes 	}
42858f0484fSRodney W. Grimes 
42958f0484fSRodney W. Grimes 	if (!MORE())
43058f0484fSRodney W. Grimes 		return;
43158f0484fSRodney W. Grimes 	c = PEEK();
43258f0484fSRodney W. Grimes 	/* we call { a repetition if followed by a digit */
43358f0484fSRodney W. Grimes 	if (!( c == '*' || c == '+' || c == '?' ||
43489b86d02SAndrey A. Chernov 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
43558f0484fSRodney W. Grimes 		return;		/* no repetition, we're done */
43658f0484fSRodney W. Grimes 	NEXT();
43758f0484fSRodney W. Grimes 
43851295a4dSJordan K. Hubbard 	(void)REQUIRE(!wascaret, REG_BADRPT);
43958f0484fSRodney W. Grimes 	switch (c) {
44058f0484fSRodney W. Grimes 	case '*':	/* implemented as +? */
44158f0484fSRodney W. Grimes 		/* this case does not require the (y|) trick, noKLUDGE */
44258f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
44358f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
44458f0484fSRodney W. Grimes 		INSERT(OQUEST_, pos);
44558f0484fSRodney W. Grimes 		ASTERN(O_QUEST, pos);
44658f0484fSRodney W. Grimes 		break;
44758f0484fSRodney W. Grimes 	case '+':
44858f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
44958f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
45058f0484fSRodney W. Grimes 		break;
45158f0484fSRodney W. Grimes 	case '?':
45258f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
45358f0484fSRodney W. Grimes 		INSERT(OCH_, pos);		/* offset slightly wrong */
45458f0484fSRodney W. Grimes 		ASTERN(OOR1, pos);		/* this one's right */
45558f0484fSRodney W. Grimes 		AHEAD(pos);			/* fix the OCH_ */
45658f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset very wrong... */
45758f0484fSRodney W. Grimes 		AHEAD(THERE());			/* ...so fix it */
45858f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
45958f0484fSRodney W. Grimes 		break;
46058f0484fSRodney W. Grimes 	case '{':
46158f0484fSRodney W. Grimes 		count = p_count(p);
46258f0484fSRodney W. Grimes 		if (EAT(',')) {
46389b86d02SAndrey A. Chernov 			if (isdigit((uch)PEEK())) {
46458f0484fSRodney W. Grimes 				count2 = p_count(p);
46551295a4dSJordan K. Hubbard 				(void)REQUIRE(count <= count2, REG_BADBR);
46658f0484fSRodney W. Grimes 			} else		/* single number with comma */
46758f0484fSRodney W. Grimes 				count2 = INFINITY;
46858f0484fSRodney W. Grimes 		} else		/* just a single number */
46958f0484fSRodney W. Grimes 			count2 = count;
47058f0484fSRodney W. Grimes 		repeat(p, pos, count, count2);
47158f0484fSRodney W. Grimes 		if (!EAT('}')) {	/* error heuristics */
47258f0484fSRodney W. Grimes 			while (MORE() && PEEK() != '}')
47358f0484fSRodney W. Grimes 				NEXT();
47451295a4dSJordan K. Hubbard 			(void)REQUIRE(MORE(), REG_EBRACE);
47558f0484fSRodney W. Grimes 			SETERROR(REG_BADBR);
47658f0484fSRodney W. Grimes 		}
47758f0484fSRodney W. Grimes 		break;
47858f0484fSRodney W. Grimes 	}
47958f0484fSRodney W. Grimes 
48058f0484fSRodney W. Grimes 	if (!MORE())
48158f0484fSRodney W. Grimes 		return;
48258f0484fSRodney W. Grimes 	c = PEEK();
48358f0484fSRodney W. Grimes 	if (!( c == '*' || c == '+' || c == '?' ||
48489b86d02SAndrey A. Chernov 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
48558f0484fSRodney W. Grimes 		return;
48658f0484fSRodney W. Grimes 	SETERROR(REG_BADRPT);
48758f0484fSRodney W. Grimes }
48858f0484fSRodney W. Grimes 
48958f0484fSRodney W. Grimes /*
49058f0484fSRodney W. Grimes  - p_str - string (no metacharacters) "parser"
4918fb3f3f6SDavid E. O'Brien  == static void p_str(struct parse *p);
49258f0484fSRodney W. Grimes  */
49358f0484fSRodney W. Grimes static void
49458f0484fSRodney W. Grimes p_str(p)
4958fb3f3f6SDavid E. O'Brien struct parse *p;
49658f0484fSRodney W. Grimes {
49751295a4dSJordan K. Hubbard 	(void)REQUIRE(MORE(), REG_EMPTY);
49858f0484fSRodney W. Grimes 	while (MORE())
499e5996857STim J. Robbins 		ordinary(p, WGETNEXT());
50058f0484fSRodney W. Grimes }
50158f0484fSRodney W. Grimes 
50258f0484fSRodney W. Grimes /*
50358f0484fSRodney W. Grimes  - p_bre - BRE parser top level, anchoring and concatenation
5048fb3f3f6SDavid E. O'Brien  == static void p_bre(struct parse *p, int end1, \
5058fb3f3f6SDavid E. O'Brien  ==	int end2);
50658f0484fSRodney W. Grimes  * Giving end1 as OUT essentially eliminates the end1/end2 check.
50758f0484fSRodney W. Grimes  *
50858f0484fSRodney W. Grimes  * This implementation is a bit of a kludge, in that a trailing $ is first
50980f36366STim J. Robbins  * taken as an ordinary character and then revised to be an anchor.
51058f0484fSRodney W. Grimes  * The amount of lookahead needed to avoid this kludge is excessive.
51158f0484fSRodney W. Grimes  */
51258f0484fSRodney W. Grimes static void
51358f0484fSRodney W. Grimes p_bre(p, end1, end2)
5148fb3f3f6SDavid E. O'Brien struct parse *p;
5158fb3f3f6SDavid E. O'Brien int end1;			/* first terminating character */
5168fb3f3f6SDavid E. O'Brien int end2;			/* second terminating character */
51758f0484fSRodney W. Grimes {
5188fb3f3f6SDavid E. O'Brien 	sopno start = HERE();
5198fb3f3f6SDavid E. O'Brien 	int first = 1;			/* first subexpression? */
5208fb3f3f6SDavid E. O'Brien 	int wasdollar = 0;
52158f0484fSRodney W. Grimes 
52258f0484fSRodney W. Grimes 	if (EAT('^')) {
52358f0484fSRodney W. Grimes 		EMIT(OBOL, 0);
52458f0484fSRodney W. Grimes 		p->g->iflags |= USEBOL;
52558f0484fSRodney W. Grimes 		p->g->nbol++;
52658f0484fSRodney W. Grimes 	}
52758f0484fSRodney W. Grimes 	while (MORE() && !SEETWO(end1, end2)) {
52858f0484fSRodney W. Grimes 		wasdollar = p_simp_re(p, first);
52958f0484fSRodney W. Grimes 		first = 0;
53058f0484fSRodney W. Grimes 	}
53158f0484fSRodney W. Grimes 	if (wasdollar) {	/* oops, that was a trailing anchor */
53258f0484fSRodney W. Grimes 		DROP(1);
53358f0484fSRodney W. Grimes 		EMIT(OEOL, 0);
53458f0484fSRodney W. Grimes 		p->g->iflags |= USEEOL;
53558f0484fSRodney W. Grimes 		p->g->neol++;
53658f0484fSRodney W. Grimes 	}
53758f0484fSRodney W. Grimes 
53851295a4dSJordan K. Hubbard 	(void)REQUIRE(HERE() != start, REG_EMPTY);	/* require nonempty */
53958f0484fSRodney W. Grimes }
54058f0484fSRodney W. Grimes 
54158f0484fSRodney W. Grimes /*
54258f0484fSRodney W. Grimes  - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
5438fb3f3f6SDavid E. O'Brien  == static int p_simp_re(struct parse *p, int starordinary);
54458f0484fSRodney W. Grimes  */
54558f0484fSRodney W. Grimes static int			/* was the simple RE an unbackslashed $? */
54658f0484fSRodney W. Grimes p_simp_re(p, starordinary)
5478fb3f3f6SDavid E. O'Brien struct parse *p;
54858f0484fSRodney W. Grimes int starordinary;		/* is a leading * an ordinary character? */
54958f0484fSRodney W. Grimes {
5508fb3f3f6SDavid E. O'Brien 	int c;
5518fb3f3f6SDavid E. O'Brien 	int count;
5528fb3f3f6SDavid E. O'Brien 	int count2;
5538fb3f3f6SDavid E. O'Brien 	sopno pos;
5548fb3f3f6SDavid E. O'Brien 	int i;
555e5996857STim J. Robbins 	wint_t wc;
5568fb3f3f6SDavid E. O'Brien 	sopno subno;
55758f0484fSRodney W. Grimes #	define	BACKSL	(1<<CHAR_BIT)
55858f0484fSRodney W. Grimes 
55958f0484fSRodney W. Grimes 	pos = HERE();		/* repetion op, if any, covers from here */
56058f0484fSRodney W. Grimes 
56158f0484fSRodney W. Grimes 	assert(MORE());		/* caller should have ensured this */
56258f0484fSRodney W. Grimes 	c = GETNEXT();
56358f0484fSRodney W. Grimes 	if (c == '\\') {
56451295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EESCAPE);
56589b86d02SAndrey A. Chernov 		c = BACKSL | GETNEXT();
56658f0484fSRodney W. Grimes 	}
56758f0484fSRodney W. Grimes 	switch (c) {
56858f0484fSRodney W. Grimes 	case '.':
56958f0484fSRodney W. Grimes 		if (p->g->cflags&REG_NEWLINE)
57058f0484fSRodney W. Grimes 			nonnewline(p);
57158f0484fSRodney W. Grimes 		else
57258f0484fSRodney W. Grimes 			EMIT(OANY, 0);
57358f0484fSRodney W. Grimes 		break;
57458f0484fSRodney W. Grimes 	case '[':
57558f0484fSRodney W. Grimes 		p_bracket(p);
57658f0484fSRodney W. Grimes 		break;
57758f0484fSRodney W. Grimes 	case BACKSL|'{':
57858f0484fSRodney W. Grimes 		SETERROR(REG_BADRPT);
57958f0484fSRodney W. Grimes 		break;
58058f0484fSRodney W. Grimes 	case BACKSL|'(':
58158f0484fSRodney W. Grimes 		p->g->nsub++;
58258f0484fSRodney W. Grimes 		subno = p->g->nsub;
58358f0484fSRodney W. Grimes 		if (subno < NPAREN)
58458f0484fSRodney W. Grimes 			p->pbegin[subno] = HERE();
58558f0484fSRodney W. Grimes 		EMIT(OLPAREN, subno);
58658f0484fSRodney W. Grimes 		/* the MORE here is an error heuristic */
58758f0484fSRodney W. Grimes 		if (MORE() && !SEETWO('\\', ')'))
58858f0484fSRodney W. Grimes 			p_bre(p, '\\', ')');
58958f0484fSRodney W. Grimes 		if (subno < NPAREN) {
59058f0484fSRodney W. Grimes 			p->pend[subno] = HERE();
59158f0484fSRodney W. Grimes 			assert(p->pend[subno] != 0);
59258f0484fSRodney W. Grimes 		}
59358f0484fSRodney W. Grimes 		EMIT(ORPAREN, subno);
59451295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
59558f0484fSRodney W. Grimes 		break;
59658f0484fSRodney W. Grimes 	case BACKSL|')':	/* should not get here -- must be user */
59758f0484fSRodney W. Grimes 	case BACKSL|'}':
59858f0484fSRodney W. Grimes 		SETERROR(REG_EPAREN);
59958f0484fSRodney W. Grimes 		break;
60058f0484fSRodney W. Grimes 	case BACKSL|'1':
60158f0484fSRodney W. Grimes 	case BACKSL|'2':
60258f0484fSRodney W. Grimes 	case BACKSL|'3':
60358f0484fSRodney W. Grimes 	case BACKSL|'4':
60458f0484fSRodney W. Grimes 	case BACKSL|'5':
60558f0484fSRodney W. Grimes 	case BACKSL|'6':
60658f0484fSRodney W. Grimes 	case BACKSL|'7':
60758f0484fSRodney W. Grimes 	case BACKSL|'8':
60858f0484fSRodney W. Grimes 	case BACKSL|'9':
60958f0484fSRodney W. Grimes 		i = (c&~BACKSL) - '0';
61058f0484fSRodney W. Grimes 		assert(i < NPAREN);
61158f0484fSRodney W. Grimes 		if (p->pend[i] != 0) {
61258f0484fSRodney W. Grimes 			assert(i <= p->g->nsub);
61358f0484fSRodney W. Grimes 			EMIT(OBACK_, i);
61458f0484fSRodney W. Grimes 			assert(p->pbegin[i] != 0);
61558f0484fSRodney W. Grimes 			assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
61658f0484fSRodney W. Grimes 			assert(OP(p->strip[p->pend[i]]) == ORPAREN);
61758f0484fSRodney W. Grimes 			(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
61858f0484fSRodney W. Grimes 			EMIT(O_BACK, i);
61958f0484fSRodney W. Grimes 		} else
62058f0484fSRodney W. Grimes 			SETERROR(REG_ESUBREG);
62158f0484fSRodney W. Grimes 		p->g->backrefs = 1;
62258f0484fSRodney W. Grimes 		break;
62358f0484fSRodney W. Grimes 	case '*':
62451295a4dSJordan K. Hubbard 		(void)REQUIRE(starordinary, REG_BADRPT);
62558f0484fSRodney W. Grimes 		/* FALLTHROUGH */
62658f0484fSRodney W. Grimes 	default:
627e5996857STim J. Robbins 		p->next--;
628e5996857STim J. Robbins 		wc = WGETNEXT();
629e5996857STim J. Robbins 		ordinary(p, wc);
63058f0484fSRodney W. Grimes 		break;
63158f0484fSRodney W. Grimes 	}
63258f0484fSRodney W. Grimes 
63358f0484fSRodney W. Grimes 	if (EAT('*')) {		/* implemented as +? */
63458f0484fSRodney W. Grimes 		/* this case does not require the (y|) trick, noKLUDGE */
63558f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
63658f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
63758f0484fSRodney W. Grimes 		INSERT(OQUEST_, pos);
63858f0484fSRodney W. Grimes 		ASTERN(O_QUEST, pos);
63958f0484fSRodney W. Grimes 	} else if (EATTWO('\\', '{')) {
64058f0484fSRodney W. Grimes 		count = p_count(p);
64158f0484fSRodney W. Grimes 		if (EAT(',')) {
64289b86d02SAndrey A. Chernov 			if (MORE() && isdigit((uch)PEEK())) {
64358f0484fSRodney W. Grimes 				count2 = p_count(p);
64451295a4dSJordan K. Hubbard 				(void)REQUIRE(count <= count2, REG_BADBR);
64558f0484fSRodney W. Grimes 			} else		/* single number with comma */
64658f0484fSRodney W. Grimes 				count2 = INFINITY;
64758f0484fSRodney W. Grimes 		} else		/* just a single number */
64858f0484fSRodney W. Grimes 			count2 = count;
64958f0484fSRodney W. Grimes 		repeat(p, pos, count, count2);
65058f0484fSRodney W. Grimes 		if (!EATTWO('\\', '}')) {	/* error heuristics */
65158f0484fSRodney W. Grimes 			while (MORE() && !SEETWO('\\', '}'))
65258f0484fSRodney W. Grimes 				NEXT();
65351295a4dSJordan K. Hubbard 			(void)REQUIRE(MORE(), REG_EBRACE);
65458f0484fSRodney W. Grimes 			SETERROR(REG_BADBR);
65558f0484fSRodney W. Grimes 		}
65689b86d02SAndrey A. Chernov 	} else if (c == '$')     /* $ (but not \$) ends it */
65758f0484fSRodney W. Grimes 		return(1);
65858f0484fSRodney W. Grimes 
65958f0484fSRodney W. Grimes 	return(0);
66058f0484fSRodney W. Grimes }
66158f0484fSRodney W. Grimes 
66258f0484fSRodney W. Grimes /*
66358f0484fSRodney W. Grimes  - p_count - parse a repetition count
6648fb3f3f6SDavid E. O'Brien  == static int p_count(struct parse *p);
66558f0484fSRodney W. Grimes  */
66658f0484fSRodney W. Grimes static int			/* the value */
66758f0484fSRodney W. Grimes p_count(p)
6688fb3f3f6SDavid E. O'Brien struct parse *p;
66958f0484fSRodney W. Grimes {
6708fb3f3f6SDavid E. O'Brien 	int count = 0;
6718fb3f3f6SDavid E. O'Brien 	int ndigits = 0;
67258f0484fSRodney W. Grimes 
67389b86d02SAndrey A. Chernov 	while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
67458f0484fSRodney W. Grimes 		count = count*10 + (GETNEXT() - '0');
67558f0484fSRodney W. Grimes 		ndigits++;
67658f0484fSRodney W. Grimes 	}
67758f0484fSRodney W. Grimes 
67851295a4dSJordan K. Hubbard 	(void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
67958f0484fSRodney W. Grimes 	return(count);
68058f0484fSRodney W. Grimes }
68158f0484fSRodney W. Grimes 
68258f0484fSRodney W. Grimes /*
68358f0484fSRodney W. Grimes  - p_bracket - parse a bracketed character list
6848fb3f3f6SDavid E. O'Brien  == static void p_bracket(struct parse *p);
68558f0484fSRodney W. Grimes  */
68658f0484fSRodney W. Grimes static void
68758f0484fSRodney W. Grimes p_bracket(p)
6888fb3f3f6SDavid E. O'Brien struct parse *p;
68958f0484fSRodney W. Grimes {
690e5996857STim J. Robbins 	cset *cs;
691e5996857STim J. Robbins 	wint_t ch;
69258f0484fSRodney W. Grimes 
69358f0484fSRodney W. Grimes 	/* Dept of Truly Sickening Special-Case Kludges */
69458f0484fSRodney W. Grimes 	if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
69558f0484fSRodney W. Grimes 		EMIT(OBOW, 0);
69658f0484fSRodney W. Grimes 		NEXTn(6);
69758f0484fSRodney W. Grimes 		return;
69858f0484fSRodney W. Grimes 	}
69958f0484fSRodney W. Grimes 	if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
70058f0484fSRodney W. Grimes 		EMIT(OEOW, 0);
70158f0484fSRodney W. Grimes 		NEXTn(6);
70258f0484fSRodney W. Grimes 		return;
70358f0484fSRodney W. Grimes 	}
70458f0484fSRodney W. Grimes 
705e5996857STim J. Robbins 	if ((cs = allocset(p)) == NULL)
706e5996857STim J. Robbins 		return;
707e5996857STim J. Robbins 
708e5996857STim J. Robbins 	if (p->g->cflags&REG_ICASE)
709e5996857STim J. Robbins 		cs->icase = 1;
71058f0484fSRodney W. Grimes 	if (EAT('^'))
711e5996857STim J. Robbins 		cs->invert = 1;
71258f0484fSRodney W. Grimes 	if (EAT(']'))
713e5996857STim J. Robbins 		CHadd(p, cs, ']');
71458f0484fSRodney W. Grimes 	else if (EAT('-'))
715e5996857STim J. Robbins 		CHadd(p, cs, '-');
71658f0484fSRodney W. Grimes 	while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
71758f0484fSRodney W. Grimes 		p_b_term(p, cs);
71858f0484fSRodney W. Grimes 	if (EAT('-'))
719e5996857STim J. Robbins 		CHadd(p, cs, '-');
72051295a4dSJordan K. Hubbard 	(void)MUSTEAT(']', REG_EBRACK);
72158f0484fSRodney W. Grimes 
72258f0484fSRodney W. Grimes 	if (p->error != 0)	/* don't mess things up further */
72358f0484fSRodney W. Grimes 		return;
72458f0484fSRodney W. Grimes 
725e5996857STim J. Robbins 	if (cs->invert && p->g->cflags&REG_NEWLINE)
726e5996857STim J. Robbins 		cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
72758f0484fSRodney W. Grimes 
728e5996857STim J. Robbins 	if ((ch = singleton(cs)) != OUT) {	/* optimize singleton sets */
729e5996857STim J. Robbins 		ordinary(p, ch);
73058f0484fSRodney W. Grimes 		freeset(p, cs);
73158f0484fSRodney W. Grimes 	} else
732e5996857STim J. Robbins 		EMIT(OANYOF, (int)(cs - p->g->sets));
73358f0484fSRodney W. Grimes }
73458f0484fSRodney W. Grimes 
73558f0484fSRodney W. Grimes /*
73658f0484fSRodney W. Grimes  - p_b_term - parse one term of a bracketed character list
7378fb3f3f6SDavid E. O'Brien  == static void p_b_term(struct parse *p, cset *cs);
73858f0484fSRodney W. Grimes  */
73958f0484fSRodney W. Grimes static void
74058f0484fSRodney W. Grimes p_b_term(p, cs)
7418fb3f3f6SDavid E. O'Brien struct parse *p;
7428fb3f3f6SDavid E. O'Brien cset *cs;
74358f0484fSRodney W. Grimes {
7448fb3f3f6SDavid E. O'Brien 	char c;
745e5996857STim J. Robbins 	wint_t start, finish;
746e5996857STim J. Robbins 	wint_t i;
74758f0484fSRodney W. Grimes 
74858f0484fSRodney W. Grimes 	/* classify what we've got */
74958f0484fSRodney W. Grimes 	switch ((MORE()) ? PEEK() : '\0') {
75058f0484fSRodney W. Grimes 	case '[':
75158f0484fSRodney W. Grimes 		c = (MORE2()) ? PEEK2() : '\0';
75258f0484fSRodney W. Grimes 		break;
75358f0484fSRodney W. Grimes 	case '-':
75458f0484fSRodney W. Grimes 		SETERROR(REG_ERANGE);
75558f0484fSRodney W. Grimes 		return;			/* NOTE RETURN */
75658f0484fSRodney W. Grimes 		break;
75758f0484fSRodney W. Grimes 	default:
75858f0484fSRodney W. Grimes 		c = '\0';
75958f0484fSRodney W. Grimes 		break;
76058f0484fSRodney W. Grimes 	}
76158f0484fSRodney W. Grimes 
76258f0484fSRodney W. Grimes 	switch (c) {
76358f0484fSRodney W. Grimes 	case ':':		/* character class */
76458f0484fSRodney W. Grimes 		NEXT2();
76551295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
76658f0484fSRodney W. Grimes 		c = PEEK();
76751295a4dSJordan K. Hubbard 		(void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
76858f0484fSRodney W. Grimes 		p_b_cclass(p, cs);
76951295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
77051295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
77158f0484fSRodney W. Grimes 		break;
77258f0484fSRodney W. Grimes 	case '=':		/* equivalence class */
77358f0484fSRodney W. Grimes 		NEXT2();
77451295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
77558f0484fSRodney W. Grimes 		c = PEEK();
77651295a4dSJordan K. Hubbard 		(void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
77758f0484fSRodney W. Grimes 		p_b_eclass(p, cs);
77851295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
77951295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
78058f0484fSRodney W. Grimes 		break;
78158f0484fSRodney W. Grimes 	default:		/* symbol, ordinary character, or range */
78258f0484fSRodney W. Grimes 		start = p_b_symbol(p);
78358f0484fSRodney W. Grimes 		if (SEE('-') && MORE2() && PEEK2() != ']') {
78458f0484fSRodney W. Grimes 			/* range */
78558f0484fSRodney W. Grimes 			NEXT();
78658f0484fSRodney W. Grimes 			if (EAT('-'))
78758f0484fSRodney W. Grimes 				finish = '-';
78858f0484fSRodney W. Grimes 			else
78958f0484fSRodney W. Grimes 				finish = p_b_symbol(p);
79058f0484fSRodney W. Grimes 		} else
79158f0484fSRodney W. Grimes 			finish = start;
7925c551438SAndrey A. Chernov 		if (start == finish)
793e5996857STim J. Robbins 			CHadd(p, cs, start);
7945c551438SAndrey A. Chernov 		else {
795b5a6eb18SAndrey A. Chernov 			if (__collate_load_error) {
796b5a6eb18SAndrey A. Chernov 				(void)REQUIRE((uch)start <= (uch)finish, REG_ERANGE);
797e5996857STim J. Robbins 				CHaddrange(p, cs, start, finish);
798b5a6eb18SAndrey A. Chernov 			} else {
799c61cea72SAndrey A. Chernov 				(void)REQUIRE(__collate_range_cmp(start, finish) <= 0, REG_ERANGE);
800e5996857STim J. Robbins 				for (i = 0; i <= UCHAR_MAX; i++) {
801c61cea72SAndrey A. Chernov 					if (   __collate_range_cmp(start, i) <= 0
802c61cea72SAndrey A. Chernov 					    && __collate_range_cmp(i, finish) <= 0
8035c551438SAndrey A. Chernov 					   )
804e5996857STim J. Robbins 						CHadd(p, cs, i);
8055c551438SAndrey A. Chernov 				}
8065c551438SAndrey A. Chernov 			}
807b5a6eb18SAndrey A. Chernov 		}
80858f0484fSRodney W. Grimes 		break;
80958f0484fSRodney W. Grimes 	}
81058f0484fSRodney W. Grimes }
81158f0484fSRodney W. Grimes 
81258f0484fSRodney W. Grimes /*
81358f0484fSRodney W. Grimes  - p_b_cclass - parse a character-class name and deal with it
8148fb3f3f6SDavid E. O'Brien  == static void p_b_cclass(struct parse *p, cset *cs);
81558f0484fSRodney W. Grimes  */
81658f0484fSRodney W. Grimes static void
81758f0484fSRodney W. Grimes p_b_cclass(p, cs)
8188fb3f3f6SDavid E. O'Brien struct parse *p;
8198fb3f3f6SDavid E. O'Brien cset *cs;
82058f0484fSRodney W. Grimes {
8218fb3f3f6SDavid E. O'Brien 	char *sp = p->next;
8228fb3f3f6SDavid E. O'Brien 	size_t len;
823e5996857STim J. Robbins 	wctype_t wct;
824e5996857STim J. Robbins 	char clname[16];
82558f0484fSRodney W. Grimes 
826b5363c4aSAndrey A. Chernov 	while (MORE() && isalpha((uch)PEEK()))
82758f0484fSRodney W. Grimes 		NEXT();
82858f0484fSRodney W. Grimes 	len = p->next - sp;
829e5996857STim J. Robbins 	if (len >= sizeof(clname) - 1) {
83058f0484fSRodney W. Grimes 		SETERROR(REG_ECTYPE);
83158f0484fSRodney W. Grimes 		return;
83258f0484fSRodney W. Grimes 	}
833e5996857STim J. Robbins 	memcpy(clname, sp, len);
834e5996857STim J. Robbins 	clname[len] = '\0';
835e5996857STim J. Robbins 	if ((wct = wctype(clname)) == 0) {
836e5996857STim J. Robbins 		SETERROR(REG_ECTYPE);
837e5996857STim J. Robbins 		return;
838b5363c4aSAndrey A. Chernov 	}
839e5996857STim J. Robbins 	CHaddtype(p, cs, wct);
84058f0484fSRodney W. Grimes }
84158f0484fSRodney W. Grimes 
84258f0484fSRodney W. Grimes /*
84358f0484fSRodney W. Grimes  - p_b_eclass - parse an equivalence-class name and deal with it
8448fb3f3f6SDavid E. O'Brien  == static void p_b_eclass(struct parse *p, cset *cs);
84558f0484fSRodney W. Grimes  *
84658f0484fSRodney W. Grimes  * This implementation is incomplete. xxx
84758f0484fSRodney W. Grimes  */
84858f0484fSRodney W. Grimes static void
84958f0484fSRodney W. Grimes p_b_eclass(p, cs)
8508fb3f3f6SDavid E. O'Brien struct parse *p;
8518fb3f3f6SDavid E. O'Brien cset *cs;
85258f0484fSRodney W. Grimes {
853e5996857STim J. Robbins 	wint_t c;
85458f0484fSRodney W. Grimes 
85558f0484fSRodney W. Grimes 	c = p_b_coll_elem(p, '=');
856e5996857STim J. Robbins 	CHadd(p, cs, c);
85758f0484fSRodney W. Grimes }
85858f0484fSRodney W. Grimes 
85958f0484fSRodney W. Grimes /*
86058f0484fSRodney W. Grimes  - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
8618fb3f3f6SDavid E. O'Brien  == static char p_b_symbol(struct parse *p);
86258f0484fSRodney W. Grimes  */
863e5996857STim J. Robbins static wint_t			/* value of symbol */
86458f0484fSRodney W. Grimes p_b_symbol(p)
8658fb3f3f6SDavid E. O'Brien struct parse *p;
86658f0484fSRodney W. Grimes {
867e5996857STim J. Robbins 	wint_t value;
86858f0484fSRodney W. Grimes 
86951295a4dSJordan K. Hubbard 	(void)REQUIRE(MORE(), REG_EBRACK);
87058f0484fSRodney W. Grimes 	if (!EATTWO('[', '.'))
871e5996857STim J. Robbins 		return(WGETNEXT());
87258f0484fSRodney W. Grimes 
87358f0484fSRodney W. Grimes 	/* collating symbol */
87458f0484fSRodney W. Grimes 	value = p_b_coll_elem(p, '.');
87551295a4dSJordan K. Hubbard 	(void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
87658f0484fSRodney W. Grimes 	return(value);
87758f0484fSRodney W. Grimes }
87858f0484fSRodney W. Grimes 
87958f0484fSRodney W. Grimes /*
88058f0484fSRodney W. Grimes  - p_b_coll_elem - parse a collating-element name and look it up
8818fb3f3f6SDavid E. O'Brien  == static char p_b_coll_elem(struct parse *p, int endc);
88258f0484fSRodney W. Grimes  */
883e5996857STim J. Robbins static wint_t			/* value of collating element */
88458f0484fSRodney W. Grimes p_b_coll_elem(p, endc)
8858fb3f3f6SDavid E. O'Brien struct parse *p;
886e5996857STim J. Robbins wint_t endc;			/* name ended by endc,']' */
88758f0484fSRodney W. Grimes {
8888fb3f3f6SDavid E. O'Brien 	char *sp = p->next;
8898fb3f3f6SDavid E. O'Brien 	struct cname *cp;
8908fb3f3f6SDavid E. O'Brien 	int len;
891e5996857STim J. Robbins 	mbstate_t mbs;
892e5996857STim J. Robbins 	wchar_t wc;
893e5996857STim J. Robbins 	size_t clen;
89458f0484fSRodney W. Grimes 
89558f0484fSRodney W. Grimes 	while (MORE() && !SEETWO(endc, ']'))
89658f0484fSRodney W. Grimes 		NEXT();
89758f0484fSRodney W. Grimes 	if (!MORE()) {
89858f0484fSRodney W. Grimes 		SETERROR(REG_EBRACK);
89958f0484fSRodney W. Grimes 		return(0);
90058f0484fSRodney W. Grimes 	}
90158f0484fSRodney W. Grimes 	len = p->next - sp;
90258f0484fSRodney W. Grimes 	for (cp = cnames; cp->name != NULL; cp++)
90358f0484fSRodney W. Grimes 		if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
90458f0484fSRodney W. Grimes 			return(cp->code);	/* known name */
905e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
906e5996857STim J. Robbins 	if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
907e5996857STim J. Robbins 		return (wc);			/* single character */
908e5996857STim J. Robbins 	else if (clen == (size_t)-1 || clen == (size_t)-2)
909e5996857STim J. Robbins 		SETERROR(REG_ILLSEQ);
910e5996857STim J. Robbins 	else
91158f0484fSRodney W. Grimes 		SETERROR(REG_ECOLLATE);		/* neither */
91258f0484fSRodney W. Grimes 	return(0);
91358f0484fSRodney W. Grimes }
91458f0484fSRodney W. Grimes 
91558f0484fSRodney W. Grimes /*
91658f0484fSRodney W. Grimes  - othercase - return the case counterpart of an alphabetic
91758f0484fSRodney W. Grimes  == static char othercase(int ch);
91858f0484fSRodney W. Grimes  */
919e5996857STim J. Robbins static wint_t			/* if no counterpart, return ch */
92058f0484fSRodney W. Grimes othercase(ch)
921e5996857STim J. Robbins wint_t ch;
92258f0484fSRodney W. Grimes {
923e5996857STim J. Robbins 	assert(iswalpha(ch));
924e5996857STim J. Robbins 	if (iswupper(ch))
925e5996857STim J. Robbins 		return(towlower(ch));
926e5996857STim J. Robbins 	else if (iswlower(ch))
927e5996857STim J. Robbins 		return(towupper(ch));
92858f0484fSRodney W. Grimes 	else			/* peculiar, but could happen */
92958f0484fSRodney W. Grimes 		return(ch);
93058f0484fSRodney W. Grimes }
93158f0484fSRodney W. Grimes 
93258f0484fSRodney W. Grimes /*
93358f0484fSRodney W. Grimes  - bothcases - emit a dualcase version of a two-case character
9348fb3f3f6SDavid E. O'Brien  == static void bothcases(struct parse *p, int ch);
93558f0484fSRodney W. Grimes  *
93658f0484fSRodney W. Grimes  * Boy, is this implementation ever a kludge...
93758f0484fSRodney W. Grimes  */
93858f0484fSRodney W. Grimes static void
93958f0484fSRodney W. Grimes bothcases(p, ch)
9408fb3f3f6SDavid E. O'Brien struct parse *p;
941e5996857STim J. Robbins wint_t ch;
94258f0484fSRodney W. Grimes {
9438fb3f3f6SDavid E. O'Brien 	char *oldnext = p->next;
9448fb3f3f6SDavid E. O'Brien 	char *oldend = p->end;
945e5996857STim J. Robbins 	char bracket[3 + MB_LEN_MAX];
946e5996857STim J. Robbins 	size_t n;
947e5996857STim J. Robbins 	mbstate_t mbs;
94858f0484fSRodney W. Grimes 
94958f0484fSRodney W. Grimes 	assert(othercase(ch) != ch);	/* p_bracket() would recurse */
95058f0484fSRodney W. Grimes 	p->next = bracket;
951e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
952e5996857STim J. Robbins 	n = wcrtomb(bracket, ch, &mbs);
953e5996857STim J. Robbins 	assert(n != (size_t)-1);
954e5996857STim J. Robbins 	bracket[n] = ']';
955e5996857STim J. Robbins 	bracket[n + 1] = '\0';
956e5996857STim J. Robbins 	p->end = bracket+n+1;
95758f0484fSRodney W. Grimes 	p_bracket(p);
958e5996857STim J. Robbins 	assert(p->next == p->end);
95958f0484fSRodney W. Grimes 	p->next = oldnext;
96058f0484fSRodney W. Grimes 	p->end = oldend;
96158f0484fSRodney W. Grimes }
96258f0484fSRodney W. Grimes 
96358f0484fSRodney W. Grimes /*
96458f0484fSRodney W. Grimes  - ordinary - emit an ordinary character
9658fb3f3f6SDavid E. O'Brien  == static void ordinary(struct parse *p, int ch);
96658f0484fSRodney W. Grimes  */
96758f0484fSRodney W. Grimes static void
96858f0484fSRodney W. Grimes ordinary(p, ch)
9698fb3f3f6SDavid E. O'Brien struct parse *p;
970e5996857STim J. Robbins wint_t ch;
97158f0484fSRodney W. Grimes {
972e5996857STim J. Robbins 	cset *cs;
97358f0484fSRodney W. Grimes 
974e5996857STim J. Robbins 	if ((p->g->cflags&REG_ICASE) && iswalpha(ch) && othercase(ch) != ch)
97558f0484fSRodney W. Grimes 		bothcases(p, ch);
976e5996857STim J. Robbins 	else if ((ch & OPDMASK) == ch)
977e5996857STim J. Robbins 		EMIT(OCHAR, ch);
978e5996857STim J. Robbins 	else {
979e5996857STim J. Robbins 		/*
980e5996857STim J. Robbins 		 * Kludge: character is too big to fit into an OCHAR operand.
981e5996857STim J. Robbins 		 * Emit a singleton set.
982e5996857STim J. Robbins 		 */
983e5996857STim J. Robbins 		if ((cs = allocset(p)) == NULL)
984e5996857STim J. Robbins 			return;
985e5996857STim J. Robbins 		CHadd(p, cs, ch);
986e5996857STim J. Robbins 		EMIT(OANYOF, (int)(cs - p->g->sets));
987e5996857STim J. Robbins 	}
98858f0484fSRodney W. Grimes }
98958f0484fSRodney W. Grimes 
99058f0484fSRodney W. Grimes /*
99158f0484fSRodney W. Grimes  - nonnewline - emit REG_NEWLINE version of OANY
9928fb3f3f6SDavid E. O'Brien  == static void nonnewline(struct parse *p);
99358f0484fSRodney W. Grimes  *
99458f0484fSRodney W. Grimes  * Boy, is this implementation ever a kludge...
99558f0484fSRodney W. Grimes  */
99658f0484fSRodney W. Grimes static void
99758f0484fSRodney W. Grimes nonnewline(p)
9988fb3f3f6SDavid E. O'Brien struct parse *p;
99958f0484fSRodney W. Grimes {
10008fb3f3f6SDavid E. O'Brien 	char *oldnext = p->next;
10018fb3f3f6SDavid E. O'Brien 	char *oldend = p->end;
100258f0484fSRodney W. Grimes 	char bracket[4];
100358f0484fSRodney W. Grimes 
100458f0484fSRodney W. Grimes 	p->next = bracket;
100558f0484fSRodney W. Grimes 	p->end = bracket+3;
100658f0484fSRodney W. Grimes 	bracket[0] = '^';
100758f0484fSRodney W. Grimes 	bracket[1] = '\n';
100858f0484fSRodney W. Grimes 	bracket[2] = ']';
100958f0484fSRodney W. Grimes 	bracket[3] = '\0';
101058f0484fSRodney W. Grimes 	p_bracket(p);
101158f0484fSRodney W. Grimes 	assert(p->next == bracket+3);
101258f0484fSRodney W. Grimes 	p->next = oldnext;
101358f0484fSRodney W. Grimes 	p->end = oldend;
101458f0484fSRodney W. Grimes }
101558f0484fSRodney W. Grimes 
101658f0484fSRodney W. Grimes /*
101758f0484fSRodney W. Grimes  - repeat - generate code for a bounded repetition, recursively if needed
10188fb3f3f6SDavid E. O'Brien  == static void repeat(struct parse *p, sopno start, int from, int to);
101958f0484fSRodney W. Grimes  */
102058f0484fSRodney W. Grimes static void
102158f0484fSRodney W. Grimes repeat(p, start, from, to)
10228fb3f3f6SDavid E. O'Brien struct parse *p;
102358f0484fSRodney W. Grimes sopno start;			/* operand from here to end of strip */
102458f0484fSRodney W. Grimes int from;			/* repeated from this number */
102558f0484fSRodney W. Grimes int to;				/* to this number of times (maybe INFINITY) */
102658f0484fSRodney W. Grimes {
10278fb3f3f6SDavid E. O'Brien 	sopno finish = HERE();
102858f0484fSRodney W. Grimes #	define	N	2
102958f0484fSRodney W. Grimes #	define	INF	3
103058f0484fSRodney W. Grimes #	define	REP(f, t)	((f)*8 + (t))
103158f0484fSRodney W. Grimes #	define	MAP(n)	(((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
10328fb3f3f6SDavid E. O'Brien 	sopno copy;
103358f0484fSRodney W. Grimes 
103458f0484fSRodney W. Grimes 	if (p->error != 0)	/* head off possible runaway recursion */
103558f0484fSRodney W. Grimes 		return;
103658f0484fSRodney W. Grimes 
103758f0484fSRodney W. Grimes 	assert(from <= to);
103858f0484fSRodney W. Grimes 
103958f0484fSRodney W. Grimes 	switch (REP(MAP(from), MAP(to))) {
104058f0484fSRodney W. Grimes 	case REP(0, 0):			/* must be user doing this */
104158f0484fSRodney W. Grimes 		DROP(finish-start);	/* drop the operand */
104258f0484fSRodney W. Grimes 		break;
104358f0484fSRodney W. Grimes 	case REP(0, 1):			/* as x{1,1}? */
104458f0484fSRodney W. Grimes 	case REP(0, N):			/* as x{1,n}? */
104558f0484fSRodney W. Grimes 	case REP(0, INF):		/* as x{1,}? */
104658f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
104758f0484fSRodney W. Grimes 		INSERT(OCH_, start);		/* offset is wrong... */
104858f0484fSRodney W. Grimes 		repeat(p, start+1, 1, to);
104958f0484fSRodney W. Grimes 		ASTERN(OOR1, start);
105058f0484fSRodney W. Grimes 		AHEAD(start);			/* ... fix it */
105158f0484fSRodney W. Grimes 		EMIT(OOR2, 0);
105258f0484fSRodney W. Grimes 		AHEAD(THERE());
105358f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
105458f0484fSRodney W. Grimes 		break;
105558f0484fSRodney W. Grimes 	case REP(1, 1):			/* trivial case */
105658f0484fSRodney W. Grimes 		/* done */
105758f0484fSRodney W. Grimes 		break;
105858f0484fSRodney W. Grimes 	case REP(1, N):			/* as x?x{1,n-1} */
105958f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
106058f0484fSRodney W. Grimes 		INSERT(OCH_, start);
106158f0484fSRodney W. Grimes 		ASTERN(OOR1, start);
106258f0484fSRodney W. Grimes 		AHEAD(start);
106358f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset very wrong... */
106458f0484fSRodney W. Grimes 		AHEAD(THERE());			/* ...so fix it */
106558f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
106658f0484fSRodney W. Grimes 		copy = dupl(p, start+1, finish+1);
106758f0484fSRodney W. Grimes 		assert(copy == finish+4);
106858f0484fSRodney W. Grimes 		repeat(p, copy, 1, to-1);
106958f0484fSRodney W. Grimes 		break;
107058f0484fSRodney W. Grimes 	case REP(1, INF):		/* as x+ */
107158f0484fSRodney W. Grimes 		INSERT(OPLUS_, start);
107258f0484fSRodney W. Grimes 		ASTERN(O_PLUS, start);
107358f0484fSRodney W. Grimes 		break;
107458f0484fSRodney W. Grimes 	case REP(N, N):			/* as xx{m-1,n-1} */
107558f0484fSRodney W. Grimes 		copy = dupl(p, start, finish);
107658f0484fSRodney W. Grimes 		repeat(p, copy, from-1, to-1);
107758f0484fSRodney W. Grimes 		break;
107858f0484fSRodney W. Grimes 	case REP(N, INF):		/* as xx{n-1,INF} */
107958f0484fSRodney W. Grimes 		copy = dupl(p, start, finish);
108058f0484fSRodney W. Grimes 		repeat(p, copy, from-1, to);
108158f0484fSRodney W. Grimes 		break;
108258f0484fSRodney W. Grimes 	default:			/* "can't happen" */
108358f0484fSRodney W. Grimes 		SETERROR(REG_ASSERT);	/* just in case */
108458f0484fSRodney W. Grimes 		break;
108558f0484fSRodney W. Grimes 	}
108658f0484fSRodney W. Grimes }
108758f0484fSRodney W. Grimes 
108858f0484fSRodney W. Grimes /*
1089e5996857STim J. Robbins  - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1090e5996857STim J. Robbins  - character from the parse struct, signals a REG_ILLSEQ error if the
1091e5996857STim J. Robbins  - character can't be converted. Returns the number of bytes consumed.
1092e5996857STim J. Robbins  */
1093e5996857STim J. Robbins static wint_t
1094e5996857STim J. Robbins wgetnext(p)
1095e5996857STim J. Robbins struct parse *p;
1096e5996857STim J. Robbins {
1097e5996857STim J. Robbins 	mbstate_t mbs;
1098e5996857STim J. Robbins 	wchar_t wc;
1099e5996857STim J. Robbins 	size_t n;
1100e5996857STim J. Robbins 
1101e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1102e5996857STim J. Robbins 	n = mbrtowc(&wc, p->next, p->end - p->next, &mbs);
1103e5996857STim J. Robbins 	if (n == (size_t)-1 || n == (size_t)-2) {
1104e5996857STim J. Robbins 		SETERROR(REG_ILLSEQ);
1105e5996857STim J. Robbins 		return (0);
1106e5996857STim J. Robbins 	}
1107e5996857STim J. Robbins 	if (n == 0)
1108e5996857STim J. Robbins 		n = 1;
1109e5996857STim J. Robbins 	p->next += n;
1110e5996857STim J. Robbins 	return (wc);
1111e5996857STim J. Robbins }
1112e5996857STim J. Robbins 
1113e5996857STim J. Robbins /*
111458f0484fSRodney W. Grimes  - seterr - set an error condition
11158fb3f3f6SDavid E. O'Brien  == static int seterr(struct parse *p, int e);
111658f0484fSRodney W. Grimes  */
111758f0484fSRodney W. Grimes static int			/* useless but makes type checking happy */
111858f0484fSRodney W. Grimes seterr(p, e)
11198fb3f3f6SDavid E. O'Brien struct parse *p;
112058f0484fSRodney W. Grimes int e;
112158f0484fSRodney W. Grimes {
112258f0484fSRodney W. Grimes 	if (p->error == 0)	/* keep earliest error condition */
112358f0484fSRodney W. Grimes 		p->error = e;
112458f0484fSRodney W. Grimes 	p->next = nuls;		/* try to bring things to a halt */
112558f0484fSRodney W. Grimes 	p->end = nuls;
112658f0484fSRodney W. Grimes 	return(0);		/* make the return value well-defined */
112758f0484fSRodney W. Grimes }
112858f0484fSRodney W. Grimes 
112958f0484fSRodney W. Grimes /*
113058f0484fSRodney W. Grimes  - allocset - allocate a set of characters for []
11318fb3f3f6SDavid E. O'Brien  == static cset *allocset(struct parse *p);
113258f0484fSRodney W. Grimes  */
113358f0484fSRodney W. Grimes static cset *
113458f0484fSRodney W. Grimes allocset(p)
11358fb3f3f6SDavid E. O'Brien struct parse *p;
113658f0484fSRodney W. Grimes {
1137e5996857STim J. Robbins 	cset *cs, *ncs;
113858f0484fSRodney W. Grimes 
1139e5996857STim J. Robbins 	ncs = realloc(p->g->sets, (p->g->ncsets + 1) * sizeof(*ncs));
1140e5996857STim J. Robbins 	if (ncs == NULL) {
114158f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
1142e5996857STim J. Robbins 		return (NULL);
114358f0484fSRodney W. Grimes 	}
1144e5996857STim J. Robbins 	p->g->sets = ncs;
1145e5996857STim J. Robbins 	cs = &p->g->sets[p->g->ncsets++];
1146e5996857STim J. Robbins 	memset(cs, 0, sizeof(*cs));
114758f0484fSRodney W. Grimes 
114858f0484fSRodney W. Grimes 	return(cs);
114958f0484fSRodney W. Grimes }
115058f0484fSRodney W. Grimes 
115158f0484fSRodney W. Grimes /*
115258f0484fSRodney W. Grimes  - freeset - free a now-unused set
11538fb3f3f6SDavid E. O'Brien  == static void freeset(struct parse *p, cset *cs);
115458f0484fSRodney W. Grimes  */
115558f0484fSRodney W. Grimes static void
115658f0484fSRodney W. Grimes freeset(p, cs)
11578fb3f3f6SDavid E. O'Brien struct parse *p;
11588fb3f3f6SDavid E. O'Brien cset *cs;
115958f0484fSRodney W. Grimes {
11608fb3f3f6SDavid E. O'Brien 	cset *top = &p->g->sets[p->g->ncsets];
116158f0484fSRodney W. Grimes 
1162e5996857STim J. Robbins 	free(cs->wides);
1163e5996857STim J. Robbins 	free(cs->ranges);
1164e5996857STim J. Robbins 	free(cs->types);
1165e5996857STim J. Robbins 	memset(cs, 0, sizeof(*cs));
116658f0484fSRodney W. Grimes 	if (cs == top-1)	/* recover only the easy case */
116758f0484fSRodney W. Grimes 		p->g->ncsets--;
116858f0484fSRodney W. Grimes }
116958f0484fSRodney W. Grimes 
117058f0484fSRodney W. Grimes /*
1171e5996857STim J. Robbins  - singleton - Determine whether a set contains only one character,
1172e5996857STim J. Robbins  - returning it if so, otherwise returning OUT.
117358f0484fSRodney W. Grimes  */
1174e5996857STim J. Robbins static wint_t
1175e5996857STim J. Robbins singleton(cs)
11768fb3f3f6SDavid E. O'Brien cset *cs;
117758f0484fSRodney W. Grimes {
1178e5996857STim J. Robbins 	wint_t i, s, n;
117958f0484fSRodney W. Grimes 
1180e5996857STim J. Robbins 	for (i = n = 0; i < NC; i++)
1181e5996857STim J. Robbins 		if (CHIN(cs, i)) {
118258f0484fSRodney W. Grimes 			n++;
1183e5996857STim J. Robbins 			s = i;
1184e5996857STim J. Robbins 		}
1185e5996857STim J. Robbins 	if (n == 1)
1186e5996857STim J. Robbins 		return (s);
1187e5996857STim J. Robbins 	if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 &&
1188e5996857STim J. Robbins 	    cs->icase == 0)
1189e5996857STim J. Robbins 		return (cs->wides[0]);
1190e5996857STim J. Robbins 	/* Don't bother handling the other cases. */
1191e5996857STim J. Robbins 	return (OUT);
1192e5996857STim J. Robbins }
1193e5996857STim J. Robbins 
1194e5996857STim J. Robbins /*
1195e5996857STim J. Robbins  - CHadd - add character to character set.
1196e5996857STim J. Robbins  */
1197e5996857STim J. Robbins static void
1198e5996857STim J. Robbins CHadd(p, cs, ch)
1199e5996857STim J. Robbins struct parse *p;
1200e5996857STim J. Robbins cset *cs;
1201e5996857STim J. Robbins wint_t ch;
1202e5996857STim J. Robbins {
120333176f17STim J. Robbins 	wint_t nch, *newwides;
1204e5996857STim J. Robbins 	assert(ch >= 0);
120533176f17STim J. Robbins 	if (ch < NC)
1206e5996857STim J. Robbins 		cs->bmp[ch >> 3] |= 1 << (ch & 7);
120733176f17STim J. Robbins 	else {
1208e5996857STim J. Robbins 		newwides = realloc(cs->wides, (cs->nwides + 1) *
1209e5996857STim J. Robbins 		    sizeof(*cs->wides));
1210e5996857STim J. Robbins 		if (newwides == NULL) {
1211e5996857STim J. Robbins 			SETERROR(REG_ESPACE);
1212e5996857STim J. Robbins 			return;
1213e5996857STim J. Robbins 		}
1214e5996857STim J. Robbins 		cs->wides = newwides;
1215e5996857STim J. Robbins 		cs->wides[cs->nwides++] = ch;
1216e5996857STim J. Robbins 	}
121733176f17STim J. Robbins 	if (cs->icase) {
121833176f17STim J. Robbins 		if ((nch = towlower(ch)) < NC)
121933176f17STim J. Robbins 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
122033176f17STim J. Robbins 		if ((nch = towupper(ch)) < NC)
122133176f17STim J. Robbins 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
122233176f17STim J. Robbins 	}
1223e5996857STim J. Robbins }
1224e5996857STim J. Robbins 
1225e5996857STim J. Robbins /*
1226e5996857STim J. Robbins  - CHaddrange - add all characters in the range [min,max] to a character set.
1227e5996857STim J. Robbins  */
1228e5996857STim J. Robbins static void
1229e5996857STim J. Robbins CHaddrange(p, cs, min, max)
1230e5996857STim J. Robbins struct parse *p;
1231e5996857STim J. Robbins cset *cs;
1232e5996857STim J. Robbins wint_t min, max;
1233e5996857STim J. Robbins {
1234e5996857STim J. Robbins 	crange *newranges;
1235e5996857STim J. Robbins 
1236e5996857STim J. Robbins 	for (; min < NC && min <= max; min++)
1237e5996857STim J. Robbins 		CHadd(p, cs, min);
1238e5996857STim J. Robbins 	if (min >= max)
1239e5996857STim J. Robbins 		return;
1240e5996857STim J. Robbins 	newranges = realloc(cs->ranges, (cs->nranges + 1) *
1241e5996857STim J. Robbins 	    sizeof(*cs->ranges));
1242e5996857STim J. Robbins 	if (newranges == NULL) {
1243e5996857STim J. Robbins 		SETERROR(REG_ESPACE);
1244e5996857STim J. Robbins 		return;
1245e5996857STim J. Robbins 	}
1246e5996857STim J. Robbins 	cs->ranges = newranges;
1247e5996857STim J. Robbins 	cs->ranges[cs->nranges].min = min;
1248e5996857STim J. Robbins 	cs->ranges[cs->nranges].min = max;
1249e5996857STim J. Robbins 	cs->nranges++;
1250e5996857STim J. Robbins }
1251e5996857STim J. Robbins 
1252e5996857STim J. Robbins /*
1253e5996857STim J. Robbins  - CHaddtype - add all characters of a certain type to a character set.
1254e5996857STim J. Robbins  */
1255e5996857STim J. Robbins static void
1256e5996857STim J. Robbins CHaddtype(p, cs, wct)
1257e5996857STim J. Robbins struct parse *p;
1258e5996857STim J. Robbins cset *cs;
1259e5996857STim J. Robbins wctype_t wct;
1260e5996857STim J. Robbins {
1261e5996857STim J. Robbins 	wint_t i;
1262e5996857STim J. Robbins 	wctype_t *newtypes;
1263e5996857STim J. Robbins 
126433176f17STim J. Robbins 	for (i = 0; i < NC; i++)
1265e5996857STim J. Robbins 		if (iswctype(i, wct))
1266e5996857STim J. Robbins 			CHadd(p, cs, i);
1267e5996857STim J. Robbins 	newtypes = realloc(cs->types, (cs->ntypes + 1) *
1268e5996857STim J. Robbins 	    sizeof(*cs->types));
1269e5996857STim J. Robbins 	if (newtypes == NULL) {
1270e5996857STim J. Robbins 		SETERROR(REG_ESPACE);
1271e5996857STim J. Robbins 		return;
1272e5996857STim J. Robbins 	}
1273e5996857STim J. Robbins 	cs->types = newtypes;
1274e5996857STim J. Robbins 	cs->types[cs->ntypes++] = wct;
127558f0484fSRodney W. Grimes }
127658f0484fSRodney W. Grimes 
127758f0484fSRodney W. Grimes /*
127858f0484fSRodney W. Grimes  - dupl - emit a duplicate of a bunch of sops
12798fb3f3f6SDavid E. O'Brien  == static sopno dupl(struct parse *p, sopno start, sopno finish);
128058f0484fSRodney W. Grimes  */
128158f0484fSRodney W. Grimes static sopno			/* start of duplicate */
128258f0484fSRodney W. Grimes dupl(p, start, finish)
12838fb3f3f6SDavid E. O'Brien struct parse *p;
128458f0484fSRodney W. Grimes sopno start;			/* from here */
128558f0484fSRodney W. Grimes sopno finish;			/* to this less one */
128658f0484fSRodney W. Grimes {
12878fb3f3f6SDavid E. O'Brien 	sopno ret = HERE();
12888fb3f3f6SDavid E. O'Brien 	sopno len = finish - start;
128958f0484fSRodney W. Grimes 
129058f0484fSRodney W. Grimes 	assert(finish >= start);
129158f0484fSRodney W. Grimes 	if (len == 0)
129258f0484fSRodney W. Grimes 		return(ret);
129358f0484fSRodney W. Grimes 	enlarge(p, p->ssize + len);	/* this many unexpected additions */
129458f0484fSRodney W. Grimes 	assert(p->ssize >= p->slen + len);
129558f0484fSRodney W. Grimes 	(void) memcpy((char *)(p->strip + p->slen),
129658f0484fSRodney W. Grimes 		(char *)(p->strip + start), (size_t)len*sizeof(sop));
129758f0484fSRodney W. Grimes 	p->slen += len;
129858f0484fSRodney W. Grimes 	return(ret);
129958f0484fSRodney W. Grimes }
130058f0484fSRodney W. Grimes 
130158f0484fSRodney W. Grimes /*
130258f0484fSRodney W. Grimes  - doemit - emit a strip operator
13038fb3f3f6SDavid E. O'Brien  == static void doemit(struct parse *p, sop op, size_t opnd);
130458f0484fSRodney W. Grimes  *
130558f0484fSRodney W. Grimes  * It might seem better to implement this as a macro with a function as
130658f0484fSRodney W. Grimes  * hard-case backup, but it's just too big and messy unless there are
130758f0484fSRodney W. Grimes  * some changes to the data structures.  Maybe later.
130858f0484fSRodney W. Grimes  */
130958f0484fSRodney W. Grimes static void
131058f0484fSRodney W. Grimes doemit(p, op, opnd)
13118fb3f3f6SDavid E. O'Brien struct parse *p;
131258f0484fSRodney W. Grimes sop op;
131358f0484fSRodney W. Grimes size_t opnd;
131458f0484fSRodney W. Grimes {
131558f0484fSRodney W. Grimes 	/* avoid making error situations worse */
131658f0484fSRodney W. Grimes 	if (p->error != 0)
131758f0484fSRodney W. Grimes 		return;
131858f0484fSRodney W. Grimes 
131958f0484fSRodney W. Grimes 	/* deal with oversize operands ("can't happen", more or less) */
132058f0484fSRodney W. Grimes 	assert(opnd < 1<<OPSHIFT);
132158f0484fSRodney W. Grimes 
132258f0484fSRodney W. Grimes 	/* deal with undersized strip */
132358f0484fSRodney W. Grimes 	if (p->slen >= p->ssize)
132458f0484fSRodney W. Grimes 		enlarge(p, (p->ssize+1) / 2 * 3);	/* +50% */
132558f0484fSRodney W. Grimes 	assert(p->slen < p->ssize);
132658f0484fSRodney W. Grimes 
132758f0484fSRodney W. Grimes 	/* finally, it's all reduced to the easy case */
132858f0484fSRodney W. Grimes 	p->strip[p->slen++] = SOP(op, opnd);
132958f0484fSRodney W. Grimes }
133058f0484fSRodney W. Grimes 
133158f0484fSRodney W. Grimes /*
133258f0484fSRodney W. Grimes  - doinsert - insert a sop into the strip
13338fb3f3f6SDavid E. O'Brien  == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
133458f0484fSRodney W. Grimes  */
133558f0484fSRodney W. Grimes static void
133658f0484fSRodney W. Grimes doinsert(p, op, opnd, pos)
13378fb3f3f6SDavid E. O'Brien struct parse *p;
133858f0484fSRodney W. Grimes sop op;
133958f0484fSRodney W. Grimes size_t opnd;
134058f0484fSRodney W. Grimes sopno pos;
134158f0484fSRodney W. Grimes {
13428fb3f3f6SDavid E. O'Brien 	sopno sn;
13438fb3f3f6SDavid E. O'Brien 	sop s;
13448fb3f3f6SDavid E. O'Brien 	int i;
134558f0484fSRodney W. Grimes 
134658f0484fSRodney W. Grimes 	/* avoid making error situations worse */
134758f0484fSRodney W. Grimes 	if (p->error != 0)
134858f0484fSRodney W. Grimes 		return;
134958f0484fSRodney W. Grimes 
135058f0484fSRodney W. Grimes 	sn = HERE();
135158f0484fSRodney W. Grimes 	EMIT(op, opnd);		/* do checks, ensure space */
135258f0484fSRodney W. Grimes 	assert(HERE() == sn+1);
135358f0484fSRodney W. Grimes 	s = p->strip[sn];
135458f0484fSRodney W. Grimes 
135558f0484fSRodney W. Grimes 	/* adjust paren pointers */
135658f0484fSRodney W. Grimes 	assert(pos > 0);
135758f0484fSRodney W. Grimes 	for (i = 1; i < NPAREN; i++) {
135858f0484fSRodney W. Grimes 		if (p->pbegin[i] >= pos) {
135958f0484fSRodney W. Grimes 			p->pbegin[i]++;
136058f0484fSRodney W. Grimes 		}
136158f0484fSRodney W. Grimes 		if (p->pend[i] >= pos) {
136258f0484fSRodney W. Grimes 			p->pend[i]++;
136358f0484fSRodney W. Grimes 		}
136458f0484fSRodney W. Grimes 	}
136558f0484fSRodney W. Grimes 
136658f0484fSRodney W. Grimes 	memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
136758f0484fSRodney W. Grimes 						(HERE()-pos-1)*sizeof(sop));
136858f0484fSRodney W. Grimes 	p->strip[pos] = s;
136958f0484fSRodney W. Grimes }
137058f0484fSRodney W. Grimes 
137158f0484fSRodney W. Grimes /*
137258f0484fSRodney W. Grimes  - dofwd - complete a forward reference
13738fb3f3f6SDavid E. O'Brien  == static void dofwd(struct parse *p, sopno pos, sop value);
137458f0484fSRodney W. Grimes  */
137558f0484fSRodney W. Grimes static void
137658f0484fSRodney W. Grimes dofwd(p, pos, value)
13778fb3f3f6SDavid E. O'Brien struct parse *p;
13788fb3f3f6SDavid E. O'Brien sopno pos;
137958f0484fSRodney W. Grimes sop value;
138058f0484fSRodney W. Grimes {
138158f0484fSRodney W. Grimes 	/* avoid making error situations worse */
138258f0484fSRodney W. Grimes 	if (p->error != 0)
138358f0484fSRodney W. Grimes 		return;
138458f0484fSRodney W. Grimes 
138558f0484fSRodney W. Grimes 	assert(value < 1<<OPSHIFT);
138658f0484fSRodney W. Grimes 	p->strip[pos] = OP(p->strip[pos]) | value;
138758f0484fSRodney W. Grimes }
138858f0484fSRodney W. Grimes 
138958f0484fSRodney W. Grimes /*
139058f0484fSRodney W. Grimes  - enlarge - enlarge the strip
13918fb3f3f6SDavid E. O'Brien  == static void enlarge(struct parse *p, sopno size);
139258f0484fSRodney W. Grimes  */
139358f0484fSRodney W. Grimes static void
139458f0484fSRodney W. Grimes enlarge(p, size)
13958fb3f3f6SDavid E. O'Brien struct parse *p;
13968fb3f3f6SDavid E. O'Brien sopno size;
139758f0484fSRodney W. Grimes {
13988fb3f3f6SDavid E. O'Brien 	sop *sp;
139958f0484fSRodney W. Grimes 
140058f0484fSRodney W. Grimes 	if (p->ssize >= size)
140158f0484fSRodney W. Grimes 		return;
140258f0484fSRodney W. Grimes 
140358f0484fSRodney W. Grimes 	sp = (sop *)realloc(p->strip, size*sizeof(sop));
140458f0484fSRodney W. Grimes 	if (sp == NULL) {
140558f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
140658f0484fSRodney W. Grimes 		return;
140758f0484fSRodney W. Grimes 	}
140858f0484fSRodney W. Grimes 	p->strip = sp;
140958f0484fSRodney W. Grimes 	p->ssize = size;
141058f0484fSRodney W. Grimes }
141158f0484fSRodney W. Grimes 
141258f0484fSRodney W. Grimes /*
141358f0484fSRodney W. Grimes  - stripsnug - compact the strip
14148fb3f3f6SDavid E. O'Brien  == static void stripsnug(struct parse *p, struct re_guts *g);
141558f0484fSRodney W. Grimes  */
141658f0484fSRodney W. Grimes static void
141758f0484fSRodney W. Grimes stripsnug(p, g)
14188fb3f3f6SDavid E. O'Brien struct parse *p;
14198fb3f3f6SDavid E. O'Brien struct re_guts *g;
142058f0484fSRodney W. Grimes {
142158f0484fSRodney W. Grimes 	g->nstates = p->slen;
142258f0484fSRodney W. Grimes 	g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
142358f0484fSRodney W. Grimes 	if (g->strip == NULL) {
142458f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
142558f0484fSRodney W. Grimes 		g->strip = p->strip;
142658f0484fSRodney W. Grimes 	}
142758f0484fSRodney W. Grimes }
142858f0484fSRodney W. Grimes 
142958f0484fSRodney W. Grimes /*
143058f0484fSRodney W. Grimes  - findmust - fill in must and mlen with longest mandatory literal string
14318fb3f3f6SDavid E. O'Brien  == static void findmust(struct parse *p, struct re_guts *g);
143258f0484fSRodney W. Grimes  *
143358f0484fSRodney W. Grimes  * This algorithm could do fancy things like analyzing the operands of |
143458f0484fSRodney W. Grimes  * for common subsequences.  Someday.  This code is simple and finds most
143558f0484fSRodney W. Grimes  * of the interesting cases.
143658f0484fSRodney W. Grimes  *
143758f0484fSRodney W. Grimes  * Note that must and mlen got initialized during setup.
143858f0484fSRodney W. Grimes  */
143958f0484fSRodney W. Grimes static void
144058f0484fSRodney W. Grimes findmust(p, g)
144158f0484fSRodney W. Grimes struct parse *p;
14428fb3f3f6SDavid E. O'Brien struct re_guts *g;
144358f0484fSRodney W. Grimes {
14448fb3f3f6SDavid E. O'Brien 	sop *scan;
144558f0484fSRodney W. Grimes 	sop *start;
14468fb3f3f6SDavid E. O'Brien 	sop *newstart;
14478fb3f3f6SDavid E. O'Brien 	sopno newlen;
14488fb3f3f6SDavid E. O'Brien 	sop s;
14498fb3f3f6SDavid E. O'Brien 	char *cp;
1450e6a886d8SDaniel C. Sobral 	int offset;
1451e5996857STim J. Robbins 	char buf[MB_LEN_MAX];
1452e5996857STim J. Robbins 	size_t clen;
1453e5996857STim J. Robbins 	mbstate_t mbs;
145458f0484fSRodney W. Grimes 
145558f0484fSRodney W. Grimes 	/* avoid making error situations worse */
145658f0484fSRodney W. Grimes 	if (p->error != 0)
145758f0484fSRodney W. Grimes 		return;
145858f0484fSRodney W. Grimes 
1459e5996857STim J. Robbins 	/*
1460e5996857STim J. Robbins 	 * It's not generally safe to do a ``char'' substring search on
1461e5996857STim J. Robbins 	 * multibyte character strings, but it's safe for at least
1462e5996857STim J. Robbins 	 * UTF-8 (see RFC 3629).
1463e5996857STim J. Robbins 	 */
1464e5996857STim J. Robbins 	if (MB_CUR_MAX > 1 &&
1465e5996857STim J. Robbins 	    strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
1466e5996857STim J. Robbins 		return;
1467e5996857STim J. Robbins 
146858f0484fSRodney W. Grimes 	/* find the longest OCHAR sequence in strip */
146958f0484fSRodney W. Grimes 	newlen = 0;
1470e6a886d8SDaniel C. Sobral 	offset = 0;
1471e6a886d8SDaniel C. Sobral 	g->moffset = 0;
147258f0484fSRodney W. Grimes 	scan = g->strip + 1;
147358f0484fSRodney W. Grimes 	do {
147458f0484fSRodney W. Grimes 		s = *scan++;
147558f0484fSRodney W. Grimes 		switch (OP(s)) {
147658f0484fSRodney W. Grimes 		case OCHAR:		/* sequence member */
1477e5996857STim J. Robbins 			if (newlen == 0) {		/* new sequence */
1478e5996857STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
147958f0484fSRodney W. Grimes 				newstart = scan - 1;
1480e5996857STim J. Robbins 			}
1481e5996857STim J. Robbins 			clen = wcrtomb(buf, OPND(s), &mbs);
1482e5996857STim J. Robbins 			if (clen == (size_t)-1)
1483e5996857STim J. Robbins 				goto toohard;
1484e5996857STim J. Robbins 			newlen += clen;
148558f0484fSRodney W. Grimes 			break;
148658f0484fSRodney W. Grimes 		case OPLUS_:		/* things that don't break one */
148758f0484fSRodney W. Grimes 		case OLPAREN:
148858f0484fSRodney W. Grimes 		case ORPAREN:
148958f0484fSRodney W. Grimes 			break;
149058f0484fSRodney W. Grimes 		case OQUEST_:		/* things that must be skipped */
149158f0484fSRodney W. Grimes 		case OCH_:
149280f36366STim J. Robbins 			offset = altoffset(scan, offset);
149358f0484fSRodney W. Grimes 			scan--;
149458f0484fSRodney W. Grimes 			do {
149558f0484fSRodney W. Grimes 				scan += OPND(s);
149658f0484fSRodney W. Grimes 				s = *scan;
149758f0484fSRodney W. Grimes 				/* assert() interferes w debug printouts */
149858f0484fSRodney W. Grimes 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
149958f0484fSRodney W. Grimes 							OP(s) != OOR2) {
150058f0484fSRodney W. Grimes 					g->iflags |= BAD;
150158f0484fSRodney W. Grimes 					return;
150258f0484fSRodney W. Grimes 				}
150358f0484fSRodney W. Grimes 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
15047fed38d0SPhilippe Charnier 			/* FALLTHROUGH */
1505e6a886d8SDaniel C. Sobral 		case OBOW:		/* things that break a sequence */
1506e6a886d8SDaniel C. Sobral 		case OEOW:
1507e6a886d8SDaniel C. Sobral 		case OBOL:
1508e6a886d8SDaniel C. Sobral 		case OEOL:
1509e6a886d8SDaniel C. Sobral 		case O_QUEST:
1510e6a886d8SDaniel C. Sobral 		case O_CH:
1511e6a886d8SDaniel C. Sobral 		case OEND:
151258f0484fSRodney W. Grimes 			if (newlen > g->mlen) {		/* ends one */
151358f0484fSRodney W. Grimes 				start = newstart;
151458f0484fSRodney W. Grimes 				g->mlen = newlen;
1515e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1516e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1517e6a886d8SDaniel C. Sobral 					offset = newlen;
1518e6a886d8SDaniel C. Sobral 				} else
1519e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1520e6a886d8SDaniel C. Sobral 			} else {
1521e6a886d8SDaniel C. Sobral 				if (offset > -1)
1522e6a886d8SDaniel C. Sobral 					offset += newlen;
152358f0484fSRodney W. Grimes 			}
152458f0484fSRodney W. Grimes 			newlen = 0;
152558f0484fSRodney W. Grimes 			break;
1526e6a886d8SDaniel C. Sobral 		case OANY:
1527e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1528e6a886d8SDaniel C. Sobral 				start = newstart;
1529e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1530e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1531e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1532e6a886d8SDaniel C. Sobral 					offset = newlen;
1533e6a886d8SDaniel C. Sobral 				} else
1534e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1535e6a886d8SDaniel C. Sobral 			} else {
1536e6a886d8SDaniel C. Sobral 				if (offset > -1)
1537e6a886d8SDaniel C. Sobral 					offset += newlen;
1538e6a886d8SDaniel C. Sobral 			}
1539e6a886d8SDaniel C. Sobral 			if (offset > -1)
1540e6a886d8SDaniel C. Sobral 				offset++;
1541e6a886d8SDaniel C. Sobral 			newlen = 0;
1542e6a886d8SDaniel C. Sobral 			break;
1543e6a886d8SDaniel C. Sobral 		case OANYOF:		/* may or may not invalidate offset */
1544e6a886d8SDaniel C. Sobral 			/* First, everything as OANY */
1545e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1546e6a886d8SDaniel C. Sobral 				start = newstart;
1547e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1548e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1549e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1550e6a886d8SDaniel C. Sobral 					offset = newlen;
1551e6a886d8SDaniel C. Sobral 				} else
1552e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1553e6a886d8SDaniel C. Sobral 			} else {
1554e6a886d8SDaniel C. Sobral 				if (offset > -1)
1555e6a886d8SDaniel C. Sobral 					offset += newlen;
1556e6a886d8SDaniel C. Sobral 			}
1557e6a886d8SDaniel C. Sobral 			if (offset > -1)
1558e6a886d8SDaniel C. Sobral 				offset++;
1559e6a886d8SDaniel C. Sobral 			newlen = 0;
1560e6a886d8SDaniel C. Sobral 			break;
1561e5996857STim J. Robbins 		toohard:
1562e6a886d8SDaniel C. Sobral 		default:
1563e6a886d8SDaniel C. Sobral 			/* Anything here makes it impossible or too hard
1564e6a886d8SDaniel C. Sobral 			 * to calculate the offset -- so we give up;
1565e6a886d8SDaniel C. Sobral 			 * save the last known good offset, in case the
1566e6a886d8SDaniel C. Sobral 			 * must sequence doesn't occur later.
1567e6a886d8SDaniel C. Sobral 			 */
1568e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1569e6a886d8SDaniel C. Sobral 				start = newstart;
1570e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1571e6a886d8SDaniel C. Sobral 				if (offset > -1)
1572e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1573e6a886d8SDaniel C. Sobral 				else
1574e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1575e6a886d8SDaniel C. Sobral 			}
1576e6a886d8SDaniel C. Sobral 			offset = -1;
1577e6a886d8SDaniel C. Sobral 			newlen = 0;
1578e6a886d8SDaniel C. Sobral 			break;
157958f0484fSRodney W. Grimes 		}
158058f0484fSRodney W. Grimes 	} while (OP(s) != OEND);
158158f0484fSRodney W. Grimes 
1582e6a886d8SDaniel C. Sobral 	if (g->mlen == 0) {		/* there isn't one */
1583e6a886d8SDaniel C. Sobral 		g->moffset = -1;
158458f0484fSRodney W. Grimes 		return;
1585e6a886d8SDaniel C. Sobral 	}
158658f0484fSRodney W. Grimes 
158758f0484fSRodney W. Grimes 	/* turn it into a character string */
158858f0484fSRodney W. Grimes 	g->must = malloc((size_t)g->mlen + 1);
158958f0484fSRodney W. Grimes 	if (g->must == NULL) {		/* argh; just forget it */
159058f0484fSRodney W. Grimes 		g->mlen = 0;
1591e6a886d8SDaniel C. Sobral 		g->moffset = -1;
159258f0484fSRodney W. Grimes 		return;
159358f0484fSRodney W. Grimes 	}
159458f0484fSRodney W. Grimes 	cp = g->must;
159558f0484fSRodney W. Grimes 	scan = start;
1596e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1597e5996857STim J. Robbins 	while (cp < g->must + g->mlen) {
159858f0484fSRodney W. Grimes 		while (OP(s = *scan++) != OCHAR)
159958f0484fSRodney W. Grimes 			continue;
1600e5996857STim J. Robbins 		clen = wcrtomb(cp, OPND(s), &mbs);
1601e5996857STim J. Robbins 		assert(clen != (size_t)-1);
1602e5996857STim J. Robbins 		cp += clen;
160358f0484fSRodney W. Grimes 	}
160458f0484fSRodney W. Grimes 	assert(cp == g->must + g->mlen);
160558f0484fSRodney W. Grimes 	*cp++ = '\0';		/* just on general principles */
160658f0484fSRodney W. Grimes }
160758f0484fSRodney W. Grimes 
160858f0484fSRodney W. Grimes /*
1609e6a886d8SDaniel C. Sobral  - altoffset - choose biggest offset among multiple choices
161080f36366STim J. Robbins  == static int altoffset(sop *scan, int offset);
1611e6a886d8SDaniel C. Sobral  *
1612e6a886d8SDaniel C. Sobral  * Compute, recursively if necessary, the largest offset among multiple
1613e6a886d8SDaniel C. Sobral  * re paths.
1614e6a886d8SDaniel C. Sobral  */
1615e6a886d8SDaniel C. Sobral static int
161680f36366STim J. Robbins altoffset(scan, offset)
1617e6a886d8SDaniel C. Sobral sop *scan;
1618e6a886d8SDaniel C. Sobral int offset;
1619e6a886d8SDaniel C. Sobral {
1620e6a886d8SDaniel C. Sobral 	int largest;
1621e6a886d8SDaniel C. Sobral 	int try;
1622e6a886d8SDaniel C. Sobral 	sop s;
1623e6a886d8SDaniel C. Sobral 
1624e6a886d8SDaniel C. Sobral 	/* If we gave up already on offsets, return */
1625e6a886d8SDaniel C. Sobral 	if (offset == -1)
1626e6a886d8SDaniel C. Sobral 		return -1;
1627e6a886d8SDaniel C. Sobral 
1628e6a886d8SDaniel C. Sobral 	largest = 0;
1629e6a886d8SDaniel C. Sobral 	try = 0;
1630e6a886d8SDaniel C. Sobral 	s = *scan++;
1631e6a886d8SDaniel C. Sobral 	while (OP(s) != O_QUEST && OP(s) != O_CH) {
1632e6a886d8SDaniel C. Sobral 		switch (OP(s)) {
1633e6a886d8SDaniel C. Sobral 		case OOR1:
1634e6a886d8SDaniel C. Sobral 			if (try > largest)
1635e6a886d8SDaniel C. Sobral 				largest = try;
1636e6a886d8SDaniel C. Sobral 			try = 0;
1637e6a886d8SDaniel C. Sobral 			break;
1638e6a886d8SDaniel C. Sobral 		case OQUEST_:
1639e6a886d8SDaniel C. Sobral 		case OCH_:
164080f36366STim J. Robbins 			try = altoffset(scan, try);
1641e6a886d8SDaniel C. Sobral 			if (try == -1)
1642e6a886d8SDaniel C. Sobral 				return -1;
1643e6a886d8SDaniel C. Sobral 			scan--;
1644e6a886d8SDaniel C. Sobral 			do {
1645e6a886d8SDaniel C. Sobral 				scan += OPND(s);
1646e6a886d8SDaniel C. Sobral 				s = *scan;
1647e6a886d8SDaniel C. Sobral 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
1648e6a886d8SDaniel C. Sobral 							OP(s) != OOR2)
1649e6a886d8SDaniel C. Sobral 					return -1;
1650e6a886d8SDaniel C. Sobral 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
16518f9e434fSDaniel C. Sobral 			/* We must skip to the next position, or we'll
16528f9e434fSDaniel C. Sobral 			 * leave altoffset() too early.
16538f9e434fSDaniel C. Sobral 			 */
16548f9e434fSDaniel C. Sobral 			scan++;
1655e6a886d8SDaniel C. Sobral 			break;
1656e6a886d8SDaniel C. Sobral 		case OANYOF:
1657e6a886d8SDaniel C. Sobral 		case OCHAR:
1658e6a886d8SDaniel C. Sobral 		case OANY:
1659e6a886d8SDaniel C. Sobral 			try++;
1660e6a886d8SDaniel C. Sobral 		case OBOW:
1661e6a886d8SDaniel C. Sobral 		case OEOW:
1662e6a886d8SDaniel C. Sobral 		case OLPAREN:
1663e6a886d8SDaniel C. Sobral 		case ORPAREN:
1664e6a886d8SDaniel C. Sobral 		case OOR2:
1665e6a886d8SDaniel C. Sobral 			break;
1666e6a886d8SDaniel C. Sobral 		default:
1667e6a886d8SDaniel C. Sobral 			try = -1;
1668e6a886d8SDaniel C. Sobral 			break;
1669e6a886d8SDaniel C. Sobral 		}
1670e6a886d8SDaniel C. Sobral 		if (try == -1)
1671e6a886d8SDaniel C. Sobral 			return -1;
1672e6a886d8SDaniel C. Sobral 		s = *scan++;
1673e6a886d8SDaniel C. Sobral 	}
1674e6a886d8SDaniel C. Sobral 
1675e6a886d8SDaniel C. Sobral 	if (try > largest)
1676e6a886d8SDaniel C. Sobral 		largest = try;
1677e6a886d8SDaniel C. Sobral 
1678e6a886d8SDaniel C. Sobral 	return largest+offset;
1679e6a886d8SDaniel C. Sobral }
1680e6a886d8SDaniel C. Sobral 
1681e6a886d8SDaniel C. Sobral /*
16826049d9f0SDaniel C. Sobral  - computejumps - compute char jumps for BM scan
16838fb3f3f6SDavid E. O'Brien  == static void computejumps(struct parse *p, struct re_guts *g);
16846049d9f0SDaniel C. Sobral  *
16856049d9f0SDaniel C. Sobral  * This algorithm assumes g->must exists and is has size greater than
16866049d9f0SDaniel C. Sobral  * zero. It's based on the algorithm found on Computer Algorithms by
16876049d9f0SDaniel C. Sobral  * Sara Baase.
16886049d9f0SDaniel C. Sobral  *
16896049d9f0SDaniel C. Sobral  * A char jump is the number of characters one needs to jump based on
16906049d9f0SDaniel C. Sobral  * the value of the character from the text that was mismatched.
16916049d9f0SDaniel C. Sobral  */
16926049d9f0SDaniel C. Sobral static void
16936049d9f0SDaniel C. Sobral computejumps(p, g)
16946049d9f0SDaniel C. Sobral struct parse *p;
16956049d9f0SDaniel C. Sobral struct re_guts *g;
16966049d9f0SDaniel C. Sobral {
16976049d9f0SDaniel C. Sobral 	int ch;
16986049d9f0SDaniel C. Sobral 	int mindex;
16996049d9f0SDaniel C. Sobral 
17006049d9f0SDaniel C. Sobral 	/* Avoid making errors worse */
17016049d9f0SDaniel C. Sobral 	if (p->error != 0)
17026049d9f0SDaniel C. Sobral 		return;
17036049d9f0SDaniel C. Sobral 
1704517bffcaSDaniel C. Sobral 	g->charjump = (int*) malloc((NC + 1) * sizeof(int));
17056049d9f0SDaniel C. Sobral 	if (g->charjump == NULL)	/* Not a fatal error */
17066049d9f0SDaniel C. Sobral 		return;
1707c5e125bbSDaniel C. Sobral 	/* Adjust for signed chars, if necessary */
1708c5e125bbSDaniel C. Sobral 	g->charjump = &g->charjump[-(CHAR_MIN)];
17096049d9f0SDaniel C. Sobral 
17106049d9f0SDaniel C. Sobral 	/* If the character does not exist in the pattern, the jump
17116049d9f0SDaniel C. Sobral 	 * is equal to the number of characters in the pattern.
17126049d9f0SDaniel C. Sobral 	 */
1713c5e125bbSDaniel C. Sobral 	for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
17146049d9f0SDaniel C. Sobral 		g->charjump[ch] = g->mlen;
17156049d9f0SDaniel C. Sobral 
17166049d9f0SDaniel C. Sobral 	/* If the character does exist, compute the jump that would
17176049d9f0SDaniel C. Sobral 	 * take us to the last character in the pattern equal to it
17186049d9f0SDaniel C. Sobral 	 * (notice that we match right to left, so that last character
17196049d9f0SDaniel C. Sobral 	 * is the first one that would be matched).
17206049d9f0SDaniel C. Sobral 	 */
17216049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex < g->mlen; mindex++)
1722e0554a53SJacques Vidrine 		g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1;
17236049d9f0SDaniel C. Sobral }
17246049d9f0SDaniel C. Sobral 
17256049d9f0SDaniel C. Sobral /*
17266049d9f0SDaniel C. Sobral  - computematchjumps - compute match jumps for BM scan
17278fb3f3f6SDavid E. O'Brien  == static void computematchjumps(struct parse *p, struct re_guts *g);
17286049d9f0SDaniel C. Sobral  *
17296049d9f0SDaniel C. Sobral  * This algorithm assumes g->must exists and is has size greater than
17306049d9f0SDaniel C. Sobral  * zero. It's based on the algorithm found on Computer Algorithms by
17316049d9f0SDaniel C. Sobral  * Sara Baase.
17326049d9f0SDaniel C. Sobral  *
17336049d9f0SDaniel C. Sobral  * A match jump is the number of characters one needs to advance based
17346049d9f0SDaniel C. Sobral  * on the already-matched suffix.
17356049d9f0SDaniel C. Sobral  * Notice that all values here are minus (g->mlen-1), because of the way
17366049d9f0SDaniel C. Sobral  * the search algorithm works.
17376049d9f0SDaniel C. Sobral  */
17386049d9f0SDaniel C. Sobral static void
17396049d9f0SDaniel C. Sobral computematchjumps(p, g)
17406049d9f0SDaniel C. Sobral struct parse *p;
17416049d9f0SDaniel C. Sobral struct re_guts *g;
17426049d9f0SDaniel C. Sobral {
17436049d9f0SDaniel C. Sobral 	int mindex;		/* General "must" iterator */
17446049d9f0SDaniel C. Sobral 	int suffix;		/* Keeps track of matching suffix */
17456049d9f0SDaniel C. Sobral 	int ssuffix;		/* Keeps track of suffixes' suffix */
17466049d9f0SDaniel C. Sobral 	int* pmatches;		/* pmatches[k] points to the next i
17476049d9f0SDaniel C. Sobral 				 * such that i+1...mlen is a substring
17486049d9f0SDaniel C. Sobral 				 * of k+1...k+mlen-i-1
17496049d9f0SDaniel C. Sobral 				 */
17506049d9f0SDaniel C. Sobral 
17516049d9f0SDaniel C. Sobral 	/* Avoid making errors worse */
17526049d9f0SDaniel C. Sobral 	if (p->error != 0)
17536049d9f0SDaniel C. Sobral 		return;
17546049d9f0SDaniel C. Sobral 
1755517bffcaSDaniel C. Sobral 	pmatches = (int*) malloc(g->mlen * sizeof(unsigned int));
17566049d9f0SDaniel C. Sobral 	if (pmatches == NULL) {
17576049d9f0SDaniel C. Sobral 		g->matchjump = NULL;
17586049d9f0SDaniel C. Sobral 		return;
17596049d9f0SDaniel C. Sobral 	}
17606049d9f0SDaniel C. Sobral 
1761517bffcaSDaniel C. Sobral 	g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int));
17626049d9f0SDaniel C. Sobral 	if (g->matchjump == NULL)	/* Not a fatal error */
17636049d9f0SDaniel C. Sobral 		return;
17646049d9f0SDaniel C. Sobral 
17656049d9f0SDaniel C. Sobral 	/* Set maximum possible jump for each character in the pattern */
17666049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex < g->mlen; mindex++)
17676049d9f0SDaniel C. Sobral 		g->matchjump[mindex] = 2*g->mlen - mindex - 1;
17686049d9f0SDaniel C. Sobral 
17696049d9f0SDaniel C. Sobral 	/* Compute pmatches[] */
17706049d9f0SDaniel C. Sobral 	for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
17716049d9f0SDaniel C. Sobral 	    mindex--, suffix--) {
17726049d9f0SDaniel C. Sobral 		pmatches[mindex] = suffix;
17736049d9f0SDaniel C. Sobral 
17746049d9f0SDaniel C. Sobral 		/* If a mismatch is found, interrupting the substring,
17756049d9f0SDaniel C. Sobral 		 * compute the matchjump for that position. If no
17766049d9f0SDaniel C. Sobral 		 * mismatch is found, then a text substring mismatched
17776049d9f0SDaniel C. Sobral 		 * against the suffix will also mismatch against the
17786049d9f0SDaniel C. Sobral 		 * substring.
17796049d9f0SDaniel C. Sobral 		 */
17806049d9f0SDaniel C. Sobral 		while (suffix < g->mlen
17816049d9f0SDaniel C. Sobral 		    && g->must[mindex] != g->must[suffix]) {
17826049d9f0SDaniel C. Sobral 			g->matchjump[suffix] = MIN(g->matchjump[suffix],
17836049d9f0SDaniel C. Sobral 			    g->mlen - mindex - 1);
17846049d9f0SDaniel C. Sobral 			suffix = pmatches[suffix];
17856049d9f0SDaniel C. Sobral 		}
17866049d9f0SDaniel C. Sobral 	}
17876049d9f0SDaniel C. Sobral 
17886049d9f0SDaniel C. Sobral 	/* Compute the matchjump up to the last substring found to jump
17896049d9f0SDaniel C. Sobral 	 * to the beginning of the largest must pattern prefix matching
17906049d9f0SDaniel C. Sobral 	 * it's own suffix.
17916049d9f0SDaniel C. Sobral 	 */
17926049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex <= suffix; mindex++)
17936049d9f0SDaniel C. Sobral 		g->matchjump[mindex] = MIN(g->matchjump[mindex],
17946049d9f0SDaniel C. Sobral 		    g->mlen + suffix - mindex);
17956049d9f0SDaniel C. Sobral 
17966049d9f0SDaniel C. Sobral         ssuffix = pmatches[suffix];
17976049d9f0SDaniel C. Sobral         while (suffix < g->mlen) {
17989868274bSDaniel C. Sobral                 while (suffix <= ssuffix && suffix < g->mlen) {
17996049d9f0SDaniel C. Sobral                         g->matchjump[suffix] = MIN(g->matchjump[suffix],
18006049d9f0SDaniel C. Sobral 			    g->mlen + ssuffix - suffix);
18016049d9f0SDaniel C. Sobral                         suffix++;
18026049d9f0SDaniel C. Sobral                 }
18038e2f75b8SDaniel C. Sobral 		if (suffix < g->mlen)
18046049d9f0SDaniel C. Sobral                 	ssuffix = pmatches[ssuffix];
18056049d9f0SDaniel C. Sobral         }
18066049d9f0SDaniel C. Sobral 
18076049d9f0SDaniel C. Sobral 	free(pmatches);
18086049d9f0SDaniel C. Sobral }
18096049d9f0SDaniel C. Sobral 
18106049d9f0SDaniel C. Sobral /*
181158f0484fSRodney W. Grimes  - pluscount - count + nesting
18128fb3f3f6SDavid E. O'Brien  == static sopno pluscount(struct parse *p, struct re_guts *g);
181358f0484fSRodney W. Grimes  */
181458f0484fSRodney W. Grimes static sopno			/* nesting depth */
181558f0484fSRodney W. Grimes pluscount(p, g)
181658f0484fSRodney W. Grimes struct parse *p;
18178fb3f3f6SDavid E. O'Brien struct re_guts *g;
181858f0484fSRodney W. Grimes {
18198fb3f3f6SDavid E. O'Brien 	sop *scan;
18208fb3f3f6SDavid E. O'Brien 	sop s;
18218fb3f3f6SDavid E. O'Brien 	sopno plusnest = 0;
18228fb3f3f6SDavid E. O'Brien 	sopno maxnest = 0;
182358f0484fSRodney W. Grimes 
182458f0484fSRodney W. Grimes 	if (p->error != 0)
182558f0484fSRodney W. Grimes 		return(0);	/* there may not be an OEND */
182658f0484fSRodney W. Grimes 
182758f0484fSRodney W. Grimes 	scan = g->strip + 1;
182858f0484fSRodney W. Grimes 	do {
182958f0484fSRodney W. Grimes 		s = *scan++;
183058f0484fSRodney W. Grimes 		switch (OP(s)) {
183158f0484fSRodney W. Grimes 		case OPLUS_:
183258f0484fSRodney W. Grimes 			plusnest++;
183358f0484fSRodney W. Grimes 			break;
183458f0484fSRodney W. Grimes 		case O_PLUS:
183558f0484fSRodney W. Grimes 			if (plusnest > maxnest)
183658f0484fSRodney W. Grimes 				maxnest = plusnest;
183758f0484fSRodney W. Grimes 			plusnest--;
183858f0484fSRodney W. Grimes 			break;
183958f0484fSRodney W. Grimes 		}
184058f0484fSRodney W. Grimes 	} while (OP(s) != OEND);
184158f0484fSRodney W. Grimes 	if (plusnest != 0)
184258f0484fSRodney W. Grimes 		g->iflags |= BAD;
184358f0484fSRodney W. Grimes 	return(maxnest);
184458f0484fSRodney W. Grimes }
1845