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