regex2.h revision 1.12 1 1.12 lukem /* $NetBSD: regex2.h,v 1.12 2009/02/12 05:06:54 lukem Exp $ */
2 1.5 cgd
3 1.4 cgd /*-
4 1.4 cgd * Copyright (c) 1992, 1993, 1994
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.10 agc *
7 1.10 agc * This code is derived from software contributed to Berkeley by
8 1.10 agc * Henry Spencer.
9 1.10 agc *
10 1.10 agc * Redistribution and use in source and binary forms, with or without
11 1.10 agc * modification, are permitted provided that the following conditions
12 1.10 agc * are met:
13 1.10 agc * 1. Redistributions of source code must retain the above copyright
14 1.10 agc * notice, this list of conditions and the following disclaimer.
15 1.10 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.10 agc * notice, this list of conditions and the following disclaimer in the
17 1.10 agc * documentation and/or other materials provided with the distribution.
18 1.10 agc * 3. Neither the name of the University nor the names of its contributors
19 1.10 agc * may be used to endorse or promote products derived from this software
20 1.10 agc * without specific prior written permission.
21 1.10 agc *
22 1.10 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.10 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.10 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.10 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.10 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.10 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.10 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.10 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.10 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.10 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.10 agc * SUCH DAMAGE.
33 1.10 agc *
34 1.10 agc * @(#)regex2.h 8.4 (Berkeley) 3/20/94
35 1.10 agc */
36 1.10 agc
37 1.10 agc /*-
38 1.10 agc * Copyright (c) 1992, 1993, 1994 Henry Spencer.
39 1.4 cgd *
40 1.4 cgd * This code is derived from software contributed to Berkeley by
41 1.4 cgd * Henry Spencer.
42 1.4 cgd *
43 1.4 cgd * Redistribution and use in source and binary forms, with or without
44 1.4 cgd * modification, are permitted provided that the following conditions
45 1.4 cgd * are met:
46 1.4 cgd * 1. Redistributions of source code must retain the above copyright
47 1.4 cgd * notice, this list of conditions and the following disclaimer.
48 1.4 cgd * 2. Redistributions in binary form must reproduce the above copyright
49 1.4 cgd * notice, this list of conditions and the following disclaimer in the
50 1.4 cgd * documentation and/or other materials provided with the distribution.
51 1.4 cgd * 3. All advertising materials mentioning features or use of this software
52 1.4 cgd * must display the following acknowledgement:
53 1.4 cgd * This product includes software developed by the University of
54 1.4 cgd * California, Berkeley and its contributors.
55 1.4 cgd * 4. Neither the name of the University nor the names of its contributors
56 1.4 cgd * may be used to endorse or promote products derived from this software
57 1.4 cgd * without specific prior written permission.
58 1.4 cgd *
59 1.4 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.4 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.4 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.4 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.4 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.4 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.4 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.4 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.4 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.4 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.4 cgd * SUCH DAMAGE.
70 1.4 cgd *
71 1.4 cgd * @(#)regex2.h 8.4 (Berkeley) 3/20/94
72 1.4 cgd */
73 1.4 cgd
74 1.1 jtc /*
75 1.1 jtc * First, the stuff that ends up in the outside-world include file
76 1.1 jtc = typedef off_t regoff_t;
77 1.1 jtc = typedef struct {
78 1.1 jtc = int re_magic;
79 1.1 jtc = size_t re_nsub; // number of parenthesized subexpressions
80 1.3 jtc = const char *re_endp; // end pointer for REG_PEND
81 1.1 jtc = struct re_guts *re_g; // none of your business :-)
82 1.1 jtc = } regex_t;
83 1.1 jtc = typedef struct {
84 1.1 jtc = regoff_t rm_so; // start of match
85 1.1 jtc = regoff_t rm_eo; // end of match
86 1.1 jtc = } regmatch_t;
87 1.1 jtc */
88 1.1 jtc /*
89 1.1 jtc * internals of regex_t
90 1.1 jtc */
91 1.1 jtc #define MAGIC1 ((('r'^0200)<<8) | 'e')
92 1.1 jtc
93 1.1 jtc /*
94 1.1 jtc * The internal representation is a *strip*, a sequence of
95 1.1 jtc * operators ending with an endmarker. (Some terminology etc. is a
96 1.1 jtc * historical relic of earlier versions which used multiple strips.)
97 1.1 jtc * Certain oddities in the representation are there to permit running
98 1.1 jtc * the machinery backwards; in particular, any deviation from sequential
99 1.1 jtc * flow must be marked at both its source and its destination. Some
100 1.1 jtc * fine points:
101 1.1 jtc *
102 1.1 jtc * - OPLUS_ and O_PLUS are *inside* the loop they create.
103 1.1 jtc * - OQUEST_ and O_QUEST are *outside* the bypass they create.
104 1.1 jtc * - OCH_ and O_CH are *outside* the multi-way branch they create, while
105 1.1 jtc * OOR1 and OOR2 are respectively the end and the beginning of one of
106 1.1 jtc * the branches. Note that there is an implicit OOR2 following OCH_
107 1.1 jtc * and an implicit OOR1 preceding O_CH.
108 1.1 jtc *
109 1.1 jtc * In state representations, an operator's bit is on to signify a state
110 1.1 jtc * immediately *preceding* "execution" of that operator.
111 1.1 jtc */
112 1.7 christos typedef u_int32_t sop; /* strip operator */
113 1.8 drochner typedef int sopno;
114 1.7 christos #define OPRMASK ((u_int32_t)0xf8000000UL)
115 1.7 christos #define OPDMASK ((u_int32_t)0x07ffffffUL)
116 1.8 drochner #define OPSHIFT ((unsigned)27)
117 1.1 jtc #define OP(n) ((n)&OPRMASK)
118 1.8 drochner #define OPND(n) ((int)((n)&OPDMASK))
119 1.1 jtc #define SOP(op, opnd) ((op)|(opnd))
120 1.8 drochner
121 1.8 drochner #define OPC(n) (((u_int32_t)(n))<<OPSHIFT)
122 1.7 christos /* operators meaning operand */
123 1.7 christos /* (back, fwd are offsets) */
124 1.7 christos #define OEND OPC(1) /* endmarker - */
125 1.7 christos #define OCHAR OPC(2) /* character unsigned char */
126 1.7 christos #define OBOL OPC(3) /* left anchor - */
127 1.7 christos #define OEOL OPC(4) /* right anchor - */
128 1.7 christos #define OANY OPC(5) /* . - */
129 1.7 christos #define OANYOF OPC(6) /* [...] set number */
130 1.7 christos #define OBACK_ OPC(7) /* begin \d paren number */
131 1.7 christos #define O_BACK OPC(8) /* end \d paren number */
132 1.7 christos #define OPLUS_ OPC(9) /* + prefix fwd to suffix */
133 1.7 christos #define O_PLUS OPC(10) /* + suffix back to prefix */
134 1.7 christos #define OQUEST_ OPC(11) /* ? prefix fwd to suffix */
135 1.7 christos #define O_QUEST OPC(12) /* ? suffix back to prefix */
136 1.7 christos #define OLPAREN OPC(13) /* ( fwd to ) */
137 1.7 christos #define ORPAREN OPC(14) /* ) back to ( */
138 1.7 christos #define OCH_ OPC(15) /* begin choice fwd to OOR2 */
139 1.7 christos #define OOR1 OPC(16) /* | pt. 1 back to OOR1 or OCH_ */
140 1.7 christos #define OOR2 OPC(17) /* | pt. 2 fwd to OOR2 or O_CH */
141 1.7 christos #define O_CH OPC(18) /* end choice back to OOR1 */
142 1.7 christos #define OBOW OPC(19) /* begin word - */
143 1.7 christos #define OEOW OPC(20) /* end word - */
144 1.1 jtc
145 1.1 jtc /*
146 1.1 jtc * Structure for [] character-set representation. Character sets are
147 1.1 jtc * done as bit vectors, grouped 8 to a byte vector for compactness.
148 1.1 jtc * The individual set therefore has both a pointer to the byte vector
149 1.1 jtc * and a mask to pick out the relevant bit of each byte. A hash code
150 1.1 jtc * simplifies testing whether two sets could be identical.
151 1.1 jtc *
152 1.1 jtc * This will get trickier for multicharacter collating elements. As
153 1.1 jtc * preliminary hooks for dealing with such things, we also carry along
154 1.1 jtc * a string of multi-character elements, and decide the size of the
155 1.1 jtc * vectors at run time.
156 1.1 jtc */
157 1.1 jtc typedef struct {
158 1.2 jtc uch *ptr; /* -> uch [csetsize] */
159 1.2 jtc uch mask; /* bit within array */
160 1.2 jtc uch hash; /* hash code */
161 1.1 jtc size_t smultis;
162 1.1 jtc char *multis; /* -> char[smulti] ab\0cd\0ef\0\0 */
163 1.1 jtc } cset;
164 1.1 jtc /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
165 1.2 jtc #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
166 1.2 jtc #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
167 1.2 jtc #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask)
168 1.2 jtc #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */
169 1.2 jtc #define MCsub(p, cs, cp) mcsub(p, cs, cp)
170 1.2 jtc #define MCin(p, cs, cp) mcin(p, cs, cp)
171 1.1 jtc
172 1.1 jtc /* stuff for character categories */
173 1.1 jtc typedef unsigned char cat_t;
174 1.1 jtc
175 1.1 jtc /*
176 1.1 jtc * main compiled-expression structure
177 1.1 jtc */
178 1.1 jtc struct re_guts {
179 1.1 jtc int magic;
180 1.1 jtc # define MAGIC2 ((('R'^0200)<<8)|'E')
181 1.1 jtc sop *strip; /* malloced area for strip */
182 1.1 jtc int csetsize; /* number of bits in a cset vector */
183 1.1 jtc int ncsets; /* number of csets in use */
184 1.1 jtc cset *sets; /* -> cset [ncsets] */
185 1.2 jtc uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */
186 1.1 jtc int cflags; /* copy of regcomp() cflags argument */
187 1.1 jtc sopno nstates; /* = number of sops */
188 1.1 jtc sopno firststate; /* the initial OEND (normally 0) */
189 1.1 jtc sopno laststate; /* the final OEND */
190 1.1 jtc int iflags; /* internal flags */
191 1.1 jtc # define USEBOL 01 /* used ^ */
192 1.1 jtc # define USEEOL 02 /* used $ */
193 1.1 jtc # define BAD 04 /* something wrong */
194 1.1 jtc int nbol; /* number of ^ used */
195 1.1 jtc int neol; /* number of $ used */
196 1.1 jtc int ncategories; /* how many character categories */
197 1.1 jtc cat_t *categories; /* ->catspace[-CHAR_MIN] */
198 1.1 jtc char *must; /* match must contain this string */
199 1.1 jtc int mlen; /* length of must */
200 1.12 lukem size_t nsub; /* copy of re_nsub */
201 1.1 jtc int backrefs; /* does it use back references? */
202 1.1 jtc sopno nplus; /* how deep does it nest +s? */
203 1.1 jtc /* catspace must be last */
204 1.1 jtc cat_t catspace[1]; /* actually [NC] */
205 1.1 jtc };
206 1.1 jtc
207 1.1 jtc /* misc utilities */
208 1.1 jtc #define OUT (CHAR_MAX+1) /* a non-character value */
209 1.11 enami #define ISWORD(c) (isalnum((unsigned char)c) || (c) == '_')
210