xref: /freebsd/lib/libc/regex/regcomp.c (revision 79c9a695c3ca1a2c673ba717b8763657a43f9151)
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  *
63c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
73c87aa1dSDavid Chisnall  * All rights reserved.
83c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
93c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
103c87aa1dSDavid Chisnall  *
1158f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
1258f0484fSRodney W. Grimes  * Henry Spencer.
1358f0484fSRodney W. Grimes  *
1458f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1558f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1658f0484fSRodney W. Grimes  * are met:
1758f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1858f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1958f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
2058f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
2158f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
22fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2358f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2458f0484fSRodney W. Grimes  *    without specific prior written permission.
2558f0484fSRodney W. Grimes  *
2658f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2758f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2858f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2958f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3058f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3158f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3258f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3358f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3458f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3558f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3658f0484fSRodney W. Grimes  * SUCH DAMAGE.
3758f0484fSRodney W. Grimes  *
3858f0484fSRodney W. Grimes  *	@(#)regcomp.c	8.5 (Berkeley) 3/20/94
3958f0484fSRodney W. Grimes  */
4058f0484fSRodney W. Grimes 
4158f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
4258f0484fSRodney W. Grimes static char sccsid[] = "@(#)regcomp.c	8.5 (Berkeley) 3/20/94";
4358f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
44333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
45333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #include <sys/types.h>
4858f0484fSRodney W. Grimes #include <stdio.h>
4958f0484fSRodney W. Grimes #include <string.h>
5058f0484fSRodney W. Grimes #include <ctype.h>
5158f0484fSRodney W. Grimes #include <limits.h>
5258f0484fSRodney W. Grimes #include <stdlib.h>
5358f0484fSRodney W. Grimes #include <regex.h>
5415ae9efaSKyle Evans #include <stdbool.h>
55e5996857STim J. Robbins #include <wchar.h>
56e5996857STim J. Robbins #include <wctype.h>
5758f0484fSRodney W. Grimes 
581daad8f5SAndrey A. Chernov #include "collate.h"
591daad8f5SAndrey A. Chernov 
6058f0484fSRodney W. Grimes #include "utils.h"
6158f0484fSRodney W. Grimes #include "regex2.h"
6258f0484fSRodney W. Grimes 
6358f0484fSRodney W. Grimes #include "cname.h"
6458f0484fSRodney W. Grimes 
6558f0484fSRodney W. Grimes /*
6615ae9efaSKyle Evans  * Branching context, used to keep track of branch state for all of the branch-
6715ae9efaSKyle Evans  * aware functions. In addition to keeping track of branch positions for the
6815ae9efaSKyle Evans  * p_branch_* functions, we use this to simplify some clumsiness in BREs for
6915ae9efaSKyle Evans  * detection of whether ^ is acting as an anchor or being used erroneously and
7015ae9efaSKyle Evans  * also for whether we're in a sub-expression or not.
7115ae9efaSKyle Evans  */
7215ae9efaSKyle Evans struct branchc {
7315ae9efaSKyle Evans 	sopno start;
7415ae9efaSKyle Evans 	sopno back;
7515ae9efaSKyle Evans 	sopno fwd;
7615ae9efaSKyle Evans 
7715ae9efaSKyle Evans 	int nbranch;
7815ae9efaSKyle Evans 	int nchain;
7915ae9efaSKyle Evans 	bool outer;
8015ae9efaSKyle Evans 	bool terminate;
8115ae9efaSKyle Evans };
8215ae9efaSKyle Evans 
8315ae9efaSKyle Evans /*
8458f0484fSRodney W. Grimes  * parse structure, passed up and down to avoid global variables and
8558f0484fSRodney W. Grimes  * other clumsinesses
8658f0484fSRodney W. Grimes  */
8758f0484fSRodney W. Grimes struct parse {
888d0f9a93SPedro F. Giffuni 	const char *next;	/* next character in RE */
898d0f9a93SPedro F. Giffuni 	const char *end;	/* end of string (-> NUL normally) */
9058f0484fSRodney W. Grimes 	int error;		/* has an error been seen? */
9158f0484fSRodney W. Grimes 	sop *strip;		/* malloced strip */
9258f0484fSRodney W. Grimes 	sopno ssize;		/* malloced strip size (allocated) */
9358f0484fSRodney W. Grimes 	sopno slen;		/* malloced strip length (used) */
9458f0484fSRodney W. Grimes 	int ncsalloc;		/* number of csets allocated */
9558f0484fSRodney W. Grimes 	struct re_guts *g;
9658f0484fSRodney W. Grimes #	define	NPAREN	10	/* we need to remember () 1-9 for back refs */
9758f0484fSRodney W. Grimes 	sopno pbegin[NPAREN];	/* -> ( ([0] unused) */
9858f0484fSRodney W. Grimes 	sopno pend[NPAREN];	/* -> ) ([0] unused) */
9915ae9efaSKyle Evans 	bool allowbranch;	/* can this expression branch? */
10015ae9efaSKyle Evans 	bool bre;		/* convenience; is this a BRE? */
10115ae9efaSKyle Evans 	bool (*parse_expr)(struct parse *, struct branchc *);
10215ae9efaSKyle Evans 	void (*pre_parse)(struct parse *, struct branchc *);
10315ae9efaSKyle Evans 	void (*post_parse)(struct parse *, struct branchc *);
10458f0484fSRodney W. Grimes };
10558f0484fSRodney W. Grimes 
10658f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */
10758f0484fSRodney W. Grimes #ifdef __cplusplus
10858f0484fSRodney W. Grimes extern "C" {
10958f0484fSRodney W. Grimes #endif
11058f0484fSRodney W. Grimes 
11158f0484fSRodney W. Grimes /* === regcomp.c === */
11215ae9efaSKyle Evans static bool p_ere_exp(struct parse *p, struct branchc *bc);
113c05ac53bSDavid E. O'Brien static void p_str(struct parse *p);
11415ae9efaSKyle Evans static int p_branch_eat_delim(struct parse *p, struct branchc *bc);
11515ae9efaSKyle Evans static void p_branch_ins_offset(struct parse *p, struct branchc *bc);
11615ae9efaSKyle Evans static void p_branch_fix_tail(struct parse *p, struct branchc *bc);
1173ea376f6SKyle Evans static bool p_branch_empty(struct parse *p, struct branchc *bc);
11815ae9efaSKyle Evans static bool p_branch_do(struct parse *p, struct branchc *bc);
11915ae9efaSKyle Evans static void p_bre_pre_parse(struct parse *p, struct branchc *bc);
12015ae9efaSKyle Evans static void p_bre_post_parse(struct parse *p, struct branchc *bc);
12115ae9efaSKyle Evans static void p_re(struct parse *p, int end1, int end2);
12215ae9efaSKyle Evans static bool p_simp_re(struct parse *p, struct branchc *bc);
123c05ac53bSDavid E. O'Brien static int p_count(struct parse *p);
124c05ac53bSDavid E. O'Brien static void p_bracket(struct parse *p);
125c05ac53bSDavid E. O'Brien static void p_b_term(struct parse *p, cset *cs);
126c05ac53bSDavid E. O'Brien static void p_b_cclass(struct parse *p, cset *cs);
127c05ac53bSDavid E. O'Brien static void p_b_eclass(struct parse *p, cset *cs);
128e5996857STim J. Robbins static wint_t p_b_symbol(struct parse *p);
129e5996857STim J. Robbins static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
130e5996857STim J. Robbins static wint_t othercase(wint_t ch);
131e5996857STim J. Robbins static void bothcases(struct parse *p, wint_t ch);
132e5996857STim J. Robbins static void ordinary(struct parse *p, wint_t ch);
133c05ac53bSDavid E. O'Brien static void nonnewline(struct parse *p);
134c05ac53bSDavid E. O'Brien static void repeat(struct parse *p, sopno start, int from, int to);
135c05ac53bSDavid E. O'Brien static int seterr(struct parse *p, int e);
136c05ac53bSDavid E. O'Brien static cset *allocset(struct parse *p);
137c05ac53bSDavid E. O'Brien static void freeset(struct parse *p, cset *cs);
138e5996857STim J. Robbins static void CHadd(struct parse *p, cset *cs, wint_t ch);
139e5996857STim J. Robbins static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max);
140e5996857STim J. Robbins static void CHaddtype(struct parse *p, cset *cs, wctype_t wct);
141e5996857STim J. Robbins static wint_t singleton(cset *cs);
142c05ac53bSDavid E. O'Brien static sopno dupl(struct parse *p, sopno start, sopno finish);
143c05ac53bSDavid E. O'Brien static void doemit(struct parse *p, sop op, size_t opnd);
144c05ac53bSDavid E. O'Brien static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
145c05ac53bSDavid E. O'Brien static void dofwd(struct parse *p, sopno pos, sop value);
1462bf213ebSKevin Lo static int enlarge(struct parse *p, sopno size);
147c05ac53bSDavid E. O'Brien static void stripsnug(struct parse *p, struct re_guts *g);
148c05ac53bSDavid E. O'Brien static void findmust(struct parse *p, struct re_guts *g);
14980f36366STim J. Robbins static int altoffset(sop *scan, int offset);
150c05ac53bSDavid E. O'Brien static void computejumps(struct parse *p, struct re_guts *g);
151c05ac53bSDavid E. O'Brien static void computematchjumps(struct parse *p, struct re_guts *g);
152c05ac53bSDavid E. O'Brien static sopno pluscount(struct parse *p, struct re_guts *g);
153e5996857STim J. Robbins static wint_t wgetnext(struct parse *p);
15458f0484fSRodney W. Grimes 
15558f0484fSRodney W. Grimes #ifdef __cplusplus
15658f0484fSRodney W. Grimes }
15758f0484fSRodney W. Grimes #endif
15858f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */
15958f0484fSRodney W. Grimes 
16058f0484fSRodney W. Grimes static char nuls[10];		/* place to point scanner in event of error */
16158f0484fSRodney W. Grimes 
16258f0484fSRodney W. Grimes /*
16358f0484fSRodney W. Grimes  * macros for use with parse structure
16458f0484fSRodney W. Grimes  * BEWARE:  these know that the parse structure is named `p' !!!
16558f0484fSRodney W. Grimes  */
16658f0484fSRodney W. Grimes #define	PEEK()	(*p->next)
16758f0484fSRodney W. Grimes #define	PEEK2()	(*(p->next+1))
16858f0484fSRodney W. Grimes #define	MORE()	(p->next < p->end)
16958f0484fSRodney W. Grimes #define	MORE2()	(p->next+1 < p->end)
17058f0484fSRodney W. Grimes #define	SEE(c)	(MORE() && PEEK() == (c))
17158f0484fSRodney W. Grimes #define	SEETWO(a, b)	(MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
17215ae9efaSKyle Evans #define	SEESPEC(a)	(p->bre ? SEETWO('\\', a) : SEE(a))
17358f0484fSRodney W. Grimes #define	EAT(c)	((SEE(c)) ? (NEXT(), 1) : 0)
17458f0484fSRodney W. Grimes #define	EATTWO(a, b)	((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
17558f0484fSRodney W. Grimes #define	NEXT()	(p->next++)
17658f0484fSRodney W. Grimes #define	NEXT2()	(p->next += 2)
17758f0484fSRodney W. Grimes #define	NEXTn(n)	(p->next += (n))
17858f0484fSRodney W. Grimes #define	GETNEXT()	(*p->next++)
179e5996857STim J. Robbins #define	WGETNEXT()	wgetnext(p)
18058f0484fSRodney W. Grimes #define	SETERROR(e)	seterr(p, (e))
18158f0484fSRodney W. Grimes #define	REQUIRE(co, e)	((co) || SETERROR(e))
18258f0484fSRodney W. Grimes #define	MUSTSEE(c, e)	(REQUIRE(MORE() && PEEK() == (c), e))
18358f0484fSRodney W. Grimes #define	MUSTEAT(c, e)	(REQUIRE(MORE() && GETNEXT() == (c), e))
18458f0484fSRodney W. Grimes #define	MUSTNOTSEE(c, e)	(REQUIRE(!MORE() || PEEK() != (c), e))
18558f0484fSRodney W. Grimes #define	EMIT(op, sopnd)	doemit(p, (sop)(op), (size_t)(sopnd))
18658f0484fSRodney W. Grimes #define	INSERT(op, pos)	doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
18758f0484fSRodney W. Grimes #define	AHEAD(pos)		dofwd(p, pos, HERE()-(pos))
18858f0484fSRodney W. Grimes #define	ASTERN(sop, pos)	EMIT(sop, HERE()-pos)
18958f0484fSRodney W. Grimes #define	HERE()		(p->slen)
19058f0484fSRodney W. Grimes #define	THERE()		(p->slen - 1)
19158f0484fSRodney W. Grimes #define	THERETHERE()	(p->slen - 2)
19258f0484fSRodney W. Grimes #define	DROP(n)	(p->slen -= (n))
19358f0484fSRodney W. Grimes 
19458f0484fSRodney W. Grimes #ifndef NDEBUG
19558f0484fSRodney W. Grimes static int never = 0;		/* for use in asserts; shuts lint up */
19658f0484fSRodney W. Grimes #else
19758f0484fSRodney W. Grimes #define	never	0		/* some <assert.h>s have bugs too */
19858f0484fSRodney W. Grimes #endif
19958f0484fSRodney W. Grimes 
2006049d9f0SDaniel C. Sobral /* Macro used by computejump()/computematchjump() */
2016049d9f0SDaniel C. Sobral #define MIN(a,b)	((a)<(b)?(a):(b))
2026049d9f0SDaniel C. Sobral 
20358f0484fSRodney W. Grimes /*
20458f0484fSRodney W. Grimes  - regcomp - interface for parser and compilation
20558f0484fSRodney W. Grimes  = extern int regcomp(regex_t *, const char *, int);
20658f0484fSRodney W. Grimes  = #define	REG_BASIC	0000
20758f0484fSRodney W. Grimes  = #define	REG_EXTENDED	0001
20858f0484fSRodney W. Grimes  = #define	REG_ICASE	0002
20958f0484fSRodney W. Grimes  = #define	REG_NOSUB	0004
21058f0484fSRodney W. Grimes  = #define	REG_NEWLINE	0010
21158f0484fSRodney W. Grimes  = #define	REG_NOSPEC	0020
21258f0484fSRodney W. Grimes  = #define	REG_PEND	0040
21358f0484fSRodney W. Grimes  = #define	REG_DUMP	0200
21458f0484fSRodney W. Grimes  */
21558f0484fSRodney W. Grimes int				/* 0 success, otherwise REG_something */
21654a648d1SXin LI regcomp(regex_t * __restrict preg,
21754a648d1SXin LI 	const char * __restrict pattern,
21854a648d1SXin LI 	int cflags)
21958f0484fSRodney W. Grimes {
22058f0484fSRodney W. Grimes 	struct parse pa;
2218fb3f3f6SDavid E. O'Brien 	struct re_guts *g;
2228fb3f3f6SDavid E. O'Brien 	struct parse *p = &pa;
2238fb3f3f6SDavid E. O'Brien 	int i;
2248fb3f3f6SDavid E. O'Brien 	size_t len;
225a89629abSXin LI 	size_t maxlen;
22658f0484fSRodney W. Grimes #ifdef REDEBUG
22758f0484fSRodney W. Grimes #	define	GOODFLAGS(f)	(f)
22858f0484fSRodney W. Grimes #else
22958f0484fSRodney W. Grimes #	define	GOODFLAGS(f)	((f)&~REG_DUMP)
23058f0484fSRodney W. Grimes #endif
23158f0484fSRodney W. Grimes 
23258f0484fSRodney W. Grimes 	cflags = GOODFLAGS(cflags);
23358f0484fSRodney W. Grimes 	if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
23458f0484fSRodney W. Grimes 		return(REG_INVARG);
23558f0484fSRodney W. Grimes 
23658f0484fSRodney W. Grimes 	if (cflags&REG_PEND) {
23758f0484fSRodney W. Grimes 		if (preg->re_endp < pattern)
23858f0484fSRodney W. Grimes 			return(REG_INVARG);
23958f0484fSRodney W. Grimes 		len = preg->re_endp - pattern;
24058f0484fSRodney W. Grimes 	} else
2418d0f9a93SPedro F. Giffuni 		len = strlen(pattern);
24258f0484fSRodney W. Grimes 
24358f0484fSRodney W. Grimes 	/* do the mallocs early so failure handling is easy */
24480f36366STim J. Robbins 	g = (struct re_guts *)malloc(sizeof(struct re_guts));
24558f0484fSRodney W. Grimes 	if (g == NULL)
24658f0484fSRodney W. Grimes 		return(REG_ESPACE);
247a89629abSXin LI 	/*
248a89629abSXin LI 	 * Limit the pattern space to avoid a 32-bit overflow on buffer
249a89629abSXin LI 	 * extension.  Also avoid any signed overflow in case of conversion
250a89629abSXin LI 	 * so make the real limit based on a 31-bit overflow.
251a89629abSXin LI 	 *
252a89629abSXin LI 	 * Likely not applicable on 64-bit systems but handle the case
253a89629abSXin LI 	 * generically (who are we to stop people from using ~715MB+
254a89629abSXin LI 	 * patterns?).
255a89629abSXin LI 	 */
256a89629abSXin LI 	maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3;
257a89629abSXin LI 	if (len >= maxlen) {
258a89629abSXin LI 		free((char *)g);
259a89629abSXin LI 		return(REG_ESPACE);
260a89629abSXin LI 	}
26158f0484fSRodney W. Grimes 	p->ssize = len/(size_t)2*(size_t)3 + (size_t)1;	/* ugh */
262a89629abSXin LI 	assert(p->ssize >= len);
263a89629abSXin LI 
264e234ddefSPedro F. Giffuni 	p->strip = (sop *)malloc(p->ssize * sizeof(sop));
26558f0484fSRodney W. Grimes 	p->slen = 0;
26658f0484fSRodney W. Grimes 	if (p->strip == NULL) {
26758f0484fSRodney W. Grimes 		free((char *)g);
26858f0484fSRodney W. Grimes 		return(REG_ESPACE);
26958f0484fSRodney W. Grimes 	}
27058f0484fSRodney W. Grimes 
27158f0484fSRodney W. Grimes 	/* set things up */
27258f0484fSRodney W. Grimes 	p->g = g;
2738d0f9a93SPedro F. Giffuni 	p->next = pattern;	/* convenience; we do not modify it */
27458f0484fSRodney W. Grimes 	p->end = p->next + len;
27558f0484fSRodney W. Grimes 	p->error = 0;
27658f0484fSRodney W. Grimes 	p->ncsalloc = 0;
27758f0484fSRodney W. Grimes 	for (i = 0; i < NPAREN; i++) {
27858f0484fSRodney W. Grimes 		p->pbegin[i] = 0;
27958f0484fSRodney W. Grimes 		p->pend[i] = 0;
28058f0484fSRodney W. Grimes 	}
28115ae9efaSKyle Evans 	if (cflags & REG_EXTENDED) {
28215ae9efaSKyle Evans 		p->allowbranch = true;
28315ae9efaSKyle Evans 		p->bre = false;
28415ae9efaSKyle Evans 		p->parse_expr = p_ere_exp;
28515ae9efaSKyle Evans 		p->pre_parse = NULL;
28615ae9efaSKyle Evans 		p->post_parse = NULL;
28715ae9efaSKyle Evans 	} else {
28815ae9efaSKyle Evans 		p->allowbranch = false;
28915ae9efaSKyle Evans 		p->bre = true;
29015ae9efaSKyle Evans 		p->parse_expr = p_simp_re;
29115ae9efaSKyle Evans 		p->pre_parse = p_bre_pre_parse;
29215ae9efaSKyle Evans 		p->post_parse = p_bre_post_parse;
29315ae9efaSKyle Evans 	}
29458f0484fSRodney W. Grimes 	g->sets = NULL;
29558f0484fSRodney W. Grimes 	g->ncsets = 0;
29658f0484fSRodney W. Grimes 	g->cflags = cflags;
29758f0484fSRodney W. Grimes 	g->iflags = 0;
29858f0484fSRodney W. Grimes 	g->nbol = 0;
29958f0484fSRodney W. Grimes 	g->neol = 0;
30058f0484fSRodney W. Grimes 	g->must = NULL;
301e6a886d8SDaniel C. Sobral 	g->moffset = -1;
3026b709b74SDaniel C. Sobral 	g->charjump = NULL;
3036b709b74SDaniel C. Sobral 	g->matchjump = NULL;
30458f0484fSRodney W. Grimes 	g->mlen = 0;
30558f0484fSRodney W. Grimes 	g->nsub = 0;
30658f0484fSRodney W. Grimes 	g->backrefs = 0;
30758f0484fSRodney W. Grimes 
30858f0484fSRodney W. Grimes 	/* do it */
30958f0484fSRodney W. Grimes 	EMIT(OEND, 0);
31058f0484fSRodney W. Grimes 	g->firststate = THERE();
31115ae9efaSKyle Evans 	if (cflags & REG_NOSPEC)
31258f0484fSRodney W. Grimes 		p_str(p);
31358f0484fSRodney W. Grimes 	else
31415ae9efaSKyle Evans 		p_re(p, OUT, OUT);
31558f0484fSRodney W. Grimes 	EMIT(OEND, 0);
31658f0484fSRodney W. Grimes 	g->laststate = THERE();
31758f0484fSRodney W. Grimes 
31858f0484fSRodney W. Grimes 	/* tidy up loose ends and fill things in */
31958f0484fSRodney W. Grimes 	stripsnug(p, g);
32058f0484fSRodney W. Grimes 	findmust(p, g);
3216049d9f0SDaniel C. Sobral 	/* only use Boyer-Moore algorithm if the pattern is bigger
3226049d9f0SDaniel C. Sobral 	 * than three characters
3236049d9f0SDaniel C. Sobral 	 */
3246049d9f0SDaniel C. Sobral 	if(g->mlen > 3) {
3256049d9f0SDaniel C. Sobral 		computejumps(p, g);
3266049d9f0SDaniel C. Sobral 		computematchjumps(p, g);
3274d41cae8SDaniel C. Sobral 		if(g->matchjump == NULL && g->charjump != NULL) {
3286049d9f0SDaniel C. Sobral 			free(g->charjump);
3296049d9f0SDaniel C. Sobral 			g->charjump = NULL;
3306049d9f0SDaniel C. Sobral 		}
3316049d9f0SDaniel C. Sobral 	}
33258f0484fSRodney W. Grimes 	g->nplus = pluscount(p, g);
33358f0484fSRodney W. Grimes 	g->magic = MAGIC2;
33458f0484fSRodney W. Grimes 	preg->re_nsub = g->nsub;
33558f0484fSRodney W. Grimes 	preg->re_g = g;
33658f0484fSRodney W. Grimes 	preg->re_magic = MAGIC1;
33758f0484fSRodney W. Grimes #ifndef REDEBUG
33858f0484fSRodney W. Grimes 	/* not debugging, so can't rely on the assert() in regexec() */
33958f0484fSRodney W. Grimes 	if (g->iflags&BAD)
34058f0484fSRodney W. Grimes 		SETERROR(REG_ASSERT);
34158f0484fSRodney W. Grimes #endif
34258f0484fSRodney W. Grimes 
34358f0484fSRodney W. Grimes 	/* win or lose, we're done */
34458f0484fSRodney W. Grimes 	if (p->error != 0)	/* lose */
34558f0484fSRodney W. Grimes 		regfree(preg);
34658f0484fSRodney W. Grimes 	return(p->error);
34758f0484fSRodney W. Grimes }
34858f0484fSRodney W. Grimes 
34958f0484fSRodney W. Grimes /*
35015ae9efaSKyle Evans  - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op,
35115ae9efaSKyle Evans  - return whether we should terminate or not
35215ae9efaSKyle Evans  == static bool p_ere_exp(struct parse *p);
35358f0484fSRodney W. Grimes  */
35415ae9efaSKyle Evans static bool
35515ae9efaSKyle Evans p_ere_exp(struct parse *p, struct branchc *bc)
35658f0484fSRodney W. Grimes {
3578fb3f3f6SDavid E. O'Brien 	char c;
358e5996857STim J. Robbins 	wint_t wc;
3598fb3f3f6SDavid E. O'Brien 	sopno pos;
3608fb3f3f6SDavid E. O'Brien 	int count;
3618fb3f3f6SDavid E. O'Brien 	int count2;
3628fb3f3f6SDavid E. O'Brien 	sopno subno;
36358f0484fSRodney W. Grimes 	int wascaret = 0;
36458f0484fSRodney W. Grimes 
36558f0484fSRodney W. Grimes 	assert(MORE());		/* caller should have ensured this */
36658f0484fSRodney W. Grimes 	c = GETNEXT();
36758f0484fSRodney W. Grimes 
36858f0484fSRodney W. Grimes 	pos = HERE();
36958f0484fSRodney W. Grimes 	switch (c) {
37058f0484fSRodney W. Grimes 	case '(':
37151295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EPAREN);
37258f0484fSRodney W. Grimes 		p->g->nsub++;
37358f0484fSRodney W. Grimes 		subno = p->g->nsub;
37458f0484fSRodney W. Grimes 		if (subno < NPAREN)
37558f0484fSRodney W. Grimes 			p->pbegin[subno] = HERE();
37658f0484fSRodney W. Grimes 		EMIT(OLPAREN, subno);
37758f0484fSRodney W. Grimes 		if (!SEE(')'))
37815ae9efaSKyle Evans 			p_re(p, ')', IGN);
37958f0484fSRodney W. Grimes 		if (subno < NPAREN) {
38058f0484fSRodney W. Grimes 			p->pend[subno] = HERE();
38158f0484fSRodney W. Grimes 			assert(p->pend[subno] != 0);
38258f0484fSRodney W. Grimes 		}
38358f0484fSRodney W. Grimes 		EMIT(ORPAREN, subno);
38451295a4dSJordan K. Hubbard 		(void)MUSTEAT(')', REG_EPAREN);
38558f0484fSRodney W. Grimes 		break;
38658f0484fSRodney W. Grimes #ifndef POSIX_MISTAKE
38758f0484fSRodney W. Grimes 	case ')':		/* happens only if no current unmatched ( */
38858f0484fSRodney W. Grimes 		/*
38958f0484fSRodney W. Grimes 		 * You may ask, why the ifndef?  Because I didn't notice
39058f0484fSRodney W. Grimes 		 * this until slightly too late for 1003.2, and none of the
39158f0484fSRodney W. Grimes 		 * other 1003.2 regular-expression reviewers noticed it at
39258f0484fSRodney W. Grimes 		 * all.  So an unmatched ) is legal POSIX, at least until
39358f0484fSRodney W. Grimes 		 * we can get it fixed.
39458f0484fSRodney W. Grimes 		 */
39558f0484fSRodney W. Grimes 		SETERROR(REG_EPAREN);
39658f0484fSRodney W. Grimes 		break;
39758f0484fSRodney W. Grimes #endif
39858f0484fSRodney W. Grimes 	case '^':
39958f0484fSRodney W. Grimes 		EMIT(OBOL, 0);
40058f0484fSRodney W. Grimes 		p->g->iflags |= USEBOL;
40158f0484fSRodney W. Grimes 		p->g->nbol++;
40258f0484fSRodney W. Grimes 		wascaret = 1;
40358f0484fSRodney W. Grimes 		break;
40458f0484fSRodney W. Grimes 	case '$':
40558f0484fSRodney W. Grimes 		EMIT(OEOL, 0);
40658f0484fSRodney W. Grimes 		p->g->iflags |= USEEOL;
40758f0484fSRodney W. Grimes 		p->g->neol++;
40858f0484fSRodney W. Grimes 		break;
40958f0484fSRodney W. Grimes 	case '|':
41058f0484fSRodney W. Grimes 		SETERROR(REG_EMPTY);
41158f0484fSRodney W. Grimes 		break;
41258f0484fSRodney W. Grimes 	case '*':
41358f0484fSRodney W. Grimes 	case '+':
41458f0484fSRodney W. Grimes 	case '?':
41558f0484fSRodney W. Grimes 		SETERROR(REG_BADRPT);
41658f0484fSRodney W. Grimes 		break;
41758f0484fSRodney W. Grimes 	case '.':
41858f0484fSRodney W. Grimes 		if (p->g->cflags&REG_NEWLINE)
41958f0484fSRodney W. Grimes 			nonnewline(p);
42058f0484fSRodney W. Grimes 		else
42158f0484fSRodney W. Grimes 			EMIT(OANY, 0);
42258f0484fSRodney W. Grimes 		break;
42358f0484fSRodney W. Grimes 	case '[':
42458f0484fSRodney W. Grimes 		p_bracket(p);
42558f0484fSRodney W. Grimes 		break;
42658f0484fSRodney W. Grimes 	case '\\':
42751295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EESCAPE);
428e5996857STim J. Robbins 		wc = WGETNEXT();
4296e283683SPedro F. Giffuni 		switch (wc) {
4306e283683SPedro F. Giffuni 		case '<':
4316e283683SPedro F. Giffuni 			EMIT(OBOW, 0);
4326e283683SPedro F. Giffuni 			break;
4336e283683SPedro F. Giffuni 		case '>':
4346e283683SPedro F. Giffuni 			EMIT(OEOW, 0);
4356e283683SPedro F. Giffuni 			break;
4366e283683SPedro F. Giffuni 		default:
437e5996857STim J. Robbins 			ordinary(p, wc);
43858f0484fSRodney W. Grimes 			break;
4396e283683SPedro F. Giffuni 		}
4406e283683SPedro F. Giffuni 		break;
44158f0484fSRodney W. Grimes 	case '{':		/* okay as ordinary except if digit follows */
44289b86d02SAndrey A. Chernov 		(void)REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
44358f0484fSRodney W. Grimes 		/* FALLTHROUGH */
44458f0484fSRodney W. Grimes 	default:
4459806ef78SBrooks Davis 		if (p->error != 0)
44615ae9efaSKyle Evans 			return (false);
447e5996857STim J. Robbins 		p->next--;
448e5996857STim J. Robbins 		wc = WGETNEXT();
449e5996857STim J. Robbins 		ordinary(p, wc);
45058f0484fSRodney W. Grimes 		break;
45158f0484fSRodney W. Grimes 	}
45258f0484fSRodney W. Grimes 
45358f0484fSRodney W. Grimes 	if (!MORE())
45415ae9efaSKyle Evans 		return (false);
45558f0484fSRodney W. Grimes 	c = PEEK();
45658f0484fSRodney W. Grimes 	/* we call { a repetition if followed by a digit */
45758f0484fSRodney W. Grimes 	if (!( c == '*' || c == '+' || c == '?' ||
45889b86d02SAndrey A. Chernov 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
45915ae9efaSKyle Evans 		return (false);		/* no repetition, we're done */
46058f0484fSRodney W. Grimes 	NEXT();
46158f0484fSRodney W. Grimes 
46251295a4dSJordan K. Hubbard 	(void)REQUIRE(!wascaret, REG_BADRPT);
46358f0484fSRodney W. Grimes 	switch (c) {
46458f0484fSRodney W. Grimes 	case '*':	/* implemented as +? */
46558f0484fSRodney W. Grimes 		/* this case does not require the (y|) trick, noKLUDGE */
46658f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
46758f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
46858f0484fSRodney W. Grimes 		INSERT(OQUEST_, pos);
46958f0484fSRodney W. Grimes 		ASTERN(O_QUEST, pos);
47058f0484fSRodney W. Grimes 		break;
47158f0484fSRodney W. Grimes 	case '+':
47258f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
47358f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
47458f0484fSRodney W. Grimes 		break;
47558f0484fSRodney W. Grimes 	case '?':
47658f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
47758f0484fSRodney W. Grimes 		INSERT(OCH_, pos);		/* offset slightly wrong */
47858f0484fSRodney W. Grimes 		ASTERN(OOR1, pos);		/* this one's right */
47958f0484fSRodney W. Grimes 		AHEAD(pos);			/* fix the OCH_ */
48058f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset very wrong... */
48158f0484fSRodney W. Grimes 		AHEAD(THERE());			/* ...so fix it */
48258f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
48358f0484fSRodney W. Grimes 		break;
48458f0484fSRodney W. Grimes 	case '{':
48558f0484fSRodney W. Grimes 		count = p_count(p);
48658f0484fSRodney W. Grimes 		if (EAT(',')) {
48789b86d02SAndrey A. Chernov 			if (isdigit((uch)PEEK())) {
48858f0484fSRodney W. Grimes 				count2 = p_count(p);
48951295a4dSJordan K. Hubbard 				(void)REQUIRE(count <= count2, REG_BADBR);
49058f0484fSRodney W. Grimes 			} else		/* single number with comma */
49158f0484fSRodney W. Grimes 				count2 = INFINITY;
49258f0484fSRodney W. Grimes 		} else		/* just a single number */
49358f0484fSRodney W. Grimes 			count2 = count;
49458f0484fSRodney W. Grimes 		repeat(p, pos, count, count2);
49558f0484fSRodney W. Grimes 		if (!EAT('}')) {	/* error heuristics */
49658f0484fSRodney W. Grimes 			while (MORE() && PEEK() != '}')
49758f0484fSRodney W. Grimes 				NEXT();
49851295a4dSJordan K. Hubbard 			(void)REQUIRE(MORE(), REG_EBRACE);
49958f0484fSRodney W. Grimes 			SETERROR(REG_BADBR);
50058f0484fSRodney W. Grimes 		}
50158f0484fSRodney W. Grimes 		break;
50258f0484fSRodney W. Grimes 	}
50358f0484fSRodney W. Grimes 
50458f0484fSRodney W. Grimes 	if (!MORE())
50515ae9efaSKyle Evans 		return (false);
50658f0484fSRodney W. Grimes 	c = PEEK();
50758f0484fSRodney W. Grimes 	if (!( c == '*' || c == '+' || c == '?' ||
50889b86d02SAndrey A. Chernov 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
50915ae9efaSKyle Evans 		return (false);
51058f0484fSRodney W. Grimes 	SETERROR(REG_BADRPT);
51115ae9efaSKyle Evans 	return (false);
51258f0484fSRodney W. Grimes }
51358f0484fSRodney W. Grimes 
51458f0484fSRodney W. Grimes /*
51558f0484fSRodney W. Grimes  - p_str - string (no metacharacters) "parser"
5168fb3f3f6SDavid E. O'Brien  == static void p_str(struct parse *p);
51758f0484fSRodney W. Grimes  */
51858f0484fSRodney W. Grimes static void
51954a648d1SXin LI p_str(struct parse *p)
52058f0484fSRodney W. Grimes {
52151295a4dSJordan K. Hubbard 	(void)REQUIRE(MORE(), REG_EMPTY);
52258f0484fSRodney W. Grimes 	while (MORE())
523e5996857STim J. Robbins 		ordinary(p, WGETNEXT());
52458f0484fSRodney W. Grimes }
52558f0484fSRodney W. Grimes 
52658f0484fSRodney W. Grimes /*
52715ae9efaSKyle Evans  * Eat consecutive branch delimiters for the kind of expression that we are
52815ae9efaSKyle Evans  * parsing, return the number of delimiters that we ate.
52915ae9efaSKyle Evans  */
53015ae9efaSKyle Evans static int
53115ae9efaSKyle Evans p_branch_eat_delim(struct parse *p, struct branchc *bc)
53215ae9efaSKyle Evans {
53315ae9efaSKyle Evans 	int nskip;
53415ae9efaSKyle Evans 
53515ae9efaSKyle Evans 	nskip = 0;
53615ae9efaSKyle Evans 	while (EAT('|'))
53715ae9efaSKyle Evans 		++nskip;
53815ae9efaSKyle Evans 	return (nskip);
53915ae9efaSKyle Evans }
54015ae9efaSKyle Evans 
54115ae9efaSKyle Evans /*
54215ae9efaSKyle Evans  * Insert necessary branch book-keeping operations. This emits a
54315ae9efaSKyle Evans  * bogus 'next' offset, since we still have more to parse
54415ae9efaSKyle Evans  */
54515ae9efaSKyle Evans static void
54615ae9efaSKyle Evans p_branch_ins_offset(struct parse *p, struct branchc *bc)
54715ae9efaSKyle Evans {
54815ae9efaSKyle Evans 
54915ae9efaSKyle Evans 	if (bc->nbranch == 0) {
55015ae9efaSKyle Evans 		INSERT(OCH_, bc->start);	/* offset is wrong */
55115ae9efaSKyle Evans 		bc->fwd = bc->start;
55215ae9efaSKyle Evans 		bc->back = bc->start;
55315ae9efaSKyle Evans 	}
55415ae9efaSKyle Evans 
55515ae9efaSKyle Evans 	ASTERN(OOR1, bc->back);
55615ae9efaSKyle Evans 	bc->back = THERE();
55715ae9efaSKyle Evans 	AHEAD(bc->fwd);			/* fix previous offset */
55815ae9efaSKyle Evans 	bc->fwd = HERE();
55915ae9efaSKyle Evans 	EMIT(OOR2, 0);			/* offset is very wrong */
56015ae9efaSKyle Evans 	++bc->nbranch;
56115ae9efaSKyle Evans }
56215ae9efaSKyle Evans 
56315ae9efaSKyle Evans /*
56415ae9efaSKyle Evans  * Fix the offset of the tail branch, if we actually had any branches.
56515ae9efaSKyle Evans  * This is to correct the bogus placeholder offset that we use.
56615ae9efaSKyle Evans  */
56715ae9efaSKyle Evans static void
56815ae9efaSKyle Evans p_branch_fix_tail(struct parse *p, struct branchc *bc)
56915ae9efaSKyle Evans {
57015ae9efaSKyle Evans 
57115ae9efaSKyle Evans 	/* Fix bogus offset at the tail if we actually have branches */
57215ae9efaSKyle Evans 	if (bc->nbranch > 0) {
57315ae9efaSKyle Evans 		AHEAD(bc->fwd);
57415ae9efaSKyle Evans 		ASTERN(O_CH, bc->back);
57515ae9efaSKyle Evans 	}
57615ae9efaSKyle Evans }
57715ae9efaSKyle Evans 
57815ae9efaSKyle Evans /*
57915ae9efaSKyle Evans  * Signal to the parser that an empty branch has been encountered; this will,
58015ae9efaSKyle Evans  * in the future, be used to allow for more permissive behavior with empty
5813ea376f6SKyle Evans  * branches. The return value should indicate whether parsing may continue
5823ea376f6SKyle Evans  * or not.
58315ae9efaSKyle Evans  */
5843ea376f6SKyle Evans static bool
58515ae9efaSKyle Evans p_branch_empty(struct parse *p, struct branchc *bc)
58615ae9efaSKyle Evans {
58715ae9efaSKyle Evans 
58815ae9efaSKyle Evans 	SETERROR(REG_EMPTY);
5893ea376f6SKyle Evans 	return (false);
59015ae9efaSKyle Evans }
59115ae9efaSKyle Evans 
59215ae9efaSKyle Evans /*
59315ae9efaSKyle Evans  * Take care of any branching requirements. This includes inserting the
59415ae9efaSKyle Evans  * appropriate branching instructions as well as eating all of the branch
59515ae9efaSKyle Evans  * delimiters until we either run out of pattern or need to parse more pattern.
59615ae9efaSKyle Evans  */
59715ae9efaSKyle Evans static bool
59815ae9efaSKyle Evans p_branch_do(struct parse *p, struct branchc *bc)
59915ae9efaSKyle Evans {
60015ae9efaSKyle Evans 	int ate = 0;
60115ae9efaSKyle Evans 
60215ae9efaSKyle Evans 	ate = p_branch_eat_delim(p, bc);
60315ae9efaSKyle Evans 	if (ate == 0)
60415ae9efaSKyle Evans 		return (false);
6053ea376f6SKyle Evans 	else if ((ate > 1 || (bc->outer && !MORE())) && !p_branch_empty(p, bc))
6063ea376f6SKyle Evans 		/*
6073ea376f6SKyle Evans 		 * Halt parsing only if we have an empty branch and p_branch_empty
6083ea376f6SKyle Evans 		 * indicates that we must not continue. In the future, this will not
6093ea376f6SKyle Evans 		 * necessarily be an error.
6103ea376f6SKyle Evans 		 */
6113ea376f6SKyle Evans 		return (false);
61215ae9efaSKyle Evans 	p_branch_ins_offset(p, bc);
61315ae9efaSKyle Evans 
61415ae9efaSKyle Evans 	return (true);
61515ae9efaSKyle Evans }
61615ae9efaSKyle Evans 
61715ae9efaSKyle Evans static void
61815ae9efaSKyle Evans p_bre_pre_parse(struct parse *p, struct branchc *bc)
61915ae9efaSKyle Evans {
62015ae9efaSKyle Evans 
62115ae9efaSKyle Evans 	(void) bc;
62215ae9efaSKyle Evans 	/*
62315ae9efaSKyle Evans 	 * Does not move cleanly into expression parser because of
62415ae9efaSKyle Evans 	 * ordinary interpration of * at the beginning position of
62515ae9efaSKyle Evans 	 * an expression.
62615ae9efaSKyle Evans 	 */
62715ae9efaSKyle Evans 	if (EAT('^')) {
62815ae9efaSKyle Evans 		EMIT(OBOL, 0);
62915ae9efaSKyle Evans 		p->g->iflags |= USEBOL;
63015ae9efaSKyle Evans 		p->g->nbol++;
63115ae9efaSKyle Evans 	}
63215ae9efaSKyle Evans }
63315ae9efaSKyle Evans 
63415ae9efaSKyle Evans static void
63515ae9efaSKyle Evans p_bre_post_parse(struct parse *p, struct branchc *bc)
63615ae9efaSKyle Evans {
63715ae9efaSKyle Evans 
63815ae9efaSKyle Evans 	/* Expression is terminating due to EOL token */
63915ae9efaSKyle Evans 	if (bc->terminate) {
64015ae9efaSKyle Evans 		DROP(1);
64115ae9efaSKyle Evans 		EMIT(OEOL, 0);
64215ae9efaSKyle Evans 		p->g->iflags |= USEEOL;
64315ae9efaSKyle Evans 		p->g->neol++;
64415ae9efaSKyle Evans 	}
64515ae9efaSKyle Evans }
64615ae9efaSKyle Evans 
64715ae9efaSKyle Evans /*
64815ae9efaSKyle Evans  - p_re - Top level parser, concatenation and BRE anchoring
64915ae9efaSKyle Evans  == static void p_re(struct parse *p, int end1, int end2);
65058f0484fSRodney W. Grimes  * Giving end1 as OUT essentially eliminates the end1/end2 check.
65158f0484fSRodney W. Grimes  *
65258f0484fSRodney W. Grimes  * This implementation is a bit of a kludge, in that a trailing $ is first
65380f36366STim J. Robbins  * taken as an ordinary character and then revised to be an anchor.
65458f0484fSRodney W. Grimes  * The amount of lookahead needed to avoid this kludge is excessive.
65558f0484fSRodney W. Grimes  */
65658f0484fSRodney W. Grimes static void
65715ae9efaSKyle Evans p_re(struct parse *p,
6585249ac86SKevin Lo 	int end1,	/* first terminating character */
65915ae9efaSKyle Evans 	int end2)	/* second terminating character; ignored for EREs */
66058f0484fSRodney W. Grimes {
66115ae9efaSKyle Evans 	struct branchc bc;
66258f0484fSRodney W. Grimes 
66315ae9efaSKyle Evans 	bc.nbranch = 0;
66415ae9efaSKyle Evans 	if (end1 == OUT && end2 == OUT)
66515ae9efaSKyle Evans 		bc.outer = true;
66615ae9efaSKyle Evans 	else
66715ae9efaSKyle Evans 		bc.outer = false;
66815ae9efaSKyle Evans #define	SEEEND()	(!p->bre ? SEE(end1) : SEETWO(end1, end2))
66915ae9efaSKyle Evans 	for (;;) {
67015ae9efaSKyle Evans 		bc.start = HERE();
67115ae9efaSKyle Evans 		bc.nchain = 0;
67215ae9efaSKyle Evans 		bc.terminate = false;
67315ae9efaSKyle Evans 		if (p->pre_parse != NULL)
67415ae9efaSKyle Evans 			p->pre_parse(p, &bc);
675*79c9a695SKyle Evans 		while (MORE() && (!p->allowbranch || !SEESPEC('|')) && !SEEEND()) {
67615ae9efaSKyle Evans 			bc.terminate = p->parse_expr(p, &bc);
67715ae9efaSKyle Evans 			++bc.nchain;
67858f0484fSRodney W. Grimes 		}
67915ae9efaSKyle Evans 		if (p->post_parse != NULL)
68015ae9efaSKyle Evans 			p->post_parse(p, &bc);
68115ae9efaSKyle Evans 		(void) REQUIRE(HERE() != bc.start, REG_EMPTY);
68215ae9efaSKyle Evans 		if (!p->allowbranch)
68315ae9efaSKyle Evans 			break;
68415ae9efaSKyle Evans 		/*
68515ae9efaSKyle Evans 		 * p_branch_do's return value indicates whether we should
68615ae9efaSKyle Evans 		 * continue parsing or not. This is both for correctness and
68715ae9efaSKyle Evans 		 * a slight optimization, because it will check if we've
68815ae9efaSKyle Evans 		 * encountered an empty branch or the end of the string
68915ae9efaSKyle Evans 		 * immediately following a branch delimiter.
69015ae9efaSKyle Evans 		 */
69115ae9efaSKyle Evans 		if (!p_branch_do(p, &bc))
69215ae9efaSKyle Evans 			break;
69358f0484fSRodney W. Grimes 	}
69415ae9efaSKyle Evans #undef SEE_END
69515ae9efaSKyle Evans 	if (p->allowbranch)
69615ae9efaSKyle Evans 		p_branch_fix_tail(p, &bc);
69715ae9efaSKyle Evans 	assert(!MORE() || SEE(end1));
69858f0484fSRodney W. Grimes }
69958f0484fSRodney W. Grimes 
70058f0484fSRodney W. Grimes /*
70158f0484fSRodney W. Grimes  - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
70215ae9efaSKyle Evans  == static bool p_simp_re(struct parse *p, struct branchc *bc);
70358f0484fSRodney W. Grimes  */
70415ae9efaSKyle Evans static bool			/* was the simple RE an unbackslashed $? */
70515ae9efaSKyle Evans p_simp_re(struct parse *p, struct branchc *bc)
70658f0484fSRodney W. Grimes {
7078fb3f3f6SDavid E. O'Brien 	int c;
7088fb3f3f6SDavid E. O'Brien 	int count;
7098fb3f3f6SDavid E. O'Brien 	int count2;
7108fb3f3f6SDavid E. O'Brien 	sopno pos;
7118fb3f3f6SDavid E. O'Brien 	int i;
712e5996857STim J. Robbins 	wint_t wc;
7138fb3f3f6SDavid E. O'Brien 	sopno subno;
71458f0484fSRodney W. Grimes #	define	BACKSL	(1<<CHAR_BIT)
71558f0484fSRodney W. Grimes 
71632223c1bSPedro F. Giffuni 	pos = HERE();		/* repetition op, if any, covers from here */
71758f0484fSRodney W. Grimes 
71858f0484fSRodney W. Grimes 	assert(MORE());		/* caller should have ensured this */
71958f0484fSRodney W. Grimes 	c = GETNEXT();
72058f0484fSRodney W. Grimes 	if (c == '\\') {
72151295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EESCAPE);
72289b86d02SAndrey A. Chernov 		c = BACKSL | GETNEXT();
72358f0484fSRodney W. Grimes 	}
72458f0484fSRodney W. Grimes 	switch (c) {
72558f0484fSRodney W. Grimes 	case '.':
72658f0484fSRodney W. Grimes 		if (p->g->cflags&REG_NEWLINE)
72758f0484fSRodney W. Grimes 			nonnewline(p);
72858f0484fSRodney W. Grimes 		else
72958f0484fSRodney W. Grimes 			EMIT(OANY, 0);
73058f0484fSRodney W. Grimes 		break;
73158f0484fSRodney W. Grimes 	case '[':
73258f0484fSRodney W. Grimes 		p_bracket(p);
73358f0484fSRodney W. Grimes 		break;
7346e283683SPedro F. Giffuni 	case BACKSL|'<':
7356e283683SPedro F. Giffuni 		EMIT(OBOW, 0);
7366e283683SPedro F. Giffuni 		break;
7376e283683SPedro F. Giffuni 	case BACKSL|'>':
7386e283683SPedro F. Giffuni 		EMIT(OEOW, 0);
7396e283683SPedro F. Giffuni 		break;
74058f0484fSRodney W. Grimes 	case BACKSL|'{':
74158f0484fSRodney W. Grimes 		SETERROR(REG_BADRPT);
74258f0484fSRodney W. Grimes 		break;
74358f0484fSRodney W. Grimes 	case BACKSL|'(':
74458f0484fSRodney W. Grimes 		p->g->nsub++;
74558f0484fSRodney W. Grimes 		subno = p->g->nsub;
74658f0484fSRodney W. Grimes 		if (subno < NPAREN)
74758f0484fSRodney W. Grimes 			p->pbegin[subno] = HERE();
74858f0484fSRodney W. Grimes 		EMIT(OLPAREN, subno);
74958f0484fSRodney W. Grimes 		/* the MORE here is an error heuristic */
75058f0484fSRodney W. Grimes 		if (MORE() && !SEETWO('\\', ')'))
75115ae9efaSKyle Evans 			p_re(p, '\\', ')');
75258f0484fSRodney W. Grimes 		if (subno < NPAREN) {
75358f0484fSRodney W. Grimes 			p->pend[subno] = HERE();
75458f0484fSRodney W. Grimes 			assert(p->pend[subno] != 0);
75558f0484fSRodney W. Grimes 		}
75658f0484fSRodney W. Grimes 		EMIT(ORPAREN, subno);
75751295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
75858f0484fSRodney W. Grimes 		break;
75958f0484fSRodney W. Grimes 	case BACKSL|')':	/* should not get here -- must be user */
76058f0484fSRodney W. Grimes 	case BACKSL|'}':
76158f0484fSRodney W. Grimes 		SETERROR(REG_EPAREN);
76258f0484fSRodney W. Grimes 		break;
76358f0484fSRodney W. Grimes 	case BACKSL|'1':
76458f0484fSRodney W. Grimes 	case BACKSL|'2':
76558f0484fSRodney W. Grimes 	case BACKSL|'3':
76658f0484fSRodney W. Grimes 	case BACKSL|'4':
76758f0484fSRodney W. Grimes 	case BACKSL|'5':
76858f0484fSRodney W. Grimes 	case BACKSL|'6':
76958f0484fSRodney W. Grimes 	case BACKSL|'7':
77058f0484fSRodney W. Grimes 	case BACKSL|'8':
77158f0484fSRodney W. Grimes 	case BACKSL|'9':
77258f0484fSRodney W. Grimes 		i = (c&~BACKSL) - '0';
77358f0484fSRodney W. Grimes 		assert(i < NPAREN);
77458f0484fSRodney W. Grimes 		if (p->pend[i] != 0) {
77558f0484fSRodney W. Grimes 			assert(i <= p->g->nsub);
77658f0484fSRodney W. Grimes 			EMIT(OBACK_, i);
77758f0484fSRodney W. Grimes 			assert(p->pbegin[i] != 0);
77858f0484fSRodney W. Grimes 			assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
77958f0484fSRodney W. Grimes 			assert(OP(p->strip[p->pend[i]]) == ORPAREN);
78058f0484fSRodney W. Grimes 			(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
78158f0484fSRodney W. Grimes 			EMIT(O_BACK, i);
78258f0484fSRodney W. Grimes 		} else
78358f0484fSRodney W. Grimes 			SETERROR(REG_ESUBREG);
78458f0484fSRodney W. Grimes 		p->g->backrefs = 1;
78558f0484fSRodney W. Grimes 		break;
78658f0484fSRodney W. Grimes 	case '*':
78715ae9efaSKyle Evans 		/*
78815ae9efaSKyle Evans 		 * Ordinary if used as the first character beyond BOL anchor of
78915ae9efaSKyle Evans 		 * a (sub-)expression, counts as a bad repetition operator if it
79015ae9efaSKyle Evans 		 * appears otherwise.
79115ae9efaSKyle Evans 		 */
79215ae9efaSKyle Evans 		(void)REQUIRE(bc->nchain == 0, REG_BADRPT);
79358f0484fSRodney W. Grimes 		/* FALLTHROUGH */
79458f0484fSRodney W. Grimes 	default:
7959806ef78SBrooks Davis 		if (p->error != 0)
79615ae9efaSKyle Evans 			return (false);	/* Definitely not $... */
797e5996857STim J. Robbins 		p->next--;
798e5996857STim J. Robbins 		wc = WGETNEXT();
799e5996857STim J. Robbins 		ordinary(p, wc);
80058f0484fSRodney W. Grimes 		break;
80158f0484fSRodney W. Grimes 	}
80258f0484fSRodney W. Grimes 
80358f0484fSRodney W. Grimes 	if (EAT('*')) {		/* implemented as +? */
80458f0484fSRodney W. Grimes 		/* this case does not require the (y|) trick, noKLUDGE */
80558f0484fSRodney W. Grimes 		INSERT(OPLUS_, pos);
80658f0484fSRodney W. Grimes 		ASTERN(O_PLUS, pos);
80758f0484fSRodney W. Grimes 		INSERT(OQUEST_, pos);
80858f0484fSRodney W. Grimes 		ASTERN(O_QUEST, pos);
80958f0484fSRodney W. Grimes 	} else if (EATTWO('\\', '{')) {
81058f0484fSRodney W. Grimes 		count = p_count(p);
81158f0484fSRodney W. Grimes 		if (EAT(',')) {
81289b86d02SAndrey A. Chernov 			if (MORE() && isdigit((uch)PEEK())) {
81358f0484fSRodney W. Grimes 				count2 = p_count(p);
81451295a4dSJordan K. Hubbard 				(void)REQUIRE(count <= count2, REG_BADBR);
81558f0484fSRodney W. Grimes 			} else		/* single number with comma */
81658f0484fSRodney W. Grimes 				count2 = INFINITY;
81758f0484fSRodney W. Grimes 		} else		/* just a single number */
81858f0484fSRodney W. Grimes 			count2 = count;
81958f0484fSRodney W. Grimes 		repeat(p, pos, count, count2);
82058f0484fSRodney W. Grimes 		if (!EATTWO('\\', '}')) {	/* error heuristics */
82158f0484fSRodney W. Grimes 			while (MORE() && !SEETWO('\\', '}'))
82258f0484fSRodney W. Grimes 				NEXT();
82351295a4dSJordan K. Hubbard 			(void)REQUIRE(MORE(), REG_EBRACE);
82458f0484fSRodney W. Grimes 			SETERROR(REG_BADBR);
82558f0484fSRodney W. Grimes 		}
82689b86d02SAndrey A. Chernov 	} else if (c == '$')     /* $ (but not \$) ends it */
82715ae9efaSKyle Evans 		return (true);
82858f0484fSRodney W. Grimes 
82915ae9efaSKyle Evans 	return (false);
83058f0484fSRodney W. Grimes }
83158f0484fSRodney W. Grimes 
83258f0484fSRodney W. Grimes /*
83358f0484fSRodney W. Grimes  - p_count - parse a repetition count
8348fb3f3f6SDavid E. O'Brien  == static int p_count(struct parse *p);
83558f0484fSRodney W. Grimes  */
83658f0484fSRodney W. Grimes static int			/* the value */
83754a648d1SXin LI p_count(struct parse *p)
83858f0484fSRodney W. Grimes {
8398fb3f3f6SDavid E. O'Brien 	int count = 0;
8408fb3f3f6SDavid E. O'Brien 	int ndigits = 0;
84158f0484fSRodney W. Grimes 
84289b86d02SAndrey A. Chernov 	while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
84358f0484fSRodney W. Grimes 		count = count*10 + (GETNEXT() - '0');
84458f0484fSRodney W. Grimes 		ndigits++;
84558f0484fSRodney W. Grimes 	}
84658f0484fSRodney W. Grimes 
84751295a4dSJordan K. Hubbard 	(void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
84858f0484fSRodney W. Grimes 	return(count);
84958f0484fSRodney W. Grimes }
85058f0484fSRodney W. Grimes 
85158f0484fSRodney W. Grimes /*
85258f0484fSRodney W. Grimes  - p_bracket - parse a bracketed character list
8538fb3f3f6SDavid E. O'Brien  == static void p_bracket(struct parse *p);
85458f0484fSRodney W. Grimes  */
85558f0484fSRodney W. Grimes static void
85654a648d1SXin LI p_bracket(struct parse *p)
85758f0484fSRodney W. Grimes {
858e5996857STim J. Robbins 	cset *cs;
859e5996857STim J. Robbins 	wint_t ch;
86058f0484fSRodney W. Grimes 
86158f0484fSRodney W. Grimes 	/* Dept of Truly Sickening Special-Case Kludges */
86258f0484fSRodney W. Grimes 	if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
86358f0484fSRodney W. Grimes 		EMIT(OBOW, 0);
86458f0484fSRodney W. Grimes 		NEXTn(6);
86558f0484fSRodney W. Grimes 		return;
86658f0484fSRodney W. Grimes 	}
86758f0484fSRodney W. Grimes 	if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
86858f0484fSRodney W. Grimes 		EMIT(OEOW, 0);
86958f0484fSRodney W. Grimes 		NEXTn(6);
87058f0484fSRodney W. Grimes 		return;
87158f0484fSRodney W. Grimes 	}
87258f0484fSRodney W. Grimes 
873e5996857STim J. Robbins 	if ((cs = allocset(p)) == NULL)
874e5996857STim J. Robbins 		return;
875e5996857STim J. Robbins 
876e5996857STim J. Robbins 	if (p->g->cflags&REG_ICASE)
877e5996857STim J. Robbins 		cs->icase = 1;
87858f0484fSRodney W. Grimes 	if (EAT('^'))
879e5996857STim J. Robbins 		cs->invert = 1;
88058f0484fSRodney W. Grimes 	if (EAT(']'))
881e5996857STim J. Robbins 		CHadd(p, cs, ']');
88258f0484fSRodney W. Grimes 	else if (EAT('-'))
883e5996857STim J. Robbins 		CHadd(p, cs, '-');
88458f0484fSRodney W. Grimes 	while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
88558f0484fSRodney W. Grimes 		p_b_term(p, cs);
88658f0484fSRodney W. Grimes 	if (EAT('-'))
887e5996857STim J. Robbins 		CHadd(p, cs, '-');
88851295a4dSJordan K. Hubbard 	(void)MUSTEAT(']', REG_EBRACK);
88958f0484fSRodney W. Grimes 
89058f0484fSRodney W. Grimes 	if (p->error != 0)	/* don't mess things up further */
89158f0484fSRodney W. Grimes 		return;
89258f0484fSRodney W. Grimes 
893e5996857STim J. Robbins 	if (cs->invert && p->g->cflags&REG_NEWLINE)
894e5996857STim J. Robbins 		cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
89558f0484fSRodney W. Grimes 
896e5996857STim J. Robbins 	if ((ch = singleton(cs)) != OUT) {	/* optimize singleton sets */
897e5996857STim J. Robbins 		ordinary(p, ch);
89858f0484fSRodney W. Grimes 		freeset(p, cs);
89958f0484fSRodney W. Grimes 	} else
900e5996857STim J. Robbins 		EMIT(OANYOF, (int)(cs - p->g->sets));
90158f0484fSRodney W. Grimes }
90258f0484fSRodney W. Grimes 
90358f0484fSRodney W. Grimes /*
90458f0484fSRodney W. Grimes  - p_b_term - parse one term of a bracketed character list
9058fb3f3f6SDavid E. O'Brien  == static void p_b_term(struct parse *p, cset *cs);
90658f0484fSRodney W. Grimes  */
90758f0484fSRodney W. Grimes static void
90854a648d1SXin LI p_b_term(struct parse *p, cset *cs)
90958f0484fSRodney W. Grimes {
9108fb3f3f6SDavid E. O'Brien 	char c;
911e5996857STim J. Robbins 	wint_t start, finish;
9121daad8f5SAndrey A. Chernov 	wint_t i;
9131daad8f5SAndrey A. Chernov 	struct xlocale_collate *table =
9141daad8f5SAndrey A. Chernov 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
91558f0484fSRodney W. Grimes 
91658f0484fSRodney W. Grimes 	/* classify what we've got */
91758f0484fSRodney W. Grimes 	switch ((MORE()) ? PEEK() : '\0') {
91858f0484fSRodney W. Grimes 	case '[':
91958f0484fSRodney W. Grimes 		c = (MORE2()) ? PEEK2() : '\0';
92058f0484fSRodney W. Grimes 		break;
92158f0484fSRodney W. Grimes 	case '-':
92258f0484fSRodney W. Grimes 		SETERROR(REG_ERANGE);
92358f0484fSRodney W. Grimes 		return;			/* NOTE RETURN */
92458f0484fSRodney W. Grimes 	default:
92558f0484fSRodney W. Grimes 		c = '\0';
92658f0484fSRodney W. Grimes 		break;
92758f0484fSRodney W. Grimes 	}
92858f0484fSRodney W. Grimes 
92958f0484fSRodney W. Grimes 	switch (c) {
93058f0484fSRodney W. Grimes 	case ':':		/* character class */
93158f0484fSRodney W. Grimes 		NEXT2();
93251295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
93358f0484fSRodney W. Grimes 		c = PEEK();
93451295a4dSJordan K. Hubbard 		(void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
93558f0484fSRodney W. Grimes 		p_b_cclass(p, cs);
93651295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
93751295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
93858f0484fSRodney W. Grimes 		break;
93958f0484fSRodney W. Grimes 	case '=':		/* equivalence class */
94058f0484fSRodney W. Grimes 		NEXT2();
94151295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
94258f0484fSRodney W. Grimes 		c = PEEK();
94351295a4dSJordan K. Hubbard 		(void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
94458f0484fSRodney W. Grimes 		p_b_eclass(p, cs);
94551295a4dSJordan K. Hubbard 		(void)REQUIRE(MORE(), REG_EBRACK);
94651295a4dSJordan K. Hubbard 		(void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
94758f0484fSRodney W. Grimes 		break;
94858f0484fSRodney W. Grimes 	default:		/* symbol, ordinary character, or range */
94958f0484fSRodney W. Grimes 		start = p_b_symbol(p);
95058f0484fSRodney W. Grimes 		if (SEE('-') && MORE2() && PEEK2() != ']') {
95158f0484fSRodney W. Grimes 			/* range */
95258f0484fSRodney W. Grimes 			NEXT();
95358f0484fSRodney W. Grimes 			if (EAT('-'))
95458f0484fSRodney W. Grimes 				finish = '-';
95558f0484fSRodney W. Grimes 			else
95658f0484fSRodney W. Grimes 				finish = p_b_symbol(p);
95758f0484fSRodney W. Grimes 		} else
95858f0484fSRodney W. Grimes 			finish = start;
9595c551438SAndrey A. Chernov 		if (start == finish)
960e5996857STim J. Robbins 			CHadd(p, cs, start);
9615c551438SAndrey A. Chernov 		else {
96212eae8c8SAndrey A. Chernov 			if (table->__collate_load_error || MB_CUR_MAX > 1) {
96312eae8c8SAndrey A. Chernov 				(void)REQUIRE(start <= finish, REG_ERANGE);
964e5996857STim J. Robbins 				CHaddrange(p, cs, start, finish);
9651daad8f5SAndrey A. Chernov 			} else {
96612eae8c8SAndrey A. Chernov 				(void)REQUIRE(__wcollate_range_cmp(start, finish) <= 0, REG_ERANGE);
9671daad8f5SAndrey A. Chernov 				for (i = 0; i <= UCHAR_MAX; i++) {
96812eae8c8SAndrey A. Chernov 					if (   __wcollate_range_cmp(start, i) <= 0
96912eae8c8SAndrey A. Chernov 					    && __wcollate_range_cmp(i, finish) <= 0
9701daad8f5SAndrey A. Chernov 					   )
9711daad8f5SAndrey A. Chernov 						CHadd(p, cs, i);
9721daad8f5SAndrey A. Chernov 				}
9731daad8f5SAndrey A. Chernov 			}
974b5a6eb18SAndrey A. Chernov 		}
97558f0484fSRodney W. Grimes 		break;
97658f0484fSRodney W. Grimes 	}
97758f0484fSRodney W. Grimes }
97858f0484fSRodney W. Grimes 
97958f0484fSRodney W. Grimes /*
98058f0484fSRodney W. Grimes  - p_b_cclass - parse a character-class name and deal with it
9818fb3f3f6SDavid E. O'Brien  == static void p_b_cclass(struct parse *p, cset *cs);
98258f0484fSRodney W. Grimes  */
98358f0484fSRodney W. Grimes static void
98454a648d1SXin LI p_b_cclass(struct parse *p, cset *cs)
98558f0484fSRodney W. Grimes {
9868d0f9a93SPedro F. Giffuni 	const char *sp = p->next;
9878fb3f3f6SDavid E. O'Brien 	size_t len;
988e5996857STim J. Robbins 	wctype_t wct;
989e5996857STim J. Robbins 	char clname[16];
99058f0484fSRodney W. Grimes 
991b5363c4aSAndrey A. Chernov 	while (MORE() && isalpha((uch)PEEK()))
99258f0484fSRodney W. Grimes 		NEXT();
99358f0484fSRodney W. Grimes 	len = p->next - sp;
994e5996857STim J. Robbins 	if (len >= sizeof(clname) - 1) {
99558f0484fSRodney W. Grimes 		SETERROR(REG_ECTYPE);
99658f0484fSRodney W. Grimes 		return;
99758f0484fSRodney W. Grimes 	}
998e5996857STim J. Robbins 	memcpy(clname, sp, len);
999e5996857STim J. Robbins 	clname[len] = '\0';
1000e5996857STim J. Robbins 	if ((wct = wctype(clname)) == 0) {
1001e5996857STim J. Robbins 		SETERROR(REG_ECTYPE);
1002e5996857STim J. Robbins 		return;
1003b5363c4aSAndrey A. Chernov 	}
1004e5996857STim J. Robbins 	CHaddtype(p, cs, wct);
100558f0484fSRodney W. Grimes }
100658f0484fSRodney W. Grimes 
100758f0484fSRodney W. Grimes /*
100858f0484fSRodney W. Grimes  - p_b_eclass - parse an equivalence-class name and deal with it
10098fb3f3f6SDavid E. O'Brien  == static void p_b_eclass(struct parse *p, cset *cs);
101058f0484fSRodney W. Grimes  *
101158f0484fSRodney W. Grimes  * This implementation is incomplete. xxx
101258f0484fSRodney W. Grimes  */
101358f0484fSRodney W. Grimes static void
101454a648d1SXin LI p_b_eclass(struct parse *p, cset *cs)
101558f0484fSRodney W. Grimes {
1016e5996857STim J. Robbins 	wint_t c;
101758f0484fSRodney W. Grimes 
101858f0484fSRodney W. Grimes 	c = p_b_coll_elem(p, '=');
1019e5996857STim J. Robbins 	CHadd(p, cs, c);
102058f0484fSRodney W. Grimes }
102158f0484fSRodney W. Grimes 
102258f0484fSRodney W. Grimes /*
102358f0484fSRodney W. Grimes  - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
10242bf213ebSKevin Lo  == static wint_t p_b_symbol(struct parse *p);
102558f0484fSRodney W. Grimes  */
1026e5996857STim J. Robbins static wint_t			/* value of symbol */
102754a648d1SXin LI p_b_symbol(struct parse *p)
102858f0484fSRodney W. Grimes {
1029e5996857STim J. Robbins 	wint_t value;
103058f0484fSRodney W. Grimes 
103151295a4dSJordan K. Hubbard 	(void)REQUIRE(MORE(), REG_EBRACK);
103258f0484fSRodney W. Grimes 	if (!EATTWO('[', '.'))
1033e5996857STim J. Robbins 		return(WGETNEXT());
103458f0484fSRodney W. Grimes 
103558f0484fSRodney W. Grimes 	/* collating symbol */
103658f0484fSRodney W. Grimes 	value = p_b_coll_elem(p, '.');
103751295a4dSJordan K. Hubbard 	(void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
103858f0484fSRodney W. Grimes 	return(value);
103958f0484fSRodney W. Grimes }
104058f0484fSRodney W. Grimes 
104158f0484fSRodney W. Grimes /*
104258f0484fSRodney W. Grimes  - p_b_coll_elem - parse a collating-element name and look it up
10432bf213ebSKevin Lo  == static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
104458f0484fSRodney W. Grimes  */
1045e5996857STim J. Robbins static wint_t			/* value of collating element */
104654a648d1SXin LI p_b_coll_elem(struct parse *p,
104754a648d1SXin LI 	wint_t endc)		/* name ended by endc,']' */
104858f0484fSRodney W. Grimes {
10498d0f9a93SPedro F. Giffuni 	const char *sp = p->next;
10508fb3f3f6SDavid E. O'Brien 	struct cname *cp;
1051e5996857STim J. Robbins 	mbstate_t mbs;
1052e5996857STim J. Robbins 	wchar_t wc;
10538d0f9a93SPedro F. Giffuni 	size_t clen, len;
105458f0484fSRodney W. Grimes 
105558f0484fSRodney W. Grimes 	while (MORE() && !SEETWO(endc, ']'))
105658f0484fSRodney W. Grimes 		NEXT();
105758f0484fSRodney W. Grimes 	if (!MORE()) {
105858f0484fSRodney W. Grimes 		SETERROR(REG_EBRACK);
105958f0484fSRodney W. Grimes 		return(0);
106058f0484fSRodney W. Grimes 	}
106158f0484fSRodney W. Grimes 	len = p->next - sp;
106258f0484fSRodney W. Grimes 	for (cp = cnames; cp->name != NULL; cp++)
106358f0484fSRodney W. Grimes 		if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
106458f0484fSRodney W. Grimes 			return(cp->code);	/* known name */
1065e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1066e5996857STim J. Robbins 	if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
1067e5996857STim J. Robbins 		return (wc);			/* single character */
1068e5996857STim J. Robbins 	else if (clen == (size_t)-1 || clen == (size_t)-2)
1069e5996857STim J. Robbins 		SETERROR(REG_ILLSEQ);
1070e5996857STim J. Robbins 	else
107158f0484fSRodney W. Grimes 		SETERROR(REG_ECOLLATE);		/* neither */
107258f0484fSRodney W. Grimes 	return(0);
107358f0484fSRodney W. Grimes }
107458f0484fSRodney W. Grimes 
107558f0484fSRodney W. Grimes /*
107658f0484fSRodney W. Grimes  - othercase - return the case counterpart of an alphabetic
10772bf213ebSKevin Lo  == static wint_t othercase(wint_t ch);
107858f0484fSRodney W. Grimes  */
1079e5996857STim J. Robbins static wint_t			/* if no counterpart, return ch */
108054a648d1SXin LI othercase(wint_t ch)
108158f0484fSRodney W. Grimes {
1082e5996857STim J. Robbins 	assert(iswalpha(ch));
1083e5996857STim J. Robbins 	if (iswupper(ch))
1084e5996857STim J. Robbins 		return(towlower(ch));
1085e5996857STim J. Robbins 	else if (iswlower(ch))
1086e5996857STim J. Robbins 		return(towupper(ch));
108758f0484fSRodney W. Grimes 	else			/* peculiar, but could happen */
108858f0484fSRodney W. Grimes 		return(ch);
108958f0484fSRodney W. Grimes }
109058f0484fSRodney W. Grimes 
109158f0484fSRodney W. Grimes /*
109258f0484fSRodney W. Grimes  - bothcases - emit a dualcase version of a two-case character
10932bf213ebSKevin Lo  == static void bothcases(struct parse *p, wint_t ch);
109458f0484fSRodney W. Grimes  *
109558f0484fSRodney W. Grimes  * Boy, is this implementation ever a kludge...
109658f0484fSRodney W. Grimes  */
109758f0484fSRodney W. Grimes static void
109854a648d1SXin LI bothcases(struct parse *p, wint_t ch)
109958f0484fSRodney W. Grimes {
11008d0f9a93SPedro F. Giffuni 	const char *oldnext = p->next;
11018d0f9a93SPedro F. Giffuni 	const char *oldend = p->end;
1102e5996857STim J. Robbins 	char bracket[3 + MB_LEN_MAX];
1103e5996857STim J. Robbins 	size_t n;
1104e5996857STim J. Robbins 	mbstate_t mbs;
110558f0484fSRodney W. Grimes 
110658f0484fSRodney W. Grimes 	assert(othercase(ch) != ch);	/* p_bracket() would recurse */
110758f0484fSRodney W. Grimes 	p->next = bracket;
1108e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1109e5996857STim J. Robbins 	n = wcrtomb(bracket, ch, &mbs);
1110e5996857STim J. Robbins 	assert(n != (size_t)-1);
1111e5996857STim J. Robbins 	bracket[n] = ']';
1112e5996857STim J. Robbins 	bracket[n + 1] = '\0';
1113e5996857STim J. Robbins 	p->end = bracket+n+1;
111458f0484fSRodney W. Grimes 	p_bracket(p);
1115e5996857STim J. Robbins 	assert(p->next == p->end);
111658f0484fSRodney W. Grimes 	p->next = oldnext;
111758f0484fSRodney W. Grimes 	p->end = oldend;
111858f0484fSRodney W. Grimes }
111958f0484fSRodney W. Grimes 
112058f0484fSRodney W. Grimes /*
112158f0484fSRodney W. Grimes  - ordinary - emit an ordinary character
11222bf213ebSKevin Lo  == static void ordinary(struct parse *p, wint_t ch);
112358f0484fSRodney W. Grimes  */
112458f0484fSRodney W. Grimes static void
112554a648d1SXin LI ordinary(struct parse *p, wint_t ch)
112658f0484fSRodney W. Grimes {
1127e5996857STim J. Robbins 	cset *cs;
112858f0484fSRodney W. Grimes 
1129e5996857STim J. Robbins 	if ((p->g->cflags&REG_ICASE) && iswalpha(ch) && othercase(ch) != ch)
113058f0484fSRodney W. Grimes 		bothcases(p, ch);
1131e5996857STim J. Robbins 	else if ((ch & OPDMASK) == ch)
1132e5996857STim J. Robbins 		EMIT(OCHAR, ch);
1133e5996857STim J. Robbins 	else {
1134e5996857STim J. Robbins 		/*
1135e5996857STim J. Robbins 		 * Kludge: character is too big to fit into an OCHAR operand.
1136e5996857STim J. Robbins 		 * Emit a singleton set.
1137e5996857STim J. Robbins 		 */
1138e5996857STim J. Robbins 		if ((cs = allocset(p)) == NULL)
1139e5996857STim J. Robbins 			return;
1140e5996857STim J. Robbins 		CHadd(p, cs, ch);
1141e5996857STim J. Robbins 		EMIT(OANYOF, (int)(cs - p->g->sets));
1142e5996857STim J. Robbins 	}
114358f0484fSRodney W. Grimes }
114458f0484fSRodney W. Grimes 
114558f0484fSRodney W. Grimes /*
114658f0484fSRodney W. Grimes  - nonnewline - emit REG_NEWLINE version of OANY
11478fb3f3f6SDavid E. O'Brien  == static void nonnewline(struct parse *p);
114858f0484fSRodney W. Grimes  *
114958f0484fSRodney W. Grimes  * Boy, is this implementation ever a kludge...
115058f0484fSRodney W. Grimes  */
115158f0484fSRodney W. Grimes static void
115254a648d1SXin LI nonnewline(struct parse *p)
115358f0484fSRodney W. Grimes {
11548d0f9a93SPedro F. Giffuni 	const char *oldnext = p->next;
11558d0f9a93SPedro F. Giffuni 	const char *oldend = p->end;
115658f0484fSRodney W. Grimes 	char bracket[4];
115758f0484fSRodney W. Grimes 
115858f0484fSRodney W. Grimes 	p->next = bracket;
115958f0484fSRodney W. Grimes 	p->end = bracket+3;
116058f0484fSRodney W. Grimes 	bracket[0] = '^';
116158f0484fSRodney W. Grimes 	bracket[1] = '\n';
116258f0484fSRodney W. Grimes 	bracket[2] = ']';
116358f0484fSRodney W. Grimes 	bracket[3] = '\0';
116458f0484fSRodney W. Grimes 	p_bracket(p);
116558f0484fSRodney W. Grimes 	assert(p->next == bracket+3);
116658f0484fSRodney W. Grimes 	p->next = oldnext;
116758f0484fSRodney W. Grimes 	p->end = oldend;
116858f0484fSRodney W. Grimes }
116958f0484fSRodney W. Grimes 
117058f0484fSRodney W. Grimes /*
117158f0484fSRodney W. Grimes  - repeat - generate code for a bounded repetition, recursively if needed
11728fb3f3f6SDavid E. O'Brien  == static void repeat(struct parse *p, sopno start, int from, int to);
117358f0484fSRodney W. Grimes  */
117458f0484fSRodney W. Grimes static void
117554a648d1SXin LI repeat(struct parse *p,
117654a648d1SXin LI 	sopno start,		/* operand from here to end of strip */
117754a648d1SXin LI 	int from,		/* repeated from this number */
117854a648d1SXin LI 	int to)			/* to this number of times (maybe INFINITY) */
117958f0484fSRodney W. Grimes {
11808fb3f3f6SDavid E. O'Brien 	sopno finish = HERE();
118158f0484fSRodney W. Grimes #	define	N	2
118258f0484fSRodney W. Grimes #	define	INF	3
118358f0484fSRodney W. Grimes #	define	REP(f, t)	((f)*8 + (t))
118458f0484fSRodney W. Grimes #	define	MAP(n)	(((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
11858fb3f3f6SDavid E. O'Brien 	sopno copy;
118658f0484fSRodney W. Grimes 
118758f0484fSRodney W. Grimes 	if (p->error != 0)	/* head off possible runaway recursion */
118858f0484fSRodney W. Grimes 		return;
118958f0484fSRodney W. Grimes 
119058f0484fSRodney W. Grimes 	assert(from <= to);
119158f0484fSRodney W. Grimes 
119258f0484fSRodney W. Grimes 	switch (REP(MAP(from), MAP(to))) {
119358f0484fSRodney W. Grimes 	case REP(0, 0):			/* must be user doing this */
119458f0484fSRodney W. Grimes 		DROP(finish-start);	/* drop the operand */
119558f0484fSRodney W. Grimes 		break;
119658f0484fSRodney W. Grimes 	case REP(0, 1):			/* as x{1,1}? */
119758f0484fSRodney W. Grimes 	case REP(0, N):			/* as x{1,n}? */
119858f0484fSRodney W. Grimes 	case REP(0, INF):		/* as x{1,}? */
119958f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
120058f0484fSRodney W. Grimes 		INSERT(OCH_, start);		/* offset is wrong... */
120158f0484fSRodney W. Grimes 		repeat(p, start+1, 1, to);
120258f0484fSRodney W. Grimes 		ASTERN(OOR1, start);
120358f0484fSRodney W. Grimes 		AHEAD(start);			/* ... fix it */
120458f0484fSRodney W. Grimes 		EMIT(OOR2, 0);
120558f0484fSRodney W. Grimes 		AHEAD(THERE());
120658f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
120758f0484fSRodney W. Grimes 		break;
120858f0484fSRodney W. Grimes 	case REP(1, 1):			/* trivial case */
120958f0484fSRodney W. Grimes 		/* done */
121058f0484fSRodney W. Grimes 		break;
121158f0484fSRodney W. Grimes 	case REP(1, N):			/* as x?x{1,n-1} */
121258f0484fSRodney W. Grimes 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
121358f0484fSRodney W. Grimes 		INSERT(OCH_, start);
121458f0484fSRodney W. Grimes 		ASTERN(OOR1, start);
121558f0484fSRodney W. Grimes 		AHEAD(start);
121658f0484fSRodney W. Grimes 		EMIT(OOR2, 0);			/* offset very wrong... */
121758f0484fSRodney W. Grimes 		AHEAD(THERE());			/* ...so fix it */
121858f0484fSRodney W. Grimes 		ASTERN(O_CH, THERETHERE());
121958f0484fSRodney W. Grimes 		copy = dupl(p, start+1, finish+1);
122058f0484fSRodney W. Grimes 		assert(copy == finish+4);
122158f0484fSRodney W. Grimes 		repeat(p, copy, 1, to-1);
122258f0484fSRodney W. Grimes 		break;
122358f0484fSRodney W. Grimes 	case REP(1, INF):		/* as x+ */
122458f0484fSRodney W. Grimes 		INSERT(OPLUS_, start);
122558f0484fSRodney W. Grimes 		ASTERN(O_PLUS, start);
122658f0484fSRodney W. Grimes 		break;
122758f0484fSRodney W. Grimes 	case REP(N, N):			/* as xx{m-1,n-1} */
122858f0484fSRodney W. Grimes 		copy = dupl(p, start, finish);
122958f0484fSRodney W. Grimes 		repeat(p, copy, from-1, to-1);
123058f0484fSRodney W. Grimes 		break;
123158f0484fSRodney W. Grimes 	case REP(N, INF):		/* as xx{n-1,INF} */
123258f0484fSRodney W. Grimes 		copy = dupl(p, start, finish);
123358f0484fSRodney W. Grimes 		repeat(p, copy, from-1, to);
123458f0484fSRodney W. Grimes 		break;
123558f0484fSRodney W. Grimes 	default:			/* "can't happen" */
123658f0484fSRodney W. Grimes 		SETERROR(REG_ASSERT);	/* just in case */
123758f0484fSRodney W. Grimes 		break;
123858f0484fSRodney W. Grimes 	}
123958f0484fSRodney W. Grimes }
124058f0484fSRodney W. Grimes 
124158f0484fSRodney W. Grimes /*
1242e5996857STim J. Robbins  - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1243e5996857STim J. Robbins  - character from the parse struct, signals a REG_ILLSEQ error if the
1244e5996857STim J. Robbins  - character can't be converted. Returns the number of bytes consumed.
1245e5996857STim J. Robbins  */
1246e5996857STim J. Robbins static wint_t
124754a648d1SXin LI wgetnext(struct parse *p)
1248e5996857STim J. Robbins {
1249e5996857STim J. Robbins 	mbstate_t mbs;
1250e5996857STim J. Robbins 	wchar_t wc;
1251e5996857STim J. Robbins 	size_t n;
1252e5996857STim J. Robbins 
1253e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1254e5996857STim J. Robbins 	n = mbrtowc(&wc, p->next, p->end - p->next, &mbs);
1255e5996857STim J. Robbins 	if (n == (size_t)-1 || n == (size_t)-2) {
1256e5996857STim J. Robbins 		SETERROR(REG_ILLSEQ);
1257e5996857STim J. Robbins 		return (0);
1258e5996857STim J. Robbins 	}
1259e5996857STim J. Robbins 	if (n == 0)
1260e5996857STim J. Robbins 		n = 1;
1261e5996857STim J. Robbins 	p->next += n;
1262e5996857STim J. Robbins 	return (wc);
1263e5996857STim J. Robbins }
1264e5996857STim J. Robbins 
1265e5996857STim J. Robbins /*
126658f0484fSRodney W. Grimes  - seterr - set an error condition
12678fb3f3f6SDavid E. O'Brien  == static int seterr(struct parse *p, int e);
126858f0484fSRodney W. Grimes  */
126958f0484fSRodney W. Grimes static int			/* useless but makes type checking happy */
127054a648d1SXin LI seterr(struct parse *p, int e)
127158f0484fSRodney W. Grimes {
127258f0484fSRodney W. Grimes 	if (p->error == 0)	/* keep earliest error condition */
127358f0484fSRodney W. Grimes 		p->error = e;
127458f0484fSRodney W. Grimes 	p->next = nuls;		/* try to bring things to a halt */
127558f0484fSRodney W. Grimes 	p->end = nuls;
127658f0484fSRodney W. Grimes 	return(0);		/* make the return value well-defined */
127758f0484fSRodney W. Grimes }
127858f0484fSRodney W. Grimes 
127958f0484fSRodney W. Grimes /*
128058f0484fSRodney W. Grimes  - allocset - allocate a set of characters for []
12818fb3f3f6SDavid E. O'Brien  == static cset *allocset(struct parse *p);
128258f0484fSRodney W. Grimes  */
128358f0484fSRodney W. Grimes static cset *
128454a648d1SXin LI allocset(struct parse *p)
128558f0484fSRodney W. Grimes {
1286e5996857STim J. Robbins 	cset *cs, *ncs;
128758f0484fSRodney W. Grimes 
12889f36610fSPedro F. Giffuni 	ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs));
1289e5996857STim J. Robbins 	if (ncs == NULL) {
129058f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
1291e5996857STim J. Robbins 		return (NULL);
129258f0484fSRodney W. Grimes 	}
1293e5996857STim J. Robbins 	p->g->sets = ncs;
1294e5996857STim J. Robbins 	cs = &p->g->sets[p->g->ncsets++];
1295e5996857STim J. Robbins 	memset(cs, 0, sizeof(*cs));
129658f0484fSRodney W. Grimes 
129758f0484fSRodney W. Grimes 	return(cs);
129858f0484fSRodney W. Grimes }
129958f0484fSRodney W. Grimes 
130058f0484fSRodney W. Grimes /*
130158f0484fSRodney W. Grimes  - freeset - free a now-unused set
13028fb3f3f6SDavid E. O'Brien  == static void freeset(struct parse *p, cset *cs);
130358f0484fSRodney W. Grimes  */
130458f0484fSRodney W. Grimes static void
130554a648d1SXin LI freeset(struct parse *p, cset *cs)
130658f0484fSRodney W. Grimes {
13078fb3f3f6SDavid E. O'Brien 	cset *top = &p->g->sets[p->g->ncsets];
130858f0484fSRodney W. Grimes 
1309e5996857STim J. Robbins 	free(cs->wides);
1310e5996857STim J. Robbins 	free(cs->ranges);
1311e5996857STim J. Robbins 	free(cs->types);
1312e5996857STim J. Robbins 	memset(cs, 0, sizeof(*cs));
131358f0484fSRodney W. Grimes 	if (cs == top-1)	/* recover only the easy case */
131458f0484fSRodney W. Grimes 		p->g->ncsets--;
131558f0484fSRodney W. Grimes }
131658f0484fSRodney W. Grimes 
131758f0484fSRodney W. Grimes /*
1318e5996857STim J. Robbins  - singleton - Determine whether a set contains only one character,
1319e5996857STim J. Robbins  - returning it if so, otherwise returning OUT.
132058f0484fSRodney W. Grimes  */
1321e5996857STim J. Robbins static wint_t
132254a648d1SXin LI singleton(cset *cs)
132358f0484fSRodney W. Grimes {
1324e5996857STim J. Robbins 	wint_t i, s, n;
132558f0484fSRodney W. Grimes 
1326e5996857STim J. Robbins 	for (i = n = 0; i < NC; i++)
1327e5996857STim J. Robbins 		if (CHIN(cs, i)) {
132858f0484fSRodney W. Grimes 			n++;
1329e5996857STim J. Robbins 			s = i;
1330e5996857STim J. Robbins 		}
1331e5996857STim J. Robbins 	if (n == 1)
1332e5996857STim J. Robbins 		return (s);
1333e5996857STim J. Robbins 	if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 &&
1334e5996857STim J. Robbins 	    cs->icase == 0)
1335e5996857STim J. Robbins 		return (cs->wides[0]);
1336e5996857STim J. Robbins 	/* Don't bother handling the other cases. */
1337e5996857STim J. Robbins 	return (OUT);
1338e5996857STim J. Robbins }
1339e5996857STim J. Robbins 
1340e5996857STim J. Robbins /*
1341e5996857STim J. Robbins  - CHadd - add character to character set.
1342e5996857STim J. Robbins  */
1343e5996857STim J. Robbins static void
134454a648d1SXin LI CHadd(struct parse *p, cset *cs, wint_t ch)
1345e5996857STim J. Robbins {
134633176f17STim J. Robbins 	wint_t nch, *newwides;
1347e5996857STim J. Robbins 	assert(ch >= 0);
134833176f17STim J. Robbins 	if (ch < NC)
1349e5996857STim J. Robbins 		cs->bmp[ch >> 3] |= 1 << (ch & 7);
135033176f17STim J. Robbins 	else {
13519f36610fSPedro F. Giffuni 		newwides = reallocarray(cs->wides, cs->nwides + 1,
1352e5996857STim J. Robbins 		    sizeof(*cs->wides));
1353e5996857STim J. Robbins 		if (newwides == NULL) {
1354e5996857STim J. Robbins 			SETERROR(REG_ESPACE);
1355e5996857STim J. Robbins 			return;
1356e5996857STim J. Robbins 		}
1357e5996857STim J. Robbins 		cs->wides = newwides;
1358e5996857STim J. Robbins 		cs->wides[cs->nwides++] = ch;
1359e5996857STim J. Robbins 	}
136033176f17STim J. Robbins 	if (cs->icase) {
136133176f17STim J. Robbins 		if ((nch = towlower(ch)) < NC)
136233176f17STim J. Robbins 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
136333176f17STim J. Robbins 		if ((nch = towupper(ch)) < NC)
136433176f17STim J. Robbins 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
136533176f17STim J. Robbins 	}
1366e5996857STim J. Robbins }
1367e5996857STim J. Robbins 
1368e5996857STim J. Robbins /*
1369e5996857STim J. Robbins  - CHaddrange - add all characters in the range [min,max] to a character set.
1370e5996857STim J. Robbins  */
1371e5996857STim J. Robbins static void
137254a648d1SXin LI CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max)
1373e5996857STim J. Robbins {
1374e5996857STim J. Robbins 	crange *newranges;
1375e5996857STim J. Robbins 
1376e5996857STim J. Robbins 	for (; min < NC && min <= max; min++)
1377e5996857STim J. Robbins 		CHadd(p, cs, min);
1378e5996857STim J. Robbins 	if (min >= max)
1379e5996857STim J. Robbins 		return;
13809f36610fSPedro F. Giffuni 	newranges = reallocarray(cs->ranges, cs->nranges + 1,
1381e5996857STim J. Robbins 	    sizeof(*cs->ranges));
1382e5996857STim J. Robbins 	if (newranges == NULL) {
1383e5996857STim J. Robbins 		SETERROR(REG_ESPACE);
1384e5996857STim J. Robbins 		return;
1385e5996857STim J. Robbins 	}
1386e5996857STim J. Robbins 	cs->ranges = newranges;
1387e5996857STim J. Robbins 	cs->ranges[cs->nranges].min = min;
1388d0ebccdeSXin LI 	cs->ranges[cs->nranges].max = max;
1389e5996857STim J. Robbins 	cs->nranges++;
1390e5996857STim J. Robbins }
1391e5996857STim J. Robbins 
1392e5996857STim J. Robbins /*
1393e5996857STim J. Robbins  - CHaddtype - add all characters of a certain type to a character set.
1394e5996857STim J. Robbins  */
1395e5996857STim J. Robbins static void
139654a648d1SXin LI CHaddtype(struct parse *p, cset *cs, wctype_t wct)
1397e5996857STim J. Robbins {
1398e5996857STim J. Robbins 	wint_t i;
1399e5996857STim J. Robbins 	wctype_t *newtypes;
1400e5996857STim J. Robbins 
140133176f17STim J. Robbins 	for (i = 0; i < NC; i++)
1402e5996857STim J. Robbins 		if (iswctype(i, wct))
1403e5996857STim J. Robbins 			CHadd(p, cs, i);
14049f36610fSPedro F. Giffuni 	newtypes = reallocarray(cs->types, cs->ntypes + 1,
1405e5996857STim J. Robbins 	    sizeof(*cs->types));
1406e5996857STim J. Robbins 	if (newtypes == NULL) {
1407e5996857STim J. Robbins 		SETERROR(REG_ESPACE);
1408e5996857STim J. Robbins 		return;
1409e5996857STim J. Robbins 	}
1410e5996857STim J. Robbins 	cs->types = newtypes;
1411e5996857STim J. Robbins 	cs->types[cs->ntypes++] = wct;
141258f0484fSRodney W. Grimes }
141358f0484fSRodney W. Grimes 
141458f0484fSRodney W. Grimes /*
141558f0484fSRodney W. Grimes  - dupl - emit a duplicate of a bunch of sops
14168fb3f3f6SDavid E. O'Brien  == static sopno dupl(struct parse *p, sopno start, sopno finish);
141758f0484fSRodney W. Grimes  */
141858f0484fSRodney W. Grimes static sopno			/* start of duplicate */
141954a648d1SXin LI dupl(struct parse *p,
142054a648d1SXin LI 	sopno start,		/* from here */
142154a648d1SXin LI 	sopno finish)		/* to this less one */
142258f0484fSRodney W. Grimes {
14238fb3f3f6SDavid E. O'Brien 	sopno ret = HERE();
14248fb3f3f6SDavid E. O'Brien 	sopno len = finish - start;
142558f0484fSRodney W. Grimes 
142658f0484fSRodney W. Grimes 	assert(finish >= start);
142758f0484fSRodney W. Grimes 	if (len == 0)
142858f0484fSRodney W. Grimes 		return(ret);
14292bf213ebSKevin Lo 	if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */
14302bf213ebSKevin Lo 		return(ret);
143158f0484fSRodney W. Grimes 	(void) memcpy((char *)(p->strip + p->slen),
143258f0484fSRodney W. Grimes 		(char *)(p->strip + start), (size_t)len*sizeof(sop));
143358f0484fSRodney W. Grimes 	p->slen += len;
143458f0484fSRodney W. Grimes 	return(ret);
143558f0484fSRodney W. Grimes }
143658f0484fSRodney W. Grimes 
143758f0484fSRodney W. Grimes /*
143858f0484fSRodney W. Grimes  - doemit - emit a strip operator
14398fb3f3f6SDavid E. O'Brien  == static void doemit(struct parse *p, sop op, size_t opnd);
144058f0484fSRodney W. Grimes  *
144158f0484fSRodney W. Grimes  * It might seem better to implement this as a macro with a function as
144258f0484fSRodney W. Grimes  * hard-case backup, but it's just too big and messy unless there are
144358f0484fSRodney W. Grimes  * some changes to the data structures.  Maybe later.
144458f0484fSRodney W. Grimes  */
144558f0484fSRodney W. Grimes static void
144654a648d1SXin LI doemit(struct parse *p, sop op, size_t opnd)
144758f0484fSRodney W. Grimes {
144858f0484fSRodney W. Grimes 	/* avoid making error situations worse */
144958f0484fSRodney W. Grimes 	if (p->error != 0)
145058f0484fSRodney W. Grimes 		return;
145158f0484fSRodney W. Grimes 
145258f0484fSRodney W. Grimes 	/* deal with oversize operands ("can't happen", more or less) */
145358f0484fSRodney W. Grimes 	assert(opnd < 1<<OPSHIFT);
145458f0484fSRodney W. Grimes 
145558f0484fSRodney W. Grimes 	/* deal with undersized strip */
145658f0484fSRodney W. Grimes 	if (p->slen >= p->ssize)
14572bf213ebSKevin Lo 		if (!enlarge(p, (p->ssize+1) / 2 * 3))	/* +50% */
14582bf213ebSKevin Lo 			return;
145958f0484fSRodney W. Grimes 
146058f0484fSRodney W. Grimes 	/* finally, it's all reduced to the easy case */
146158f0484fSRodney W. Grimes 	p->strip[p->slen++] = SOP(op, opnd);
146258f0484fSRodney W. Grimes }
146358f0484fSRodney W. Grimes 
146458f0484fSRodney W. Grimes /*
146558f0484fSRodney W. Grimes  - doinsert - insert a sop into the strip
14668fb3f3f6SDavid E. O'Brien  == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
146758f0484fSRodney W. Grimes  */
146858f0484fSRodney W. Grimes static void
146954a648d1SXin LI doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
147058f0484fSRodney W. Grimes {
14718fb3f3f6SDavid E. O'Brien 	sopno sn;
14728fb3f3f6SDavid E. O'Brien 	sop s;
14738fb3f3f6SDavid E. O'Brien 	int i;
147458f0484fSRodney W. Grimes 
147558f0484fSRodney W. Grimes 	/* avoid making error situations worse */
147658f0484fSRodney W. Grimes 	if (p->error != 0)
147758f0484fSRodney W. Grimes 		return;
147858f0484fSRodney W. Grimes 
147958f0484fSRodney W. Grimes 	sn = HERE();
148058f0484fSRodney W. Grimes 	EMIT(op, opnd);		/* do checks, ensure space */
148158f0484fSRodney W. Grimes 	assert(HERE() == sn+1);
148258f0484fSRodney W. Grimes 	s = p->strip[sn];
148358f0484fSRodney W. Grimes 
148458f0484fSRodney W. Grimes 	/* adjust paren pointers */
148558f0484fSRodney W. Grimes 	assert(pos > 0);
148658f0484fSRodney W. Grimes 	for (i = 1; i < NPAREN; i++) {
148758f0484fSRodney W. Grimes 		if (p->pbegin[i] >= pos) {
148858f0484fSRodney W. Grimes 			p->pbegin[i]++;
148958f0484fSRodney W. Grimes 		}
149058f0484fSRodney W. Grimes 		if (p->pend[i] >= pos) {
149158f0484fSRodney W. Grimes 			p->pend[i]++;
149258f0484fSRodney W. Grimes 		}
149358f0484fSRodney W. Grimes 	}
149458f0484fSRodney W. Grimes 
149558f0484fSRodney W. Grimes 	memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
149658f0484fSRodney W. Grimes 						(HERE()-pos-1)*sizeof(sop));
149758f0484fSRodney W. Grimes 	p->strip[pos] = s;
149858f0484fSRodney W. Grimes }
149958f0484fSRodney W. Grimes 
150058f0484fSRodney W. Grimes /*
150158f0484fSRodney W. Grimes  - dofwd - complete a forward reference
15028fb3f3f6SDavid E. O'Brien  == static void dofwd(struct parse *p, sopno pos, sop value);
150358f0484fSRodney W. Grimes  */
150458f0484fSRodney W. Grimes static void
150554a648d1SXin LI dofwd(struct parse *p, sopno pos, sop value)
150658f0484fSRodney W. Grimes {
150758f0484fSRodney W. Grimes 	/* avoid making error situations worse */
150858f0484fSRodney W. Grimes 	if (p->error != 0)
150958f0484fSRodney W. Grimes 		return;
151058f0484fSRodney W. Grimes 
151158f0484fSRodney W. Grimes 	assert(value < 1<<OPSHIFT);
151258f0484fSRodney W. Grimes 	p->strip[pos] = OP(p->strip[pos]) | value;
151358f0484fSRodney W. Grimes }
151458f0484fSRodney W. Grimes 
151558f0484fSRodney W. Grimes /*
151658f0484fSRodney W. Grimes  - enlarge - enlarge the strip
15172bf213ebSKevin Lo  == static int enlarge(struct parse *p, sopno size);
151858f0484fSRodney W. Grimes  */
15192bf213ebSKevin Lo static int
152054a648d1SXin LI enlarge(struct parse *p, sopno size)
152158f0484fSRodney W. Grimes {
15228fb3f3f6SDavid E. O'Brien 	sop *sp;
152358f0484fSRodney W. Grimes 
152458f0484fSRodney W. Grimes 	if (p->ssize >= size)
15252bf213ebSKevin Lo 		return 1;
152658f0484fSRodney W. Grimes 
15279f36610fSPedro F. Giffuni 	sp = reallocarray(p->strip, size, sizeof(sop));
152858f0484fSRodney W. Grimes 	if (sp == NULL) {
152958f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
15302bf213ebSKevin Lo 		return 0;
153158f0484fSRodney W. Grimes 	}
153258f0484fSRodney W. Grimes 	p->strip = sp;
153358f0484fSRodney W. Grimes 	p->ssize = size;
15342bf213ebSKevin Lo 	return 1;
153558f0484fSRodney W. Grimes }
153658f0484fSRodney W. Grimes 
153758f0484fSRodney W. Grimes /*
153858f0484fSRodney W. Grimes  - stripsnug - compact the strip
15398fb3f3f6SDavid E. O'Brien  == static void stripsnug(struct parse *p, struct re_guts *g);
154058f0484fSRodney W. Grimes  */
154158f0484fSRodney W. Grimes static void
154254a648d1SXin LI stripsnug(struct parse *p, struct re_guts *g)
154358f0484fSRodney W. Grimes {
154458f0484fSRodney W. Grimes 	g->nstates = p->slen;
15459f36610fSPedro F. Giffuni 	g->strip = reallocarray((char *)p->strip, p->slen, sizeof(sop));
154658f0484fSRodney W. Grimes 	if (g->strip == NULL) {
154758f0484fSRodney W. Grimes 		SETERROR(REG_ESPACE);
154858f0484fSRodney W. Grimes 		g->strip = p->strip;
154958f0484fSRodney W. Grimes 	}
155058f0484fSRodney W. Grimes }
155158f0484fSRodney W. Grimes 
155258f0484fSRodney W. Grimes /*
155358f0484fSRodney W. Grimes  - findmust - fill in must and mlen with longest mandatory literal string
15548fb3f3f6SDavid E. O'Brien  == static void findmust(struct parse *p, struct re_guts *g);
155558f0484fSRodney W. Grimes  *
155658f0484fSRodney W. Grimes  * This algorithm could do fancy things like analyzing the operands of |
155758f0484fSRodney W. Grimes  * for common subsequences.  Someday.  This code is simple and finds most
155858f0484fSRodney W. Grimes  * of the interesting cases.
155958f0484fSRodney W. Grimes  *
156058f0484fSRodney W. Grimes  * Note that must and mlen got initialized during setup.
156158f0484fSRodney W. Grimes  */
156258f0484fSRodney W. Grimes static void
156354a648d1SXin LI findmust(struct parse *p, struct re_guts *g)
156458f0484fSRodney W. Grimes {
15658fb3f3f6SDavid E. O'Brien 	sop *scan;
1566d58abbfeSPedro F. Giffuni 	sop *start = NULL;
1567d58abbfeSPedro F. Giffuni 	sop *newstart = NULL;
15688fb3f3f6SDavid E. O'Brien 	sopno newlen;
15698fb3f3f6SDavid E. O'Brien 	sop s;
15708fb3f3f6SDavid E. O'Brien 	char *cp;
1571e6a886d8SDaniel C. Sobral 	int offset;
1572e5996857STim J. Robbins 	char buf[MB_LEN_MAX];
1573e5996857STim J. Robbins 	size_t clen;
1574e5996857STim J. Robbins 	mbstate_t mbs;
157558f0484fSRodney W. Grimes 
157658f0484fSRodney W. Grimes 	/* avoid making error situations worse */
157758f0484fSRodney W. Grimes 	if (p->error != 0)
157858f0484fSRodney W. Grimes 		return;
157958f0484fSRodney W. Grimes 
1580e5996857STim J. Robbins 	/*
1581e5996857STim J. Robbins 	 * It's not generally safe to do a ``char'' substring search on
1582e5996857STim J. Robbins 	 * multibyte character strings, but it's safe for at least
1583e5996857STim J. Robbins 	 * UTF-8 (see RFC 3629).
1584e5996857STim J. Robbins 	 */
1585e5996857STim J. Robbins 	if (MB_CUR_MAX > 1 &&
1586e5996857STim J. Robbins 	    strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
1587e5996857STim J. Robbins 		return;
1588e5996857STim J. Robbins 
158958f0484fSRodney W. Grimes 	/* find the longest OCHAR sequence in strip */
159058f0484fSRodney W. Grimes 	newlen = 0;
1591e6a886d8SDaniel C. Sobral 	offset = 0;
1592e6a886d8SDaniel C. Sobral 	g->moffset = 0;
159358f0484fSRodney W. Grimes 	scan = g->strip + 1;
159458f0484fSRodney W. Grimes 	do {
159558f0484fSRodney W. Grimes 		s = *scan++;
159658f0484fSRodney W. Grimes 		switch (OP(s)) {
159758f0484fSRodney W. Grimes 		case OCHAR:		/* sequence member */
1598e5996857STim J. Robbins 			if (newlen == 0) {		/* new sequence */
1599e5996857STim J. Robbins 				memset(&mbs, 0, sizeof(mbs));
160058f0484fSRodney W. Grimes 				newstart = scan - 1;
1601e5996857STim J. Robbins 			}
1602e5996857STim J. Robbins 			clen = wcrtomb(buf, OPND(s), &mbs);
1603e5996857STim J. Robbins 			if (clen == (size_t)-1)
1604e5996857STim J. Robbins 				goto toohard;
1605e5996857STim J. Robbins 			newlen += clen;
160658f0484fSRodney W. Grimes 			break;
160758f0484fSRodney W. Grimes 		case OPLUS_:		/* things that don't break one */
160858f0484fSRodney W. Grimes 		case OLPAREN:
160958f0484fSRodney W. Grimes 		case ORPAREN:
161058f0484fSRodney W. Grimes 			break;
161158f0484fSRodney W. Grimes 		case OQUEST_:		/* things that must be skipped */
161258f0484fSRodney W. Grimes 		case OCH_:
161380f36366STim J. Robbins 			offset = altoffset(scan, offset);
161458f0484fSRodney W. Grimes 			scan--;
161558f0484fSRodney W. Grimes 			do {
161658f0484fSRodney W. Grimes 				scan += OPND(s);
161758f0484fSRodney W. Grimes 				s = *scan;
161858f0484fSRodney W. Grimes 				/* assert() interferes w debug printouts */
161958f0484fSRodney W. Grimes 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
162058f0484fSRodney W. Grimes 							OP(s) != OOR2) {
162158f0484fSRodney W. Grimes 					g->iflags |= BAD;
162258f0484fSRodney W. Grimes 					return;
162358f0484fSRodney W. Grimes 				}
162458f0484fSRodney W. Grimes 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
16257fed38d0SPhilippe Charnier 			/* FALLTHROUGH */
1626e6a886d8SDaniel C. Sobral 		case OBOW:		/* things that break a sequence */
1627e6a886d8SDaniel C. Sobral 		case OEOW:
1628e6a886d8SDaniel C. Sobral 		case OBOL:
1629e6a886d8SDaniel C. Sobral 		case OEOL:
1630e6a886d8SDaniel C. Sobral 		case O_QUEST:
1631e6a886d8SDaniel C. Sobral 		case O_CH:
1632e6a886d8SDaniel C. Sobral 		case OEND:
163358f0484fSRodney W. Grimes 			if (newlen > g->mlen) {		/* ends one */
163458f0484fSRodney W. Grimes 				start = newstart;
163558f0484fSRodney W. Grimes 				g->mlen = newlen;
1636e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1637e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1638e6a886d8SDaniel C. Sobral 					offset = newlen;
1639e6a886d8SDaniel C. Sobral 				} else
1640e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1641e6a886d8SDaniel C. Sobral 			} else {
1642e6a886d8SDaniel C. Sobral 				if (offset > -1)
1643e6a886d8SDaniel C. Sobral 					offset += newlen;
164458f0484fSRodney W. Grimes 			}
164558f0484fSRodney W. Grimes 			newlen = 0;
164658f0484fSRodney W. Grimes 			break;
1647e6a886d8SDaniel C. Sobral 		case OANY:
1648e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1649e6a886d8SDaniel C. Sobral 				start = newstart;
1650e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1651e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1652e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1653e6a886d8SDaniel C. Sobral 					offset = newlen;
1654e6a886d8SDaniel C. Sobral 				} else
1655e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1656e6a886d8SDaniel C. Sobral 			} else {
1657e6a886d8SDaniel C. Sobral 				if (offset > -1)
1658e6a886d8SDaniel C. Sobral 					offset += newlen;
1659e6a886d8SDaniel C. Sobral 			}
1660e6a886d8SDaniel C. Sobral 			if (offset > -1)
1661e6a886d8SDaniel C. Sobral 				offset++;
1662e6a886d8SDaniel C. Sobral 			newlen = 0;
1663e6a886d8SDaniel C. Sobral 			break;
1664e6a886d8SDaniel C. Sobral 		case OANYOF:		/* may or may not invalidate offset */
1665e6a886d8SDaniel C. Sobral 			/* First, everything as OANY */
1666e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1667e6a886d8SDaniel C. Sobral 				start = newstart;
1668e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1669e6a886d8SDaniel C. Sobral 				if (offset > -1) {
1670e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1671e6a886d8SDaniel C. Sobral 					offset = newlen;
1672e6a886d8SDaniel C. Sobral 				} else
1673e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1674e6a886d8SDaniel C. Sobral 			} else {
1675e6a886d8SDaniel C. Sobral 				if (offset > -1)
1676e6a886d8SDaniel C. Sobral 					offset += newlen;
1677e6a886d8SDaniel C. Sobral 			}
1678e6a886d8SDaniel C. Sobral 			if (offset > -1)
1679e6a886d8SDaniel C. Sobral 				offset++;
1680e6a886d8SDaniel C. Sobral 			newlen = 0;
1681e6a886d8SDaniel C. Sobral 			break;
1682e5996857STim J. Robbins 		toohard:
1683e6a886d8SDaniel C. Sobral 		default:
1684e6a886d8SDaniel C. Sobral 			/* Anything here makes it impossible or too hard
1685e6a886d8SDaniel C. Sobral 			 * to calculate the offset -- so we give up;
1686e6a886d8SDaniel C. Sobral 			 * save the last known good offset, in case the
1687e6a886d8SDaniel C. Sobral 			 * must sequence doesn't occur later.
1688e6a886d8SDaniel C. Sobral 			 */
1689e6a886d8SDaniel C. Sobral 			if (newlen > g->mlen) {		/* ends one */
1690e6a886d8SDaniel C. Sobral 				start = newstart;
1691e6a886d8SDaniel C. Sobral 				g->mlen = newlen;
1692e6a886d8SDaniel C. Sobral 				if (offset > -1)
1693e6a886d8SDaniel C. Sobral 					g->moffset += offset;
1694e6a886d8SDaniel C. Sobral 				else
1695e6a886d8SDaniel C. Sobral 					g->moffset = offset;
1696e6a886d8SDaniel C. Sobral 			}
1697e6a886d8SDaniel C. Sobral 			offset = -1;
1698e6a886d8SDaniel C. Sobral 			newlen = 0;
1699e6a886d8SDaniel C. Sobral 			break;
170058f0484fSRodney W. Grimes 		}
170158f0484fSRodney W. Grimes 	} while (OP(s) != OEND);
170258f0484fSRodney W. Grimes 
1703e6a886d8SDaniel C. Sobral 	if (g->mlen == 0) {		/* there isn't one */
1704e6a886d8SDaniel C. Sobral 		g->moffset = -1;
170558f0484fSRodney W. Grimes 		return;
1706e6a886d8SDaniel C. Sobral 	}
170758f0484fSRodney W. Grimes 
170858f0484fSRodney W. Grimes 	/* turn it into a character string */
170958f0484fSRodney W. Grimes 	g->must = malloc((size_t)g->mlen + 1);
171058f0484fSRodney W. Grimes 	if (g->must == NULL) {		/* argh; just forget it */
171158f0484fSRodney W. Grimes 		g->mlen = 0;
1712e6a886d8SDaniel C. Sobral 		g->moffset = -1;
171358f0484fSRodney W. Grimes 		return;
171458f0484fSRodney W. Grimes 	}
171558f0484fSRodney W. Grimes 	cp = g->must;
171658f0484fSRodney W. Grimes 	scan = start;
1717e5996857STim J. Robbins 	memset(&mbs, 0, sizeof(mbs));
1718e5996857STim J. Robbins 	while (cp < g->must + g->mlen) {
171958f0484fSRodney W. Grimes 		while (OP(s = *scan++) != OCHAR)
172058f0484fSRodney W. Grimes 			continue;
1721e5996857STim J. Robbins 		clen = wcrtomb(cp, OPND(s), &mbs);
1722e5996857STim J. Robbins 		assert(clen != (size_t)-1);
1723e5996857STim J. Robbins 		cp += clen;
172458f0484fSRodney W. Grimes 	}
172558f0484fSRodney W. Grimes 	assert(cp == g->must + g->mlen);
172658f0484fSRodney W. Grimes 	*cp++ = '\0';		/* just on general principles */
172758f0484fSRodney W. Grimes }
172858f0484fSRodney W. Grimes 
172958f0484fSRodney W. Grimes /*
1730e6a886d8SDaniel C. Sobral  - altoffset - choose biggest offset among multiple choices
173180f36366STim J. Robbins  == static int altoffset(sop *scan, int offset);
1732e6a886d8SDaniel C. Sobral  *
1733e6a886d8SDaniel C. Sobral  * Compute, recursively if necessary, the largest offset among multiple
1734e6a886d8SDaniel C. Sobral  * re paths.
1735e6a886d8SDaniel C. Sobral  */
1736e6a886d8SDaniel C. Sobral static int
173754a648d1SXin LI altoffset(sop *scan, int offset)
1738e6a886d8SDaniel C. Sobral {
1739e6a886d8SDaniel C. Sobral 	int largest;
1740e6a886d8SDaniel C. Sobral 	int try;
1741e6a886d8SDaniel C. Sobral 	sop s;
1742e6a886d8SDaniel C. Sobral 
1743e6a886d8SDaniel C. Sobral 	/* If we gave up already on offsets, return */
1744e6a886d8SDaniel C. Sobral 	if (offset == -1)
1745e6a886d8SDaniel C. Sobral 		return -1;
1746e6a886d8SDaniel C. Sobral 
1747e6a886d8SDaniel C. Sobral 	largest = 0;
1748e6a886d8SDaniel C. Sobral 	try = 0;
1749e6a886d8SDaniel C. Sobral 	s = *scan++;
1750e6a886d8SDaniel C. Sobral 	while (OP(s) != O_QUEST && OP(s) != O_CH) {
1751e6a886d8SDaniel C. Sobral 		switch (OP(s)) {
1752e6a886d8SDaniel C. Sobral 		case OOR1:
1753e6a886d8SDaniel C. Sobral 			if (try > largest)
1754e6a886d8SDaniel C. Sobral 				largest = try;
1755e6a886d8SDaniel C. Sobral 			try = 0;
1756e6a886d8SDaniel C. Sobral 			break;
1757e6a886d8SDaniel C. Sobral 		case OQUEST_:
1758e6a886d8SDaniel C. Sobral 		case OCH_:
175980f36366STim J. Robbins 			try = altoffset(scan, try);
1760e6a886d8SDaniel C. Sobral 			if (try == -1)
1761e6a886d8SDaniel C. Sobral 				return -1;
1762e6a886d8SDaniel C. Sobral 			scan--;
1763e6a886d8SDaniel C. Sobral 			do {
1764e6a886d8SDaniel C. Sobral 				scan += OPND(s);
1765e6a886d8SDaniel C. Sobral 				s = *scan;
1766e6a886d8SDaniel C. Sobral 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
1767e6a886d8SDaniel C. Sobral 							OP(s) != OOR2)
1768e6a886d8SDaniel C. Sobral 					return -1;
1769e6a886d8SDaniel C. Sobral 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
17708f9e434fSDaniel C. Sobral 			/* We must skip to the next position, or we'll
17718f9e434fSDaniel C. Sobral 			 * leave altoffset() too early.
17728f9e434fSDaniel C. Sobral 			 */
17738f9e434fSDaniel C. Sobral 			scan++;
1774e6a886d8SDaniel C. Sobral 			break;
1775e6a886d8SDaniel C. Sobral 		case OANYOF:
1776e6a886d8SDaniel C. Sobral 		case OCHAR:
1777e6a886d8SDaniel C. Sobral 		case OANY:
1778e6a886d8SDaniel C. Sobral 			try++;
1779e6a886d8SDaniel C. Sobral 		case OBOW:
1780e6a886d8SDaniel C. Sobral 		case OEOW:
1781e6a886d8SDaniel C. Sobral 		case OLPAREN:
1782e6a886d8SDaniel C. Sobral 		case ORPAREN:
1783e6a886d8SDaniel C. Sobral 		case OOR2:
1784e6a886d8SDaniel C. Sobral 			break;
1785e6a886d8SDaniel C. Sobral 		default:
1786e6a886d8SDaniel C. Sobral 			try = -1;
1787e6a886d8SDaniel C. Sobral 			break;
1788e6a886d8SDaniel C. Sobral 		}
1789e6a886d8SDaniel C. Sobral 		if (try == -1)
1790e6a886d8SDaniel C. Sobral 			return -1;
1791e6a886d8SDaniel C. Sobral 		s = *scan++;
1792e6a886d8SDaniel C. Sobral 	}
1793e6a886d8SDaniel C. Sobral 
1794e6a886d8SDaniel C. Sobral 	if (try > largest)
1795e6a886d8SDaniel C. Sobral 		largest = try;
1796e6a886d8SDaniel C. Sobral 
1797e6a886d8SDaniel C. Sobral 	return largest+offset;
1798e6a886d8SDaniel C. Sobral }
1799e6a886d8SDaniel C. Sobral 
1800e6a886d8SDaniel C. Sobral /*
18016049d9f0SDaniel C. Sobral  - computejumps - compute char jumps for BM scan
18028fb3f3f6SDavid E. O'Brien  == static void computejumps(struct parse *p, struct re_guts *g);
18036049d9f0SDaniel C. Sobral  *
18046049d9f0SDaniel C. Sobral  * This algorithm assumes g->must exists and is has size greater than
18056049d9f0SDaniel C. Sobral  * zero. It's based on the algorithm found on Computer Algorithms by
18066049d9f0SDaniel C. Sobral  * Sara Baase.
18076049d9f0SDaniel C. Sobral  *
18086049d9f0SDaniel C. Sobral  * A char jump is the number of characters one needs to jump based on
18096049d9f0SDaniel C. Sobral  * the value of the character from the text that was mismatched.
18106049d9f0SDaniel C. Sobral  */
18116049d9f0SDaniel C. Sobral static void
181254a648d1SXin LI computejumps(struct parse *p, struct re_guts *g)
18136049d9f0SDaniel C. Sobral {
18146049d9f0SDaniel C. Sobral 	int ch;
18156049d9f0SDaniel C. Sobral 	int mindex;
18166049d9f0SDaniel C. Sobral 
18176049d9f0SDaniel C. Sobral 	/* Avoid making errors worse */
18186049d9f0SDaniel C. Sobral 	if (p->error != 0)
18196049d9f0SDaniel C. Sobral 		return;
18206049d9f0SDaniel C. Sobral 
1821517bffcaSDaniel C. Sobral 	g->charjump = (int*) malloc((NC + 1) * sizeof(int));
18226049d9f0SDaniel C. Sobral 	if (g->charjump == NULL)	/* Not a fatal error */
18236049d9f0SDaniel C. Sobral 		return;
1824c5e125bbSDaniel C. Sobral 	/* Adjust for signed chars, if necessary */
1825c5e125bbSDaniel C. Sobral 	g->charjump = &g->charjump[-(CHAR_MIN)];
18266049d9f0SDaniel C. Sobral 
18276049d9f0SDaniel C. Sobral 	/* If the character does not exist in the pattern, the jump
18286049d9f0SDaniel C. Sobral 	 * is equal to the number of characters in the pattern.
18296049d9f0SDaniel C. Sobral 	 */
1830c5e125bbSDaniel C. Sobral 	for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
18316049d9f0SDaniel C. Sobral 		g->charjump[ch] = g->mlen;
18326049d9f0SDaniel C. Sobral 
18336049d9f0SDaniel C. Sobral 	/* If the character does exist, compute the jump that would
18346049d9f0SDaniel C. Sobral 	 * take us to the last character in the pattern equal to it
18356049d9f0SDaniel C. Sobral 	 * (notice that we match right to left, so that last character
18366049d9f0SDaniel C. Sobral 	 * is the first one that would be matched).
18376049d9f0SDaniel C. Sobral 	 */
18386049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex < g->mlen; mindex++)
1839e0554a53SJacques Vidrine 		g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1;
18406049d9f0SDaniel C. Sobral }
18416049d9f0SDaniel C. Sobral 
18426049d9f0SDaniel C. Sobral /*
18436049d9f0SDaniel C. Sobral  - computematchjumps - compute match jumps for BM scan
18448fb3f3f6SDavid E. O'Brien  == static void computematchjumps(struct parse *p, struct re_guts *g);
18456049d9f0SDaniel C. Sobral  *
18466049d9f0SDaniel C. Sobral  * This algorithm assumes g->must exists and is has size greater than
18476049d9f0SDaniel C. Sobral  * zero. It's based on the algorithm found on Computer Algorithms by
18486049d9f0SDaniel C. Sobral  * Sara Baase.
18496049d9f0SDaniel C. Sobral  *
18506049d9f0SDaniel C. Sobral  * A match jump is the number of characters one needs to advance based
18516049d9f0SDaniel C. Sobral  * on the already-matched suffix.
18526049d9f0SDaniel C. Sobral  * Notice that all values here are minus (g->mlen-1), because of the way
18536049d9f0SDaniel C. Sobral  * the search algorithm works.
18546049d9f0SDaniel C. Sobral  */
18556049d9f0SDaniel C. Sobral static void
185654a648d1SXin LI computematchjumps(struct parse *p, struct re_guts *g)
18576049d9f0SDaniel C. Sobral {
18586049d9f0SDaniel C. Sobral 	int mindex;		/* General "must" iterator */
18596049d9f0SDaniel C. Sobral 	int suffix;		/* Keeps track of matching suffix */
18606049d9f0SDaniel C. Sobral 	int ssuffix;		/* Keeps track of suffixes' suffix */
18616049d9f0SDaniel C. Sobral 	int* pmatches;		/* pmatches[k] points to the next i
18626049d9f0SDaniel C. Sobral 				 * such that i+1...mlen is a substring
18636049d9f0SDaniel C. Sobral 				 * of k+1...k+mlen-i-1
18646049d9f0SDaniel C. Sobral 				 */
18656049d9f0SDaniel C. Sobral 
18666049d9f0SDaniel C. Sobral 	/* Avoid making errors worse */
18676049d9f0SDaniel C. Sobral 	if (p->error != 0)
18686049d9f0SDaniel C. Sobral 		return;
18696049d9f0SDaniel C. Sobral 
187012eb0bcaSPedro F. Giffuni 	pmatches = (int*) malloc(g->mlen * sizeof(int));
18716049d9f0SDaniel C. Sobral 	if (pmatches == NULL) {
18726049d9f0SDaniel C. Sobral 		g->matchjump = NULL;
18736049d9f0SDaniel C. Sobral 		return;
18746049d9f0SDaniel C. Sobral 	}
18756049d9f0SDaniel C. Sobral 
187612eb0bcaSPedro F. Giffuni 	g->matchjump = (int*) malloc(g->mlen * sizeof(int));
1877cfbebadcSXin LI 	if (g->matchjump == NULL) {	/* Not a fatal error */
1878cfbebadcSXin LI 		free(pmatches);
18796049d9f0SDaniel C. Sobral 		return;
1880cfbebadcSXin LI 	}
18816049d9f0SDaniel C. Sobral 
18826049d9f0SDaniel C. Sobral 	/* Set maximum possible jump for each character in the pattern */
18836049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex < g->mlen; mindex++)
18846049d9f0SDaniel C. Sobral 		g->matchjump[mindex] = 2*g->mlen - mindex - 1;
18856049d9f0SDaniel C. Sobral 
18866049d9f0SDaniel C. Sobral 	/* Compute pmatches[] */
18876049d9f0SDaniel C. Sobral 	for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
18886049d9f0SDaniel C. Sobral 	    mindex--, suffix--) {
18896049d9f0SDaniel C. Sobral 		pmatches[mindex] = suffix;
18906049d9f0SDaniel C. Sobral 
18916049d9f0SDaniel C. Sobral 		/* If a mismatch is found, interrupting the substring,
18926049d9f0SDaniel C. Sobral 		 * compute the matchjump for that position. If no
18936049d9f0SDaniel C. Sobral 		 * mismatch is found, then a text substring mismatched
18946049d9f0SDaniel C. Sobral 		 * against the suffix will also mismatch against the
18956049d9f0SDaniel C. Sobral 		 * substring.
18966049d9f0SDaniel C. Sobral 		 */
18976049d9f0SDaniel C. Sobral 		while (suffix < g->mlen
18986049d9f0SDaniel C. Sobral 		    && g->must[mindex] != g->must[suffix]) {
18996049d9f0SDaniel C. Sobral 			g->matchjump[suffix] = MIN(g->matchjump[suffix],
19006049d9f0SDaniel C. Sobral 			    g->mlen - mindex - 1);
19016049d9f0SDaniel C. Sobral 			suffix = pmatches[suffix];
19026049d9f0SDaniel C. Sobral 		}
19036049d9f0SDaniel C. Sobral 	}
19046049d9f0SDaniel C. Sobral 
19056049d9f0SDaniel C. Sobral 	/* Compute the matchjump up to the last substring found to jump
19066049d9f0SDaniel C. Sobral 	 * to the beginning of the largest must pattern prefix matching
19076049d9f0SDaniel C. Sobral 	 * it's own suffix.
19086049d9f0SDaniel C. Sobral 	 */
19096049d9f0SDaniel C. Sobral 	for (mindex = 0; mindex <= suffix; mindex++)
19106049d9f0SDaniel C. Sobral 		g->matchjump[mindex] = MIN(g->matchjump[mindex],
19116049d9f0SDaniel C. Sobral 		    g->mlen + suffix - mindex);
19126049d9f0SDaniel C. Sobral 
19136049d9f0SDaniel C. Sobral         ssuffix = pmatches[suffix];
19146049d9f0SDaniel C. Sobral         while (suffix < g->mlen) {
19159868274bSDaniel C. Sobral                 while (suffix <= ssuffix && suffix < g->mlen) {
19166049d9f0SDaniel C. Sobral                         g->matchjump[suffix] = MIN(g->matchjump[suffix],
19176049d9f0SDaniel C. Sobral 			    g->mlen + ssuffix - suffix);
19186049d9f0SDaniel C. Sobral                         suffix++;
19196049d9f0SDaniel C. Sobral                 }
19208e2f75b8SDaniel C. Sobral 		if (suffix < g->mlen)
19216049d9f0SDaniel C. Sobral                 	ssuffix = pmatches[ssuffix];
19226049d9f0SDaniel C. Sobral         }
19236049d9f0SDaniel C. Sobral 
19246049d9f0SDaniel C. Sobral 	free(pmatches);
19256049d9f0SDaniel C. Sobral }
19266049d9f0SDaniel C. Sobral 
19276049d9f0SDaniel C. Sobral /*
192858f0484fSRodney W. Grimes  - pluscount - count + nesting
19298fb3f3f6SDavid E. O'Brien  == static sopno pluscount(struct parse *p, struct re_guts *g);
193058f0484fSRodney W. Grimes  */
193158f0484fSRodney W. Grimes static sopno			/* nesting depth */
193254a648d1SXin LI pluscount(struct parse *p, struct re_guts *g)
193358f0484fSRodney W. Grimes {
19348fb3f3f6SDavid E. O'Brien 	sop *scan;
19358fb3f3f6SDavid E. O'Brien 	sop s;
19368fb3f3f6SDavid E. O'Brien 	sopno plusnest = 0;
19378fb3f3f6SDavid E. O'Brien 	sopno maxnest = 0;
193858f0484fSRodney W. Grimes 
193958f0484fSRodney W. Grimes 	if (p->error != 0)
194058f0484fSRodney W. Grimes 		return(0);	/* there may not be an OEND */
194158f0484fSRodney W. Grimes 
194258f0484fSRodney W. Grimes 	scan = g->strip + 1;
194358f0484fSRodney W. Grimes 	do {
194458f0484fSRodney W. Grimes 		s = *scan++;
194558f0484fSRodney W. Grimes 		switch (OP(s)) {
194658f0484fSRodney W. Grimes 		case OPLUS_:
194758f0484fSRodney W. Grimes 			plusnest++;
194858f0484fSRodney W. Grimes 			break;
194958f0484fSRodney W. Grimes 		case O_PLUS:
195058f0484fSRodney W. Grimes 			if (plusnest > maxnest)
195158f0484fSRodney W. Grimes 				maxnest = plusnest;
195258f0484fSRodney W. Grimes 			plusnest--;
195358f0484fSRodney W. Grimes 			break;
195458f0484fSRodney W. Grimes 		}
195558f0484fSRodney W. Grimes 	} while (OP(s) != OEND);
195658f0484fSRodney W. Grimes 	if (plusnest != 0)
195758f0484fSRodney W. Grimes 		g->iflags |= BAD;
195858f0484fSRodney W. Grimes 	return(maxnest);
195958f0484fSRodney W. Grimes }
1960