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