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