Guido van Rossum | b674c3b | 1992-01-19 16:32:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | regexpr.h |
| 4 | |
| 5 | Author: Tatu Ylonen <ylo@ngs.fi> |
| 6 | |
| 7 | Copyright (c) 1991 Tatu Ylonen, Espoo, Finland |
| 8 | |
| 9 | Permission to use, copy, modify, distribute, and sell this software |
| 10 | and its documentation for any purpose is hereby granted without fee, |
| 11 | provided that the above copyright notice appear in all copies. This |
| 12 | software is provided "as is" without express or implied warranty. |
| 13 | |
| 14 | Created: Thu Sep 26 17:15:36 1991 ylo |
| 15 | Last modified: Mon Nov 4 15:49:46 1991 ylo |
| 16 | |
| 17 | */ |
| 18 | |
| 19 | #ifndef REGEXPR_H |
| 20 | #define REGEXPR_H |
| 21 | |
Guido van Rossum | 9db23a8 | 1992-01-27 16:48:48 +0000 | [diff] [blame] | 22 | #if defined(__STDC__) || defined(THINK_C) |
| 23 | #undef HAVE_PROTOTYPES |
| 24 | #define HAVE_PROTOTYPES |
| 25 | #endif |
| 26 | |
Guido van Rossum | acbe8da | 1993-04-15 15:33:52 +0000 | [diff] [blame] | 27 | #define RE_NREGS 100 /* number of registers available */ |
Guido van Rossum | b674c3b | 1992-01-19 16:32:47 +0000 | [diff] [blame] | 28 | |
| 29 | typedef struct re_pattern_buffer |
| 30 | { |
| 31 | char *buffer; /* compiled pattern */ |
| 32 | int allocated; /* allocated size of compiled pattern */ |
| 33 | int used; /* actual length of compiled pattern */ |
| 34 | char *fastmap; /* fastmap[ch] is true if ch can start pattern */ |
| 35 | char *translate; /* translation to apply during compilation/matching */ |
| 36 | char fastmap_accurate; /* true if fastmap is valid */ |
| 37 | char can_be_null; /* true if can match empty string */ |
| 38 | char uses_registers; /* registers are used and need to be initialized */ |
| 39 | char anchor; /* anchor: 0=none 1=begline 2=begbuf */ |
| 40 | } *regexp_t; |
| 41 | |
| 42 | typedef struct re_registers |
| 43 | { |
| 44 | int start[RE_NREGS]; /* start offset of region */ |
| 45 | int end[RE_NREGS]; /* end offset of region */ |
| 46 | } *regexp_registers_t; |
| 47 | |
| 48 | /* bit definitions for syntax */ |
| 49 | #define RE_NO_BK_PARENS 1 /* no quoting for parentheses */ |
| 50 | #define RE_NO_BK_VBAR 2 /* no quoting for vertical bar */ |
| 51 | #define RE_BK_PLUS_QM 4 /* quoting needed for + and ? */ |
| 52 | #define RE_TIGHT_VBAR 8 /* | binds tighter than ^ and $ */ |
| 53 | #define RE_NEWLINE_OR 16 /* treat newline as or */ |
| 54 | #define RE_CONTEXT_INDEP_OPS 32 /* ^$?*+ are special in all contexts */ |
| 55 | #define RE_ANSI_HEX 64 /* ansi sequences (\n etc) and \xhh */ |
| 56 | #define RE_NO_GNU_EXTENSIONS 128 /* no gnu extensions */ |
| 57 | |
| 58 | /* definitions for some common regexp styles */ |
| 59 | #define RE_SYNTAX_AWK (RE_NO_BK_PARENS|RE_NO_BK_VBAR|RE_CONTEXT_INDEP_OPS) |
| 60 | #define RE_SYNTAX_EGREP (RE_SYNTAX_AWK|RE_NEWLINE_OR) |
| 61 | #define RE_SYNTAX_GREP (RE_BK_PLUS_QM|RE_NEWLINE_OR) |
| 62 | #define RE_SYNTAX_EMACS 0 |
| 63 | |
Guido van Rossum | 9db23a8 | 1992-01-27 16:48:48 +0000 | [diff] [blame] | 64 | #ifdef HAVE_PROTOTYPES |
Guido van Rossum | b674c3b | 1992-01-19 16:32:47 +0000 | [diff] [blame] | 65 | |
| 66 | int re_set_syntax(int syntax); |
| 67 | /* This sets the syntax to use and returns the previous syntax. The |
| 68 | syntax is specified by a bit mask of the above defined bits. */ |
| 69 | |
| 70 | char *re_compile_pattern(char *regex, int regex_size, regexp_t compiled); |
| 71 | /* This compiles the regexp (given in regex and length in regex_size). |
| 72 | This returns NULL if the regexp compiled successfully, and an error |
| 73 | message if an error was encountered. The buffer field must be |
| 74 | initialized to a memory area allocated by malloc (or to NULL) before |
| 75 | use, and the allocated field must be set to its length (or 0 if buffer is |
| 76 | NULL). Also, the translate field must be set to point to a valid |
| 77 | translation table, or NULL if it is not used. */ |
| 78 | |
| 79 | int re_match(regexp_t compiled, char *string, int size, int pos, |
| 80 | regexp_registers_t regs); |
| 81 | /* This tries to match the regexp against the string. This returns the |
| 82 | length of the matched portion, or -1 if the pattern could not be |
| 83 | matched and -2 if an error (such as failure stack overflow) is |
| 84 | encountered. */ |
| 85 | |
| 86 | int re_match_2(regexp_t compiled, char *string1, int size1, |
| 87 | char *string2, int size2, int pos, regexp_registers_t regs, |
| 88 | int mstop); |
| 89 | /* This tries to match the regexp to the concatenation of string1 and |
| 90 | string2. This returns the length of the matched portion, or -1 if the |
| 91 | pattern could not be matched and -2 if an error (such as failure stack |
| 92 | overflow) is encountered. */ |
| 93 | |
| 94 | int re_search(regexp_t compiled, char *string, int size, int startpos, |
| 95 | int range, regexp_registers_t regs); |
| 96 | /* This rearches for a substring matching the regexp. This returns the first |
| 97 | index at which a match is found. range specifies at how many positions to |
| 98 | try matching; positive values indicate searching forwards, and negative |
| 99 | values indicate searching backwards. mstop specifies the offset beyond |
| 100 | which a match must not go. This returns -1 if no match is found, and |
| 101 | -2 if an error (such as failure stack overflow) is encountered. */ |
| 102 | |
| 103 | int re_search_2(regexp_t compiled, char *string1, int size1, |
| 104 | char *string2, int size2, int startpos, int range, |
| 105 | regexp_registers_t regs, int mstop); |
| 106 | /* This is like re_search, but search from the concatenation of string1 and |
| 107 | string2. */ |
| 108 | |
| 109 | void re_compile_fastmap(regexp_t compiled); |
| 110 | /* This computes the fastmap for the regexp. For this to have any effect, |
| 111 | the calling program must have initialized the fastmap field to point |
| 112 | to an array of 256 characters. */ |
| 113 | |
| 114 | char *re_comp(char *s); |
| 115 | /* BSD 4.2 regex library routine re_comp. This compiles the regexp into |
| 116 | an internal buffer. This returns NULL if the regexp was compiled |
| 117 | successfully, and an error message if there was an error. */ |
| 118 | |
| 119 | int re_exec(char *s); |
| 120 | /* BSD 4.2 regexp library routine re_exec. This returns true if the string |
| 121 | matches the regular expression (that is, a matching part is found |
| 122 | anywhere in the string). */ |
| 123 | |
Guido van Rossum | 9db23a8 | 1992-01-27 16:48:48 +0000 | [diff] [blame] | 124 | #else /* HAVE_PROTOTYPES */ |
Guido van Rossum | b674c3b | 1992-01-19 16:32:47 +0000 | [diff] [blame] | 125 | |
| 126 | int re_set_syntax(); |
| 127 | char *re_compile_pattern(); |
| 128 | int re_match(); |
| 129 | int re_match_2(); |
| 130 | int re_search(); |
| 131 | int re_search_2(); |
| 132 | void re_compile_fastmap(); |
| 133 | char *re_comp(); |
| 134 | int re_exec(); |
| 135 | |
Guido van Rossum | 9db23a8 | 1992-01-27 16:48:48 +0000 | [diff] [blame] | 136 | #endif /* HAVE_PROTOTYPES */ |
Guido van Rossum | b674c3b | 1992-01-19 16:32:47 +0000 | [diff] [blame] | 137 | |
| 138 | #endif /* REGEXPR_H */ |
| 139 | |
| 140 | |