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