2 * Internal interface definitions, etc., for the reg package
4 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
6 * Development of this software was funded, in part, by Cray Research Inc.,
7 * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
8 * Corporation, none of whom are responsible for the results. The author
11 * Redistribution and use in source and binary forms -- with or without
12 * modification -- are permitted for any purpose, provided that
13 * redistributions in source form retain this entire copyright notice and
14 * indicate the origin and nature of any modifications.
16 * I'd appreciate being given credit for this package in the documentation of
17 * software which uses it, but that is not a requirement.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Environmental customization. It should not (I hope) be necessary to alter
33 * the file you are now reading -- regcustom.h should handle it all, given
34 * care here and elsewhere.
36 #include "regcustom.h"
39 * Things that regcustom.h might override.
46 #define NDEBUG /* no assertions */
48 #endif /* !REG_DEBUG */
54 #define VOID void /* for function return values */
57 #define DISCARD void /* for throwing values away */
60 #define PVOID void * /* generic pointer */
63 #define VS(x) ((void*)(x)) /* cast something to generic ptr */
66 #define NOPARMS void /* for empty parm lists */
69 /* function-pointer declarator */
72 #define FUNCPTR(name, args) (*name)args
74 #define FUNCPTR(name, args) (*name)()
78 /* memory allocation */
80 #define MALLOC(n) malloc(n)
83 #define REALLOC(p, n) realloc(VS(p), n)
86 #define FREE(p) free(VS(p))
89 /* want size of a char in bits, and max value in bounded quantifiers */
90 #ifndef _POSIX2_RE_DUP_MAX
91 #define _POSIX2_RE_DUP_MAX 255 /* normally from <limits.h> */
101 #define DUPMAX _POSIX2_RE_DUP_MAX
102 #define DUPINF (DUPMAX+1)
104 #define REMAGIC 0xFED7 /* magic number for main struct */
107 * debugging facilities
110 /* FDEBUG does finite-state tracing */
111 #define FDEBUG(arglist) { if (v->eflags®_FTRACE) printf arglist; }
112 /* MDEBUG does higher-level tracing */
113 #define MDEBUG(arglist) { if (v->eflags®_MTRACE) printf arglist; }
115 #define FDEBUG(arglist) {}
116 #define MDEBUG(arglist) {}
120 * bitmap manipulation
122 #define UBITS (CHAR_BIT * sizeof(unsigned))
123 #define BSET(uv, sn) ((uv)[(sn)/UBITS] |= (unsigned)1 << ((sn)%UBITS))
124 #define ISBSET(uv, sn) ((uv)[(sn)/UBITS] & ((unsigned)1 << ((sn)%UBITS)))
127 * We dissect a chr into byts for colormap table indexing. Here we define a
128 * byt, which will be the same as a byte on most machines... The exact size of
129 * a byt is not critical, but about 8 bits is good, and extraction of 8-bit
130 * chunks is sometimes especially fast.
134 #define BYTBITS 8 /* bits in a byt */
136 #define BYTTAB (1<<BYTBITS) /* size of table with one entry per byt value */
137 #define BYTMASK (BYTTAB-1) /* bit mask for byt */
138 #define NBYTS ((CHRBITS+BYTBITS-1)/BYTBITS)
139 /* the definition of GETCOLOR(), below, assumes NBYTS <= 4 */
142 * As soon as possible, we map chrs into equivalence classes -- "colors" --
143 * which are of much more manageable number.
146 typedef short color; /* colors of characters */
147 typedef int pcolor; /* what color promotes to */
148 #define MAX_COLOR SHRT_MAX /* max color value */
149 #define COLORLESS (-1) /* impossible color */
150 #define WHITE 0 /* default color, parent of all others */
153 * A colormap is a tree -- more precisely, a DAG -- indexed at each level by a
154 * byt of the chr, to map the chr to a color efficiently. Because lower
155 * sections of the tree can be shared, it can exploit the usual sparseness of
156 * such a mapping table. The tree is always NBYTS levels deep (in the past it
157 * was shallower during construction but was "filled" to full depth at the end
158 * of that); areas that are unaltered as yet point to "fill blocks" which are
159 * entirely WHITE in color.
162 /* the tree itself */
164 color ccolor[BYTTAB];
167 union tree *pptr[BYTTAB];
170 struct colors colors;
173 #define tcolor colors.ccolor
174 #define tptr ptrs.pptr
176 /* Internal per-color descriptor structure for the color machinery */
178 uchr nchrs; /* number of chars of this color */
179 color sub; /* open subcolor (if any); free chain ptr */
180 #define NOSUB COLORLESS
181 struct arc *arcs; /* color chain */
183 #define FREECOL 01 /* currently free */
184 #define PSEUDO 02 /* pseudocolor, no real chars */
185 #define UNUSEDCOLOR(cd) ((cd)->flags&FREECOL)
186 union tree *block; /* block of solid color, if any */
190 * The color map itself
192 * Much of the data in the colormap struct is only used at compile time.
193 * However, the bulk of the space usage is in the "tree" structure, so it's
194 * not clear that there's much point in converting the rest to a more compact
195 * form when compilation is finished.
199 #define CMMAGIC 0x876
200 struct vars *v; /* for compile error reporting */
201 size_t ncds; /* number of colordescs */
202 size_t max; /* highest in use */
203 color free; /* beginning of free chain (if non-0) */
204 struct colordesc *cd;
205 #define CDEND(cm) (&(cm)->cd[(cm)->max + 1])
206 #define NINLINECDS ((size_t)10)
207 struct colordesc cdspace[NINLINECDS];
208 union tree tree[NBYTS]; /* tree top, plus fill blocks */
211 /* optimization magic to do fast chr->color mapping */
212 #define B0(c) ((c) & BYTMASK)
213 #define B1(c) (((c)>>BYTBITS) & BYTMASK)
214 #define B2(c) (((c)>>(2*BYTBITS)) & BYTMASK)
215 #define B3(c) (((c)>>(3*BYTBITS)) & BYTMASK)
217 #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
219 /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
221 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
224 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
228 * Interface definitions for locale-interface functions in locale.c.
231 /* Representation of a set of characters. */
233 int nchrs; /* number of chrs */
234 int chrspace; /* number of chrs possible */
235 chr *chrs; /* pointer to vector of chrs */
236 int nranges; /* number of ranges (chr pairs) */
237 int rangespace; /* number of chrs possible */
238 chr *ranges; /* pointer to vector of chr pairs */
242 * definitions for non-deterministic finite autmaton (NFA) internal
245 * Having a "from" pointer within each arc may seem redundant, but it saves a
252 int type; /* 0 if free, else an NFA arc type code */
254 struct state *from; /* where it's from (and contained within) */
255 struct state *to; /* where it's to */
256 struct arc *outchain; /* link in *from's outs chain or free chain */
257 struct arc *outchainRev; /* back-link in *from's outs chain */
258 #define freechain outchain /* we do not maintain "freechainRev" */
259 struct arc *inchain; /* *to's ins chain */
260 struct arc *inchainRev; /* back-link in *to's ins chain */
261 struct arc *colorchain; /* color's arc chain */
262 struct arc *colorchainRev; /* back-link in color's arc chain */
265 struct arcbatch { /* for bulk allocation of arcs */
266 struct arcbatch *next;
268 struct arc a[ABSIZE];
273 #define FREESTATE (-1)
274 char flag; /* marks special states */
275 int nins; /* number of inarcs */
276 struct arc *ins; /* chain of inarcs */
277 int nouts; /* number of outarcs */
278 struct arc *outs; /* chain of outarcs */
279 struct arc *free; /* chain of free arcs */
280 struct state *tmp; /* temporary for traversal algorithms */
281 struct state *next; /* chain for traversing all */
282 struct state *prev; /* back chain */
283 struct arcbatch oas; /* first arcbatch, avoid malloc in easy case */
284 int noas; /* number of arcs used in first arcbatch */
288 struct state *pre; /* pre-initial state */
289 struct state *init; /* initial state */
290 struct state *final; /* final state */
291 struct state *post; /* post-final state */
292 int nstates; /* for numbering states */
293 struct state *states; /* state-chain header */
294 struct state *slast; /* tail of the chain */
295 struct state *free; /* free list */
296 struct colormap *cm; /* the color map */
297 color bos[2]; /* colors, if any, assigned to BOS and BOL */
298 color eos[2]; /* colors, if any, assigned to EOS and EOL */
299 struct vars *v; /* simplifies compile error reporting */
300 struct nfa *parent; /* parent NFA, if any */
304 * definitions for compacted NFA
306 * The main space savings in a compacted NFA is from making the arcs as small
307 * as possible. We store only the transition color and next-state number for
308 * each arc. The list of out arcs for each state is an array beginning at
309 * cnfa.states[statenumber], and terminated by a dummy carc struct with
312 * The non-dummy carc structs are of two types: plain arcs and LACON arcs.
313 * Plain arcs just store the transition color number as "co". LACON arcs
314 * store the lookahead constraint number plus cnfa.ncolors as "co". LACON
315 * arcs can be distinguished from plain by testing for co >= cnfa.ncolors.
319 color co; /* COLORLESS is list terminator */
320 int to; /* next-state number */
324 int nstates; /* number of states */
325 int ncolors; /* number of colors */
327 #define HASLACONS 01 /* uses lookahead constraints */
328 int pre; /* setup state number */
329 int post; /* teardown state number */
330 color bos[2]; /* colors, if any, assigned to BOS and BOL */
331 color eos[2]; /* colors, if any, assigned to EOS and EOL */
332 char *stflags; /* vector of per-state flags bytes */
333 #define CNFA_NOPROGRESS 01 /* flag bit for a no-progress state */
334 struct carc **states; /* vector of pointers to outarc lists */
335 /* states[n] are pointers into a single malloc'd array of arcs */
336 struct carc *arcs; /* the area for the lists */
338 #define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
339 #define NULLCNFA(cnfa) ((cnfa).nstates == 0)
342 * This symbol limits the transient heap space used by the regex compiler,
343 * and thereby also the maximum complexity of NFAs that we'll deal with.
344 * Currently we only count NFA states and arcs against this; the other
345 * transient data is generally not large enough to notice compared to those.
346 * Note that we do not charge anything for the final output data structures
347 * (the compacted NFA and the colormap).
349 #ifndef REG_MAX_COMPILE_SPACE
350 #define REG_MAX_COMPILE_SPACE \
351 (100000 * sizeof(struct state) + 100000 * sizeof(struct arcbatch))
358 * '=' plain regex without interesting substructure (implemented as DFA)
359 * 'b' back-reference (has no substructure either)
360 * '(' capture node: captures the match of its single child
361 * '.' concatenation: matches a match for left, then a match for right
362 * '|' alternation: matches a match for left or a match for right
363 * '*' iteration: matches some number of matches of its single child
365 * Note: the right child of an alternation must be another alternation or
366 * NULL; hence, an N-way branch requires N alternation nodes, not N-1 as you
367 * might expect. This could stand to be changed. Actually I'd rather see
368 * a single alternation node with N children, but that will take revising
369 * the representation of struct subre.
371 * Note: when a backref is directly quantified, we stick the min/max counts
372 * into the backref rather than plastering an iteration node on top. This is
373 * for efficiency: there is no need to search for possible division points.
377 char op; /* see type codes above */
379 #define LONGER 01 /* prefers longer match */
380 #define SHORTER 02 /* prefers shorter match */
381 #define MIXED 04 /* mixed preference below */
382 #define CAP 010 /* capturing parens below */
383 #define BACKR 020 /* back reference below */
384 #define INUSE 0100 /* in use in final tree */
385 #define NOPROP 03 /* bits which may not propagate up */
386 #define LMIX(f) ((f)<<2) /* LONGER -> MIXED */
387 #define SMIX(f) ((f)<<1) /* SHORTER -> MIXED */
388 #define UP(f) (((f)&~NOPROP) | (LMIX(f) & SMIX(f) & MIXED))
389 #define MESSY(f) ((f)&(MIXED|CAP|BACKR))
390 #define PREF(f) ((f)&NOPROP)
391 #define PREF2(f1, f2) ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
392 #define COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
393 short id; /* ID of subre (1..ntree-1) */
394 int subno; /* subexpression number (for 'b' and '(') */
395 short min; /* min repetitions for iteration or backref */
396 short max; /* max repetitions for iteration or backref */
397 struct subre *left; /* left child, if any (also freelist chain) */
398 struct subre *right; /* right child, if any */
399 struct state *begin; /* outarcs from here... */
400 struct state *end; /* ...ending in inarcs here */
401 struct cnfa cnfa; /* compacted NFA, if any */
402 struct subre *chain; /* for bookkeeping and error cleanup */
406 * table of function pointers for generic manipulation functions. A regex_t's
407 * re_fns points to one of these.
411 void FUNCPTR(free, (regex_t *));
415 * the insides of a regex_t, hidden behind a void *
420 #define GUTSMAGIC 0xFED9
421 int cflags; /* copy of compile flags */
422 long info; /* copy of re_info */
423 size_t nsub; /* copy of re_nsub */
425 struct cnfa search; /* for fast preliminary search */
426 int ntree; /* number of subre's, plus one */
427 struct colormap cmap;
428 int FUNCPTR(compare, (const chr *, const chr *, size_t));
429 struct subre *lacons; /* lookahead-constraint vector */
430 int nlacons; /* size of lacons */
434 * Magic for allocating a variable workspace. This default version is
439 #define AllocVars(vPtr) \
441 struct vars *vPtr = &var
444 #define FreeVars(vPtr) ((void) 0)