LCOV - code coverage report
Current view: top level - src/backend/regex - regcomp.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 84.0 % 1094 919
Test Date: 2026-01-26 10:56:24 Functions: 91.4 % 35 32
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 59.8 % 792 474

             Branch data     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 *v, int wanted);
      42                 :             : static int      freev(struct vars *v, int err);
      43                 :             : static void makesearch(struct vars *v, struct nfa *nfa);
      44                 :             : static struct subre *parse(struct vars *v, int stopper, int type,
      45                 :             :                                                    struct state *init, struct state *final);
      46                 :             : static struct subre *parsebranch(struct vars *v, int stopper, int type,
      47                 :             :                                                                  struct state *left, struct state *right,
      48                 :             :                                                                  int partial);
      49                 :             : static struct subre *parseqatom(struct vars *v, int stopper, int type,
      50                 :             :                                                                 struct state *lp, struct state *rp,
      51                 :             :                                                                 struct subre *top);
      52                 :             : static void nonword(struct vars *v, int dir, struct state *lp,
      53                 :             :                                         struct state *rp);
      54                 :             : static void word(struct vars *v, int dir, struct state *lp, struct state *rp);
      55                 :             : static void charclass(struct vars *v, enum char_classes cls, struct state *lp,
      56                 :             :                                           struct state *rp);
      57                 :             : static void charclasscomplement(struct vars *v, enum char_classes cls,
      58                 :             :                                                                 struct state *lp, struct state *rp);
      59                 :             : static int      scannum(struct vars *v);
      60                 :             : static void repeat(struct vars *v, struct state *lp, struct state *rp,
      61                 :             :                                    int m, int n);
      62                 :             : static void bracket(struct vars *v, struct state *lp, struct state *rp);
      63                 :             : static void cbracket(struct vars *v, struct state *lp, struct state *rp);
      64                 :             : static void brackpart(struct vars *v, struct state *lp, struct state *rp,
      65                 :             :                                           bool *have_cclassc);
      66                 :             : static const chr *scanplain(struct vars *v);
      67                 :             : static void onechr(struct vars *v, chr c, struct state *lp, struct state *rp);
      68                 :             : static void optimizebracket(struct vars *v, struct state *lp, struct state *rp);
      69                 :             : static void wordchrs(struct vars *v);
      70                 :             : static void processlacon(struct vars *v, struct state *begin,
      71                 :             :                                                  struct state *end, int latype,
      72                 :             :                                                  struct state *lp, struct state *rp);
      73                 :             : static struct subre *subre(struct vars *v, int op, int flags,
      74                 :             :                                                    struct state *begin, struct state *end);
      75                 :             : static void freesubre(struct vars *v, struct subre *sr);
      76                 :             : static void freesubreandsiblings(struct vars *v, struct subre *sr);
      77                 :             : static void freesrnode(struct vars *v, struct subre *sr);
      78                 :             : static void removecaptures(struct vars *v, struct subre *t);
      79                 :             : static int      numst(struct subre *t, int start);
      80                 :             : static void markst(struct subre *t);
      81                 :             : static void cleanst(struct vars *v);
      82                 :             : static long nfatree(struct vars *v, struct subre *t, FILE *f);
      83                 :             : static long nfanode(struct vars *v, struct subre *t,
      84                 :             :                                         int converttosearch, FILE *f);
      85                 :             : static int      newlacon(struct vars *v, struct state *begin, struct state *end,
      86                 :             :                                          int latype);
      87                 :             : static void freelacons(struct subre *subs, int n);
      88                 :             : static void rfree(regex_t *re);
      89                 :             : static int      rstacktoodeep(void);
      90                 :             : 
      91                 :             : #ifdef REG_DEBUG
      92                 :             : static void dump(regex_t *re, FILE *f);
      93                 :             : static void dumpst(struct subre *t, FILE *f, int nfapresent);
      94                 :             : static void stdump(struct subre *t, FILE *f, int nfapresent);
      95                 :             : static const char *stid(struct subre *t, char *buf, size_t bufsize);
      96                 :             : #endif
      97                 :             : /* === regc_lex.c === */
      98                 :             : static void lexstart(struct vars *v);
      99                 :             : static void prefixes(struct vars *v);
     100                 :             : static int      next(struct vars *v);
     101                 :             : static int      lexescape(struct vars *v);
     102                 :             : static chr      lexdigits(struct vars *v, int base, int minlen, int maxlen);
     103                 :             : static int      brenext(struct vars *v, chr c);
     104                 :             : static void skip(struct vars *v);
     105                 :             : static chr      newline(void);
     106                 :             : static chr      chrnamed(struct vars *v, const chr *startp, const chr *endp,
     107                 :             :                                          chr lastresort);
     108                 :             : 
     109                 :             : /* === regc_color.c === */
     110                 :             : static void initcm(struct vars *v, struct colormap *cm);
     111                 :             : static void freecm(struct colormap *cm);
     112                 :             : static color maxcolor(struct colormap *cm);
     113                 :             : static color newcolor(struct colormap *cm);
     114                 :             : static void freecolor(struct colormap *cm, color co);
     115                 :             : static color pseudocolor(struct colormap *cm);
     116                 :             : static color subcolor(struct colormap *cm, chr c);
     117                 :             : static color subcolorhi(struct colormap *cm, color *pco);
     118                 :             : static color newsub(struct colormap *cm, color co);
     119                 :             : static int      newhicolorrow(struct colormap *cm, int oldrow);
     120                 :             : static void newhicolorcols(struct colormap *cm);
     121                 :             : static void subcolorcvec(struct vars *v, struct cvec *cv, struct state *lp,
     122                 :             :                                                  struct state *rp);
     123                 :             : static void subcoloronechr(struct vars *v, chr ch, struct state *lp,
     124                 :             :                                                    struct state *rp, color *lastsubcolor);
     125                 :             : static void subcoloronerange(struct vars *v, chr from, chr to,
     126                 :             :                                                          struct state *lp, struct state *rp,
     127                 :             :                                                          color *lastsubcolor);
     128                 :             : static void subcoloronerow(struct vars *v, int rownum, struct state *lp,
     129                 :             :                                                    struct state *rp, color *lastsubcolor);
     130                 :             : static void okcolors(struct nfa *nfa, struct colormap *cm);
     131                 :             : static void colorchain(struct colormap *cm, struct arc *a);
     132                 :             : static void uncolorchain(struct colormap *cm, struct arc *a);
     133                 :             : static void rainbow(struct nfa *nfa, struct colormap *cm, int type, color but,
     134                 :             :                                         struct state *from, struct state *to);
     135                 :             : static void colorcomplement(struct nfa *nfa, struct colormap *cm, int type,
     136                 :             :                                                         struct state *of, struct state *from,
     137                 :             :                                                         struct state *to);
     138                 :             : 
     139                 :             : #ifdef REG_DEBUG
     140                 :             : static void dumpcolors(struct colormap *cm, FILE *f);
     141                 :             : static void dumpchr(chr c, FILE *f);
     142                 :             : #endif
     143                 :             : /* === regc_nfa.c === */
     144                 :             : static struct nfa *newnfa(struct vars *v, struct colormap *cm,
     145                 :             :                                                   struct nfa *parent);
     146                 :             : static void freenfa(struct nfa *nfa);
     147                 :             : static struct state *newstate(struct nfa *nfa);
     148                 :             : static struct state *newfstate(struct nfa *nfa, int flag);
     149                 :             : static void dropstate(struct nfa *nfa, struct state *s);
     150                 :             : static void freestate(struct nfa *nfa, struct state *s);
     151                 :             : static void newarc(struct nfa *nfa, int t, color co,
     152                 :             :                                    struct state *from, struct state *to);
     153                 :             : static void createarc(struct nfa *nfa, int t, color co,
     154                 :             :                                           struct state *from, struct state *to);
     155                 :             : static struct arc *allocarc(struct nfa *nfa);
     156                 :             : static void freearc(struct nfa *nfa, struct arc *victim);
     157                 :             : static void changearcsource(struct arc *a, struct state *newfrom);
     158                 :             : static void changearctarget(struct arc *a, struct state *newto);
     159                 :             : static int      hasnonemptyout(struct state *s);
     160                 :             : static struct arc *findarc(struct state *s, int type, color co);
     161                 :             : static void cparc(struct nfa *nfa, struct arc *oa,
     162                 :             :                                   struct state *from, struct state *to);
     163                 :             : static void sortins(struct nfa *nfa, struct state *s);
     164                 :             : static int      sortins_cmp(const void *a, const void *b);
     165                 :             : static void sortouts(struct nfa *nfa, struct state *s);
     166                 :             : static int      sortouts_cmp(const void *a, const void *b);
     167                 :             : static void moveins(struct nfa *nfa, struct state *oldState,
     168                 :             :                                         struct state *newState);
     169                 :             : static void copyins(struct nfa *nfa, struct state *oldState,
     170                 :             :                                         struct state *newState);
     171                 :             : static void mergeins(struct nfa *nfa, struct state *s,
     172                 :             :                                          struct arc **arcarray, int arccount);
     173                 :             : static void moveouts(struct nfa *nfa, struct state *oldState,
     174                 :             :                                          struct state *newState);
     175                 :             : static void copyouts(struct nfa *nfa, struct state *oldState,
     176                 :             :                                          struct state *newState);
     177                 :             : static void cloneouts(struct nfa *nfa, struct state *old, struct state *from,
     178                 :             :                                           struct state *to, int type);
     179                 :             : static void delsub(struct nfa *nfa, struct state *lp, struct state *rp);
     180                 :             : static void deltraverse(struct nfa *nfa, struct state *leftend,
     181                 :             :                                                 struct state *s);
     182                 :             : static void dupnfa(struct nfa *nfa, struct state *start, struct state *stop,
     183                 :             :                                    struct state *from, struct state *to);
     184                 :             : static void duptraverse(struct nfa *nfa, struct state *s, struct state *stmp);
     185                 :             : static void removeconstraints(struct nfa *nfa, struct state *start, struct state *stop);
     186                 :             : static void removetraverse(struct nfa *nfa, struct state *s);
     187                 :             : static void cleartraverse(struct nfa *nfa, struct state *s);
     188                 :             : static struct state *single_color_transition(struct state *s1,
     189                 :             :                                                                                          struct state *s2);
     190                 :             : static void specialcolors(struct nfa *nfa);
     191                 :             : static long optimize(struct nfa *nfa, FILE *f);
     192                 :             : static void pullback(struct nfa *nfa, FILE *f);
     193                 :             : static int      pull(struct nfa *nfa, struct arc *con,
     194                 :             :                                  struct state **intermediates);
     195                 :             : static void pushfwd(struct nfa *nfa, FILE *f);
     196                 :             : static int      push(struct nfa *nfa, struct arc *con,
     197                 :             :                                  struct state **intermediates);
     198                 :             : 
     199                 :             : #define INCOMPATIBLE    1               /* destroys arc */
     200                 :             : #define SATISFIED       2                       /* constraint satisfied */
     201                 :             : #define COMPATIBLE      3                       /* compatible but not satisfied yet */
     202                 :             : #define REPLACEARC      4                       /* replace arc's color with constraint color */
     203                 :             : static int      combine(struct nfa *nfa, struct arc *con, struct arc *a);
     204                 :             : static void fixempties(struct nfa *nfa, FILE *f);
     205                 :             : static struct state *emptyreachable(struct nfa *nfa, struct state *s,
     206                 :             :                                                                         struct state *lastfound,
     207                 :             :                                                                         struct arc **inarcsorig);
     208                 :             : static int      isconstraintarc(struct arc *a);
     209                 :             : static int      hasconstraintout(struct state *s);
     210                 :             : static void fixconstraintloops(struct nfa *nfa, FILE *f);
     211                 :             : static int      findconstraintloop(struct nfa *nfa, struct state *s);
     212                 :             : static void breakconstraintloop(struct nfa *nfa, struct state *sinitial);
     213                 :             : static void clonesuccessorstates(struct nfa *nfa, struct state *ssource,
     214                 :             :                                                                  struct state *sclone,
     215                 :             :                                                                  struct state *spredecessor,
     216                 :             :                                                                  struct arc *refarc, char *curdonemap,
     217                 :             :                                                                  char *outerdonemap, int nstates);
     218                 :             : static void removecantmatch(struct nfa *nfa);
     219                 :             : static void cleanup(struct nfa *nfa);
     220                 :             : static void markreachable(struct nfa *nfa, struct state *s,
     221                 :             :                                                   struct state *okay, struct state *mark);
     222                 :             : static void markcanreach(struct nfa *nfa, struct state *s, struct state *okay,
     223                 :             :                                                  struct state *mark);
     224                 :             : static long analyze(struct nfa *nfa);
     225                 :             : static void checkmatchall(struct nfa *nfa);
     226                 :             : static bool checkmatchall_recurse(struct nfa *nfa, struct state *s,
     227                 :             :                                                                   bool **haspaths);
     228                 :             : static bool check_out_colors_match(struct state *s, color co1, color co2);
     229                 :             : static bool check_in_colors_match(struct state *s, color co1, color co2);
     230                 :             : static void compact(struct nfa *nfa, struct cnfa *cnfa);
     231                 :             : static void carcsort(struct carc *first, size_t n);
     232                 :             : static int      carc_cmp(const void *a, const void *b);
     233                 :             : static void freecnfa(struct cnfa *cnfa);
     234                 :             : static void dumpnfa(struct nfa *nfa, FILE *f);
     235                 :             : 
     236                 :             : #ifdef REG_DEBUG
     237                 :             : static void dumpstate(struct state *s, FILE *f);
     238                 :             : static void dumparcs(struct state *s, FILE *f);
     239                 :             : static void dumparc(struct arc *a, struct state *s, FILE *f);
     240                 :             : static void dumpcnfa(struct cnfa *cnfa, FILE *f);
     241                 :             : static void dumpcstate(int st, struct cnfa *cnfa, FILE *f);
     242                 :             : #endif
     243                 :             : /* === regc_cvec.c === */
     244                 :             : static struct cvec *newcvec(int nchrs, int nranges);
     245                 :             : static struct cvec *clearcvec(struct cvec *cv);
     246                 :             : static void addchr(struct cvec *cv, chr c);
     247                 :             : static void addrange(struct cvec *cv, chr from, chr to);
     248                 :             : static struct cvec *getcvec(struct vars *v, int nchrs, int nranges);
     249                 :             : static void freecvec(struct cvec *cv);
     250                 :             : 
     251                 :             : /* === regc_pg_locale.c === */
     252                 :             : static int      regc_wc_isdigit(pg_wchar c);
     253                 :             : static int      regc_wc_isalpha(pg_wchar c);
     254                 :             : static int      regc_wc_isalnum(pg_wchar c);
     255                 :             : static int      regc_wc_isword(pg_wchar c);
     256                 :             : static int      regc_wc_isupper(pg_wchar c);
     257                 :             : static int      regc_wc_islower(pg_wchar c);
     258                 :             : static int      regc_wc_isgraph(pg_wchar c);
     259                 :             : static int      regc_wc_isprint(pg_wchar c);
     260                 :             : static int      regc_wc_ispunct(pg_wchar c);
     261                 :             : static int      regc_wc_isspace(pg_wchar c);
     262                 :             : static pg_wchar regc_wc_toupper(pg_wchar c);
     263                 :             : static pg_wchar regc_wc_tolower(pg_wchar c);
     264                 :             : 
     265                 :             : /* === regc_locale.c === */
     266                 :             : static chr      element(struct vars *v, const chr *startp, const chr *endp);
     267                 :             : static struct cvec *range(struct vars *v, chr a, chr b, int cases);
     268                 :             : static int      before(chr x, chr y);
     269                 :             : static struct cvec *eclass(struct vars *v, chr c, int cases);
     270                 :             : static enum char_classes lookupcclass(struct vars *v, const chr *startp,
     271                 :             :                                                                           const chr *endp);
     272                 :             : static struct cvec *cclasscvec(struct vars *v, enum char_classes cclasscode,
     273                 :             :                                                            int cases);
     274                 :             : static int      cclass_column_index(struct colormap *cm, chr c);
     275                 :             : static struct cvec *allcases(struct vars *v, chr c);
     276                 :             : static int      cmp(const chr *x, const chr *y, size_t len);
     277                 :             : static int      casecmp(const chr *x, const chr *y, size_t len);
     278                 :             : 
     279                 :             : 
     280                 :             : /* internal variables, bundled for easy passing around */
     281                 :             : struct vars
     282                 :             : {
     283                 :             :         regex_t    *re;
     284                 :             :         const chr  *now;                        /* scan pointer into string */
     285                 :             :         const chr  *stop;                       /* end of string */
     286                 :             :         int                     err;                    /* error code (0 if none) */
     287                 :             :         int                     cflags;                 /* copy of compile flags */
     288                 :             :         int                     lasttype;               /* type of previous token */
     289                 :             :         int                     nexttype;               /* type of next token */
     290                 :             :         chr                     nextvalue;              /* value (if any) of next token */
     291                 :             :         int                     lexcon;                 /* lexical context type (see regc_lex.c) */
     292                 :             :         int                     nsubexp;                /* subexpression count */
     293                 :             :         struct subre **subs;            /* subRE pointer vector */
     294                 :             :         size_t          nsubs;                  /* length of vector */
     295                 :             :         struct subre *sub10[10];        /* initial vector, enough for most */
     296                 :             :         struct nfa *nfa;                        /* the NFA */
     297                 :             :         struct colormap *cm;            /* character color map */
     298                 :             :         color           nlcolor;                /* color of newline */
     299                 :             :         struct state *wordchrs;         /* state in nfa holding word-char outarcs */
     300                 :             :         struct subre *tree;                     /* subexpression tree */
     301                 :             :         struct subre *treechain;        /* all tree nodes allocated */
     302                 :             :         struct subre *treefree;         /* any free tree nodes */
     303                 :             :         int                     ntree;                  /* number of tree nodes, plus one */
     304                 :             :         struct cvec *cv;                        /* interface cvec */
     305                 :             :         struct cvec *cv2;                       /* utility cvec */
     306                 :             :         struct subre *lacons;           /* lookaround-constraint vector */
     307                 :             :         int                     nlacons;                /* size of lacons[]; note that only slots
     308                 :             :                                                                  * numbered 1 .. nlacons-1 are used */
     309                 :             :         size_t          spaceused;              /* approx. space used for compilation */
     310                 :             : };
     311                 :             : 
     312                 :             : /* parsing macros; most know that `v' is the struct vars pointer */
     313                 :             : #define NEXT()  (next(v))               /* advance by one token */
     314                 :             : #define SEE(t)  (v->nexttype == (t)) /* is next token this? */
     315                 :             : #define EAT(t)  (SEE(t) && next(v)) /* if next is this, swallow it */
     316                 :             : #define VISERR(vv)      ((vv)->err != 0)     /* have we seen an error yet? */
     317                 :             : #define ISERR() VISERR(v)
     318                 :             : #define VERR(vv,e)      ((vv)->nexttype = EOS, \
     319                 :             :                                          (vv)->err = ((vv)->err ? (vv)->err : (e)))
     320                 :             : #define ERR(e)  VERR(v, e)              /* record an error */
     321                 :             : #define NOERR() {if (ISERR()) return;}  /* if error seen, return */
     322                 :             : #define NOERRN()        {if (ISERR()) return NULL;} /* NOERR with retval */
     323                 :             : #define NOERRZ()        {if (ISERR()) return 0;}        /* NOERR with retval */
     324                 :             : #define INSIST(c, e) do { if (!(c)) ERR(e); } while (0) /* error if c false */
     325                 :             : #define NOTE(b) (v->re->re_info |= (b)) /* note visible condition */
     326                 :             : #define EMPTYARC(x, y)  newarc(v->nfa, EMPTY, 0, x, y)
     327                 :             : 
     328                 :             : /* token type codes, some also used as NFA arc types */
     329                 :             : #define EMPTY   'n'                             /* no token present */
     330                 :             : #define EOS 'e'                                 /* end of string */
     331                 :             : #define PLAIN   'p'                             /* ordinary character */
     332                 :             : #define DIGIT   'd'                             /* digit (in bound) */
     333                 :             : #define BACKREF 'b'                             /* back reference */
     334                 :             : #define COLLEL  'I'                             /* start of [. */
     335                 :             : #define ECLASS  'E'                             /* start of [= */
     336                 :             : #define CCLASS  'C'                             /* start of [: */
     337                 :             : #define END 'X'                                 /* end of [. [= [: */
     338                 :             : #define CCLASSS 's'                             /* char class shorthand escape */
     339                 :             : #define CCLASSC 'c'                             /* complement char class shorthand escape */
     340                 :             : #define RANGE   'R'                             /* - within [] which might be range delim. */
     341                 :             : #define LACON   'L'                             /* lookaround constraint subRE */
     342                 :             : #define AHEAD   'a'                             /* color-lookahead arc */
     343                 :             : #define BEHIND  'r'                             /* color-lookbehind arc */
     344                 :             : #define WBDRY   'w'                             /* word boundary constraint */
     345                 :             : #define NWBDRY  'W'                             /* non-word-boundary constraint */
     346                 :             : #define CANTMATCH 'x'                   /* arc that cannot match anything */
     347                 :             : #define SBEGIN  'A'                             /* beginning of string (even if not BOL) */
     348                 :             : #define SEND    'Z'                             /* end of string (even if not EOL) */
     349                 :             : 
     350                 :             : /* is an arc colored, and hence should belong to a color chain? */
     351                 :             : /* the test on "co" eliminates RAINBOW arcs, which we don't bother to chain */
     352                 :             : #define COLORED(a) \
     353                 :             :         ((a)->co >= 0 && \
     354                 :             :          ((a)->type == PLAIN || (a)->type == AHEAD || (a)->type == BEHIND))
     355                 :             : 
     356                 :             : 
     357                 :             : /* static function list */
     358                 :             : static const struct fns functions = {
     359                 :             :         rfree,                                          /* regfree insides */
     360                 :             :         rstacktoodeep                           /* check for stack getting dangerously deep */
     361                 :             : };
     362                 :             : 
     363                 :             : 
     364                 :             : 
     365                 :             : /*
     366                 :             :  * pg_regcomp - compile regular expression
     367                 :             :  *
     368                 :             :  * Note: on failure, no resources remain allocated, so pg_regfree()
     369                 :             :  * need not be applied to re.
     370                 :             :  */
     371                 :             : int
     372                 :         820 : pg_regcomp(regex_t *re,
     373                 :             :                    const chr *string,
     374                 :             :                    size_t len,
     375                 :             :                    int flags,
     376                 :             :                    Oid collation)
     377                 :             : {
     378                 :         820 :         struct vars var;
     379                 :         820 :         struct vars *v = &var;
     380                 :         820 :         struct guts *g;
     381                 :         820 :         int                     i;
     382                 :         820 :         size_t          j;
     383                 :             : 
     384                 :             : #ifdef REG_DEBUG
     385                 :             :         FILE       *debug = (flags & REG_PROGRESS) ? stdout : (FILE *) NULL;
     386                 :             : #else
     387                 :         820 :         FILE       *debug = (FILE *) NULL;
     388                 :             : #endif
     389                 :             : 
     390                 :             : #define  CNOERR()        { if (ISERR()) return freev(v, v->err); }
     391                 :             : 
     392                 :             :         /* sanity checks */
     393                 :             : 
     394   [ +  +  +  + ]:         820 :         if (re == NULL || string == NULL)
     395                 :           8 :                 return REG_INVARG;
     396   [ +  +  +  - ]:         812 :         if ((flags & REG_QUOTE) &&
     397                 :          13 :                 (flags & (REG_ADVANCED | REG_EXPANDED | REG_NEWLINE)))
     398                 :           0 :                 return REG_INVARG;
     399   [ +  +  +  - ]:         812 :         if (!(flags & REG_EXTENDED) && (flags & REG_ADVF))
     400                 :           0 :                 return REG_INVARG;
     401                 :             : 
     402                 :             :         /* Initialize locale-dependent support */
     403                 :         812 :         pg_set_regex_collation(collation);
     404                 :             : 
     405                 :             :         /* initial setup (after which freev() is callable) */
     406                 :         812 :         v->re = re;
     407                 :         812 :         v->now = string;
     408                 :         812 :         v->stop = v->now + len;
     409                 :         812 :         v->err = 0;
     410                 :         812 :         v->cflags = flags;
     411                 :         812 :         v->nsubexp = 0;
     412                 :         812 :         v->subs = v->sub10;
     413                 :         812 :         v->nsubs = 10;
     414         [ +  + ]:        8932 :         for (j = 0; j < v->nsubs; j++)
     415                 :        8120 :                 v->subs[j] = NULL;
     416                 :         812 :         v->nfa = NULL;
     417                 :         812 :         v->cm = NULL;
     418                 :         812 :         v->nlcolor = COLORLESS;
     419                 :         812 :         v->wordchrs = NULL;
     420                 :         812 :         v->tree = NULL;
     421                 :         812 :         v->treechain = NULL;
     422                 :         812 :         v->treefree = NULL;
     423                 :         812 :         v->cv = NULL;
     424                 :         812 :         v->cv2 = NULL;
     425                 :         812 :         v->lacons = NULL;
     426                 :         812 :         v->nlacons = 0;
     427                 :         812 :         v->spaceused = 0;
     428                 :         812 :         re->re_magic = REMAGIC;
     429                 :         812 :         re->re_info = 0;                     /* bits get set during parse */
     430                 :         812 :         re->re_csize = sizeof(chr);
     431                 :         812 :         re->re_collation = collation;
     432                 :         812 :         re->re_guts = NULL;
     433                 :         812 :         re->re_fns = VS(&functions);
     434                 :             : 
     435                 :             :         /* more complex setup, malloced things */
     436                 :         812 :         re->re_guts = VS(MALLOC(sizeof(struct guts)));
     437         [ +  - ]:         812 :         if (re->re_guts == NULL)
     438                 :           0 :                 return freev(v, REG_ESPACE);
     439                 :         812 :         g = (struct guts *) re->re_guts;
     440                 :         812 :         g->tree = NULL;
     441                 :         812 :         initcm(v, &g->cmap);
     442                 :         812 :         v->cm = &g->cmap;
     443                 :         812 :         g->lacons = NULL;
     444                 :         812 :         g->nlacons = 0;
     445                 :         812 :         ZAPCNFA(g->search);
     446                 :         812 :         v->nfa = newnfa(v, v->cm, (struct nfa *) NULL);
     447         [ -  + ]:         812 :         CNOERR();
     448                 :             :         /* set up a reasonably-sized transient cvec for getcvec usage */
     449                 :         812 :         v->cv = newcvec(100, 20);
     450         [ +  - ]:         812 :         if (v->cv == NULL)
     451                 :           0 :                 return freev(v, REG_ESPACE);
     452                 :             : 
     453                 :             :         /* parsing */
     454                 :         812 :         lexstart(v);                            /* also handles prefixes */
     455   [ +  +  +  + ]:         812 :         if ((v->cflags & REG_NLSTOP) || (v->cflags & REG_NLANCH))
     456                 :             :         {
     457                 :             :                 /* assign newline a unique color */
     458                 :          23 :                 v->nlcolor = subcolor(v->cm, newline());
     459                 :          23 :                 okcolors(v->nfa, v->cm);
     460                 :          23 :         }
     461         [ +  + ]:         812 :         CNOERR();
     462                 :         811 :         v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final);
     463         [ +  - ]:         811 :         assert(SEE(EOS));                       /* even if error; ISERR() => SEE(EOS) */
     464         [ +  + ]:         811 :         CNOERR();
     465         [ +  - ]:         806 :         assert(v->tree != NULL);
     466                 :             : 
     467                 :             :         /* finish setup of nfa and its subre tree */
     468                 :         806 :         specialcolors(v->nfa);
     469         [ -  + ]:         806 :         CNOERR();
     470                 :             : #ifdef REG_DEBUG
     471                 :             :         if (debug != NULL)
     472                 :             :         {
     473                 :             :                 fprintf(debug, "\n\n\n========= RAW ==========\n");
     474                 :             :                 dumpnfa(v->nfa, debug);
     475                 :             :                 dumpst(v->tree, debug, 1);
     476                 :             :         }
     477                 :             : #endif
     478         [ +  + ]:         806 :         if (v->cflags & REG_NOSUB)
     479                 :         713 :                 removecaptures(v, v->tree);
     480                 :         806 :         v->ntree = numst(v->tree, 1);
     481                 :         806 :         markst(v->tree);
     482                 :         806 :         cleanst(v);
     483                 :             : #ifdef REG_DEBUG
     484                 :             :         if (debug != NULL)
     485                 :             :         {
     486                 :             :                 fprintf(debug, "\n\n\n========= TREE FIXED ==========\n");
     487                 :             :                 dumpst(v->tree, debug, 1);
     488                 :             :         }
     489                 :             : #endif
     490                 :             : 
     491                 :             :         /* build compacted NFAs for tree and lacons */
     492                 :         806 :         re->re_info |= nfatree(v, v->tree, debug);
     493         [ +  + ]:         806 :         CNOERR();
     494   [ +  +  +  - ]:         805 :         assert(v->nlacons == 0 || v->lacons != NULL);
     495         [ +  + ]:         812 :         for (i = 1; i < v->nlacons; i++)
     496                 :             :         {
     497                 :           7 :                 struct subre *lasub = &v->lacons[i];
     498                 :             : 
     499                 :             : #ifdef REG_DEBUG
     500                 :             :                 if (debug != NULL)
     501                 :             :                         fprintf(debug, "\n\n\n========= LA%d ==========\n", i);
     502                 :             : #endif
     503                 :             : 
     504                 :             :                 /* Prepend .* to pattern if it's a lookbehind LACON */
     505                 :           7 :                 nfanode(v, lasub, !LATYPE_IS_AHEAD(lasub->latype), debug);
     506                 :           7 :         }
     507         [ -  + ]:         805 :         CNOERR();
     508         [ +  + ]:         805 :         if (v->tree->flags & SHORTER)
     509                 :          16 :                 NOTE(REG_USHORTEST);
     510                 :             : 
     511                 :             :         /* build compacted NFAs for tree, lacons, fast search */
     512                 :             : #ifdef REG_DEBUG
     513                 :             :         if (debug != NULL)
     514                 :             :                 fprintf(debug, "\n\n\n========= SEARCH ==========\n");
     515                 :             : #endif
     516                 :             :         /* can sacrifice main NFA now, so use it as work area */
     517                 :         805 :         (DISCARD) optimize(v->nfa, debug);
     518         [ -  + ]:         805 :         CNOERR();
     519                 :         805 :         makesearch(v, v->nfa);
     520         [ -  + ]:         805 :         CNOERR();
     521                 :         805 :         compact(v->nfa, &g->search);
     522         [ -  + ]:         805 :         CNOERR();
     523                 :             : 
     524                 :             :         /* looks okay, package it up */
     525                 :         805 :         re->re_nsub = v->nsubexp;
     526                 :         805 :         v->re = NULL;                                /* freev no longer frees re */
     527                 :         805 :         g->magic = GUTSMAGIC;
     528                 :         805 :         g->cflags = v->cflags;
     529                 :         805 :         g->info = re->re_info;
     530                 :         805 :         g->nsub = re->re_nsub;
     531                 :         805 :         g->tree = v->tree;
     532                 :         805 :         v->tree = NULL;
     533                 :         805 :         g->ntree = v->ntree;
     534                 :         805 :         g->compare = (v->cflags & REG_ICASE) ? casecmp : cmp;
     535                 :         805 :         g->lacons = v->lacons;
     536                 :         805 :         v->lacons = NULL;
     537                 :         805 :         g->nlacons = v->nlacons;
     538                 :             : 
     539                 :             : #ifdef REG_DEBUG
     540                 :             :         if (flags & REG_DUMP)
     541                 :             :         {
     542                 :             :                 dump(re, stdout);
     543                 :             :                 fflush(stdout);
     544                 :             :         }
     545                 :             : #endif
     546                 :             : 
     547         [ +  - ]:         805 :         assert(v->err == 0);
     548                 :         805 :         return freev(v, 0);
     549                 :         812 : }
     550                 :             : 
     551                 :             : /*
     552                 :             :  * moresubs - enlarge subRE vector
     553                 :             :  */
     554                 :             : static void
     555                 :           0 : moresubs(struct vars *v,
     556                 :             :                  int wanted)                    /* want enough room for this one */
     557                 :             : {
     558                 :           0 :         struct subre **p;
     559                 :           0 :         size_t          n;
     560                 :             : 
     561         [ #  # ]:           0 :         assert(wanted > 0 && (size_t) wanted >= v->nsubs);
     562                 :           0 :         n = (size_t) wanted * 3 / 2 + 1;
     563                 :             : 
     564         [ #  # ]:           0 :         if (v->subs == v->sub10)
     565                 :             :         {
     566                 :           0 :                 p = (struct subre **) MALLOC(n * sizeof(struct subre *));
     567         [ #  # ]:           0 :                 if (p != NULL)
     568                 :           0 :                         memcpy(VS(p), VS(v->subs),
     569                 :             :                                    v->nsubs * sizeof(struct subre *));
     570                 :           0 :         }
     571                 :             :         else
     572                 :           0 :                 p = (struct subre **) REALLOC(v->subs, n * sizeof(struct subre *));
     573         [ #  # ]:           0 :         if (p == NULL)
     574                 :             :         {
     575         [ #  # ]:           0 :                 ERR(REG_ESPACE);
     576                 :           0 :                 return;
     577                 :             :         }
     578                 :           0 :         v->subs = p;
     579         [ #  # ]:           0 :         for (p = &v->subs[v->nsubs]; v->nsubs < n; p++, v->nsubs++)
     580                 :           0 :                 *p = NULL;
     581         [ #  # ]:           0 :         assert(v->nsubs == n);
     582         [ #  # ]:           0 :         assert((size_t) wanted < v->nsubs);
     583         [ #  # ]:           0 : }
     584                 :             : 
     585                 :             : /*
     586                 :             :  * freev - free vars struct's substructures where necessary
     587                 :             :  *
     588                 :             :  * Optionally does error-number setting, and always returns error code
     589                 :             :  * (if any), to make error-handling code terser.
     590                 :             :  */
     591                 :             : static int
     592                 :         812 : freev(struct vars *v,
     593                 :             :           int err)
     594                 :             : {
     595         [ +  + ]:         812 :         if (v->re != NULL)
     596                 :           7 :                 rfree(v->re);
     597         [ +  - ]:         812 :         if (v->subs != v->sub10)
     598                 :           0 :                 FREE(v->subs);
     599         [ -  + ]:         812 :         if (v->nfa != NULL)
     600                 :         812 :                 freenfa(v->nfa);
     601         [ +  + ]:         812 :         if (v->tree != NULL)
     602                 :           1 :                 freesubre(v, v->tree);
     603         [ +  + ]:         812 :         if (v->treechain != NULL)
     604                 :           5 :                 cleanst(v);
     605         [ -  + ]:         812 :         if (v->cv != NULL)
     606                 :         812 :                 freecvec(v->cv);
     607         [ +  - ]:         812 :         if (v->cv2 != NULL)
     608                 :           0 :                 freecvec(v->cv2);
     609         [ +  - ]:         812 :         if (v->lacons != NULL)
     610                 :           0 :                 freelacons(v->lacons, v->nlacons);
     611         [ +  + ]:         812 :         ERR(err);                                       /* nop if err==0 */
     612                 :             : 
     613                 :         812 :         return v->err;
     614                 :             : }
     615                 :             : 
     616                 :             : /*
     617                 :             :  * makesearch - turn an NFA into a search NFA (implicit prepend of .*?)
     618                 :             :  * NFA must have been optimize()d already.
     619                 :             :  */
     620                 :             : static void
     621                 :         807 : makesearch(struct vars *v,
     622                 :             :                    struct nfa *nfa)
     623                 :             : {
     624                 :         807 :         struct arc *a;
     625                 :         807 :         struct arc *b;
     626                 :         807 :         struct state *pre = nfa->pre;
     627                 :         807 :         struct state *s;
     628                 :         807 :         struct state *s2;
     629                 :         807 :         struct state *slist;
     630                 :             : 
     631                 :             :         /* no loops are needed if it's anchored */
     632         [ +  + ]:        2117 :         for (a = pre->outs; a != NULL; a = a->outchain)
     633                 :             :         {
     634         [ +  - ]:        1538 :                 assert(a->type == PLAIN);
     635   [ +  +  +  + ]:        1538 :                 if (a->co != nfa->bos[0] && a->co != nfa->bos[1])
     636                 :         228 :                         break;
     637                 :        1310 :         }
     638         [ +  + ]:         807 :         if (a != NULL)
     639                 :             :         {
     640                 :             :                 /* add implicit .* in front */
     641                 :         228 :                 rainbow(nfa, v->cm, PLAIN, COLORLESS, pre, pre);
     642                 :             : 
     643                 :             :                 /* and ^* and \A* too -- not always necessary, but harmless */
     644                 :         228 :                 newarc(nfa, PLAIN, nfa->bos[0], pre, pre);
     645                 :         228 :                 newarc(nfa, PLAIN, nfa->bos[1], pre, pre);
     646                 :             : 
     647                 :             :                 /*
     648                 :             :                  * The pattern is still MATCHALL if it was before, but the max match
     649                 :             :                  * length is now infinity.
     650                 :             :                  */
     651         [ +  + ]:         228 :                 if (nfa->flags & MATCHALL)
     652                 :          16 :                         nfa->maxmatchall = DUPINF;
     653                 :         228 :         }
     654                 :             : 
     655                 :             :         /*
     656                 :             :          * Now here's the subtle part.  Because many REs have no lookback
     657                 :             :          * constraints, often knowing when you were in the pre state tells you
     658                 :             :          * little; it's the next state(s) that are informative.  But some of them
     659                 :             :          * may have other inarcs, i.e. it may be possible to make actual progress
     660                 :             :          * and then return to one of them.  We must de-optimize such cases,
     661                 :             :          * splitting each such state into progress and no-progress states.
     662                 :             :          */
     663                 :             : 
     664                 :             :         /* first, make a list of the states reachable from pre and elsewhere */
     665                 :         807 :         slist = NULL;
     666         [ +  + ]:        3174 :         for (a = pre->outs; a != NULL; a = a->outchain)
     667                 :             :         {
     668                 :        2367 :                 s = a->to;
     669         [ +  + ]:        7839 :                 for (b = s->ins; b != NULL; b = b->inchain)
     670                 :             :                 {
     671         [ +  + ]:        6004 :                         if (b->from != pre)
     672                 :         532 :                                 break;
     673                 :        5472 :                 }
     674                 :             : 
     675                 :             :                 /*
     676                 :             :                  * We want to mark states as being in the list already by having non
     677                 :             :                  * NULL tmp fields, but we can't just store the old slist value in tmp
     678                 :             :                  * because that doesn't work for the first such state.  Instead, the
     679                 :             :                  * first list entry gets its own address in tmp.
     680                 :             :                  */
     681   [ +  +  +  + ]:        2367 :                 if (b != NULL && s->tmp == NULL)
     682                 :             :                 {
     683         [ +  + ]:         209 :                         s->tmp = (slist != NULL) ? slist : s;
     684                 :         209 :                         slist = s;
     685                 :         209 :                 }
     686                 :        2367 :         }
     687                 :             : 
     688                 :             :         /* do the splits */
     689         [ +  + ]:        1016 :         for (s = slist; s != NULL; s = s2)
     690                 :             :         {
     691                 :         209 :                 s2 = newstate(nfa);
     692         [ -  + ]:         209 :                 NOERR();
     693                 :         209 :                 copyouts(nfa, s, s2);
     694         [ -  + ]:         209 :                 NOERR();
     695         [ +  + ]:       12019 :                 for (a = s->ins; a != NULL; a = b)
     696                 :             :                 {
     697                 :       11810 :                         b = a->inchain;
     698         [ +  + ]:       11810 :                         if (a->from != pre)
     699                 :             :                         {
     700                 :       11278 :                                 cparc(nfa, a, a->from, s2);
     701                 :       11278 :                                 freearc(nfa, a);
     702                 :       11278 :                         }
     703                 :       11810 :                 }
     704         [ +  + ]:         209 :                 s2 = (s->tmp != s) ? s->tmp : NULL;
     705                 :         209 :                 s->tmp = NULL;                       /* clean up while we're at it */
     706                 :         209 :         }
     707         [ -  + ]:         807 : }
     708                 :             : 
     709                 :             : /*
     710                 :             :  * parse - parse an RE
     711                 :             :  *
     712                 :             :  * This is actually just the top level, which parses a bunch of branches
     713                 :             :  * tied together with '|'.  If there's more than one, they appear in the
     714                 :             :  * tree as the children of a '|' subre.
     715                 :             :  */
     716                 :             : static struct subre *
     717                 :        1528 : parse(struct vars *v,
     718                 :             :           int stopper,                          /* EOS or ')' */
     719                 :             :           int type,                                     /* LACON (lookaround subRE) or PLAIN */
     720                 :             :           struct state *init,           /* initial state */
     721                 :             :           struct state *final)          /* final state */
     722                 :             : {
     723                 :        1528 :         struct subre *branches;         /* top level */
     724                 :        1528 :         struct subre *lastbranch;       /* latest branch */
     725                 :             : 
     726   [ +  +  +  - ]:        1528 :         assert(stopper == ')' || stopper == EOS);
     727                 :             : 
     728                 :        1528 :         branches = subre(v, '|', LONGER, init, final);
     729         [ -  + ]:        1528 :         NOERRN();
     730                 :        1528 :         lastbranch = NULL;
     731                 :        1528 :         do
     732                 :             :         {                                                       /* a branch */
     733                 :        1581 :                 struct subre *branch;
     734                 :        1581 :                 struct state *left;             /* scaffolding for branch */
     735                 :        1581 :                 struct state *right;
     736                 :             : 
     737                 :        1581 :                 left = newstate(v->nfa);
     738                 :        1581 :                 right = newstate(v->nfa);
     739         [ -  + ]:        1581 :                 NOERRN();
     740                 :        1581 :                 EMPTYARC(init, left);
     741                 :        1581 :                 EMPTYARC(right, final);
     742         [ -  + ]:        1581 :                 NOERRN();
     743                 :        1581 :                 branch = parsebranch(v, stopper, type, left, right, 0);
     744         [ +  + ]:        1581 :                 NOERRN();
     745         [ +  + ]:        1573 :                 if (lastbranch)
     746                 :          53 :                         lastbranch->sibling = branch;
     747                 :             :                 else
     748                 :        1520 :                         branches->child = branch;
     749                 :        1573 :                 branches->flags |= UP(branches->flags | branch->flags);
     750                 :        1573 :                 lastbranch = branch;
     751   [ +  +  +  +  :        1581 :         } while (EAT('|'));
                   +  + ]
     752   [ +  +  +  - ]:        1520 :         assert(SEE(stopper) || SEE(EOS));
     753                 :             : 
     754         [ +  + ]:        1520 :         if (!SEE(stopper))
     755                 :             :         {
     756         [ +  - ]:           2 :                 assert(stopper == ')' && SEE(EOS));
     757         [ -  + ]:           2 :                 ERR(REG_EPAREN);
     758                 :           2 :         }
     759                 :             : 
     760                 :             :         /* optimize out simple cases */
     761         [ +  + ]:        1520 :         if (lastbranch == branches->child)
     762                 :             :         {                                                       /* only one branch */
     763         [ +  - ]:        1480 :                 assert(lastbranch->sibling == NULL);
     764                 :        1480 :                 freesrnode(v, branches);
     765                 :        1480 :                 branches = lastbranch;
     766                 :        1480 :         }
     767         [ +  + ]:          40 :         else if (!MESSY(branches->flags))
     768                 :             :         {                                                       /* no interesting innards */
     769                 :          18 :                 freesubreandsiblings(v, branches->child);
     770                 :          18 :                 branches->child = NULL;
     771                 :          18 :                 branches->op = '=';
     772                 :          18 :         }
     773                 :             : 
     774                 :        1520 :         return branches;
     775                 :        1528 : }
     776                 :             : 
     777                 :             : /*
     778                 :             :  * parsebranch - parse one branch of an RE
     779                 :             :  *
     780                 :             :  * This mostly manages concatenation, working closely with parseqatom().
     781                 :             :  * Concatenated things are bundled up as much as possible, with separate
     782                 :             :  * '.' nodes introduced only when necessary due to substructure.
     783                 :             :  */
     784                 :             : static struct subre *
     785                 :        2163 : parsebranch(struct vars *v,
     786                 :             :                         int stopper,            /* EOS or ')' */
     787                 :             :                         int type,                       /* LACON (lookaround subRE) or PLAIN */
     788                 :             :                         struct state *left, /* leftmost state */
     789                 :             :                         struct state *right,    /* rightmost state */
     790                 :             :                         int partial)            /* is this only part of a branch? */
     791                 :             : {
     792                 :        2163 :         struct state *lp;                       /* left end of current construct */
     793                 :        2163 :         int                     seencontent;    /* is there anything in this branch yet? */
     794                 :        2163 :         struct subre *t;
     795                 :             : 
     796                 :        2163 :         lp = left;
     797                 :        2163 :         seencontent = 0;
     798                 :        2163 :         t = subre(v, '=', 0, left, right);      /* op '=' is tentative */
     799         [ -  + ]:        2163 :         NOERRN();
     800   [ +  +  +  +  :       14963 :         while (!SEE('|') && !SEE(stopper) && !SEE(EOS))
                   +  + ]
     801                 :             :         {
     802         [ +  + ]:       12811 :                 if (seencontent)
     803                 :             :                 {                                               /* implicit concat operator */
     804                 :       10676 :                         lp = newstate(v->nfa);
     805         [ -  + ]:       10676 :                         NOERRN();
     806                 :       10676 :                         moveins(v->nfa, right, lp);
     807                 :       10676 :                 }
     808                 :       12811 :                 seencontent = 1;
     809                 :             : 
     810                 :             :                 /* NB, recursion in parseqatom() may swallow rest of branch */
     811                 :       12811 :                 t = parseqatom(v, stopper, type, lp, right, t);
     812         [ +  + ]:       12811 :                 NOERRN();
     813                 :             :         }
     814                 :             : 
     815         [ +  + ]:        2152 :         if (!seencontent)
     816                 :             :         {                                                       /* empty branch */
     817         [ -  + ]:          28 :                 if (!partial)
     818                 :          28 :                         NOTE(REG_UUNSPEC);
     819         [ +  - ]:          28 :                 assert(lp == left);
     820                 :          28 :                 EMPTYARC(left, right);
     821                 :          28 :         }
     822                 :             : 
     823                 :        2152 :         return t;
     824                 :        2163 : }
     825                 :             : 
     826                 :             : /*
     827                 :             :  * parseqatom - parse one quantified atom or constraint of an RE
     828                 :             :  *
     829                 :             :  * The bookkeeping near the end cooperates very closely with parsebranch();
     830                 :             :  * in particular, it contains a recursion that can involve parsing the rest
     831                 :             :  * of the branch, making this function's name somewhat inaccurate.
     832                 :             :  *
     833                 :             :  * Usually, the return value is just "top", but in some cases where we
     834                 :             :  * have parsed the rest of the branch, we may deem "top" redundant and
     835                 :             :  * free it, returning some child subre instead.
     836                 :             :  */
     837                 :             : static struct subre *
     838                 :       12811 : parseqatom(struct vars *v,
     839                 :             :                    int stopper,                 /* EOS or ')' */
     840                 :             :                    int type,                    /* LACON (lookaround subRE) or PLAIN */
     841                 :             :                    struct state *lp,    /* left state to hang it on */
     842                 :             :                    struct state *rp,    /* right state to hang it on */
     843                 :             :                    struct subre *top)   /* subtree top */
     844                 :             : {
     845                 :       12811 :         struct state *s;                        /* temporaries for new states */
     846                 :       12811 :         struct state *s2;
     847                 :             : 
     848                 :             : #define  ARCV(t, val)    newarc(v->nfa, t, val, lp, rp)
     849                 :       12811 :         int                     m,
     850                 :             :                                 n;
     851                 :       12811 :         struct subre *atom;                     /* atom's subtree */
     852                 :       12811 :         struct subre *t;
     853                 :       12811 :         int                     cap;                    /* capturing parens? */
     854                 :       12811 :         int                     latype;                 /* lookaround constraint type */
     855                 :       12811 :         int                     subno;                  /* capturing-parens or backref number */
     856                 :       12811 :         int                     atomtype;
     857                 :       12811 :         int                     qprefer;                /* quantifier short/long preference */
     858                 :       12811 :         int                     f;
     859                 :       12811 :         struct subre **atomp;           /* where the pointer to atom is */
     860                 :             : 
     861                 :             :         /* initial bookkeeping */
     862                 :       12811 :         atom = NULL;
     863         [ +  - ]:       12811 :         assert(lp->nouts == 0);              /* must string new code */
     864         [ +  - ]:       12811 :         assert(rp->nins == 0);               /* between lp and rp */
     865                 :       12811 :         subno = 0;                                      /* just to shut lint up */
     866                 :             : 
     867                 :             :         /* an atom or constraint... */
     868                 :       12811 :         atomtype = v->nexttype;
     869   [ +  +  -  -  :       12811 :         switch (atomtype)
          -  +  -  +  +  
          -  +  +  -  +  
             +  +  +  +  
                      + ]
     870                 :             :         {
     871                 :             :                         /* first, constraints, which end by returning */
     872                 :             :                 case '^':
     873                 :         593 :                         ARCV('^', 1);
     874         [ +  + ]:         593 :                         if (v->cflags & REG_NLANCH)
     875                 :           7 :                                 ARCV(BEHIND, v->nlcolor);
     876                 :         593 :                         NEXT();
     877                 :         593 :                         return top;
     878                 :             :                         break;
     879                 :             :                 case '$':
     880                 :         574 :                         ARCV('$', 1);
     881         [ +  + ]:         574 :                         if (v->cflags & REG_NLANCH)
     882                 :           5 :                                 ARCV(AHEAD, v->nlcolor);
     883                 :         574 :                         NEXT();
     884                 :         574 :                         return top;
     885                 :             :                         break;
     886                 :             :                 case SBEGIN:
     887                 :           0 :                         ARCV('^', 1);           /* BOL */
     888                 :           0 :                         ARCV('^', 0);           /* or BOS */
     889                 :           0 :                         NEXT();
     890                 :           0 :                         return top;
     891                 :             :                         break;
     892                 :             :                 case SEND:
     893                 :           0 :                         ARCV('$', 1);           /* EOL */
     894                 :           0 :                         ARCV('$', 0);           /* or EOS */
     895                 :           0 :                         NEXT();
     896                 :           0 :                         return top;
     897                 :             :                         break;
     898                 :             :                 case '<':
     899                 :           4 :                         wordchrs(v);
     900                 :           4 :                         s = newstate(v->nfa);
     901         [ -  + ]:           4 :                         NOERRN();
     902                 :           4 :                         nonword(v, BEHIND, lp, s);
     903                 :           4 :                         word(v, AHEAD, s, rp);
     904                 :           4 :                         NEXT();
     905                 :           4 :                         return top;
     906                 :             :                         break;
     907                 :             :                 case '>':
     908                 :           3 :                         wordchrs(v);
     909                 :           3 :                         s = newstate(v->nfa);
     910         [ -  + ]:           3 :                         NOERRN();
     911                 :           3 :                         word(v, BEHIND, lp, s);
     912                 :           3 :                         nonword(v, AHEAD, s, rp);
     913                 :           3 :                         NEXT();
     914                 :           3 :                         return top;
     915                 :             :                         break;
     916                 :             :                 case WBDRY:
     917                 :           0 :                         wordchrs(v);
     918                 :           0 :                         s = newstate(v->nfa);
     919         [ #  # ]:           0 :                         NOERRN();
     920                 :           0 :                         nonword(v, BEHIND, lp, s);
     921                 :           0 :                         word(v, AHEAD, s, rp);
     922                 :           0 :                         s = newstate(v->nfa);
     923         [ #  # ]:           0 :                         NOERRN();
     924                 :           0 :                         word(v, BEHIND, lp, s);
     925                 :           0 :                         nonword(v, AHEAD, s, rp);
     926                 :           0 :                         NEXT();
     927                 :           0 :                         return top;
     928                 :             :                         break;
     929                 :             :                 case NWBDRY:
     930                 :           2 :                         wordchrs(v);
     931                 :           2 :                         s = newstate(v->nfa);
     932         [ -  + ]:           2 :                         NOERRN();
     933                 :           2 :                         word(v, BEHIND, lp, s);
     934                 :           2 :                         word(v, AHEAD, s, rp);
     935                 :           2 :                         s = newstate(v->nfa);
     936         [ +  - ]:           2 :                         NOERRN();
     937                 :           2 :                         nonword(v, BEHIND, lp, s);
     938                 :           2 :                         nonword(v, AHEAD, s, rp);
     939                 :           2 :                         NEXT();
     940                 :           2 :                         return top;
     941                 :             :                         break;
     942                 :             :                 case LACON:                             /* lookaround constraint */
     943                 :          27 :                         latype = v->nextvalue;
     944                 :          27 :                         NEXT();
     945                 :          27 :                         s = newstate(v->nfa);
     946                 :          27 :                         s2 = newstate(v->nfa);
     947         [ -  + ]:          27 :                         NOERRN();
     948                 :          27 :                         t = parse(v, ')', LACON, s, s2);
     949                 :          27 :                         freesubre(v, t);        /* internal structure irrelevant */
     950         [ +  + ]:          27 :                         NOERRN();
     951         [ +  - ]:          25 :                         assert(SEE(')'));
     952                 :          25 :                         NEXT();
     953                 :          25 :                         processlacon(v, s, s2, latype, lp, rp);
     954                 :          25 :                         return top;
     955                 :             :                         break;
     956                 :             :                         /* then errors, to get them out of the way */
     957                 :             :                 case '*':
     958                 :             :                 case '+':
     959                 :             :                 case '?':
     960                 :             :                 case '{':
     961         [ #  # ]:           0 :                         ERR(REG_BADRPT);
     962                 :           0 :                         return top;
     963                 :             :                         break;
     964                 :             :                 default:
     965         [ #  # ]:           0 :                         ERR(REG_ASSERT);
     966                 :           0 :                         return top;
     967                 :             :                         break;
     968                 :             :                         /* then plain characters, and minor variants on that theme */
     969                 :             :                 case ')':                               /* unbalanced paren */
     970         [ #  # ]:           0 :                         if ((v->cflags & REG_ADVANCED) != REG_EXTENDED)
     971                 :             :                         {
     972         [ #  # ]:           0 :                                 ERR(REG_EPAREN);
     973                 :           0 :                                 return top;
     974                 :             :                         }
     975                 :             :                         /* legal in EREs due to specification botch */
     976                 :           0 :                         NOTE(REG_UPBOTCH);
     977                 :             :                         /* fall through into case PLAIN */
     978                 :             :                         /* FALLTHROUGH */
     979                 :             :                 case PLAIN:
     980                 :       10392 :                         onechr(v, v->nextvalue, lp, rp);
     981                 :       10392 :                         okcolors(v->nfa, v->cm);
     982         [ -  + ]:       10392 :                         NOERRN();
     983                 :       10392 :                         NEXT();
     984                 :       10392 :                         break;
     985                 :             :                 case '[':
     986         [ +  + ]:          66 :                         if (v->nextvalue == 1)
     987                 :          49 :                                 bracket(v, lp, rp);
     988                 :             :                         else
     989                 :          17 :                                 cbracket(v, lp, rp);
     990   [ -  +  #  # ]:          66 :                         assert(SEE(']') || ISERR());
     991                 :          66 :                         NEXT();
     992                 :          66 :                         break;
     993                 :             :                 case CCLASSS:
     994                 :          50 :                         charclass(v, (enum char_classes) v->nextvalue, lp, rp);
     995                 :          50 :                         okcolors(v->nfa, v->cm);
     996                 :          50 :                         NEXT();
     997                 :          50 :                         break;
     998                 :             :                 case CCLASSC:
     999                 :           3 :                         charclasscomplement(v, (enum char_classes) v->nextvalue, lp, rp);
    1000                 :             :                         /* charclasscomplement() did okcolors() internally */
    1001                 :           3 :                         NEXT();
    1002                 :           3 :                         break;
    1003                 :             :                 case '.':
    1004                 :         774 :                         rainbow(v->nfa, v->cm, PLAIN,
    1005         [ +  + ]:         387 :                                         (v->cflags & REG_NLSTOP) ? v->nlcolor : COLORLESS,
    1006                 :         387 :                                         lp, rp);
    1007                 :         387 :                         NEXT();
    1008                 :         387 :                         break;
    1009                 :             :                         /* and finally the ugly stuff */
    1010                 :             :                 case '(':                               /* value flags as capturing or non */
    1011         [ +  + ]:         690 :                         cap = (type == LACON) ? 0 : v->nextvalue;
    1012         [ +  + ]:         690 :                         if (cap)
    1013                 :             :                         {
    1014                 :         648 :                                 v->nsubexp++;
    1015                 :         648 :                                 subno = v->nsubexp;
    1016         [ +  - ]:         648 :                                 if ((size_t) subno >= v->nsubs)
    1017                 :           0 :                                         moresubs(v, subno);
    1018                 :         648 :                         }
    1019                 :             :                         else
    1020                 :          42 :                                 atomtype = PLAIN;       /* something that's not '(' */
    1021                 :         690 :                         NEXT();
    1022                 :             : 
    1023                 :             :                         /*
    1024                 :             :                          * Make separate endpoint states to keep this sub-NFA distinct
    1025                 :             :                          * from what surrounds it.  We need to be sure that when we
    1026                 :             :                          * duplicate the sub-NFA for a backref, we get the right
    1027                 :             :                          * states/arcs and no others.  In particular, letting a backref
    1028                 :             :                          * duplicate the sub-NFA from lp to rp would be quite wrong,
    1029                 :             :                          * because we may add quantification superstructure around this
    1030                 :             :                          * atom below.  (Perhaps we could skip the extra states for
    1031                 :             :                          * non-capturing parens, but it seems not worth the trouble.)
    1032                 :             :                          */
    1033                 :         690 :                         s = newstate(v->nfa);
    1034                 :         690 :                         s2 = newstate(v->nfa);
    1035         [ -  + ]:         690 :                         NOERRN();
    1036                 :             :                         /* We may not need these arcs, but keep things connected for now */
    1037                 :         690 :                         EMPTYARC(lp, s);
    1038                 :         690 :                         EMPTYARC(s2, rp);
    1039         [ -  + ]:         690 :                         NOERRN();
    1040                 :         690 :                         atom = parse(v, ')', type, s, s2);
    1041   [ +  +  +  - ]:         690 :                         assert(SEE(')') || ISERR());
    1042                 :         690 :                         NEXT();
    1043         [ +  + ]:         690 :                         NOERRN();
    1044         [ +  + ]:         687 :                         if (cap)
    1045                 :             :                         {
    1046         [ +  + ]:         646 :                                 if (atom->capno == 0)
    1047                 :             :                                 {
    1048                 :             :                                         /* normal case: just mark the atom as capturing */
    1049                 :         643 :                                         atom->flags |= CAP;
    1050                 :         643 :                                         atom->capno = subno;
    1051                 :         643 :                                 }
    1052                 :             :                                 else
    1053                 :             :                                 {
    1054                 :             :                                         /* generate no-op wrapper node to handle "((x))" */
    1055                 :           3 :                                         t = subre(v, '(', atom->flags | CAP, s, s2);
    1056         [ -  + ]:           3 :                                         NOERRN();
    1057                 :           3 :                                         t->capno = subno;
    1058                 :           3 :                                         t->child = atom;
    1059                 :           3 :                                         atom = t;
    1060                 :             :                                 }
    1061         [ +  - ]:         646 :                                 assert(v->subs[subno] == NULL);
    1062                 :         646 :                                 v->subs[subno] = atom;
    1063                 :         646 :                         }
    1064                 :             :                         /* postpone everything else pending possible {0} */
    1065                 :         687 :                         break;
    1066                 :             :                 case BACKREF:                   /* the Feature From The Black Lagoon */
    1067   [ +  +  -  + ]:          20 :                         INSIST(type != LACON, REG_ESUBREG);
    1068                 :          20 :                         subno = v->nextvalue;
    1069         [ +  - ]:          20 :                         assert(subno > 0);
    1070   [ +  -  #  # ]:          20 :                         INSIST(subno < v->nsubs, REG_ESUBREG);
    1071         [ +  + ]:          20 :                         NOERRN();
    1072   [ +  -  #  # ]:          18 :                         INSIST(v->subs[subno] != NULL, REG_ESUBREG);
    1073         [ -  + ]:          18 :                         NOERRN();
    1074                 :          18 :                         atom = subre(v, 'b', BACKR, lp, rp);
    1075         [ -  + ]:          18 :                         NOERRN();
    1076                 :          18 :                         atom->backno = subno;
    1077                 :          18 :                         v->subs[subno]->flags |= BRUSE;
    1078                 :          18 :                         EMPTYARC(lp, rp);       /* temporarily, so there's something */
    1079                 :          18 :                         NEXT();
    1080                 :          18 :                         break;
    1081                 :             :         }
    1082                 :             : 
    1083                 :             :         /* ...and an atom may be followed by a quantifier */
    1084   [ +  +  +  +  :       11603 :         switch (v->nexttype)
                      + ]
    1085                 :             :         {
    1086                 :             :                 case '*':
    1087                 :        3143 :                         m = 0;
    1088                 :        3143 :                         n = DUPINF;
    1089                 :        3143 :                         qprefer = (v->nextvalue) ? LONGER : SHORTER;
    1090                 :        3143 :                         NEXT();
    1091                 :        3143 :                         break;
    1092                 :             :                 case '+':
    1093                 :          95 :                         m = 1;
    1094                 :          95 :                         n = DUPINF;
    1095                 :          95 :                         qprefer = (v->nextvalue) ? LONGER : SHORTER;
    1096                 :          95 :                         NEXT();
    1097                 :          95 :                         break;
    1098                 :             :                 case '?':
    1099                 :          11 :                         m = 0;
    1100                 :          11 :                         n = 1;
    1101                 :          11 :                         qprefer = (v->nextvalue) ? LONGER : SHORTER;
    1102                 :          11 :                         NEXT();
    1103                 :          11 :                         break;
    1104                 :             :                 case '{':
    1105                 :          48 :                         NEXT();
    1106                 :          48 :                         m = scannum(v);
    1107   [ +  +  -  + ]:          48 :                         if (EAT(','))
    1108                 :             :                         {
    1109         [ +  + ]:          25 :                                 if (SEE(DIGIT))
    1110                 :          24 :                                         n = scannum(v);
    1111                 :             :                                 else
    1112                 :           1 :                                         n = DUPINF;
    1113         [ +  + ]:          25 :                                 if (m > n)
    1114                 :             :                                 {
    1115         [ -  + ]:           1 :                                         ERR(REG_BADBR);
    1116                 :           1 :                                         return top;
    1117                 :             :                                 }
    1118                 :             :                                 /* {m,n} exercises preference, even if it's {m,m} */
    1119                 :          24 :                                 qprefer = (v->nextvalue) ? LONGER : SHORTER;
    1120                 :          24 :                         }
    1121                 :             :                         else
    1122                 :             :                         {
    1123                 :          23 :                                 n = m;
    1124                 :             :                                 /* {m} passes operand's preference through */
    1125                 :          23 :                                 qprefer = 0;
    1126                 :             :                         }
    1127         [ +  - ]:          47 :                         if (!SEE('}'))
    1128                 :             :                         {                                       /* catches errors too */
    1129         [ #  # ]:           0 :                                 ERR(REG_BADBR);
    1130                 :           0 :                                 return top;
    1131                 :             :                         }
    1132                 :          47 :                         NEXT();
    1133                 :          47 :                         break;
    1134                 :             :                 default:                                /* no quantifier */
    1135                 :        8306 :                         m = n = 1;
    1136                 :        8306 :                         qprefer = 0;
    1137                 :        8306 :                         break;
    1138                 :             :         }
    1139                 :             : 
    1140                 :             :         /* annoying special case:  {0} or {0,0} cancels everything */
    1141   [ +  +  +  + ]:       11602 :         if (m == 0 && n == 0)
    1142                 :             :         {
    1143                 :             :                 /*
    1144                 :             :                  * If we had capturing subexpression(s) within the atom, we don't want
    1145                 :             :                  * to destroy them, because it's legal (if useless) to back-ref them
    1146                 :             :                  * later.  Hence, just unlink the atom from lp/rp and then ignore it.
    1147                 :             :                  */
    1148   [ +  -  -  + ]:           4 :                 if (atom != NULL && (atom->flags & CAP))
    1149                 :             :                 {
    1150                 :           4 :                         delsub(v->nfa, lp, atom->begin);
    1151                 :           4 :                         delsub(v->nfa, atom->end, rp);
    1152                 :           4 :                 }
    1153                 :             :                 else
    1154                 :             :                 {
    1155                 :             :                         /* Otherwise, we can clean up any subre infrastructure we made */
    1156         [ #  # ]:           0 :                         if (atom != NULL)
    1157                 :           0 :                                 freesubre(v, atom);
    1158                 :           0 :                         delsub(v->nfa, lp, rp);
    1159                 :             :                 }
    1160                 :           4 :                 EMPTYARC(lp, rp);
    1161                 :           4 :                 return top;
    1162                 :             :         }
    1163                 :             : 
    1164                 :             :         /* if not a messy case, avoid hard part */
    1165         [ +  - ]:       11598 :         assert(!MESSY(top->flags));
    1166         [ +  + ]:       11598 :         f = top->flags | qprefer | ((atom != NULL) ? atom->flags : 0);
    1167   [ +  +  +  +  :       11598 :         if (atomtype != '(' && atomtype != BACKREF && !MESSY(UP(f)))
                   +  + ]
    1168                 :             :         {
    1169   [ +  +  +  + ]:       10935 :                 if (!(m == 1 && n == 1))
    1170                 :        3209 :                         repeat(v, lp, rp, m, n);
    1171         [ +  + ]:       10935 :                 if (atom != NULL)
    1172                 :          37 :                         freesubre(v, atom);
    1173                 :       10935 :                 top->flags = f;
    1174                 :       10935 :                 return top;
    1175                 :             :         }
    1176                 :             : 
    1177                 :             :         /*
    1178                 :             :          * hard part:  something messy
    1179                 :             :          *
    1180                 :             :          * That is, capturing parens, back reference, short/long clash, or an atom
    1181                 :             :          * with substructure containing one of those.
    1182                 :             :          */
    1183                 :             : 
    1184                 :             :         /* now we'll need a subre for the contents even if they're boring */
    1185         [ +  - ]:         663 :         if (atom == NULL)
    1186                 :             :         {
    1187                 :           0 :                 atom = subre(v, '=', 0, lp, rp);
    1188         [ #  # ]:           0 :                 NOERRN();
    1189                 :           0 :         }
    1190                 :             : 
    1191                 :             :         /*
    1192                 :             :          * For what follows, we need the atom to have its own begin/end states
    1193                 :             :          * that are distinct from lp/rp, so that we can wrap iteration structure
    1194                 :             :          * around it.  The parenthesized-atom case above already made suitable
    1195                 :             :          * states (and we don't want to modify a capturing subre, since it's
    1196                 :             :          * already recorded in v->subs[]).  Otherwise, we need more states.
    1197                 :             :          */
    1198   [ +  +  -  + ]:         663 :         if (atom->begin == lp || atom->end == rp)
    1199                 :             :         {
    1200                 :          18 :                 s = newstate(v->nfa);
    1201                 :          18 :                 s2 = newstate(v->nfa);
    1202         [ -  + ]:          18 :                 NOERRN();
    1203                 :          18 :                 moveouts(v->nfa, lp, s);
    1204                 :          18 :                 moveins(v->nfa, rp, s2);
    1205                 :          18 :                 atom->begin = s;
    1206                 :          18 :                 atom->end = s2;
    1207                 :          18 :         }
    1208                 :             :         else
    1209                 :             :         {
    1210                 :             :                 /* The atom's OK, but we must temporarily disconnect it from lp/rp */
    1211                 :             :                 /* (this removes the EMPTY arcs we made above) */
    1212                 :         645 :                 delsub(v->nfa, lp, atom->begin);
    1213                 :         645 :                 delsub(v->nfa, atom->end, rp);
    1214                 :             :         }
    1215                 :             : 
    1216                 :             :         /*----------
    1217                 :             :          * Prepare a general-purpose state skeleton.
    1218                 :             :          *
    1219                 :             :          * In the no-backrefs case, we want this:
    1220                 :             :          *
    1221                 :             :          * [lp] ---> [s] ---prefix---> ---atom---> ---rest---> [rp]
    1222                 :             :          *
    1223                 :             :          * where prefix is some repetitions of atom, and "rest" is the remainder
    1224                 :             :          * of the branch.  In the general case we need:
    1225                 :             :          *
    1226                 :             :          * [lp] ---> [s] ---iterator---> [s2] ---rest---> [rp]
    1227                 :             :          *
    1228                 :             :          * where the iterator wraps around the atom.
    1229                 :             :          *
    1230                 :             :          * We make the s state here for both cases; s2 is made below if needed
    1231                 :             :          *----------
    1232                 :             :          */
    1233                 :         663 :         s = newstate(v->nfa);                /* set up starting state */
    1234         [ -  + ]:         663 :         NOERRN();
    1235                 :         663 :         EMPTYARC(lp, s);
    1236         [ -  + ]:         663 :         NOERRN();
    1237                 :             : 
    1238                 :             :         /* break remaining subRE into x{...} and what follows */
    1239         [ +  + ]:         663 :         t = subre(v, '.', COMBINE(qprefer, atom->flags), lp, rp);
    1240         [ -  + ]:         663 :         NOERRN();
    1241                 :         663 :         t->child = atom;
    1242                 :         663 :         atomp = &t->child;
    1243                 :             : 
    1244                 :             :         /*
    1245                 :             :          * Here we should recurse to fill t->child->sibling ... but we must
    1246                 :             :          * postpone that to the end.  One reason is that t->child may be replaced
    1247                 :             :          * below, and we don't want to worry about its sibling link.
    1248                 :             :          */
    1249                 :             : 
    1250                 :             :         /*
    1251                 :             :          * Convert top node to a concatenation of the prefix (top->child, covering
    1252                 :             :          * whatever we parsed previously) and remaining (t).  Note that the prefix
    1253                 :             :          * could be empty, in which case this concatenation node is unnecessary.
    1254                 :             :          * To keep things simple, we operate in a general way for now, and get rid
    1255                 :             :          * of unnecessary subres below.
    1256                 :             :          */
    1257         [ +  - ]:         663 :         assert(top->op == '=' && top->child == NULL);
    1258                 :         663 :         top->child = subre(v, '=', top->flags, top->begin, lp);
    1259         [ -  + ]:         663 :         NOERRN();
    1260                 :         663 :         top->op = '.';
    1261                 :         663 :         top->child->sibling = t;
    1262                 :             :         /* top->flags will get updated later */
    1263                 :             : 
    1264                 :             :         /* if it's a backref, now is the time to replicate the subNFA */
    1265         [ +  + ]:         663 :         if (atomtype == BACKREF)
    1266                 :             :         {
    1267         [ +  - ]:          18 :                 assert(atom->begin->nouts == 1);  /* just the EMPTY */
    1268                 :          18 :                 delsub(v->nfa, atom->begin, atom->end);
    1269         [ +  - ]:          18 :                 assert(v->subs[subno] != NULL);
    1270                 :             : 
    1271                 :             :                 /*
    1272                 :             :                  * And here's why the recursion got postponed: it must wait until the
    1273                 :             :                  * skeleton is filled in, because it may hit a backref that wants to
    1274                 :             :                  * copy the filled-in skeleton.
    1275                 :             :                  */
    1276                 :          36 :                 dupnfa(v->nfa, v->subs[subno]->begin, v->subs[subno]->end,
    1277                 :          18 :                            atom->begin, atom->end);
    1278         [ -  + ]:          18 :                 NOERRN();
    1279                 :             : 
    1280                 :             :                 /* The backref node's NFA should not enforce any constraints */
    1281                 :          18 :                 removeconstraints(v->nfa, atom->begin, atom->end);
    1282         [ -  + ]:          18 :                 NOERRN();
    1283                 :          18 :         }
    1284                 :             : 
    1285                 :             :         /*
    1286                 :             :          * It's quantifier time.  If the atom is just a backref, we'll let it deal
    1287                 :             :          * with quantifiers internally.
    1288                 :             :          */
    1289         [ +  + ]:         663 :         if (atomtype == BACKREF)
    1290                 :             :         {
    1291                 :             :                 /* special case:  backrefs have internal quantifiers */
    1292                 :          18 :                 EMPTYARC(s, atom->begin);    /* empty prefix */
    1293                 :             :                 /* just stuff everything into atom */
    1294                 :          18 :                 repeat(v, atom->begin, atom->end, m, n);
    1295                 :          18 :                 atom->min = (short) m;
    1296                 :          18 :                 atom->max = (short) n;
    1297         [ +  + ]:          18 :                 atom->flags |= COMBINE(qprefer, atom->flags);
    1298                 :             :                 /* rest of branch can be strung starting from atom->end */
    1299                 :          18 :                 s2 = atom->end;
    1300                 :          18 :         }
    1301   [ +  +  +  +  :         660 :         else if (m == 1 && n == 1 &&
                   +  + ]
    1302         [ +  + ]:         601 :                          (qprefer == 0 ||
    1303         [ +  + ]:          17 :                           (atom->flags & (LONGER | SHORTER | MIXED)) == 0 ||
    1304                 :          15 :                           qprefer == (atom->flags & (LONGER | SHORTER | MIXED))))
    1305                 :             :         {
    1306                 :             :                 /* no/vacuous quantifier:  done */
    1307                 :         594 :                 EMPTYARC(s, atom->begin);    /* empty prefix */
    1308                 :             :                 /* rest of branch can be strung starting from atom->end */
    1309                 :         594 :                 s2 = atom->end;
    1310                 :         594 :         }
    1311         [ +  + ]:          51 :         else if (!(atom->flags & (CAP | BACKR)))
    1312                 :             :         {
    1313                 :             :                 /*
    1314                 :             :                  * If there's no captures nor backrefs in the atom being repeated, we
    1315                 :             :                  * don't really care where the submatches of the iteration are, so we
    1316                 :             :                  * don't need an iteration node.  Make a plain DFA node instead.
    1317                 :             :                  */
    1318                 :           2 :                 EMPTYARC(s, atom->begin);    /* empty prefix */
    1319                 :           2 :                 repeat(v, atom->begin, atom->end, m, n);
    1320         [ +  - ]:           2 :                 f = COMBINE(qprefer, atom->flags);
    1321                 :           2 :                 t = subre(v, '=', f, atom->begin, atom->end);
    1322         [ -  + ]:           2 :                 NOERRN();
    1323                 :           2 :                 freesubre(v, atom);
    1324                 :           2 :                 *atomp = t;
    1325                 :             :                 /* rest of branch can be strung starting from t->end */
    1326                 :           2 :                 s2 = t->end;
    1327                 :           2 :         }
    1328   [ +  +  +  + ]:          49 :         else if (m > 0 && !(atom->flags & BACKR))
    1329                 :             :         {
    1330                 :             :                 /*
    1331                 :             :                  * If there's no backrefs involved, we can turn x{m,n} into
    1332                 :             :                  * x{m-1,n-1}x, with capturing parens in only the second x.  This is
    1333                 :             :                  * valid because we only care about capturing matches from the final
    1334                 :             :                  * iteration of the quantifier.  It's a win because we can implement
    1335                 :             :                  * the backref-free left side as a plain DFA node, since we don't
    1336                 :             :                  * really care where its submatches are.
    1337                 :             :                  */
    1338                 :          32 :                 dupnfa(v->nfa, atom->begin, atom->end, s, atom->begin);
    1339         [ +  - ]:          32 :                 assert(m >= 1 && m != DUPINF && n >= 1);
    1340         [ +  + ]:          32 :                 repeat(v, s, atom->begin, m - 1, (n == DUPINF) ? n : n - 1);
    1341         [ +  - ]:          32 :                 f = COMBINE(qprefer, atom->flags);
    1342                 :          32 :                 t = subre(v, '.', f, s, atom->end); /* prefix and atom */
    1343         [ -  + ]:          32 :                 NOERRN();
    1344                 :          32 :                 t->child = subre(v, '=', PREF(f), s, atom->begin);
    1345         [ -  + ]:          32 :                 NOERRN();
    1346                 :          32 :                 t->child->sibling = atom;
    1347                 :          32 :                 *atomp = t;
    1348                 :             :                 /* rest of branch can be strung starting from atom->end */
    1349                 :          32 :                 s2 = atom->end;
    1350                 :          32 :         }
    1351                 :             :         else
    1352                 :             :         {
    1353                 :             :                 /* general case: need an iteration node */
    1354                 :          17 :                 s2 = newstate(v->nfa);
    1355         [ -  + ]:          17 :                 NOERRN();
    1356                 :          17 :                 moveouts(v->nfa, atom->end, s2);
    1357         [ -  + ]:          17 :                 NOERRN();
    1358                 :          17 :                 dupnfa(v->nfa, atom->begin, atom->end, s, s2);
    1359                 :          17 :                 repeat(v, s, s2, m, n);
    1360         [ +  - ]:          17 :                 f = COMBINE(qprefer, atom->flags);
    1361                 :          17 :                 t = subre(v, '*', f, s, s2);
    1362         [ -  + ]:          17 :                 NOERRN();
    1363                 :          17 :                 t->min = (short) m;
    1364                 :          17 :                 t->max = (short) n;
    1365                 :          17 :                 t->child = atom;
    1366                 :          17 :                 *atomp = t;
    1367                 :             :                 /* rest of branch is to be strung from iteration's end state */
    1368                 :             :         }
    1369                 :             : 
    1370                 :             :         /* and finally, look after that postponed recursion */
    1371                 :         663 :         t = top->child->sibling;
    1372   [ +  +  +  +  :         663 :         if (!(SEE('|') || SEE(stopper) || SEE(EOS)))
                   -  + ]
    1373                 :             :         {
    1374                 :             :                 /* parse all the rest of the branch, and insert in t->child->sibling */
    1375                 :         582 :                 t->child->sibling = parsebranch(v, stopper, type, s2, rp, 1);
    1376         [ +  + ]:         582 :                 NOERRN();
    1377   [ +  +  -  +  :         579 :                 assert(SEE('|') || SEE(stopper) || SEE(EOS));
                   #  # ]
    1378                 :             : 
    1379                 :             :                 /* here's the promised update of the flags */
    1380         [ +  + ]:         579 :                 t->flags |= COMBINE(t->flags, t->child->sibling->flags);
    1381         [ +  + ]:         579 :                 top->flags |= COMBINE(top->flags, t->flags);
    1382                 :             : 
    1383                 :             :                 /* neither t nor top could be directly marked for capture as yet */
    1384         [ +  - ]:         579 :                 assert(t->capno == 0);
    1385         [ +  - ]:         579 :                 assert(top->capno == 0);
    1386                 :             : 
    1387                 :             :                 /*
    1388                 :             :                  * At this point both top and t are concatenation (op == '.') subres,
    1389                 :             :                  * and we have top->child = prefix of branch, top->child->sibling = t,
    1390                 :             :                  * t->child = messy atom (with quantification superstructure if
    1391                 :             :                  * needed), t->child->sibling = rest of branch.
    1392                 :             :                  *
    1393                 :             :                  * If the messy atom was the first thing in the branch, then
    1394                 :             :                  * top->child is vacuous and we can get rid of one level of
    1395                 :             :                  * concatenation.
    1396                 :             :                  */
    1397         [ +  - ]:         579 :                 assert(top->child->op == '=');
    1398         [ +  + ]:         579 :                 if (top->child->begin == top->child->end)
    1399                 :             :                 {
    1400         [ +  - ]:          51 :                         assert(!MESSY(top->child->flags));
    1401                 :          51 :                         freesubre(v, top->child);
    1402                 :          51 :                         top->child = t->child;
    1403                 :          51 :                         freesrnode(v, t);
    1404                 :          51 :                 }
    1405                 :             : 
    1406                 :             :                 /*
    1407                 :             :                  * Otherwise, it's possible that t->child is not messy in itself, but
    1408                 :             :                  * we considered it messy because its greediness conflicts with what
    1409                 :             :                  * preceded it.  Then it could be that the combination of t->child and
    1410                 :             :                  * the rest of the branch is also not messy, in which case we can get
    1411                 :             :                  * rid of the child concatenation by merging t->child and the rest of
    1412                 :             :                  * the branch into one plain DFA node.
    1413                 :             :                  */
    1414         [ +  + ]:         528 :                 else if (t->child->op == '=' &&
    1415   [ +  +  +  - ]:         520 :                                  t->child->sibling->op == '=' &&
    1416                 :         502 :                                  !MESSY(UP(t->child->flags | t->child->sibling->flags)))
    1417                 :             :                 {
    1418                 :           0 :                         t->op = '=';
    1419         [ #  # ]:           0 :                         t->flags = COMBINE(t->child->flags, t->child->sibling->flags);
    1420                 :           0 :                         freesubreandsiblings(v, t->child);
    1421                 :           0 :                         t->child = NULL;
    1422                 :           0 :                 }
    1423                 :         579 :         }
    1424                 :             :         else
    1425                 :             :         {
    1426                 :             :                 /*
    1427                 :             :                  * There's nothing left in the branch, so we don't need the second
    1428                 :             :                  * concatenation node 't'.  Just link s2 straight to rp.
    1429                 :             :                  */
    1430                 :          81 :                 EMPTYARC(s2, rp);
    1431                 :          81 :                 top->child->sibling = t->child;
    1432         [ +  + ]:          81 :                 top->flags |= COMBINE(top->flags, top->child->sibling->flags);
    1433                 :          81 :                 freesrnode(v, t);
    1434                 :             : 
    1435                 :             :                 /*
    1436                 :             :                  * Again, it could be that top->child is vacuous (if the messy atom
    1437                 :             :                  * was in fact the only thing in the branch).  In that case we need no
    1438                 :             :                  * concatenation at all; just replace top with top->child->sibling.
    1439                 :             :                  */
    1440         [ +  - ]:          81 :                 assert(top->child->op == '=');
    1441         [ +  + ]:          81 :                 if (top->child->begin == top->child->end)
    1442                 :             :                 {
    1443         [ +  - ]:          57 :                         assert(!MESSY(top->child->flags));
    1444                 :          57 :                         t = top->child->sibling;
    1445                 :          57 :                         top->child->sibling = NULL;
    1446                 :          57 :                         freesubre(v, top);
    1447                 :          57 :                         top = t;
    1448                 :          57 :                 }
    1449                 :             :         }
    1450                 :             : 
    1451                 :         660 :         return top;
    1452                 :       12811 : }
    1453                 :             : 
    1454                 :             : /*
    1455                 :             :  * nonword - generate arcs for non-word-character ahead or behind
    1456                 :             :  */
    1457                 :             : static void
    1458                 :          11 : nonword(struct vars *v,
    1459                 :             :                 int dir,                                /* AHEAD or BEHIND */
    1460                 :             :                 struct state *lp,
    1461                 :             :                 struct state *rp)
    1462                 :             : {
    1463                 :          11 :         int                     anchor = (dir == AHEAD) ? '$' : '^';
    1464                 :             : 
    1465   [ +  +  +  - ]:          11 :         assert(dir == AHEAD || dir == BEHIND);
    1466                 :          11 :         newarc(v->nfa, anchor, 1, lp, rp);
    1467                 :          11 :         newarc(v->nfa, anchor, 0, lp, rp);
    1468                 :          11 :         colorcomplement(v->nfa, v->cm, dir, v->wordchrs, lp, rp);
    1469                 :             :         /* (no need for special attention to \n) */
    1470                 :          11 : }
    1471                 :             : 
    1472                 :             : /*
    1473                 :             :  * word - generate arcs for word character ahead or behind
    1474                 :             :  */
    1475                 :             : static void
    1476                 :          11 : word(struct vars *v,
    1477                 :             :          int dir,                                       /* AHEAD or BEHIND */
    1478                 :             :          struct state *lp,
    1479                 :             :          struct state *rp)
    1480                 :             : {
    1481   [ +  +  +  - ]:          11 :         assert(dir == AHEAD || dir == BEHIND);
    1482                 :          11 :         cloneouts(v->nfa, v->wordchrs, lp, rp, dir);
    1483                 :             :         /* (no need for special attention to \n) */
    1484                 :          11 : }
    1485                 :             : 
    1486                 :             : /*
    1487                 :             :  * charclass - generate arcs for a character class
    1488                 :             :  *
    1489                 :             :  * This is used for both atoms (\w and sibling escapes) and for elements
    1490                 :             :  * of bracket expressions.  The caller is responsible for calling okcolors()
    1491                 :             :  * at the end of processing the atom or bracket.
    1492                 :             :  */
    1493                 :             : static void
    1494                 :          74 : charclass(struct vars *v,
    1495                 :             :                   enum char_classes cls,
    1496                 :             :                   struct state *lp,
    1497                 :             :                   struct state *rp)
    1498                 :             : {
    1499                 :          74 :         struct cvec *cv;
    1500                 :             : 
    1501                 :             :         /* obtain possibly-cached cvec for char class */
    1502                 :          74 :         NOTE(REG_ULOCALE);
    1503                 :          74 :         cv = cclasscvec(v, cls, (v->cflags & REG_ICASE));
    1504         [ -  + ]:          74 :         NOERR();
    1505                 :             : 
    1506                 :             :         /* build the arcs; this may cause color splitting */
    1507                 :          74 :         subcolorcvec(v, cv, lp, rp);
    1508         [ -  + ]:          74 : }
    1509                 :             : 
    1510                 :             : /*
    1511                 :             :  * charclasscomplement - generate arcs for a complemented character class
    1512                 :             :  *
    1513                 :             :  * This is used for both atoms (\W and sibling escapes) and for elements
    1514                 :             :  * of bracket expressions.  In bracket expressions, it is the caller's
    1515                 :             :  * responsibility that there not be any open subcolors when this is called.
    1516                 :             :  */
    1517                 :             : static void
    1518                 :           3 : charclasscomplement(struct vars *v,
    1519                 :             :                                         enum char_classes cls,
    1520                 :             :                                         struct state *lp,
    1521                 :             :                                         struct state *rp)
    1522                 :             : {
    1523                 :           3 :         struct state *cstate;
    1524                 :           3 :         struct cvec *cv;
    1525                 :             : 
    1526                 :             :         /* make dummy state to hang temporary arcs on */
    1527                 :           3 :         cstate = newstate(v->nfa);
    1528         [ -  + ]:           3 :         NOERR();
    1529                 :             : 
    1530                 :             :         /* obtain possibly-cached cvec for char class */
    1531                 :           3 :         NOTE(REG_ULOCALE);
    1532                 :           3 :         cv = cclasscvec(v, cls, (v->cflags & REG_ICASE));
    1533         [ -  + ]:           3 :         NOERR();
    1534                 :             : 
    1535                 :             :         /* build arcs for char class; this may cause color splitting */
    1536                 :           3 :         subcolorcvec(v, cv, cstate, cstate);
    1537         [ -  + ]:           3 :         NOERR();
    1538                 :             : 
    1539                 :             :         /* clean up any subcolors in the arc set */
    1540                 :           3 :         okcolors(v->nfa, v->cm);
    1541         [ -  + ]:           3 :         NOERR();
    1542                 :             : 
    1543                 :             :         /* now build output arcs for the complement of the char class */
    1544                 :           3 :         colorcomplement(v->nfa, v->cm, PLAIN, cstate, lp, rp);
    1545         [ -  + ]:           3 :         NOERR();
    1546                 :             : 
    1547                 :             :         /* clean up dummy state */
    1548                 :           3 :         dropstate(v->nfa, cstate);
    1549         [ -  + ]:           3 : }
    1550                 :             : 
    1551                 :             : /*
    1552                 :             :  * scannum - scan a number
    1553                 :             :  */
    1554                 :             : static int                                              /* value, <= DUPMAX */
    1555                 :          72 : scannum(struct vars *v)
    1556                 :             : {
    1557                 :          72 :         int                     n = 0;
    1558                 :             : 
    1559   [ +  +  +  + ]:         146 :         while (SEE(DIGIT) && n < DUPMAX)
    1560                 :             :         {
    1561                 :          74 :                 n = n * 10 + v->nextvalue;
    1562                 :          74 :                 NEXT();
    1563                 :             :         }
    1564   [ +  -  -  + ]:          72 :         if (SEE(DIGIT) || n > DUPMAX)
    1565                 :             :         {
    1566         [ #  # ]:           0 :                 ERR(REG_BADBR);
    1567                 :           0 :                 return 0;
    1568                 :             :         }
    1569                 :          72 :         return n;
    1570                 :          72 : }
    1571                 :             : 
    1572                 :             : /*
    1573                 :             :  * repeat - replicate subNFA for quantifiers
    1574                 :             :  *
    1575                 :             :  * The sub-NFA strung from lp to rp is modified to represent m to n
    1576                 :             :  * repetitions of its initial contents.
    1577                 :             :  *
    1578                 :             :  * The duplication sequences used here are chosen carefully so that any
    1579                 :             :  * pointers starting out pointing into the subexpression end up pointing into
    1580                 :             :  * the last occurrence.  (Note that it may not be strung between the same
    1581                 :             :  * left and right end states, however!)  This used to be important for the
    1582                 :             :  * subRE tree, although the important bits are now handled by the in-line
    1583                 :             :  * code in parse(), and when this is called, it doesn't matter any more.
    1584                 :             :  */
    1585                 :             : static void
    1586                 :        3417 : repeat(struct vars *v,
    1587                 :             :            struct state *lp,
    1588                 :             :            struct state *rp,
    1589                 :             :            int m,
    1590                 :             :            int n)
    1591                 :             : {
    1592                 :             : #define  SOME    2
    1593                 :             : #define  INF     3
    1594                 :             : #define  PAIR(x, y)  ((x)*4 + (y))
    1595                 :             : #define  REDUCE(x)       ( ((x) == DUPINF) ? INF : (((x) > 1) ? SOME : (x)) )
    1596   [ -  +  +  + ]:        3417 :         const int       rm = REDUCE(m);
    1597   [ +  +  +  + ]:        3417 :         const int       rn = REDUCE(n);
    1598                 :        3417 :         struct state *s;
    1599                 :        3417 :         struct state *s2;
    1600                 :             : 
    1601   [ +  -  +  +  :        3417 :         switch (PAIR(rm, rn))
          -  +  -  +  +  
                      + ]
    1602                 :             :         {
    1603                 :             :                 case PAIR(0, 0):                /* empty string */
    1604                 :           5 :                         delsub(v->nfa, lp, rp);
    1605                 :           5 :                         EMPTYARC(lp, rp);
    1606                 :           5 :                         break;
    1607                 :             :                 case PAIR(0, 1):                /* do as x| */
    1608                 :          11 :                         EMPTYARC(lp, rp);
    1609                 :          11 :                         break;
    1610                 :             :                 case PAIR(0, SOME):             /* do as x{1,n}| */
    1611                 :           0 :                         repeat(v, lp, rp, 1, n);
    1612         [ #  # ]:           0 :                         NOERR();
    1613                 :           0 :                         EMPTYARC(lp, rp);
    1614                 :           0 :                         break;
    1615                 :             :                 case PAIR(0, INF):              /* loop x around */
    1616                 :        3170 :                         s = newstate(v->nfa);
    1617         [ -  + ]:        3170 :                         NOERR();
    1618                 :        3170 :                         moveouts(v->nfa, lp, s);
    1619                 :        3170 :                         moveins(v->nfa, rp, s);
    1620                 :        3170 :                         EMPTYARC(lp, s);
    1621                 :        3170 :                         EMPTYARC(s, rp);
    1622                 :        3170 :                         break;
    1623                 :             :                 case PAIR(1, 1):                /* no action required */
    1624                 :             :                         break;
    1625                 :             :                 case PAIR(1, SOME):             /* do as x{0,n-1}x = (x{1,n-1}|)x */
    1626                 :           0 :                         s = newstate(v->nfa);
    1627         [ #  # ]:           0 :                         NOERR();
    1628                 :           0 :                         moveouts(v->nfa, lp, s);
    1629                 :           0 :                         dupnfa(v->nfa, s, rp, lp, s);
    1630         [ #  # ]:           0 :                         NOERR();
    1631                 :           0 :                         repeat(v, lp, s, 1, n - 1);
    1632         [ #  # ]:           0 :                         NOERR();
    1633                 :           0 :                         EMPTYARC(lp, s);
    1634                 :           0 :                         break;
    1635                 :             :                 case PAIR(1, INF):              /* add loopback arc */
    1636                 :          69 :                         s = newstate(v->nfa);
    1637                 :          69 :                         s2 = newstate(v->nfa);
    1638         [ -  + ]:          69 :                         NOERR();
    1639                 :          69 :                         moveouts(v->nfa, lp, s);
    1640                 :          69 :                         moveins(v->nfa, rp, s2);
    1641                 :          69 :                         EMPTYARC(lp, s);
    1642                 :          69 :                         EMPTYARC(s2, rp);
    1643                 :          69 :                         EMPTYARC(s2, s);
    1644                 :          69 :                         break;
    1645                 :             :                 case PAIR(SOME, SOME):  /* do as x{m-1,n-1}x */
    1646                 :         105 :                         s = newstate(v->nfa);
    1647         [ -  + ]:         105 :                         NOERR();
    1648                 :         105 :                         moveouts(v->nfa, lp, s);
    1649                 :         105 :                         dupnfa(v->nfa, s, rp, lp, s);
    1650         [ -  + ]:         105 :                         NOERR();
    1651                 :         105 :                         repeat(v, lp, s, m - 1, n - 1);
    1652                 :         105 :                         break;
    1653                 :             :                 case PAIR(SOME, INF):   /* do as x{m-1,}x */
    1654                 :          34 :                         s = newstate(v->nfa);
    1655         [ -  + ]:          34 :                         NOERR();
    1656                 :          34 :                         moveouts(v->nfa, lp, s);
    1657                 :          34 :                         dupnfa(v->nfa, s, rp, lp, s);
    1658         [ -  + ]:          34 :                         NOERR();
    1659                 :          34 :                         repeat(v, lp, s, m - 1, n);
    1660                 :          34 :                         break;
    1661                 :             :                 default:
    1662         [ #  # ]:           0 :                         ERR(REG_ASSERT);
    1663                 :           0 :                         break;
    1664                 :             :         }
    1665         [ -  + ]:        3417 : }
    1666                 :             : 
    1667                 :             : /*
    1668                 :             :  * bracket - handle non-complemented bracket expression
    1669                 :             :  *
    1670                 :             :  * Also called from cbracket for complemented bracket expressions.
    1671                 :             :  */
    1672                 :             : static void
    1673                 :          66 : bracket(struct vars *v,
    1674                 :             :                 struct state *lp,
    1675                 :             :                 struct state *rp)
    1676                 :             : {
    1677                 :             :         /*
    1678                 :             :          * We can't process complemented char classes (e.g. \W) immediately while
    1679                 :             :          * scanning the bracket expression, else color bookkeeping gets confused.
    1680                 :             :          * Instead, remember whether we saw any in have_cclassc[], and process
    1681                 :             :          * them at the end.
    1682                 :             :          */
    1683                 :          66 :         bool            have_cclassc[NUM_CCLASSES];
    1684                 :          66 :         bool            any_cclassc;
    1685                 :          66 :         int                     i;
    1686                 :             : 
    1687                 :          66 :         memset(have_cclassc, false, sizeof(have_cclassc));
    1688                 :             : 
    1689         [ +  - ]:          66 :         assert(SEE('['));
    1690                 :          66 :         NEXT();
    1691   [ +  +  +  + ]:         194 :         while (!SEE(']') && !SEE(EOS))
    1692                 :         128 :                 brackpart(v, lp, rp, have_cclassc);
    1693   [ -  +  #  # ]:          66 :         assert(SEE(']') || ISERR());
    1694                 :             : 
    1695                 :             :         /* close up open subcolors from the positive bracket elements */
    1696                 :          66 :         okcolors(v->nfa, v->cm);
    1697         [ -  + ]:          66 :         NOERR();
    1698                 :             : 
    1699                 :             :         /* now handle any complemented elements */
    1700                 :          66 :         any_cclassc = false;
    1701         [ +  + ]:         990 :         for (i = 0; i < NUM_CCLASSES; i++)
    1702                 :             :         {
    1703         [ +  - ]:         924 :                 if (have_cclassc[i])
    1704                 :             :                 {
    1705                 :           0 :                         charclasscomplement(v, (enum char_classes) i, lp, rp);
    1706         [ #  # ]:           0 :                         NOERR();
    1707                 :           0 :                         any_cclassc = true;
    1708                 :           0 :                 }
    1709                 :         924 :         }
    1710                 :             : 
    1711                 :             :         /*
    1712                 :             :          * If we had any complemented elements, see if we can optimize the bracket
    1713                 :             :          * into a rainbow.  Since a complemented element is the only way a WHITE
    1714                 :             :          * arc could get into the result, there's no point in checking otherwise.
    1715                 :             :          */
    1716         [ +  - ]:          66 :         if (any_cclassc)
    1717                 :           0 :                 optimizebracket(v, lp, rp);
    1718         [ -  + ]:          66 : }
    1719                 :             : 
    1720                 :             : /*
    1721                 :             :  * cbracket - handle complemented bracket expression
    1722                 :             :  *
    1723                 :             :  * We do it by calling bracket() with dummy endpoints, and then complementing
    1724                 :             :  * the result.  The alternative would be to invoke rainbow(), and then delete
    1725                 :             :  * arcs as the b.e. is seen... but that gets messy, and is really quite
    1726                 :             :  * infeasible now that rainbow() just puts out one RAINBOW arc.
    1727                 :             :  */
    1728                 :             : static void
    1729                 :          17 : cbracket(struct vars *v,
    1730                 :             :                  struct state *lp,
    1731                 :             :                  struct state *rp)
    1732                 :             : {
    1733                 :          17 :         struct state *left = newstate(v->nfa);
    1734                 :          17 :         struct state *right = newstate(v->nfa);
    1735                 :             : 
    1736         [ -  + ]:          17 :         NOERR();
    1737                 :          17 :         bracket(v, left, right);
    1738                 :             : 
    1739                 :             :         /* in NLSTOP mode, ensure newline is not part of the result set */
    1740         [ +  - ]:          17 :         if (v->cflags & REG_NLSTOP)
    1741                 :           0 :                 newarc(v->nfa, PLAIN, v->nlcolor, left, right);
    1742         [ -  + ]:          17 :         NOERR();
    1743                 :             : 
    1744         [ +  - ]:          17 :         assert(lp->nouts == 0);              /* all outarcs will be ours */
    1745                 :             : 
    1746                 :             :         /*
    1747                 :             :          * Easy part of complementing, and all there is to do since the MCCE code
    1748                 :             :          * was removed.  Note that the result of colorcomplement() cannot be a
    1749                 :             :          * rainbow, since we don't allow empty brackets; so there's no point in
    1750                 :             :          * calling optimizebracket() again.
    1751                 :             :          */
    1752                 :          17 :         colorcomplement(v->nfa, v->cm, PLAIN, left, lp, rp);
    1753         [ -  + ]:          17 :         NOERR();
    1754                 :          17 :         dropstate(v->nfa, left);
    1755         [ +  - ]:          17 :         assert(right->nins == 0);
    1756                 :          17 :         freestate(v->nfa, right);
    1757         [ -  + ]:          17 : }
    1758                 :             : 
    1759                 :             : /*
    1760                 :             :  * brackpart - handle one item (or range) within a bracket expression
    1761                 :             :  */
    1762                 :             : static void
    1763                 :         128 : brackpart(struct vars *v,
    1764                 :             :                   struct state *lp,
    1765                 :             :                   struct state *rp,
    1766                 :             :                   bool *have_cclassc)
    1767                 :             : {
    1768                 :         128 :         chr                     startc;
    1769                 :         128 :         chr                     endc;
    1770                 :         128 :         struct cvec *cv;
    1771                 :         128 :         enum char_classes cls;
    1772                 :         128 :         const chr  *startp;
    1773                 :         128 :         const chr  *endp;
    1774                 :             : 
    1775                 :             :         /* parse something, get rid of special cases, take shortcuts */
    1776   [ -  -  +  -  :         128 :         switch (v->nexttype)
             -  +  -  - ]
    1777                 :             :         {
    1778                 :             :                 case RANGE:                             /* a-b-c or other botch */
    1779         [ #  # ]:           0 :                         ERR(REG_ERANGE);
    1780                 :           0 :                         return;
    1781                 :             :                         break;
    1782                 :             :                 case PLAIN:
    1783                 :         104 :                         startc = v->nextvalue;
    1784                 :         104 :                         NEXT();
    1785                 :             :                         /* shortcut for ordinary chr (not range) */
    1786         [ +  + ]:         104 :                         if (!SEE(RANGE))
    1787                 :             :                         {
    1788                 :          80 :                                 onechr(v, startc, lp, rp);
    1789                 :          80 :                                 return;
    1790                 :             :                         }
    1791         [ -  + ]:          24 :                         NOERR();
    1792                 :          24 :                         break;
    1793                 :             :                 case COLLEL:
    1794                 :           0 :                         startp = v->now;
    1795                 :           0 :                         endp = scanplain(v);
    1796   [ #  #  #  # ]:           0 :                         INSIST(startp < endp, REG_ECOLLATE);
    1797         [ #  # ]:           0 :                         NOERR();
    1798                 :           0 :                         startc = element(v, startp, endp);
    1799         [ #  # ]:           0 :                         NOERR();
    1800                 :           0 :                         break;
    1801                 :             :                 case ECLASS:
    1802                 :           0 :                         startp = v->now;
    1803                 :           0 :                         endp = scanplain(v);
    1804   [ #  #  #  # ]:           0 :                         INSIST(startp < endp, REG_ECOLLATE);
    1805         [ #  # ]:           0 :                         NOERR();
    1806                 :           0 :                         startc = element(v, startp, endp);
    1807         [ #  # ]:           0 :                         NOERR();
    1808                 :           0 :                         cv = eclass(v, startc, (v->cflags & REG_ICASE));
    1809         [ #  # ]:           0 :                         NOERR();
    1810                 :           0 :                         subcolorcvec(v, cv, lp, rp);
    1811                 :           0 :                         return;
    1812                 :             :                         break;
    1813                 :             :                 case CCLASS:
    1814                 :          24 :                         startp = v->now;
    1815                 :          24 :                         endp = scanplain(v);
    1816   [ +  -  #  # ]:          24 :                         INSIST(startp < endp, REG_ECTYPE);
    1817         [ -  + ]:          24 :                         NOERR();
    1818                 :          24 :                         cls = lookupcclass(v, startp, endp);
    1819         [ -  + ]:          24 :                         NOERR();
    1820                 :          24 :                         charclass(v, cls, lp, rp);
    1821                 :          24 :                         return;
    1822                 :             :                         break;
    1823                 :             :                 case CCLASSS:
    1824                 :           0 :                         charclass(v, (enum char_classes) v->nextvalue, lp, rp);
    1825                 :           0 :                         NEXT();
    1826                 :           0 :                         return;
    1827                 :             :                         break;
    1828                 :             :                 case CCLASSC:
    1829                 :             :                         /* we cannot call charclasscomplement() immediately */
    1830                 :           0 :                         have_cclassc[v->nextvalue] = true;
    1831                 :           0 :                         NEXT();
    1832                 :           0 :                         return;
    1833                 :             :                         break;
    1834                 :             :                 default:
    1835         [ #  # ]:           0 :                         ERR(REG_ASSERT);
    1836                 :           0 :                         return;
    1837                 :             :                         break;
    1838                 :             :         }
    1839                 :             : 
    1840         [ +  - ]:          24 :         if (SEE(RANGE))
    1841                 :             :         {
    1842                 :          24 :                 NEXT();
    1843      [ +  -  - ]:          24 :                 switch (v->nexttype)
    1844                 :             :                 {
    1845                 :             :                         case PLAIN:
    1846                 :             :                         case RANGE:
    1847                 :          24 :                                 endc = v->nextvalue;
    1848                 :          24 :                                 NEXT();
    1849         [ -  + ]:          24 :                                 NOERR();
    1850                 :          24 :                                 break;
    1851                 :             :                         case COLLEL:
    1852                 :           0 :                                 startp = v->now;
    1853                 :           0 :                                 endp = scanplain(v);
    1854   [ #  #  #  # ]:           0 :                                 INSIST(startp < endp, REG_ECOLLATE);
    1855         [ #  # ]:           0 :                                 NOERR();
    1856                 :           0 :                                 endc = element(v, startp, endp);
    1857         [ #  # ]:           0 :                                 NOERR();
    1858                 :           0 :                                 break;
    1859                 :             :                         default:
    1860         [ #  # ]:           0 :                                 ERR(REG_ERANGE);
    1861                 :           0 :                                 return;
    1862                 :             :                                 break;
    1863                 :             :                 }
    1864                 :          24 :         }
    1865                 :             :         else
    1866                 :           0 :                 endc = startc;
    1867                 :             : 
    1868                 :             :         /*
    1869                 :             :          * Ranges are unportable.  Actually, standard C does guarantee that digits
    1870                 :             :          * are contiguous, but making that an exception is just too complicated.
    1871                 :             :          */
    1872         [ -  + ]:          24 :         if (startc != endc)
    1873                 :          24 :                 NOTE(REG_UUNPORT);
    1874                 :          24 :         cv = range(v, startc, endc, (v->cflags & REG_ICASE));
    1875         [ -  + ]:          24 :         NOERR();
    1876                 :          24 :         subcolorcvec(v, cv, lp, rp);
    1877         [ -  + ]:         128 : }
    1878                 :             : 
    1879                 :             : /*
    1880                 :             :  * scanplain - scan PLAIN contents of [. etc.
    1881                 :             :  *
    1882                 :             :  * Certain bits of trickery in regc_lex.c know that this code does not try
    1883                 :             :  * to look past the final bracket of the [. etc.
    1884                 :             :  */
    1885                 :             : static const chr *                              /* just after end of sequence */
    1886                 :          24 : scanplain(struct vars *v)
    1887                 :             : {
    1888                 :          24 :         const chr  *endp;
    1889                 :             : 
    1890   [ +  -  +  -  :          24 :         assert(SEE(COLLEL) || SEE(ECLASS) || SEE(CCLASS));
                   +  - ]
    1891                 :          24 :         NEXT();
    1892                 :             : 
    1893                 :          24 :         endp = v->now;
    1894         [ +  + ]:         144 :         while (SEE(PLAIN))
    1895                 :             :         {
    1896                 :         120 :                 endp = v->now;
    1897                 :         120 :                 NEXT();
    1898                 :             :         }
    1899                 :             : 
    1900   [ -  +  #  # ]:          24 :         assert(SEE(END) || ISERR());
    1901                 :          24 :         NEXT();
    1902                 :             : 
    1903                 :          48 :         return endp;
    1904                 :          24 : }
    1905                 :             : 
    1906                 :             : /*
    1907                 :             :  * onechr - fill in arcs for a plain character, and possible case complements
    1908                 :             :  * This is mostly a shortcut for efficient handling of the common case.
    1909                 :             :  */
    1910                 :             : static void
    1911                 :       10472 : onechr(struct vars *v,
    1912                 :             :            chr c,
    1913                 :             :            struct state *lp,
    1914                 :             :            struct state *rp)
    1915                 :             : {
    1916         [ +  + ]:       10472 :         if (!(v->cflags & REG_ICASE))
    1917                 :             :         {
    1918                 :       10333 :                 color           lastsubcolor = COLORLESS;
    1919                 :             : 
    1920                 :       10333 :                 subcoloronechr(v, c, lp, rp, &lastsubcolor);
    1921                 :             :                 return;
    1922                 :       10333 :         }
    1923                 :             : 
    1924                 :             :         /* rats, need general case anyway... */
    1925                 :         139 :         subcolorcvec(v, allcases(v, c), lp, rp);
    1926                 :       10472 : }
    1927                 :             : 
    1928                 :             : /*
    1929                 :             :  * optimizebracket - see if bracket expression can be converted to RAINBOW
    1930                 :             :  *
    1931                 :             :  * Cases such as "[\s\S]" can produce a set of arcs of all colors, which we
    1932                 :             :  * can replace by a single RAINBOW arc for efficiency.  (This might seem
    1933                 :             :  * like a silly way to write ".", but it's seemingly a common locution in
    1934                 :             :  * some other flavors of regex, so take the trouble to support it well.)
    1935                 :             :  */
    1936                 :             : static void
    1937                 :           0 : optimizebracket(struct vars *v,
    1938                 :             :                                 struct state *lp,
    1939                 :             :                                 struct state *rp)
    1940                 :             : {
    1941                 :           0 :         struct colordesc *cd;
    1942                 :           0 :         struct colordesc *end = CDEND(v->cm);
    1943                 :           0 :         struct arc *a;
    1944                 :           0 :         bool            israinbow;
    1945                 :             : 
    1946                 :             :         /*
    1947                 :             :          * Scan lp's out-arcs and transiently mark the mentioned colors.  We
    1948                 :             :          * expect that all of lp's out-arcs are plain, non-RAINBOW arcs to rp.
    1949                 :             :          * (Note: there shouldn't be any pseudocolors yet, but check anyway.)
    1950                 :             :          */
    1951         [ #  # ]:           0 :         for (a = lp->outs; a != NULL; a = a->outchain)
    1952                 :             :         {
    1953         [ #  # ]:           0 :                 assert(a->type == PLAIN);
    1954         [ #  # ]:           0 :                 assert(a->co >= 0);               /* i.e. not RAINBOW */
    1955         [ #  # ]:           0 :                 assert(a->to == rp);
    1956                 :           0 :                 cd = &v->cm->cd[a->co];
    1957         [ #  # ]:           0 :                 assert(!UNUSEDCOLOR(cd) && !(cd->flags & PSEUDO));
    1958                 :           0 :                 cd->flags |= COLMARK;
    1959                 :           0 :         }
    1960                 :             : 
    1961                 :             :         /* Scan colors, clear transient marks, check for unmarked live colors */
    1962                 :           0 :         israinbow = true;
    1963         [ #  # ]:           0 :         for (cd = v->cm->cd; cd < end; cd++)
    1964                 :             :         {
    1965         [ #  # ]:           0 :                 if (cd->flags & COLMARK)
    1966                 :           0 :                         cd->flags &= ~COLMARK;
    1967   [ #  #  #  # ]:           0 :                 else if (!UNUSEDCOLOR(cd) && !(cd->flags & PSEUDO))
    1968                 :           0 :                         israinbow = false;
    1969                 :           0 :         }
    1970                 :             : 
    1971                 :             :         /* Can't do anything if not all colors have arcs */
    1972         [ #  # ]:           0 :         if (!israinbow)
    1973                 :           0 :                 return;
    1974                 :             : 
    1975                 :             :         /* OK, drop existing arcs and replace with a rainbow */
    1976         [ #  # ]:           0 :         while ((a = lp->outs) != NULL)
    1977                 :           0 :                 freearc(v->nfa, a);
    1978                 :           0 :         newarc(v->nfa, PLAIN, RAINBOW, lp, rp);
    1979         [ #  # ]:           0 : }
    1980                 :             : 
    1981                 :             : /*
    1982                 :             :  * wordchrs - set up word-chr list for word-boundary stuff, if needed
    1983                 :             :  *
    1984                 :             :  * The list is kept as a bunch of circular arcs on an otherwise-unused state.
    1985                 :             :  *
    1986                 :             :  * Note that this must not be called while we have any open subcolors,
    1987                 :             :  * else construction of the list would confuse color bookkeeping.
    1988                 :             :  * Hence, we can't currently apply a similar optimization in
    1989                 :             :  * charclass[complement](), as those need to be usable within bracket
    1990                 :             :  * expressions.
    1991                 :             :  */
    1992                 :             : static void
    1993                 :           9 : wordchrs(struct vars *v)
    1994                 :             : {
    1995                 :           9 :         struct state *cstate;
    1996                 :           9 :         struct cvec *cv;
    1997                 :             : 
    1998         [ +  + ]:           9 :         if (v->wordchrs != NULL)
    1999                 :           3 :                 return;                                 /* done already */
    2000                 :             : 
    2001                 :             :         /* make dummy state to hang the cache arcs on */
    2002                 :           6 :         cstate = newstate(v->nfa);
    2003         [ -  + ]:           6 :         NOERR();
    2004                 :             : 
    2005                 :             :         /* obtain possibly-cached cvec for \w characters */
    2006                 :           6 :         NOTE(REG_ULOCALE);
    2007                 :           6 :         cv = cclasscvec(v, CC_WORD, (v->cflags & REG_ICASE));
    2008         [ -  + ]:           6 :         NOERR();
    2009                 :             : 
    2010                 :             :         /* build the arcs; this may cause color splitting */
    2011                 :           6 :         subcolorcvec(v, cv, cstate, cstate);
    2012         [ -  + ]:           6 :         NOERR();
    2013                 :             : 
    2014                 :             :         /* close new open subcolors to ensure the cache entry is self-contained */
    2015                 :           6 :         okcolors(v->nfa, v->cm);
    2016         [ -  + ]:           6 :         NOERR();
    2017                 :             : 
    2018                 :             :         /* success! save the cache pointer */
    2019                 :           6 :         v->wordchrs = cstate;
    2020         [ -  + ]:           9 : }
    2021                 :             : 
    2022                 :             : /*
    2023                 :             :  * processlacon - generate the NFA representation of a LACON
    2024                 :             :  *
    2025                 :             :  * In the general case this is just newlacon() + newarc(), but some cases
    2026                 :             :  * can be optimized.
    2027                 :             :  */
    2028                 :             : static void
    2029                 :          25 : processlacon(struct vars *v,
    2030                 :             :                          struct state *begin,   /* start of parsed LACON sub-re */
    2031                 :             :                          struct state *end, /* end of parsed LACON sub-re */
    2032                 :             :                          int latype,
    2033                 :             :                          struct state *lp,      /* left state to hang it on */
    2034                 :             :                          struct state *rp)      /* right state to hang it on */
    2035                 :             : {
    2036                 :          25 :         struct state *s1;
    2037                 :          25 :         int                     n;
    2038                 :             : 
    2039                 :             :         /*
    2040                 :             :          * Check for lookaround RE consisting of a single plain color arc (or set
    2041                 :             :          * of arcs); this would typically be a simple chr or a bracket expression.
    2042                 :             :          */
    2043                 :          25 :         s1 = single_color_transition(begin, end);
    2044   [ -  +  +  +  :          25 :         switch (latype)
                      + ]
    2045                 :             :         {
    2046                 :             :                 case LATYPE_AHEAD_POS:
    2047                 :             :                         /* If lookahead RE is just colorset C, convert to AHEAD(C) */
    2048         [ +  + ]:           6 :                         if (s1 != NULL)
    2049                 :             :                         {
    2050                 :           5 :                                 cloneouts(v->nfa, s1, lp, rp, AHEAD);
    2051                 :           5 :                                 return;
    2052                 :             :                         }
    2053                 :           1 :                         break;
    2054                 :             :                 case LATYPE_AHEAD_NEG:
    2055                 :             :                         /* If lookahead RE is just colorset C, convert to AHEAD(^C)|$ */
    2056         [ +  + ]:           6 :                         if (s1 != NULL)
    2057                 :             :                         {
    2058                 :           2 :                                 colorcomplement(v->nfa, v->cm, AHEAD, s1, lp, rp);
    2059                 :           2 :                                 newarc(v->nfa, '$', 1, lp, rp);
    2060                 :           2 :                                 newarc(v->nfa, '$', 0, lp, rp);
    2061                 :           2 :                                 return;
    2062                 :             :                         }
    2063                 :           4 :                         break;
    2064                 :             :                 case LATYPE_BEHIND_POS:
    2065                 :             :                         /* If lookbehind RE is just colorset C, convert to BEHIND(C) */
    2066         [ +  + ]:           9 :                         if (s1 != NULL)
    2067                 :             :                         {
    2068                 :           7 :                                 cloneouts(v->nfa, s1, lp, rp, BEHIND);
    2069                 :           7 :                                 return;
    2070                 :             :                         }
    2071                 :           2 :                         break;
    2072                 :             :                 case LATYPE_BEHIND_NEG:
    2073                 :             :                         /* If lookbehind RE is just colorset C, convert to BEHIND(^C)|^ */
    2074         [ +  - ]:           4 :                         if (s1 != NULL)
    2075                 :             :                         {
    2076                 :           4 :                                 colorcomplement(v->nfa, v->cm, BEHIND, s1, lp, rp);
    2077                 :           4 :                                 newarc(v->nfa, '^', 1, lp, rp);
    2078                 :           4 :                                 newarc(v->nfa, '^', 0, lp, rp);
    2079                 :           4 :                                 return;
    2080                 :             :                         }
    2081                 :           0 :                         break;
    2082                 :             :                 default:
    2083                 :           0 :                         assert(NOTREACHED);
    2084                 :           0 :         }
    2085                 :             : 
    2086                 :             :         /* General case: we need a LACON subre and arc */
    2087                 :           7 :         n = newlacon(v, begin, end, latype);
    2088                 :           7 :         newarc(v->nfa, LACON, n, lp, rp);
    2089         [ -  + ]:          25 : }
    2090                 :             : 
    2091                 :             : /*
    2092                 :             :  * subre - allocate a subre
    2093                 :             :  */
    2094                 :             : static struct subre *
    2095                 :        5121 : subre(struct vars *v,
    2096                 :             :           int op,
    2097                 :             :           int flags,
    2098                 :             :           struct state *begin,
    2099                 :             :           struct state *end)
    2100                 :             : {
    2101                 :        5121 :         struct subre *ret = v->treefree;
    2102                 :             : 
    2103                 :             :         /*
    2104                 :             :          * Checking for stack overflow here is sufficient to protect parse() and
    2105                 :             :          * its recursive subroutines.
    2106                 :             :          */
    2107         [ -  + ]:        5121 :         if (STACK_TOO_DEEP(v->re))
    2108                 :             :         {
    2109         [ #  # ]:           0 :                 ERR(REG_ETOOBIG);
    2110                 :           0 :                 return NULL;
    2111                 :             :         }
    2112                 :             : 
    2113         [ +  + ]:        5121 :         if (ret != NULL)
    2114                 :         753 :                 v->treefree = ret->child;
    2115                 :             :         else
    2116                 :             :         {
    2117                 :        4368 :                 ret = (struct subre *) MALLOC(sizeof(struct subre));
    2118         [ +  - ]:        4368 :                 if (ret == NULL)
    2119                 :             :                 {
    2120         [ #  # ]:           0 :                         ERR(REG_ESPACE);
    2121                 :           0 :                         return NULL;
    2122                 :             :                 }
    2123                 :        4368 :                 ret->chain = v->treechain;
    2124                 :        4368 :                 v->treechain = ret;
    2125                 :             :         }
    2126                 :             : 
    2127         [ +  - ]:        5121 :         assert(strchr("=b|.*(", op) != NULL);
    2128                 :             : 
    2129                 :        5121 :         ret->op = op;
    2130                 :        5121 :         ret->flags = flags;
    2131                 :        5121 :         ret->latype = (char) -1;
    2132                 :        5121 :         ret->id = 0;                         /* will be assigned later */
    2133                 :        5121 :         ret->capno = 0;
    2134                 :        5121 :         ret->backno = 0;
    2135                 :        5121 :         ret->min = ret->max = 1;
    2136                 :        5121 :         ret->child = NULL;
    2137                 :        5121 :         ret->sibling = NULL;
    2138                 :        5121 :         ret->begin = begin;
    2139                 :        5121 :         ret->end = end;
    2140                 :        5121 :         ZAPCNFA(ret->cnfa);
    2141                 :             : 
    2142                 :        5121 :         return ret;
    2143                 :        5121 : }
    2144                 :             : 
    2145                 :             : /*
    2146                 :             :  * freesubre - free a subRE subtree
    2147                 :             :  *
    2148                 :             :  * This frees child node(s) of the given subRE too,
    2149                 :             :  * but not its siblings.
    2150                 :             :  */
    2151                 :             : static void
    2152                 :        2383 : freesubre(struct vars *v,               /* might be NULL */
    2153                 :             :                   struct subre *sr)
    2154                 :             : {
    2155         [ +  + ]:        2383 :         if (sr == NULL)
    2156                 :           2 :                 return;
    2157                 :             : 
    2158         [ +  + ]:        2381 :         if (sr->child != NULL)
    2159                 :          57 :                 freesubreandsiblings(v, sr->child);
    2160                 :             : 
    2161                 :        2381 :         freesrnode(v, sr);
    2162                 :        2383 : }
    2163                 :             : 
    2164                 :             : /*
    2165                 :             :  * freesubreandsiblings - free a subRE subtree
    2166                 :             :  *
    2167                 :             :  * This frees child node(s) of the given subRE too,
    2168                 :             :  * as well as any following siblings.
    2169                 :             :  */
    2170                 :             : static void
    2171                 :        1121 : freesubreandsiblings(struct vars *v,    /* might be NULL */
    2172                 :             :                                          struct subre *sr)
    2173                 :             : {
    2174         [ +  + ]:        3309 :         while (sr != NULL)
    2175                 :             :         {
    2176                 :        2188 :                 struct subre *next = sr->sibling;
    2177                 :             : 
    2178                 :        2188 :                 freesubre(v, sr);
    2179                 :        2188 :                 sr = next;
    2180                 :        2188 :         }
    2181                 :        1121 : }
    2182                 :             : 
    2183                 :             : /*
    2184                 :             :  * freesrnode - free one node in a subRE subtree
    2185                 :             :  */
    2186                 :             : static void
    2187                 :        3993 : freesrnode(struct vars *v,              /* might be NULL */
    2188                 :             :                    struct subre *sr)
    2189                 :             : {
    2190         [ +  - ]:        3993 :         if (sr == NULL)
    2191                 :           0 :                 return;
    2192                 :             : 
    2193         [ +  + ]:        3993 :         if (!NULLCNFA(sr->cnfa))
    2194                 :          20 :                 freecnfa(&sr->cnfa);
    2195                 :        3993 :         sr->flags = 0;                               /* in particular, not INUSE */
    2196                 :        3993 :         sr->child = sr->sibling = NULL;
    2197                 :        3993 :         sr->begin = sr->end = NULL;
    2198                 :             : 
    2199   [ +  +  +  + ]:        3993 :         if (v != NULL && v->treechain != NULL)
    2200                 :             :         {
    2201                 :             :                 /* we're still parsing, maybe we can reuse the subre */
    2202                 :        3972 :                 sr->child = v->treefree;
    2203                 :        3972 :                 v->treefree = sr;
    2204                 :        3972 :         }
    2205                 :             :         else
    2206                 :          21 :                 FREE(sr);
    2207                 :        3993 : }
    2208                 :             : 
    2209                 :             : /*
    2210                 :             :  * removecaptures - remove unnecessary capture subREs
    2211                 :             :  *
    2212                 :             :  * If the caller said that it doesn't care about subexpression match data,
    2213                 :             :  * we may delete the "capture" markers on subREs that are not referenced
    2214                 :             :  * by any backrefs, and then simplify anything that's become non-messy.
    2215                 :             :  * Call this only if REG_NOSUB flag is set.
    2216                 :             :  */
    2217                 :             : static void
    2218                 :        2863 : removecaptures(struct vars *v,
    2219                 :             :                            struct subre *t)
    2220                 :             : {
    2221                 :        2863 :         struct subre *t2;
    2222                 :             : 
    2223         [ +  - ]:        2863 :         assert(t != NULL);
    2224                 :             : 
    2225                 :             :         /*
    2226                 :             :          * If this isn't itself a backref target, clear capno and tentatively
    2227                 :             :          * clear CAP flag.
    2228                 :             :          */
    2229         [ +  + ]:        2863 :         if (!(t->flags & BRUSE))
    2230                 :             :         {
    2231                 :        2852 :                 t->capno = 0;
    2232                 :        2852 :                 t->flags &= ~CAP;
    2233                 :        2852 :         }
    2234                 :             : 
    2235                 :             :         /* Now recurse to children */
    2236         [ +  + ]:        5013 :         for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
    2237                 :             :         {
    2238                 :        2150 :                 removecaptures(v, t2);
    2239                 :             :                 /* Propagate child CAP flag back up, if it's still set */
    2240         [ +  + ]:        2150 :                 if (t2->flags & CAP)
    2241                 :          23 :                         t->flags |= CAP;
    2242                 :        2150 :         }
    2243                 :             : 
    2244                 :             :         /*
    2245                 :             :          * If t now contains neither captures nor backrefs, there's no longer any
    2246                 :             :          * need to care where its sub-match boundaries are, so we can reduce it to
    2247                 :             :          * a simple DFA node.  (Note in particular that MIXED child greediness is
    2248                 :             :          * not a hindrance here, so we don't use the MESSY() macro.)
    2249                 :             :          */
    2250         [ +  + ]:        2863 :         if ((t->flags & (CAP | BACKR)) == 0)
    2251                 :             :         {
    2252         [ +  + ]:        2802 :                 if (t->child)
    2253                 :        1046 :                         freesubreandsiblings(v, t->child);
    2254                 :        2802 :                 t->child = NULL;
    2255                 :        2802 :                 t->op = '=';
    2256                 :        2802 :                 t->flags &= ~MIXED;
    2257                 :        2802 :         }
    2258                 :        2863 : }
    2259                 :             : 
    2260                 :             : /*
    2261                 :             :  * numst - number tree nodes (assigning "id" indexes)
    2262                 :             :  */
    2263                 :             : static int                                              /* next number */
    2264                 :        1112 : numst(struct subre *t,
    2265                 :             :           int start)                            /* starting point for subtree numbers */
    2266                 :             : {
    2267                 :        1112 :         int                     i;
    2268                 :        1112 :         struct subre *t2;
    2269                 :             : 
    2270         [ +  - ]:        1112 :         assert(t != NULL);
    2271                 :             : 
    2272                 :        1112 :         i = start;
    2273                 :        1112 :         t->id = i++;
    2274         [ +  + ]:        1418 :         for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
    2275                 :         306 :                 i = numst(t2, i);
    2276                 :        2224 :         return i;
    2277                 :        1112 : }
    2278                 :             : 
    2279                 :             : /*
    2280                 :             :  * markst - mark tree nodes as INUSE
    2281                 :             :  *
    2282                 :             :  * Note: this is a great deal more subtle than it looks.  During initial
    2283                 :             :  * parsing of a regex, all subres are linked into the treechain list;
    2284                 :             :  * discarded ones are also linked into the treefree list for possible reuse.
    2285                 :             :  * After we are done creating all subres required for a regex, we run markst()
    2286                 :             :  * then cleanst(), which results in discarding all subres not reachable from
    2287                 :             :  * v->tree.  We then clear v->treechain, indicating that subres must be found
    2288                 :             :  * by descending from v->tree.  This changes the behavior of freesubre(): it
    2289                 :             :  * will henceforth FREE() unwanted subres rather than sticking them into the
    2290                 :             :  * treefree list.  (Doing that any earlier would result in dangling links in
    2291                 :             :  * the treechain list.)  This all means that freev() will clean up correctly
    2292                 :             :  * if invoked before or after markst()+cleanst(); but it would not work if
    2293                 :             :  * called partway through this state conversion, so we mustn't error out
    2294                 :             :  * in or between these two functions.
    2295                 :             :  */
    2296                 :             : static void
    2297                 :        1112 : markst(struct subre *t)
    2298                 :             : {
    2299                 :        1112 :         struct subre *t2;
    2300                 :             : 
    2301         [ +  - ]:        1112 :         assert(t != NULL);
    2302                 :             : 
    2303                 :        1112 :         t->flags |= INUSE;
    2304         [ +  + ]:        1418 :         for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
    2305                 :         306 :                 markst(t2);
    2306                 :        1112 : }
    2307                 :             : 
    2308                 :             : /*
    2309                 :             :  * cleanst - free any tree nodes not marked INUSE
    2310                 :             :  */
    2311                 :             : static void
    2312                 :         811 : cleanst(struct vars *v)
    2313                 :             : {
    2314                 :         811 :         struct subre *t;
    2315                 :         811 :         struct subre *next;
    2316                 :             : 
    2317         [ +  + ]:        5179 :         for (t = v->treechain; t != NULL; t = next)
    2318                 :             :         {
    2319                 :        4368 :                 next = t->chain;
    2320         [ +  + ]:        4368 :                 if (!(t->flags & INUSE))
    2321                 :        3256 :                         FREE(t);
    2322                 :        4368 :         }
    2323                 :         811 :         v->treechain = NULL;
    2324                 :         811 :         v->treefree = NULL;                  /* just on general principles */
    2325                 :         811 : }
    2326                 :             : 
    2327                 :             : /*
    2328                 :             :  * nfatree - turn a subRE subtree into a tree of compacted NFAs
    2329                 :             :  */
    2330                 :             : static long                                             /* optimize results from top node */
    2331                 :        1112 : nfatree(struct vars *v,
    2332                 :             :                 struct subre *t,
    2333                 :             :                 FILE *f)                                /* for debug output */
    2334                 :             : {
    2335                 :        1112 :         struct subre *t2;
    2336                 :             : 
    2337         [ +  - ]:        1112 :         assert(t != NULL && t->begin != NULL);
    2338                 :             : 
    2339         [ +  + ]:        1418 :         for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
    2340                 :         306 :                 (DISCARD) nfatree(v, t2, f);
    2341                 :             : 
    2342                 :        2224 :         return nfanode(v, t, 0, f);
    2343                 :        1112 : }
    2344                 :             : 
    2345                 :             : /*
    2346                 :             :  * nfanode - do one NFA for nfatree or lacons
    2347                 :             :  *
    2348                 :             :  * If converttosearch is true, apply makesearch() to the NFA.
    2349                 :             :  */
    2350                 :             : static long                                             /* optimize results */
    2351                 :        1119 : nfanode(struct vars *v,
    2352                 :             :                 struct subre *t,
    2353                 :             :                 int converttosearch,
    2354                 :             :                 FILE *f)                                /* for debug output */
    2355                 :             : {
    2356                 :        1119 :         struct nfa *nfa;
    2357                 :        1119 :         long            ret = 0;
    2358                 :             : 
    2359         [ +  - ]:        1119 :         assert(t->begin != NULL);
    2360                 :             : 
    2361                 :             : #ifdef REG_DEBUG
    2362                 :             :         if (f != NULL)
    2363                 :             :         {
    2364                 :             :                 char            idbuf[50];
    2365                 :             : 
    2366                 :             :                 fprintf(f, "\n\n\n========= TREE NODE %s ==========\n",
    2367                 :             :                                 stid(t, idbuf, sizeof(idbuf)));
    2368                 :             :         }
    2369                 :             : #endif
    2370                 :        1119 :         nfa = newnfa(v, v->cm, v->nfa);
    2371         [ -  + ]:        1119 :         NOERRZ();
    2372                 :        1119 :         dupnfa(nfa, t->begin, t->end, nfa->init, nfa->final);
    2373                 :        1119 :         nfa->flags = v->nfa->flags;
    2374         [ -  + ]:        1119 :         if (!ISERR())
    2375                 :        1119 :                 specialcolors(nfa);
    2376         [ -  + ]:        1119 :         if (!ISERR())
    2377                 :        1119 :                 ret = optimize(nfa, f);
    2378   [ +  +  -  + ]:        1119 :         if (converttosearch && !ISERR())
    2379                 :           2 :                 makesearch(v, nfa);
    2380         [ +  + ]:        1119 :         if (!ISERR())
    2381                 :        1118 :                 compact(nfa, &t->cnfa);
    2382                 :             : 
    2383                 :        1119 :         freenfa(nfa);
    2384                 :        1119 :         return ret;
    2385                 :        1119 : }
    2386                 :             : 
    2387                 :             : /*
    2388                 :             :  * newlacon - allocate a lookaround-constraint subRE
    2389                 :             :  */
    2390                 :             : static int                                              /* lacon number */
    2391                 :           7 : newlacon(struct vars *v,
    2392                 :             :                  struct state *begin,
    2393                 :             :                  struct state *end,
    2394                 :             :                  int latype)
    2395                 :             : {
    2396                 :           7 :         int                     n;
    2397                 :           7 :         struct subre *newlacons;
    2398                 :           7 :         struct subre *sub;
    2399                 :             : 
    2400         [ +  + ]:           7 :         if (v->nlacons == 0)
    2401                 :             :         {
    2402                 :           5 :                 n = 1;                                  /* skip 0th */
    2403                 :           5 :                 newlacons = (struct subre *) MALLOC(2 * sizeof(struct subre));
    2404                 :           5 :         }
    2405                 :             :         else
    2406                 :             :         {
    2407                 :           2 :                 n = v->nlacons;
    2408                 :           2 :                 newlacons = (struct subre *) REALLOC(v->lacons,
    2409                 :             :                                                                                          (n + 1) * sizeof(struct subre));
    2410                 :             :         }
    2411         [ +  - ]:           7 :         if (newlacons == NULL)
    2412                 :             :         {
    2413         [ #  # ]:           0 :                 ERR(REG_ESPACE);
    2414                 :           0 :                 return 0;
    2415                 :             :         }
    2416                 :           7 :         v->lacons = newlacons;
    2417                 :           7 :         v->nlacons = n + 1;
    2418                 :           7 :         sub = &v->lacons[n];
    2419                 :           7 :         sub->begin = begin;
    2420                 :           7 :         sub->end = end;
    2421                 :           7 :         sub->latype = latype;
    2422                 :           7 :         ZAPCNFA(sub->cnfa);
    2423                 :           7 :         return n;
    2424                 :           7 : }
    2425                 :             : 
    2426                 :             : /*
    2427                 :             :  * freelacons - free lookaround-constraint subRE vector
    2428                 :             :  */
    2429                 :             : static void
    2430                 :           0 : freelacons(struct subre *subs,
    2431                 :             :                    int n)
    2432                 :             : {
    2433                 :           0 :         struct subre *sub;
    2434                 :           0 :         int                     i;
    2435                 :             : 
    2436         [ #  # ]:           0 :         assert(n > 0);
    2437         [ #  # ]:           0 :         for (sub = subs + 1, i = n - 1; i > 0; sub++, i--)   /* no 0th */
    2438         [ #  # ]:           0 :                 if (!NULLCNFA(sub->cnfa))
    2439                 :           0 :                         freecnfa(&sub->cnfa);
    2440                 :           0 :         FREE(subs);
    2441                 :           0 : }
    2442                 :             : 
    2443                 :             : /*
    2444                 :             :  * rfree - free a whole RE (insides of regfree)
    2445                 :             :  */
    2446                 :             : static void
    2447                 :          27 : rfree(regex_t *re)
    2448                 :             : {
    2449                 :          27 :         struct guts *g;
    2450                 :             : 
    2451   [ +  -  -  + ]:          27 :         if (re == NULL || re->re_magic != REMAGIC)
    2452                 :           0 :                 return;
    2453                 :             : 
    2454                 :          27 :         re->re_magic = 0;                    /* invalidate RE */
    2455                 :          27 :         g = (struct guts *) re->re_guts;
    2456                 :          27 :         re->re_guts = NULL;
    2457                 :          27 :         re->re_fns = NULL;
    2458         [ -  + ]:          27 :         if (g != NULL)
    2459                 :             :         {
    2460                 :          27 :                 g->magic = 0;
    2461                 :          27 :                 freecm(&g->cmap);
    2462         [ +  + ]:          27 :                 if (g->tree != NULL)
    2463                 :          20 :                         freesubre((struct vars *) NULL, g->tree);
    2464         [ +  - ]:          27 :                 if (g->lacons != NULL)
    2465                 :           0 :                         freelacons(g->lacons, g->nlacons);
    2466         [ +  + ]:          27 :                 if (!NULLCNFA(g->search))
    2467                 :          20 :                         freecnfa(&g->search);
    2468                 :          27 :                 FREE(g);
    2469                 :          27 :         }
    2470         [ -  + ]:          27 : }
    2471                 :             : 
    2472                 :             : /*
    2473                 :             :  * rstacktoodeep - check for stack getting dangerously deep
    2474                 :             :  *
    2475                 :             :  * Return nonzero to fail the operation with error code REG_ETOOBIG,
    2476                 :             :  * zero to keep going
    2477                 :             :  *
    2478                 :             :  * The current implementation is Postgres-specific.  If we ever get around
    2479                 :             :  * to splitting the regex code out as a standalone library, there will need
    2480                 :             :  * to be some API to let applications define a callback function for this.
    2481                 :             :  */
    2482                 :             : static int
    2483                 :     2800174 : rstacktoodeep(void)
    2484                 :             : {
    2485                 :     2800174 :         return stack_is_too_deep();
    2486                 :             : }
    2487                 :             : 
    2488                 :             : #ifdef REG_DEBUG
    2489                 :             : 
    2490                 :             : /*
    2491                 :             :  * dump - dump an RE in human-readable form
    2492                 :             :  */
    2493                 :             : static void
    2494                 :             : dump(regex_t *re,
    2495                 :             :          FILE *f)
    2496                 :             : {
    2497                 :             :         struct guts *g;
    2498                 :             :         int                     i;
    2499                 :             : 
    2500                 :             :         if (re->re_magic != REMAGIC)
    2501                 :             :                 fprintf(f, "bad magic number (0x%x not 0x%x)\n", re->re_magic,
    2502                 :             :                                 REMAGIC);
    2503                 :             :         if (re->re_guts == NULL)
    2504                 :             :         {
    2505                 :             :                 fprintf(f, "NULL guts!!!\n");
    2506                 :             :                 return;
    2507                 :             :         }
    2508                 :             :         g = (struct guts *) re->re_guts;
    2509                 :             :         if (g->magic != GUTSMAGIC)
    2510                 :             :                 fprintf(f, "bad guts magic number (0x%x not 0x%x)\n", g->magic,
    2511                 :             :                                 GUTSMAGIC);
    2512                 :             : 
    2513                 :             :         fprintf(f, "\n\n\n========= DUMP ==========\n");
    2514                 :             :         fprintf(f, "nsub %d, info 0%lo, csize %d, ntree %d\n",
    2515                 :             :                         (int) re->re_nsub, re->re_info, re->re_csize, g->ntree);
    2516                 :             : 
    2517                 :             :         dumpcolors(&g->cmap, f);
    2518                 :             :         if (!NULLCNFA(g->search))
    2519                 :             :         {
    2520                 :             :                 fprintf(f, "\nsearch:\n");
    2521                 :             :                 dumpcnfa(&g->search, f);
    2522                 :             :         }
    2523                 :             :         for (i = 1; i < g->nlacons; i++)
    2524                 :             :         {
    2525                 :             :                 struct subre *lasub = &g->lacons[i];
    2526                 :             :                 const char *latype;
    2527                 :             : 
    2528                 :             :                 switch (lasub->latype)
    2529                 :             :                 {
    2530                 :             :                         case LATYPE_AHEAD_POS:
    2531                 :             :                                 latype = "positive lookahead";
    2532                 :             :                                 break;
    2533                 :             :                         case LATYPE_AHEAD_NEG:
    2534                 :             :                                 latype = "negative lookahead";
    2535                 :             :                                 break;
    2536                 :             :                         case LATYPE_BEHIND_POS:
    2537                 :             :                                 latype = "positive lookbehind";
    2538                 :             :                                 break;
    2539                 :             :                         case LATYPE_BEHIND_NEG:
    2540                 :             :                                 latype = "negative lookbehind";
    2541                 :             :                                 break;
    2542                 :             :                         default:
    2543                 :             :                                 latype = "???";
    2544                 :             :                                 break;
    2545                 :             :                 }
    2546                 :             :                 fprintf(f, "\nla%d (%s):\n", i, latype);
    2547                 :             :                 dumpcnfa(&lasub->cnfa, f);
    2548                 :             :         }
    2549                 :             :         fprintf(f, "\n");
    2550                 :             :         dumpst(g->tree, f, 0);
    2551                 :             : }
    2552                 :             : 
    2553                 :             : /*
    2554                 :             :  * dumpst - dump a subRE tree
    2555                 :             :  */
    2556                 :             : static void
    2557                 :             : dumpst(struct subre *t,
    2558                 :             :            FILE *f,
    2559                 :             :            int nfapresent)                      /* is the original NFA still around? */
    2560                 :             : {
    2561                 :             :         if (t == NULL)
    2562                 :             :                 fprintf(f, "null tree\n");
    2563                 :             :         else
    2564                 :             :                 stdump(t, f, nfapresent);
    2565                 :             :         fflush(f);
    2566                 :             : }
    2567                 :             : 
    2568                 :             : /*
    2569                 :             :  * stdump - recursive guts of dumpst
    2570                 :             :  */
    2571                 :             : static void
    2572                 :             : stdump(struct subre *t,
    2573                 :             :            FILE *f,
    2574                 :             :            int nfapresent)                      /* is the original NFA still around? */
    2575                 :             : {
    2576                 :             :         char            idbuf[50];
    2577                 :             :         struct subre *t2;
    2578                 :             : 
    2579                 :             :         fprintf(f, "%s. `%c'", stid(t, idbuf, sizeof(idbuf)), t->op);
    2580                 :             :         if (t->flags & LONGER)
    2581                 :             :                 fprintf(f, " longest");
    2582                 :             :         if (t->flags & SHORTER)
    2583                 :             :                 fprintf(f, " shortest");
    2584                 :             :         if (t->flags & MIXED)
    2585                 :             :                 fprintf(f, " hasmixed");
    2586                 :             :         if (t->flags & CAP)
    2587                 :             :                 fprintf(f, " hascapture");
    2588                 :             :         if (t->flags & BACKR)
    2589                 :             :                 fprintf(f, " hasbackref");
    2590                 :             :         if (t->flags & BRUSE)
    2591                 :             :                 fprintf(f, " isreferenced");
    2592                 :             :         if (!(t->flags & INUSE))
    2593                 :             :                 fprintf(f, " UNUSED");
    2594                 :             :         if (t->latype != (char) -1)
    2595                 :             :                 fprintf(f, " latype(%d)", t->latype);
    2596                 :             :         if (t->capno != 0)
    2597                 :             :                 fprintf(f, " capture(%d)", t->capno);
    2598                 :             :         if (t->backno != 0)
    2599                 :             :                 fprintf(f, " backref(%d)", t->backno);
    2600                 :             :         if (t->min != 1 || t->max != 1)
    2601                 :             :         {
    2602                 :             :                 fprintf(f, " {%d,", t->min);
    2603                 :             :                 if (t->max != DUPINF)
    2604                 :             :                         fprintf(f, "%d", t->max);
    2605                 :             :                 fprintf(f, "}");
    2606                 :             :         }
    2607                 :             :         if (nfapresent)
    2608                 :             :                 fprintf(f, " %ld-%ld", (long) t->begin->no, (long) t->end->no);
    2609                 :             :         if (t->child != NULL)
    2610                 :             :                 fprintf(f, " C:%s", stid(t->child, idbuf, sizeof(idbuf)));
    2611                 :             :         /* printing second child isn't necessary, but it is often helpful */
    2612                 :             :         if (t->child != NULL && t->child->sibling != NULL)
    2613                 :             :                 fprintf(f, " C2:%s", stid(t->child->sibling, idbuf, sizeof(idbuf)));
    2614                 :             :         if (t->sibling != NULL)
    2615                 :             :                 fprintf(f, " S:%s", stid(t->sibling, idbuf, sizeof(idbuf)));
    2616                 :             :         if (!NULLCNFA(t->cnfa))
    2617                 :             :         {
    2618                 :             :                 fprintf(f, "\n");
    2619                 :             :                 dumpcnfa(&t->cnfa, f);
    2620                 :             :         }
    2621                 :             :         fprintf(f, "\n");
    2622                 :             :         for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
    2623                 :             :                 stdump(t2, f, nfapresent);
    2624                 :             : }
    2625                 :             : 
    2626                 :             : /*
    2627                 :             :  * stid - identify a subtree node for dumping
    2628                 :             :  */
    2629                 :             : static const char *                             /* points to buf or constant string */
    2630                 :             : stid(struct subre *t,
    2631                 :             :          char *buf,
    2632                 :             :          size_t bufsize)
    2633                 :             : {
    2634                 :             :         /* big enough for hex int or decimal t->id? */
    2635                 :             :         if (bufsize < sizeof(void *) * 2 + 3 || bufsize < sizeof(t->id) * 3 + 1)
    2636                 :             :                 return "unable";
    2637                 :             :         if (t->id != 0)
    2638                 :             :                 sprintf(buf, "%d", t->id);
    2639                 :             :         else
    2640                 :             :                 sprintf(buf, "%p", t);
    2641                 :             :         return buf;
    2642                 :             : }
    2643                 :             : #endif                                                  /* REG_DEBUG */
    2644                 :             : 
    2645                 :             : 
    2646                 :             : #include "regc_lex.c"
    2647                 :             : #include "regc_color.c"
    2648                 :             : #include "regc_nfa.c"
    2649                 :             : #include "regc_cvec.c"
    2650                 :             : #include "regc_pg_locale.c"
    2651                 :             : #include "regc_locale.c"
        

Generated by: LCOV version 2.3.2-1