22384df1ff7a95d1e27e14087a7f0d6a815a19cc
[gnulib.git] / lib / regex.h
1 /* Definitions for data structures and routines for the regular
2    expression library.
3    Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005,2006
4    Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License along
18    with this program; if not, write to the Free Software Foundation,
19    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 #ifndef _REGEX_H
22 #define _REGEX_H 1
23
24 #include <sys/types.h>
25
26 /* Allow the use in C++ code.  */
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* Define _REGEX_SOURCE to get definitions that are incompatible with
32    POSIX.  */
33 #if (!defined _REGEX_SOURCE                                     \
34      && (defined _GNU_SOURCE                                    \
35          || (!defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE \
36              && !defined _XOPEN_SOURCE)))
37 # define _REGEX_SOURCE 1
38 #endif
39
40 #if defined _REGEX_SOURCE && defined VMS
41 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
42    should be there.  */
43 # include <stddef.h>
44 #endif
45
46 #ifdef _REGEX_LARGE_OFFSETS
47
48 /* Use types and values that are wide enough to represent signed and
49    unsigned byte offsets in memory.  This currently works only when
50    the regex code is used outside of the GNU C library; it is not yet
51    supported within glibc itself, and glibc users should not define
52    _REGEX_LARGE_OFFSETS.  */
53
54 /* The type of the offset of a byte within a string.
55    For historical reasons POSIX 1003.1-2004 requires that regoff_t be
56    at least as wide as off_t.  However, many common POSIX platforms set
57    regoff_t to the more-sensible ssize_t and the Open Group has
58    signalled its intention to change the requirement to be that
59    regoff_t be at least as wide as ptrdiff_t and ssize_t; see XBD ERN
60    60 (2005-08-25).  We don't know of any hosts where ssize_t or
61    ptrdiff_t is wider than long int, so long int is safe.  */
62 typedef long int regoff_t;
63
64 /* The type of nonnegative object indexes.  Traditionally, GNU regex
65    uses 'int' for these.  Code that uses __re_idx_t should work
66    regardless of whether the type is signed.  */
67 typedef size_t __re_idx_t;
68
69 /* The type of object sizes.  */
70 typedef size_t __re_size_t;
71
72 /* The type of object sizes, in places where the traditional code
73    uses unsigned long int.  */
74 typedef size_t __re_long_size_t;
75
76 #else
77
78 /* Use types that are binary-compatible with the traditional GNU regex
79    implementation, which mishandles strings longer than INT_MAX.  */
80
81 typedef int regoff_t;
82 typedef int __re_idx_t;
83 typedef unsigned int __re_size_t;
84 typedef unsigned long int __re_long_size_t;
85
86 #endif
87
88 /* The following two types have to be signed and unsigned integer type
89    wide enough to hold a value of a pointer.  For most ANSI compilers
90    ptrdiff_t and size_t should be likely OK.  Still size of these two
91    types is 2 for Microsoft C.  Ugh... */
92 typedef long int s_reg_t;
93 typedef unsigned long int active_reg_t;
94
95 /* The following bits are used to determine the regexp syntax we
96    recognize.  The set/not-set meanings are chosen so that Emacs syntax
97    remains the value 0.  The bits are given in alphabetical order, and
98    the definitions shifted by one from the previous bit; thus, when we
99    add or remove a bit, only one other definition need change.  */
100 typedef unsigned long int reg_syntax_t;
101
102 /* If this bit is not set, then \ inside a bracket expression is literal.
103    If set, then such a \ quotes the following character.  */
104 #define REG_BACKSLASH_ESCAPE_IN_LISTS 1ul
105
106 /* If this bit is not set, then + and ? are operators, and \+ and \? are
107      literals.
108    If set, then \+ and \? are operators and + and ? are literals.  */
109 #define REG_BK_PLUS_QM (1ul << 1)
110
111 /* If this bit is set, then character classes are supported.  They are:
112      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
113      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
114    If not set, then character classes are not supported.  */
115 #define REG_CHAR_CLASSES (1ul << 2)
116
117 /* If this bit is set, then ^ and $ are always anchors (outside bracket
118      expressions, of course).
119    If this bit is not set, then it depends:
120         ^  is an anchor if it is at the beginning of a regular
121            expression or after an open-group or an alternation operator;
122         $  is an anchor if it is at the end of a regular expression, or
123            before a close-group or an alternation operator.
124
125    This bit could be (re)combined with REG_CONTEXT_INDEP_OPS, because
126    POSIX draft 11.2 says that * etc. in leading positions is undefined.
127    We already implemented a previous draft which made those constructs
128    invalid, though, so we haven't changed the code back.  */
129 #define REG_CONTEXT_INDEP_ANCHORS (1ul << 3)
130
131 /* If this bit is set, then special characters are always special
132      regardless of where they are in the pattern.
133    If this bit is not set, then special characters are special only in
134      some contexts; otherwise they are ordinary.  Specifically,
135      * + ? and intervals are only special when not after the beginning,
136      open-group, or alternation operator.  */
137 #define REG_CONTEXT_INDEP_OPS (1ul << 4)
138
139 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
140      immediately after an alternation or begin-group operator.  */
141 #define REG_CONTEXT_INVALID_OPS (1ul << 5)
142
143 /* If this bit is set, then . matches newline.
144    If not set, then it doesn't.  */
145 #define REG_DOT_NEWLINE (1ul << 6)
146
147 /* If this bit is set, then . doesn't match NUL.
148    If not set, then it does.  */
149 #define REG_DOT_NOT_NULL (1ul << 7)
150
151 /* If this bit is set, nonmatching lists [^...] do not match newline.
152    If not set, they do.  */
153 #define REG_HAT_LISTS_NOT_NEWLINE (1ul << 8)
154
155 /* If this bit is set, either \{...\} or {...} defines an
156      interval, depending on REG_NO_BK_BRACES.
157    If not set, \{, \}, {, and } are literals.  */
158 #define REG_INTERVALS (1ul << 9)
159
160 /* If this bit is set, +, ? and | aren't recognized as operators.
161    If not set, they are.  */
162 #define REG_LIMITED_OPS (1ul << 10)
163
164 /* If this bit is set, newline is an alternation operator.
165    If not set, newline is literal.  */
166 #define REG_NEWLINE_ALT (1ul << 11)
167
168 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
169      are literals.
170   If not set, then `\{...\}' defines an interval.  */
171 #define REG_NO_BK_BRACES (1ul << 12)
172
173 /* If this bit is set, (...) defines a group, and \( and \) are literals.
174    If not set, \(...\) defines a group, and ( and ) are literals.  */
175 #define REG_NO_BK_PARENS (1ul << 13)
176
177 /* If this bit is set, then \<digit> matches <digit>.
178    If not set, then \<digit> is a back-reference.  */
179 #define REG_NO_BK_REFS (1ul << 14)
180
181 /* If this bit is set, then | is an alternation operator, and \| is literal.
182    If not set, then \| is an alternation operator, and | is literal.  */
183 #define REG_NO_BK_VBAR (1ul << 15)
184
185 /* If this bit is set, then an ending range point collating higher
186      than the starting range point, as in [z-a], is invalid.
187    If not set, the containing range is empty and does not match any string.  */
188 #define REG_NO_EMPTY_RANGES (1ul << 16)
189
190 /* If this bit is set, then an unmatched ) is ordinary.
191    If not set, then an unmatched ) is invalid.  */
192 #define REG_UNMATCHED_RIGHT_PAREN_ORD (1ul << 17)
193
194 /* If this bit is set, succeed as soon as we match the whole pattern,
195    without further backtracking.  */
196 #define REG_NO_POSIX_BACKTRACKING (1ul << 18)
197
198 /* If this bit is set, do not process the GNU regex operators.
199    If not set, then the GNU regex operators are recognized. */
200 #define REG_NO_GNU_OPS (1ul << 19)
201
202 /* If this bit is set, turn on internal regex debugging.
203    If not set, and debugging was on, turn it off.
204    This only works if regex.c is compiled -DDEBUG.
205    We define this bit always, so that all that's needed to turn on
206    debugging is to recompile regex.c; the calling code can always have
207    this bit set, and it won't affect anything in the normal case. */
208 #define REG_DEBUG (1ul << 20)
209
210 /* If this bit is set, a syntactically invalid interval is treated as
211    a string of ordinary characters.  For example, the ERE 'a{1' is
212    treated as 'a\{1'.  */
213 #define REG_INVALID_INTERVAL_ORD (1ul << 21)
214
215 /* If this bit is set, then ignore case when matching.
216    If not set, then case is significant.  */
217 #define REG_IGNORE_CASE (1ul << 22)
218
219 /* This bit is used internally like REG_CONTEXT_INDEP_ANCHORS but only
220    for ^, because it is difficult to scan the regex backwards to find
221    whether ^ should be special.  */
222 #define REG_CARET_ANCHORS_HERE (1ul << 23)
223
224 /* If this bit is set, then \{ cannot be first in an bre or
225    immediately after an alternation or begin-group operator.  */
226 #define REG_CONTEXT_INVALID_DUP (1ul << 24)
227
228 /* If this bit is set, then no_sub will be set to 1 during
229    re_compile_pattern.  */
230 #define REG_NO_SUB (1ul << 25)
231
232 /* This global variable defines the particular regexp syntax to use (for
233    some interfaces).  When a regexp is compiled, the syntax used is
234    stored in the pattern buffer, so changing this does not affect
235    already-compiled regexps.  */
236 extern reg_syntax_t re_syntax_options;
237 \f
238 /* Define combinations of the above bits for the standard possibilities.
239    (The [[[ comments delimit what gets put into the Texinfo file, so
240    don't delete them!)  */
241 /* [[[begin syntaxes]]] */
242 #define REG_SYNTAX_EMACS 0
243
244 #define REG_SYNTAX_AWK                                                  \
245   (REG_BACKSLASH_ESCAPE_IN_LISTS   | REG_DOT_NOT_NULL                   \
246    | REG_NO_BK_PARENS              | REG_NO_BK_REFS                     \
247    | REG_NO_BK_VBAR                | REG_NO_EMPTY_RANGES                \
248    | REG_DOT_NEWLINE               | REG_CONTEXT_INDEP_ANCHORS          \
249    | REG_UNMATCHED_RIGHT_PAREN_ORD | REG_NO_GNU_OPS)
250
251 #define REG_SYNTAX_GNU_AWK                                              \
252   ((REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS           \
253     | REG_DEBUG)                                                        \
254    & ~(REG_DOT_NOT_NULL | REG_INTERVALS | REG_CONTEXT_INDEP_OPS         \
255        | REG_CONTEXT_INVALID_OPS ))
256
257 #define REG_SYNTAX_POSIX_AWK                                            \
258   (REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS            \
259    | REG_INTERVALS           | REG_NO_GNU_OPS)
260
261 #define REG_SYNTAX_GREP                                                 \
262   (REG_BK_PLUS_QM              | REG_CHAR_CLASSES                       \
263    | REG_HAT_LISTS_NOT_NEWLINE | REG_INTERVALS                          \
264    | REG_NEWLINE_ALT)
265
266 #define REG_SYNTAX_EGREP                                                \
267   (REG_CHAR_CLASSES        | REG_CONTEXT_INDEP_ANCHORS                  \
268    | REG_CONTEXT_INDEP_OPS | REG_HAT_LISTS_NOT_NEWLINE                  \
269    | REG_NEWLINE_ALT       | REG_NO_BK_PARENS                           \
270    | REG_NO_BK_VBAR)
271
272 #define REG_SYNTAX_POSIX_EGREP                                          \
273   (REG_SYNTAX_EGREP | REG_INTERVALS | REG_NO_BK_BRACES                  \
274    | REG_INVALID_INTERVAL_ORD)
275
276 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff.  */
277 #define REG_SYNTAX_ED REG_SYNTAX_POSIX_BASIC
278
279 #define REG_SYNTAX_SED REG_SYNTAX_POSIX_BASIC
280
281 /* Syntax bits common to both basic and extended POSIX regex syntax.  */
282 #define _REG_SYNTAX_POSIX_COMMON                                        \
283   (REG_CHAR_CLASSES | REG_DOT_NEWLINE    | REG_DOT_NOT_NULL             \
284    | REG_INTERVALS  | REG_NO_EMPTY_RANGES)
285
286 #define REG_SYNTAX_POSIX_BASIC                                          \
287   (_REG_SYNTAX_POSIX_COMMON | REG_BK_PLUS_QM | REG_CONTEXT_INVALID_DUP)
288
289 /* Differs from ..._POSIX_BASIC only in that REG_BK_PLUS_QM becomes
290    REG_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
291    isn't minimal, since other operators, such as \`, aren't disabled.  */
292 #define REG_SYNTAX_POSIX_MINIMAL_BASIC                                  \
293   (_REG_SYNTAX_POSIX_COMMON | REG_LIMITED_OPS)
294
295 #define REG_SYNTAX_POSIX_EXTENDED                                       \
296   (_REG_SYNTAX_POSIX_COMMON  | REG_CONTEXT_INDEP_ANCHORS                \
297    | REG_CONTEXT_INDEP_OPS   | REG_NO_BK_BRACES                         \
298    | REG_NO_BK_PARENS        | REG_NO_BK_VBAR                           \
299    | REG_CONTEXT_INVALID_OPS | REG_UNMATCHED_RIGHT_PAREN_ORD)
300
301 /* Differs from ..._POSIX_EXTENDED in that REG_CONTEXT_INDEP_OPS is
302    removed and REG_NO_BK_REFS is added.  */
303 #define REG_SYNTAX_POSIX_MINIMAL_EXTENDED                               \
304   (_REG_SYNTAX_POSIX_COMMON  | REG_CONTEXT_INDEP_ANCHORS                \
305    | REG_CONTEXT_INVALID_OPS | REG_NO_BK_BRACES                         \
306    | REG_NO_BK_PARENS        | REG_NO_BK_REFS                           \
307    | REG_NO_BK_VBAR          | REG_UNMATCHED_RIGHT_PAREN_ORD)
308 /* [[[end syntaxes]]] */
309 \f
310 /* Maximum number of duplicates an interval can allow.  This is
311    distinct from RE_DUP_MAX, to conform to POSIX name space rules and
312    to avoid collisions with <limits.h>.  */
313 #define REG_DUP_MAX 32767
314
315
316 /* POSIX `cflags' bits (i.e., information for `regcomp').  */
317
318 /* If this bit is set, then use extended regular expression syntax.
319    If not set, then use basic regular expression syntax.  */
320 #define REG_EXTENDED 1
321
322 /* If this bit is set, then ignore case when matching.
323    If not set, then case is significant.  */
324 #define REG_ICASE (1 << 1)
325
326 /* If this bit is set, then anchors do not match at newline
327      characters in the string.
328    If not set, then anchors do match at newlines.  */
329 #define REG_NEWLINE (1 << 2)
330
331 /* If this bit is set, then report only success or fail in regexec.
332    If not set, then returns differ between not matching and errors.  */
333 #define REG_NOSUB (1 << 3)
334
335
336 /* POSIX `eflags' bits (i.e., information for regexec).  */
337
338 /* If this bit is set, then the beginning-of-line operator doesn't match
339      the beginning of the string (presumably because it's not the
340      beginning of a line).
341    If not set, then the beginning-of-line operator does match the
342      beginning of the string.  */
343 #define REG_NOTBOL 1
344
345 /* Like REG_NOTBOL, except for the end-of-line.  */
346 #define REG_NOTEOL (1 << 1)
347
348 /* Use PMATCH[0] to delimit the start and end of the search in the
349    buffer.  */
350 #define REG_STARTEND (1 << 2)
351
352
353 /* If any error codes are removed, changed, or added, update the
354    `__re_error_msgid' table in regcomp.c.  */
355
356 typedef enum
357 {
358   _REG_ENOSYS = -1,     /* This will never happen for this implementation.  */
359 #define REG_ENOSYS      _REG_ENOSYS
360
361   _REG_NOERROR,         /* Success.  */
362 #define REG_NOERROR     _REG_NOERROR
363
364   _REG_NOMATCH,         /* Didn't find a match (for regexec).  */
365 #define REG_NOMATCH     _REG_NOMATCH
366
367   /* POSIX regcomp return error codes.  (In the order listed in the
368      standard.)  */
369
370   _REG_BADPAT,          /* Invalid pattern.  */
371 #define REG_BADPAT      _REG_BADPAT
372
373   _REG_ECOLLATE,        /* Inalid collating element.  */
374 #define REG_ECOLLATE    _REG_ECOLLATE
375
376   _REG_ECTYPE,          /* Invalid character class name.  */
377 #define REG_ECTYPE      _REG_ECTYPE
378
379   _REG_EESCAPE,         /* Trailing backslash.  */
380 #define REG_EESCAPE     _REG_EESCAPE
381
382   _REG_ESUBREG,         /* Invalid back reference.  */
383 #define REG_ESUBREG     _REG_ESUBREG
384
385   _REG_EBRACK,          /* Unmatched left bracket.  */
386 #define REG_EBRACK      _REG_EBRACK
387
388   _REG_EPAREN,          /* Parenthesis imbalance.  */
389 #define REG_EPAREN      _REG_EPAREN
390
391   _REG_EBRACE,          /* Unmatched \{.  */
392 #define REG_EBRACE      _REG_EBRACE
393
394   _REG_BADBR,           /* Invalid contents of \{\}.  */
395 #define REG_BADBR       _REG_BADBR
396
397   _REG_ERANGE,          /* Invalid range end.  */
398 #define REG_ERANGE      _REG_ERANGE
399
400   _REG_ESPACE,          /* Ran out of memory.  */
401 #define REG_ESPACE      _REG_ESPACE
402
403   _REG_BADRPT,          /* No preceding re for repetition op.  */
404 #define REG_BADRPT      _REG_BADRPT
405
406   /* Error codes we've added.  */
407
408   _REG_EEND,            /* Premature end.  */
409 #define REG_EEND        _REG_EEND
410
411   _REG_ESIZE,           /* Compiled pattern bigger than 2^16 bytes.  */
412 #define REG_ESIZE       _REG_ESIZE
413
414   _REG_ERPAREN          /* Unmatched ) or \); not returned from regcomp.  */
415 #define REG_ERPAREN     _REG_ERPAREN
416
417 } reg_errcode_t;
418 \f
419 /* In the traditional GNU implementation, regex.h defined member names
420    like `buffer' that POSIX does not allow.  These members now have
421    names with leading `re_' (e.g., `re_buffer').  Support the old
422    names only if _REGEX_SOURCE is defined.  New programs should use
423    the new names.  */
424 #ifdef _REGEX_SOURCE
425 # define _REG_RE_NAME(id) id
426 # define _REG_RM_NAME(id) id
427 #else
428 # define _REG_RE_NAME(id) re_##id
429 # define _REG_RM_NAME(id) rm_##id
430 #endif
431
432 /* The user can specify the type of the re_translate member by
433    defining the macro REG_TRANSLATE_TYPE.  In the traditional GNU
434    implementation, this macro was named RE_TRANSLATE_TYPE, but POSIX
435    does not allow this.  Support the old name only if _REGEX_SOURCE
436    and if the new name is not defined. New programs should use the new
437    name.  */
438 #ifndef REG_TRANSLATE_TYPE
439 # if defined _REGEX_SOURCE && defined RE_TRANSLATE_TYPE
440 #  define REG_TRANSLATE_TYPE RE_TRANSLATE_TYPE
441 # else
442 #  define REG_TRANSLATE_TYPE char *
443 # endif
444 #endif
445
446 /* This data structure represents a compiled pattern.  Before calling
447    the pattern compiler), the fields `re_buffer', `re_allocated', `re_fastmap',
448    `re_translate', and `re_no_sub' can be set.  After the pattern has been
449    compiled, the `re_nsub' field is available.  All other fields are
450    private to the regex routines.  */
451
452 struct re_pattern_buffer
453 {
454 /* [[[begin pattern_buffer]]] */
455         /* Space that holds the compiled pattern.  It is declared as
456           `unsigned char *' because its elements are
457            sometimes used as array indexes.  */
458   unsigned char *_REG_RE_NAME (buffer);
459
460         /* Number of bytes to which `re_buffer' points.  */
461   __re_long_size_t _REG_RE_NAME (allocated);
462
463         /* Number of bytes actually used in `re_buffer'.  */
464   __re_long_size_t _REG_RE_NAME (used);
465
466         /* Syntax setting with which the pattern was compiled.  */
467   reg_syntax_t _REG_RE_NAME (syntax);
468
469         /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
470            the fastmap, if there is one, to skip over impossible
471            starting points for matches.  */
472   char *_REG_RE_NAME (fastmap);
473
474         /* Either a translate table to apply to all characters before
475            comparing them, or zero for no translation.  The translation
476            is applied to a pattern when it is compiled and to a string
477            when it is matched.  */
478   REG_TRANSLATE_TYPE _REG_RE_NAME (translate);
479
480         /* Number of subexpressions found by the compiler.  */
481   size_t re_nsub;
482
483         /* Zero if this pattern cannot match the empty string, one else.
484            Well, in truth it's used only in `re_search_2', to see
485            whether or not we should use the fastmap, so we don't set
486            this absolutely perfectly; see `re_compile_fastmap' (the
487            `duplicate' case).  */
488   unsigned int _REG_RE_NAME (can_be_null) : 1;
489
490         /* If REG_UNALLOCATED, allocate space in the `regs' structure
491              for `max (REG_NREGS, re_nsub + 1)' groups.
492            If REG_REALLOCATE, reallocate space if necessary.
493            If REG_FIXED, use what's there.  */
494 #define REG_UNALLOCATED 0
495 #define REG_REALLOCATE 1
496 #define REG_FIXED 2
497   unsigned int _REG_RE_NAME (regs_allocated) : 2;
498
499         /* Set to zero when `regex_compile' compiles a pattern; set to one
500            by `re_compile_fastmap' if it updates the fastmap.  */
501   unsigned int _REG_RE_NAME (fastmap_accurate) : 1;
502
503         /* If set, `re_match_2' does not return information about
504            subexpressions.  */
505   unsigned int _REG_RE_NAME (no_sub) : 1;
506
507         /* If set, a beginning-of-line anchor doesn't match at the
508            beginning of the string.  */
509   unsigned int _REG_RE_NAME (not_bol) : 1;
510
511         /* Similarly for an end-of-line anchor.  */
512   unsigned int _REG_RE_NAME (not_eol) : 1;
513
514         /* If true, an anchor at a newline matches.  */
515   unsigned int _REG_RE_NAME (newline_anchor) : 1;
516
517 /* [[[end pattern_buffer]]] */
518 };
519
520 typedef struct re_pattern_buffer regex_t;
521 \f
522 /* This is the structure we store register match data in.  See
523    regex.texinfo for a full description of what registers match.  */
524 struct re_registers
525 {
526   __re_size_t _REG_RM_NAME (num_regs);
527   regoff_t *_REG_RM_NAME (start);
528   regoff_t *_REG_RM_NAME (end);
529 };
530
531
532 /* If `regs_allocated' is REG_UNALLOCATED in the pattern buffer,
533    `re_match_2' returns information about at least this many registers
534    the first time a `regs' structure is passed.  */
535 #ifndef REG_NREGS
536 # define REG_NREGS 30
537 #endif
538
539
540 /* POSIX specification for registers.  Aside from the different names than
541    `re_registers', POSIX uses an array of structures, instead of a
542    structure of arrays.  */
543 typedef struct
544 {
545   regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
546   regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
547 } regmatch_t;
548 \f
549 /* Declarations for routines.  */
550
551 /* Sets the current default syntax to SYNTAX, and return the old syntax.
552    You can also simply assign to the `re_syntax_options' variable.  */
553 extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
554
555 /* Compile the regular expression PATTERN, with length LENGTH
556    and syntax given by the global `re_syntax_options', into the buffer
557    BUFFER.  Return NULL if successful, and an error string if not.  */
558 extern const char *re_compile_pattern (const char *__pattern, size_t __length,
559                                        struct re_pattern_buffer *__buffer);
560
561
562 /* Compile a fastmap for the compiled pattern in BUFFER; used to
563    accelerate searches.  Return 0 if successful and -2 if was an
564    internal error.  */
565 extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
566
567
568 /* Search in the string STRING (with length LENGTH) for the pattern
569    compiled into BUFFER.  Start searching at position START, for RANGE
570    characters.  Return the starting position of the match, -1 for no
571    match, or -2 for an internal error.  Also return register
572    information in REGS (if REGS and BUFFER->re_no_sub are nonzero).  */
573 extern regoff_t re_search (struct re_pattern_buffer *__buffer,
574                            const char *__string, __re_idx_t __length,
575                            __re_idx_t __start, regoff_t __range,
576                            struct re_registers *__regs);
577
578
579 /* Like `re_search', but search in the concatenation of STRING1 and
580    STRING2.  Also, stop searching at index START + STOP.  */
581 extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
582                              const char *__string1, __re_idx_t __length1,
583                              const char *__string2, __re_idx_t __length2,
584                              __re_idx_t __start, regoff_t __range,
585                              struct re_registers *__regs,
586                              __re_idx_t __stop);
587
588
589 /* Like `re_search', but return how many characters in STRING the regexp
590    in BUFFER matched, starting at position START.  */
591 extern regoff_t re_match (struct re_pattern_buffer *__buffer,
592                           const char *__string, __re_idx_t __length,
593                           __re_idx_t __start, struct re_registers *__regs);
594
595
596 /* Relates to `re_match' as `re_search_2' relates to `re_search'.  */
597 extern regoff_t re_match_2 (struct re_pattern_buffer *__buffer,
598                             const char *__string1, __re_idx_t __length1,
599                             const char *__string2, __re_idx_t __length2,
600                             __re_idx_t __start, struct re_registers *__regs,
601                             __re_idx_t __stop);
602
603
604 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
605    ENDS.  Subsequent matches using BUFFER and REGS will use this memory
606    for recording register information.  STARTS and ENDS must be
607    allocated with malloc, and must each be at least `NUM_REGS * sizeof
608    (regoff_t)' bytes long.
609
610    If NUM_REGS == 0, then subsequent matches should allocate their own
611    register data.
612
613    Unless this function is called, the first search or match using
614    PATTERN_BUFFER will allocate its own register data, without
615    freeing the old data.  */
616 extern void re_set_registers (struct re_pattern_buffer *__buffer,
617                               struct re_registers *__regs,
618                               __re_size_t __num_regs,
619                               regoff_t *__starts, regoff_t *__ends);
620
621 #if defined _REGEX_RE_COMP || defined _LIBC
622 # ifndef _CRAY
623 /* 4.2 bsd compatibility.  */
624 extern char *re_comp (const char *);
625 extern int re_exec (const char *);
626 # endif
627 #endif
628
629 /* GCC 2.95 and later have "__restrict"; C99 compilers have
630    "restrict", and "configure" may have defined "restrict".  */
631 #ifndef __restrict
632 # if ! (2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__))
633 #  if defined restrict || 199901L <= __STDC_VERSION__
634 #   define __restrict restrict
635 #  else
636 #   define __restrict
637 #  endif
638 # endif
639 #endif
640 /* gcc 3.1 and up support the [restrict] syntax, but g++ doesn't.  */
641 #ifndef __restrict_arr
642 # if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) && !defined __cplusplus
643 #  define __restrict_arr __restrict
644 # else
645 #  define __restrict_arr
646 # endif
647 #endif
648
649 /* POSIX compatibility.  */
650 extern int regcomp (regex_t *__restrict __preg,
651                     const char *__restrict __pattern,
652                     int __cflags);
653
654 extern int regexec (const regex_t *__restrict __preg,
655                     const char *__restrict __string, size_t __nmatch,
656                     regmatch_t __pmatch[__restrict_arr],
657                     int __eflags);
658
659 extern size_t regerror (int __errcode, const regex_t *__restrict __preg,
660                         char *__restrict __errbuf, size_t __errbuf_size);
661
662 extern void regfree (regex_t *__preg);
663
664
665 #ifdef _REGEX_SOURCE
666
667 /* Define the POSIX-compatible member names in terms of the
668    incompatible (and deprecated) names established by _REG_RE_NAME.
669    New programs should use the re_* names.  */
670
671 # define re_allocated allocated
672 # define re_buffer buffer
673 # define re_can_be_null can_be_null
674 # define re_fastmap fastmap
675 # define re_fastmap_accurate fastmap_accurate
676 # define re_newline_anchor newline_anchor
677 # define re_no_sub no_sub
678 # define re_not_bol not_bol
679 # define re_not_eol not_eol
680 # define re_regs_allocated regs_allocated
681 # define re_syntax syntax
682 # define re_translate translate
683 # define re_used used
684
685 /* Similarly for _REG_RM_NAME.  */
686
687 # define rm_end end
688 # define rm_num_regs num_regs
689 # define rm_start start
690
691 /* Undef RE_DUP_MAX first, in case the user has already included a
692    <limits.h> with an incompatible definition.
693
694    On GNU systems, the most common spelling for RE_DUP_MAX's value in
695    <limits.h> is (0x7ffff), so define RE_DUP_MAX to that, not to
696    REG_DUP_MAX.  This avoid some duplicate-macro-definition warnings
697    with programs that include <limits.h> after this file.
698
699    New programs should not assume that regex.h defines RE_DUP_MAX; to
700    get the value of RE_DUP_MAX, they should instead include <limits.h>
701    and possibly invoke the sysconf function.  */
702
703 # undef RE_DUP_MAX
704 # define RE_DUP_MAX (0x7fff)
705
706 /* Define the following symbols for backward source compatibility.
707    These symbols violate the POSIX name space rules, and new programs
708    should avoid them.  */
709
710 # define REGS_FIXED REG_FIXED
711 # define REGS_REALLOCATE REG_REALLOCATE
712 # define REGS_UNALLOCATED REG_UNALLOCATED
713 # define RE_BACKSLASH_ESCAPE_IN_LISTS REG_BACKSLASH_ESCAPE_IN_LISTS
714 # define RE_BK_PLUS_QM REG_BK_PLUS_QM
715 # define RE_CARET_ANCHORS_HERE REG_CARET_ANCHORS_HERE
716 # define RE_CHAR_CLASSES REG_CHAR_CLASSES
717 # define RE_CONTEXT_INDEP_ANCHORS REG_CONTEXT_INDEP_ANCHORS
718 # define RE_CONTEXT_INDEP_OPS REG_CONTEXT_INDEP_OPS
719 # define RE_CONTEXT_INVALID_DUP REG_CONTEXT_INVALID_DUP
720 # define RE_CONTEXT_INVALID_OPS REG_CONTEXT_INVALID_OPS
721 # define RE_DEBUG REG_DEBUG
722 # define RE_DOT_NEWLINE REG_DOT_NEWLINE
723 # define RE_DOT_NOT_NULL REG_DOT_NOT_NULL
724 # define RE_HAT_LISTS_NOT_NEWLINE REG_HAT_LISTS_NOT_NEWLINE
725 # define RE_ICASE REG_IGNORE_CASE /* avoid collision with REG_ICASE */
726 # define RE_INTERVALS REG_INTERVALS
727 # define RE_INVALID_INTERVAL_ORD REG_INVALID_INTERVAL_ORD
728 # define RE_LIMITED_OPS REG_LIMITED_OPS
729 # define RE_NEWLINE_ALT REG_NEWLINE_ALT
730 # define RE_NO_BK_BRACES REG_NO_BK_BRACES
731 # define RE_NO_BK_PARENS REG_NO_BK_PARENS
732 # define RE_NO_BK_REFS REG_NO_BK_REFS
733 # define RE_NO_BK_VBAR REG_NO_BK_VBAR
734 # define RE_NO_EMPTY_RANGES REG_NO_EMPTY_RANGES
735 # define RE_NO_GNU_OPS REG_NO_GNU_OPS
736 # define RE_NO_POSIX_BACKTRACKING REG_NO_POSIX_BACKTRACKING
737 # define RE_NO_SUB REG_NO_SUB
738 # define RE_NREGS REG_NREGS
739 # define RE_SYNTAX_AWK REG_SYNTAX_AWK
740 # define RE_SYNTAX_ED REG_SYNTAX_ED
741 # define RE_SYNTAX_EGREP REG_SYNTAX_EGREP
742 # define RE_SYNTAX_EMACS REG_SYNTAX_EMACS
743 # define RE_SYNTAX_GNU_AWK REG_SYNTAX_GNU_AWK
744 # define RE_SYNTAX_GREP REG_SYNTAX_GREP
745 # define RE_SYNTAX_POSIX_AWK REG_SYNTAX_POSIX_AWK
746 # define RE_SYNTAX_POSIX_BASIC REG_SYNTAX_POSIX_BASIC
747 # define RE_SYNTAX_POSIX_EGREP REG_SYNTAX_POSIX_EGREP
748 # define RE_SYNTAX_POSIX_EXTENDED REG_SYNTAX_POSIX_EXTENDED
749 # define RE_SYNTAX_POSIX_MINIMAL_BASIC REG_SYNTAX_POSIX_MINIMAL_BASIC
750 # define RE_SYNTAX_POSIX_MINIMAL_EXTENDED REG_SYNTAX_POSIX_MINIMAL_EXTENDED
751 # define RE_SYNTAX_SED REG_SYNTAX_SED
752 # define RE_UNMATCHED_RIGHT_PAREN_ORD REG_UNMATCHED_RIGHT_PAREN_ORD
753 # ifndef RE_TRANSLATE_TYPE
754 #  define RE_TRANSLATE_TYPE REG_TRANSLATE_TYPE
755 # endif
756
757 #endif /* defined _REGEX_SOURCE */
758
759 #ifdef __cplusplus
760 }
761 #endif  /* C++ */
762
763 #endif /* regex.h */
764 \f
765 /*
766 Local variables:
767 make-backup-files: t
768 version-control: t
769 trim-versions-without-asking: nil
770 End:
771 */