| 1 | /* Definitions for data structures and routines for the regular
|
|---|
| 2 | expression library.
|
|---|
| 3 | Copyright (C) 1985, 1989-1993, 1995-1998, 2000-2003, 2005-2012
|
|---|
| 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 3, 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, see <http://www.gnu.org/licenses/>. */
|
|---|
| 19 |
|
|---|
| 20 | #ifndef _REGEX_H
|
|---|
| 21 | #define _REGEX_H 1
|
|---|
| 22 |
|
|---|
| 23 | #include <sys/types.h>
|
|---|
| 24 |
|
|---|
| 25 | /* Allow the use in C++ code. */
|
|---|
| 26 | #ifdef __cplusplus
|
|---|
| 27 | extern "C" {
|
|---|
| 28 | #endif
|
|---|
| 29 |
|
|---|
| 30 | /* Define __USE_GNU to declare GNU extensions that violate the
|
|---|
| 31 | POSIX name space rules. */
|
|---|
| 32 | #ifdef _GNU_SOURCE
|
|---|
| 33 | # define __USE_GNU 1
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 | #ifdef _REGEX_LARGE_OFFSETS
|
|---|
| 37 |
|
|---|
| 38 | /* Use types and values that are wide enough to represent signed and
|
|---|
| 39 | unsigned byte offsets in memory. This currently works only when
|
|---|
| 40 | the regex code is used outside of the GNU C library; it is not yet
|
|---|
| 41 | supported within glibc itself, and glibc users should not define
|
|---|
| 42 | _REGEX_LARGE_OFFSETS. */
|
|---|
| 43 |
|
|---|
| 44 | /* The type of nonnegative object indexes. Traditionally, GNU regex
|
|---|
| 45 | uses 'int' for these. Code that uses __re_idx_t should work
|
|---|
| 46 | regardless of whether the type is signed. */
|
|---|
| 47 | typedef size_t __re_idx_t;
|
|---|
| 48 |
|
|---|
| 49 | /* The type of object sizes. */
|
|---|
| 50 | typedef size_t __re_size_t;
|
|---|
| 51 |
|
|---|
| 52 | /* The type of object sizes, in places where the traditional code
|
|---|
| 53 | uses unsigned long int. */
|
|---|
| 54 | typedef size_t __re_long_size_t;
|
|---|
| 55 |
|
|---|
| 56 | #else
|
|---|
| 57 |
|
|---|
| 58 | /* The traditional GNU regex implementation mishandles strings longer
|
|---|
| 59 | than INT_MAX. */
|
|---|
| 60 | typedef int __re_idx_t;
|
|---|
| 61 | typedef unsigned int __re_size_t;
|
|---|
| 62 | typedef unsigned long int __re_long_size_t;
|
|---|
| 63 |
|
|---|
| 64 | #endif
|
|---|
| 65 |
|
|---|
| 66 | /* The following two types have to be signed and unsigned integer type
|
|---|
| 67 | wide enough to hold a value of a pointer. For most ANSI compilers
|
|---|
| 68 | ptrdiff_t and size_t should be likely OK. Still size of these two
|
|---|
| 69 | types is 2 for Microsoft C. Ugh... */
|
|---|
| 70 | typedef long int s_reg_t;
|
|---|
| 71 | typedef unsigned long int active_reg_t;
|
|---|
| 72 |
|
|---|
| 73 | /* The following bits are used to determine the regexp syntax we
|
|---|
| 74 | recognize. The set/not-set meanings are chosen so that Emacs syntax
|
|---|
| 75 | remains the value 0. The bits are given in alphabetical order, and
|
|---|
| 76 | the definitions shifted by one from the previous bit; thus, when we
|
|---|
| 77 | add or remove a bit, only one other definition need change. */
|
|---|
| 78 | typedef unsigned long int reg_syntax_t;
|
|---|
| 79 |
|
|---|
| 80 | #ifdef __USE_GNU
|
|---|
| 81 | /* If this bit is not set, then \ inside a bracket expression is literal.
|
|---|
| 82 | If set, then such a \ quotes the following character. */
|
|---|
| 83 | # define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
|
|---|
| 84 |
|
|---|
| 85 | /* If this bit is not set, then + and ? are operators, and \+ and \? are
|
|---|
| 86 | literals.
|
|---|
| 87 | If set, then \+ and \? are operators and + and ? are literals. */
|
|---|
| 88 | # define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
|
|---|
| 89 |
|
|---|
| 90 | /* If this bit is set, then character classes are supported. They are:
|
|---|
| 91 | [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
|
|---|
| 92 | [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
|
|---|
| 93 | If not set, then character classes are not supported. */
|
|---|
| 94 | # define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
|
|---|
| 95 |
|
|---|
| 96 | /* If this bit is set, then ^ and $ are always anchors (outside bracket
|
|---|
| 97 | expressions, of course).
|
|---|
| 98 | If this bit is not set, then it depends:
|
|---|
| 99 | ^ is an anchor if it is at the beginning of a regular
|
|---|
| 100 | expression or after an open-group or an alternation operator;
|
|---|
| 101 | $ is an anchor if it is at the end of a regular expression, or
|
|---|
| 102 | before a close-group or an alternation operator.
|
|---|
| 103 |
|
|---|
| 104 | This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
|
|---|
| 105 | POSIX draft 11.2 says that * etc. in leading positions is undefined.
|
|---|
| 106 | We already implemented a previous draft which made those constructs
|
|---|
| 107 | invalid, though, so we haven't changed the code back. */
|
|---|
| 108 | # define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
|
|---|
| 109 |
|
|---|
| 110 | /* If this bit is set, then special characters are always special
|
|---|
| 111 | regardless of where they are in the pattern.
|
|---|
| 112 | If this bit is not set, then special characters are special only in
|
|---|
| 113 | some contexts; otherwise they are ordinary. Specifically,
|
|---|
| 114 | * + ? and intervals are only special when not after the beginning,
|
|---|
| 115 | open-group, or alternation operator. */
|
|---|
| 116 | # define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
|
|---|
| 117 |
|
|---|
| 118 | /* If this bit is set, then *, +, ?, and { cannot be first in an re or
|
|---|
| 119 | immediately after an alternation or begin-group operator. */
|
|---|
| 120 | # define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
|
|---|
| 121 |
|
|---|
| 122 | /* If this bit is set, then . matches newline.
|
|---|
| 123 | If not set, then it doesn't. */
|
|---|
| 124 | # define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
|
|---|
| 125 |
|
|---|
| 126 | /* If this bit is set, then . doesn't match NUL.
|
|---|
| 127 | If not set, then it does. */
|
|---|
| 128 | # define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
|
|---|
| 129 |
|
|---|
| 130 | /* If this bit is set, nonmatching lists [^...] do not match newline.
|
|---|
| 131 | If not set, they do. */
|
|---|
| 132 | # define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
|
|---|
| 133 |
|
|---|
| 134 | /* If this bit is set, either \{...\} or {...} defines an
|
|---|
| 135 | interval, depending on RE_NO_BK_BRACES.
|
|---|
| 136 | If not set, \{, \}, {, and } are literals. */
|
|---|
| 137 | # define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
|
|---|
| 138 |
|
|---|
| 139 | /* If this bit is set, +, ? and | aren't recognized as operators.
|
|---|
| 140 | If not set, they are. */
|
|---|
| 141 | # define RE_LIMITED_OPS (RE_INTERVALS << 1)
|
|---|
| 142 |
|
|---|
| 143 | /* If this bit is set, newline is an alternation operator.
|
|---|
| 144 | If not set, newline is literal. */
|
|---|
| 145 | # define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
|
|---|
| 146 |
|
|---|
| 147 | /* If this bit is set, then '{...}' defines an interval, and \{ and \}
|
|---|
| 148 | are literals.
|
|---|
| 149 | If not set, then '\{...\}' defines an interval. */
|
|---|
| 150 | # define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
|
|---|
| 151 |
|
|---|
| 152 | /* If this bit is set, (...) defines a group, and \( and \) are literals.
|
|---|
| 153 | If not set, \(...\) defines a group, and ( and ) are literals. */
|
|---|
| 154 | # define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
|
|---|
| 155 |
|
|---|
| 156 | /* If this bit is set, then \<digit> matches <digit>.
|
|---|
| 157 | If not set, then \<digit> is a back-reference. */
|
|---|
| 158 | # define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
|
|---|
| 159 |
|
|---|
| 160 | /* If this bit is set, then | is an alternation operator, and \| is literal.
|
|---|
| 161 | If not set, then \| is an alternation operator, and | is literal. */
|
|---|
| 162 | # define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
|
|---|
| 163 |
|
|---|
| 164 | /* If this bit is set, then an ending range point collating higher
|
|---|
| 165 | than the starting range point, as in [z-a], is invalid.
|
|---|
| 166 | If not set, then when ending range point collates higher than the
|
|---|
| 167 | starting range point, the range is ignored. */
|
|---|
| 168 | # define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
|
|---|
| 169 |
|
|---|
| 170 | /* If this bit is set, then an unmatched ) is ordinary.
|
|---|
| 171 | If not set, then an unmatched ) is invalid. */
|
|---|
| 172 | # define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
|
|---|
| 173 |
|
|---|
| 174 | /* If this bit is set, succeed as soon as we match the whole pattern,
|
|---|
| 175 | without further backtracking. */
|
|---|
| 176 | # define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
|
|---|
| 177 |
|
|---|
| 178 | /* If this bit is set, do not process the GNU regex operators.
|
|---|
| 179 | If not set, then the GNU regex operators are recognized. */
|
|---|
| 180 | # define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
|
|---|
| 181 |
|
|---|
| 182 | /* If this bit is set, turn on internal regex debugging.
|
|---|
| 183 | If not set, and debugging was on, turn it off.
|
|---|
| 184 | This only works if regex.c is compiled -DDEBUG.
|
|---|
| 185 | We define this bit always, so that all that's needed to turn on
|
|---|
| 186 | debugging is to recompile regex.c; the calling code can always have
|
|---|
| 187 | this bit set, and it won't affect anything in the normal case. */
|
|---|
| 188 | # define RE_DEBUG (RE_NO_GNU_OPS << 1)
|
|---|
| 189 |
|
|---|
| 190 | /* If this bit is set, a syntactically invalid interval is treated as
|
|---|
| 191 | a string of ordinary characters. For example, the ERE 'a{1' is
|
|---|
| 192 | treated as 'a\{1'. */
|
|---|
| 193 | # define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
|
|---|
| 194 |
|
|---|
| 195 | /* If this bit is set, then ignore case when matching.
|
|---|
| 196 | If not set, then case is significant. */
|
|---|
| 197 | # define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
|
|---|
| 198 |
|
|---|
| 199 | /* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only
|
|---|
| 200 | for ^, because it is difficult to scan the regex backwards to find
|
|---|
| 201 | whether ^ should be special. */
|
|---|
| 202 | # define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)
|
|---|
| 203 |
|
|---|
| 204 | /* If this bit is set, then \{ cannot be first in a regex or
|
|---|
| 205 | immediately after an alternation, open-group or \} operator. */
|
|---|
| 206 | # define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)
|
|---|
| 207 |
|
|---|
| 208 | /* If this bit is set, then no_sub will be set to 1 during
|
|---|
| 209 | re_compile_pattern. */
|
|---|
| 210 | # define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)
|
|---|
| 211 | #endif
|
|---|
| 212 |
|
|---|
| 213 | /* This global variable defines the particular regexp syntax to use (for
|
|---|
| 214 | some interfaces). When a regexp is compiled, the syntax used is
|
|---|
| 215 | stored in the pattern buffer, so changing this does not affect
|
|---|
| 216 | already-compiled regexps. */
|
|---|
| 217 | extern reg_syntax_t re_syntax_options;
|
|---|
| 218 | |
|---|
| 219 |
|
|---|
| 220 | #ifdef __USE_GNU
|
|---|
| 221 | /* Define combinations of the above bits for the standard possibilities.
|
|---|
| 222 | (The [[[ comments delimit what gets put into the Texinfo file, so
|
|---|
| 223 | don't delete them!) */
|
|---|
| 224 | /* [[[begin syntaxes]]] */
|
|---|
| 225 | # define RE_SYNTAX_EMACS 0
|
|---|
| 226 |
|
|---|
| 227 | # define RE_SYNTAX_AWK \
|
|---|
| 228 | (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
|
|---|
| 229 | | RE_NO_BK_PARENS | RE_NO_BK_REFS \
|
|---|
| 230 | | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
|
|---|
| 231 | | RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \
|
|---|
| 232 | | RE_CHAR_CLASSES \
|
|---|
| 233 | | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
|
|---|
| 234 |
|
|---|
| 235 | # define RE_SYNTAX_GNU_AWK \
|
|---|
| 236 | ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
|
|---|
| 237 | | RE_INVALID_INTERVAL_ORD) \
|
|---|
| 238 | & ~(RE_DOT_NOT_NULL | RE_CONTEXT_INDEP_OPS \
|
|---|
| 239 | | RE_CONTEXT_INVALID_OPS ))
|
|---|
| 240 |
|
|---|
| 241 | # define RE_SYNTAX_POSIX_AWK \
|
|---|
| 242 | (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
|
|---|
| 243 | | RE_INTERVALS | RE_NO_GNU_OPS \
|
|---|
| 244 | | RE_INVALID_INTERVAL_ORD)
|
|---|
| 245 |
|
|---|
| 246 | # define RE_SYNTAX_GREP \
|
|---|
| 247 | (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
|
|---|
| 248 | | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
|
|---|
| 249 | | RE_NEWLINE_ALT)
|
|---|
| 250 |
|
|---|
| 251 | # define RE_SYNTAX_EGREP \
|
|---|
| 252 | (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
|
|---|
| 253 | | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
|
|---|
| 254 | | RE_NEWLINE_ALT | RE_NO_BK_PARENS \
|
|---|
| 255 | | RE_NO_BK_VBAR)
|
|---|
| 256 |
|
|---|
| 257 | # define RE_SYNTAX_POSIX_EGREP \
|
|---|
| 258 | (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \
|
|---|
| 259 | | RE_INVALID_INTERVAL_ORD)
|
|---|
| 260 |
|
|---|
| 261 | /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
|
|---|
| 262 | # define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
|
|---|
| 263 |
|
|---|
| 264 | # define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
|
|---|
| 265 |
|
|---|
| 266 | /* Syntax bits common to both basic and extended POSIX regex syntax. */
|
|---|
| 267 | # define _RE_SYNTAX_POSIX_COMMON \
|
|---|
| 268 | (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
|
|---|
| 269 | | RE_INTERVALS | RE_NO_EMPTY_RANGES)
|
|---|
| 270 |
|
|---|
| 271 | # define RE_SYNTAX_POSIX_BASIC \
|
|---|
| 272 | (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)
|
|---|
| 273 |
|
|---|
| 274 | /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
|
|---|
| 275 | RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
|
|---|
| 276 | isn't minimal, since other operators, such as \`, aren't disabled. */
|
|---|
| 277 | # define RE_SYNTAX_POSIX_MINIMAL_BASIC \
|
|---|
| 278 | (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
|
|---|
| 279 |
|
|---|
| 280 | # define RE_SYNTAX_POSIX_EXTENDED \
|
|---|
| 281 | (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
|
|---|
| 282 | | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
|
|---|
| 283 | | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
|
|---|
| 284 | | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
|
|---|
| 285 |
|
|---|
| 286 | /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
|
|---|
| 287 | removed and RE_NO_BK_REFS is added. */
|
|---|
| 288 | # define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
|
|---|
| 289 | (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
|
|---|
| 290 | | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
|
|---|
| 291 | | RE_NO_BK_PARENS | RE_NO_BK_REFS \
|
|---|
| 292 | | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
|
|---|
| 293 | /* [[[end syntaxes]]] */
|
|---|
| 294 |
|
|---|
| 295 | /* Maximum number of duplicates an interval can allow. POSIX-conforming
|
|---|
| 296 | systems might define this in <limits.h>, but we want our
|
|---|
| 297 | value, so remove any previous define. */
|
|---|
| 298 | # ifdef _REGEX_INCLUDE_LIMITS_H
|
|---|
| 299 | # include <limits.h>
|
|---|
| 300 | # endif
|
|---|
| 301 | # ifdef RE_DUP_MAX
|
|---|
| 302 | # undef RE_DUP_MAX
|
|---|
| 303 | # endif
|
|---|
| 304 |
|
|---|
| 305 | /* RE_DUP_MAX is 2**15 - 1 because an earlier implementation stored
|
|---|
| 306 | the counter as a 2-byte signed integer. This is no longer true, so
|
|---|
| 307 | RE_DUP_MAX could be increased to (INT_MAX / 10 - 1), or to
|
|---|
| 308 | ((SIZE_MAX - 9) / 10) if _REGEX_LARGE_OFFSETS is defined.
|
|---|
| 309 | However, there would be a huge performance problem if someone
|
|---|
| 310 | actually used a pattern like a\{214748363\}, so RE_DUP_MAX retains
|
|---|
| 311 | its historical value. */
|
|---|
| 312 | # define RE_DUP_MAX (0x7fff)
|
|---|
| 313 | #endif
|
|---|
| 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 | _REG_NOERROR = 0, /* Success. */
|
|---|
| 360 | _REG_NOMATCH, /* Didn't find a match (for regexec). */
|
|---|
| 361 |
|
|---|
| 362 | /* POSIX regcomp return error codes. (In the order listed in the
|
|---|
| 363 | standard.) */
|
|---|
| 364 | _REG_BADPAT, /* Invalid pattern. */
|
|---|
| 365 | _REG_ECOLLATE, /* Invalid collating element. */
|
|---|
| 366 | _REG_ECTYPE, /* Invalid character class name. */
|
|---|
| 367 | _REG_EESCAPE, /* Trailing backslash. */
|
|---|
| 368 | _REG_ESUBREG, /* Invalid back reference. */
|
|---|
| 369 | _REG_EBRACK, /* Unmatched left bracket. */
|
|---|
| 370 | _REG_EPAREN, /* Parenthesis imbalance. */
|
|---|
| 371 | _REG_EBRACE, /* Unmatched \{. */
|
|---|
| 372 | _REG_BADBR, /* Invalid contents of \{\}. */
|
|---|
| 373 | _REG_ERANGE, /* Invalid range end. */
|
|---|
| 374 | _REG_ESPACE, /* Ran out of memory. */
|
|---|
| 375 | _REG_BADRPT, /* No preceding re for repetition op. */
|
|---|
| 376 |
|
|---|
| 377 | /* Error codes we've added. */
|
|---|
| 378 | _REG_EEND, /* Premature end. */
|
|---|
| 379 | _REG_ESIZE, /* Too large (e.g., repeat count too large). */
|
|---|
| 380 | _REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
|
|---|
| 381 | } reg_errcode_t;
|
|---|
| 382 |
|
|---|
| 383 | #if defined _XOPEN_SOURCE || defined __USE_XOPEN2K
|
|---|
| 384 | # define REG_ENOSYS _REG_ENOSYS
|
|---|
| 385 | #endif
|
|---|
| 386 | #define REG_NOERROR _REG_NOERROR
|
|---|
| 387 | #define REG_NOMATCH _REG_NOMATCH
|
|---|
| 388 | #define REG_BADPAT _REG_BADPAT
|
|---|
| 389 | #define REG_ECOLLATE _REG_ECOLLATE
|
|---|
| 390 | #define REG_ECTYPE _REG_ECTYPE
|
|---|
| 391 | #define REG_EESCAPE _REG_EESCAPE
|
|---|
| 392 | #define REG_ESUBREG _REG_ESUBREG
|
|---|
| 393 | #define REG_EBRACK _REG_EBRACK
|
|---|
| 394 | #define REG_EPAREN _REG_EPAREN
|
|---|
| 395 | #define REG_EBRACE _REG_EBRACE
|
|---|
| 396 | #define REG_BADBR _REG_BADBR
|
|---|
| 397 | #define REG_ERANGE _REG_ERANGE
|
|---|
| 398 | #define REG_ESPACE _REG_ESPACE
|
|---|
| 399 | #define REG_BADRPT _REG_BADRPT
|
|---|
| 400 | #define REG_EEND _REG_EEND
|
|---|
| 401 | #define REG_ESIZE _REG_ESIZE
|
|---|
| 402 | #define REG_ERPAREN _REG_ERPAREN
|
|---|
| 403 | |
|---|
| 404 |
|
|---|
| 405 | /* This data structure represents a compiled pattern. Before calling
|
|---|
| 406 | the pattern compiler, the fields 'buffer', 'allocated', 'fastmap',
|
|---|
| 407 | and 'translate' can be set. After the pattern has been compiled,
|
|---|
| 408 | the fields 're_nsub', 'not_bol' and 'not_eol' are available. All
|
|---|
| 409 | other fields are private to the regex routines. */
|
|---|
| 410 |
|
|---|
| 411 | #ifndef RE_TRANSLATE_TYPE
|
|---|
| 412 | # define __RE_TRANSLATE_TYPE unsigned char *
|
|---|
| 413 | # ifdef __USE_GNU
|
|---|
| 414 | # define RE_TRANSLATE_TYPE __RE_TRANSLATE_TYPE
|
|---|
| 415 | # endif
|
|---|
| 416 | #endif
|
|---|
| 417 |
|
|---|
| 418 | #ifdef __USE_GNU
|
|---|
| 419 | # define __REPB_PREFIX(name) name
|
|---|
| 420 | #else
|
|---|
| 421 | # define __REPB_PREFIX(name) __##name
|
|---|
| 422 | #endif
|
|---|
| 423 |
|
|---|
| 424 | struct re_pattern_buffer
|
|---|
| 425 | {
|
|---|
| 426 | /* Space that holds the compiled pattern. The type
|
|---|
| 427 | 'struct re_dfa_t' is private and is not declared here. */
|
|---|
| 428 | struct re_dfa_t *__REPB_PREFIX(buffer);
|
|---|
| 429 |
|
|---|
| 430 | /* Number of bytes to which 'buffer' points. */
|
|---|
| 431 | __re_long_size_t __REPB_PREFIX(allocated);
|
|---|
| 432 |
|
|---|
| 433 | /* Number of bytes actually used in 'buffer'. */
|
|---|
| 434 | __re_long_size_t __REPB_PREFIX(used);
|
|---|
| 435 |
|
|---|
| 436 | /* Syntax setting with which the pattern was compiled. */
|
|---|
| 437 | reg_syntax_t __REPB_PREFIX(syntax);
|
|---|
| 438 |
|
|---|
| 439 | /* Pointer to a fastmap, if any, otherwise zero. re_search uses the
|
|---|
| 440 | fastmap, if there is one, to skip over impossible starting points
|
|---|
| 441 | for matches. */
|
|---|
| 442 | char *__REPB_PREFIX(fastmap);
|
|---|
| 443 |
|
|---|
| 444 | /* Either a translate table to apply to all characters before
|
|---|
| 445 | comparing them, or zero for no translation. The translation is
|
|---|
| 446 | applied to a pattern when it is compiled and to a string when it
|
|---|
| 447 | is matched. */
|
|---|
| 448 | __RE_TRANSLATE_TYPE __REPB_PREFIX(translate);
|
|---|
| 449 |
|
|---|
| 450 | /* Number of subexpressions found by the compiler. */
|
|---|
| 451 | size_t re_nsub;
|
|---|
| 452 |
|
|---|
| 453 | /* Zero if this pattern cannot match the empty string, one else.
|
|---|
| 454 | Well, in truth it's used only in 're_search_2', to see whether or
|
|---|
| 455 | not we should use the fastmap, so we don't set this absolutely
|
|---|
| 456 | perfectly; see 're_compile_fastmap' (the "duplicate" case). */
|
|---|
| 457 | unsigned __REPB_PREFIX(can_be_null) : 1;
|
|---|
| 458 |
|
|---|
| 459 | /* If REGS_UNALLOCATED, allocate space in the 'regs' structure
|
|---|
| 460 | for 'max (RE_NREGS, re_nsub + 1)' groups.
|
|---|
| 461 | If REGS_REALLOCATE, reallocate space if necessary.
|
|---|
| 462 | If REGS_FIXED, use what's there. */
|
|---|
| 463 | #ifdef __USE_GNU
|
|---|
| 464 | # define REGS_UNALLOCATED 0
|
|---|
| 465 | # define REGS_REALLOCATE 1
|
|---|
| 466 | # define REGS_FIXED 2
|
|---|
| 467 | #endif
|
|---|
| 468 | unsigned __REPB_PREFIX(regs_allocated) : 2;
|
|---|
| 469 |
|
|---|
| 470 | /* Set to zero when 're_compile_pattern' compiles a pattern; set to
|
|---|
| 471 | one by 're_compile_fastmap' if it updates the fastmap. */
|
|---|
| 472 | unsigned __REPB_PREFIX(fastmap_accurate) : 1;
|
|---|
| 473 |
|
|---|
| 474 | /* If set, 're_match_2' does not return information about
|
|---|
| 475 | subexpressions. */
|
|---|
| 476 | unsigned __REPB_PREFIX(no_sub) : 1;
|
|---|
| 477 |
|
|---|
| 478 | /* If set, a beginning-of-line anchor doesn't match at the beginning
|
|---|
| 479 | of the string. */
|
|---|
| 480 | unsigned __REPB_PREFIX(not_bol) : 1;
|
|---|
| 481 |
|
|---|
| 482 | /* Similarly for an end-of-line anchor. */
|
|---|
| 483 | unsigned __REPB_PREFIX(not_eol) : 1;
|
|---|
| 484 |
|
|---|
| 485 | /* If true, an anchor at a newline matches. */
|
|---|
| 486 | unsigned __REPB_PREFIX(newline_anchor) : 1;
|
|---|
| 487 | };
|
|---|
| 488 |
|
|---|
| 489 | typedef struct re_pattern_buffer regex_t;
|
|---|
| 490 | |
|---|
| 491 |
|
|---|
| 492 | /* Type for byte offsets within the string. POSIX mandates this. */
|
|---|
| 493 | #ifdef _REGEX_LARGE_OFFSETS
|
|---|
| 494 | /* POSIX 1003.1-2008 requires that regoff_t be at least as wide as
|
|---|
| 495 | ptrdiff_t and ssize_t. We don't know of any hosts where ptrdiff_t
|
|---|
| 496 | is wider than ssize_t, so ssize_t is safe. */
|
|---|
| 497 | typedef ssize_t regoff_t;
|
|---|
| 498 | #else
|
|---|
| 499 | /* The traditional GNU regex implementation mishandles strings longer
|
|---|
| 500 | than INT_MAX. */
|
|---|
| 501 | typedef int regoff_t;
|
|---|
| 502 | #endif
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 | #ifdef __USE_GNU
|
|---|
| 506 | /* This is the structure we store register match data in. See
|
|---|
| 507 | regex.texinfo for a full description of what registers match. */
|
|---|
| 508 | struct re_registers
|
|---|
| 509 | {
|
|---|
| 510 | __re_size_t num_regs;
|
|---|
| 511 | regoff_t *start;
|
|---|
| 512 | regoff_t *end;
|
|---|
| 513 | };
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 | /* If 'regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
|
|---|
| 517 | 're_match_2' returns information about at least this many registers
|
|---|
| 518 | the first time a 'regs' structure is passed. */
|
|---|
| 519 | # ifndef RE_NREGS
|
|---|
| 520 | # define RE_NREGS 30
|
|---|
| 521 | # endif
|
|---|
| 522 | #endif
|
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 | /* POSIX specification for registers. Aside from the different names than
|
|---|
| 526 | 're_registers', POSIX uses an array of structures, instead of a
|
|---|
| 527 | structure of arrays. */
|
|---|
| 528 | typedef struct
|
|---|
| 529 | {
|
|---|
| 530 | regoff_t rm_so; /* Byte offset from string's start to substring's start. */
|
|---|
| 531 | regoff_t rm_eo; /* Byte offset from string's start to substring's end. */
|
|---|
| 532 | } regmatch_t;
|
|---|
| 533 | |
|---|
| 534 |
|
|---|
| 535 | /* Declarations for routines. */
|
|---|
| 536 |
|
|---|
| 537 | #ifdef __USE_GNU
|
|---|
| 538 | /* Sets the current default syntax to SYNTAX, and return the old syntax.
|
|---|
| 539 | You can also simply assign to the 're_syntax_options' variable. */
|
|---|
| 540 | extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
|
|---|
| 541 |
|
|---|
| 542 | /* Compile the regular expression PATTERN, with length LENGTH
|
|---|
| 543 | and syntax given by the global 're_syntax_options', into the buffer
|
|---|
| 544 | BUFFER. Return NULL if successful, and an error string if not.
|
|---|
| 545 |
|
|---|
| 546 | To free the allocated storage, you must call 'regfree' on BUFFER.
|
|---|
| 547 | Note that the translate table must either have been initialised by
|
|---|
| 548 | 'regcomp', with a malloc'ed value, or set to NULL before calling
|
|---|
| 549 | 'regfree'. */
|
|---|
| 550 | extern const char *re_compile_pattern (const char *__pattern, size_t __length,
|
|---|
| 551 | struct re_pattern_buffer *__buffer);
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 | /* Compile a fastmap for the compiled pattern in BUFFER; used to
|
|---|
| 555 | accelerate searches. Return 0 if successful and -2 if was an
|
|---|
| 556 | internal error. */
|
|---|
| 557 | extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 | /* Search in the string STRING (with length LENGTH) for the pattern
|
|---|
| 561 | compiled into BUFFER. Start searching at position START, for RANGE
|
|---|
| 562 | characters. Return the starting position of the match, -1 for no
|
|---|
| 563 | match, or -2 for an internal error. Also return register
|
|---|
| 564 | information in REGS (if REGS and BUFFER->no_sub are nonzero). */
|
|---|
| 565 | extern regoff_t re_search (struct re_pattern_buffer *__buffer,
|
|---|
| 566 | const char *__string, __re_idx_t __length,
|
|---|
| 567 | __re_idx_t __start, regoff_t __range,
|
|---|
| 568 | struct re_registers *__regs);
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 | /* Like 're_search', but search in the concatenation of STRING1 and
|
|---|
| 572 | STRING2. Also, stop searching at index START + STOP. */
|
|---|
| 573 | extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
|
|---|
| 574 | const char *__string1, __re_idx_t __length1,
|
|---|
| 575 | const char *__string2, __re_idx_t __length2,
|
|---|
| 576 | __re_idx_t __start, regoff_t __range,
|
|---|
| 577 | struct re_registers *__regs,
|
|---|
| 578 | __re_idx_t __stop);
|
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 | /* Like 're_search', but return how many characters in STRING the regexp
|
|---|
| 582 | in BUFFER matched, starting at position START. */
|
|---|
| 583 | extern regoff_t re_match (struct re_pattern_buffer *__buffer,
|
|---|
| 584 | const char *__string, __re_idx_t __length,
|
|---|
| 585 | __re_idx_t __start, struct re_registers *__regs);
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 | /* Relates to 're_match' as 're_search_2' relates to 're_search'. */
|
|---|
| 589 | extern regoff_t re_match_2 (struct re_pattern_buffer *__buffer,
|
|---|
| 590 | const char *__string1, __re_idx_t __length1,
|
|---|
| 591 | const char *__string2, __re_idx_t __length2,
|
|---|
| 592 | __re_idx_t __start, struct re_registers *__regs,
|
|---|
| 593 | __re_idx_t __stop);
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 | /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
|
|---|
| 597 | ENDS. Subsequent matches using BUFFER and REGS will use this memory
|
|---|
| 598 | for recording register information. STARTS and ENDS must be
|
|---|
| 599 | allocated with malloc, and must each be at least 'NUM_REGS * sizeof
|
|---|
| 600 | (regoff_t)' bytes long.
|
|---|
| 601 |
|
|---|
| 602 | If NUM_REGS == 0, then subsequent matches should allocate their own
|
|---|
| 603 | register data.
|
|---|
| 604 |
|
|---|
| 605 | Unless this function is called, the first search or match using
|
|---|
| 606 | BUFFER will allocate its own register data, without
|
|---|
| 607 | freeing the old data. */
|
|---|
| 608 | extern void re_set_registers (struct re_pattern_buffer *__buffer,
|
|---|
| 609 | struct re_registers *__regs,
|
|---|
| 610 | __re_size_t __num_regs,
|
|---|
| 611 | regoff_t *__starts, regoff_t *__ends);
|
|---|
| 612 | #endif /* Use GNU */
|
|---|
| 613 |
|
|---|
| 614 | #if defined _REGEX_RE_COMP || (defined _LIBC && defined __USE_BSD)
|
|---|
| 615 | # ifndef _CRAY
|
|---|
| 616 | /* 4.2 bsd compatibility. */
|
|---|
| 617 | extern char *re_comp (const char *);
|
|---|
| 618 | extern int re_exec (const char *);
|
|---|
| 619 | # endif
|
|---|
| 620 | #endif
|
|---|
| 621 |
|
|---|
| 622 | /* GCC 2.95 and later have "__restrict"; C99 compilers have
|
|---|
| 623 | "restrict", and "configure" may have defined "restrict".
|
|---|
| 624 | Other compilers use __restrict, __restrict__, and _Restrict, and
|
|---|
| 625 | 'configure' might #define 'restrict' to those words, so pick a
|
|---|
| 626 | different name. */
|
|---|
| 627 | #ifndef _Restrict_
|
|---|
| 628 | # if 199901L <= __STDC_VERSION__
|
|---|
| 629 | # define _Restrict_ restrict
|
|---|
| 630 | # elif 2 < __GNUC__ || (2 == __GNUC__ && 95 <= __GNUC_MINOR__)
|
|---|
| 631 | # define _Restrict_ __restrict
|
|---|
| 632 | # else
|
|---|
| 633 | # define _Restrict_
|
|---|
| 634 | # endif
|
|---|
| 635 | #endif
|
|---|
| 636 | /* gcc 3.1 and up support the [restrict] syntax. Don't trust
|
|---|
| 637 | sys/cdefs.h's definition of __restrict_arr, though, as it
|
|---|
| 638 | mishandles gcc -ansi -pedantic. */
|
|---|
| 639 | #ifndef _Restrict_arr_
|
|---|
| 640 | # if ((199901L <= __STDC_VERSION__ \
|
|---|
| 641 | || ((3 < __GNUC__ || (3 == __GNUC__ && 1 <= __GNUC_MINOR__)) \
|
|---|
| 642 | && !defined __STRICT_ANSI__)) \
|
|---|
| 643 | && !defined __GNUG__)
|
|---|
| 644 | # define _Restrict_arr_ _Restrict_
|
|---|
| 645 | # else
|
|---|
| 646 | # define _Restrict_arr_
|
|---|
| 647 | # endif
|
|---|
| 648 | #endif
|
|---|
| 649 |
|
|---|
| 650 | /* POSIX compatibility. */
|
|---|
| 651 | extern int regcomp (regex_t *_Restrict_ __preg,
|
|---|
| 652 | const char *_Restrict_ __pattern,
|
|---|
| 653 | int __cflags);
|
|---|
| 654 |
|
|---|
| 655 | extern int regexec (const regex_t *_Restrict_ __preg,
|
|---|
| 656 | const char *_Restrict_ __string, size_t __nmatch,
|
|---|
| 657 | regmatch_t __pmatch[_Restrict_arr_],
|
|---|
| 658 | int __eflags);
|
|---|
| 659 |
|
|---|
| 660 | extern size_t regerror (int __errcode, const regex_t *_Restrict_ __preg,
|
|---|
| 661 | char *_Restrict_ __errbuf, size_t __errbuf_size);
|
|---|
| 662 |
|
|---|
| 663 | extern void regfree (regex_t *__preg);
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 | #ifdef __cplusplus
|
|---|
| 667 | }
|
|---|
| 668 | #endif /* C++ */
|
|---|
| 669 |
|
|---|
| 670 | #endif /* regex.h */
|
|---|