/[dtapublic]/projs/trunk/shared_source/c_tcl_base_7_5_w_mods/regcomp.c
ViewVC logotype

Annotation of /projs/trunk/shared_source/c_tcl_base_7_5_w_mods/regcomp.c

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:keywords Header

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25