Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

regex.h

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

Generated on Sat May 4 23:45:39 2002 for vdk 2.0.1 by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002