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