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