PostgreSQL Source Code  git master
regexec.c
Go to the documentation of this file.
1 /*
2  * re_*exec and friends - match REs
3  *
4  * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
5  *
6  * Development of this software was funded, in part, by Cray Research Inc.,
7  * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
8  * Corporation, none of whom are responsible for the results. The author
9  * thanks all of them.
10  *
11  * Redistribution and use in source and binary forms -- with or without
12  * modification -- are permitted for any purpose, provided that
13  * redistributions in source form retain this entire copyright notice and
14  * indicate the origin and nature of any modifications.
15  *
16  * I'd appreciate being given credit for this package in the documentation
17  * of software which uses it, but that is not a requirement.
18  *
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22  * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * src/backend/regex/regexec.c
31  *
32  */
33 
34 #include "regex/regguts.h"
35 
36 
37 
38 /* lazy-DFA representation */
39 struct arcp
40 { /* "pointer" to an outarc */
41  struct sset *ss;
43 };
44 
45 struct sset
46 { /* state set */
47  unsigned *states; /* pointer to bitvector */
48  unsigned hash; /* hash of bitvector */
49 #define HASH(bv, nw) (((nw) == 1) ? *(bv) : hash(bv, nw))
50 #define HIT(h,bv,ss,nw) ((ss)->hash == (h) && ((nw) == 1 || \
51  memcmp(VS(bv), VS((ss)->states), (nw)*sizeof(unsigned)) == 0))
52  int flags;
53 #define STARTER 01 /* the initial state set */
54 #define POSTSTATE 02 /* includes the goal state */
55 #define LOCKED 04 /* locked in cache */
56 #define NOPROGRESS 010 /* zero-progress state set */
57  struct arcp ins; /* chain of inarcs pointing here */
58  chr *lastseen; /* last entered on arrival here */
59  struct sset **outs; /* outarc vector indexed by color */
60  struct arcp *inchain; /* chain-pointer vector for outarcs */
61 };
62 
63 struct dfa
64 {
65  int nssets; /* size of cache */
66  int nssused; /* how many entries occupied yet */
67  int nstates; /* number of states */
68  int ncolors; /* length of outarc and inchain vectors */
69  int wordsper; /* length of state-set bitvectors */
70  struct sset *ssets; /* state-set cache */
71  unsigned *statesarea; /* bitvector storage */
72  unsigned *work; /* pointer to work area within statesarea */
73  struct sset **outsarea; /* outarc-vector storage */
74  struct arcp *incarea; /* inchain storage */
75  struct cnfa *cnfa;
76  struct colormap *cm;
77  chr *lastpost; /* location of last cache-flushed success */
78  chr *lastnopr; /* location of last cache-flushed NOPROGRESS */
79  struct sset *search; /* replacement-search-pointer memory */
80  int backno; /* if DFA for a backref, subno it refers to */
81  short backmin; /* min repetitions for backref */
82  short backmax; /* max repetitions for backref */
83  bool ismalloced; /* should this struct dfa be freed? */
84  bool arraysmalloced; /* should its subsidiary arrays be freed? */
85 };
86 
87 #define WORK 1 /* number of work bitvectors needed */
88 
89 /* setup for non-malloc allocation for small cases */
90 #define FEWSTATES 20 /* must be less than UBITS */
91 #define FEWCOLORS 15
92 struct smalldfa
93 {
94  struct dfa dfa; /* must be first */
95  struct sset ssets[FEWSTATES * 2];
96  unsigned statesarea[FEWSTATES * 2 + WORK];
97  struct sset *outsarea[FEWSTATES * 2 * FEWCOLORS];
98  struct arcp incarea[FEWSTATES * 2 * FEWCOLORS];
99 };
100 
101 #define DOMALLOC ((struct smalldfa *)NULL) /* force malloc */
102 
103 
104 
105 /* internal variables, bundled for easy passing around */
106 struct vars
107 {
108  regex_t *re;
109  struct guts *g;
110  int eflags; /* copies of arguments */
111  size_t nmatch;
114  chr *start; /* start of string */
115  chr *search_start; /* search start of string */
116  chr *stop; /* just past end of string */
117  int err; /* error code if any (0 none) */
118  struct dfa **subdfas; /* per-tree-subre DFAs */
119  struct dfa **ladfas; /* per-lacon-subre DFAs */
120  struct sset **lblastcss; /* per-lacon-subre lookbehind restart data */
121  chr **lblastcp; /* per-lacon-subre lookbehind restart data */
122  struct smalldfa dfa1;
123  struct smalldfa dfa2;
124 };
125 
126 #define VISERR(vv) ((vv)->err != 0) /* have we seen an error yet? */
127 #define ISERR() VISERR(v)
128 #define VERR(vv,e) ((vv)->err = ((vv)->err ? (vv)->err : (e)))
129 #define ERR(e) VERR(v, e) /* record an error */
130 #define NOERR() {if (ISERR()) return v->err;} /* if error seen, return it */
131 #define OFF(p) ((p) - v->start)
132 #define LOFF(p) ((long)OFF(p))
133 
134 
135 
136 /*
137  * forward declarations
138  */
139 /* === regexec.c === */
140 static struct dfa *getsubdfa(struct vars *, struct subre *);
141 static struct dfa *getladfa(struct vars *, int);
142 static int find(struct vars *, struct cnfa *, struct colormap *);
143 static int cfind(struct vars *, struct cnfa *, struct colormap *);
144 static int cfindloop(struct vars *, struct cnfa *, struct colormap *, struct dfa *, struct dfa *, chr **);
145 static void zapallsubs(regmatch_t *, size_t);
146 static void zaptreesubs(struct vars *, struct subre *);
147 static void subset(struct vars *, struct subre *, chr *, chr *);
148 static int cdissect(struct vars *, struct subre *, chr *, chr *);
149 static int ccondissect(struct vars *, struct subre *, chr *, chr *);
150 static int crevcondissect(struct vars *, struct subre *, chr *, chr *);
151 static int cbrdissect(struct vars *, struct subre *, chr *, chr *);
152 static int caltdissect(struct vars *, struct subre *, chr *, chr *);
153 static int citerdissect(struct vars *, struct subre *, chr *, chr *);
154 static int creviterdissect(struct vars *, struct subre *, chr *, chr *);
155 
156 /* === rege_dfa.c === */
157 static chr *longest(struct vars *, struct dfa *, chr *, chr *, int *);
158 static chr *shortest(struct vars *, struct dfa *, chr *, chr *, chr *, chr **, int *);
159 static int matchuntil(struct vars *, struct dfa *, chr *, struct sset **, chr **);
160 static chr *dfa_backref(struct vars *, struct dfa *, chr *, chr *, chr *, bool);
161 static chr *lastcold(struct vars *, struct dfa *);
162 static struct dfa *newdfa(struct vars *, struct cnfa *, struct colormap *, struct smalldfa *);
163 static void freedfa(struct dfa *);
164 static unsigned hash(unsigned *, int);
165 static struct sset *initialize(struct vars *, struct dfa *, chr *);
166 static struct sset *miss(struct vars *, struct dfa *, struct sset *, color, chr *, chr *);
167 static int lacon(struct vars *, struct cnfa *, chr *, color);
168 static struct sset *getvacant(struct vars *, struct dfa *, chr *, chr *);
169 static struct sset *pickss(struct vars *, struct dfa *, chr *, chr *);
170 
171 
172 /*
173  * pg_regexec - match regular expression
174  */
175 int
177  const chr *string,
178  size_t len,
179  size_t search_start,
180  rm_detail_t *details,
181  size_t nmatch,
182  regmatch_t pmatch[],
183  int flags)
184 {
185  struct vars var;
186  register struct vars *v = &var;
187  int st;
188  size_t n;
189  size_t i;
190  int backref;
191 
192 #define LOCALMAT 20
193  regmatch_t mat[LOCALMAT];
194 
195 #define LOCALDFAS 40
196  struct dfa *subdfas[LOCALDFAS];
197 
198  /* sanity checks */
199  if (re == NULL || string == NULL || re->re_magic != REMAGIC)
200  return REG_INVARG;
201  if (re->re_csize != sizeof(chr))
202  return REG_MIXED;
203 
204  /* Initialize locale-dependent support */
206 
207  /* setup */
208  v->re = re;
209  v->g = (struct guts *) re->re_guts;
210  if ((v->g->cflags & REG_EXPECT) && details == NULL)
211  return REG_INVARG;
212  if (v->g->info & REG_UIMPOSSIBLE)
213  return REG_NOMATCH;
214  backref = (v->g->info & REG_UBACKREF) ? 1 : 0;
215  v->eflags = flags;
216  if (v->g->cflags & REG_NOSUB)
217  nmatch = 0; /* override client */
218  v->nmatch = nmatch;
219  if (backref)
220  {
221  /* need work area */
222  if (v->g->nsub + 1 <= LOCALMAT)
223  v->pmatch = mat;
224  else
225  v->pmatch = (regmatch_t *) MALLOC((v->g->nsub + 1) *
226  sizeof(regmatch_t));
227  if (v->pmatch == NULL)
228  return REG_ESPACE;
229  v->nmatch = v->g->nsub + 1;
230  }
231  else
232  v->pmatch = pmatch;
233  v->details = details;
234  v->start = (chr *) string;
235  v->search_start = (chr *) string + search_start;
236  v->stop = (chr *) string + len;
237  v->err = 0;
238  v->subdfas = NULL;
239  v->ladfas = NULL;
240  v->lblastcss = NULL;
241  v->lblastcp = NULL;
242  /* below this point, "goto cleanup" will behave sanely */
243 
244  assert(v->g->ntree >= 0);
245  n = (size_t) v->g->ntree;
246  if (n <= LOCALDFAS)
247  v->subdfas = subdfas;
248  else
249  {
250  v->subdfas = (struct dfa **) MALLOC(n * sizeof(struct dfa *));
251  if (v->subdfas == NULL)
252  {
253  st = REG_ESPACE;
254  goto cleanup;
255  }
256  }
257  for (i = 0; i < n; i++)
258  v->subdfas[i] = NULL;
259 
260  assert(v->g->nlacons >= 0);
261  n = (size_t) v->g->nlacons;
262  if (n > 0)
263  {
264  v->ladfas = (struct dfa **) MALLOC(n * sizeof(struct dfa *));
265  if (v->ladfas == NULL)
266  {
267  st = REG_ESPACE;
268  goto cleanup;
269  }
270  for (i = 0; i < n; i++)
271  v->ladfas[i] = NULL;
272  v->lblastcss = (struct sset **) MALLOC(n * sizeof(struct sset *));
273  v->lblastcp = (chr **) MALLOC(n * sizeof(chr *));
274  if (v->lblastcss == NULL || v->lblastcp == NULL)
275  {
276  st = REG_ESPACE;
277  goto cleanup;
278  }
279  for (i = 0; i < n; i++)
280  {
281  v->lblastcss[i] = NULL;
282  v->lblastcp[i] = NULL;
283  }
284  }
285 
286  /* do it */
287  assert(v->g->tree != NULL);
288  if (backref)
289  st = cfind(v, &v->g->tree->cnfa, &v->g->cmap);
290  else
291  st = find(v, &v->g->tree->cnfa, &v->g->cmap);
292 
293  /* copy (portion of) match vector over if necessary */
294  if (st == REG_OKAY && v->pmatch != pmatch && nmatch > 0)
295  {
296  zapallsubs(pmatch, nmatch);
297  n = (nmatch < v->nmatch) ? nmatch : v->nmatch;
298  memcpy(VS(pmatch), VS(v->pmatch), n * sizeof(regmatch_t));
299  }
300 
301  /* clean up */
302 cleanup:
303  if (v->pmatch != pmatch && v->pmatch != mat)
304  FREE(v->pmatch);
305  if (v->subdfas != NULL)
306  {
307  n = (size_t) v->g->ntree;
308  for (i = 0; i < n; i++)
309  {
310  if (v->subdfas[i] != NULL)
311  freedfa(v->subdfas[i]);
312  }
313  if (v->subdfas != subdfas)
314  FREE(v->subdfas);
315  }
316  if (v->ladfas != NULL)
317  {
318  n = (size_t) v->g->nlacons;
319  for (i = 0; i < n; i++)
320  {
321  if (v->ladfas[i] != NULL)
322  freedfa(v->ladfas[i]);
323  }
324  FREE(v->ladfas);
325  }
326  if (v->lblastcss != NULL)
327  FREE(v->lblastcss);
328  if (v->lblastcp != NULL)
329  FREE(v->lblastcp);
330 
331 #ifdef REG_DEBUG
332  if (v->eflags & (REG_FTRACE | REG_MTRACE))
333  fflush(stdout);
334 #endif
335 
336  return st;
337 }
338 
339 /*
340  * getsubdfa - create or re-fetch the DFA for a tree subre node
341  *
342  * We only need to create the DFA once per overall regex execution.
343  * The DFA will be freed by the cleanup step in pg_regexec().
344  */
345 static struct dfa *
346 getsubdfa(struct vars *v,
347  struct subre *t)
348 {
349  struct dfa *d = v->subdfas[t->id];
350 
351  if (d == NULL)
352  {
353  d = newdfa(v, &t->cnfa, &v->g->cmap, DOMALLOC);
354  if (ISERR())
355  return NULL;
356  /* set up additional info if this is a backref node */
357  if (t->op == 'b')
358  {
359  d->backno = t->backno;
360  d->backmin = t->min;
361  d->backmax = t->max;
362  }
363  v->subdfas[t->id] = d;
364  }
365  return d;
366 }
367 
368 /*
369  * getladfa - create or re-fetch the DFA for a LACON subre node
370  *
371  * Same as above, but for LACONs.
372  */
373 static struct dfa *
374 getladfa(struct vars *v,
375  int n)
376 {
377  assert(n > 0 && n < v->g->nlacons && v->g->lacons != NULL);
378 
379  if (v->ladfas[n] == NULL)
380  {
381  struct subre *sub = &v->g->lacons[n];
382 
383  v->ladfas[n] = newdfa(v, &sub->cnfa, &v->g->cmap, DOMALLOC);
384  if (ISERR())
385  return NULL;
386  /* a LACON can't contain a backref, so nothing else to do */
387  }
388  return v->ladfas[n];
389 }
390 
391 /*
392  * find - find a match for the main NFA (no-complications case)
393  */
394 static int
395 find(struct vars *v,
396  struct cnfa *cnfa,
397  struct colormap *cm)
398 {
399  struct dfa *s;
400  struct dfa *d;
401  chr *begin;
402  chr *end = NULL;
403  chr *cold;
404  chr *open; /* open and close of range of possible starts */
405  chr *close;
406  int hitend;
407  int shorter = (v->g->tree->flags & SHORTER) ? 1 : 0;
408 
409  /* first, a shot with the search RE */
410  s = newdfa(v, &v->g->search, cm, &v->dfa1);
411  assert(!(ISERR() && s != NULL));
412  NOERR();
413  MDEBUG(("\nsearch at %ld\n", LOFF(v->start)));
414  cold = NULL;
415  close = shortest(v, s, v->search_start, v->search_start, v->stop,
416  &cold, (int *) NULL);
417  freedfa(s);
418  NOERR();
419  if (v->g->cflags & REG_EXPECT)
420  {
421  assert(v->details != NULL);
422  if (cold != NULL)
423  v->details->rm_extend.rm_so = OFF(cold);
424  else
425  v->details->rm_extend.rm_so = OFF(v->stop);
426  v->details->rm_extend.rm_eo = OFF(v->stop); /* unknown */
427  }
428  if (close == NULL) /* not found */
429  return REG_NOMATCH;
430  if (v->nmatch == 0) /* found, don't need exact location */
431  return REG_OKAY;
432 
433  /* find starting point and match */
434  assert(cold != NULL);
435  open = cold;
436  cold = NULL;
437  MDEBUG(("between %ld and %ld\n", LOFF(open), LOFF(close)));
438  d = newdfa(v, cnfa, cm, &v->dfa1);
439  assert(!(ISERR() && d != NULL));
440  NOERR();
441  for (begin = open; begin <= close; begin++)
442  {
443  MDEBUG(("\nfind trying at %ld\n", LOFF(begin)));
444  if (shorter)
445  end = shortest(v, d, begin, begin, v->stop,
446  (chr **) NULL, &hitend);
447  else
448  end = longest(v, d, begin, v->stop, &hitend);
449  if (ISERR())
450  {
451  freedfa(d);
452  return v->err;
453  }
454  if (hitend && cold == NULL)
455  cold = begin;
456  if (end != NULL)
457  break; /* NOTE BREAK OUT */
458  }
459  assert(end != NULL); /* search RE succeeded so loop should */
460  freedfa(d);
461 
462  /* and pin down details */
463  assert(v->nmatch > 0);
464  v->pmatch[0].rm_so = OFF(begin);
465  v->pmatch[0].rm_eo = OFF(end);
466  if (v->g->cflags & REG_EXPECT)
467  {
468  if (cold != NULL)
469  v->details->rm_extend.rm_so = OFF(cold);
470  else
471  v->details->rm_extend.rm_so = OFF(v->stop);
472  v->details->rm_extend.rm_eo = OFF(v->stop); /* unknown */
473  }
474  if (v->nmatch == 1) /* no need for submatches */
475  return REG_OKAY;
476 
477  /* find submatches */
478  zapallsubs(v->pmatch, v->nmatch);
479  return cdissect(v, v->g->tree, begin, end);
480 }
481 
482 /*
483  * cfind - find a match for the main NFA (with complications)
484  */
485 static int
486 cfind(struct vars *v,
487  struct cnfa *cnfa,
488  struct colormap *cm)
489 {
490  struct dfa *s;
491  struct dfa *d;
492  chr *cold;
493  int ret;
494 
495  s = newdfa(v, &v->g->search, cm, &v->dfa1);
496  NOERR();
497  d = newdfa(v, cnfa, cm, &v->dfa2);
498  if (ISERR())
499  {
500  assert(d == NULL);
501  freedfa(s);
502  return v->err;
503  }
504 
505  ret = cfindloop(v, cnfa, cm, d, s, &cold);
506 
507  freedfa(d);
508  freedfa(s);
509  NOERR();
510  if (v->g->cflags & REG_EXPECT)
511  {
512  assert(v->details != NULL);
513  if (cold != NULL)
514  v->details->rm_extend.rm_so = OFF(cold);
515  else
516  v->details->rm_extend.rm_so = OFF(v->stop);
517  v->details->rm_extend.rm_eo = OFF(v->stop); /* unknown */
518  }
519  return ret;
520 }
521 
522 /*
523  * cfindloop - the heart of cfind
524  */
525 static int
526 cfindloop(struct vars *v,
527  struct cnfa *cnfa,
528  struct colormap *cm,
529  struct dfa *d,
530  struct dfa *s,
531  chr **coldp) /* where to put coldstart pointer */
532 {
533  chr *begin;
534  chr *end;
535  chr *cold;
536  chr *open; /* open and close of range of possible starts */
537  chr *close;
538  chr *estart;
539  chr *estop;
540  int er;
541  int shorter = v->g->tree->flags & SHORTER;
542  int hitend;
543 
544  assert(d != NULL && s != NULL);
545  cold = NULL;
546  close = v->search_start;
547  do
548  {
549  /* Search with the search RE for match range at/beyond "close" */
550  MDEBUG(("\ncsearch at %ld\n", LOFF(close)));
551  close = shortest(v, s, close, close, v->stop, &cold, (int *) NULL);
552  if (ISERR())
553  {
554  *coldp = cold;
555  return v->err;
556  }
557  if (close == NULL)
558  break; /* no more possible match anywhere */
559  assert(cold != NULL);
560  open = cold;
561  cold = NULL;
562  /* Search for matches starting between "open" and "close" inclusive */
563  MDEBUG(("cbetween %ld and %ld\n", LOFF(open), LOFF(close)));
564  for (begin = open; begin <= close; begin++)
565  {
566  MDEBUG(("\ncfind trying at %ld\n", LOFF(begin)));
567  estart = begin;
568  estop = v->stop;
569  for (;;)
570  {
571  /* Here we use the top node's detailed RE */
572  if (shorter)
573  end = shortest(v, d, begin, estart,
574  estop, (chr **) NULL, &hitend);
575  else
576  end = longest(v, d, begin, estop,
577  &hitend);
578  if (ISERR())
579  {
580  *coldp = cold;
581  return v->err;
582  }
583  if (hitend && cold == NULL)
584  cold = begin;
585  if (end == NULL)
586  break; /* no match with this begin point, try next */
587  MDEBUG(("tentative end %ld\n", LOFF(end)));
588  /* Dissect the potential match to see if it really matches */
589  zapallsubs(v->pmatch, v->nmatch);
590  er = cdissect(v, v->g->tree, begin, end);
591  if (er == REG_OKAY)
592  {
593  if (v->nmatch > 0)
594  {
595  v->pmatch[0].rm_so = OFF(begin);
596  v->pmatch[0].rm_eo = OFF(end);
597  }
598  *coldp = cold;
599  return REG_OKAY;
600  }
601  if (er != REG_NOMATCH)
602  {
603  ERR(er);
604  *coldp = cold;
605  return er;
606  }
607  /* Try next longer/shorter match with same begin point */
608  if (shorter)
609  {
610  if (end == estop)
611  break; /* no more, so try next begin point */
612  estart = end + 1;
613  }
614  else
615  {
616  if (end == begin)
617  break; /* no more, so try next begin point */
618  estop = end - 1;
619  }
620  } /* end loop over endpoint positions */
621  } /* end loop over beginning positions */
622 
623  /*
624  * If we get here, there is no possible match starting at or before
625  * "close", so consider matches beyond that. We'll do a fresh search
626  * with the search RE to find a new promising match range.
627  */
628  close++;
629  } while (close < v->stop);
630 
631  *coldp = cold;
632  return REG_NOMATCH;
633 }
634 
635 /*
636  * zapallsubs - initialize all subexpression matches to "no match"
637  */
638 static void
640  size_t n)
641 {
642  size_t i;
643 
644  for (i = n - 1; i > 0; i--)
645  {
646  p[i].rm_so = -1;
647  p[i].rm_eo = -1;
648  }
649 }
650 
651 /*
652  * zaptreesubs - initialize subexpressions within subtree to "no match"
653  */
654 static void
655 zaptreesubs(struct vars *v,
656  struct subre *t)
657 {
658  int n = t->capno;
659  struct subre *t2;
660 
661  if (n > 0)
662  {
663  if ((size_t) n < v->nmatch)
664  {
665  v->pmatch[n].rm_so = -1;
666  v->pmatch[n].rm_eo = -1;
667  }
668  }
669 
670  for (t2 = t->child; t2 != NULL; t2 = t2->sibling)
671  zaptreesubs(v, t2);
672 }
673 
674 /*
675  * subset - set subexpression match data for a successful subre
676  */
677 static void
678 subset(struct vars *v,
679  struct subre *sub,
680  chr *begin,
681  chr *end)
682 {
683  int n = sub->capno;
684 
685  assert(n > 0);
686  if ((size_t) n >= v->nmatch)
687  return;
688 
689  MDEBUG(("%d: setting %d = %ld-%ld\n", sub->id, n, LOFF(begin), LOFF(end)));
690  v->pmatch[n].rm_so = OFF(begin);
691  v->pmatch[n].rm_eo = OFF(end);
692 }
693 
694 /*
695  * cdissect - check backrefs and determine subexpression matches
696  *
697  * cdissect recursively processes a subre tree to check matching of backrefs
698  * and/or identify submatch boundaries for capture nodes. The proposed match
699  * runs from "begin" to "end" (not including "end"), and we are basically
700  * "dissecting" it to see where the submatches are.
701  *
702  * Before calling any level of cdissect, the caller must have run the node's
703  * DFA and found that the proposed substring satisfies the DFA. (We make
704  * the caller do that because in concatenation and iteration nodes, it's
705  * much faster to check all the substrings against the child DFAs before we
706  * recurse.) Also, caller must have cleared subexpression match data via
707  * zaptreesubs (or zapallsubs at the top level).
708  */
709 static int /* regexec return code */
710 cdissect(struct vars *v,
711  struct subre *t,
712  chr *begin, /* beginning of relevant substring */
713  chr *end) /* end of same */
714 {
715  int er;
716 
717  assert(t != NULL);
718  MDEBUG(("%d: cdissect %c %ld-%ld\n", t->id, t->op, LOFF(begin), LOFF(end)));
719 
720  /* handy place to check for operation cancel */
721  if (CANCEL_REQUESTED(v->re))
722  return REG_CANCEL;
723  /* ... and stack overrun */
724  if (STACK_TOO_DEEP(v->re))
725  return REG_ETOOBIG;
726 
727  switch (t->op)
728  {
729  case '=': /* terminal node */
730  assert(t->child == NULL);
731  er = REG_OKAY; /* no action, parent did the work */
732  break;
733  case 'b': /* back reference */
734  assert(t->child == NULL);
735  er = cbrdissect(v, t, begin, end);
736  break;
737  case '.': /* concatenation */
738  assert(t->child != NULL);
739  if (t->child->flags & SHORTER) /* reverse scan */
740  er = crevcondissect(v, t, begin, end);
741  else
742  er = ccondissect(v, t, begin, end);
743  break;
744  case '|': /* alternation */
745  assert(t->child != NULL);
746  er = caltdissect(v, t, begin, end);
747  break;
748  case '*': /* iteration */
749  assert(t->child != NULL);
750  if (t->child->flags & SHORTER) /* reverse scan */
751  er = creviterdissect(v, t, begin, end);
752  else
753  er = citerdissect(v, t, begin, end);
754  break;
755  case '(': /* no-op capture node */
756  assert(t->child != NULL);
757  assert(t->capno > 0);
758  er = cdissect(v, t->child, begin, end);
759  break;
760  default:
761  er = REG_ASSERT;
762  break;
763  }
764 
765  /*
766  * We should never have a match failure unless backrefs lurk below;
767  * otherwise, either caller failed to check the DFA, or there's some
768  * inconsistency between the DFA and the node's innards.
769  */
770  assert(er != REG_NOMATCH || (t->flags & BACKR));
771 
772  /*
773  * If this node is marked as capturing, save successful match's location.
774  */
775  if (t->capno > 0 && er == REG_OKAY)
776  subset(v, t, begin, end);
777 
778  return er;
779 }
780 
781 /*
782  * ccondissect - dissect match for concatenation node
783  */
784 static int /* regexec return code */
785 ccondissect(struct vars *v,
786  struct subre *t,
787  chr *begin, /* beginning of relevant substring */
788  chr *end) /* end of same */
789 {
790  struct subre *left = t->child;
791  struct subre *right = left->sibling;
792  struct dfa *d;
793  struct dfa *d2;
794  chr *mid;
795  int er;
796 
797  assert(t->op == '.');
798  assert(left != NULL && left->cnfa.nstates > 0);
799  assert(right != NULL && right->cnfa.nstates > 0);
800  assert(right->sibling == NULL);
801  assert(!(left->flags & SHORTER));
802 
803  d = getsubdfa(v, left);
804  NOERR();
805  d2 = getsubdfa(v, right);
806  NOERR();
807  MDEBUG(("%d: ccondissect %ld-%ld\n", t->id, LOFF(begin), LOFF(end)));
808 
809  /* pick a tentative midpoint */
810  mid = longest(v, d, begin, end, (int *) NULL);
811  NOERR();
812  if (mid == NULL)
813  return REG_NOMATCH;
814  MDEBUG(("%d: tentative midpoint %ld\n", t->id, LOFF(mid)));
815 
816  /* iterate until satisfaction or failure */
817  for (;;)
818  {
819  /* try this midpoint on for size */
820  if (longest(v, d2, mid, end, (int *) NULL) == end)
821  {
822  er = cdissect(v, left, begin, mid);
823  if (er == REG_OKAY)
824  {
825  er = cdissect(v, right, mid, end);
826  if (er == REG_OKAY)
827  {
828  /* satisfaction */
829  MDEBUG(("%d: successful\n", t->id));
830  return REG_OKAY;
831  }
832  }
833  if (er != REG_NOMATCH)
834  return er;
835  }
836  NOERR();
837 
838  /* that midpoint didn't work, find a new one */
839  if (mid == begin)
840  {
841  /* all possibilities exhausted */
842  MDEBUG(("%d: no midpoint\n", t->id));
843  return REG_NOMATCH;
844  }
845  mid = longest(v, d, begin, mid - 1, (int *) NULL);
846  NOERR();
847  if (mid == NULL)
848  {
849  /* failed to find a new one */
850  MDEBUG(("%d: failed midpoint\n", t->id));
851  return REG_NOMATCH;
852  }
853  MDEBUG(("%d: new midpoint %ld\n", t->id, LOFF(mid)));
854  zaptreesubs(v, left);
855  zaptreesubs(v, right);
856  }
857 
858  /* can't get here */
859  return REG_ASSERT;
860 }
861 
862 /*
863  * crevcondissect - dissect match for concatenation node, shortest-first
864  */
865 static int /* regexec return code */
867  struct subre *t,
868  chr *begin, /* beginning of relevant substring */
869  chr *end) /* end of same */
870 {
871  struct subre *left = t->child;
872  struct subre *right = left->sibling;
873  struct dfa *d;
874  struct dfa *d2;
875  chr *mid;
876  int er;
877 
878  assert(t->op == '.');
879  assert(left != NULL && left->cnfa.nstates > 0);
880  assert(right != NULL && right->cnfa.nstates > 0);
881  assert(right->sibling == NULL);
882  assert(left->flags & SHORTER);
883 
884  d = getsubdfa(v, left);
885  NOERR();
886  d2 = getsubdfa(v, right);
887  NOERR();
888  MDEBUG(("%d: crevcondissect %ld-%ld\n", t->id, LOFF(begin), LOFF(end)));
889 
890  /* pick a tentative midpoint */
891  mid = shortest(v, d, begin, begin, end, (chr **) NULL, (int *) NULL);
892  NOERR();
893  if (mid == NULL)
894  return REG_NOMATCH;
895  MDEBUG(("%d: tentative midpoint %ld\n", t->id, LOFF(mid)));
896 
897  /* iterate until satisfaction or failure */
898  for (;;)
899  {
900  /* try this midpoint on for size */
901  if (longest(v, d2, mid, end, (int *) NULL) == end)
902  {
903  er = cdissect(v, left, begin, mid);
904  if (er == REG_OKAY)
905  {
906  er = cdissect(v, right, mid, end);
907  if (er == REG_OKAY)
908  {
909  /* satisfaction */
910  MDEBUG(("%d: successful\n", t->id));
911  return REG_OKAY;
912  }
913  }
914  if (er != REG_NOMATCH)
915  return er;
916  }
917  NOERR();
918 
919  /* that midpoint didn't work, find a new one */
920  if (mid == end)
921  {
922  /* all possibilities exhausted */
923  MDEBUG(("%d: no midpoint\n", t->id));
924  return REG_NOMATCH;
925  }
926  mid = shortest(v, d, begin, mid + 1, end, (chr **) NULL, (int *) NULL);
927  NOERR();
928  if (mid == NULL)
929  {
930  /* failed to find a new one */
931  MDEBUG(("%d: failed midpoint\n", t->id));
932  return REG_NOMATCH;
933  }
934  MDEBUG(("%d: new midpoint %ld\n", t->id, LOFF(mid)));
935  zaptreesubs(v, left);
936  zaptreesubs(v, right);
937  }
938 
939  /* can't get here */
940  return REG_ASSERT;
941 }
942 
943 /*
944  * cbrdissect - dissect match for backref node
945  *
946  * The backref match might already have been verified by dfa_backref(),
947  * but we don't know that for sure so must check it here.
948  */
949 static int /* regexec return code */
950 cbrdissect(struct vars *v,
951  struct subre *t,
952  chr *begin, /* beginning of relevant substring */
953  chr *end) /* end of same */
954 {
955  int n = t->backno;
956  size_t numreps;
957  size_t tlen;
958  size_t brlen;
959  chr *brstring;
960  chr *p;
961  int min = t->min;
962  int max = t->max;
963 
964  assert(t != NULL);
965  assert(t->op == 'b');
966  assert(n >= 0);
967  assert((size_t) n < v->nmatch);
968 
969  MDEBUG(("%d: cbrdissect %d{%d-%d} %ld-%ld\n", t->id, n, min, max,
970  LOFF(begin), LOFF(end)));
971 
972  /* get the backreferenced string */
973  if (v->pmatch[n].rm_so == -1)
974  return REG_NOMATCH;
975  brstring = v->start + v->pmatch[n].rm_so;
976  brlen = v->pmatch[n].rm_eo - v->pmatch[n].rm_so;
977 
978  /* special cases for zero-length strings */
979  if (brlen == 0)
980  {
981  /*
982  * matches only if target is zero length, but any number of
983  * repetitions can be considered to be present
984  */
985  if (begin == end && min <= max)
986  {
987  MDEBUG(("%d: backref matched trivially\n", t->id));
988  return REG_OKAY;
989  }
990  return REG_NOMATCH;
991  }
992  if (begin == end)
993  {
994  /* matches only if zero repetitions are okay */
995  if (min == 0)
996  {
997  MDEBUG(("%d: backref matched trivially\n", t->id));
998  return REG_OKAY;
999  }
1000  return REG_NOMATCH;
1001  }
1002 
1003  /*
1004  * check target length to see if it could possibly be an allowed number of
1005  * repetitions of brstring
1006  */
1007  assert(end > begin);
1008  tlen = end - begin;
1009  if (tlen % brlen != 0)
1010  return REG_NOMATCH;
1011  numreps = tlen / brlen;
1012  if (numreps < min || (numreps > max && max != DUPINF))
1013  return REG_NOMATCH;
1014 
1015  /* okay, compare the actual string contents */
1016  p = begin;
1017  while (numreps-- > 0)
1018  {
1019  if ((*v->g->compare) (brstring, p, brlen) != 0)
1020  return REG_NOMATCH;
1021  p += brlen;
1022  }
1023 
1024  MDEBUG(("%d: backref matched\n", t->id));
1025  return REG_OKAY;
1026 }
1027 
1028 /*
1029  * caltdissect - dissect match for alternation node
1030  */
1031 static int /* regexec return code */
1032 caltdissect(struct vars *v,
1033  struct subre *t,
1034  chr *begin, /* beginning of relevant substring */
1035  chr *end) /* end of same */
1036 {
1037  struct dfa *d;
1038  int er;
1039 
1040  assert(t->op == '|');
1041 
1042  t = t->child;
1043  /* there should be at least 2 alternatives */
1044  assert(t != NULL && t->sibling != NULL);
1045 
1046  while (t != NULL)
1047  {
1048  assert(t->cnfa.nstates > 0);
1049 
1050  MDEBUG(("%d: caltdissect %ld-%ld\n", t->id, LOFF(begin), LOFF(end)));
1051 
1052  d = getsubdfa(v, t);
1053  NOERR();
1054  if (longest(v, d, begin, end, (int *) NULL) == end)
1055  {
1056  MDEBUG(("%d: caltdissect matched\n", t->id));
1057  er = cdissect(v, t, begin, end);
1058  if (er != REG_NOMATCH)
1059  return er;
1060  }
1061  NOERR();
1062 
1063  t = t->sibling;
1064  }
1065 
1066  return REG_NOMATCH;
1067 }
1068 
1069 /*
1070  * citerdissect - dissect match for iteration node
1071  */
1072 static int /* regexec return code */
1073 citerdissect(struct vars *v,
1074  struct subre *t,
1075  chr *begin, /* beginning of relevant substring */
1076  chr *end) /* end of same */
1077 {
1078  struct dfa *d;
1079  chr **endpts;
1080  chr *limit;
1081  int min_matches;
1082  size_t max_matches;
1083  int nverified;
1084  int k;
1085  int i;
1086  int er;
1087 
1088  assert(t->op == '*');
1089  assert(t->child != NULL && t->child->cnfa.nstates > 0);
1090  assert(!(t->child->flags & SHORTER));
1091  assert(begin <= end);
1092 
1093  MDEBUG(("%d: citerdissect %ld-%ld\n", t->id, LOFF(begin), LOFF(end)));
1094 
1095  /*
1096  * For the moment, assume the minimum number of matches is 1. If zero
1097  * matches are allowed, and the target string is empty, we are allowed to
1098  * match regardless of the contents of the iter node --- but we would
1099  * prefer to match once, so that capturing parens get set. (An example of
1100  * the concern here is a pattern like "()*\1", which historically this
1101  * code has allowed to succeed.) Therefore, we deal with the zero-matches
1102  * case at the bottom, after failing to find any other way to match.
1103  */
1104  min_matches = t->min;
1105  if (min_matches <= 0)
1106  min_matches = 1;
1107 
1108  /*
1109  * We need workspace to track the endpoints of each sub-match. Normally
1110  * we consider only nonzero-length sub-matches, so there can be at most
1111  * end-begin of them. However, if min is larger than that, we will also
1112  * consider zero-length sub-matches in order to find enough matches.
1113  *
1114  * For convenience, endpts[0] contains the "begin" pointer and we store
1115  * sub-match endpoints in endpts[1..max_matches].
1116  */
1117  max_matches = end - begin;
1118  if (max_matches > t->max && t->max != DUPINF)
1119  max_matches = t->max;
1120  if (max_matches < min_matches)
1121  max_matches = min_matches;
1122  endpts = (chr **) MALLOC((max_matches + 1) * sizeof(chr *));
1123  if (endpts == NULL)
1124  return REG_ESPACE;
1125  endpts[0] = begin;
1126 
1127  d = getsubdfa(v, t->child);
1128  if (ISERR())
1129  {
1130  FREE(endpts);
1131  return v->err;
1132  }
1133 
1134  /*
1135  * Our strategy is to first find a set of sub-match endpoints that are
1136  * valid according to the child node's DFA, and then recursively dissect
1137  * each sub-match to confirm validity. If any validity check fails,
1138  * backtrack the last sub-match and try again. And, when we next try for
1139  * a validity check, we need not recheck any successfully verified
1140  * sub-matches that we didn't move the endpoints of. nverified remembers
1141  * how many sub-matches are currently known okay.
1142  */
1143 
1144  /* initialize to consider first sub-match */
1145  nverified = 0;
1146  k = 1;
1147  limit = end;
1148 
1149  /* iterate until satisfaction or failure */
1150  while (k > 0)
1151  {
1152  /* try to find an endpoint for the k'th sub-match */
1153  endpts[k] = longest(v, d, endpts[k - 1], limit, (int *) NULL);
1154  if (ISERR())
1155  {
1156  FREE(endpts);
1157  return v->err;
1158  }
1159  if (endpts[k] == NULL)
1160  {
1161  /* no match possible, so see if we can shorten previous one */
1162  k--;
1163  goto backtrack;
1164  }
1165  MDEBUG(("%d: working endpoint %d: %ld\n",
1166  t->id, k, LOFF(endpts[k])));
1167 
1168  /* k'th sub-match can no longer be considered verified */
1169  if (nverified >= k)
1170  nverified = k - 1;
1171 
1172  if (endpts[k] != end)
1173  {
1174  /* haven't reached end yet, try another iteration if allowed */
1175  if (k >= max_matches)
1176  {
1177  /* must try to shorten some previous match */
1178  k--;
1179  goto backtrack;
1180  }
1181 
1182  /* reject zero-length match unless necessary to achieve min */
1183  if (endpts[k] == endpts[k - 1] &&
1184  (k >= min_matches || min_matches - k < end - endpts[k]))
1185  goto backtrack;
1186 
1187  k++;
1188  limit = end;
1189  continue;
1190  }
1191 
1192  /*
1193  * We've identified a way to divide the string into k sub-matches that
1194  * works so far as the child DFA can tell. If k is an allowed number
1195  * of matches, start the slow part: recurse to verify each sub-match.
1196  * We always have k <= max_matches, needn't check that.
1197  */
1198  if (k < min_matches)
1199  goto backtrack;
1200 
1201  MDEBUG(("%d: verifying %d..%d\n", t->id, nverified + 1, k));
1202 
1203  for (i = nverified + 1; i <= k; i++)
1204  {
1205  zaptreesubs(v, t->child);
1206  er = cdissect(v, t->child, endpts[i - 1], endpts[i]);
1207  if (er == REG_OKAY)
1208  {
1209  nverified = i;
1210  continue;
1211  }
1212  if (er == REG_NOMATCH)
1213  break;
1214  /* oops, something failed */
1215  FREE(endpts);
1216  return er;
1217  }
1218 
1219  if (i > k)
1220  {
1221  /* satisfaction */
1222  MDEBUG(("%d: successful\n", t->id));
1223  FREE(endpts);
1224  return REG_OKAY;
1225  }
1226 
1227  /* match failed to verify, so backtrack */
1228 
1229 backtrack:
1230 
1231  /*
1232  * Must consider shorter versions of the current sub-match. However,
1233  * we'll only ask for a zero-length match if necessary.
1234  */
1235  while (k > 0)
1236  {
1237  chr *prev_end = endpts[k - 1];
1238 
1239  if (endpts[k] > prev_end)
1240  {
1241  limit = endpts[k] - 1;
1242  if (limit > prev_end ||
1243  (k < min_matches && min_matches - k >= end - prev_end))
1244  {
1245  /* break out of backtrack loop, continue the outer one */
1246  break;
1247  }
1248  }
1249  /* can't shorten k'th sub-match any more, consider previous one */
1250  k--;
1251  }
1252  }
1253 
1254  /* all possibilities exhausted */
1255  FREE(endpts);
1256 
1257  /*
1258  * Now consider the possibility that we can match to a zero-length string
1259  * by using zero repetitions.
1260  */
1261  if (t->min == 0 && begin == end)
1262  {
1263  MDEBUG(("%d: allowing zero matches\n", t->id));
1264  return REG_OKAY;
1265  }
1266 
1267  MDEBUG(("%d: failed\n", t->id));
1268  return REG_NOMATCH;
1269 }
1270 
1271 /*
1272  * creviterdissect - dissect match for iteration node, shortest-first
1273  */
1274 static int /* regexec return code */
1276  struct subre *t,
1277  chr *begin, /* beginning of relevant substring */
1278  chr *end) /* end of same */
1279 {
1280  struct dfa *d;
1281  chr **endpts;
1282  chr *limit;
1283  int min_matches;
1284  size_t max_matches;
1285  int nverified;
1286  int k;
1287  int i;
1288  int er;
1289 
1290  assert(t->op == '*');
1291  assert(t->child != NULL && t->child->cnfa.nstates > 0);
1292  assert(t->child->flags & SHORTER);
1293  assert(begin <= end);
1294 
1295  MDEBUG(("%d: creviterdissect %ld-%ld\n", t->id, LOFF(begin), LOFF(end)));
1296 
1297  /*
1298  * If zero matches are allowed, and target string is empty, just declare
1299  * victory. OTOH, if target string isn't empty, zero matches can't work
1300  * so we pretend the min is 1.
1301  */
1302  min_matches = t->min;
1303  if (min_matches <= 0)
1304  {
1305  if (begin == end)
1306  {
1307  MDEBUG(("%d: allowing zero matches\n", t->id));
1308  return REG_OKAY;
1309  }
1310  min_matches = 1;
1311  }
1312 
1313  /*
1314  * We need workspace to track the endpoints of each sub-match. Normally
1315  * we consider only nonzero-length sub-matches, so there can be at most
1316  * end-begin of them. However, if min is larger than that, we will also
1317  * consider zero-length sub-matches in order to find enough matches.
1318  *
1319  * For convenience, endpts[0] contains the "begin" pointer and we store
1320  * sub-match endpoints in endpts[1..max_matches].
1321  */
1322  max_matches = end - begin;
1323  if (max_matches > t->max && t->max != DUPINF)
1324  max_matches = t->max;
1325  if (max_matches < min_matches)
1326  max_matches = min_matches;
1327  endpts = (chr **) MALLOC((max_matches + 1) * sizeof(chr *));
1328  if (endpts == NULL)
1329  return REG_ESPACE;
1330  endpts[0] = begin;
1331 
1332  d = getsubdfa(v, t->child);
1333  if (ISERR())
1334  {
1335  FREE(endpts);
1336  return v->err;
1337  }
1338 
1339  /*
1340  * Our strategy is to first find a set of sub-match endpoints that are
1341  * valid according to the child node's DFA, and then recursively dissect
1342  * each sub-match to confirm validity. If any validity check fails,
1343  * backtrack the last sub-match and try again. And, when we next try for
1344  * a validity check, we need not recheck any successfully verified
1345  * sub-matches that we didn't move the endpoints of. nverified remembers
1346  * how many sub-matches are currently known okay.
1347  */
1348 
1349  /* initialize to consider first sub-match */
1350  nverified = 0;
1351  k = 1;
1352  limit = begin;
1353 
1354  /* iterate until satisfaction or failure */
1355  while (k > 0)
1356  {
1357  /* disallow zero-length match unless necessary to achieve min */
1358  if (limit == endpts[k - 1] &&
1359  limit != end &&
1360  (k >= min_matches || min_matches - k < end - limit))
1361  limit++;
1362 
1363  /* if this is the last allowed sub-match, it must reach to the end */
1364  if (k >= max_matches)
1365  limit = end;
1366 
1367  /* try to find an endpoint for the k'th sub-match */
1368  endpts[k] = shortest(v, d, endpts[k - 1], limit, end,
1369  (chr **) NULL, (int *) NULL);
1370  if (ISERR())
1371  {
1372  FREE(endpts);
1373  return v->err;
1374  }
1375  if (endpts[k] == NULL)
1376  {
1377  /* no match possible, so see if we can lengthen previous one */
1378  k--;
1379  goto backtrack;
1380  }
1381  MDEBUG(("%d: working endpoint %d: %ld\n",
1382  t->id, k, LOFF(endpts[k])));
1383 
1384  /* k'th sub-match can no longer be considered verified */
1385  if (nverified >= k)
1386  nverified = k - 1;
1387 
1388  if (endpts[k] != end)
1389  {
1390  /* haven't reached end yet, try another iteration if allowed */
1391  if (k >= max_matches)
1392  {
1393  /* must try to lengthen some previous match */
1394  k--;
1395  goto backtrack;
1396  }
1397 
1398  k++;
1399  limit = endpts[k - 1];
1400  continue;
1401  }
1402 
1403  /*
1404  * We've identified a way to divide the string into k sub-matches that
1405  * works so far as the child DFA can tell. If k is an allowed number
1406  * of matches, start the slow part: recurse to verify each sub-match.
1407  * We always have k <= max_matches, needn't check that.
1408  */
1409  if (k < min_matches)
1410  goto backtrack;
1411 
1412  MDEBUG(("%d: verifying %d..%d\n", t->id, nverified + 1, k));
1413 
1414  for (i = nverified + 1; i <= k; i++)
1415  {
1416  zaptreesubs(v, t->child);
1417  er = cdissect(v, t->child, endpts[i - 1], endpts[i]);
1418  if (er == REG_OKAY)
1419  {
1420  nverified = i;
1421  continue;
1422  }
1423  if (er == REG_NOMATCH)
1424  break;
1425  /* oops, something failed */
1426  FREE(endpts);
1427  return er;
1428  }
1429 
1430  if (i > k)
1431  {
1432  /* satisfaction */
1433  MDEBUG(("%d: successful\n", t->id));
1434  FREE(endpts);
1435  return REG_OKAY;
1436  }
1437 
1438  /* match failed to verify, so backtrack */
1439 
1440 backtrack:
1441 
1442  /*
1443  * Must consider longer versions of the current sub-match.
1444  */
1445  while (k > 0)
1446  {
1447  if (endpts[k] < end)
1448  {
1449  limit = endpts[k] + 1;
1450  /* break out of backtrack loop, continue the outer one */
1451  break;
1452  }
1453  /* can't lengthen k'th sub-match any more, consider previous one */
1454  k--;
1455  }
1456  }
1457 
1458  /* all possibilities exhausted */
1459  MDEBUG(("%d: failed\n", t->id));
1460  FREE(endpts);
1461  return REG_NOMATCH;
1462 }
1463 
1464 
1465 
1466 #include "rege_dfa.c"
#define REG_UIMPOSSIBLE
Definition: regex.h:74
static chr * shortest(struct vars *, struct dfa *, chr *, chr *, chr *, chr **, int *)
struct sset * ssets
Definition: regexec.c:70
struct dfa ** ladfas
Definition: regexec.c:119
static struct sset * getvacant(struct vars *, struct dfa *, chr *, chr *)
chr * lastseen
Definition: regexec.c:58
static int find(struct vars *, struct cnfa *, struct colormap *)
Definition: regexec.c:395
struct sset ** outs
Definition: regexec.c:59
#define REG_EXPECT
Definition: regex.h:115
struct subre * child
Definition: regguts.h:495
short backmin
Definition: regexec.c:81
int capno
Definition: regguts.h:491
struct arcp * incarea
Definition: regexec.c:74
struct subre * lacons
Definition: regguts.h:538
static struct sset * miss(struct vars *, struct dfa *, struct sset *, color, chr *, chr *)
struct subre * tree
Definition: regguts.h:533
static unsigned hash(unsigned *, int)
struct colormap * cm
Definition: regexec.c:76
#define REG_ESPACE
Definition: regex.h:151
struct cnfa * cnfa
Definition: regexec.c:75
regoff_t rm_so
Definition: regex.h:87
Definition: regguts.h:401
short color
Definition: regguts.h:150
char op
Definition: regguts.h:473
#define WORK
Definition: regexec.c:87
int nlacons
Definition: regguts.h:539
#define LOFF(p)
Definition: regexec.c:132
short min
Definition: regguts.h:493
#define REMAGIC
Definition: regguts.h:96
#define REG_FTRACE
Definition: regex.h:129
unsigned * work
Definition: regexec.c:72
static chr * lastcold(struct vars *, struct dfa *)
#define FREE(ptr)
Definition: cryptohash.c:37
static struct dfa * newdfa(struct vars *, struct cnfa *, struct colormap *, struct smalldfa *)
static chr * longest(struct vars *, struct dfa *, chr *, chr *, int *)
struct state * begin
Definition: regguts.h:497
struct sset * search
Definition: regexec.c:79
rm_detail_t * details
Definition: regexec.c:113
static int matchuntil(struct vars *, struct dfa *, chr *, struct sset **, chr **)
#define FEWCOLORS
Definition: regexec.c:91
#define MDEBUG(arglist)
Definition: regguts.h:117
int re_csize
Definition: regex.h:76
#define REG_MTRACE
Definition: regex.h:130
chr * start
Definition: regexec.c:114
regoff_t rm_eo
Definition: regex.h:88
#define MALLOC(n)
Definition: regcustom.h:60
Definition: regexec.c:63
struct sset ** lblastcss
Definition: regexec.c:120
chr * search_start
Definition: regexec.c:115
int nstates
Definition: regguts.h:403
int id
Definition: regguts.h:490
int re_magic
Definition: regex.h:57
struct cnfa search
Definition: regguts.h:534
pg_wchar chr
Definition: regcustom.h:66
#define REG_OKAY
Definition: regex.h:139
unsigned * states
Definition: regexec.c:47
bool ismalloced
Definition: regexec.c:83
regex_t * re
Definition: regcomp.c:240
static void zaptreesubs(struct vars *, struct subre *)
Definition: regexec.c:655
struct subre * sibling
Definition: regguts.h:496
static struct dfa * getsubdfa(struct vars *, struct subre *)
Definition: regexec.c:346
static int cfindloop(struct vars *, struct cnfa *, struct colormap *, struct dfa *, struct dfa *, chr **)
Definition: regexec.c:526
struct arcp * inchain
Definition: regexec.c:60
#define DOMALLOC
Definition: regexec.c:101
#define REG_INVARG
Definition: regex.h:154
#define FEWSTATES
Definition: regexec.c:90
struct smalldfa dfa2
Definition: regexec.c:123
char flags
Definition: regguts.h:474
bool arraysmalloced
Definition: regexec.c:84
struct colormap cmap
Definition: regguts.h:536
#define assert(TEST)
Definition: imath.c:73
int ncolors
Definition: regexec.c:68
static int cbrdissect(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:950
int nstates
Definition: regexec.c:67
int nssets
Definition: regexec.c:65
int nssused
Definition: regexec.c:66
int wordsper
Definition: regexec.c:69
struct guts * g
Definition: regexec.c:109
int err
Definition: regcomp.c:243
#define LOCALMAT
int eflags
Definition: regexec.c:110
int backno
Definition: regguts.h:492
static int creviterdissect(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:1275
static void cleanup(void)
Definition: bootstrap.c:862
static struct sset * initialize(struct vars *, struct dfa *, chr *)
chr * lastnopr
Definition: regexec.c:78
#define BACKR
Definition: regguts.h:479
Definition: regguts.h:526
int ntree
Definition: regguts.h:535
#define REG_UBACKREF
Definition: regex.h:60
Definition: regexec.c:39
static int caltdissect(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:1032
#define LOCALDFAS
#define DUPINF
Definition: regguts.h:94
struct dfa ** subdfas
Definition: regexec.c:118
#define REG_ASSERT
Definition: regex.h:153
int backno
Definition: regexec.c:80
void pg_set_regex_collation(Oid collation)
short max
Definition: regguts.h:494
regmatch_t * pmatch
Definition: regexec.c:112
static int lacon(struct vars *, struct cnfa *, chr *, color)
#define ERR(e)
Definition: regexec.c:129
short backmax
Definition: regexec.c:82
chr * stop
Definition: regexec.c:116
#define OFF(p)
Definition: regexec.c:131
static chr * dfa_backref(struct vars *, struct dfa *, chr *, chr *, chr *, bool)
static int ccondissect(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:785
#define REG_ETOOBIG
Definition: regex.h:157
static int citerdissect(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:1073
#define REG_NOSUB
Definition: regex.h:109
static void freedfa(struct dfa *)
#define VS(x)
Definition: regguts.h:61
struct state * end
Definition: regguts.h:498
static int cdissect(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:710
long info
Definition: regguts.h:531
Definition: regguts.h:471
struct cnfa cnfa
Definition: regguts.h:499
size_t nmatch
Definition: regexec.c:111
static void zapallsubs(regmatch_t *, size_t)
Definition: regexec.c:639
regmatch_t rm_extend
Definition: regex.h:94
const chr * stop
Definition: regcomp.c:242
char * re_guts
Definition: regex.h:80
int cflags
Definition: regguts.h:530
struct sset ** outsarea
Definition: regexec.c:73
int pg_regexec(regex_t *re, const chr *string, size_t len, size_t search_start, rm_detail_t *details, size_t nmatch, regmatch_t pmatch[], int flags)
Definition: regexec.c:176
Definition: regexec.c:45
static struct sset * pickss(struct vars *, struct dfa *, chr *, chr *)
size_t nsub
Definition: regguts.h:532
#define SHORTER
Definition: regguts.h:476
#define CANCEL_REQUESTED(re)
Definition: regguts.h:516
int i
#define REG_NOMATCH
Definition: regex.h:140
chr * lastpost
Definition: regexec.c:77
chr ** lblastcp
Definition: regexec.c:121
#define REG_MIXED
Definition: regex.h:155
#define NOERR()
Definition: regexec.c:130
unsigned hash
Definition: regexec.c:48
struct sset * ss
Definition: regexec.c:41
unsigned * statesarea
Definition: regexec.c:71
static struct dfa * getladfa(struct vars *, int)
Definition: regexec.c:374
#define close(a)
Definition: win32.h:12
static int crevcondissect(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:866
int flags
Definition: regexec.c:52
Definition: regcomp.c:238
static void subset(struct vars *, struct subre *, chr *, chr *)
Definition: regexec.c:678
Oid re_collation
Definition: regex.h:78
Definition: regex.h:55
#define STACK_TOO_DEEP(re)
Definition: regguts.h:519
struct smalldfa dfa1
Definition: regexec.c:122
static int cfind(struct vars *, struct cnfa *, struct colormap *)
Definition: regexec.c:486
color co
Definition: regexec.c:42
#define ISERR()
Definition: regexec.c:127
#define REG_CANCEL
Definition: regex.h:159