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