]> git.ipfire.org Git - thirdparty/squid.git/blob - compat/GnuRegex.h
Merge from trunk
[thirdparty/squid.git] / compat / GnuRegex.h
1 /*
2 * $Id$
3 */
4 #ifndef SQUID_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #ifndef SQUID_REGEXP_LIBRARY_H
9 #define SQUID_REGEXP_LIBRARY_H
10
11 #if !USE_GNUREGEX /* try the system one by default */
12
13 /* POSIX says that <sys/types.h> must be included (by the caller) before
14 * <regex.h>. */
15 #if HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18 #if HAVE_REGEX_H
19 #include <regex.h>
20 #endif
21
22
23 #else /* USE_GNUREGEX */
24
25 #ifdef __cplusplus
26 extern "C"
27 {
28 #endif
29
30 /* Definitions for data structures and routines for the regular
31 * expression library, version 0.12.
32 *
33 * Copyright (C) 1985, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2, or (at your option)
38 * any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License
46 * along with this program; if not, write to the Free Software
47 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. */
48
49 /* POSIX says that <sys/types.h> must be included (by the caller) before
50 * <regex.h>. */
51
52 #ifdef VMS
53 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
54 * should be there. */
55 #include <stddef.h>
56 #endif
57
58
59 /* The following bits are used to determine the regexp syntax we
60 * recognize. The set/not-set meanings are chosen so that Emacs syntax
61 * remains the value 0. The bits are given in alphabetical order, and
62 * the definitions shifted by one from the previous bit; thus, when we
63 * add or remove a bit, only one other definition need change. */
64 typedef unsigned reg_syntax_t;
65
66 /* If this bit is not set, then \ inside a bracket expression is literal.
67 * If set, then such a \ quotes the following character. */
68 #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
69
70 /* If this bit is not set, then + and ? are operators, and \+ and \? are
71 * literals.
72 * If set, then \+ and \? are operators and + and ? are literals. */
73 #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
74
75 /* If this bit is set, then character classes are supported. They are:
76 * [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
77 * [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
78 * If not set, then character classes are not supported. */
79 #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
80
81 /* If this bit is set, then ^ and $ are always anchors (outside bracket
82 * expressions, of course).
83 * If this bit is not set, then it depends:
84 * ^ is an anchor if it is at the beginning of a regular
85 * expression or after an open-group or an alternation operator;
86 * $ is an anchor if it is at the end of a regular expression, or
87 * before a close-group or an alternation operator.
88 *
89 * This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
90 * POSIX draft 11.2 says that * etc. in leading positions is undefined.
91 * We already implemented a previous draft which made those constructs
92 * invalid, though, so we haven't changed the code back. */
93 #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
94
95 /* If this bit is set, then special characters are always special
96 * regardless of where they are in the pattern.
97 * If this bit is not set, then special characters are special only in
98 * some contexts; otherwise they are ordinary. Specifically,
99 * * + ? and intervals are only special when not after the beginning,
100 * open-group, or alternation operator. */
101 #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
102
103 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
104 * immediately after an alternation or begin-group operator. */
105 #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
106
107 /* If this bit is set, then . matches newline.
108 * If not set, then it doesn't. */
109 #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
110
111 /* If this bit is set, then . doesn't match NUL.
112 * If not set, then it does. */
113 #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
114
115 /* If this bit is set, nonmatching lists [^...] do not match newline.
116 * If not set, they do. */
117 #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
118
119 /* If this bit is set, either \{...\} or {...} defines an
120 * interval, depending on RE_NO_BK_BRACES.
121 * If not set, \{, \}, {, and } are literals. */
122 #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
123
124 /* If this bit is set, +, ? and | aren't recognized as operators.
125 * If not set, they are. */
126 #define RE_LIMITED_OPS (RE_INTERVALS << 1)
127
128 /* If this bit is set, newline is an alternation operator.
129 * If not set, newline is literal. */
130 #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
131
132 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
133 * are literals.
134 * If not set, then `\{...\}' defines an interval. */
135 #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
136
137 /* If this bit is set, (...) defines a group, and \( and \) are literals.
138 * If not set, \(...\) defines a group, and ( and ) are literals. */
139 #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
140
141 /* If this bit is set, then \<digit> matches <digit>.
142 * If not set, then \<digit> is a back-reference. */
143 #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
144
145 /* If this bit is set, then | is an alternation operator, and \| is literal.
146 * If not set, then \| is an alternation operator, and | is literal. */
147 #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
148
149 /* If this bit is set, then an ending range point collating higher
150 * than the starting range point, as in [z-a], is invalid.
151 * If not set, then when ending range point collates higher than the
152 * starting range point, the range is ignored. */
153 #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
154
155 /* If this bit is set, then an unmatched ) is ordinary.
156 * If not set, then an unmatched ) is invalid. */
157 #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
158
159 \f
160 /* Define combinations of the above bits for the standard possibilities.
161 * (The [[[ comments delimit what gets put into the Texinfo file, so
162 * don't delete them!) */
163 /* [[[begin syntaxes]]] */
164 #define RE_SYNTAX_EMACS 0
165
166 #define RE_SYNTAX_AWK \
167 (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
168 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
169 | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
170 | RE_UNMATCHED_RIGHT_PAREN_ORD)
171
172 #define RE_SYNTAX_POSIX_AWK \
173 (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
174
175 #define RE_SYNTAX_GREP \
176 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
177 | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
178 | RE_NEWLINE_ALT)
179
180 #define RE_SYNTAX_EGREP \
181 (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
182 | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
183 | RE_NEWLINE_ALT | RE_NO_BK_PARENS \
184 | RE_NO_BK_VBAR)
185
186 #define RE_SYNTAX_POSIX_EGREP \
187 (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
188
189 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
190 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
191
192 #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
193
194 /* Syntax bits common to both basic and extended POSIX regex syntax. */
195 #define _RE_SYNTAX_POSIX_COMMON \
196 (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
197 | RE_INTERVALS | RE_NO_EMPTY_RANGES)
198
199 #define RE_SYNTAX_POSIX_BASIC \
200 (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
201
202 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
203 * RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
204 * isn't minimal, since other operators, such as \`, aren't disabled. */
205 #define RE_SYNTAX_POSIX_MINIMAL_BASIC \
206 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
207
208 #define RE_SYNTAX_POSIX_EXTENDED \
209 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
210 | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
211 | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
212 | RE_UNMATCHED_RIGHT_PAREN_ORD)
213
214 /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
215 * replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
216 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
217 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
218 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
219 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
220 | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
221 /* [[[end syntaxes]]] */
222 \f
223 /* Maximum number of duplicates an interval can allow. Some systems
224 * (erroneously) define this in other header files, but we want our
225 * value, so remove any previous define. */
226 #ifdef RE_DUP_MAX
227 #undef RE_DUP_MAX
228 #endif
229 #define RE_DUP_MAX ((1 << 15) - 1)
230
231
232 /* POSIX `cflags' bits (i.e., information for `regcomp'). */
233
234 /* If this bit is set, then use extended regular expression syntax.
235 * If not set, then use basic regular expression syntax. */
236 #define REG_EXTENDED 1
237
238 /* If this bit is set, then ignore case when matching.
239 * If not set, then case is significant. */
240 #define REG_ICASE (REG_EXTENDED << 1)
241
242 /* If this bit is set, then anchors do not match at newline
243 * characters in the string.
244 * If not set, then anchors do match at newlines. */
245 #define REG_NEWLINE (REG_ICASE << 1)
246
247 /* If this bit is set, then report only success or fail in regexec.
248 * If not set, then returns differ between not matching and errors. */
249 #define REG_NOSUB (REG_NEWLINE << 1)
250
251
252 /* POSIX `eflags' bits (i.e., information for regexec). */
253
254 /* If this bit is set, then the beginning-of-line operator doesn't match
255 * the beginning of the string (presumably because it's not the
256 * beginning of a line).
257 * If not set, then the beginning-of-line operator does match the
258 * beginning of the string. */
259 #define REG_NOTBOL 1
260
261 /* Like REG_NOTBOL, except for the end-of-line. */
262 #define REG_NOTEOL (1 << 1)
263
264
265 /* If any error codes are removed, changed, or added, update the
266 * `re_error_msg' table in regex.c. */
267 typedef enum {
268 REG_NOERROR = 0, /* Success. */
269 REG_NOMATCH, /* Didn't find a match (for regexec). */
270
271 /* POSIX regcomp return error codes. (In the order listed in the
272 * standard.) */
273 REG_BADPAT, /* Invalid pattern. */
274 REG_ECOLLATE, /* Not implemented. */
275 REG_ECTYPE, /* Invalid character class name. */
276 REG_EESCAPE, /* Trailing backslash. */
277 REG_ESUBREG, /* Invalid back reference. */
278 REG_EBRACK, /* Unmatched left bracket. */
279 REG_EPAREN, /* Parenthesis imbalance. */
280 REG_EBRACE, /* Unmatched \{. */
281 REG_BADBR, /* Invalid contents of \{\}. */
282 REG_ERANGE, /* Invalid range end. */
283 REG_ESPACE, /* Ran out of memory. */
284 REG_BADRPT, /* No preceding re for repetition op. */
285
286 /* Error codes we've added. */
287 REG_EEND, /* Premature end. */
288 REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
289 REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
290 } reg_errcode_t;
291 \f
292 /* This data structure represents a compiled pattern. Before calling
293 * the pattern compiler, the fields `buffer', `allocated', `fastmap',
294 * `translate', and `no_sub' can be set. After the pattern has been
295 * compiled, the `re_nsub' field is available. All other fields are
296 * private to the regex routines. */
297
298 struct re_pattern_buffer {
299 /* [[[begin pattern_buffer]]] */
300 /* Space that holds the compiled pattern. It is declared as
301 * `unsigned char *' because its elements are
302 * sometimes used as array indexes. */
303 unsigned char *buffer;
304
305 /* Number of bytes to which `buffer' points. */
306 unsigned long allocated;
307
308 /* Number of bytes actually used in `buffer'. */
309 unsigned long used;
310
311 /* Syntax setting with which the pattern was compiled. */
312 reg_syntax_t syntax;
313
314 /* Pointer to a fastmap, if any, otherwise zero. re_search uses
315 * the fastmap, if there is one, to skip over impossible
316 * starting points for matches. */
317 char *fastmap;
318
319 /* Either a translate table to apply to all characters before
320 * comparing them, or zero for no translation. The translation
321 * is applied to a pattern when it is compiled and to a string
322 * when it is matched. */
323 char *translate;
324
325 /* Number of subexpressions found by the compiler. */
326 size_t re_nsub;
327
328 /* Zero if this pattern cannot match the empty string, one else.
329 * Well, in truth it's used only in `re_search_2', to see
330 * whether or not we should use the fastmap, so we don't set
331 * this absolutely perfectly; see `re_compile_fastmap' (the
332 * `duplicate' case). */
333 unsigned can_be_null:1;
334
335 /* If REGS_UNALLOCATED, allocate space in the `regs' structure
336 * for `max (RE_NREGS, re_nsub + 1)' groups.
337 * If REGS_REALLOCATE, reallocate space if necessary.
338 * If REGS_FIXED, use what's there. */
339 #define REGS_UNALLOCATED 0
340 #define REGS_REALLOCATE 1
341 #define REGS_FIXED 2
342 unsigned regs_allocated:2;
343
344 /* Set to zero when `regex_compile' compiles a pattern; set to one
345 * by `re_compile_fastmap' if it updates the fastmap. */
346 unsigned fastmap_accurate:1;
347
348 /* If set, `re_match_2' does not return information about
349 * subexpressions. */
350 unsigned no_sub:1;
351
352 /* If set, a beginning-of-line anchor doesn't match at the
353 * beginning of the string. */
354 unsigned not_bol:1;
355
356 /* Similarly for an end-of-line anchor. */
357 unsigned not_eol:1;
358
359 /* If true, an anchor at a newline matches. */
360 unsigned newline_anchor:1;
361
362 /* [[[end pattern_buffer]]] */
363 };
364
365 typedef struct re_pattern_buffer regex_t;
366
367
368 /* search.c (search_buffer) in Emacs needs this one opcode value. It is
369 * defined both in `regex.c' and here. */
370 #define RE_EXACTN_VALUE 1
371 \f
372 /* Type for byte offsets within the string. POSIX mandates this. */
373 typedef int regoff_t;
374
375
376 /* This is the structure we store register match data in. See
377 * regex.texinfo for a full description of what registers match. */
378 struct re_registers {
379 unsigned num_regs;
380 regoff_t *start;
381 regoff_t *end;
382 };
383
384
385 /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
386 * `re_match_2' returns information about at least this many registers
387 * the first time a `regs' structure is passed. */
388 #ifndef RE_NREGS
389 #define RE_NREGS 30
390 #endif
391
392
393 /* POSIX specification for registers. Aside from the different names than
394 * `re_registers', POSIX uses an array of structures, instead of a
395 * structure of arrays. */
396 typedef struct {
397 regoff_t rm_so; /* Byte offset from string's start to substring's start. */
398 regoff_t rm_eo; /* Byte offset from string's start to substring's end. */
399 } regmatch_t;
400 \f
401 /* Declarations for routines. */
402
403 /* To avoid duplicating every routine declaration -- once with a
404 * prototype (if we are ANSI), and once without (if we aren't) -- we
405 * use the following macro to declare argument types. This
406 * unfortunately clutters up the declarations a bit, but I think it's
407 * worth it. */
408
409 #if STDC_HEADERS
410
411 #define _RE_ARGS(args) args
412
413 #else /* not __STDC__ */
414
415 #define _RE_ARGS(args) ()
416
417 #endif /* not __STDC__ */
418
419 /* POSIX compatibility. */
420 extern int regcomp _RE_ARGS((regex_t * preg, const char *pattern, int cflags));
421 extern int regexec
422 _RE_ARGS((const regex_t * preg, const char *string, size_t nmatch,
423 regmatch_t pmatch[], int eflags));
424 extern size_t regerror
425 _RE_ARGS((int errcode, const regex_t * preg, char *errbuf,
426 size_t errbuf_size));
427 extern void regfree _RE_ARGS((regex_t * preg));
428
429 #ifdef __cplusplus
430 }
431 #endif
432
433 #endif /* USE_GNUREGEX */
434 #endif /* SQUID_REGEXP_LIBRARY_H */
435 \f
436 /*
437 * Local variables:
438 * make-backup-files: t
439 * version-control: t
440 * trim-versions-without-asking: nil
441 * End:
442 */