regex.h

00001 /* Definitions for data structures and routines for the regular
00002    expression library, version 0.12.
00003 
00004    Copyright (C) 1985, 1989, 1990, 1991, 1992, 1993 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 _REGEX_H
00021 #define _REGEX_H 1
00022 
00023 /* POSIX says that <sys/types.h> must be included (by the caller) before
00024    <regex.h>.  */
00025 
00026 #ifdef 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 /* This global variable defines the particular regexp syntax to use (for
00134    some interfaces).  When a regexp is compiled, the syntax used is
00135    stored in the pattern buffer, so changing this does not affect
00136    already-compiled regexps.  */
00137 extern reg_syntax_t re_syntax_options;
00138 
00139 /* Define combinations of the above bits for the standard possibilities.
00140    (The [[[ comments delimit what gets put into the Texinfo file, so
00141    don't delete them!)  */ 
00142 /* [[[begin syntaxes]]] */
00143 #define RE_SYNTAX_EMACS 0
00144 
00145 #define RE_SYNTAX_AWK                                                   \
00146   (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL                       \
00147    | RE_NO_BK_PARENS            | RE_NO_BK_REFS                         \
00148    | RE_NO_BK_VBAR               | RE_NO_EMPTY_RANGES                   \
00149    | RE_UNMATCHED_RIGHT_PAREN_ORD)
00150 
00151 #define RE_SYNTAX_POSIX_AWK                                             \
00152   (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS)
00153 
00154 #define RE_SYNTAX_GREP                                                  \
00155   (RE_BK_PLUS_QM              | RE_CHAR_CLASSES                         \
00156    | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS                            \
00157    | RE_NEWLINE_ALT)
00158 
00159 #define RE_SYNTAX_EGREP                                                 \
00160   (RE_CHAR_CLASSES        | RE_CONTEXT_INDEP_ANCHORS                    \
00161    | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE                    \
00162    | RE_NEWLINE_ALT       | RE_NO_BK_PARENS                             \
00163    | RE_NO_BK_VBAR)
00164 
00165 #define RE_SYNTAX_POSIX_EGREP                                           \
00166   (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
00167 
00168 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff.  */
00169 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
00170 
00171 #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
00172 
00173 /* Syntax bits common to both basic and extended POSIX regex syntax.  */
00174 #define _RE_SYNTAX_POSIX_COMMON                                         \
00175   (RE_CHAR_CLASSES | RE_DOT_NEWLINE      | RE_DOT_NOT_NULL              \
00176    | RE_INTERVALS  | RE_NO_EMPTY_RANGES)
00177 
00178 #define RE_SYNTAX_POSIX_BASIC                                           \
00179   (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
00180 
00181 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
00182    RE_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
00183    isn't minimal, since other operators, such as \`, aren't disabled.  */
00184 #define RE_SYNTAX_POSIX_MINIMAL_BASIC                                   \
00185   (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
00186 
00187 #define RE_SYNTAX_POSIX_EXTENDED                                        \
00188   (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS                   \
00189    | RE_CONTEXT_INDEP_OPS  | RE_NO_BK_BRACES                            \
00190    | RE_NO_BK_PARENS       | RE_NO_BK_VBAR                              \
00191    | RE_UNMATCHED_RIGHT_PAREN_ORD)
00192 
00193 /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
00194    replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added.  */
00195 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED                                \
00196   (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS                  \
00197    | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES                           \
00198    | RE_NO_BK_PARENS        | RE_NO_BK_REFS                             \
00199    | RE_NO_BK_VBAR          | RE_UNMATCHED_RIGHT_PAREN_ORD)
00200 /* [[[end syntaxes]]] */
00201 
00202 /* Maximum number of duplicates an interval can allow.  Some systems
00203    (erroneously) define this in other header files, but we want our
00204    value, so remove any previous define.  */
00205 #ifdef RE_DUP_MAX
00206 #undef RE_DUP_MAX
00207 #endif
00208 #define RE_DUP_MAX ((1 << 15) - 1) 
00209 
00210 
00211 /* POSIX `cflags' bits (i.e., information for `regcomp').  */
00212 
00213 /* If this bit is set, then use extended regular expression syntax.
00214    If not set, then use basic regular expression syntax.  */
00215 #define REG_EXTENDED 1
00216 
00217 /* If this bit is set, then ignore case when matching.
00218    If not set, then case is significant.  */
00219 #define REG_ICASE (REG_EXTENDED << 1)
00220  
00221 /* If this bit is set, then anchors do not match at newline
00222      characters in the string.
00223    If not set, then anchors do match at newlines.  */
00224 #define REG_NEWLINE (REG_ICASE << 1)
00225 
00226 /* If this bit is set, then report only success or fail in regexec.
00227    If not set, then returns differ between not matching and errors.  */
00228 #define REG_NOSUB (REG_NEWLINE << 1)
00229 
00230 
00231 /* POSIX `eflags' bits (i.e., information for regexec).  */
00232 
00233 /* If this bit is set, then the beginning-of-line operator doesn't match
00234      the beginning of the string (presumably because it's not the
00235      beginning of a line).
00236    If not set, then the beginning-of-line operator does match the
00237      beginning of the string.  */
00238 #define REG_NOTBOL 1
00239 
00240 /* Like REG_NOTBOL, except for the end-of-line.  */
00241 #define REG_NOTEOL (1 << 1)
00242 
00243 
00244 /* If any error codes are removed, changed, or added, update the
00245    `re_error_msg' table in regex.c.  */
00246 typedef enum
00247 {
00248   REG_NOERROR = 0,      /* Success.  */
00249   REG_NOMATCH,          /* Didn't find a match (for regexec).  */
00250 
00251   /* POSIX regcomp return error codes.  (In the order listed in the
00252      standard.)  */
00253   REG_BADPAT,           /* Invalid pattern.  */
00254   REG_ECOLLATE,         /* Not implemented.  */
00255   REG_ECTYPE,           /* Invalid character class name.  */
00256   REG_EESCAPE,          /* Trailing backslash.  */
00257   REG_ESUBREG,          /* Invalid back reference.  */
00258   REG_EBRACK,           /* Unmatched left bracket.  */
00259   REG_EPAREN,           /* Parenthesis imbalance.  */ 
00260   REG_EBRACE,           /* Unmatched \{.  */
00261   REG_BADBR,            /* Invalid contents of \{\}.  */
00262   REG_ERANGE,           /* Invalid range end.  */
00263   REG_ESPACE,           /* Ran out of memory.  */
00264   REG_BADRPT,           /* No preceding re for repetition op.  */
00265 
00266   /* Error codes we've added.  */
00267   REG_EEND,             /* Premature end.  */
00268   REG_ESIZE,            /* Compiled pattern bigger than 2^16 bytes.  */
00269   REG_ERPAREN           /* Unmatched ) or \); not returned from regcomp.  */
00270 } reg_errcode_t;
00271 
00272 /* This data structure represents a compiled pattern.  Before calling
00273    the pattern compiler, the fields `buffer', `allocated', `fastmap',
00274    `translate', and `no_sub' can be set.  After the pattern has been
00275    compiled, the `re_nsub' field is available.  All other fields are
00276    private to the regex routines.  */
00277 
00278 struct re_pattern_buffer
00279 {
00280 /* [[[begin pattern_buffer]]] */
00281         /* Space that holds the compiled pattern.  It is declared as
00282           `unsigned char *' because its elements are
00283            sometimes used as array indexes.  */
00284   unsigned char *buffer;
00285 
00286         /* Number of bytes to which `buffer' points.  */
00287   unsigned long allocated;
00288 
00289         /* Number of bytes actually used in `buffer'.  */
00290   unsigned long used;   
00291 
00292         /* Syntax setting with which the pattern was compiled.  */
00293   reg_syntax_t syntax;
00294 
00295         /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
00296            the fastmap, if there is one, to skip over impossible
00297            starting points for matches.  */
00298   char *fastmap;
00299 
00300         /* Either a translate table to apply to all characters before
00301            comparing them, or zero for no translation.  The translation
00302            is applied to a pattern when it is compiled and to a string
00303            when it is matched.  */
00304   char *translate;
00305 
00306         /* Number of subexpressions found by the compiler.  */
00307   size_t re_nsub;
00308 
00309         /* Zero if this pattern cannot match the empty string, one else.
00310            Well, in truth it's used only in `re_search_2', to see
00311            whether or not we should use the fastmap, so we don't set
00312            this absolutely perfectly; see `re_compile_fastmap' (the
00313            `duplicate' case).  */
00314   unsigned can_be_null : 1;
00315 
00316         /* If REGS_UNALLOCATED, allocate space in the `regs' structure
00317              for `max (RE_NREGS, re_nsub + 1)' groups.
00318            If REGS_REALLOCATE, reallocate space if necessary.
00319            If REGS_FIXED, use what's there.  */
00320 #define REGS_UNALLOCATED 0
00321 #define REGS_REALLOCATE 1
00322 #define REGS_FIXED 2
00323   unsigned regs_allocated : 2;
00324 
00325         /* Set to zero when `regex_compile' compiles a pattern; set to one
00326            by `re_compile_fastmap' if it updates the fastmap.  */
00327   unsigned fastmap_accurate : 1;
00328 
00329         /* If set, `re_match_2' does not return information about
00330            subexpressions.  */
00331   unsigned no_sub : 1;
00332 
00333         /* If set, a beginning-of-line anchor doesn't match at the
00334            beginning of the string.  */ 
00335   unsigned not_bol : 1;
00336 
00337         /* Similarly for an end-of-line anchor.  */
00338   unsigned not_eol : 1;
00339 
00340         /* If true, an anchor at a newline matches.  */
00341   unsigned newline_anchor : 1;
00342 
00343 /* [[[end pattern_buffer]]] */
00344 };
00345 
00346 typedef struct re_pattern_buffer regex_t;
00347 
00348 
00349 /* search.c (search_buffer) in Emacs needs this one opcode value.  It is
00350    defined both in `regex.c' and here.  */
00351 #define RE_EXACTN_VALUE 1
00352 
00353 /* Type for byte offsets within the string.  POSIX mandates this.  */
00354 typedef int regoff_t;
00355 
00356 
00357 /* This is the structure we store register match data in.  See
00358    regex.texinfo for a full description of what registers match.  */
00359 struct re_registers
00360 {
00361   unsigned num_regs;
00362   regoff_t *start;
00363   regoff_t *end;
00364 };
00365 
00366 
00367 /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
00368    `re_match_2' returns information about at least this many registers
00369    the first time a `regs' structure is passed.  */
00370 #ifndef RE_NREGS
00371 #define RE_NREGS 30
00372 #endif
00373 
00374 
00375 /* POSIX specification for registers.  Aside from the different names than
00376    `re_registers', POSIX uses an array of structures, instead of a
00377    structure of arrays.  */
00378 typedef struct
00379 {
00380   regoff_t rm_so;  /* Byte offset from string's start to substring's start.  */
00381   regoff_t rm_eo;  /* Byte offset from string's start to substring's end.  */
00382 } regmatch_t;
00383 
00384 /* Declarations for routines.  */
00385 
00386 /* To avoid duplicating every routine declaration -- once with a
00387    prototype (if we are ANSI), and once without (if we aren't) -- we
00388    use the following macro to declare argument types.  This
00389    unfortunately clutters up the declarations a bit, but I think it's
00390    worth it.  */
00391 
00392 #if __STDC__
00393 
00394 #define _RE_ARGS(args) args
00395 
00396 #else /* not __STDC__ */
00397 
00398 #define _RE_ARGS(args) ()
00399 
00400 #endif /* not __STDC__ */
00401 
00402 /* Sets the current default syntax to SYNTAX, and return the old syntax.
00403    You can also simply assign to the `re_syntax_options' variable.  */
00404 extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
00405 
00406 /* Compile the regular expression PATTERN, with length LENGTH
00407    and syntax given by the global `re_syntax_options', into the buffer
00408    BUFFER.  Return NULL if successful, and an error string if not.  */
00409 extern const char *re_compile_pattern
00410   _RE_ARGS ((const char *pattern, int length,
00411              struct re_pattern_buffer *buffer));
00412 
00413 
00414 /* Compile a fastmap for the compiled pattern in BUFFER; used to
00415    accelerate searches.  Return 0 if successful and -2 if was an
00416    internal error.  */
00417 extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
00418 
00419 
00420 /* Search in the string STRING (with length LENGTH) for the pattern
00421    compiled into BUFFER.  Start searching at position START, for RANGE
00422    characters.  Return the starting position of the match, -1 for no
00423    match, or -2 for an internal error.  Also return register
00424    information in REGS (if REGS and BUFFER->no_sub are nonzero).  */
00425 extern int re_search
00426   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
00427             int length, int start, int range, struct re_registers *regs));
00428 
00429 
00430 /* Like `re_search', but search in the concatenation of STRING1 and
00431    STRING2.  Also, stop searching at index START + STOP.  */
00432 extern int re_search_2
00433   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
00434              int length1, const char *string2, int length2,
00435              int start, int range, struct re_registers *regs, int stop));
00436 
00437 
00438 /* Like `re_search', but return how many characters in STRING the regexp
00439    in BUFFER matched, starting at position START.  */
00440 extern int re_match
00441   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
00442              int length, int start, struct re_registers *regs));
00443 
00444 
00445 /* Relates to `re_match' as `re_search_2' relates to `re_search'.  */
00446 extern int re_match_2 
00447   _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
00448              int length1, const char *string2, int length2,
00449              int start, struct re_registers *regs, int stop));
00450 
00451 
00452 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
00453    ENDS.  Subsequent matches using BUFFER and REGS will use this memory
00454    for recording register information.  STARTS and ENDS must be
00455    allocated with malloc, and must each be at least `NUM_REGS * sizeof
00456    (regoff_t)' bytes long.
00457 
00458    If NUM_REGS == 0, then subsequent matches should allocate their own
00459    register data.
00460 
00461    Unless this function is called, the first search or match using
00462    PATTERN_BUFFER will allocate its own register data, without
00463    freeing the old data.  */
00464 extern void re_set_registers
00465   _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
00466              unsigned num_regs, regoff_t *starts, regoff_t *ends));
00467 
00468 /* 4.2 bsd compatibility.  */
00469 extern char *re_comp _RE_ARGS ((const char *));
00470 extern int re_exec _RE_ARGS ((const char *));
00471 
00472 /* POSIX compatibility.  */
00473 extern int regcomp _RE_ARGS ((regex_t *preg, const char *pattern, int cflags));
00474 extern int regexec
00475   _RE_ARGS ((const regex_t *preg, const char *string, size_t nmatch,
00476              regmatch_t pmatch[], int eflags));
00477 extern size_t regerror
00478   _RE_ARGS ((int errcode, const regex_t *preg, char *errbuf,
00479              size_t errbuf_size));
00480 extern void regfree _RE_ARGS ((regex_t *preg));
00481 
00482 #endif /* not __REGEXP_LIBRARY_H__ */
00483 
00484 /*
00485 Local variables:
00486 make-backup-files: t
00487 version-control: t
00488 trim-versions-without-asking: nil
00489 End:
00490 */

Generated on Fri Jan 12 14:48:27 2007 for vdk 2.4.0 by  doxygen 1.5.1