]> granicus.if.org Git - postgresql/blob - src/include/regex/regguts.h
Remove cvs keywords from all files.
[postgresql] / src / include / regex / regguts.h
1 /*
2  * Internal interface definitions, etc., for the reg package
3  *
4  * Copyright (c) 1998, 1999 Henry Spencer.      All rights reserved.
5  *
6  * Development of this software was funded, in part, by Cray Research Inc.,
7  * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
8  * Corporation, none of whom are responsible for the results.  The author
9  * thanks all of them.
10  *
11  * Redistribution and use in source and binary forms -- with or without
12  * modification -- are permitted for any purpose, provided that
13  * redistributions in source form retain this entire copyright notice and
14  * indicate the origin and nature of any modifications.
15  *
16  * I'd appreciate being given credit for this package in the documentation
17  * of software which uses it, but that is not a requirement.
18  *
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
22  * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * src/include/regex/regguts.h
31  */
32
33
34
35 /*
36  * Environmental customization.  It should not (I hope) be necessary to
37  * alter the file you are now reading -- regcustom.h should handle it all,
38  * given care here and elsewhere.
39  */
40 #include "regcustom.h"
41
42
43
44 /*
45  * Things that regcustom.h might override.
46  */
47
48 /* assertions */
49 #ifndef assert
50 #ifndef REG_DEBUG
51 #define  NDEBUG                                 /* no assertions */
52 #endif
53 #include <assert.h>
54 #endif
55
56 /* voids */
57 #ifndef DISCARD
58 #define DISCARD void                    /* for throwing values away */
59 #endif
60 #ifndef VS
61 #define VS(x)   ((void *)(x))   /* cast something to generic ptr */
62 #endif
63
64 /* function-pointer declarator */
65 #ifndef FUNCPTR
66 #define FUNCPTR(name, args) (*(name)) args
67 #endif
68
69 /* memory allocation */
70 #ifndef MALLOC
71 #define MALLOC(n)       malloc(n)
72 #endif
73 #ifndef REALLOC
74 #define REALLOC(p, n)   realloc(VS(p), n)
75 #endif
76 #ifndef FREE
77 #define FREE(p)         free(VS(p))
78 #endif
79
80 /* want size of a char in bits, and max value in bounded quantifiers */
81 #ifndef CHAR_BIT
82 #include <limits.h>
83 #endif
84 #ifndef _POSIX2_RE_DUP_MAX
85 #define _POSIX2_RE_DUP_MAX      255 /* normally from <limits.h> */
86 #endif
87
88
89
90 /*
91  * misc
92  */
93
94 #define NOTREACHED      0
95 #define xxx             1
96
97 #define DUPMAX  _POSIX2_RE_DUP_MAX
98 #define INFINITY        (DUPMAX+1)
99
100 #define REMAGIC 0xfed7                  /* magic number for main struct */
101
102
103
104 /*
105  * debugging facilities
106  */
107 #ifdef REG_DEBUG
108 /* FDEBUG does finite-state tracing */
109 #define FDEBUG(arglist) { if (v->eflags&REG_FTRACE) printf arglist; }
110 /* MDEBUG does higher-level tracing */
111 #define MDEBUG(arglist) { if (v->eflags&REG_MTRACE) printf arglist; }
112 #else
113 #define FDEBUG(arglist) {}
114 #define MDEBUG(arglist) {}
115 #endif
116
117
118
119 /*
120  * bitmap manipulation
121  */
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)))
125
126
127
128 /*
129  * We dissect a chr into byts for colormap table indexing.      Here we define
130  * a byt, which will be the same as a byte on most machines...  The exact
131  * size of a byt is not critical, but about 8 bits is good, and extraction
132  * of 8-bit chunks is sometimes especially fast.
133  */
134 #ifndef BYTBITS
135 #define BYTBITS 8                               /* bits in a byt */
136 #endif
137 #define BYTTAB  (1<<BYTBITS)    /* size of table with one entry per byt value */
138 #define BYTMASK (BYTTAB-1)              /* bit mask for byt */
139 #define NBYTS   ((CHRBITS+BYTBITS-1)/BYTBITS)
140 /* the definition of GETCOLOR(), below, assumes NBYTS <= 4 */
141
142
143
144 /*
145  * As soon as possible, we map chrs into equivalence classes -- "colors" --
146  * which are of much more manageable number.
147  */
148 typedef short color;                    /* colors of characters */
149 typedef int pcolor;                             /* what color promotes to */
150
151 #define COLORLESS       (-1)            /* impossible color */
152 #define WHITE           0                       /* default color, parent of all others */
153
154
155
156 /*
157  * A colormap is a tree -- more precisely, a DAG -- indexed at each level
158  * by a byt of the chr, to map the chr to a color efficiently.  Because
159  * lower sections of the tree can be shared, it can exploit the usual
160  * sparseness of such a mapping table.  The tree is always NBYTS levels
161  * deep (in the past it was shallower during construction but was "filled"
162  * to full depth at the end of that); areas that are unaltered as yet point
163  * to "fill blocks" which are entirely WHITE in color.
164  */
165
166 /* the tree itself */
167 struct colors
168 {
169         color           ccolor[BYTTAB];
170 };
171 struct ptrs
172 {
173         union tree *pptr[BYTTAB];
174 };
175 union tree
176 {
177         struct colors colors;
178         struct ptrs ptrs;
179 };
180
181 #define tcolor  colors.ccolor
182 #define tptr    ptrs.pptr
183
184 /* internal per-color descriptor structure for the color machinery */
185 struct colordesc
186 {
187         uchr            nchrs;                  /* number of chars of this color */
188         color           sub;                    /* open subcolor (if any); free chain ptr */
189 #define  NOSUB   COLORLESS
190         struct arc *arcs;                       /* color chain */
191         int                     flags;
192 #define  FREECOL 01                             /* currently free */
193 #define  PSEUDO  02                             /* pseudocolor, no real chars */
194 #define  UNUSEDCOLOR(cd) ((cd)->flags&FREECOL)
195         union tree *block;                      /* block of solid color, if any */
196 };
197
198 /* the color map itself */
199 struct colormap
200 {
201         int                     magic;
202 #define  CMMAGIC 0x876
203         struct vars *v;                         /* for compile error reporting */
204         size_t          ncds;                   /* number of colordescs */
205         size_t          max;                    /* highest in use */
206         color           free;                   /* beginning of free chain (if non-0) */
207         struct colordesc *cd;
208 #define  CDEND(cm)       (&(cm)->cd[(cm)->max + 1])
209 #define  NINLINECDS  ((size_t)10)
210         struct colordesc cdspace[NINLINECDS];
211         union tree      tree[NBYTS];    /* tree top, plus fill blocks */
212 };
213
214 /* optimization magic to do fast chr->color mapping */
215 #define B0(c)   ((c) & BYTMASK)
216 #define B1(c)   (((c)>>BYTBITS) & BYTMASK)
217 #define B2(c)   (((c)>>(2*BYTBITS)) & BYTMASK)
218 #define B3(c)   (((c)>>(3*BYTBITS)) & BYTMASK)
219 #if NBYTS == 1
220 #define GETCOLOR(cm, c) ((cm)->tree->tcolor[B0(c)])
221 #endif
222 /* beware, for NBYTS>1, GETCOLOR() is unsafe -- 2nd arg used repeatedly */
223 #if NBYTS == 2
224 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B1(c)]->tcolor[B0(c)])
225 #endif
226 #if NBYTS == 4
227 #define GETCOLOR(cm, c) ((cm)->tree->tptr[B3(c)]->tptr[B2(c)]->tptr[B1(c)]->tcolor[B0(c)])
228 #endif
229
230
231 /*
232  * Interface definitions for locale-interface functions in locale.c.
233  */
234
235 /* Representation of a set of characters. */
236 struct cvec
237 {
238         int                     nchrs;                  /* number of chrs */
239         int                     chrspace;               /* number of chrs possible */
240         chr                *chrs;                       /* pointer to vector of chrs */
241         int                     nranges;                /* number of ranges (chr pairs) */
242         int                     rangespace;             /* number of chrs possible */
243         chr                *ranges;                     /* pointer to vector of chr pairs */
244         /* both batches of chrs are on the end */
245 };
246
247
248 /*
249  * definitions for NFA internal representation
250  *
251  * Having a "from" pointer within each arc may seem redundant, but it
252  * saves a lot of hassle.
253  */
254 struct state;
255
256 struct arc
257 {
258         int                     type;
259 #define  ARCFREE '\0'
260         color           co;
261         struct state *from;                     /* where it's from (and contained within) */
262         struct state *to;                       /* where it's to */
263         struct arc *outchain;           /* *from's outs chain or free chain */
264 #define  freechain       outchain
265         struct arc *inchain;            /* *to's ins chain */
266         struct arc *colorchain;         /* color's arc chain */
267         struct arc *colorchainRev;      /* back-link in color's arc chain */
268 };
269
270 struct arcbatch
271 {                                                               /* for bulk allocation of arcs */
272         struct arcbatch *next;
273 #define  ABSIZE  10
274         struct arc      a[ABSIZE];
275 };
276
277 struct state
278 {
279         int                     no;
280 #define  FREESTATE       (-1)
281         char            flag;                   /* marks special states */
282         int                     nins;                   /* number of inarcs */
283         struct arc *ins;                        /* chain of inarcs */
284         int                     nouts;                  /* number of outarcs */
285         struct arc *outs;                       /* chain of outarcs */
286         struct arc *free;                       /* chain of free arcs */
287         struct state *tmp;                      /* temporary for traversal algorithms */
288         struct state *next;                     /* chain for traversing all */
289         struct state *prev;                     /* back chain */
290         struct arcbatch oas;            /* first arcbatch, avoid malloc in easy case */
291         int                     noas;                   /* number of arcs used in first arcbatch */
292 };
293
294 struct nfa
295 {
296         struct state *pre;                      /* pre-initial state */
297         struct state *init;                     /* initial state */
298         struct state *final;            /* final state */
299         struct state *post;                     /* post-final state */
300         int                     nstates;                /* for numbering states */
301         struct state *states;           /* state-chain header */
302         struct state *slast;            /* tail of the chain */
303         struct state *free;                     /* free list */
304         struct colormap *cm;            /* the color map */
305         color           bos[2];                 /* colors, if any, assigned to BOS and BOL */
306         color           eos[2];                 /* colors, if any, assigned to EOS and EOL */
307         size_t          size;                   /* Current NFA size; differs from nstates as
308                                                                  * it also counts the number of states created
309                                                                  * by children of this state. */
310         struct vars *v;                         /* simplifies compile error reporting */
311         struct nfa *parent;                     /* parent NFA, if any */
312 };
313
314
315
316 /*
317  * definitions for compacted NFA
318  */
319 struct carc
320 {
321         color           co;                             /* COLORLESS is list terminator */
322         int                     to;                             /* state number */
323 };
324
325 struct cnfa
326 {
327         int                     nstates;                /* number of states */
328         int                     ncolors;                /* number of colors */
329         int                     flags;
330 #define  HASLACONS       01                     /* uses lookahead constraints */
331         int                     pre;                    /* setup state number */
332         int                     post;                   /* teardown state number */
333         color           bos[2];                 /* colors, if any, assigned to BOS and BOL */
334         color           eos[2];                 /* colors, if any, assigned to EOS and EOL */
335         struct carc **states;           /* vector of pointers to outarc lists */
336         struct carc *arcs;                      /* the area for the lists */
337 };
338
339 #define ZAPCNFA(cnfa)   ((cnfa).nstates = 0)
340 #define NULLCNFA(cnfa)  ((cnfa).nstates == 0)
341
342 /*
343  * Used to limit the maximum NFA size to something sane. [Tcl Bug 1810264]
344  */
345 #ifndef REG_MAX_STATES
346 #define REG_MAX_STATES  100000
347 #endif
348
349 /*
350  * subexpression tree
351  */
352 struct subre
353 {
354         char            op;                             /* '|', '.' (concat), 'b' (backref), '(', '=' */
355         char            flags;
356 #define  LONGER  01                             /* prefers longer match */
357 #define  SHORTER 02                             /* prefers shorter match */
358 #define  MIXED   04                             /* mixed preference below */
359 #define  CAP 010                                /* capturing parens below */
360 #define  BACKR   020                    /* back reference below */
361 #define  INUSE   0100                   /* in use in final tree */
362 #define  LOCAL   03                             /* bits which may not propagate up */
363 #define  LMIX(f) ((f)<<2)               /* LONGER -> MIXED */
364 #define  SMIX(f) ((f)<<1)               /* SHORTER -> MIXED */
365 #define  UP(f)   (((f)&~LOCAL) | (LMIX(f) & SMIX(f) & MIXED))
366 #define  MESSY(f)        ((f)&(MIXED|CAP|BACKR))
367 #define  PREF(f) ((f)&LOCAL)
368 #define  PREF2(f1, f2)   ((PREF(f1) != 0) ? PREF(f1) : PREF(f2))
369 #define  COMBINE(f1, f2) (UP((f1)|(f2)) | PREF2(f1, f2))
370         short           retry;                  /* index into retry memory */
371         int                     subno;                  /* subexpression number (for 'b' and '(') */
372         short           min;                    /* min repetitions, for backref only */
373         short           max;                    /* max repetitions, for backref only */
374         struct subre *left;                     /* left child, if any (also freelist chain) */
375         struct subre *right;            /* right child, if any */
376         struct state *begin;            /* outarcs from here... */
377         struct state *end;                      /* ...ending in inarcs here */
378         struct cnfa cnfa;                       /* compacted NFA, if any */
379         struct subre *chain;            /* for bookkeeping and error cleanup */
380 };
381
382
383
384 /*
385  * table of function pointers for generic manipulation functions
386  * A regex_t's re_fns points to one of these.
387  */
388 struct fns
389 {
390         void            FUNCPTR(free, (regex_t *));
391 };
392
393
394
395 /*
396  * the insides of a regex_t, hidden behind a void *
397  */
398 struct guts
399 {
400         int                     magic;
401 #define  GUTSMAGIC       0xfed9
402         int                     cflags;                 /* copy of compile flags */
403         long            info;                   /* copy of re_info */
404         size_t          nsub;                   /* copy of re_nsub */
405         struct subre *tree;
406         struct cnfa search;                     /* for fast preliminary search */
407         int                     ntree;
408         struct colormap cmap;
409         int                     FUNCPTR(compare, (const chr *, const chr *, size_t));
410         struct subre *lacons;           /* lookahead-constraint vector */
411         int                     nlacons;                /* size of lacons */
412 };