56bb1f953341c41e9ad536e290c54f17b5c71463
[debian/amanda] / regex-src / regex2.h
1 /*
2  * First, the stuff that ends up in the outside-world include file
3  = typedef off_t regoff_t;
4  = typedef struct {
5  =      int re_magic;
6  =      size_t re_nsub;         // number of parenthesized subexpressions
7  =      const char *re_endp;    // end pointer for REG_PEND
8  =      struct re_guts *re_g;   // none of your business :-)
9  = } regex_t;
10  = typedef struct {
11  =      regoff_t rm_so;         // start of match
12  =      regoff_t rm_eo;         // end of match
13  = } regmatch_t;
14  */
15 /*
16  * internals of regex_t
17  */
18 #define MAGIC1  ((('r'^0200)<<8) | 'e')
19
20 /*
21  * The internal representation is a *strip*, a sequence of
22  * operators ending with an endmarker.  (Some terminology etc. is a
23  * historical relic of earlier versions which used multiple strips.)
24  * Certain oddities in the representation are there to permit running
25  * the machinery backwards; in particular, any deviation from sequential
26  * flow must be marked at both its source and its destination.  Some
27  * fine points:
28  *
29  * - OPLUS_ and O_PLUS are *inside* the loop they create.
30  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
31  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
32  *   OOR1 and OOR2 are respectively the end and the beginning of one of
33  *   the branches.  Note that there is an implicit OOR2 following OCH_
34  *   and an implicit OOR1 preceding O_CH.
35  *
36  * In state representations, an operator's bit is on to signify a state
37  * immediately *preceding* "execution" of that operator.
38  */
39 typedef unsigned long sop;      /* strip operator */
40 typedef long sopno;
41 #define OPRMASK 0xf8000000
42 #define OPDMASK 0x07ffffff
43 #define OPSHIFT ((unsigned)27)
44 #define OP(n)   ((n)&OPRMASK)
45 #define OPND(n) ((n)&OPDMASK)
46 #define SOP(op, opnd)   ((op)|(opnd))
47
48 #ifndef UNSIGNEDLONG1
49 #ifndef NO_UL_CNSTS
50 #define UNSIGNEDLONG1 1ul
51 #else
52 #define UNSIGNEDLONG1 ((unsigned long)1)
53 #endif
54 #endif
55
56 #define MAKE_UNSIGNED_LONG(num) (UNSIGNEDLONG1*num)
57 #define SHIFTED_UL(num) (MAKE_UNSIGNED_LONG(num) << OPSHIFT)
58
59 /* operators                       meaning      operand                 */
60 /*                                              (back, fwd are offsets) */
61 #define OEND    (SHIFTED_UL(1)) /* endmarker    -                       */
62 #define OCHAR   (SHIFTED_UL(2)) /* character    unsigned char           */
63 #define OBOL    (SHIFTED_UL(3)) /* left anchor  -                       */
64 #define OEOL    (SHIFTED_UL(4)) /* right anchor -                       */
65 #define OANY    (SHIFTED_UL(5)) /* .            -                       */
66 #define OANYOF  (SHIFTED_UL(6)) /* [...]        set number              */
67 #define OBACK_  (SHIFTED_UL(7)) /* begin \d     paren number            */
68 #define O_BACK  (SHIFTED_UL(8)) /* end \d       paren number            */
69 #define OPLUS_  (SHIFTED_UL(9)) /* + prefix     fwd to suffix           */
70 #define O_PLUS  (SHIFTED_UL(10))        /* + suffix     back to prefix          */
71 #define OQUEST_ (SHIFTED_UL(11))        /* ? prefix     fwd to suffix           */
72 #define O_QUEST (SHIFTED_UL(12))        /* ? suffix     back to prefix          */
73 #define OLPAREN (SHIFTED_UL(13))        /* (            fwd to )                */
74 #define ORPAREN (SHIFTED_UL(14))        /* )            back to (               */
75 #define OCH_    (SHIFTED_UL(15))        /* begin choice fwd to OOR2             */
76 #define OOR1    (SHIFTED_UL(16))        /* | pt. 1      back to OOR1 or OCH_    */
77 #define OOR2    (SHIFTED_UL(17))        /* | pt. 2      fwd to OOR2 or O_CH     */
78 #define O_CH    (SHIFTED_UL(18))        /* end choice   back to OOR1            */
79 #define OBOW    (SHIFTED_UL(19))        /* begin word   -                       */
80 #define OEOW    (SHIFTED_UL(20))        /* end word     -                       */
81
82 /*
83  * Structure for [] character-set representation.  Character sets are
84  * done as bit vectors, grouped 8 to a byte vector for compactness.
85  * The individual set therefore has both a pointer to the byte vector
86  * and a mask to pick out the relevant bit of each byte.  A hash code
87  * simplifies testing whether two sets could be identical.
88  *
89  * This will get trickier for multicharacter collating elements.  As
90  * preliminary hooks for dealing with such things, we also carry along
91  * a string of multi-character elements, and decide the size of the
92  * vectors at run time.
93  */
94 typedef struct {
95         uch *ptr;               /* -> uch [csetsize] */
96         uch mask;               /* bit within array */
97         uch hash;               /* hash code */
98         size_t smultis;
99         char *multis;           /* -> char[smulti]  ab\0cd\0ef\0\0 */
100 } cset;
101 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
102 #define CHadd(cs, c)    ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
103 #define CHsub(cs, c)    ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
104 #define CHIN(cs, c)     ((cs)->ptr[(uch)(c)] & (cs)->mask)
105 #define MCadd(p, cs, cp)        mcadd(p, cs, cp)        /* regcomp() internal fns */
106 #define MCsub(p, cs, cp)        mcsub(p, cs, cp)
107 #define MCin(p, cs, cp) mcin(p, cs, cp)
108
109 /* stuff for character categories */
110 typedef unsigned char cat_t;
111
112 /*
113  * main compiled-expression structure
114  */
115 struct re_guts {
116         int magic;
117 #               define  MAGIC2  ((('R'^0200)<<8)|'E')
118         sop *strip;             /* malloced area for strip */
119         int csetsize;           /* number of bits in a cset vector */
120         int ncsets;             /* number of csets in use */
121         cset *sets;             /* -> cset [ncsets] */
122         uch *setbits;           /* -> uch[csetsize][ncsets/CHAR_BIT] */
123         int cflags;             /* copy of regcomp() cflags argument */
124         sopno nstates;          /* = number of sops */
125         sopno firststate;       /* the initial OEND (normally 0) */
126         sopno laststate;        /* the final OEND */
127         int iflags;             /* internal flags */
128 #               define  USEBOL  01      /* used ^ */
129 #               define  USEEOL  02      /* used $ */
130 #               define  BAD     04      /* something wrong */
131         int nbol;               /* number of ^ used */
132         int neol;               /* number of $ used */
133         int ncategories;        /* how many character categories */
134         cat_t *categories;      /* ->catspace[-CHAR_MIN] */
135         char *must;             /* match must contain this string */
136         int mlen;               /* length of must */
137         size_t nsub;            /* copy of re_nsub */
138         int backrefs;           /* does it use back references? */
139         sopno nplus;            /* how deep does it nest +s? */
140         /* catspace must be last */
141         cat_t catspace[1];      /* actually [NC] */
142 };
143
144 /* misc utilities */
145 #define OUT     (CHAR_MAX+1)    /* a non-character value */
146 #define ISWORD(c)       (isalnum(c) || (c) == '_')