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