xref: /aosp_15_r20/external/llvm/lib/Support/regcomp.c (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker /*-
2*9880d681SAndroid Build Coastguard Worker  * This code is derived from OpenBSD's libc/regex, original license follows:
3*9880d681SAndroid Build Coastguard Worker  *
4*9880d681SAndroid Build Coastguard Worker  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5*9880d681SAndroid Build Coastguard Worker  * Copyright (c) 1992, 1993, 1994
6*9880d681SAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
7*9880d681SAndroid Build Coastguard Worker  *
8*9880d681SAndroid Build Coastguard Worker  * This code is derived from software contributed to Berkeley by
9*9880d681SAndroid Build Coastguard Worker  * Henry Spencer.
10*9880d681SAndroid Build Coastguard Worker  *
11*9880d681SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
12*9880d681SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
13*9880d681SAndroid Build Coastguard Worker  * are met:
14*9880d681SAndroid Build Coastguard Worker  * 1. Redistributions of source code must retain the above copyright
15*9880d681SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
16*9880d681SAndroid Build Coastguard Worker  * 2. Redistributions in binary form must reproduce the above copyright
17*9880d681SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in the
18*9880d681SAndroid Build Coastguard Worker  *    documentation and/or other materials provided with the distribution.
19*9880d681SAndroid Build Coastguard Worker  * 3. Neither the name of the University nor the names of its contributors
20*9880d681SAndroid Build Coastguard Worker  *    may be used to endorse or promote products derived from this software
21*9880d681SAndroid Build Coastguard Worker  *    without specific prior written permission.
22*9880d681SAndroid Build Coastguard Worker  *
23*9880d681SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*9880d681SAndroid Build Coastguard Worker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*9880d681SAndroid Build Coastguard Worker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*9880d681SAndroid Build Coastguard Worker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*9880d681SAndroid Build Coastguard Worker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*9880d681SAndroid Build Coastguard Worker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*9880d681SAndroid Build Coastguard Worker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*9880d681SAndroid Build Coastguard Worker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*9880d681SAndroid Build Coastguard Worker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*9880d681SAndroid Build Coastguard Worker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*9880d681SAndroid Build Coastguard Worker  * SUCH DAMAGE.
34*9880d681SAndroid Build Coastguard Worker  *
35*9880d681SAndroid Build Coastguard Worker  *	@(#)regcomp.c	8.5 (Berkeley) 3/20/94
36*9880d681SAndroid Build Coastguard Worker  */
37*9880d681SAndroid Build Coastguard Worker 
38*9880d681SAndroid Build Coastguard Worker #include <sys/types.h>
39*9880d681SAndroid Build Coastguard Worker #include <stdio.h>
40*9880d681SAndroid Build Coastguard Worker #include <string.h>
41*9880d681SAndroid Build Coastguard Worker #include <ctype.h>
42*9880d681SAndroid Build Coastguard Worker #include <limits.h>
43*9880d681SAndroid Build Coastguard Worker #include <stdlib.h>
44*9880d681SAndroid Build Coastguard Worker #include "regex_impl.h"
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker #include "regutils.h"
47*9880d681SAndroid Build Coastguard Worker #include "regex2.h"
48*9880d681SAndroid Build Coastguard Worker 
49*9880d681SAndroid Build Coastguard Worker #include "regcclass.h"
50*9880d681SAndroid Build Coastguard Worker #include "regcname.h"
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/config.h"
53*9880d681SAndroid Build Coastguard Worker #if HAVE_STDINT_H
54*9880d681SAndroid Build Coastguard Worker #include <stdint.h>
55*9880d681SAndroid Build Coastguard Worker #else
56*9880d681SAndroid Build Coastguard Worker /* Pessimistically bound memory use */
57*9880d681SAndroid Build Coastguard Worker #define SIZE_MAX UINT_MAX
58*9880d681SAndroid Build Coastguard Worker #endif
59*9880d681SAndroid Build Coastguard Worker 
60*9880d681SAndroid Build Coastguard Worker /*
61*9880d681SAndroid Build Coastguard Worker  * parse structure, passed up and down to avoid global variables and
62*9880d681SAndroid Build Coastguard Worker  * other clumsinesses
63*9880d681SAndroid Build Coastguard Worker  */
64*9880d681SAndroid Build Coastguard Worker struct parse {
65*9880d681SAndroid Build Coastguard Worker 	char *next;		/* next character in RE */
66*9880d681SAndroid Build Coastguard Worker 	char *end;		/* end of string (-> NUL normally) */
67*9880d681SAndroid Build Coastguard Worker 	int error;		/* has an error been seen? */
68*9880d681SAndroid Build Coastguard Worker 	sop *strip;		/* malloced strip */
69*9880d681SAndroid Build Coastguard Worker 	sopno ssize;		/* malloced strip size (allocated) */
70*9880d681SAndroid Build Coastguard Worker 	sopno slen;		/* malloced strip length (used) */
71*9880d681SAndroid Build Coastguard Worker 	int ncsalloc;		/* number of csets allocated */
72*9880d681SAndroid Build Coastguard Worker 	struct re_guts *g;
73*9880d681SAndroid Build Coastguard Worker #	define	NPAREN	10	/* we need to remember () 1-9 for back refs */
74*9880d681SAndroid Build Coastguard Worker 	sopno pbegin[NPAREN];	/* -> ( ([0] unused) */
75*9880d681SAndroid Build Coastguard Worker 	sopno pend[NPAREN];	/* -> ) ([0] unused) */
76*9880d681SAndroid Build Coastguard Worker };
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker static void p_ere(struct parse *, int);
79*9880d681SAndroid Build Coastguard Worker static void p_ere_exp(struct parse *);
80*9880d681SAndroid Build Coastguard Worker static void p_str(struct parse *);
81*9880d681SAndroid Build Coastguard Worker static void p_bre(struct parse *, int, int);
82*9880d681SAndroid Build Coastguard Worker static int p_simp_re(struct parse *, int);
83*9880d681SAndroid Build Coastguard Worker static int p_count(struct parse *);
84*9880d681SAndroid Build Coastguard Worker static void p_bracket(struct parse *);
85*9880d681SAndroid Build Coastguard Worker static void p_b_term(struct parse *, cset *);
86*9880d681SAndroid Build Coastguard Worker static void p_b_cclass(struct parse *, cset *);
87*9880d681SAndroid Build Coastguard Worker static void p_b_eclass(struct parse *, cset *);
88*9880d681SAndroid Build Coastguard Worker static char p_b_symbol(struct parse *);
89*9880d681SAndroid Build Coastguard Worker static char p_b_coll_elem(struct parse *, int);
90*9880d681SAndroid Build Coastguard Worker static char othercase(int);
91*9880d681SAndroid Build Coastguard Worker static void bothcases(struct parse *, int);
92*9880d681SAndroid Build Coastguard Worker static void ordinary(struct parse *, int);
93*9880d681SAndroid Build Coastguard Worker static void nonnewline(struct parse *);
94*9880d681SAndroid Build Coastguard Worker static void repeat(struct parse *, sopno, int, int);
95*9880d681SAndroid Build Coastguard Worker static int seterr(struct parse *, int);
96*9880d681SAndroid Build Coastguard Worker static cset *allocset(struct parse *);
97*9880d681SAndroid Build Coastguard Worker static void freeset(struct parse *, cset *);
98*9880d681SAndroid Build Coastguard Worker static int freezeset(struct parse *, cset *);
99*9880d681SAndroid Build Coastguard Worker static int firstch(struct parse *, cset *);
100*9880d681SAndroid Build Coastguard Worker static int nch(struct parse *, cset *);
101*9880d681SAndroid Build Coastguard Worker static void mcadd(struct parse *, cset *, const char *);
102*9880d681SAndroid Build Coastguard Worker static void mcinvert(struct parse *, cset *);
103*9880d681SAndroid Build Coastguard Worker static void mccase(struct parse *, cset *);
104*9880d681SAndroid Build Coastguard Worker static int isinsets(struct re_guts *, int);
105*9880d681SAndroid Build Coastguard Worker static int samesets(struct re_guts *, int, int);
106*9880d681SAndroid Build Coastguard Worker static void categorize(struct parse *, struct re_guts *);
107*9880d681SAndroid Build Coastguard Worker static sopno dupl(struct parse *, sopno, sopno);
108*9880d681SAndroid Build Coastguard Worker static void doemit(struct parse *, sop, size_t);
109*9880d681SAndroid Build Coastguard Worker static void doinsert(struct parse *, sop, size_t, sopno);
110*9880d681SAndroid Build Coastguard Worker static void dofwd(struct parse *, sopno, sop);
111*9880d681SAndroid Build Coastguard Worker static void enlarge(struct parse *, sopno);
112*9880d681SAndroid Build Coastguard Worker static void stripsnug(struct parse *, struct re_guts *);
113*9880d681SAndroid Build Coastguard Worker static void findmust(struct parse *, struct re_guts *);
114*9880d681SAndroid Build Coastguard Worker static sopno pluscount(struct parse *, struct re_guts *);
115*9880d681SAndroid Build Coastguard Worker 
116*9880d681SAndroid Build Coastguard Worker static char nuls[10];		/* place to point scanner in event of error */
117*9880d681SAndroid Build Coastguard Worker 
118*9880d681SAndroid Build Coastguard Worker /*
119*9880d681SAndroid Build Coastguard Worker  * macros for use with parse structure
120*9880d681SAndroid Build Coastguard Worker  * BEWARE:  these know that the parse structure is named `p' !!!
121*9880d681SAndroid Build Coastguard Worker  */
122*9880d681SAndroid Build Coastguard Worker #define	PEEK()	(*p->next)
123*9880d681SAndroid Build Coastguard Worker #define	PEEK2()	(*(p->next+1))
124*9880d681SAndroid Build Coastguard Worker #define	MORE()	(p->next < p->end)
125*9880d681SAndroid Build Coastguard Worker #define	MORE2()	(p->next+1 < p->end)
126*9880d681SAndroid Build Coastguard Worker #define	SEE(c)	(MORE() && PEEK() == (c))
127*9880d681SAndroid Build Coastguard Worker #define	SEETWO(a, b)	(MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
128*9880d681SAndroid Build Coastguard Worker #define	EAT(c)	((SEE(c)) ? (NEXT(), 1) : 0)
129*9880d681SAndroid Build Coastguard Worker #define	EATTWO(a, b)	((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
130*9880d681SAndroid Build Coastguard Worker #define	NEXT()	(p->next++)
131*9880d681SAndroid Build Coastguard Worker #define	NEXT2()	(p->next += 2)
132*9880d681SAndroid Build Coastguard Worker #define	NEXTn(n)	(p->next += (n))
133*9880d681SAndroid Build Coastguard Worker #define	GETNEXT()	(*p->next++)
134*9880d681SAndroid Build Coastguard Worker #define	SETERROR(e)	seterr(p, (e))
135*9880d681SAndroid Build Coastguard Worker #define	REQUIRE(co, e)	(void)((co) || SETERROR(e))
136*9880d681SAndroid Build Coastguard Worker #define	MUSTSEE(c, e)	(REQUIRE(MORE() && PEEK() == (c), e))
137*9880d681SAndroid Build Coastguard Worker #define	MUSTEAT(c, e)	(REQUIRE(MORE() && GETNEXT() == (c), e))
138*9880d681SAndroid Build Coastguard Worker #define	MUSTNOTSEE(c, e)	(REQUIRE(!MORE() || PEEK() != (c), e))
139*9880d681SAndroid Build Coastguard Worker #define	EMIT(op, sopnd)	doemit(p, (sop)(op), (size_t)(sopnd))
140*9880d681SAndroid Build Coastguard Worker #define	INSERT(op, pos)	doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
141*9880d681SAndroid Build Coastguard Worker #define	AHEAD(pos)		dofwd(p, pos, HERE()-(pos))
142*9880d681SAndroid Build Coastguard Worker #define	ASTERN(sop, pos)	EMIT(sop, HERE()-pos)
143*9880d681SAndroid Build Coastguard Worker #define	HERE()		(p->slen)
144*9880d681SAndroid Build Coastguard Worker #define	THERE()		(p->slen - 1)
145*9880d681SAndroid Build Coastguard Worker #define	THERETHERE()	(p->slen - 2)
146*9880d681SAndroid Build Coastguard Worker #define	DROP(n)	(p->slen -= (n))
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker #ifdef	_POSIX2_RE_DUP_MAX
149*9880d681SAndroid Build Coastguard Worker #define	DUPMAX	_POSIX2_RE_DUP_MAX
150*9880d681SAndroid Build Coastguard Worker #else
151*9880d681SAndroid Build Coastguard Worker #define	DUPMAX	255
152*9880d681SAndroid Build Coastguard Worker #endif
153*9880d681SAndroid Build Coastguard Worker #define	INFINITY	(DUPMAX + 1)
154*9880d681SAndroid Build Coastguard Worker 
155*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
156*9880d681SAndroid Build Coastguard Worker static int never = 0;		/* for use in asserts; shuts lint up */
157*9880d681SAndroid Build Coastguard Worker #else
158*9880d681SAndroid Build Coastguard Worker #define	never	0		/* some <assert.h>s have bugs too */
159*9880d681SAndroid Build Coastguard Worker #endif
160*9880d681SAndroid Build Coastguard Worker 
161*9880d681SAndroid Build Coastguard Worker /*
162*9880d681SAndroid Build Coastguard Worker  - llvm_regcomp - interface for parser and compilation
163*9880d681SAndroid Build Coastguard Worker  */
164*9880d681SAndroid Build Coastguard Worker int				/* 0 success, otherwise REG_something */
llvm_regcomp(llvm_regex_t * preg,const char * pattern,int cflags)165*9880d681SAndroid Build Coastguard Worker llvm_regcomp(llvm_regex_t *preg, const char *pattern, int cflags)
166*9880d681SAndroid Build Coastguard Worker {
167*9880d681SAndroid Build Coastguard Worker 	struct parse pa;
168*9880d681SAndroid Build Coastguard Worker 	struct re_guts *g;
169*9880d681SAndroid Build Coastguard Worker 	struct parse *p = &pa;
170*9880d681SAndroid Build Coastguard Worker 	int i;
171*9880d681SAndroid Build Coastguard Worker 	size_t len;
172*9880d681SAndroid Build Coastguard Worker #ifdef REDEBUG
173*9880d681SAndroid Build Coastguard Worker #	define	GOODFLAGS(f)	(f)
174*9880d681SAndroid Build Coastguard Worker #else
175*9880d681SAndroid Build Coastguard Worker #	define	GOODFLAGS(f)	((f)&~REG_DUMP)
176*9880d681SAndroid Build Coastguard Worker #endif
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker 	cflags = GOODFLAGS(cflags);
179*9880d681SAndroid Build Coastguard Worker 	if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
180*9880d681SAndroid Build Coastguard Worker 		return(REG_INVARG);
181*9880d681SAndroid Build Coastguard Worker 
182*9880d681SAndroid Build Coastguard Worker 	if (cflags&REG_PEND) {
183*9880d681SAndroid Build Coastguard Worker 		if (preg->re_endp < pattern)
184*9880d681SAndroid Build Coastguard Worker 			return(REG_INVARG);
185*9880d681SAndroid Build Coastguard Worker 		len = preg->re_endp - pattern;
186*9880d681SAndroid Build Coastguard Worker 	} else
187*9880d681SAndroid Build Coastguard Worker 		len = strlen((const char *)pattern);
188*9880d681SAndroid Build Coastguard Worker 
189*9880d681SAndroid Build Coastguard Worker 	/* do the mallocs early so failure handling is easy */
190*9880d681SAndroid Build Coastguard Worker 	g = (struct re_guts *)malloc(sizeof(struct re_guts) +
191*9880d681SAndroid Build Coastguard Worker 							(NC-1)*sizeof(cat_t));
192*9880d681SAndroid Build Coastguard Worker 	if (g == NULL)
193*9880d681SAndroid Build Coastguard Worker 		return(REG_ESPACE);
194*9880d681SAndroid Build Coastguard Worker 	p->ssize = len/(size_t)2*(size_t)3 + (size_t)1;	/* ugh */
195*9880d681SAndroid Build Coastguard Worker 	p->strip = (sop *)calloc(p->ssize, sizeof(sop));
196*9880d681SAndroid Build Coastguard Worker 	p->slen = 0;
197*9880d681SAndroid Build Coastguard Worker 	if (p->strip == NULL) {
198*9880d681SAndroid Build Coastguard Worker 		free((char *)g);
199*9880d681SAndroid Build Coastguard Worker 		return(REG_ESPACE);
200*9880d681SAndroid Build Coastguard Worker 	}
201*9880d681SAndroid Build Coastguard Worker 
202*9880d681SAndroid Build Coastguard Worker 	/* set things up */
203*9880d681SAndroid Build Coastguard Worker 	p->g = g;
204*9880d681SAndroid Build Coastguard Worker 	p->next = (char *)pattern;	/* convenience; we do not modify it */
205*9880d681SAndroid Build Coastguard Worker 	p->end = p->next + len;
206*9880d681SAndroid Build Coastguard Worker 	p->error = 0;
207*9880d681SAndroid Build Coastguard Worker 	p->ncsalloc = 0;
208*9880d681SAndroid Build Coastguard Worker 	for (i = 0; i < NPAREN; i++) {
209*9880d681SAndroid Build Coastguard Worker 		p->pbegin[i] = 0;
210*9880d681SAndroid Build Coastguard Worker 		p->pend[i] = 0;
211*9880d681SAndroid Build Coastguard Worker 	}
212*9880d681SAndroid Build Coastguard Worker 	g->csetsize = NC;
213*9880d681SAndroid Build Coastguard Worker 	g->sets = NULL;
214*9880d681SAndroid Build Coastguard Worker 	g->setbits = NULL;
215*9880d681SAndroid Build Coastguard Worker 	g->ncsets = 0;
216*9880d681SAndroid Build Coastguard Worker 	g->cflags = cflags;
217*9880d681SAndroid Build Coastguard Worker 	g->iflags = 0;
218*9880d681SAndroid Build Coastguard Worker 	g->nbol = 0;
219*9880d681SAndroid Build Coastguard Worker 	g->neol = 0;
220*9880d681SAndroid Build Coastguard Worker 	g->must = NULL;
221*9880d681SAndroid Build Coastguard Worker 	g->mlen = 0;
222*9880d681SAndroid Build Coastguard Worker 	g->nsub = 0;
223*9880d681SAndroid Build Coastguard Worker 	g->ncategories = 1;	/* category 0 is "everything else" */
224*9880d681SAndroid Build Coastguard Worker 	g->categories = &g->catspace[-(CHAR_MIN)];
225*9880d681SAndroid Build Coastguard Worker 	(void) memset((char *)g->catspace, 0, NC*sizeof(cat_t));
226*9880d681SAndroid Build Coastguard Worker 	g->backrefs = 0;
227*9880d681SAndroid Build Coastguard Worker 
228*9880d681SAndroid Build Coastguard Worker 	/* do it */
229*9880d681SAndroid Build Coastguard Worker 	EMIT(OEND, 0);
230*9880d681SAndroid Build Coastguard Worker 	g->firststate = THERE();
231*9880d681SAndroid Build Coastguard Worker 	if (cflags&REG_EXTENDED)
232*9880d681SAndroid Build Coastguard Worker 		p_ere(p, OUT);
233*9880d681SAndroid Build Coastguard Worker 	else if (cflags&REG_NOSPEC)
234*9880d681SAndroid Build Coastguard Worker 		p_str(p);
235*9880d681SAndroid Build Coastguard Worker 	else
236*9880d681SAndroid Build Coastguard Worker 		p_bre(p, OUT, OUT);
237*9880d681SAndroid Build Coastguard Worker 	EMIT(OEND, 0);
238*9880d681SAndroid Build Coastguard Worker 	g->laststate = THERE();
239*9880d681SAndroid Build Coastguard Worker 
240*9880d681SAndroid Build Coastguard Worker 	/* tidy up loose ends and fill things in */
241*9880d681SAndroid Build Coastguard Worker 	categorize(p, g);
242*9880d681SAndroid Build Coastguard Worker 	stripsnug(p, g);
243*9880d681SAndroid Build Coastguard Worker 	findmust(p, g);
244*9880d681SAndroid Build Coastguard Worker 	g->nplus = pluscount(p, g);
245*9880d681SAndroid Build Coastguard Worker 	g->magic = MAGIC2;
246*9880d681SAndroid Build Coastguard Worker 	preg->re_nsub = g->nsub;
247*9880d681SAndroid Build Coastguard Worker 	preg->re_g = g;
248*9880d681SAndroid Build Coastguard Worker 	preg->re_magic = MAGIC1;
249*9880d681SAndroid Build Coastguard Worker #ifndef REDEBUG
250*9880d681SAndroid Build Coastguard Worker 	/* not debugging, so can't rely on the assert() in llvm_regexec() */
251*9880d681SAndroid Build Coastguard Worker 	if (g->iflags&REGEX_BAD)
252*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ASSERT);
253*9880d681SAndroid Build Coastguard Worker #endif
254*9880d681SAndroid Build Coastguard Worker 
255*9880d681SAndroid Build Coastguard Worker 	/* win or lose, we're done */
256*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)	/* lose */
257*9880d681SAndroid Build Coastguard Worker 		llvm_regfree(preg);
258*9880d681SAndroid Build Coastguard Worker 	return(p->error);
259*9880d681SAndroid Build Coastguard Worker }
260*9880d681SAndroid Build Coastguard Worker 
261*9880d681SAndroid Build Coastguard Worker /*
262*9880d681SAndroid Build Coastguard Worker  - p_ere - ERE parser top level, concatenation and alternation
263*9880d681SAndroid Build Coastguard Worker  */
264*9880d681SAndroid Build Coastguard Worker static void
p_ere(struct parse * p,int stop)265*9880d681SAndroid Build Coastguard Worker p_ere(struct parse *p, int stop)	/* character this ERE should end at */
266*9880d681SAndroid Build Coastguard Worker {
267*9880d681SAndroid Build Coastguard Worker 	char c;
268*9880d681SAndroid Build Coastguard Worker 	sopno prevback = 0;
269*9880d681SAndroid Build Coastguard Worker 	sopno prevfwd = 0;
270*9880d681SAndroid Build Coastguard Worker 	sopno conc;
271*9880d681SAndroid Build Coastguard Worker 	int first = 1;		/* is this the first alternative? */
272*9880d681SAndroid Build Coastguard Worker 
273*9880d681SAndroid Build Coastguard Worker 	for (;;) {
274*9880d681SAndroid Build Coastguard Worker 		/* do a bunch of concatenated expressions */
275*9880d681SAndroid Build Coastguard Worker 		conc = HERE();
276*9880d681SAndroid Build Coastguard Worker 		while (MORE() && (c = PEEK()) != '|' && c != stop)
277*9880d681SAndroid Build Coastguard Worker 			p_ere_exp(p);
278*9880d681SAndroid Build Coastguard Worker 		REQUIRE(HERE() != conc, REG_EMPTY);	/* require nonempty */
279*9880d681SAndroid Build Coastguard Worker 
280*9880d681SAndroid Build Coastguard Worker 		if (!EAT('|'))
281*9880d681SAndroid Build Coastguard Worker 			break;		/* NOTE BREAK OUT */
282*9880d681SAndroid Build Coastguard Worker 
283*9880d681SAndroid Build Coastguard Worker 		if (first) {
284*9880d681SAndroid Build Coastguard Worker 			INSERT(OCH_, conc);	/* offset is wrong */
285*9880d681SAndroid Build Coastguard Worker 			prevfwd = conc;
286*9880d681SAndroid Build Coastguard Worker 			prevback = conc;
287*9880d681SAndroid Build Coastguard Worker 			first = 0;
288*9880d681SAndroid Build Coastguard Worker 		}
289*9880d681SAndroid Build Coastguard Worker 		ASTERN(OOR1, prevback);
290*9880d681SAndroid Build Coastguard Worker 		prevback = THERE();
291*9880d681SAndroid Build Coastguard Worker 		AHEAD(prevfwd);			/* fix previous offset */
292*9880d681SAndroid Build Coastguard Worker 		prevfwd = HERE();
293*9880d681SAndroid Build Coastguard Worker 		EMIT(OOR2, 0);			/* offset is very wrong */
294*9880d681SAndroid Build Coastguard Worker 	}
295*9880d681SAndroid Build Coastguard Worker 
296*9880d681SAndroid Build Coastguard Worker 	if (!first) {		/* tail-end fixups */
297*9880d681SAndroid Build Coastguard Worker 		AHEAD(prevfwd);
298*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_CH, prevback);
299*9880d681SAndroid Build Coastguard Worker 	}
300*9880d681SAndroid Build Coastguard Worker 
301*9880d681SAndroid Build Coastguard Worker 	assert(!MORE() || SEE(stop));
302*9880d681SAndroid Build Coastguard Worker }
303*9880d681SAndroid Build Coastguard Worker 
304*9880d681SAndroid Build Coastguard Worker /*
305*9880d681SAndroid Build Coastguard Worker  - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
306*9880d681SAndroid Build Coastguard Worker  */
307*9880d681SAndroid Build Coastguard Worker static void
p_ere_exp(struct parse * p)308*9880d681SAndroid Build Coastguard Worker p_ere_exp(struct parse *p)
309*9880d681SAndroid Build Coastguard Worker {
310*9880d681SAndroid Build Coastguard Worker 	char c;
311*9880d681SAndroid Build Coastguard Worker 	sopno pos;
312*9880d681SAndroid Build Coastguard Worker 	int count;
313*9880d681SAndroid Build Coastguard Worker 	int count2;
314*9880d681SAndroid Build Coastguard Worker 	int backrefnum;
315*9880d681SAndroid Build Coastguard Worker 	sopno subno;
316*9880d681SAndroid Build Coastguard Worker 	int wascaret = 0;
317*9880d681SAndroid Build Coastguard Worker 
318*9880d681SAndroid Build Coastguard Worker 	assert(MORE());		/* caller should have ensured this */
319*9880d681SAndroid Build Coastguard Worker 	c = GETNEXT();
320*9880d681SAndroid Build Coastguard Worker 
321*9880d681SAndroid Build Coastguard Worker 	pos = HERE();
322*9880d681SAndroid Build Coastguard Worker 	switch (c) {
323*9880d681SAndroid Build Coastguard Worker 	case '(':
324*9880d681SAndroid Build Coastguard Worker 		REQUIRE(MORE(), REG_EPAREN);
325*9880d681SAndroid Build Coastguard Worker 		p->g->nsub++;
326*9880d681SAndroid Build Coastguard Worker 		subno = p->g->nsub;
327*9880d681SAndroid Build Coastguard Worker 		if (subno < NPAREN)
328*9880d681SAndroid Build Coastguard Worker 			p->pbegin[subno] = HERE();
329*9880d681SAndroid Build Coastguard Worker 		EMIT(OLPAREN, subno);
330*9880d681SAndroid Build Coastguard Worker 		if (!SEE(')'))
331*9880d681SAndroid Build Coastguard Worker 			p_ere(p, ')');
332*9880d681SAndroid Build Coastguard Worker 		if (subno < NPAREN) {
333*9880d681SAndroid Build Coastguard Worker 			p->pend[subno] = HERE();
334*9880d681SAndroid Build Coastguard Worker 			assert(p->pend[subno] != 0);
335*9880d681SAndroid Build Coastguard Worker 		}
336*9880d681SAndroid Build Coastguard Worker 		EMIT(ORPAREN, subno);
337*9880d681SAndroid Build Coastguard Worker 		MUSTEAT(')', REG_EPAREN);
338*9880d681SAndroid Build Coastguard Worker 		break;
339*9880d681SAndroid Build Coastguard Worker #ifndef POSIX_MISTAKE
340*9880d681SAndroid Build Coastguard Worker 	case ')':		/* happens only if no current unmatched ( */
341*9880d681SAndroid Build Coastguard Worker 		/*
342*9880d681SAndroid Build Coastguard Worker 		 * You may ask, why the ifndef?  Because I didn't notice
343*9880d681SAndroid Build Coastguard Worker 		 * this until slightly too late for 1003.2, and none of the
344*9880d681SAndroid Build Coastguard Worker 		 * other 1003.2 regular-expression reviewers noticed it at
345*9880d681SAndroid Build Coastguard Worker 		 * all.  So an unmatched ) is legal POSIX, at least until
346*9880d681SAndroid Build Coastguard Worker 		 * we can get it fixed.
347*9880d681SAndroid Build Coastguard Worker 		 */
348*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_EPAREN);
349*9880d681SAndroid Build Coastguard Worker 		break;
350*9880d681SAndroid Build Coastguard Worker #endif
351*9880d681SAndroid Build Coastguard Worker 	case '^':
352*9880d681SAndroid Build Coastguard Worker 		EMIT(OBOL, 0);
353*9880d681SAndroid Build Coastguard Worker 		p->g->iflags |= USEBOL;
354*9880d681SAndroid Build Coastguard Worker 		p->g->nbol++;
355*9880d681SAndroid Build Coastguard Worker 		wascaret = 1;
356*9880d681SAndroid Build Coastguard Worker 		break;
357*9880d681SAndroid Build Coastguard Worker 	case '$':
358*9880d681SAndroid Build Coastguard Worker 		EMIT(OEOL, 0);
359*9880d681SAndroid Build Coastguard Worker 		p->g->iflags |= USEEOL;
360*9880d681SAndroid Build Coastguard Worker 		p->g->neol++;
361*9880d681SAndroid Build Coastguard Worker 		break;
362*9880d681SAndroid Build Coastguard Worker 	case '|':
363*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_EMPTY);
364*9880d681SAndroid Build Coastguard Worker 		break;
365*9880d681SAndroid Build Coastguard Worker 	case '*':
366*9880d681SAndroid Build Coastguard Worker 	case '+':
367*9880d681SAndroid Build Coastguard Worker 	case '?':
368*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_BADRPT);
369*9880d681SAndroid Build Coastguard Worker 		break;
370*9880d681SAndroid Build Coastguard Worker 	case '.':
371*9880d681SAndroid Build Coastguard Worker 		if (p->g->cflags&REG_NEWLINE)
372*9880d681SAndroid Build Coastguard Worker 			nonnewline(p);
373*9880d681SAndroid Build Coastguard Worker 		else
374*9880d681SAndroid Build Coastguard Worker 			EMIT(OANY, 0);
375*9880d681SAndroid Build Coastguard Worker 		break;
376*9880d681SAndroid Build Coastguard Worker 	case '[':
377*9880d681SAndroid Build Coastguard Worker 		p_bracket(p);
378*9880d681SAndroid Build Coastguard Worker 		break;
379*9880d681SAndroid Build Coastguard Worker 	case '\\':
380*9880d681SAndroid Build Coastguard Worker 		REQUIRE(MORE(), REG_EESCAPE);
381*9880d681SAndroid Build Coastguard Worker 		c = GETNEXT();
382*9880d681SAndroid Build Coastguard Worker 		if (c >= '1' && c <= '9') {
383*9880d681SAndroid Build Coastguard Worker 			/* \[0-9] is taken to be a back-reference to a previously specified
384*9880d681SAndroid Build Coastguard Worker 			 * matching group. backrefnum will hold the number. The matching
385*9880d681SAndroid Build Coastguard Worker 			 * group must exist (i.e. if \4 is found there must have been at
386*9880d681SAndroid Build Coastguard Worker 			 * least 4 matching groups specified in the pattern previously).
387*9880d681SAndroid Build Coastguard Worker 			 */
388*9880d681SAndroid Build Coastguard Worker 			backrefnum = c - '0';
389*9880d681SAndroid Build Coastguard Worker 			if (p->pend[backrefnum] == 0) {
390*9880d681SAndroid Build Coastguard Worker 				SETERROR(REG_ESUBREG);
391*9880d681SAndroid Build Coastguard Worker 				break;
392*9880d681SAndroid Build Coastguard Worker 			}
393*9880d681SAndroid Build Coastguard Worker 
394*9880d681SAndroid Build Coastguard Worker 			/* Make sure everything checks out and emit the sequence
395*9880d681SAndroid Build Coastguard Worker 			 * that marks a back-reference to the parse structure.
396*9880d681SAndroid Build Coastguard Worker 			 */
397*9880d681SAndroid Build Coastguard Worker 			assert(backrefnum <= p->g->nsub);
398*9880d681SAndroid Build Coastguard Worker 			EMIT(OBACK_, backrefnum);
399*9880d681SAndroid Build Coastguard Worker 			assert(p->pbegin[backrefnum] != 0);
400*9880d681SAndroid Build Coastguard Worker 			assert(OP(p->strip[p->pbegin[backrefnum]]) != OLPAREN);
401*9880d681SAndroid Build Coastguard Worker 			assert(OP(p->strip[p->pend[backrefnum]]) != ORPAREN);
402*9880d681SAndroid Build Coastguard Worker 			(void) dupl(p, p->pbegin[backrefnum]+1, p->pend[backrefnum]);
403*9880d681SAndroid Build Coastguard Worker 			EMIT(O_BACK, backrefnum);
404*9880d681SAndroid Build Coastguard Worker 			p->g->backrefs = 1;
405*9880d681SAndroid Build Coastguard Worker 		} else {
406*9880d681SAndroid Build Coastguard Worker 			/* Other chars are simply themselves when escaped with a backslash.
407*9880d681SAndroid Build Coastguard Worker 			 */
408*9880d681SAndroid Build Coastguard Worker 			ordinary(p, c);
409*9880d681SAndroid Build Coastguard Worker 		}
410*9880d681SAndroid Build Coastguard Worker 		break;
411*9880d681SAndroid Build Coastguard Worker 	case '{':		/* okay as ordinary except if digit follows */
412*9880d681SAndroid Build Coastguard Worker 		REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
413*9880d681SAndroid Build Coastguard Worker 		/* FALLTHROUGH */
414*9880d681SAndroid Build Coastguard Worker 	default:
415*9880d681SAndroid Build Coastguard Worker 		ordinary(p, c);
416*9880d681SAndroid Build Coastguard Worker 		break;
417*9880d681SAndroid Build Coastguard Worker 	}
418*9880d681SAndroid Build Coastguard Worker 
419*9880d681SAndroid Build Coastguard Worker 	if (!MORE())
420*9880d681SAndroid Build Coastguard Worker 		return;
421*9880d681SAndroid Build Coastguard Worker 	c = PEEK();
422*9880d681SAndroid Build Coastguard Worker 	/* we call { a repetition if followed by a digit */
423*9880d681SAndroid Build Coastguard Worker 	if (!( c == '*' || c == '+' || c == '?' ||
424*9880d681SAndroid Build Coastguard Worker 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
425*9880d681SAndroid Build Coastguard Worker 		return;		/* no repetition, we're done */
426*9880d681SAndroid Build Coastguard Worker 	NEXT();
427*9880d681SAndroid Build Coastguard Worker 
428*9880d681SAndroid Build Coastguard Worker 	REQUIRE(!wascaret, REG_BADRPT);
429*9880d681SAndroid Build Coastguard Worker 	switch (c) {
430*9880d681SAndroid Build Coastguard Worker 	case '*':	/* implemented as +? */
431*9880d681SAndroid Build Coastguard Worker 		/* this case does not require the (y|) trick, noKLUDGE */
432*9880d681SAndroid Build Coastguard Worker 		INSERT(OPLUS_, pos);
433*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_PLUS, pos);
434*9880d681SAndroid Build Coastguard Worker 		INSERT(OQUEST_, pos);
435*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_QUEST, pos);
436*9880d681SAndroid Build Coastguard Worker 		break;
437*9880d681SAndroid Build Coastguard Worker 	case '+':
438*9880d681SAndroid Build Coastguard Worker 		INSERT(OPLUS_, pos);
439*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_PLUS, pos);
440*9880d681SAndroid Build Coastguard Worker 		break;
441*9880d681SAndroid Build Coastguard Worker 	case '?':
442*9880d681SAndroid Build Coastguard Worker 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
443*9880d681SAndroid Build Coastguard Worker 		INSERT(OCH_, pos);		/* offset slightly wrong */
444*9880d681SAndroid Build Coastguard Worker 		ASTERN(OOR1, pos);		/* this one's right */
445*9880d681SAndroid Build Coastguard Worker 		AHEAD(pos);			/* fix the OCH_ */
446*9880d681SAndroid Build Coastguard Worker 		EMIT(OOR2, 0);			/* offset very wrong... */
447*9880d681SAndroid Build Coastguard Worker 		AHEAD(THERE());			/* ...so fix it */
448*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_CH, THERETHERE());
449*9880d681SAndroid Build Coastguard Worker 		break;
450*9880d681SAndroid Build Coastguard Worker 	case '{':
451*9880d681SAndroid Build Coastguard Worker 		count = p_count(p);
452*9880d681SAndroid Build Coastguard Worker 		if (EAT(',')) {
453*9880d681SAndroid Build Coastguard Worker 			if (isdigit((uch)PEEK())) {
454*9880d681SAndroid Build Coastguard Worker 				count2 = p_count(p);
455*9880d681SAndroid Build Coastguard Worker 				REQUIRE(count <= count2, REG_BADBR);
456*9880d681SAndroid Build Coastguard Worker 			} else		/* single number with comma */
457*9880d681SAndroid Build Coastguard Worker 				count2 = INFINITY;
458*9880d681SAndroid Build Coastguard Worker 		} else		/* just a single number */
459*9880d681SAndroid Build Coastguard Worker 			count2 = count;
460*9880d681SAndroid Build Coastguard Worker 		repeat(p, pos, count, count2);
461*9880d681SAndroid Build Coastguard Worker 		if (!EAT('}')) {	/* error heuristics */
462*9880d681SAndroid Build Coastguard Worker 			while (MORE() && PEEK() != '}')
463*9880d681SAndroid Build Coastguard Worker 				NEXT();
464*9880d681SAndroid Build Coastguard Worker 			REQUIRE(MORE(), REG_EBRACE);
465*9880d681SAndroid Build Coastguard Worker 			SETERROR(REG_BADBR);
466*9880d681SAndroid Build Coastguard Worker 		}
467*9880d681SAndroid Build Coastguard Worker 		break;
468*9880d681SAndroid Build Coastguard Worker 	}
469*9880d681SAndroid Build Coastguard Worker 
470*9880d681SAndroid Build Coastguard Worker 	if (!MORE())
471*9880d681SAndroid Build Coastguard Worker 		return;
472*9880d681SAndroid Build Coastguard Worker 	c = PEEK();
473*9880d681SAndroid Build Coastguard Worker 	if (!( c == '*' || c == '+' || c == '?' ||
474*9880d681SAndroid Build Coastguard Worker 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
475*9880d681SAndroid Build Coastguard Worker 		return;
476*9880d681SAndroid Build Coastguard Worker 	SETERROR(REG_BADRPT);
477*9880d681SAndroid Build Coastguard Worker }
478*9880d681SAndroid Build Coastguard Worker 
479*9880d681SAndroid Build Coastguard Worker /*
480*9880d681SAndroid Build Coastguard Worker  - p_str - string (no metacharacters) "parser"
481*9880d681SAndroid Build Coastguard Worker  */
482*9880d681SAndroid Build Coastguard Worker static void
p_str(struct parse * p)483*9880d681SAndroid Build Coastguard Worker p_str(struct parse *p)
484*9880d681SAndroid Build Coastguard Worker {
485*9880d681SAndroid Build Coastguard Worker 	REQUIRE(MORE(), REG_EMPTY);
486*9880d681SAndroid Build Coastguard Worker 	while (MORE())
487*9880d681SAndroid Build Coastguard Worker 		ordinary(p, GETNEXT());
488*9880d681SAndroid Build Coastguard Worker }
489*9880d681SAndroid Build Coastguard Worker 
490*9880d681SAndroid Build Coastguard Worker /*
491*9880d681SAndroid Build Coastguard Worker  - p_bre - BRE parser top level, anchoring and concatenation
492*9880d681SAndroid Build Coastguard Worker  * Giving end1 as OUT essentially eliminates the end1/end2 check.
493*9880d681SAndroid Build Coastguard Worker  *
494*9880d681SAndroid Build Coastguard Worker  * This implementation is a bit of a kludge, in that a trailing $ is first
495*9880d681SAndroid Build Coastguard Worker  * taken as an ordinary character and then revised to be an anchor.  The
496*9880d681SAndroid Build Coastguard Worker  * only undesirable side effect is that '$' gets included as a character
497*9880d681SAndroid Build Coastguard Worker  * category in such cases.  This is fairly harmless; not worth fixing.
498*9880d681SAndroid Build Coastguard Worker  * The amount of lookahead needed to avoid this kludge is excessive.
499*9880d681SAndroid Build Coastguard Worker  */
500*9880d681SAndroid Build Coastguard Worker static void
p_bre(struct parse * p,int end1,int end2)501*9880d681SAndroid Build Coastguard Worker p_bre(struct parse *p,
502*9880d681SAndroid Build Coastguard Worker     int end1,		/* first terminating character */
503*9880d681SAndroid Build Coastguard Worker     int end2)		/* second terminating character */
504*9880d681SAndroid Build Coastguard Worker {
505*9880d681SAndroid Build Coastguard Worker 	sopno start = HERE();
506*9880d681SAndroid Build Coastguard Worker 	int first = 1;			/* first subexpression? */
507*9880d681SAndroid Build Coastguard Worker 	int wasdollar = 0;
508*9880d681SAndroid Build Coastguard Worker 
509*9880d681SAndroid Build Coastguard Worker 	if (EAT('^')) {
510*9880d681SAndroid Build Coastguard Worker 		EMIT(OBOL, 0);
511*9880d681SAndroid Build Coastguard Worker 		p->g->iflags |= USEBOL;
512*9880d681SAndroid Build Coastguard Worker 		p->g->nbol++;
513*9880d681SAndroid Build Coastguard Worker 	}
514*9880d681SAndroid Build Coastguard Worker 	while (MORE() && !SEETWO(end1, end2)) {
515*9880d681SAndroid Build Coastguard Worker 		wasdollar = p_simp_re(p, first);
516*9880d681SAndroid Build Coastguard Worker 		first = 0;
517*9880d681SAndroid Build Coastguard Worker 	}
518*9880d681SAndroid Build Coastguard Worker 	if (wasdollar) {	/* oops, that was a trailing anchor */
519*9880d681SAndroid Build Coastguard Worker 		DROP(1);
520*9880d681SAndroid Build Coastguard Worker 		EMIT(OEOL, 0);
521*9880d681SAndroid Build Coastguard Worker 		p->g->iflags |= USEEOL;
522*9880d681SAndroid Build Coastguard Worker 		p->g->neol++;
523*9880d681SAndroid Build Coastguard Worker 	}
524*9880d681SAndroid Build Coastguard Worker 
525*9880d681SAndroid Build Coastguard Worker 	REQUIRE(HERE() != start, REG_EMPTY);	/* require nonempty */
526*9880d681SAndroid Build Coastguard Worker }
527*9880d681SAndroid Build Coastguard Worker 
528*9880d681SAndroid Build Coastguard Worker /*
529*9880d681SAndroid Build Coastguard Worker  - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
530*9880d681SAndroid Build Coastguard Worker  */
531*9880d681SAndroid Build Coastguard Worker static int			/* was the simple RE an unbackslashed $? */
p_simp_re(struct parse * p,int starordinary)532*9880d681SAndroid Build Coastguard Worker p_simp_re(struct parse *p,
533*9880d681SAndroid Build Coastguard Worker     int starordinary)		/* is a leading * an ordinary character? */
534*9880d681SAndroid Build Coastguard Worker {
535*9880d681SAndroid Build Coastguard Worker 	int c;
536*9880d681SAndroid Build Coastguard Worker 	int count;
537*9880d681SAndroid Build Coastguard Worker 	int count2;
538*9880d681SAndroid Build Coastguard Worker 	sopno pos;
539*9880d681SAndroid Build Coastguard Worker 	int i;
540*9880d681SAndroid Build Coastguard Worker 	sopno subno;
541*9880d681SAndroid Build Coastguard Worker #	define	BACKSL	(1<<CHAR_BIT)
542*9880d681SAndroid Build Coastguard Worker 
543*9880d681SAndroid Build Coastguard Worker         pos = HERE(); /* repetition op, if any, covers from here */
544*9880d681SAndroid Build Coastguard Worker 
545*9880d681SAndroid Build Coastguard Worker         assert(MORE()); /* caller should have ensured this */
546*9880d681SAndroid Build Coastguard Worker         c = GETNEXT();
547*9880d681SAndroid Build Coastguard Worker 	if (c == '\\') {
548*9880d681SAndroid Build Coastguard Worker 		REQUIRE(MORE(), REG_EESCAPE);
549*9880d681SAndroid Build Coastguard Worker 		c = BACKSL | GETNEXT();
550*9880d681SAndroid Build Coastguard Worker 	}
551*9880d681SAndroid Build Coastguard Worker 	switch (c) {
552*9880d681SAndroid Build Coastguard Worker 	case '.':
553*9880d681SAndroid Build Coastguard Worker 		if (p->g->cflags&REG_NEWLINE)
554*9880d681SAndroid Build Coastguard Worker 			nonnewline(p);
555*9880d681SAndroid Build Coastguard Worker 		else
556*9880d681SAndroid Build Coastguard Worker 			EMIT(OANY, 0);
557*9880d681SAndroid Build Coastguard Worker 		break;
558*9880d681SAndroid Build Coastguard Worker 	case '[':
559*9880d681SAndroid Build Coastguard Worker 		p_bracket(p);
560*9880d681SAndroid Build Coastguard Worker 		break;
561*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'{':
562*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_BADRPT);
563*9880d681SAndroid Build Coastguard Worker 		break;
564*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'(':
565*9880d681SAndroid Build Coastguard Worker 		p->g->nsub++;
566*9880d681SAndroid Build Coastguard Worker 		subno = p->g->nsub;
567*9880d681SAndroid Build Coastguard Worker 		if (subno < NPAREN)
568*9880d681SAndroid Build Coastguard Worker 			p->pbegin[subno] = HERE();
569*9880d681SAndroid Build Coastguard Worker 		EMIT(OLPAREN, subno);
570*9880d681SAndroid Build Coastguard Worker 		/* the MORE here is an error heuristic */
571*9880d681SAndroid Build Coastguard Worker 		if (MORE() && !SEETWO('\\', ')'))
572*9880d681SAndroid Build Coastguard Worker 			p_bre(p, '\\', ')');
573*9880d681SAndroid Build Coastguard Worker 		if (subno < NPAREN) {
574*9880d681SAndroid Build Coastguard Worker 			p->pend[subno] = HERE();
575*9880d681SAndroid Build Coastguard Worker 			assert(p->pend[subno] != 0);
576*9880d681SAndroid Build Coastguard Worker 		}
577*9880d681SAndroid Build Coastguard Worker 		EMIT(ORPAREN, subno);
578*9880d681SAndroid Build Coastguard Worker 		REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
579*9880d681SAndroid Build Coastguard Worker 		break;
580*9880d681SAndroid Build Coastguard Worker 	case BACKSL|')':	/* should not get here -- must be user */
581*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'}':
582*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_EPAREN);
583*9880d681SAndroid Build Coastguard Worker 		break;
584*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'1':
585*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'2':
586*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'3':
587*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'4':
588*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'5':
589*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'6':
590*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'7':
591*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'8':
592*9880d681SAndroid Build Coastguard Worker 	case BACKSL|'9':
593*9880d681SAndroid Build Coastguard Worker 		i = (c&~BACKSL) - '0';
594*9880d681SAndroid Build Coastguard Worker 		assert(i < NPAREN);
595*9880d681SAndroid Build Coastguard Worker 		if (p->pend[i] != 0) {
596*9880d681SAndroid Build Coastguard Worker 			assert(i <= p->g->nsub);
597*9880d681SAndroid Build Coastguard Worker 			EMIT(OBACK_, i);
598*9880d681SAndroid Build Coastguard Worker 			assert(p->pbegin[i] != 0);
599*9880d681SAndroid Build Coastguard Worker 			assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
600*9880d681SAndroid Build Coastguard Worker 			assert(OP(p->strip[p->pend[i]]) == ORPAREN);
601*9880d681SAndroid Build Coastguard Worker 			(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
602*9880d681SAndroid Build Coastguard Worker 			EMIT(O_BACK, i);
603*9880d681SAndroid Build Coastguard Worker 		} else
604*9880d681SAndroid Build Coastguard Worker 			SETERROR(REG_ESUBREG);
605*9880d681SAndroid Build Coastguard Worker 		p->g->backrefs = 1;
606*9880d681SAndroid Build Coastguard Worker 		break;
607*9880d681SAndroid Build Coastguard Worker 	case '*':
608*9880d681SAndroid Build Coastguard Worker 		REQUIRE(starordinary, REG_BADRPT);
609*9880d681SAndroid Build Coastguard Worker 		/* FALLTHROUGH */
610*9880d681SAndroid Build Coastguard Worker 	default:
611*9880d681SAndroid Build Coastguard Worker 		ordinary(p, (char)c);
612*9880d681SAndroid Build Coastguard Worker 		break;
613*9880d681SAndroid Build Coastguard Worker 	}
614*9880d681SAndroid Build Coastguard Worker 
615*9880d681SAndroid Build Coastguard Worker 	if (EAT('*')) {		/* implemented as +? */
616*9880d681SAndroid Build Coastguard Worker 		/* this case does not require the (y|) trick, noKLUDGE */
617*9880d681SAndroid Build Coastguard Worker 		INSERT(OPLUS_, pos);
618*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_PLUS, pos);
619*9880d681SAndroid Build Coastguard Worker 		INSERT(OQUEST_, pos);
620*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_QUEST, pos);
621*9880d681SAndroid Build Coastguard Worker 	} else if (EATTWO('\\', '{')) {
622*9880d681SAndroid Build Coastguard Worker 		count = p_count(p);
623*9880d681SAndroid Build Coastguard Worker 		if (EAT(',')) {
624*9880d681SAndroid Build Coastguard Worker 			if (MORE() && isdigit((uch)PEEK())) {
625*9880d681SAndroid Build Coastguard Worker 				count2 = p_count(p);
626*9880d681SAndroid Build Coastguard Worker 				REQUIRE(count <= count2, REG_BADBR);
627*9880d681SAndroid Build Coastguard Worker 			} else		/* single number with comma */
628*9880d681SAndroid Build Coastguard Worker 				count2 = INFINITY;
629*9880d681SAndroid Build Coastguard Worker 		} else		/* just a single number */
630*9880d681SAndroid Build Coastguard Worker 			count2 = count;
631*9880d681SAndroid Build Coastguard Worker 		repeat(p, pos, count, count2);
632*9880d681SAndroid Build Coastguard Worker 		if (!EATTWO('\\', '}')) {	/* error heuristics */
633*9880d681SAndroid Build Coastguard Worker 			while (MORE() && !SEETWO('\\', '}'))
634*9880d681SAndroid Build Coastguard Worker 				NEXT();
635*9880d681SAndroid Build Coastguard Worker 			REQUIRE(MORE(), REG_EBRACE);
636*9880d681SAndroid Build Coastguard Worker 			SETERROR(REG_BADBR);
637*9880d681SAndroid Build Coastguard Worker 		}
638*9880d681SAndroid Build Coastguard Worker 	} else if (c == '$')	/* $ (but not \$) ends it */
639*9880d681SAndroid Build Coastguard Worker 		return(1);
640*9880d681SAndroid Build Coastguard Worker 
641*9880d681SAndroid Build Coastguard Worker 	return(0);
642*9880d681SAndroid Build Coastguard Worker }
643*9880d681SAndroid Build Coastguard Worker 
644*9880d681SAndroid Build Coastguard Worker /*
645*9880d681SAndroid Build Coastguard Worker  - p_count - parse a repetition count
646*9880d681SAndroid Build Coastguard Worker  */
647*9880d681SAndroid Build Coastguard Worker static int			/* the value */
p_count(struct parse * p)648*9880d681SAndroid Build Coastguard Worker p_count(struct parse *p)
649*9880d681SAndroid Build Coastguard Worker {
650*9880d681SAndroid Build Coastguard Worker 	int count = 0;
651*9880d681SAndroid Build Coastguard Worker 	int ndigits = 0;
652*9880d681SAndroid Build Coastguard Worker 
653*9880d681SAndroid Build Coastguard Worker 	while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
654*9880d681SAndroid Build Coastguard Worker 		count = count*10 + (GETNEXT() - '0');
655*9880d681SAndroid Build Coastguard Worker 		ndigits++;
656*9880d681SAndroid Build Coastguard Worker 	}
657*9880d681SAndroid Build Coastguard Worker 
658*9880d681SAndroid Build Coastguard Worker 	REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
659*9880d681SAndroid Build Coastguard Worker 	return(count);
660*9880d681SAndroid Build Coastguard Worker }
661*9880d681SAndroid Build Coastguard Worker 
662*9880d681SAndroid Build Coastguard Worker /*
663*9880d681SAndroid Build Coastguard Worker  - p_bracket - parse a bracketed character list
664*9880d681SAndroid Build Coastguard Worker  *
665*9880d681SAndroid Build Coastguard Worker  * Note a significant property of this code:  if the allocset() did SETERROR,
666*9880d681SAndroid Build Coastguard Worker  * no set operations are done.
667*9880d681SAndroid Build Coastguard Worker  */
668*9880d681SAndroid Build Coastguard Worker static void
p_bracket(struct parse * p)669*9880d681SAndroid Build Coastguard Worker p_bracket(struct parse *p)
670*9880d681SAndroid Build Coastguard Worker {
671*9880d681SAndroid Build Coastguard Worker 	cset *cs;
672*9880d681SAndroid Build Coastguard Worker 	int invert = 0;
673*9880d681SAndroid Build Coastguard Worker 
674*9880d681SAndroid Build Coastguard Worker 	/* Dept of Truly Sickening Special-Case Kludges */
675*9880d681SAndroid Build Coastguard Worker 	if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
676*9880d681SAndroid Build Coastguard Worker 		EMIT(OBOW, 0);
677*9880d681SAndroid Build Coastguard Worker 		NEXTn(6);
678*9880d681SAndroid Build Coastguard Worker 		return;
679*9880d681SAndroid Build Coastguard Worker 	}
680*9880d681SAndroid Build Coastguard Worker 	if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
681*9880d681SAndroid Build Coastguard Worker 		EMIT(OEOW, 0);
682*9880d681SAndroid Build Coastguard Worker 		NEXTn(6);
683*9880d681SAndroid Build Coastguard Worker 		return;
684*9880d681SAndroid Build Coastguard Worker 	}
685*9880d681SAndroid Build Coastguard Worker 
686*9880d681SAndroid Build Coastguard Worker 	if ((cs = allocset(p)) == NULL) {
687*9880d681SAndroid Build Coastguard Worker 		/* allocset did set error status in p */
688*9880d681SAndroid Build Coastguard Worker 		return;
689*9880d681SAndroid Build Coastguard Worker 	}
690*9880d681SAndroid Build Coastguard Worker 
691*9880d681SAndroid Build Coastguard Worker 	if (EAT('^'))
692*9880d681SAndroid Build Coastguard Worker 		invert++;	/* make note to invert set at end */
693*9880d681SAndroid Build Coastguard Worker 	if (EAT(']'))
694*9880d681SAndroid Build Coastguard Worker 		CHadd(cs, ']');
695*9880d681SAndroid Build Coastguard Worker 	else if (EAT('-'))
696*9880d681SAndroid Build Coastguard Worker 		CHadd(cs, '-');
697*9880d681SAndroid Build Coastguard Worker 	while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
698*9880d681SAndroid Build Coastguard Worker 		p_b_term(p, cs);
699*9880d681SAndroid Build Coastguard Worker 	if (EAT('-'))
700*9880d681SAndroid Build Coastguard Worker 		CHadd(cs, '-');
701*9880d681SAndroid Build Coastguard Worker 	MUSTEAT(']', REG_EBRACK);
702*9880d681SAndroid Build Coastguard Worker 
703*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0) {	/* don't mess things up further */
704*9880d681SAndroid Build Coastguard Worker 		freeset(p, cs);
705*9880d681SAndroid Build Coastguard Worker 		return;
706*9880d681SAndroid Build Coastguard Worker 	}
707*9880d681SAndroid Build Coastguard Worker 
708*9880d681SAndroid Build Coastguard Worker 	if (p->g->cflags&REG_ICASE) {
709*9880d681SAndroid Build Coastguard Worker 		int i;
710*9880d681SAndroid Build Coastguard Worker 		int ci;
711*9880d681SAndroid Build Coastguard Worker 
712*9880d681SAndroid Build Coastguard Worker 		for (i = p->g->csetsize - 1; i >= 0; i--)
713*9880d681SAndroid Build Coastguard Worker 			if (CHIN(cs, i) && isalpha(i)) {
714*9880d681SAndroid Build Coastguard Worker 				ci = othercase(i);
715*9880d681SAndroid Build Coastguard Worker 				if (ci != i)
716*9880d681SAndroid Build Coastguard Worker 					CHadd(cs, ci);
717*9880d681SAndroid Build Coastguard Worker 			}
718*9880d681SAndroid Build Coastguard Worker 		if (cs->multis != NULL)
719*9880d681SAndroid Build Coastguard Worker 			mccase(p, cs);
720*9880d681SAndroid Build Coastguard Worker 	}
721*9880d681SAndroid Build Coastguard Worker 	if (invert) {
722*9880d681SAndroid Build Coastguard Worker 		int i;
723*9880d681SAndroid Build Coastguard Worker 
724*9880d681SAndroid Build Coastguard Worker 		for (i = p->g->csetsize - 1; i >= 0; i--)
725*9880d681SAndroid Build Coastguard Worker 			if (CHIN(cs, i))
726*9880d681SAndroid Build Coastguard Worker 				CHsub(cs, i);
727*9880d681SAndroid Build Coastguard Worker 			else
728*9880d681SAndroid Build Coastguard Worker 				CHadd(cs, i);
729*9880d681SAndroid Build Coastguard Worker 		if (p->g->cflags&REG_NEWLINE)
730*9880d681SAndroid Build Coastguard Worker 			CHsub(cs, '\n');
731*9880d681SAndroid Build Coastguard Worker 		if (cs->multis != NULL)
732*9880d681SAndroid Build Coastguard Worker 			mcinvert(p, cs);
733*9880d681SAndroid Build Coastguard Worker 	}
734*9880d681SAndroid Build Coastguard Worker 
735*9880d681SAndroid Build Coastguard Worker 	assert(cs->multis == NULL);		/* xxx */
736*9880d681SAndroid Build Coastguard Worker 
737*9880d681SAndroid Build Coastguard Worker 	if (nch(p, cs) == 1) {		/* optimize singleton sets */
738*9880d681SAndroid Build Coastguard Worker 		ordinary(p, firstch(p, cs));
739*9880d681SAndroid Build Coastguard Worker 		freeset(p, cs);
740*9880d681SAndroid Build Coastguard Worker 	} else
741*9880d681SAndroid Build Coastguard Worker 		EMIT(OANYOF, freezeset(p, cs));
742*9880d681SAndroid Build Coastguard Worker }
743*9880d681SAndroid Build Coastguard Worker 
744*9880d681SAndroid Build Coastguard Worker /*
745*9880d681SAndroid Build Coastguard Worker  - p_b_term - parse one term of a bracketed character list
746*9880d681SAndroid Build Coastguard Worker  */
747*9880d681SAndroid Build Coastguard Worker static void
p_b_term(struct parse * p,cset * cs)748*9880d681SAndroid Build Coastguard Worker p_b_term(struct parse *p, cset *cs)
749*9880d681SAndroid Build Coastguard Worker {
750*9880d681SAndroid Build Coastguard Worker 	char c;
751*9880d681SAndroid Build Coastguard Worker 	char start, finish;
752*9880d681SAndroid Build Coastguard Worker 	int i;
753*9880d681SAndroid Build Coastguard Worker 
754*9880d681SAndroid Build Coastguard Worker 	/* classify what we've got */
755*9880d681SAndroid Build Coastguard Worker 	switch ((MORE()) ? PEEK() : '\0') {
756*9880d681SAndroid Build Coastguard Worker 	case '[':
757*9880d681SAndroid Build Coastguard Worker 		c = (MORE2()) ? PEEK2() : '\0';
758*9880d681SAndroid Build Coastguard Worker 		break;
759*9880d681SAndroid Build Coastguard Worker 	case '-':
760*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ERANGE);
761*9880d681SAndroid Build Coastguard Worker 		return;			/* NOTE RETURN */
762*9880d681SAndroid Build Coastguard Worker 		break;
763*9880d681SAndroid Build Coastguard Worker 	default:
764*9880d681SAndroid Build Coastguard Worker 		c = '\0';
765*9880d681SAndroid Build Coastguard Worker 		break;
766*9880d681SAndroid Build Coastguard Worker 	}
767*9880d681SAndroid Build Coastguard Worker 
768*9880d681SAndroid Build Coastguard Worker 	switch (c) {
769*9880d681SAndroid Build Coastguard Worker 	case ':':		/* character class */
770*9880d681SAndroid Build Coastguard Worker 		NEXT2();
771*9880d681SAndroid Build Coastguard Worker 		REQUIRE(MORE(), REG_EBRACK);
772*9880d681SAndroid Build Coastguard Worker 		c = PEEK();
773*9880d681SAndroid Build Coastguard Worker 		REQUIRE(c != '-' && c != ']', REG_ECTYPE);
774*9880d681SAndroid Build Coastguard Worker 		p_b_cclass(p, cs);
775*9880d681SAndroid Build Coastguard Worker 		REQUIRE(MORE(), REG_EBRACK);
776*9880d681SAndroid Build Coastguard Worker 		REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
777*9880d681SAndroid Build Coastguard Worker 		break;
778*9880d681SAndroid Build Coastguard Worker 	case '=':		/* equivalence class */
779*9880d681SAndroid Build Coastguard Worker 		NEXT2();
780*9880d681SAndroid Build Coastguard Worker 		REQUIRE(MORE(), REG_EBRACK);
781*9880d681SAndroid Build Coastguard Worker 		c = PEEK();
782*9880d681SAndroid Build Coastguard Worker 		REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
783*9880d681SAndroid Build Coastguard Worker 		p_b_eclass(p, cs);
784*9880d681SAndroid Build Coastguard Worker 		REQUIRE(MORE(), REG_EBRACK);
785*9880d681SAndroid Build Coastguard Worker 		REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
786*9880d681SAndroid Build Coastguard Worker 		break;
787*9880d681SAndroid Build Coastguard Worker 	default:		/* symbol, ordinary character, or range */
788*9880d681SAndroid Build Coastguard Worker /* xxx revision needed for multichar stuff */
789*9880d681SAndroid Build Coastguard Worker 		start = p_b_symbol(p);
790*9880d681SAndroid Build Coastguard Worker 		if (SEE('-') && MORE2() && PEEK2() != ']') {
791*9880d681SAndroid Build Coastguard Worker 			/* range */
792*9880d681SAndroid Build Coastguard Worker 			NEXT();
793*9880d681SAndroid Build Coastguard Worker 			if (EAT('-'))
794*9880d681SAndroid Build Coastguard Worker 				finish = '-';
795*9880d681SAndroid Build Coastguard Worker 			else
796*9880d681SAndroid Build Coastguard Worker 				finish = p_b_symbol(p);
797*9880d681SAndroid Build Coastguard Worker 		} else
798*9880d681SAndroid Build Coastguard Worker 			finish = start;
799*9880d681SAndroid Build Coastguard Worker /* xxx what about signed chars here... */
800*9880d681SAndroid Build Coastguard Worker 		REQUIRE(start <= finish, REG_ERANGE);
801*9880d681SAndroid Build Coastguard Worker 		for (i = start; i <= finish; i++)
802*9880d681SAndroid Build Coastguard Worker 			CHadd(cs, i);
803*9880d681SAndroid Build Coastguard Worker 		break;
804*9880d681SAndroid Build Coastguard Worker 	}
805*9880d681SAndroid Build Coastguard Worker }
806*9880d681SAndroid Build Coastguard Worker 
807*9880d681SAndroid Build Coastguard Worker /*
808*9880d681SAndroid Build Coastguard Worker  - p_b_cclass - parse a character-class name and deal with it
809*9880d681SAndroid Build Coastguard Worker  */
810*9880d681SAndroid Build Coastguard Worker static void
p_b_cclass(struct parse * p,cset * cs)811*9880d681SAndroid Build Coastguard Worker p_b_cclass(struct parse *p, cset *cs)
812*9880d681SAndroid Build Coastguard Worker {
813*9880d681SAndroid Build Coastguard Worker 	char *sp = p->next;
814*9880d681SAndroid Build Coastguard Worker 	struct cclass *cp;
815*9880d681SAndroid Build Coastguard Worker 	size_t len;
816*9880d681SAndroid Build Coastguard Worker 	const char *u;
817*9880d681SAndroid Build Coastguard Worker 	char c;
818*9880d681SAndroid Build Coastguard Worker 
819*9880d681SAndroid Build Coastguard Worker 	while (MORE() && isalpha((uch)PEEK()))
820*9880d681SAndroid Build Coastguard Worker 		NEXT();
821*9880d681SAndroid Build Coastguard Worker 	len = p->next - sp;
822*9880d681SAndroid Build Coastguard Worker 	for (cp = cclasses; cp->name != NULL; cp++)
823*9880d681SAndroid Build Coastguard Worker 		if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
824*9880d681SAndroid Build Coastguard Worker 			break;
825*9880d681SAndroid Build Coastguard Worker 	if (cp->name == NULL) {
826*9880d681SAndroid Build Coastguard Worker 		/* oops, didn't find it */
827*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ECTYPE);
828*9880d681SAndroid Build Coastguard Worker 		return;
829*9880d681SAndroid Build Coastguard Worker 	}
830*9880d681SAndroid Build Coastguard Worker 
831*9880d681SAndroid Build Coastguard Worker 	u = cp->chars;
832*9880d681SAndroid Build Coastguard Worker 	while ((c = *u++) != '\0')
833*9880d681SAndroid Build Coastguard Worker 		CHadd(cs, c);
834*9880d681SAndroid Build Coastguard Worker 	for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
835*9880d681SAndroid Build Coastguard Worker 		MCadd(p, cs, u);
836*9880d681SAndroid Build Coastguard Worker }
837*9880d681SAndroid Build Coastguard Worker 
838*9880d681SAndroid Build Coastguard Worker /*
839*9880d681SAndroid Build Coastguard Worker  - p_b_eclass - parse an equivalence-class name and deal with it
840*9880d681SAndroid Build Coastguard Worker  *
841*9880d681SAndroid Build Coastguard Worker  * This implementation is incomplete. xxx
842*9880d681SAndroid Build Coastguard Worker  */
843*9880d681SAndroid Build Coastguard Worker static void
p_b_eclass(struct parse * p,cset * cs)844*9880d681SAndroid Build Coastguard Worker p_b_eclass(struct parse *p, cset *cs)
845*9880d681SAndroid Build Coastguard Worker {
846*9880d681SAndroid Build Coastguard Worker 	char c;
847*9880d681SAndroid Build Coastguard Worker 
848*9880d681SAndroid Build Coastguard Worker 	c = p_b_coll_elem(p, '=');
849*9880d681SAndroid Build Coastguard Worker 	CHadd(cs, c);
850*9880d681SAndroid Build Coastguard Worker }
851*9880d681SAndroid Build Coastguard Worker 
852*9880d681SAndroid Build Coastguard Worker /*
853*9880d681SAndroid Build Coastguard Worker  - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
854*9880d681SAndroid Build Coastguard Worker  */
855*9880d681SAndroid Build Coastguard Worker static char			/* value of symbol */
p_b_symbol(struct parse * p)856*9880d681SAndroid Build Coastguard Worker p_b_symbol(struct parse *p)
857*9880d681SAndroid Build Coastguard Worker {
858*9880d681SAndroid Build Coastguard Worker 	char value;
859*9880d681SAndroid Build Coastguard Worker 
860*9880d681SAndroid Build Coastguard Worker 	REQUIRE(MORE(), REG_EBRACK);
861*9880d681SAndroid Build Coastguard Worker 	if (!EATTWO('[', '.'))
862*9880d681SAndroid Build Coastguard Worker 		return(GETNEXT());
863*9880d681SAndroid Build Coastguard Worker 
864*9880d681SAndroid Build Coastguard Worker 	/* collating symbol */
865*9880d681SAndroid Build Coastguard Worker 	value = p_b_coll_elem(p, '.');
866*9880d681SAndroid Build Coastguard Worker 	REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
867*9880d681SAndroid Build Coastguard Worker 	return(value);
868*9880d681SAndroid Build Coastguard Worker }
869*9880d681SAndroid Build Coastguard Worker 
870*9880d681SAndroid Build Coastguard Worker /*
871*9880d681SAndroid Build Coastguard Worker  - p_b_coll_elem - parse a collating-element name and look it up
872*9880d681SAndroid Build Coastguard Worker  */
873*9880d681SAndroid Build Coastguard Worker static char			/* value of collating element */
p_b_coll_elem(struct parse * p,int endc)874*9880d681SAndroid Build Coastguard Worker p_b_coll_elem(struct parse *p,
875*9880d681SAndroid Build Coastguard Worker     int endc)			/* name ended by endc,']' */
876*9880d681SAndroid Build Coastguard Worker {
877*9880d681SAndroid Build Coastguard Worker 	char *sp = p->next;
878*9880d681SAndroid Build Coastguard Worker 	struct cname *cp;
879*9880d681SAndroid Build Coastguard Worker 	int len;
880*9880d681SAndroid Build Coastguard Worker 
881*9880d681SAndroid Build Coastguard Worker 	while (MORE() && !SEETWO(endc, ']'))
882*9880d681SAndroid Build Coastguard Worker 		NEXT();
883*9880d681SAndroid Build Coastguard Worker 	if (!MORE()) {
884*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_EBRACK);
885*9880d681SAndroid Build Coastguard Worker 		return(0);
886*9880d681SAndroid Build Coastguard Worker 	}
887*9880d681SAndroid Build Coastguard Worker 	len = p->next - sp;
888*9880d681SAndroid Build Coastguard Worker 	for (cp = cnames; cp->name != NULL; cp++)
889*9880d681SAndroid Build Coastguard Worker 		if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
890*9880d681SAndroid Build Coastguard Worker 			return(cp->code);	/* known name */
891*9880d681SAndroid Build Coastguard Worker 	if (len == 1)
892*9880d681SAndroid Build Coastguard Worker 		return(*sp);	/* single character */
893*9880d681SAndroid Build Coastguard Worker 	SETERROR(REG_ECOLLATE);			/* neither */
894*9880d681SAndroid Build Coastguard Worker 	return(0);
895*9880d681SAndroid Build Coastguard Worker }
896*9880d681SAndroid Build Coastguard Worker 
897*9880d681SAndroid Build Coastguard Worker /*
898*9880d681SAndroid Build Coastguard Worker  - othercase - return the case counterpart of an alphabetic
899*9880d681SAndroid Build Coastguard Worker  */
900*9880d681SAndroid Build Coastguard Worker static char			/* if no counterpart, return ch */
othercase(int ch)901*9880d681SAndroid Build Coastguard Worker othercase(int ch)
902*9880d681SAndroid Build Coastguard Worker {
903*9880d681SAndroid Build Coastguard Worker 	ch = (uch)ch;
904*9880d681SAndroid Build Coastguard Worker 	assert(isalpha(ch));
905*9880d681SAndroid Build Coastguard Worker 	if (isupper(ch))
906*9880d681SAndroid Build Coastguard Worker 		return ((uch)tolower(ch));
907*9880d681SAndroid Build Coastguard Worker 	else if (islower(ch))
908*9880d681SAndroid Build Coastguard Worker 		return ((uch)toupper(ch));
909*9880d681SAndroid Build Coastguard Worker 	else			/* peculiar, but could happen */
910*9880d681SAndroid Build Coastguard Worker 		return(ch);
911*9880d681SAndroid Build Coastguard Worker }
912*9880d681SAndroid Build Coastguard Worker 
913*9880d681SAndroid Build Coastguard Worker /*
914*9880d681SAndroid Build Coastguard Worker  - bothcases - emit a dualcase version of a two-case character
915*9880d681SAndroid Build Coastguard Worker  *
916*9880d681SAndroid Build Coastguard Worker  * Boy, is this implementation ever a kludge...
917*9880d681SAndroid Build Coastguard Worker  */
918*9880d681SAndroid Build Coastguard Worker static void
bothcases(struct parse * p,int ch)919*9880d681SAndroid Build Coastguard Worker bothcases(struct parse *p, int ch)
920*9880d681SAndroid Build Coastguard Worker {
921*9880d681SAndroid Build Coastguard Worker 	char *oldnext = p->next;
922*9880d681SAndroid Build Coastguard Worker 	char *oldend = p->end;
923*9880d681SAndroid Build Coastguard Worker 	char bracket[3];
924*9880d681SAndroid Build Coastguard Worker 
925*9880d681SAndroid Build Coastguard Worker 	ch = (uch)ch;
926*9880d681SAndroid Build Coastguard Worker 	assert(othercase(ch) != ch);	/* p_bracket() would recurse */
927*9880d681SAndroid Build Coastguard Worker 	p->next = bracket;
928*9880d681SAndroid Build Coastguard Worker 	p->end = bracket+2;
929*9880d681SAndroid Build Coastguard Worker 	bracket[0] = ch;
930*9880d681SAndroid Build Coastguard Worker 	bracket[1] = ']';
931*9880d681SAndroid Build Coastguard Worker 	bracket[2] = '\0';
932*9880d681SAndroid Build Coastguard Worker 	p_bracket(p);
933*9880d681SAndroid Build Coastguard Worker 	assert(p->next == bracket+2);
934*9880d681SAndroid Build Coastguard Worker 	p->next = oldnext;
935*9880d681SAndroid Build Coastguard Worker 	p->end = oldend;
936*9880d681SAndroid Build Coastguard Worker }
937*9880d681SAndroid Build Coastguard Worker 
938*9880d681SAndroid Build Coastguard Worker /*
939*9880d681SAndroid Build Coastguard Worker  - ordinary - emit an ordinary character
940*9880d681SAndroid Build Coastguard Worker  */
941*9880d681SAndroid Build Coastguard Worker static void
ordinary(struct parse * p,int ch)942*9880d681SAndroid Build Coastguard Worker ordinary(struct parse *p, int ch)
943*9880d681SAndroid Build Coastguard Worker {
944*9880d681SAndroid Build Coastguard Worker 	cat_t *cap = p->g->categories;
945*9880d681SAndroid Build Coastguard Worker 
946*9880d681SAndroid Build Coastguard Worker 	if ((p->g->cflags&REG_ICASE) && isalpha((uch)ch) && othercase(ch) != ch)
947*9880d681SAndroid Build Coastguard Worker 		bothcases(p, ch);
948*9880d681SAndroid Build Coastguard Worker 	else {
949*9880d681SAndroid Build Coastguard Worker 		EMIT(OCHAR, (uch)ch);
950*9880d681SAndroid Build Coastguard Worker 		if (cap[ch] == 0)
951*9880d681SAndroid Build Coastguard Worker 			cap[ch] = p->g->ncategories++;
952*9880d681SAndroid Build Coastguard Worker 	}
953*9880d681SAndroid Build Coastguard Worker }
954*9880d681SAndroid Build Coastguard Worker 
955*9880d681SAndroid Build Coastguard Worker /*
956*9880d681SAndroid Build Coastguard Worker  - nonnewline - emit REG_NEWLINE version of OANY
957*9880d681SAndroid Build Coastguard Worker  *
958*9880d681SAndroid Build Coastguard Worker  * Boy, is this implementation ever a kludge...
959*9880d681SAndroid Build Coastguard Worker  */
960*9880d681SAndroid Build Coastguard Worker static void
nonnewline(struct parse * p)961*9880d681SAndroid Build Coastguard Worker nonnewline(struct parse *p)
962*9880d681SAndroid Build Coastguard Worker {
963*9880d681SAndroid Build Coastguard Worker 	char *oldnext = p->next;
964*9880d681SAndroid Build Coastguard Worker 	char *oldend = p->end;
965*9880d681SAndroid Build Coastguard Worker 	char bracket[4];
966*9880d681SAndroid Build Coastguard Worker 
967*9880d681SAndroid Build Coastguard Worker 	p->next = bracket;
968*9880d681SAndroid Build Coastguard Worker 	p->end = bracket+3;
969*9880d681SAndroid Build Coastguard Worker 	bracket[0] = '^';
970*9880d681SAndroid Build Coastguard Worker 	bracket[1] = '\n';
971*9880d681SAndroid Build Coastguard Worker 	bracket[2] = ']';
972*9880d681SAndroid Build Coastguard Worker 	bracket[3] = '\0';
973*9880d681SAndroid Build Coastguard Worker 	p_bracket(p);
974*9880d681SAndroid Build Coastguard Worker 	assert(p->next == bracket+3);
975*9880d681SAndroid Build Coastguard Worker 	p->next = oldnext;
976*9880d681SAndroid Build Coastguard Worker 	p->end = oldend;
977*9880d681SAndroid Build Coastguard Worker }
978*9880d681SAndroid Build Coastguard Worker 
979*9880d681SAndroid Build Coastguard Worker /*
980*9880d681SAndroid Build Coastguard Worker  - repeat - generate code for a bounded repetition, recursively if needed
981*9880d681SAndroid Build Coastguard Worker  */
982*9880d681SAndroid Build Coastguard Worker static void
repeat(struct parse * p,sopno start,int from,int to)983*9880d681SAndroid Build Coastguard Worker repeat(struct parse *p,
984*9880d681SAndroid Build Coastguard Worker     sopno start,		/* operand from here to end of strip */
985*9880d681SAndroid Build Coastguard Worker     int from,			/* repeated from this number */
986*9880d681SAndroid Build Coastguard Worker     int to)			/* to this number of times (maybe INFINITY) */
987*9880d681SAndroid Build Coastguard Worker {
988*9880d681SAndroid Build Coastguard Worker 	sopno finish = HERE();
989*9880d681SAndroid Build Coastguard Worker #	define	N	2
990*9880d681SAndroid Build Coastguard Worker #	define	INF	3
991*9880d681SAndroid Build Coastguard Worker #	define	REP(f, t)	((f)*8 + (t))
992*9880d681SAndroid Build Coastguard Worker #	define	MAP(n)	(((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
993*9880d681SAndroid Build Coastguard Worker 	sopno copy;
994*9880d681SAndroid Build Coastguard Worker 
995*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)	/* head off possible runaway recursion */
996*9880d681SAndroid Build Coastguard Worker 		return;
997*9880d681SAndroid Build Coastguard Worker 
998*9880d681SAndroid Build Coastguard Worker 	assert(from <= to);
999*9880d681SAndroid Build Coastguard Worker 
1000*9880d681SAndroid Build Coastguard Worker 	switch (REP(MAP(from), MAP(to))) {
1001*9880d681SAndroid Build Coastguard Worker 	case REP(0, 0):			/* must be user doing this */
1002*9880d681SAndroid Build Coastguard Worker 		DROP(finish-start);	/* drop the operand */
1003*9880d681SAndroid Build Coastguard Worker 		break;
1004*9880d681SAndroid Build Coastguard Worker 	case REP(0, 1):			/* as x{1,1}? */
1005*9880d681SAndroid Build Coastguard Worker 	case REP(0, N):			/* as x{1,n}? */
1006*9880d681SAndroid Build Coastguard Worker 	case REP(0, INF):		/* as x{1,}? */
1007*9880d681SAndroid Build Coastguard Worker 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1008*9880d681SAndroid Build Coastguard Worker 		INSERT(OCH_, start);		/* offset is wrong... */
1009*9880d681SAndroid Build Coastguard Worker 		repeat(p, start+1, 1, to);
1010*9880d681SAndroid Build Coastguard Worker 		ASTERN(OOR1, start);
1011*9880d681SAndroid Build Coastguard Worker 		AHEAD(start);			/* ... fix it */
1012*9880d681SAndroid Build Coastguard Worker 		EMIT(OOR2, 0);
1013*9880d681SAndroid Build Coastguard Worker 		AHEAD(THERE());
1014*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_CH, THERETHERE());
1015*9880d681SAndroid Build Coastguard Worker 		break;
1016*9880d681SAndroid Build Coastguard Worker 	case REP(1, 1):			/* trivial case */
1017*9880d681SAndroid Build Coastguard Worker 		/* done */
1018*9880d681SAndroid Build Coastguard Worker 		break;
1019*9880d681SAndroid Build Coastguard Worker 	case REP(1, N):			/* as x?x{1,n-1} */
1020*9880d681SAndroid Build Coastguard Worker 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1021*9880d681SAndroid Build Coastguard Worker 		INSERT(OCH_, start);
1022*9880d681SAndroid Build Coastguard Worker 		ASTERN(OOR1, start);
1023*9880d681SAndroid Build Coastguard Worker 		AHEAD(start);
1024*9880d681SAndroid Build Coastguard Worker 		EMIT(OOR2, 0);			/* offset very wrong... */
1025*9880d681SAndroid Build Coastguard Worker 		AHEAD(THERE());			/* ...so fix it */
1026*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_CH, THERETHERE());
1027*9880d681SAndroid Build Coastguard Worker 		copy = dupl(p, start+1, finish+1);
1028*9880d681SAndroid Build Coastguard Worker 		assert(copy == finish+4);
1029*9880d681SAndroid Build Coastguard Worker 		repeat(p, copy, 1, to-1);
1030*9880d681SAndroid Build Coastguard Worker 		break;
1031*9880d681SAndroid Build Coastguard Worker 	case REP(1, INF):		/* as x+ */
1032*9880d681SAndroid Build Coastguard Worker 		INSERT(OPLUS_, start);
1033*9880d681SAndroid Build Coastguard Worker 		ASTERN(O_PLUS, start);
1034*9880d681SAndroid Build Coastguard Worker 		break;
1035*9880d681SAndroid Build Coastguard Worker 	case REP(N, N):			/* as xx{m-1,n-1} */
1036*9880d681SAndroid Build Coastguard Worker 		copy = dupl(p, start, finish);
1037*9880d681SAndroid Build Coastguard Worker 		repeat(p, copy, from-1, to-1);
1038*9880d681SAndroid Build Coastguard Worker 		break;
1039*9880d681SAndroid Build Coastguard Worker 	case REP(N, INF):		/* as xx{n-1,INF} */
1040*9880d681SAndroid Build Coastguard Worker 		copy = dupl(p, start, finish);
1041*9880d681SAndroid Build Coastguard Worker 		repeat(p, copy, from-1, to);
1042*9880d681SAndroid Build Coastguard Worker 		break;
1043*9880d681SAndroid Build Coastguard Worker 	default:			/* "can't happen" */
1044*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ASSERT);	/* just in case */
1045*9880d681SAndroid Build Coastguard Worker 		break;
1046*9880d681SAndroid Build Coastguard Worker 	}
1047*9880d681SAndroid Build Coastguard Worker }
1048*9880d681SAndroid Build Coastguard Worker 
1049*9880d681SAndroid Build Coastguard Worker /*
1050*9880d681SAndroid Build Coastguard Worker  - seterr - set an error condition
1051*9880d681SAndroid Build Coastguard Worker  */
1052*9880d681SAndroid Build Coastguard Worker static int			/* useless but makes type checking happy */
seterr(struct parse * p,int e)1053*9880d681SAndroid Build Coastguard Worker seterr(struct parse *p, int e)
1054*9880d681SAndroid Build Coastguard Worker {
1055*9880d681SAndroid Build Coastguard Worker 	if (p->error == 0)	/* keep earliest error condition */
1056*9880d681SAndroid Build Coastguard Worker 		p->error = e;
1057*9880d681SAndroid Build Coastguard Worker 	p->next = nuls;		/* try to bring things to a halt */
1058*9880d681SAndroid Build Coastguard Worker 	p->end = nuls;
1059*9880d681SAndroid Build Coastguard Worker 	return(0);		/* make the return value well-defined */
1060*9880d681SAndroid Build Coastguard Worker }
1061*9880d681SAndroid Build Coastguard Worker 
1062*9880d681SAndroid Build Coastguard Worker /*
1063*9880d681SAndroid Build Coastguard Worker  - allocset - allocate a set of characters for []
1064*9880d681SAndroid Build Coastguard Worker  */
1065*9880d681SAndroid Build Coastguard Worker static cset *
allocset(struct parse * p)1066*9880d681SAndroid Build Coastguard Worker allocset(struct parse *p)
1067*9880d681SAndroid Build Coastguard Worker {
1068*9880d681SAndroid Build Coastguard Worker 	int no = p->g->ncsets++;
1069*9880d681SAndroid Build Coastguard Worker 	size_t nc;
1070*9880d681SAndroid Build Coastguard Worker 	size_t nbytes;
1071*9880d681SAndroid Build Coastguard Worker 	cset *cs;
1072*9880d681SAndroid Build Coastguard Worker 	size_t css = (size_t)p->g->csetsize;
1073*9880d681SAndroid Build Coastguard Worker 	int i;
1074*9880d681SAndroid Build Coastguard Worker 
1075*9880d681SAndroid Build Coastguard Worker 	if (no >= p->ncsalloc) {	/* need another column of space */
1076*9880d681SAndroid Build Coastguard Worker 		void *ptr;
1077*9880d681SAndroid Build Coastguard Worker 
1078*9880d681SAndroid Build Coastguard Worker 		p->ncsalloc += CHAR_BIT;
1079*9880d681SAndroid Build Coastguard Worker 		nc = p->ncsalloc;
1080*9880d681SAndroid Build Coastguard Worker 		if (nc > SIZE_MAX / sizeof(cset))
1081*9880d681SAndroid Build Coastguard Worker 			goto nomem;
1082*9880d681SAndroid Build Coastguard Worker 		assert(nc % CHAR_BIT == 0);
1083*9880d681SAndroid Build Coastguard Worker 		nbytes = nc / CHAR_BIT * css;
1084*9880d681SAndroid Build Coastguard Worker 
1085*9880d681SAndroid Build Coastguard Worker 		ptr = (cset *)realloc((char *)p->g->sets, nc * sizeof(cset));
1086*9880d681SAndroid Build Coastguard Worker 		if (ptr == NULL)
1087*9880d681SAndroid Build Coastguard Worker 			goto nomem;
1088*9880d681SAndroid Build Coastguard Worker 		p->g->sets = ptr;
1089*9880d681SAndroid Build Coastguard Worker 
1090*9880d681SAndroid Build Coastguard Worker 		ptr = (uch *)realloc((char *)p->g->setbits, nbytes);
1091*9880d681SAndroid Build Coastguard Worker 		if (ptr == NULL)
1092*9880d681SAndroid Build Coastguard Worker 			goto nomem;
1093*9880d681SAndroid Build Coastguard Worker 		p->g->setbits = ptr;
1094*9880d681SAndroid Build Coastguard Worker 
1095*9880d681SAndroid Build Coastguard Worker 		for (i = 0; i < no; i++)
1096*9880d681SAndroid Build Coastguard Worker 			p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT);
1097*9880d681SAndroid Build Coastguard Worker 
1098*9880d681SAndroid Build Coastguard Worker 		(void) memset((char *)p->g->setbits + (nbytes - css), 0, css);
1099*9880d681SAndroid Build Coastguard Worker 	}
1100*9880d681SAndroid Build Coastguard Worker 	/* XXX should not happen */
1101*9880d681SAndroid Build Coastguard Worker 	if (p->g->sets == NULL || p->g->setbits == NULL)
1102*9880d681SAndroid Build Coastguard Worker 		goto nomem;
1103*9880d681SAndroid Build Coastguard Worker 
1104*9880d681SAndroid Build Coastguard Worker 	cs = &p->g->sets[no];
1105*9880d681SAndroid Build Coastguard Worker 	cs->ptr = p->g->setbits + css*((no)/CHAR_BIT);
1106*9880d681SAndroid Build Coastguard Worker 	cs->mask = 1 << ((no) % CHAR_BIT);
1107*9880d681SAndroid Build Coastguard Worker 	cs->hash = 0;
1108*9880d681SAndroid Build Coastguard Worker 	cs->smultis = 0;
1109*9880d681SAndroid Build Coastguard Worker 	cs->multis = NULL;
1110*9880d681SAndroid Build Coastguard Worker 
1111*9880d681SAndroid Build Coastguard Worker 	return(cs);
1112*9880d681SAndroid Build Coastguard Worker nomem:
1113*9880d681SAndroid Build Coastguard Worker 	free(p->g->sets);
1114*9880d681SAndroid Build Coastguard Worker 	p->g->sets = NULL;
1115*9880d681SAndroid Build Coastguard Worker 	free(p->g->setbits);
1116*9880d681SAndroid Build Coastguard Worker 	p->g->setbits = NULL;
1117*9880d681SAndroid Build Coastguard Worker 
1118*9880d681SAndroid Build Coastguard Worker 	SETERROR(REG_ESPACE);
1119*9880d681SAndroid Build Coastguard Worker 	/* caller's responsibility not to do set ops */
1120*9880d681SAndroid Build Coastguard Worker 	return(NULL);
1121*9880d681SAndroid Build Coastguard Worker }
1122*9880d681SAndroid Build Coastguard Worker 
1123*9880d681SAndroid Build Coastguard Worker /*
1124*9880d681SAndroid Build Coastguard Worker  - freeset - free a now-unused set
1125*9880d681SAndroid Build Coastguard Worker  */
1126*9880d681SAndroid Build Coastguard Worker static void
freeset(struct parse * p,cset * cs)1127*9880d681SAndroid Build Coastguard Worker freeset(struct parse *p, cset *cs)
1128*9880d681SAndroid Build Coastguard Worker {
1129*9880d681SAndroid Build Coastguard Worker 	size_t i;
1130*9880d681SAndroid Build Coastguard Worker 	cset *top = &p->g->sets[p->g->ncsets];
1131*9880d681SAndroid Build Coastguard Worker 	size_t css = (size_t)p->g->csetsize;
1132*9880d681SAndroid Build Coastguard Worker 
1133*9880d681SAndroid Build Coastguard Worker 	for (i = 0; i < css; i++)
1134*9880d681SAndroid Build Coastguard Worker 		CHsub(cs, i);
1135*9880d681SAndroid Build Coastguard Worker 	if (cs == top-1)	/* recover only the easy case */
1136*9880d681SAndroid Build Coastguard Worker 		p->g->ncsets--;
1137*9880d681SAndroid Build Coastguard Worker }
1138*9880d681SAndroid Build Coastguard Worker 
1139*9880d681SAndroid Build Coastguard Worker /*
1140*9880d681SAndroid Build Coastguard Worker  - freezeset - final processing on a set of characters
1141*9880d681SAndroid Build Coastguard Worker  *
1142*9880d681SAndroid Build Coastguard Worker  * The main task here is merging identical sets.  This is usually a waste
1143*9880d681SAndroid Build Coastguard Worker  * of time (although the hash code minimizes the overhead), but can win
1144*9880d681SAndroid Build Coastguard Worker  * big if REG_ICASE is being used.  REG_ICASE, by the way, is why the hash
1145*9880d681SAndroid Build Coastguard Worker  * is done using addition rather than xor -- all ASCII [aA] sets xor to
1146*9880d681SAndroid Build Coastguard Worker  * the same value!
1147*9880d681SAndroid Build Coastguard Worker  */
1148*9880d681SAndroid Build Coastguard Worker static int			/* set number */
freezeset(struct parse * p,cset * cs)1149*9880d681SAndroid Build Coastguard Worker freezeset(struct parse *p, cset *cs)
1150*9880d681SAndroid Build Coastguard Worker {
1151*9880d681SAndroid Build Coastguard Worker 	uch h = cs->hash;
1152*9880d681SAndroid Build Coastguard Worker 	size_t i;
1153*9880d681SAndroid Build Coastguard Worker 	cset *top = &p->g->sets[p->g->ncsets];
1154*9880d681SAndroid Build Coastguard Worker 	cset *cs2;
1155*9880d681SAndroid Build Coastguard Worker 	size_t css = (size_t)p->g->csetsize;
1156*9880d681SAndroid Build Coastguard Worker 
1157*9880d681SAndroid Build Coastguard Worker 	/* look for an earlier one which is the same */
1158*9880d681SAndroid Build Coastguard Worker 	for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1159*9880d681SAndroid Build Coastguard Worker 		if (cs2->hash == h && cs2 != cs) {
1160*9880d681SAndroid Build Coastguard Worker 			/* maybe */
1161*9880d681SAndroid Build Coastguard Worker 			for (i = 0; i < css; i++)
1162*9880d681SAndroid Build Coastguard Worker 				if (!!CHIN(cs2, i) != !!CHIN(cs, i))
1163*9880d681SAndroid Build Coastguard Worker 					break;		/* no */
1164*9880d681SAndroid Build Coastguard Worker 			if (i == css)
1165*9880d681SAndroid Build Coastguard Worker 				break;			/* yes */
1166*9880d681SAndroid Build Coastguard Worker 		}
1167*9880d681SAndroid Build Coastguard Worker 
1168*9880d681SAndroid Build Coastguard Worker 	if (cs2 < top) {	/* found one */
1169*9880d681SAndroid Build Coastguard Worker 		freeset(p, cs);
1170*9880d681SAndroid Build Coastguard Worker 		cs = cs2;
1171*9880d681SAndroid Build Coastguard Worker 	}
1172*9880d681SAndroid Build Coastguard Worker 
1173*9880d681SAndroid Build Coastguard Worker 	return((int)(cs - p->g->sets));
1174*9880d681SAndroid Build Coastguard Worker }
1175*9880d681SAndroid Build Coastguard Worker 
1176*9880d681SAndroid Build Coastguard Worker /*
1177*9880d681SAndroid Build Coastguard Worker  - firstch - return first character in a set (which must have at least one)
1178*9880d681SAndroid Build Coastguard Worker  */
1179*9880d681SAndroid Build Coastguard Worker static int			/* character; there is no "none" value */
firstch(struct parse * p,cset * cs)1180*9880d681SAndroid Build Coastguard Worker firstch(struct parse *p, cset *cs)
1181*9880d681SAndroid Build Coastguard Worker {
1182*9880d681SAndroid Build Coastguard Worker 	size_t i;
1183*9880d681SAndroid Build Coastguard Worker 	size_t css = (size_t)p->g->csetsize;
1184*9880d681SAndroid Build Coastguard Worker 
1185*9880d681SAndroid Build Coastguard Worker 	for (i = 0; i < css; i++)
1186*9880d681SAndroid Build Coastguard Worker 		if (CHIN(cs, i))
1187*9880d681SAndroid Build Coastguard Worker 			return((char)i);
1188*9880d681SAndroid Build Coastguard Worker 	assert(never);
1189*9880d681SAndroid Build Coastguard Worker 	return(0);		/* arbitrary */
1190*9880d681SAndroid Build Coastguard Worker }
1191*9880d681SAndroid Build Coastguard Worker 
1192*9880d681SAndroid Build Coastguard Worker /*
1193*9880d681SAndroid Build Coastguard Worker  - nch - number of characters in a set
1194*9880d681SAndroid Build Coastguard Worker  */
1195*9880d681SAndroid Build Coastguard Worker static int
nch(struct parse * p,cset * cs)1196*9880d681SAndroid Build Coastguard Worker nch(struct parse *p, cset *cs)
1197*9880d681SAndroid Build Coastguard Worker {
1198*9880d681SAndroid Build Coastguard Worker 	size_t i;
1199*9880d681SAndroid Build Coastguard Worker 	size_t css = (size_t)p->g->csetsize;
1200*9880d681SAndroid Build Coastguard Worker 	int n = 0;
1201*9880d681SAndroid Build Coastguard Worker 
1202*9880d681SAndroid Build Coastguard Worker 	for (i = 0; i < css; i++)
1203*9880d681SAndroid Build Coastguard Worker 		if (CHIN(cs, i))
1204*9880d681SAndroid Build Coastguard Worker 			n++;
1205*9880d681SAndroid Build Coastguard Worker 	return(n);
1206*9880d681SAndroid Build Coastguard Worker }
1207*9880d681SAndroid Build Coastguard Worker 
1208*9880d681SAndroid Build Coastguard Worker /*
1209*9880d681SAndroid Build Coastguard Worker  - mcadd - add a collating element to a cset
1210*9880d681SAndroid Build Coastguard Worker  */
1211*9880d681SAndroid Build Coastguard Worker static void
mcadd(struct parse * p,cset * cs,const char * cp)1212*9880d681SAndroid Build Coastguard Worker mcadd( struct parse *p, cset *cs, const char *cp)
1213*9880d681SAndroid Build Coastguard Worker {
1214*9880d681SAndroid Build Coastguard Worker 	size_t oldend = cs->smultis;
1215*9880d681SAndroid Build Coastguard Worker 	void *np;
1216*9880d681SAndroid Build Coastguard Worker 
1217*9880d681SAndroid Build Coastguard Worker 	cs->smultis += strlen(cp) + 1;
1218*9880d681SAndroid Build Coastguard Worker 	np = realloc(cs->multis, cs->smultis);
1219*9880d681SAndroid Build Coastguard Worker 	if (np == NULL) {
1220*9880d681SAndroid Build Coastguard Worker 		if (cs->multis)
1221*9880d681SAndroid Build Coastguard Worker 			free(cs->multis);
1222*9880d681SAndroid Build Coastguard Worker 		cs->multis = NULL;
1223*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ESPACE);
1224*9880d681SAndroid Build Coastguard Worker 		return;
1225*9880d681SAndroid Build Coastguard Worker 	}
1226*9880d681SAndroid Build Coastguard Worker 	cs->multis = np;
1227*9880d681SAndroid Build Coastguard Worker 
1228*9880d681SAndroid Build Coastguard Worker 	llvm_strlcpy(cs->multis + oldend - 1, cp, cs->smultis - oldend + 1);
1229*9880d681SAndroid Build Coastguard Worker }
1230*9880d681SAndroid Build Coastguard Worker 
1231*9880d681SAndroid Build Coastguard Worker /*
1232*9880d681SAndroid Build Coastguard Worker  - mcinvert - invert the list of collating elements in a cset
1233*9880d681SAndroid Build Coastguard Worker  *
1234*9880d681SAndroid Build Coastguard Worker  * This would have to know the set of possibilities.  Implementation
1235*9880d681SAndroid Build Coastguard Worker  * is deferred.
1236*9880d681SAndroid Build Coastguard Worker  */
1237*9880d681SAndroid Build Coastguard Worker /* ARGSUSED */
1238*9880d681SAndroid Build Coastguard Worker static void
mcinvert(struct parse * p,cset * cs)1239*9880d681SAndroid Build Coastguard Worker mcinvert(struct parse *p, cset *cs)
1240*9880d681SAndroid Build Coastguard Worker {
1241*9880d681SAndroid Build Coastguard Worker 	assert(cs->multis == NULL);	/* xxx */
1242*9880d681SAndroid Build Coastguard Worker }
1243*9880d681SAndroid Build Coastguard Worker 
1244*9880d681SAndroid Build Coastguard Worker /*
1245*9880d681SAndroid Build Coastguard Worker  - mccase - add case counterparts of the list of collating elements in a cset
1246*9880d681SAndroid Build Coastguard Worker  *
1247*9880d681SAndroid Build Coastguard Worker  * This would have to know the set of possibilities.  Implementation
1248*9880d681SAndroid Build Coastguard Worker  * is deferred.
1249*9880d681SAndroid Build Coastguard Worker  */
1250*9880d681SAndroid Build Coastguard Worker /* ARGSUSED */
1251*9880d681SAndroid Build Coastguard Worker static void
mccase(struct parse * p,cset * cs)1252*9880d681SAndroid Build Coastguard Worker mccase(struct parse *p, cset *cs)
1253*9880d681SAndroid Build Coastguard Worker {
1254*9880d681SAndroid Build Coastguard Worker 	assert(cs->multis == NULL);	/* xxx */
1255*9880d681SAndroid Build Coastguard Worker }
1256*9880d681SAndroid Build Coastguard Worker 
1257*9880d681SAndroid Build Coastguard Worker /*
1258*9880d681SAndroid Build Coastguard Worker  - isinsets - is this character in any sets?
1259*9880d681SAndroid Build Coastguard Worker  */
1260*9880d681SAndroid Build Coastguard Worker static int			/* predicate */
isinsets(struct re_guts * g,int c)1261*9880d681SAndroid Build Coastguard Worker isinsets(struct re_guts *g, int c)
1262*9880d681SAndroid Build Coastguard Worker {
1263*9880d681SAndroid Build Coastguard Worker 	uch *col;
1264*9880d681SAndroid Build Coastguard Worker 	int i;
1265*9880d681SAndroid Build Coastguard Worker 	int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1266*9880d681SAndroid Build Coastguard Worker 	unsigned uc = (uch)c;
1267*9880d681SAndroid Build Coastguard Worker 
1268*9880d681SAndroid Build Coastguard Worker 	for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1269*9880d681SAndroid Build Coastguard Worker 		if (col[uc] != 0)
1270*9880d681SAndroid Build Coastguard Worker 			return(1);
1271*9880d681SAndroid Build Coastguard Worker 	return(0);
1272*9880d681SAndroid Build Coastguard Worker }
1273*9880d681SAndroid Build Coastguard Worker 
1274*9880d681SAndroid Build Coastguard Worker /*
1275*9880d681SAndroid Build Coastguard Worker  - samesets - are these two characters in exactly the same sets?
1276*9880d681SAndroid Build Coastguard Worker  */
1277*9880d681SAndroid Build Coastguard Worker static int			/* predicate */
samesets(struct re_guts * g,int c1,int c2)1278*9880d681SAndroid Build Coastguard Worker samesets(struct re_guts *g, int c1, int c2)
1279*9880d681SAndroid Build Coastguard Worker {
1280*9880d681SAndroid Build Coastguard Worker 	uch *col;
1281*9880d681SAndroid Build Coastguard Worker 	int i;
1282*9880d681SAndroid Build Coastguard Worker 	int ncols = (g->ncsets+(CHAR_BIT-1)) / CHAR_BIT;
1283*9880d681SAndroid Build Coastguard Worker 	unsigned uc1 = (uch)c1;
1284*9880d681SAndroid Build Coastguard Worker 	unsigned uc2 = (uch)c2;
1285*9880d681SAndroid Build Coastguard Worker 
1286*9880d681SAndroid Build Coastguard Worker 	for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1287*9880d681SAndroid Build Coastguard Worker 		if (col[uc1] != col[uc2])
1288*9880d681SAndroid Build Coastguard Worker 			return(0);
1289*9880d681SAndroid Build Coastguard Worker 	return(1);
1290*9880d681SAndroid Build Coastguard Worker }
1291*9880d681SAndroid Build Coastguard Worker 
1292*9880d681SAndroid Build Coastguard Worker /*
1293*9880d681SAndroid Build Coastguard Worker  - categorize - sort out character categories
1294*9880d681SAndroid Build Coastguard Worker  */
1295*9880d681SAndroid Build Coastguard Worker static void
categorize(struct parse * p,struct re_guts * g)1296*9880d681SAndroid Build Coastguard Worker categorize(struct parse *p, struct re_guts *g)
1297*9880d681SAndroid Build Coastguard Worker {
1298*9880d681SAndroid Build Coastguard Worker 	cat_t *cats = g->categories;
1299*9880d681SAndroid Build Coastguard Worker 	int c;
1300*9880d681SAndroid Build Coastguard Worker 	int c2;
1301*9880d681SAndroid Build Coastguard Worker 	cat_t cat;
1302*9880d681SAndroid Build Coastguard Worker 
1303*9880d681SAndroid Build Coastguard Worker 	/* avoid making error situations worse */
1304*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)
1305*9880d681SAndroid Build Coastguard Worker 		return;
1306*9880d681SAndroid Build Coastguard Worker 
1307*9880d681SAndroid Build Coastguard Worker 	for (c = CHAR_MIN; c <= CHAR_MAX; c++)
1308*9880d681SAndroid Build Coastguard Worker 		if (cats[c] == 0 && isinsets(g, c)) {
1309*9880d681SAndroid Build Coastguard Worker 			cat = g->ncategories++;
1310*9880d681SAndroid Build Coastguard Worker 			cats[c] = cat;
1311*9880d681SAndroid Build Coastguard Worker 			for (c2 = c+1; c2 <= CHAR_MAX; c2++)
1312*9880d681SAndroid Build Coastguard Worker 				if (cats[c2] == 0 && samesets(g, c, c2))
1313*9880d681SAndroid Build Coastguard Worker 					cats[c2] = cat;
1314*9880d681SAndroid Build Coastguard Worker 		}
1315*9880d681SAndroid Build Coastguard Worker }
1316*9880d681SAndroid Build Coastguard Worker 
1317*9880d681SAndroid Build Coastguard Worker /*
1318*9880d681SAndroid Build Coastguard Worker  - dupl - emit a duplicate of a bunch of sops
1319*9880d681SAndroid Build Coastguard Worker  */
1320*9880d681SAndroid Build Coastguard Worker static sopno			/* start of duplicate */
dupl(struct parse * p,sopno start,sopno finish)1321*9880d681SAndroid Build Coastguard Worker dupl(struct parse *p,
1322*9880d681SAndroid Build Coastguard Worker     sopno start,		/* from here */
1323*9880d681SAndroid Build Coastguard Worker     sopno finish)		/* to this less one */
1324*9880d681SAndroid Build Coastguard Worker {
1325*9880d681SAndroid Build Coastguard Worker 	sopno ret = HERE();
1326*9880d681SAndroid Build Coastguard Worker 	sopno len = finish - start;
1327*9880d681SAndroid Build Coastguard Worker 
1328*9880d681SAndroid Build Coastguard Worker 	assert(finish >= start);
1329*9880d681SAndroid Build Coastguard Worker 	if (len == 0)
1330*9880d681SAndroid Build Coastguard Worker 		return(ret);
1331*9880d681SAndroid Build Coastguard Worker 	enlarge(p, p->ssize + len);	/* this many unexpected additions */
1332*9880d681SAndroid Build Coastguard Worker 	assert(p->ssize >= p->slen + len);
1333*9880d681SAndroid Build Coastguard Worker 	(void) memmove((char *)(p->strip + p->slen),
1334*9880d681SAndroid Build Coastguard Worker 		(char *)(p->strip + start), (size_t)len*sizeof(sop));
1335*9880d681SAndroid Build Coastguard Worker 	p->slen += len;
1336*9880d681SAndroid Build Coastguard Worker 	return(ret);
1337*9880d681SAndroid Build Coastguard Worker }
1338*9880d681SAndroid Build Coastguard Worker 
1339*9880d681SAndroid Build Coastguard Worker /*
1340*9880d681SAndroid Build Coastguard Worker  - doemit - emit a strip operator
1341*9880d681SAndroid Build Coastguard Worker  *
1342*9880d681SAndroid Build Coastguard Worker  * It might seem better to implement this as a macro with a function as
1343*9880d681SAndroid Build Coastguard Worker  * hard-case backup, but it's just too big and messy unless there are
1344*9880d681SAndroid Build Coastguard Worker  * some changes to the data structures.  Maybe later.
1345*9880d681SAndroid Build Coastguard Worker  */
1346*9880d681SAndroid Build Coastguard Worker static void
doemit(struct parse * p,sop op,size_t opnd)1347*9880d681SAndroid Build Coastguard Worker doemit(struct parse *p, sop op, size_t opnd)
1348*9880d681SAndroid Build Coastguard Worker {
1349*9880d681SAndroid Build Coastguard Worker 	/* avoid making error situations worse */
1350*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)
1351*9880d681SAndroid Build Coastguard Worker 		return;
1352*9880d681SAndroid Build Coastguard Worker 
1353*9880d681SAndroid Build Coastguard Worker 	/* deal with oversize operands ("can't happen", more or less) */
1354*9880d681SAndroid Build Coastguard Worker 	assert(opnd < 1<<OPSHIFT);
1355*9880d681SAndroid Build Coastguard Worker 
1356*9880d681SAndroid Build Coastguard Worker 	/* deal with undersized strip */
1357*9880d681SAndroid Build Coastguard Worker 	if (p->slen >= p->ssize)
1358*9880d681SAndroid Build Coastguard Worker 		enlarge(p, (p->ssize+1) / 2 * 3);	/* +50% */
1359*9880d681SAndroid Build Coastguard Worker 	assert(p->slen < p->ssize);
1360*9880d681SAndroid Build Coastguard Worker 
1361*9880d681SAndroid Build Coastguard Worker 	/* finally, it's all reduced to the easy case */
1362*9880d681SAndroid Build Coastguard Worker 	p->strip[p->slen++] = SOP(op, opnd);
1363*9880d681SAndroid Build Coastguard Worker }
1364*9880d681SAndroid Build Coastguard Worker 
1365*9880d681SAndroid Build Coastguard Worker /*
1366*9880d681SAndroid Build Coastguard Worker  - doinsert - insert a sop into the strip
1367*9880d681SAndroid Build Coastguard Worker  */
1368*9880d681SAndroid Build Coastguard Worker static void
doinsert(struct parse * p,sop op,size_t opnd,sopno pos)1369*9880d681SAndroid Build Coastguard Worker doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1370*9880d681SAndroid Build Coastguard Worker {
1371*9880d681SAndroid Build Coastguard Worker 	sopno sn;
1372*9880d681SAndroid Build Coastguard Worker 	sop s;
1373*9880d681SAndroid Build Coastguard Worker 	int i;
1374*9880d681SAndroid Build Coastguard Worker 
1375*9880d681SAndroid Build Coastguard Worker 	/* avoid making error situations worse */
1376*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)
1377*9880d681SAndroid Build Coastguard Worker 		return;
1378*9880d681SAndroid Build Coastguard Worker 
1379*9880d681SAndroid Build Coastguard Worker 	sn = HERE();
1380*9880d681SAndroid Build Coastguard Worker 	EMIT(op, opnd);		/* do checks, ensure space */
1381*9880d681SAndroid Build Coastguard Worker 	assert(HERE() == sn+1);
1382*9880d681SAndroid Build Coastguard Worker 	s = p->strip[sn];
1383*9880d681SAndroid Build Coastguard Worker 
1384*9880d681SAndroid Build Coastguard Worker 	/* adjust paren pointers */
1385*9880d681SAndroid Build Coastguard Worker 	assert(pos > 0);
1386*9880d681SAndroid Build Coastguard Worker 	for (i = 1; i < NPAREN; i++) {
1387*9880d681SAndroid Build Coastguard Worker 		if (p->pbegin[i] >= pos) {
1388*9880d681SAndroid Build Coastguard Worker 			p->pbegin[i]++;
1389*9880d681SAndroid Build Coastguard Worker 		}
1390*9880d681SAndroid Build Coastguard Worker 		if (p->pend[i] >= pos) {
1391*9880d681SAndroid Build Coastguard Worker 			p->pend[i]++;
1392*9880d681SAndroid Build Coastguard Worker 		}
1393*9880d681SAndroid Build Coastguard Worker 	}
1394*9880d681SAndroid Build Coastguard Worker 
1395*9880d681SAndroid Build Coastguard Worker 	memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1396*9880d681SAndroid Build Coastguard Worker 						(HERE()-pos-1)*sizeof(sop));
1397*9880d681SAndroid Build Coastguard Worker 	p->strip[pos] = s;
1398*9880d681SAndroid Build Coastguard Worker }
1399*9880d681SAndroid Build Coastguard Worker 
1400*9880d681SAndroid Build Coastguard Worker /*
1401*9880d681SAndroid Build Coastguard Worker  - dofwd - complete a forward reference
1402*9880d681SAndroid Build Coastguard Worker  */
1403*9880d681SAndroid Build Coastguard Worker static void
dofwd(struct parse * p,sopno pos,sop value)1404*9880d681SAndroid Build Coastguard Worker dofwd(struct parse *p, sopno pos, sop value)
1405*9880d681SAndroid Build Coastguard Worker {
1406*9880d681SAndroid Build Coastguard Worker 	/* avoid making error situations worse */
1407*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)
1408*9880d681SAndroid Build Coastguard Worker 		return;
1409*9880d681SAndroid Build Coastguard Worker 
1410*9880d681SAndroid Build Coastguard Worker 	assert(value < 1<<OPSHIFT);
1411*9880d681SAndroid Build Coastguard Worker 	p->strip[pos] = OP(p->strip[pos]) | value;
1412*9880d681SAndroid Build Coastguard Worker }
1413*9880d681SAndroid Build Coastguard Worker 
1414*9880d681SAndroid Build Coastguard Worker /*
1415*9880d681SAndroid Build Coastguard Worker  - enlarge - enlarge the strip
1416*9880d681SAndroid Build Coastguard Worker  */
1417*9880d681SAndroid Build Coastguard Worker static void
enlarge(struct parse * p,sopno size)1418*9880d681SAndroid Build Coastguard Worker enlarge(struct parse *p, sopno size)
1419*9880d681SAndroid Build Coastguard Worker {
1420*9880d681SAndroid Build Coastguard Worker 	sop *sp;
1421*9880d681SAndroid Build Coastguard Worker 
1422*9880d681SAndroid Build Coastguard Worker 	if (p->ssize >= size)
1423*9880d681SAndroid Build Coastguard Worker 		return;
1424*9880d681SAndroid Build Coastguard Worker 
1425*9880d681SAndroid Build Coastguard Worker 	if ((uintptr_t)size > SIZE_MAX / sizeof(sop)) {
1426*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ESPACE);
1427*9880d681SAndroid Build Coastguard Worker 		return;
1428*9880d681SAndroid Build Coastguard Worker 	}
1429*9880d681SAndroid Build Coastguard Worker 
1430*9880d681SAndroid Build Coastguard Worker 	sp = (sop *)realloc(p->strip, size*sizeof(sop));
1431*9880d681SAndroid Build Coastguard Worker 	if (sp == NULL) {
1432*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ESPACE);
1433*9880d681SAndroid Build Coastguard Worker 		return;
1434*9880d681SAndroid Build Coastguard Worker 	}
1435*9880d681SAndroid Build Coastguard Worker 	p->strip = sp;
1436*9880d681SAndroid Build Coastguard Worker 	p->ssize = size;
1437*9880d681SAndroid Build Coastguard Worker }
1438*9880d681SAndroid Build Coastguard Worker 
1439*9880d681SAndroid Build Coastguard Worker /*
1440*9880d681SAndroid Build Coastguard Worker  - stripsnug - compact the strip
1441*9880d681SAndroid Build Coastguard Worker  */
1442*9880d681SAndroid Build Coastguard Worker static void
stripsnug(struct parse * p,struct re_guts * g)1443*9880d681SAndroid Build Coastguard Worker stripsnug(struct parse *p, struct re_guts *g)
1444*9880d681SAndroid Build Coastguard Worker {
1445*9880d681SAndroid Build Coastguard Worker 	g->nstates = p->slen;
1446*9880d681SAndroid Build Coastguard Worker 	if ((uintptr_t)p->slen > SIZE_MAX / sizeof(sop)) {
1447*9880d681SAndroid Build Coastguard Worker 		g->strip = p->strip;
1448*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ESPACE);
1449*9880d681SAndroid Build Coastguard Worker 		return;
1450*9880d681SAndroid Build Coastguard Worker 	}
1451*9880d681SAndroid Build Coastguard Worker 
1452*9880d681SAndroid Build Coastguard Worker 	g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1453*9880d681SAndroid Build Coastguard Worker 	if (g->strip == NULL) {
1454*9880d681SAndroid Build Coastguard Worker 		SETERROR(REG_ESPACE);
1455*9880d681SAndroid Build Coastguard Worker 		g->strip = p->strip;
1456*9880d681SAndroid Build Coastguard Worker 	}
1457*9880d681SAndroid Build Coastguard Worker }
1458*9880d681SAndroid Build Coastguard Worker 
1459*9880d681SAndroid Build Coastguard Worker /*
1460*9880d681SAndroid Build Coastguard Worker  - findmust - fill in must and mlen with longest mandatory literal string
1461*9880d681SAndroid Build Coastguard Worker  *
1462*9880d681SAndroid Build Coastguard Worker  * This algorithm could do fancy things like analyzing the operands of |
1463*9880d681SAndroid Build Coastguard Worker  * for common subsequences.  Someday.  This code is simple and finds most
1464*9880d681SAndroid Build Coastguard Worker  * of the interesting cases.
1465*9880d681SAndroid Build Coastguard Worker  *
1466*9880d681SAndroid Build Coastguard Worker  * Note that must and mlen got initialized during setup.
1467*9880d681SAndroid Build Coastguard Worker  */
1468*9880d681SAndroid Build Coastguard Worker static void
findmust(struct parse * p,struct re_guts * g)1469*9880d681SAndroid Build Coastguard Worker findmust(struct parse *p, struct re_guts *g)
1470*9880d681SAndroid Build Coastguard Worker {
1471*9880d681SAndroid Build Coastguard Worker 	sop *scan;
1472*9880d681SAndroid Build Coastguard Worker 	sop *start = 0; /* start initialized in the default case, after that */
1473*9880d681SAndroid Build Coastguard Worker 	sop *newstart = 0; /* newstart was initialized in the OCHAR case */
1474*9880d681SAndroid Build Coastguard Worker 	sopno newlen;
1475*9880d681SAndroid Build Coastguard Worker 	sop s;
1476*9880d681SAndroid Build Coastguard Worker 	char *cp;
1477*9880d681SAndroid Build Coastguard Worker 	sopno i;
1478*9880d681SAndroid Build Coastguard Worker 
1479*9880d681SAndroid Build Coastguard Worker 	/* avoid making error situations worse */
1480*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)
1481*9880d681SAndroid Build Coastguard Worker 		return;
1482*9880d681SAndroid Build Coastguard Worker 
1483*9880d681SAndroid Build Coastguard Worker 	/* find the longest OCHAR sequence in strip */
1484*9880d681SAndroid Build Coastguard Worker 	newlen = 0;
1485*9880d681SAndroid Build Coastguard Worker 	scan = g->strip + 1;
1486*9880d681SAndroid Build Coastguard Worker 	do {
1487*9880d681SAndroid Build Coastguard Worker 		s = *scan++;
1488*9880d681SAndroid Build Coastguard Worker 		switch (OP(s)) {
1489*9880d681SAndroid Build Coastguard Worker 		case OCHAR:		/* sequence member */
1490*9880d681SAndroid Build Coastguard Worker 			if (newlen == 0)		/* new sequence */
1491*9880d681SAndroid Build Coastguard Worker 				newstart = scan - 1;
1492*9880d681SAndroid Build Coastguard Worker 			newlen++;
1493*9880d681SAndroid Build Coastguard Worker 			break;
1494*9880d681SAndroid Build Coastguard Worker 		case OPLUS_:		/* things that don't break one */
1495*9880d681SAndroid Build Coastguard Worker 		case OLPAREN:
1496*9880d681SAndroid Build Coastguard Worker 		case ORPAREN:
1497*9880d681SAndroid Build Coastguard Worker 			break;
1498*9880d681SAndroid Build Coastguard Worker 		case OQUEST_:		/* things that must be skipped */
1499*9880d681SAndroid Build Coastguard Worker 		case OCH_:
1500*9880d681SAndroid Build Coastguard Worker 			scan--;
1501*9880d681SAndroid Build Coastguard Worker 			do {
1502*9880d681SAndroid Build Coastguard Worker 				scan += OPND(s);
1503*9880d681SAndroid Build Coastguard Worker 				s = *scan;
1504*9880d681SAndroid Build Coastguard Worker 				/* assert() interferes w debug printouts */
1505*9880d681SAndroid Build Coastguard Worker 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
1506*9880d681SAndroid Build Coastguard Worker 							OP(s) != OOR2) {
1507*9880d681SAndroid Build Coastguard Worker 					g->iflags |= REGEX_BAD;
1508*9880d681SAndroid Build Coastguard Worker 					return;
1509*9880d681SAndroid Build Coastguard Worker 				}
1510*9880d681SAndroid Build Coastguard Worker 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
1511*9880d681SAndroid Build Coastguard Worker 			/* fallthrough */
1512*9880d681SAndroid Build Coastguard Worker 		default:		/* things that break a sequence */
1513*9880d681SAndroid Build Coastguard Worker 			if (newlen > g->mlen) {		/* ends one */
1514*9880d681SAndroid Build Coastguard Worker 				start = newstart;
1515*9880d681SAndroid Build Coastguard Worker 				g->mlen = newlen;
1516*9880d681SAndroid Build Coastguard Worker 			}
1517*9880d681SAndroid Build Coastguard Worker 			newlen = 0;
1518*9880d681SAndroid Build Coastguard Worker 			break;
1519*9880d681SAndroid Build Coastguard Worker 		}
1520*9880d681SAndroid Build Coastguard Worker 	} while (OP(s) != OEND);
1521*9880d681SAndroid Build Coastguard Worker 
1522*9880d681SAndroid Build Coastguard Worker 	if (g->mlen == 0)		/* there isn't one */
1523*9880d681SAndroid Build Coastguard Worker 		return;
1524*9880d681SAndroid Build Coastguard Worker 
1525*9880d681SAndroid Build Coastguard Worker 	/* turn it into a character string */
1526*9880d681SAndroid Build Coastguard Worker 	g->must = malloc((size_t)g->mlen + 1);
1527*9880d681SAndroid Build Coastguard Worker 	if (g->must == NULL) {		/* argh; just forget it */
1528*9880d681SAndroid Build Coastguard Worker 		g->mlen = 0;
1529*9880d681SAndroid Build Coastguard Worker 		return;
1530*9880d681SAndroid Build Coastguard Worker 	}
1531*9880d681SAndroid Build Coastguard Worker 	cp = g->must;
1532*9880d681SAndroid Build Coastguard Worker 	scan = start;
1533*9880d681SAndroid Build Coastguard Worker 	for (i = g->mlen; i > 0; i--) {
1534*9880d681SAndroid Build Coastguard Worker 		while (OP(s = *scan++) != OCHAR)
1535*9880d681SAndroid Build Coastguard Worker 			continue;
1536*9880d681SAndroid Build Coastguard Worker 		assert(cp < g->must + g->mlen);
1537*9880d681SAndroid Build Coastguard Worker 		*cp++ = (char)OPND(s);
1538*9880d681SAndroid Build Coastguard Worker 	}
1539*9880d681SAndroid Build Coastguard Worker 	assert(cp == g->must + g->mlen);
1540*9880d681SAndroid Build Coastguard Worker 	*cp++ = '\0';		/* just on general principles */
1541*9880d681SAndroid Build Coastguard Worker }
1542*9880d681SAndroid Build Coastguard Worker 
1543*9880d681SAndroid Build Coastguard Worker /*
1544*9880d681SAndroid Build Coastguard Worker  - pluscount - count + nesting
1545*9880d681SAndroid Build Coastguard Worker  */
1546*9880d681SAndroid Build Coastguard Worker static sopno			/* nesting depth */
pluscount(struct parse * p,struct re_guts * g)1547*9880d681SAndroid Build Coastguard Worker pluscount(struct parse *p, struct re_guts *g)
1548*9880d681SAndroid Build Coastguard Worker {
1549*9880d681SAndroid Build Coastguard Worker 	sop *scan;
1550*9880d681SAndroid Build Coastguard Worker 	sop s;
1551*9880d681SAndroid Build Coastguard Worker 	sopno plusnest = 0;
1552*9880d681SAndroid Build Coastguard Worker 	sopno maxnest = 0;
1553*9880d681SAndroid Build Coastguard Worker 
1554*9880d681SAndroid Build Coastguard Worker 	if (p->error != 0)
1555*9880d681SAndroid Build Coastguard Worker 		return(0);	/* there may not be an OEND */
1556*9880d681SAndroid Build Coastguard Worker 
1557*9880d681SAndroid Build Coastguard Worker 	scan = g->strip + 1;
1558*9880d681SAndroid Build Coastguard Worker 	do {
1559*9880d681SAndroid Build Coastguard Worker 		s = *scan++;
1560*9880d681SAndroid Build Coastguard Worker 		switch (OP(s)) {
1561*9880d681SAndroid Build Coastguard Worker 		case OPLUS_:
1562*9880d681SAndroid Build Coastguard Worker 			plusnest++;
1563*9880d681SAndroid Build Coastguard Worker 			break;
1564*9880d681SAndroid Build Coastguard Worker 		case O_PLUS:
1565*9880d681SAndroid Build Coastguard Worker 			if (plusnest > maxnest)
1566*9880d681SAndroid Build Coastguard Worker 				maxnest = plusnest;
1567*9880d681SAndroid Build Coastguard Worker 			plusnest--;
1568*9880d681SAndroid Build Coastguard Worker 			break;
1569*9880d681SAndroid Build Coastguard Worker 		}
1570*9880d681SAndroid Build Coastguard Worker 	} while (OP(s) != OEND);
1571*9880d681SAndroid Build Coastguard Worker 	if (plusnest != 0)
1572*9880d681SAndroid Build Coastguard Worker 		g->iflags |= REGEX_BAD;
1573*9880d681SAndroid Build Coastguard Worker 	return(maxnest);
1574*9880d681SAndroid Build Coastguard Worker }
1575