xref: /netbsd-src/external/gpl2/diffutils/dist/lib/posix/regex.h (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1*75f6d617Schristos /*	$NetBSD: regex.h,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2*75f6d617Schristos 
3*75f6d617Schristos /* Definitions for data structures and routines for the regular
4*75f6d617Schristos    expression library, version 0.12.
5*75f6d617Schristos    Copyright (C) 1985,1989-1993,1995-1998, 2000 Free Software Foundation, Inc.
6*75f6d617Schristos 
7*75f6d617Schristos    This program is free software; you can redistribute it and/or modify
8*75f6d617Schristos    it under the terms of the GNU General Public License as published by
9*75f6d617Schristos    the Free Software Foundation; either version 2, or (at your option)
10*75f6d617Schristos    any later version.
11*75f6d617Schristos 
12*75f6d617Schristos    This program is distributed in the hope that it will be useful,
13*75f6d617Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*75f6d617Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*75f6d617Schristos    GNU General Public License for more details.
16*75f6d617Schristos 
17*75f6d617Schristos    You should have received a copy of the GNU General Public License
18*75f6d617Schristos    along with this program; if not, write to the Free Software Foundation,
19*75f6d617Schristos    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20*75f6d617Schristos 
21*75f6d617Schristos #ifndef _REGEX_H
22*75f6d617Schristos #define _REGEX_H 1
23*75f6d617Schristos 
24*75f6d617Schristos /* Allow the use in C++ code.  */
25*75f6d617Schristos #ifdef __cplusplus
26*75f6d617Schristos extern "C" {
27*75f6d617Schristos #endif
28*75f6d617Schristos 
29*75f6d617Schristos /* POSIX says that <sys/types.h> must be included (by the caller) before
30*75f6d617Schristos    <regex.h>.  */
31*75f6d617Schristos 
32*75f6d617Schristos #if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS
33*75f6d617Schristos /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
34*75f6d617Schristos    should be there.  */
35*75f6d617Schristos # include <stddef.h>
36*75f6d617Schristos #endif
37*75f6d617Schristos 
38*75f6d617Schristos /* The following two types have to be signed and unsigned integer type
39*75f6d617Schristos    wide enough to hold a value of a pointer.  For most ANSI compilers
40*75f6d617Schristos    ptrdiff_t and size_t should be likely OK.  Still size of these two
41*75f6d617Schristos    types is 2 for Microsoft C.  Ugh... */
42*75f6d617Schristos typedef long int s_reg_t;
43*75f6d617Schristos typedef unsigned long int active_reg_t;
44*75f6d617Schristos 
45*75f6d617Schristos /* The following bits are used to determine the regexp syntax we
46*75f6d617Schristos    recognize.  The set/not-set meanings are chosen so that Emacs syntax
47*75f6d617Schristos    remains the value 0.  The bits are given in alphabetical order, and
48*75f6d617Schristos    the definitions shifted by one from the previous bit; thus, when we
49*75f6d617Schristos    add or remove a bit, only one other definition need change.  */
50*75f6d617Schristos typedef unsigned long int reg_syntax_t;
51*75f6d617Schristos 
52*75f6d617Schristos /* If this bit is not set, then \ inside a bracket expression is literal.
53*75f6d617Schristos    If set, then such a \ quotes the following character.  */
54*75f6d617Schristos #define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
55*75f6d617Schristos 
56*75f6d617Schristos /* If this bit is not set, then + and ? are operators, and \+ and \? are
57*75f6d617Schristos      literals.
58*75f6d617Schristos    If set, then \+ and \? are operators and + and ? are literals.  */
59*75f6d617Schristos #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
60*75f6d617Schristos 
61*75f6d617Schristos /* If this bit is set, then character classes are supported.  They are:
62*75f6d617Schristos      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
63*75f6d617Schristos      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
64*75f6d617Schristos    If not set, then character classes are not supported.  */
65*75f6d617Schristos #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
66*75f6d617Schristos 
67*75f6d617Schristos /* If this bit is set, then ^ and $ are always anchors (outside bracket
68*75f6d617Schristos      expressions, of course).
69*75f6d617Schristos    If this bit is not set, then it depends:
70*75f6d617Schristos         ^  is an anchor if it is at the beginning of a regular
71*75f6d617Schristos            expression or after an open-group or an alternation operator;
72*75f6d617Schristos         $  is an anchor if it is at the end of a regular expression, or
73*75f6d617Schristos            before a close-group or an alternation operator.
74*75f6d617Schristos 
75*75f6d617Schristos    This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
76*75f6d617Schristos    POSIX draft 11.2 says that * etc. in leading positions is undefined.
77*75f6d617Schristos    We already implemented a previous draft which made those constructs
78*75f6d617Schristos    invalid, though, so we haven't changed the code back.  */
79*75f6d617Schristos #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
80*75f6d617Schristos 
81*75f6d617Schristos /* If this bit is set, then special characters are always special
82*75f6d617Schristos      regardless of where they are in the pattern.
83*75f6d617Schristos    If this bit is not set, then special characters are special only in
84*75f6d617Schristos      some contexts; otherwise they are ordinary.  Specifically,
85*75f6d617Schristos      * + ? and intervals are only special when not after the beginning,
86*75f6d617Schristos      open-group, or alternation operator.  */
87*75f6d617Schristos #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
88*75f6d617Schristos 
89*75f6d617Schristos /* If this bit is set, then *, +, ?, and { cannot be first in an re or
90*75f6d617Schristos      immediately after an alternation or begin-group operator.  */
91*75f6d617Schristos #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
92*75f6d617Schristos 
93*75f6d617Schristos /* If this bit is set, then . matches newline.
94*75f6d617Schristos    If not set, then it doesn't.  */
95*75f6d617Schristos #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
96*75f6d617Schristos 
97*75f6d617Schristos /* If this bit is set, then . doesn't match NUL.
98*75f6d617Schristos    If not set, then it does.  */
99*75f6d617Schristos #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
100*75f6d617Schristos 
101*75f6d617Schristos /* If this bit is set, nonmatching lists [^...] do not match newline.
102*75f6d617Schristos    If not set, they do.  */
103*75f6d617Schristos #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
104*75f6d617Schristos 
105*75f6d617Schristos /* If this bit is set, either \{...\} or {...} defines an
106*75f6d617Schristos      interval, depending on RE_NO_BK_BRACES.
107*75f6d617Schristos    If not set, \{, \}, {, and } are literals.  */
108*75f6d617Schristos #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
109*75f6d617Schristos 
110*75f6d617Schristos /* If this bit is set, +, ? and | aren't recognized as operators.
111*75f6d617Schristos    If not set, they are.  */
112*75f6d617Schristos #define RE_LIMITED_OPS (RE_INTERVALS << 1)
113*75f6d617Schristos 
114*75f6d617Schristos /* If this bit is set, newline is an alternation operator.
115*75f6d617Schristos    If not set, newline is literal.  */
116*75f6d617Schristos #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
117*75f6d617Schristos 
118*75f6d617Schristos /* If this bit is set, then `{...}' defines an interval, and \{ and \}
119*75f6d617Schristos      are literals.
120*75f6d617Schristos   If not set, then `\{...\}' defines an interval.  */
121*75f6d617Schristos #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
122*75f6d617Schristos 
123*75f6d617Schristos /* If this bit is set, (...) defines a group, and \( and \) are literals.
124*75f6d617Schristos    If not set, \(...\) defines a group, and ( and ) are literals.  */
125*75f6d617Schristos #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
126*75f6d617Schristos 
127*75f6d617Schristos /* If this bit is set, then \<digit> matches <digit>.
128*75f6d617Schristos    If not set, then \<digit> is a back-reference.  */
129*75f6d617Schristos #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
130*75f6d617Schristos 
131*75f6d617Schristos /* If this bit is set, then | is an alternation operator, and \| is literal.
132*75f6d617Schristos    If not set, then \| is an alternation operator, and | is literal.  */
133*75f6d617Schristos #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
134*75f6d617Schristos 
135*75f6d617Schristos /* If this bit is set, then an ending range point collating higher
136*75f6d617Schristos      than the starting range point, as in [z-a], is invalid.
137*75f6d617Schristos    If not set, then when ending range point collates higher than the
138*75f6d617Schristos      starting range point, the range is ignored.  */
139*75f6d617Schristos #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
140*75f6d617Schristos 
141*75f6d617Schristos /* If this bit is set, then an unmatched ) is ordinary.
142*75f6d617Schristos    If not set, then an unmatched ) is invalid.  */
143*75f6d617Schristos #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
144*75f6d617Schristos 
145*75f6d617Schristos /* If this bit is set, succeed as soon as we match the whole pattern,
146*75f6d617Schristos    without further backtracking.  */
147*75f6d617Schristos #define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
148*75f6d617Schristos 
149*75f6d617Schristos /* If this bit is set, do not process the GNU regex operators.
150*75f6d617Schristos    If not set, then the GNU regex operators are recognized. */
151*75f6d617Schristos #define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
152*75f6d617Schristos 
153*75f6d617Schristos /* If this bit is set, turn on internal regex debugging.
154*75f6d617Schristos    If not set, and debugging was on, turn it off.
155*75f6d617Schristos    This only works if regex.c is compiled -DDEBUG.
156*75f6d617Schristos    We define this bit always, so that all that's needed to turn on
157*75f6d617Schristos    debugging is to recompile regex.c; the calling code can always have
158*75f6d617Schristos    this bit set, and it won't affect anything in the normal case. */
159*75f6d617Schristos #define RE_DEBUG (RE_NO_GNU_OPS << 1)
160*75f6d617Schristos 
161*75f6d617Schristos /* If this bit is set, a syntactically invalid interval is treated as
162*75f6d617Schristos    a string of ordinary characters.  For example, the ERE 'a{1' is
163*75f6d617Schristos    treated as 'a\{1'.  */
164*75f6d617Schristos #define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
165*75f6d617Schristos 
166*75f6d617Schristos /* This global variable defines the particular regexp syntax to use (for
167*75f6d617Schristos    some interfaces).  When a regexp is compiled, the syntax used is
168*75f6d617Schristos    stored in the pattern buffer, so changing this does not affect
169*75f6d617Schristos    already-compiled regexps.  */
170*75f6d617Schristos extern reg_syntax_t re_syntax_options;
171*75f6d617Schristos 
172*75f6d617Schristos /* Define combinations of the above bits for the standard possibilities.
173*75f6d617Schristos    (The [[[ comments delimit what gets put into the Texinfo file, so
174*75f6d617Schristos    don't delete them!)  */
175*75f6d617Schristos /* [[[begin syntaxes]]] */
176*75f6d617Schristos #define RE_SYNTAX_EMACS 0
177*75f6d617Schristos 
178*75f6d617Schristos #define RE_SYNTAX_AWK							\
179*75f6d617Schristos   (RE_BACKSLASH_ESCAPE_IN_LISTS   | RE_DOT_NOT_NULL			\
180*75f6d617Schristos    | RE_NO_BK_PARENS              | RE_NO_BK_REFS			\
181*75f6d617Schristos    | RE_NO_BK_VBAR                | RE_NO_EMPTY_RANGES			\
182*75f6d617Schristos    | RE_DOT_NEWLINE		  | RE_CONTEXT_INDEP_ANCHORS		\
183*75f6d617Schristos    | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
184*75f6d617Schristos 
185*75f6d617Schristos #define RE_SYNTAX_GNU_AWK						\
186*75f6d617Schristos   ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG)	\
187*75f6d617Schristos    & ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS))
188*75f6d617Schristos 
189*75f6d617Schristos #define RE_SYNTAX_POSIX_AWK 						\
190*75f6d617Schristos   (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS		\
191*75f6d617Schristos    | RE_INTERVALS	    | RE_NO_GNU_OPS)
192*75f6d617Schristos 
193*75f6d617Schristos #define RE_SYNTAX_GREP							\
194*75f6d617Schristos   (RE_BK_PLUS_QM              | RE_CHAR_CLASSES				\
195*75f6d617Schristos    | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS				\
196*75f6d617Schristos    | RE_NEWLINE_ALT)
197*75f6d617Schristos 
198*75f6d617Schristos #define RE_SYNTAX_EGREP							\
199*75f6d617Schristos   (RE_CHAR_CLASSES        | RE_CONTEXT_INDEP_ANCHORS			\
200*75f6d617Schristos    | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE			\
201*75f6d617Schristos    | RE_NEWLINE_ALT       | RE_NO_BK_PARENS				\
202*75f6d617Schristos    | RE_NO_BK_VBAR)
203*75f6d617Schristos 
204*75f6d617Schristos #define RE_SYNTAX_POSIX_EGREP						\
205*75f6d617Schristos   (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES			\
206*75f6d617Schristos    | RE_INVALID_INTERVAL_ORD)
207*75f6d617Schristos 
208*75f6d617Schristos /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff.  */
209*75f6d617Schristos #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
210*75f6d617Schristos 
211*75f6d617Schristos #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
212*75f6d617Schristos 
213*75f6d617Schristos /* Syntax bits common to both basic and extended POSIX regex syntax.  */
214*75f6d617Schristos #define _RE_SYNTAX_POSIX_COMMON						\
215*75f6d617Schristos   (RE_CHAR_CLASSES | RE_DOT_NEWLINE      | RE_DOT_NOT_NULL		\
216*75f6d617Schristos    | RE_INTERVALS  | RE_NO_EMPTY_RANGES)
217*75f6d617Schristos 
218*75f6d617Schristos #define RE_SYNTAX_POSIX_BASIC						\
219*75f6d617Schristos   (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
220*75f6d617Schristos 
221*75f6d617Schristos /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
222*75f6d617Schristos    RE_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
223*75f6d617Schristos    isn't minimal, since other operators, such as \`, aren't disabled.  */
224*75f6d617Schristos #define RE_SYNTAX_POSIX_MINIMAL_BASIC					\
225*75f6d617Schristos   (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
226*75f6d617Schristos 
227*75f6d617Schristos #define RE_SYNTAX_POSIX_EXTENDED					\
228*75f6d617Schristos   (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS			\
229*75f6d617Schristos    | RE_CONTEXT_INDEP_OPS   | RE_NO_BK_BRACES				\
230*75f6d617Schristos    | RE_NO_BK_PARENS        | RE_NO_BK_VBAR				\
231*75f6d617Schristos    | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
232*75f6d617Schristos 
233*75f6d617Schristos /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
234*75f6d617Schristos    removed and RE_NO_BK_REFS is added.  */
235*75f6d617Schristos #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED				\
236*75f6d617Schristos   (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS			\
237*75f6d617Schristos    | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES				\
238*75f6d617Schristos    | RE_NO_BK_PARENS        | RE_NO_BK_REFS				\
239*75f6d617Schristos    | RE_NO_BK_VBAR	    | RE_UNMATCHED_RIGHT_PAREN_ORD)
240*75f6d617Schristos /* [[[end syntaxes]]] */
241*75f6d617Schristos 
242*75f6d617Schristos /* Maximum number of duplicates an interval can allow.  Some systems
243*75f6d617Schristos    (erroneously) define this in other header files, but we want our
244*75f6d617Schristos    value, so remove any previous define.  */
245*75f6d617Schristos #ifdef RE_DUP_MAX
246*75f6d617Schristos # undef RE_DUP_MAX
247*75f6d617Schristos #endif
248*75f6d617Schristos /* If sizeof(int) == 2, then ((1 << 15) - 1) overflows.  */
249*75f6d617Schristos #define RE_DUP_MAX (0x7fff)
250*75f6d617Schristos 
251*75f6d617Schristos 
252*75f6d617Schristos /* POSIX `cflags' bits (i.e., information for `regcomp').  */
253*75f6d617Schristos 
254*75f6d617Schristos /* If this bit is set, then use extended regular expression syntax.
255*75f6d617Schristos    If not set, then use basic regular expression syntax.  */
256*75f6d617Schristos #define REG_EXTENDED 1
257*75f6d617Schristos 
258*75f6d617Schristos /* If this bit is set, then ignore case when matching.
259*75f6d617Schristos    If not set, then case is significant.  */
260*75f6d617Schristos #define REG_ICASE (REG_EXTENDED << 1)
261*75f6d617Schristos 
262*75f6d617Schristos /* If this bit is set, then anchors do not match at newline
263*75f6d617Schristos      characters in the string.
264*75f6d617Schristos    If not set, then anchors do match at newlines.  */
265*75f6d617Schristos #define REG_NEWLINE (REG_ICASE << 1)
266*75f6d617Schristos 
267*75f6d617Schristos /* If this bit is set, then report only success or fail in regexec.
268*75f6d617Schristos    If not set, then returns differ between not matching and errors.  */
269*75f6d617Schristos #define REG_NOSUB (REG_NEWLINE << 1)
270*75f6d617Schristos 
271*75f6d617Schristos 
272*75f6d617Schristos /* POSIX `eflags' bits (i.e., information for regexec).  */
273*75f6d617Schristos 
274*75f6d617Schristos /* If this bit is set, then the beginning-of-line operator doesn't match
275*75f6d617Schristos      the beginning of the string (presumably because it's not the
276*75f6d617Schristos      beginning of a line).
277*75f6d617Schristos    If not set, then the beginning-of-line operator does match the
278*75f6d617Schristos      beginning of the string.  */
279*75f6d617Schristos #define REG_NOTBOL 1
280*75f6d617Schristos 
281*75f6d617Schristos /* Like REG_NOTBOL, except for the end-of-line.  */
282*75f6d617Schristos #define REG_NOTEOL (1 << 1)
283*75f6d617Schristos 
284*75f6d617Schristos 
285*75f6d617Schristos /* If any error codes are removed, changed, or added, update the
286*75f6d617Schristos    `re_error_msg' table in regex.c.  */
287*75f6d617Schristos typedef enum
288*75f6d617Schristos {
289*75f6d617Schristos #ifdef _XOPEN_SOURCE
290*75f6d617Schristos   REG_ENOSYS = -1,	/* This will never happen for this implementation.  */
291*75f6d617Schristos #endif
292*75f6d617Schristos 
293*75f6d617Schristos   REG_NOERROR = 0,	/* Success.  */
294*75f6d617Schristos   REG_NOMATCH,		/* Didn't find a match (for regexec).  */
295*75f6d617Schristos 
296*75f6d617Schristos   /* POSIX regcomp return error codes.  (In the order listed in the
297*75f6d617Schristos      standard.)  */
298*75f6d617Schristos   REG_BADPAT,		/* Invalid pattern.  */
299*75f6d617Schristos   REG_ECOLLATE,		/* Not implemented.  */
300*75f6d617Schristos   REG_ECTYPE,		/* Invalid character class name.  */
301*75f6d617Schristos   REG_EESCAPE,		/* Trailing backslash.  */
302*75f6d617Schristos   REG_ESUBREG,		/* Invalid back reference.  */
303*75f6d617Schristos   REG_EBRACK,		/* Unmatched left bracket.  */
304*75f6d617Schristos   REG_EPAREN,		/* Parenthesis imbalance.  */
305*75f6d617Schristos   REG_EBRACE,		/* Unmatched \{.  */
306*75f6d617Schristos   REG_BADBR,		/* Invalid contents of \{\}.  */
307*75f6d617Schristos   REG_ERANGE,		/* Invalid range end.  */
308*75f6d617Schristos   REG_ESPACE,		/* Ran out of memory.  */
309*75f6d617Schristos   REG_BADRPT,		/* No preceding re for repetition op.  */
310*75f6d617Schristos 
311*75f6d617Schristos   /* Error codes we've added.  */
312*75f6d617Schristos   REG_EEND,		/* Premature end.  */
313*75f6d617Schristos   REG_ESIZE,		/* Compiled pattern bigger than 2^16 bytes.  */
314*75f6d617Schristos   REG_ERPAREN		/* Unmatched ) or \); not returned from regcomp.  */
315*75f6d617Schristos } reg_errcode_t;
316*75f6d617Schristos 
317*75f6d617Schristos /* This data structure represents a compiled pattern.  Before calling
318*75f6d617Schristos    the pattern compiler, the fields `buffer', `allocated', `fastmap',
319*75f6d617Schristos    `translate', and `no_sub' can be set.  After the pattern has been
320*75f6d617Schristos    compiled, the `re_nsub' field is available.  All other fields are
321*75f6d617Schristos    private to the regex routines.  */
322*75f6d617Schristos 
323*75f6d617Schristos #ifndef RE_TRANSLATE_TYPE
324*75f6d617Schristos # define RE_TRANSLATE_TYPE char *
325*75f6d617Schristos #endif
326*75f6d617Schristos 
327*75f6d617Schristos struct re_pattern_buffer
328*75f6d617Schristos {
329*75f6d617Schristos /* [[[begin pattern_buffer]]] */
330*75f6d617Schristos 	/* Space that holds the compiled pattern.  It is declared as
331*75f6d617Schristos           `unsigned char *' because its elements are
332*75f6d617Schristos            sometimes used as array indexes.  */
333*75f6d617Schristos   unsigned char *buffer;
334*75f6d617Schristos 
335*75f6d617Schristos 	/* Number of bytes to which `buffer' points.  */
336*75f6d617Schristos   unsigned long int allocated;
337*75f6d617Schristos 
338*75f6d617Schristos 	/* Number of bytes actually used in `buffer'.  */
339*75f6d617Schristos   unsigned long int used;
340*75f6d617Schristos 
341*75f6d617Schristos         /* Syntax setting with which the pattern was compiled.  */
342*75f6d617Schristos   reg_syntax_t syntax;
343*75f6d617Schristos 
344*75f6d617Schristos         /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
345*75f6d617Schristos            the fastmap, if there is one, to skip over impossible
346*75f6d617Schristos            starting points for matches.  */
347*75f6d617Schristos   char *fastmap;
348*75f6d617Schristos 
349*75f6d617Schristos         /* Either a translate table to apply to all characters before
350*75f6d617Schristos            comparing them, or zero for no translation.  The translation
351*75f6d617Schristos            is applied to a pattern when it is compiled and to a string
352*75f6d617Schristos            when it is matched.  */
353*75f6d617Schristos   RE_TRANSLATE_TYPE translate;
354*75f6d617Schristos 
355*75f6d617Schristos 	/* Number of subexpressions found by the compiler.  */
356*75f6d617Schristos   size_t re_nsub;
357*75f6d617Schristos 
358*75f6d617Schristos         /* Zero if this pattern cannot match the empty string, one else.
359*75f6d617Schristos            Well, in truth it's used only in `re_search_2', to see
360*75f6d617Schristos            whether or not we should use the fastmap, so we don't set
361*75f6d617Schristos            this absolutely perfectly; see `re_compile_fastmap' (the
362*75f6d617Schristos            `duplicate' case).  */
363*75f6d617Schristos   unsigned can_be_null : 1;
364*75f6d617Schristos 
365*75f6d617Schristos         /* If REGS_UNALLOCATED, allocate space in the `regs' structure
366*75f6d617Schristos              for `max (RE_NREGS, re_nsub + 1)' groups.
367*75f6d617Schristos            If REGS_REALLOCATE, reallocate space if necessary.
368*75f6d617Schristos            If REGS_FIXED, use what's there.  */
369*75f6d617Schristos #define REGS_UNALLOCATED 0
370*75f6d617Schristos #define REGS_REALLOCATE 1
371*75f6d617Schristos #define REGS_FIXED 2
372*75f6d617Schristos   unsigned regs_allocated : 2;
373*75f6d617Schristos 
374*75f6d617Schristos         /* Set to zero when `regex_compile' compiles a pattern; set to one
375*75f6d617Schristos            by `re_compile_fastmap' if it updates the fastmap.  */
376*75f6d617Schristos   unsigned fastmap_accurate : 1;
377*75f6d617Schristos 
378*75f6d617Schristos         /* If set, `re_match_2' does not return information about
379*75f6d617Schristos            subexpressions.  */
380*75f6d617Schristos   unsigned no_sub : 1;
381*75f6d617Schristos 
382*75f6d617Schristos         /* If set, a beginning-of-line anchor doesn't match at the
383*75f6d617Schristos            beginning of the string.  */
384*75f6d617Schristos   unsigned not_bol : 1;
385*75f6d617Schristos 
386*75f6d617Schristos         /* Similarly for an end-of-line anchor.  */
387*75f6d617Schristos   unsigned not_eol : 1;
388*75f6d617Schristos 
389*75f6d617Schristos         /* If true, an anchor at a newline matches.  */
390*75f6d617Schristos   unsigned newline_anchor : 1;
391*75f6d617Schristos 
392*75f6d617Schristos /* [[[end pattern_buffer]]] */
393*75f6d617Schristos };
394*75f6d617Schristos 
395*75f6d617Schristos typedef struct re_pattern_buffer regex_t;
396*75f6d617Schristos 
397*75f6d617Schristos /* Type for byte offsets within the string.  POSIX mandates this.  */
398*75f6d617Schristos typedef int regoff_t;
399*75f6d617Schristos 
400*75f6d617Schristos 
401*75f6d617Schristos /* This is the structure we store register match data in.  See
402*75f6d617Schristos    regex.texinfo for a full description of what registers match.  */
403*75f6d617Schristos struct re_registers
404*75f6d617Schristos {
405*75f6d617Schristos   unsigned num_regs;
406*75f6d617Schristos   regoff_t *start;
407*75f6d617Schristos   regoff_t *end;
408*75f6d617Schristos };
409*75f6d617Schristos 
410*75f6d617Schristos 
411*75f6d617Schristos /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
412*75f6d617Schristos    `re_match_2' returns information about at least this many registers
413*75f6d617Schristos    the first time a `regs' structure is passed.  */
414*75f6d617Schristos #ifndef RE_NREGS
415*75f6d617Schristos # define RE_NREGS 30
416*75f6d617Schristos #endif
417*75f6d617Schristos 
418*75f6d617Schristos 
419*75f6d617Schristos /* POSIX specification for registers.  Aside from the different names than
420*75f6d617Schristos    `re_registers', POSIX uses an array of structures, instead of a
421*75f6d617Schristos    structure of arrays.  */
422*75f6d617Schristos typedef struct
423*75f6d617Schristos {
424*75f6d617Schristos   regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
425*75f6d617Schristos   regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
426*75f6d617Schristos } regmatch_t;
427*75f6d617Schristos 
428*75f6d617Schristos /* Declarations for routines.  */
429*75f6d617Schristos 
430*75f6d617Schristos /* To avoid duplicating every routine declaration -- once with a
431*75f6d617Schristos    prototype (if we are ANSI), and once without (if we aren't) -- we
432*75f6d617Schristos    use the following macro to declare argument types.  This
433*75f6d617Schristos    unfortunately clutters up the declarations a bit, but I think it's
434*75f6d617Schristos    worth it.  */
435*75f6d617Schristos 
436*75f6d617Schristos #if __STDC__
437*75f6d617Schristos 
438*75f6d617Schristos # define _RE_ARGS(args) args
439*75f6d617Schristos 
440*75f6d617Schristos #else /* not __STDC__ */
441*75f6d617Schristos 
442*75f6d617Schristos # define _RE_ARGS(args) ()
443*75f6d617Schristos 
444*75f6d617Schristos #endif /* not __STDC__ */
445*75f6d617Schristos 
446*75f6d617Schristos /* Sets the current default syntax to SYNTAX, and return the old syntax.
447*75f6d617Schristos    You can also simply assign to the `re_syntax_options' variable.  */
448*75f6d617Schristos extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
449*75f6d617Schristos 
450*75f6d617Schristos /* Compile the regular expression PATTERN, with length LENGTH
451*75f6d617Schristos    and syntax given by the global `re_syntax_options', into the buffer
452*75f6d617Schristos    BUFFER.  Return NULL if successful, and an error string if not.  */
453*75f6d617Schristos extern const char *re_compile_pattern
454*75f6d617Schristos   _RE_ARGS ((const char *pattern, size_t length,
455*75f6d617Schristos              struct re_pattern_buffer *buffer));
456*75f6d617Schristos 
457*75f6d617Schristos 
458*75f6d617Schristos /* Compile a fastmap for the compiled pattern in BUFFER; used to
459*75f6d617Schristos    accelerate searches.  Return 0 if successful and -2 if was an
460*75f6d617Schristos    internal error.  */
461*75f6d617Schristos extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
462*75f6d617Schristos 
463*75f6d617Schristos 
464*75f6d617Schristos /* Search in the string STRING (with length LENGTH) for the pattern
465*75f6d617Schristos    compiled into BUFFER.  Start searching at position START, for RANGE
466*75f6d617Schristos    characters.  Return the starting position of the match, -1 for no
467*75f6d617Schristos    match, or -2 for an internal error.  Also return register
468*75f6d617Schristos    information in REGS (if REGS and BUFFER->no_sub are nonzero).  */
469*75f6d617Schristos extern int re_search
470*75f6d617Schristos   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
471*75f6d617Schristos             int length, int start, int range, struct re_registers *regs));
472*75f6d617Schristos 
473*75f6d617Schristos 
474*75f6d617Schristos /* Like `re_search', but search in the concatenation of STRING1 and
475*75f6d617Schristos    STRING2.  Also, stop searching at index START + STOP.  */
476*75f6d617Schristos extern int re_search_2
477*75f6d617Schristos   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
478*75f6d617Schristos              int length1, const char *string2, int length2,
479*75f6d617Schristos              int start, int range, struct re_registers *regs, int stop));
480*75f6d617Schristos 
481*75f6d617Schristos 
482*75f6d617Schristos /* Like `re_search', but return how many characters in STRING the regexp
483*75f6d617Schristos    in BUFFER matched, starting at position START.  */
484*75f6d617Schristos extern int re_match
485*75f6d617Schristos   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
486*75f6d617Schristos              int length, int start, struct re_registers *regs));
487*75f6d617Schristos 
488*75f6d617Schristos 
489*75f6d617Schristos /* Relates to `re_match' as `re_search_2' relates to `re_search'.  */
490*75f6d617Schristos extern int re_match_2
491*75f6d617Schristos   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
492*75f6d617Schristos              int length1, const char *string2, int length2,
493*75f6d617Schristos              int start, struct re_registers *regs, int stop));
494*75f6d617Schristos 
495*75f6d617Schristos 
496*75f6d617Schristos /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
497*75f6d617Schristos    ENDS.  Subsequent matches using BUFFER and REGS will use this memory
498*75f6d617Schristos    for recording register information.  STARTS and ENDS must be
499*75f6d617Schristos    allocated with malloc, and must each be at least `NUM_REGS * sizeof
500*75f6d617Schristos    (regoff_t)' bytes long.
501*75f6d617Schristos 
502*75f6d617Schristos    If NUM_REGS == 0, then subsequent matches should allocate their own
503*75f6d617Schristos    register data.
504*75f6d617Schristos 
505*75f6d617Schristos    Unless this function is called, the first search or match using
506*75f6d617Schristos    PATTERN_BUFFER will allocate its own register data, without
507*75f6d617Schristos    freeing the old data.  */
508*75f6d617Schristos extern void re_set_registers
509*75f6d617Schristos   _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
510*75f6d617Schristos              unsigned num_regs, regoff_t *starts, regoff_t *ends));
511*75f6d617Schristos 
512*75f6d617Schristos #if defined _REGEX_RE_COMP || defined _LIBC
513*75f6d617Schristos # ifndef _CRAY
514*75f6d617Schristos /* 4.2 bsd compatibility.  */
515*75f6d617Schristos extern char *re_comp _RE_ARGS ((const char *));
516*75f6d617Schristos extern int re_exec _RE_ARGS ((const char *));
517*75f6d617Schristos # endif
518*75f6d617Schristos #endif
519*75f6d617Schristos 
520*75f6d617Schristos /* GCC 2.95 and later have "__restrict"; C99 compilers have
521*75f6d617Schristos    "restrict", and "configure" may have defined "restrict".  */
522*75f6d617Schristos #ifndef __restrict
523*75f6d617Schristos # if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))
524*75f6d617Schristos #  if defined restrict || 199901L <= __STDC_VERSION__
525*75f6d617Schristos #   define __restrict restrict
526*75f6d617Schristos #  else
527*75f6d617Schristos #   define __restrict
528*75f6d617Schristos #  endif
529*75f6d617Schristos # endif
530*75f6d617Schristos #endif
531*75f6d617Schristos /* For now unconditionally define __restrict_arr to expand to nothing.
532*75f6d617Schristos    Ideally we would have a test for the compiler which allows defining
533*75f6d617Schristos    it to restrict.  */
534*75f6d617Schristos #define __restrict_arr
535*75f6d617Schristos 
536*75f6d617Schristos /* POSIX compatibility.  */
537*75f6d617Schristos extern int regcomp _RE_ARGS ((regex_t *__restrict __preg,
538*75f6d617Schristos 			      const char *__restrict __pattern,
539*75f6d617Schristos 			      int __cflags));
540*75f6d617Schristos 
541*75f6d617Schristos extern int regexec _RE_ARGS ((const regex_t *__restrict __preg,
542*75f6d617Schristos 			      const char *__restrict __string, size_t __nmatch,
543*75f6d617Schristos 			      regmatch_t __pmatch[__restrict_arr],
544*75f6d617Schristos 			      int __eflags));
545*75f6d617Schristos 
546*75f6d617Schristos extern size_t regerror _RE_ARGS ((int __errcode, const regex_t *__preg,
547*75f6d617Schristos 				  char *__errbuf, size_t __errbuf_size));
548*75f6d617Schristos 
549*75f6d617Schristos extern void regfree _RE_ARGS ((regex_t *__preg));
550*75f6d617Schristos 
551*75f6d617Schristos 
552*75f6d617Schristos #ifdef __cplusplus
553*75f6d617Schristos }
554*75f6d617Schristos #endif	/* C++ */
555*75f6d617Schristos 
556*75f6d617Schristos #endif /* regex.h */
557*75f6d617Schristos 
558*75f6d617Schristos /*
559*75f6d617Schristos Local variables:
560*75f6d617Schristos make-backup-files: t
561*75f6d617Schristos version-control: t
562*75f6d617Schristos trim-versions-without-asking: nil
563*75f6d617Schristos End:
564*75f6d617Schristos */
565