xref: /freebsd/lib/libc/regex/regcomp.c (revision 99429157e8615dc3b7f11afbe3ed92de7476a5db)
1 /*-
2  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3  * Copyright (c) 1992, 1993, 1994
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Copyright (c) 2011 The FreeBSD Foundation
7  * All rights reserved.
8  * Portions of this software were developed by David Chisnall
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Henry Spencer.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)regcomp.c	8.5 (Berkeley) 3/20/94
39  */
40 
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid[] = "@(#)regcomp.c	8.5 (Berkeley) 3/20/94";
43 #endif /* LIBC_SCCS and not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <sys/types.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <ctype.h>
51 #include <limits.h>
52 #include <stdlib.h>
53 #include <regex.h>
54 #include <wchar.h>
55 #include <wctype.h>
56 
57 #include "collate.h"
58 
59 #include "utils.h"
60 #include "regex2.h"
61 
62 #include "cname.h"
63 
64 /*
65  * parse structure, passed up and down to avoid global variables and
66  * other clumsinesses
67  */
68 struct parse {
69 	const char *next;	/* next character in RE */
70 	const char *end;	/* end of string (-> NUL normally) */
71 	int error;		/* has an error been seen? */
72 	sop *strip;		/* malloced strip */
73 	sopno ssize;		/* malloced strip size (allocated) */
74 	sopno slen;		/* malloced strip length (used) */
75 	int ncsalloc;		/* number of csets allocated */
76 	struct re_guts *g;
77 #	define	NPAREN	10	/* we need to remember () 1-9 for back refs */
78 	sopno pbegin[NPAREN];	/* -> ( ([0] unused) */
79 	sopno pend[NPAREN];	/* -> ) ([0] unused) */
80 };
81 
82 /* ========= begin header generated by ./mkh ========= */
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 /* === regcomp.c === */
88 static void p_ere(struct parse *p, int stop);
89 static void p_ere_exp(struct parse *p);
90 static void p_str(struct parse *p);
91 static void p_bre(struct parse *p, int end1, int end2);
92 static int p_simp_re(struct parse *p, int starordinary);
93 static int p_count(struct parse *p);
94 static void p_bracket(struct parse *p);
95 static void p_b_term(struct parse *p, cset *cs);
96 static void p_b_cclass(struct parse *p, cset *cs);
97 static void p_b_eclass(struct parse *p, cset *cs);
98 static wint_t p_b_symbol(struct parse *p);
99 static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
100 static wint_t othercase(wint_t ch);
101 static void bothcases(struct parse *p, wint_t ch);
102 static void ordinary(struct parse *p, wint_t ch);
103 static void nonnewline(struct parse *p);
104 static void repeat(struct parse *p, sopno start, int from, int to);
105 static int seterr(struct parse *p, int e);
106 static cset *allocset(struct parse *p);
107 static void freeset(struct parse *p, cset *cs);
108 static void CHadd(struct parse *p, cset *cs, wint_t ch);
109 static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max);
110 static void CHaddtype(struct parse *p, cset *cs, wctype_t wct);
111 static wint_t singleton(cset *cs);
112 static sopno dupl(struct parse *p, sopno start, sopno finish);
113 static void doemit(struct parse *p, sop op, size_t opnd);
114 static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
115 static void dofwd(struct parse *p, sopno pos, sop value);
116 static int enlarge(struct parse *p, sopno size);
117 static void stripsnug(struct parse *p, struct re_guts *g);
118 static void findmust(struct parse *p, struct re_guts *g);
119 static int altoffset(sop *scan, int offset);
120 static void computejumps(struct parse *p, struct re_guts *g);
121 static void computematchjumps(struct parse *p, struct re_guts *g);
122 static sopno pluscount(struct parse *p, struct re_guts *g);
123 static wint_t wgetnext(struct parse *p);
124 
125 #ifdef __cplusplus
126 }
127 #endif
128 /* ========= end header generated by ./mkh ========= */
129 
130 static char nuls[10];		/* place to point scanner in event of error */
131 
132 /*
133  * macros for use with parse structure
134  * BEWARE:  these know that the parse structure is named `p' !!!
135  */
136 #define	PEEK()	(*p->next)
137 #define	PEEK2()	(*(p->next+1))
138 #define	MORE()	(p->next < p->end)
139 #define	MORE2()	(p->next+1 < p->end)
140 #define	SEE(c)	(MORE() && PEEK() == (c))
141 #define	SEETWO(a, b)	(MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
142 #define	EAT(c)	((SEE(c)) ? (NEXT(), 1) : 0)
143 #define	EATTWO(a, b)	((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
144 #define	NEXT()	(p->next++)
145 #define	NEXT2()	(p->next += 2)
146 #define	NEXTn(n)	(p->next += (n))
147 #define	GETNEXT()	(*p->next++)
148 #define	WGETNEXT()	wgetnext(p)
149 #define	SETERROR(e)	seterr(p, (e))
150 #define	REQUIRE(co, e)	((co) || SETERROR(e))
151 #define	MUSTSEE(c, e)	(REQUIRE(MORE() && PEEK() == (c), e))
152 #define	MUSTEAT(c, e)	(REQUIRE(MORE() && GETNEXT() == (c), e))
153 #define	MUSTNOTSEE(c, e)	(REQUIRE(!MORE() || PEEK() != (c), e))
154 #define	EMIT(op, sopnd)	doemit(p, (sop)(op), (size_t)(sopnd))
155 #define	INSERT(op, pos)	doinsert(p, (sop)(op), HERE()-(pos)+1, pos)
156 #define	AHEAD(pos)		dofwd(p, pos, HERE()-(pos))
157 #define	ASTERN(sop, pos)	EMIT(sop, HERE()-pos)
158 #define	HERE()		(p->slen)
159 #define	THERE()		(p->slen - 1)
160 #define	THERETHERE()	(p->slen - 2)
161 #define	DROP(n)	(p->slen -= (n))
162 
163 #ifndef NDEBUG
164 static int never = 0;		/* for use in asserts; shuts lint up */
165 #else
166 #define	never	0		/* some <assert.h>s have bugs too */
167 #endif
168 
169 /* Macro used by computejump()/computematchjump() */
170 #define MIN(a,b)	((a)<(b)?(a):(b))
171 
172 /*
173  - regcomp - interface for parser and compilation
174  = extern int regcomp(regex_t *, const char *, int);
175  = #define	REG_BASIC	0000
176  = #define	REG_EXTENDED	0001
177  = #define	REG_ICASE	0002
178  = #define	REG_NOSUB	0004
179  = #define	REG_NEWLINE	0010
180  = #define	REG_NOSPEC	0020
181  = #define	REG_PEND	0040
182  = #define	REG_DUMP	0200
183  */
184 int				/* 0 success, otherwise REG_something */
185 regcomp(regex_t * __restrict preg,
186 	const char * __restrict pattern,
187 	int cflags)
188 {
189 	struct parse pa;
190 	struct re_guts *g;
191 	struct parse *p = &pa;
192 	int i;
193 	size_t len;
194 	size_t maxlen;
195 #ifdef REDEBUG
196 #	define	GOODFLAGS(f)	(f)
197 #else
198 #	define	GOODFLAGS(f)	((f)&~REG_DUMP)
199 #endif
200 
201 	cflags = GOODFLAGS(cflags);
202 	if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
203 		return(REG_INVARG);
204 
205 	if (cflags&REG_PEND) {
206 		if (preg->re_endp < pattern)
207 			return(REG_INVARG);
208 		len = preg->re_endp - pattern;
209 	} else
210 		len = strlen(pattern);
211 
212 	/* do the mallocs early so failure handling is easy */
213 	g = (struct re_guts *)malloc(sizeof(struct re_guts));
214 	if (g == NULL)
215 		return(REG_ESPACE);
216 	/*
217 	 * Limit the pattern space to avoid a 32-bit overflow on buffer
218 	 * extension.  Also avoid any signed overflow in case of conversion
219 	 * so make the real limit based on a 31-bit overflow.
220 	 *
221 	 * Likely not applicable on 64-bit systems but handle the case
222 	 * generically (who are we to stop people from using ~715MB+
223 	 * patterns?).
224 	 */
225 	maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3;
226 	if (len >= maxlen) {
227 		free((char *)g);
228 		return(REG_ESPACE);
229 	}
230 	p->ssize = len/(size_t)2*(size_t)3 + (size_t)1;	/* ugh */
231 	assert(p->ssize >= len);
232 
233 	p->strip = (sop *)malloc(p->ssize * sizeof(sop));
234 	p->slen = 0;
235 	if (p->strip == NULL) {
236 		free((char *)g);
237 		return(REG_ESPACE);
238 	}
239 
240 	/* set things up */
241 	p->g = g;
242 	p->next = pattern;	/* convenience; we do not modify it */
243 	p->end = p->next + len;
244 	p->error = 0;
245 	p->ncsalloc = 0;
246 	for (i = 0; i < NPAREN; i++) {
247 		p->pbegin[i] = 0;
248 		p->pend[i] = 0;
249 	}
250 	g->sets = NULL;
251 	g->ncsets = 0;
252 	g->cflags = cflags;
253 	g->iflags = 0;
254 	g->nbol = 0;
255 	g->neol = 0;
256 	g->must = NULL;
257 	g->moffset = -1;
258 	g->charjump = NULL;
259 	g->matchjump = NULL;
260 	g->mlen = 0;
261 	g->nsub = 0;
262 	g->backrefs = 0;
263 
264 	/* do it */
265 	EMIT(OEND, 0);
266 	g->firststate = THERE();
267 	if (cflags&REG_EXTENDED)
268 		p_ere(p, OUT);
269 	else if (cflags&REG_NOSPEC)
270 		p_str(p);
271 	else
272 		p_bre(p, OUT, OUT);
273 	EMIT(OEND, 0);
274 	g->laststate = THERE();
275 
276 	/* tidy up loose ends and fill things in */
277 	stripsnug(p, g);
278 	findmust(p, g);
279 	/* only use Boyer-Moore algorithm if the pattern is bigger
280 	 * than three characters
281 	 */
282 	if(g->mlen > 3) {
283 		computejumps(p, g);
284 		computematchjumps(p, g);
285 		if(g->matchjump == NULL && g->charjump != NULL) {
286 			free(g->charjump);
287 			g->charjump = NULL;
288 		}
289 	}
290 	g->nplus = pluscount(p, g);
291 	g->magic = MAGIC2;
292 	preg->re_nsub = g->nsub;
293 	preg->re_g = g;
294 	preg->re_magic = MAGIC1;
295 #ifndef REDEBUG
296 	/* not debugging, so can't rely on the assert() in regexec() */
297 	if (g->iflags&BAD)
298 		SETERROR(REG_ASSERT);
299 #endif
300 
301 	/* win or lose, we're done */
302 	if (p->error != 0)	/* lose */
303 		regfree(preg);
304 	return(p->error);
305 }
306 
307 /*
308  - p_ere - ERE parser top level, concatenation and alternation
309  == static void p_ere(struct parse *p, int_t stop);
310  */
311 static void
312 p_ere(struct parse *p,
313 	int stop)		/* character this ERE should end at */
314 {
315 	char c;
316 	sopno prevback;
317 	sopno prevfwd;
318 	sopno conc;
319 	int first = 1;		/* is this the first alternative? */
320 
321 	for (;;) {
322 		/* do a bunch of concatenated expressions */
323 		conc = HERE();
324 		while (MORE() && (c = PEEK()) != '|' && c != stop)
325 			p_ere_exp(p);
326 		(void)REQUIRE(HERE() != conc, REG_EMPTY);	/* require nonempty */
327 
328 		if (!EAT('|'))
329 			break;		/* NOTE BREAK OUT */
330 
331 		if (first) {
332 			INSERT(OCH_, conc);	/* offset is wrong */
333 			prevfwd = conc;
334 			prevback = conc;
335 			first = 0;
336 		}
337 		ASTERN(OOR1, prevback);
338 		prevback = THERE();
339 		AHEAD(prevfwd);			/* fix previous offset */
340 		prevfwd = HERE();
341 		EMIT(OOR2, 0);			/* offset is very wrong */
342 	}
343 
344 	if (!first) {		/* tail-end fixups */
345 		AHEAD(prevfwd);
346 		ASTERN(O_CH, prevback);
347 	}
348 
349 	assert(!MORE() || SEE(stop));
350 }
351 
352 /*
353  - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
354  == static void p_ere_exp(struct parse *p);
355  */
356 static void
357 p_ere_exp(struct parse *p)
358 {
359 	char c;
360 	wint_t wc;
361 	sopno pos;
362 	int count;
363 	int count2;
364 	sopno subno;
365 	int wascaret = 0;
366 
367 	assert(MORE());		/* caller should have ensured this */
368 	c = GETNEXT();
369 
370 	pos = HERE();
371 	switch (c) {
372 	case '(':
373 		(void)REQUIRE(MORE(), REG_EPAREN);
374 		p->g->nsub++;
375 		subno = p->g->nsub;
376 		if (subno < NPAREN)
377 			p->pbegin[subno] = HERE();
378 		EMIT(OLPAREN, subno);
379 		if (!SEE(')'))
380 			p_ere(p, ')');
381 		if (subno < NPAREN) {
382 			p->pend[subno] = HERE();
383 			assert(p->pend[subno] != 0);
384 		}
385 		EMIT(ORPAREN, subno);
386 		(void)MUSTEAT(')', REG_EPAREN);
387 		break;
388 #ifndef POSIX_MISTAKE
389 	case ')':		/* happens only if no current unmatched ( */
390 		/*
391 		 * You may ask, why the ifndef?  Because I didn't notice
392 		 * this until slightly too late for 1003.2, and none of the
393 		 * other 1003.2 regular-expression reviewers noticed it at
394 		 * all.  So an unmatched ) is legal POSIX, at least until
395 		 * we can get it fixed.
396 		 */
397 		SETERROR(REG_EPAREN);
398 		break;
399 #endif
400 	case '^':
401 		EMIT(OBOL, 0);
402 		p->g->iflags |= USEBOL;
403 		p->g->nbol++;
404 		wascaret = 1;
405 		break;
406 	case '$':
407 		EMIT(OEOL, 0);
408 		p->g->iflags |= USEEOL;
409 		p->g->neol++;
410 		break;
411 	case '|':
412 		SETERROR(REG_EMPTY);
413 		break;
414 	case '*':
415 	case '+':
416 	case '?':
417 		SETERROR(REG_BADRPT);
418 		break;
419 	case '.':
420 		if (p->g->cflags&REG_NEWLINE)
421 			nonnewline(p);
422 		else
423 			EMIT(OANY, 0);
424 		break;
425 	case '[':
426 		p_bracket(p);
427 		break;
428 	case '\\':
429 		(void)REQUIRE(MORE(), REG_EESCAPE);
430 		wc = WGETNEXT();
431 		switch (wc) {
432 		case '<':
433 			EMIT(OBOW, 0);
434 			break;
435 		case '>':
436 			EMIT(OEOW, 0);
437 			break;
438 		default:
439 			ordinary(p, wc);
440 			break;
441 		}
442 		break;
443 	case '{':		/* okay as ordinary except if digit follows */
444 		(void)REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT);
445 		/* FALLTHROUGH */
446 	default:
447 		if (p->error != 0)
448 			return;
449 		p->next--;
450 		wc = WGETNEXT();
451 		ordinary(p, wc);
452 		break;
453 	}
454 
455 	if (!MORE())
456 		return;
457 	c = PEEK();
458 	/* we call { a repetition if followed by a digit */
459 	if (!( c == '*' || c == '+' || c == '?' ||
460 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ))
461 		return;		/* no repetition, we're done */
462 	NEXT();
463 
464 	(void)REQUIRE(!wascaret, REG_BADRPT);
465 	switch (c) {
466 	case '*':	/* implemented as +? */
467 		/* this case does not require the (y|) trick, noKLUDGE */
468 		INSERT(OPLUS_, pos);
469 		ASTERN(O_PLUS, pos);
470 		INSERT(OQUEST_, pos);
471 		ASTERN(O_QUEST, pos);
472 		break;
473 	case '+':
474 		INSERT(OPLUS_, pos);
475 		ASTERN(O_PLUS, pos);
476 		break;
477 	case '?':
478 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
479 		INSERT(OCH_, pos);		/* offset slightly wrong */
480 		ASTERN(OOR1, pos);		/* this one's right */
481 		AHEAD(pos);			/* fix the OCH_ */
482 		EMIT(OOR2, 0);			/* offset very wrong... */
483 		AHEAD(THERE());			/* ...so fix it */
484 		ASTERN(O_CH, THERETHERE());
485 		break;
486 	case '{':
487 		count = p_count(p);
488 		if (EAT(',')) {
489 			if (isdigit((uch)PEEK())) {
490 				count2 = p_count(p);
491 				(void)REQUIRE(count <= count2, REG_BADBR);
492 			} else		/* single number with comma */
493 				count2 = INFINITY;
494 		} else		/* just a single number */
495 			count2 = count;
496 		repeat(p, pos, count, count2);
497 		if (!EAT('}')) {	/* error heuristics */
498 			while (MORE() && PEEK() != '}')
499 				NEXT();
500 			(void)REQUIRE(MORE(), REG_EBRACE);
501 			SETERROR(REG_BADBR);
502 		}
503 		break;
504 	}
505 
506 	if (!MORE())
507 		return;
508 	c = PEEK();
509 	if (!( c == '*' || c == '+' || c == '?' ||
510 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
511 		return;
512 	SETERROR(REG_BADRPT);
513 }
514 
515 /*
516  - p_str - string (no metacharacters) "parser"
517  == static void p_str(struct parse *p);
518  */
519 static void
520 p_str(struct parse *p)
521 {
522 	(void)REQUIRE(MORE(), REG_EMPTY);
523 	while (MORE())
524 		ordinary(p, WGETNEXT());
525 }
526 
527 /*
528  - p_bre - BRE parser top level, anchoring and concatenation
529  == static void p_bre(struct parse *p,  int end1, \
530  ==	int end2);
531  * Giving end1 as OUT essentially eliminates the end1/end2 check.
532  *
533  * This implementation is a bit of a kludge, in that a trailing $ is first
534  * taken as an ordinary character and then revised to be an anchor.
535  * The amount of lookahead needed to avoid this kludge is excessive.
536  */
537 static void
538 p_bre(struct parse *p,
539 	int end1,		/* first terminating character */
540 	int end2)		/* second terminating character */
541 {
542 	sopno start = HERE();
543 	int first = 1;			/* first subexpression? */
544 	int wasdollar = 0;
545 
546 	if (EAT('^')) {
547 		EMIT(OBOL, 0);
548 		p->g->iflags |= USEBOL;
549 		p->g->nbol++;
550 	}
551 	while (MORE() && !SEETWO(end1, end2)) {
552 		wasdollar = p_simp_re(p, first);
553 		first = 0;
554 	}
555 	if (wasdollar) {	/* oops, that was a trailing anchor */
556 		DROP(1);
557 		EMIT(OEOL, 0);
558 		p->g->iflags |= USEEOL;
559 		p->g->neol++;
560 	}
561 
562 	(void)REQUIRE(HERE() != start, REG_EMPTY);	/* require nonempty */
563 }
564 
565 /*
566  - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
567  == static int p_simp_re(struct parse *p, int starordinary);
568  */
569 static int			/* was the simple RE an unbackslashed $? */
570 p_simp_re(struct parse *p,
571 	int starordinary)	/* is a leading * an ordinary character? */
572 {
573 	int c;
574 	int count;
575 	int count2;
576 	sopno pos;
577 	int i;
578 	wint_t wc;
579 	sopno subno;
580 #	define	BACKSL	(1<<CHAR_BIT)
581 
582 	pos = HERE();		/* repetition op, if any, covers from here */
583 
584 	assert(MORE());		/* caller should have ensured this */
585 	c = GETNEXT();
586 	if (c == '\\') {
587 		(void)REQUIRE(MORE(), REG_EESCAPE);
588 		c = BACKSL | GETNEXT();
589 	}
590 	switch (c) {
591 	case '.':
592 		if (p->g->cflags&REG_NEWLINE)
593 			nonnewline(p);
594 		else
595 			EMIT(OANY, 0);
596 		break;
597 	case '[':
598 		p_bracket(p);
599 		break;
600 	case BACKSL|'<':
601 		EMIT(OBOW, 0);
602 		break;
603 	case BACKSL|'>':
604 		EMIT(OEOW, 0);
605 		break;
606 	case BACKSL|'{':
607 		SETERROR(REG_BADRPT);
608 		break;
609 	case BACKSL|'(':
610 		p->g->nsub++;
611 		subno = p->g->nsub;
612 		if (subno < NPAREN)
613 			p->pbegin[subno] = HERE();
614 		EMIT(OLPAREN, subno);
615 		/* the MORE here is an error heuristic */
616 		if (MORE() && !SEETWO('\\', ')'))
617 			p_bre(p, '\\', ')');
618 		if (subno < NPAREN) {
619 			p->pend[subno] = HERE();
620 			assert(p->pend[subno] != 0);
621 		}
622 		EMIT(ORPAREN, subno);
623 		(void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
624 		break;
625 	case BACKSL|')':	/* should not get here -- must be user */
626 	case BACKSL|'}':
627 		SETERROR(REG_EPAREN);
628 		break;
629 	case BACKSL|'1':
630 	case BACKSL|'2':
631 	case BACKSL|'3':
632 	case BACKSL|'4':
633 	case BACKSL|'5':
634 	case BACKSL|'6':
635 	case BACKSL|'7':
636 	case BACKSL|'8':
637 	case BACKSL|'9':
638 		i = (c&~BACKSL) - '0';
639 		assert(i < NPAREN);
640 		if (p->pend[i] != 0) {
641 			assert(i <= p->g->nsub);
642 			EMIT(OBACK_, i);
643 			assert(p->pbegin[i] != 0);
644 			assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
645 			assert(OP(p->strip[p->pend[i]]) == ORPAREN);
646 			(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
647 			EMIT(O_BACK, i);
648 		} else
649 			SETERROR(REG_ESUBREG);
650 		p->g->backrefs = 1;
651 		break;
652 	case '*':
653 		(void)REQUIRE(starordinary, REG_BADRPT);
654 		/* FALLTHROUGH */
655 	default:
656 		if (p->error != 0)
657 			return(0);	/* Definitely not $... */
658 		p->next--;
659 		wc = WGETNEXT();
660 		ordinary(p, wc);
661 		break;
662 	}
663 
664 	if (EAT('*')) {		/* implemented as +? */
665 		/* this case does not require the (y|) trick, noKLUDGE */
666 		INSERT(OPLUS_, pos);
667 		ASTERN(O_PLUS, pos);
668 		INSERT(OQUEST_, pos);
669 		ASTERN(O_QUEST, pos);
670 	} else if (EATTWO('\\', '{')) {
671 		count = p_count(p);
672 		if (EAT(',')) {
673 			if (MORE() && isdigit((uch)PEEK())) {
674 				count2 = p_count(p);
675 				(void)REQUIRE(count <= count2, REG_BADBR);
676 			} else		/* single number with comma */
677 				count2 = INFINITY;
678 		} else		/* just a single number */
679 			count2 = count;
680 		repeat(p, pos, count, count2);
681 		if (!EATTWO('\\', '}')) {	/* error heuristics */
682 			while (MORE() && !SEETWO('\\', '}'))
683 				NEXT();
684 			(void)REQUIRE(MORE(), REG_EBRACE);
685 			SETERROR(REG_BADBR);
686 		}
687 	} else if (c == '$')     /* $ (but not \$) ends it */
688 		return(1);
689 
690 	return(0);
691 }
692 
693 /*
694  - p_count - parse a repetition count
695  == static int p_count(struct parse *p);
696  */
697 static int			/* the value */
698 p_count(struct parse *p)
699 {
700 	int count = 0;
701 	int ndigits = 0;
702 
703 	while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
704 		count = count*10 + (GETNEXT() - '0');
705 		ndigits++;
706 	}
707 
708 	(void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
709 	return(count);
710 }
711 
712 /*
713  - p_bracket - parse a bracketed character list
714  == static void p_bracket(struct parse *p);
715  */
716 static void
717 p_bracket(struct parse *p)
718 {
719 	cset *cs;
720 	wint_t ch;
721 
722 	/* Dept of Truly Sickening Special-Case Kludges */
723 	if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
724 		EMIT(OBOW, 0);
725 		NEXTn(6);
726 		return;
727 	}
728 	if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
729 		EMIT(OEOW, 0);
730 		NEXTn(6);
731 		return;
732 	}
733 
734 	if ((cs = allocset(p)) == NULL)
735 		return;
736 
737 	if (p->g->cflags&REG_ICASE)
738 		cs->icase = 1;
739 	if (EAT('^'))
740 		cs->invert = 1;
741 	if (EAT(']'))
742 		CHadd(p, cs, ']');
743 	else if (EAT('-'))
744 		CHadd(p, cs, '-');
745 	while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
746 		p_b_term(p, cs);
747 	if (EAT('-'))
748 		CHadd(p, cs, '-');
749 	(void)MUSTEAT(']', REG_EBRACK);
750 
751 	if (p->error != 0)	/* don't mess things up further */
752 		return;
753 
754 	if (cs->invert && p->g->cflags&REG_NEWLINE)
755 		cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
756 
757 	if ((ch = singleton(cs)) != OUT) {	/* optimize singleton sets */
758 		ordinary(p, ch);
759 		freeset(p, cs);
760 	} else
761 		EMIT(OANYOF, (int)(cs - p->g->sets));
762 }
763 
764 /*
765  - p_b_term - parse one term of a bracketed character list
766  == static void p_b_term(struct parse *p, cset *cs);
767  */
768 static void
769 p_b_term(struct parse *p, cset *cs)
770 {
771 	char c;
772 	wint_t start, finish;
773 	wint_t i;
774 	struct xlocale_collate *table =
775 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
776 
777 	/* classify what we've got */
778 	switch ((MORE()) ? PEEK() : '\0') {
779 	case '[':
780 		c = (MORE2()) ? PEEK2() : '\0';
781 		break;
782 	case '-':
783 		SETERROR(REG_ERANGE);
784 		return;			/* NOTE RETURN */
785 	default:
786 		c = '\0';
787 		break;
788 	}
789 
790 	switch (c) {
791 	case ':':		/* character class */
792 		NEXT2();
793 		(void)REQUIRE(MORE(), REG_EBRACK);
794 		c = PEEK();
795 		(void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
796 		p_b_cclass(p, cs);
797 		(void)REQUIRE(MORE(), REG_EBRACK);
798 		(void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
799 		break;
800 	case '=':		/* equivalence class */
801 		NEXT2();
802 		(void)REQUIRE(MORE(), REG_EBRACK);
803 		c = PEEK();
804 		(void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
805 		p_b_eclass(p, cs);
806 		(void)REQUIRE(MORE(), REG_EBRACK);
807 		(void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
808 		break;
809 	default:		/* symbol, ordinary character, or range */
810 		start = p_b_symbol(p);
811 		if (SEE('-') && MORE2() && PEEK2() != ']') {
812 			/* range */
813 			NEXT();
814 			if (EAT('-'))
815 				finish = '-';
816 			else
817 				finish = p_b_symbol(p);
818 		} else
819 			finish = start;
820 		if (start == finish)
821 			CHadd(p, cs, start);
822 		else {
823 			if (table->__collate_load_error || MB_CUR_MAX > 1) {
824 				(void)REQUIRE(start <= finish, REG_ERANGE);
825 				CHaddrange(p, cs, start, finish);
826 			} else {
827 				(void)REQUIRE(__wcollate_range_cmp(start, finish) <= 0, REG_ERANGE);
828 				for (i = 0; i <= UCHAR_MAX; i++) {
829 					if (   __wcollate_range_cmp(start, i) <= 0
830 					    && __wcollate_range_cmp(i, finish) <= 0
831 					   )
832 						CHadd(p, cs, i);
833 				}
834 			}
835 		}
836 		break;
837 	}
838 }
839 
840 /*
841  - p_b_cclass - parse a character-class name and deal with it
842  == static void p_b_cclass(struct parse *p, cset *cs);
843  */
844 static void
845 p_b_cclass(struct parse *p, cset *cs)
846 {
847 	const char *sp = p->next;
848 	size_t len;
849 	wctype_t wct;
850 	char clname[16];
851 
852 	while (MORE() && isalpha((uch)PEEK()))
853 		NEXT();
854 	len = p->next - sp;
855 	if (len >= sizeof(clname) - 1) {
856 		SETERROR(REG_ECTYPE);
857 		return;
858 	}
859 	memcpy(clname, sp, len);
860 	clname[len] = '\0';
861 	if ((wct = wctype(clname)) == 0) {
862 		SETERROR(REG_ECTYPE);
863 		return;
864 	}
865 	CHaddtype(p, cs, wct);
866 }
867 
868 /*
869  - p_b_eclass - parse an equivalence-class name and deal with it
870  == static void p_b_eclass(struct parse *p, cset *cs);
871  *
872  * This implementation is incomplete. xxx
873  */
874 static void
875 p_b_eclass(struct parse *p, cset *cs)
876 {
877 	wint_t c;
878 
879 	c = p_b_coll_elem(p, '=');
880 	CHadd(p, cs, c);
881 }
882 
883 /*
884  - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
885  == static wint_t p_b_symbol(struct parse *p);
886  */
887 static wint_t			/* value of symbol */
888 p_b_symbol(struct parse *p)
889 {
890 	wint_t value;
891 
892 	(void)REQUIRE(MORE(), REG_EBRACK);
893 	if (!EATTWO('[', '.'))
894 		return(WGETNEXT());
895 
896 	/* collating symbol */
897 	value = p_b_coll_elem(p, '.');
898 	(void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
899 	return(value);
900 }
901 
902 /*
903  - p_b_coll_elem - parse a collating-element name and look it up
904  == static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
905  */
906 static wint_t			/* value of collating element */
907 p_b_coll_elem(struct parse *p,
908 	wint_t endc)		/* name ended by endc,']' */
909 {
910 	const char *sp = p->next;
911 	struct cname *cp;
912 	mbstate_t mbs;
913 	wchar_t wc;
914 	size_t clen, len;
915 
916 	while (MORE() && !SEETWO(endc, ']'))
917 		NEXT();
918 	if (!MORE()) {
919 		SETERROR(REG_EBRACK);
920 		return(0);
921 	}
922 	len = p->next - sp;
923 	for (cp = cnames; cp->name != NULL; cp++)
924 		if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
925 			return(cp->code);	/* known name */
926 	memset(&mbs, 0, sizeof(mbs));
927 	if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
928 		return (wc);			/* single character */
929 	else if (clen == (size_t)-1 || clen == (size_t)-2)
930 		SETERROR(REG_ILLSEQ);
931 	else
932 		SETERROR(REG_ECOLLATE);		/* neither */
933 	return(0);
934 }
935 
936 /*
937  - othercase - return the case counterpart of an alphabetic
938  == static wint_t othercase(wint_t ch);
939  */
940 static wint_t			/* if no counterpart, return ch */
941 othercase(wint_t ch)
942 {
943 	assert(iswalpha(ch));
944 	if (iswupper(ch))
945 		return(towlower(ch));
946 	else if (iswlower(ch))
947 		return(towupper(ch));
948 	else			/* peculiar, but could happen */
949 		return(ch);
950 }
951 
952 /*
953  - bothcases - emit a dualcase version of a two-case character
954  == static void bothcases(struct parse *p, wint_t ch);
955  *
956  * Boy, is this implementation ever a kludge...
957  */
958 static void
959 bothcases(struct parse *p, wint_t ch)
960 {
961 	const char *oldnext = p->next;
962 	const char *oldend = p->end;
963 	char bracket[3 + MB_LEN_MAX];
964 	size_t n;
965 	mbstate_t mbs;
966 
967 	assert(othercase(ch) != ch);	/* p_bracket() would recurse */
968 	p->next = bracket;
969 	memset(&mbs, 0, sizeof(mbs));
970 	n = wcrtomb(bracket, ch, &mbs);
971 	assert(n != (size_t)-1);
972 	bracket[n] = ']';
973 	bracket[n + 1] = '\0';
974 	p->end = bracket+n+1;
975 	p_bracket(p);
976 	assert(p->next == p->end);
977 	p->next = oldnext;
978 	p->end = oldend;
979 }
980 
981 /*
982  - ordinary - emit an ordinary character
983  == static void ordinary(struct parse *p, wint_t ch);
984  */
985 static void
986 ordinary(struct parse *p, wint_t ch)
987 {
988 	cset *cs;
989 
990 	if ((p->g->cflags&REG_ICASE) && iswalpha(ch) && othercase(ch) != ch)
991 		bothcases(p, ch);
992 	else if ((ch & OPDMASK) == ch)
993 		EMIT(OCHAR, ch);
994 	else {
995 		/*
996 		 * Kludge: character is too big to fit into an OCHAR operand.
997 		 * Emit a singleton set.
998 		 */
999 		if ((cs = allocset(p)) == NULL)
1000 			return;
1001 		CHadd(p, cs, ch);
1002 		EMIT(OANYOF, (int)(cs - p->g->sets));
1003 	}
1004 }
1005 
1006 /*
1007  - nonnewline - emit REG_NEWLINE version of OANY
1008  == static void nonnewline(struct parse *p);
1009  *
1010  * Boy, is this implementation ever a kludge...
1011  */
1012 static void
1013 nonnewline(struct parse *p)
1014 {
1015 	const char *oldnext = p->next;
1016 	const char *oldend = p->end;
1017 	char bracket[4];
1018 
1019 	p->next = bracket;
1020 	p->end = bracket+3;
1021 	bracket[0] = '^';
1022 	bracket[1] = '\n';
1023 	bracket[2] = ']';
1024 	bracket[3] = '\0';
1025 	p_bracket(p);
1026 	assert(p->next == bracket+3);
1027 	p->next = oldnext;
1028 	p->end = oldend;
1029 }
1030 
1031 /*
1032  - repeat - generate code for a bounded repetition, recursively if needed
1033  == static void repeat(struct parse *p, sopno start, int from, int to);
1034  */
1035 static void
1036 repeat(struct parse *p,
1037 	sopno start,		/* operand from here to end of strip */
1038 	int from,		/* repeated from this number */
1039 	int to)			/* to this number of times (maybe INFINITY) */
1040 {
1041 	sopno finish = HERE();
1042 #	define	N	2
1043 #	define	INF	3
1044 #	define	REP(f, t)	((f)*8 + (t))
1045 #	define	MAP(n)	(((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
1046 	sopno copy;
1047 
1048 	if (p->error != 0)	/* head off possible runaway recursion */
1049 		return;
1050 
1051 	assert(from <= to);
1052 
1053 	switch (REP(MAP(from), MAP(to))) {
1054 	case REP(0, 0):			/* must be user doing this */
1055 		DROP(finish-start);	/* drop the operand */
1056 		break;
1057 	case REP(0, 1):			/* as x{1,1}? */
1058 	case REP(0, N):			/* as x{1,n}? */
1059 	case REP(0, INF):		/* as x{1,}? */
1060 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1061 		INSERT(OCH_, start);		/* offset is wrong... */
1062 		repeat(p, start+1, 1, to);
1063 		ASTERN(OOR1, start);
1064 		AHEAD(start);			/* ... fix it */
1065 		EMIT(OOR2, 0);
1066 		AHEAD(THERE());
1067 		ASTERN(O_CH, THERETHERE());
1068 		break;
1069 	case REP(1, 1):			/* trivial case */
1070 		/* done */
1071 		break;
1072 	case REP(1, N):			/* as x?x{1,n-1} */
1073 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1074 		INSERT(OCH_, start);
1075 		ASTERN(OOR1, start);
1076 		AHEAD(start);
1077 		EMIT(OOR2, 0);			/* offset very wrong... */
1078 		AHEAD(THERE());			/* ...so fix it */
1079 		ASTERN(O_CH, THERETHERE());
1080 		copy = dupl(p, start+1, finish+1);
1081 		assert(copy == finish+4);
1082 		repeat(p, copy, 1, to-1);
1083 		break;
1084 	case REP(1, INF):		/* as x+ */
1085 		INSERT(OPLUS_, start);
1086 		ASTERN(O_PLUS, start);
1087 		break;
1088 	case REP(N, N):			/* as xx{m-1,n-1} */
1089 		copy = dupl(p, start, finish);
1090 		repeat(p, copy, from-1, to-1);
1091 		break;
1092 	case REP(N, INF):		/* as xx{n-1,INF} */
1093 		copy = dupl(p, start, finish);
1094 		repeat(p, copy, from-1, to);
1095 		break;
1096 	default:			/* "can't happen" */
1097 		SETERROR(REG_ASSERT);	/* just in case */
1098 		break;
1099 	}
1100 }
1101 
1102 /*
1103  - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
1104  - character from the parse struct, signals a REG_ILLSEQ error if the
1105  - character can't be converted. Returns the number of bytes consumed.
1106  */
1107 static wint_t
1108 wgetnext(struct parse *p)
1109 {
1110 	mbstate_t mbs;
1111 	wchar_t wc;
1112 	size_t n;
1113 
1114 	memset(&mbs, 0, sizeof(mbs));
1115 	n = mbrtowc(&wc, p->next, p->end - p->next, &mbs);
1116 	if (n == (size_t)-1 || n == (size_t)-2) {
1117 		SETERROR(REG_ILLSEQ);
1118 		return (0);
1119 	}
1120 	if (n == 0)
1121 		n = 1;
1122 	p->next += n;
1123 	return (wc);
1124 }
1125 
1126 /*
1127  - seterr - set an error condition
1128  == static int seterr(struct parse *p, int e);
1129  */
1130 static int			/* useless but makes type checking happy */
1131 seterr(struct parse *p, int e)
1132 {
1133 	if (p->error == 0)	/* keep earliest error condition */
1134 		p->error = e;
1135 	p->next = nuls;		/* try to bring things to a halt */
1136 	p->end = nuls;
1137 	return(0);		/* make the return value well-defined */
1138 }
1139 
1140 /*
1141  - allocset - allocate a set of characters for []
1142  == static cset *allocset(struct parse *p);
1143  */
1144 static cset *
1145 allocset(struct parse *p)
1146 {
1147 	cset *cs, *ncs;
1148 
1149 	ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs));
1150 	if (ncs == NULL) {
1151 		SETERROR(REG_ESPACE);
1152 		return (NULL);
1153 	}
1154 	p->g->sets = ncs;
1155 	cs = &p->g->sets[p->g->ncsets++];
1156 	memset(cs, 0, sizeof(*cs));
1157 
1158 	return(cs);
1159 }
1160 
1161 /*
1162  - freeset - free a now-unused set
1163  == static void freeset(struct parse *p, cset *cs);
1164  */
1165 static void
1166 freeset(struct parse *p, cset *cs)
1167 {
1168 	cset *top = &p->g->sets[p->g->ncsets];
1169 
1170 	free(cs->wides);
1171 	free(cs->ranges);
1172 	free(cs->types);
1173 	memset(cs, 0, sizeof(*cs));
1174 	if (cs == top-1)	/* recover only the easy case */
1175 		p->g->ncsets--;
1176 }
1177 
1178 /*
1179  - singleton - Determine whether a set contains only one character,
1180  - returning it if so, otherwise returning OUT.
1181  */
1182 static wint_t
1183 singleton(cset *cs)
1184 {
1185 	wint_t i, s, n;
1186 
1187 	for (i = n = 0; i < NC; i++)
1188 		if (CHIN(cs, i)) {
1189 			n++;
1190 			s = i;
1191 		}
1192 	if (n == 1)
1193 		return (s);
1194 	if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 &&
1195 	    cs->icase == 0)
1196 		return (cs->wides[0]);
1197 	/* Don't bother handling the other cases. */
1198 	return (OUT);
1199 }
1200 
1201 /*
1202  - CHadd - add character to character set.
1203  */
1204 static void
1205 CHadd(struct parse *p, cset *cs, wint_t ch)
1206 {
1207 	wint_t nch, *newwides;
1208 	assert(ch >= 0);
1209 	if (ch < NC)
1210 		cs->bmp[ch >> 3] |= 1 << (ch & 7);
1211 	else {
1212 		newwides = reallocarray(cs->wides, cs->nwides + 1,
1213 		    sizeof(*cs->wides));
1214 		if (newwides == NULL) {
1215 			SETERROR(REG_ESPACE);
1216 			return;
1217 		}
1218 		cs->wides = newwides;
1219 		cs->wides[cs->nwides++] = ch;
1220 	}
1221 	if (cs->icase) {
1222 		if ((nch = towlower(ch)) < NC)
1223 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
1224 		if ((nch = towupper(ch)) < NC)
1225 			cs->bmp[nch >> 3] |= 1 << (nch & 7);
1226 	}
1227 }
1228 
1229 /*
1230  - CHaddrange - add all characters in the range [min,max] to a character set.
1231  */
1232 static void
1233 CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max)
1234 {
1235 	crange *newranges;
1236 
1237 	for (; min < NC && min <= max; min++)
1238 		CHadd(p, cs, min);
1239 	if (min >= max)
1240 		return;
1241 	newranges = reallocarray(cs->ranges, cs->nranges + 1,
1242 	    sizeof(*cs->ranges));
1243 	if (newranges == NULL) {
1244 		SETERROR(REG_ESPACE);
1245 		return;
1246 	}
1247 	cs->ranges = newranges;
1248 	cs->ranges[cs->nranges].min = min;
1249 	cs->ranges[cs->nranges].max = max;
1250 	cs->nranges++;
1251 }
1252 
1253 /*
1254  - CHaddtype - add all characters of a certain type to a character set.
1255  */
1256 static void
1257 CHaddtype(struct parse *p, cset *cs, wctype_t wct)
1258 {
1259 	wint_t i;
1260 	wctype_t *newtypes;
1261 
1262 	for (i = 0; i < NC; i++)
1263 		if (iswctype(i, wct))
1264 			CHadd(p, cs, i);
1265 	newtypes = reallocarray(cs->types, cs->ntypes + 1,
1266 	    sizeof(*cs->types));
1267 	if (newtypes == NULL) {
1268 		SETERROR(REG_ESPACE);
1269 		return;
1270 	}
1271 	cs->types = newtypes;
1272 	cs->types[cs->ntypes++] = wct;
1273 }
1274 
1275 /*
1276  - dupl - emit a duplicate of a bunch of sops
1277  == static sopno dupl(struct parse *p, sopno start, sopno finish);
1278  */
1279 static sopno			/* start of duplicate */
1280 dupl(struct parse *p,
1281 	sopno start,		/* from here */
1282 	sopno finish)		/* to this less one */
1283 {
1284 	sopno ret = HERE();
1285 	sopno len = finish - start;
1286 
1287 	assert(finish >= start);
1288 	if (len == 0)
1289 		return(ret);
1290 	if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */
1291 		return(ret);
1292 	(void) memcpy((char *)(p->strip + p->slen),
1293 		(char *)(p->strip + start), (size_t)len*sizeof(sop));
1294 	p->slen += len;
1295 	return(ret);
1296 }
1297 
1298 /*
1299  - doemit - emit a strip operator
1300  == static void doemit(struct parse *p, sop op, size_t opnd);
1301  *
1302  * It might seem better to implement this as a macro with a function as
1303  * hard-case backup, but it's just too big and messy unless there are
1304  * some changes to the data structures.  Maybe later.
1305  */
1306 static void
1307 doemit(struct parse *p, sop op, size_t opnd)
1308 {
1309 	/* avoid making error situations worse */
1310 	if (p->error != 0)
1311 		return;
1312 
1313 	/* deal with oversize operands ("can't happen", more or less) */
1314 	assert(opnd < 1<<OPSHIFT);
1315 
1316 	/* deal with undersized strip */
1317 	if (p->slen >= p->ssize)
1318 		if (!enlarge(p, (p->ssize+1) / 2 * 3))	/* +50% */
1319 			return;
1320 
1321 	/* finally, it's all reduced to the easy case */
1322 	p->strip[p->slen++] = SOP(op, opnd);
1323 }
1324 
1325 /*
1326  - doinsert - insert a sop into the strip
1327  == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
1328  */
1329 static void
1330 doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1331 {
1332 	sopno sn;
1333 	sop s;
1334 	int i;
1335 
1336 	/* avoid making error situations worse */
1337 	if (p->error != 0)
1338 		return;
1339 
1340 	sn = HERE();
1341 	EMIT(op, opnd);		/* do checks, ensure space */
1342 	assert(HERE() == sn+1);
1343 	s = p->strip[sn];
1344 
1345 	/* adjust paren pointers */
1346 	assert(pos > 0);
1347 	for (i = 1; i < NPAREN; i++) {
1348 		if (p->pbegin[i] >= pos) {
1349 			p->pbegin[i]++;
1350 		}
1351 		if (p->pend[i] >= pos) {
1352 			p->pend[i]++;
1353 		}
1354 	}
1355 
1356 	memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1357 						(HERE()-pos-1)*sizeof(sop));
1358 	p->strip[pos] = s;
1359 }
1360 
1361 /*
1362  - dofwd - complete a forward reference
1363  == static void dofwd(struct parse *p, sopno pos, sop value);
1364  */
1365 static void
1366 dofwd(struct parse *p, sopno pos, sop value)
1367 {
1368 	/* avoid making error situations worse */
1369 	if (p->error != 0)
1370 		return;
1371 
1372 	assert(value < 1<<OPSHIFT);
1373 	p->strip[pos] = OP(p->strip[pos]) | value;
1374 }
1375 
1376 /*
1377  - enlarge - enlarge the strip
1378  == static int enlarge(struct parse *p, sopno size);
1379  */
1380 static int
1381 enlarge(struct parse *p, sopno size)
1382 {
1383 	sop *sp;
1384 
1385 	if (p->ssize >= size)
1386 		return 1;
1387 
1388 	sp = reallocarray(p->strip, size, sizeof(sop));
1389 	if (sp == NULL) {
1390 		SETERROR(REG_ESPACE);
1391 		return 0;
1392 	}
1393 	p->strip = sp;
1394 	p->ssize = size;
1395 	return 1;
1396 }
1397 
1398 /*
1399  - stripsnug - compact the strip
1400  == static void stripsnug(struct parse *p, struct re_guts *g);
1401  */
1402 static void
1403 stripsnug(struct parse *p, struct re_guts *g)
1404 {
1405 	g->nstates = p->slen;
1406 	g->strip = reallocarray((char *)p->strip, p->slen, sizeof(sop));
1407 	if (g->strip == NULL) {
1408 		SETERROR(REG_ESPACE);
1409 		g->strip = p->strip;
1410 	}
1411 }
1412 
1413 /*
1414  - findmust - fill in must and mlen with longest mandatory literal string
1415  == static void findmust(struct parse *p, struct re_guts *g);
1416  *
1417  * This algorithm could do fancy things like analyzing the operands of |
1418  * for common subsequences.  Someday.  This code is simple and finds most
1419  * of the interesting cases.
1420  *
1421  * Note that must and mlen got initialized during setup.
1422  */
1423 static void
1424 findmust(struct parse *p, struct re_guts *g)
1425 {
1426 	sop *scan;
1427 	sop *start = NULL;
1428 	sop *newstart = NULL;
1429 	sopno newlen;
1430 	sop s;
1431 	char *cp;
1432 	int offset;
1433 	char buf[MB_LEN_MAX];
1434 	size_t clen;
1435 	mbstate_t mbs;
1436 
1437 	/* avoid making error situations worse */
1438 	if (p->error != 0)
1439 		return;
1440 
1441 	/*
1442 	 * It's not generally safe to do a ``char'' substring search on
1443 	 * multibyte character strings, but it's safe for at least
1444 	 * UTF-8 (see RFC 3629).
1445 	 */
1446 	if (MB_CUR_MAX > 1 &&
1447 	    strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
1448 		return;
1449 
1450 	/* find the longest OCHAR sequence in strip */
1451 	newlen = 0;
1452 	offset = 0;
1453 	g->moffset = 0;
1454 	scan = g->strip + 1;
1455 	do {
1456 		s = *scan++;
1457 		switch (OP(s)) {
1458 		case OCHAR:		/* sequence member */
1459 			if (newlen == 0) {		/* new sequence */
1460 				memset(&mbs, 0, sizeof(mbs));
1461 				newstart = scan - 1;
1462 			}
1463 			clen = wcrtomb(buf, OPND(s), &mbs);
1464 			if (clen == (size_t)-1)
1465 				goto toohard;
1466 			newlen += clen;
1467 			break;
1468 		case OPLUS_:		/* things that don't break one */
1469 		case OLPAREN:
1470 		case ORPAREN:
1471 			break;
1472 		case OQUEST_:		/* things that must be skipped */
1473 		case OCH_:
1474 			offset = altoffset(scan, offset);
1475 			scan--;
1476 			do {
1477 				scan += OPND(s);
1478 				s = *scan;
1479 				/* assert() interferes w debug printouts */
1480 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
1481 							OP(s) != OOR2) {
1482 					g->iflags |= BAD;
1483 					return;
1484 				}
1485 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
1486 			/* FALLTHROUGH */
1487 		case OBOW:		/* things that break a sequence */
1488 		case OEOW:
1489 		case OBOL:
1490 		case OEOL:
1491 		case O_QUEST:
1492 		case O_CH:
1493 		case OEND:
1494 			if (newlen > g->mlen) {		/* ends one */
1495 				start = newstart;
1496 				g->mlen = newlen;
1497 				if (offset > -1) {
1498 					g->moffset += offset;
1499 					offset = newlen;
1500 				} else
1501 					g->moffset = offset;
1502 			} else {
1503 				if (offset > -1)
1504 					offset += newlen;
1505 			}
1506 			newlen = 0;
1507 			break;
1508 		case OANY:
1509 			if (newlen > g->mlen) {		/* ends one */
1510 				start = newstart;
1511 				g->mlen = newlen;
1512 				if (offset > -1) {
1513 					g->moffset += offset;
1514 					offset = newlen;
1515 				} else
1516 					g->moffset = offset;
1517 			} else {
1518 				if (offset > -1)
1519 					offset += newlen;
1520 			}
1521 			if (offset > -1)
1522 				offset++;
1523 			newlen = 0;
1524 			break;
1525 		case OANYOF:		/* may or may not invalidate offset */
1526 			/* First, everything as OANY */
1527 			if (newlen > g->mlen) {		/* ends one */
1528 				start = newstart;
1529 				g->mlen = newlen;
1530 				if (offset > -1) {
1531 					g->moffset += offset;
1532 					offset = newlen;
1533 				} else
1534 					g->moffset = offset;
1535 			} else {
1536 				if (offset > -1)
1537 					offset += newlen;
1538 			}
1539 			if (offset > -1)
1540 				offset++;
1541 			newlen = 0;
1542 			break;
1543 		toohard:
1544 		default:
1545 			/* Anything here makes it impossible or too hard
1546 			 * to calculate the offset -- so we give up;
1547 			 * save the last known good offset, in case the
1548 			 * must sequence doesn't occur later.
1549 			 */
1550 			if (newlen > g->mlen) {		/* ends one */
1551 				start = newstart;
1552 				g->mlen = newlen;
1553 				if (offset > -1)
1554 					g->moffset += offset;
1555 				else
1556 					g->moffset = offset;
1557 			}
1558 			offset = -1;
1559 			newlen = 0;
1560 			break;
1561 		}
1562 	} while (OP(s) != OEND);
1563 
1564 	if (g->mlen == 0) {		/* there isn't one */
1565 		g->moffset = -1;
1566 		return;
1567 	}
1568 
1569 	/* turn it into a character string */
1570 	g->must = malloc((size_t)g->mlen + 1);
1571 	if (g->must == NULL) {		/* argh; just forget it */
1572 		g->mlen = 0;
1573 		g->moffset = -1;
1574 		return;
1575 	}
1576 	cp = g->must;
1577 	scan = start;
1578 	memset(&mbs, 0, sizeof(mbs));
1579 	while (cp < g->must + g->mlen) {
1580 		while (OP(s = *scan++) != OCHAR)
1581 			continue;
1582 		clen = wcrtomb(cp, OPND(s), &mbs);
1583 		assert(clen != (size_t)-1);
1584 		cp += clen;
1585 	}
1586 	assert(cp == g->must + g->mlen);
1587 	*cp++ = '\0';		/* just on general principles */
1588 }
1589 
1590 /*
1591  - altoffset - choose biggest offset among multiple choices
1592  == static int altoffset(sop *scan, int offset);
1593  *
1594  * Compute, recursively if necessary, the largest offset among multiple
1595  * re paths.
1596  */
1597 static int
1598 altoffset(sop *scan, int offset)
1599 {
1600 	int largest;
1601 	int try;
1602 	sop s;
1603 
1604 	/* If we gave up already on offsets, return */
1605 	if (offset == -1)
1606 		return -1;
1607 
1608 	largest = 0;
1609 	try = 0;
1610 	s = *scan++;
1611 	while (OP(s) != O_QUEST && OP(s) != O_CH) {
1612 		switch (OP(s)) {
1613 		case OOR1:
1614 			if (try > largest)
1615 				largest = try;
1616 			try = 0;
1617 			break;
1618 		case OQUEST_:
1619 		case OCH_:
1620 			try = altoffset(scan, try);
1621 			if (try == -1)
1622 				return -1;
1623 			scan--;
1624 			do {
1625 				scan += OPND(s);
1626 				s = *scan;
1627 				if (OP(s) != O_QUEST && OP(s) != O_CH &&
1628 							OP(s) != OOR2)
1629 					return -1;
1630 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
1631 			/* We must skip to the next position, or we'll
1632 			 * leave altoffset() too early.
1633 			 */
1634 			scan++;
1635 			break;
1636 		case OANYOF:
1637 		case OCHAR:
1638 		case OANY:
1639 			try++;
1640 		case OBOW:
1641 		case OEOW:
1642 		case OLPAREN:
1643 		case ORPAREN:
1644 		case OOR2:
1645 			break;
1646 		default:
1647 			try = -1;
1648 			break;
1649 		}
1650 		if (try == -1)
1651 			return -1;
1652 		s = *scan++;
1653 	}
1654 
1655 	if (try > largest)
1656 		largest = try;
1657 
1658 	return largest+offset;
1659 }
1660 
1661 /*
1662  - computejumps - compute char jumps for BM scan
1663  == static void computejumps(struct parse *p, struct re_guts *g);
1664  *
1665  * This algorithm assumes g->must exists and is has size greater than
1666  * zero. It's based on the algorithm found on Computer Algorithms by
1667  * Sara Baase.
1668  *
1669  * A char jump is the number of characters one needs to jump based on
1670  * the value of the character from the text that was mismatched.
1671  */
1672 static void
1673 computejumps(struct parse *p, struct re_guts *g)
1674 {
1675 	int ch;
1676 	int mindex;
1677 
1678 	/* Avoid making errors worse */
1679 	if (p->error != 0)
1680 		return;
1681 
1682 	g->charjump = (int*) malloc((NC + 1) * sizeof(int));
1683 	if (g->charjump == NULL)	/* Not a fatal error */
1684 		return;
1685 	/* Adjust for signed chars, if necessary */
1686 	g->charjump = &g->charjump[-(CHAR_MIN)];
1687 
1688 	/* If the character does not exist in the pattern, the jump
1689 	 * is equal to the number of characters in the pattern.
1690 	 */
1691 	for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
1692 		g->charjump[ch] = g->mlen;
1693 
1694 	/* If the character does exist, compute the jump that would
1695 	 * take us to the last character in the pattern equal to it
1696 	 * (notice that we match right to left, so that last character
1697 	 * is the first one that would be matched).
1698 	 */
1699 	for (mindex = 0; mindex < g->mlen; mindex++)
1700 		g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1;
1701 }
1702 
1703 /*
1704  - computematchjumps - compute match jumps for BM scan
1705  == static void computematchjumps(struct parse *p, struct re_guts *g);
1706  *
1707  * This algorithm assumes g->must exists and is has size greater than
1708  * zero. It's based on the algorithm found on Computer Algorithms by
1709  * Sara Baase.
1710  *
1711  * A match jump is the number of characters one needs to advance based
1712  * on the already-matched suffix.
1713  * Notice that all values here are minus (g->mlen-1), because of the way
1714  * the search algorithm works.
1715  */
1716 static void
1717 computematchjumps(struct parse *p, struct re_guts *g)
1718 {
1719 	int mindex;		/* General "must" iterator */
1720 	int suffix;		/* Keeps track of matching suffix */
1721 	int ssuffix;		/* Keeps track of suffixes' suffix */
1722 	int* pmatches;		/* pmatches[k] points to the next i
1723 				 * such that i+1...mlen is a substring
1724 				 * of k+1...k+mlen-i-1
1725 				 */
1726 
1727 	/* Avoid making errors worse */
1728 	if (p->error != 0)
1729 		return;
1730 
1731 	pmatches = (int*) malloc(g->mlen * sizeof(int));
1732 	if (pmatches == NULL) {
1733 		g->matchjump = NULL;
1734 		return;
1735 	}
1736 
1737 	g->matchjump = (int*) malloc(g->mlen * sizeof(int));
1738 	if (g->matchjump == NULL) {	/* Not a fatal error */
1739 		free(pmatches);
1740 		return;
1741 	}
1742 
1743 	/* Set maximum possible jump for each character in the pattern */
1744 	for (mindex = 0; mindex < g->mlen; mindex++)
1745 		g->matchjump[mindex] = 2*g->mlen - mindex - 1;
1746 
1747 	/* Compute pmatches[] */
1748 	for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0;
1749 	    mindex--, suffix--) {
1750 		pmatches[mindex] = suffix;
1751 
1752 		/* If a mismatch is found, interrupting the substring,
1753 		 * compute the matchjump for that position. If no
1754 		 * mismatch is found, then a text substring mismatched
1755 		 * against the suffix will also mismatch against the
1756 		 * substring.
1757 		 */
1758 		while (suffix < g->mlen
1759 		    && g->must[mindex] != g->must[suffix]) {
1760 			g->matchjump[suffix] = MIN(g->matchjump[suffix],
1761 			    g->mlen - mindex - 1);
1762 			suffix = pmatches[suffix];
1763 		}
1764 	}
1765 
1766 	/* Compute the matchjump up to the last substring found to jump
1767 	 * to the beginning of the largest must pattern prefix matching
1768 	 * it's own suffix.
1769 	 */
1770 	for (mindex = 0; mindex <= suffix; mindex++)
1771 		g->matchjump[mindex] = MIN(g->matchjump[mindex],
1772 		    g->mlen + suffix - mindex);
1773 
1774         ssuffix = pmatches[suffix];
1775         while (suffix < g->mlen) {
1776                 while (suffix <= ssuffix && suffix < g->mlen) {
1777                         g->matchjump[suffix] = MIN(g->matchjump[suffix],
1778 			    g->mlen + ssuffix - suffix);
1779                         suffix++;
1780                 }
1781 		if (suffix < g->mlen)
1782                 	ssuffix = pmatches[ssuffix];
1783         }
1784 
1785 	free(pmatches);
1786 }
1787 
1788 /*
1789  - pluscount - count + nesting
1790  == static sopno pluscount(struct parse *p, struct re_guts *g);
1791  */
1792 static sopno			/* nesting depth */
1793 pluscount(struct parse *p, struct re_guts *g)
1794 {
1795 	sop *scan;
1796 	sop s;
1797 	sopno plusnest = 0;
1798 	sopno maxnest = 0;
1799 
1800 	if (p->error != 0)
1801 		return(0);	/* there may not be an OEND */
1802 
1803 	scan = g->strip + 1;
1804 	do {
1805 		s = *scan++;
1806 		switch (OP(s)) {
1807 		case OPLUS_:
1808 			plusnest++;
1809 			break;
1810 		case O_PLUS:
1811 			if (plusnest > maxnest)
1812 				maxnest = plusnest;
1813 			plusnest--;
1814 			break;
1815 		}
1816 	} while (OP(s) != OEND);
1817 	if (plusnest != 0)
1818 		g->iflags |= BAD;
1819 	return(maxnest);
1820 }
1821