158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 Henry Spencer. 358f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 458f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 558f0484fSRodney W. Grimes * 63c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 73c87aa1dSDavid Chisnall * All rights reserved. 83c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 93c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 103c87aa1dSDavid Chisnall * 1158f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 1258f0484fSRodney W. Grimes * Henry Spencer. 1358f0484fSRodney W. Grimes * 1458f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1558f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1658f0484fSRodney W. Grimes * are met: 1758f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1858f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1958f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 2058f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 2158f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 22fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 2358f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2458f0484fSRodney W. Grimes * without specific prior written permission. 2558f0484fSRodney W. Grimes * 2658f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2758f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2858f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2958f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3058f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3158f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3258f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3358f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3458f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3558f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3658f0484fSRodney W. Grimes * SUCH DAMAGE. 3758f0484fSRodney W. Grimes * 3858f0484fSRodney W. Grimes * @(#)regcomp.c 8.5 (Berkeley) 3/20/94 3958f0484fSRodney W. Grimes */ 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 4258f0484fSRodney W. Grimes static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94"; 4358f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 44333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 45333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 4658f0484fSRodney W. Grimes 4758f0484fSRodney W. Grimes #include <sys/types.h> 4858f0484fSRodney W. Grimes #include <stdio.h> 4958f0484fSRodney W. Grimes #include <string.h> 5058f0484fSRodney W. Grimes #include <ctype.h> 5158f0484fSRodney W. Grimes #include <limits.h> 5258f0484fSRodney W. Grimes #include <stdlib.h> 5358f0484fSRodney W. Grimes #include <regex.h> 5415ae9efaSKyle Evans #include <stdbool.h> 55e5996857STim J. Robbins #include <wchar.h> 56e5996857STim J. Robbins #include <wctype.h> 5758f0484fSRodney W. Grimes 581daad8f5SAndrey A. Chernov #include "collate.h" 591daad8f5SAndrey A. Chernov 6058f0484fSRodney W. Grimes #include "utils.h" 6158f0484fSRodney W. Grimes #include "regex2.h" 6258f0484fSRodney W. Grimes 6358f0484fSRodney W. Grimes #include "cname.h" 6458f0484fSRodney W. Grimes 6558f0484fSRodney W. Grimes /* 6615ae9efaSKyle Evans * Branching context, used to keep track of branch state for all of the branch- 6715ae9efaSKyle Evans * aware functions. In addition to keeping track of branch positions for the 6815ae9efaSKyle Evans * p_branch_* functions, we use this to simplify some clumsiness in BREs for 6915ae9efaSKyle Evans * detection of whether ^ is acting as an anchor or being used erroneously and 7015ae9efaSKyle Evans * also for whether we're in a sub-expression or not. 7115ae9efaSKyle Evans */ 7215ae9efaSKyle Evans struct branchc { 7315ae9efaSKyle Evans sopno start; 7415ae9efaSKyle Evans sopno back; 7515ae9efaSKyle Evans sopno fwd; 7615ae9efaSKyle Evans 7715ae9efaSKyle Evans int nbranch; 7815ae9efaSKyle Evans int nchain; 7915ae9efaSKyle Evans bool outer; 8015ae9efaSKyle Evans bool terminate; 8115ae9efaSKyle Evans }; 8215ae9efaSKyle Evans 8315ae9efaSKyle Evans /* 8458f0484fSRodney W. Grimes * parse structure, passed up and down to avoid global variables and 8558f0484fSRodney W. Grimes * other clumsinesses 8658f0484fSRodney W. Grimes */ 8758f0484fSRodney W. Grimes struct parse { 888d0f9a93SPedro F. Giffuni const char *next; /* next character in RE */ 898d0f9a93SPedro F. Giffuni const char *end; /* end of string (-> NUL normally) */ 9058f0484fSRodney W. Grimes int error; /* has an error been seen? */ 9158f0484fSRodney W. Grimes sop *strip; /* malloced strip */ 9258f0484fSRodney W. Grimes sopno ssize; /* malloced strip size (allocated) */ 9358f0484fSRodney W. Grimes sopno slen; /* malloced strip length (used) */ 9458f0484fSRodney W. Grimes int ncsalloc; /* number of csets allocated */ 9558f0484fSRodney W. Grimes struct re_guts *g; 9658f0484fSRodney W. Grimes # define NPAREN 10 /* we need to remember () 1-9 for back refs */ 9758f0484fSRodney W. Grimes sopno pbegin[NPAREN]; /* -> ( ([0] unused) */ 9858f0484fSRodney W. Grimes sopno pend[NPAREN]; /* -> ) ([0] unused) */ 9915ae9efaSKyle Evans bool allowbranch; /* can this expression branch? */ 10015ae9efaSKyle Evans bool bre; /* convenience; is this a BRE? */ 10115ae9efaSKyle Evans bool (*parse_expr)(struct parse *, struct branchc *); 10215ae9efaSKyle Evans void (*pre_parse)(struct parse *, struct branchc *); 10315ae9efaSKyle Evans void (*post_parse)(struct parse *, struct branchc *); 10458f0484fSRodney W. Grimes }; 10558f0484fSRodney W. Grimes 10658f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */ 10758f0484fSRodney W. Grimes #ifdef __cplusplus 10858f0484fSRodney W. Grimes extern "C" { 10958f0484fSRodney W. Grimes #endif 11058f0484fSRodney W. Grimes 11158f0484fSRodney W. Grimes /* === regcomp.c === */ 11215ae9efaSKyle Evans static bool p_ere_exp(struct parse *p, struct branchc *bc); 113c05ac53bSDavid E. O'Brien static void p_str(struct parse *p); 11415ae9efaSKyle Evans static int p_branch_eat_delim(struct parse *p, struct branchc *bc); 11515ae9efaSKyle Evans static void p_branch_ins_offset(struct parse *p, struct branchc *bc); 11615ae9efaSKyle Evans static void p_branch_fix_tail(struct parse *p, struct branchc *bc); 1173ea376f6SKyle Evans static bool p_branch_empty(struct parse *p, struct branchc *bc); 11815ae9efaSKyle Evans static bool p_branch_do(struct parse *p, struct branchc *bc); 11915ae9efaSKyle Evans static void p_bre_pre_parse(struct parse *p, struct branchc *bc); 12015ae9efaSKyle Evans static void p_bre_post_parse(struct parse *p, struct branchc *bc); 12115ae9efaSKyle Evans static void p_re(struct parse *p, int end1, int end2); 12215ae9efaSKyle Evans static bool p_simp_re(struct parse *p, struct branchc *bc); 123c05ac53bSDavid E. O'Brien static int p_count(struct parse *p); 124c05ac53bSDavid E. O'Brien static void p_bracket(struct parse *p); 125c05ac53bSDavid E. O'Brien static void p_b_term(struct parse *p, cset *cs); 126c05ac53bSDavid E. O'Brien static void p_b_cclass(struct parse *p, cset *cs); 127c05ac53bSDavid E. O'Brien static void p_b_eclass(struct parse *p, cset *cs); 128e5996857STim J. Robbins static wint_t p_b_symbol(struct parse *p); 129e5996857STim J. Robbins static wint_t p_b_coll_elem(struct parse *p, wint_t endc); 130e5996857STim J. Robbins static wint_t othercase(wint_t ch); 131e5996857STim J. Robbins static void bothcases(struct parse *p, wint_t ch); 132e5996857STim J. Robbins static void ordinary(struct parse *p, wint_t ch); 133c05ac53bSDavid E. O'Brien static void nonnewline(struct parse *p); 134c05ac53bSDavid E. O'Brien static void repeat(struct parse *p, sopno start, int from, int to); 135c05ac53bSDavid E. O'Brien static int seterr(struct parse *p, int e); 136c05ac53bSDavid E. O'Brien static cset *allocset(struct parse *p); 137c05ac53bSDavid E. O'Brien static void freeset(struct parse *p, cset *cs); 138e5996857STim J. Robbins static void CHadd(struct parse *p, cset *cs, wint_t ch); 139e5996857STim J. Robbins static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max); 140e5996857STim J. Robbins static void CHaddtype(struct parse *p, cset *cs, wctype_t wct); 141e5996857STim J. Robbins static wint_t singleton(cset *cs); 142c05ac53bSDavid E. O'Brien static sopno dupl(struct parse *p, sopno start, sopno finish); 143c05ac53bSDavid E. O'Brien static void doemit(struct parse *p, sop op, size_t opnd); 144c05ac53bSDavid E. O'Brien static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 145c05ac53bSDavid E. O'Brien static void dofwd(struct parse *p, sopno pos, sop value); 1462bf213ebSKevin Lo static int enlarge(struct parse *p, sopno size); 147c05ac53bSDavid E. O'Brien static void stripsnug(struct parse *p, struct re_guts *g); 148c05ac53bSDavid E. O'Brien static void findmust(struct parse *p, struct re_guts *g); 14980f36366STim J. Robbins static int altoffset(sop *scan, int offset); 150c05ac53bSDavid E. O'Brien static void computejumps(struct parse *p, struct re_guts *g); 151c05ac53bSDavid E. O'Brien static void computematchjumps(struct parse *p, struct re_guts *g); 152c05ac53bSDavid E. O'Brien static sopno pluscount(struct parse *p, struct re_guts *g); 153e5996857STim J. Robbins static wint_t wgetnext(struct parse *p); 15458f0484fSRodney W. Grimes 15558f0484fSRodney W. Grimes #ifdef __cplusplus 15658f0484fSRodney W. Grimes } 15758f0484fSRodney W. Grimes #endif 15858f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */ 15958f0484fSRodney W. Grimes 16058f0484fSRodney W. Grimes static char nuls[10]; /* place to point scanner in event of error */ 16158f0484fSRodney W. Grimes 16258f0484fSRodney W. Grimes /* 16358f0484fSRodney W. Grimes * macros for use with parse structure 16458f0484fSRodney W. Grimes * BEWARE: these know that the parse structure is named `p' !!! 16558f0484fSRodney W. Grimes */ 16658f0484fSRodney W. Grimes #define PEEK() (*p->next) 16758f0484fSRodney W. Grimes #define PEEK2() (*(p->next+1)) 16858f0484fSRodney W. Grimes #define MORE() (p->next < p->end) 16958f0484fSRodney W. Grimes #define MORE2() (p->next+1 < p->end) 17058f0484fSRodney W. Grimes #define SEE(c) (MORE() && PEEK() == (c)) 17158f0484fSRodney W. Grimes #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b)) 17215ae9efaSKyle Evans #define SEESPEC(a) (p->bre ? SEETWO('\\', a) : SEE(a)) 17358f0484fSRodney W. Grimes #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0) 17458f0484fSRodney W. Grimes #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0) 17558f0484fSRodney W. Grimes #define NEXT() (p->next++) 17658f0484fSRodney W. Grimes #define NEXT2() (p->next += 2) 17758f0484fSRodney W. Grimes #define NEXTn(n) (p->next += (n)) 17858f0484fSRodney W. Grimes #define GETNEXT() (*p->next++) 179e5996857STim J. Robbins #define WGETNEXT() wgetnext(p) 18058f0484fSRodney W. Grimes #define SETERROR(e) seterr(p, (e)) 18158f0484fSRodney W. Grimes #define REQUIRE(co, e) ((co) || SETERROR(e)) 18258f0484fSRodney W. Grimes #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e)) 18358f0484fSRodney W. Grimes #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e)) 18458f0484fSRodney W. Grimes #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e)) 18558f0484fSRodney W. Grimes #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd)) 18658f0484fSRodney W. Grimes #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos) 18758f0484fSRodney W. Grimes #define AHEAD(pos) dofwd(p, pos, HERE()-(pos)) 18858f0484fSRodney W. Grimes #define ASTERN(sop, pos) EMIT(sop, HERE()-pos) 18958f0484fSRodney W. Grimes #define HERE() (p->slen) 19058f0484fSRodney W. Grimes #define THERE() (p->slen - 1) 19158f0484fSRodney W. Grimes #define THERETHERE() (p->slen - 2) 19258f0484fSRodney W. Grimes #define DROP(n) (p->slen -= (n)) 19358f0484fSRodney W. Grimes 19458f0484fSRodney W. Grimes #ifndef NDEBUG 19558f0484fSRodney W. Grimes static int never = 0; /* for use in asserts; shuts lint up */ 19658f0484fSRodney W. Grimes #else 19758f0484fSRodney W. Grimes #define never 0 /* some <assert.h>s have bugs too */ 19858f0484fSRodney W. Grimes #endif 19958f0484fSRodney W. Grimes 2006049d9f0SDaniel C. Sobral /* Macro used by computejump()/computematchjump() */ 2016049d9f0SDaniel C. Sobral #define MIN(a,b) ((a)<(b)?(a):(b)) 2026049d9f0SDaniel C. Sobral 20358f0484fSRodney W. Grimes /* 20458f0484fSRodney W. Grimes - regcomp - interface for parser and compilation 20558f0484fSRodney W. Grimes = extern int regcomp(regex_t *, const char *, int); 20658f0484fSRodney W. Grimes = #define REG_BASIC 0000 20758f0484fSRodney W. Grimes = #define REG_EXTENDED 0001 20858f0484fSRodney W. Grimes = #define REG_ICASE 0002 20958f0484fSRodney W. Grimes = #define REG_NOSUB 0004 21058f0484fSRodney W. Grimes = #define REG_NEWLINE 0010 21158f0484fSRodney W. Grimes = #define REG_NOSPEC 0020 21258f0484fSRodney W. Grimes = #define REG_PEND 0040 21358f0484fSRodney W. Grimes = #define REG_DUMP 0200 21458f0484fSRodney W. Grimes */ 21558f0484fSRodney W. Grimes int /* 0 success, otherwise REG_something */ 21654a648d1SXin LI regcomp(regex_t * __restrict preg, 21754a648d1SXin LI const char * __restrict pattern, 21854a648d1SXin LI int cflags) 21958f0484fSRodney W. Grimes { 22058f0484fSRodney W. Grimes struct parse pa; 2218fb3f3f6SDavid E. O'Brien struct re_guts *g; 2228fb3f3f6SDavid E. O'Brien struct parse *p = &pa; 2238fb3f3f6SDavid E. O'Brien int i; 2248fb3f3f6SDavid E. O'Brien size_t len; 225a89629abSXin LI size_t maxlen; 22658f0484fSRodney W. Grimes #ifdef REDEBUG 22758f0484fSRodney W. Grimes # define GOODFLAGS(f) (f) 22858f0484fSRodney W. Grimes #else 22958f0484fSRodney W. Grimes # define GOODFLAGS(f) ((f)&~REG_DUMP) 23058f0484fSRodney W. Grimes #endif 23158f0484fSRodney W. Grimes 23258f0484fSRodney W. Grimes cflags = GOODFLAGS(cflags); 23358f0484fSRodney W. Grimes if ((cflags®_EXTENDED) && (cflags®_NOSPEC)) 23458f0484fSRodney W. Grimes return(REG_INVARG); 23558f0484fSRodney W. Grimes 23658f0484fSRodney W. Grimes if (cflags®_PEND) { 23758f0484fSRodney W. Grimes if (preg->re_endp < pattern) 23858f0484fSRodney W. Grimes return(REG_INVARG); 23958f0484fSRodney W. Grimes len = preg->re_endp - pattern; 24058f0484fSRodney W. Grimes } else 2418d0f9a93SPedro F. Giffuni len = strlen(pattern); 24258f0484fSRodney W. Grimes 24358f0484fSRodney W. Grimes /* do the mallocs early so failure handling is easy */ 24480f36366STim J. Robbins g = (struct re_guts *)malloc(sizeof(struct re_guts)); 24558f0484fSRodney W. Grimes if (g == NULL) 24658f0484fSRodney W. Grimes return(REG_ESPACE); 247a89629abSXin LI /* 248a89629abSXin LI * Limit the pattern space to avoid a 32-bit overflow on buffer 249a89629abSXin LI * extension. Also avoid any signed overflow in case of conversion 250a89629abSXin LI * so make the real limit based on a 31-bit overflow. 251a89629abSXin LI * 252a89629abSXin LI * Likely not applicable on 64-bit systems but handle the case 253a89629abSXin LI * generically (who are we to stop people from using ~715MB+ 254a89629abSXin LI * patterns?). 255a89629abSXin LI */ 256a89629abSXin LI maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3; 257a89629abSXin LI if (len >= maxlen) { 258a89629abSXin LI free((char *)g); 259a89629abSXin LI return(REG_ESPACE); 260a89629abSXin LI } 26158f0484fSRodney W. Grimes p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */ 262a89629abSXin LI assert(p->ssize >= len); 263a89629abSXin LI 264e234ddefSPedro F. Giffuni p->strip = (sop *)malloc(p->ssize * sizeof(sop)); 26558f0484fSRodney W. Grimes p->slen = 0; 26658f0484fSRodney W. Grimes if (p->strip == NULL) { 26758f0484fSRodney W. Grimes free((char *)g); 26858f0484fSRodney W. Grimes return(REG_ESPACE); 26958f0484fSRodney W. Grimes } 27058f0484fSRodney W. Grimes 27158f0484fSRodney W. Grimes /* set things up */ 27258f0484fSRodney W. Grimes p->g = g; 2738d0f9a93SPedro F. Giffuni p->next = pattern; /* convenience; we do not modify it */ 27458f0484fSRodney W. Grimes p->end = p->next + len; 27558f0484fSRodney W. Grimes p->error = 0; 27658f0484fSRodney W. Grimes p->ncsalloc = 0; 27758f0484fSRodney W. Grimes for (i = 0; i < NPAREN; i++) { 27858f0484fSRodney W. Grimes p->pbegin[i] = 0; 27958f0484fSRodney W. Grimes p->pend[i] = 0; 28058f0484fSRodney W. Grimes } 28115ae9efaSKyle Evans if (cflags & REG_EXTENDED) { 28215ae9efaSKyle Evans p->allowbranch = true; 28315ae9efaSKyle Evans p->bre = false; 28415ae9efaSKyle Evans p->parse_expr = p_ere_exp; 28515ae9efaSKyle Evans p->pre_parse = NULL; 28615ae9efaSKyle Evans p->post_parse = NULL; 28715ae9efaSKyle Evans } else { 28815ae9efaSKyle Evans p->allowbranch = false; 28915ae9efaSKyle Evans p->bre = true; 29015ae9efaSKyle Evans p->parse_expr = p_simp_re; 29115ae9efaSKyle Evans p->pre_parse = p_bre_pre_parse; 29215ae9efaSKyle Evans p->post_parse = p_bre_post_parse; 29315ae9efaSKyle Evans } 29458f0484fSRodney W. Grimes g->sets = NULL; 29558f0484fSRodney W. Grimes g->ncsets = 0; 29658f0484fSRodney W. Grimes g->cflags = cflags; 29758f0484fSRodney W. Grimes g->iflags = 0; 29858f0484fSRodney W. Grimes g->nbol = 0; 29958f0484fSRodney W. Grimes g->neol = 0; 30058f0484fSRodney W. Grimes g->must = NULL; 301e6a886d8SDaniel C. Sobral g->moffset = -1; 3026b709b74SDaniel C. Sobral g->charjump = NULL; 3036b709b74SDaniel C. Sobral g->matchjump = NULL; 30458f0484fSRodney W. Grimes g->mlen = 0; 30558f0484fSRodney W. Grimes g->nsub = 0; 30658f0484fSRodney W. Grimes g->backrefs = 0; 30758f0484fSRodney W. Grimes 30858f0484fSRodney W. Grimes /* do it */ 30958f0484fSRodney W. Grimes EMIT(OEND, 0); 31058f0484fSRodney W. Grimes g->firststate = THERE(); 31115ae9efaSKyle Evans if (cflags & REG_NOSPEC) 31258f0484fSRodney W. Grimes p_str(p); 31358f0484fSRodney W. Grimes else 31415ae9efaSKyle Evans p_re(p, OUT, OUT); 31558f0484fSRodney W. Grimes EMIT(OEND, 0); 31658f0484fSRodney W. Grimes g->laststate = THERE(); 31758f0484fSRodney W. Grimes 31858f0484fSRodney W. Grimes /* tidy up loose ends and fill things in */ 31958f0484fSRodney W. Grimes stripsnug(p, g); 32058f0484fSRodney W. Grimes findmust(p, g); 3216049d9f0SDaniel C. Sobral /* only use Boyer-Moore algorithm if the pattern is bigger 3226049d9f0SDaniel C. Sobral * than three characters 3236049d9f0SDaniel C. Sobral */ 3246049d9f0SDaniel C. Sobral if(g->mlen > 3) { 3256049d9f0SDaniel C. Sobral computejumps(p, g); 3266049d9f0SDaniel C. Sobral computematchjumps(p, g); 3274d41cae8SDaniel C. Sobral if(g->matchjump == NULL && g->charjump != NULL) { 3286049d9f0SDaniel C. Sobral free(g->charjump); 3296049d9f0SDaniel C. Sobral g->charjump = NULL; 3306049d9f0SDaniel C. Sobral } 3316049d9f0SDaniel C. Sobral } 33258f0484fSRodney W. Grimes g->nplus = pluscount(p, g); 33358f0484fSRodney W. Grimes g->magic = MAGIC2; 33458f0484fSRodney W. Grimes preg->re_nsub = g->nsub; 33558f0484fSRodney W. Grimes preg->re_g = g; 33658f0484fSRodney W. Grimes preg->re_magic = MAGIC1; 33758f0484fSRodney W. Grimes #ifndef REDEBUG 33858f0484fSRodney W. Grimes /* not debugging, so can't rely on the assert() in regexec() */ 33958f0484fSRodney W. Grimes if (g->iflags&BAD) 34058f0484fSRodney W. Grimes SETERROR(REG_ASSERT); 34158f0484fSRodney W. Grimes #endif 34258f0484fSRodney W. Grimes 34358f0484fSRodney W. Grimes /* win or lose, we're done */ 34458f0484fSRodney W. Grimes if (p->error != 0) /* lose */ 34558f0484fSRodney W. Grimes regfree(preg); 34658f0484fSRodney W. Grimes return(p->error); 34758f0484fSRodney W. Grimes } 34858f0484fSRodney W. Grimes 34958f0484fSRodney W. Grimes /* 35015ae9efaSKyle Evans - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op, 35115ae9efaSKyle Evans - return whether we should terminate or not 35215ae9efaSKyle Evans == static bool p_ere_exp(struct parse *p); 35358f0484fSRodney W. Grimes */ 35415ae9efaSKyle Evans static bool 35515ae9efaSKyle Evans p_ere_exp(struct parse *p, struct branchc *bc) 35658f0484fSRodney W. Grimes { 3578fb3f3f6SDavid E. O'Brien char c; 358e5996857STim J. Robbins wint_t wc; 3598fb3f3f6SDavid E. O'Brien sopno pos; 3608fb3f3f6SDavid E. O'Brien int count; 3618fb3f3f6SDavid E. O'Brien int count2; 3628fb3f3f6SDavid E. O'Brien sopno subno; 36358f0484fSRodney W. Grimes int wascaret = 0; 36458f0484fSRodney W. Grimes 36558f0484fSRodney W. Grimes assert(MORE()); /* caller should have ensured this */ 36658f0484fSRodney W. Grimes c = GETNEXT(); 36758f0484fSRodney W. Grimes 36858f0484fSRodney W. Grimes pos = HERE(); 36958f0484fSRodney W. Grimes switch (c) { 37058f0484fSRodney W. Grimes case '(': 37151295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EPAREN); 37258f0484fSRodney W. Grimes p->g->nsub++; 37358f0484fSRodney W. Grimes subno = p->g->nsub; 37458f0484fSRodney W. Grimes if (subno < NPAREN) 37558f0484fSRodney W. Grimes p->pbegin[subno] = HERE(); 37658f0484fSRodney W. Grimes EMIT(OLPAREN, subno); 37758f0484fSRodney W. Grimes if (!SEE(')')) 37815ae9efaSKyle Evans p_re(p, ')', IGN); 37958f0484fSRodney W. Grimes if (subno < NPAREN) { 38058f0484fSRodney W. Grimes p->pend[subno] = HERE(); 38158f0484fSRodney W. Grimes assert(p->pend[subno] != 0); 38258f0484fSRodney W. Grimes } 38358f0484fSRodney W. Grimes EMIT(ORPAREN, subno); 38451295a4dSJordan K. Hubbard (void)MUSTEAT(')', REG_EPAREN); 38558f0484fSRodney W. Grimes break; 38658f0484fSRodney W. Grimes #ifndef POSIX_MISTAKE 38758f0484fSRodney W. Grimes case ')': /* happens only if no current unmatched ( */ 38858f0484fSRodney W. Grimes /* 38958f0484fSRodney W. Grimes * You may ask, why the ifndef? Because I didn't notice 39058f0484fSRodney W. Grimes * this until slightly too late for 1003.2, and none of the 39158f0484fSRodney W. Grimes * other 1003.2 regular-expression reviewers noticed it at 39258f0484fSRodney W. Grimes * all. So an unmatched ) is legal POSIX, at least until 39358f0484fSRodney W. Grimes * we can get it fixed. 39458f0484fSRodney W. Grimes */ 39558f0484fSRodney W. Grimes SETERROR(REG_EPAREN); 39658f0484fSRodney W. Grimes break; 39758f0484fSRodney W. Grimes #endif 39858f0484fSRodney W. Grimes case '^': 39958f0484fSRodney W. Grimes EMIT(OBOL, 0); 40058f0484fSRodney W. Grimes p->g->iflags |= USEBOL; 40158f0484fSRodney W. Grimes p->g->nbol++; 40258f0484fSRodney W. Grimes wascaret = 1; 40358f0484fSRodney W. Grimes break; 40458f0484fSRodney W. Grimes case '$': 40558f0484fSRodney W. Grimes EMIT(OEOL, 0); 40658f0484fSRodney W. Grimes p->g->iflags |= USEEOL; 40758f0484fSRodney W. Grimes p->g->neol++; 40858f0484fSRodney W. Grimes break; 40958f0484fSRodney W. Grimes case '|': 41058f0484fSRodney W. Grimes SETERROR(REG_EMPTY); 41158f0484fSRodney W. Grimes break; 41258f0484fSRodney W. Grimes case '*': 41358f0484fSRodney W. Grimes case '+': 41458f0484fSRodney W. Grimes case '?': 415*a4a80168SKyle Evans case '{': 41658f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 41758f0484fSRodney W. Grimes break; 41858f0484fSRodney W. Grimes case '.': 41958f0484fSRodney W. Grimes if (p->g->cflags®_NEWLINE) 42058f0484fSRodney W. Grimes nonnewline(p); 42158f0484fSRodney W. Grimes else 42258f0484fSRodney W. Grimes EMIT(OANY, 0); 42358f0484fSRodney W. Grimes break; 42458f0484fSRodney W. Grimes case '[': 42558f0484fSRodney W. Grimes p_bracket(p); 42658f0484fSRodney W. Grimes break; 42758f0484fSRodney W. Grimes case '\\': 42851295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EESCAPE); 429e5996857STim J. Robbins wc = WGETNEXT(); 4306e283683SPedro F. Giffuni switch (wc) { 4316e283683SPedro F. Giffuni case '<': 4326e283683SPedro F. Giffuni EMIT(OBOW, 0); 4336e283683SPedro F. Giffuni break; 4346e283683SPedro F. Giffuni case '>': 4356e283683SPedro F. Giffuni EMIT(OEOW, 0); 4366e283683SPedro F. Giffuni break; 4376e283683SPedro F. Giffuni default: 438e5996857STim J. Robbins ordinary(p, wc); 43958f0484fSRodney W. Grimes break; 4406e283683SPedro F. Giffuni } 4416e283683SPedro F. Giffuni break; 44258f0484fSRodney W. Grimes default: 4439806ef78SBrooks Davis if (p->error != 0) 44415ae9efaSKyle Evans return (false); 445e5996857STim J. Robbins p->next--; 446e5996857STim J. Robbins wc = WGETNEXT(); 447e5996857STim J. Robbins ordinary(p, wc); 44858f0484fSRodney W. Grimes break; 44958f0484fSRodney W. Grimes } 45058f0484fSRodney W. Grimes 45158f0484fSRodney W. Grimes if (!MORE()) 45215ae9efaSKyle Evans return (false); 45358f0484fSRodney W. Grimes c = PEEK(); 45458f0484fSRodney W. Grimes /* we call { a repetition if followed by a digit */ 455*a4a80168SKyle Evans if (!( c == '*' || c == '+' || c == '?' || c == '{')) 45615ae9efaSKyle Evans return (false); /* no repetition, we're done */ 457*a4a80168SKyle Evans else if (c == '{') 458*a4a80168SKyle Evans (void)REQUIRE(MORE2() && \ 459*a4a80168SKyle Evans (isdigit((uch)PEEK2()) || PEEK2() == ','), REG_BADRPT); 46058f0484fSRodney W. Grimes NEXT(); 46158f0484fSRodney W. Grimes 46251295a4dSJordan K. Hubbard (void)REQUIRE(!wascaret, REG_BADRPT); 46358f0484fSRodney W. Grimes switch (c) { 46458f0484fSRodney W. Grimes case '*': /* implemented as +? */ 46558f0484fSRodney W. Grimes /* this case does not require the (y|) trick, noKLUDGE */ 46658f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 46758f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 46858f0484fSRodney W. Grimes INSERT(OQUEST_, pos); 46958f0484fSRodney W. Grimes ASTERN(O_QUEST, pos); 47058f0484fSRodney W. Grimes break; 47158f0484fSRodney W. Grimes case '+': 47258f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 47358f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 47458f0484fSRodney W. Grimes break; 47558f0484fSRodney W. Grimes case '?': 47658f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 47758f0484fSRodney W. Grimes INSERT(OCH_, pos); /* offset slightly wrong */ 47858f0484fSRodney W. Grimes ASTERN(OOR1, pos); /* this one's right */ 47958f0484fSRodney W. Grimes AHEAD(pos); /* fix the OCH_ */ 48058f0484fSRodney W. Grimes EMIT(OOR2, 0); /* offset very wrong... */ 48158f0484fSRodney W. Grimes AHEAD(THERE()); /* ...so fix it */ 48258f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 48358f0484fSRodney W. Grimes break; 48458f0484fSRodney W. Grimes case '{': 48558f0484fSRodney W. Grimes count = p_count(p); 48658f0484fSRodney W. Grimes if (EAT(',')) { 48789b86d02SAndrey A. Chernov if (isdigit((uch)PEEK())) { 48858f0484fSRodney W. Grimes count2 = p_count(p); 48951295a4dSJordan K. Hubbard (void)REQUIRE(count <= count2, REG_BADBR); 49058f0484fSRodney W. Grimes } else /* single number with comma */ 49158f0484fSRodney W. Grimes count2 = INFINITY; 49258f0484fSRodney W. Grimes } else /* just a single number */ 49358f0484fSRodney W. Grimes count2 = count; 49458f0484fSRodney W. Grimes repeat(p, pos, count, count2); 49558f0484fSRodney W. Grimes if (!EAT('}')) { /* error heuristics */ 49658f0484fSRodney W. Grimes while (MORE() && PEEK() != '}') 49758f0484fSRodney W. Grimes NEXT(); 49851295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACE); 49958f0484fSRodney W. Grimes SETERROR(REG_BADBR); 50058f0484fSRodney W. Grimes } 50158f0484fSRodney W. Grimes break; 50258f0484fSRodney W. Grimes } 50358f0484fSRodney W. Grimes 50458f0484fSRodney W. Grimes if (!MORE()) 50515ae9efaSKyle Evans return (false); 50658f0484fSRodney W. Grimes c = PEEK(); 50758f0484fSRodney W. Grimes if (!( c == '*' || c == '+' || c == '?' || 50889b86d02SAndrey A. Chernov (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) ) 50915ae9efaSKyle Evans return (false); 51058f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 51115ae9efaSKyle Evans return (false); 51258f0484fSRodney W. Grimes } 51358f0484fSRodney W. Grimes 51458f0484fSRodney W. Grimes /* 51558f0484fSRodney W. Grimes - p_str - string (no metacharacters) "parser" 5168fb3f3f6SDavid E. O'Brien == static void p_str(struct parse *p); 51758f0484fSRodney W. Grimes */ 51858f0484fSRodney W. Grimes static void 51954a648d1SXin LI p_str(struct parse *p) 52058f0484fSRodney W. Grimes { 52151295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EMPTY); 52258f0484fSRodney W. Grimes while (MORE()) 523e5996857STim J. Robbins ordinary(p, WGETNEXT()); 52458f0484fSRodney W. Grimes } 52558f0484fSRodney W. Grimes 52658f0484fSRodney W. Grimes /* 52715ae9efaSKyle Evans * Eat consecutive branch delimiters for the kind of expression that we are 52815ae9efaSKyle Evans * parsing, return the number of delimiters that we ate. 52915ae9efaSKyle Evans */ 53015ae9efaSKyle Evans static int 53115ae9efaSKyle Evans p_branch_eat_delim(struct parse *p, struct branchc *bc) 53215ae9efaSKyle Evans { 53315ae9efaSKyle Evans int nskip; 53415ae9efaSKyle Evans 53515ae9efaSKyle Evans nskip = 0; 53615ae9efaSKyle Evans while (EAT('|')) 53715ae9efaSKyle Evans ++nskip; 53815ae9efaSKyle Evans return (nskip); 53915ae9efaSKyle Evans } 54015ae9efaSKyle Evans 54115ae9efaSKyle Evans /* 54215ae9efaSKyle Evans * Insert necessary branch book-keeping operations. This emits a 54315ae9efaSKyle Evans * bogus 'next' offset, since we still have more to parse 54415ae9efaSKyle Evans */ 54515ae9efaSKyle Evans static void 54615ae9efaSKyle Evans p_branch_ins_offset(struct parse *p, struct branchc *bc) 54715ae9efaSKyle Evans { 54815ae9efaSKyle Evans 54915ae9efaSKyle Evans if (bc->nbranch == 0) { 55015ae9efaSKyle Evans INSERT(OCH_, bc->start); /* offset is wrong */ 55115ae9efaSKyle Evans bc->fwd = bc->start; 55215ae9efaSKyle Evans bc->back = bc->start; 55315ae9efaSKyle Evans } 55415ae9efaSKyle Evans 55515ae9efaSKyle Evans ASTERN(OOR1, bc->back); 55615ae9efaSKyle Evans bc->back = THERE(); 55715ae9efaSKyle Evans AHEAD(bc->fwd); /* fix previous offset */ 55815ae9efaSKyle Evans bc->fwd = HERE(); 55915ae9efaSKyle Evans EMIT(OOR2, 0); /* offset is very wrong */ 56015ae9efaSKyle Evans ++bc->nbranch; 56115ae9efaSKyle Evans } 56215ae9efaSKyle Evans 56315ae9efaSKyle Evans /* 56415ae9efaSKyle Evans * Fix the offset of the tail branch, if we actually had any branches. 56515ae9efaSKyle Evans * This is to correct the bogus placeholder offset that we use. 56615ae9efaSKyle Evans */ 56715ae9efaSKyle Evans static void 56815ae9efaSKyle Evans p_branch_fix_tail(struct parse *p, struct branchc *bc) 56915ae9efaSKyle Evans { 57015ae9efaSKyle Evans 57115ae9efaSKyle Evans /* Fix bogus offset at the tail if we actually have branches */ 57215ae9efaSKyle Evans if (bc->nbranch > 0) { 57315ae9efaSKyle Evans AHEAD(bc->fwd); 57415ae9efaSKyle Evans ASTERN(O_CH, bc->back); 57515ae9efaSKyle Evans } 57615ae9efaSKyle Evans } 57715ae9efaSKyle Evans 57815ae9efaSKyle Evans /* 57915ae9efaSKyle Evans * Signal to the parser that an empty branch has been encountered; this will, 58015ae9efaSKyle Evans * in the future, be used to allow for more permissive behavior with empty 5813ea376f6SKyle Evans * branches. The return value should indicate whether parsing may continue 5823ea376f6SKyle Evans * or not. 58315ae9efaSKyle Evans */ 5843ea376f6SKyle Evans static bool 58515ae9efaSKyle Evans p_branch_empty(struct parse *p, struct branchc *bc) 58615ae9efaSKyle Evans { 58715ae9efaSKyle Evans 58815ae9efaSKyle Evans SETERROR(REG_EMPTY); 5893ea376f6SKyle Evans return (false); 59015ae9efaSKyle Evans } 59115ae9efaSKyle Evans 59215ae9efaSKyle Evans /* 59315ae9efaSKyle Evans * Take care of any branching requirements. This includes inserting the 59415ae9efaSKyle Evans * appropriate branching instructions as well as eating all of the branch 59515ae9efaSKyle Evans * delimiters until we either run out of pattern or need to parse more pattern. 59615ae9efaSKyle Evans */ 59715ae9efaSKyle Evans static bool 59815ae9efaSKyle Evans p_branch_do(struct parse *p, struct branchc *bc) 59915ae9efaSKyle Evans { 60015ae9efaSKyle Evans int ate = 0; 60115ae9efaSKyle Evans 60215ae9efaSKyle Evans ate = p_branch_eat_delim(p, bc); 60315ae9efaSKyle Evans if (ate == 0) 60415ae9efaSKyle Evans return (false); 6053ea376f6SKyle Evans else if ((ate > 1 || (bc->outer && !MORE())) && !p_branch_empty(p, bc)) 6063ea376f6SKyle Evans /* 6073ea376f6SKyle Evans * Halt parsing only if we have an empty branch and p_branch_empty 6083ea376f6SKyle Evans * indicates that we must not continue. In the future, this will not 6093ea376f6SKyle Evans * necessarily be an error. 6103ea376f6SKyle Evans */ 6113ea376f6SKyle Evans return (false); 61215ae9efaSKyle Evans p_branch_ins_offset(p, bc); 61315ae9efaSKyle Evans 61415ae9efaSKyle Evans return (true); 61515ae9efaSKyle Evans } 61615ae9efaSKyle Evans 61715ae9efaSKyle Evans static void 61815ae9efaSKyle Evans p_bre_pre_parse(struct parse *p, struct branchc *bc) 61915ae9efaSKyle Evans { 62015ae9efaSKyle Evans 62115ae9efaSKyle Evans (void) bc; 62215ae9efaSKyle Evans /* 62315ae9efaSKyle Evans * Does not move cleanly into expression parser because of 62415ae9efaSKyle Evans * ordinary interpration of * at the beginning position of 62515ae9efaSKyle Evans * an expression. 62615ae9efaSKyle Evans */ 62715ae9efaSKyle Evans if (EAT('^')) { 62815ae9efaSKyle Evans EMIT(OBOL, 0); 62915ae9efaSKyle Evans p->g->iflags |= USEBOL; 63015ae9efaSKyle Evans p->g->nbol++; 63115ae9efaSKyle Evans } 63215ae9efaSKyle Evans } 63315ae9efaSKyle Evans 63415ae9efaSKyle Evans static void 63515ae9efaSKyle Evans p_bre_post_parse(struct parse *p, struct branchc *bc) 63615ae9efaSKyle Evans { 63715ae9efaSKyle Evans 63815ae9efaSKyle Evans /* Expression is terminating due to EOL token */ 63915ae9efaSKyle Evans if (bc->terminate) { 64015ae9efaSKyle Evans DROP(1); 64115ae9efaSKyle Evans EMIT(OEOL, 0); 64215ae9efaSKyle Evans p->g->iflags |= USEEOL; 64315ae9efaSKyle Evans p->g->neol++; 64415ae9efaSKyle Evans } 64515ae9efaSKyle Evans } 64615ae9efaSKyle Evans 64715ae9efaSKyle Evans /* 64815ae9efaSKyle Evans - p_re - Top level parser, concatenation and BRE anchoring 64915ae9efaSKyle Evans == static void p_re(struct parse *p, int end1, int end2); 65058f0484fSRodney W. Grimes * Giving end1 as OUT essentially eliminates the end1/end2 check. 65158f0484fSRodney W. Grimes * 65258f0484fSRodney W. Grimes * This implementation is a bit of a kludge, in that a trailing $ is first 65380f36366STim J. Robbins * taken as an ordinary character and then revised to be an anchor. 65458f0484fSRodney W. Grimes * The amount of lookahead needed to avoid this kludge is excessive. 65558f0484fSRodney W. Grimes */ 65658f0484fSRodney W. Grimes static void 65715ae9efaSKyle Evans p_re(struct parse *p, 6585249ac86SKevin Lo int end1, /* first terminating character */ 65915ae9efaSKyle Evans int end2) /* second terminating character; ignored for EREs */ 66058f0484fSRodney W. Grimes { 66115ae9efaSKyle Evans struct branchc bc; 66258f0484fSRodney W. Grimes 66315ae9efaSKyle Evans bc.nbranch = 0; 66415ae9efaSKyle Evans if (end1 == OUT && end2 == OUT) 66515ae9efaSKyle Evans bc.outer = true; 66615ae9efaSKyle Evans else 66715ae9efaSKyle Evans bc.outer = false; 66815ae9efaSKyle Evans #define SEEEND() (!p->bre ? SEE(end1) : SEETWO(end1, end2)) 66915ae9efaSKyle Evans for (;;) { 67015ae9efaSKyle Evans bc.start = HERE(); 67115ae9efaSKyle Evans bc.nchain = 0; 67215ae9efaSKyle Evans bc.terminate = false; 67315ae9efaSKyle Evans if (p->pre_parse != NULL) 67415ae9efaSKyle Evans p->pre_parse(p, &bc); 67579c9a695SKyle Evans while (MORE() && (!p->allowbranch || !SEESPEC('|')) && !SEEEND()) { 67615ae9efaSKyle Evans bc.terminate = p->parse_expr(p, &bc); 67715ae9efaSKyle Evans ++bc.nchain; 67858f0484fSRodney W. Grimes } 67915ae9efaSKyle Evans if (p->post_parse != NULL) 68015ae9efaSKyle Evans p->post_parse(p, &bc); 68115ae9efaSKyle Evans (void) REQUIRE(HERE() != bc.start, REG_EMPTY); 68215ae9efaSKyle Evans if (!p->allowbranch) 68315ae9efaSKyle Evans break; 68415ae9efaSKyle Evans /* 68515ae9efaSKyle Evans * p_branch_do's return value indicates whether we should 68615ae9efaSKyle Evans * continue parsing or not. This is both for correctness and 68715ae9efaSKyle Evans * a slight optimization, because it will check if we've 68815ae9efaSKyle Evans * encountered an empty branch or the end of the string 68915ae9efaSKyle Evans * immediately following a branch delimiter. 69015ae9efaSKyle Evans */ 69115ae9efaSKyle Evans if (!p_branch_do(p, &bc)) 69215ae9efaSKyle Evans break; 69358f0484fSRodney W. Grimes } 69415ae9efaSKyle Evans #undef SEE_END 69515ae9efaSKyle Evans if (p->allowbranch) 69615ae9efaSKyle Evans p_branch_fix_tail(p, &bc); 69715ae9efaSKyle Evans assert(!MORE() || SEE(end1)); 69858f0484fSRodney W. Grimes } 69958f0484fSRodney W. Grimes 70058f0484fSRodney W. Grimes /* 70158f0484fSRodney W. Grimes - p_simp_re - parse a simple RE, an atom possibly followed by a repetition 70215ae9efaSKyle Evans == static bool p_simp_re(struct parse *p, struct branchc *bc); 70358f0484fSRodney W. Grimes */ 70415ae9efaSKyle Evans static bool /* was the simple RE an unbackslashed $? */ 70515ae9efaSKyle Evans p_simp_re(struct parse *p, struct branchc *bc) 70658f0484fSRodney W. Grimes { 7078fb3f3f6SDavid E. O'Brien int c; 7088fb3f3f6SDavid E. O'Brien int count; 7098fb3f3f6SDavid E. O'Brien int count2; 7108fb3f3f6SDavid E. O'Brien sopno pos; 7118fb3f3f6SDavid E. O'Brien int i; 712e5996857STim J. Robbins wint_t wc; 7138fb3f3f6SDavid E. O'Brien sopno subno; 71458f0484fSRodney W. Grimes # define BACKSL (1<<CHAR_BIT) 71558f0484fSRodney W. Grimes 71632223c1bSPedro F. Giffuni pos = HERE(); /* repetition op, if any, covers from here */ 71758f0484fSRodney W. Grimes 71858f0484fSRodney W. Grimes assert(MORE()); /* caller should have ensured this */ 71958f0484fSRodney W. Grimes c = GETNEXT(); 72058f0484fSRodney W. Grimes if (c == '\\') { 72151295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EESCAPE); 72289b86d02SAndrey A. Chernov c = BACKSL | GETNEXT(); 72358f0484fSRodney W. Grimes } 72458f0484fSRodney W. Grimes switch (c) { 72558f0484fSRodney W. Grimes case '.': 72658f0484fSRodney W. Grimes if (p->g->cflags®_NEWLINE) 72758f0484fSRodney W. Grimes nonnewline(p); 72858f0484fSRodney W. Grimes else 72958f0484fSRodney W. Grimes EMIT(OANY, 0); 73058f0484fSRodney W. Grimes break; 73158f0484fSRodney W. Grimes case '[': 73258f0484fSRodney W. Grimes p_bracket(p); 73358f0484fSRodney W. Grimes break; 7346e283683SPedro F. Giffuni case BACKSL|'<': 7356e283683SPedro F. Giffuni EMIT(OBOW, 0); 7366e283683SPedro F. Giffuni break; 7376e283683SPedro F. Giffuni case BACKSL|'>': 7386e283683SPedro F. Giffuni EMIT(OEOW, 0); 7396e283683SPedro F. Giffuni break; 74058f0484fSRodney W. Grimes case BACKSL|'{': 74158f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 74258f0484fSRodney W. Grimes break; 74358f0484fSRodney W. Grimes case BACKSL|'(': 74458f0484fSRodney W. Grimes p->g->nsub++; 74558f0484fSRodney W. Grimes subno = p->g->nsub; 74658f0484fSRodney W. Grimes if (subno < NPAREN) 74758f0484fSRodney W. Grimes p->pbegin[subno] = HERE(); 74858f0484fSRodney W. Grimes EMIT(OLPAREN, subno); 74958f0484fSRodney W. Grimes /* the MORE here is an error heuristic */ 75058f0484fSRodney W. Grimes if (MORE() && !SEETWO('\\', ')')) 75115ae9efaSKyle Evans p_re(p, '\\', ')'); 75258f0484fSRodney W. Grimes if (subno < NPAREN) { 75358f0484fSRodney W. Grimes p->pend[subno] = HERE(); 75458f0484fSRodney W. Grimes assert(p->pend[subno] != 0); 75558f0484fSRodney W. Grimes } 75658f0484fSRodney W. Grimes EMIT(ORPAREN, subno); 75751295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN); 75858f0484fSRodney W. Grimes break; 75958f0484fSRodney W. Grimes case BACKSL|')': /* should not get here -- must be user */ 76058f0484fSRodney W. Grimes SETERROR(REG_EPAREN); 76158f0484fSRodney W. Grimes break; 76258f0484fSRodney W. Grimes case BACKSL|'1': 76358f0484fSRodney W. Grimes case BACKSL|'2': 76458f0484fSRodney W. Grimes case BACKSL|'3': 76558f0484fSRodney W. Grimes case BACKSL|'4': 76658f0484fSRodney W. Grimes case BACKSL|'5': 76758f0484fSRodney W. Grimes case BACKSL|'6': 76858f0484fSRodney W. Grimes case BACKSL|'7': 76958f0484fSRodney W. Grimes case BACKSL|'8': 77058f0484fSRodney W. Grimes case BACKSL|'9': 77158f0484fSRodney W. Grimes i = (c&~BACKSL) - '0'; 77258f0484fSRodney W. Grimes assert(i < NPAREN); 77358f0484fSRodney W. Grimes if (p->pend[i] != 0) { 77458f0484fSRodney W. Grimes assert(i <= p->g->nsub); 77558f0484fSRodney W. Grimes EMIT(OBACK_, i); 77658f0484fSRodney W. Grimes assert(p->pbegin[i] != 0); 77758f0484fSRodney W. Grimes assert(OP(p->strip[p->pbegin[i]]) == OLPAREN); 77858f0484fSRodney W. Grimes assert(OP(p->strip[p->pend[i]]) == ORPAREN); 77958f0484fSRodney W. Grimes (void) dupl(p, p->pbegin[i]+1, p->pend[i]); 78058f0484fSRodney W. Grimes EMIT(O_BACK, i); 78158f0484fSRodney W. Grimes } else 78258f0484fSRodney W. Grimes SETERROR(REG_ESUBREG); 78358f0484fSRodney W. Grimes p->g->backrefs = 1; 78458f0484fSRodney W. Grimes break; 78558f0484fSRodney W. Grimes case '*': 78615ae9efaSKyle Evans /* 78715ae9efaSKyle Evans * Ordinary if used as the first character beyond BOL anchor of 78815ae9efaSKyle Evans * a (sub-)expression, counts as a bad repetition operator if it 78915ae9efaSKyle Evans * appears otherwise. 79015ae9efaSKyle Evans */ 79115ae9efaSKyle Evans (void)REQUIRE(bc->nchain == 0, REG_BADRPT); 79258f0484fSRodney W. Grimes /* FALLTHROUGH */ 79358f0484fSRodney W. Grimes default: 7949806ef78SBrooks Davis if (p->error != 0) 79515ae9efaSKyle Evans return (false); /* Definitely not $... */ 796e5996857STim J. Robbins p->next--; 797e5996857STim J. Robbins wc = WGETNEXT(); 798e5996857STim J. Robbins ordinary(p, wc); 79958f0484fSRodney W. Grimes break; 80058f0484fSRodney W. Grimes } 80158f0484fSRodney W. Grimes 80258f0484fSRodney W. Grimes if (EAT('*')) { /* implemented as +? */ 80358f0484fSRodney W. Grimes /* this case does not require the (y|) trick, noKLUDGE */ 80458f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 80558f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 80658f0484fSRodney W. Grimes INSERT(OQUEST_, pos); 80758f0484fSRodney W. Grimes ASTERN(O_QUEST, pos); 80858f0484fSRodney W. Grimes } else if (EATTWO('\\', '{')) { 80958f0484fSRodney W. Grimes count = p_count(p); 81058f0484fSRodney W. Grimes if (EAT(',')) { 81189b86d02SAndrey A. Chernov if (MORE() && isdigit((uch)PEEK())) { 81258f0484fSRodney W. Grimes count2 = p_count(p); 81351295a4dSJordan K. Hubbard (void)REQUIRE(count <= count2, REG_BADBR); 81458f0484fSRodney W. Grimes } else /* single number with comma */ 81558f0484fSRodney W. Grimes count2 = INFINITY; 81658f0484fSRodney W. Grimes } else /* just a single number */ 81758f0484fSRodney W. Grimes count2 = count; 81858f0484fSRodney W. Grimes repeat(p, pos, count, count2); 81958f0484fSRodney W. Grimes if (!EATTWO('\\', '}')) { /* error heuristics */ 82058f0484fSRodney W. Grimes while (MORE() && !SEETWO('\\', '}')) 82158f0484fSRodney W. Grimes NEXT(); 82251295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACE); 82358f0484fSRodney W. Grimes SETERROR(REG_BADBR); 82458f0484fSRodney W. Grimes } 82589b86d02SAndrey A. Chernov } else if (c == '$') /* $ (but not \$) ends it */ 82615ae9efaSKyle Evans return (true); 82758f0484fSRodney W. Grimes 82815ae9efaSKyle Evans return (false); 82958f0484fSRodney W. Grimes } 83058f0484fSRodney W. Grimes 83158f0484fSRodney W. Grimes /* 83258f0484fSRodney W. Grimes - p_count - parse a repetition count 8338fb3f3f6SDavid E. O'Brien == static int p_count(struct parse *p); 83458f0484fSRodney W. Grimes */ 83558f0484fSRodney W. Grimes static int /* the value */ 83654a648d1SXin LI p_count(struct parse *p) 83758f0484fSRodney W. Grimes { 8388fb3f3f6SDavid E. O'Brien int count = 0; 8398fb3f3f6SDavid E. O'Brien int ndigits = 0; 84058f0484fSRodney W. Grimes 84189b86d02SAndrey A. Chernov while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) { 84258f0484fSRodney W. Grimes count = count*10 + (GETNEXT() - '0'); 84358f0484fSRodney W. Grimes ndigits++; 84458f0484fSRodney W. Grimes } 84558f0484fSRodney W. Grimes 84651295a4dSJordan K. Hubbard (void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR); 84758f0484fSRodney W. Grimes return(count); 84858f0484fSRodney W. Grimes } 84958f0484fSRodney W. Grimes 85058f0484fSRodney W. Grimes /* 85158f0484fSRodney W. Grimes - p_bracket - parse a bracketed character list 8528fb3f3f6SDavid E. O'Brien == static void p_bracket(struct parse *p); 85358f0484fSRodney W. Grimes */ 85458f0484fSRodney W. Grimes static void 85554a648d1SXin LI p_bracket(struct parse *p) 85658f0484fSRodney W. Grimes { 857e5996857STim J. Robbins cset *cs; 858e5996857STim J. Robbins wint_t ch; 85958f0484fSRodney W. Grimes 86058f0484fSRodney W. Grimes /* Dept of Truly Sickening Special-Case Kludges */ 86158f0484fSRodney W. Grimes if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) { 86258f0484fSRodney W. Grimes EMIT(OBOW, 0); 86358f0484fSRodney W. Grimes NEXTn(6); 86458f0484fSRodney W. Grimes return; 86558f0484fSRodney W. Grimes } 86658f0484fSRodney W. Grimes if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) { 86758f0484fSRodney W. Grimes EMIT(OEOW, 0); 86858f0484fSRodney W. Grimes NEXTn(6); 86958f0484fSRodney W. Grimes return; 87058f0484fSRodney W. Grimes } 87158f0484fSRodney W. Grimes 872e5996857STim J. Robbins if ((cs = allocset(p)) == NULL) 873e5996857STim J. Robbins return; 874e5996857STim J. Robbins 875e5996857STim J. Robbins if (p->g->cflags®_ICASE) 876e5996857STim J. Robbins cs->icase = 1; 87758f0484fSRodney W. Grimes if (EAT('^')) 878e5996857STim J. Robbins cs->invert = 1; 87958f0484fSRodney W. Grimes if (EAT(']')) 880e5996857STim J. Robbins CHadd(p, cs, ']'); 88158f0484fSRodney W. Grimes else if (EAT('-')) 882e5996857STim J. Robbins CHadd(p, cs, '-'); 88358f0484fSRodney W. Grimes while (MORE() && PEEK() != ']' && !SEETWO('-', ']')) 88458f0484fSRodney W. Grimes p_b_term(p, cs); 88558f0484fSRodney W. Grimes if (EAT('-')) 886e5996857STim J. Robbins CHadd(p, cs, '-'); 88751295a4dSJordan K. Hubbard (void)MUSTEAT(']', REG_EBRACK); 88858f0484fSRodney W. Grimes 88958f0484fSRodney W. Grimes if (p->error != 0) /* don't mess things up further */ 89058f0484fSRodney W. Grimes return; 89158f0484fSRodney W. Grimes 892e5996857STim J. Robbins if (cs->invert && p->g->cflags®_NEWLINE) 893e5996857STim J. Robbins cs->bmp['\n' >> 3] |= 1 << ('\n' & 7); 89458f0484fSRodney W. Grimes 895e5996857STim J. Robbins if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */ 896e5996857STim J. Robbins ordinary(p, ch); 89758f0484fSRodney W. Grimes freeset(p, cs); 89858f0484fSRodney W. Grimes } else 899e5996857STim J. Robbins EMIT(OANYOF, (int)(cs - p->g->sets)); 90058f0484fSRodney W. Grimes } 90158f0484fSRodney W. Grimes 90258f0484fSRodney W. Grimes /* 90358f0484fSRodney W. Grimes - p_b_term - parse one term of a bracketed character list 9048fb3f3f6SDavid E. O'Brien == static void p_b_term(struct parse *p, cset *cs); 90558f0484fSRodney W. Grimes */ 90658f0484fSRodney W. Grimes static void 90754a648d1SXin LI p_b_term(struct parse *p, cset *cs) 90858f0484fSRodney W. Grimes { 9098fb3f3f6SDavid E. O'Brien char c; 910e5996857STim J. Robbins wint_t start, finish; 9111daad8f5SAndrey A. Chernov wint_t i; 9121daad8f5SAndrey A. Chernov struct xlocale_collate *table = 9131daad8f5SAndrey A. Chernov (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; 91458f0484fSRodney W. Grimes 91558f0484fSRodney W. Grimes /* classify what we've got */ 91658f0484fSRodney W. Grimes switch ((MORE()) ? PEEK() : '\0') { 91758f0484fSRodney W. Grimes case '[': 91858f0484fSRodney W. Grimes c = (MORE2()) ? PEEK2() : '\0'; 91958f0484fSRodney W. Grimes break; 92058f0484fSRodney W. Grimes case '-': 92158f0484fSRodney W. Grimes SETERROR(REG_ERANGE); 92258f0484fSRodney W. Grimes return; /* NOTE RETURN */ 92358f0484fSRodney W. Grimes default: 92458f0484fSRodney W. Grimes c = '\0'; 92558f0484fSRodney W. Grimes break; 92658f0484fSRodney W. Grimes } 92758f0484fSRodney W. Grimes 92858f0484fSRodney W. Grimes switch (c) { 92958f0484fSRodney W. Grimes case ':': /* character class */ 93058f0484fSRodney W. Grimes NEXT2(); 93151295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 93258f0484fSRodney W. Grimes c = PEEK(); 93351295a4dSJordan K. Hubbard (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE); 93458f0484fSRodney W. Grimes p_b_cclass(p, cs); 93551295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 93651295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE); 93758f0484fSRodney W. Grimes break; 93858f0484fSRodney W. Grimes case '=': /* equivalence class */ 93958f0484fSRodney W. Grimes NEXT2(); 94051295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 94158f0484fSRodney W. Grimes c = PEEK(); 94251295a4dSJordan K. Hubbard (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE); 94358f0484fSRodney W. Grimes p_b_eclass(p, cs); 94451295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 94551295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE); 94658f0484fSRodney W. Grimes break; 94758f0484fSRodney W. Grimes default: /* symbol, ordinary character, or range */ 94858f0484fSRodney W. Grimes start = p_b_symbol(p); 94958f0484fSRodney W. Grimes if (SEE('-') && MORE2() && PEEK2() != ']') { 95058f0484fSRodney W. Grimes /* range */ 95158f0484fSRodney W. Grimes NEXT(); 95258f0484fSRodney W. Grimes if (EAT('-')) 95358f0484fSRodney W. Grimes finish = '-'; 95458f0484fSRodney W. Grimes else 95558f0484fSRodney W. Grimes finish = p_b_symbol(p); 95658f0484fSRodney W. Grimes } else 95758f0484fSRodney W. Grimes finish = start; 9585c551438SAndrey A. Chernov if (start == finish) 959e5996857STim J. Robbins CHadd(p, cs, start); 9605c551438SAndrey A. Chernov else { 96112eae8c8SAndrey A. Chernov if (table->__collate_load_error || MB_CUR_MAX > 1) { 96212eae8c8SAndrey A. Chernov (void)REQUIRE(start <= finish, REG_ERANGE); 963e5996857STim J. Robbins CHaddrange(p, cs, start, finish); 9641daad8f5SAndrey A. Chernov } else { 96512eae8c8SAndrey A. Chernov (void)REQUIRE(__wcollate_range_cmp(start, finish) <= 0, REG_ERANGE); 9661daad8f5SAndrey A. Chernov for (i = 0; i <= UCHAR_MAX; i++) { 96712eae8c8SAndrey A. Chernov if ( __wcollate_range_cmp(start, i) <= 0 96812eae8c8SAndrey A. Chernov && __wcollate_range_cmp(i, finish) <= 0 9691daad8f5SAndrey A. Chernov ) 9701daad8f5SAndrey A. Chernov CHadd(p, cs, i); 9711daad8f5SAndrey A. Chernov } 9721daad8f5SAndrey A. Chernov } 973b5a6eb18SAndrey A. Chernov } 97458f0484fSRodney W. Grimes break; 97558f0484fSRodney W. Grimes } 97658f0484fSRodney W. Grimes } 97758f0484fSRodney W. Grimes 97858f0484fSRodney W. Grimes /* 97958f0484fSRodney W. Grimes - p_b_cclass - parse a character-class name and deal with it 9808fb3f3f6SDavid E. O'Brien == static void p_b_cclass(struct parse *p, cset *cs); 98158f0484fSRodney W. Grimes */ 98258f0484fSRodney W. Grimes static void 98354a648d1SXin LI p_b_cclass(struct parse *p, cset *cs) 98458f0484fSRodney W. Grimes { 9858d0f9a93SPedro F. Giffuni const char *sp = p->next; 9868fb3f3f6SDavid E. O'Brien size_t len; 987e5996857STim J. Robbins wctype_t wct; 988e5996857STim J. Robbins char clname[16]; 98958f0484fSRodney W. Grimes 990b5363c4aSAndrey A. Chernov while (MORE() && isalpha((uch)PEEK())) 99158f0484fSRodney W. Grimes NEXT(); 99258f0484fSRodney W. Grimes len = p->next - sp; 993e5996857STim J. Robbins if (len >= sizeof(clname) - 1) { 99458f0484fSRodney W. Grimes SETERROR(REG_ECTYPE); 99558f0484fSRodney W. Grimes return; 99658f0484fSRodney W. Grimes } 997e5996857STim J. Robbins memcpy(clname, sp, len); 998e5996857STim J. Robbins clname[len] = '\0'; 999e5996857STim J. Robbins if ((wct = wctype(clname)) == 0) { 1000e5996857STim J. Robbins SETERROR(REG_ECTYPE); 1001e5996857STim J. Robbins return; 1002b5363c4aSAndrey A. Chernov } 1003e5996857STim J. Robbins CHaddtype(p, cs, wct); 100458f0484fSRodney W. Grimes } 100558f0484fSRodney W. Grimes 100658f0484fSRodney W. Grimes /* 100758f0484fSRodney W. Grimes - p_b_eclass - parse an equivalence-class name and deal with it 10088fb3f3f6SDavid E. O'Brien == static void p_b_eclass(struct parse *p, cset *cs); 100958f0484fSRodney W. Grimes * 101058f0484fSRodney W. Grimes * This implementation is incomplete. xxx 101158f0484fSRodney W. Grimes */ 101258f0484fSRodney W. Grimes static void 101354a648d1SXin LI p_b_eclass(struct parse *p, cset *cs) 101458f0484fSRodney W. Grimes { 1015e5996857STim J. Robbins wint_t c; 101658f0484fSRodney W. Grimes 101758f0484fSRodney W. Grimes c = p_b_coll_elem(p, '='); 1018e5996857STim J. Robbins CHadd(p, cs, c); 101958f0484fSRodney W. Grimes } 102058f0484fSRodney W. Grimes 102158f0484fSRodney W. Grimes /* 102258f0484fSRodney W. Grimes - p_b_symbol - parse a character or [..]ed multicharacter collating symbol 10232bf213ebSKevin Lo == static wint_t p_b_symbol(struct parse *p); 102458f0484fSRodney W. Grimes */ 1025e5996857STim J. Robbins static wint_t /* value of symbol */ 102654a648d1SXin LI p_b_symbol(struct parse *p) 102758f0484fSRodney W. Grimes { 1028e5996857STim J. Robbins wint_t value; 102958f0484fSRodney W. Grimes 103051295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 103158f0484fSRodney W. Grimes if (!EATTWO('[', '.')) 1032e5996857STim J. Robbins return(WGETNEXT()); 103358f0484fSRodney W. Grimes 103458f0484fSRodney W. Grimes /* collating symbol */ 103558f0484fSRodney W. Grimes value = p_b_coll_elem(p, '.'); 103651295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE); 103758f0484fSRodney W. Grimes return(value); 103858f0484fSRodney W. Grimes } 103958f0484fSRodney W. Grimes 104058f0484fSRodney W. Grimes /* 104158f0484fSRodney W. Grimes - p_b_coll_elem - parse a collating-element name and look it up 10422bf213ebSKevin Lo == static wint_t p_b_coll_elem(struct parse *p, wint_t endc); 104358f0484fSRodney W. Grimes */ 1044e5996857STim J. Robbins static wint_t /* value of collating element */ 104554a648d1SXin LI p_b_coll_elem(struct parse *p, 104654a648d1SXin LI wint_t endc) /* name ended by endc,']' */ 104758f0484fSRodney W. Grimes { 10488d0f9a93SPedro F. Giffuni const char *sp = p->next; 10498fb3f3f6SDavid E. O'Brien struct cname *cp; 1050e5996857STim J. Robbins mbstate_t mbs; 1051e5996857STim J. Robbins wchar_t wc; 10528d0f9a93SPedro F. Giffuni size_t clen, len; 105358f0484fSRodney W. Grimes 105458f0484fSRodney W. Grimes while (MORE() && !SEETWO(endc, ']')) 105558f0484fSRodney W. Grimes NEXT(); 105658f0484fSRodney W. Grimes if (!MORE()) { 105758f0484fSRodney W. Grimes SETERROR(REG_EBRACK); 105858f0484fSRodney W. Grimes return(0); 105958f0484fSRodney W. Grimes } 106058f0484fSRodney W. Grimes len = p->next - sp; 106158f0484fSRodney W. Grimes for (cp = cnames; cp->name != NULL; cp++) 106258f0484fSRodney W. Grimes if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0') 106358f0484fSRodney W. Grimes return(cp->code); /* known name */ 1064e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1065e5996857STim J. Robbins if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len) 1066e5996857STim J. Robbins return (wc); /* single character */ 1067e5996857STim J. Robbins else if (clen == (size_t)-1 || clen == (size_t)-2) 1068e5996857STim J. Robbins SETERROR(REG_ILLSEQ); 1069e5996857STim J. Robbins else 107058f0484fSRodney W. Grimes SETERROR(REG_ECOLLATE); /* neither */ 107158f0484fSRodney W. Grimes return(0); 107258f0484fSRodney W. Grimes } 107358f0484fSRodney W. Grimes 107458f0484fSRodney W. Grimes /* 107558f0484fSRodney W. Grimes - othercase - return the case counterpart of an alphabetic 10762bf213ebSKevin Lo == static wint_t othercase(wint_t ch); 107758f0484fSRodney W. Grimes */ 1078e5996857STim J. Robbins static wint_t /* if no counterpart, return ch */ 107954a648d1SXin LI othercase(wint_t ch) 108058f0484fSRodney W. Grimes { 1081e5996857STim J. Robbins assert(iswalpha(ch)); 1082e5996857STim J. Robbins if (iswupper(ch)) 1083e5996857STim J. Robbins return(towlower(ch)); 1084e5996857STim J. Robbins else if (iswlower(ch)) 1085e5996857STim J. Robbins return(towupper(ch)); 108658f0484fSRodney W. Grimes else /* peculiar, but could happen */ 108758f0484fSRodney W. Grimes return(ch); 108858f0484fSRodney W. Grimes } 108958f0484fSRodney W. Grimes 109058f0484fSRodney W. Grimes /* 109158f0484fSRodney W. Grimes - bothcases - emit a dualcase version of a two-case character 10922bf213ebSKevin Lo == static void bothcases(struct parse *p, wint_t ch); 109358f0484fSRodney W. Grimes * 109458f0484fSRodney W. Grimes * Boy, is this implementation ever a kludge... 109558f0484fSRodney W. Grimes */ 109658f0484fSRodney W. Grimes static void 109754a648d1SXin LI bothcases(struct parse *p, wint_t ch) 109858f0484fSRodney W. Grimes { 10998d0f9a93SPedro F. Giffuni const char *oldnext = p->next; 11008d0f9a93SPedro F. Giffuni const char *oldend = p->end; 1101e5996857STim J. Robbins char bracket[3 + MB_LEN_MAX]; 1102e5996857STim J. Robbins size_t n; 1103e5996857STim J. Robbins mbstate_t mbs; 110458f0484fSRodney W. Grimes 110558f0484fSRodney W. Grimes assert(othercase(ch) != ch); /* p_bracket() would recurse */ 110658f0484fSRodney W. Grimes p->next = bracket; 1107e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1108e5996857STim J. Robbins n = wcrtomb(bracket, ch, &mbs); 1109e5996857STim J. Robbins assert(n != (size_t)-1); 1110e5996857STim J. Robbins bracket[n] = ']'; 1111e5996857STim J. Robbins bracket[n + 1] = '\0'; 1112e5996857STim J. Robbins p->end = bracket+n+1; 111358f0484fSRodney W. Grimes p_bracket(p); 1114e5996857STim J. Robbins assert(p->next == p->end); 111558f0484fSRodney W. Grimes p->next = oldnext; 111658f0484fSRodney W. Grimes p->end = oldend; 111758f0484fSRodney W. Grimes } 111858f0484fSRodney W. Grimes 111958f0484fSRodney W. Grimes /* 112058f0484fSRodney W. Grimes - ordinary - emit an ordinary character 11212bf213ebSKevin Lo == static void ordinary(struct parse *p, wint_t ch); 112258f0484fSRodney W. Grimes */ 112358f0484fSRodney W. Grimes static void 112454a648d1SXin LI ordinary(struct parse *p, wint_t ch) 112558f0484fSRodney W. Grimes { 1126e5996857STim J. Robbins cset *cs; 112758f0484fSRodney W. Grimes 1128e5996857STim J. Robbins if ((p->g->cflags®_ICASE) && iswalpha(ch) && othercase(ch) != ch) 112958f0484fSRodney W. Grimes bothcases(p, ch); 1130e5996857STim J. Robbins else if ((ch & OPDMASK) == ch) 1131e5996857STim J. Robbins EMIT(OCHAR, ch); 1132e5996857STim J. Robbins else { 1133e5996857STim J. Robbins /* 1134e5996857STim J. Robbins * Kludge: character is too big to fit into an OCHAR operand. 1135e5996857STim J. Robbins * Emit a singleton set. 1136e5996857STim J. Robbins */ 1137e5996857STim J. Robbins if ((cs = allocset(p)) == NULL) 1138e5996857STim J. Robbins return; 1139e5996857STim J. Robbins CHadd(p, cs, ch); 1140e5996857STim J. Robbins EMIT(OANYOF, (int)(cs - p->g->sets)); 1141e5996857STim J. Robbins } 114258f0484fSRodney W. Grimes } 114358f0484fSRodney W. Grimes 114458f0484fSRodney W. Grimes /* 114558f0484fSRodney W. Grimes - nonnewline - emit REG_NEWLINE version of OANY 11468fb3f3f6SDavid E. O'Brien == static void nonnewline(struct parse *p); 114758f0484fSRodney W. Grimes * 114858f0484fSRodney W. Grimes * Boy, is this implementation ever a kludge... 114958f0484fSRodney W. Grimes */ 115058f0484fSRodney W. Grimes static void 115154a648d1SXin LI nonnewline(struct parse *p) 115258f0484fSRodney W. Grimes { 11538d0f9a93SPedro F. Giffuni const char *oldnext = p->next; 11548d0f9a93SPedro F. Giffuni const char *oldend = p->end; 115558f0484fSRodney W. Grimes char bracket[4]; 115658f0484fSRodney W. Grimes 115758f0484fSRodney W. Grimes p->next = bracket; 115858f0484fSRodney W. Grimes p->end = bracket+3; 115958f0484fSRodney W. Grimes bracket[0] = '^'; 116058f0484fSRodney W. Grimes bracket[1] = '\n'; 116158f0484fSRodney W. Grimes bracket[2] = ']'; 116258f0484fSRodney W. Grimes bracket[3] = '\0'; 116358f0484fSRodney W. Grimes p_bracket(p); 116458f0484fSRodney W. Grimes assert(p->next == bracket+3); 116558f0484fSRodney W. Grimes p->next = oldnext; 116658f0484fSRodney W. Grimes p->end = oldend; 116758f0484fSRodney W. Grimes } 116858f0484fSRodney W. Grimes 116958f0484fSRodney W. Grimes /* 117058f0484fSRodney W. Grimes - repeat - generate code for a bounded repetition, recursively if needed 11718fb3f3f6SDavid E. O'Brien == static void repeat(struct parse *p, sopno start, int from, int to); 117258f0484fSRodney W. Grimes */ 117358f0484fSRodney W. Grimes static void 117454a648d1SXin LI repeat(struct parse *p, 117554a648d1SXin LI sopno start, /* operand from here to end of strip */ 117654a648d1SXin LI int from, /* repeated from this number */ 117754a648d1SXin LI int to) /* to this number of times (maybe INFINITY) */ 117858f0484fSRodney W. Grimes { 11798fb3f3f6SDavid E. O'Brien sopno finish = HERE(); 118058f0484fSRodney W. Grimes # define N 2 118158f0484fSRodney W. Grimes # define INF 3 118258f0484fSRodney W. Grimes # define REP(f, t) ((f)*8 + (t)) 118358f0484fSRodney W. Grimes # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N) 11848fb3f3f6SDavid E. O'Brien sopno copy; 118558f0484fSRodney W. Grimes 118658f0484fSRodney W. Grimes if (p->error != 0) /* head off possible runaway recursion */ 118758f0484fSRodney W. Grimes return; 118858f0484fSRodney W. Grimes 118958f0484fSRodney W. Grimes assert(from <= to); 119058f0484fSRodney W. Grimes 119158f0484fSRodney W. Grimes switch (REP(MAP(from), MAP(to))) { 119258f0484fSRodney W. Grimes case REP(0, 0): /* must be user doing this */ 119358f0484fSRodney W. Grimes DROP(finish-start); /* drop the operand */ 119458f0484fSRodney W. Grimes break; 119558f0484fSRodney W. Grimes case REP(0, 1): /* as x{1,1}? */ 119658f0484fSRodney W. Grimes case REP(0, N): /* as x{1,n}? */ 119758f0484fSRodney W. Grimes case REP(0, INF): /* as x{1,}? */ 119858f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 119958f0484fSRodney W. Grimes INSERT(OCH_, start); /* offset is wrong... */ 120058f0484fSRodney W. Grimes repeat(p, start+1, 1, to); 120158f0484fSRodney W. Grimes ASTERN(OOR1, start); 120258f0484fSRodney W. Grimes AHEAD(start); /* ... fix it */ 120358f0484fSRodney W. Grimes EMIT(OOR2, 0); 120458f0484fSRodney W. Grimes AHEAD(THERE()); 120558f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 120658f0484fSRodney W. Grimes break; 120758f0484fSRodney W. Grimes case REP(1, 1): /* trivial case */ 120858f0484fSRodney W. Grimes /* done */ 120958f0484fSRodney W. Grimes break; 121058f0484fSRodney W. Grimes case REP(1, N): /* as x?x{1,n-1} */ 121158f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 121258f0484fSRodney W. Grimes INSERT(OCH_, start); 121358f0484fSRodney W. Grimes ASTERN(OOR1, start); 121458f0484fSRodney W. Grimes AHEAD(start); 121558f0484fSRodney W. Grimes EMIT(OOR2, 0); /* offset very wrong... */ 121658f0484fSRodney W. Grimes AHEAD(THERE()); /* ...so fix it */ 121758f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 121858f0484fSRodney W. Grimes copy = dupl(p, start+1, finish+1); 121958f0484fSRodney W. Grimes assert(copy == finish+4); 122058f0484fSRodney W. Grimes repeat(p, copy, 1, to-1); 122158f0484fSRodney W. Grimes break; 122258f0484fSRodney W. Grimes case REP(1, INF): /* as x+ */ 122358f0484fSRodney W. Grimes INSERT(OPLUS_, start); 122458f0484fSRodney W. Grimes ASTERN(O_PLUS, start); 122558f0484fSRodney W. Grimes break; 122658f0484fSRodney W. Grimes case REP(N, N): /* as xx{m-1,n-1} */ 122758f0484fSRodney W. Grimes copy = dupl(p, start, finish); 122858f0484fSRodney W. Grimes repeat(p, copy, from-1, to-1); 122958f0484fSRodney W. Grimes break; 123058f0484fSRodney W. Grimes case REP(N, INF): /* as xx{n-1,INF} */ 123158f0484fSRodney W. Grimes copy = dupl(p, start, finish); 123258f0484fSRodney W. Grimes repeat(p, copy, from-1, to); 123358f0484fSRodney W. Grimes break; 123458f0484fSRodney W. Grimes default: /* "can't happen" */ 123558f0484fSRodney W. Grimes SETERROR(REG_ASSERT); /* just in case */ 123658f0484fSRodney W. Grimes break; 123758f0484fSRodney W. Grimes } 123858f0484fSRodney W. Grimes } 123958f0484fSRodney W. Grimes 124058f0484fSRodney W. Grimes /* 1241e5996857STim J. Robbins - wgetnext - helper function for WGETNEXT() macro. Gets the next wide 1242e5996857STim J. Robbins - character from the parse struct, signals a REG_ILLSEQ error if the 1243e5996857STim J. Robbins - character can't be converted. Returns the number of bytes consumed. 1244e5996857STim J. Robbins */ 1245e5996857STim J. Robbins static wint_t 124654a648d1SXin LI wgetnext(struct parse *p) 1247e5996857STim J. Robbins { 1248e5996857STim J. Robbins mbstate_t mbs; 1249e5996857STim J. Robbins wchar_t wc; 1250e5996857STim J. Robbins size_t n; 1251e5996857STim J. Robbins 1252e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1253e5996857STim J. Robbins n = mbrtowc(&wc, p->next, p->end - p->next, &mbs); 1254e5996857STim J. Robbins if (n == (size_t)-1 || n == (size_t)-2) { 1255e5996857STim J. Robbins SETERROR(REG_ILLSEQ); 1256e5996857STim J. Robbins return (0); 1257e5996857STim J. Robbins } 1258e5996857STim J. Robbins if (n == 0) 1259e5996857STim J. Robbins n = 1; 1260e5996857STim J. Robbins p->next += n; 1261e5996857STim J. Robbins return (wc); 1262e5996857STim J. Robbins } 1263e5996857STim J. Robbins 1264e5996857STim J. Robbins /* 126558f0484fSRodney W. Grimes - seterr - set an error condition 12668fb3f3f6SDavid E. O'Brien == static int seterr(struct parse *p, int e); 126758f0484fSRodney W. Grimes */ 126858f0484fSRodney W. Grimes static int /* useless but makes type checking happy */ 126954a648d1SXin LI seterr(struct parse *p, int e) 127058f0484fSRodney W. Grimes { 127158f0484fSRodney W. Grimes if (p->error == 0) /* keep earliest error condition */ 127258f0484fSRodney W. Grimes p->error = e; 127358f0484fSRodney W. Grimes p->next = nuls; /* try to bring things to a halt */ 127458f0484fSRodney W. Grimes p->end = nuls; 127558f0484fSRodney W. Grimes return(0); /* make the return value well-defined */ 127658f0484fSRodney W. Grimes } 127758f0484fSRodney W. Grimes 127858f0484fSRodney W. Grimes /* 127958f0484fSRodney W. Grimes - allocset - allocate a set of characters for [] 12808fb3f3f6SDavid E. O'Brien == static cset *allocset(struct parse *p); 128158f0484fSRodney W. Grimes */ 128258f0484fSRodney W. Grimes static cset * 128354a648d1SXin LI allocset(struct parse *p) 128458f0484fSRodney W. Grimes { 1285e5996857STim J. Robbins cset *cs, *ncs; 128658f0484fSRodney W. Grimes 12879f36610fSPedro F. Giffuni ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs)); 1288e5996857STim J. Robbins if (ncs == NULL) { 128958f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 1290e5996857STim J. Robbins return (NULL); 129158f0484fSRodney W. Grimes } 1292e5996857STim J. Robbins p->g->sets = ncs; 1293e5996857STim J. Robbins cs = &p->g->sets[p->g->ncsets++]; 1294e5996857STim J. Robbins memset(cs, 0, sizeof(*cs)); 129558f0484fSRodney W. Grimes 129658f0484fSRodney W. Grimes return(cs); 129758f0484fSRodney W. Grimes } 129858f0484fSRodney W. Grimes 129958f0484fSRodney W. Grimes /* 130058f0484fSRodney W. Grimes - freeset - free a now-unused set 13018fb3f3f6SDavid E. O'Brien == static void freeset(struct parse *p, cset *cs); 130258f0484fSRodney W. Grimes */ 130358f0484fSRodney W. Grimes static void 130454a648d1SXin LI freeset(struct parse *p, cset *cs) 130558f0484fSRodney W. Grimes { 13068fb3f3f6SDavid E. O'Brien cset *top = &p->g->sets[p->g->ncsets]; 130758f0484fSRodney W. Grimes 1308e5996857STim J. Robbins free(cs->wides); 1309e5996857STim J. Robbins free(cs->ranges); 1310e5996857STim J. Robbins free(cs->types); 1311e5996857STim J. Robbins memset(cs, 0, sizeof(*cs)); 131258f0484fSRodney W. Grimes if (cs == top-1) /* recover only the easy case */ 131358f0484fSRodney W. Grimes p->g->ncsets--; 131458f0484fSRodney W. Grimes } 131558f0484fSRodney W. Grimes 131658f0484fSRodney W. Grimes /* 1317e5996857STim J. Robbins - singleton - Determine whether a set contains only one character, 1318e5996857STim J. Robbins - returning it if so, otherwise returning OUT. 131958f0484fSRodney W. Grimes */ 1320e5996857STim J. Robbins static wint_t 132154a648d1SXin LI singleton(cset *cs) 132258f0484fSRodney W. Grimes { 1323e5996857STim J. Robbins wint_t i, s, n; 132458f0484fSRodney W. Grimes 1325e5996857STim J. Robbins for (i = n = 0; i < NC; i++) 1326e5996857STim J. Robbins if (CHIN(cs, i)) { 132758f0484fSRodney W. Grimes n++; 1328e5996857STim J. Robbins s = i; 1329e5996857STim J. Robbins } 1330e5996857STim J. Robbins if (n == 1) 1331e5996857STim J. Robbins return (s); 1332e5996857STim J. Robbins if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 && 1333e5996857STim J. Robbins cs->icase == 0) 1334e5996857STim J. Robbins return (cs->wides[0]); 1335e5996857STim J. Robbins /* Don't bother handling the other cases. */ 1336e5996857STim J. Robbins return (OUT); 1337e5996857STim J. Robbins } 1338e5996857STim J. Robbins 1339e5996857STim J. Robbins /* 1340e5996857STim J. Robbins - CHadd - add character to character set. 1341e5996857STim J. Robbins */ 1342e5996857STim J. Robbins static void 134354a648d1SXin LI CHadd(struct parse *p, cset *cs, wint_t ch) 1344e5996857STim J. Robbins { 134533176f17STim J. Robbins wint_t nch, *newwides; 1346e5996857STim J. Robbins assert(ch >= 0); 134733176f17STim J. Robbins if (ch < NC) 1348e5996857STim J. Robbins cs->bmp[ch >> 3] |= 1 << (ch & 7); 134933176f17STim J. Robbins else { 13509f36610fSPedro F. Giffuni newwides = reallocarray(cs->wides, cs->nwides + 1, 1351e5996857STim J. Robbins sizeof(*cs->wides)); 1352e5996857STim J. Robbins if (newwides == NULL) { 1353e5996857STim J. Robbins SETERROR(REG_ESPACE); 1354e5996857STim J. Robbins return; 1355e5996857STim J. Robbins } 1356e5996857STim J. Robbins cs->wides = newwides; 1357e5996857STim J. Robbins cs->wides[cs->nwides++] = ch; 1358e5996857STim J. Robbins } 135933176f17STim J. Robbins if (cs->icase) { 136033176f17STim J. Robbins if ((nch = towlower(ch)) < NC) 136133176f17STim J. Robbins cs->bmp[nch >> 3] |= 1 << (nch & 7); 136233176f17STim J. Robbins if ((nch = towupper(ch)) < NC) 136333176f17STim J. Robbins cs->bmp[nch >> 3] |= 1 << (nch & 7); 136433176f17STim J. Robbins } 1365e5996857STim J. Robbins } 1366e5996857STim J. Robbins 1367e5996857STim J. Robbins /* 1368e5996857STim J. Robbins - CHaddrange - add all characters in the range [min,max] to a character set. 1369e5996857STim J. Robbins */ 1370e5996857STim J. Robbins static void 137154a648d1SXin LI CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max) 1372e5996857STim J. Robbins { 1373e5996857STim J. Robbins crange *newranges; 1374e5996857STim J. Robbins 1375e5996857STim J. Robbins for (; min < NC && min <= max; min++) 1376e5996857STim J. Robbins CHadd(p, cs, min); 1377e5996857STim J. Robbins if (min >= max) 1378e5996857STim J. Robbins return; 13799f36610fSPedro F. Giffuni newranges = reallocarray(cs->ranges, cs->nranges + 1, 1380e5996857STim J. Robbins sizeof(*cs->ranges)); 1381e5996857STim J. Robbins if (newranges == NULL) { 1382e5996857STim J. Robbins SETERROR(REG_ESPACE); 1383e5996857STim J. Robbins return; 1384e5996857STim J. Robbins } 1385e5996857STim J. Robbins cs->ranges = newranges; 1386e5996857STim J. Robbins cs->ranges[cs->nranges].min = min; 1387d0ebccdeSXin LI cs->ranges[cs->nranges].max = max; 1388e5996857STim J. Robbins cs->nranges++; 1389e5996857STim J. Robbins } 1390e5996857STim J. Robbins 1391e5996857STim J. Robbins /* 1392e5996857STim J. Robbins - CHaddtype - add all characters of a certain type to a character set. 1393e5996857STim J. Robbins */ 1394e5996857STim J. Robbins static void 139554a648d1SXin LI CHaddtype(struct parse *p, cset *cs, wctype_t wct) 1396e5996857STim J. Robbins { 1397e5996857STim J. Robbins wint_t i; 1398e5996857STim J. Robbins wctype_t *newtypes; 1399e5996857STim J. Robbins 140033176f17STim J. Robbins for (i = 0; i < NC; i++) 1401e5996857STim J. Robbins if (iswctype(i, wct)) 1402e5996857STim J. Robbins CHadd(p, cs, i); 14039f36610fSPedro F. Giffuni newtypes = reallocarray(cs->types, cs->ntypes + 1, 1404e5996857STim J. Robbins sizeof(*cs->types)); 1405e5996857STim J. Robbins if (newtypes == NULL) { 1406e5996857STim J. Robbins SETERROR(REG_ESPACE); 1407e5996857STim J. Robbins return; 1408e5996857STim J. Robbins } 1409e5996857STim J. Robbins cs->types = newtypes; 1410e5996857STim J. Robbins cs->types[cs->ntypes++] = wct; 141158f0484fSRodney W. Grimes } 141258f0484fSRodney W. Grimes 141358f0484fSRodney W. Grimes /* 141458f0484fSRodney W. Grimes - dupl - emit a duplicate of a bunch of sops 14158fb3f3f6SDavid E. O'Brien == static sopno dupl(struct parse *p, sopno start, sopno finish); 141658f0484fSRodney W. Grimes */ 141758f0484fSRodney W. Grimes static sopno /* start of duplicate */ 141854a648d1SXin LI dupl(struct parse *p, 141954a648d1SXin LI sopno start, /* from here */ 142054a648d1SXin LI sopno finish) /* to this less one */ 142158f0484fSRodney W. Grimes { 14228fb3f3f6SDavid E. O'Brien sopno ret = HERE(); 14238fb3f3f6SDavid E. O'Brien sopno len = finish - start; 142458f0484fSRodney W. Grimes 142558f0484fSRodney W. Grimes assert(finish >= start); 142658f0484fSRodney W. Grimes if (len == 0) 142758f0484fSRodney W. Grimes return(ret); 14282bf213ebSKevin Lo if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */ 14292bf213ebSKevin Lo return(ret); 143058f0484fSRodney W. Grimes (void) memcpy((char *)(p->strip + p->slen), 143158f0484fSRodney W. Grimes (char *)(p->strip + start), (size_t)len*sizeof(sop)); 143258f0484fSRodney W. Grimes p->slen += len; 143358f0484fSRodney W. Grimes return(ret); 143458f0484fSRodney W. Grimes } 143558f0484fSRodney W. Grimes 143658f0484fSRodney W. Grimes /* 143758f0484fSRodney W. Grimes - doemit - emit a strip operator 14388fb3f3f6SDavid E. O'Brien == static void doemit(struct parse *p, sop op, size_t opnd); 143958f0484fSRodney W. Grimes * 144058f0484fSRodney W. Grimes * It might seem better to implement this as a macro with a function as 144158f0484fSRodney W. Grimes * hard-case backup, but it's just too big and messy unless there are 144258f0484fSRodney W. Grimes * some changes to the data structures. Maybe later. 144358f0484fSRodney W. Grimes */ 144458f0484fSRodney W. Grimes static void 144554a648d1SXin LI doemit(struct parse *p, sop op, size_t opnd) 144658f0484fSRodney W. Grimes { 144758f0484fSRodney W. Grimes /* avoid making error situations worse */ 144858f0484fSRodney W. Grimes if (p->error != 0) 144958f0484fSRodney W. Grimes return; 145058f0484fSRodney W. Grimes 145158f0484fSRodney W. Grimes /* deal with oversize operands ("can't happen", more or less) */ 145258f0484fSRodney W. Grimes assert(opnd < 1<<OPSHIFT); 145358f0484fSRodney W. Grimes 145458f0484fSRodney W. Grimes /* deal with undersized strip */ 145558f0484fSRodney W. Grimes if (p->slen >= p->ssize) 14562bf213ebSKevin Lo if (!enlarge(p, (p->ssize+1) / 2 * 3)) /* +50% */ 14572bf213ebSKevin Lo return; 145858f0484fSRodney W. Grimes 145958f0484fSRodney W. Grimes /* finally, it's all reduced to the easy case */ 146058f0484fSRodney W. Grimes p->strip[p->slen++] = SOP(op, opnd); 146158f0484fSRodney W. Grimes } 146258f0484fSRodney W. Grimes 146358f0484fSRodney W. Grimes /* 146458f0484fSRodney W. Grimes - doinsert - insert a sop into the strip 14658fb3f3f6SDavid E. O'Brien == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 146658f0484fSRodney W. Grimes */ 146758f0484fSRodney W. Grimes static void 146854a648d1SXin LI doinsert(struct parse *p, sop op, size_t opnd, sopno pos) 146958f0484fSRodney W. Grimes { 14708fb3f3f6SDavid E. O'Brien sopno sn; 14718fb3f3f6SDavid E. O'Brien sop s; 14728fb3f3f6SDavid E. O'Brien int i; 147358f0484fSRodney W. Grimes 147458f0484fSRodney W. Grimes /* avoid making error situations worse */ 147558f0484fSRodney W. Grimes if (p->error != 0) 147658f0484fSRodney W. Grimes return; 147758f0484fSRodney W. Grimes 147858f0484fSRodney W. Grimes sn = HERE(); 147958f0484fSRodney W. Grimes EMIT(op, opnd); /* do checks, ensure space */ 148058f0484fSRodney W. Grimes assert(HERE() == sn+1); 148158f0484fSRodney W. Grimes s = p->strip[sn]; 148258f0484fSRodney W. Grimes 148358f0484fSRodney W. Grimes /* adjust paren pointers */ 148458f0484fSRodney W. Grimes assert(pos > 0); 148558f0484fSRodney W. Grimes for (i = 1; i < NPAREN; i++) { 148658f0484fSRodney W. Grimes if (p->pbegin[i] >= pos) { 148758f0484fSRodney W. Grimes p->pbegin[i]++; 148858f0484fSRodney W. Grimes } 148958f0484fSRodney W. Grimes if (p->pend[i] >= pos) { 149058f0484fSRodney W. Grimes p->pend[i]++; 149158f0484fSRodney W. Grimes } 149258f0484fSRodney W. Grimes } 149358f0484fSRodney W. Grimes 149458f0484fSRodney W. Grimes memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos], 149558f0484fSRodney W. Grimes (HERE()-pos-1)*sizeof(sop)); 149658f0484fSRodney W. Grimes p->strip[pos] = s; 149758f0484fSRodney W. Grimes } 149858f0484fSRodney W. Grimes 149958f0484fSRodney W. Grimes /* 150058f0484fSRodney W. Grimes - dofwd - complete a forward reference 15018fb3f3f6SDavid E. O'Brien == static void dofwd(struct parse *p, sopno pos, sop value); 150258f0484fSRodney W. Grimes */ 150358f0484fSRodney W. Grimes static void 150454a648d1SXin LI dofwd(struct parse *p, sopno pos, sop value) 150558f0484fSRodney W. Grimes { 150658f0484fSRodney W. Grimes /* avoid making error situations worse */ 150758f0484fSRodney W. Grimes if (p->error != 0) 150858f0484fSRodney W. Grimes return; 150958f0484fSRodney W. Grimes 151058f0484fSRodney W. Grimes assert(value < 1<<OPSHIFT); 151158f0484fSRodney W. Grimes p->strip[pos] = OP(p->strip[pos]) | value; 151258f0484fSRodney W. Grimes } 151358f0484fSRodney W. Grimes 151458f0484fSRodney W. Grimes /* 151558f0484fSRodney W. Grimes - enlarge - enlarge the strip 15162bf213ebSKevin Lo == static int enlarge(struct parse *p, sopno size); 151758f0484fSRodney W. Grimes */ 15182bf213ebSKevin Lo static int 151954a648d1SXin LI enlarge(struct parse *p, sopno size) 152058f0484fSRodney W. Grimes { 15218fb3f3f6SDavid E. O'Brien sop *sp; 152258f0484fSRodney W. Grimes 152358f0484fSRodney W. Grimes if (p->ssize >= size) 15242bf213ebSKevin Lo return 1; 152558f0484fSRodney W. Grimes 15269f36610fSPedro F. Giffuni sp = reallocarray(p->strip, size, sizeof(sop)); 152758f0484fSRodney W. Grimes if (sp == NULL) { 152858f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 15292bf213ebSKevin Lo return 0; 153058f0484fSRodney W. Grimes } 153158f0484fSRodney W. Grimes p->strip = sp; 153258f0484fSRodney W. Grimes p->ssize = size; 15332bf213ebSKevin Lo return 1; 153458f0484fSRodney W. Grimes } 153558f0484fSRodney W. Grimes 153658f0484fSRodney W. Grimes /* 153758f0484fSRodney W. Grimes - stripsnug - compact the strip 15388fb3f3f6SDavid E. O'Brien == static void stripsnug(struct parse *p, struct re_guts *g); 153958f0484fSRodney W. Grimes */ 154058f0484fSRodney W. Grimes static void 154154a648d1SXin LI stripsnug(struct parse *p, struct re_guts *g) 154258f0484fSRodney W. Grimes { 154358f0484fSRodney W. Grimes g->nstates = p->slen; 15449f36610fSPedro F. Giffuni g->strip = reallocarray((char *)p->strip, p->slen, sizeof(sop)); 154558f0484fSRodney W. Grimes if (g->strip == NULL) { 154658f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 154758f0484fSRodney W. Grimes g->strip = p->strip; 154858f0484fSRodney W. Grimes } 154958f0484fSRodney W. Grimes } 155058f0484fSRodney W. Grimes 155158f0484fSRodney W. Grimes /* 155258f0484fSRodney W. Grimes - findmust - fill in must and mlen with longest mandatory literal string 15538fb3f3f6SDavid E. O'Brien == static void findmust(struct parse *p, struct re_guts *g); 155458f0484fSRodney W. Grimes * 155558f0484fSRodney W. Grimes * This algorithm could do fancy things like analyzing the operands of | 155658f0484fSRodney W. Grimes * for common subsequences. Someday. This code is simple and finds most 155758f0484fSRodney W. Grimes * of the interesting cases. 155858f0484fSRodney W. Grimes * 155958f0484fSRodney W. Grimes * Note that must and mlen got initialized during setup. 156058f0484fSRodney W. Grimes */ 156158f0484fSRodney W. Grimes static void 156254a648d1SXin LI findmust(struct parse *p, struct re_guts *g) 156358f0484fSRodney W. Grimes { 15648fb3f3f6SDavid E. O'Brien sop *scan; 1565d58abbfeSPedro F. Giffuni sop *start = NULL; 1566d58abbfeSPedro F. Giffuni sop *newstart = NULL; 15678fb3f3f6SDavid E. O'Brien sopno newlen; 15688fb3f3f6SDavid E. O'Brien sop s; 15698fb3f3f6SDavid E. O'Brien char *cp; 1570e6a886d8SDaniel C. Sobral int offset; 1571e5996857STim J. Robbins char buf[MB_LEN_MAX]; 1572e5996857STim J. Robbins size_t clen; 1573e5996857STim J. Robbins mbstate_t mbs; 157458f0484fSRodney W. Grimes 157558f0484fSRodney W. Grimes /* avoid making error situations worse */ 157658f0484fSRodney W. Grimes if (p->error != 0) 157758f0484fSRodney W. Grimes return; 157858f0484fSRodney W. Grimes 1579e5996857STim J. Robbins /* 1580e5996857STim J. Robbins * It's not generally safe to do a ``char'' substring search on 1581e5996857STim J. Robbins * multibyte character strings, but it's safe for at least 1582e5996857STim J. Robbins * UTF-8 (see RFC 3629). 1583e5996857STim J. Robbins */ 1584e5996857STim J. Robbins if (MB_CUR_MAX > 1 && 1585e5996857STim J. Robbins strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0) 1586e5996857STim J. Robbins return; 1587e5996857STim J. Robbins 158858f0484fSRodney W. Grimes /* find the longest OCHAR sequence in strip */ 158958f0484fSRodney W. Grimes newlen = 0; 1590e6a886d8SDaniel C. Sobral offset = 0; 1591e6a886d8SDaniel C. Sobral g->moffset = 0; 159258f0484fSRodney W. Grimes scan = g->strip + 1; 159358f0484fSRodney W. Grimes do { 159458f0484fSRodney W. Grimes s = *scan++; 159558f0484fSRodney W. Grimes switch (OP(s)) { 159658f0484fSRodney W. Grimes case OCHAR: /* sequence member */ 1597e5996857STim J. Robbins if (newlen == 0) { /* new sequence */ 1598e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 159958f0484fSRodney W. Grimes newstart = scan - 1; 1600e5996857STim J. Robbins } 1601e5996857STim J. Robbins clen = wcrtomb(buf, OPND(s), &mbs); 1602e5996857STim J. Robbins if (clen == (size_t)-1) 1603e5996857STim J. Robbins goto toohard; 1604e5996857STim J. Robbins newlen += clen; 160558f0484fSRodney W. Grimes break; 160658f0484fSRodney W. Grimes case OPLUS_: /* things that don't break one */ 160758f0484fSRodney W. Grimes case OLPAREN: 160858f0484fSRodney W. Grimes case ORPAREN: 160958f0484fSRodney W. Grimes break; 161058f0484fSRodney W. Grimes case OQUEST_: /* things that must be skipped */ 161158f0484fSRodney W. Grimes case OCH_: 161280f36366STim J. Robbins offset = altoffset(scan, offset); 161358f0484fSRodney W. Grimes scan--; 161458f0484fSRodney W. Grimes do { 161558f0484fSRodney W. Grimes scan += OPND(s); 161658f0484fSRodney W. Grimes s = *scan; 161758f0484fSRodney W. Grimes /* assert() interferes w debug printouts */ 161858f0484fSRodney W. Grimes if (OP(s) != O_QUEST && OP(s) != O_CH && 161958f0484fSRodney W. Grimes OP(s) != OOR2) { 162058f0484fSRodney W. Grimes g->iflags |= BAD; 162158f0484fSRodney W. Grimes return; 162258f0484fSRodney W. Grimes } 162358f0484fSRodney W. Grimes } while (OP(s) != O_QUEST && OP(s) != O_CH); 16247fed38d0SPhilippe Charnier /* FALLTHROUGH */ 1625e6a886d8SDaniel C. Sobral case OBOW: /* things that break a sequence */ 1626e6a886d8SDaniel C. Sobral case OEOW: 1627e6a886d8SDaniel C. Sobral case OBOL: 1628e6a886d8SDaniel C. Sobral case OEOL: 1629e6a886d8SDaniel C. Sobral case O_QUEST: 1630e6a886d8SDaniel C. Sobral case O_CH: 1631e6a886d8SDaniel C. Sobral case OEND: 163258f0484fSRodney W. Grimes if (newlen > g->mlen) { /* ends one */ 163358f0484fSRodney W. Grimes start = newstart; 163458f0484fSRodney W. Grimes g->mlen = newlen; 1635e6a886d8SDaniel C. Sobral if (offset > -1) { 1636e6a886d8SDaniel C. Sobral g->moffset += offset; 1637e6a886d8SDaniel C. Sobral offset = newlen; 1638e6a886d8SDaniel C. Sobral } else 1639e6a886d8SDaniel C. Sobral g->moffset = offset; 1640e6a886d8SDaniel C. Sobral } else { 1641e6a886d8SDaniel C. Sobral if (offset > -1) 1642e6a886d8SDaniel C. Sobral offset += newlen; 164358f0484fSRodney W. Grimes } 164458f0484fSRodney W. Grimes newlen = 0; 164558f0484fSRodney W. Grimes break; 1646e6a886d8SDaniel C. Sobral case OANY: 1647e6a886d8SDaniel C. Sobral if (newlen > g->mlen) { /* ends one */ 1648e6a886d8SDaniel C. Sobral start = newstart; 1649e6a886d8SDaniel C. Sobral g->mlen = newlen; 1650e6a886d8SDaniel C. Sobral if (offset > -1) { 1651e6a886d8SDaniel C. Sobral g->moffset += offset; 1652e6a886d8SDaniel C. Sobral offset = newlen; 1653e6a886d8SDaniel C. Sobral } else 1654e6a886d8SDaniel C. Sobral g->moffset = offset; 1655e6a886d8SDaniel C. Sobral } else { 1656e6a886d8SDaniel C. Sobral if (offset > -1) 1657e6a886d8SDaniel C. Sobral offset += newlen; 1658e6a886d8SDaniel C. Sobral } 1659e6a886d8SDaniel C. Sobral if (offset > -1) 1660e6a886d8SDaniel C. Sobral offset++; 1661e6a886d8SDaniel C. Sobral newlen = 0; 1662e6a886d8SDaniel C. Sobral break; 1663e6a886d8SDaniel C. Sobral case OANYOF: /* may or may not invalidate offset */ 1664e6a886d8SDaniel C. Sobral /* First, everything as OANY */ 1665e6a886d8SDaniel C. Sobral if (newlen > g->mlen) { /* ends one */ 1666e6a886d8SDaniel C. Sobral start = newstart; 1667e6a886d8SDaniel C. Sobral g->mlen = newlen; 1668e6a886d8SDaniel C. Sobral if (offset > -1) { 1669e6a886d8SDaniel C. Sobral g->moffset += offset; 1670e6a886d8SDaniel C. Sobral offset = newlen; 1671e6a886d8SDaniel C. Sobral } else 1672e6a886d8SDaniel C. Sobral g->moffset = offset; 1673e6a886d8SDaniel C. Sobral } else { 1674e6a886d8SDaniel C. Sobral if (offset > -1) 1675e6a886d8SDaniel C. Sobral offset += newlen; 1676e6a886d8SDaniel C. Sobral } 1677e6a886d8SDaniel C. Sobral if (offset > -1) 1678e6a886d8SDaniel C. Sobral offset++; 1679e6a886d8SDaniel C. Sobral newlen = 0; 1680e6a886d8SDaniel C. Sobral break; 1681e5996857STim J. Robbins toohard: 1682e6a886d8SDaniel C. Sobral default: 1683e6a886d8SDaniel C. Sobral /* Anything here makes it impossible or too hard 1684e6a886d8SDaniel C. Sobral * to calculate the offset -- so we give up; 1685e6a886d8SDaniel C. Sobral * save the last known good offset, in case the 1686e6a886d8SDaniel C. Sobral * must sequence doesn't occur later. 1687e6a886d8SDaniel C. Sobral */ 1688e6a886d8SDaniel C. Sobral if (newlen > g->mlen) { /* ends one */ 1689e6a886d8SDaniel C. Sobral start = newstart; 1690e6a886d8SDaniel C. Sobral g->mlen = newlen; 1691e6a886d8SDaniel C. Sobral if (offset > -1) 1692e6a886d8SDaniel C. Sobral g->moffset += offset; 1693e6a886d8SDaniel C. Sobral else 1694e6a886d8SDaniel C. Sobral g->moffset = offset; 1695e6a886d8SDaniel C. Sobral } 1696e6a886d8SDaniel C. Sobral offset = -1; 1697e6a886d8SDaniel C. Sobral newlen = 0; 1698e6a886d8SDaniel C. Sobral break; 169958f0484fSRodney W. Grimes } 170058f0484fSRodney W. Grimes } while (OP(s) != OEND); 170158f0484fSRodney W. Grimes 1702e6a886d8SDaniel C. Sobral if (g->mlen == 0) { /* there isn't one */ 1703e6a886d8SDaniel C. Sobral g->moffset = -1; 170458f0484fSRodney W. Grimes return; 1705e6a886d8SDaniel C. Sobral } 170658f0484fSRodney W. Grimes 170758f0484fSRodney W. Grimes /* turn it into a character string */ 170858f0484fSRodney W. Grimes g->must = malloc((size_t)g->mlen + 1); 170958f0484fSRodney W. Grimes if (g->must == NULL) { /* argh; just forget it */ 171058f0484fSRodney W. Grimes g->mlen = 0; 1711e6a886d8SDaniel C. Sobral g->moffset = -1; 171258f0484fSRodney W. Grimes return; 171358f0484fSRodney W. Grimes } 171458f0484fSRodney W. Grimes cp = g->must; 171558f0484fSRodney W. Grimes scan = start; 1716e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1717e5996857STim J. Robbins while (cp < g->must + g->mlen) { 171858f0484fSRodney W. Grimes while (OP(s = *scan++) != OCHAR) 171958f0484fSRodney W. Grimes continue; 1720e5996857STim J. Robbins clen = wcrtomb(cp, OPND(s), &mbs); 1721e5996857STim J. Robbins assert(clen != (size_t)-1); 1722e5996857STim J. Robbins cp += clen; 172358f0484fSRodney W. Grimes } 172458f0484fSRodney W. Grimes assert(cp == g->must + g->mlen); 172558f0484fSRodney W. Grimes *cp++ = '\0'; /* just on general principles */ 172658f0484fSRodney W. Grimes } 172758f0484fSRodney W. Grimes 172858f0484fSRodney W. Grimes /* 1729e6a886d8SDaniel C. Sobral - altoffset - choose biggest offset among multiple choices 173080f36366STim J. Robbins == static int altoffset(sop *scan, int offset); 1731e6a886d8SDaniel C. Sobral * 1732e6a886d8SDaniel C. Sobral * Compute, recursively if necessary, the largest offset among multiple 1733e6a886d8SDaniel C. Sobral * re paths. 1734e6a886d8SDaniel C. Sobral */ 1735e6a886d8SDaniel C. Sobral static int 173654a648d1SXin LI altoffset(sop *scan, int offset) 1737e6a886d8SDaniel C. Sobral { 1738e6a886d8SDaniel C. Sobral int largest; 1739e6a886d8SDaniel C. Sobral int try; 1740e6a886d8SDaniel C. Sobral sop s; 1741e6a886d8SDaniel C. Sobral 1742e6a886d8SDaniel C. Sobral /* If we gave up already on offsets, return */ 1743e6a886d8SDaniel C. Sobral if (offset == -1) 1744e6a886d8SDaniel C. Sobral return -1; 1745e6a886d8SDaniel C. Sobral 1746e6a886d8SDaniel C. Sobral largest = 0; 1747e6a886d8SDaniel C. Sobral try = 0; 1748e6a886d8SDaniel C. Sobral s = *scan++; 1749e6a886d8SDaniel C. Sobral while (OP(s) != O_QUEST && OP(s) != O_CH) { 1750e6a886d8SDaniel C. Sobral switch (OP(s)) { 1751e6a886d8SDaniel C. Sobral case OOR1: 1752e6a886d8SDaniel C. Sobral if (try > largest) 1753e6a886d8SDaniel C. Sobral largest = try; 1754e6a886d8SDaniel C. Sobral try = 0; 1755e6a886d8SDaniel C. Sobral break; 1756e6a886d8SDaniel C. Sobral case OQUEST_: 1757e6a886d8SDaniel C. Sobral case OCH_: 175880f36366STim J. Robbins try = altoffset(scan, try); 1759e6a886d8SDaniel C. Sobral if (try == -1) 1760e6a886d8SDaniel C. Sobral return -1; 1761e6a886d8SDaniel C. Sobral scan--; 1762e6a886d8SDaniel C. Sobral do { 1763e6a886d8SDaniel C. Sobral scan += OPND(s); 1764e6a886d8SDaniel C. Sobral s = *scan; 1765e6a886d8SDaniel C. Sobral if (OP(s) != O_QUEST && OP(s) != O_CH && 1766e6a886d8SDaniel C. Sobral OP(s) != OOR2) 1767e6a886d8SDaniel C. Sobral return -1; 1768e6a886d8SDaniel C. Sobral } while (OP(s) != O_QUEST && OP(s) != O_CH); 17698f9e434fSDaniel C. Sobral /* We must skip to the next position, or we'll 17708f9e434fSDaniel C. Sobral * leave altoffset() too early. 17718f9e434fSDaniel C. Sobral */ 17728f9e434fSDaniel C. Sobral scan++; 1773e6a886d8SDaniel C. Sobral break; 1774e6a886d8SDaniel C. Sobral case OANYOF: 1775e6a886d8SDaniel C. Sobral case OCHAR: 1776e6a886d8SDaniel C. Sobral case OANY: 1777e6a886d8SDaniel C. Sobral try++; 1778e6a886d8SDaniel C. Sobral case OBOW: 1779e6a886d8SDaniel C. Sobral case OEOW: 1780e6a886d8SDaniel C. Sobral case OLPAREN: 1781e6a886d8SDaniel C. Sobral case ORPAREN: 1782e6a886d8SDaniel C. Sobral case OOR2: 1783e6a886d8SDaniel C. Sobral break; 1784e6a886d8SDaniel C. Sobral default: 1785e6a886d8SDaniel C. Sobral try = -1; 1786e6a886d8SDaniel C. Sobral break; 1787e6a886d8SDaniel C. Sobral } 1788e6a886d8SDaniel C. Sobral if (try == -1) 1789e6a886d8SDaniel C. Sobral return -1; 1790e6a886d8SDaniel C. Sobral s = *scan++; 1791e6a886d8SDaniel C. Sobral } 1792e6a886d8SDaniel C. Sobral 1793e6a886d8SDaniel C. Sobral if (try > largest) 1794e6a886d8SDaniel C. Sobral largest = try; 1795e6a886d8SDaniel C. Sobral 1796e6a886d8SDaniel C. Sobral return largest+offset; 1797e6a886d8SDaniel C. Sobral } 1798e6a886d8SDaniel C. Sobral 1799e6a886d8SDaniel C. Sobral /* 18006049d9f0SDaniel C. Sobral - computejumps - compute char jumps for BM scan 18018fb3f3f6SDavid E. O'Brien == static void computejumps(struct parse *p, struct re_guts *g); 18026049d9f0SDaniel C. Sobral * 18036049d9f0SDaniel C. Sobral * This algorithm assumes g->must exists and is has size greater than 18046049d9f0SDaniel C. Sobral * zero. It's based on the algorithm found on Computer Algorithms by 18056049d9f0SDaniel C. Sobral * Sara Baase. 18066049d9f0SDaniel C. Sobral * 18076049d9f0SDaniel C. Sobral * A char jump is the number of characters one needs to jump based on 18086049d9f0SDaniel C. Sobral * the value of the character from the text that was mismatched. 18096049d9f0SDaniel C. Sobral */ 18106049d9f0SDaniel C. Sobral static void 181154a648d1SXin LI computejumps(struct parse *p, struct re_guts *g) 18126049d9f0SDaniel C. Sobral { 18136049d9f0SDaniel C. Sobral int ch; 18146049d9f0SDaniel C. Sobral int mindex; 18156049d9f0SDaniel C. Sobral 18166049d9f0SDaniel C. Sobral /* Avoid making errors worse */ 18176049d9f0SDaniel C. Sobral if (p->error != 0) 18186049d9f0SDaniel C. Sobral return; 18196049d9f0SDaniel C. Sobral 1820517bffcaSDaniel C. Sobral g->charjump = (int*) malloc((NC + 1) * sizeof(int)); 18216049d9f0SDaniel C. Sobral if (g->charjump == NULL) /* Not a fatal error */ 18226049d9f0SDaniel C. Sobral return; 1823c5e125bbSDaniel C. Sobral /* Adjust for signed chars, if necessary */ 1824c5e125bbSDaniel C. Sobral g->charjump = &g->charjump[-(CHAR_MIN)]; 18256049d9f0SDaniel C. Sobral 18266049d9f0SDaniel C. Sobral /* If the character does not exist in the pattern, the jump 18276049d9f0SDaniel C. Sobral * is equal to the number of characters in the pattern. 18286049d9f0SDaniel C. Sobral */ 1829c5e125bbSDaniel C. Sobral for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++) 18306049d9f0SDaniel C. Sobral g->charjump[ch] = g->mlen; 18316049d9f0SDaniel C. Sobral 18326049d9f0SDaniel C. Sobral /* If the character does exist, compute the jump that would 18336049d9f0SDaniel C. Sobral * take us to the last character in the pattern equal to it 18346049d9f0SDaniel C. Sobral * (notice that we match right to left, so that last character 18356049d9f0SDaniel C. Sobral * is the first one that would be matched). 18366049d9f0SDaniel C. Sobral */ 18376049d9f0SDaniel C. Sobral for (mindex = 0; mindex < g->mlen; mindex++) 1838e0554a53SJacques Vidrine g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1; 18396049d9f0SDaniel C. Sobral } 18406049d9f0SDaniel C. Sobral 18416049d9f0SDaniel C. Sobral /* 18426049d9f0SDaniel C. Sobral - computematchjumps - compute match jumps for BM scan 18438fb3f3f6SDavid E. O'Brien == static void computematchjumps(struct parse *p, struct re_guts *g); 18446049d9f0SDaniel C. Sobral * 18456049d9f0SDaniel C. Sobral * This algorithm assumes g->must exists and is has size greater than 18466049d9f0SDaniel C. Sobral * zero. It's based on the algorithm found on Computer Algorithms by 18476049d9f0SDaniel C. Sobral * Sara Baase. 18486049d9f0SDaniel C. Sobral * 18496049d9f0SDaniel C. Sobral * A match jump is the number of characters one needs to advance based 18506049d9f0SDaniel C. Sobral * on the already-matched suffix. 18516049d9f0SDaniel C. Sobral * Notice that all values here are minus (g->mlen-1), because of the way 18526049d9f0SDaniel C. Sobral * the search algorithm works. 18536049d9f0SDaniel C. Sobral */ 18546049d9f0SDaniel C. Sobral static void 185554a648d1SXin LI computematchjumps(struct parse *p, struct re_guts *g) 18566049d9f0SDaniel C. Sobral { 18576049d9f0SDaniel C. Sobral int mindex; /* General "must" iterator */ 18586049d9f0SDaniel C. Sobral int suffix; /* Keeps track of matching suffix */ 18596049d9f0SDaniel C. Sobral int ssuffix; /* Keeps track of suffixes' suffix */ 18606049d9f0SDaniel C. Sobral int* pmatches; /* pmatches[k] points to the next i 18616049d9f0SDaniel C. Sobral * such that i+1...mlen is a substring 18626049d9f0SDaniel C. Sobral * of k+1...k+mlen-i-1 18636049d9f0SDaniel C. Sobral */ 18646049d9f0SDaniel C. Sobral 18656049d9f0SDaniel C. Sobral /* Avoid making errors worse */ 18666049d9f0SDaniel C. Sobral if (p->error != 0) 18676049d9f0SDaniel C. Sobral return; 18686049d9f0SDaniel C. Sobral 186912eb0bcaSPedro F. Giffuni pmatches = (int*) malloc(g->mlen * sizeof(int)); 18706049d9f0SDaniel C. Sobral if (pmatches == NULL) { 18716049d9f0SDaniel C. Sobral g->matchjump = NULL; 18726049d9f0SDaniel C. Sobral return; 18736049d9f0SDaniel C. Sobral } 18746049d9f0SDaniel C. Sobral 187512eb0bcaSPedro F. Giffuni g->matchjump = (int*) malloc(g->mlen * sizeof(int)); 1876cfbebadcSXin LI if (g->matchjump == NULL) { /* Not a fatal error */ 1877cfbebadcSXin LI free(pmatches); 18786049d9f0SDaniel C. Sobral return; 1879cfbebadcSXin LI } 18806049d9f0SDaniel C. Sobral 18816049d9f0SDaniel C. Sobral /* Set maximum possible jump for each character in the pattern */ 18826049d9f0SDaniel C. Sobral for (mindex = 0; mindex < g->mlen; mindex++) 18836049d9f0SDaniel C. Sobral g->matchjump[mindex] = 2*g->mlen - mindex - 1; 18846049d9f0SDaniel C. Sobral 18856049d9f0SDaniel C. Sobral /* Compute pmatches[] */ 18866049d9f0SDaniel C. Sobral for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0; 18876049d9f0SDaniel C. Sobral mindex--, suffix--) { 18886049d9f0SDaniel C. Sobral pmatches[mindex] = suffix; 18896049d9f0SDaniel C. Sobral 18906049d9f0SDaniel C. Sobral /* If a mismatch is found, interrupting the substring, 18916049d9f0SDaniel C. Sobral * compute the matchjump for that position. If no 18926049d9f0SDaniel C. Sobral * mismatch is found, then a text substring mismatched 18936049d9f0SDaniel C. Sobral * against the suffix will also mismatch against the 18946049d9f0SDaniel C. Sobral * substring. 18956049d9f0SDaniel C. Sobral */ 18966049d9f0SDaniel C. Sobral while (suffix < g->mlen 18976049d9f0SDaniel C. Sobral && g->must[mindex] != g->must[suffix]) { 18986049d9f0SDaniel C. Sobral g->matchjump[suffix] = MIN(g->matchjump[suffix], 18996049d9f0SDaniel C. Sobral g->mlen - mindex - 1); 19006049d9f0SDaniel C. Sobral suffix = pmatches[suffix]; 19016049d9f0SDaniel C. Sobral } 19026049d9f0SDaniel C. Sobral } 19036049d9f0SDaniel C. Sobral 19046049d9f0SDaniel C. Sobral /* Compute the matchjump up to the last substring found to jump 19056049d9f0SDaniel C. Sobral * to the beginning of the largest must pattern prefix matching 19066049d9f0SDaniel C. Sobral * it's own suffix. 19076049d9f0SDaniel C. Sobral */ 19086049d9f0SDaniel C. Sobral for (mindex = 0; mindex <= suffix; mindex++) 19096049d9f0SDaniel C. Sobral g->matchjump[mindex] = MIN(g->matchjump[mindex], 19106049d9f0SDaniel C. Sobral g->mlen + suffix - mindex); 19116049d9f0SDaniel C. Sobral 19126049d9f0SDaniel C. Sobral ssuffix = pmatches[suffix]; 19136049d9f0SDaniel C. Sobral while (suffix < g->mlen) { 19149868274bSDaniel C. Sobral while (suffix <= ssuffix && suffix < g->mlen) { 19156049d9f0SDaniel C. Sobral g->matchjump[suffix] = MIN(g->matchjump[suffix], 19166049d9f0SDaniel C. Sobral g->mlen + ssuffix - suffix); 19176049d9f0SDaniel C. Sobral suffix++; 19186049d9f0SDaniel C. Sobral } 19198e2f75b8SDaniel C. Sobral if (suffix < g->mlen) 19206049d9f0SDaniel C. Sobral ssuffix = pmatches[ssuffix]; 19216049d9f0SDaniel C. Sobral } 19226049d9f0SDaniel C. Sobral 19236049d9f0SDaniel C. Sobral free(pmatches); 19246049d9f0SDaniel C. Sobral } 19256049d9f0SDaniel C. Sobral 19266049d9f0SDaniel C. Sobral /* 192758f0484fSRodney W. Grimes - pluscount - count + nesting 19288fb3f3f6SDavid E. O'Brien == static sopno pluscount(struct parse *p, struct re_guts *g); 192958f0484fSRodney W. Grimes */ 193058f0484fSRodney W. Grimes static sopno /* nesting depth */ 193154a648d1SXin LI pluscount(struct parse *p, struct re_guts *g) 193258f0484fSRodney W. Grimes { 19338fb3f3f6SDavid E. O'Brien sop *scan; 19348fb3f3f6SDavid E. O'Brien sop s; 19358fb3f3f6SDavid E. O'Brien sopno plusnest = 0; 19368fb3f3f6SDavid E. O'Brien sopno maxnest = 0; 193758f0484fSRodney W. Grimes 193858f0484fSRodney W. Grimes if (p->error != 0) 193958f0484fSRodney W. Grimes return(0); /* there may not be an OEND */ 194058f0484fSRodney W. Grimes 194158f0484fSRodney W. Grimes scan = g->strip + 1; 194258f0484fSRodney W. Grimes do { 194358f0484fSRodney W. Grimes s = *scan++; 194458f0484fSRodney W. Grimes switch (OP(s)) { 194558f0484fSRodney W. Grimes case OPLUS_: 194658f0484fSRodney W. Grimes plusnest++; 194758f0484fSRodney W. Grimes break; 194858f0484fSRodney W. Grimes case O_PLUS: 194958f0484fSRodney W. Grimes if (plusnest > maxnest) 195058f0484fSRodney W. Grimes maxnest = plusnest; 195158f0484fSRodney W. Grimes plusnest--; 195258f0484fSRodney W. Grimes break; 195358f0484fSRodney W. Grimes } 195458f0484fSRodney W. Grimes } while (OP(s) != OEND); 195558f0484fSRodney W. Grimes if (plusnest != 0) 195658f0484fSRodney W. Grimes g->iflags |= BAD; 195758f0484fSRodney W. Grimes return(maxnest); 195858f0484fSRodney W. Grimes } 1959