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