Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 1 | /************************************************* |
| 2 | * Perl-Compatible Regular Expressions * |
| 3 | *************************************************/ |
| 4 | |
| 5 | |
Guido van Rossum | dda6696 | 1998-05-07 15:32:44 +0000 | [diff] [blame] | 6 | #define PCRE_VERSION "1.09 28-Apr-1998" |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 7 | |
| 8 | |
| 9 | /* This is a library of functions to support regular expressions whose syntax |
| 10 | and semantics are as close as possible to those of the Perl 5 language. See |
| 11 | the file Tech.Notes for some information on the internals. |
| 12 | |
| 13 | Written by: Philip Hazel <ph10@cam.ac.uk> |
| 14 | |
Guido van Rossum | 042ff9e | 1998-04-03 21:13:31 +0000 | [diff] [blame] | 15 | Copyright (c) 1998 University of Cambridge |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 16 | |
| 17 | ----------------------------------------------------------------------------- |
| 18 | Permission is granted to anyone to use this software for any purpose on any |
| 19 | computer system, and to redistribute it freely, subject to the following |
| 20 | restrictions: |
| 21 | |
| 22 | 1. This software is distributed in the hope that it will be useful, |
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 25 | |
| 26 | 2. The origin of this software must not be misrepresented, either by |
| 27 | explicit claim or by omission. |
| 28 | |
| 29 | 3. Altered versions must be plainly marked as such, and must not be |
| 30 | misrepresented as being the original software. |
| 31 | ----------------------------------------------------------------------------- |
| 32 | */ |
| 33 | |
| 34 | /* This header contains definitions that are shared between the different |
| 35 | modules, but which are not relevant to the outside. */ |
| 36 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 37 | |
| 38 | /* To cope with SunOS4 and other systems that lack memmove() but have bcopy(), |
| 39 | define a macro for memmove() if USE_BCOPY is defined. */ |
| 40 | |
| 41 | #ifdef USE_BCOPY |
Guido van Rossum | 557dea1 | 1997-12-22 22:46:52 +0000 | [diff] [blame] | 42 | #undef memmove /* some systems may have a macro */ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 43 | #define memmove(a, b, c) bcopy(b, a, c) |
| 44 | #endif |
| 45 | |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 46 | /* Standard C headers plus the external interface definition */ |
| 47 | |
| 48 | #include <ctype.h> |
| 49 | #include <limits.h> |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 50 | #include <setjmp.h> |
| 51 | #include <stddef.h> |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <string.h> |
| 55 | #include "pcre.h" |
| 56 | |
Guido van Rossum | 557dea1 | 1997-12-22 22:46:52 +0000 | [diff] [blame] | 57 | /* In case there is no definition of offsetof() provided - though any proper |
| 58 | Standard C system should have one. */ |
| 59 | |
| 60 | #ifndef offsetof |
| 61 | #define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field)) |
| 62 | #endif |
| 63 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 64 | /* Private options flags start at the most significant end of the two bytes. |
| 65 | The public options defined in pcre.h start at the least significant end. Make |
| 66 | sure they don't overlap! */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 68 | #define PCRE_FIRSTSET 0x8000 /* first_char is set */ |
| 69 | #define PCRE_STARTLINE 0x4000 /* start after \n for multiline */ |
| 70 | #define PCRE_COMPILED_CASELESS 0x2000 /* like it says */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 71 | |
| 72 | /* Options for the "extra" block produced by pcre_study(). */ |
| 73 | |
| 74 | #define PCRE_STUDY_CASELESS 0x01 /* study was caseless */ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 75 | #define PCRE_STUDY_MAPPED 0x02 /* a map of starting chars exists */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 76 | |
| 77 | /* Masks for identifying the public options: all permitted at compile time, |
| 78 | only some permitted at run or study time. */ |
| 79 | |
| 80 | #ifdef FOR_PYTHON |
| 81 | #define PUBLIC_OPTIONS \ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 82 | (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ |
Guido van Rossum | dda6696 | 1998-05-07 15:32:44 +0000 | [diff] [blame] | 83 | PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY| \ |
| 84 | PCRE_LOCALE) |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 85 | #else |
| 86 | #define PUBLIC_OPTIONS \ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 87 | (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \ |
Guido van Rossum | dda6696 | 1998-05-07 15:32:44 +0000 | [diff] [blame] | 88 | PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY) |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 89 | #endif |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 90 | #define PUBLIC_EXEC_OPTIONS \ |
| 91 | (PCRE_CASELESS|PCRE_ANCHORED|PCRE_MULTILINE|PCRE_NOTBOL|PCRE_NOTEOL| \ |
| 92 | PCRE_DOTALL|PCRE_DOLLAR_ENDONLY) |
| 93 | |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 94 | #define PUBLIC_STUDY_OPTIONS (PCRE_CASELESS) |
| 95 | |
| 96 | /* Magic number to provide a small check against being handed junk. */ |
| 97 | |
| 98 | #define MAGIC_NUMBER 0x50435245 /* 'PCRE' */ |
| 99 | |
| 100 | /* Miscellaneous definitions */ |
| 101 | |
| 102 | typedef int BOOL; |
| 103 | |
| 104 | #define FALSE 0 |
| 105 | #define TRUE 1 |
| 106 | |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 107 | /* These are escaped items that aren't just an encoding of a particular data |
| 108 | value such as \n. They must have non-zero values, as check_escape() returns |
| 109 | their negation. Also, they must appear in the same order as in the opcode |
| 110 | definitions below, up to ESC_Z. The final one must be ESC_REF as subsequent |
| 111 | values are used for \1, \2, \3, etc. There is a test in the code for an escape |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 112 | greater than ESC_b and less than ESC_X to detect the types that may be |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 113 | repeated. If any new escapes are put in-between that don't consume a character, |
| 114 | that code will have to change. */ |
| 115 | |
| 116 | enum { ESC_A = 1, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 117 | |
| 118 | /* These are not Perl escapes, so can't appear in the */ |
| 119 | ESC_X, /* simple table-lookup because they must be conditional */ |
| 120 | /* on PCRE_EXTRA. */ |
| 121 | ESC_Z, |
| 122 | ESC_REF }; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 123 | |
| 124 | /* Opcode table: OP_BRA must be last, as all values >= it are used for brackets |
| 125 | that extract substrings. Starting from 1 (i.e. after OP_END), the values up to |
Guido van Rossum | 58132c6 | 1997-12-17 00:24:13 +0000 | [diff] [blame] | 126 | OP_EOD must correspond in order to the list of escapes immediately above. */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 127 | |
| 128 | enum { |
| 129 | OP_END, /* End of pattern */ |
| 130 | |
| 131 | /* Values corresponding to backslashed metacharacters */ |
| 132 | |
| 133 | OP_SOD, /* Start of data: \A */ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 134 | OP_NOT_WORD_BOUNDARY, /* \B */ |
| 135 | OP_WORD_BOUNDARY, /* \b */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 136 | OP_NOT_DIGIT, /* \D */ |
| 137 | OP_DIGIT, /* \d */ |
| 138 | OP_NOT_WHITESPACE, /* \S */ |
| 139 | OP_WHITESPACE, /* \s */ |
| 140 | OP_NOT_WORDCHAR, /* \W */ |
| 141 | OP_WORDCHAR, /* \w */ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 142 | OP_CUT, /* The analogue of Prolog's "cut" operation (extension) */ |
Guido van Rossum | 58132c6 | 1997-12-17 00:24:13 +0000 | [diff] [blame] | 143 | OP_EOD, /* End of data: \Z. */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 145 | OP_NOT_WORD_BOUNDARY_L, /* localized \B */ |
| 146 | OP_WORD_BOUNDARY_L, /* localized \b */ |
| 147 | OP_NOT_WORDCHAR_L, /* localized \W */ |
| 148 | OP_WORDCHAR_L, /* localized \w */ |
| 149 | |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 150 | OP_CIRC, /* Start of line - varies with multiline switch */ |
| 151 | OP_DOLL, /* End of line - varies with multiline switch */ |
| 152 | OP_ANY, /* Match any character */ |
| 153 | OP_CHARS, /* Match string of characters */ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 154 | OP_NOT, /* Match anything but the following char */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 155 | |
| 156 | OP_STAR, /* The maximizing and minimizing versions of */ |
| 157 | OP_MINSTAR, /* all these opcodes must come in pairs, with */ |
| 158 | OP_PLUS, /* the minimizing one second. */ |
| 159 | OP_MINPLUS, /* This first set applies to single characters */ |
| 160 | OP_QUERY, |
| 161 | OP_MINQUERY, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 162 | OP_UPTO, /* From 0 to n matches */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 163 | OP_MINUPTO, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 164 | OP_EXACT, /* Exactly n matches */ |
| 165 | |
| 166 | OP_NOTSTAR, /* The maximizing and minimizing versions of */ |
| 167 | OP_NOTMINSTAR, /* all these opcodes must come in pairs, with */ |
| 168 | OP_NOTPLUS, /* the minimizing one second. */ |
| 169 | OP_NOTMINPLUS, /* This first set applies to "not" single characters */ |
| 170 | OP_NOTQUERY, |
| 171 | OP_NOTMINQUERY, |
| 172 | OP_NOTUPTO, /* From 0 to n matches */ |
| 173 | OP_NOTMINUPTO, |
| 174 | OP_NOTEXACT, /* Exactly n matches */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 175 | |
| 176 | OP_TYPESTAR, /* The maximizing and minimizing versions of */ |
| 177 | OP_TYPEMINSTAR, /* all these opcodes must come in pairs, with */ |
| 178 | OP_TYPEPLUS, /* the minimizing one second. These codes must */ |
| 179 | OP_TYPEMINPLUS, /* be in exactly the same order as those above. */ |
| 180 | OP_TYPEQUERY, /* This set applies to character types such as \d */ |
| 181 | OP_TYPEMINQUERY, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 182 | OP_TYPEUPTO, /* From 0 to n matches */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 183 | OP_TYPEMINUPTO, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 184 | OP_TYPEEXACT, /* Exactly n matches */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 185 | |
| 186 | OP_CRSTAR, /* The maximizing and minimizing versions of */ |
| 187 | OP_CRMINSTAR, /* all these opcodes must come in pairs, with */ |
| 188 | OP_CRPLUS, /* the minimizing one second. These codes must */ |
| 189 | OP_CRMINPLUS, /* be in exactly the same order as those above. */ |
| 190 | OP_CRQUERY, /* These are for character classes and back refs */ |
| 191 | OP_CRMINQUERY, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 192 | OP_CRRANGE, /* These are different to the three seta above. */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 193 | OP_CRMINRANGE, |
| 194 | |
| 195 | OP_CLASS, /* Match a character class */ |
Guido van Rossum | 042ff9e | 1998-04-03 21:13:31 +0000 | [diff] [blame] | 196 | OP_NEGCLASS, /* Match a character class, specified negatively */ |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 197 | OP_CLASS_L, /* Match a character class */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 198 | OP_REF, /* Match a back reference */ |
| 199 | |
| 200 | OP_ALT, /* Start of alternation */ |
| 201 | OP_KET, /* End of group that doesn't have an unbounded repeat */ |
| 202 | OP_KETRMAX, /* These two must remain together and in this */ |
| 203 | OP_KETRMIN, /* order. They are for groups the repeat for ever. */ |
| 204 | |
| 205 | OP_ASSERT, |
| 206 | OP_ASSERT_NOT, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 207 | OP_ONCE, /* Once matched, don't back up into the subpattern */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 208 | |
| 209 | OP_BRAZERO, /* These two must remain together and in this */ |
| 210 | OP_BRAMINZERO, /* order. */ |
| 211 | |
| 212 | OP_BRA /* This and greater values are used for brackets that |
| 213 | extract substrings. */ |
| 214 | }; |
| 215 | |
| 216 | /* The highest extraction number. This is limited by the number of opcodes |
| 217 | left after OP_BRA, i.e. 255 - OP_BRA. We actually set it somewhat lower. */ |
| 218 | |
| 219 | #define EXTRACT_MAX 99 |
| 220 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 221 | /* The texts of compile-time error messages are defined as macros here so that |
| 222 | they can be accessed by the POSIX wrapper and converted into error codes. Yes, |
| 223 | I could have used error codes in the first place, but didn't feel like changing |
| 224 | just to accommodate the POSIX wrapper. */ |
| 225 | |
| 226 | #define ERR1 "\\ at end of pattern" |
| 227 | #define ERR2 "\\c at end of pattern" |
| 228 | #define ERR3 "unrecognized character follows \\" |
| 229 | #define ERR4 "numbers out of order in {} quantifier" |
| 230 | #define ERR5 "number too big in {} quantifier" |
| 231 | #define ERR6 "missing terminating ] for character class" |
| 232 | #define ERR7 "invalid escape sequence in character class" |
| 233 | #define ERR8 "range out of order in character class" |
| 234 | #define ERR9 "nothing to repeat" |
| 235 | #define ERR10 "operand of unlimited repeat could match the empty string" |
| 236 | #define ERR11 "internal error: unexpected repeat" |
| 237 | #define ERR12 "unrecognized character after (?" |
| 238 | #define ERR13 "too many capturing parenthesized sub-patterns" |
| 239 | #define ERR14 "missing )" |
| 240 | #define ERR15 "back reference to non-existent subpattern" |
| 241 | #define ERR16 "erroffset passed as NULL" |
| 242 | #define ERR17 "unknown option bit(s) set" |
| 243 | #define ERR18 "missing ) after comment" |
| 244 | #define ERR19 "too many sets of parentheses" |
| 245 | #define ERR20 "regular expression too large" |
| 246 | #define ERR21 "failed to get memory" |
| 247 | #define ERR22 "unmatched brackets" |
| 248 | #define ERR23 "internal error: code overflow" |
| 249 | |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 250 | /* All character handling must be done as unsigned characters. Otherwise there |
| 251 | are problems with top-bit-set characters and functions such as isspace(). |
| 252 | However, we leave the interface to the outside world as char *, because that |
| 253 | should make things easier for callers. We define a short type for unsigned char |
| 254 | to save lots of typing. I tried "uchar", but it causes problems on Digital |
| 255 | Unix, where it is defined in sys/types, so use "uschar" instead. */ |
| 256 | |
| 257 | typedef unsigned char uschar; |
| 258 | |
| 259 | /* The real format of the start of the pcre block; the actual code vector |
| 260 | runs on as long as necessary after the end. */ |
| 261 | |
| 262 | typedef struct real_pcre { |
| 263 | unsigned int magic_number; |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 264 | unsigned short int options; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 265 | unsigned char top_bracket; |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 266 | unsigned char top_backref; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 267 | unsigned char first_char; |
| 268 | unsigned char code[1]; |
| 269 | } real_pcre; |
| 270 | |
| 271 | /* The real format of the extra block returned by pcre_study(). */ |
| 272 | |
| 273 | typedef struct real_pcre_extra { |
| 274 | unsigned char options; |
| 275 | unsigned char start_bits[32]; |
| 276 | } real_pcre_extra; |
| 277 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 278 | /* Global tables from chartables.c */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 279 | |
| 280 | extern uschar pcre_lcc[]; |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 281 | extern uschar pcre_fcc[]; |
| 282 | extern uschar pcre_cbits[]; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 283 | extern uschar pcre_ctypes[]; |
| 284 | |
| 285 | /* Bit definitions for entries in pcre_ctypes[]. */ |
| 286 | |
| 287 | #define ctype_space 0x01 |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 288 | #define ctype_letter 0x02 |
| 289 | #define ctype_digit 0x04 |
| 290 | #define ctype_xdigit 0x08 |
| 291 | #define ctype_word 0x10 /* alphameric or '_' */ |
| 292 | #define ctype_odigit 0x20 /* octal digit */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 293 | #define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */ |
| 294 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 295 | /* Offsets for the bitmap tables */ |
| 296 | |
| 297 | #define cbit_digit 0 |
| 298 | #define cbit_letter 32 |
| 299 | #define cbit_word 64 |
| 300 | #define cbit_space 96 |
| 301 | #define cbit_length 128 /* Length of the cbits table */ |
| 302 | |
| 303 | /* End of internal.h */ |