158f0484fSRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 458f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 Henry Spencer. 558f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 658f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 758f0484fSRodney W. Grimes * 83c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 93c87aa1dSDavid Chisnall * All rights reserved. 103c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 113c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 123c87aa1dSDavid Chisnall * 1358f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 1458f0484fSRodney W. Grimes * Henry Spencer. 1558f0484fSRodney W. Grimes * 1658f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1758f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1858f0484fSRodney W. Grimes * are met: 1958f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 2058f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 2158f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 2258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 2358f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 24fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 2558f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2658f0484fSRodney W. Grimes * without specific prior written permission. 2758f0484fSRodney W. Grimes * 2858f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2958f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3058f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3158f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3258f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3358f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3458f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3558f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3658f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3758f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3858f0484fSRodney W. Grimes * SUCH DAMAGE. 3958f0484fSRodney W. Grimes * 4058f0484fSRodney W. Grimes * @(#)regcomp.c 8.5 (Berkeley) 3/20/94 4158f0484fSRodney W. Grimes */ 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 4458f0484fSRodney W. Grimes static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94"; 4558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 46333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 47333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 4858f0484fSRodney W. Grimes 4958f0484fSRodney W. Grimes #include <sys/types.h> 5058f0484fSRodney W. Grimes #include <stdio.h> 5158f0484fSRodney W. Grimes #include <string.h> 5258f0484fSRodney W. Grimes #include <ctype.h> 5358f0484fSRodney W. Grimes #include <limits.h> 5458f0484fSRodney W. Grimes #include <stdlib.h> 5558f0484fSRodney W. Grimes #include <regex.h> 5615ae9efaSKyle Evans #include <stdbool.h> 57e5996857STim J. Robbins #include <wchar.h> 58e5996857STim J. Robbins #include <wctype.h> 5958f0484fSRodney W. Grimes 60fe5bf674SKyle Evans #ifndef LIBREGEX 611daad8f5SAndrey A. Chernov #include "collate.h" 62fe5bf674SKyle Evans #endif 631daad8f5SAndrey A. Chernov 6458f0484fSRodney W. Grimes #include "utils.h" 6558f0484fSRodney W. Grimes #include "regex2.h" 6658f0484fSRodney W. Grimes 6758f0484fSRodney W. Grimes #include "cname.h" 6858f0484fSRodney W. Grimes 6958f0484fSRodney W. Grimes /* 7015ae9efaSKyle Evans * Branching context, used to keep track of branch state for all of the branch- 7115ae9efaSKyle Evans * aware functions. In addition to keeping track of branch positions for the 7215ae9efaSKyle Evans * p_branch_* functions, we use this to simplify some clumsiness in BREs for 7315ae9efaSKyle Evans * detection of whether ^ is acting as an anchor or being used erroneously and 7415ae9efaSKyle Evans * also for whether we're in a sub-expression or not. 7515ae9efaSKyle Evans */ 7615ae9efaSKyle Evans struct branchc { 7715ae9efaSKyle Evans sopno start; 7815ae9efaSKyle Evans sopno back; 7915ae9efaSKyle Evans sopno fwd; 8015ae9efaSKyle Evans 8115ae9efaSKyle Evans int nbranch; 8215ae9efaSKyle Evans int nchain; 8315ae9efaSKyle Evans bool outer; 8415ae9efaSKyle Evans bool terminate; 8515ae9efaSKyle Evans }; 8615ae9efaSKyle Evans 8715ae9efaSKyle Evans /* 8858f0484fSRodney W. Grimes * parse structure, passed up and down to avoid global variables and 8958f0484fSRodney W. Grimes * other clumsinesses 9058f0484fSRodney W. Grimes */ 9158f0484fSRodney W. Grimes struct parse { 928d0f9a93SPedro F. Giffuni const char *next; /* next character in RE */ 938d0f9a93SPedro F. Giffuni const char *end; /* end of string (-> NUL normally) */ 9458f0484fSRodney W. Grimes int error; /* has an error been seen? */ 9558f0484fSRodney W. Grimes sop *strip; /* malloced strip */ 9658f0484fSRodney W. Grimes sopno ssize; /* malloced strip size (allocated) */ 9758f0484fSRodney W. Grimes sopno slen; /* malloced strip length (used) */ 9858f0484fSRodney W. Grimes int ncsalloc; /* number of csets allocated */ 9958f0484fSRodney W. Grimes struct re_guts *g; 10058f0484fSRodney W. Grimes # define NPAREN 10 /* we need to remember () 1-9 for back refs */ 10158f0484fSRodney W. Grimes sopno pbegin[NPAREN]; /* -> ( ([0] unused) */ 10258f0484fSRodney W. Grimes sopno pend[NPAREN]; /* -> ) ([0] unused) */ 10315ae9efaSKyle Evans bool allowbranch; /* can this expression branch? */ 10415ae9efaSKyle Evans bool bre; /* convenience; is this a BRE? */ 10515ae9efaSKyle Evans bool (*parse_expr)(struct parse *, struct branchc *); 10615ae9efaSKyle Evans void (*pre_parse)(struct parse *, struct branchc *); 10715ae9efaSKyle Evans void (*post_parse)(struct parse *, struct branchc *); 10858f0484fSRodney W. Grimes }; 10958f0484fSRodney W. Grimes 11058f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */ 11158f0484fSRodney W. Grimes #ifdef __cplusplus 11258f0484fSRodney W. Grimes extern "C" { 11358f0484fSRodney W. Grimes #endif 11458f0484fSRodney W. Grimes 11558f0484fSRodney W. Grimes /* === regcomp.c === */ 11615ae9efaSKyle Evans static bool p_ere_exp(struct parse *p, struct branchc *bc); 117c05ac53bSDavid E. O'Brien static void p_str(struct parse *p); 11815ae9efaSKyle Evans static int p_branch_eat_delim(struct parse *p, struct branchc *bc); 11915ae9efaSKyle Evans static void p_branch_ins_offset(struct parse *p, struct branchc *bc); 12015ae9efaSKyle Evans static void p_branch_fix_tail(struct parse *p, struct branchc *bc); 1213ea376f6SKyle Evans static bool p_branch_empty(struct parse *p, struct branchc *bc); 12215ae9efaSKyle Evans static bool p_branch_do(struct parse *p, struct branchc *bc); 12315ae9efaSKyle Evans static void p_bre_pre_parse(struct parse *p, struct branchc *bc); 12415ae9efaSKyle Evans static void p_bre_post_parse(struct parse *p, struct branchc *bc); 12515ae9efaSKyle Evans static void p_re(struct parse *p, int end1, int end2); 12615ae9efaSKyle Evans static bool p_simp_re(struct parse *p, struct branchc *bc); 127c05ac53bSDavid E. O'Brien static int p_count(struct parse *p); 128c05ac53bSDavid E. O'Brien static void p_bracket(struct parse *p); 129fe5bf674SKyle Evans static int p_range_cmp(wchar_t c1, wchar_t c2); 130c05ac53bSDavid E. O'Brien static void p_b_term(struct parse *p, cset *cs); 131c05ac53bSDavid E. O'Brien static void p_b_cclass(struct parse *p, cset *cs); 132c05ac53bSDavid E. O'Brien static void p_b_eclass(struct parse *p, cset *cs); 133e5996857STim J. Robbins static wint_t p_b_symbol(struct parse *p); 134e5996857STim J. Robbins static wint_t p_b_coll_elem(struct parse *p, wint_t endc); 135e5996857STim J. Robbins static wint_t othercase(wint_t ch); 136e5996857STim J. Robbins static void bothcases(struct parse *p, wint_t ch); 137e5996857STim J. Robbins static void ordinary(struct parse *p, wint_t ch); 138c05ac53bSDavid E. O'Brien static void nonnewline(struct parse *p); 139c05ac53bSDavid E. O'Brien static void repeat(struct parse *p, sopno start, int from, int to); 140c05ac53bSDavid E. O'Brien static int seterr(struct parse *p, int e); 141c05ac53bSDavid E. O'Brien static cset *allocset(struct parse *p); 142c05ac53bSDavid E. O'Brien static void freeset(struct parse *p, cset *cs); 143e5996857STim J. Robbins static void CHadd(struct parse *p, cset *cs, wint_t ch); 144e5996857STim J. Robbins static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max); 145e5996857STim J. Robbins static void CHaddtype(struct parse *p, cset *cs, wctype_t wct); 146e5996857STim J. Robbins static wint_t singleton(cset *cs); 147c05ac53bSDavid E. O'Brien static sopno dupl(struct parse *p, sopno start, sopno finish); 148c05ac53bSDavid E. O'Brien static void doemit(struct parse *p, sop op, size_t opnd); 149c05ac53bSDavid E. O'Brien static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 150c05ac53bSDavid E. O'Brien static void dofwd(struct parse *p, sopno pos, sop value); 1512bf213ebSKevin Lo static int enlarge(struct parse *p, sopno size); 152c05ac53bSDavid E. O'Brien static void stripsnug(struct parse *p, struct re_guts *g); 153c05ac53bSDavid E. O'Brien static void findmust(struct parse *p, struct re_guts *g); 15480f36366STim J. Robbins static int altoffset(sop *scan, int offset); 155c05ac53bSDavid E. O'Brien static void computejumps(struct parse *p, struct re_guts *g); 156c05ac53bSDavid E. O'Brien static void computematchjumps(struct parse *p, struct re_guts *g); 157c05ac53bSDavid E. O'Brien static sopno pluscount(struct parse *p, struct re_guts *g); 158e5996857STim J. Robbins static wint_t wgetnext(struct parse *p); 15958f0484fSRodney W. Grimes 16058f0484fSRodney W. Grimes #ifdef __cplusplus 16158f0484fSRodney W. Grimes } 16258f0484fSRodney W. Grimes #endif 16358f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */ 16458f0484fSRodney W. Grimes 16558f0484fSRodney W. Grimes static char nuls[10]; /* place to point scanner in event of error */ 16658f0484fSRodney W. Grimes 16758f0484fSRodney W. Grimes /* 16858f0484fSRodney W. Grimes * macros for use with parse structure 16958f0484fSRodney W. Grimes * BEWARE: these know that the parse structure is named `p' !!! 17058f0484fSRodney W. Grimes */ 17158f0484fSRodney W. Grimes #define PEEK() (*p->next) 17258f0484fSRodney W. Grimes #define PEEK2() (*(p->next+1)) 17358f0484fSRodney W. Grimes #define MORE() (p->next < p->end) 17458f0484fSRodney W. Grimes #define MORE2() (p->next+1 < p->end) 17558f0484fSRodney W. Grimes #define SEE(c) (MORE() && PEEK() == (c)) 17658f0484fSRodney W. Grimes #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b)) 17715ae9efaSKyle Evans #define SEESPEC(a) (p->bre ? SEETWO('\\', a) : SEE(a)) 17858f0484fSRodney W. Grimes #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0) 17958f0484fSRodney W. Grimes #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0) 18058f0484fSRodney W. Grimes #define NEXT() (p->next++) 18158f0484fSRodney W. Grimes #define NEXT2() (p->next += 2) 18258f0484fSRodney W. Grimes #define NEXTn(n) (p->next += (n)) 18358f0484fSRodney W. Grimes #define GETNEXT() (*p->next++) 184e5996857STim J. Robbins #define WGETNEXT() wgetnext(p) 18558f0484fSRodney W. Grimes #define SETERROR(e) seterr(p, (e)) 18658f0484fSRodney W. Grimes #define REQUIRE(co, e) ((co) || SETERROR(e)) 18758f0484fSRodney W. Grimes #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e)) 18858f0484fSRodney W. Grimes #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e)) 18958f0484fSRodney W. Grimes #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e)) 19058f0484fSRodney W. Grimes #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd)) 19158f0484fSRodney W. Grimes #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos) 19258f0484fSRodney W. Grimes #define AHEAD(pos) dofwd(p, pos, HERE()-(pos)) 19358f0484fSRodney W. Grimes #define ASTERN(sop, pos) EMIT(sop, HERE()-pos) 19458f0484fSRodney W. Grimes #define HERE() (p->slen) 19558f0484fSRodney W. Grimes #define THERE() (p->slen - 1) 19658f0484fSRodney W. Grimes #define THERETHERE() (p->slen - 2) 19758f0484fSRodney W. Grimes #define DROP(n) (p->slen -= (n)) 19858f0484fSRodney W. Grimes 19958f0484fSRodney W. Grimes #ifndef NDEBUG 20058f0484fSRodney W. Grimes static int never = 0; /* for use in asserts; shuts lint up */ 20158f0484fSRodney W. Grimes #else 20258f0484fSRodney W. Grimes #define never 0 /* some <assert.h>s have bugs too */ 20358f0484fSRodney W. Grimes #endif 20458f0484fSRodney W. Grimes 2056049d9f0SDaniel C. Sobral /* Macro used by computejump()/computematchjump() */ 2066049d9f0SDaniel C. Sobral #define MIN(a,b) ((a)<(b)?(a):(b)) 2076049d9f0SDaniel C. Sobral 20858f0484fSRodney W. Grimes /* 20958f0484fSRodney W. Grimes - regcomp - interface for parser and compilation 21058f0484fSRodney W. Grimes = extern int regcomp(regex_t *, const char *, int); 21158f0484fSRodney W. Grimes = #define REG_BASIC 0000 21258f0484fSRodney W. Grimes = #define REG_EXTENDED 0001 21358f0484fSRodney W. Grimes = #define REG_ICASE 0002 21458f0484fSRodney W. Grimes = #define REG_NOSUB 0004 21558f0484fSRodney W. Grimes = #define REG_NEWLINE 0010 21658f0484fSRodney W. Grimes = #define REG_NOSPEC 0020 21758f0484fSRodney W. Grimes = #define REG_PEND 0040 21858f0484fSRodney W. Grimes = #define REG_DUMP 0200 21958f0484fSRodney W. Grimes */ 22058f0484fSRodney W. Grimes int /* 0 success, otherwise REG_something */ 22154a648d1SXin LI regcomp(regex_t * __restrict preg, 22254a648d1SXin LI const char * __restrict pattern, 22354a648d1SXin LI int cflags) 22458f0484fSRodney W. Grimes { 22558f0484fSRodney W. Grimes struct parse pa; 2268fb3f3f6SDavid E. O'Brien struct re_guts *g; 2278fb3f3f6SDavid E. O'Brien struct parse *p = &pa; 2288fb3f3f6SDavid E. O'Brien int i; 2298fb3f3f6SDavid E. O'Brien size_t len; 230a89629abSXin LI size_t maxlen; 23158f0484fSRodney W. Grimes #ifdef REDEBUG 23258f0484fSRodney W. Grimes # define GOODFLAGS(f) (f) 23358f0484fSRodney W. Grimes #else 23458f0484fSRodney W. Grimes # define GOODFLAGS(f) ((f)&~REG_DUMP) 23558f0484fSRodney W. Grimes #endif 23658f0484fSRodney W. Grimes 23758f0484fSRodney W. Grimes cflags = GOODFLAGS(cflags); 23858f0484fSRodney W. Grimes if ((cflags®_EXTENDED) && (cflags®_NOSPEC)) 23958f0484fSRodney W. Grimes return(REG_INVARG); 24058f0484fSRodney W. Grimes 24158f0484fSRodney W. Grimes if (cflags®_PEND) { 24258f0484fSRodney W. Grimes if (preg->re_endp < pattern) 24358f0484fSRodney W. Grimes return(REG_INVARG); 24458f0484fSRodney W. Grimes len = preg->re_endp - pattern; 24558f0484fSRodney W. Grimes } else 2468d0f9a93SPedro F. Giffuni len = strlen(pattern); 24758f0484fSRodney W. Grimes 24858f0484fSRodney W. Grimes /* do the mallocs early so failure handling is easy */ 24980f36366STim J. Robbins g = (struct re_guts *)malloc(sizeof(struct re_guts)); 25058f0484fSRodney W. Grimes if (g == NULL) 25158f0484fSRodney W. Grimes return(REG_ESPACE); 252a89629abSXin LI /* 253a89629abSXin LI * Limit the pattern space to avoid a 32-bit overflow on buffer 254a89629abSXin LI * extension. Also avoid any signed overflow in case of conversion 255a89629abSXin LI * so make the real limit based on a 31-bit overflow. 256a89629abSXin LI * 257a89629abSXin LI * Likely not applicable on 64-bit systems but handle the case 258a89629abSXin LI * generically (who are we to stop people from using ~715MB+ 259a89629abSXin LI * patterns?). 260a89629abSXin LI */ 261a89629abSXin LI maxlen = ((size_t)-1 >> 1) / sizeof(sop) * 2 / 3; 262a89629abSXin LI if (len >= maxlen) { 263a89629abSXin LI free((char *)g); 264a89629abSXin LI return(REG_ESPACE); 265a89629abSXin LI } 26658f0484fSRodney W. Grimes p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */ 267a89629abSXin LI assert(p->ssize >= len); 268a89629abSXin LI 269e234ddefSPedro F. Giffuni p->strip = (sop *)malloc(p->ssize * sizeof(sop)); 27058f0484fSRodney W. Grimes p->slen = 0; 27158f0484fSRodney W. Grimes if (p->strip == NULL) { 27258f0484fSRodney W. Grimes free((char *)g); 27358f0484fSRodney W. Grimes return(REG_ESPACE); 27458f0484fSRodney W. Grimes } 27558f0484fSRodney W. Grimes 27658f0484fSRodney W. Grimes /* set things up */ 27758f0484fSRodney W. Grimes p->g = g; 2788d0f9a93SPedro F. Giffuni p->next = pattern; /* convenience; we do not modify it */ 27958f0484fSRodney W. Grimes p->end = p->next + len; 28058f0484fSRodney W. Grimes p->error = 0; 28158f0484fSRodney W. Grimes p->ncsalloc = 0; 28258f0484fSRodney W. Grimes for (i = 0; i < NPAREN; i++) { 28358f0484fSRodney W. Grimes p->pbegin[i] = 0; 28458f0484fSRodney W. Grimes p->pend[i] = 0; 28558f0484fSRodney W. Grimes } 28615ae9efaSKyle Evans if (cflags & REG_EXTENDED) { 28715ae9efaSKyle Evans p->allowbranch = true; 28815ae9efaSKyle Evans p->bre = false; 28915ae9efaSKyle Evans p->parse_expr = p_ere_exp; 29015ae9efaSKyle Evans p->pre_parse = NULL; 29115ae9efaSKyle Evans p->post_parse = NULL; 29215ae9efaSKyle Evans } else { 29315ae9efaSKyle Evans p->allowbranch = false; 29415ae9efaSKyle Evans p->bre = true; 29515ae9efaSKyle Evans p->parse_expr = p_simp_re; 29615ae9efaSKyle Evans p->pre_parse = p_bre_pre_parse; 29715ae9efaSKyle Evans p->post_parse = p_bre_post_parse; 29815ae9efaSKyle Evans } 29958f0484fSRodney W. Grimes g->sets = NULL; 30058f0484fSRodney W. Grimes g->ncsets = 0; 30158f0484fSRodney W. Grimes g->cflags = cflags; 30258f0484fSRodney W. Grimes g->iflags = 0; 30358f0484fSRodney W. Grimes g->nbol = 0; 30458f0484fSRodney W. Grimes g->neol = 0; 30558f0484fSRodney W. Grimes g->must = NULL; 306e6a886d8SDaniel C. Sobral g->moffset = -1; 3076b709b74SDaniel C. Sobral g->charjump = NULL; 3086b709b74SDaniel C. Sobral g->matchjump = NULL; 30958f0484fSRodney W. Grimes g->mlen = 0; 31058f0484fSRodney W. Grimes g->nsub = 0; 31158f0484fSRodney W. Grimes g->backrefs = 0; 31258f0484fSRodney W. Grimes 31358f0484fSRodney W. Grimes /* do it */ 31458f0484fSRodney W. Grimes EMIT(OEND, 0); 31558f0484fSRodney W. Grimes g->firststate = THERE(); 31615ae9efaSKyle Evans if (cflags & REG_NOSPEC) 31758f0484fSRodney W. Grimes p_str(p); 31858f0484fSRodney W. Grimes else 31915ae9efaSKyle Evans p_re(p, OUT, OUT); 32058f0484fSRodney W. Grimes EMIT(OEND, 0); 32158f0484fSRodney W. Grimes g->laststate = THERE(); 32258f0484fSRodney W. Grimes 32358f0484fSRodney W. Grimes /* tidy up loose ends and fill things in */ 32458f0484fSRodney W. Grimes stripsnug(p, g); 32558f0484fSRodney W. Grimes findmust(p, g); 3266049d9f0SDaniel C. Sobral /* only use Boyer-Moore algorithm if the pattern is bigger 3276049d9f0SDaniel C. Sobral * than three characters 3286049d9f0SDaniel C. Sobral */ 3296049d9f0SDaniel C. Sobral if(g->mlen > 3) { 3306049d9f0SDaniel C. Sobral computejumps(p, g); 3316049d9f0SDaniel C. Sobral computematchjumps(p, g); 3324d41cae8SDaniel C. Sobral if(g->matchjump == NULL && g->charjump != NULL) { 3336049d9f0SDaniel C. Sobral free(g->charjump); 3346049d9f0SDaniel C. Sobral g->charjump = NULL; 3356049d9f0SDaniel C. Sobral } 3366049d9f0SDaniel C. Sobral } 33758f0484fSRodney W. Grimes g->nplus = pluscount(p, g); 33858f0484fSRodney W. Grimes g->magic = MAGIC2; 33958f0484fSRodney W. Grimes preg->re_nsub = g->nsub; 34058f0484fSRodney W. Grimes preg->re_g = g; 34158f0484fSRodney W. Grimes preg->re_magic = MAGIC1; 34258f0484fSRodney W. Grimes #ifndef REDEBUG 34358f0484fSRodney W. Grimes /* not debugging, so can't rely on the assert() in regexec() */ 34458f0484fSRodney W. Grimes if (g->iflags&BAD) 34558f0484fSRodney W. Grimes SETERROR(REG_ASSERT); 34658f0484fSRodney W. Grimes #endif 34758f0484fSRodney W. Grimes 34858f0484fSRodney W. Grimes /* win or lose, we're done */ 34958f0484fSRodney W. Grimes if (p->error != 0) /* lose */ 35058f0484fSRodney W. Grimes regfree(preg); 35158f0484fSRodney W. Grimes return(p->error); 35258f0484fSRodney W. Grimes } 35358f0484fSRodney W. Grimes 35458f0484fSRodney W. Grimes /* 35515ae9efaSKyle Evans - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op, 35615ae9efaSKyle Evans - return whether we should terminate or not 35715ae9efaSKyle Evans == static bool p_ere_exp(struct parse *p); 35858f0484fSRodney W. Grimes */ 35915ae9efaSKyle Evans static bool 36015ae9efaSKyle Evans p_ere_exp(struct parse *p, struct branchc *bc) 36158f0484fSRodney W. Grimes { 3628fb3f3f6SDavid E. O'Brien char c; 363e5996857STim J. Robbins wint_t wc; 3648fb3f3f6SDavid E. O'Brien sopno pos; 3658fb3f3f6SDavid E. O'Brien int count; 3668fb3f3f6SDavid E. O'Brien int count2; 3678fb3f3f6SDavid E. O'Brien sopno subno; 36858f0484fSRodney W. Grimes int wascaret = 0; 36958f0484fSRodney W. Grimes 3704f8f1c79SKyle Evans (void)bc; 37158f0484fSRodney W. Grimes assert(MORE()); /* caller should have ensured this */ 37258f0484fSRodney W. Grimes c = GETNEXT(); 37358f0484fSRodney W. Grimes 37458f0484fSRodney W. Grimes pos = HERE(); 37558f0484fSRodney W. Grimes switch (c) { 37658f0484fSRodney W. Grimes case '(': 37751295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EPAREN); 37858f0484fSRodney W. Grimes p->g->nsub++; 37958f0484fSRodney W. Grimes subno = p->g->nsub; 38058f0484fSRodney W. Grimes if (subno < NPAREN) 38158f0484fSRodney W. Grimes p->pbegin[subno] = HERE(); 38258f0484fSRodney W. Grimes EMIT(OLPAREN, subno); 38358f0484fSRodney W. Grimes if (!SEE(')')) 38415ae9efaSKyle Evans p_re(p, ')', IGN); 38558f0484fSRodney W. Grimes if (subno < NPAREN) { 38658f0484fSRodney W. Grimes p->pend[subno] = HERE(); 38758f0484fSRodney W. Grimes assert(p->pend[subno] != 0); 38858f0484fSRodney W. Grimes } 38958f0484fSRodney W. Grimes EMIT(ORPAREN, subno); 39051295a4dSJordan K. Hubbard (void)MUSTEAT(')', REG_EPAREN); 39158f0484fSRodney W. Grimes break; 39258f0484fSRodney W. Grimes #ifndef POSIX_MISTAKE 39358f0484fSRodney W. Grimes case ')': /* happens only if no current unmatched ( */ 39458f0484fSRodney W. Grimes /* 39558f0484fSRodney W. Grimes * You may ask, why the ifndef? Because I didn't notice 39658f0484fSRodney W. Grimes * this until slightly too late for 1003.2, and none of the 39758f0484fSRodney W. Grimes * other 1003.2 regular-expression reviewers noticed it at 39858f0484fSRodney W. Grimes * all. So an unmatched ) is legal POSIX, at least until 39958f0484fSRodney W. Grimes * we can get it fixed. 40058f0484fSRodney W. Grimes */ 40158f0484fSRodney W. Grimes SETERROR(REG_EPAREN); 40258f0484fSRodney W. Grimes break; 40358f0484fSRodney W. Grimes #endif 40458f0484fSRodney W. Grimes case '^': 40558f0484fSRodney W. Grimes EMIT(OBOL, 0); 40658f0484fSRodney W. Grimes p->g->iflags |= USEBOL; 40758f0484fSRodney W. Grimes p->g->nbol++; 40858f0484fSRodney W. Grimes wascaret = 1; 40958f0484fSRodney W. Grimes break; 41058f0484fSRodney W. Grimes case '$': 41158f0484fSRodney W. Grimes EMIT(OEOL, 0); 41258f0484fSRodney W. Grimes p->g->iflags |= USEEOL; 41358f0484fSRodney W. Grimes p->g->neol++; 41458f0484fSRodney W. Grimes break; 41558f0484fSRodney W. Grimes case '|': 41658f0484fSRodney W. Grimes SETERROR(REG_EMPTY); 41758f0484fSRodney W. Grimes break; 41858f0484fSRodney W. Grimes case '*': 41958f0484fSRodney W. Grimes case '+': 42058f0484fSRodney W. Grimes case '?': 421a4a80168SKyle Evans case '{': 42258f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 42358f0484fSRodney W. Grimes break; 42458f0484fSRodney W. Grimes case '.': 42558f0484fSRodney W. Grimes if (p->g->cflags®_NEWLINE) 42658f0484fSRodney W. Grimes nonnewline(p); 42758f0484fSRodney W. Grimes else 42858f0484fSRodney W. Grimes EMIT(OANY, 0); 42958f0484fSRodney W. Grimes break; 43058f0484fSRodney W. Grimes case '[': 43158f0484fSRodney W. Grimes p_bracket(p); 43258f0484fSRodney W. Grimes break; 43358f0484fSRodney W. Grimes case '\\': 43451295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EESCAPE); 435e5996857STim J. Robbins wc = WGETNEXT(); 4366e283683SPedro F. Giffuni switch (wc) { 4376e283683SPedro F. Giffuni case '<': 4386e283683SPedro F. Giffuni EMIT(OBOW, 0); 4396e283683SPedro F. Giffuni break; 4406e283683SPedro F. Giffuni case '>': 4416e283683SPedro F. Giffuni EMIT(OEOW, 0); 4426e283683SPedro F. Giffuni break; 4436e283683SPedro F. Giffuni default: 444e5996857STim J. Robbins ordinary(p, wc); 44558f0484fSRodney W. Grimes break; 4466e283683SPedro F. Giffuni } 4476e283683SPedro F. Giffuni break; 44858f0484fSRodney W. Grimes default: 4499806ef78SBrooks Davis if (p->error != 0) 45015ae9efaSKyle Evans return (false); 451e5996857STim J. Robbins p->next--; 452e5996857STim J. Robbins wc = WGETNEXT(); 453e5996857STim J. Robbins ordinary(p, wc); 45458f0484fSRodney W. Grimes break; 45558f0484fSRodney W. Grimes } 45658f0484fSRodney W. Grimes 45758f0484fSRodney W. Grimes if (!MORE()) 45815ae9efaSKyle Evans return (false); 45958f0484fSRodney W. Grimes c = PEEK(); 46058f0484fSRodney W. Grimes /* we call { a repetition if followed by a digit */ 461a4a80168SKyle Evans if (!( c == '*' || c == '+' || c == '?' || c == '{')) 46215ae9efaSKyle Evans return (false); /* no repetition, we're done */ 463a4a80168SKyle Evans else if (c == '{') 464a4a80168SKyle Evans (void)REQUIRE(MORE2() && \ 465a4a80168SKyle Evans (isdigit((uch)PEEK2()) || PEEK2() == ','), REG_BADRPT); 46658f0484fSRodney W. Grimes NEXT(); 46758f0484fSRodney W. Grimes 46851295a4dSJordan K. Hubbard (void)REQUIRE(!wascaret, REG_BADRPT); 46958f0484fSRodney W. Grimes switch (c) { 47058f0484fSRodney W. Grimes case '*': /* implemented as +? */ 47158f0484fSRodney W. Grimes /* this case does not require the (y|) trick, noKLUDGE */ 47258f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 47358f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 47458f0484fSRodney W. Grimes INSERT(OQUEST_, pos); 47558f0484fSRodney W. Grimes ASTERN(O_QUEST, pos); 47658f0484fSRodney W. Grimes break; 47758f0484fSRodney W. Grimes case '+': 47858f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 47958f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 48058f0484fSRodney W. Grimes break; 48158f0484fSRodney W. Grimes case '?': 48258f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 48358f0484fSRodney W. Grimes INSERT(OCH_, pos); /* offset slightly wrong */ 48458f0484fSRodney W. Grimes ASTERN(OOR1, pos); /* this one's right */ 48558f0484fSRodney W. Grimes AHEAD(pos); /* fix the OCH_ */ 48658f0484fSRodney W. Grimes EMIT(OOR2, 0); /* offset very wrong... */ 48758f0484fSRodney W. Grimes AHEAD(THERE()); /* ...so fix it */ 48858f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 48958f0484fSRodney W. Grimes break; 49058f0484fSRodney W. Grimes case '{': 49158f0484fSRodney W. Grimes count = p_count(p); 49258f0484fSRodney W. Grimes if (EAT(',')) { 49389b86d02SAndrey A. Chernov if (isdigit((uch)PEEK())) { 49458f0484fSRodney W. Grimes count2 = p_count(p); 49551295a4dSJordan K. Hubbard (void)REQUIRE(count <= count2, REG_BADBR); 49658f0484fSRodney W. Grimes } else /* single number with comma */ 49758f0484fSRodney W. Grimes count2 = INFINITY; 49858f0484fSRodney W. Grimes } else /* just a single number */ 49958f0484fSRodney W. Grimes count2 = count; 50058f0484fSRodney W. Grimes repeat(p, pos, count, count2); 50158f0484fSRodney W. Grimes if (!EAT('}')) { /* error heuristics */ 50258f0484fSRodney W. Grimes while (MORE() && PEEK() != '}') 50358f0484fSRodney W. Grimes NEXT(); 50451295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACE); 50558f0484fSRodney W. Grimes SETERROR(REG_BADBR); 50658f0484fSRodney W. Grimes } 50758f0484fSRodney W. Grimes break; 50858f0484fSRodney W. Grimes } 50958f0484fSRodney W. Grimes 51058f0484fSRodney W. Grimes if (!MORE()) 51115ae9efaSKyle Evans return (false); 51258f0484fSRodney W. Grimes c = PEEK(); 51358f0484fSRodney W. Grimes if (!( c == '*' || c == '+' || c == '?' || 51489b86d02SAndrey A. Chernov (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) ) 51515ae9efaSKyle Evans return (false); 51658f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 51715ae9efaSKyle Evans return (false); 51858f0484fSRodney W. Grimes } 51958f0484fSRodney W. Grimes 52058f0484fSRodney W. Grimes /* 52158f0484fSRodney W. Grimes - p_str - string (no metacharacters) "parser" 5228fb3f3f6SDavid E. O'Brien == static void p_str(struct parse *p); 52358f0484fSRodney W. Grimes */ 52458f0484fSRodney W. Grimes static void 52554a648d1SXin LI p_str(struct parse *p) 52658f0484fSRodney W. Grimes { 52751295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EMPTY); 52858f0484fSRodney W. Grimes while (MORE()) 529e5996857STim J. Robbins ordinary(p, WGETNEXT()); 53058f0484fSRodney W. Grimes } 53158f0484fSRodney W. Grimes 53258f0484fSRodney W. Grimes /* 53315ae9efaSKyle Evans * Eat consecutive branch delimiters for the kind of expression that we are 53415ae9efaSKyle Evans * parsing, return the number of delimiters that we ate. 53515ae9efaSKyle Evans */ 53615ae9efaSKyle Evans static int 53715ae9efaSKyle Evans p_branch_eat_delim(struct parse *p, struct branchc *bc) 53815ae9efaSKyle Evans { 53915ae9efaSKyle Evans int nskip; 54015ae9efaSKyle Evans 5414f8f1c79SKyle Evans (void)bc; 54215ae9efaSKyle Evans nskip = 0; 54315ae9efaSKyle Evans while (EAT('|')) 54415ae9efaSKyle Evans ++nskip; 54515ae9efaSKyle Evans return (nskip); 54615ae9efaSKyle Evans } 54715ae9efaSKyle Evans 54815ae9efaSKyle Evans /* 54915ae9efaSKyle Evans * Insert necessary branch book-keeping operations. This emits a 55015ae9efaSKyle Evans * bogus 'next' offset, since we still have more to parse 55115ae9efaSKyle Evans */ 55215ae9efaSKyle Evans static void 55315ae9efaSKyle Evans p_branch_ins_offset(struct parse *p, struct branchc *bc) 55415ae9efaSKyle Evans { 55515ae9efaSKyle Evans 55615ae9efaSKyle Evans if (bc->nbranch == 0) { 55715ae9efaSKyle Evans INSERT(OCH_, bc->start); /* offset is wrong */ 55815ae9efaSKyle Evans bc->fwd = bc->start; 55915ae9efaSKyle Evans bc->back = bc->start; 56015ae9efaSKyle Evans } 56115ae9efaSKyle Evans 56215ae9efaSKyle Evans ASTERN(OOR1, bc->back); 56315ae9efaSKyle Evans bc->back = THERE(); 56415ae9efaSKyle Evans AHEAD(bc->fwd); /* fix previous offset */ 56515ae9efaSKyle Evans bc->fwd = HERE(); 56615ae9efaSKyle Evans EMIT(OOR2, 0); /* offset is very wrong */ 56715ae9efaSKyle Evans ++bc->nbranch; 56815ae9efaSKyle Evans } 56915ae9efaSKyle Evans 57015ae9efaSKyle Evans /* 57115ae9efaSKyle Evans * Fix the offset of the tail branch, if we actually had any branches. 57215ae9efaSKyle Evans * This is to correct the bogus placeholder offset that we use. 57315ae9efaSKyle Evans */ 57415ae9efaSKyle Evans static void 57515ae9efaSKyle Evans p_branch_fix_tail(struct parse *p, struct branchc *bc) 57615ae9efaSKyle Evans { 57715ae9efaSKyle Evans 57815ae9efaSKyle Evans /* Fix bogus offset at the tail if we actually have branches */ 57915ae9efaSKyle Evans if (bc->nbranch > 0) { 58015ae9efaSKyle Evans AHEAD(bc->fwd); 58115ae9efaSKyle Evans ASTERN(O_CH, bc->back); 58215ae9efaSKyle Evans } 58315ae9efaSKyle Evans } 58415ae9efaSKyle Evans 58515ae9efaSKyle Evans /* 58615ae9efaSKyle Evans * Signal to the parser that an empty branch has been encountered; this will, 58715ae9efaSKyle Evans * in the future, be used to allow for more permissive behavior with empty 5883ea376f6SKyle Evans * branches. The return value should indicate whether parsing may continue 5893ea376f6SKyle Evans * or not. 59015ae9efaSKyle Evans */ 5913ea376f6SKyle Evans static bool 59215ae9efaSKyle Evans p_branch_empty(struct parse *p, struct branchc *bc) 59315ae9efaSKyle Evans { 59415ae9efaSKyle Evans 5954f8f1c79SKyle Evans (void)bc; 59615ae9efaSKyle Evans SETERROR(REG_EMPTY); 5973ea376f6SKyle Evans return (false); 59815ae9efaSKyle Evans } 59915ae9efaSKyle Evans 60015ae9efaSKyle Evans /* 60115ae9efaSKyle Evans * Take care of any branching requirements. This includes inserting the 60215ae9efaSKyle Evans * appropriate branching instructions as well as eating all of the branch 60315ae9efaSKyle Evans * delimiters until we either run out of pattern or need to parse more pattern. 60415ae9efaSKyle Evans */ 60515ae9efaSKyle Evans static bool 60615ae9efaSKyle Evans p_branch_do(struct parse *p, struct branchc *bc) 60715ae9efaSKyle Evans { 60815ae9efaSKyle Evans int ate = 0; 60915ae9efaSKyle Evans 61015ae9efaSKyle Evans ate = p_branch_eat_delim(p, bc); 61115ae9efaSKyle Evans if (ate == 0) 61215ae9efaSKyle Evans return (false); 6133ea376f6SKyle Evans else if ((ate > 1 || (bc->outer && !MORE())) && !p_branch_empty(p, bc)) 6143ea376f6SKyle Evans /* 6153ea376f6SKyle Evans * Halt parsing only if we have an empty branch and p_branch_empty 6163ea376f6SKyle Evans * indicates that we must not continue. In the future, this will not 6173ea376f6SKyle Evans * necessarily be an error. 6183ea376f6SKyle Evans */ 6193ea376f6SKyle Evans return (false); 62015ae9efaSKyle Evans p_branch_ins_offset(p, bc); 62115ae9efaSKyle Evans 62215ae9efaSKyle Evans return (true); 62315ae9efaSKyle Evans } 62415ae9efaSKyle Evans 62515ae9efaSKyle Evans static void 62615ae9efaSKyle Evans p_bre_pre_parse(struct parse *p, struct branchc *bc) 62715ae9efaSKyle Evans { 62815ae9efaSKyle Evans 62915ae9efaSKyle Evans (void) bc; 63015ae9efaSKyle Evans /* 63115ae9efaSKyle Evans * Does not move cleanly into expression parser because of 63215ae9efaSKyle Evans * ordinary interpration of * at the beginning position of 63315ae9efaSKyle Evans * an expression. 63415ae9efaSKyle Evans */ 63515ae9efaSKyle Evans if (EAT('^')) { 63615ae9efaSKyle Evans EMIT(OBOL, 0); 63715ae9efaSKyle Evans p->g->iflags |= USEBOL; 63815ae9efaSKyle Evans p->g->nbol++; 63915ae9efaSKyle Evans } 64015ae9efaSKyle Evans } 64115ae9efaSKyle Evans 64215ae9efaSKyle Evans static void 64315ae9efaSKyle Evans p_bre_post_parse(struct parse *p, struct branchc *bc) 64415ae9efaSKyle Evans { 64515ae9efaSKyle Evans 64615ae9efaSKyle Evans /* Expression is terminating due to EOL token */ 64715ae9efaSKyle Evans if (bc->terminate) { 64815ae9efaSKyle Evans DROP(1); 64915ae9efaSKyle Evans EMIT(OEOL, 0); 65015ae9efaSKyle Evans p->g->iflags |= USEEOL; 65115ae9efaSKyle Evans p->g->neol++; 65215ae9efaSKyle Evans } 65315ae9efaSKyle Evans } 65415ae9efaSKyle Evans 65515ae9efaSKyle Evans /* 65615ae9efaSKyle Evans - p_re - Top level parser, concatenation and BRE anchoring 65715ae9efaSKyle Evans == static void p_re(struct parse *p, int end1, int end2); 65858f0484fSRodney W. Grimes * Giving end1 as OUT essentially eliminates the end1/end2 check. 65958f0484fSRodney W. Grimes * 66058f0484fSRodney W. Grimes * This implementation is a bit of a kludge, in that a trailing $ is first 66180f36366STim J. Robbins * taken as an ordinary character and then revised to be an anchor. 66258f0484fSRodney W. Grimes * The amount of lookahead needed to avoid this kludge is excessive. 66358f0484fSRodney W. Grimes */ 66458f0484fSRodney W. Grimes static void 66515ae9efaSKyle Evans p_re(struct parse *p, 6665249ac86SKevin Lo int end1, /* first terminating character */ 66715ae9efaSKyle Evans int end2) /* second terminating character; ignored for EREs */ 66858f0484fSRodney W. Grimes { 66915ae9efaSKyle Evans struct branchc bc; 67058f0484fSRodney W. Grimes 67115ae9efaSKyle Evans bc.nbranch = 0; 67215ae9efaSKyle Evans if (end1 == OUT && end2 == OUT) 67315ae9efaSKyle Evans bc.outer = true; 67415ae9efaSKyle Evans else 67515ae9efaSKyle Evans bc.outer = false; 67615ae9efaSKyle Evans #define SEEEND() (!p->bre ? SEE(end1) : SEETWO(end1, end2)) 67715ae9efaSKyle Evans for (;;) { 67815ae9efaSKyle Evans bc.start = HERE(); 67915ae9efaSKyle Evans bc.nchain = 0; 68015ae9efaSKyle Evans bc.terminate = false; 68115ae9efaSKyle Evans if (p->pre_parse != NULL) 68215ae9efaSKyle Evans p->pre_parse(p, &bc); 68379c9a695SKyle Evans while (MORE() && (!p->allowbranch || !SEESPEC('|')) && !SEEEND()) { 68415ae9efaSKyle Evans bc.terminate = p->parse_expr(p, &bc); 68515ae9efaSKyle Evans ++bc.nchain; 68658f0484fSRodney W. Grimes } 68715ae9efaSKyle Evans if (p->post_parse != NULL) 68815ae9efaSKyle Evans p->post_parse(p, &bc); 68915ae9efaSKyle Evans (void) REQUIRE(HERE() != bc.start, REG_EMPTY); 69015ae9efaSKyle Evans if (!p->allowbranch) 69115ae9efaSKyle Evans break; 69215ae9efaSKyle Evans /* 69315ae9efaSKyle Evans * p_branch_do's return value indicates whether we should 69415ae9efaSKyle Evans * continue parsing or not. This is both for correctness and 69515ae9efaSKyle Evans * a slight optimization, because it will check if we've 69615ae9efaSKyle Evans * encountered an empty branch or the end of the string 69715ae9efaSKyle Evans * immediately following a branch delimiter. 69815ae9efaSKyle Evans */ 69915ae9efaSKyle Evans if (!p_branch_do(p, &bc)) 70015ae9efaSKyle Evans break; 70158f0484fSRodney W. Grimes } 70215ae9efaSKyle Evans #undef SEE_END 70315ae9efaSKyle Evans if (p->allowbranch) 70415ae9efaSKyle Evans p_branch_fix_tail(p, &bc); 70515ae9efaSKyle Evans assert(!MORE() || SEE(end1)); 70658f0484fSRodney W. Grimes } 70758f0484fSRodney W. Grimes 70858f0484fSRodney W. Grimes /* 70958f0484fSRodney W. Grimes - p_simp_re - parse a simple RE, an atom possibly followed by a repetition 71015ae9efaSKyle Evans == static bool p_simp_re(struct parse *p, struct branchc *bc); 71158f0484fSRodney W. Grimes */ 71215ae9efaSKyle Evans static bool /* was the simple RE an unbackslashed $? */ 71315ae9efaSKyle Evans p_simp_re(struct parse *p, struct branchc *bc) 71458f0484fSRodney W. Grimes { 7158fb3f3f6SDavid E. O'Brien int c; 7168fb3f3f6SDavid E. O'Brien int count; 7178fb3f3f6SDavid E. O'Brien int count2; 7188fb3f3f6SDavid E. O'Brien sopno pos; 7198fb3f3f6SDavid E. O'Brien int i; 720e5996857STim J. Robbins wint_t wc; 7218fb3f3f6SDavid E. O'Brien sopno subno; 72258f0484fSRodney W. Grimes # define BACKSL (1<<CHAR_BIT) 72358f0484fSRodney W. Grimes 72432223c1bSPedro F. Giffuni pos = HERE(); /* repetition op, if any, covers from here */ 72558f0484fSRodney W. Grimes 72658f0484fSRodney W. Grimes assert(MORE()); /* caller should have ensured this */ 72758f0484fSRodney W. Grimes c = GETNEXT(); 72858f0484fSRodney W. Grimes if (c == '\\') { 72951295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EESCAPE); 73089b86d02SAndrey A. Chernov c = BACKSL | GETNEXT(); 73158f0484fSRodney W. Grimes } 73258f0484fSRodney W. Grimes switch (c) { 73358f0484fSRodney W. Grimes case '.': 73458f0484fSRodney W. Grimes if (p->g->cflags®_NEWLINE) 73558f0484fSRodney W. Grimes nonnewline(p); 73658f0484fSRodney W. Grimes else 73758f0484fSRodney W. Grimes EMIT(OANY, 0); 73858f0484fSRodney W. Grimes break; 73958f0484fSRodney W. Grimes case '[': 74058f0484fSRodney W. Grimes p_bracket(p); 74158f0484fSRodney W. Grimes break; 7426e283683SPedro F. Giffuni case BACKSL|'<': 7436e283683SPedro F. Giffuni EMIT(OBOW, 0); 7446e283683SPedro F. Giffuni break; 7456e283683SPedro F. Giffuni case BACKSL|'>': 7466e283683SPedro F. Giffuni EMIT(OEOW, 0); 7476e283683SPedro F. Giffuni break; 74858f0484fSRodney W. Grimes case BACKSL|'{': 74958f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 75058f0484fSRodney W. Grimes break; 75158f0484fSRodney W. Grimes case BACKSL|'(': 75258f0484fSRodney W. Grimes p->g->nsub++; 75358f0484fSRodney W. Grimes subno = p->g->nsub; 75458f0484fSRodney W. Grimes if (subno < NPAREN) 75558f0484fSRodney W. Grimes p->pbegin[subno] = HERE(); 75658f0484fSRodney W. Grimes EMIT(OLPAREN, subno); 75758f0484fSRodney W. Grimes /* the MORE here is an error heuristic */ 75858f0484fSRodney W. Grimes if (MORE() && !SEETWO('\\', ')')) 75915ae9efaSKyle Evans p_re(p, '\\', ')'); 76058f0484fSRodney W. Grimes if (subno < NPAREN) { 76158f0484fSRodney W. Grimes p->pend[subno] = HERE(); 76258f0484fSRodney W. Grimes assert(p->pend[subno] != 0); 76358f0484fSRodney W. Grimes } 76458f0484fSRodney W. Grimes EMIT(ORPAREN, subno); 76551295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN); 76658f0484fSRodney W. Grimes break; 76758f0484fSRodney W. Grimes case BACKSL|')': /* should not get here -- must be user */ 76858f0484fSRodney W. Grimes SETERROR(REG_EPAREN); 76958f0484fSRodney W. Grimes break; 77058f0484fSRodney W. Grimes case BACKSL|'1': 77158f0484fSRodney W. Grimes case BACKSL|'2': 77258f0484fSRodney W. Grimes case BACKSL|'3': 77358f0484fSRodney W. Grimes case BACKSL|'4': 77458f0484fSRodney W. Grimes case BACKSL|'5': 77558f0484fSRodney W. Grimes case BACKSL|'6': 77658f0484fSRodney W. Grimes case BACKSL|'7': 77758f0484fSRodney W. Grimes case BACKSL|'8': 77858f0484fSRodney W. Grimes case BACKSL|'9': 77958f0484fSRodney W. Grimes i = (c&~BACKSL) - '0'; 78058f0484fSRodney W. Grimes assert(i < NPAREN); 78158f0484fSRodney W. Grimes if (p->pend[i] != 0) { 78258f0484fSRodney W. Grimes assert(i <= p->g->nsub); 78358f0484fSRodney W. Grimes EMIT(OBACK_, i); 78458f0484fSRodney W. Grimes assert(p->pbegin[i] != 0); 78558f0484fSRodney W. Grimes assert(OP(p->strip[p->pbegin[i]]) == OLPAREN); 78658f0484fSRodney W. Grimes assert(OP(p->strip[p->pend[i]]) == ORPAREN); 78758f0484fSRodney W. Grimes (void) dupl(p, p->pbegin[i]+1, p->pend[i]); 78858f0484fSRodney W. Grimes EMIT(O_BACK, i); 78958f0484fSRodney W. Grimes } else 79058f0484fSRodney W. Grimes SETERROR(REG_ESUBREG); 79158f0484fSRodney W. Grimes p->g->backrefs = 1; 79258f0484fSRodney W. Grimes break; 79358f0484fSRodney W. Grimes case '*': 79415ae9efaSKyle Evans /* 79515ae9efaSKyle Evans * Ordinary if used as the first character beyond BOL anchor of 79615ae9efaSKyle Evans * a (sub-)expression, counts as a bad repetition operator if it 79715ae9efaSKyle Evans * appears otherwise. 79815ae9efaSKyle Evans */ 79915ae9efaSKyle Evans (void)REQUIRE(bc->nchain == 0, REG_BADRPT); 80058f0484fSRodney W. Grimes /* FALLTHROUGH */ 80158f0484fSRodney W. Grimes default: 8029806ef78SBrooks Davis if (p->error != 0) 80315ae9efaSKyle Evans return (false); /* Definitely not $... */ 804e5996857STim J. Robbins p->next--; 805e5996857STim J. Robbins wc = WGETNEXT(); 806e5996857STim J. Robbins ordinary(p, wc); 80758f0484fSRodney W. Grimes break; 80858f0484fSRodney W. Grimes } 80958f0484fSRodney W. Grimes 81058f0484fSRodney W. Grimes if (EAT('*')) { /* implemented as +? */ 81158f0484fSRodney W. Grimes /* this case does not require the (y|) trick, noKLUDGE */ 81258f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 81358f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 81458f0484fSRodney W. Grimes INSERT(OQUEST_, pos); 81558f0484fSRodney W. Grimes ASTERN(O_QUEST, pos); 81658f0484fSRodney W. Grimes } else if (EATTWO('\\', '{')) { 81758f0484fSRodney W. Grimes count = p_count(p); 81858f0484fSRodney W. Grimes if (EAT(',')) { 81989b86d02SAndrey A. Chernov if (MORE() && isdigit((uch)PEEK())) { 82058f0484fSRodney W. Grimes count2 = p_count(p); 82151295a4dSJordan K. Hubbard (void)REQUIRE(count <= count2, REG_BADBR); 82258f0484fSRodney W. Grimes } else /* single number with comma */ 82358f0484fSRodney W. Grimes count2 = INFINITY; 82458f0484fSRodney W. Grimes } else /* just a single number */ 82558f0484fSRodney W. Grimes count2 = count; 82658f0484fSRodney W. Grimes repeat(p, pos, count, count2); 82758f0484fSRodney W. Grimes if (!EATTWO('\\', '}')) { /* error heuristics */ 82858f0484fSRodney W. Grimes while (MORE() && !SEETWO('\\', '}')) 82958f0484fSRodney W. Grimes NEXT(); 83051295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACE); 83158f0484fSRodney W. Grimes SETERROR(REG_BADBR); 83258f0484fSRodney W. Grimes } 83389b86d02SAndrey A. Chernov } else if (c == '$') /* $ (but not \$) ends it */ 83415ae9efaSKyle Evans return (true); 83558f0484fSRodney W. Grimes 83615ae9efaSKyle Evans return (false); 83758f0484fSRodney W. Grimes } 83858f0484fSRodney W. Grimes 83958f0484fSRodney W. Grimes /* 84058f0484fSRodney W. Grimes - p_count - parse a repetition count 8418fb3f3f6SDavid E. O'Brien == static int p_count(struct parse *p); 84258f0484fSRodney W. Grimes */ 84358f0484fSRodney W. Grimes static int /* the value */ 84454a648d1SXin LI p_count(struct parse *p) 84558f0484fSRodney W. Grimes { 8468fb3f3f6SDavid E. O'Brien int count = 0; 8478fb3f3f6SDavid E. O'Brien int ndigits = 0; 84858f0484fSRodney W. Grimes 84989b86d02SAndrey A. Chernov while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) { 85058f0484fSRodney W. Grimes count = count*10 + (GETNEXT() - '0'); 85158f0484fSRodney W. Grimes ndigits++; 85258f0484fSRodney W. Grimes } 85358f0484fSRodney W. Grimes 85451295a4dSJordan K. Hubbard (void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR); 85558f0484fSRodney W. Grimes return(count); 85658f0484fSRodney W. Grimes } 85758f0484fSRodney W. Grimes 85858f0484fSRodney W. Grimes /* 85958f0484fSRodney W. Grimes - p_bracket - parse a bracketed character list 8608fb3f3f6SDavid E. O'Brien == static void p_bracket(struct parse *p); 86158f0484fSRodney W. Grimes */ 86258f0484fSRodney W. Grimes static void 86354a648d1SXin LI p_bracket(struct parse *p) 86458f0484fSRodney W. Grimes { 865e5996857STim J. Robbins cset *cs; 866e5996857STim J. Robbins wint_t ch; 86758f0484fSRodney W. Grimes 86858f0484fSRodney W. Grimes /* Dept of Truly Sickening Special-Case Kludges */ 86958f0484fSRodney W. Grimes if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) { 87058f0484fSRodney W. Grimes EMIT(OBOW, 0); 87158f0484fSRodney W. Grimes NEXTn(6); 87258f0484fSRodney W. Grimes return; 87358f0484fSRodney W. Grimes } 87458f0484fSRodney W. Grimes if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) { 87558f0484fSRodney W. Grimes EMIT(OEOW, 0); 87658f0484fSRodney W. Grimes NEXTn(6); 87758f0484fSRodney W. Grimes return; 87858f0484fSRodney W. Grimes } 87958f0484fSRodney W. Grimes 880e5996857STim J. Robbins if ((cs = allocset(p)) == NULL) 881e5996857STim J. Robbins return; 882e5996857STim J. Robbins 883e5996857STim J. Robbins if (p->g->cflags®_ICASE) 884e5996857STim J. Robbins cs->icase = 1; 88558f0484fSRodney W. Grimes if (EAT('^')) 886e5996857STim J. Robbins cs->invert = 1; 88758f0484fSRodney W. Grimes if (EAT(']')) 888e5996857STim J. Robbins CHadd(p, cs, ']'); 88958f0484fSRodney W. Grimes else if (EAT('-')) 890e5996857STim J. Robbins CHadd(p, cs, '-'); 89158f0484fSRodney W. Grimes while (MORE() && PEEK() != ']' && !SEETWO('-', ']')) 89258f0484fSRodney W. Grimes p_b_term(p, cs); 89358f0484fSRodney W. Grimes if (EAT('-')) 894e5996857STim J. Robbins CHadd(p, cs, '-'); 89551295a4dSJordan K. Hubbard (void)MUSTEAT(']', REG_EBRACK); 89658f0484fSRodney W. Grimes 89758f0484fSRodney W. Grimes if (p->error != 0) /* don't mess things up further */ 89858f0484fSRodney W. Grimes return; 89958f0484fSRodney W. Grimes 900e5996857STim J. Robbins if (cs->invert && p->g->cflags®_NEWLINE) 901e5996857STim J. Robbins cs->bmp['\n' >> 3] |= 1 << ('\n' & 7); 90258f0484fSRodney W. Grimes 903e5996857STim J. Robbins if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */ 904e5996857STim J. Robbins ordinary(p, ch); 90558f0484fSRodney W. Grimes freeset(p, cs); 90658f0484fSRodney W. Grimes } else 907e5996857STim J. Robbins EMIT(OANYOF, (int)(cs - p->g->sets)); 90858f0484fSRodney W. Grimes } 90958f0484fSRodney W. Grimes 910fe5bf674SKyle Evans static int 911fe5bf674SKyle Evans p_range_cmp(wchar_t c1, wchar_t c2) 912fe5bf674SKyle Evans { 913fe5bf674SKyle Evans #ifndef LIBREGEX 914fe5bf674SKyle Evans return __wcollate_range_cmp(c1, c2); 915fe5bf674SKyle Evans #else 916fe5bf674SKyle Evans /* Copied from libc/collate __wcollate_range_cmp */ 917fe5bf674SKyle Evans wchar_t s1[2], s2[2]; 918fe5bf674SKyle Evans 919fe5bf674SKyle Evans s1[0] = c1; 920fe5bf674SKyle Evans s1[1] = L'\0'; 921fe5bf674SKyle Evans s2[0] = c2; 922fe5bf674SKyle Evans s2[1] = L'\0'; 923fe5bf674SKyle Evans return (wcscoll(s1, s2)); 924fe5bf674SKyle Evans #endif 925fe5bf674SKyle Evans } 926fe5bf674SKyle Evans 92758f0484fSRodney W. Grimes /* 92858f0484fSRodney W. Grimes - p_b_term - parse one term of a bracketed character list 9298fb3f3f6SDavid E. O'Brien == static void p_b_term(struct parse *p, cset *cs); 93058f0484fSRodney W. Grimes */ 93158f0484fSRodney W. Grimes static void 93254a648d1SXin LI p_b_term(struct parse *p, cset *cs) 93358f0484fSRodney W. Grimes { 9348fb3f3f6SDavid E. O'Brien char c; 935e5996857STim J. Robbins wint_t start, finish; 9361daad8f5SAndrey A. Chernov wint_t i; 937fe5bf674SKyle Evans #ifndef LIBREGEX 9381daad8f5SAndrey A. Chernov struct xlocale_collate *table = 9391daad8f5SAndrey A. Chernov (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; 940fe5bf674SKyle Evans #endif 94158f0484fSRodney W. Grimes /* classify what we've got */ 94258f0484fSRodney W. Grimes switch ((MORE()) ? PEEK() : '\0') { 94358f0484fSRodney W. Grimes case '[': 94458f0484fSRodney W. Grimes c = (MORE2()) ? PEEK2() : '\0'; 94558f0484fSRodney W. Grimes break; 94658f0484fSRodney W. Grimes case '-': 94758f0484fSRodney W. Grimes SETERROR(REG_ERANGE); 94858f0484fSRodney W. Grimes return; /* NOTE RETURN */ 94958f0484fSRodney W. Grimes default: 95058f0484fSRodney W. Grimes c = '\0'; 95158f0484fSRodney W. Grimes break; 95258f0484fSRodney W. Grimes } 95358f0484fSRodney W. Grimes 95458f0484fSRodney W. Grimes switch (c) { 95558f0484fSRodney W. Grimes case ':': /* character class */ 95658f0484fSRodney W. Grimes NEXT2(); 95751295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 95858f0484fSRodney W. Grimes c = PEEK(); 95951295a4dSJordan K. Hubbard (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE); 96058f0484fSRodney W. Grimes p_b_cclass(p, cs); 96151295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 96251295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE); 96358f0484fSRodney W. Grimes break; 96458f0484fSRodney W. Grimes case '=': /* equivalence class */ 96558f0484fSRodney W. Grimes NEXT2(); 96651295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 96758f0484fSRodney W. Grimes c = PEEK(); 96851295a4dSJordan K. Hubbard (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE); 96958f0484fSRodney W. Grimes p_b_eclass(p, cs); 97051295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 97151295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE); 97258f0484fSRodney W. Grimes break; 97358f0484fSRodney W. Grimes default: /* symbol, ordinary character, or range */ 97458f0484fSRodney W. Grimes start = p_b_symbol(p); 97558f0484fSRodney W. Grimes if (SEE('-') && MORE2() && PEEK2() != ']') { 97658f0484fSRodney W. Grimes /* range */ 97758f0484fSRodney W. Grimes NEXT(); 97858f0484fSRodney W. Grimes if (EAT('-')) 97958f0484fSRodney W. Grimes finish = '-'; 98058f0484fSRodney W. Grimes else 98158f0484fSRodney W. Grimes finish = p_b_symbol(p); 98258f0484fSRodney W. Grimes } else 98358f0484fSRodney W. Grimes finish = start; 9845c551438SAndrey A. Chernov if (start == finish) 985e5996857STim J. Robbins CHadd(p, cs, start); 9865c551438SAndrey A. Chernov else { 987fe5bf674SKyle Evans #ifndef LIBREGEX 98812eae8c8SAndrey A. Chernov if (table->__collate_load_error || MB_CUR_MAX > 1) { 989fe5bf674SKyle Evans #else 990fe5bf674SKyle Evans if (MB_CUR_MAX > 1) { 991fe5bf674SKyle Evans #endif 99212eae8c8SAndrey A. Chernov (void)REQUIRE(start <= finish, REG_ERANGE); 993e5996857STim J. Robbins CHaddrange(p, cs, start, finish); 9941daad8f5SAndrey A. Chernov } else { 995fe5bf674SKyle Evans (void)REQUIRE(p_range_cmp(start, finish) <= 0, REG_ERANGE); 9961daad8f5SAndrey A. Chernov for (i = 0; i <= UCHAR_MAX; i++) { 997fe5bf674SKyle Evans if (p_range_cmp(start, i) <= 0 && 998fe5bf674SKyle Evans p_range_cmp(i, finish) <= 0 ) 9991daad8f5SAndrey A. Chernov CHadd(p, cs, i); 10001daad8f5SAndrey A. Chernov } 10011daad8f5SAndrey A. Chernov } 1002b5a6eb18SAndrey A. Chernov } 100358f0484fSRodney W. Grimes break; 100458f0484fSRodney W. Grimes } 100558f0484fSRodney W. Grimes } 100658f0484fSRodney W. Grimes 100758f0484fSRodney W. Grimes /* 100858f0484fSRodney W. Grimes - p_b_cclass - parse a character-class name and deal with it 10098fb3f3f6SDavid E. O'Brien == static void p_b_cclass(struct parse *p, cset *cs); 101058f0484fSRodney W. Grimes */ 101158f0484fSRodney W. Grimes static void 101254a648d1SXin LI p_b_cclass(struct parse *p, cset *cs) 101358f0484fSRodney W. Grimes { 10148d0f9a93SPedro F. Giffuni const char *sp = p->next; 10158fb3f3f6SDavid E. O'Brien size_t len; 1016e5996857STim J. Robbins wctype_t wct; 1017e5996857STim J. Robbins char clname[16]; 101858f0484fSRodney W. Grimes 1019b5363c4aSAndrey A. Chernov while (MORE() && isalpha((uch)PEEK())) 102058f0484fSRodney W. Grimes NEXT(); 102158f0484fSRodney W. Grimes len = p->next - sp; 1022e5996857STim J. Robbins if (len >= sizeof(clname) - 1) { 102358f0484fSRodney W. Grimes SETERROR(REG_ECTYPE); 102458f0484fSRodney W. Grimes return; 102558f0484fSRodney W. Grimes } 1026e5996857STim J. Robbins memcpy(clname, sp, len); 1027e5996857STim J. Robbins clname[len] = '\0'; 1028e5996857STim J. Robbins if ((wct = wctype(clname)) == 0) { 1029e5996857STim J. Robbins SETERROR(REG_ECTYPE); 1030e5996857STim J. Robbins return; 1031b5363c4aSAndrey A. Chernov } 1032e5996857STim J. Robbins CHaddtype(p, cs, wct); 103358f0484fSRodney W. Grimes } 103458f0484fSRodney W. Grimes 103558f0484fSRodney W. Grimes /* 103658f0484fSRodney W. Grimes - p_b_eclass - parse an equivalence-class name and deal with it 10378fb3f3f6SDavid E. O'Brien == static void p_b_eclass(struct parse *p, cset *cs); 103858f0484fSRodney W. Grimes * 103958f0484fSRodney W. Grimes * This implementation is incomplete. xxx 104058f0484fSRodney W. Grimes */ 104158f0484fSRodney W. Grimes static void 104254a648d1SXin LI p_b_eclass(struct parse *p, cset *cs) 104358f0484fSRodney W. Grimes { 1044e5996857STim J. Robbins wint_t c; 104558f0484fSRodney W. Grimes 104658f0484fSRodney W. Grimes c = p_b_coll_elem(p, '='); 1047e5996857STim J. Robbins CHadd(p, cs, c); 104858f0484fSRodney W. Grimes } 104958f0484fSRodney W. Grimes 105058f0484fSRodney W. Grimes /* 105158f0484fSRodney W. Grimes - p_b_symbol - parse a character or [..]ed multicharacter collating symbol 10522bf213ebSKevin Lo == static wint_t p_b_symbol(struct parse *p); 105358f0484fSRodney W. Grimes */ 1054e5996857STim J. Robbins static wint_t /* value of symbol */ 105554a648d1SXin LI p_b_symbol(struct parse *p) 105658f0484fSRodney W. Grimes { 1057e5996857STim J. Robbins wint_t value; 105858f0484fSRodney W. Grimes 105951295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 106058f0484fSRodney W. Grimes if (!EATTWO('[', '.')) 1061e5996857STim J. Robbins return(WGETNEXT()); 106258f0484fSRodney W. Grimes 106358f0484fSRodney W. Grimes /* collating symbol */ 106458f0484fSRodney W. Grimes value = p_b_coll_elem(p, '.'); 106551295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE); 106658f0484fSRodney W. Grimes return(value); 106758f0484fSRodney W. Grimes } 106858f0484fSRodney W. Grimes 106958f0484fSRodney W. Grimes /* 107058f0484fSRodney W. Grimes - p_b_coll_elem - parse a collating-element name and look it up 10712bf213ebSKevin Lo == static wint_t p_b_coll_elem(struct parse *p, wint_t endc); 107258f0484fSRodney W. Grimes */ 1073e5996857STim J. Robbins static wint_t /* value of collating element */ 107454a648d1SXin LI p_b_coll_elem(struct parse *p, 107554a648d1SXin LI wint_t endc) /* name ended by endc,']' */ 107658f0484fSRodney W. Grimes { 10778d0f9a93SPedro F. Giffuni const char *sp = p->next; 10788fb3f3f6SDavid E. O'Brien struct cname *cp; 1079e5996857STim J. Robbins mbstate_t mbs; 1080e5996857STim J. Robbins wchar_t wc; 10818d0f9a93SPedro F. Giffuni size_t clen, len; 108258f0484fSRodney W. Grimes 108358f0484fSRodney W. Grimes while (MORE() && !SEETWO(endc, ']')) 108458f0484fSRodney W. Grimes NEXT(); 108558f0484fSRodney W. Grimes if (!MORE()) { 108658f0484fSRodney W. Grimes SETERROR(REG_EBRACK); 108758f0484fSRodney W. Grimes return(0); 108858f0484fSRodney W. Grimes } 108958f0484fSRodney W. Grimes len = p->next - sp; 109058f0484fSRodney W. Grimes for (cp = cnames; cp->name != NULL; cp++) 10910f23ab8aSPedro F. Giffuni if (strncmp(cp->name, sp, len) == 0 && strlen(cp->name) == len) 109258f0484fSRodney W. Grimes return(cp->code); /* known name */ 1093e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1094e5996857STim J. Robbins if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len) 1095e5996857STim J. Robbins return (wc); /* single character */ 1096e5996857STim J. Robbins else if (clen == (size_t)-1 || clen == (size_t)-2) 1097e5996857STim J. Robbins SETERROR(REG_ILLSEQ); 1098e5996857STim J. Robbins else 109958f0484fSRodney W. Grimes SETERROR(REG_ECOLLATE); /* neither */ 110058f0484fSRodney W. Grimes return(0); 110158f0484fSRodney W. Grimes } 110258f0484fSRodney W. Grimes 110358f0484fSRodney W. Grimes /* 110458f0484fSRodney W. Grimes - othercase - return the case counterpart of an alphabetic 11052bf213ebSKevin Lo == static wint_t othercase(wint_t ch); 110658f0484fSRodney W. Grimes */ 1107e5996857STim J. Robbins static wint_t /* if no counterpart, return ch */ 110854a648d1SXin LI othercase(wint_t ch) 110958f0484fSRodney W. Grimes { 1110e5996857STim J. Robbins assert(iswalpha(ch)); 1111e5996857STim J. Robbins if (iswupper(ch)) 1112e5996857STim J. Robbins return(towlower(ch)); 1113e5996857STim J. Robbins else if (iswlower(ch)) 1114e5996857STim J. Robbins return(towupper(ch)); 111558f0484fSRodney W. Grimes else /* peculiar, but could happen */ 111658f0484fSRodney W. Grimes return(ch); 111758f0484fSRodney W. Grimes } 111858f0484fSRodney W. Grimes 111958f0484fSRodney W. Grimes /* 112058f0484fSRodney W. Grimes - bothcases - emit a dualcase version of a two-case character 11212bf213ebSKevin Lo == static void bothcases(struct parse *p, wint_t ch); 112258f0484fSRodney W. Grimes * 112358f0484fSRodney W. Grimes * Boy, is this implementation ever a kludge... 112458f0484fSRodney W. Grimes */ 112558f0484fSRodney W. Grimes static void 112654a648d1SXin LI bothcases(struct parse *p, wint_t ch) 112758f0484fSRodney W. Grimes { 11288d0f9a93SPedro F. Giffuni const char *oldnext = p->next; 11298d0f9a93SPedro F. Giffuni const char *oldend = p->end; 1130e5996857STim J. Robbins char bracket[3 + MB_LEN_MAX]; 1131e5996857STim J. Robbins size_t n; 1132e5996857STim J. Robbins mbstate_t mbs; 113358f0484fSRodney W. Grimes 113458f0484fSRodney W. Grimes assert(othercase(ch) != ch); /* p_bracket() would recurse */ 113558f0484fSRodney W. Grimes p->next = bracket; 1136e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1137e5996857STim J. Robbins n = wcrtomb(bracket, ch, &mbs); 1138e5996857STim J. Robbins assert(n != (size_t)-1); 1139e5996857STim J. Robbins bracket[n] = ']'; 1140e5996857STim J. Robbins bracket[n + 1] = '\0'; 1141e5996857STim J. Robbins p->end = bracket+n+1; 114258f0484fSRodney W. Grimes p_bracket(p); 1143e5996857STim J. Robbins assert(p->next == p->end); 114458f0484fSRodney W. Grimes p->next = oldnext; 114558f0484fSRodney W. Grimes p->end = oldend; 114658f0484fSRodney W. Grimes } 114758f0484fSRodney W. Grimes 114858f0484fSRodney W. Grimes /* 114958f0484fSRodney W. Grimes - ordinary - emit an ordinary character 11502bf213ebSKevin Lo == static void ordinary(struct parse *p, wint_t ch); 115158f0484fSRodney W. Grimes */ 115258f0484fSRodney W. Grimes static void 115354a648d1SXin LI ordinary(struct parse *p, wint_t ch) 115458f0484fSRodney W. Grimes { 1155e5996857STim J. Robbins cset *cs; 115658f0484fSRodney W. Grimes 1157e5996857STim J. Robbins if ((p->g->cflags®_ICASE) && iswalpha(ch) && othercase(ch) != ch) 115858f0484fSRodney W. Grimes bothcases(p, ch); 1159e5996857STim J. Robbins else if ((ch & OPDMASK) == ch) 1160e5996857STim J. Robbins EMIT(OCHAR, ch); 1161e5996857STim J. Robbins else { 1162e5996857STim J. Robbins /* 1163e5996857STim J. Robbins * Kludge: character is too big to fit into an OCHAR operand. 1164e5996857STim J. Robbins * Emit a singleton set. 1165e5996857STim J. Robbins */ 1166e5996857STim J. Robbins if ((cs = allocset(p)) == NULL) 1167e5996857STim J. Robbins return; 1168e5996857STim J. Robbins CHadd(p, cs, ch); 1169e5996857STim J. Robbins EMIT(OANYOF, (int)(cs - p->g->sets)); 1170e5996857STim J. Robbins } 117158f0484fSRodney W. Grimes } 117258f0484fSRodney W. Grimes 117358f0484fSRodney W. Grimes /* 117458f0484fSRodney W. Grimes - nonnewline - emit REG_NEWLINE version of OANY 11758fb3f3f6SDavid E. O'Brien == static void nonnewline(struct parse *p); 117658f0484fSRodney W. Grimes * 117758f0484fSRodney W. Grimes * Boy, is this implementation ever a kludge... 117858f0484fSRodney W. Grimes */ 117958f0484fSRodney W. Grimes static void 118054a648d1SXin LI nonnewline(struct parse *p) 118158f0484fSRodney W. Grimes { 11828d0f9a93SPedro F. Giffuni const char *oldnext = p->next; 11838d0f9a93SPedro F. Giffuni const char *oldend = p->end; 118458f0484fSRodney W. Grimes char bracket[4]; 118558f0484fSRodney W. Grimes 118658f0484fSRodney W. Grimes p->next = bracket; 118758f0484fSRodney W. Grimes p->end = bracket+3; 118858f0484fSRodney W. Grimes bracket[0] = '^'; 118958f0484fSRodney W. Grimes bracket[1] = '\n'; 119058f0484fSRodney W. Grimes bracket[2] = ']'; 119158f0484fSRodney W. Grimes bracket[3] = '\0'; 119258f0484fSRodney W. Grimes p_bracket(p); 119358f0484fSRodney W. Grimes assert(p->next == bracket+3); 119458f0484fSRodney W. Grimes p->next = oldnext; 119558f0484fSRodney W. Grimes p->end = oldend; 119658f0484fSRodney W. Grimes } 119758f0484fSRodney W. Grimes 119858f0484fSRodney W. Grimes /* 119958f0484fSRodney W. Grimes - repeat - generate code for a bounded repetition, recursively if needed 12008fb3f3f6SDavid E. O'Brien == static void repeat(struct parse *p, sopno start, int from, int to); 120158f0484fSRodney W. Grimes */ 120258f0484fSRodney W. Grimes static void 120354a648d1SXin LI repeat(struct parse *p, 120454a648d1SXin LI sopno start, /* operand from here to end of strip */ 120554a648d1SXin LI int from, /* repeated from this number */ 120654a648d1SXin LI int to) /* to this number of times (maybe INFINITY) */ 120758f0484fSRodney W. Grimes { 12088fb3f3f6SDavid E. O'Brien sopno finish = HERE(); 120958f0484fSRodney W. Grimes # define N 2 121058f0484fSRodney W. Grimes # define INF 3 121158f0484fSRodney W. Grimes # define REP(f, t) ((f)*8 + (t)) 121258f0484fSRodney W. Grimes # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N) 12138fb3f3f6SDavid E. O'Brien sopno copy; 121458f0484fSRodney W. Grimes 121558f0484fSRodney W. Grimes if (p->error != 0) /* head off possible runaway recursion */ 121658f0484fSRodney W. Grimes return; 121758f0484fSRodney W. Grimes 121858f0484fSRodney W. Grimes assert(from <= to); 121958f0484fSRodney W. Grimes 122058f0484fSRodney W. Grimes switch (REP(MAP(from), MAP(to))) { 122158f0484fSRodney W. Grimes case REP(0, 0): /* must be user doing this */ 122258f0484fSRodney W. Grimes DROP(finish-start); /* drop the operand */ 122358f0484fSRodney W. Grimes break; 122458f0484fSRodney W. Grimes case REP(0, 1): /* as x{1,1}? */ 122558f0484fSRodney W. Grimes case REP(0, N): /* as x{1,n}? */ 122658f0484fSRodney W. Grimes case REP(0, INF): /* as x{1,}? */ 122758f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 122858f0484fSRodney W. Grimes INSERT(OCH_, start); /* offset is wrong... */ 122958f0484fSRodney W. Grimes repeat(p, start+1, 1, to); 123058f0484fSRodney W. Grimes ASTERN(OOR1, start); 123158f0484fSRodney W. Grimes AHEAD(start); /* ... fix it */ 123258f0484fSRodney W. Grimes EMIT(OOR2, 0); 123358f0484fSRodney W. Grimes AHEAD(THERE()); 123458f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 123558f0484fSRodney W. Grimes break; 123658f0484fSRodney W. Grimes case REP(1, 1): /* trivial case */ 123758f0484fSRodney W. Grimes /* done */ 123858f0484fSRodney W. Grimes break; 123958f0484fSRodney W. Grimes case REP(1, N): /* as x?x{1,n-1} */ 124058f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 124158f0484fSRodney W. Grimes INSERT(OCH_, start); 124258f0484fSRodney W. Grimes ASTERN(OOR1, start); 124358f0484fSRodney W. Grimes AHEAD(start); 124458f0484fSRodney W. Grimes EMIT(OOR2, 0); /* offset very wrong... */ 124558f0484fSRodney W. Grimes AHEAD(THERE()); /* ...so fix it */ 124658f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 124758f0484fSRodney W. Grimes copy = dupl(p, start+1, finish+1); 124858f0484fSRodney W. Grimes assert(copy == finish+4); 124958f0484fSRodney W. Grimes repeat(p, copy, 1, to-1); 125058f0484fSRodney W. Grimes break; 125158f0484fSRodney W. Grimes case REP(1, INF): /* as x+ */ 125258f0484fSRodney W. Grimes INSERT(OPLUS_, start); 125358f0484fSRodney W. Grimes ASTERN(O_PLUS, start); 125458f0484fSRodney W. Grimes break; 125558f0484fSRodney W. Grimes case REP(N, N): /* as xx{m-1,n-1} */ 125658f0484fSRodney W. Grimes copy = dupl(p, start, finish); 125758f0484fSRodney W. Grimes repeat(p, copy, from-1, to-1); 125858f0484fSRodney W. Grimes break; 125958f0484fSRodney W. Grimes case REP(N, INF): /* as xx{n-1,INF} */ 126058f0484fSRodney W. Grimes copy = dupl(p, start, finish); 126158f0484fSRodney W. Grimes repeat(p, copy, from-1, to); 126258f0484fSRodney W. Grimes break; 126358f0484fSRodney W. Grimes default: /* "can't happen" */ 126458f0484fSRodney W. Grimes SETERROR(REG_ASSERT); /* just in case */ 126558f0484fSRodney W. Grimes break; 126658f0484fSRodney W. Grimes } 126758f0484fSRodney W. Grimes } 126858f0484fSRodney W. Grimes 126958f0484fSRodney W. Grimes /* 1270e5996857STim J. Robbins - wgetnext - helper function for WGETNEXT() macro. Gets the next wide 1271e5996857STim J. Robbins - character from the parse struct, signals a REG_ILLSEQ error if the 1272e5996857STim J. Robbins - character can't be converted. Returns the number of bytes consumed. 1273e5996857STim J. Robbins */ 1274e5996857STim J. Robbins static wint_t 127554a648d1SXin LI wgetnext(struct parse *p) 1276e5996857STim J. Robbins { 1277e5996857STim J. Robbins mbstate_t mbs; 1278e5996857STim J. Robbins wchar_t wc; 1279e5996857STim J. Robbins size_t n; 1280e5996857STim J. Robbins 1281e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1282e5996857STim J. Robbins n = mbrtowc(&wc, p->next, p->end - p->next, &mbs); 1283e5996857STim J. Robbins if (n == (size_t)-1 || n == (size_t)-2) { 1284e5996857STim J. Robbins SETERROR(REG_ILLSEQ); 1285e5996857STim J. Robbins return (0); 1286e5996857STim J. Robbins } 1287e5996857STim J. Robbins if (n == 0) 1288e5996857STim J. Robbins n = 1; 1289e5996857STim J. Robbins p->next += n; 1290e5996857STim J. Robbins return (wc); 1291e5996857STim J. Robbins } 1292e5996857STim J. Robbins 1293e5996857STim J. Robbins /* 129458f0484fSRodney W. Grimes - seterr - set an error condition 12958fb3f3f6SDavid E. O'Brien == static int seterr(struct parse *p, int e); 129658f0484fSRodney W. Grimes */ 129758f0484fSRodney W. Grimes static int /* useless but makes type checking happy */ 129854a648d1SXin LI seterr(struct parse *p, int e) 129958f0484fSRodney W. Grimes { 130058f0484fSRodney W. Grimes if (p->error == 0) /* keep earliest error condition */ 130158f0484fSRodney W. Grimes p->error = e; 130258f0484fSRodney W. Grimes p->next = nuls; /* try to bring things to a halt */ 130358f0484fSRodney W. Grimes p->end = nuls; 130458f0484fSRodney W. Grimes return(0); /* make the return value well-defined */ 130558f0484fSRodney W. Grimes } 130658f0484fSRodney W. Grimes 130758f0484fSRodney W. Grimes /* 130858f0484fSRodney W. Grimes - allocset - allocate a set of characters for [] 13098fb3f3f6SDavid E. O'Brien == static cset *allocset(struct parse *p); 131058f0484fSRodney W. Grimes */ 131158f0484fSRodney W. Grimes static cset * 131254a648d1SXin LI allocset(struct parse *p) 131358f0484fSRodney W. Grimes { 1314e5996857STim J. Robbins cset *cs, *ncs; 131558f0484fSRodney W. Grimes 13169f36610fSPedro F. Giffuni ncs = reallocarray(p->g->sets, p->g->ncsets + 1, sizeof(*ncs)); 1317e5996857STim J. Robbins if (ncs == NULL) { 131858f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 1319e5996857STim J. Robbins return (NULL); 132058f0484fSRodney W. Grimes } 1321e5996857STim J. Robbins p->g->sets = ncs; 1322e5996857STim J. Robbins cs = &p->g->sets[p->g->ncsets++]; 1323e5996857STim J. Robbins memset(cs, 0, sizeof(*cs)); 132458f0484fSRodney W. Grimes 132558f0484fSRodney W. Grimes return(cs); 132658f0484fSRodney W. Grimes } 132758f0484fSRodney W. Grimes 132858f0484fSRodney W. Grimes /* 132958f0484fSRodney W. Grimes - freeset - free a now-unused set 13308fb3f3f6SDavid E. O'Brien == static void freeset(struct parse *p, cset *cs); 133158f0484fSRodney W. Grimes */ 133258f0484fSRodney W. Grimes static void 133354a648d1SXin LI freeset(struct parse *p, cset *cs) 133458f0484fSRodney W. Grimes { 13358fb3f3f6SDavid E. O'Brien cset *top = &p->g->sets[p->g->ncsets]; 133658f0484fSRodney W. Grimes 1337e5996857STim J. Robbins free(cs->wides); 1338e5996857STim J. Robbins free(cs->ranges); 1339e5996857STim J. Robbins free(cs->types); 1340e5996857STim J. Robbins memset(cs, 0, sizeof(*cs)); 134158f0484fSRodney W. Grimes if (cs == top-1) /* recover only the easy case */ 134258f0484fSRodney W. Grimes p->g->ncsets--; 134358f0484fSRodney W. Grimes } 134458f0484fSRodney W. Grimes 134558f0484fSRodney W. Grimes /* 1346e5996857STim J. Robbins - singleton - Determine whether a set contains only one character, 1347e5996857STim J. Robbins - returning it if so, otherwise returning OUT. 134858f0484fSRodney W. Grimes */ 1349e5996857STim J. Robbins static wint_t 135054a648d1SXin LI singleton(cset *cs) 135158f0484fSRodney W. Grimes { 1352e5996857STim J. Robbins wint_t i, s, n; 135358f0484fSRodney W. Grimes 1354e5996857STim J. Robbins for (i = n = 0; i < NC; i++) 1355e5996857STim J. Robbins if (CHIN(cs, i)) { 135658f0484fSRodney W. Grimes n++; 1357e5996857STim J. Robbins s = i; 1358e5996857STim J. Robbins } 1359e5996857STim J. Robbins if (n == 1) 1360e5996857STim J. Robbins return (s); 1361e5996857STim J. Robbins if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 && 1362e5996857STim J. Robbins cs->icase == 0) 1363e5996857STim J. Robbins return (cs->wides[0]); 1364e5996857STim J. Robbins /* Don't bother handling the other cases. */ 1365e5996857STim J. Robbins return (OUT); 1366e5996857STim J. Robbins } 1367e5996857STim J. Robbins 1368e5996857STim J. Robbins /* 1369e5996857STim J. Robbins - CHadd - add character to character set. 1370e5996857STim J. Robbins */ 1371e5996857STim J. Robbins static void 137254a648d1SXin LI CHadd(struct parse *p, cset *cs, wint_t ch) 1373e5996857STim J. Robbins { 137433176f17STim J. Robbins wint_t nch, *newwides; 1375e5996857STim J. Robbins assert(ch >= 0); 137633176f17STim J. Robbins if (ch < NC) 1377e5996857STim J. Robbins cs->bmp[ch >> 3] |= 1 << (ch & 7); 137833176f17STim J. Robbins else { 13799f36610fSPedro F. Giffuni newwides = reallocarray(cs->wides, cs->nwides + 1, 1380e5996857STim J. Robbins sizeof(*cs->wides)); 1381e5996857STim J. Robbins if (newwides == NULL) { 1382e5996857STim J. Robbins SETERROR(REG_ESPACE); 1383e5996857STim J. Robbins return; 1384e5996857STim J. Robbins } 1385e5996857STim J. Robbins cs->wides = newwides; 1386e5996857STim J. Robbins cs->wides[cs->nwides++] = ch; 1387e5996857STim J. Robbins } 138833176f17STim J. Robbins if (cs->icase) { 138933176f17STim J. Robbins if ((nch = towlower(ch)) < NC) 139033176f17STim J. Robbins cs->bmp[nch >> 3] |= 1 << (nch & 7); 139133176f17STim J. Robbins if ((nch = towupper(ch)) < NC) 139233176f17STim J. Robbins cs->bmp[nch >> 3] |= 1 << (nch & 7); 139333176f17STim J. Robbins } 1394e5996857STim J. Robbins } 1395e5996857STim J. Robbins 1396e5996857STim J. Robbins /* 1397e5996857STim J. Robbins - CHaddrange - add all characters in the range [min,max] to a character set. 1398e5996857STim J. Robbins */ 1399e5996857STim J. Robbins static void 140054a648d1SXin LI CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max) 1401e5996857STim J. Robbins { 1402e5996857STim J. Robbins crange *newranges; 1403e5996857STim J. Robbins 1404e5996857STim J. Robbins for (; min < NC && min <= max; min++) 1405e5996857STim J. Robbins CHadd(p, cs, min); 1406e5996857STim J. Robbins if (min >= max) 1407e5996857STim J. Robbins return; 14089f36610fSPedro F. Giffuni newranges = reallocarray(cs->ranges, cs->nranges + 1, 1409e5996857STim J. Robbins sizeof(*cs->ranges)); 1410e5996857STim J. Robbins if (newranges == NULL) { 1411e5996857STim J. Robbins SETERROR(REG_ESPACE); 1412e5996857STim J. Robbins return; 1413e5996857STim J. Robbins } 1414e5996857STim J. Robbins cs->ranges = newranges; 1415e5996857STim J. Robbins cs->ranges[cs->nranges].min = min; 1416d0ebccdeSXin LI cs->ranges[cs->nranges].max = max; 1417e5996857STim J. Robbins cs->nranges++; 1418e5996857STim J. Robbins } 1419e5996857STim J. Robbins 1420e5996857STim J. Robbins /* 1421e5996857STim J. Robbins - CHaddtype - add all characters of a certain type to a character set. 1422e5996857STim J. Robbins */ 1423e5996857STim J. Robbins static void 142454a648d1SXin LI CHaddtype(struct parse *p, cset *cs, wctype_t wct) 1425e5996857STim J. Robbins { 1426e5996857STim J. Robbins wint_t i; 1427e5996857STim J. Robbins wctype_t *newtypes; 1428e5996857STim J. Robbins 142933176f17STim J. Robbins for (i = 0; i < NC; i++) 1430e5996857STim J. Robbins if (iswctype(i, wct)) 1431e5996857STim J. Robbins CHadd(p, cs, i); 14329f36610fSPedro F. Giffuni newtypes = reallocarray(cs->types, cs->ntypes + 1, 1433e5996857STim J. Robbins sizeof(*cs->types)); 1434e5996857STim J. Robbins if (newtypes == NULL) { 1435e5996857STim J. Robbins SETERROR(REG_ESPACE); 1436e5996857STim J. Robbins return; 1437e5996857STim J. Robbins } 1438e5996857STim J. Robbins cs->types = newtypes; 1439e5996857STim J. Robbins cs->types[cs->ntypes++] = wct; 144058f0484fSRodney W. Grimes } 144158f0484fSRodney W. Grimes 144258f0484fSRodney W. Grimes /* 144358f0484fSRodney W. Grimes - dupl - emit a duplicate of a bunch of sops 14448fb3f3f6SDavid E. O'Brien == static sopno dupl(struct parse *p, sopno start, sopno finish); 144558f0484fSRodney W. Grimes */ 144658f0484fSRodney W. Grimes static sopno /* start of duplicate */ 144754a648d1SXin LI dupl(struct parse *p, 144854a648d1SXin LI sopno start, /* from here */ 144954a648d1SXin LI sopno finish) /* to this less one */ 145058f0484fSRodney W. Grimes { 14518fb3f3f6SDavid E. O'Brien sopno ret = HERE(); 14528fb3f3f6SDavid E. O'Brien sopno len = finish - start; 145358f0484fSRodney W. Grimes 145458f0484fSRodney W. Grimes assert(finish >= start); 145558f0484fSRodney W. Grimes if (len == 0) 145658f0484fSRodney W. Grimes return(ret); 14572bf213ebSKevin Lo if (!enlarge(p, p->ssize + len)) /* this many unexpected additions */ 14582bf213ebSKevin Lo return(ret); 145958f0484fSRodney W. Grimes (void) memcpy((char *)(p->strip + p->slen), 146058f0484fSRodney W. Grimes (char *)(p->strip + start), (size_t)len*sizeof(sop)); 146158f0484fSRodney W. Grimes p->slen += len; 146258f0484fSRodney W. Grimes return(ret); 146358f0484fSRodney W. Grimes } 146458f0484fSRodney W. Grimes 146558f0484fSRodney W. Grimes /* 146658f0484fSRodney W. Grimes - doemit - emit a strip operator 14678fb3f3f6SDavid E. O'Brien == static void doemit(struct parse *p, sop op, size_t opnd); 146858f0484fSRodney W. Grimes * 146958f0484fSRodney W. Grimes * It might seem better to implement this as a macro with a function as 147058f0484fSRodney W. Grimes * hard-case backup, but it's just too big and messy unless there are 147158f0484fSRodney W. Grimes * some changes to the data structures. Maybe later. 147258f0484fSRodney W. Grimes */ 147358f0484fSRodney W. Grimes static void 147454a648d1SXin LI doemit(struct parse *p, sop op, size_t opnd) 147558f0484fSRodney W. Grimes { 147658f0484fSRodney W. Grimes /* avoid making error situations worse */ 147758f0484fSRodney W. Grimes if (p->error != 0) 147858f0484fSRodney W. Grimes return; 147958f0484fSRodney W. Grimes 148058f0484fSRodney W. Grimes /* deal with oversize operands ("can't happen", more or less) */ 148158f0484fSRodney W. Grimes assert(opnd < 1<<OPSHIFT); 148258f0484fSRodney W. Grimes 148358f0484fSRodney W. Grimes /* deal with undersized strip */ 148458f0484fSRodney W. Grimes if (p->slen >= p->ssize) 14852bf213ebSKevin Lo if (!enlarge(p, (p->ssize+1) / 2 * 3)) /* +50% */ 14862bf213ebSKevin Lo return; 148758f0484fSRodney W. Grimes 148858f0484fSRodney W. Grimes /* finally, it's all reduced to the easy case */ 148958f0484fSRodney W. Grimes p->strip[p->slen++] = SOP(op, opnd); 149058f0484fSRodney W. Grimes } 149158f0484fSRodney W. Grimes 149258f0484fSRodney W. Grimes /* 149358f0484fSRodney W. Grimes - doinsert - insert a sop into the strip 14948fb3f3f6SDavid E. O'Brien == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 149558f0484fSRodney W. Grimes */ 149658f0484fSRodney W. Grimes static void 149754a648d1SXin LI doinsert(struct parse *p, sop op, size_t opnd, sopno pos) 149858f0484fSRodney W. Grimes { 14998fb3f3f6SDavid E. O'Brien sopno sn; 15008fb3f3f6SDavid E. O'Brien sop s; 15018fb3f3f6SDavid E. O'Brien int i; 150258f0484fSRodney W. Grimes 150358f0484fSRodney W. Grimes /* avoid making error situations worse */ 150458f0484fSRodney W. Grimes if (p->error != 0) 150558f0484fSRodney W. Grimes return; 150658f0484fSRodney W. Grimes 150758f0484fSRodney W. Grimes sn = HERE(); 150858f0484fSRodney W. Grimes EMIT(op, opnd); /* do checks, ensure space */ 150958f0484fSRodney W. Grimes assert(HERE() == sn+1); 151058f0484fSRodney W. Grimes s = p->strip[sn]; 151158f0484fSRodney W. Grimes 151258f0484fSRodney W. Grimes /* adjust paren pointers */ 151358f0484fSRodney W. Grimes assert(pos > 0); 151458f0484fSRodney W. Grimes for (i = 1; i < NPAREN; i++) { 151558f0484fSRodney W. Grimes if (p->pbegin[i] >= pos) { 151658f0484fSRodney W. Grimes p->pbegin[i]++; 151758f0484fSRodney W. Grimes } 151858f0484fSRodney W. Grimes if (p->pend[i] >= pos) { 151958f0484fSRodney W. Grimes p->pend[i]++; 152058f0484fSRodney W. Grimes } 152158f0484fSRodney W. Grimes } 152258f0484fSRodney W. Grimes 152358f0484fSRodney W. Grimes memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos], 152458f0484fSRodney W. Grimes (HERE()-pos-1)*sizeof(sop)); 152558f0484fSRodney W. Grimes p->strip[pos] = s; 152658f0484fSRodney W. Grimes } 152758f0484fSRodney W. Grimes 152858f0484fSRodney W. Grimes /* 152958f0484fSRodney W. Grimes - dofwd - complete a forward reference 15308fb3f3f6SDavid E. O'Brien == static void dofwd(struct parse *p, sopno pos, sop value); 153158f0484fSRodney W. Grimes */ 153258f0484fSRodney W. Grimes static void 153354a648d1SXin LI dofwd(struct parse *p, sopno pos, sop value) 153458f0484fSRodney W. Grimes { 153558f0484fSRodney W. Grimes /* avoid making error situations worse */ 153658f0484fSRodney W. Grimes if (p->error != 0) 153758f0484fSRodney W. Grimes return; 153858f0484fSRodney W. Grimes 153958f0484fSRodney W. Grimes assert(value < 1<<OPSHIFT); 154058f0484fSRodney W. Grimes p->strip[pos] = OP(p->strip[pos]) | value; 154158f0484fSRodney W. Grimes } 154258f0484fSRodney W. Grimes 154358f0484fSRodney W. Grimes /* 154458f0484fSRodney W. Grimes - enlarge - enlarge the strip 15452bf213ebSKevin Lo == static int enlarge(struct parse *p, sopno size); 154658f0484fSRodney W. Grimes */ 15472bf213ebSKevin Lo static int 154854a648d1SXin LI enlarge(struct parse *p, sopno size) 154958f0484fSRodney W. Grimes { 15508fb3f3f6SDavid E. O'Brien sop *sp; 155158f0484fSRodney W. Grimes 155258f0484fSRodney W. Grimes if (p->ssize >= size) 15532bf213ebSKevin Lo return 1; 155458f0484fSRodney W. Grimes 15559f36610fSPedro F. Giffuni sp = reallocarray(p->strip, size, sizeof(sop)); 155658f0484fSRodney W. Grimes if (sp == NULL) { 155758f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 15582bf213ebSKevin Lo return 0; 155958f0484fSRodney W. Grimes } 156058f0484fSRodney W. Grimes p->strip = sp; 156158f0484fSRodney W. Grimes p->ssize = size; 15622bf213ebSKevin Lo return 1; 156358f0484fSRodney W. Grimes } 156458f0484fSRodney W. Grimes 156558f0484fSRodney W. Grimes /* 156658f0484fSRodney W. Grimes - stripsnug - compact the strip 15678fb3f3f6SDavid E. O'Brien == static void stripsnug(struct parse *p, struct re_guts *g); 156858f0484fSRodney W. Grimes */ 156958f0484fSRodney W. Grimes static void 157054a648d1SXin LI stripsnug(struct parse *p, struct re_guts *g) 157158f0484fSRodney W. Grimes { 157258f0484fSRodney W. Grimes g->nstates = p->slen; 15739f36610fSPedro F. Giffuni g->strip = reallocarray((char *)p->strip, p->slen, sizeof(sop)); 157458f0484fSRodney W. Grimes if (g->strip == NULL) { 157558f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 157658f0484fSRodney W. Grimes g->strip = p->strip; 157758f0484fSRodney W. Grimes } 157858f0484fSRodney W. Grimes } 157958f0484fSRodney W. Grimes 158058f0484fSRodney W. Grimes /* 158158f0484fSRodney W. Grimes - findmust - fill in must and mlen with longest mandatory literal string 15828fb3f3f6SDavid E. O'Brien == static void findmust(struct parse *p, struct re_guts *g); 158358f0484fSRodney W. Grimes * 158458f0484fSRodney W. Grimes * This algorithm could do fancy things like analyzing the operands of | 158558f0484fSRodney W. Grimes * for common subsequences. Someday. This code is simple and finds most 158658f0484fSRodney W. Grimes * of the interesting cases. 158758f0484fSRodney W. Grimes * 158858f0484fSRodney W. Grimes * Note that must and mlen got initialized during setup. 158958f0484fSRodney W. Grimes */ 159058f0484fSRodney W. Grimes static void 159154a648d1SXin LI findmust(struct parse *p, struct re_guts *g) 159258f0484fSRodney W. Grimes { 15938fb3f3f6SDavid E. O'Brien sop *scan; 1594d58abbfeSPedro F. Giffuni sop *start = NULL; 1595d58abbfeSPedro F. Giffuni sop *newstart = NULL; 15968fb3f3f6SDavid E. O'Brien sopno newlen; 15978fb3f3f6SDavid E. O'Brien sop s; 15988fb3f3f6SDavid E. O'Brien char *cp; 1599e6a886d8SDaniel C. Sobral int offset; 1600e5996857STim J. Robbins char buf[MB_LEN_MAX]; 1601e5996857STim J. Robbins size_t clen; 1602e5996857STim J. Robbins mbstate_t mbs; 160358f0484fSRodney W. Grimes 160458f0484fSRodney W. Grimes /* avoid making error situations worse */ 160558f0484fSRodney W. Grimes if (p->error != 0) 160658f0484fSRodney W. Grimes return; 160758f0484fSRodney W. Grimes 1608e5996857STim J. Robbins /* 1609e5996857STim J. Robbins * It's not generally safe to do a ``char'' substring search on 1610e5996857STim J. Robbins * multibyte character strings, but it's safe for at least 1611e5996857STim J. Robbins * UTF-8 (see RFC 3629). 1612e5996857STim J. Robbins */ 1613e5996857STim J. Robbins if (MB_CUR_MAX > 1 && 1614e5996857STim J. Robbins strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0) 1615e5996857STim J. Robbins return; 1616e5996857STim J. Robbins 161758f0484fSRodney W. Grimes /* find the longest OCHAR sequence in strip */ 161858f0484fSRodney W. Grimes newlen = 0; 1619e6a886d8SDaniel C. Sobral offset = 0; 1620e6a886d8SDaniel C. Sobral g->moffset = 0; 162158f0484fSRodney W. Grimes scan = g->strip + 1; 162258f0484fSRodney W. Grimes do { 162358f0484fSRodney W. Grimes s = *scan++; 162458f0484fSRodney W. Grimes switch (OP(s)) { 162558f0484fSRodney W. Grimes case OCHAR: /* sequence member */ 1626e5996857STim J. Robbins if (newlen == 0) { /* new sequence */ 1627e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 162858f0484fSRodney W. Grimes newstart = scan - 1; 1629e5996857STim J. Robbins } 1630e5996857STim J. Robbins clen = wcrtomb(buf, OPND(s), &mbs); 1631e5996857STim J. Robbins if (clen == (size_t)-1) 1632e5996857STim J. Robbins goto toohard; 1633e5996857STim J. Robbins newlen += clen; 163458f0484fSRodney W. Grimes break; 163558f0484fSRodney W. Grimes case OPLUS_: /* things that don't break one */ 163658f0484fSRodney W. Grimes case OLPAREN: 163758f0484fSRodney W. Grimes case ORPAREN: 163858f0484fSRodney W. Grimes break; 163958f0484fSRodney W. Grimes case OQUEST_: /* things that must be skipped */ 164058f0484fSRodney W. Grimes case OCH_: 164180f36366STim J. Robbins offset = altoffset(scan, offset); 164258f0484fSRodney W. Grimes scan--; 164358f0484fSRodney W. Grimes do { 164458f0484fSRodney W. Grimes scan += OPND(s); 164558f0484fSRodney W. Grimes s = *scan; 164658f0484fSRodney W. Grimes /* assert() interferes w debug printouts */ 16474f8f1c79SKyle Evans if (OP(s) != (sop)O_QUEST && 16484f8f1c79SKyle Evans OP(s) != (sop)O_CH && OP(s) != (sop)OOR2) { 164958f0484fSRodney W. Grimes g->iflags |= BAD; 165058f0484fSRodney W. Grimes return; 165158f0484fSRodney W. Grimes } 16524f8f1c79SKyle Evans } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH); 16537fed38d0SPhilippe Charnier /* FALLTHROUGH */ 1654e6a886d8SDaniel C. Sobral case OBOW: /* things that break a sequence */ 1655e6a886d8SDaniel C. Sobral case OEOW: 1656e6a886d8SDaniel C. Sobral case OBOL: 1657e6a886d8SDaniel C. Sobral case OEOL: 1658e6a886d8SDaniel C. Sobral case O_QUEST: 1659e6a886d8SDaniel C. Sobral case O_CH: 1660e6a886d8SDaniel C. Sobral case OEND: 16614f8f1c79SKyle Evans if (newlen > (sopno)g->mlen) { /* ends one */ 166258f0484fSRodney W. Grimes start = newstart; 166358f0484fSRodney W. Grimes g->mlen = newlen; 1664e6a886d8SDaniel C. Sobral if (offset > -1) { 1665e6a886d8SDaniel C. Sobral g->moffset += offset; 1666e6a886d8SDaniel C. Sobral offset = newlen; 1667e6a886d8SDaniel C. Sobral } else 1668e6a886d8SDaniel C. Sobral g->moffset = offset; 1669e6a886d8SDaniel C. Sobral } else { 1670e6a886d8SDaniel C. Sobral if (offset > -1) 1671e6a886d8SDaniel C. Sobral offset += newlen; 167258f0484fSRodney W. Grimes } 167358f0484fSRodney W. Grimes newlen = 0; 167458f0484fSRodney W. Grimes break; 1675e6a886d8SDaniel C. Sobral case OANY: 16764f8f1c79SKyle Evans if (newlen > (sopno)g->mlen) { /* ends one */ 1677e6a886d8SDaniel C. Sobral start = newstart; 1678e6a886d8SDaniel C. Sobral g->mlen = newlen; 1679e6a886d8SDaniel C. Sobral if (offset > -1) { 1680e6a886d8SDaniel C. Sobral g->moffset += offset; 1681e6a886d8SDaniel C. Sobral offset = newlen; 1682e6a886d8SDaniel C. Sobral } else 1683e6a886d8SDaniel C. Sobral g->moffset = offset; 1684e6a886d8SDaniel C. Sobral } else { 1685e6a886d8SDaniel C. Sobral if (offset > -1) 1686e6a886d8SDaniel C. Sobral offset += newlen; 1687e6a886d8SDaniel C. Sobral } 1688e6a886d8SDaniel C. Sobral if (offset > -1) 1689e6a886d8SDaniel C. Sobral offset++; 1690e6a886d8SDaniel C. Sobral newlen = 0; 1691e6a886d8SDaniel C. Sobral break; 1692e6a886d8SDaniel C. Sobral case OANYOF: /* may or may not invalidate offset */ 1693e6a886d8SDaniel C. Sobral /* First, everything as OANY */ 16944f8f1c79SKyle Evans if (newlen > (sopno)g->mlen) { /* ends one */ 1695e6a886d8SDaniel C. Sobral start = newstart; 1696e6a886d8SDaniel C. Sobral g->mlen = newlen; 1697e6a886d8SDaniel C. Sobral if (offset > -1) { 1698e6a886d8SDaniel C. Sobral g->moffset += offset; 1699e6a886d8SDaniel C. Sobral offset = newlen; 1700e6a886d8SDaniel C. Sobral } else 1701e6a886d8SDaniel C. Sobral g->moffset = offset; 1702e6a886d8SDaniel C. Sobral } else { 1703e6a886d8SDaniel C. Sobral if (offset > -1) 1704e6a886d8SDaniel C. Sobral offset += newlen; 1705e6a886d8SDaniel C. Sobral } 1706e6a886d8SDaniel C. Sobral if (offset > -1) 1707e6a886d8SDaniel C. Sobral offset++; 1708e6a886d8SDaniel C. Sobral newlen = 0; 1709e6a886d8SDaniel C. Sobral break; 1710e5996857STim J. Robbins toohard: 1711e6a886d8SDaniel C. Sobral default: 1712e6a886d8SDaniel C. Sobral /* Anything here makes it impossible or too hard 1713e6a886d8SDaniel C. Sobral * to calculate the offset -- so we give up; 1714e6a886d8SDaniel C. Sobral * save the last known good offset, in case the 1715e6a886d8SDaniel C. Sobral * must sequence doesn't occur later. 1716e6a886d8SDaniel C. Sobral */ 17174f8f1c79SKyle Evans if (newlen > (sopno)g->mlen) { /* ends one */ 1718e6a886d8SDaniel C. Sobral start = newstart; 1719e6a886d8SDaniel C. Sobral g->mlen = newlen; 1720e6a886d8SDaniel C. Sobral if (offset > -1) 1721e6a886d8SDaniel C. Sobral g->moffset += offset; 1722e6a886d8SDaniel C. Sobral else 1723e6a886d8SDaniel C. Sobral g->moffset = offset; 1724e6a886d8SDaniel C. Sobral } 1725e6a886d8SDaniel C. Sobral offset = -1; 1726e6a886d8SDaniel C. Sobral newlen = 0; 1727e6a886d8SDaniel C. Sobral break; 172858f0484fSRodney W. Grimes } 172958f0484fSRodney W. Grimes } while (OP(s) != OEND); 173058f0484fSRodney W. Grimes 1731e6a886d8SDaniel C. Sobral if (g->mlen == 0) { /* there isn't one */ 1732e6a886d8SDaniel C. Sobral g->moffset = -1; 173358f0484fSRodney W. Grimes return; 1734e6a886d8SDaniel C. Sobral } 173558f0484fSRodney W. Grimes 173658f0484fSRodney W. Grimes /* turn it into a character string */ 173758f0484fSRodney W. Grimes g->must = malloc((size_t)g->mlen + 1); 173858f0484fSRodney W. Grimes if (g->must == NULL) { /* argh; just forget it */ 173958f0484fSRodney W. Grimes g->mlen = 0; 1740e6a886d8SDaniel C. Sobral g->moffset = -1; 174158f0484fSRodney W. Grimes return; 174258f0484fSRodney W. Grimes } 174358f0484fSRodney W. Grimes cp = g->must; 174458f0484fSRodney W. Grimes scan = start; 1745e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1746e5996857STim J. Robbins while (cp < g->must + g->mlen) { 174758f0484fSRodney W. Grimes while (OP(s = *scan++) != OCHAR) 174858f0484fSRodney W. Grimes continue; 1749e5996857STim J. Robbins clen = wcrtomb(cp, OPND(s), &mbs); 1750e5996857STim J. Robbins assert(clen != (size_t)-1); 1751e5996857STim J. Robbins cp += clen; 175258f0484fSRodney W. Grimes } 175358f0484fSRodney W. Grimes assert(cp == g->must + g->mlen); 175458f0484fSRodney W. Grimes *cp++ = '\0'; /* just on general principles */ 175558f0484fSRodney W. Grimes } 175658f0484fSRodney W. Grimes 175758f0484fSRodney W. Grimes /* 1758e6a886d8SDaniel C. Sobral - altoffset - choose biggest offset among multiple choices 175980f36366STim J. Robbins == static int altoffset(sop *scan, int offset); 1760e6a886d8SDaniel C. Sobral * 1761e6a886d8SDaniel C. Sobral * Compute, recursively if necessary, the largest offset among multiple 1762e6a886d8SDaniel C. Sobral * re paths. 1763e6a886d8SDaniel C. Sobral */ 1764e6a886d8SDaniel C. Sobral static int 176554a648d1SXin LI altoffset(sop *scan, int offset) 1766e6a886d8SDaniel C. Sobral { 1767e6a886d8SDaniel C. Sobral int largest; 1768e6a886d8SDaniel C. Sobral int try; 1769e6a886d8SDaniel C. Sobral sop s; 1770e6a886d8SDaniel C. Sobral 1771e6a886d8SDaniel C. Sobral /* If we gave up already on offsets, return */ 1772e6a886d8SDaniel C. Sobral if (offset == -1) 1773e6a886d8SDaniel C. Sobral return -1; 1774e6a886d8SDaniel C. Sobral 1775e6a886d8SDaniel C. Sobral largest = 0; 1776e6a886d8SDaniel C. Sobral try = 0; 1777e6a886d8SDaniel C. Sobral s = *scan++; 17784f8f1c79SKyle Evans while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH) { 1779e6a886d8SDaniel C. Sobral switch (OP(s)) { 1780e6a886d8SDaniel C. Sobral case OOR1: 1781e6a886d8SDaniel C. Sobral if (try > largest) 1782e6a886d8SDaniel C. Sobral largest = try; 1783e6a886d8SDaniel C. Sobral try = 0; 1784e6a886d8SDaniel C. Sobral break; 1785e6a886d8SDaniel C. Sobral case OQUEST_: 1786e6a886d8SDaniel C. Sobral case OCH_: 178780f36366STim J. Robbins try = altoffset(scan, try); 1788e6a886d8SDaniel C. Sobral if (try == -1) 1789e6a886d8SDaniel C. Sobral return -1; 1790e6a886d8SDaniel C. Sobral scan--; 1791e6a886d8SDaniel C. Sobral do { 1792e6a886d8SDaniel C. Sobral scan += OPND(s); 1793e6a886d8SDaniel C. Sobral s = *scan; 17944f8f1c79SKyle Evans if (OP(s) != (sop)O_QUEST && 17954f8f1c79SKyle Evans OP(s) != (sop)O_CH && OP(s) != (sop)OOR2) 1796e6a886d8SDaniel C. Sobral return -1; 17974f8f1c79SKyle Evans } while (OP(s) != (sop)O_QUEST && OP(s) != (sop)O_CH); 17988f9e434fSDaniel C. Sobral /* We must skip to the next position, or we'll 17998f9e434fSDaniel C. Sobral * leave altoffset() too early. 18008f9e434fSDaniel C. Sobral */ 18018f9e434fSDaniel C. Sobral scan++; 1802e6a886d8SDaniel C. Sobral break; 1803e6a886d8SDaniel C. Sobral case OANYOF: 1804e6a886d8SDaniel C. Sobral case OCHAR: 1805e6a886d8SDaniel C. Sobral case OANY: 1806e6a886d8SDaniel C. Sobral try++; 1807e6a886d8SDaniel C. Sobral case OBOW: 1808e6a886d8SDaniel C. Sobral case OEOW: 1809e6a886d8SDaniel C. Sobral case OLPAREN: 1810e6a886d8SDaniel C. Sobral case ORPAREN: 1811e6a886d8SDaniel C. Sobral case OOR2: 1812e6a886d8SDaniel C. Sobral break; 1813e6a886d8SDaniel C. Sobral default: 1814e6a886d8SDaniel C. Sobral try = -1; 1815e6a886d8SDaniel C. Sobral break; 1816e6a886d8SDaniel C. Sobral } 1817e6a886d8SDaniel C. Sobral if (try == -1) 1818e6a886d8SDaniel C. Sobral return -1; 1819e6a886d8SDaniel C. Sobral s = *scan++; 1820e6a886d8SDaniel C. Sobral } 1821e6a886d8SDaniel C. Sobral 1822e6a886d8SDaniel C. Sobral if (try > largest) 1823e6a886d8SDaniel C. Sobral largest = try; 1824e6a886d8SDaniel C. Sobral 1825e6a886d8SDaniel C. Sobral return largest+offset; 1826e6a886d8SDaniel C. Sobral } 1827e6a886d8SDaniel C. Sobral 1828e6a886d8SDaniel C. Sobral /* 18296049d9f0SDaniel C. Sobral - computejumps - compute char jumps for BM scan 18308fb3f3f6SDavid E. O'Brien == static void computejumps(struct parse *p, struct re_guts *g); 18316049d9f0SDaniel C. Sobral * 18326049d9f0SDaniel C. Sobral * This algorithm assumes g->must exists and is has size greater than 18336049d9f0SDaniel C. Sobral * zero. It's based on the algorithm found on Computer Algorithms by 18346049d9f0SDaniel C. Sobral * Sara Baase. 18356049d9f0SDaniel C. Sobral * 18366049d9f0SDaniel C. Sobral * A char jump is the number of characters one needs to jump based on 18376049d9f0SDaniel C. Sobral * the value of the character from the text that was mismatched. 18386049d9f0SDaniel C. Sobral */ 18396049d9f0SDaniel C. Sobral static void 184054a648d1SXin LI computejumps(struct parse *p, struct re_guts *g) 18416049d9f0SDaniel C. Sobral { 18426049d9f0SDaniel C. Sobral int ch; 18436049d9f0SDaniel C. Sobral int mindex; 18446049d9f0SDaniel C. Sobral 18456049d9f0SDaniel C. Sobral /* Avoid making errors worse */ 18466049d9f0SDaniel C. Sobral if (p->error != 0) 18476049d9f0SDaniel C. Sobral return; 18486049d9f0SDaniel C. Sobral 1849*e2a87ae3SYuri Pankov g->charjump = (int *)malloc((NC_MAX + 1) * sizeof(int)); 18506049d9f0SDaniel C. Sobral if (g->charjump == NULL) /* Not a fatal error */ 18516049d9f0SDaniel C. Sobral return; 1852c5e125bbSDaniel C. Sobral /* Adjust for signed chars, if necessary */ 1853*e2a87ae3SYuri Pankov g->charjump = &g->charjump[-(CHAR_MIN)]; 18546049d9f0SDaniel C. Sobral 18556049d9f0SDaniel C. Sobral /* If the character does not exist in the pattern, the jump 18566049d9f0SDaniel C. Sobral * is equal to the number of characters in the pattern. 18576049d9f0SDaniel C. Sobral */ 1858*e2a87ae3SYuri Pankov for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++) 18596049d9f0SDaniel C. Sobral g->charjump[ch] = g->mlen; 18606049d9f0SDaniel C. Sobral 18616049d9f0SDaniel C. Sobral /* If the character does exist, compute the jump that would 18626049d9f0SDaniel C. Sobral * take us to the last character in the pattern equal to it 18636049d9f0SDaniel C. Sobral * (notice that we match right to left, so that last character 18646049d9f0SDaniel C. Sobral * is the first one that would be matched). 18656049d9f0SDaniel C. Sobral */ 18666049d9f0SDaniel C. Sobral for (mindex = 0; mindex < g->mlen; mindex++) 1867e0554a53SJacques Vidrine g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1; 18686049d9f0SDaniel C. Sobral } 18696049d9f0SDaniel C. Sobral 18706049d9f0SDaniel C. Sobral /* 18716049d9f0SDaniel C. Sobral - computematchjumps - compute match jumps for BM scan 18728fb3f3f6SDavid E. O'Brien == static void computematchjumps(struct parse *p, struct re_guts *g); 18736049d9f0SDaniel C. Sobral * 18746049d9f0SDaniel C. Sobral * This algorithm assumes g->must exists and is has size greater than 18756049d9f0SDaniel C. Sobral * zero. It's based on the algorithm found on Computer Algorithms by 18766049d9f0SDaniel C. Sobral * Sara Baase. 18776049d9f0SDaniel C. Sobral * 18786049d9f0SDaniel C. Sobral * A match jump is the number of characters one needs to advance based 18796049d9f0SDaniel C. Sobral * on the already-matched suffix. 18806049d9f0SDaniel C. Sobral * Notice that all values here are minus (g->mlen-1), because of the way 18816049d9f0SDaniel C. Sobral * the search algorithm works. 18826049d9f0SDaniel C. Sobral */ 18836049d9f0SDaniel C. Sobral static void 188454a648d1SXin LI computematchjumps(struct parse *p, struct re_guts *g) 18856049d9f0SDaniel C. Sobral { 18866049d9f0SDaniel C. Sobral int mindex; /* General "must" iterator */ 18876049d9f0SDaniel C. Sobral int suffix; /* Keeps track of matching suffix */ 18886049d9f0SDaniel C. Sobral int ssuffix; /* Keeps track of suffixes' suffix */ 18896049d9f0SDaniel C. Sobral int* pmatches; /* pmatches[k] points to the next i 18906049d9f0SDaniel C. Sobral * such that i+1...mlen is a substring 18916049d9f0SDaniel C. Sobral * of k+1...k+mlen-i-1 18926049d9f0SDaniel C. Sobral */ 18936049d9f0SDaniel C. Sobral 18946049d9f0SDaniel C. Sobral /* Avoid making errors worse */ 18956049d9f0SDaniel C. Sobral if (p->error != 0) 18966049d9f0SDaniel C. Sobral return; 18976049d9f0SDaniel C. Sobral 189812eb0bcaSPedro F. Giffuni pmatches = (int*) malloc(g->mlen * sizeof(int)); 18996049d9f0SDaniel C. Sobral if (pmatches == NULL) { 19006049d9f0SDaniel C. Sobral g->matchjump = NULL; 19016049d9f0SDaniel C. Sobral return; 19026049d9f0SDaniel C. Sobral } 19036049d9f0SDaniel C. Sobral 190412eb0bcaSPedro F. Giffuni g->matchjump = (int*) malloc(g->mlen * sizeof(int)); 1905cfbebadcSXin LI if (g->matchjump == NULL) { /* Not a fatal error */ 1906cfbebadcSXin LI free(pmatches); 19076049d9f0SDaniel C. Sobral return; 1908cfbebadcSXin LI } 19096049d9f0SDaniel C. Sobral 19106049d9f0SDaniel C. Sobral /* Set maximum possible jump for each character in the pattern */ 19116049d9f0SDaniel C. Sobral for (mindex = 0; mindex < g->mlen; mindex++) 19126049d9f0SDaniel C. Sobral g->matchjump[mindex] = 2*g->mlen - mindex - 1; 19136049d9f0SDaniel C. Sobral 19146049d9f0SDaniel C. Sobral /* Compute pmatches[] */ 19156049d9f0SDaniel C. Sobral for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0; 19166049d9f0SDaniel C. Sobral mindex--, suffix--) { 19176049d9f0SDaniel C. Sobral pmatches[mindex] = suffix; 19186049d9f0SDaniel C. Sobral 19196049d9f0SDaniel C. Sobral /* If a mismatch is found, interrupting the substring, 19206049d9f0SDaniel C. Sobral * compute the matchjump for that position. If no 19216049d9f0SDaniel C. Sobral * mismatch is found, then a text substring mismatched 19226049d9f0SDaniel C. Sobral * against the suffix will also mismatch against the 19236049d9f0SDaniel C. Sobral * substring. 19246049d9f0SDaniel C. Sobral */ 19256049d9f0SDaniel C. Sobral while (suffix < g->mlen 19266049d9f0SDaniel C. Sobral && g->must[mindex] != g->must[suffix]) { 19276049d9f0SDaniel C. Sobral g->matchjump[suffix] = MIN(g->matchjump[suffix], 19286049d9f0SDaniel C. Sobral g->mlen - mindex - 1); 19296049d9f0SDaniel C. Sobral suffix = pmatches[suffix]; 19306049d9f0SDaniel C. Sobral } 19316049d9f0SDaniel C. Sobral } 19326049d9f0SDaniel C. Sobral 19336049d9f0SDaniel C. Sobral /* Compute the matchjump up to the last substring found to jump 19346049d9f0SDaniel C. Sobral * to the beginning of the largest must pattern prefix matching 19356049d9f0SDaniel C. Sobral * it's own suffix. 19366049d9f0SDaniel C. Sobral */ 19376049d9f0SDaniel C. Sobral for (mindex = 0; mindex <= suffix; mindex++) 19386049d9f0SDaniel C. Sobral g->matchjump[mindex] = MIN(g->matchjump[mindex], 19396049d9f0SDaniel C. Sobral g->mlen + suffix - mindex); 19406049d9f0SDaniel C. Sobral 19416049d9f0SDaniel C. Sobral ssuffix = pmatches[suffix]; 19426049d9f0SDaniel C. Sobral while (suffix < g->mlen) { 19439868274bSDaniel C. Sobral while (suffix <= ssuffix && suffix < g->mlen) { 19446049d9f0SDaniel C. Sobral g->matchjump[suffix] = MIN(g->matchjump[suffix], 19456049d9f0SDaniel C. Sobral g->mlen + ssuffix - suffix); 19466049d9f0SDaniel C. Sobral suffix++; 19476049d9f0SDaniel C. Sobral } 19488e2f75b8SDaniel C. Sobral if (suffix < g->mlen) 19496049d9f0SDaniel C. Sobral ssuffix = pmatches[ssuffix]; 19506049d9f0SDaniel C. Sobral } 19516049d9f0SDaniel C. Sobral 19526049d9f0SDaniel C. Sobral free(pmatches); 19536049d9f0SDaniel C. Sobral } 19546049d9f0SDaniel C. Sobral 19556049d9f0SDaniel C. Sobral /* 195658f0484fSRodney W. Grimes - pluscount - count + nesting 19578fb3f3f6SDavid E. O'Brien == static sopno pluscount(struct parse *p, struct re_guts *g); 195858f0484fSRodney W. Grimes */ 195958f0484fSRodney W. Grimes static sopno /* nesting depth */ 196054a648d1SXin LI pluscount(struct parse *p, struct re_guts *g) 196158f0484fSRodney W. Grimes { 19628fb3f3f6SDavid E. O'Brien sop *scan; 19638fb3f3f6SDavid E. O'Brien sop s; 19648fb3f3f6SDavid E. O'Brien sopno plusnest = 0; 19658fb3f3f6SDavid E. O'Brien sopno maxnest = 0; 196658f0484fSRodney W. Grimes 196758f0484fSRodney W. Grimes if (p->error != 0) 196858f0484fSRodney W. Grimes return(0); /* there may not be an OEND */ 196958f0484fSRodney W. Grimes 197058f0484fSRodney W. Grimes scan = g->strip + 1; 197158f0484fSRodney W. Grimes do { 197258f0484fSRodney W. Grimes s = *scan++; 197358f0484fSRodney W. Grimes switch (OP(s)) { 197458f0484fSRodney W. Grimes case OPLUS_: 197558f0484fSRodney W. Grimes plusnest++; 197658f0484fSRodney W. Grimes break; 197758f0484fSRodney W. Grimes case O_PLUS: 197858f0484fSRodney W. Grimes if (plusnest > maxnest) 197958f0484fSRodney W. Grimes maxnest = plusnest; 198058f0484fSRodney W. Grimes plusnest--; 198158f0484fSRodney W. Grimes break; 198258f0484fSRodney W. Grimes } 198358f0484fSRodney W. Grimes } while (OP(s) != OEND); 198458f0484fSRodney W. Grimes if (plusnest != 0) 198558f0484fSRodney W. Grimes g->iflags |= BAD; 198658f0484fSRodney W. Grimes return(maxnest); 198758f0484fSRodney W. Grimes } 1988