158f0484fSRodney W. Grimes /*- 258f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 Henry Spencer. 358f0484fSRodney W. Grimes * Copyright (c) 1992, 1993, 1994 458f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 558f0484fSRodney W. Grimes * 658f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 758f0484fSRodney W. Grimes * Henry Spencer. 858f0484fSRodney W. Grimes * 958f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1058f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1158f0484fSRodney W. Grimes * are met: 1258f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1458f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1558f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1658f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1758f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1858f0484fSRodney W. Grimes * must display the following acknowledgement: 1958f0484fSRodney W. Grimes * This product includes software developed by the University of 2058f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2158f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2258f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2358f0484fSRodney W. Grimes * without specific prior written permission. 2458f0484fSRodney W. Grimes * 2558f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2658f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2758f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2858f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2958f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3058f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3158f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3258f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3358f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3458f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3558f0484fSRodney W. Grimes * SUCH DAMAGE. 3658f0484fSRodney W. Grimes * 3758f0484fSRodney W. Grimes * @(#)regcomp.c 8.5 (Berkeley) 3/20/94 3858f0484fSRodney W. Grimes */ 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 4158f0484fSRodney W. Grimes static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94"; 4258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 43333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 44333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 4558f0484fSRodney W. Grimes 4658f0484fSRodney W. Grimes #include <sys/types.h> 4758f0484fSRodney W. Grimes #include <stdio.h> 4858f0484fSRodney W. Grimes #include <string.h> 4958f0484fSRodney W. Grimes #include <ctype.h> 5058f0484fSRodney W. Grimes #include <limits.h> 5158f0484fSRodney W. Grimes #include <stdlib.h> 5258f0484fSRodney W. Grimes #include <regex.h> 53e5996857STim J. Robbins #include <wchar.h> 54e5996857STim J. Robbins #include <wctype.h> 5558f0484fSRodney W. Grimes 56c61cea72SAndrey A. Chernov #include "collate.h" 57c61cea72SAndrey A. Chernov 5858f0484fSRodney W. Grimes #include "utils.h" 5958f0484fSRodney W. Grimes #include "regex2.h" 6058f0484fSRodney W. Grimes 6158f0484fSRodney W. Grimes #include "cname.h" 6258f0484fSRodney W. Grimes 6358f0484fSRodney W. Grimes /* 6458f0484fSRodney W. Grimes * parse structure, passed up and down to avoid global variables and 6558f0484fSRodney W. Grimes * other clumsinesses 6658f0484fSRodney W. Grimes */ 6758f0484fSRodney W. Grimes struct parse { 6858f0484fSRodney W. Grimes char *next; /* next character in RE */ 6958f0484fSRodney W. Grimes char *end; /* end of string (-> NUL normally) */ 7058f0484fSRodney W. Grimes int error; /* has an error been seen? */ 7158f0484fSRodney W. Grimes sop *strip; /* malloced strip */ 7258f0484fSRodney W. Grimes sopno ssize; /* malloced strip size (allocated) */ 7358f0484fSRodney W. Grimes sopno slen; /* malloced strip length (used) */ 7458f0484fSRodney W. Grimes int ncsalloc; /* number of csets allocated */ 7558f0484fSRodney W. Grimes struct re_guts *g; 7658f0484fSRodney W. Grimes # define NPAREN 10 /* we need to remember () 1-9 for back refs */ 7758f0484fSRodney W. Grimes sopno pbegin[NPAREN]; /* -> ( ([0] unused) */ 7858f0484fSRodney W. Grimes sopno pend[NPAREN]; /* -> ) ([0] unused) */ 7958f0484fSRodney W. Grimes }; 8058f0484fSRodney W. Grimes 8158f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */ 8258f0484fSRodney W. Grimes #ifdef __cplusplus 8358f0484fSRodney W. Grimes extern "C" { 8458f0484fSRodney W. Grimes #endif 8558f0484fSRodney W. Grimes 8658f0484fSRodney W. Grimes /* === regcomp.c === */ 87e5996857STim J. Robbins static void p_ere(struct parse *p, wint_t stop); 88c05ac53bSDavid E. O'Brien static void p_ere_exp(struct parse *p); 89c05ac53bSDavid E. O'Brien static void p_str(struct parse *p); 90e5996857STim J. Robbins static void p_bre(struct parse *p, wint_t end1, wint_t end2); 91c05ac53bSDavid E. O'Brien static int p_simp_re(struct parse *p, int starordinary); 92c05ac53bSDavid E. O'Brien static int p_count(struct parse *p); 93c05ac53bSDavid E. O'Brien static void p_bracket(struct parse *p); 94c05ac53bSDavid E. O'Brien static void p_b_term(struct parse *p, cset *cs); 95c05ac53bSDavid E. O'Brien static void p_b_cclass(struct parse *p, cset *cs); 96c05ac53bSDavid E. O'Brien static void p_b_eclass(struct parse *p, cset *cs); 97e5996857STim J. Robbins static wint_t p_b_symbol(struct parse *p); 98e5996857STim J. Robbins static wint_t p_b_coll_elem(struct parse *p, wint_t endc); 99e5996857STim J. Robbins static wint_t othercase(wint_t ch); 100e5996857STim J. Robbins static void bothcases(struct parse *p, wint_t ch); 101e5996857STim J. Robbins static void ordinary(struct parse *p, wint_t ch); 102c05ac53bSDavid E. O'Brien static void nonnewline(struct parse *p); 103c05ac53bSDavid E. O'Brien static void repeat(struct parse *p, sopno start, int from, int to); 104c05ac53bSDavid E. O'Brien static int seterr(struct parse *p, int e); 105c05ac53bSDavid E. O'Brien static cset *allocset(struct parse *p); 106c05ac53bSDavid E. O'Brien static void freeset(struct parse *p, cset *cs); 107e5996857STim J. Robbins static void CHadd(struct parse *p, cset *cs, wint_t ch); 108e5996857STim J. Robbins static void CHaddrange(struct parse *p, cset *cs, wint_t min, wint_t max); 109e5996857STim J. Robbins static void CHaddtype(struct parse *p, cset *cs, wctype_t wct); 110e5996857STim J. Robbins static wint_t singleton(cset *cs); 111c05ac53bSDavid E. O'Brien static sopno dupl(struct parse *p, sopno start, sopno finish); 112c05ac53bSDavid E. O'Brien static void doemit(struct parse *p, sop op, size_t opnd); 113c05ac53bSDavid E. O'Brien static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 114c05ac53bSDavid E. O'Brien static void dofwd(struct parse *p, sopno pos, sop value); 115c05ac53bSDavid E. O'Brien static void enlarge(struct parse *p, sopno size); 116c05ac53bSDavid E. O'Brien static void stripsnug(struct parse *p, struct re_guts *g); 117c05ac53bSDavid E. O'Brien static void findmust(struct parse *p, struct re_guts *g); 11880f36366STim J. Robbins static int altoffset(sop *scan, int offset); 119c05ac53bSDavid E. O'Brien static void computejumps(struct parse *p, struct re_guts *g); 120c05ac53bSDavid E. O'Brien static void computematchjumps(struct parse *p, struct re_guts *g); 121c05ac53bSDavid E. O'Brien static sopno pluscount(struct parse *p, struct re_guts *g); 122e5996857STim J. Robbins static wint_t wgetnext(struct parse *p); 12358f0484fSRodney W. Grimes 12458f0484fSRodney W. Grimes #ifdef __cplusplus 12558f0484fSRodney W. Grimes } 12658f0484fSRodney W. Grimes #endif 12758f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */ 12858f0484fSRodney W. Grimes 12958f0484fSRodney W. Grimes static char nuls[10]; /* place to point scanner in event of error */ 13058f0484fSRodney W. Grimes 13158f0484fSRodney W. Grimes /* 13258f0484fSRodney W. Grimes * macros for use with parse structure 13358f0484fSRodney W. Grimes * BEWARE: these know that the parse structure is named `p' !!! 13458f0484fSRodney W. Grimes */ 13558f0484fSRodney W. Grimes #define PEEK() (*p->next) 13658f0484fSRodney W. Grimes #define PEEK2() (*(p->next+1)) 13758f0484fSRodney W. Grimes #define MORE() (p->next < p->end) 13858f0484fSRodney W. Grimes #define MORE2() (p->next+1 < p->end) 13958f0484fSRodney W. Grimes #define SEE(c) (MORE() && PEEK() == (c)) 14058f0484fSRodney W. Grimes #define SEETWO(a, b) (MORE() && MORE2() && PEEK() == (a) && PEEK2() == (b)) 14158f0484fSRodney W. Grimes #define EAT(c) ((SEE(c)) ? (NEXT(), 1) : 0) 14258f0484fSRodney W. Grimes #define EATTWO(a, b) ((SEETWO(a, b)) ? (NEXT2(), 1) : 0) 14358f0484fSRodney W. Grimes #define NEXT() (p->next++) 14458f0484fSRodney W. Grimes #define NEXT2() (p->next += 2) 14558f0484fSRodney W. Grimes #define NEXTn(n) (p->next += (n)) 14658f0484fSRodney W. Grimes #define GETNEXT() (*p->next++) 147e5996857STim J. Robbins #define WGETNEXT() wgetnext(p) 14858f0484fSRodney W. Grimes #define SETERROR(e) seterr(p, (e)) 14958f0484fSRodney W. Grimes #define REQUIRE(co, e) ((co) || SETERROR(e)) 15058f0484fSRodney W. Grimes #define MUSTSEE(c, e) (REQUIRE(MORE() && PEEK() == (c), e)) 15158f0484fSRodney W. Grimes #define MUSTEAT(c, e) (REQUIRE(MORE() && GETNEXT() == (c), e)) 15258f0484fSRodney W. Grimes #define MUSTNOTSEE(c, e) (REQUIRE(!MORE() || PEEK() != (c), e)) 15358f0484fSRodney W. Grimes #define EMIT(op, sopnd) doemit(p, (sop)(op), (size_t)(sopnd)) 15458f0484fSRodney W. Grimes #define INSERT(op, pos) doinsert(p, (sop)(op), HERE()-(pos)+1, pos) 15558f0484fSRodney W. Grimes #define AHEAD(pos) dofwd(p, pos, HERE()-(pos)) 15658f0484fSRodney W. Grimes #define ASTERN(sop, pos) EMIT(sop, HERE()-pos) 15758f0484fSRodney W. Grimes #define HERE() (p->slen) 15858f0484fSRodney W. Grimes #define THERE() (p->slen - 1) 15958f0484fSRodney W. Grimes #define THERETHERE() (p->slen - 2) 16058f0484fSRodney W. Grimes #define DROP(n) (p->slen -= (n)) 16158f0484fSRodney W. Grimes 16258f0484fSRodney W. Grimes #ifndef NDEBUG 16358f0484fSRodney W. Grimes static int never = 0; /* for use in asserts; shuts lint up */ 16458f0484fSRodney W. Grimes #else 16558f0484fSRodney W. Grimes #define never 0 /* some <assert.h>s have bugs too */ 16658f0484fSRodney W. Grimes #endif 16758f0484fSRodney W. Grimes 1686049d9f0SDaniel C. Sobral /* Macro used by computejump()/computematchjump() */ 1696049d9f0SDaniel C. Sobral #define MIN(a,b) ((a)<(b)?(a):(b)) 1706049d9f0SDaniel C. Sobral 17158f0484fSRodney W. Grimes /* 17258f0484fSRodney W. Grimes - regcomp - interface for parser and compilation 17358f0484fSRodney W. Grimes = extern int regcomp(regex_t *, const char *, int); 17458f0484fSRodney W. Grimes = #define REG_BASIC 0000 17558f0484fSRodney W. Grimes = #define REG_EXTENDED 0001 17658f0484fSRodney W. Grimes = #define REG_ICASE 0002 17758f0484fSRodney W. Grimes = #define REG_NOSUB 0004 17858f0484fSRodney W. Grimes = #define REG_NEWLINE 0010 17958f0484fSRodney W. Grimes = #define REG_NOSPEC 0020 18058f0484fSRodney W. Grimes = #define REG_PEND 0040 18158f0484fSRodney W. Grimes = #define REG_DUMP 0200 18258f0484fSRodney W. Grimes */ 18358f0484fSRodney W. Grimes int /* 0 success, otherwise REG_something */ 18458f0484fSRodney W. Grimes regcomp(preg, pattern, cflags) 1854047df8dSMike Barcroft regex_t * __restrict preg; 1864047df8dSMike Barcroft const char * __restrict pattern; 18758f0484fSRodney W. Grimes int cflags; 18858f0484fSRodney W. Grimes { 18958f0484fSRodney W. Grimes struct parse pa; 1908fb3f3f6SDavid E. O'Brien struct re_guts *g; 1918fb3f3f6SDavid E. O'Brien struct parse *p = &pa; 1928fb3f3f6SDavid E. O'Brien int i; 1938fb3f3f6SDavid E. O'Brien size_t len; 19458f0484fSRodney W. Grimes #ifdef REDEBUG 19558f0484fSRodney W. Grimes # define GOODFLAGS(f) (f) 19658f0484fSRodney W. Grimes #else 19758f0484fSRodney W. Grimes # define GOODFLAGS(f) ((f)&~REG_DUMP) 19858f0484fSRodney W. Grimes #endif 19958f0484fSRodney W. Grimes 20058f0484fSRodney W. Grimes cflags = GOODFLAGS(cflags); 20158f0484fSRodney W. Grimes if ((cflags®_EXTENDED) && (cflags®_NOSPEC)) 20258f0484fSRodney W. Grimes return(REG_INVARG); 20358f0484fSRodney W. Grimes 20458f0484fSRodney W. Grimes if (cflags®_PEND) { 20558f0484fSRodney W. Grimes if (preg->re_endp < pattern) 20658f0484fSRodney W. Grimes return(REG_INVARG); 20758f0484fSRodney W. Grimes len = preg->re_endp - pattern; 20858f0484fSRodney W. Grimes } else 20958f0484fSRodney W. Grimes len = strlen((char *)pattern); 21058f0484fSRodney W. Grimes 21158f0484fSRodney W. Grimes /* do the mallocs early so failure handling is easy */ 21280f36366STim J. Robbins g = (struct re_guts *)malloc(sizeof(struct re_guts)); 21358f0484fSRodney W. Grimes if (g == NULL) 21458f0484fSRodney W. Grimes return(REG_ESPACE); 21558f0484fSRodney W. Grimes p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */ 21658f0484fSRodney W. Grimes p->strip = (sop *)malloc(p->ssize * sizeof(sop)); 21758f0484fSRodney W. Grimes p->slen = 0; 21858f0484fSRodney W. Grimes if (p->strip == NULL) { 21958f0484fSRodney W. Grimes free((char *)g); 22058f0484fSRodney W. Grimes return(REG_ESPACE); 22158f0484fSRodney W. Grimes } 22258f0484fSRodney W. Grimes 22358f0484fSRodney W. Grimes /* set things up */ 22458f0484fSRodney W. Grimes p->g = g; 22558f0484fSRodney W. Grimes p->next = (char *)pattern; /* convenience; we do not modify it */ 22658f0484fSRodney W. Grimes p->end = p->next + len; 22758f0484fSRodney W. Grimes p->error = 0; 22858f0484fSRodney W. Grimes p->ncsalloc = 0; 22958f0484fSRodney W. Grimes for (i = 0; i < NPAREN; i++) { 23058f0484fSRodney W. Grimes p->pbegin[i] = 0; 23158f0484fSRodney W. Grimes p->pend[i] = 0; 23258f0484fSRodney W. Grimes } 23358f0484fSRodney W. Grimes g->sets = NULL; 23458f0484fSRodney W. Grimes g->ncsets = 0; 23558f0484fSRodney W. Grimes g->cflags = cflags; 23658f0484fSRodney W. Grimes g->iflags = 0; 23758f0484fSRodney W. Grimes g->nbol = 0; 23858f0484fSRodney W. Grimes g->neol = 0; 23958f0484fSRodney W. Grimes g->must = NULL; 240e6a886d8SDaniel C. Sobral g->moffset = -1; 2416b709b74SDaniel C. Sobral g->charjump = NULL; 2426b709b74SDaniel C. Sobral g->matchjump = NULL; 24358f0484fSRodney W. Grimes g->mlen = 0; 24458f0484fSRodney W. Grimes g->nsub = 0; 24558f0484fSRodney W. Grimes g->backrefs = 0; 24658f0484fSRodney W. Grimes 24758f0484fSRodney W. Grimes /* do it */ 24858f0484fSRodney W. Grimes EMIT(OEND, 0); 24958f0484fSRodney W. Grimes g->firststate = THERE(); 25058f0484fSRodney W. Grimes if (cflags®_EXTENDED) 25158f0484fSRodney W. Grimes p_ere(p, OUT); 25258f0484fSRodney W. Grimes else if (cflags®_NOSPEC) 25358f0484fSRodney W. Grimes p_str(p); 25458f0484fSRodney W. Grimes else 25558f0484fSRodney W. Grimes p_bre(p, OUT, OUT); 25658f0484fSRodney W. Grimes EMIT(OEND, 0); 25758f0484fSRodney W. Grimes g->laststate = THERE(); 25858f0484fSRodney W. Grimes 25958f0484fSRodney W. Grimes /* tidy up loose ends and fill things in */ 26058f0484fSRodney W. Grimes stripsnug(p, g); 26158f0484fSRodney W. Grimes findmust(p, g); 2626049d9f0SDaniel C. Sobral /* only use Boyer-Moore algorithm if the pattern is bigger 2636049d9f0SDaniel C. Sobral * than three characters 2646049d9f0SDaniel C. Sobral */ 2656049d9f0SDaniel C. Sobral if(g->mlen > 3) { 2666049d9f0SDaniel C. Sobral computejumps(p, g); 2676049d9f0SDaniel C. Sobral computematchjumps(p, g); 2684d41cae8SDaniel C. Sobral if(g->matchjump == NULL && g->charjump != NULL) { 2696049d9f0SDaniel C. Sobral free(g->charjump); 2706049d9f0SDaniel C. Sobral g->charjump = NULL; 2716049d9f0SDaniel C. Sobral } 2726049d9f0SDaniel C. Sobral } 27358f0484fSRodney W. Grimes g->nplus = pluscount(p, g); 27458f0484fSRodney W. Grimes g->magic = MAGIC2; 27558f0484fSRodney W. Grimes preg->re_nsub = g->nsub; 27658f0484fSRodney W. Grimes preg->re_g = g; 27758f0484fSRodney W. Grimes preg->re_magic = MAGIC1; 27858f0484fSRodney W. Grimes #ifndef REDEBUG 27958f0484fSRodney W. Grimes /* not debugging, so can't rely on the assert() in regexec() */ 28058f0484fSRodney W. Grimes if (g->iflags&BAD) 28158f0484fSRodney W. Grimes SETERROR(REG_ASSERT); 28258f0484fSRodney W. Grimes #endif 28358f0484fSRodney W. Grimes 28458f0484fSRodney W. Grimes /* win or lose, we're done */ 28558f0484fSRodney W. Grimes if (p->error != 0) /* lose */ 28658f0484fSRodney W. Grimes regfree(preg); 28758f0484fSRodney W. Grimes return(p->error); 28858f0484fSRodney W. Grimes } 28958f0484fSRodney W. Grimes 29058f0484fSRodney W. Grimes /* 29158f0484fSRodney W. Grimes - p_ere - ERE parser top level, concatenation and alternation 2928fb3f3f6SDavid E. O'Brien == static void p_ere(struct parse *p, int stop); 29358f0484fSRodney W. Grimes */ 29458f0484fSRodney W. Grimes static void 29558f0484fSRodney W. Grimes p_ere(p, stop) 2968fb3f3f6SDavid E. O'Brien struct parse *p; 29758f0484fSRodney W. Grimes int stop; /* character this ERE should end at */ 29858f0484fSRodney W. Grimes { 2998fb3f3f6SDavid E. O'Brien char c; 3008fb3f3f6SDavid E. O'Brien sopno prevback; 3018fb3f3f6SDavid E. O'Brien sopno prevfwd; 3028fb3f3f6SDavid E. O'Brien sopno conc; 3038fb3f3f6SDavid E. O'Brien int first = 1; /* is this the first alternative? */ 30458f0484fSRodney W. Grimes 30558f0484fSRodney W. Grimes for (;;) { 30658f0484fSRodney W. Grimes /* do a bunch of concatenated expressions */ 30758f0484fSRodney W. Grimes conc = HERE(); 30858f0484fSRodney W. Grimes while (MORE() && (c = PEEK()) != '|' && c != stop) 30958f0484fSRodney W. Grimes p_ere_exp(p); 31051295a4dSJordan K. Hubbard (void)REQUIRE(HERE() != conc, REG_EMPTY); /* require nonempty */ 31158f0484fSRodney W. Grimes 31258f0484fSRodney W. Grimes if (!EAT('|')) 31358f0484fSRodney W. Grimes break; /* NOTE BREAK OUT */ 31458f0484fSRodney W. Grimes 31558f0484fSRodney W. Grimes if (first) { 31658f0484fSRodney W. Grimes INSERT(OCH_, conc); /* offset is wrong */ 31758f0484fSRodney W. Grimes prevfwd = conc; 31858f0484fSRodney W. Grimes prevback = conc; 31958f0484fSRodney W. Grimes first = 0; 32058f0484fSRodney W. Grimes } 32158f0484fSRodney W. Grimes ASTERN(OOR1, prevback); 32258f0484fSRodney W. Grimes prevback = THERE(); 32358f0484fSRodney W. Grimes AHEAD(prevfwd); /* fix previous offset */ 32458f0484fSRodney W. Grimes prevfwd = HERE(); 32558f0484fSRodney W. Grimes EMIT(OOR2, 0); /* offset is very wrong */ 32658f0484fSRodney W. Grimes } 32758f0484fSRodney W. Grimes 32858f0484fSRodney W. Grimes if (!first) { /* tail-end fixups */ 32958f0484fSRodney W. Grimes AHEAD(prevfwd); 33058f0484fSRodney W. Grimes ASTERN(O_CH, prevback); 33158f0484fSRodney W. Grimes } 33258f0484fSRodney W. Grimes 33358f0484fSRodney W. Grimes assert(!MORE() || SEE(stop)); 33458f0484fSRodney W. Grimes } 33558f0484fSRodney W. Grimes 33658f0484fSRodney W. Grimes /* 33758f0484fSRodney W. Grimes - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op 3388fb3f3f6SDavid E. O'Brien == static void p_ere_exp(struct parse *p); 33958f0484fSRodney W. Grimes */ 34058f0484fSRodney W. Grimes static void 34158f0484fSRodney W. Grimes p_ere_exp(p) 3428fb3f3f6SDavid E. O'Brien struct parse *p; 34358f0484fSRodney W. Grimes { 3448fb3f3f6SDavid E. O'Brien char c; 345e5996857STim J. Robbins wint_t wc; 3468fb3f3f6SDavid E. O'Brien sopno pos; 3478fb3f3f6SDavid E. O'Brien int count; 3488fb3f3f6SDavid E. O'Brien int count2; 3498fb3f3f6SDavid E. O'Brien sopno subno; 35058f0484fSRodney W. Grimes int wascaret = 0; 35158f0484fSRodney W. Grimes 35258f0484fSRodney W. Grimes assert(MORE()); /* caller should have ensured this */ 35358f0484fSRodney W. Grimes c = GETNEXT(); 35458f0484fSRodney W. Grimes 35558f0484fSRodney W. Grimes pos = HERE(); 35658f0484fSRodney W. Grimes switch (c) { 35758f0484fSRodney W. Grimes case '(': 35851295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EPAREN); 35958f0484fSRodney W. Grimes p->g->nsub++; 36058f0484fSRodney W. Grimes subno = p->g->nsub; 36158f0484fSRodney W. Grimes if (subno < NPAREN) 36258f0484fSRodney W. Grimes p->pbegin[subno] = HERE(); 36358f0484fSRodney W. Grimes EMIT(OLPAREN, subno); 36458f0484fSRodney W. Grimes if (!SEE(')')) 36558f0484fSRodney W. Grimes p_ere(p, ')'); 36658f0484fSRodney W. Grimes if (subno < NPAREN) { 36758f0484fSRodney W. Grimes p->pend[subno] = HERE(); 36858f0484fSRodney W. Grimes assert(p->pend[subno] != 0); 36958f0484fSRodney W. Grimes } 37058f0484fSRodney W. Grimes EMIT(ORPAREN, subno); 37151295a4dSJordan K. Hubbard (void)MUSTEAT(')', REG_EPAREN); 37258f0484fSRodney W. Grimes break; 37358f0484fSRodney W. Grimes #ifndef POSIX_MISTAKE 37458f0484fSRodney W. Grimes case ')': /* happens only if no current unmatched ( */ 37558f0484fSRodney W. Grimes /* 37658f0484fSRodney W. Grimes * You may ask, why the ifndef? Because I didn't notice 37758f0484fSRodney W. Grimes * this until slightly too late for 1003.2, and none of the 37858f0484fSRodney W. Grimes * other 1003.2 regular-expression reviewers noticed it at 37958f0484fSRodney W. Grimes * all. So an unmatched ) is legal POSIX, at least until 38058f0484fSRodney W. Grimes * we can get it fixed. 38158f0484fSRodney W. Grimes */ 38258f0484fSRodney W. Grimes SETERROR(REG_EPAREN); 38358f0484fSRodney W. Grimes break; 38458f0484fSRodney W. Grimes #endif 38558f0484fSRodney W. Grimes case '^': 38658f0484fSRodney W. Grimes EMIT(OBOL, 0); 38758f0484fSRodney W. Grimes p->g->iflags |= USEBOL; 38858f0484fSRodney W. Grimes p->g->nbol++; 38958f0484fSRodney W. Grimes wascaret = 1; 39058f0484fSRodney W. Grimes break; 39158f0484fSRodney W. Grimes case '$': 39258f0484fSRodney W. Grimes EMIT(OEOL, 0); 39358f0484fSRodney W. Grimes p->g->iflags |= USEEOL; 39458f0484fSRodney W. Grimes p->g->neol++; 39558f0484fSRodney W. Grimes break; 39658f0484fSRodney W. Grimes case '|': 39758f0484fSRodney W. Grimes SETERROR(REG_EMPTY); 39858f0484fSRodney W. Grimes break; 39958f0484fSRodney W. Grimes case '*': 40058f0484fSRodney W. Grimes case '+': 40158f0484fSRodney W. Grimes case '?': 40258f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 40358f0484fSRodney W. Grimes break; 40458f0484fSRodney W. Grimes case '.': 40558f0484fSRodney W. Grimes if (p->g->cflags®_NEWLINE) 40658f0484fSRodney W. Grimes nonnewline(p); 40758f0484fSRodney W. Grimes else 40858f0484fSRodney W. Grimes EMIT(OANY, 0); 40958f0484fSRodney W. Grimes break; 41058f0484fSRodney W. Grimes case '[': 41158f0484fSRodney W. Grimes p_bracket(p); 41258f0484fSRodney W. Grimes break; 41358f0484fSRodney W. Grimes case '\\': 41451295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EESCAPE); 415e5996857STim J. Robbins wc = WGETNEXT(); 416e5996857STim J. Robbins ordinary(p, wc); 41758f0484fSRodney W. Grimes break; 41858f0484fSRodney W. Grimes case '{': /* okay as ordinary except if digit follows */ 41989b86d02SAndrey A. Chernov (void)REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT); 42058f0484fSRodney W. Grimes /* FALLTHROUGH */ 42158f0484fSRodney W. Grimes default: 422e5996857STim J. Robbins p->next--; 423e5996857STim J. Robbins wc = WGETNEXT(); 424e5996857STim J. Robbins ordinary(p, wc); 42558f0484fSRodney W. Grimes break; 42658f0484fSRodney W. Grimes } 42758f0484fSRodney W. Grimes 42858f0484fSRodney W. Grimes if (!MORE()) 42958f0484fSRodney W. Grimes return; 43058f0484fSRodney W. Grimes c = PEEK(); 43158f0484fSRodney W. Grimes /* we call { a repetition if followed by a digit */ 43258f0484fSRodney W. Grimes if (!( c == '*' || c == '+' || c == '?' || 43389b86d02SAndrey A. Chernov (c == '{' && MORE2() && isdigit((uch)PEEK2())) )) 43458f0484fSRodney W. Grimes return; /* no repetition, we're done */ 43558f0484fSRodney W. Grimes NEXT(); 43658f0484fSRodney W. Grimes 43751295a4dSJordan K. Hubbard (void)REQUIRE(!wascaret, REG_BADRPT); 43858f0484fSRodney W. Grimes switch (c) { 43958f0484fSRodney W. Grimes case '*': /* implemented as +? */ 44058f0484fSRodney W. Grimes /* this case does not require the (y|) trick, noKLUDGE */ 44158f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 44258f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 44358f0484fSRodney W. Grimes INSERT(OQUEST_, pos); 44458f0484fSRodney W. Grimes ASTERN(O_QUEST, pos); 44558f0484fSRodney W. Grimes break; 44658f0484fSRodney W. Grimes case '+': 44758f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 44858f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 44958f0484fSRodney W. Grimes break; 45058f0484fSRodney W. Grimes case '?': 45158f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 45258f0484fSRodney W. Grimes INSERT(OCH_, pos); /* offset slightly wrong */ 45358f0484fSRodney W. Grimes ASTERN(OOR1, pos); /* this one's right */ 45458f0484fSRodney W. Grimes AHEAD(pos); /* fix the OCH_ */ 45558f0484fSRodney W. Grimes EMIT(OOR2, 0); /* offset very wrong... */ 45658f0484fSRodney W. Grimes AHEAD(THERE()); /* ...so fix it */ 45758f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 45858f0484fSRodney W. Grimes break; 45958f0484fSRodney W. Grimes case '{': 46058f0484fSRodney W. Grimes count = p_count(p); 46158f0484fSRodney W. Grimes if (EAT(',')) { 46289b86d02SAndrey A. Chernov if (isdigit((uch)PEEK())) { 46358f0484fSRodney W. Grimes count2 = p_count(p); 46451295a4dSJordan K. Hubbard (void)REQUIRE(count <= count2, REG_BADBR); 46558f0484fSRodney W. Grimes } else /* single number with comma */ 46658f0484fSRodney W. Grimes count2 = INFINITY; 46758f0484fSRodney W. Grimes } else /* just a single number */ 46858f0484fSRodney W. Grimes count2 = count; 46958f0484fSRodney W. Grimes repeat(p, pos, count, count2); 47058f0484fSRodney W. Grimes if (!EAT('}')) { /* error heuristics */ 47158f0484fSRodney W. Grimes while (MORE() && PEEK() != '}') 47258f0484fSRodney W. Grimes NEXT(); 47351295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACE); 47458f0484fSRodney W. Grimes SETERROR(REG_BADBR); 47558f0484fSRodney W. Grimes } 47658f0484fSRodney W. Grimes break; 47758f0484fSRodney W. Grimes } 47858f0484fSRodney W. Grimes 47958f0484fSRodney W. Grimes if (!MORE()) 48058f0484fSRodney W. Grimes return; 48158f0484fSRodney W. Grimes c = PEEK(); 48258f0484fSRodney W. Grimes if (!( c == '*' || c == '+' || c == '?' || 48389b86d02SAndrey A. Chernov (c == '{' && MORE2() && isdigit((uch)PEEK2())) ) ) 48458f0484fSRodney W. Grimes return; 48558f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 48658f0484fSRodney W. Grimes } 48758f0484fSRodney W. Grimes 48858f0484fSRodney W. Grimes /* 48958f0484fSRodney W. Grimes - p_str - string (no metacharacters) "parser" 4908fb3f3f6SDavid E. O'Brien == static void p_str(struct parse *p); 49158f0484fSRodney W. Grimes */ 49258f0484fSRodney W. Grimes static void 49358f0484fSRodney W. Grimes p_str(p) 4948fb3f3f6SDavid E. O'Brien struct parse *p; 49558f0484fSRodney W. Grimes { 49651295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EMPTY); 49758f0484fSRodney W. Grimes while (MORE()) 498e5996857STim J. Robbins ordinary(p, WGETNEXT()); 49958f0484fSRodney W. Grimes } 50058f0484fSRodney W. Grimes 50158f0484fSRodney W. Grimes /* 50258f0484fSRodney W. Grimes - p_bre - BRE parser top level, anchoring and concatenation 5038fb3f3f6SDavid E. O'Brien == static void p_bre(struct parse *p, int end1, \ 5048fb3f3f6SDavid E. O'Brien == int end2); 50558f0484fSRodney W. Grimes * Giving end1 as OUT essentially eliminates the end1/end2 check. 50658f0484fSRodney W. Grimes * 50758f0484fSRodney W. Grimes * This implementation is a bit of a kludge, in that a trailing $ is first 50880f36366STim J. Robbins * taken as an ordinary character and then revised to be an anchor. 50958f0484fSRodney W. Grimes * The amount of lookahead needed to avoid this kludge is excessive. 51058f0484fSRodney W. Grimes */ 51158f0484fSRodney W. Grimes static void 51258f0484fSRodney W. Grimes p_bre(p, end1, end2) 5138fb3f3f6SDavid E. O'Brien struct parse *p; 5148fb3f3f6SDavid E. O'Brien int end1; /* first terminating character */ 5158fb3f3f6SDavid E. O'Brien int end2; /* second terminating character */ 51658f0484fSRodney W. Grimes { 5178fb3f3f6SDavid E. O'Brien sopno start = HERE(); 5188fb3f3f6SDavid E. O'Brien int first = 1; /* first subexpression? */ 5198fb3f3f6SDavid E. O'Brien int wasdollar = 0; 52058f0484fSRodney W. Grimes 52158f0484fSRodney W. Grimes if (EAT('^')) { 52258f0484fSRodney W. Grimes EMIT(OBOL, 0); 52358f0484fSRodney W. Grimes p->g->iflags |= USEBOL; 52458f0484fSRodney W. Grimes p->g->nbol++; 52558f0484fSRodney W. Grimes } 52658f0484fSRodney W. Grimes while (MORE() && !SEETWO(end1, end2)) { 52758f0484fSRodney W. Grimes wasdollar = p_simp_re(p, first); 52858f0484fSRodney W. Grimes first = 0; 52958f0484fSRodney W. Grimes } 53058f0484fSRodney W. Grimes if (wasdollar) { /* oops, that was a trailing anchor */ 53158f0484fSRodney W. Grimes DROP(1); 53258f0484fSRodney W. Grimes EMIT(OEOL, 0); 53358f0484fSRodney W. Grimes p->g->iflags |= USEEOL; 53458f0484fSRodney W. Grimes p->g->neol++; 53558f0484fSRodney W. Grimes } 53658f0484fSRodney W. Grimes 53751295a4dSJordan K. Hubbard (void)REQUIRE(HERE() != start, REG_EMPTY); /* require nonempty */ 53858f0484fSRodney W. Grimes } 53958f0484fSRodney W. Grimes 54058f0484fSRodney W. Grimes /* 54158f0484fSRodney W. Grimes - p_simp_re - parse a simple RE, an atom possibly followed by a repetition 5428fb3f3f6SDavid E. O'Brien == static int p_simp_re(struct parse *p, int starordinary); 54358f0484fSRodney W. Grimes */ 54458f0484fSRodney W. Grimes static int /* was the simple RE an unbackslashed $? */ 54558f0484fSRodney W. Grimes p_simp_re(p, starordinary) 5468fb3f3f6SDavid E. O'Brien struct parse *p; 54758f0484fSRodney W. Grimes int starordinary; /* is a leading * an ordinary character? */ 54858f0484fSRodney W. Grimes { 5498fb3f3f6SDavid E. O'Brien int c; 5508fb3f3f6SDavid E. O'Brien int count; 5518fb3f3f6SDavid E. O'Brien int count2; 5528fb3f3f6SDavid E. O'Brien sopno pos; 5538fb3f3f6SDavid E. O'Brien int i; 554e5996857STim J. Robbins wint_t wc; 5558fb3f3f6SDavid E. O'Brien sopno subno; 55658f0484fSRodney W. Grimes # define BACKSL (1<<CHAR_BIT) 55758f0484fSRodney W. Grimes 55858f0484fSRodney W. Grimes pos = HERE(); /* repetion op, if any, covers from here */ 55958f0484fSRodney W. Grimes 56058f0484fSRodney W. Grimes assert(MORE()); /* caller should have ensured this */ 56158f0484fSRodney W. Grimes c = GETNEXT(); 56258f0484fSRodney W. Grimes if (c == '\\') { 56351295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EESCAPE); 56489b86d02SAndrey A. Chernov c = BACKSL | GETNEXT(); 56558f0484fSRodney W. Grimes } 56658f0484fSRodney W. Grimes switch (c) { 56758f0484fSRodney W. Grimes case '.': 56858f0484fSRodney W. Grimes if (p->g->cflags®_NEWLINE) 56958f0484fSRodney W. Grimes nonnewline(p); 57058f0484fSRodney W. Grimes else 57158f0484fSRodney W. Grimes EMIT(OANY, 0); 57258f0484fSRodney W. Grimes break; 57358f0484fSRodney W. Grimes case '[': 57458f0484fSRodney W. Grimes p_bracket(p); 57558f0484fSRodney W. Grimes break; 57658f0484fSRodney W. Grimes case BACKSL|'{': 57758f0484fSRodney W. Grimes SETERROR(REG_BADRPT); 57858f0484fSRodney W. Grimes break; 57958f0484fSRodney W. Grimes case BACKSL|'(': 58058f0484fSRodney W. Grimes p->g->nsub++; 58158f0484fSRodney W. Grimes subno = p->g->nsub; 58258f0484fSRodney W. Grimes if (subno < NPAREN) 58358f0484fSRodney W. Grimes p->pbegin[subno] = HERE(); 58458f0484fSRodney W. Grimes EMIT(OLPAREN, subno); 58558f0484fSRodney W. Grimes /* the MORE here is an error heuristic */ 58658f0484fSRodney W. Grimes if (MORE() && !SEETWO('\\', ')')) 58758f0484fSRodney W. Grimes p_bre(p, '\\', ')'); 58858f0484fSRodney W. Grimes if (subno < NPAREN) { 58958f0484fSRodney W. Grimes p->pend[subno] = HERE(); 59058f0484fSRodney W. Grimes assert(p->pend[subno] != 0); 59158f0484fSRodney W. Grimes } 59258f0484fSRodney W. Grimes EMIT(ORPAREN, subno); 59351295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('\\', ')'), REG_EPAREN); 59458f0484fSRodney W. Grimes break; 59558f0484fSRodney W. Grimes case BACKSL|')': /* should not get here -- must be user */ 59658f0484fSRodney W. Grimes case BACKSL|'}': 59758f0484fSRodney W. Grimes SETERROR(REG_EPAREN); 59858f0484fSRodney W. Grimes break; 59958f0484fSRodney W. Grimes case BACKSL|'1': 60058f0484fSRodney W. Grimes case BACKSL|'2': 60158f0484fSRodney W. Grimes case BACKSL|'3': 60258f0484fSRodney W. Grimes case BACKSL|'4': 60358f0484fSRodney W. Grimes case BACKSL|'5': 60458f0484fSRodney W. Grimes case BACKSL|'6': 60558f0484fSRodney W. Grimes case BACKSL|'7': 60658f0484fSRodney W. Grimes case BACKSL|'8': 60758f0484fSRodney W. Grimes case BACKSL|'9': 60858f0484fSRodney W. Grimes i = (c&~BACKSL) - '0'; 60958f0484fSRodney W. Grimes assert(i < NPAREN); 61058f0484fSRodney W. Grimes if (p->pend[i] != 0) { 61158f0484fSRodney W. Grimes assert(i <= p->g->nsub); 61258f0484fSRodney W. Grimes EMIT(OBACK_, i); 61358f0484fSRodney W. Grimes assert(p->pbegin[i] != 0); 61458f0484fSRodney W. Grimes assert(OP(p->strip[p->pbegin[i]]) == OLPAREN); 61558f0484fSRodney W. Grimes assert(OP(p->strip[p->pend[i]]) == ORPAREN); 61658f0484fSRodney W. Grimes (void) dupl(p, p->pbegin[i]+1, p->pend[i]); 61758f0484fSRodney W. Grimes EMIT(O_BACK, i); 61858f0484fSRodney W. Grimes } else 61958f0484fSRodney W. Grimes SETERROR(REG_ESUBREG); 62058f0484fSRodney W. Grimes p->g->backrefs = 1; 62158f0484fSRodney W. Grimes break; 62258f0484fSRodney W. Grimes case '*': 62351295a4dSJordan K. Hubbard (void)REQUIRE(starordinary, REG_BADRPT); 62458f0484fSRodney W. Grimes /* FALLTHROUGH */ 62558f0484fSRodney W. Grimes default: 626e5996857STim J. Robbins p->next--; 627e5996857STim J. Robbins wc = WGETNEXT(); 628e5996857STim J. Robbins ordinary(p, wc); 62958f0484fSRodney W. Grimes break; 63058f0484fSRodney W. Grimes } 63158f0484fSRodney W. Grimes 63258f0484fSRodney W. Grimes if (EAT('*')) { /* implemented as +? */ 63358f0484fSRodney W. Grimes /* this case does not require the (y|) trick, noKLUDGE */ 63458f0484fSRodney W. Grimes INSERT(OPLUS_, pos); 63558f0484fSRodney W. Grimes ASTERN(O_PLUS, pos); 63658f0484fSRodney W. Grimes INSERT(OQUEST_, pos); 63758f0484fSRodney W. Grimes ASTERN(O_QUEST, pos); 63858f0484fSRodney W. Grimes } else if (EATTWO('\\', '{')) { 63958f0484fSRodney W. Grimes count = p_count(p); 64058f0484fSRodney W. Grimes if (EAT(',')) { 64189b86d02SAndrey A. Chernov if (MORE() && isdigit((uch)PEEK())) { 64258f0484fSRodney W. Grimes count2 = p_count(p); 64351295a4dSJordan K. Hubbard (void)REQUIRE(count <= count2, REG_BADBR); 64458f0484fSRodney W. Grimes } else /* single number with comma */ 64558f0484fSRodney W. Grimes count2 = INFINITY; 64658f0484fSRodney W. Grimes } else /* just a single number */ 64758f0484fSRodney W. Grimes count2 = count; 64858f0484fSRodney W. Grimes repeat(p, pos, count, count2); 64958f0484fSRodney W. Grimes if (!EATTWO('\\', '}')) { /* error heuristics */ 65058f0484fSRodney W. Grimes while (MORE() && !SEETWO('\\', '}')) 65158f0484fSRodney W. Grimes NEXT(); 65251295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACE); 65358f0484fSRodney W. Grimes SETERROR(REG_BADBR); 65458f0484fSRodney W. Grimes } 65589b86d02SAndrey A. Chernov } else if (c == '$') /* $ (but not \$) ends it */ 65658f0484fSRodney W. Grimes return(1); 65758f0484fSRodney W. Grimes 65858f0484fSRodney W. Grimes return(0); 65958f0484fSRodney W. Grimes } 66058f0484fSRodney W. Grimes 66158f0484fSRodney W. Grimes /* 66258f0484fSRodney W. Grimes - p_count - parse a repetition count 6638fb3f3f6SDavid E. O'Brien == static int p_count(struct parse *p); 66458f0484fSRodney W. Grimes */ 66558f0484fSRodney W. Grimes static int /* the value */ 66658f0484fSRodney W. Grimes p_count(p) 6678fb3f3f6SDavid E. O'Brien struct parse *p; 66858f0484fSRodney W. Grimes { 6698fb3f3f6SDavid E. O'Brien int count = 0; 6708fb3f3f6SDavid E. O'Brien int ndigits = 0; 67158f0484fSRodney W. Grimes 67289b86d02SAndrey A. Chernov while (MORE() && isdigit((uch)PEEK()) && count <= DUPMAX) { 67358f0484fSRodney W. Grimes count = count*10 + (GETNEXT() - '0'); 67458f0484fSRodney W. Grimes ndigits++; 67558f0484fSRodney W. Grimes } 67658f0484fSRodney W. Grimes 67751295a4dSJordan K. Hubbard (void)REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR); 67858f0484fSRodney W. Grimes return(count); 67958f0484fSRodney W. Grimes } 68058f0484fSRodney W. Grimes 68158f0484fSRodney W. Grimes /* 68258f0484fSRodney W. Grimes - p_bracket - parse a bracketed character list 6838fb3f3f6SDavid E. O'Brien == static void p_bracket(struct parse *p); 68458f0484fSRodney W. Grimes */ 68558f0484fSRodney W. Grimes static void 68658f0484fSRodney W. Grimes p_bracket(p) 6878fb3f3f6SDavid E. O'Brien struct parse *p; 68858f0484fSRodney W. Grimes { 689e5996857STim J. Robbins cset *cs; 690e5996857STim J. Robbins wint_t ch; 69158f0484fSRodney W. Grimes 69258f0484fSRodney W. Grimes /* Dept of Truly Sickening Special-Case Kludges */ 69358f0484fSRodney W. Grimes if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) { 69458f0484fSRodney W. Grimes EMIT(OBOW, 0); 69558f0484fSRodney W. Grimes NEXTn(6); 69658f0484fSRodney W. Grimes return; 69758f0484fSRodney W. Grimes } 69858f0484fSRodney W. Grimes if (p->next + 5 < p->end && strncmp(p->next, "[:>:]]", 6) == 0) { 69958f0484fSRodney W. Grimes EMIT(OEOW, 0); 70058f0484fSRodney W. Grimes NEXTn(6); 70158f0484fSRodney W. Grimes return; 70258f0484fSRodney W. Grimes } 70358f0484fSRodney W. Grimes 704e5996857STim J. Robbins if ((cs = allocset(p)) == NULL) 705e5996857STim J. Robbins return; 706e5996857STim J. Robbins 707e5996857STim J. Robbins if (p->g->cflags®_ICASE) 708e5996857STim J. Robbins cs->icase = 1; 70958f0484fSRodney W. Grimes if (EAT('^')) 710e5996857STim J. Robbins cs->invert = 1; 71158f0484fSRodney W. Grimes if (EAT(']')) 712e5996857STim J. Robbins CHadd(p, cs, ']'); 71358f0484fSRodney W. Grimes else if (EAT('-')) 714e5996857STim J. Robbins CHadd(p, cs, '-'); 71558f0484fSRodney W. Grimes while (MORE() && PEEK() != ']' && !SEETWO('-', ']')) 71658f0484fSRodney W. Grimes p_b_term(p, cs); 71758f0484fSRodney W. Grimes if (EAT('-')) 718e5996857STim J. Robbins CHadd(p, cs, '-'); 71951295a4dSJordan K. Hubbard (void)MUSTEAT(']', REG_EBRACK); 72058f0484fSRodney W. Grimes 72158f0484fSRodney W. Grimes if (p->error != 0) /* don't mess things up further */ 72258f0484fSRodney W. Grimes return; 72358f0484fSRodney W. Grimes 724e5996857STim J. Robbins if (cs->invert && p->g->cflags®_NEWLINE) 725e5996857STim J. Robbins cs->bmp['\n' >> 3] |= 1 << ('\n' & 7); 72658f0484fSRodney W. Grimes 727e5996857STim J. Robbins if ((ch = singleton(cs)) != OUT) { /* optimize singleton sets */ 728e5996857STim J. Robbins ordinary(p, ch); 72958f0484fSRodney W. Grimes freeset(p, cs); 73058f0484fSRodney W. Grimes } else 731e5996857STim J. Robbins EMIT(OANYOF, (int)(cs - p->g->sets)); 73258f0484fSRodney W. Grimes } 73358f0484fSRodney W. Grimes 73458f0484fSRodney W. Grimes /* 73558f0484fSRodney W. Grimes - p_b_term - parse one term of a bracketed character list 7368fb3f3f6SDavid E. O'Brien == static void p_b_term(struct parse *p, cset *cs); 73758f0484fSRodney W. Grimes */ 73858f0484fSRodney W. Grimes static void 73958f0484fSRodney W. Grimes p_b_term(p, cs) 7408fb3f3f6SDavid E. O'Brien struct parse *p; 7418fb3f3f6SDavid E. O'Brien cset *cs; 74258f0484fSRodney W. Grimes { 7438fb3f3f6SDavid E. O'Brien char c; 744e5996857STim J. Robbins wint_t start, finish; 745e5996857STim J. Robbins wint_t i; 74658f0484fSRodney W. Grimes 74758f0484fSRodney W. Grimes /* classify what we've got */ 74858f0484fSRodney W. Grimes switch ((MORE()) ? PEEK() : '\0') { 74958f0484fSRodney W. Grimes case '[': 75058f0484fSRodney W. Grimes c = (MORE2()) ? PEEK2() : '\0'; 75158f0484fSRodney W. Grimes break; 75258f0484fSRodney W. Grimes case '-': 75358f0484fSRodney W. Grimes SETERROR(REG_ERANGE); 75458f0484fSRodney W. Grimes return; /* NOTE RETURN */ 75558f0484fSRodney W. Grimes break; 75658f0484fSRodney W. Grimes default: 75758f0484fSRodney W. Grimes c = '\0'; 75858f0484fSRodney W. Grimes break; 75958f0484fSRodney W. Grimes } 76058f0484fSRodney W. Grimes 76158f0484fSRodney W. Grimes switch (c) { 76258f0484fSRodney W. Grimes case ':': /* character class */ 76358f0484fSRodney W. Grimes NEXT2(); 76451295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 76558f0484fSRodney W. Grimes c = PEEK(); 76651295a4dSJordan K. Hubbard (void)REQUIRE(c != '-' && c != ']', REG_ECTYPE); 76758f0484fSRodney W. Grimes p_b_cclass(p, cs); 76851295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 76951295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO(':', ']'), REG_ECTYPE); 77058f0484fSRodney W. Grimes break; 77158f0484fSRodney W. Grimes case '=': /* equivalence class */ 77258f0484fSRodney W. Grimes NEXT2(); 77351295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 77458f0484fSRodney W. Grimes c = PEEK(); 77551295a4dSJordan K. Hubbard (void)REQUIRE(c != '-' && c != ']', REG_ECOLLATE); 77658f0484fSRodney W. Grimes p_b_eclass(p, cs); 77751295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 77851295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('=', ']'), REG_ECOLLATE); 77958f0484fSRodney W. Grimes break; 78058f0484fSRodney W. Grimes default: /* symbol, ordinary character, or range */ 78158f0484fSRodney W. Grimes start = p_b_symbol(p); 78258f0484fSRodney W. Grimes if (SEE('-') && MORE2() && PEEK2() != ']') { 78358f0484fSRodney W. Grimes /* range */ 78458f0484fSRodney W. Grimes NEXT(); 78558f0484fSRodney W. Grimes if (EAT('-')) 78658f0484fSRodney W. Grimes finish = '-'; 78758f0484fSRodney W. Grimes else 78858f0484fSRodney W. Grimes finish = p_b_symbol(p); 78958f0484fSRodney W. Grimes } else 79058f0484fSRodney W. Grimes finish = start; 7915c551438SAndrey A. Chernov if (start == finish) 792e5996857STim J. Robbins CHadd(p, cs, start); 7935c551438SAndrey A. Chernov else { 794b5a6eb18SAndrey A. Chernov if (__collate_load_error) { 795b5a6eb18SAndrey A. Chernov (void)REQUIRE((uch)start <= (uch)finish, REG_ERANGE); 796e5996857STim J. Robbins CHaddrange(p, cs, start, finish); 797b5a6eb18SAndrey A. Chernov } else { 798c61cea72SAndrey A. Chernov (void)REQUIRE(__collate_range_cmp(start, finish) <= 0, REG_ERANGE); 799e5996857STim J. Robbins for (i = 0; i <= UCHAR_MAX; i++) { 800c61cea72SAndrey A. Chernov if ( __collate_range_cmp(start, i) <= 0 801c61cea72SAndrey A. Chernov && __collate_range_cmp(i, finish) <= 0 8025c551438SAndrey A. Chernov ) 803e5996857STim J. Robbins CHadd(p, cs, i); 8045c551438SAndrey A. Chernov } 8055c551438SAndrey A. Chernov } 806b5a6eb18SAndrey A. Chernov } 80758f0484fSRodney W. Grimes break; 80858f0484fSRodney W. Grimes } 80958f0484fSRodney W. Grimes } 81058f0484fSRodney W. Grimes 81158f0484fSRodney W. Grimes /* 81258f0484fSRodney W. Grimes - p_b_cclass - parse a character-class name and deal with it 8138fb3f3f6SDavid E. O'Brien == static void p_b_cclass(struct parse *p, cset *cs); 81458f0484fSRodney W. Grimes */ 81558f0484fSRodney W. Grimes static void 81658f0484fSRodney W. Grimes p_b_cclass(p, cs) 8178fb3f3f6SDavid E. O'Brien struct parse *p; 8188fb3f3f6SDavid E. O'Brien cset *cs; 81958f0484fSRodney W. Grimes { 8208fb3f3f6SDavid E. O'Brien char *sp = p->next; 8218fb3f3f6SDavid E. O'Brien size_t len; 822e5996857STim J. Robbins wctype_t wct; 823e5996857STim J. Robbins char clname[16]; 82458f0484fSRodney W. Grimes 825b5363c4aSAndrey A. Chernov while (MORE() && isalpha((uch)PEEK())) 82658f0484fSRodney W. Grimes NEXT(); 82758f0484fSRodney W. Grimes len = p->next - sp; 828e5996857STim J. Robbins if (len >= sizeof(clname) - 1) { 82958f0484fSRodney W. Grimes SETERROR(REG_ECTYPE); 83058f0484fSRodney W. Grimes return; 83158f0484fSRodney W. Grimes } 832e5996857STim J. Robbins memcpy(clname, sp, len); 833e5996857STim J. Robbins clname[len] = '\0'; 834e5996857STim J. Robbins if ((wct = wctype(clname)) == 0) { 835e5996857STim J. Robbins SETERROR(REG_ECTYPE); 836e5996857STim J. Robbins return; 837b5363c4aSAndrey A. Chernov } 838e5996857STim J. Robbins CHaddtype(p, cs, wct); 83958f0484fSRodney W. Grimes } 84058f0484fSRodney W. Grimes 84158f0484fSRodney W. Grimes /* 84258f0484fSRodney W. Grimes - p_b_eclass - parse an equivalence-class name and deal with it 8438fb3f3f6SDavid E. O'Brien == static void p_b_eclass(struct parse *p, cset *cs); 84458f0484fSRodney W. Grimes * 84558f0484fSRodney W. Grimes * This implementation is incomplete. xxx 84658f0484fSRodney W. Grimes */ 84758f0484fSRodney W. Grimes static void 84858f0484fSRodney W. Grimes p_b_eclass(p, cs) 8498fb3f3f6SDavid E. O'Brien struct parse *p; 8508fb3f3f6SDavid E. O'Brien cset *cs; 85158f0484fSRodney W. Grimes { 852e5996857STim J. Robbins wint_t c; 85358f0484fSRodney W. Grimes 85458f0484fSRodney W. Grimes c = p_b_coll_elem(p, '='); 855e5996857STim J. Robbins CHadd(p, cs, c); 85658f0484fSRodney W. Grimes } 85758f0484fSRodney W. Grimes 85858f0484fSRodney W. Grimes /* 85958f0484fSRodney W. Grimes - p_b_symbol - parse a character or [..]ed multicharacter collating symbol 8608fb3f3f6SDavid E. O'Brien == static char p_b_symbol(struct parse *p); 86158f0484fSRodney W. Grimes */ 862e5996857STim J. Robbins static wint_t /* value of symbol */ 86358f0484fSRodney W. Grimes p_b_symbol(p) 8648fb3f3f6SDavid E. O'Brien struct parse *p; 86558f0484fSRodney W. Grimes { 866e5996857STim J. Robbins wint_t value; 86758f0484fSRodney W. Grimes 86851295a4dSJordan K. Hubbard (void)REQUIRE(MORE(), REG_EBRACK); 86958f0484fSRodney W. Grimes if (!EATTWO('[', '.')) 870e5996857STim J. Robbins return(WGETNEXT()); 87158f0484fSRodney W. Grimes 87258f0484fSRodney W. Grimes /* collating symbol */ 87358f0484fSRodney W. Grimes value = p_b_coll_elem(p, '.'); 87451295a4dSJordan K. Hubbard (void)REQUIRE(EATTWO('.', ']'), REG_ECOLLATE); 87558f0484fSRodney W. Grimes return(value); 87658f0484fSRodney W. Grimes } 87758f0484fSRodney W. Grimes 87858f0484fSRodney W. Grimes /* 87958f0484fSRodney W. Grimes - p_b_coll_elem - parse a collating-element name and look it up 8808fb3f3f6SDavid E. O'Brien == static char p_b_coll_elem(struct parse *p, int endc); 88158f0484fSRodney W. Grimes */ 882e5996857STim J. Robbins static wint_t /* value of collating element */ 88358f0484fSRodney W. Grimes p_b_coll_elem(p, endc) 8848fb3f3f6SDavid E. O'Brien struct parse *p; 885e5996857STim J. Robbins wint_t endc; /* name ended by endc,']' */ 88658f0484fSRodney W. Grimes { 8878fb3f3f6SDavid E. O'Brien char *sp = p->next; 8888fb3f3f6SDavid E. O'Brien struct cname *cp; 8898fb3f3f6SDavid E. O'Brien int len; 890e5996857STim J. Robbins mbstate_t mbs; 891e5996857STim J. Robbins wchar_t wc; 892e5996857STim J. Robbins size_t clen; 89358f0484fSRodney W. Grimes 89458f0484fSRodney W. Grimes while (MORE() && !SEETWO(endc, ']')) 89558f0484fSRodney W. Grimes NEXT(); 89658f0484fSRodney W. Grimes if (!MORE()) { 89758f0484fSRodney W. Grimes SETERROR(REG_EBRACK); 89858f0484fSRodney W. Grimes return(0); 89958f0484fSRodney W. Grimes } 90058f0484fSRodney W. Grimes len = p->next - sp; 90158f0484fSRodney W. Grimes for (cp = cnames; cp->name != NULL; cp++) 90258f0484fSRodney W. Grimes if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0') 90358f0484fSRodney W. Grimes return(cp->code); /* known name */ 904e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 905e5996857STim J. Robbins if ((clen = mbrtowc(&wc, sp, len, &mbs)) == len) 906e5996857STim J. Robbins return (wc); /* single character */ 907e5996857STim J. Robbins else if (clen == (size_t)-1 || clen == (size_t)-2) 908e5996857STim J. Robbins SETERROR(REG_ILLSEQ); 909e5996857STim J. Robbins else 91058f0484fSRodney W. Grimes SETERROR(REG_ECOLLATE); /* neither */ 91158f0484fSRodney W. Grimes return(0); 91258f0484fSRodney W. Grimes } 91358f0484fSRodney W. Grimes 91458f0484fSRodney W. Grimes /* 91558f0484fSRodney W. Grimes - othercase - return the case counterpart of an alphabetic 91658f0484fSRodney W. Grimes == static char othercase(int ch); 91758f0484fSRodney W. Grimes */ 918e5996857STim J. Robbins static wint_t /* if no counterpart, return ch */ 91958f0484fSRodney W. Grimes othercase(ch) 920e5996857STim J. Robbins wint_t ch; 92158f0484fSRodney W. Grimes { 922e5996857STim J. Robbins assert(iswalpha(ch)); 923e5996857STim J. Robbins if (iswupper(ch)) 924e5996857STim J. Robbins return(towlower(ch)); 925e5996857STim J. Robbins else if (iswlower(ch)) 926e5996857STim J. Robbins return(towupper(ch)); 92758f0484fSRodney W. Grimes else /* peculiar, but could happen */ 92858f0484fSRodney W. Grimes return(ch); 92958f0484fSRodney W. Grimes } 93058f0484fSRodney W. Grimes 93158f0484fSRodney W. Grimes /* 93258f0484fSRodney W. Grimes - bothcases - emit a dualcase version of a two-case character 9338fb3f3f6SDavid E. O'Brien == static void bothcases(struct parse *p, int ch); 93458f0484fSRodney W. Grimes * 93558f0484fSRodney W. Grimes * Boy, is this implementation ever a kludge... 93658f0484fSRodney W. Grimes */ 93758f0484fSRodney W. Grimes static void 93858f0484fSRodney W. Grimes bothcases(p, ch) 9398fb3f3f6SDavid E. O'Brien struct parse *p; 940e5996857STim J. Robbins wint_t ch; 94158f0484fSRodney W. Grimes { 9428fb3f3f6SDavid E. O'Brien char *oldnext = p->next; 9438fb3f3f6SDavid E. O'Brien char *oldend = p->end; 944e5996857STim J. Robbins char bracket[3 + MB_LEN_MAX]; 945e5996857STim J. Robbins size_t n; 946e5996857STim J. Robbins mbstate_t mbs; 94758f0484fSRodney W. Grimes 94858f0484fSRodney W. Grimes assert(othercase(ch) != ch); /* p_bracket() would recurse */ 94958f0484fSRodney W. Grimes p->next = bracket; 950e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 951e5996857STim J. Robbins n = wcrtomb(bracket, ch, &mbs); 952e5996857STim J. Robbins assert(n != (size_t)-1); 953e5996857STim J. Robbins bracket[n] = ']'; 954e5996857STim J. Robbins bracket[n + 1] = '\0'; 955e5996857STim J. Robbins p->end = bracket+n+1; 95658f0484fSRodney W. Grimes p_bracket(p); 957e5996857STim J. Robbins assert(p->next == p->end); 95858f0484fSRodney W. Grimes p->next = oldnext; 95958f0484fSRodney W. Grimes p->end = oldend; 96058f0484fSRodney W. Grimes } 96158f0484fSRodney W. Grimes 96258f0484fSRodney W. Grimes /* 96358f0484fSRodney W. Grimes - ordinary - emit an ordinary character 9648fb3f3f6SDavid E. O'Brien == static void ordinary(struct parse *p, int ch); 96558f0484fSRodney W. Grimes */ 96658f0484fSRodney W. Grimes static void 96758f0484fSRodney W. Grimes ordinary(p, ch) 9688fb3f3f6SDavid E. O'Brien struct parse *p; 969e5996857STim J. Robbins wint_t ch; 97058f0484fSRodney W. Grimes { 971e5996857STim J. Robbins cset *cs; 97258f0484fSRodney W. Grimes 973e5996857STim J. Robbins if ((p->g->cflags®_ICASE) && iswalpha(ch) && othercase(ch) != ch) 97458f0484fSRodney W. Grimes bothcases(p, ch); 975e5996857STim J. Robbins else if ((ch & OPDMASK) == ch) 976e5996857STim J. Robbins EMIT(OCHAR, ch); 977e5996857STim J. Robbins else { 978e5996857STim J. Robbins /* 979e5996857STim J. Robbins * Kludge: character is too big to fit into an OCHAR operand. 980e5996857STim J. Robbins * Emit a singleton set. 981e5996857STim J. Robbins */ 982e5996857STim J. Robbins if ((cs = allocset(p)) == NULL) 983e5996857STim J. Robbins return; 984e5996857STim J. Robbins CHadd(p, cs, ch); 985e5996857STim J. Robbins EMIT(OANYOF, (int)(cs - p->g->sets)); 986e5996857STim J. Robbins } 98758f0484fSRodney W. Grimes } 98858f0484fSRodney W. Grimes 98958f0484fSRodney W. Grimes /* 99058f0484fSRodney W. Grimes - nonnewline - emit REG_NEWLINE version of OANY 9918fb3f3f6SDavid E. O'Brien == static void nonnewline(struct parse *p); 99258f0484fSRodney W. Grimes * 99358f0484fSRodney W. Grimes * Boy, is this implementation ever a kludge... 99458f0484fSRodney W. Grimes */ 99558f0484fSRodney W. Grimes static void 99658f0484fSRodney W. Grimes nonnewline(p) 9978fb3f3f6SDavid E. O'Brien struct parse *p; 99858f0484fSRodney W. Grimes { 9998fb3f3f6SDavid E. O'Brien char *oldnext = p->next; 10008fb3f3f6SDavid E. O'Brien char *oldend = p->end; 100158f0484fSRodney W. Grimes char bracket[4]; 100258f0484fSRodney W. Grimes 100358f0484fSRodney W. Grimes p->next = bracket; 100458f0484fSRodney W. Grimes p->end = bracket+3; 100558f0484fSRodney W. Grimes bracket[0] = '^'; 100658f0484fSRodney W. Grimes bracket[1] = '\n'; 100758f0484fSRodney W. Grimes bracket[2] = ']'; 100858f0484fSRodney W. Grimes bracket[3] = '\0'; 100958f0484fSRodney W. Grimes p_bracket(p); 101058f0484fSRodney W. Grimes assert(p->next == bracket+3); 101158f0484fSRodney W. Grimes p->next = oldnext; 101258f0484fSRodney W. Grimes p->end = oldend; 101358f0484fSRodney W. Grimes } 101458f0484fSRodney W. Grimes 101558f0484fSRodney W. Grimes /* 101658f0484fSRodney W. Grimes - repeat - generate code for a bounded repetition, recursively if needed 10178fb3f3f6SDavid E. O'Brien == static void repeat(struct parse *p, sopno start, int from, int to); 101858f0484fSRodney W. Grimes */ 101958f0484fSRodney W. Grimes static void 102058f0484fSRodney W. Grimes repeat(p, start, from, to) 10218fb3f3f6SDavid E. O'Brien struct parse *p; 102258f0484fSRodney W. Grimes sopno start; /* operand from here to end of strip */ 102358f0484fSRodney W. Grimes int from; /* repeated from this number */ 102458f0484fSRodney W. Grimes int to; /* to this number of times (maybe INFINITY) */ 102558f0484fSRodney W. Grimes { 10268fb3f3f6SDavid E. O'Brien sopno finish = HERE(); 102758f0484fSRodney W. Grimes # define N 2 102858f0484fSRodney W. Grimes # define INF 3 102958f0484fSRodney W. Grimes # define REP(f, t) ((f)*8 + (t)) 103058f0484fSRodney W. Grimes # define MAP(n) (((n) <= 1) ? (n) : ((n) == INFINITY) ? INF : N) 10318fb3f3f6SDavid E. O'Brien sopno copy; 103258f0484fSRodney W. Grimes 103358f0484fSRodney W. Grimes if (p->error != 0) /* head off possible runaway recursion */ 103458f0484fSRodney W. Grimes return; 103558f0484fSRodney W. Grimes 103658f0484fSRodney W. Grimes assert(from <= to); 103758f0484fSRodney W. Grimes 103858f0484fSRodney W. Grimes switch (REP(MAP(from), MAP(to))) { 103958f0484fSRodney W. Grimes case REP(0, 0): /* must be user doing this */ 104058f0484fSRodney W. Grimes DROP(finish-start); /* drop the operand */ 104158f0484fSRodney W. Grimes break; 104258f0484fSRodney W. Grimes case REP(0, 1): /* as x{1,1}? */ 104358f0484fSRodney W. Grimes case REP(0, N): /* as x{1,n}? */ 104458f0484fSRodney W. Grimes case REP(0, INF): /* as x{1,}? */ 104558f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 104658f0484fSRodney W. Grimes INSERT(OCH_, start); /* offset is wrong... */ 104758f0484fSRodney W. Grimes repeat(p, start+1, 1, to); 104858f0484fSRodney W. Grimes ASTERN(OOR1, start); 104958f0484fSRodney W. Grimes AHEAD(start); /* ... fix it */ 105058f0484fSRodney W. Grimes EMIT(OOR2, 0); 105158f0484fSRodney W. Grimes AHEAD(THERE()); 105258f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 105358f0484fSRodney W. Grimes break; 105458f0484fSRodney W. Grimes case REP(1, 1): /* trivial case */ 105558f0484fSRodney W. Grimes /* done */ 105658f0484fSRodney W. Grimes break; 105758f0484fSRodney W. Grimes case REP(1, N): /* as x?x{1,n-1} */ 105858f0484fSRodney W. Grimes /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */ 105958f0484fSRodney W. Grimes INSERT(OCH_, start); 106058f0484fSRodney W. Grimes ASTERN(OOR1, start); 106158f0484fSRodney W. Grimes AHEAD(start); 106258f0484fSRodney W. Grimes EMIT(OOR2, 0); /* offset very wrong... */ 106358f0484fSRodney W. Grimes AHEAD(THERE()); /* ...so fix it */ 106458f0484fSRodney W. Grimes ASTERN(O_CH, THERETHERE()); 106558f0484fSRodney W. Grimes copy = dupl(p, start+1, finish+1); 106658f0484fSRodney W. Grimes assert(copy == finish+4); 106758f0484fSRodney W. Grimes repeat(p, copy, 1, to-1); 106858f0484fSRodney W. Grimes break; 106958f0484fSRodney W. Grimes case REP(1, INF): /* as x+ */ 107058f0484fSRodney W. Grimes INSERT(OPLUS_, start); 107158f0484fSRodney W. Grimes ASTERN(O_PLUS, start); 107258f0484fSRodney W. Grimes break; 107358f0484fSRodney W. Grimes case REP(N, N): /* as xx{m-1,n-1} */ 107458f0484fSRodney W. Grimes copy = dupl(p, start, finish); 107558f0484fSRodney W. Grimes repeat(p, copy, from-1, to-1); 107658f0484fSRodney W. Grimes break; 107758f0484fSRodney W. Grimes case REP(N, INF): /* as xx{n-1,INF} */ 107858f0484fSRodney W. Grimes copy = dupl(p, start, finish); 107958f0484fSRodney W. Grimes repeat(p, copy, from-1, to); 108058f0484fSRodney W. Grimes break; 108158f0484fSRodney W. Grimes default: /* "can't happen" */ 108258f0484fSRodney W. Grimes SETERROR(REG_ASSERT); /* just in case */ 108358f0484fSRodney W. Grimes break; 108458f0484fSRodney W. Grimes } 108558f0484fSRodney W. Grimes } 108658f0484fSRodney W. Grimes 108758f0484fSRodney W. Grimes /* 1088e5996857STim J. Robbins - wgetnext - helper function for WGETNEXT() macro. Gets the next wide 1089e5996857STim J. Robbins - character from the parse struct, signals a REG_ILLSEQ error if the 1090e5996857STim J. Robbins - character can't be converted. Returns the number of bytes consumed. 1091e5996857STim J. Robbins */ 1092e5996857STim J. Robbins static wint_t 1093e5996857STim J. Robbins wgetnext(p) 1094e5996857STim J. Robbins struct parse *p; 1095e5996857STim J. Robbins { 1096e5996857STim J. Robbins mbstate_t mbs; 1097e5996857STim J. Robbins wchar_t wc; 1098e5996857STim J. Robbins size_t n; 1099e5996857STim J. Robbins 1100e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1101e5996857STim J. Robbins n = mbrtowc(&wc, p->next, p->end - p->next, &mbs); 1102e5996857STim J. Robbins if (n == (size_t)-1 || n == (size_t)-2) { 1103e5996857STim J. Robbins SETERROR(REG_ILLSEQ); 1104e5996857STim J. Robbins return (0); 1105e5996857STim J. Robbins } 1106e5996857STim J. Robbins if (n == 0) 1107e5996857STim J. Robbins n = 1; 1108e5996857STim J. Robbins p->next += n; 1109e5996857STim J. Robbins return (wc); 1110e5996857STim J. Robbins } 1111e5996857STim J. Robbins 1112e5996857STim J. Robbins /* 111358f0484fSRodney W. Grimes - seterr - set an error condition 11148fb3f3f6SDavid E. O'Brien == static int seterr(struct parse *p, int e); 111558f0484fSRodney W. Grimes */ 111658f0484fSRodney W. Grimes static int /* useless but makes type checking happy */ 111758f0484fSRodney W. Grimes seterr(p, e) 11188fb3f3f6SDavid E. O'Brien struct parse *p; 111958f0484fSRodney W. Grimes int e; 112058f0484fSRodney W. Grimes { 112158f0484fSRodney W. Grimes if (p->error == 0) /* keep earliest error condition */ 112258f0484fSRodney W. Grimes p->error = e; 112358f0484fSRodney W. Grimes p->next = nuls; /* try to bring things to a halt */ 112458f0484fSRodney W. Grimes p->end = nuls; 112558f0484fSRodney W. Grimes return(0); /* make the return value well-defined */ 112658f0484fSRodney W. Grimes } 112758f0484fSRodney W. Grimes 112858f0484fSRodney W. Grimes /* 112958f0484fSRodney W. Grimes - allocset - allocate a set of characters for [] 11308fb3f3f6SDavid E. O'Brien == static cset *allocset(struct parse *p); 113158f0484fSRodney W. Grimes */ 113258f0484fSRodney W. Grimes static cset * 113358f0484fSRodney W. Grimes allocset(p) 11348fb3f3f6SDavid E. O'Brien struct parse *p; 113558f0484fSRodney W. Grimes { 1136e5996857STim J. Robbins cset *cs, *ncs; 113758f0484fSRodney W. Grimes 1138e5996857STim J. Robbins ncs = realloc(p->g->sets, (p->g->ncsets + 1) * sizeof(*ncs)); 1139e5996857STim J. Robbins if (ncs == NULL) { 114058f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 1141e5996857STim J. Robbins return (NULL); 114258f0484fSRodney W. Grimes } 1143e5996857STim J. Robbins p->g->sets = ncs; 1144e5996857STim J. Robbins cs = &p->g->sets[p->g->ncsets++]; 1145e5996857STim J. Robbins memset(cs, 0, sizeof(*cs)); 114658f0484fSRodney W. Grimes 114758f0484fSRodney W. Grimes return(cs); 114858f0484fSRodney W. Grimes } 114958f0484fSRodney W. Grimes 115058f0484fSRodney W. Grimes /* 115158f0484fSRodney W. Grimes - freeset - free a now-unused set 11528fb3f3f6SDavid E. O'Brien == static void freeset(struct parse *p, cset *cs); 115358f0484fSRodney W. Grimes */ 115458f0484fSRodney W. Grimes static void 115558f0484fSRodney W. Grimes freeset(p, cs) 11568fb3f3f6SDavid E. O'Brien struct parse *p; 11578fb3f3f6SDavid E. O'Brien cset *cs; 115858f0484fSRodney W. Grimes { 11598fb3f3f6SDavid E. O'Brien cset *top = &p->g->sets[p->g->ncsets]; 116058f0484fSRodney W. Grimes 1161e5996857STim J. Robbins free(cs->wides); 1162e5996857STim J. Robbins free(cs->ranges); 1163e5996857STim J. Robbins free(cs->types); 1164e5996857STim J. Robbins memset(cs, 0, sizeof(*cs)); 116558f0484fSRodney W. Grimes if (cs == top-1) /* recover only the easy case */ 116658f0484fSRodney W. Grimes p->g->ncsets--; 116758f0484fSRodney W. Grimes } 116858f0484fSRodney W. Grimes 116958f0484fSRodney W. Grimes /* 1170e5996857STim J. Robbins - singleton - Determine whether a set contains only one character, 1171e5996857STim J. Robbins - returning it if so, otherwise returning OUT. 117258f0484fSRodney W. Grimes */ 1173e5996857STim J. Robbins static wint_t 1174e5996857STim J. Robbins singleton(cs) 11758fb3f3f6SDavid E. O'Brien cset *cs; 117658f0484fSRodney W. Grimes { 1177e5996857STim J. Robbins wint_t i, s, n; 117858f0484fSRodney W. Grimes 1179e5996857STim J. Robbins for (i = n = 0; i < NC; i++) 1180e5996857STim J. Robbins if (CHIN(cs, i)) { 118158f0484fSRodney W. Grimes n++; 1182e5996857STim J. Robbins s = i; 1183e5996857STim J. Robbins } 1184e5996857STim J. Robbins if (n == 1) 1185e5996857STim J. Robbins return (s); 1186e5996857STim J. Robbins if (cs->nwides == 1 && cs->nranges == 0 && cs->ntypes == 0 && 1187e5996857STim J. Robbins cs->icase == 0) 1188e5996857STim J. Robbins return (cs->wides[0]); 1189e5996857STim J. Robbins /* Don't bother handling the other cases. */ 1190e5996857STim J. Robbins return (OUT); 1191e5996857STim J. Robbins } 1192e5996857STim J. Robbins 1193e5996857STim J. Robbins /* 1194e5996857STim J. Robbins - CHadd - add character to character set. 1195e5996857STim J. Robbins */ 1196e5996857STim J. Robbins static void 1197e5996857STim J. Robbins CHadd(p, cs, ch) 1198e5996857STim J. Robbins struct parse *p; 1199e5996857STim J. Robbins cset *cs; 1200e5996857STim J. Robbins wint_t ch; 1201e5996857STim J. Robbins { 120233176f17STim J. Robbins wint_t nch, *newwides; 1203e5996857STim J. Robbins assert(ch >= 0); 120433176f17STim J. Robbins if (ch < NC) 1205e5996857STim J. Robbins cs->bmp[ch >> 3] |= 1 << (ch & 7); 120633176f17STim J. Robbins else { 1207e5996857STim J. Robbins newwides = realloc(cs->wides, (cs->nwides + 1) * 1208e5996857STim J. Robbins sizeof(*cs->wides)); 1209e5996857STim J. Robbins if (newwides == NULL) { 1210e5996857STim J. Robbins SETERROR(REG_ESPACE); 1211e5996857STim J. Robbins return; 1212e5996857STim J. Robbins } 1213e5996857STim J. Robbins cs->wides = newwides; 1214e5996857STim J. Robbins cs->wides[cs->nwides++] = ch; 1215e5996857STim J. Robbins } 121633176f17STim J. Robbins if (cs->icase) { 121733176f17STim J. Robbins if ((nch = towlower(ch)) < NC) 121833176f17STim J. Robbins cs->bmp[nch >> 3] |= 1 << (nch & 7); 121933176f17STim J. Robbins if ((nch = towupper(ch)) < NC) 122033176f17STim J. Robbins cs->bmp[nch >> 3] |= 1 << (nch & 7); 122133176f17STim J. Robbins } 1222e5996857STim J. Robbins } 1223e5996857STim J. Robbins 1224e5996857STim J. Robbins /* 1225e5996857STim J. Robbins - CHaddrange - add all characters in the range [min,max] to a character set. 1226e5996857STim J. Robbins */ 1227e5996857STim J. Robbins static void 1228e5996857STim J. Robbins CHaddrange(p, cs, min, max) 1229e5996857STim J. Robbins struct parse *p; 1230e5996857STim J. Robbins cset *cs; 1231e5996857STim J. Robbins wint_t min, max; 1232e5996857STim J. Robbins { 1233e5996857STim J. Robbins crange *newranges; 1234e5996857STim J. Robbins 1235e5996857STim J. Robbins for (; min < NC && min <= max; min++) 1236e5996857STim J. Robbins CHadd(p, cs, min); 1237e5996857STim J. Robbins if (min >= max) 1238e5996857STim J. Robbins return; 1239e5996857STim J. Robbins newranges = realloc(cs->ranges, (cs->nranges + 1) * 1240e5996857STim J. Robbins sizeof(*cs->ranges)); 1241e5996857STim J. Robbins if (newranges == NULL) { 1242e5996857STim J. Robbins SETERROR(REG_ESPACE); 1243e5996857STim J. Robbins return; 1244e5996857STim J. Robbins } 1245e5996857STim J. Robbins cs->ranges = newranges; 1246e5996857STim J. Robbins cs->ranges[cs->nranges].min = min; 1247e5996857STim J. Robbins cs->ranges[cs->nranges].min = max; 1248e5996857STim J. Robbins cs->nranges++; 1249e5996857STim J. Robbins } 1250e5996857STim J. Robbins 1251e5996857STim J. Robbins /* 1252e5996857STim J. Robbins - CHaddtype - add all characters of a certain type to a character set. 1253e5996857STim J. Robbins */ 1254e5996857STim J. Robbins static void 1255e5996857STim J. Robbins CHaddtype(p, cs, wct) 1256e5996857STim J. Robbins struct parse *p; 1257e5996857STim J. Robbins cset *cs; 1258e5996857STim J. Robbins wctype_t wct; 1259e5996857STim J. Robbins { 1260e5996857STim J. Robbins wint_t i; 1261e5996857STim J. Robbins wctype_t *newtypes; 1262e5996857STim J. Robbins 126333176f17STim J. Robbins for (i = 0; i < NC; i++) 1264e5996857STim J. Robbins if (iswctype(i, wct)) 1265e5996857STim J. Robbins CHadd(p, cs, i); 1266e5996857STim J. Robbins newtypes = realloc(cs->types, (cs->ntypes + 1) * 1267e5996857STim J. Robbins sizeof(*cs->types)); 1268e5996857STim J. Robbins if (newtypes == NULL) { 1269e5996857STim J. Robbins SETERROR(REG_ESPACE); 1270e5996857STim J. Robbins return; 1271e5996857STim J. Robbins } 1272e5996857STim J. Robbins cs->types = newtypes; 1273e5996857STim J. Robbins cs->types[cs->ntypes++] = wct; 127458f0484fSRodney W. Grimes } 127558f0484fSRodney W. Grimes 127658f0484fSRodney W. Grimes /* 127758f0484fSRodney W. Grimes - dupl - emit a duplicate of a bunch of sops 12788fb3f3f6SDavid E. O'Brien == static sopno dupl(struct parse *p, sopno start, sopno finish); 127958f0484fSRodney W. Grimes */ 128058f0484fSRodney W. Grimes static sopno /* start of duplicate */ 128158f0484fSRodney W. Grimes dupl(p, start, finish) 12828fb3f3f6SDavid E. O'Brien struct parse *p; 128358f0484fSRodney W. Grimes sopno start; /* from here */ 128458f0484fSRodney W. Grimes sopno finish; /* to this less one */ 128558f0484fSRodney W. Grimes { 12868fb3f3f6SDavid E. O'Brien sopno ret = HERE(); 12878fb3f3f6SDavid E. O'Brien sopno len = finish - start; 128858f0484fSRodney W. Grimes 128958f0484fSRodney W. Grimes assert(finish >= start); 129058f0484fSRodney W. Grimes if (len == 0) 129158f0484fSRodney W. Grimes return(ret); 129258f0484fSRodney W. Grimes enlarge(p, p->ssize + len); /* this many unexpected additions */ 129358f0484fSRodney W. Grimes assert(p->ssize >= p->slen + len); 129458f0484fSRodney W. Grimes (void) memcpy((char *)(p->strip + p->slen), 129558f0484fSRodney W. Grimes (char *)(p->strip + start), (size_t)len*sizeof(sop)); 129658f0484fSRodney W. Grimes p->slen += len; 129758f0484fSRodney W. Grimes return(ret); 129858f0484fSRodney W. Grimes } 129958f0484fSRodney W. Grimes 130058f0484fSRodney W. Grimes /* 130158f0484fSRodney W. Grimes - doemit - emit a strip operator 13028fb3f3f6SDavid E. O'Brien == static void doemit(struct parse *p, sop op, size_t opnd); 130358f0484fSRodney W. Grimes * 130458f0484fSRodney W. Grimes * It might seem better to implement this as a macro with a function as 130558f0484fSRodney W. Grimes * hard-case backup, but it's just too big and messy unless there are 130658f0484fSRodney W. Grimes * some changes to the data structures. Maybe later. 130758f0484fSRodney W. Grimes */ 130858f0484fSRodney W. Grimes static void 130958f0484fSRodney W. Grimes doemit(p, op, opnd) 13108fb3f3f6SDavid E. O'Brien struct parse *p; 131158f0484fSRodney W. Grimes sop op; 131258f0484fSRodney W. Grimes size_t opnd; 131358f0484fSRodney W. Grimes { 131458f0484fSRodney W. Grimes /* avoid making error situations worse */ 131558f0484fSRodney W. Grimes if (p->error != 0) 131658f0484fSRodney W. Grimes return; 131758f0484fSRodney W. Grimes 131858f0484fSRodney W. Grimes /* deal with oversize operands ("can't happen", more or less) */ 131958f0484fSRodney W. Grimes assert(opnd < 1<<OPSHIFT); 132058f0484fSRodney W. Grimes 132158f0484fSRodney W. Grimes /* deal with undersized strip */ 132258f0484fSRodney W. Grimes if (p->slen >= p->ssize) 132358f0484fSRodney W. Grimes enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */ 132458f0484fSRodney W. Grimes assert(p->slen < p->ssize); 132558f0484fSRodney W. Grimes 132658f0484fSRodney W. Grimes /* finally, it's all reduced to the easy case */ 132758f0484fSRodney W. Grimes p->strip[p->slen++] = SOP(op, opnd); 132858f0484fSRodney W. Grimes } 132958f0484fSRodney W. Grimes 133058f0484fSRodney W. Grimes /* 133158f0484fSRodney W. Grimes - doinsert - insert a sop into the strip 13328fb3f3f6SDavid E. O'Brien == static void doinsert(struct parse *p, sop op, size_t opnd, sopno pos); 133358f0484fSRodney W. Grimes */ 133458f0484fSRodney W. Grimes static void 133558f0484fSRodney W. Grimes doinsert(p, op, opnd, pos) 13368fb3f3f6SDavid E. O'Brien struct parse *p; 133758f0484fSRodney W. Grimes sop op; 133858f0484fSRodney W. Grimes size_t opnd; 133958f0484fSRodney W. Grimes sopno pos; 134058f0484fSRodney W. Grimes { 13418fb3f3f6SDavid E. O'Brien sopno sn; 13428fb3f3f6SDavid E. O'Brien sop s; 13438fb3f3f6SDavid E. O'Brien int i; 134458f0484fSRodney W. Grimes 134558f0484fSRodney W. Grimes /* avoid making error situations worse */ 134658f0484fSRodney W. Grimes if (p->error != 0) 134758f0484fSRodney W. Grimes return; 134858f0484fSRodney W. Grimes 134958f0484fSRodney W. Grimes sn = HERE(); 135058f0484fSRodney W. Grimes EMIT(op, opnd); /* do checks, ensure space */ 135158f0484fSRodney W. Grimes assert(HERE() == sn+1); 135258f0484fSRodney W. Grimes s = p->strip[sn]; 135358f0484fSRodney W. Grimes 135458f0484fSRodney W. Grimes /* adjust paren pointers */ 135558f0484fSRodney W. Grimes assert(pos > 0); 135658f0484fSRodney W. Grimes for (i = 1; i < NPAREN; i++) { 135758f0484fSRodney W. Grimes if (p->pbegin[i] >= pos) { 135858f0484fSRodney W. Grimes p->pbegin[i]++; 135958f0484fSRodney W. Grimes } 136058f0484fSRodney W. Grimes if (p->pend[i] >= pos) { 136158f0484fSRodney W. Grimes p->pend[i]++; 136258f0484fSRodney W. Grimes } 136358f0484fSRodney W. Grimes } 136458f0484fSRodney W. Grimes 136558f0484fSRodney W. Grimes memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos], 136658f0484fSRodney W. Grimes (HERE()-pos-1)*sizeof(sop)); 136758f0484fSRodney W. Grimes p->strip[pos] = s; 136858f0484fSRodney W. Grimes } 136958f0484fSRodney W. Grimes 137058f0484fSRodney W. Grimes /* 137158f0484fSRodney W. Grimes - dofwd - complete a forward reference 13728fb3f3f6SDavid E. O'Brien == static void dofwd(struct parse *p, sopno pos, sop value); 137358f0484fSRodney W. Grimes */ 137458f0484fSRodney W. Grimes static void 137558f0484fSRodney W. Grimes dofwd(p, pos, value) 13768fb3f3f6SDavid E. O'Brien struct parse *p; 13778fb3f3f6SDavid E. O'Brien sopno pos; 137858f0484fSRodney W. Grimes sop value; 137958f0484fSRodney W. Grimes { 138058f0484fSRodney W. Grimes /* avoid making error situations worse */ 138158f0484fSRodney W. Grimes if (p->error != 0) 138258f0484fSRodney W. Grimes return; 138358f0484fSRodney W. Grimes 138458f0484fSRodney W. Grimes assert(value < 1<<OPSHIFT); 138558f0484fSRodney W. Grimes p->strip[pos] = OP(p->strip[pos]) | value; 138658f0484fSRodney W. Grimes } 138758f0484fSRodney W. Grimes 138858f0484fSRodney W. Grimes /* 138958f0484fSRodney W. Grimes - enlarge - enlarge the strip 13908fb3f3f6SDavid E. O'Brien == static void enlarge(struct parse *p, sopno size); 139158f0484fSRodney W. Grimes */ 139258f0484fSRodney W. Grimes static void 139358f0484fSRodney W. Grimes enlarge(p, size) 13948fb3f3f6SDavid E. O'Brien struct parse *p; 13958fb3f3f6SDavid E. O'Brien sopno size; 139658f0484fSRodney W. Grimes { 13978fb3f3f6SDavid E. O'Brien sop *sp; 139858f0484fSRodney W. Grimes 139958f0484fSRodney W. Grimes if (p->ssize >= size) 140058f0484fSRodney W. Grimes return; 140158f0484fSRodney W. Grimes 140258f0484fSRodney W. Grimes sp = (sop *)realloc(p->strip, size*sizeof(sop)); 140358f0484fSRodney W. Grimes if (sp == NULL) { 140458f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 140558f0484fSRodney W. Grimes return; 140658f0484fSRodney W. Grimes } 140758f0484fSRodney W. Grimes p->strip = sp; 140858f0484fSRodney W. Grimes p->ssize = size; 140958f0484fSRodney W. Grimes } 141058f0484fSRodney W. Grimes 141158f0484fSRodney W. Grimes /* 141258f0484fSRodney W. Grimes - stripsnug - compact the strip 14138fb3f3f6SDavid E. O'Brien == static void stripsnug(struct parse *p, struct re_guts *g); 141458f0484fSRodney W. Grimes */ 141558f0484fSRodney W. Grimes static void 141658f0484fSRodney W. Grimes stripsnug(p, g) 14178fb3f3f6SDavid E. O'Brien struct parse *p; 14188fb3f3f6SDavid E. O'Brien struct re_guts *g; 141958f0484fSRodney W. Grimes { 142058f0484fSRodney W. Grimes g->nstates = p->slen; 142158f0484fSRodney W. Grimes g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop)); 142258f0484fSRodney W. Grimes if (g->strip == NULL) { 142358f0484fSRodney W. Grimes SETERROR(REG_ESPACE); 142458f0484fSRodney W. Grimes g->strip = p->strip; 142558f0484fSRodney W. Grimes } 142658f0484fSRodney W. Grimes } 142758f0484fSRodney W. Grimes 142858f0484fSRodney W. Grimes /* 142958f0484fSRodney W. Grimes - findmust - fill in must and mlen with longest mandatory literal string 14308fb3f3f6SDavid E. O'Brien == static void findmust(struct parse *p, struct re_guts *g); 143158f0484fSRodney W. Grimes * 143258f0484fSRodney W. Grimes * This algorithm could do fancy things like analyzing the operands of | 143358f0484fSRodney W. Grimes * for common subsequences. Someday. This code is simple and finds most 143458f0484fSRodney W. Grimes * of the interesting cases. 143558f0484fSRodney W. Grimes * 143658f0484fSRodney W. Grimes * Note that must and mlen got initialized during setup. 143758f0484fSRodney W. Grimes */ 143858f0484fSRodney W. Grimes static void 143958f0484fSRodney W. Grimes findmust(p, g) 144058f0484fSRodney W. Grimes struct parse *p; 14418fb3f3f6SDavid E. O'Brien struct re_guts *g; 144258f0484fSRodney W. Grimes { 14438fb3f3f6SDavid E. O'Brien sop *scan; 144458f0484fSRodney W. Grimes sop *start; 14458fb3f3f6SDavid E. O'Brien sop *newstart; 14468fb3f3f6SDavid E. O'Brien sopno newlen; 14478fb3f3f6SDavid E. O'Brien sop s; 14488fb3f3f6SDavid E. O'Brien char *cp; 1449e6a886d8SDaniel C. Sobral int offset; 1450e5996857STim J. Robbins char buf[MB_LEN_MAX]; 1451e5996857STim J. Robbins size_t clen; 1452e5996857STim J. Robbins mbstate_t mbs; 145358f0484fSRodney W. Grimes 145458f0484fSRodney W. Grimes /* avoid making error situations worse */ 145558f0484fSRodney W. Grimes if (p->error != 0) 145658f0484fSRodney W. Grimes return; 145758f0484fSRodney W. Grimes 1458e5996857STim J. Robbins /* 1459e5996857STim J. Robbins * It's not generally safe to do a ``char'' substring search on 1460e5996857STim J. Robbins * multibyte character strings, but it's safe for at least 1461e5996857STim J. Robbins * UTF-8 (see RFC 3629). 1462e5996857STim J. Robbins */ 1463e5996857STim J. Robbins if (MB_CUR_MAX > 1 && 1464e5996857STim J. Robbins strcmp(_CurrentRuneLocale->__encoding, "UTF-8") != 0) 1465e5996857STim J. Robbins return; 1466e5996857STim J. Robbins 146758f0484fSRodney W. Grimes /* find the longest OCHAR sequence in strip */ 146858f0484fSRodney W. Grimes newlen = 0; 1469e6a886d8SDaniel C. Sobral offset = 0; 1470e6a886d8SDaniel C. Sobral g->moffset = 0; 147158f0484fSRodney W. Grimes scan = g->strip + 1; 147258f0484fSRodney W. Grimes do { 147358f0484fSRodney W. Grimes s = *scan++; 147458f0484fSRodney W. Grimes switch (OP(s)) { 147558f0484fSRodney W. Grimes case OCHAR: /* sequence member */ 1476e5996857STim J. Robbins if (newlen == 0) { /* new sequence */ 1477e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 147858f0484fSRodney W. Grimes newstart = scan - 1; 1479e5996857STim J. Robbins } 1480e5996857STim J. Robbins clen = wcrtomb(buf, OPND(s), &mbs); 1481e5996857STim J. Robbins if (clen == (size_t)-1) 1482e5996857STim J. Robbins goto toohard; 1483e5996857STim J. Robbins newlen += clen; 148458f0484fSRodney W. Grimes break; 148558f0484fSRodney W. Grimes case OPLUS_: /* things that don't break one */ 148658f0484fSRodney W. Grimes case OLPAREN: 148758f0484fSRodney W. Grimes case ORPAREN: 148858f0484fSRodney W. Grimes break; 148958f0484fSRodney W. Grimes case OQUEST_: /* things that must be skipped */ 149058f0484fSRodney W. Grimes case OCH_: 149180f36366STim J. Robbins offset = altoffset(scan, offset); 149258f0484fSRodney W. Grimes scan--; 149358f0484fSRodney W. Grimes do { 149458f0484fSRodney W. Grimes scan += OPND(s); 149558f0484fSRodney W. Grimes s = *scan; 149658f0484fSRodney W. Grimes /* assert() interferes w debug printouts */ 149758f0484fSRodney W. Grimes if (OP(s) != O_QUEST && OP(s) != O_CH && 149858f0484fSRodney W. Grimes OP(s) != OOR2) { 149958f0484fSRodney W. Grimes g->iflags |= BAD; 150058f0484fSRodney W. Grimes return; 150158f0484fSRodney W. Grimes } 150258f0484fSRodney W. Grimes } while (OP(s) != O_QUEST && OP(s) != O_CH); 15037fed38d0SPhilippe Charnier /* FALLTHROUGH */ 1504e6a886d8SDaniel C. Sobral case OBOW: /* things that break a sequence */ 1505e6a886d8SDaniel C. Sobral case OEOW: 1506e6a886d8SDaniel C. Sobral case OBOL: 1507e6a886d8SDaniel C. Sobral case OEOL: 1508e6a886d8SDaniel C. Sobral case O_QUEST: 1509e6a886d8SDaniel C. Sobral case O_CH: 1510e6a886d8SDaniel C. Sobral case OEND: 151158f0484fSRodney W. Grimes if (newlen > g->mlen) { /* ends one */ 151258f0484fSRodney W. Grimes start = newstart; 151358f0484fSRodney W. Grimes g->mlen = newlen; 1514e6a886d8SDaniel C. Sobral if (offset > -1) { 1515e6a886d8SDaniel C. Sobral g->moffset += offset; 1516e6a886d8SDaniel C. Sobral offset = newlen; 1517e6a886d8SDaniel C. Sobral } else 1518e6a886d8SDaniel C. Sobral g->moffset = offset; 1519e6a886d8SDaniel C. Sobral } else { 1520e6a886d8SDaniel C. Sobral if (offset > -1) 1521e6a886d8SDaniel C. Sobral offset += newlen; 152258f0484fSRodney W. Grimes } 152358f0484fSRodney W. Grimes newlen = 0; 152458f0484fSRodney W. Grimes break; 1525e6a886d8SDaniel C. Sobral case OANY: 1526e6a886d8SDaniel C. Sobral if (newlen > g->mlen) { /* ends one */ 1527e6a886d8SDaniel C. Sobral start = newstart; 1528e6a886d8SDaniel C. Sobral g->mlen = newlen; 1529e6a886d8SDaniel C. Sobral if (offset > -1) { 1530e6a886d8SDaniel C. Sobral g->moffset += offset; 1531e6a886d8SDaniel C. Sobral offset = newlen; 1532e6a886d8SDaniel C. Sobral } else 1533e6a886d8SDaniel C. Sobral g->moffset = offset; 1534e6a886d8SDaniel C. Sobral } else { 1535e6a886d8SDaniel C. Sobral if (offset > -1) 1536e6a886d8SDaniel C. Sobral offset += newlen; 1537e6a886d8SDaniel C. Sobral } 1538e6a886d8SDaniel C. Sobral if (offset > -1) 1539e6a886d8SDaniel C. Sobral offset++; 1540e6a886d8SDaniel C. Sobral newlen = 0; 1541e6a886d8SDaniel C. Sobral break; 1542e6a886d8SDaniel C. Sobral case OANYOF: /* may or may not invalidate offset */ 1543e6a886d8SDaniel C. Sobral /* First, everything as OANY */ 1544e6a886d8SDaniel C. Sobral if (newlen > g->mlen) { /* ends one */ 1545e6a886d8SDaniel C. Sobral start = newstart; 1546e6a886d8SDaniel C. Sobral g->mlen = newlen; 1547e6a886d8SDaniel C. Sobral if (offset > -1) { 1548e6a886d8SDaniel C. Sobral g->moffset += offset; 1549e6a886d8SDaniel C. Sobral offset = newlen; 1550e6a886d8SDaniel C. Sobral } else 1551e6a886d8SDaniel C. Sobral g->moffset = offset; 1552e6a886d8SDaniel C. Sobral } else { 1553e6a886d8SDaniel C. Sobral if (offset > -1) 1554e6a886d8SDaniel C. Sobral offset += newlen; 1555e6a886d8SDaniel C. Sobral } 1556e6a886d8SDaniel C. Sobral if (offset > -1) 1557e6a886d8SDaniel C. Sobral offset++; 1558e6a886d8SDaniel C. Sobral newlen = 0; 1559e6a886d8SDaniel C. Sobral break; 1560e5996857STim J. Robbins toohard: 1561e6a886d8SDaniel C. Sobral default: 1562e6a886d8SDaniel C. Sobral /* Anything here makes it impossible or too hard 1563e6a886d8SDaniel C. Sobral * to calculate the offset -- so we give up; 1564e6a886d8SDaniel C. Sobral * save the last known good offset, in case the 1565e6a886d8SDaniel C. Sobral * must sequence doesn't occur later. 1566e6a886d8SDaniel C. Sobral */ 1567e6a886d8SDaniel C. Sobral if (newlen > g->mlen) { /* ends one */ 1568e6a886d8SDaniel C. Sobral start = newstart; 1569e6a886d8SDaniel C. Sobral g->mlen = newlen; 1570e6a886d8SDaniel C. Sobral if (offset > -1) 1571e6a886d8SDaniel C. Sobral g->moffset += offset; 1572e6a886d8SDaniel C. Sobral else 1573e6a886d8SDaniel C. Sobral g->moffset = offset; 1574e6a886d8SDaniel C. Sobral } 1575e6a886d8SDaniel C. Sobral offset = -1; 1576e6a886d8SDaniel C. Sobral newlen = 0; 1577e6a886d8SDaniel C. Sobral break; 157858f0484fSRodney W. Grimes } 157958f0484fSRodney W. Grimes } while (OP(s) != OEND); 158058f0484fSRodney W. Grimes 1581e6a886d8SDaniel C. Sobral if (g->mlen == 0) { /* there isn't one */ 1582e6a886d8SDaniel C. Sobral g->moffset = -1; 158358f0484fSRodney W. Grimes return; 1584e6a886d8SDaniel C. Sobral } 158558f0484fSRodney W. Grimes 158658f0484fSRodney W. Grimes /* turn it into a character string */ 158758f0484fSRodney W. Grimes g->must = malloc((size_t)g->mlen + 1); 158858f0484fSRodney W. Grimes if (g->must == NULL) { /* argh; just forget it */ 158958f0484fSRodney W. Grimes g->mlen = 0; 1590e6a886d8SDaniel C. Sobral g->moffset = -1; 159158f0484fSRodney W. Grimes return; 159258f0484fSRodney W. Grimes } 159358f0484fSRodney W. Grimes cp = g->must; 159458f0484fSRodney W. Grimes scan = start; 1595e5996857STim J. Robbins memset(&mbs, 0, sizeof(mbs)); 1596e5996857STim J. Robbins while (cp < g->must + g->mlen) { 159758f0484fSRodney W. Grimes while (OP(s = *scan++) != OCHAR) 159858f0484fSRodney W. Grimes continue; 1599e5996857STim J. Robbins clen = wcrtomb(cp, OPND(s), &mbs); 1600e5996857STim J. Robbins assert(clen != (size_t)-1); 1601e5996857STim J. Robbins cp += clen; 160258f0484fSRodney W. Grimes } 160358f0484fSRodney W. Grimes assert(cp == g->must + g->mlen); 160458f0484fSRodney W. Grimes *cp++ = '\0'; /* just on general principles */ 160558f0484fSRodney W. Grimes } 160658f0484fSRodney W. Grimes 160758f0484fSRodney W. Grimes /* 1608e6a886d8SDaniel C. Sobral - altoffset - choose biggest offset among multiple choices 160980f36366STim J. Robbins == static int altoffset(sop *scan, int offset); 1610e6a886d8SDaniel C. Sobral * 1611e6a886d8SDaniel C. Sobral * Compute, recursively if necessary, the largest offset among multiple 1612e6a886d8SDaniel C. Sobral * re paths. 1613e6a886d8SDaniel C. Sobral */ 1614e6a886d8SDaniel C. Sobral static int 161580f36366STim J. Robbins altoffset(scan, offset) 1616e6a886d8SDaniel C. Sobral sop *scan; 1617e6a886d8SDaniel C. Sobral int offset; 1618e6a886d8SDaniel C. Sobral { 1619e6a886d8SDaniel C. Sobral int largest; 1620e6a886d8SDaniel C. Sobral int try; 1621e6a886d8SDaniel C. Sobral sop s; 1622e6a886d8SDaniel C. Sobral 1623e6a886d8SDaniel C. Sobral /* If we gave up already on offsets, return */ 1624e6a886d8SDaniel C. Sobral if (offset == -1) 1625e6a886d8SDaniel C. Sobral return -1; 1626e6a886d8SDaniel C. Sobral 1627e6a886d8SDaniel C. Sobral largest = 0; 1628e6a886d8SDaniel C. Sobral try = 0; 1629e6a886d8SDaniel C. Sobral s = *scan++; 1630e6a886d8SDaniel C. Sobral while (OP(s) != O_QUEST && OP(s) != O_CH) { 1631e6a886d8SDaniel C. Sobral switch (OP(s)) { 1632e6a886d8SDaniel C. Sobral case OOR1: 1633e6a886d8SDaniel C. Sobral if (try > largest) 1634e6a886d8SDaniel C. Sobral largest = try; 1635e6a886d8SDaniel C. Sobral try = 0; 1636e6a886d8SDaniel C. Sobral break; 1637e6a886d8SDaniel C. Sobral case OQUEST_: 1638e6a886d8SDaniel C. Sobral case OCH_: 163980f36366STim J. Robbins try = altoffset(scan, try); 1640e6a886d8SDaniel C. Sobral if (try == -1) 1641e6a886d8SDaniel C. Sobral return -1; 1642e6a886d8SDaniel C. Sobral scan--; 1643e6a886d8SDaniel C. Sobral do { 1644e6a886d8SDaniel C. Sobral scan += OPND(s); 1645e6a886d8SDaniel C. Sobral s = *scan; 1646e6a886d8SDaniel C. Sobral if (OP(s) != O_QUEST && OP(s) != O_CH && 1647e6a886d8SDaniel C. Sobral OP(s) != OOR2) 1648e6a886d8SDaniel C. Sobral return -1; 1649e6a886d8SDaniel C. Sobral } while (OP(s) != O_QUEST && OP(s) != O_CH); 16508f9e434fSDaniel C. Sobral /* We must skip to the next position, or we'll 16518f9e434fSDaniel C. Sobral * leave altoffset() too early. 16528f9e434fSDaniel C. Sobral */ 16538f9e434fSDaniel C. Sobral scan++; 1654e6a886d8SDaniel C. Sobral break; 1655e6a886d8SDaniel C. Sobral case OANYOF: 1656e6a886d8SDaniel C. Sobral case OCHAR: 1657e6a886d8SDaniel C. Sobral case OANY: 1658e6a886d8SDaniel C. Sobral try++; 1659e6a886d8SDaniel C. Sobral case OBOW: 1660e6a886d8SDaniel C. Sobral case OEOW: 1661e6a886d8SDaniel C. Sobral case OLPAREN: 1662e6a886d8SDaniel C. Sobral case ORPAREN: 1663e6a886d8SDaniel C. Sobral case OOR2: 1664e6a886d8SDaniel C. Sobral break; 1665e6a886d8SDaniel C. Sobral default: 1666e6a886d8SDaniel C. Sobral try = -1; 1667e6a886d8SDaniel C. Sobral break; 1668e6a886d8SDaniel C. Sobral } 1669e6a886d8SDaniel C. Sobral if (try == -1) 1670e6a886d8SDaniel C. Sobral return -1; 1671e6a886d8SDaniel C. Sobral s = *scan++; 1672e6a886d8SDaniel C. Sobral } 1673e6a886d8SDaniel C. Sobral 1674e6a886d8SDaniel C. Sobral if (try > largest) 1675e6a886d8SDaniel C. Sobral largest = try; 1676e6a886d8SDaniel C. Sobral 1677e6a886d8SDaniel C. Sobral return largest+offset; 1678e6a886d8SDaniel C. Sobral } 1679e6a886d8SDaniel C. Sobral 1680e6a886d8SDaniel C. Sobral /* 16816049d9f0SDaniel C. Sobral - computejumps - compute char jumps for BM scan 16828fb3f3f6SDavid E. O'Brien == static void computejumps(struct parse *p, struct re_guts *g); 16836049d9f0SDaniel C. Sobral * 16846049d9f0SDaniel C. Sobral * This algorithm assumes g->must exists and is has size greater than 16856049d9f0SDaniel C. Sobral * zero. It's based on the algorithm found on Computer Algorithms by 16866049d9f0SDaniel C. Sobral * Sara Baase. 16876049d9f0SDaniel C. Sobral * 16886049d9f0SDaniel C. Sobral * A char jump is the number of characters one needs to jump based on 16896049d9f0SDaniel C. Sobral * the value of the character from the text that was mismatched. 16906049d9f0SDaniel C. Sobral */ 16916049d9f0SDaniel C. Sobral static void 16926049d9f0SDaniel C. Sobral computejumps(p, g) 16936049d9f0SDaniel C. Sobral struct parse *p; 16946049d9f0SDaniel C. Sobral struct re_guts *g; 16956049d9f0SDaniel C. Sobral { 16966049d9f0SDaniel C. Sobral int ch; 16976049d9f0SDaniel C. Sobral int mindex; 16986049d9f0SDaniel C. Sobral 16996049d9f0SDaniel C. Sobral /* Avoid making errors worse */ 17006049d9f0SDaniel C. Sobral if (p->error != 0) 17016049d9f0SDaniel C. Sobral return; 17026049d9f0SDaniel C. Sobral 1703517bffcaSDaniel C. Sobral g->charjump = (int*) malloc((NC + 1) * sizeof(int)); 17046049d9f0SDaniel C. Sobral if (g->charjump == NULL) /* Not a fatal error */ 17056049d9f0SDaniel C. Sobral return; 1706c5e125bbSDaniel C. Sobral /* Adjust for signed chars, if necessary */ 1707c5e125bbSDaniel C. Sobral g->charjump = &g->charjump[-(CHAR_MIN)]; 17086049d9f0SDaniel C. Sobral 17096049d9f0SDaniel C. Sobral /* If the character does not exist in the pattern, the jump 17106049d9f0SDaniel C. Sobral * is equal to the number of characters in the pattern. 17116049d9f0SDaniel C. Sobral */ 1712c5e125bbSDaniel C. Sobral for (ch = CHAR_MIN; ch < (CHAR_MAX + 1); ch++) 17136049d9f0SDaniel C. Sobral g->charjump[ch] = g->mlen; 17146049d9f0SDaniel C. Sobral 17156049d9f0SDaniel C. Sobral /* If the character does exist, compute the jump that would 17166049d9f0SDaniel C. Sobral * take us to the last character in the pattern equal to it 17176049d9f0SDaniel C. Sobral * (notice that we match right to left, so that last character 17186049d9f0SDaniel C. Sobral * is the first one that would be matched). 17196049d9f0SDaniel C. Sobral */ 17206049d9f0SDaniel C. Sobral for (mindex = 0; mindex < g->mlen; mindex++) 1721e0554a53SJacques Vidrine g->charjump[(int)g->must[mindex]] = g->mlen - mindex - 1; 17226049d9f0SDaniel C. Sobral } 17236049d9f0SDaniel C. Sobral 17246049d9f0SDaniel C. Sobral /* 17256049d9f0SDaniel C. Sobral - computematchjumps - compute match jumps for BM scan 17268fb3f3f6SDavid E. O'Brien == static void computematchjumps(struct parse *p, struct re_guts *g); 17276049d9f0SDaniel C. Sobral * 17286049d9f0SDaniel C. Sobral * This algorithm assumes g->must exists and is has size greater than 17296049d9f0SDaniel C. Sobral * zero. It's based on the algorithm found on Computer Algorithms by 17306049d9f0SDaniel C. Sobral * Sara Baase. 17316049d9f0SDaniel C. Sobral * 17326049d9f0SDaniel C. Sobral * A match jump is the number of characters one needs to advance based 17336049d9f0SDaniel C. Sobral * on the already-matched suffix. 17346049d9f0SDaniel C. Sobral * Notice that all values here are minus (g->mlen-1), because of the way 17356049d9f0SDaniel C. Sobral * the search algorithm works. 17366049d9f0SDaniel C. Sobral */ 17376049d9f0SDaniel C. Sobral static void 17386049d9f0SDaniel C. Sobral computematchjumps(p, g) 17396049d9f0SDaniel C. Sobral struct parse *p; 17406049d9f0SDaniel C. Sobral struct re_guts *g; 17416049d9f0SDaniel C. Sobral { 17426049d9f0SDaniel C. Sobral int mindex; /* General "must" iterator */ 17436049d9f0SDaniel C. Sobral int suffix; /* Keeps track of matching suffix */ 17446049d9f0SDaniel C. Sobral int ssuffix; /* Keeps track of suffixes' suffix */ 17456049d9f0SDaniel C. Sobral int* pmatches; /* pmatches[k] points to the next i 17466049d9f0SDaniel C. Sobral * such that i+1...mlen is a substring 17476049d9f0SDaniel C. Sobral * of k+1...k+mlen-i-1 17486049d9f0SDaniel C. Sobral */ 17496049d9f0SDaniel C. Sobral 17506049d9f0SDaniel C. Sobral /* Avoid making errors worse */ 17516049d9f0SDaniel C. Sobral if (p->error != 0) 17526049d9f0SDaniel C. Sobral return; 17536049d9f0SDaniel C. Sobral 1754517bffcaSDaniel C. Sobral pmatches = (int*) malloc(g->mlen * sizeof(unsigned int)); 17556049d9f0SDaniel C. Sobral if (pmatches == NULL) { 17566049d9f0SDaniel C. Sobral g->matchjump = NULL; 17576049d9f0SDaniel C. Sobral return; 17586049d9f0SDaniel C. Sobral } 17596049d9f0SDaniel C. Sobral 1760517bffcaSDaniel C. Sobral g->matchjump = (int*) malloc(g->mlen * sizeof(unsigned int)); 17616049d9f0SDaniel C. Sobral if (g->matchjump == NULL) /* Not a fatal error */ 17626049d9f0SDaniel C. Sobral return; 17636049d9f0SDaniel C. Sobral 17646049d9f0SDaniel C. Sobral /* Set maximum possible jump for each character in the pattern */ 17656049d9f0SDaniel C. Sobral for (mindex = 0; mindex < g->mlen; mindex++) 17666049d9f0SDaniel C. Sobral g->matchjump[mindex] = 2*g->mlen - mindex - 1; 17676049d9f0SDaniel C. Sobral 17686049d9f0SDaniel C. Sobral /* Compute pmatches[] */ 17696049d9f0SDaniel C. Sobral for (mindex = g->mlen - 1, suffix = g->mlen; mindex >= 0; 17706049d9f0SDaniel C. Sobral mindex--, suffix--) { 17716049d9f0SDaniel C. Sobral pmatches[mindex] = suffix; 17726049d9f0SDaniel C. Sobral 17736049d9f0SDaniel C. Sobral /* If a mismatch is found, interrupting the substring, 17746049d9f0SDaniel C. Sobral * compute the matchjump for that position. If no 17756049d9f0SDaniel C. Sobral * mismatch is found, then a text substring mismatched 17766049d9f0SDaniel C. Sobral * against the suffix will also mismatch against the 17776049d9f0SDaniel C. Sobral * substring. 17786049d9f0SDaniel C. Sobral */ 17796049d9f0SDaniel C. Sobral while (suffix < g->mlen 17806049d9f0SDaniel C. Sobral && g->must[mindex] != g->must[suffix]) { 17816049d9f0SDaniel C. Sobral g->matchjump[suffix] = MIN(g->matchjump[suffix], 17826049d9f0SDaniel C. Sobral g->mlen - mindex - 1); 17836049d9f0SDaniel C. Sobral suffix = pmatches[suffix]; 17846049d9f0SDaniel C. Sobral } 17856049d9f0SDaniel C. Sobral } 17866049d9f0SDaniel C. Sobral 17876049d9f0SDaniel C. Sobral /* Compute the matchjump up to the last substring found to jump 17886049d9f0SDaniel C. Sobral * to the beginning of the largest must pattern prefix matching 17896049d9f0SDaniel C. Sobral * it's own suffix. 17906049d9f0SDaniel C. Sobral */ 17916049d9f0SDaniel C. Sobral for (mindex = 0; mindex <= suffix; mindex++) 17926049d9f0SDaniel C. Sobral g->matchjump[mindex] = MIN(g->matchjump[mindex], 17936049d9f0SDaniel C. Sobral g->mlen + suffix - mindex); 17946049d9f0SDaniel C. Sobral 17956049d9f0SDaniel C. Sobral ssuffix = pmatches[suffix]; 17966049d9f0SDaniel C. Sobral while (suffix < g->mlen) { 17979868274bSDaniel C. Sobral while (suffix <= ssuffix && suffix < g->mlen) { 17986049d9f0SDaniel C. Sobral g->matchjump[suffix] = MIN(g->matchjump[suffix], 17996049d9f0SDaniel C. Sobral g->mlen + ssuffix - suffix); 18006049d9f0SDaniel C. Sobral suffix++; 18016049d9f0SDaniel C. Sobral } 18028e2f75b8SDaniel C. Sobral if (suffix < g->mlen) 18036049d9f0SDaniel C. Sobral ssuffix = pmatches[ssuffix]; 18046049d9f0SDaniel C. Sobral } 18056049d9f0SDaniel C. Sobral 18066049d9f0SDaniel C. Sobral free(pmatches); 18076049d9f0SDaniel C. Sobral } 18086049d9f0SDaniel C. Sobral 18096049d9f0SDaniel C. Sobral /* 181058f0484fSRodney W. Grimes - pluscount - count + nesting 18118fb3f3f6SDavid E. O'Brien == static sopno pluscount(struct parse *p, struct re_guts *g); 181258f0484fSRodney W. Grimes */ 181358f0484fSRodney W. Grimes static sopno /* nesting depth */ 181458f0484fSRodney W. Grimes pluscount(p, g) 181558f0484fSRodney W. Grimes struct parse *p; 18168fb3f3f6SDavid E. O'Brien struct re_guts *g; 181758f0484fSRodney W. Grimes { 18188fb3f3f6SDavid E. O'Brien sop *scan; 18198fb3f3f6SDavid E. O'Brien sop s; 18208fb3f3f6SDavid E. O'Brien sopno plusnest = 0; 18218fb3f3f6SDavid E. O'Brien sopno maxnest = 0; 182258f0484fSRodney W. Grimes 182358f0484fSRodney W. Grimes if (p->error != 0) 182458f0484fSRodney W. Grimes return(0); /* there may not be an OEND */ 182558f0484fSRodney W. Grimes 182658f0484fSRodney W. Grimes scan = g->strip + 1; 182758f0484fSRodney W. Grimes do { 182858f0484fSRodney W. Grimes s = *scan++; 182958f0484fSRodney W. Grimes switch (OP(s)) { 183058f0484fSRodney W. Grimes case OPLUS_: 183158f0484fSRodney W. Grimes plusnest++; 183258f0484fSRodney W. Grimes break; 183358f0484fSRodney W. Grimes case O_PLUS: 183458f0484fSRodney W. Grimes if (plusnest > maxnest) 183558f0484fSRodney W. Grimes maxnest = plusnest; 183658f0484fSRodney W. Grimes plusnest--; 183758f0484fSRodney W. Grimes break; 183858f0484fSRodney W. Grimes } 183958f0484fSRodney W. Grimes } while (OP(s) != OEND); 184058f0484fSRodney W. Grimes if (plusnest != 0) 184158f0484fSRodney W. Grimes g->iflags |= BAD; 184258f0484fSRodney W. Grimes return(maxnest); 184358f0484fSRodney W. Grimes } 1844