00001 /* Definitions for data structures and routines for the regular 00002 expression library, version 0.12. 00003 00004 Copyright (C) 1985, 89, 90, 91, 92, 93, 95 Free Software Foundation, Inc. 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2, or (at your option) 00009 any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 00019 00020 #ifndef __REGEXP_LIBRARY_H__ 00021 #define __REGEXP_LIBRARY_H__ 00022 00023 /* POSIX says that <sys/types.h> must be included (by the caller) before 00024 <regex.h>. */ 00025 00026 #if !defined (_POSIX_C_SOURCE) && !defined (_POSIX_SOURCE) && defined (VMS) 00027 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it 00028 should be there. */ 00029 #include <stddef.h> 00030 #endif 00031 00032 00033 /* The following bits are used to determine the regexp syntax we 00034 recognize. The set/not-set meanings are chosen so that Emacs syntax 00035 remains the value 0. The bits are given in alphabetical order, and 00036 the definitions shifted by one from the previous bit; thus, when we 00037 add or remove a bit, only one other definition need change. */ 00038 typedef unsigned reg_syntax_t; 00039 00040 /* If this bit is not set, then \ inside a bracket expression is literal. 00041 If set, then such a \ quotes the following character. */ 00042 #define RE_BACKSLASH_ESCAPE_IN_LISTS (1) 00043 00044 /* If this bit is not set, then + and ? are operators, and \+ and \? are 00045 literals. 00046 If set, then \+ and \? are operators and + and ? are literals. */ 00047 #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1) 00048 00049 /* If this bit is set, then character classes are supported. They are: 00050 [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:], 00051 [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:]. 00052 If not set, then character classes are not supported. */ 00053 #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1) 00054 00055 /* If this bit is set, then ^ and $ are always anchors (outside bracket 00056 expressions, of course). 00057 If this bit is not set, then it depends: 00058 ^ is an anchor if it is at the beginning of a regular 00059 expression or after an open-group or an alternation operator; 00060 $ is an anchor if it is at the end of a regular expression, or 00061 before a close-group or an alternation operator. 00062 00063 This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because 00064 POSIX draft 11.2 says that * etc. in leading positions is undefined. 00065 We already implemented a previous draft which made those constructs 00066 invalid, though, so we haven't changed the code back. */ 00067 #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1) 00068 00069 /* If this bit is set, then special characters are always special 00070 regardless of where they are in the pattern. 00071 If this bit is not set, then special characters are special only in 00072 some contexts; otherwise they are ordinary. Specifically, 00073 * + ? and intervals are only special when not after the beginning, 00074 open-group, or alternation operator. */ 00075 #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1) 00076 00077 /* If this bit is set, then *, +, ?, and { cannot be first in an re or 00078 immediately after an alternation or begin-group operator. */ 00079 #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1) 00080 00081 /* If this bit is set, then . matches newline. 00082 If not set, then it doesn't. */ 00083 #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1) 00084 00085 /* If this bit is set, then . doesn't match NUL. 00086 If not set, then it does. */ 00087 #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1) 00088 00089 /* If this bit is set, nonmatching lists [^...] do not match newline. 00090 If not set, they do. */ 00091 #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1) 00092 00093 /* If this bit is set, either \{...\} or {...} defines an 00094 interval, depending on RE_NO_BK_BRACES. 00095 If not set, \{, \}, {, and } are literals. */ 00096 #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1) 00097 00098 /* If this bit is set, +, ? and | aren't recognized as operators. 00099 If not set, they are. */ 00100 #define RE_LIMITED_OPS (RE_INTERVALS << 1) 00101 00102 /* If this bit is set, newline is an alternation operator. 00103 If not set, newline is literal. */ 00104 #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1) 00105 00106 /* If this bit is set, then `{...}' defines an interval, and \{ and \} 00107 are literals. 00108 If not set, then `\{...\}' defines an interval. */ 00109 #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1) 00110 00111 /* If this bit is set, (...) defines a group, and \( and \) are literals. 00112 If not set, \(...\) defines a group, and ( and ) are literals. */ 00113 #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1) 00114 00115 /* If this bit is set, then <digit> matches <digit>. 00116 If not set, then <digit> is a back-reference. */ 00117 #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1) 00118 00119 /* If this bit is set, then | is an alternation operator, and \| is literal. 00120 If not set, then \| is an alternation operator, and | is literal. */ 00121 #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1) 00122 00123 /* If this bit is set, then an ending range point collating higher 00124 than the starting range point, as in [z-a], is invalid. 00125 If not set, then when ending range point collates higher than the 00126 starting range point, the range is ignored. */ 00127 #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1) 00128 00129 /* If this bit is set, then an unmatched ) is ordinary. 00130 If not set, then an unmatched ) is invalid. */ 00131 #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1) 00132 00133 /* If this bit is set, succeed as soon as we match the whole pattern, 00134 without further backtracking. */ 00135 #define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1) 00136 00137 /* This global variable defines the particular regexp syntax to use (for 00138 some interfaces). When a regexp is compiled, the syntax used is 00139 stored in the pattern buffer, so changing this does not affect 00140 already-compiled regexps. */ 00141 extern reg_syntax_t re_syntax_options; 00142 00143 /* Define combinations of the above bits for the standard possibilities. 00144 (The [[[ comments delimit what gets put into the Texinfo file, so 00145 don't delete them!) */ 00146 /* [[[begin syntaxes]]] */ 00147 #define RE_SYNTAX_EMACS 0 00148 00149 #define RE_SYNTAX_AWK \ 00150 (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \ 00151 | RE_NO_BK_PARENS | RE_NO_BK_REFS \ 00152 | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \ 00153 | RE_UNMATCHED_RIGHT_PAREN_ORD) 00154 00155 #define RE_SYNTAX_POSIX_AWK \ 00156 (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS) 00157 00158 #define RE_SYNTAX_GREP \ 00159 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \ 00160 | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \ 00161 | RE_NEWLINE_ALT) 00162 00163 #define RE_SYNTAX_EGREP \ 00164 (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \ 00165 | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \ 00166 | RE_NEWLINE_ALT | RE_NO_BK_PARENS \ 00167 | RE_NO_BK_VBAR) 00168 00169 #define RE_SYNTAX_POSIX_EGREP \ 00170 (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES) 00171 00172 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */ 00173 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC 00174 00175 #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC 00176 00177 /* Syntax bits common to both basic and extended POSIX regex syntax. */ 00178 #define _RE_SYNTAX_POSIX_COMMON \ 00179 (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \ 00180 | RE_INTERVALS | RE_NO_EMPTY_RANGES) 00181 00182 #define RE_SYNTAX_POSIX_BASIC \ 00183 (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM) 00184 00185 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes 00186 RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this 00187 isn't minimal, since other operators, such as \`, aren't disabled. */ 00188 #define RE_SYNTAX_POSIX_MINIMAL_BASIC \ 00189 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS) 00190 00191 #define RE_SYNTAX_POSIX_EXTENDED \ 00192 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ 00193 | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \ 00194 | RE_NO_BK_PARENS | RE_NO_BK_VBAR \ 00195 | RE_UNMATCHED_RIGHT_PAREN_ORD) 00196 00197 /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS 00198 replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */ 00199 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \ 00200 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ 00201 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \ 00202 | RE_NO_BK_PARENS | RE_NO_BK_REFS \ 00203 | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD) 00204 /* [[[end syntaxes]]] */ 00205 00206 /* Maximum number of duplicates an interval can allow. Some systems 00207 (erroneously) define this in other header files, but we want our 00208 value, so remove any previous define. */ 00209 #ifdef RE_DUP_MAX 00210 #undef RE_DUP_MAX 00211 #endif 00212 #define RE_DUP_MAX ((1 << 15) - 1) 00213 00214 00215 /* POSIX `cflags' bits (i.e., information for `regcomp'). */ 00216 00217 /* If this bit is set, then use extended regular expression syntax. 00218 If not set, then use basic regular expression syntax. */ 00219 #define REG_EXTENDED 1 00220 00221 /* If this bit is set, then ignore case when matching. 00222 If not set, then case is significant. */ 00223 #define REG_ICASE (REG_EXTENDED << 1) 00224 00225 /* If this bit is set, then anchors do not match at newline 00226 characters in the string. 00227 If not set, then anchors do match at newlines. */ 00228 #define REG_NEWLINE (REG_ICASE << 1) 00229 00230 /* If this bit is set, then report only success or fail in regexec. 00231 If not set, then returns differ between not matching and errors. */ 00232 #define REG_NOSUB (REG_NEWLINE << 1) 00233 00234 00235 /* POSIX `eflags' bits (i.e., information for regexec). */ 00236 00237 /* If this bit is set, then the beginning-of-line operator doesn't match 00238 the beginning of the string (presumably because it's not the 00239 beginning of a line). 00240 If not set, then the beginning-of-line operator does match the 00241 beginning of the string. */ 00242 #define REG_NOTBOL 1 00243 00244 /* Like REG_NOTBOL, except for the end-of-line. */ 00245 #define REG_NOTEOL (1 << 1) 00246 00247 00248 /* If any error codes are removed, changed, or added, update the 00249 `re_error_msg' table in regex.c. */ 00250 typedef enum 00251 { 00252 REG_NOERROR = 0, /* Success. */ 00253 REG_NOMATCH, /* Didn't find a match (for regexec). */ 00254 00255 /* POSIX regcomp return error codes. (In the order listed in the 00256 standard.) */ 00257 REG_BADPAT, /* Invalid pattern. */ 00258 REG_ECOLLATE, /* Not implemented. */ 00259 REG_ECTYPE, /* Invalid character class name. */ 00260 REG_EESCAPE, /* Trailing backslash. */ 00261 REG_ESUBREG, /* Invalid back reference. */ 00262 REG_EBRACK, /* Unmatched left bracket. */ 00263 REG_EPAREN, /* Parenthesis imbalance. */ 00264 REG_EBRACE, /* Unmatched \{. */ 00265 REG_BADBR, /* Invalid contents of \{\}. */ 00266 REG_ERANGE, /* Invalid range end. */ 00267 REG_ESPACE, /* Ran out of memory. */ 00268 REG_BADRPT, /* No preceding re for repetition op. */ 00269 00270 /* Error codes we've added. */ 00271 REG_EEND, /* Premature end. */ 00272 REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */ 00273 REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */ 00274 } reg_errcode_t; 00275 00276 /* This data structure represents a compiled pattern. Before calling 00277 the pattern compiler, the fields `buffer', `allocated', `fastmap', 00278 `translate', and `no_sub' can be set. After the pattern has been 00279 compiled, the `re_nsub' field is available. All other fields are 00280 private to the regex routines. */ 00281 00282 struct re_pattern_buffer 00283 { 00284 /* [[[begin pattern_buffer]]] */ 00285 /* Space that holds the compiled pattern. It is declared as 00286 `unsigned char *' because its elements are 00287 sometimes used as array indexes. */ 00288 unsigned char *buffer; 00289 00290 /* Number of bytes to which `buffer' points. */ 00291 unsigned long allocated; 00292 00293 /* Number of bytes actually used in `buffer'. */ 00294 unsigned long used; 00295 00296 /* Syntax setting with which the pattern was compiled. */ 00297 reg_syntax_t syntax; 00298 00299 /* Pointer to a fastmap, if any, otherwise zero. re_search uses 00300 the fastmap, if there is one, to skip over impossible 00301 starting points for matches. */ 00302 char *fastmap; 00303 00304 /* Either a translate table to apply to all characters before 00305 comparing them, or zero for no translation. The translation 00306 is applied to a pattern when it is compiled and to a string 00307 when it is matched. */ 00308 char *translate; 00309 00310 /* Number of subexpressions found by the compiler. */ 00311 size_t re_nsub; 00312 00313 /* Zero if this pattern cannot match the empty string, one else. 00314 Well, in truth it's used only in `re_search_2', to see 00315 whether or not we should use the fastmap, so we don't set 00316 this absolutely perfectly; see `re_compile_fastmap' (the 00317 `duplicate' case). */ 00318 unsigned can_be_null : 1; 00319 00320 /* If REGS_UNALLOCATED, allocate space in the `regs' structure 00321 for `max (RE_NREGS, re_nsub + 1)' groups. 00322 If REGS_REALLOCATE, reallocate space if necessary. 00323 If REGS_FIXED, use what's there. */ 00324 #define REGS_UNALLOCATED 0 00325 #define REGS_REALLOCATE 1 00326 #define REGS_FIXED 2 00327 unsigned regs_allocated : 2; 00328 00329 /* Set to zero when `regex_compile' compiles a pattern; set to one 00330 by `re_compile_fastmap' if it updates the fastmap. */ 00331 unsigned fastmap_accurate : 1; 00332 00333 /* If set, `re_match_2' does not return information about 00334 subexpressions. */ 00335 unsigned no_sub : 1; 00336 00337 /* If set, a beginning-of-line anchor doesn't match at the 00338 beginning of the string. */ 00339 unsigned not_bol : 1; 00340 00341 /* Similarly for an end-of-line anchor. */ 00342 unsigned not_eol : 1; 00343 00344 /* If true, an anchor at a newline matches. */ 00345 unsigned newline_anchor : 1; 00346 00347 /* [[[end pattern_buffer]]] */ 00348 }; 00349 00350 typedef struct re_pattern_buffer regex_t; 00351 00352 /* Type for byte offsets within the string. POSIX mandates this. */ 00353 typedef int regoff_t; 00354 00355 00356 /* This is the structure we store register match data in. See 00357 regex.texinfo for a full description of what registers match. */ 00358 struct re_registers 00359 { 00360 unsigned num_regs; 00361 regoff_t *start; 00362 regoff_t *end; 00363 }; 00364 00365 00366 /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer, 00367 `re_match_2' returns information about at least this many registers 00368 the first time a `regs' structure is passed. */ 00369 #ifndef RE_NREGS 00370 #define RE_NREGS 30 00371 #endif 00372 00373 00374 /* POSIX specification for registers. Aside from the different names than 00375 `re_registers', POSIX uses an array of structures, instead of a 00376 structure of arrays. */ 00377 typedef struct 00378 { 00379 regoff_t rm_so; /* Byte offset from string's start to substring's start. */ 00380 regoff_t rm_eo; /* Byte offset from string's start to substring's end. */ 00381 } regmatch_t; 00382 00383 /* Declarations for routines. */ 00384 00385 /* To avoid duplicating every routine declaration -- once with a 00386 prototype (if we are ANSI), and once without (if we aren't) -- we 00387 use the following macro to declare argument types. This 00388 unfortunately clutters up the declarations a bit, but I think it's 00389 worth it. */ 00390 00391 #if __STDC__ 00392 00393 #define _RE_ARGS(args) args 00394 00395 #else /* not __STDC__ */ 00396 00397 #define _RE_ARGS(args) () 00398 00399 #endif /* not __STDC__ */ 00400 00401 /* Sets the current default syntax to SYNTAX, and return the old syntax. 00402 You can also simply assign to the `re_syntax_options' variable. */ 00403 extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax)); 00404 00405 /* Compile the regular expression PATTERN, with length LENGTH 00406 and syntax given by the global `re_syntax_options', into the buffer 00407 BUFFER. Return NULL if successful, and an error string if not. */ 00408 extern const char *re_compile_pattern 00409 _RE_ARGS ((const char *pattern, int length, 00410 struct re_pattern_buffer *buffer)); 00411 00412 00413 /* Compile a fastmap for the compiled pattern in BUFFER; used to 00414 accelerate searches. Return 0 if successful and -2 if was an 00415 internal error. */ 00416 extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer)); 00417 00418 00419 /* Search in the string STRING (with length LENGTH) for the pattern 00420 compiled into BUFFER. Start searching at position START, for RANGE 00421 characters. Return the starting position of the match, -1 for no 00422 match, or -2 for an internal error. Also return register 00423 information in REGS (if REGS and BUFFER->no_sub are nonzero). */ 00424 extern int re_search 00425 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string, 00426 int length, int start, int range, struct re_registers *regs)); 00427 00428 00429 /* Like `re_search', but search in the concatenation of STRING1 and 00430 STRING2. Also, stop searching at index START + STOP. */ 00431 extern int re_search_2 00432 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1, 00433 int length1, const char *string2, int length2, 00434 int start, int range, struct re_registers *regs, int stop)); 00435 00436 00437 /* Like `re_search', but return how many characters in STRING the regexp 00438 in BUFFER matched, starting at position START. */ 00439 extern int re_match 00440 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string, 00441 int length, int start, struct re_registers *regs)); 00442 00443 00444 /* Relates to `re_match' as `re_search_2' relates to `re_search'. */ 00445 extern int re_match_2 00446 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1, 00447 int length1, const char *string2, int length2, 00448 int start, struct re_registers *regs, int stop)); 00449 00450 00451 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and 00452 ENDS. Subsequent matches using BUFFER and REGS will use this memory 00453 for recording register information. STARTS and ENDS must be 00454 allocated with malloc, and must each be at least `NUM_REGS * sizeof 00455 (regoff_t)' bytes long. 00456 00457 If NUM_REGS == 0, then subsequent matches should allocate their own 00458 register data. 00459 00460 Unless this function is called, the first search or match using 00461 PATTERN_BUFFER will allocate its own register data, without 00462 freeing the old data. */ 00463 extern void re_set_registers 00464 _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs, 00465 unsigned num_regs, regoff_t *starts, regoff_t *ends)); 00466 00467 #ifdef _REGEX_RE_COMP 00468 /* 4.2 bsd compatibility. */ 00469 extern char *re_comp _RE_ARGS ((const char *)); 00470 extern int re_exec _RE_ARGS ((const char *)); 00471 #endif 00472 00473 /* POSIX compatibility. */ 00474 extern int regcomp _RE_ARGS ((regex_t *preg, const char *pattern, int cflags)); 00475 extern int regexec 00476 _RE_ARGS ((const regex_t *preg, const char *string, size_t nmatch, 00477 regmatch_t pmatch[], int eflags)); 00478 extern size_t regerror 00479 _RE_ARGS ((int errcode, const regex_t *preg, char *errbuf, 00480 size_t errbuf_size)); 00481 extern void regfree _RE_ARGS ((regex_t *preg)); 00482 00483 #endif /* not __REGEXP_LIBRARY_H__ */ 00484 00485 /* 00486 Local variables: 00487 make-backup-files: t 00488 version-control: t 00489 trim-versions-without-asking: nil 00490 End: 00491 */