LCOV - differential code coverage report
Current view: top level - src/backend/regex - regcomp.c (source / functions) Coverage Total Hit LBC UIC GBC GIC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 96.8 % 1003 971 23 9 15 502 454 17 487 7
Current Date: 2023-04-08 17:13:01 Functions: 100.0 % 35 35 35 34 1
Baseline: 15 Line coverage date bins:
Baseline Date: 2023-04-08 15:09:40 (240..) days: 96.8 % 1003 971 23 9 15 502 454 17 487
Legend: Lines: hit not hit Function coverage date bins:
(240..) days: 50.7 % 69 35 35 34

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

Generated by: LCOV version v1.16-55-g56c0a2a