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