Home | History | Annotate | Line # | Download | only in regex
regcomp.c revision 1.41
      1  1.41  christos /*	$NetBSD: regcomp.c,v 1.41 2021/02/25 13:42:16 christos Exp $	*/
      2   1.6       cgd 
      3   1.5       cgd /*-
      4  1.39  christos  * SPDX-License-Identifier: BSD-3-Clause
      5  1.39  christos  *
      6  1.39  christos  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
      7   1.5       cgd  * Copyright (c) 1992, 1993, 1994
      8   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
      9   1.5       cgd  *
     10  1.39  christos  * Copyright (c) 2011 The FreeBSD Foundation
     11  1.39  christos  * All rights reserved.
     12  1.39  christos  * Portions of this software were developed by David Chisnall
     13  1.39  christos  * under sponsorship from the FreeBSD Foundation.
     14  1.39  christos  *
     15   1.5       cgd  * This code is derived from software contributed to Berkeley by
     16   1.5       cgd  * Henry Spencer.
     17   1.5       cgd  *
     18   1.5       cgd  * Redistribution and use in source and binary forms, with or without
     19   1.5       cgd  * modification, are permitted provided that the following conditions
     20   1.5       cgd  * are met:
     21   1.5       cgd  * 1. Redistributions of source code must retain the above copyright
     22   1.5       cgd  *    notice, this list of conditions and the following disclaimer.
     23   1.5       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     24   1.5       cgd  *    notice, this list of conditions and the following disclaimer in the
     25   1.5       cgd  *    documentation and/or other materials provided with the distribution.
     26  1.18       agc  * 3. Neither the name of the University nor the names of its contributors
     27  1.18       agc  *    may be used to endorse or promote products derived from this software
     28  1.18       agc  *    without specific prior written permission.
     29  1.18       agc  *
     30  1.18       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     31  1.18       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32  1.18       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  1.18       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     34  1.18       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  1.18       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  1.18       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  1.18       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  1.18       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  1.18       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  1.18       agc  * SUCH DAMAGE.
     41  1.18       agc  *
     42  1.18       agc  *	@(#)regcomp.c	8.5 (Berkeley) 3/20/94
     43  1.18       agc  */
     44  1.18       agc 
     45   1.7  christos #include <sys/cdefs.h>
     46   1.6       cgd #if 0
     47   1.5       cgd static char sccsid[] = "@(#)regcomp.c	8.5 (Berkeley) 3/20/94";
     48  1.39  christos __FBSDID("$FreeBSD: head/lib/libc/regex/regcomp.c 368359 2020-12-05 03:18:48Z kevans $");
     49   1.6       cgd #endif
     50  1.41  christos __RCSID("$NetBSD: regcomp.c,v 1.41 2021/02/25 13:42:16 christos Exp $");
     51  1.39  christos 
     52  1.39  christos #define _OPENBSD_SOURCE
     53  1.39  christos #define REGEX_GNU_EXTENSIONS
     54   1.5       cgd 
     55   1.8       jtc #include "namespace.h"
     56   1.1       jtc #include <sys/types.h>
     57  1.39  christos #include <stdio.h>
     58  1.39  christos #include <string.h>
     59   1.1       jtc #include <ctype.h>
     60   1.1       jtc #include <limits.h>
     61   1.1       jtc #include <stdlib.h>
     62  1.28  junyoung #include <regex.h>
     63  1.39  christos #include <stdbool.h>
     64  1.39  christos #include <wchar.h>
     65  1.39  christos #include <wctype.h>
     66   1.8       jtc 
     67   1.8       jtc #ifdef __weak_alias
     68  1.16   mycroft __weak_alias(regcomp,_regcomp)
     69   1.8       jtc #endif
     70   1.1       jtc 
     71  1.39  christos #ifdef REGEX_LIBC_COLLATE
     72  1.39  christos #include "collate.h"
     73  1.39  christos #endif
     74  1.39  christos 
     75   1.1       jtc #include "utils.h"
     76   1.1       jtc #include "regex2.h"
     77   1.1       jtc 
     78   1.1       jtc #include "cname.h"
     79   1.1       jtc 
     80   1.1       jtc /*
     81  1.39  christos  * Branching context, used to keep track of branch state for all of the branch-
     82  1.39  christos  * aware functions. In addition to keeping track of branch positions for the
     83  1.39  christos  * p_branch_* functions, we use this to simplify some clumsiness in BREs for
     84  1.39  christos  * detection of whether ^ is acting as an anchor or being used erroneously and
     85  1.39  christos  * also for whether we're in a sub-expression or not.
     86  1.39  christos  */
     87  1.39  christos struct branchc {
     88  1.39  christos 	sopno start;
     89  1.39  christos 	sopno back;
     90  1.39  christos 	sopno fwd;
     91  1.39  christos 
     92  1.39  christos 	int nbranch;
     93  1.39  christos 	int nchain;
     94  1.39  christos 	bool outer;
     95  1.39  christos 	bool terminate;
     96  1.39  christos };
     97  1.39  christos 
     98  1.39  christos /*
     99   1.1       jtc  * parse structure, passed up and down to avoid global variables and
    100   1.1       jtc  * other clumsinesses
    101   1.1       jtc  */
    102   1.1       jtc struct parse {
    103  1.21      yamt 	const char *next;	/* next character in RE */
    104  1.21      yamt 	const char *end;	/* end of string (-> NUL normally) */
    105   1.1       jtc 	int error;		/* has an error been seen? */
    106  1.39  christos 	int gnuext;
    107   1.1       jtc 	sop *strip;		/* malloced strip */
    108   1.1       jtc 	sopno ssize;		/* malloced strip size (allocated) */
    109   1.1       jtc 	sopno slen;		/* malloced strip length (used) */
    110  1.33  christos 	size_t ncsalloc;	/* number of csets allocated */
    111   1.1       jtc 	struct re_guts *g;
    112   1.1       jtc #	define	NPAREN	10	/* we need to remember () 1-9 for back refs */
    113   1.1       jtc 	sopno pbegin[NPAREN];	/* -> ( ([0] unused) */
    114   1.1       jtc 	sopno pend[NPAREN];	/* -> ) ([0] unused) */
    115  1.39  christos 	bool allowbranch;	/* can this expression branch? */
    116  1.39  christos 	bool bre;		/* convenience; is this a BRE? */
    117  1.39  christos 	int pflags;		/* other parsing flags -- legacy escapes? */
    118  1.39  christos 	bool (*parse_expr)(struct parse *, struct branchc *);
    119  1.39  christos 	void (*pre_parse)(struct parse *, struct branchc *);
    120  1.39  christos 	void (*post_parse)(struct parse *, struct branchc *);
    121   1.1       jtc };
    122   1.1       jtc 
    123  1.39  christos #define PFLAG_LEGACY_ESC	0x00000001
    124  1.39  christos 
    125   1.5       cgd /* ========= begin header generated by ./mkh ========= */
    126   1.5       cgd #ifdef __cplusplus
    127   1.5       cgd extern "C" {
    128   1.5       cgd #endif
    129   1.5       cgd 
    130   1.5       cgd /* === regcomp.c === */
    131  1.39  christos static bool p_ere_exp(struct parse *p, struct branchc *bc);
    132  1.26  junyoung static void p_str(struct parse *p);
    133  1.39  christos static int p_branch_eat_delim(struct parse *p, struct branchc *bc);
    134  1.39  christos static void p_branch_ins_offset(struct parse *p, struct branchc *bc);
    135  1.39  christos static void p_branch_fix_tail(struct parse *p, struct branchc *bc);
    136  1.39  christos static bool p_branch_empty(struct parse *p, struct branchc *bc);
    137  1.39  christos static bool p_branch_do(struct parse *p, struct branchc *bc);
    138  1.39  christos static void p_bre_pre_parse(struct parse *p, struct branchc *bc);
    139  1.39  christos static void p_bre_post_parse(struct parse *p, struct branchc *bc);
    140  1.39  christos static void p_re(struct parse *p, int end1, int end2);
    141  1.39  christos static bool p_simp_re(struct parse *p, struct branchc *bc);
    142  1.26  junyoung static int p_count(struct parse *p);
    143  1.26  junyoung static void p_bracket(struct parse *p);
    144  1.39  christos static int p_range_cmp(wchar_t c1, wchar_t c2);
    145  1.26  junyoung static void p_b_term(struct parse *p, cset *cs);
    146  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    147  1.39  christos static int p_b_pseudoclass(struct parse *p, char c);
    148  1.39  christos #endif
    149  1.26  junyoung static void p_b_cclass(struct parse *p, cset *cs);
    150  1.39  christos static void p_b_cclass_named(struct parse *p, cset *cs, const char[]);
    151  1.26  junyoung static void p_b_eclass(struct parse *p, cset *cs);
    152  1.39  christos static wint_t p_b_symbol(struct parse *p);
    153  1.39  christos static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
    154  1.39  christos static bool may_escape(struct parse *p, const wint_t ch);
    155  1.39  christos static wint_t othercase(wint_t ch);
    156  1.39  christos static void bothcases(struct parse *p, wint_t ch);
    157  1.39  christos static void ordinary(struct parse *p, wint_t ch);
    158  1.26  junyoung static void nonnewline(struct parse *p);
    159  1.39  christos static void repeat(struct parse *p, sopno start, int from, int to);
    160  1.26  junyoung static int seterr(struct parse *p, int e);
    161  1.26  junyoung static cset *allocset(struct parse *p);
    162  1.26  junyoung static void freeset(struct parse *p, cset *cs);
    163  1.39  christos static void CHadd(struct parse *p, cset *cs, wint_t ch);
    164  1.39  christos static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max);
    165  1.39  christos static void CHaddtype(struct parse *p, cset *cs, wctype_t wct);
    166  1.39  christos static wint_t singleton(cset *cs);
    167  1.26  junyoung static sopno dupl(struct parse *p, sopno start, sopno finish);
    168  1.39  christos static void doemit(struct parse *p, sop op, size_t opnd);
    169  1.39  christos static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
    170  1.39  christos static void dofwd(struct parse *p, sopno pos, sop value);
    171  1.30  christos static int enlarge(struct parse *p, sopno size);
    172  1.26  junyoung static void stripsnug(struct parse *p, struct re_guts *g);
    173  1.26  junyoung static void findmust(struct parse *p, struct re_guts *g);
    174  1.39  christos static int altoffset(sop *scan, int offset);
    175  1.39  christos static void computejumps(struct parse *p, struct re_guts *g);
    176  1.39  christos static void computematchjumps(struct parse *p, struct re_guts *g);
    177  1.26  junyoung static sopno pluscount(struct parse *p, struct re_guts *g);
    178  1.39  christos static wint_t wgetnext(struct parse *p);
    179   1.5       cgd 
    180   1.5       cgd #ifdef __cplusplus
    181   1.5       cgd }
    182   1.5       cgd #endif
    183   1.5       cgd /* ========= end header generated by ./mkh ========= */
    184   1.1       jtc 
    185   1.1       jtc static char nuls[10];		/* place to point scanner in event of error */
    186   1.1       jtc 
    187   1.1       jtc /*
    188   1.1       jtc  * macros for use with parse structure
    189   1.1       jtc  * BEWARE:  these know that the parse structure is named `p' !!!
    190   1.1       jtc  */
    191   1.1       jtc #define	PEEK()	(*p->next)
    192   1.1       jtc #define	PEEK2()	(*(p->next+1))
    193   1.1       jtc #define	MORE()	(p->next < p->end)
    194   1.1       jtc #define	MORE2()	(p->next+1 < p->end)
    195   1.1       jtc #define	SEE(c)	(MORE() && PEEK() == (c))
    196   1.1       jtc #define	SEETWO(a, b)	(MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b))
    197  1.39  christos #define	SEESPEC(a)	(p->bre ? SEETWO('\\', a) : SEE(a))
    198   1.1       jtc #define	EAT(c)	((SEE(c)) ? (NEXT(), 1) : 0)
    199   1.1       jtc #define	EATTWO(a, b)	((SEETWO(a, b)) ? (NEXT2(), 1) : 0)
    200  1.39  christos #define	EATSPEC(a)	(p->bre ? EATTWO('\\', a) : EAT(a))
    201   1.1       jtc #define	NEXT()	(p->next++)
    202   1.1       jtc #define	NEXT2()	(p->next += 2)
    203   1.1       jtc #define	NEXTn(n)	(p->next += (n))
    204   1.1       jtc #define	GETNEXT()	(*p->next++)
    205  1.39  christos #define	WGETNEXT()	wgetnext(p)
    206   1.1       jtc #define	SETERROR(e)	seterr(p, (e))
    207  1.39  christos #define	REQUIRE(co, e)	((co) || SETERROR(e))
    208   1.1       jtc #define	MUSTSEE(c, e)	(REQUIRE(MORE() && PEEK() == (c), e))
    209  1.39  christos #define	MUSTEAT(c, e)	(REQUIRE(MORE() && GETNEXT() == (c), e))
    210   1.1       jtc #define	MUSTNOTSEE(c, e)	(REQUIRE(!MORE() || PEEK() != (c), e))
    211  1.40  christos #define	EMIT(op, sopnd)	doemit(p, (op), (sopnd))
    212  1.40  christos #define	INSERT(op, pos)	doinsert(p, (op), HERE()-(pos)+1, pos)
    213  1.12  drochner #define	AHEAD(pos)		dofwd(p, pos, HERE()-(pos))
    214   1.1       jtc #define	ASTERN(sop, pos)	EMIT(sop, HERE()-pos)
    215   1.1       jtc #define	HERE()		(p->slen)
    216   1.1       jtc #define	THERE()		(p->slen - 1)
    217   1.4       jtc #define	THERETHERE()	(p->slen - 2)
    218   1.1       jtc #define	DROP(n)	(p->slen -= (n))
    219   1.1       jtc 
    220  1.39  christos /* Macro used by computejump()/computematchjump() */
    221  1.41  christos #ifndef MIN
    222  1.39  christos #define MIN(a,b)	((a)<(b)?(a):(b))
    223  1.41  christos #endif
    224  1.30  christos 
    225  1.39  christos static int				/* 0 success, otherwise REG_something */
    226  1.39  christos regcomp_internal(regex_t * __restrict preg,
    227  1.39  christos 	const char * __restrict pattern,
    228  1.39  christos 	int cflags, int pflags)
    229   1.1       jtc {
    230   1.1       jtc 	struct parse pa;
    231   1.9     perry 	struct re_guts *g;
    232   1.9     perry 	struct parse *p = &pa;
    233   1.9     perry 	int i;
    234   1.9     perry 	size_t len;
    235  1.39  christos 	size_t maxlen;
    236   1.3       jtc #ifdef REDEBUG
    237   1.3       jtc #	define	GOODFLAGS(f)	(f)
    238   1.3       jtc #else
    239   1.3       jtc #	define	GOODFLAGS(f)	((f)&~REG_DUMP)
    240   1.3       jtc #endif
    241   1.1       jtc 
    242  1.14     lukem 	_DIAGASSERT(preg != NULL);
    243  1.14     lukem 	_DIAGASSERT(pattern != NULL);
    244  1.14     lukem 
    245   1.3       jtc 	cflags = GOODFLAGS(cflags);
    246   1.1       jtc 	if ((cflags&REG_EXTENDED) && (cflags&REG_NOSPEC))
    247   1.1       jtc 		return(REG_INVARG);
    248   1.1       jtc 
    249   1.1       jtc 	if (cflags&REG_PEND) {
    250   1.1       jtc 		if (preg->re_endp < pattern)
    251   1.1       jtc 			return(REG_INVARG);
    252   1.1       jtc 		len = preg->re_endp - pattern;
    253   1.1       jtc 	} else
    254  1.11  christos 		len = strlen(pattern);
    255   1.1       jtc 
    256   1.1       jtc 	/* do the mallocs early so failure handling is easy */
    257  1.39  christos 	g = malloc(sizeof(*g));
    258   1.1       jtc 	if (g == NULL)
    259   1.1       jtc 		return(REG_ESPACE);
    260  1.39  christos 	/*
    261  1.39  christos 	 * Limit the pattern space to avoid a 32-bit overflow on buffer
    262  1.39  christos 	 * extension.  Also avoid any signed overflow in case of conversion
    263  1.39  christos 	 * so make the real limit based on a 31-bit overflow.
    264  1.39  christos 	 *
    265  1.39  christos 	 * Likely not applicable on 64-bit systems but handle the case
    266  1.39  christos 	 * generically (who are we to stop people from using ~715MB+
    267  1.39  christos 	 * patterns?).
    268  1.39  christos 	 */
    269  1.40  christos 	maxlen = ((size_t)-1 >> 1) / sizeof(*p->strip) * 2 / 3;
    270  1.39  christos 	if (len >= maxlen) {
    271  1.40  christos 		free(g);
    272  1.39  christos 		return(REG_ESPACE);
    273  1.39  christos 	}
    274  1.40  christos 	p->ssize = (sopno)(len / 2 * 3 + 1);	/* ugh */
    275  1.39  christos 	assert(p->ssize >= len);
    276  1.39  christos 
    277  1.39  christos 	p->strip = calloc(p->ssize, sizeof(*p->strip));
    278   1.1       jtc 	p->slen = 0;
    279   1.1       jtc 	if (p->strip == NULL) {
    280  1.40  christos 		free(g);
    281   1.1       jtc 		return(REG_ESPACE);
    282   1.1       jtc 	}
    283   1.1       jtc 
    284   1.1       jtc 	/* set things up */
    285   1.1       jtc 	p->g = g;
    286  1.39  christos 	p->next = pattern;	/* convenience; we do not modify it */
    287   1.1       jtc 	p->end = p->next + len;
    288   1.1       jtc 	p->error = 0;
    289   1.1       jtc 	p->ncsalloc = 0;
    290  1.39  christos 	p->pflags = pflags;
    291   1.1       jtc 	for (i = 0; i < NPAREN; i++) {
    292   1.1       jtc 		p->pbegin[i] = 0;
    293   1.1       jtc 		p->pend[i] = 0;
    294   1.1       jtc 	}
    295  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    296  1.39  christos 	if ((cflags & REG_GNU) == 0) {
    297  1.39  christos 		p->gnuext = false;
    298  1.39  christos 		p->allowbranch = (cflags & REG_EXTENDED) != 0;
    299  1.39  christos 	} else
    300  1.39  christos 		p->gnuext = p->allowbranch = true;
    301  1.39  christos #else
    302  1.39  christos 	p->gnuext = false;
    303  1.39  christos 	p->allowbranch = (cflags & REG_EXTENDED) != 0;
    304  1.39  christos #endif
    305  1.39  christos 	if (cflags & REG_EXTENDED) {
    306  1.39  christos 		p->bre = false;
    307  1.39  christos 		p->parse_expr = p_ere_exp;
    308  1.39  christos 		p->pre_parse = NULL;
    309  1.39  christos 		p->post_parse = NULL;
    310  1.39  christos 	} else {
    311  1.39  christos 		p->bre = true;
    312  1.39  christos 		p->parse_expr = p_simp_re;
    313  1.39  christos 		p->pre_parse = p_bre_pre_parse;
    314  1.39  christos 		p->post_parse = p_bre_post_parse;
    315  1.39  christos 	}
    316   1.1       jtc 	g->sets = NULL;
    317   1.1       jtc 	g->ncsets = 0;
    318   1.1       jtc 	g->cflags = cflags;
    319   1.1       jtc 	g->iflags = 0;
    320   1.1       jtc 	g->nbol = 0;
    321   1.1       jtc 	g->neol = 0;
    322   1.1       jtc 	g->must = NULL;
    323  1.39  christos 	g->moffset = -1;
    324  1.39  christos 	g->charjump = NULL;
    325  1.39  christos 	g->matchjump = NULL;
    326   1.1       jtc 	g->mlen = 0;
    327   1.1       jtc 	g->nsub = 0;
    328   1.1       jtc 	g->backrefs = 0;
    329   1.1       jtc 
    330   1.1       jtc 	/* do it */
    331   1.1       jtc 	EMIT(OEND, 0);
    332   1.1       jtc 	g->firststate = THERE();
    333  1.39  christos 	if (cflags & REG_NOSPEC)
    334   1.1       jtc 		p_str(p);
    335   1.1       jtc 	else
    336  1.39  christos 		p_re(p, OUT, OUT);
    337   1.1       jtc 	EMIT(OEND, 0);
    338   1.1       jtc 	g->laststate = THERE();
    339   1.1       jtc 
    340   1.1       jtc 	/* tidy up loose ends and fill things in */
    341   1.1       jtc 	stripsnug(p, g);
    342   1.1       jtc 	findmust(p, g);
    343  1.39  christos 	/* only use Boyer-Moore algorithm if the pattern is bigger
    344  1.39  christos 	 * than three characters
    345  1.39  christos 	 */
    346  1.39  christos 	if(g->mlen > 3) {
    347  1.39  christos 		computejumps(p, g);
    348  1.39  christos 		computematchjumps(p, g);
    349  1.39  christos 		if(g->matchjump == NULL && g->charjump != NULL) {
    350  1.39  christos 			free(g->charjump);
    351  1.39  christos 			g->charjump = NULL;
    352  1.39  christos 		}
    353  1.39  christos 	}
    354   1.1       jtc 	g->nplus = pluscount(p, g);
    355   1.1       jtc 	g->magic = MAGIC2;
    356   1.1       jtc 	preg->re_nsub = g->nsub;
    357   1.1       jtc 	preg->re_g = g;
    358   1.1       jtc 	preg->re_magic = MAGIC1;
    359   1.1       jtc #ifndef REDEBUG
    360   1.1       jtc 	/* not debugging, so can't rely on the assert() in regexec() */
    361   1.1       jtc 	if (g->iflags&BAD)
    362   1.1       jtc 		SETERROR(REG_ASSERT);
    363   1.1       jtc #endif
    364   1.1       jtc 
    365   1.1       jtc 	/* win or lose, we're done */
    366   1.1       jtc 	if (p->error != 0)	/* lose */
    367   1.1       jtc 		regfree(preg);
    368   1.1       jtc 	return(p->error);
    369   1.1       jtc }
    370   1.1       jtc 
    371   1.1       jtc /*
    372  1.39  christos  - regcomp - interface for parser and compilation
    373  1.39  christos  = extern int regcomp(regex_t *, const char *, int);
    374  1.39  christos  = #define	REG_BASIC	0000
    375  1.39  christos  = #define	REG_EXTENDED	0001
    376  1.39  christos  = #define	REG_ICASE	0002
    377  1.39  christos  = #define	REG_NOSUB	0004
    378  1.39  christos  = #define	REG_NEWLINE	0010
    379  1.39  christos  = #define	REG_NOSPEC	0020
    380  1.39  christos  = #define	REG_PEND	0040
    381  1.39  christos  = #define	REG_DUMP	0200
    382   1.1       jtc  */
    383  1.39  christos int				/* 0 success, otherwise REG_something */
    384  1.39  christos regcomp(regex_t * __restrict preg,
    385  1.39  christos 	const char * __restrict pattern,
    386  1.39  christos 	int cflags)
    387   1.1       jtc {
    388  1.30  christos 
    389  1.39  christos 	return (regcomp_internal(preg, pattern, cflags, 0));
    390   1.1       jtc }
    391   1.1       jtc 
    392   1.1       jtc /*
    393  1.39  christos  - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op,
    394  1.39  christos  - return whether we should terminate or not
    395  1.39  christos  == static bool p_ere_exp(struct parse *p);
    396   1.1       jtc  */
    397  1.39  christos static bool
    398  1.39  christos p_ere_exp(struct parse *p, struct branchc *bc)
    399   1.1       jtc {
    400   1.9     perry 	char c;
    401  1.39  christos 	wint_t wc;
    402   1.9     perry 	sopno pos;
    403   1.9     perry 	int count;
    404   1.9     perry 	int count2;
    405  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    406  1.40  christos 	size_t i;
    407  1.39  christos 	int handled;
    408  1.39  christos #endif
    409   1.9     perry 	sopno subno;
    410   1.1       jtc 	int wascaret = 0;
    411   1.1       jtc 
    412  1.14     lukem 	_DIAGASSERT(p != NULL);
    413  1.14     lukem 
    414  1.39  christos 	(void)bc;
    415   1.1       jtc 	assert(MORE());		/* caller should have ensured this */
    416   1.1       jtc 	c = GETNEXT();
    417   1.1       jtc 
    418  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    419  1.39  christos 	handled = 0;
    420  1.39  christos #endif
    421   1.1       jtc 	pos = HERE();
    422   1.1       jtc 	switch (c) {
    423   1.1       jtc 	case '(':
    424  1.39  christos 		(void)REQUIRE(MORE(), REG_EPAREN);
    425   1.1       jtc 		p->g->nsub++;
    426  1.40  christos 		subno = (sopno)p->g->nsub;
    427   1.1       jtc 		if (subno < NPAREN)
    428   1.1       jtc 			p->pbegin[subno] = HERE();
    429   1.1       jtc 		EMIT(OLPAREN, subno);
    430   1.1       jtc 		if (!SEE(')'))
    431  1.39  christos 			p_re(p, ')', IGN);
    432   1.1       jtc 		if (subno < NPAREN) {
    433   1.1       jtc 			p->pend[subno] = HERE();
    434   1.1       jtc 			assert(p->pend[subno] != 0);
    435   1.1       jtc 		}
    436   1.1       jtc 		EMIT(ORPAREN, subno);
    437  1.39  christos 		(void)MUSTEAT(')', REG_EPAREN);
    438   1.1       jtc 		break;
    439   1.1       jtc #ifndef POSIX_MISTAKE
    440   1.1       jtc 	case ')':		/* happens only if no current unmatched ( */
    441   1.1       jtc 		/*
    442   1.1       jtc 		 * You may ask, why the ifndef?  Because I didn't notice
    443   1.1       jtc 		 * this until slightly too late for 1003.2, and none of the
    444   1.1       jtc 		 * other 1003.2 regular-expression reviewers noticed it at
    445   1.1       jtc 		 * all.  So an unmatched ) is legal POSIX, at least until
    446   1.1       jtc 		 * we can get it fixed.
    447   1.1       jtc 		 */
    448   1.1       jtc 		SETERROR(REG_EPAREN);
    449   1.1       jtc 		break;
    450   1.1       jtc #endif
    451   1.1       jtc 	case '^':
    452   1.1       jtc 		EMIT(OBOL, 0);
    453   1.1       jtc 		p->g->iflags |= USEBOL;
    454   1.1       jtc 		p->g->nbol++;
    455   1.1       jtc 		wascaret = 1;
    456   1.1       jtc 		break;
    457   1.1       jtc 	case '$':
    458   1.1       jtc 		EMIT(OEOL, 0);
    459   1.1       jtc 		p->g->iflags |= USEEOL;
    460   1.1       jtc 		p->g->neol++;
    461   1.1       jtc 		break;
    462   1.1       jtc 	case '|':
    463   1.1       jtc 		SETERROR(REG_EMPTY);
    464   1.1       jtc 		break;
    465   1.1       jtc 	case '*':
    466   1.1       jtc 	case '+':
    467   1.1       jtc 	case '?':
    468  1.39  christos 	case '{':
    469   1.1       jtc 		SETERROR(REG_BADRPT);
    470   1.1       jtc 		break;
    471   1.1       jtc 	case '.':
    472   1.1       jtc 		if (p->g->cflags&REG_NEWLINE)
    473   1.1       jtc 			nonnewline(p);
    474   1.1       jtc 		else
    475   1.1       jtc 			EMIT(OANY, 0);
    476   1.1       jtc 		break;
    477   1.1       jtc 	case '[':
    478   1.1       jtc 		p_bracket(p);
    479   1.1       jtc 		break;
    480   1.1       jtc 	case '\\':
    481  1.39  christos 		(void)REQUIRE(MORE(), REG_EESCAPE);
    482  1.39  christos 		wc = WGETNEXT();
    483  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    484  1.39  christos 		if (p->gnuext) {
    485  1.39  christos 			handled = 1;
    486  1.39  christos 			switch (wc) {
    487  1.39  christos 			case '`':
    488  1.39  christos 				EMIT(OBOS, 0);
    489  1.39  christos 				break;
    490  1.39  christos 			case '\'':
    491  1.39  christos 				EMIT(OEOS, 0);
    492  1.39  christos 				break;
    493  1.39  christos 			case 'B':
    494  1.39  christos 				EMIT(ONWBND, 0);
    495  1.39  christos 				break;
    496  1.39  christos 			case 'b':
    497  1.39  christos 				EMIT(OWBND, 0);
    498  1.39  christos 				break;
    499  1.39  christos 			case 'W':
    500  1.39  christos 			case 'w':
    501  1.39  christos 			case 'S':
    502  1.39  christos 			case 's':
    503  1.39  christos 				p_b_pseudoclass(p, wc);
    504  1.39  christos 				break;
    505  1.39  christos 			case '1':
    506  1.39  christos 			case '2':
    507  1.39  christos 			case '3':
    508  1.39  christos 			case '4':
    509  1.39  christos 			case '5':
    510  1.39  christos 			case '6':
    511  1.39  christos 			case '7':
    512  1.39  christos 			case '8':
    513  1.39  christos 			case '9':
    514  1.39  christos 				i = wc - '0';
    515  1.39  christos 				assert(i < NPAREN);
    516  1.39  christos 				if (p->pend[i] != 0) {
    517  1.39  christos 					assert(i <= p->g->nsub);
    518  1.39  christos 					EMIT(OBACK_, i);
    519  1.39  christos 					assert(p->pbegin[i] != 0);
    520  1.39  christos 					assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
    521  1.39  christos 					assert(OP(p->strip[p->pend[i]]) == ORPAREN);
    522  1.39  christos 					(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
    523  1.39  christos 					EMIT(O_BACK, i);
    524  1.39  christos 				} else
    525  1.39  christos 					SETERROR(REG_ESUBREG);
    526  1.39  christos 				p->g->backrefs = 1;
    527  1.39  christos 				break;
    528  1.39  christos 			default:
    529  1.39  christos 				handled = 0;
    530  1.39  christos 			}
    531  1.39  christos 			/* Don't proceed to the POSIX bits if we've already handled it */
    532  1.39  christos 			if (handled)
    533  1.39  christos 				break;
    534  1.39  christos 		}
    535  1.39  christos #endif
    536  1.39  christos 		switch (wc) {
    537  1.39  christos 		case '<':
    538  1.39  christos 			EMIT(OBOW, 0);
    539  1.39  christos 			break;
    540  1.39  christos 		case '>':
    541  1.39  christos 			EMIT(OEOW, 0);
    542  1.39  christos 			break;
    543  1.39  christos 		default:
    544  1.39  christos 			if (may_escape(p, wc))
    545  1.39  christos 				ordinary(p, wc);
    546  1.39  christos 			else
    547  1.39  christos 				SETERROR(REG_EESCAPE);
    548  1.39  christos 			break;
    549  1.39  christos 		}
    550   1.1       jtc 		break;
    551   1.1       jtc 	default:
    552  1.38  christos 		if (p->error != 0)
    553  1.39  christos 			return (false);
    554  1.39  christos 		p->next--;
    555  1.39  christos 		wc = WGETNEXT();
    556  1.39  christos 		ordinary(p, wc);
    557   1.1       jtc 		break;
    558   1.1       jtc 	}
    559   1.1       jtc 
    560   1.1       jtc 	if (!MORE())
    561  1.39  christos 		return (false);
    562   1.1       jtc 	c = PEEK();
    563   1.1       jtc 	/* we call { a repetition if followed by a digit */
    564  1.39  christos 	if (!( c == '*' || c == '+' || c == '?' || c == '{'))
    565  1.39  christos 		return (false);		/* no repetition, we're done */
    566  1.39  christos 	else if (c == '{')
    567  1.39  christos 		(void)REQUIRE(MORE2() && \
    568  1.39  christos 		    (isdigit((uch)PEEK2()) || PEEK2() == ','), REG_BADRPT);
    569   1.1       jtc 	NEXT();
    570   1.1       jtc 
    571  1.39  christos 	(void)REQUIRE(!wascaret, REG_BADRPT);
    572   1.1       jtc 	switch (c) {
    573   1.1       jtc 	case '*':	/* implemented as +? */
    574   1.4       jtc 		/* this case does not require the (y|) trick, noKLUDGE */
    575   1.1       jtc 		INSERT(OPLUS_, pos);
    576   1.1       jtc 		ASTERN(O_PLUS, pos);
    577   1.1       jtc 		INSERT(OQUEST_, pos);
    578   1.1       jtc 		ASTERN(O_QUEST, pos);
    579   1.1       jtc 		break;
    580   1.1       jtc 	case '+':
    581   1.1       jtc 		INSERT(OPLUS_, pos);
    582   1.1       jtc 		ASTERN(O_PLUS, pos);
    583   1.1       jtc 		break;
    584   1.1       jtc 	case '?':
    585   1.4       jtc 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
    586   1.4       jtc 		INSERT(OCH_, pos);		/* offset slightly wrong */
    587   1.4       jtc 		ASTERN(OOR1, pos);		/* this one's right */
    588   1.4       jtc 		AHEAD(pos);			/* fix the OCH_ */
    589   1.4       jtc 		EMIT(OOR2, 0);			/* offset very wrong... */
    590   1.4       jtc 		AHEAD(THERE());			/* ...so fix it */
    591   1.4       jtc 		ASTERN(O_CH, THERETHERE());
    592   1.1       jtc 		break;
    593   1.1       jtc 	case '{':
    594   1.1       jtc 		count = p_count(p);
    595   1.1       jtc 		if (EAT(',')) {
    596  1.39  christos 			if (isdigit((uch)PEEK())) {
    597   1.1       jtc 				count2 = p_count(p);
    598  1.39  christos 				(void)REQUIRE(count <= count2, REG_BADBR);
    599   1.1       jtc 			} else		/* single number with comma */
    600   1.1       jtc 				count2 = INFINITY;
    601   1.1       jtc 		} else		/* just a single number */
    602   1.1       jtc 			count2 = count;
    603  1.39  christos 		repeat(p, pos, count, count2);
    604   1.1       jtc 		if (!EAT('}')) {	/* error heuristics */
    605   1.1       jtc 			while (MORE() && PEEK() != '}')
    606   1.1       jtc 				NEXT();
    607  1.39  christos 			(void)REQUIRE(MORE(), REG_EBRACE);
    608   1.1       jtc 			SETERROR(REG_BADBR);
    609   1.1       jtc 		}
    610   1.1       jtc 		break;
    611   1.1       jtc 	}
    612   1.1       jtc 
    613   1.1       jtc 	if (!MORE())
    614  1.39  christos 		return (false);
    615   1.1       jtc 	c = PEEK();
    616   1.1       jtc 	if (!( c == '*' || c == '+' || c == '?' ||
    617  1.39  christos 				(c == '{' && MORE2() && isdigit((uch)PEEK2())) ) )
    618  1.39  christos 		return (false);
    619   1.1       jtc 	SETERROR(REG_BADRPT);
    620  1.39  christos 	return (false);
    621   1.1       jtc }
    622   1.1       jtc 
    623   1.1       jtc /*
    624   1.1       jtc  - p_str - string (no metacharacters) "parser"
    625   1.9     perry  == static void p_str(struct parse *p);
    626   1.1       jtc  */
    627   1.1       jtc static void
    628  1.39  christos p_str(struct parse *p)
    629   1.1       jtc {
    630  1.39  christos 	(void)REQUIRE(MORE(), REG_EMPTY);
    631  1.39  christos 	while (MORE())
    632  1.39  christos 		ordinary(p, WGETNEXT());
    633  1.39  christos }
    634  1.14     lukem 
    635  1.39  christos /*
    636  1.39  christos  * Eat consecutive branch delimiters for the kind of expression that we are
    637  1.39  christos  * parsing, return the number of delimiters that we ate.
    638  1.39  christos  */
    639  1.39  christos static int
    640  1.39  christos p_branch_eat_delim(struct parse *p, struct branchc *bc)
    641  1.39  christos {
    642  1.39  christos 	int nskip;
    643  1.14     lukem 
    644  1.39  christos 	(void)bc;
    645  1.39  christos 	nskip = 0;
    646  1.39  christos 	while (EATSPEC('|'))
    647  1.39  christos 		++nskip;
    648  1.39  christos 	return (nskip);
    649   1.1       jtc }
    650   1.1       jtc 
    651   1.1       jtc /*
    652  1.39  christos  * Insert necessary branch book-keeping operations. This emits a
    653  1.39  christos  * bogus 'next' offset, since we still have more to parse
    654   1.1       jtc  */
    655   1.1       jtc static void
    656  1.39  christos p_branch_ins_offset(struct parse *p, struct branchc *bc)
    657   1.9     perry {
    658   1.1       jtc 
    659  1.39  christos 	if (bc->nbranch == 0) {
    660  1.39  christos 		INSERT(OCH_, bc->start);	/* offset is wrong */
    661  1.39  christos 		bc->fwd = bc->start;
    662  1.39  christos 		bc->back = bc->start;
    663  1.39  christos 	}
    664  1.39  christos 
    665  1.39  christos 	ASTERN(OOR1, bc->back);
    666  1.39  christos 	bc->back = THERE();
    667  1.39  christos 	AHEAD(bc->fwd);			/* fix previous offset */
    668  1.39  christos 	bc->fwd = HERE();
    669  1.39  christos 	EMIT(OOR2, 0);			/* offset is very wrong */
    670  1.39  christos 	++bc->nbranch;
    671  1.39  christos }
    672  1.39  christos 
    673  1.39  christos /*
    674  1.39  christos  * Fix the offset of the tail branch, if we actually had any branches.
    675  1.39  christos  * This is to correct the bogus placeholder offset that we use.
    676  1.39  christos  */
    677  1.39  christos static void
    678  1.39  christos p_branch_fix_tail(struct parse *p, struct branchc *bc)
    679  1.39  christos {
    680  1.14     lukem 
    681  1.39  christos 	/* Fix bogus offset at the tail if we actually have branches */
    682  1.39  christos 	if (bc->nbranch > 0) {
    683  1.39  christos 		AHEAD(bc->fwd);
    684  1.39  christos 		ASTERN(O_CH, bc->back);
    685  1.30  christos 	}
    686  1.39  christos }
    687  1.39  christos 
    688  1.39  christos /*
    689  1.39  christos  * Signal to the parser that an empty branch has been encountered; this will,
    690  1.39  christos  * in the future, be used to allow for more permissive behavior with empty
    691  1.39  christos  * branches. The return value should indicate whether parsing may continue
    692  1.39  christos  * or not.
    693  1.39  christos  */
    694  1.39  christos static bool
    695  1.39  christos p_branch_empty(struct parse *p, struct branchc *bc)
    696  1.39  christos {
    697  1.39  christos 
    698  1.39  christos 	(void)bc;
    699  1.39  christos 	SETERROR(REG_EMPTY);
    700  1.39  christos 	return (false);
    701  1.39  christos }
    702  1.39  christos 
    703  1.39  christos /*
    704  1.39  christos  * Take care of any branching requirements. This includes inserting the
    705  1.39  christos  * appropriate branching instructions as well as eating all of the branch
    706  1.39  christos  * delimiters until we either run out of pattern or need to parse more pattern.
    707  1.39  christos  */
    708  1.39  christos static bool
    709  1.39  christos p_branch_do(struct parse *p, struct branchc *bc)
    710  1.39  christos {
    711  1.39  christos 	int ate = 0;
    712  1.39  christos 
    713  1.39  christos 	ate = p_branch_eat_delim(p, bc);
    714  1.39  christos 	if (ate == 0)
    715  1.39  christos 		return (false);
    716  1.39  christos 	else if ((ate > 1 || (bc->outer && !MORE())) && !p_branch_empty(p, bc))
    717  1.39  christos 		/*
    718  1.39  christos 		 * Halt parsing only if we have an empty branch and p_branch_empty
    719  1.39  christos 		 * indicates that we must not continue. In the future, this will not
    720  1.39  christos 		 * necessarily be an error.
    721  1.39  christos 		 */
    722  1.39  christos 		return (false);
    723  1.39  christos 	p_branch_ins_offset(p, bc);
    724  1.39  christos 
    725  1.39  christos 	return (true);
    726  1.39  christos }
    727  1.30  christos 
    728  1.39  christos static void
    729  1.39  christos p_bre_pre_parse(struct parse *p, struct branchc *bc)
    730  1.39  christos {
    731  1.14     lukem 
    732  1.40  christos 	(void)bc;
    733  1.39  christos 	/*
    734  1.39  christos 	 * Does not move cleanly into expression parser because of
    735  1.39  christos 	 * ordinary interpration of * at the beginning position of
    736  1.39  christos 	 * an expression.
    737  1.39  christos 	 */
    738   1.1       jtc 	if (EAT('^')) {
    739   1.1       jtc 		EMIT(OBOL, 0);
    740   1.1       jtc 		p->g->iflags |= USEBOL;
    741   1.1       jtc 		p->g->nbol++;
    742   1.1       jtc 	}
    743  1.39  christos }
    744  1.39  christos 
    745  1.39  christos static void
    746  1.39  christos p_bre_post_parse(struct parse *p, struct branchc *bc)
    747  1.39  christos {
    748  1.39  christos 
    749  1.39  christos 	/* Expression is terminating due to EOL token */
    750  1.39  christos 	if (bc->terminate) {
    751   1.1       jtc 		DROP(1);
    752   1.1       jtc 		EMIT(OEOL, 0);
    753   1.1       jtc 		p->g->iflags |= USEEOL;
    754   1.1       jtc 		p->g->neol++;
    755   1.1       jtc 	}
    756  1.39  christos }
    757   1.1       jtc 
    758  1.39  christos /*
    759  1.39  christos  - p_re - Top level parser, concatenation and BRE anchoring
    760  1.39  christos  == static void p_re(struct parse *p, int end1, int end2);
    761  1.39  christos  * Giving end1 as OUT essentially eliminates the end1/end2 check.
    762  1.39  christos  *
    763  1.39  christos  * This implementation is a bit of a kludge, in that a trailing $ is first
    764  1.39  christos  * taken as an ordinary character and then revised to be an anchor.
    765  1.39  christos  * The amount of lookahead needed to avoid this kludge is excessive.
    766  1.39  christos  */
    767  1.39  christos static void
    768  1.39  christos p_re(struct parse *p,
    769  1.39  christos 	int end1,	/* first terminating character */
    770  1.39  christos 	int end2)	/* second terminating character; ignored for EREs */
    771  1.39  christos {
    772  1.39  christos 	struct branchc bc;
    773  1.39  christos 
    774  1.39  christos 	bc.nbranch = 0;
    775  1.39  christos 	if (end1 == OUT && end2 == OUT)
    776  1.39  christos 		bc.outer = true;
    777  1.39  christos 	else
    778  1.39  christos 		bc.outer = false;
    779  1.39  christos #define	SEEEND()	(!p->bre ? SEE(end1) : SEETWO(end1, end2))
    780  1.39  christos 	for (;;) {
    781  1.39  christos 		bc.start = HERE();
    782  1.39  christos 		bc.nchain = 0;
    783  1.39  christos 		bc.terminate = false;
    784  1.39  christos 		if (p->pre_parse != NULL)
    785  1.39  christos 			p->pre_parse(p, &bc);
    786  1.39  christos 		while (MORE() && (!p->allowbranch || !SEESPEC('|')) && !SEEEND()) {
    787  1.39  christos 			bc.terminate = p->parse_expr(p, &bc);
    788  1.39  christos 			++bc.nchain;
    789  1.39  christos 		}
    790  1.39  christos 		if (p->post_parse != NULL)
    791  1.39  christos 			p->post_parse(p, &bc);
    792  1.39  christos 		(void) REQUIRE(p->gnuext || HERE() != bc.start, REG_EMPTY);
    793  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    794  1.39  christos 		if (p->gnuext && HERE() == bc.start && !p_branch_empty(p, &bc))
    795  1.39  christos 			break;
    796  1.39  christos #endif
    797  1.39  christos 		if (!p->allowbranch)
    798  1.39  christos 			break;
    799  1.39  christos 		/*
    800  1.39  christos 		 * p_branch_do's return value indicates whether we should
    801  1.39  christos 		 * continue parsing or not. This is both for correctness and
    802  1.39  christos 		 * a slight optimization, because it will check if we've
    803  1.39  christos 		 * encountered an empty branch or the end of the string
    804  1.39  christos 		 * immediately following a branch delimiter.
    805  1.39  christos 		 */
    806  1.39  christos 		if (!p_branch_do(p, &bc))
    807  1.39  christos 			break;
    808  1.39  christos 	}
    809  1.39  christos #undef SEE_END
    810  1.39  christos 	if (p->allowbranch)
    811  1.39  christos 		p_branch_fix_tail(p, &bc);
    812  1.39  christos 	assert(!MORE() || SEE(end1));
    813   1.1       jtc }
    814   1.1       jtc 
    815   1.1       jtc /*
    816   1.1       jtc  - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
    817  1.39  christos  == static bool p_simp_re(struct parse *p, struct branchc *bc);
    818   1.1       jtc  */
    819  1.39  christos static bool			/* was the simple RE an unbackslashed $? */
    820  1.39  christos p_simp_re(struct parse *p, struct branchc *bc)
    821   1.1       jtc {
    822   1.9     perry 	int c;
    823  1.39  christos 	int cc;			/* convenient/control character */
    824   1.9     perry 	int count;
    825   1.9     perry 	int count2;
    826  1.39  christos 	sopno pos;
    827  1.39  christos 	bool handled;
    828  1.40  christos 	size_t i;
    829  1.39  christos 	wint_t wc;
    830   1.9     perry 	sopno subno;
    831   1.1       jtc #	define	BACKSL	(1<<CHAR_BIT)
    832   1.1       jtc 
    833  1.39  christos 	pos = HERE();		/* repetition op, if any, covers from here */
    834  1.39  christos 	handled = false;
    835   1.1       jtc 
    836   1.1       jtc 	assert(MORE());		/* caller should have ensured this */
    837   1.1       jtc 	c = GETNEXT();
    838   1.1       jtc 	if (c == '\\') {
    839  1.39  christos 		(void)REQUIRE(MORE(), REG_EESCAPE);
    840  1.39  christos 		cc = GETNEXT();
    841  1.39  christos 		c = BACKSL | cc;
    842  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    843  1.39  christos 		if (p->gnuext) {
    844  1.39  christos 			handled = true;
    845  1.39  christos 			switch (c) {
    846  1.39  christos 			case BACKSL|'`':
    847  1.39  christos 				EMIT(OBOS, 0);
    848  1.39  christos 				break;
    849  1.39  christos 			case BACKSL|'\'':
    850  1.39  christos 				EMIT(OEOS, 0);
    851  1.39  christos 				break;
    852  1.39  christos 			case BACKSL|'B':
    853  1.39  christos 				EMIT(ONWBND, 0);
    854  1.39  christos 				break;
    855  1.39  christos 			case BACKSL|'b':
    856  1.39  christos 				EMIT(OWBND, 0);
    857  1.39  christos 				break;
    858  1.39  christos 			case BACKSL|'W':
    859  1.39  christos 			case BACKSL|'w':
    860  1.39  christos 			case BACKSL|'S':
    861  1.39  christos 			case BACKSL|'s':
    862  1.39  christos 				p_b_pseudoclass(p, cc);
    863  1.39  christos 				break;
    864  1.39  christos 			default:
    865  1.39  christos 				handled = false;
    866  1.39  christos 			}
    867  1.39  christos 		}
    868  1.39  christos #endif
    869   1.1       jtc 	}
    870  1.39  christos 	if (!handled) {
    871  1.39  christos 		switch (c) {
    872  1.39  christos 		case '.':
    873  1.39  christos 			if (p->g->cflags&REG_NEWLINE)
    874  1.39  christos 				nonnewline(p);
    875  1.39  christos 			else
    876  1.39  christos 				EMIT(OANY, 0);
    877  1.39  christos 			break;
    878  1.39  christos 		case '[':
    879  1.39  christos 			p_bracket(p);
    880  1.39  christos 			break;
    881  1.39  christos 		case BACKSL|'<':
    882  1.39  christos 			EMIT(OBOW, 0);
    883  1.39  christos 			break;
    884  1.39  christos 		case BACKSL|'>':
    885  1.39  christos 			EMIT(OEOW, 0);
    886  1.39  christos 			break;
    887  1.39  christos 		case BACKSL|'{':
    888  1.39  christos 			SETERROR(REG_BADRPT);
    889  1.39  christos 			break;
    890  1.39  christos 		case BACKSL|'(':
    891  1.39  christos 			p->g->nsub++;
    892  1.40  christos 			subno = (sopno)p->g->nsub;
    893  1.39  christos 			if (subno < NPAREN)
    894  1.39  christos 				p->pbegin[subno] = HERE();
    895  1.39  christos 			EMIT(OLPAREN, subno);
    896  1.39  christos 			/* the MORE here is an error heuristic */
    897  1.39  christos 			if (MORE() && !SEETWO('\\', ')'))
    898  1.39  christos 				p_re(p, '\\', ')');
    899  1.39  christos 			if (subno < NPAREN) {
    900  1.39  christos 				p->pend[subno] = HERE();
    901  1.39  christos 				assert(p->pend[subno] != 0);
    902  1.39  christos 			}
    903  1.39  christos 			EMIT(ORPAREN, subno);
    904  1.39  christos 			(void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN);
    905  1.39  christos 			break;
    906  1.39  christos 		case BACKSL|')':	/* should not get here -- must be user */
    907  1.39  christos 			SETERROR(REG_EPAREN);
    908  1.39  christos 			break;
    909  1.39  christos 		case BACKSL|'1':
    910  1.39  christos 		case BACKSL|'2':
    911  1.39  christos 		case BACKSL|'3':
    912  1.39  christos 		case BACKSL|'4':
    913  1.39  christos 		case BACKSL|'5':
    914  1.39  christos 		case BACKSL|'6':
    915  1.39  christos 		case BACKSL|'7':
    916  1.39  christos 		case BACKSL|'8':
    917  1.39  christos 		case BACKSL|'9':
    918  1.39  christos 			i = (c&~BACKSL) - '0';
    919  1.39  christos 			assert(i < NPAREN);
    920  1.39  christos 			if (p->pend[i] != 0) {
    921  1.39  christos 				assert(i <= p->g->nsub);
    922  1.39  christos 				EMIT(OBACK_, i);
    923  1.39  christos 				assert(p->pbegin[i] != 0);
    924  1.39  christos 				assert(OP(p->strip[p->pbegin[i]]) == OLPAREN);
    925  1.39  christos 				assert(OP(p->strip[p->pend[i]]) == ORPAREN);
    926  1.39  christos 				(void) dupl(p, p->pbegin[i]+1, p->pend[i]);
    927  1.39  christos 				EMIT(O_BACK, i);
    928  1.39  christos 			} else
    929  1.39  christos 				SETERROR(REG_ESUBREG);
    930  1.39  christos 			p->g->backrefs = 1;
    931  1.39  christos 			break;
    932  1.39  christos 		case '*':
    933  1.39  christos 			/*
    934  1.39  christos 			 * Ordinary if used as the first character beyond BOL anchor of
    935  1.39  christos 			 * a (sub-)expression, counts as a bad repetition operator if it
    936  1.39  christos 			 * appears otherwise.
    937  1.39  christos 			 */
    938  1.39  christos 			(void)REQUIRE(bc->nchain == 0, REG_BADRPT);
    939  1.39  christos 			/* FALLTHROUGH */
    940  1.39  christos 		default:
    941  1.39  christos 			if (p->error != 0)
    942  1.39  christos 				return (false);	/* Definitely not $... */
    943  1.39  christos 			p->next--;
    944  1.39  christos 			wc = WGETNEXT();
    945  1.39  christos 			if ((c & BACKSL) == 0 || may_escape(p, wc))
    946  1.39  christos 				ordinary(p, wc);
    947  1.39  christos 			else
    948  1.39  christos 				SETERROR(REG_EESCAPE);
    949  1.39  christos 			break;
    950   1.1       jtc 		}
    951   1.1       jtc 	}
    952   1.1       jtc 
    953   1.1       jtc 	if (EAT('*')) {		/* implemented as +? */
    954   1.4       jtc 		/* this case does not require the (y|) trick, noKLUDGE */
    955   1.1       jtc 		INSERT(OPLUS_, pos);
    956   1.1       jtc 		ASTERN(O_PLUS, pos);
    957   1.1       jtc 		INSERT(OQUEST_, pos);
    958   1.1       jtc 		ASTERN(O_QUEST, pos);
    959  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
    960  1.39  christos 	} else if (p->gnuext && EATTWO('\\', '?')) {
    961  1.39  christos 		INSERT(OQUEST_, pos);
    962  1.39  christos 		ASTERN(O_QUEST, pos);
    963  1.39  christos 	} else if (p->gnuext && EATTWO('\\', '+')) {
    964  1.39  christos 		INSERT(OPLUS_, pos);
    965  1.39  christos 		ASTERN(O_PLUS, pos);
    966  1.39  christos #endif
    967   1.1       jtc 	} else if (EATTWO('\\', '{')) {
    968   1.1       jtc 		count = p_count(p);
    969   1.1       jtc 		if (EAT(',')) {
    970  1.39  christos 			if (MORE() && isdigit((uch)PEEK())) {
    971   1.1       jtc 				count2 = p_count(p);
    972  1.39  christos 				(void)REQUIRE(count <= count2, REG_BADBR);
    973   1.1       jtc 			} else		/* single number with comma */
    974   1.1       jtc 				count2 = INFINITY;
    975   1.1       jtc 		} else		/* just a single number */
    976   1.1       jtc 			count2 = count;
    977  1.39  christos 		repeat(p, pos, count, count2);
    978   1.1       jtc 		if (!EATTWO('\\', '}')) {	/* error heuristics */
    979   1.1       jtc 			while (MORE() && !SEETWO('\\', '}'))
    980   1.1       jtc 				NEXT();
    981  1.39  christos 			(void)REQUIRE(MORE(), REG_EBRACE);
    982   1.1       jtc 			SETERROR(REG_BADBR);
    983   1.1       jtc 		}
    984  1.39  christos 	} else if (c == '$')     /* $ (but not \$) ends it */
    985  1.39  christos 		return (true);
    986   1.1       jtc 
    987  1.39  christos 	return (false);
    988   1.1       jtc }
    989   1.1       jtc 
    990   1.1       jtc /*
    991   1.1       jtc  - p_count - parse a repetition count
    992   1.9     perry  == static int p_count(struct parse *p);
    993   1.1       jtc  */
    994   1.1       jtc static int			/* the value */
    995  1.39  christos p_count(struct parse *p)
    996   1.1       jtc {
    997   1.9     perry 	int count = 0;
    998   1.9     perry 	int ndigits = 0;
    999   1.1       jtc 
   1000  1.39  christos 	while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) {
   1001   1.1       jtc 		count = count*10 + (GETNEXT() - '0');
   1002   1.1       jtc 		ndigits++;
   1003   1.1       jtc 	}
   1004   1.1       jtc 
   1005  1.39  christos 	(void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR);
   1006   1.1       jtc 	return(count);
   1007   1.1       jtc }
   1008   1.1       jtc 
   1009   1.1       jtc /*
   1010   1.1       jtc  - p_bracket - parse a bracketed character list
   1011   1.9     perry  == static void p_bracket(struct parse *p);
   1012   1.1       jtc  */
   1013   1.1       jtc static void
   1014  1.39  christos p_bracket(struct parse *p)
   1015   1.1       jtc {
   1016  1.14     lukem 	cset *cs;
   1017  1.39  christos 	wint_t ch;
   1018  1.14     lukem 
   1019   1.1       jtc 	/* Dept of Truly Sickening Special-Case Kludges */
   1020  1.39  christos 	if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) {
   1021   1.1       jtc 		EMIT(OBOW, 0);
   1022   1.1       jtc 		NEXTn(6);
   1023   1.1       jtc 		return;
   1024   1.1       jtc 	}
   1025  1.39  christos 	if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) {
   1026   1.1       jtc 		EMIT(OEOW, 0);
   1027   1.1       jtc 		NEXTn(6);
   1028   1.1       jtc 		return;
   1029   1.1       jtc 	}
   1030   1.1       jtc 
   1031  1.39  christos 	if ((cs = allocset(p)) == NULL)
   1032  1.39  christos 		return;
   1033  1.39  christos 
   1034  1.39  christos 	if (p->g->cflags&REG_ICASE)
   1035  1.39  christos 		cs->icase = 1;
   1036   1.1       jtc 	if (EAT('^'))
   1037  1.39  christos 		cs->invert = 1;
   1038   1.1       jtc 	if (EAT(']'))
   1039  1.39  christos 		CHadd(p, cs, ']');
   1040   1.1       jtc 	else if (EAT('-'))
   1041  1.39  christos 		CHadd(p, cs, '-');
   1042   1.1       jtc 	while (MORE() && PEEK() != ']' && !SEETWO('-', ']'))
   1043   1.1       jtc 		p_b_term(p, cs);
   1044   1.1       jtc 	if (EAT('-'))
   1045  1.39  christos 		CHadd(p, cs, '-');
   1046  1.39  christos 	(void)MUSTEAT(']', REG_EBRACK);
   1047   1.1       jtc 
   1048   1.1       jtc 	if (p->error != 0)	/* don't mess things up further */
   1049   1.1       jtc 		return;
   1050   1.1       jtc 
   1051  1.39  christos 	if (cs->invert && p->g->cflags&REG_NEWLINE)
   1052  1.39  christos 		cs->bmp['\n' >> 3] |= 1 << ('\n' & 7);
   1053   1.1       jtc 
   1054  1.39  christos 	if ((ch = singleton(cs)) != OUT) {	/* optimize singleton sets */
   1055  1.39  christos 		ordinary(p, ch);
   1056  1.39  christos 		freeset(p, cs);
   1057  1.39  christos 	} else
   1058  1.40  christos 		EMIT(OANYOF, (size_t)(cs - p->g->sets));
   1059  1.39  christos }
   1060   1.1       jtc 
   1061  1.39  christos static int
   1062  1.39  christos p_range_cmp(wchar_t c1, wchar_t c2)
   1063  1.39  christos {
   1064  1.39  christos #ifdef REGEX_LIBC_COLLATE
   1065  1.39  christos 	return __wcollate_range_cmp(c1, c2);
   1066  1.39  christos #else
   1067  1.39  christos 	/* Copied from libc/collate __wcollate_range_cmp */
   1068  1.39  christos 	wchar_t s1[2], s2[2];
   1069   1.1       jtc 
   1070  1.39  christos 	s1[0] = c1;
   1071  1.39  christos 	s1[1] = L'\0';
   1072  1.39  christos 	s2[0] = c2;
   1073  1.39  christos 	s2[1] = L'\0';
   1074  1.39  christos 	return (wcscoll(s1, s2));
   1075  1.39  christos #endif
   1076   1.1       jtc }
   1077   1.1       jtc 
   1078   1.1       jtc /*
   1079   1.1       jtc  - p_b_term - parse one term of a bracketed character list
   1080   1.9     perry  == static void p_b_term(struct parse *p, cset *cs);
   1081   1.1       jtc  */
   1082   1.1       jtc static void
   1083  1.39  christos p_b_term(struct parse *p, cset *cs)
   1084   1.1       jtc {
   1085   1.9     perry 	char c;
   1086  1.39  christos 	wint_t start, finish;
   1087  1.39  christos 	wint_t i;
   1088  1.39  christos #ifdef REGEX_LIBC_COLLATE
   1089  1.39  christos 	struct xlocale_collate *table =
   1090  1.39  christos 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
   1091  1.39  christos #endif
   1092   1.1       jtc 
   1093  1.14     lukem 	_DIAGASSERT(p != NULL);
   1094  1.14     lukem 	_DIAGASSERT(cs != NULL);
   1095  1.14     lukem 
   1096   1.1       jtc 	/* classify what we've got */
   1097   1.1       jtc 	switch ((MORE()) ? PEEK() : '\0') {
   1098   1.1       jtc 	case '[':
   1099   1.1       jtc 		c = (MORE2()) ? PEEK2() : '\0';
   1100   1.1       jtc 		break;
   1101   1.1       jtc 	case '-':
   1102   1.1       jtc 		SETERROR(REG_ERANGE);
   1103   1.1       jtc 		return;			/* NOTE RETURN */
   1104   1.1       jtc 	default:
   1105   1.1       jtc 		c = '\0';
   1106   1.1       jtc 		break;
   1107   1.1       jtc 	}
   1108   1.1       jtc 
   1109   1.1       jtc 	switch (c) {
   1110   1.1       jtc 	case ':':		/* character class */
   1111   1.1       jtc 		NEXT2();
   1112  1.39  christos 		(void)REQUIRE(MORE(), REG_EBRACK);
   1113   1.1       jtc 		c = PEEK();
   1114  1.39  christos 		(void)REQUIRE(c != '-' && c != ']', REG_ECTYPE);
   1115   1.1       jtc 		p_b_cclass(p, cs);
   1116  1.39  christos 		(void)REQUIRE(MORE(), REG_EBRACK);
   1117  1.39  christos 		(void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE);
   1118   1.1       jtc 		break;
   1119   1.1       jtc 	case '=':		/* equivalence class */
   1120   1.1       jtc 		NEXT2();
   1121  1.39  christos 		(void)REQUIRE(MORE(), REG_EBRACK);
   1122   1.1       jtc 		c = PEEK();
   1123  1.39  christos 		(void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE);
   1124   1.1       jtc 		p_b_eclass(p, cs);
   1125  1.39  christos 		(void)REQUIRE(MORE(), REG_EBRACK);
   1126  1.39  christos 		(void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE);
   1127   1.1       jtc 		break;
   1128   1.1       jtc 	default:		/* symbol, ordinary character, or range */
   1129   1.1       jtc 		start = p_b_symbol(p);
   1130   1.1       jtc 		if (SEE('-') && MORE2() && PEEK2() != ']') {
   1131   1.1       jtc 			/* range */
   1132   1.1       jtc 			NEXT();
   1133   1.1       jtc 			if (EAT('-'))
   1134   1.1       jtc 				finish = '-';
   1135   1.1       jtc 			else
   1136   1.1       jtc 				finish = p_b_symbol(p);
   1137   1.1       jtc 		} else
   1138   1.1       jtc 			finish = start;
   1139  1.39  christos 		if (start == finish)
   1140  1.39  christos 			CHadd(p, cs, start);
   1141  1.39  christos 		else {
   1142  1.39  christos #ifdef REGEX_LIBC_COLLATE
   1143  1.39  christos 			if (table->__collate_load_error || MB_CUR_MAX > 1) {
   1144  1.39  christos #else
   1145  1.39  christos 			if (MB_CUR_MAX > 1) {
   1146  1.39  christos #endif
   1147  1.39  christos 				(void)REQUIRE(start <= finish, REG_ERANGE);
   1148  1.39  christos 				CHaddrange(p, cs, start, finish);
   1149  1.39  christos 			} else {
   1150  1.39  christos 				(void)REQUIRE(p_range_cmp(start, finish) <= 0, REG_ERANGE);
   1151  1.39  christos 				for (i = 0; i <= UCHAR_MAX; i++) {
   1152  1.39  christos 					if (p_range_cmp(start, i) <= 0 &&
   1153  1.39  christos 					    p_range_cmp(i, finish) <= 0 )
   1154  1.39  christos 						CHadd(p, cs, i);
   1155  1.39  christos 				}
   1156  1.39  christos 			}
   1157  1.39  christos 		}
   1158  1.39  christos 		break;
   1159  1.39  christos 	}
   1160  1.39  christos }
   1161  1.39  christos 
   1162  1.39  christos #ifdef REGEX_GNU_EXTENSIONS
   1163  1.39  christos /*
   1164  1.39  christos  - p_b_pseudoclass - parse a pseudo-class (\w, \W, \s, \S)
   1165  1.39  christos  == static int p_b_pseudoclass(struct parse *p, char c)
   1166  1.39  christos  */
   1167  1.39  christos static int
   1168  1.39  christos p_b_pseudoclass(struct parse *p, char c) {
   1169  1.39  christos 	cset *cs;
   1170  1.39  christos 
   1171  1.39  christos 	if ((cs = allocset(p)) == NULL)
   1172  1.39  christos 		return(0);
   1173  1.39  christos 
   1174  1.39  christos 	if (p->g->cflags&REG_ICASE)
   1175  1.39  christos 		cs->icase = 1;
   1176  1.39  christos 
   1177  1.39  christos 	switch (c) {
   1178  1.39  christos 	case 'W':
   1179  1.39  christos 		cs->invert = 1;
   1180  1.39  christos 		/* FALLTHROUGH */
   1181  1.39  christos 	case 'w':
   1182  1.39  christos 		p_b_cclass_named(p, cs, "alnum");
   1183   1.1       jtc 		break;
   1184  1.39  christos 	case 'S':
   1185  1.39  christos 		cs->invert = 1;
   1186  1.39  christos 		/* FALLTHROUGH */
   1187  1.39  christos 	case 's':
   1188  1.39  christos 		p_b_cclass_named(p, cs, "space");
   1189  1.39  christos 		break;
   1190  1.39  christos 	default:
   1191  1.39  christos 		return(0);
   1192   1.1       jtc 	}
   1193  1.39  christos 
   1194  1.40  christos 	EMIT(OANYOF, (size_t)(cs - p->g->sets));
   1195  1.39  christos 	return(1);
   1196   1.1       jtc }
   1197  1.39  christos #endif
   1198   1.1       jtc 
   1199   1.1       jtc /*
   1200   1.1       jtc  - p_b_cclass - parse a character-class name and deal with it
   1201   1.9     perry  == static void p_b_cclass(struct parse *p, cset *cs);
   1202   1.1       jtc  */
   1203   1.1       jtc static void
   1204  1.39  christos p_b_cclass(struct parse *p, cset *cs)
   1205   1.1       jtc {
   1206  1.39  christos 	const char *sp = p->next;
   1207   1.9     perry 	size_t len;
   1208  1.39  christos 	char clname[16];
   1209   1.1       jtc 
   1210  1.39  christos 	while (MORE() && isalpha((uch)PEEK()))
   1211   1.1       jtc 		NEXT();
   1212   1.1       jtc 	len = p->next - sp;
   1213  1.39  christos 	if (len >= sizeof(clname) - 1) {
   1214   1.1       jtc 		SETERROR(REG_ECTYPE);
   1215   1.1       jtc 		return;
   1216   1.1       jtc 	}
   1217  1.39  christos 	memcpy(clname, sp, len);
   1218  1.39  christos 	clname[len] = '\0';
   1219  1.39  christos 
   1220  1.39  christos 	p_b_cclass_named(p, cs, clname);
   1221  1.39  christos }
   1222  1.39  christos /*
   1223  1.39  christos  - p_b_cclass_named - deal with a named character class
   1224  1.39  christos  == static void p_b_cclass_named(struct parse *p, cset *cs, const char []);
   1225  1.39  christos  */
   1226  1.39  christos static void
   1227  1.39  christos p_b_cclass_named(struct parse *p, cset *cs, const char clname[]) {
   1228  1.39  christos 	wctype_t wct;
   1229   1.1       jtc 
   1230  1.39  christos 	if ((wct = wctype(clname)) == 0) {
   1231  1.39  christos 		SETERROR(REG_ECTYPE);
   1232  1.39  christos 		return;
   1233  1.39  christos 	}
   1234  1.39  christos 	CHaddtype(p, cs, wct);
   1235   1.1       jtc }
   1236   1.1       jtc 
   1237   1.1       jtc /*
   1238   1.1       jtc  - p_b_eclass - parse an equivalence-class name and deal with it
   1239   1.9     perry  == static void p_b_eclass(struct parse *p, cset *cs);
   1240   1.1       jtc  *
   1241   1.1       jtc  * This implementation is incomplete. xxx
   1242   1.1       jtc  */
   1243   1.1       jtc static void
   1244  1.39  christos p_b_eclass(struct parse *p, cset *cs)
   1245   1.1       jtc {
   1246  1.39  christos 	wint_t c;
   1247   1.1       jtc 
   1248  1.14     lukem 	_DIAGASSERT(p != NULL);
   1249  1.14     lukem 	_DIAGASSERT(cs != NULL);
   1250  1.14     lukem 
   1251   1.1       jtc 	c = p_b_coll_elem(p, '=');
   1252  1.39  christos 	CHadd(p, cs, c);
   1253   1.1       jtc }
   1254   1.1       jtc 
   1255   1.1       jtc /*
   1256   1.1       jtc  - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
   1257  1.39  christos  == static wint_t p_b_symbol(struct parse *p);
   1258   1.1       jtc  */
   1259  1.39  christos static wint_t			/* value of symbol */
   1260  1.39  christos p_b_symbol(struct parse *p)
   1261   1.1       jtc {
   1262  1.39  christos 	wint_t value;
   1263   1.1       jtc 
   1264  1.14     lukem 	_DIAGASSERT(p != NULL);
   1265  1.14     lukem 
   1266  1.39  christos 	(void)REQUIRE(MORE(), REG_EBRACK);
   1267   1.1       jtc 	if (!EATTWO('[', '.'))
   1268  1.39  christos 		return(WGETNEXT());
   1269   1.1       jtc 
   1270   1.1       jtc 	/* collating symbol */
   1271   1.1       jtc 	value = p_b_coll_elem(p, '.');
   1272  1.39  christos 	(void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE);
   1273   1.1       jtc 	return(value);
   1274   1.1       jtc }
   1275   1.1       jtc 
   1276   1.1       jtc /*
   1277   1.1       jtc  - p_b_coll_elem - parse a collating-element name and look it up
   1278  1.39  christos  == static wint_t p_b_coll_elem(struct parse *p, wint_t endc);
   1279   1.1       jtc  */
   1280  1.39  christos static wint_t			/* value of collating element */
   1281  1.39  christos p_b_coll_elem(struct parse *p,
   1282  1.39  christos 	wint_t endc)		/* name ended by endc,']' */
   1283  1.39  christos {
   1284  1.39  christos 	const char *sp = p->next;
   1285  1.39  christos 	struct cname *cp;
   1286  1.39  christos 	mbstate_t mbs;
   1287  1.39  christos 	wchar_t wc;
   1288  1.39  christos 	size_t clen, len;
   1289   1.1       jtc 
   1290  1.14     lukem 	_DIAGASSERT(p != NULL);
   1291  1.14     lukem 
   1292   1.1       jtc 	while (MORE() && !SEETWO(endc, ']'))
   1293   1.1       jtc 		NEXT();
   1294   1.1       jtc 	if (!MORE()) {
   1295   1.1       jtc 		SETERROR(REG_EBRACK);
   1296   1.1       jtc 		return(0);
   1297   1.1       jtc 	}
   1298   1.1       jtc 	len = p->next - sp;
   1299   1.1       jtc 	for (cp = cnames; cp->name != NULL; cp++)
   1300  1.37  christos 		if (strncmp(cp->name, sp, len) == 0 && strlen(cp->name) == len)
   1301   1.1       jtc 			return(cp->code);	/* known name */
   1302  1.39  christos 	memset(&mbs, 0, sizeof(mbs));
   1303  1.39  christos 	if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len)
   1304  1.39  christos 		return (wc);			/* single character */
   1305  1.39  christos 	else if (clen == (size_t)-1 || clen == (size_t)-2)
   1306  1.39  christos 		SETERROR(REG_ILLSEQ);
   1307  1.39  christos 	else
   1308  1.39  christos 		SETERROR(REG_ECOLLATE);		/* neither */
   1309   1.1       jtc 	return(0);
   1310   1.1       jtc }
   1311   1.1       jtc 
   1312   1.1       jtc /*
   1313  1.39  christos  - may_escape - determine whether 'ch' is escape-able in the current context
   1314  1.39  christos  == static int may_escape(struct parse *p, const wint_t ch)
   1315  1.39  christos  */
   1316  1.39  christos static bool
   1317  1.39  christos may_escape(struct parse *p, const wint_t ch)
   1318  1.39  christos {
   1319  1.39  christos 
   1320  1.39  christos 	if ((p->pflags & PFLAG_LEGACY_ESC) != 0)
   1321  1.39  christos 		return (true);
   1322  1.39  christos 	if (isalpha(ch) || ch == '\'' || ch == '`')
   1323  1.39  christos 		return (false);
   1324  1.39  christos 	return (true);
   1325  1.39  christos #ifdef NOTYET
   1326  1.39  christos 	/*
   1327  1.39  christos 	 * Build a whitelist of characters that may be escaped to produce an
   1328  1.39  christos 	 * ordinary in the current context. This assumes that these have not
   1329  1.39  christos 	 * been otherwise interpreted as a special character. Escaping an
   1330  1.39  christos 	 * ordinary character yields undefined results according to
   1331  1.39  christos 	 * IEEE 1003.1-2008. Some extensions (notably, some GNU extensions) take
   1332  1.39  christos 	 * advantage of this and use escaped ordinary characters to provide
   1333  1.39  christos 	 * special meaning, e.g. \b, \B, \w, \W, \s, \S.
   1334  1.39  christos 	 */
   1335  1.39  christos 	switch(ch) {
   1336  1.39  christos 	case '|':
   1337  1.39  christos 	case '+':
   1338  1.39  christos 	case '?':
   1339  1.39  christos 		/* The above characters may not be escaped in BREs */
   1340  1.39  christos 		if (!(p->g->cflags&REG_EXTENDED))
   1341  1.39  christos 			return (false);
   1342  1.39  christos 		/* Fallthrough */
   1343  1.39  christos 	case '(':
   1344  1.39  christos 	case ')':
   1345  1.39  christos 	case '{':
   1346  1.39  christos 	case '}':
   1347  1.39  christos 	case '.':
   1348  1.39  christos 	case '[':
   1349  1.39  christos 	case ']':
   1350  1.39  christos 	case '\\':
   1351  1.39  christos 	case '*':
   1352  1.39  christos 	case '^':
   1353  1.39  christos 	case '$':
   1354  1.39  christos 		return (true);
   1355  1.39  christos 	default:
   1356  1.39  christos 		return (false);
   1357  1.39  christos 	}
   1358  1.39  christos #endif
   1359  1.39  christos }
   1360  1.39  christos 
   1361  1.39  christos /*
   1362   1.1       jtc  - othercase - return the case counterpart of an alphabetic
   1363  1.39  christos  == static wint_t othercase(wint_t ch);
   1364   1.1       jtc  */
   1365  1.39  christos static wint_t			/* if no counterpart, return ch */
   1366  1.39  christos othercase(wint_t ch)
   1367  1.39  christos {
   1368  1.39  christos 	assert(iswalpha(ch));
   1369  1.39  christos 	if (iswupper(ch))
   1370  1.39  christos 		return(towlower(ch));
   1371  1.39  christos 	else if (iswlower(ch))
   1372  1.39  christos 		return(towupper(ch));
   1373   1.1       jtc 	else			/* peculiar, but could happen */
   1374   1.1       jtc 		return(ch);
   1375   1.1       jtc }
   1376   1.1       jtc 
   1377   1.1       jtc /*
   1378   1.1       jtc  - bothcases - emit a dualcase version of a two-case character
   1379  1.39  christos  == static void bothcases(struct parse *p, wint_t ch);
   1380   1.1       jtc  *
   1381   1.1       jtc  * Boy, is this implementation ever a kludge...
   1382   1.1       jtc  */
   1383   1.1       jtc static void
   1384  1.39  christos bothcases(struct parse *p, wint_t ch)
   1385   1.1       jtc {
   1386  1.39  christos 	const char *oldnext = p->next;
   1387  1.39  christos 	const char *oldend = p->end;
   1388  1.39  christos 	char bracket[3 + MB_LEN_MAX];
   1389  1.39  christos 	size_t n;
   1390  1.39  christos 	mbstate_t mbs;
   1391   1.1       jtc 
   1392  1.14     lukem 	_DIAGASSERT(p != NULL);
   1393  1.14     lukem 
   1394   1.1       jtc 	assert(othercase(ch) != ch);	/* p_bracket() would recurse */
   1395   1.1       jtc 	p->next = bracket;
   1396  1.39  christos 	memset(&mbs, 0, sizeof(mbs));
   1397  1.39  christos 	n = wcrtomb(bracket, ch, &mbs);
   1398  1.39  christos 	assert(n != (size_t)-1);
   1399  1.39  christos 	bracket[n] = ']';
   1400  1.39  christos 	bracket[n + 1] = '\0';
   1401  1.39  christos 	p->end = bracket+n+1;
   1402   1.1       jtc 	p_bracket(p);
   1403  1.39  christos 	assert(p->next == p->end);
   1404   1.1       jtc 	p->next = oldnext;
   1405   1.1       jtc 	p->end = oldend;
   1406   1.1       jtc }
   1407   1.1       jtc 
   1408   1.1       jtc /*
   1409   1.1       jtc  - ordinary - emit an ordinary character
   1410  1.39  christos  == static void ordinary(struct parse *p, wint_t ch);
   1411   1.1       jtc  */
   1412   1.1       jtc static void
   1413  1.39  christos ordinary(struct parse *p, wint_t ch)
   1414   1.1       jtc {
   1415  1.39  christos 	cset *cs;
   1416   1.1       jtc 
   1417  1.14     lukem 	_DIAGASSERT(p != NULL);
   1418  1.14     lukem 
   1419  1.39  christos 	if ((p->g->cflags&REG_ICASE) && iswalpha(ch) && othercase(ch) != ch)
   1420  1.39  christos 		bothcases(p, ch);
   1421  1.40  christos 	else if ((wint_t)(ch & OPDMASK) == ch)
   1422  1.40  christos 		EMIT(OCHAR, (size_t)ch);
   1423   1.1       jtc 	else {
   1424  1.39  christos 		/*
   1425  1.39  christos 		 * Kludge: character is too big to fit into an OCHAR operand.
   1426  1.39  christos 		 * Emit a singleton set.
   1427  1.39  christos 		 */
   1428  1.39  christos 		if ((cs = allocset(p)) == NULL)
   1429  1.39  christos 			return;
   1430  1.39  christos 		CHadd(p, cs, ch);
   1431  1.40  christos 		EMIT(OANYOF, (size_t)(cs - p->g->sets));
   1432   1.1       jtc 	}
   1433   1.1       jtc }
   1434   1.1       jtc 
   1435   1.1       jtc /*
   1436   1.1       jtc  - nonnewline - emit REG_NEWLINE version of OANY
   1437   1.9     perry  == static void nonnewline(struct parse *p);
   1438   1.1       jtc  *
   1439   1.1       jtc  * Boy, is this implementation ever a kludge...
   1440   1.1       jtc  */
   1441   1.1       jtc static void
   1442  1.39  christos nonnewline(struct parse *p)
   1443   1.1       jtc {
   1444  1.39  christos 	const char *oldnext = p->next;
   1445  1.39  christos 	const char *oldend = p->end;
   1446   1.1       jtc 	char bracket[4];
   1447   1.1       jtc 
   1448  1.14     lukem 	_DIAGASSERT(p != NULL);
   1449  1.14     lukem 
   1450   1.1       jtc 	p->next = bracket;
   1451   1.1       jtc 	p->end = bracket+3;
   1452   1.1       jtc 	bracket[0] = '^';
   1453   1.1       jtc 	bracket[1] = '\n';
   1454   1.1       jtc 	bracket[2] = ']';
   1455   1.1       jtc 	bracket[3] = '\0';
   1456   1.1       jtc 	p_bracket(p);
   1457   1.1       jtc 	assert(p->next == bracket+3);
   1458   1.1       jtc 	p->next = oldnext;
   1459   1.1       jtc 	p->end = oldend;
   1460   1.1       jtc }
   1461   1.1       jtc 
   1462   1.1       jtc /*
   1463   1.1       jtc  - repeat - generate code for a bounded repetition, recursively if needed
   1464  1.39  christos  == static void repeat(struct parse *p, sopno start, int from, int to);
   1465   1.1       jtc  */
   1466   1.1       jtc static void
   1467  1.39  christos repeat(struct parse *p,
   1468  1.39  christos 	sopno start,		/* operand from here to end of strip */
   1469  1.39  christos 	int from,		/* repeated from this number */
   1470  1.39  christos 	int to)			/* to this number of times (maybe INFINITY) */
   1471   1.1       jtc {
   1472  1.39  christos 	sopno finish = HERE();
   1473   1.1       jtc #	define	N	2
   1474   1.1       jtc #	define	INF	3
   1475   1.1       jtc #	define	REP(f, t)	((f)*8 + (t))
   1476   1.1       jtc #	define	MAP(n)	(((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N)
   1477   1.9     perry 	sopno copy;
   1478   1.1       jtc 
   1479  1.14     lukem 	_DIAGASSERT(p != NULL);
   1480  1.14     lukem 
   1481  1.39  christos 	if (p->error != 0)	/* head off possible runaway recursion */
   1482  1.30  christos 		return;
   1483  1.30  christos 
   1484   1.1       jtc 	assert(from <= to);
   1485   1.1       jtc 
   1486   1.1       jtc 	switch (REP(MAP(from), MAP(to))) {
   1487   1.1       jtc 	case REP(0, 0):			/* must be user doing this */
   1488   1.1       jtc 		DROP(finish-start);	/* drop the operand */
   1489   1.1       jtc 		break;
   1490   1.1       jtc 	case REP(0, 1):			/* as x{1,1}? */
   1491   1.1       jtc 	case REP(0, N):			/* as x{1,n}? */
   1492   1.1       jtc 	case REP(0, INF):		/* as x{1,}? */
   1493   1.4       jtc 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
   1494   1.4       jtc 		INSERT(OCH_, start);		/* offset is wrong... */
   1495  1.39  christos 		repeat(p, start+1, 1, to);
   1496   1.4       jtc 		ASTERN(OOR1, start);
   1497   1.1       jtc 		AHEAD(start);			/* ... fix it */
   1498   1.4       jtc 		EMIT(OOR2, 0);
   1499   1.4       jtc 		AHEAD(THERE());
   1500   1.4       jtc 		ASTERN(O_CH, THERETHERE());
   1501   1.1       jtc 		break;
   1502   1.1       jtc 	case REP(1, 1):			/* trivial case */
   1503   1.1       jtc 		/* done */
   1504   1.1       jtc 		break;
   1505   1.1       jtc 	case REP(1, N):			/* as x?x{1,n-1} */
   1506   1.4       jtc 		/* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
   1507   1.4       jtc 		INSERT(OCH_, start);
   1508   1.4       jtc 		ASTERN(OOR1, start);
   1509   1.4       jtc 		AHEAD(start);
   1510   1.4       jtc 		EMIT(OOR2, 0);			/* offset very wrong... */
   1511   1.4       jtc 		AHEAD(THERE());			/* ...so fix it */
   1512   1.4       jtc 		ASTERN(O_CH, THERETHERE());
   1513   1.1       jtc 		copy = dupl(p, start+1, finish+1);
   1514   1.4       jtc 		assert(copy == finish+4);
   1515  1.39  christos 		repeat(p, copy, 1, to-1);
   1516   1.1       jtc 		break;
   1517   1.1       jtc 	case REP(1, INF):		/* as x+ */
   1518   1.1       jtc 		INSERT(OPLUS_, start);
   1519   1.1       jtc 		ASTERN(O_PLUS, start);
   1520   1.1       jtc 		break;
   1521   1.1       jtc 	case REP(N, N):			/* as xx{m-1,n-1} */
   1522   1.1       jtc 		copy = dupl(p, start, finish);
   1523  1.39  christos 		repeat(p, copy, from-1, to-1);
   1524   1.1       jtc 		break;
   1525   1.1       jtc 	case REP(N, INF):		/* as xx{n-1,INF} */
   1526   1.1       jtc 		copy = dupl(p, start, finish);
   1527  1.39  christos 		repeat(p, copy, from-1, to);
   1528   1.1       jtc 		break;
   1529   1.1       jtc 	default:			/* "can't happen" */
   1530   1.1       jtc 		SETERROR(REG_ASSERT);	/* just in case */
   1531   1.1       jtc 		break;
   1532   1.1       jtc 	}
   1533   1.1       jtc }
   1534   1.1       jtc 
   1535   1.1       jtc /*
   1536  1.39  christos  - wgetnext - helper function for WGETNEXT() macro. Gets the next wide
   1537  1.39  christos  - character from the parse struct, signals a REG_ILLSEQ error if the
   1538  1.39  christos  - character can't be converted. Returns the number of bytes consumed.
   1539  1.39  christos  */
   1540  1.39  christos static wint_t
   1541  1.39  christos wgetnext(struct parse *p)
   1542  1.39  christos {
   1543  1.39  christos 	mbstate_t mbs;
   1544  1.39  christos 	wchar_t wc;
   1545  1.39  christos 	size_t n;
   1546  1.39  christos 
   1547  1.39  christos 	memset(&mbs, 0, sizeof(mbs));
   1548  1.40  christos 	n = mbrtowc(&wc, p->next, (size_t)(p->end - p->next), &mbs);
   1549  1.39  christos 	if (n == (size_t)-1 || n == (size_t)-2) {
   1550  1.39  christos 		SETERROR(REG_ILLSEQ);
   1551  1.39  christos 		return (0);
   1552  1.39  christos 	}
   1553  1.39  christos 	if (n == 0)
   1554  1.39  christos 		n = 1;
   1555  1.39  christos 	p->next += n;
   1556  1.39  christos 	return (wc);
   1557  1.39  christos }
   1558  1.39  christos 
   1559  1.39  christos /*
   1560   1.1       jtc  - seterr - set an error condition
   1561   1.9     perry  == static int seterr(struct parse *p, int e);
   1562   1.1       jtc  */
   1563   1.1       jtc static int			/* useless but makes type checking happy */
   1564  1.39  christos seterr(struct parse *p, int e)
   1565   1.1       jtc {
   1566  1.14     lukem 
   1567  1.14     lukem 	_DIAGASSERT(p != NULL);
   1568  1.14     lukem 
   1569   1.1       jtc 	if (p->error == 0)	/* keep earliest error condition */
   1570   1.1       jtc 		p->error = e;
   1571   1.1       jtc 	p->next = nuls;		/* try to bring things to a halt */
   1572   1.1       jtc 	p->end = nuls;
   1573   1.1       jtc 	return(0);		/* make the return value well-defined */
   1574   1.1       jtc }
   1575   1.1       jtc 
   1576   1.1       jtc /*
   1577   1.1       jtc  - allocset - allocate a set of characters for []
   1578   1.9     perry  == static cset *allocset(struct parse *p);
   1579   1.1       jtc  */
   1580   1.1       jtc static cset *
   1581  1.39  christos allocset(struct parse *p)
   1582   1.1       jtc {
   1583  1.39  christos 	cset *cs, *ncs;
   1584   1.1       jtc 
   1585  1.14     lukem 	_DIAGASSERT(p != NULL);
   1586  1.14     lukem 
   1587  1.39  christos 	ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs));
   1588  1.39  christos 	if (ncs == NULL) {
   1589  1.39  christos 		SETERROR(REG_ESPACE);
   1590  1.39  christos 		return (NULL);
   1591   1.1       jtc 	}
   1592  1.39  christos 	p->g->sets = ncs;
   1593  1.39  christos 	cs = &p->g->sets[p->g->ncsets++];
   1594  1.39  christos 	memset(cs, 0, sizeof(*cs));
   1595   1.1       jtc 
   1596   1.1       jtc 	return(cs);
   1597   1.1       jtc }
   1598   1.1       jtc 
   1599   1.1       jtc /*
   1600   1.1       jtc  - freeset - free a now-unused set
   1601   1.9     perry  == static void freeset(struct parse *p, cset *cs);
   1602   1.1       jtc  */
   1603   1.1       jtc static void
   1604  1.39  christos freeset(struct parse *p, cset *cs)
   1605   1.1       jtc {
   1606  1.14     lukem 	cset *top;
   1607  1.14     lukem 
   1608  1.14     lukem 	_DIAGASSERT(p != NULL);
   1609  1.14     lukem 	_DIAGASSERT(cs != NULL);
   1610  1.14     lukem 
   1611  1.14     lukem 	top = &p->g->sets[p->g->ncsets];
   1612   1.1       jtc 
   1613  1.39  christos 	free(cs->wides);
   1614  1.39  christos 	free(cs->ranges);
   1615  1.39  christos 	free(cs->types);
   1616  1.39  christos 	memset(cs, 0, sizeof(*cs));
   1617   1.1       jtc 	if (cs == top-1)	/* recover only the easy case */
   1618   1.1       jtc 		p->g->ncsets--;
   1619   1.1       jtc }
   1620   1.1       jtc 
   1621   1.1       jtc /*
   1622  1.39  christos  - singleton - Determine whether a set contains only one character,
   1623  1.39  christos  - returning it if so, otherwise returning OUT.
   1624   1.1       jtc  */
   1625  1.39  christos static wint_t
   1626  1.39  christos singleton(cset *cs)
   1627   1.1       jtc {
   1628  1.39  christos 	wint_t i, s, n;
   1629   1.1       jtc 
   1630  1.39  christos 	for (i = n = 0; i < NC; i++)
   1631  1.39  christos 		if (CHIN(cs, i)) {
   1632   1.1       jtc 			n++;
   1633  1.39  christos 			s = i;
   1634  1.39  christos 		}
   1635  1.39  christos 	if (n == 1)
   1636  1.39  christos 		return (s);
   1637  1.39  christos 	if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 &&
   1638  1.39  christos 	    cs->icase == 0)
   1639  1.39  christos 		return (cs->wides[0]);
   1640  1.39  christos 	/* Don't bother handling the other cases. */
   1641  1.39  christos 	return (OUT);
   1642   1.1       jtc }
   1643   1.1       jtc 
   1644   1.1       jtc /*
   1645  1.39  christos  - CHadd - add character to character set.
   1646   1.1       jtc  */
   1647   1.1       jtc static void
   1648  1.39  christos CHadd(struct parse *p, cset *cs, wint_t ch)
   1649   1.1       jtc {
   1650  1.39  christos 	wint_t nch, *newwides;
   1651  1.14     lukem 
   1652  1.14     lukem 	_DIAGASSERT(p != NULL);
   1653  1.14     lukem 	_DIAGASSERT(cs != NULL);
   1654  1.14     lukem 
   1655  1.39  christos 	assert(ch >= 0);
   1656  1.39  christos 	if (ch < NC)
   1657  1.40  christos 		cs->bmp[(unsigned)ch >> 3] |= 1 << (ch & 7);
   1658  1.39  christos 	else {
   1659  1.39  christos 		newwides = reallocarray(cs->wides, cs->nwides + 1,
   1660  1.39  christos 		    sizeof(*cs->wides));
   1661  1.39  christos 		if (newwides == NULL) {
   1662  1.39  christos 			SETERROR(REG_ESPACE);
   1663  1.39  christos 			return;
   1664  1.39  christos 		}
   1665  1.39  christos 		cs->wides = newwides;
   1666  1.39  christos 		cs->wides[cs->nwides++] = ch;
   1667  1.39  christos 	}
   1668  1.39  christos 	if (cs->icase) {
   1669  1.39  christos 		if ((nch = towlower(ch)) < NC)
   1670  1.40  christos 			cs->bmp[(unsigned)nch >> 3] |= 1 << (nch & 7);
   1671  1.39  christos 		if ((nch = towupper(ch)) < NC)
   1672  1.40  christos 			cs->bmp[(unsigned)nch >> 3] |= 1 << (nch & 7);
   1673   1.1       jtc 	}
   1674   1.1       jtc }
   1675   1.1       jtc 
   1676   1.1       jtc /*
   1677  1.39  christos  - CHaddrange - add all characters in the range [min,max] to a character set.
   1678   1.1       jtc  */
   1679   1.1       jtc static void
   1680  1.39  christos CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max)
   1681   1.1       jtc {
   1682  1.39  christos 	crange *newranges;
   1683  1.14     lukem 
   1684  1.39  christos 	_DIAGASSERT(p != NULL);
   1685  1.14     lukem 	_DIAGASSERT(cs != NULL);
   1686  1.14     lukem 
   1687  1.39  christos 	for (; min < NC && min <= max; min++)
   1688  1.39  christos 		CHadd(p, cs, min);
   1689  1.39  christos 	if (min >= max)
   1690  1.39  christos 		return;
   1691  1.39  christos 	newranges = reallocarray(cs->ranges, cs->nranges + 1,
   1692  1.39  christos 	    sizeof(*cs->ranges));
   1693  1.39  christos 	if (newranges == NULL) {
   1694  1.39  christos 		SETERROR(REG_ESPACE);
   1695   1.1       jtc 		return;
   1696   1.1       jtc 	}
   1697  1.39  christos 	cs->ranges = newranges;
   1698  1.39  christos 	cs->ranges[cs->nranges].min = min;
   1699  1.39  christos 	cs->ranges[cs->nranges].max = max;
   1700  1.39  christos 	cs->nranges++;
   1701   1.1       jtc }
   1702   1.1       jtc 
   1703   1.1       jtc /*
   1704  1.39  christos  - CHaddtype - add all characters of a certain type to a character set.
   1705   1.1       jtc  */
   1706   1.1       jtc static void
   1707  1.39  christos CHaddtype(struct parse *p, cset *cs, wctype_t wct)
   1708   1.1       jtc {
   1709  1.39  christos 	wint_t i;
   1710  1.39  christos 	wctype_t *newtypes;
   1711  1.14     lukem 
   1712  1.14     lukem 	_DIAGASSERT(p != NULL);
   1713  1.14     lukem 	_DIAGASSERT(cs != NULL);
   1714  1.14     lukem 
   1715  1.39  christos 	for (i = 0; i < NC; i++)
   1716  1.39  christos 		if (iswctype(i, wct))
   1717  1.39  christos 			CHadd(p, cs, i);
   1718  1.39  christos 	newtypes = reallocarray(cs->types, cs->ntypes + 1,
   1719  1.39  christos 	    sizeof(*cs->types));
   1720  1.39  christos 	if (newtypes == NULL) {
   1721  1.39  christos 		SETERROR(REG_ESPACE);
   1722   1.1       jtc 		return;
   1723  1.39  christos 	}
   1724  1.39  christos 	cs->types = newtypes;
   1725  1.39  christos 	cs->types[cs->ntypes++] = wct;
   1726   1.1       jtc }
   1727   1.1       jtc 
   1728   1.1       jtc /*
   1729   1.1       jtc  - dupl - emit a duplicate of a bunch of sops
   1730   1.9     perry  == static sopno dupl(struct parse *p, sopno start, sopno finish);
   1731   1.1       jtc  */
   1732   1.1       jtc static sopno			/* start of duplicate */
   1733  1.39  christos dupl(struct parse *p,
   1734  1.39  christos 	sopno start,		/* from here */
   1735  1.39  christos 	sopno finish)		/* to this less one */
   1736   1.1       jtc {
   1737  1.39  christos 	sopno ret = HERE();
   1738   1.9     perry 	sopno len = finish - start;
   1739   1.1       jtc 
   1740  1.14     lukem 	_DIAGASSERT(p != NULL);
   1741  1.14     lukem 
   1742   1.1       jtc 	assert(finish >= start);
   1743   1.1       jtc 	if (len == 0)
   1744   1.1       jtc 		return(ret);
   1745  1.39  christos 	if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */
   1746  1.39  christos 		return(ret);
   1747  1.40  christos 	(void) memcpy(p->strip + p->slen,
   1748  1.40  christos 	    p->strip + start, len * sizeof(*p->strip));
   1749   1.1       jtc 	p->slen += len;
   1750   1.1       jtc 	return(ret);
   1751   1.1       jtc }
   1752   1.1       jtc 
   1753   1.1       jtc /*
   1754   1.1       jtc  - doemit - emit a strip operator
   1755   1.9     perry  == static void doemit(struct parse *p, sop op, size_t opnd);
   1756   1.1       jtc  *
   1757   1.1       jtc  * It might seem better to implement this as a macro with a function as
   1758   1.1       jtc  * hard-case backup, but it's just too big and messy unless there are
   1759   1.1       jtc  * some changes to the data structures.  Maybe later.
   1760   1.1       jtc  */
   1761   1.1       jtc static void
   1762  1.39  christos doemit(struct parse *p, sop op, size_t opnd)
   1763   1.1       jtc {
   1764   1.1       jtc 	/* avoid making error situations worse */
   1765   1.1       jtc 	if (p->error != 0)
   1766   1.1       jtc 		return;
   1767   1.1       jtc 
   1768  1.39  christos 	_DIAGASSERT(p != NULL);
   1769  1.39  christos 
   1770   1.1       jtc 	/* deal with oversize operands ("can't happen", more or less) */
   1771   1.1       jtc 	assert(opnd < 1<<OPSHIFT);
   1772   1.1       jtc 
   1773   1.1       jtc 	/* deal with undersized strip */
   1774   1.1       jtc 	if (p->slen >= p->ssize)
   1775  1.30  christos 		if (!enlarge(p, (p->ssize+1) / 2 * 3))	/* +50% */
   1776  1.30  christos 			return;
   1777   1.1       jtc 
   1778   1.1       jtc 	/* finally, it's all reduced to the easy case */
   1779  1.40  christos 	p->strip[p->slen++] = (sopno)SOP(op, opnd);
   1780   1.1       jtc }
   1781   1.1       jtc 
   1782   1.1       jtc /*
   1783   1.1       jtc  - doinsert - insert a sop into the strip
   1784   1.9     perry  == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos);
   1785   1.1       jtc  */
   1786   1.1       jtc static void
   1787  1.39  christos doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
   1788   1.1       jtc {
   1789   1.9     perry 	sopno sn;
   1790   1.9     perry 	sop s;
   1791   1.9     perry 	int i;
   1792   1.1       jtc 
   1793  1.14     lukem 	_DIAGASSERT(p != NULL);
   1794  1.14     lukem 
   1795   1.1       jtc 	/* avoid making error situations worse */
   1796   1.1       jtc 	if (p->error != 0)
   1797   1.1       jtc 		return;
   1798   1.1       jtc 
   1799   1.1       jtc 	sn = HERE();
   1800   1.1       jtc 	EMIT(op, opnd);		/* do checks, ensure space */
   1801   1.1       jtc 	assert(HERE() == sn+1);
   1802   1.1       jtc 	s = p->strip[sn];
   1803   1.1       jtc 
   1804   1.1       jtc 	/* adjust paren pointers */
   1805   1.1       jtc 	assert(pos > 0);
   1806   1.1       jtc 	for (i = 1; i < NPAREN; i++) {
   1807   1.1       jtc 		if (p->pbegin[i] >= pos) {
   1808   1.1       jtc 			p->pbegin[i]++;
   1809   1.1       jtc 		}
   1810   1.1       jtc 		if (p->pend[i] >= pos) {
   1811   1.1       jtc 			p->pend[i]++;
   1812   1.1       jtc 		}
   1813   1.1       jtc 	}
   1814   1.1       jtc 
   1815  1.40  christos 	memmove(&p->strip[pos+1], &p->strip[pos],
   1816  1.40  christos 	    (HERE()-pos-1)*sizeof(*p->strip));
   1817   1.1       jtc 	p->strip[pos] = s;
   1818   1.1       jtc }
   1819   1.1       jtc 
   1820   1.1       jtc /*
   1821   1.1       jtc  - dofwd - complete a forward reference
   1822   1.9     perry  == static void dofwd(struct parse *p, sopno pos, sop value);
   1823   1.1       jtc  */
   1824   1.1       jtc static void
   1825  1.39  christos dofwd(struct parse *p, sopno pos, sop value)
   1826   1.1       jtc {
   1827  1.14     lukem 
   1828  1.14     lukem 	_DIAGASSERT(p != NULL);
   1829  1.14     lukem 
   1830   1.1       jtc 	/* avoid making error situations worse */
   1831   1.1       jtc 	if (p->error != 0)
   1832   1.1       jtc 		return;
   1833   1.1       jtc 
   1834   1.1       jtc 	assert(value < 1<<OPSHIFT);
   1835  1.39  christos 	p->strip[pos] = OP(p->strip[pos]) | value;
   1836   1.1       jtc }
   1837   1.1       jtc 
   1838   1.1       jtc /*
   1839   1.1       jtc  - enlarge - enlarge the strip
   1840  1.39  christos  == static int enlarge(struct parse *p, sopno size);
   1841   1.1       jtc  */
   1842  1.30  christos static int
   1843  1.35     joerg enlarge(struct parse *p, sopno size)
   1844   1.1       jtc {
   1845  1.39  christos 	sop *sp;
   1846  1.39  christos 
   1847  1.14     lukem 	_DIAGASSERT(p != NULL);
   1848  1.14     lukem 
   1849   1.1       jtc 	if (p->ssize >= size)
   1850  1.30  christos 		return 1;
   1851   1.1       jtc 
   1852  1.40  christos 	sp = reallocarray(p->strip, size, sizeof(*p->strip));
   1853  1.39  christos 	if (sp == NULL) {
   1854   1.1       jtc 		SETERROR(REG_ESPACE);
   1855  1.30  christos 		return 0;
   1856   1.1       jtc 	}
   1857  1.39  christos 	p->strip = sp;
   1858  1.35     joerg 	p->ssize = size;
   1859  1.30  christos 	return 1;
   1860   1.1       jtc }
   1861   1.1       jtc 
   1862   1.1       jtc /*
   1863   1.1       jtc  - stripsnug - compact the strip
   1864   1.9     perry  == static void stripsnug(struct parse *p, struct re_guts *g);
   1865   1.1       jtc  */
   1866   1.1       jtc static void
   1867  1.39  christos stripsnug(struct parse *p, struct re_guts *g)
   1868   1.1       jtc {
   1869  1.14     lukem 
   1870  1.14     lukem 	_DIAGASSERT(p != NULL);
   1871  1.14     lukem 	_DIAGASSERT(g != NULL);
   1872  1.14     lukem 
   1873   1.1       jtc 	g->nstates = p->slen;
   1874  1.40  christos 	g->strip = reallocarray(p->strip, p->slen, sizeof(*p->strip));
   1875  1.39  christos 	if (g->strip == NULL) {
   1876  1.39  christos 		SETERROR(REG_ESPACE);
   1877  1.39  christos 		g->strip = p->strip;
   1878  1.39  christos 	}
   1879   1.1       jtc }
   1880   1.1       jtc 
   1881   1.1       jtc /*
   1882   1.1       jtc  - findmust - fill in must and mlen with longest mandatory literal string
   1883   1.9     perry  == static void findmust(struct parse *p, struct re_guts *g);
   1884   1.1       jtc  *
   1885   1.1       jtc  * This algorithm could do fancy things like analyzing the operands of |
   1886   1.1       jtc  * for common subsequences.  Someday.  This code is simple and finds most
   1887   1.1       jtc  * of the interesting cases.
   1888   1.1       jtc  *
   1889   1.1       jtc  * Note that must and mlen got initialized during setup.
   1890   1.1       jtc  */
   1891   1.1       jtc static void
   1892  1.39  christos findmust(struct parse *p, struct re_guts *g)
   1893   1.1       jtc {
   1894   1.9     perry 	sop *scan;
   1895   1.7  christos 	sop *start = NULL;
   1896   1.9     perry 	sop *newstart = NULL;
   1897   1.9     perry 	sopno newlen;
   1898   1.9     perry 	sop s;
   1899   1.9     perry 	char *cp;
   1900  1.39  christos 	int offset;
   1901  1.39  christos 	char buf[MB_LEN_MAX];
   1902  1.39  christos 	size_t clen;
   1903  1.39  christos 	mbstate_t mbs;
   1904   1.1       jtc 
   1905  1.14     lukem 	_DIAGASSERT(p != NULL);
   1906  1.14     lukem 	_DIAGASSERT(g != NULL);
   1907  1.14     lukem 
   1908   1.1       jtc 	/* avoid making error situations worse */
   1909   1.1       jtc 	if (p->error != 0)
   1910   1.1       jtc 		return;
   1911   1.1       jtc 
   1912  1.39  christos #ifdef notyet
   1913  1.39  christos 	/*
   1914  1.39  christos 	 * It's not generally safe to do a ``char'' substring search on
   1915  1.39  christos 	 * multibyte character strings, but it's safe for at least
   1916  1.39  christos 	 * UTF-8 (see RFC 3629).
   1917  1.39  christos 	 */
   1918  1.39  christos 	if (MB_CUR_MAX > 1 &&
   1919  1.39  christos 	    strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0)
   1920  1.39  christos 		return;
   1921  1.39  christos #endif
   1922  1.39  christos 
   1923   1.1       jtc 	/* find the longest OCHAR sequence in strip */
   1924   1.1       jtc 	newlen = 0;
   1925  1.39  christos 	offset = 0;
   1926  1.39  christos 	g->moffset = 0;
   1927   1.1       jtc 	scan = g->strip + 1;
   1928   1.1       jtc 	do {
   1929   1.1       jtc 		s = *scan++;
   1930   1.1       jtc 		switch (OP(s)) {
   1931   1.1       jtc 		case OCHAR:		/* sequence member */
   1932  1.39  christos 			if (newlen == 0) {		/* new sequence */
   1933  1.39  christos 				memset(&mbs, 0, sizeof(mbs));
   1934   1.1       jtc 				newstart = scan - 1;
   1935  1.39  christos 			}
   1936  1.40  christos 			clen = wcrtomb(buf, (int)OPND(s), &mbs);
   1937  1.39  christos 			if (clen == (size_t)-1)
   1938  1.39  christos 				goto toohard;
   1939  1.40  christos 			newlen += (sopno)clen;
   1940   1.1       jtc 			break;
   1941   1.1       jtc 		case OPLUS_:		/* things that don't break one */
   1942   1.1       jtc 		case OLPAREN:
   1943   1.1       jtc 		case ORPAREN:
   1944   1.1       jtc 			break;
   1945   1.1       jtc 		case OQUEST_:		/* things that must be skipped */
   1946   1.1       jtc 		case OCH_:
   1947  1.39  christos 			offset = altoffset(scan, offset);
   1948   1.1       jtc 			scan--;
   1949   1.1       jtc 			do {
   1950   1.1       jtc 				scan += OPND(s);
   1951   1.1       jtc 				s = *scan;
   1952   1.1       jtc 				/* assert() interferes w debug printouts */
   1953  1.40  christos 				if (OP(s) != O_QUEST &&
   1954  1.40  christos 				    OP(s) != O_CH && OP(s) != OOR2) {
   1955   1.1       jtc 					g->iflags |= BAD;
   1956   1.1       jtc 					return;
   1957   1.1       jtc 				}
   1958  1.40  christos 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
   1959  1.11  christos 			/* FALLTHROUGH */
   1960  1.39  christos 		case OBOW:		/* things that break a sequence */
   1961  1.39  christos 		case OEOW:
   1962  1.39  christos 		case OBOL:
   1963  1.39  christos 		case OEOL:
   1964  1.39  christos 		case OBOS:
   1965  1.39  christos 		case OEOS:
   1966  1.39  christos 		case OWBND:
   1967  1.39  christos 		case ONWBND:
   1968  1.39  christos 		case O_QUEST:
   1969  1.39  christos 		case O_CH:
   1970  1.39  christos 		case OEND:
   1971  1.39  christos 			if (newlen > (sopno)g->mlen) {		/* ends one */
   1972   1.1       jtc 				start = newstart;
   1973   1.1       jtc 				g->mlen = newlen;
   1974  1.39  christos 				if (offset > -1) {
   1975  1.39  christos 					g->moffset += offset;
   1976  1.39  christos 					offset = newlen;
   1977  1.39  christos 				} else
   1978  1.39  christos 					g->moffset = offset;
   1979  1.39  christos 			} else {
   1980  1.39  christos 				if (offset > -1)
   1981  1.39  christos 					offset += newlen;
   1982   1.1       jtc 			}
   1983   1.1       jtc 			newlen = 0;
   1984   1.1       jtc 			break;
   1985  1.39  christos 		case OANY:
   1986  1.39  christos 			if (newlen > (sopno)g->mlen) {		/* ends one */
   1987  1.39  christos 				start = newstart;
   1988  1.39  christos 				g->mlen = newlen;
   1989  1.39  christos 				if (offset > -1) {
   1990  1.39  christos 					g->moffset += offset;
   1991  1.39  christos 					offset = newlen;
   1992  1.39  christos 				} else
   1993  1.39  christos 					g->moffset = offset;
   1994  1.39  christos 			} else {
   1995  1.39  christos 				if (offset > -1)
   1996  1.39  christos 					offset += newlen;
   1997  1.39  christos 			}
   1998  1.39  christos 			if (offset > -1)
   1999  1.39  christos 				offset++;
   2000  1.39  christos 			newlen = 0;
   2001  1.39  christos 			break;
   2002  1.39  christos 		case OANYOF:		/* may or may not invalidate offset */
   2003  1.39  christos 			/* First, everything as OANY */
   2004  1.39  christos 			if (newlen > (sopno)g->mlen) {		/* ends one */
   2005  1.39  christos 				start = newstart;
   2006  1.39  christos 				g->mlen = newlen;
   2007  1.39  christos 				if (offset > -1) {
   2008  1.39  christos 					g->moffset += offset;
   2009  1.39  christos 					offset = newlen;
   2010  1.39  christos 				} else
   2011  1.39  christos 					g->moffset = offset;
   2012  1.39  christos 			} else {
   2013  1.39  christos 				if (offset > -1)
   2014  1.39  christos 					offset += newlen;
   2015  1.39  christos 			}
   2016  1.39  christos 			if (offset > -1)
   2017  1.39  christos 				offset++;
   2018  1.39  christos 			newlen = 0;
   2019  1.39  christos 			break;
   2020  1.40  christos 		toohard:/*FALLTHROUGH*/
   2021  1.39  christos 		default:
   2022  1.39  christos 			/* Anything here makes it impossible or too hard
   2023  1.39  christos 			 * to calculate the offset -- so we give up;
   2024  1.39  christos 			 * save the last known good offset, in case the
   2025  1.39  christos 			 * must sequence doesn't occur later.
   2026  1.39  christos 			 */
   2027  1.39  christos 			if (newlen > (sopno)g->mlen) {		/* ends one */
   2028  1.39  christos 				start = newstart;
   2029  1.39  christos 				g->mlen = newlen;
   2030  1.39  christos 				if (offset > -1)
   2031  1.39  christos 					g->moffset += offset;
   2032  1.39  christos 				else
   2033  1.39  christos 					g->moffset = offset;
   2034  1.39  christos 			}
   2035  1.39  christos 			offset = -1;
   2036  1.39  christos 			newlen = 0;
   2037  1.39  christos 			break;
   2038   1.1       jtc 		}
   2039   1.1       jtc 	} while (OP(s) != OEND);
   2040   1.1       jtc 
   2041  1.39  christos 	if (g->mlen == 0) {		/* there isn't one */
   2042  1.39  christos 		g->moffset = -1;
   2043   1.1       jtc 		return;
   2044  1.39  christos 	}
   2045   1.1       jtc 
   2046   1.1       jtc 	/* turn it into a character string */
   2047   1.1       jtc 	g->must = malloc((size_t)g->mlen + 1);
   2048   1.1       jtc 	if (g->must == NULL) {		/* argh; just forget it */
   2049   1.1       jtc 		g->mlen = 0;
   2050  1.39  christos 		g->moffset = -1;
   2051   1.1       jtc 		return;
   2052   1.1       jtc 	}
   2053   1.1       jtc 	cp = g->must;
   2054   1.1       jtc 	scan = start;
   2055  1.39  christos 	memset(&mbs, 0, sizeof(mbs));
   2056  1.39  christos 	while (cp < g->must + g->mlen) {
   2057   1.1       jtc 		while (OP(s = *scan++) != OCHAR)
   2058   1.1       jtc 			continue;
   2059  1.40  christos 		clen = wcrtomb(cp, (int)OPND(s), &mbs);
   2060  1.39  christos 		assert(clen != (size_t)-1);
   2061  1.39  christos 		cp += clen;
   2062   1.1       jtc 	}
   2063   1.2       jtc 	assert(cp == g->must + g->mlen);
   2064   1.1       jtc 	*cp++ = '\0';		/* just on general principles */
   2065   1.1       jtc }
   2066   1.1       jtc 
   2067   1.1       jtc /*
   2068  1.39  christos  - altoffset - choose biggest offset among multiple choices
   2069  1.39  christos  == static int altoffset(sop *scan, int offset);
   2070  1.39  christos  *
   2071  1.39  christos  * Compute, recursively if necessary, the largest offset among multiple
   2072  1.39  christos  * re paths.
   2073  1.39  christos  */
   2074  1.39  christos static int
   2075  1.39  christos altoffset(sop *scan, int offset)
   2076  1.39  christos {
   2077  1.39  christos 	int largest;
   2078  1.39  christos 	int try;
   2079  1.39  christos 	sop s;
   2080  1.39  christos 
   2081  1.39  christos 	_DIAGASSERT(scan != NULL);
   2082  1.39  christos 
   2083  1.39  christos 	/* If we gave up already on offsets, return */
   2084  1.39  christos 	if (offset == -1)
   2085  1.39  christos 		return -1;
   2086  1.39  christos 
   2087  1.39  christos 	largest = 0;
   2088  1.39  christos 	try = 0;
   2089  1.39  christos 	s = *scan++;
   2090  1.40  christos 	while (OP(s) != O_QUEST && OP(s) != O_CH) {
   2091  1.39  christos 		switch (OP(s)) {
   2092  1.39  christos 		case OOR1:
   2093  1.39  christos 			if (try > largest)
   2094  1.39  christos 				largest = try;
   2095  1.39  christos 			try = 0;
   2096  1.39  christos 			break;
   2097  1.39  christos 		case OQUEST_:
   2098  1.39  christos 		case OCH_:
   2099  1.39  christos 			try = altoffset(scan, try);
   2100  1.39  christos 			if (try == -1)
   2101  1.39  christos 				return -1;
   2102  1.39  christos 			scan--;
   2103  1.39  christos 			do {
   2104  1.39  christos 				scan += OPND(s);
   2105  1.39  christos 				s = *scan;
   2106  1.40  christos 				if (OP(s) != O_QUEST &&
   2107  1.40  christos 				    OP(s) != O_CH && OP(s) != OOR2)
   2108  1.39  christos 					return -1;
   2109  1.40  christos 			} while (OP(s) != O_QUEST && OP(s) != O_CH);
   2110  1.39  christos 			/* We must skip to the next position, or we'll
   2111  1.39  christos 			 * leave altoffset() too early.
   2112  1.39  christos 			 */
   2113  1.39  christos 			scan++;
   2114  1.39  christos 			break;
   2115  1.39  christos 		case OANYOF:
   2116  1.39  christos 		case OCHAR:
   2117  1.39  christos 		case OANY:
   2118  1.39  christos 			try++;
   2119  1.40  christos 			/*FALLTHROUGH*/
   2120  1.39  christos 		case OBOW:
   2121  1.39  christos 		case OEOW:
   2122  1.39  christos 		case OWBND:
   2123  1.39  christos 		case ONWBND:
   2124  1.39  christos 		case OLPAREN:
   2125  1.39  christos 		case ORPAREN:
   2126  1.39  christos 		case OOR2:
   2127  1.39  christos 			break;
   2128  1.39  christos 		default:
   2129  1.39  christos 			try = -1;
   2130  1.39  christos 			break;
   2131  1.39  christos 		}
   2132  1.39  christos 		if (try == -1)
   2133  1.39  christos 			return -1;
   2134  1.39  christos 		s = *scan++;
   2135  1.39  christos 	}
   2136  1.39  christos 
   2137  1.39  christos 	if (try > largest)
   2138  1.39  christos 		largest = try;
   2139  1.39  christos 
   2140  1.39  christos 	return largest+offset;
   2141  1.39  christos }
   2142  1.39  christos 
   2143  1.39  christos /*
   2144  1.39  christos  - computejumps - compute char jumps for BM scan
   2145  1.39  christos  == static void computejumps(struct parse *p, struct re_guts *g);
   2146  1.39  christos  *
   2147  1.39  christos  * This algorithm assumes g->must exists and is has size greater than
   2148  1.39  christos  * zero. It's based on the algorithm found on Computer Algorithms by
   2149  1.39  christos  * Sara Baase.
   2150  1.39  christos  *
   2151  1.39  christos  * A char jump is the number of characters one needs to jump based on
   2152  1.39  christos  * the value of the character from the text that was mismatched.
   2153  1.39  christos  */
   2154  1.39  christos static void
   2155  1.39  christos computejumps(struct parse *p, struct re_guts *g)
   2156  1.39  christos {
   2157  1.39  christos 	int ch;
   2158  1.39  christos 	size_t mindex;
   2159  1.39  christos 
   2160  1.39  christos 	_DIAGASSERT(p != NULL);
   2161  1.39  christos 	_DIAGASSERT(g != NULL);
   2162  1.39  christos 
   2163  1.39  christos 	/* Avoid making errors worse */
   2164  1.39  christos 	if (p->error != 0)
   2165  1.39  christos 		return;
   2166  1.39  christos 
   2167  1.39  christos 	g->charjump = calloc((NC_MAX + 1), sizeof(*g->charjump));
   2168  1.39  christos 	if (g->charjump == NULL)	/* Not a fatal error */
   2169  1.39  christos 		return;
   2170  1.39  christos 	/* Adjust for signed chars, if necessary */
   2171  1.39  christos 	g->charjump = &g->charjump[-(CHAR_MIN)];
   2172  1.39  christos 
   2173  1.39  christos 	/* If the character does not exist in the pattern, the jump
   2174  1.39  christos 	 * is equal to the number of characters in the pattern.
   2175  1.39  christos 	 */
   2176  1.39  christos 	for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++)
   2177  1.39  christos 		g->charjump[ch] = g->mlen;
   2178  1.39  christos 
   2179  1.39  christos 	/* If the character does exist, compute the jump that would
   2180  1.39  christos 	 * take us to the last character in the pattern equal to it
   2181  1.39  christos 	 * (notice that we match right to left, so that last character
   2182  1.39  christos 	 * is the first one that would be matched).
   2183  1.39  christos 	 */
   2184  1.39  christos 	for (mindex = 0; mindex < g->mlen; mindex++)
   2185  1.39  christos 		g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1;
   2186  1.39  christos }
   2187  1.39  christos 
   2188  1.39  christos /*
   2189  1.39  christos  - computematchjumps - compute match jumps for BM scan
   2190  1.39  christos  == static void computematchjumps(struct parse *p, struct re_guts *g);
   2191  1.39  christos  *
   2192  1.39  christos  * This algorithm assumes g->must exists and is has size greater than
   2193  1.39  christos  * zero. It's based on the algorithm found on Computer Algorithms by
   2194  1.39  christos  * Sara Baase.
   2195  1.39  christos  *
   2196  1.39  christos  * A match jump is the number of characters one needs to advance based
   2197  1.39  christos  * on the already-matched suffix.
   2198  1.39  christos  * Notice that all values here are minus (g->mlen-1), because of the way
   2199  1.39  christos  * the search algorithm works.
   2200  1.39  christos  */
   2201  1.39  christos static void
   2202  1.39  christos computematchjumps(struct parse *p, struct re_guts *g)
   2203  1.39  christos {
   2204  1.39  christos 	size_t mindex;		/* General "must" iterator */
   2205  1.39  christos 	size_t suffix;		/* Keeps track of matching suffix */
   2206  1.39  christos 	size_t ssuffix;		/* Keeps track of suffixes' suffix */
   2207  1.39  christos 	size_t* pmatches;	/* pmatches[k] points to the next i
   2208  1.39  christos 				 * such that i+1...mlen is a substring
   2209  1.39  christos 				 * of k+1...k+mlen-i-1
   2210  1.39  christos 				 */
   2211  1.39  christos 
   2212  1.39  christos 	_DIAGASSERT(p != NULL);
   2213  1.39  christos 	_DIAGASSERT(g != NULL);
   2214  1.39  christos 
   2215  1.39  christos 	/* Avoid making errors worse */
   2216  1.39  christos 	if (p->error != 0)
   2217  1.39  christos 		return;
   2218  1.39  christos 
   2219  1.39  christos 	pmatches = calloc(g->mlen, sizeof(*pmatches));
   2220  1.39  christos 	if (pmatches == NULL) {
   2221  1.39  christos 		g->matchjump = NULL;
   2222  1.39  christos 		return;
   2223  1.39  christos 	}
   2224  1.39  christos 
   2225  1.39  christos 	g->matchjump = calloc(g->mlen, sizeof(*g->matchjump));
   2226  1.39  christos 	if (g->matchjump == NULL) {	/* Not a fatal error */
   2227  1.39  christos 		free(pmatches);
   2228  1.39  christos 		return;
   2229  1.39  christos 	}
   2230  1.39  christos 
   2231  1.39  christos 	/* Set maximum possible jump for each character in the pattern */
   2232  1.39  christos 	for (mindex = 0; mindex < g->mlen; mindex++)
   2233  1.39  christos 		g->matchjump[mindex] = 2 * g->mlen - mindex - 1;
   2234  1.39  christos 
   2235  1.39  christos 	/* Compute pmatches[] */
   2236  1.39  christos 	for (suffix = mindex = g->mlen; mindex-- > 0; suffix--) {
   2237  1.39  christos 		pmatches[mindex] = suffix;
   2238  1.39  christos 
   2239  1.39  christos 		/* If a mismatch is found, interrupting the substring,
   2240  1.39  christos 		 * compute the matchjump for that position. If no
   2241  1.39  christos 		 * mismatch is found, then a text substring mismatched
   2242  1.39  christos 		 * against the suffix will also mismatch against the
   2243  1.39  christos 		 * substring.
   2244  1.39  christos 		 */
   2245  1.39  christos 		while (suffix < g->mlen
   2246  1.39  christos 		    && g->must[mindex] != g->must[suffix]) {
   2247  1.39  christos 			g->matchjump[suffix] = MIN(g->matchjump[suffix],
   2248  1.39  christos 			    g->mlen - mindex - 1);
   2249  1.39  christos 			suffix = pmatches[suffix];
   2250  1.39  christos 		}
   2251  1.39  christos 	}
   2252  1.39  christos 
   2253  1.39  christos 	/* Compute the matchjump up to the last substring found to jump
   2254  1.39  christos 	 * to the beginning of the largest must pattern prefix matching
   2255  1.39  christos 	 * it's own suffix.
   2256  1.39  christos 	 */
   2257  1.39  christos 	for (mindex = 0; mindex <= suffix; mindex++)
   2258  1.39  christos 		g->matchjump[mindex] = MIN(g->matchjump[mindex],
   2259  1.39  christos 		    g->mlen + suffix - mindex);
   2260  1.39  christos 
   2261  1.39  christos         ssuffix = pmatches[suffix];
   2262  1.39  christos         while (suffix < g->mlen) {
   2263  1.39  christos                 while (suffix <= ssuffix && suffix < g->mlen) {
   2264  1.39  christos                         g->matchjump[suffix] = MIN(g->matchjump[suffix],
   2265  1.39  christos 			    g->mlen + ssuffix - suffix);
   2266  1.39  christos                         suffix++;
   2267  1.39  christos                 }
   2268  1.39  christos 		if (suffix < g->mlen)
   2269  1.39  christos                 	ssuffix = pmatches[ssuffix];
   2270  1.39  christos         }
   2271  1.39  christos 
   2272  1.39  christos 	free(pmatches);
   2273  1.39  christos }
   2274  1.39  christos 
   2275  1.39  christos /*
   2276   1.1       jtc  - pluscount - count + nesting
   2277   1.9     perry  == static sopno pluscount(struct parse *p, struct re_guts *g);
   2278   1.1       jtc  */
   2279   1.1       jtc static sopno			/* nesting depth */
   2280  1.39  christos pluscount(struct parse *p, struct re_guts *g)
   2281   1.1       jtc {
   2282   1.9     perry 	sop *scan;
   2283   1.9     perry 	sop s;
   2284   1.9     perry 	sopno plusnest = 0;
   2285   1.9     perry 	sopno maxnest = 0;
   2286  1.14     lukem 
   2287  1.14     lukem 	_DIAGASSERT(p != NULL);
   2288  1.14     lukem 	_DIAGASSERT(g != NULL);
   2289   1.1       jtc 
   2290   1.1       jtc 	if (p->error != 0)
   2291   1.1       jtc 		return(0);	/* there may not be an OEND */
   2292   1.1       jtc 
   2293   1.1       jtc 	scan = g->strip + 1;
   2294   1.1       jtc 	do {
   2295   1.1       jtc 		s = *scan++;
   2296   1.1       jtc 		switch (OP(s)) {
   2297   1.1       jtc 		case OPLUS_:
   2298   1.1       jtc 			plusnest++;
   2299   1.1       jtc 			break;
   2300   1.1       jtc 		case O_PLUS:
   2301   1.1       jtc 			if (plusnest > maxnest)
   2302   1.1       jtc 				maxnest = plusnest;
   2303   1.1       jtc 			plusnest--;
   2304   1.1       jtc 			break;
   2305   1.1       jtc 		}
   2306   1.1       jtc 	} while (OP(s) != OEND);
   2307   1.1       jtc 	if (plusnest != 0)
   2308   1.1       jtc 		g->iflags |= BAD;
   2309   1.1       jtc 	return(maxnest);
   2310   1.1       jtc }
   2311