LCOV - code coverage report
Current view: top level - src/backend/regex - regcomp.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 682 799 85.4 %
Date: 2019-11-15 23:07:02 Functions: 31 32 96.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * re_*comp and friends - compile REs
       3             :  * This file #includes several others (see the bottom).
       4             :  *
       5             :  * Copyright (c) 1998, 1999 Henry Spencer.  All rights reserved.
       6             :  *
       7             :  * Development of this software was funded, in part, by Cray Research Inc.,
       8             :  * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
       9             :  * Corporation, none of whom are responsible for the results.  The author
      10             :  * thanks all of them.
      11             :  *
      12             :  * Redistribution and use in source and binary forms -- with or without
      13             :  * modification -- are permitted for any purpose, provided that
      14             :  * redistributions in source form retain this entire copyright notice and
      15             :  * indicate the origin and nature of any modifications.
      16             :  *
      17             :  * I'd appreciate being given credit for this package in the documentation
      18             :  * of software which uses it, but that is not a requirement.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
      21             :  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
      22             :  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
      23             :  * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      24             :  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      25             :  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
      26             :  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
      27             :  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
      28             :  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
      29             :  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      30             :  *
      31             :  * src/backend/regex/regcomp.c
      32             :  *
      33             :  */
      34             : 
      35             : #include "regex/regguts.h"
      36             : 
      37             : /*
      38             :  * forward declarations, up here so forward datatypes etc. are defined early
      39             :  */
      40             : /* === regcomp.c === */
      41             : static void moresubs(struct vars *, int);
      42             : static int  freev(struct vars *, int);
      43             : static void makesearch(struct vars *, struct nfa *);
      44             : static struct subre *parse(struct vars *, int, int, struct state *, struct state *);
      45             : static struct subre *parsebranch(struct vars *, int, int, struct state *, struct state *, int);
      46             : static void parseqatom(struct vars *, int, int, struct state *, struct state *, struct subre *);
      47             : static void nonword(struct vars *, int, struct state *, struct state *);
      48             : static void word(struct vars *, int, struct state *, struct state *);
      49             : static int  scannum(struct vars *);
      50             : static void repeat(struct vars *, struct state *, struct state *, int, int);
      51             : static void bracket(struct vars *, struct state *, struct state *);
      52             : static void cbracket(struct vars *, struct state *, struct state *);
      53             : static void brackpart(struct vars *, struct state *, struct state *);
      54             : static const chr *scanplain(struct vars *);
      55             : static void onechr(struct vars *, chr, struct state *, struct state *);
      56             : static void wordchrs(struct vars *);
      57             : static void processlacon(struct vars *, struct state *, struct state *, int,
      58             :                          struct state *, struct state *);
      59             : static struct subre *subre(struct vars *, int, int, struct state *, struct state *);
      60             : static void freesubre(struct vars *, struct subre *);
      61             : static void freesrnode(struct vars *, struct subre *);
      62             : static void optst(struct vars *, struct subre *);
      63             : static int  numst(struct subre *, int);
      64             : static void markst(struct subre *);
      65             : static void cleanst(struct vars *);
      66             : static long nfatree(struct vars *, struct subre *, FILE *);
      67             : static long nfanode(struct vars *, struct subre *, int, FILE *);
      68             : static int  newlacon(struct vars *, struct state *, struct state *, int);
      69             : static void freelacons(struct subre *, int);
      70             : static void rfree(regex_t *);
      71             : static int  rcancelrequested(void);
      72             : static int  rstacktoodeep(void);
      73             : 
      74             : #ifdef REG_DEBUG
      75             : static void dump(regex_t *, FILE *);
      76             : static void dumpst(struct subre *, FILE *, int);
      77             : static void stdump(struct subre *, FILE *, int);
      78             : static const char *stid(struct subre *, char *, size_t);
      79             : #endif
      80             : /* === regc_lex.c === */
      81             : static void lexstart(struct vars *);
      82             : static void prefixes(struct vars *);
      83             : static void lexnest(struct vars *, const chr *, const chr *);
      84             : static void lexword(struct vars *);
      85             : static int  next(struct vars *);
      86             : static int  lexescape(struct vars *);
      87             : static chr  lexdigits(struct vars *, int, int, int);
      88             : static int  brenext(struct vars *, chr);
      89             : static void skip(struct vars *);
      90             : static chr  newline(void);
      91             : static chr  chrnamed(struct vars *, const chr *, const chr *, chr);
      92             : 
      93             : /* === regc_color.c === */
      94             : static void initcm(struct vars *, struct colormap *);
      95             : static void freecm(struct colormap *);
      96             : static color maxcolor(struct colormap *);
      97             : static color newcolor(struct colormap *);
      98             : static void freecolor(struct colormap *, color);
      99             : static color pseudocolor(struct colormap *);
     100             : static color subcolor(struct colormap *, chr);
     101             : static color subcolorhi(struct colormap *, color *);
     102             : static color newsub(struct colormap *, color);
     103             : static int  newhicolorrow(struct colormap *, int);
     104             : static void newhicolorcols(struct colormap *);
     105             : static void subcolorcvec(struct vars *, struct cvec *, struct state *, struct state *);
     106             : static void subcoloronechr(struct vars *, chr, struct state *, struct state *, color *);
     107             : static void subcoloronerange(struct vars *, chr, chr, struct state *, struct state *, color *);
     108             : static void subcoloronerow(struct vars *, int, struct state *, struct state *, color *);
     109             : static void okcolors(struct nfa *, struct colormap *);
     110             : static void colorchain(struct colormap *, struct arc *);
     111             : static void uncolorchain(struct colormap *, struct arc *);
     112             : static void rainbow(struct nfa *, struct colormap *, int, color, struct state *, struct state *);
     113             : static void colorcomplement(struct nfa *, struct colormap *, int, struct state *, struct state *, struct state *);
     114             : 
     115             : #ifdef REG_DEBUG
     116             : static void dumpcolors(struct colormap *, FILE *);
     117             : static void dumpchr(chr, FILE *);
     118             : #endif
     119             : /* === regc_nfa.c === */
     120             : static struct nfa *newnfa(struct vars *, struct colormap *, struct nfa *);
     121             : static void freenfa(struct nfa *);
     122             : static struct state *newstate(struct nfa *);
     123             : static struct state *newfstate(struct nfa *, int flag);
     124             : static void dropstate(struct nfa *, struct state *);
     125             : static void freestate(struct nfa *, struct state *);
     126             : static void destroystate(struct nfa *, struct state *);
     127             : static void newarc(struct nfa *, int, color, struct state *, struct state *);
     128             : static void createarc(struct nfa *, int, color, struct state *, struct state *);
     129             : static struct arc *allocarc(struct nfa *, struct state *);
     130             : static void freearc(struct nfa *, struct arc *);
     131             : static void changearctarget(struct arc *, struct state *);
     132             : static int  hasnonemptyout(struct state *);
     133             : static struct arc *findarc(struct state *, int, color);
     134             : static void cparc(struct nfa *, struct arc *, struct state *, struct state *);
     135             : static void sortins(struct nfa *, struct state *);
     136             : static int  sortins_cmp(const void *, const void *);
     137             : static void sortouts(struct nfa *, struct state *);
     138             : static int  sortouts_cmp(const void *, const void *);
     139             : static void moveins(struct nfa *, struct state *, struct state *);
     140             : static void copyins(struct nfa *, struct state *, struct state *);
     141             : static void mergeins(struct nfa *, struct state *, struct arc **, int);
     142             : static void moveouts(struct nfa *, struct state *, struct state *);
     143             : static void copyouts(struct nfa *, struct state *, struct state *);
     144             : static void cloneouts(struct nfa *, struct state *, struct state *, struct state *, int);
     145             : static void delsub(struct nfa *, struct state *, struct state *);
     146             : static void deltraverse(struct nfa *, struct state *, struct state *);
     147             : static void dupnfa(struct nfa *, struct state *, struct state *, struct state *, struct state *);
     148             : static void duptraverse(struct nfa *, struct state *, struct state *);
     149             : static void cleartraverse(struct nfa *, struct state *);
     150             : static struct state *single_color_transition(struct state *, struct state *);
     151             : static void specialcolors(struct nfa *);
     152             : static long optimize(struct nfa *, FILE *);
     153             : static void pullback(struct nfa *, FILE *);
     154             : static int  pull(struct nfa *, struct arc *, struct state **);
     155             : static void pushfwd(struct nfa *, FILE *);
     156             : static int  push(struct nfa *, struct arc *, struct state **);
     157             : 
     158             : #define INCOMPATIBLE    1       /* destroys arc */
     159             : #define SATISFIED   2           /* constraint satisfied */
     160             : #define COMPATIBLE  3           /* compatible but not satisfied yet */
     161             : static int  combine(struct arc *, struct arc *);
     162             : static void fixempties(struct nfa *, FILE *);
     163             : static struct state *emptyreachable(struct nfa *, struct state *,
     164             :                                     struct state *, struct arc **);
     165             : static int  isconstraintarc(struct arc *);
     166             : static int  hasconstraintout(struct state *);
     167             : static void fixconstraintloops(struct nfa *, FILE *);
     168             : static int  findconstraintloop(struct nfa *, struct state *);
     169             : static void breakconstraintloop(struct nfa *, struct state *);
     170             : static void clonesuccessorstates(struct nfa *, struct state *, struct state *,
     171             :                                  struct state *, struct arc *,
     172             :                                  char *, char *, int);
     173             : static void cleanup(struct nfa *);
     174             : static void markreachable(struct nfa *, struct state *, struct state *, struct state *);
     175             : static void markcanreach(struct nfa *, struct state *, struct state *, struct state *);
     176             : static long analyze(struct nfa *);
     177             : static void compact(struct nfa *, struct cnfa *);
     178             : static void carcsort(struct carc *, size_t);
     179             : static int  carc_cmp(const void *, const void *);
     180             : static void freecnfa(struct cnfa *);
     181             : static void dumpnfa(struct nfa *, FILE *);
     182             : 
     183             : #ifdef REG_DEBUG
     184             : static void dumpstate(struct state *, FILE *);
     185             : static void dumparcs(struct state *, FILE *);
     186             : static void dumparc(struct arc *, struct state *, FILE *);
     187             : static void dumpcnfa(struct cnfa *, FILE *);
     188             : static void dumpcstate(int, struct cnfa *, FILE *);
     189             : #endif
     190             : /* === regc_cvec.c === */
     191             : static struct cvec *newcvec(int, int);
     192             : static struct cvec *clearcvec(struct cvec *);
     193             : static void addchr(struct cvec *, chr);
     194             : static void addrange(struct cvec *, chr, chr);
     195             : static struct cvec *getcvec(struct vars *, int, int);
     196             : static void freecvec(struct cvec *);
     197             : 
     198             : /* === regc_pg_locale.c === */
     199             : static int  pg_wc_isdigit(pg_wchar c);
     200             : static int  pg_wc_isalpha(pg_wchar c);
     201             : static int  pg_wc_isalnum(pg_wchar c);
     202             : static int  pg_wc_isupper(pg_wchar c);
     203             : static int  pg_wc_islower(pg_wchar c);
     204             : static int  pg_wc_isgraph(pg_wchar c);
     205             : static int  pg_wc_isprint(pg_wchar c);
     206             : static int  pg_wc_ispunct(pg_wchar c);
     207             : static int  pg_wc_isspace(pg_wchar c);
     208             : static pg_wchar pg_wc_toupper(pg_wchar c);
     209             : static pg_wchar pg_wc_tolower(pg_wchar c);
     210             : 
     211             : /* === regc_locale.c === */
     212             : static chr  element(struct vars *, const chr *, const chr *);
     213             : static struct cvec *range(struct vars *, chr, chr, int);
     214             : static int  before(chr, chr);
     215             : static struct cvec *eclass(struct vars *, chr, int);
     216             : static struct cvec *cclass(struct vars *, const chr *, const chr *, int);
     217             : static int  cclass_column_index(struct colormap *, chr);
     218             : static struct cvec *allcases(struct vars *, chr);
     219             : static int  cmp(const chr *, const chr *, size_t);
     220             : static int  casecmp(const chr *, const chr *, size_t);
     221             : 
     222             : 
     223             : /* internal variables, bundled for easy passing around */
     224             : struct vars
     225             : {
     226             :     regex_t    *re;
     227             :     const chr  *now;            /* scan pointer into string */
     228             :     const chr  *stop;           /* end of string */
     229             :     const chr  *savenow;        /* saved now and stop for "subroutine call" */
     230             :     const chr  *savestop;
     231             :     int         err;            /* error code (0 if none) */
     232             :     int         cflags;         /* copy of compile flags */
     233             :     int         lasttype;       /* type of previous token */
     234             :     int         nexttype;       /* type of next token */
     235             :     chr         nextvalue;      /* value (if any) of next token */
     236             :     int         lexcon;         /* lexical context type (see lex.c) */
     237             :     int         nsubexp;        /* subexpression count */
     238             :     struct subre **subs;        /* subRE pointer vector */
     239             :     size_t      nsubs;          /* length of vector */
     240             :     struct subre *sub10[10];    /* initial vector, enough for most */
     241             :     struct nfa *nfa;            /* the NFA */
     242             :     struct colormap *cm;        /* character color map */
     243             :     color       nlcolor;        /* color of newline */
     244             :     struct state *wordchrs;     /* state in nfa holding word-char outarcs */
     245             :     struct subre *tree;         /* subexpression tree */
     246             :     struct subre *treechain;    /* all tree nodes allocated */
     247             :     struct subre *treefree;     /* any free tree nodes */
     248             :     int         ntree;          /* number of tree nodes, plus one */
     249             :     struct cvec *cv;            /* interface cvec */
     250             :     struct cvec *cv2;           /* utility cvec */
     251             :     struct subre *lacons;       /* lookaround-constraint vector */
     252             :     int         nlacons;        /* size of lacons[]; note that only slots
     253             :                                  * numbered 1 .. nlacons-1 are used */
     254             :     size_t      spaceused;      /* approx. space used for compilation */
     255             : };
     256             : 
     257             : /* parsing macros; most know that `v' is the struct vars pointer */
     258             : #define NEXT()  (next(v))       /* advance by one token */
     259             : #define SEE(t)  (v->nexttype == (t)) /* is next token this? */
     260             : #define EAT(t)  (SEE(t) && next(v)) /* if next is this, swallow it */
     261             : #define VISERR(vv)  ((vv)->err != 0) /* have we seen an error yet? */
     262             : #define ISERR() VISERR(v)
     263             : #define VERR(vv,e)  ((vv)->nexttype = EOS, \
     264             :                      (vv)->err = ((vv)->err ? (vv)->err : (e)))
     265             : #define ERR(e)  VERR(v, e)      /* record an error */
     266             : #define NOERR() {if (ISERR()) return;}  /* if error seen, return */
     267             : #define NOERRN()    {if (ISERR()) return NULL;} /* NOERR with retval */
     268             : #define NOERRZ()    {if (ISERR()) return 0;}    /* NOERR with retval */
     269             : #define INSIST(c, e) do { if (!(c)) ERR(e); } while (0) /* error if c false */
     270             : #define NOTE(b) (v->re->re_info |= (b)) /* note visible condition */
     271             : #define EMPTYARC(x, y)  newarc(v->nfa, EMPTY, 0, x, y)
     272             : 
     273             : /* token type codes, some also used as NFA arc types */
     274             : #define EMPTY   'n'             /* no token present */
     275             : #define EOS 'e'                 /* end of string */
     276             : #define PLAIN   'p'             /* ordinary character */
     277             : #define DIGIT   'd'             /* digit (in bound) */
     278             : #define BACKREF 'b'             /* back reference */
     279             : #define COLLEL  'I'             /* start of [. */
     280             : #define ECLASS  'E'             /* start of [= */
     281             : #define CCLASS  'C'             /* start of [: */
     282             : #define END 'X'                 /* end of [. [= [: */
     283             : #define RANGE   'R'             /* - within [] which might be range delim. */
     284             : #define LACON   'L'             /* lookaround constraint subRE */
     285             : #define AHEAD   'a'             /* color-lookahead arc */
     286             : #define BEHIND  'r'             /* color-lookbehind arc */
     287             : #define WBDRY   'w'             /* word boundary constraint */
     288             : #define NWBDRY  'W'             /* non-word-boundary constraint */
     289             : #define SBEGIN  'A'             /* beginning of string (even if not BOL) */
     290             : #define SEND    'Z'             /* end of string (even if not EOL) */
     291             : 
     292             : /* is an arc colored, and hence on a color chain? */
     293             : #define COLORED(a) \
     294             :     ((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND)
     295             : 
     296             : 
     297             : /* static function list */
     298             : static const struct fns functions = {
     299             :     rfree,                      /* regfree insides */
     300             :     rcancelrequested,           /* check for cancel request */
     301             :     rstacktoodeep               /* check for stack getting dangerously deep */
     302             : };
     303             : 
     304             : 
     305             : 
     306             : /*
     307             :  * pg_regcomp - compile regular expression
     308             :  *
     309             :  * Note: on failure, no resources remain allocated, so pg_regfree()
     310             :  * need not be applied to re.
     311             :  */
     312             : int
     313        2830 : pg_regcomp(regex_t *re,
     314             :            const chr *string,
     315             :            size_t len,
     316             :            int flags,
     317             :            Oid collation)
     318             : {
     319             :     struct vars var;
     320        2830 :     struct vars *v = &var;
     321             :     struct guts *g;
     322             :     int         i;
     323             :     size_t      j;
     324             : 
     325             : #ifdef REG_DEBUG
     326             :     FILE       *debug = (flags & REG_PROGRESS) ? stdout : (FILE *) NULL;
     327             : #else
     328        2830 :     FILE       *debug = (FILE *) NULL;
     329             : #endif
     330             : 
     331             : #define  CNOERR()    { if (ISERR()) return freev(v, v->err); }
     332             : 
     333             :     /* sanity checks */
     334             : 
     335        2830 :     if (re == NULL || string == NULL)
     336           0 :         return REG_INVARG;
     337        2854 :     if ((flags & REG_QUOTE) &&
     338          24 :         (flags & (REG_ADVANCED | REG_EXPANDED | REG_NEWLINE)))
     339           0 :         return REG_INVARG;
     340        2830 :     if (!(flags & REG_EXTENDED) && (flags & REG_ADVF))
     341           0 :         return REG_INVARG;
     342             : 
     343             :     /* Initialize locale-dependent support */
     344        2830 :     pg_set_regex_collation(collation);
     345             : 
     346             :     /* initial setup (after which freev() is callable) */
     347        2830 :     v->re = re;
     348        2830 :     v->now = string;
     349        2830 :     v->stop = v->now + len;
     350        2830 :     v->savenow = v->savestop = NULL;
     351        2830 :     v->err = 0;
     352        2830 :     v->cflags = flags;
     353        2830 :     v->nsubexp = 0;
     354        2830 :     v->subs = v->sub10;
     355        2830 :     v->nsubs = 10;
     356       31130 :     for (j = 0; j < v->nsubs; j++)
     357       28300 :         v->subs[j] = NULL;
     358        2830 :     v->nfa = NULL;
     359        2830 :     v->cm = NULL;
     360        2830 :     v->nlcolor = COLORLESS;
     361        2830 :     v->wordchrs = NULL;
     362        2830 :     v->tree = NULL;
     363        2830 :     v->treechain = NULL;
     364        2830 :     v->treefree = NULL;
     365        2830 :     v->cv = NULL;
     366        2830 :     v->cv2 = NULL;
     367        2830 :     v->lacons = NULL;
     368        2830 :     v->nlacons = 0;
     369        2830 :     v->spaceused = 0;
     370        2830 :     re->re_magic = REMAGIC;
     371        2830 :     re->re_info = 0;         /* bits get set during parse */
     372        2830 :     re->re_csize = sizeof(chr);
     373        2830 :     re->re_collation = collation;
     374        2830 :     re->re_guts = NULL;
     375        2830 :     re->re_fns = VS(&functions);
     376             : 
     377             :     /* more complex setup, malloced things */
     378        2830 :     re->re_guts = VS(MALLOC(sizeof(struct guts)));
     379        2830 :     if (re->re_guts == NULL)
     380           0 :         return freev(v, REG_ESPACE);
     381        2830 :     g = (struct guts *) re->re_guts;
     382        2830 :     g->tree = NULL;
     383        2830 :     initcm(v, &g->cmap);
     384        2830 :     v->cm = &g->cmap;
     385        2830 :     g->lacons = NULL;
     386        2830 :     g->nlacons = 0;
     387        2830 :     ZAPCNFA(g->search);
     388        2830 :     v->nfa = newnfa(v, v->cm, (struct nfa *) NULL);
     389        2830 :     CNOERR();
     390             :     /* set up a reasonably-sized transient cvec for getcvec usage */
     391        2830 :     v->cv = newcvec(100, 20);
     392        2830 :     if (v->cv == NULL)
     393           0 :         return freev(v, REG_ESPACE);
     394             : 
     395             :     /* parsing */
     396        2830 :     lexstart(v);                /* also handles prefixes */
     397        2830 :     if ((v->cflags & REG_NLSTOP) || (v->cflags & REG_NLANCH))
     398             :     {
     399             :         /* assign newline a unique color */
     400         520 :         v->nlcolor = subcolor(v->cm, newline());
     401         520 :         okcolors(v->nfa, v->cm);
     402             :     }
     403        2830 :     CNOERR();
     404        2826 :     v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final);
     405             :     assert(SEE(EOS));           /* even if error; ISERR() => SEE(EOS) */
     406        2826 :     CNOERR();
     407             :     assert(v->tree != NULL);
     408             : 
     409             :     /* finish setup of nfa and its subre tree */
     410        2806 :     specialcolors(v->nfa);
     411        2806 :     CNOERR();
     412             : #ifdef REG_DEBUG
     413             :     if (debug != NULL)
     414             :     {
     415             :         fprintf(debug, "\n\n\n========= RAW ==========\n");
     416             :         dumpnfa(v->nfa, debug);
     417             :         dumpst(v->tree, debug, 1);
     418             :     }
     419             : #endif
     420        2806 :     optst(v, v->tree);
     421        2806 :     v->ntree = numst(v->tree, 1);
     422        2806 :     markst(v->tree);
     423        2806 :     cleanst(v);
     424             : #ifdef REG_DEBUG
     425             :     if (debug != NULL)
     426             :     {
     427             :         fprintf(debug, "\n\n\n========= TREE FIXED ==========\n");
     428             :         dumpst(v->tree, debug, 1);
     429             :     }
     430             : #endif
     431             : 
     432             :     /* build compacted NFAs for tree and lacons */
     433        2806 :     re->re_info |= nfatree(v, v->tree, debug);
     434        2806 :     CNOERR();
     435             :     assert(v->nlacons == 0 || v->lacons != NULL);
     436        2838 :     for (i = 1; i < v->nlacons; i++)
     437             :     {
     438          36 :         struct subre *lasub = &v->lacons[i];
     439             : 
     440             : #ifdef REG_DEBUG
     441             :         if (debug != NULL)
     442             :             fprintf(debug, "\n\n\n========= LA%d ==========\n", i);
     443             : #endif
     444             : 
     445             :         /* Prepend .* to pattern if it's a lookbehind LACON */
     446          36 :         nfanode(v, lasub, !LATYPE_IS_AHEAD(lasub->subno), debug);
     447             :     }
     448        2802 :     CNOERR();
     449        2802 :     if (v->tree->flags & SHORTER)
     450          66 :         NOTE(REG_USHORTEST);
     451             : 
     452             :     /* build compacted NFAs for tree, lacons, fast search */
     453             : #ifdef REG_DEBUG
     454             :     if (debug != NULL)
     455             :         fprintf(debug, "\n\n\n========= SEARCH ==========\n");
     456             : #endif
     457             :     /* can sacrifice main NFA now, so use it as work area */
     458        2802 :     (DISCARD) optimize(v->nfa, debug);
     459        2802 :     CNOERR();
     460        2802 :     makesearch(v, v->nfa);
     461        2802 :     CNOERR();
     462        2802 :     compact(v->nfa, &g->search);
     463        2802 :     CNOERR();
     464             : 
     465             :     /* looks okay, package it up */
     466        2802 :     re->re_nsub = v->nsubexp;
     467        2802 :     v->re = NULL;                /* freev no longer frees re */
     468        2802 :     g->magic = GUTSMAGIC;
     469        2802 :     g->cflags = v->cflags;
     470        2802 :     g->info = re->re_info;
     471        2802 :     g->nsub = re->re_nsub;
     472        2802 :     g->tree = v->tree;
     473        2802 :     v->tree = NULL;
     474        2802 :     g->ntree = v->ntree;
     475        2802 :     g->compare = (v->cflags & REG_ICASE) ? casecmp : cmp;
     476        2802 :     g->lacons = v->lacons;
     477        2802 :     v->lacons = NULL;
     478        2802 :     g->nlacons = v->nlacons;
     479             : 
     480             : #ifdef REG_DEBUG
     481             :     if (flags & REG_DUMP)
     482             :         dump(re, stdout);
     483             : #endif
     484             : 
     485             :     assert(v->err == 0);
     486        2802 :     return freev(v, 0);
     487             : }
     488             : 
     489             : /*
     490             :  * moresubs - enlarge subRE vector
     491             :  */
     492             : static void
     493           0 : moresubs(struct vars *v,
     494             :          int wanted)            /* want enough room for this one */
     495             : {
     496             :     struct subre **p;
     497             :     size_t      n;
     498             : 
     499             :     assert(wanted > 0 && (size_t) wanted >= v->nsubs);
     500           0 :     n = (size_t) wanted * 3 / 2 + 1;
     501             : 
     502           0 :     if (v->subs == v->sub10)
     503             :     {
     504           0 :         p = (struct subre **) MALLOC(n * sizeof(struct subre *));
     505           0 :         if (p != NULL)
     506           0 :             memcpy(VS(p), VS(v->subs),
     507           0 :                    v->nsubs * sizeof(struct subre *));
     508             :     }
     509             :     else
     510           0 :         p = (struct subre **) REALLOC(v->subs, n * sizeof(struct subre *));
     511           0 :     if (p == NULL)
     512             :     {
     513           0 :         ERR(REG_ESPACE);
     514           0 :         return;
     515             :     }
     516           0 :     v->subs = p;
     517           0 :     for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++)
     518           0 :         *p = NULL;
     519             :     assert(v->nsubs == n);
     520             :     assert((size_t) wanted < v->nsubs);
     521             : }
     522             : 
     523             : /*
     524             :  * freev - free vars struct's substructures where necessary
     525             :  *
     526             :  * Optionally does error-number setting, and always returns error code
     527             :  * (if any), to make error-handling code terser.
     528             :  */
     529             : static int
     530        2830 : freev(struct vars *v,
     531             :       int err)
     532             : {
     533        2830 :     if (v->re != NULL)
     534          28 :         rfree(v->re);
     535        2830 :     if (v->subs != v->sub10)
     536           0 :         FREE(v->subs);
     537        2830 :     if (v->nfa != NULL)
     538        2830 :         freenfa(v->nfa);
     539        2830 :     if (v->tree != NULL)
     540           4 :         freesubre(v, v->tree);
     541        2830 :     if (v->treechain != NULL)
     542          20 :         cleanst(v);
     543        2830 :     if (v->cv != NULL)
     544        2830 :         freecvec(v->cv);
     545        2830 :     if (v->cv2 != NULL)
     546           0 :         freecvec(v->cv2);
     547        2830 :     if (v->lacons != NULL)
     548           0 :         freelacons(v->lacons, v->nlacons);
     549        2830 :     ERR(err);                   /* nop if err==0 */
     550             : 
     551        2830 :     return v->err;
     552             : }
     553             : 
     554             : /*
     555             :  * makesearch - turn an NFA into a search NFA (implicit prepend of .*?)
     556             :  * NFA must have been optimize()d already.
     557             :  */
     558             : static void
     559        2810 : makesearch(struct vars *v,
     560             :            struct nfa *nfa)
     561             : {
     562             :     struct arc *a;
     563             :     struct arc *b;
     564        2810 :     struct state *pre = nfa->pre;
     565             :     struct state *s;
     566             :     struct state *s2;
     567             :     struct state *slist;
     568             : 
     569             :     /* no loops are needed if it's anchored */
     570        6648 :     for (a = pre->outs; a != NULL; a = a->outchain)
     571             :     {
     572             :         assert(a->type == PLAIN);
     573        5014 :         if (a->co != nfa->bos[0] && a->co != nfa->bos[1])
     574        1176 :             break;
     575             :     }
     576        2810 :     if (a != NULL)
     577             :     {
     578             :         /* add implicit .* in front */
     579        1176 :         rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre);
     580             : 
     581             :         /* and ^* and \A* too -- not always necessary, but harmless */
     582        1176 :         newarc(nfa, PLAIN, nfa->bos[0], pre, pre);
     583        1176 :         newarc(nfa, PLAIN, nfa->bos[1], pre, pre);
     584             :     }
     585             : 
     586             :     /*
     587             :      * Now here's the subtle part.  Because many REs have no lookback
     588             :      * constraints, often knowing when you were in the pre state tells you
     589             :      * little; it's the next state(s) that are informative.  But some of them
     590             :      * may have other inarcs, i.e. it may be possible to make actual progress
     591             :      * and then return to one of them.  We must de-optimize such cases,
     592             :      * splitting each such state into progress and no-progress states.
     593             :      */
     594             : 
     595             :     /* first, make a list of the states reachable from pre and elsewhere */
     596        2810 :     slist = NULL;
     597       18908 :     for (a = pre->outs; a != NULL; a = a->outchain)
     598             :     {
     599       16098 :         s = a->to;
     600      124320 :         for (b = s->ins; b != NULL; b = b->inchain)
     601             :         {
     602      109700 :             if (b->from != pre)
     603        1478 :                 break;
     604             :         }
     605             : 
     606             :         /*
     607             :          * We want to mark states as being in the list already by having non
     608             :          * NULL tmp fields, but we can't just store the old slist value in tmp
     609             :          * because that doesn't work for the first such state.  Instead, the
     610             :          * first list entry gets its own address in tmp.
     611             :          */
     612       16098 :         if (b != NULL && s->tmp == NULL)
     613             :         {
     614         708 :             s->tmp = (slist != NULL) ? slist : s;
     615         708 :             slist = s;
     616             :         }
     617             :     }
     618             : 
     619             :     /* do the splits */
     620        3518 :     for (s = slist; s != NULL; s = s2)
     621             :     {
     622         708 :         s2 = newstate(nfa);
     623         708 :         NOERR();
     624         708 :         copyouts(nfa, s, s2);
     625         708 :         NOERR();
     626        9214 :         for (a = s->ins; a != NULL; a = b)
     627             :         {
     628        8506 :             b = a->inchain;
     629        8506 :             if (a->from != pre)
     630             :             {
     631        7028 :                 cparc(nfa, a, a->from, s2);
     632        7028 :                 freearc(nfa, a);
     633             :             }
     634             :         }
     635         708 :         s2 = (s->tmp != s) ? s->tmp : NULL;
     636         708 :         s->tmp = NULL;           /* clean up while we're at it */
     637             :     }
     638             : }
     639             : 
     640             : /*
     641             :  * parse - parse an RE
     642             :  *
     643             :  * This is actually just the top level, which parses a bunch of branches
     644             :  * tied together with '|'.  They appear in the tree as the left children
     645             :  * of a chain of '|' subres.
     646             :  */
     647             : static struct subre *
     648        4572 : parse(struct vars *v,
     649             :       int stopper,              /* EOS or ')' */
     650             :       int type,                 /* LACON (lookaround subRE) or PLAIN */
     651             :       struct state *init,       /* initial state */
     652             :       struct state *final)      /* final state */
     653             : {
     654             :     struct state *left;         /* scaffolding for branch */
     655             :     struct state *right;
     656             :     struct subre *branches;     /* top level */
     657             :     struct subre *branch;       /* current branch */
     658             :     struct subre *t;            /* temporary */
     659             :     int         firstbranch;    /* is this the first branch? */
     660             : 
     661             :     assert(stopper == ')' || stopper == EOS);
     662             : 
     663        4572 :     branches = subre(v, '|', LONGER, init, final);
     664        4572 :     NOERRN();
     665        4572 :     branch = branches;
     666        4572 :     firstbranch = 1;
     667             :     do
     668             :     {                           /* a branch */
     669        4712 :         if (!firstbranch)
     670             :         {
     671             :             /* need a place to hang it */
     672         140 :             branch->right = subre(v, '|', LONGER, init, final);
     673         140 :             NOERRN();
     674         140 :             branch = branch->right;
     675             :         }
     676        4712 :         firstbranch = 0;
     677        4712 :         left = newstate(v->nfa);
     678        4712 :         right = newstate(v->nfa);
     679        4712 :         NOERRN();
     680        4712 :         EMPTYARC(init, left);
     681        4712 :         EMPTYARC(right, final);
     682        4712 :         NOERRN();
     683        4712 :         branch->left = parsebranch(v, stopper, type, left, right, 0);
     684        4712 :         NOERRN();
     685        4680 :         branch->flags |= UP(branch->flags | branch->left->flags);
     686        4680 :         if ((branch->flags & ~branches->flags) != 0)  /* new flags */
     687          24 :             for (t = branches; t != branch; t = t->right)
     688          12 :                 t->flags |= branch->flags;
     689        4680 :     } while (EAT('|'));
     690             :     assert(SEE(stopper) || SEE(EOS));
     691             : 
     692        4540 :     if (!SEE(stopper))
     693             :     {
     694             :         assert(stopper == ')' && SEE(EOS));
     695           8 :         ERR(REG_EPAREN);
     696             :     }
     697             : 
     698             :     /* optimize out simple cases */
     699        4540 :     if (branch == branches)
     700             :     {                           /* only one branch */
     701             :         assert(branch->right == NULL);
     702        4408 :         t = branch->left;
     703        4408 :         branch->left = NULL;
     704        4408 :         freesubre(v, branches);
     705        4408 :         branches = t;
     706             :     }
     707         132 :     else if (!MESSY(branches->flags))
     708             :     {                           /* no interesting innards */
     709          56 :         freesubre(v, branches->left);
     710          56 :         branches->left = NULL;
     711          56 :         freesubre(v, branches->right);
     712          56 :         branches->right = NULL;
     713          56 :         branches->op = '=';
     714             :     }
     715             : 
     716        4540 :     return branches;
     717             : }
     718             : 
     719             : /*
     720             :  * parsebranch - parse one branch of an RE
     721             :  *
     722             :  * This mostly manages concatenation, working closely with parseqatom().
     723             :  * Concatenated things are bundled up as much as possible, with separate
     724             :  * ',' nodes introduced only when necessary due to substructure.
     725             :  */
     726             : static struct subre *
     727        6012 : parsebranch(struct vars *v,
     728             :             int stopper,        /* EOS or ')' */
     729             :             int type,           /* LACON (lookaround subRE) or PLAIN */
     730             :             struct state *left, /* leftmost state */
     731             :             struct state *right,    /* rightmost state */
     732             :             int partial)        /* is this only part of a branch? */
     733             : {
     734             :     struct state *lp;           /* left end of current construct */
     735             :     int         seencontent;    /* is there anything in this branch yet? */
     736             :     struct subre *t;
     737             : 
     738        6012 :     lp = left;
     739        6012 :     seencontent = 0;
     740        6012 :     t = subre(v, '=', 0, left, right);  /* op '=' is tentative */
     741        6012 :     NOERRN();
     742       49010 :     while (!SEE('|') && !SEE(stopper) && !SEE(EOS))
     743             :     {
     744       37030 :         if (seencontent)
     745             :         {                       /* implicit concat operator */
     746       31118 :             lp = newstate(v->nfa);
     747       31118 :             NOERRN();
     748       31118 :             moveins(v->nfa, right, lp);
     749             :         }
     750       37030 :         seencontent = 1;
     751             : 
     752             :         /* NB, recursion in parseqatom() may swallow rest of branch */
     753       37030 :         parseqatom(v, stopper, type, lp, right, t);
     754       37030 :         NOERRN();
     755             :     }
     756             : 
     757        5968 :     if (!seencontent)
     758             :     {                           /* empty branch */
     759         100 :         if (!partial)
     760         100 :             NOTE(REG_UUNSPEC);
     761             :         assert(lp == left);
     762         100 :         EMPTYARC(left, right);
     763             :     }
     764             : 
     765        5968 :     return t;
     766             : }
     767             : 
     768             : /*
     769             :  * parseqatom - parse one quantified atom or constraint of an RE
     770             :  *
     771             :  * The bookkeeping near the end cooperates very closely with parsebranch();
     772             :  * in particular, it contains a recursion that can involve parsing the rest
     773             :  * of the branch, making this function's name somewhat inaccurate.
     774             :  */
     775             : static void
     776       37030 : parseqatom(struct vars *v,
     777             :            int stopper,         /* EOS or ')' */
     778             :            int type,            /* LACON (lookaround subRE) or PLAIN */
     779             :            struct state *lp,    /* left state to hang it on */
     780             :            struct state *rp,    /* right state to hang it on */
     781             :            struct subre *top)   /* subtree top */
     782             : {
     783             :     struct state *s;            /* temporaries for new states */
     784             :     struct state *s2;
     785             : 
     786             : #define  ARCV(t, val)    newarc(v->nfa, t, val, lp, rp)
     787             :     int         m,
     788             :                 n;
     789             :     struct subre *atom;         /* atom's subtree */
     790             :     struct subre *t;
     791             :     int         cap;            /* capturing parens? */
     792             :     int         latype;         /* lookaround constraint type */
     793             :     int         subno;          /* capturing-parens or backref number */
     794             :     int         atomtype;
     795             :     int         qprefer;        /* quantifier short/long preference */
     796             :     int         f;
     797             :     struct subre **atomp;       /* where the pointer to atom is */
     798             : 
     799             :     /* initial bookkeeping */
     800       37030 :     atom = NULL;
     801             :     assert(lp->nouts == 0);      /* must string new code */
     802             :     assert(rp->nins == 0);       /* between lp and rp */
     803       37030 :     subno = 0;                  /* just to shut lint up */
     804             : 
     805             :     /* an atom or constraint... */
     806       37030 :     atomtype = v->nexttype;
     807       37030 :     switch (atomtype)
     808             :     {
     809             :             /* first, constraints, which end by returning */
     810             :         case '^':
     811        2146 :             ARCV('^', 1);
     812        2146 :             if (v->cflags & REG_NLANCH)
     813         480 :                 ARCV(BEHIND, v->nlcolor);
     814        2146 :             NEXT();
     815        2146 :             return;
     816             :             break;
     817             :         case '$':
     818        1708 :             ARCV('$', 1);
     819        1708 :             if (v->cflags & REG_NLANCH)
     820         476 :                 ARCV(AHEAD, v->nlcolor);
     821        1708 :             NEXT();
     822        1708 :             return;
     823             :             break;
     824             :         case SBEGIN:
     825           0 :             ARCV('^', 1);       /* BOL */
     826           0 :             ARCV('^', 0);       /* or BOS */
     827           0 :             NEXT();
     828           0 :             return;
     829             :             break;
     830             :         case SEND:
     831           0 :             ARCV('$', 1);       /* EOL */
     832           0 :             ARCV('$', 0);       /* or EOS */
     833           0 :             NEXT();
     834           0 :             return;
     835             :             break;
     836             :         case '<':
     837           4 :             wordchrs(v);        /* does NEXT() */
     838           4 :             s = newstate(v->nfa);
     839           4 :             NOERR();
     840           4 :             nonword(v, BEHIND, lp, s);
     841           4 :             word(v, AHEAD, s, rp);
     842           4 :             return;
     843             :             break;
     844             :         case '>':
     845           4 :             wordchrs(v);        /* does NEXT() */
     846           4 :             s = newstate(v->nfa);
     847           4 :             NOERR();
     848           4 :             word(v, BEHIND, lp, s);
     849           4 :             nonword(v, AHEAD, s, rp);
     850           4 :             return;
     851             :             break;
     852             :         case WBDRY:
     853           0 :             wordchrs(v);        /* does NEXT() */
     854           0 :             s = newstate(v->nfa);
     855           0 :             NOERR();
     856           0 :             nonword(v, BEHIND, lp, s);
     857           0 :             word(v, AHEAD, s, rp);
     858           0 :             s = newstate(v->nfa);
     859           0 :             NOERR();
     860           0 :             word(v, BEHIND, lp, s);
     861           0 :             nonword(v, AHEAD, s, rp);
     862           0 :             return;
     863             :             break;
     864             :         case NWBDRY:
     865           8 :             wordchrs(v);        /* does NEXT() */
     866           8 :             s = newstate(v->nfa);
     867           8 :             NOERR();
     868           8 :             word(v, BEHIND, lp, s);
     869           8 :             word(v, AHEAD, s, rp);
     870           8 :             s = newstate(v->nfa);
     871           8 :             NOERR();
     872           8 :             nonword(v, BEHIND, lp, s);
     873           8 :             nonword(v, AHEAD, s, rp);
     874           8 :             return;
     875             :             break;
     876             :         case LACON:             /* lookaround constraint */
     877         116 :             latype = v->nextvalue;
     878         116 :             NEXT();
     879         116 :             s = newstate(v->nfa);
     880         116 :             s2 = newstate(v->nfa);
     881         116 :             NOERR();
     882         116 :             t = parse(v, ')', LACON, s, s2);
     883         116 :             freesubre(v, t);    /* internal structure irrelevant */
     884         116 :             NOERR();
     885             :             assert(SEE(')'));
     886         108 :             NEXT();
     887         108 :             processlacon(v, s, s2, latype, lp, rp);
     888         108 :             return;
     889             :             break;
     890             :             /* then errors, to get them out of the way */
     891             :         case '*':
     892             :         case '+':
     893             :         case '?':
     894             :         case '{':
     895           0 :             ERR(REG_BADRPT);
     896           0 :             return;
     897             :             break;
     898             :         default:
     899           0 :             ERR(REG_ASSERT);
     900           0 :             return;
     901             :             break;
     902             :             /* then plain characters, and minor variants on that theme */
     903             :         case ')':               /* unbalanced paren */
     904           0 :             if ((v->cflags & REG_ADVANCED) != REG_EXTENDED)
     905             :             {
     906           0 :                 ERR(REG_EPAREN);
     907           0 :                 return;
     908             :             }
     909             :             /* legal in EREs due to specification botch */
     910           0 :             NOTE(REG_UPBOTCH);
     911             :             /* fall through into case PLAIN */
     912             :             /* FALLTHROUGH */
     913             :         case PLAIN:
     914       29994 :             onechr(v, v->nextvalue, lp, rp);
     915       29994 :             okcolors(v->nfa, v->cm);
     916       29994 :             NOERR();
     917       29994 :             NEXT();
     918       29994 :             break;
     919             :         case '[':
     920         594 :             if (v->nextvalue == 1)
     921         216 :                 bracket(v, lp, rp);
     922             :             else
     923         378 :                 cbracket(v, lp, rp);
     924             :             assert(SEE(']') || ISERR());
     925         594 :             NEXT();
     926         594 :             break;
     927             :         case '.':
     928        1270 :             rainbow(v->nfa, v->cm, PLAIN,
     929        1270 :                     (v->cflags & REG_NLSTOP) ? v->nlcolor : COLORLESS,
     930             :                     lp, rp);
     931         782 :             NEXT();
     932         782 :             break;
     933             :             /* and finally the ugly stuff */
     934             :         case '(':               /* value flags as capturing or non */
     935        1630 :             cap = (type == LACON) ? 0 : v->nextvalue;
     936        1630 :             if (cap)
     937             :             {
     938        1514 :                 v->nsubexp++;
     939        1514 :                 subno = v->nsubexp;
     940        1514 :                 if ((size_t) subno >= v->nsubs)
     941           0 :                     moresubs(v, subno);
     942             :                 assert((size_t) subno < v->nsubs);
     943             :             }
     944             :             else
     945         116 :                 atomtype = PLAIN;   /* something that's not '(' */
     946        1630 :             NEXT();
     947             :             /* need new endpoints because tree will contain pointers */
     948        1630 :             s = newstate(v->nfa);
     949        1630 :             s2 = newstate(v->nfa);
     950        1630 :             NOERR();
     951        1630 :             EMPTYARC(lp, s);
     952        1630 :             EMPTYARC(s2, rp);
     953        1630 :             NOERR();
     954        1630 :             atom = parse(v, ')', type, s, s2);
     955             :             assert(SEE(')') || ISERR());
     956        1630 :             NEXT();
     957        1630 :             NOERR();
     958        1618 :             if (cap)
     959             :             {
     960        1506 :                 v->subs[subno] = atom;
     961        1506 :                 t = subre(v, '(', atom->flags | CAP, lp, rp);
     962        1506 :                 NOERR();
     963        1506 :                 t->subno = subno;
     964        1506 :                 t->left = atom;
     965        1506 :                 atom = t;
     966             :             }
     967             :             /* postpone everything else pending possible {0} */
     968        1618 :             break;
     969             :         case BACKREF:           /* the Feature From The Black Lagoon */
     970          44 :             INSIST(type != LACON, REG_ESUBREG);
     971          44 :             INSIST(v->nextvalue < v->nsubs, REG_ESUBREG);
     972          44 :             INSIST(v->subs[v->nextvalue] != NULL, REG_ESUBREG);
     973          44 :             NOERR();
     974             :             assert(v->nextvalue > 0);
     975          36 :             atom = subre(v, 'b', BACKR, lp, rp);
     976          36 :             NOERR();
     977          36 :             subno = v->nextvalue;
     978          36 :             atom->subno = subno;
     979          36 :             EMPTYARC(lp, rp);   /* temporarily, so there's something */
     980          36 :             NEXT();
     981          36 :             break;
     982             :     }
     983             : 
     984             :     /* ...and an atom may be followed by a quantifier */
     985       33024 :     switch (v->nexttype)
     986             :     {
     987             :         case '*':
     988       13234 :             m = 0;
     989       13234 :             n = DUPINF;
     990       13234 :             qprefer = (v->nextvalue) ? LONGER : SHORTER;
     991       13234 :             NEXT();
     992       13234 :             break;
     993             :         case '+':
     994         268 :             m = 1;
     995         268 :             n = DUPINF;
     996         268 :             qprefer = (v->nextvalue) ? LONGER : SHORTER;
     997         268 :             NEXT();
     998         268 :             break;
     999             :         case '?':
    1000          32 :             m = 0;
    1001          32 :             n = 1;
    1002          32 :             qprefer = (v->nextvalue) ? LONGER : SHORTER;
    1003          32 :             NEXT();
    1004          32 :             break;
    1005             :         case '{':
    1006         278 :             NEXT();
    1007         278 :             m = scannum(v);
    1008         278 :             if (EAT(','))
    1009             :             {
    1010         104 :                 if (SEE(DIGIT))
    1011         100 :                     n = scannum(v);
    1012             :                 else
    1013           4 :                     n = DUPINF;
    1014         104 :                 if (m > n)
    1015             :                 {
    1016           4 :                     ERR(REG_BADBR);
    1017           4 :                     return;
    1018             :                 }
    1019             :                 /* {m,n} exercises preference, even if it's {m,m} */
    1020         100 :                 qprefer = (v->nextvalue) ? LONGER : SHORTER;
    1021             :             }
    1022             :             else
    1023             :             {
    1024         174 :                 n = m;
    1025             :                 /* {m} passes operand's preference through */
    1026         174 :                 qprefer = 0;
    1027             :             }
    1028         274 :             if (!SEE('}'))
    1029             :             {                   /* catches errors too */
    1030           0 :                 ERR(REG_BADBR);
    1031           0 :                 return;
    1032             :             }
    1033         274 :             NEXT();
    1034         274 :             break;
    1035             :         default:                /* no quantifier */
    1036       19212 :             m = n = 1;
    1037       19212 :             qprefer = 0;
    1038       19212 :             break;
    1039             :     }
    1040             : 
    1041             :     /* annoying special case:  {0} or {0,0} cancels everything */
    1042       33020 :     if (m == 0 && n == 0)
    1043             :     {
    1044           0 :         if (atom != NULL)
    1045           0 :             freesubre(v, atom);
    1046           0 :         if (atomtype == '(')
    1047           0 :             v->subs[subno] = NULL;
    1048           0 :         delsub(v->nfa, lp, rp);
    1049           0 :         EMPTYARC(lp, rp);
    1050           0 :         return;
    1051             :     }
    1052             : 
    1053             :     /* if not a messy case, avoid hard part */
    1054             :     assert(!MESSY(top->flags));
    1055       33020 :     f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0);
    1056       33020 :     if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f)))
    1057             :     {
    1058       31464 :         if (!(m == 1 && n == 1))
    1059       13460 :             repeat(v, lp, rp, m, n);
    1060       31464 :         if (atom != NULL)
    1061          94 :             freesubre(v, atom);
    1062       31464 :         top->flags = f;
    1063       31464 :         return;
    1064             :     }
    1065             : 
    1066             :     /*
    1067             :      * hard part:  something messy
    1068             :      *
    1069             :      * That is, capturing parens, back reference, short/long clash, or an atom
    1070             :      * with substructure containing one of those.
    1071             :      */
    1072             : 
    1073             :     /* now we'll need a subre for the contents even if they're boring */
    1074        1556 :     if (atom == NULL)
    1075             :     {
    1076           0 :         atom = subre(v, '=', 0, lp, rp);
    1077           0 :         NOERR();
    1078             :     }
    1079             : 
    1080             :     /*----------
    1081             :      * Prepare a general-purpose state skeleton.
    1082             :      *
    1083             :      * In the no-backrefs case, we want this:
    1084             :      *
    1085             :      * [lp] ---> [s] ---prefix---> [begin] ---atom---> [end] ---rest---> [rp]
    1086             :      *
    1087             :      * where prefix is some repetitions of atom.  In the general case we need
    1088             :      *
    1089             :      * [lp] ---> [s] ---iterator---> [s2] ---rest---> [rp]
    1090             :      *
    1091             :      * where the iterator wraps around [begin] ---atom---> [end]
    1092             :      *
    1093             :      * We make the s state here for both cases; s2 is made below if needed
    1094             :      *----------
    1095             :      */
    1096        1556 :     s = newstate(v->nfa);        /* first, new endpoints for the atom */
    1097        1556 :     s2 = newstate(v->nfa);
    1098        1556 :     NOERR();
    1099        1556 :     moveouts(v->nfa, lp, s);
    1100        1556 :     moveins(v->nfa, rp, s2);
    1101        1556 :     NOERR();
    1102        1556 :     atom->begin = s;
    1103        1556 :     atom->end = s2;
    1104        1556 :     s = newstate(v->nfa);        /* set up starting state */
    1105        1556 :     NOERR();
    1106        1556 :     EMPTYARC(lp, s);
    1107        1556 :     NOERR();
    1108             : 
    1109             :     /* break remaining subRE into x{...} and what follows */
    1110        1556 :     t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
    1111        1556 :     NOERR();
    1112        1556 :     t->left = atom;
    1113        1556 :     atomp = &t->left;
    1114             : 
    1115             :     /* here we should recurse... but we must postpone that to the end */
    1116             : 
    1117             :     /* split top into prefix and remaining */
    1118             :     assert(top->op == '=' && top->left == NULL && top->right == NULL);
    1119        1556 :     top->left = subre(v, '=', top->flags, top->begin, lp);
    1120        1556 :     NOERR();
    1121        1556 :     top->op = '.';
    1122        1556 :     top->right = t;
    1123             : 
    1124             :     /* if it's a backref, now is the time to replicate the subNFA */
    1125        1556 :     if (atomtype == BACKREF)
    1126             :     {
    1127             :         assert(atom->begin->nouts == 1);  /* just the EMPTY */
    1128          36 :         delsub(v->nfa, atom->begin, atom->end);
    1129             :         assert(v->subs[subno] != NULL);
    1130             : 
    1131             :         /*
    1132             :          * And here's why the recursion got postponed: it must wait until the
    1133             :          * skeleton is filled in, because it may hit a backref that wants to
    1134             :          * copy the filled-in skeleton.
    1135             :          */
    1136          36 :         dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end,
    1137             :                atom->begin, atom->end);
    1138          36 :         NOERR();
    1139             :     }
    1140             : 
    1141             :     /*
    1142             :      * It's quantifier time.  If the atom is just a backref, we'll let it deal
    1143             :      * with quantifiers internally.
    1144             :      */
    1145        1556 :     if (atomtype == BACKREF)
    1146             :     {
    1147             :         /* special case:  backrefs have internal quantifiers */
    1148          36 :         EMPTYARC(s, atom->begin);    /* empty prefix */
    1149             :         /* just stuff everything into atom */
    1150          36 :         repeat(v, atom->begin, atom->end, m, n);
    1151          36 :         atom->min = (short) m;
    1152          36 :         atom->max = (short) n;
    1153          36 :         atom->flags |= COMBINE(qprefer, atom->flags);
    1154             :         /* rest of branch can be strung starting from atom->end */
    1155          36 :         s2 = atom->end;
    1156             :     }
    1157        1520 :     else if (m == 1 && n == 1 &&
    1158          72 :              (qprefer == 0 ||
    1159         134 :               (atom->flags & (LONGER | SHORTER | MIXED)) == 0 ||
    1160          62 :               qprefer == (atom->flags & (LONGER | SHORTER | MIXED))))
    1161             :     {
    1162             :         /* no/vacuous quantifier:  done */
    1163        1310 :         EMPTYARC(s, atom->begin);    /* empty prefix */
    1164             :         /* rest of branch can be strung starting from atom->end */
    1165        1310 :         s2 = atom->end;
    1166             :     }
    1167         210 :     else if (m > 0 && !(atom->flags & BACKR))
    1168             :     {
    1169             :         /*
    1170             :          * If there's no backrefs involved, we can turn x{m,n} into
    1171             :          * x{m-1,n-1}x, with capturing parens in only the second x.  This is
    1172             :          * valid because we only care about capturing matches from the final
    1173             :          * iteration of the quantifier.  It's a win because we can implement
    1174             :          * the backref-free left side as a plain DFA node, since we don't
    1175             :          * really care where its submatches are.
    1176             :          */
    1177         146 :         dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin);
    1178             :         assert(m >= 1 && m != DUPINF && n >= 1);
    1179         146 :         repeat(v, s, atom->begin, m - 1, (n == DUPINF) ? n : n - 1);
    1180         146 :         f = COMBINE(qprefer, atom->flags);
    1181         146 :         t = subre(v, '.', f, s, atom->end); /* prefix and atom */
    1182         146 :         NOERR();
    1183         146 :         t->left = subre(v, '=', PREF(f), s, atom->begin);
    1184         146 :         NOERR();
    1185         146 :         t->right = atom;
    1186         146 :         *atomp = t;
    1187             :         /* rest of branch can be strung starting from atom->end */
    1188         146 :         s2 = atom->end;
    1189             :     }
    1190             :     else
    1191             :     {
    1192             :         /* general case: need an iteration node */
    1193          64 :         s2 = newstate(v->nfa);
    1194          64 :         NOERR();
    1195          64 :         moveouts(v->nfa, atom->end, s2);
    1196          64 :         NOERR();
    1197          64 :         dupnfa(v->nfa, atom->begin, atom->end, s, s2);
    1198          64 :         repeat(v, s, s2, m, n);
    1199          64 :         f = COMBINE(qprefer, atom->flags);
    1200          64 :         t = subre(v, '*', f, s, s2);
    1201          64 :         NOERR();
    1202          64 :         t->min = (short) m;
    1203          64 :         t->max = (short) n;
    1204          64 :         t->left = atom;
    1205          64 :         *atomp = t;
    1206             :         /* rest of branch is to be strung from iteration's end state */
    1207             :     }
    1208             : 
    1209             :     /* and finally, look after that postponed recursion */
    1210        1556 :     t = top->right;
    1211        1556 :     if (!(SEE('|') || SEE(stopper) || SEE(EOS)))
    1212        1300 :         t->right = parsebranch(v, stopper, type, s2, rp, 1);
    1213             :     else
    1214             :     {
    1215         256 :         EMPTYARC(s2, rp);
    1216         256 :         t->right = subre(v, '=', 0, s2, rp);
    1217             :     }
    1218        1556 :     NOERR();
    1219             :     assert(SEE('|') || SEE(stopper) || SEE(EOS));
    1220        1544 :     t->flags |= COMBINE(t->flags, t->right->flags);
    1221        1544 :     top->flags |= COMBINE(top->flags, t->flags);
    1222             : }
    1223             : 
    1224             : /*
    1225             :  * nonword - generate arcs for non-word-character ahead or behind
    1226             :  */
    1227             : static void
    1228          24 : nonword(struct vars *v,
    1229             :         int dir,                /* AHEAD or BEHIND */
    1230             :         struct state *lp,
    1231             :         struct state *rp)
    1232             : {
    1233          24 :     int         anchor = (dir == AHEAD) ? '$' : '^';
    1234             : 
    1235             :     assert(dir == AHEAD || dir == BEHIND);
    1236          24 :     newarc(v->nfa, anchor, 1, lp, rp);
    1237          24 :     newarc(v->nfa, anchor, 0, lp, rp);
    1238          24 :     colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
    1239             :     /* (no need for special attention to \n) */
    1240          24 : }
    1241             : 
    1242             : /*
    1243             :  * word - generate arcs for word character ahead or behind
    1244             :  */
    1245             : static void
    1246          24 : word(struct vars *v,
    1247             :      int dir,                   /* AHEAD or BEHIND */
    1248             :      struct state *lp,
    1249             :      struct state *rp)
    1250             : {
    1251             :     assert(dir == AHEAD || dir == BEHIND);
    1252          24 :     cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
    1253             :     /* (no need for special attention to \n) */
    1254          24 : }
    1255             : 
    1256             : /*
    1257             :  * scannum - scan a number
    1258             :  */
    1259             : static int                      /* value, <= DUPMAX */
    1260         378 : scannum(struct vars *v)
    1261             : {
    1262         378 :     int         n = 0;
    1263             : 
    1264        1178 :     while (SEE(DIGIT) && n < DUPMAX)
    1265             :     {
    1266         422 :         n = n * 10 + v->nextvalue;
    1267         422 :         NEXT();
    1268             :     }
    1269         378 :     if (SEE(DIGIT) || n > DUPMAX)
    1270             :     {
    1271           0 :         ERR(REG_BADBR);
    1272           0 :         return 0;
    1273             :     }
    1274         378 :     return n;
    1275             : }
    1276             : 
    1277             : /*
    1278             :  * repeat - replicate subNFA for quantifiers
    1279             :  *
    1280             :  * The sub-NFA strung from lp to rp is modified to represent m to n
    1281             :  * repetitions of its initial contents.
    1282             :  *
    1283             :  * The duplication sequences used here are chosen carefully so that any
    1284             :  * pointers starting out pointing into the subexpression end up pointing into
    1285             :  * the last occurrence.  (Note that it may not be strung between the same
    1286             :  * left and right end states, however!)  This used to be important for the
    1287             :  * subRE tree, although the important bits are now handled by the in-line
    1288             :  * code in parse(), and when this is called, it doesn't matter any more.
    1289             :  */
    1290             : static void
    1291       14646 : repeat(struct vars *v,
    1292             :        struct state *lp,
    1293             :        struct state *rp,
    1294             :        int m,
    1295             :        int n)
    1296             : {
    1297             : #define  SOME    2
    1298             : #define  INF     3
    1299             : #define  PAIR(x, y)  ((x)*4 + (y))
    1300             : #define  REDUCE(x)   ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )
    1301       14646 :     const int   rm = REDUCE(m);
    1302       14646 :     const int   rn = REDUCE(n);
    1303             :     struct state *s;
    1304             :     struct state *s2;
    1305             : 
    1306       14646 :     switch (PAIR(rm, rn))
    1307             :     {
    1308             :         case PAIR(0, 0):        /* empty string */
    1309          30 :             delsub(v->nfa, lp, rp);
    1310          30 :             EMPTYARC(lp, rp);
    1311          30 :             break;
    1312             :         case PAIR(0, 1):        /* do as x| */
    1313          32 :             EMPTYARC(lp, rp);
    1314          32 :             break;
    1315             :         case PAIR(0, SOME):     /* do as x{1,n}| */
    1316           0 :             repeat(v, lp, rp, 1, n);
    1317           0 :             NOERR();
    1318           0 :             EMPTYARC(lp, rp);
    1319           0 :             break;
    1320             :         case PAIR(0, INF):      /* loop x around */
    1321       13342 :             s = newstate(v->nfa);
    1322       13342 :             NOERR();
    1323       13342 :             moveouts(v->nfa, lp, s);
    1324       13342 :             moveins(v->nfa, rp, s);
    1325       13342 :             EMPTYARC(lp, s);
    1326       13342 :             EMPTYARC(s, rp);
    1327       13342 :             break;
    1328             :         case PAIR(1, 1):        /* no action required */
    1329         138 :             break;
    1330             :         case PAIR(1, SOME):     /* do as x{0,n-1}x = (x{1,n-1}|)x */
    1331           0 :             s = newstate(v->nfa);
    1332           0 :             NOERR();
    1333           0 :             moveouts(v->nfa, lp, s);
    1334           0 :             dupnfa(v->nfa, s, rp, lp, s);
    1335           0 :             NOERR();
    1336           0 :             repeat(v, lp, s, 1, n - 1);
    1337           0 :             NOERR();
    1338           0 :             EMPTYARC(lp, s);
    1339           0 :             break;
    1340             :         case PAIR(1, INF):      /* add loopback arc */
    1341         164 :             s = newstate(v->nfa);
    1342         164 :             s2 = newstate(v->nfa);
    1343         164 :             NOERR();
    1344         164 :             moveouts(v->nfa, lp, s);
    1345         164 :             moveins(v->nfa, rp, s2);
    1346         164 :             EMPTYARC(lp, s);
    1347         164 :             EMPTYARC(s2, rp);
    1348         164 :             EMPTYARC(s2, s);
    1349         164 :             break;
    1350             :         case PAIR(SOME, SOME):  /* do as x{m-1,n-1}x */
    1351         804 :             s = newstate(v->nfa);
    1352         804 :             NOERR();
    1353         804 :             moveouts(v->nfa, lp, s);
    1354         804 :             dupnfa(v->nfa, s, rp, lp, s);
    1355         804 :             NOERR();
    1356         804 :             repeat(v, lp, s, m - 1, n - 1);
    1357         804 :             break;
    1358             :         case PAIR(SOME, INF):   /* do as x{m-1,}x */
    1359         136 :             s = newstate(v->nfa);
    1360         136 :             NOERR();
    1361         136 :             moveouts(v->nfa, lp, s);
    1362         136 :             dupnfa(v->nfa, s, rp, lp, s);
    1363         136 :             NOERR();
    1364         136 :             repeat(v, lp, s, m - 1, n);
    1365         136 :             break;
    1366             :         default:
    1367           0 :             ERR(REG_ASSERT);
    1368           0 :             break;
    1369             :     }
    1370             : }
    1371             : 
    1372             : /*
    1373             :  * bracket - handle non-complemented bracket expression
    1374             :  * Also called from cbracket for complemented bracket expressions.
    1375             :  */
    1376             : static void
    1377         606 : bracket(struct vars *v,
    1378             :         struct state *lp,
    1379             :         struct state *rp)
    1380             : {
    1381             :     assert(SEE('['));
    1382         606 :     NEXT();
    1383        2190 :     while (!SEE(']') && !SEE(EOS))
    1384         978 :         brackpart(v, lp, rp);
    1385             :     assert(SEE(']') || ISERR());
    1386         606 :     okcolors(v->nfa, v->cm);
    1387         606 : }
    1388             : 
    1389             : /*
    1390             :  * cbracket - handle complemented bracket expression
    1391             :  * We do it by calling bracket() with dummy endpoints, and then complementing
    1392             :  * the result.  The alternative would be to invoke rainbow(), and then delete
    1393             :  * arcs as the b.e. is seen... but that gets messy.
    1394             :  */
    1395             : static void
    1396         378 : cbracket(struct vars *v,
    1397             :          struct state *lp,
    1398             :          struct state *rp)
    1399             : {
    1400         378 :     struct state *left = newstate(v->nfa);
    1401         378 :     struct state *right = newstate(v->nfa);
    1402             : 
    1403         378 :     NOERR();
    1404         378 :     bracket(v, left, right);
    1405         378 :     if (v->cflags & REG_NLSTOP)
    1406           0 :         newarc(v->nfa, PLAIN, v->nlcolor, left, right);
    1407         378 :     NOERR();
    1408             : 
    1409             :     assert(lp->nouts == 0);      /* all outarcs will be ours */
    1410             : 
    1411             :     /*
    1412             :      * Easy part of complementing, and all there is to do since the MCCE code
    1413             :      * was removed.
    1414             :      */
    1415         378 :     colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp);
    1416         378 :     NOERR();
    1417         378 :     dropstate(v->nfa, left);
    1418             :     assert(right->nins == 0);
    1419         378 :     freestate(v->nfa, right);
    1420             : }
    1421             : 
    1422             : /*
    1423             :  * brackpart - handle one item (or range) within a bracket expression
    1424             :  */
    1425             : static void
    1426         978 : brackpart(struct vars *v,
    1427             :           struct state *lp,
    1428             :           struct state *rp)
    1429             : {
    1430             :     chr         startc;
    1431             :     chr         endc;
    1432             :     struct cvec *cv;
    1433             :     const chr  *startp;
    1434             :     const chr  *endp;
    1435             :     chr         c[1];
    1436             : 
    1437             :     /* parse something, get rid of special cases, take shortcuts */
    1438         978 :     switch (v->nexttype)
    1439             :     {
    1440             :         case RANGE:             /* a-b-c or other botch */
    1441           0 :             ERR(REG_ERANGE);
    1442         582 :             return;
    1443             :             break;
    1444             :         case PLAIN:
    1445         858 :             c[0] = v->nextvalue;
    1446         858 :             NEXT();
    1447             :             /* shortcut for ordinary chr (not range) */
    1448         858 :             if (!SEE(RANGE))
    1449             :             {
    1450         462 :                 onechr(v, c[0], lp, rp);
    1451         462 :                 return;
    1452             :             }
    1453         396 :             startc = element(v, c, c + 1);
    1454         396 :             NOERR();
    1455         396 :             break;
    1456             :         case COLLEL:
    1457           0 :             startp = v->now;
    1458           0 :             endp = scanplain(v);
    1459           0 :             INSIST(startp < endp, REG_ECOLLATE);
    1460           0 :             NOERR();
    1461           0 :             startc = element(v, startp, endp);
    1462           0 :             NOERR();
    1463           0 :             break;
    1464             :         case ECLASS:
    1465           0 :             startp = v->now;
    1466           0 :             endp = scanplain(v);
    1467           0 :             INSIST(startp < endp, REG_ECOLLATE);
    1468           0 :             NOERR();
    1469           0 :             startc = element(v, startp, endp);
    1470           0 :             NOERR();
    1471           0 :             cv = eclass(v, startc, (v->cflags & REG_ICASE));
    1472           0 :             NOERR();
    1473           0 :             subcolorcvec(v, cv, lp, rp);
    1474           0 :             return;
    1475             :             break;
    1476             :         case CCLASS:
    1477         120 :             startp = v->now;
    1478         120 :             endp = scanplain(v);
    1479         120 :             INSIST(startp < endp, REG_ECTYPE);
    1480         120 :             NOERR();
    1481         120 :             cv = cclass(v, startp, endp, (v->cflags & REG_ICASE));
    1482         120 :             NOERR();
    1483         120 :             subcolorcvec(v, cv, lp, rp);
    1484         120 :             return;
    1485             :             break;
    1486             :         default:
    1487           0 :             ERR(REG_ASSERT);
    1488           0 :             return;
    1489             :             break;
    1490             :     }
    1491             : 
    1492         396 :     if (SEE(RANGE))
    1493             :     {
    1494         396 :         NEXT();
    1495         396 :         switch (v->nexttype)
    1496             :         {
    1497             :             case PLAIN:
    1498             :             case RANGE:
    1499         396 :                 c[0] = v->nextvalue;
    1500         396 :                 NEXT();
    1501         396 :                 endc = element(v, c, c + 1);
    1502         396 :                 NOERR();
    1503         396 :                 break;
    1504             :             case COLLEL:
    1505           0 :                 startp = v->now;
    1506           0 :                 endp = scanplain(v);
    1507           0 :                 INSIST(startp < endp, REG_ECOLLATE);
    1508           0 :                 NOERR();
    1509           0 :                 endc = element(v, startp, endp);
    1510           0 :                 NOERR();
    1511           0 :                 break;
    1512             :             default:
    1513           0 :                 ERR(REG_ERANGE);
    1514           0 :                 return;
    1515             :                 break;
    1516             :         }
    1517             :     }
    1518             :     else
    1519           0 :         endc = startc;
    1520             : 
    1521             :     /*
    1522             :      * Ranges are unportable.  Actually, standard C does guarantee that digits
    1523             :      * are contiguous, but making that an exception is just too complicated.
    1524             :      */
    1525         396 :     if (startc != endc)
    1526         396 :         NOTE(REG_UUNPORT);
    1527         396 :     cv = range(v, startc, endc, (v->cflags & REG_ICASE));
    1528         396 :     NOERR();
    1529         396 :     subcolorcvec(v, cv, lp, rp);
    1530             : }
    1531             : 
    1532             : /*
    1533             :  * scanplain - scan PLAIN contents of [. etc.
    1534             :  *
    1535             :  * Certain bits of trickery in lex.c know that this code does not try
    1536             :  * to look past the final bracket of the [. etc.
    1537             :  */
    1538             : static const chr *              /* just after end of sequence */
    1539         120 : scanplain(struct vars *v)
    1540             : {
    1541             :     const chr  *endp;
    1542             : 
    1543             :     assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
    1544         120 :     NEXT();
    1545             : 
    1546         120 :     endp = v->now;
    1547         840 :     while (SEE(PLAIN))
    1548             :     {
    1549         600 :         endp = v->now;
    1550         600 :         NEXT();
    1551             :     }
    1552             : 
    1553             :     assert(SEE(END) || ISERR());
    1554         120 :     NEXT();
    1555             : 
    1556         120 :     return endp;
    1557             : }
    1558             : 
    1559             : /*
    1560             :  * onechr - fill in arcs for a plain character, and possible case complements
    1561             :  * This is mostly a shortcut for efficient handling of the common case.
    1562             :  */
    1563             : static void
    1564       30456 : onechr(struct vars *v,
    1565             :        chr c,
    1566             :        struct state *lp,
    1567             :        struct state *rp)
    1568             : {
    1569       30456 :     if (!(v->cflags & REG_ICASE))
    1570             :     {
    1571       29540 :         color       lastsubcolor = COLORLESS;
    1572             : 
    1573       29540 :         subcoloronechr(v, c, lp, rp, &lastsubcolor);
    1574       29540 :         return;
    1575             :     }
    1576             : 
    1577             :     /* rats, need general case anyway... */
    1578         916 :     subcolorcvec(v, allcases(v, c), lp, rp);
    1579             : }
    1580             : 
    1581             : /*
    1582             :  * wordchrs - set up word-chr list for word-boundary stuff, if needed
    1583             :  *
    1584             :  * The list is kept as a bunch of arcs between two dummy states; it's
    1585             :  * disposed of by the unreachable-states sweep in NFA optimization.
    1586             :  * Does NEXT().  Must not be called from any unusual lexical context.
    1587             :  * This should be reconciled with the \w etc. handling in lex.c, and
    1588             :  * should be cleaned up to reduce dependencies on input scanning.
    1589             :  */
    1590             : static void
    1591          16 : wordchrs(struct vars *v)
    1592             : {
    1593             :     struct state *left;
    1594             :     struct state *right;
    1595             : 
    1596          16 :     if (v->wordchrs != NULL)
    1597             :     {
    1598           4 :         NEXT();                 /* for consistency */
    1599           4 :         return;
    1600             :     }
    1601             : 
    1602          12 :     left = newstate(v->nfa);
    1603          12 :     right = newstate(v->nfa);
    1604          12 :     NOERR();
    1605             :     /* fine point:  implemented with [::], and lexer will set REG_ULOCALE */
    1606          12 :     lexword(v);
    1607          12 :     NEXT();
    1608             :     assert(v->savenow != NULL && SEE('['));
    1609          12 :     bracket(v, left, right);
    1610             :     assert((v->savenow != NULL && SEE(']')) || ISERR());
    1611          12 :     NEXT();
    1612          12 :     NOERR();
    1613          12 :     v->wordchrs = left;
    1614             : }
    1615             : 
    1616             : /*
    1617             :  * processlacon - generate the NFA representation of a LACON
    1618             :  *
    1619             :  * In the general case this is just newlacon() + newarc(), but some cases
    1620             :  * can be optimized.
    1621             :  */
    1622             : static void
    1623         108 : processlacon(struct vars *v,
    1624             :              struct state *begin,   /* start of parsed LACON sub-re */
    1625             :              struct state *end, /* end of parsed LACON sub-re */
    1626             :              int latype,
    1627             :              struct state *lp,  /* left state to hang it on */
    1628             :              struct state *rp)  /* right state to hang it on */
    1629             : {
    1630             :     struct state *s1;
    1631             :     int         n;
    1632             : 
    1633             :     /*
    1634             :      * Check for lookaround RE consisting of a single plain color arc (or set
    1635             :      * of arcs); this would typically be a simple chr or a bracket expression.
    1636             :      */
    1637         108 :     s1 = single_color_transition(begin, end);
    1638         108 :     switch (latype)
    1639             :     {
    1640             :         case LATYPE_AHEAD_POS:
    1641             :             /* If lookahead RE is just colorset C, convert to AHEAD(C) */
    1642          24 :             if (s1 != NULL)
    1643             :             {
    1644          20 :                 cloneouts(v->nfa, s1, lp, rp, AHEAD);
    1645          20 :                 return;
    1646             :             }
    1647           4 :             break;
    1648             :         case LATYPE_AHEAD_NEG:
    1649             :             /* If lookahead RE is just colorset C, convert to AHEAD(^C)|$ */
    1650          32 :             if (s1 != NULL)
    1651             :             {
    1652           8 :                 colorcomplement(v->nfa, v->cm, AHEAD, s1, lp, rp);
    1653           8 :                 newarc(v->nfa, '$', 1, lp, rp);
    1654           8 :                 newarc(v->nfa, '$', 0, lp, rp);
    1655           8 :                 return;
    1656             :             }
    1657          24 :             break;
    1658             :         case LATYPE_BEHIND_POS:
    1659             :             /* If lookbehind RE is just colorset C, convert to BEHIND(C) */
    1660          36 :             if (s1 != NULL)
    1661             :             {
    1662          28 :                 cloneouts(v->nfa, s1, lp, rp, BEHIND);
    1663          28 :                 return;
    1664             :             }
    1665           8 :             break;
    1666             :         case LATYPE_BEHIND_NEG:
    1667             :             /* If lookbehind RE is just colorset C, convert to BEHIND(^C)|^ */
    1668          16 :             if (s1 != NULL)
    1669             :             {
    1670          16 :                 colorcomplement(v->nfa, v->cm, BEHIND, s1, lp, rp);
    1671          16 :                 newarc(v->nfa, '^', 1, lp, rp);
    1672          16 :                 newarc(v->nfa, '^', 0, lp, rp);
    1673          16 :                 return;
    1674             :             }
    1675           0 :             break;
    1676             :         default:
    1677             :             assert(NOTREACHED);
    1678             :     }
    1679             : 
    1680             :     /* General case: we need a LACON subre and arc */
    1681          36 :     n = newlacon(v, begin, end, latype);
    1682          36 :     newarc(v->nfa, LACON, n, lp, rp);
    1683             : }
    1684             : 
    1685             : /*
    1686             :  * subre - allocate a subre
    1687             :  */
    1688             : static struct subre *
    1689       15990 : subre(struct vars *v,
    1690             :       int op,
    1691             :       int flags,
    1692             :       struct state *begin,
    1693             :       struct state *end)
    1694             : {
    1695       15990 :     struct subre *ret = v->treefree;
    1696             : 
    1697             :     /*
    1698             :      * Checking for stack overflow here is sufficient to protect parse() and
    1699             :      * its recursive subroutines.
    1700             :      */
    1701       15990 :     if (STACK_TOO_DEEP(v->re))
    1702             :     {
    1703           0 :         ERR(REG_ETOOBIG);
    1704           0 :         return NULL;
    1705             :     }
    1706             : 
    1707       15990 :     if (ret != NULL)
    1708        1676 :         v->treefree = ret->left;
    1709             :     else
    1710             :     {
    1711       14314 :         ret = (struct subre *) MALLOC(sizeof(struct subre));
    1712       14314 :         if (ret == NULL)
    1713             :         {
    1714           0 :             ERR(REG_ESPACE);
    1715           0 :             return NULL;
    1716             :         }
    1717       14314 :         ret->chain = v->treechain;
    1718       14314 :         v->treechain = ret;
    1719             :     }
    1720             : 
    1721             :     assert(strchr("=b|.*(", op) != NULL);
    1722             : 
    1723       15990 :     ret->op = op;
    1724       15990 :     ret->flags = flags;
    1725       15990 :     ret->id = 0;             /* will be assigned later */
    1726       15990 :     ret->subno = 0;
    1727       15990 :     ret->min = ret->max = 1;
    1728       15990 :     ret->left = NULL;
    1729       15990 :     ret->right = NULL;
    1730       15990 :     ret->begin = begin;
    1731       15990 :     ret->end = end;
    1732       15990 :     ZAPCNFA(ret->cnfa);
    1733             : 
    1734       15990 :     return ret;
    1735             : }
    1736             : 
    1737             : /*
    1738             :  * freesubre - free a subRE subtree
    1739             :  */
    1740             : static void
    1741        5786 : freesubre(struct vars *v,       /* might be NULL */
    1742             :           struct subre *sr)
    1743             : {
    1744        5786 :     if (sr == NULL)
    1745           8 :         return;
    1746             : 
    1747        5778 :     if (sr->left != NULL)
    1748         474 :         freesubre(v, sr->left);
    1749        5778 :     if (sr->right != NULL)
    1750         294 :         freesubre(v, sr->right);
    1751             : 
    1752        5778 :     freesrnode(v, sr);
    1753             : }
    1754             : 
    1755             : /*
    1756             :  * freesrnode - free one node in a subRE subtree
    1757             :  */
    1758             : static void
    1759        5778 : freesrnode(struct vars *v,      /* might be NULL */
    1760             :            struct subre *sr)
    1761             : {
    1762        5778 :     if (sr == NULL)
    1763           0 :         return;
    1764             : 
    1765        5778 :     if (!NULLCNFA(sr->cnfa))
    1766         980 :         freecnfa(&sr->cnfa);
    1767        5778 :     sr->flags = 0;
    1768             : 
    1769        5778 :     if (v != NULL && v->treechain != NULL)
    1770             :     {
    1771             :         /* we're still parsing, maybe we can reuse the subre */
    1772        4794 :         sr->left = v->treefree;
    1773        4794 :         v->treefree = sr;
    1774             :     }
    1775             :     else
    1776         984 :         FREE(sr);
    1777             : }
    1778             : 
    1779             : /*
    1780             :  * optst - optimize a subRE subtree
    1781             :  */
    1782             : static void
    1783        2806 : optst(struct vars *v,
    1784             :       struct subre *t)
    1785             : {
    1786             :     /*
    1787             :      * DGP (2007-11-13): I assume it was the programmer's intent to eventually
    1788             :      * come back and add code to optimize subRE trees, but the routine coded
    1789             :      * just spends effort traversing the tree and doing nothing. We can do
    1790             :      * nothing with less effort.
    1791             :      */
    1792        2806 :     return;
    1793             : }
    1794             : 
    1795             : /*
    1796             :  * numst - number tree nodes (assigning "id" indexes)
    1797             :  */
    1798             : static int                      /* next number */
    1799       11056 : numst(struct subre *t,
    1800             :       int start)                /* starting point for subtree numbers */
    1801             : {
    1802             :     int         i;
    1803             : 
    1804             :     assert(t != NULL);
    1805             : 
    1806       11056 :     i = start;
    1807       11056 :     t->id = (short) i++;
    1808       11056 :     if (t->left != NULL)
    1809        4940 :         i = numst(t->left, i);
    1810       11056 :     if (t->right != NULL)
    1811        3310 :         i = numst(t->right, i);
    1812       11056 :     return i;
    1813             : }
    1814             : 
    1815             : /*
    1816             :  * markst - mark tree nodes as INUSE
    1817             :  *
    1818             :  * Note: this is a great deal more subtle than it looks.  During initial
    1819             :  * parsing of a regex, all subres are linked into the treechain list;
    1820             :  * discarded ones are also linked into the treefree list for possible reuse.
    1821             :  * After we are done creating all subres required for a regex, we run markst()
    1822             :  * then cleanst(), which results in discarding all subres not reachable from
    1823             :  * v->tree.  We then clear v->treechain, indicating that subres must be found
    1824             :  * by descending from v->tree.  This changes the behavior of freesubre(): it
    1825             :  * will henceforth FREE() unwanted subres rather than sticking them into the
    1826             :  * treefree list.  (Doing that any earlier would result in dangling links in
    1827             :  * the treechain list.)  This all means that freev() will clean up correctly
    1828             :  * if invoked before or after markst()+cleanst(); but it would not work if
    1829             :  * called partway through this state conversion, so we mustn't error out
    1830             :  * in or between these two functions.
    1831             :  */
    1832             : static void
    1833       11056 : markst(struct subre *t)
    1834             : {
    1835             :     assert(t != NULL);
    1836             : 
    1837       11056 :     t->flags |= INUSE;
    1838       11056 :     if (t->left != NULL)
    1839        4940 :         markst(t->left);
    1840       11056 :     if (t->right != NULL)
    1841        3310 :         markst(t->right);
    1842       11056 : }
    1843             : 
    1844             : /*
    1845             :  * cleanst - free any tree nodes not marked INUSE
    1846             :  */
    1847             : static void
    1848        2826 : cleanst(struct vars *v)
    1849             : {
    1850             :     struct subre *t;
    1851             :     struct subre *next;
    1852             : 
    1853       17140 :     for (t = v->treechain; t != NULL; t = next)
    1854             :     {
    1855       14314 :         next = t->chain;
    1856       14314 :         if (!(t->flags & INUSE))
    1857        3258 :             FREE(t);
    1858             :     }
    1859        2826 :     v->treechain = NULL;
    1860        2826 :     v->treefree = NULL;          /* just on general principles */
    1861        2826 : }
    1862             : 
    1863             : /*
    1864             :  * nfatree - turn a subRE subtree into a tree of compacted NFAs
    1865             :  */
    1866             : static long                     /* optimize results from top node */
    1867       11056 : nfatree(struct vars *v,
    1868             :         struct subre *t,
    1869             :         FILE *f)                /* for debug output */
    1870             : {
    1871             :     assert(t != NULL && t->begin != NULL);
    1872             : 
    1873       11056 :     if (t->left != NULL)
    1874        4940 :         (DISCARD) nfatree(v, t->left, f);
    1875       11056 :     if (t->right != NULL)
    1876        3310 :         (DISCARD) nfatree(v, t->right, f);
    1877             : 
    1878       11056 :     return nfanode(v, t, 0, f);
    1879             : }
    1880             : 
    1881             : /*
    1882             :  * nfanode - do one NFA for nfatree or lacons
    1883             :  *
    1884             :  * If converttosearch is true, apply makesearch() to the NFA.
    1885             :  */
    1886             : static long                     /* optimize results */
    1887       11092 : nfanode(struct vars *v,
    1888             :         struct subre *t,
    1889             :         int converttosearch,
    1890             :         FILE *f)                /* for debug output */
    1891             : {
    1892             :     struct nfa *nfa;
    1893       11092 :     long        ret = 0;
    1894             : 
    1895             :     assert(t->begin != NULL);
    1896             : 
    1897             : #ifdef REG_DEBUG
    1898             :     if (f != NULL)
    1899             :     {
    1900             :         char        idbuf[50];
    1901             : 
    1902             :         fprintf(f, "\n\n\n========= TREE NODE %s ==========\n",
    1903             :                 stid(t, idbuf, sizeof(idbuf)));
    1904             :     }
    1905             : #endif
    1906       11092 :     nfa = newnfa(v, v->cm, v->nfa);
    1907       11092 :     NOERRZ();
    1908       11092 :     dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final);
    1909       11092 :     if (!ISERR())
    1910       11092 :         specialcolors(nfa);
    1911       11092 :     if (!ISERR())
    1912       11092 :         ret = optimize(nfa, f);
    1913       11092 :     if (converttosearch && !ISERR())
    1914           8 :         makesearch(v, nfa);
    1915       11092 :     if (!ISERR())
    1916       11088 :         compact(nfa, &t->cnfa);
    1917             : 
    1918       11092 :     freenfa(nfa);
    1919       11092 :     return ret;
    1920             : }
    1921             : 
    1922             : /*
    1923             :  * newlacon - allocate a lookaround-constraint subRE
    1924             :  */
    1925             : static int                      /* lacon number */
    1926          36 : newlacon(struct vars *v,
    1927             :          struct state *begin,
    1928             :          struct state *end,
    1929             :          int latype)
    1930             : {
    1931             :     int         n;
    1932             :     struct subre *newlacons;
    1933             :     struct subre *sub;
    1934             : 
    1935          36 :     if (v->nlacons == 0)
    1936             :     {
    1937          28 :         n = 1;                  /* skip 0th */
    1938          28 :         newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
    1939             :     }
    1940             :     else
    1941             :     {
    1942           8 :         n = v->nlacons;
    1943           8 :         newlacons = (struct subre *) REALLOC(v->lacons,
    1944             :                                              (n + 1) * sizeof(struct subre));
    1945             :     }
    1946          36 :     if (newlacons == NULL)
    1947             :     {
    1948           0 :         ERR(REG_ESPACE);
    1949           0 :         return 0;
    1950             :     }
    1951          36 :     v->lacons = newlacons;
    1952          36 :     v->nlacons = n + 1;
    1953          36 :     sub = &v->lacons[n];
    1954          36 :     sub->begin = begin;
    1955          36 :     sub->end = end;
    1956          36 :     sub->subno = latype;
    1957          36 :     ZAPCNFA(sub->cnfa);
    1958          36 :     return n;
    1959             : }
    1960             : 
    1961             : /*
    1962             :  * freelacons - free lookaround-constraint subRE vector
    1963             :  */
    1964             : static void
    1965          14 : freelacons(struct subre *subs,
    1966             :            int n)
    1967             : {
    1968             :     struct subre *sub;
    1969             :     int         i;
    1970             : 
    1971             :     assert(n > 0);
    1972          28 :     for (sub = subs + 1, i = n - 1; i > 0; sub++, i--)   /* no 0th */
    1973          14 :         if (!NULLCNFA(sub->cnfa))
    1974          14 :             freecnfa(&sub->cnfa);
    1975          14 :     FREE(subs);
    1976          14 : }
    1977             : 
    1978             : /*
    1979             :  * rfree - free a whole RE (insides of regfree)
    1980             :  */
    1981             : static void
    1982         312 : rfree(regex_t *re)
    1983             : {
    1984             :     struct guts *g;
    1985             : 
    1986         312 :     if (re == NULL || re->re_magic != REMAGIC)
    1987           0 :         return;
    1988             : 
    1989         312 :     re->re_magic = 0;            /* invalidate RE */
    1990         312 :     g = (struct guts *) re->re_guts;
    1991         312 :     re->re_guts = NULL;
    1992         312 :     re->re_fns = NULL;
    1993         312 :     if (g != NULL)
    1994             :     {
    1995         312 :         g->magic = 0;
    1996         312 :         freecm(&g->cmap);
    1997         312 :         if (g->tree != NULL)
    1998         284 :             freesubre((struct vars *) NULL, g->tree);
    1999         312 :         if (g->lacons != NULL)
    2000          14 :             freelacons(g->lacons, g->nlacons);
    2001         312 :         if (!NULLCNFA(g->search))
    2002         284 :             freecnfa(&g->search);
    2003         312 :         FREE(g);
    2004             :     }
    2005             : }
    2006             : 
    2007             : /*
    2008             :  * rcancelrequested - check for external request to cancel regex operation
    2009             :  *
    2010             :  * Return nonzero to fail the operation with error code REG_CANCEL,
    2011             :  * zero to keep going
    2012             :  *
    2013             :  * The current implementation is Postgres-specific.  If we ever get around
    2014             :  * to splitting the regex code out as a standalone library, there will need
    2015             :  * to be some API to let applications define a callback function for this.
    2016             :  */
    2017             : static int
    2018    11789458 : rcancelrequested(void)
    2019             : {
    2020    11789458 :     return InterruptPending && (QueryCancelPending || ProcDiePending);
    2021             : }
    2022             : 
    2023             : /*
    2024             :  * rstacktoodeep - check for stack getting dangerously deep
    2025             :  *
    2026             :  * Return nonzero to fail the operation with error code REG_ETOOBIG,
    2027             :  * zero to keep going
    2028             :  *
    2029             :  * The current implementation is Postgres-specific.  If we ever get around
    2030             :  * to splitting the regex code out as a standalone library, there will need
    2031             :  * to be some API to let applications define a callback function for this.
    2032             :  */
    2033             : static int
    2034    12162376 : rstacktoodeep(void)
    2035             : {
    2036    12162376 :     return stack_is_too_deep();
    2037             : }
    2038             : 
    2039             : #ifdef REG_DEBUG
    2040             : 
    2041             : /*
    2042             :  * dump - dump an RE in human-readable form
    2043             :  */
    2044             : static void
    2045             : dump(regex_t *re,
    2046             :      FILE *f)
    2047             : {
    2048             :     struct guts *g;
    2049             :     int         i;
    2050             : 
    2051             :     if (re->re_magic != REMAGIC)
    2052             :         fprintf(f, "bad magic number (0x%x not 0x%x)\n", re->re_magic,
    2053             :                 REMAGIC);
    2054             :     if (re->re_guts == NULL)
    2055             :     {
    2056             :         fprintf(f, "NULL guts!!!\n");
    2057             :         return;
    2058             :     }
    2059             :     g = (struct guts *) re->re_guts;
    2060             :     if (g->magic != GUTSMAGIC)
    2061             :         fprintf(f, "bad guts magic number (0x%x not 0x%x)\n", g->magic,
    2062             :                 GUTSMAGIC);
    2063             : 
    2064             :     fprintf(f, "\n\n\n========= DUMP ==========\n");
    2065             :     fprintf(f, "nsub %d, info 0%lo, csize %d, ntree %d\n",
    2066             :             (int) re->re_nsub, re->re_info, re->re_csize, g->ntree);
    2067             : 
    2068             :     dumpcolors(&g->cmap, f);
    2069             :     if (!NULLCNFA(g->search))
    2070             :     {
    2071             :         fprintf(f, "\nsearch:\n");
    2072             :         dumpcnfa(&g->search, f);
    2073             :     }
    2074             :     for (i = 1; i < g->nlacons; i++)
    2075             :     {
    2076             :         struct subre *lasub = &g->lacons[i];
    2077             :         const char *latype;
    2078             : 
    2079             :         switch (lasub->subno)
    2080             :         {
    2081             :             case LATYPE_AHEAD_POS:
    2082             :                 latype = "positive lookahead";
    2083             :                 break;
    2084             :             case LATYPE_AHEAD_NEG:
    2085             :                 latype = "negative lookahead";
    2086             :                 break;
    2087             :             case LATYPE_BEHIND_POS:
    2088             :                 latype = "positive lookbehind";
    2089             :                 break;
    2090             :             case LATYPE_BEHIND_NEG:
    2091             :                 latype = "negative lookbehind";
    2092             :                 break;
    2093             :             default:
    2094             :                 latype = "???";
    2095             :                 break;
    2096             :         }
    2097             :         fprintf(f, "\nla%d (%s):\n", i, latype);
    2098             :         dumpcnfa(&lasub->cnfa, f);
    2099             :     }
    2100             :     fprintf(f, "\n");
    2101             :     dumpst(g->tree, f, 0);
    2102             : }
    2103             : 
    2104             : /*
    2105             :  * dumpst - dump a subRE tree
    2106             :  */
    2107             : static void
    2108             : dumpst(struct subre *t,
    2109             :        FILE *f,
    2110             :        int nfapresent)          /* is the original NFA still around? */
    2111             : {
    2112             :     if (t == NULL)
    2113             :         fprintf(f, "null tree\n");
    2114             :     else
    2115             :         stdump(t, f, nfapresent);
    2116             :     fflush(f);
    2117             : }
    2118             : 
    2119             : /*
    2120             :  * stdump - recursive guts of dumpst
    2121             :  */
    2122             : static void
    2123             : stdump(struct subre *t,
    2124             :        FILE *f,
    2125             :        int nfapresent)          /* is the original NFA still around? */
    2126             : {
    2127             :     char        idbuf[50];
    2128             : 
    2129             :     fprintf(f, "%s. `%c'", stid(t, idbuf, sizeof(idbuf)), t->op);
    2130             :     if (t->flags & LONGER)
    2131             :         fprintf(f, " longest");
    2132             :     if (t->flags & SHORTER)
    2133             :         fprintf(f, " shortest");
    2134             :     if (t->flags & MIXED)
    2135             :         fprintf(f, " hasmixed");
    2136             :     if (t->flags & CAP)
    2137             :         fprintf(f, " hascapture");
    2138             :     if (t->flags & BACKR)
    2139             :         fprintf(f, " hasbackref");
    2140             :     if (!(t->flags & INUSE))
    2141             :         fprintf(f, " UNUSED");
    2142             :     if (t->subno != 0)
    2143             :         fprintf(f, " (#%d)", t->subno);
    2144             :     if (t->min != 1 || t->max != 1)
    2145             :     {
    2146             :         fprintf(f, " {%d,", t->min);
    2147             :         if (t->max != DUPINF)
    2148             :             fprintf(f, "%d", t->max);
    2149             :         fprintf(f, "}");
    2150             :     }
    2151             :     if (nfapresent)
    2152             :         fprintf(f, " %ld-%ld", (long) t->begin->no, (long) t->end->no);
    2153             :     if (t->left != NULL)
    2154             :         fprintf(f, " L:%s", stid(t->left, idbuf, sizeof(idbuf)));
    2155             :     if (t->right != NULL)
    2156             :         fprintf(f, " R:%s", stid(t->right, idbuf, sizeof(idbuf)));
    2157             :     if (!NULLCNFA(t->cnfa))
    2158             :     {
    2159             :         fprintf(f, "\n");
    2160             :         dumpcnfa(&t->cnfa, f);
    2161             :     }
    2162             :     fprintf(f, "\n");
    2163             :     if (t->left != NULL)
    2164             :         stdump(t->left, f, nfapresent);
    2165             :     if (t->right != NULL)
    2166             :         stdump(t->right, f, nfapresent);
    2167             : }
    2168             : 
    2169             : /*
    2170             :  * stid - identify a subtree node for dumping
    2171             :  */
    2172             : static const char *             /* points to buf or constant string */
    2173             : stid(struct subre *t,
    2174             :      char *buf,
    2175             :      size_t bufsize)
    2176             : {
    2177             :     /* big enough for hex int or decimal t->id? */
    2178             :     if (bufsize < sizeof(void *) * 2 + 3 || bufsize < sizeof(t->id) * 3 + 1)
    2179             :         return "unable";
    2180             :     if (t->id != 0)
    2181             :         sprintf(buf, "%d", t->id);
    2182             :     else
    2183             :         sprintf(buf, "%p", t);
    2184             :     return buf;
    2185             : }
    2186             : #endif                          /* REG_DEBUG */
    2187             : 
    2188             : 
    2189             : #include "regc_lex.c"
    2190             : #include "regc_color.c"
    2191             : #include "regc_nfa.c"
    2192             : #include "regc_cvec.c"
    2193             : #include "regc_pg_locale.c"
    2194             : #include "regc_locale.c"

Generated by: LCOV version 1.13