Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Pcre objects */ |
| 33 | |
| 34 | #include "Python.h" |
| 35 | |
| 36 | #ifndef Py_eval_input |
| 37 | /* For Python 1.4, graminit.h has to be explicitly included */ |
| 38 | #include "graminit.h" |
| 39 | #define Py_eval_input eval_input |
| 40 | #endif |
| 41 | |
| 42 | #ifndef FOR_PYTHON |
| 43 | #define FOR_PYTHON |
| 44 | #endif |
| 45 | |
| 46 | #include "pcre.h" |
| 47 | #include "pcre-internal.h" |
| 48 | |
| 49 | static PyObject *ErrorObject; |
| 50 | |
| 51 | typedef struct { |
| 52 | PyObject_HEAD |
| 53 | pcre *regex; |
| 54 | pcre_extra *regex_extra; |
| 55 | int num_groups; |
| 56 | } PcreObject; |
| 57 | |
| 58 | staticforward PyTypeObject Pcre_Type; |
| 59 | |
| 60 | #define PcreObject_Check(v) ((v)->ob_type == &Pcre_Type) |
| 61 | #define NORMAL 0 |
| 62 | #define CHARCLASS 1 |
| 63 | #define REPLACEMENT 2 |
| 64 | |
| 65 | #define CHAR 0 |
| 66 | #define MEMORY_REFERENCE 1 |
| 67 | #define SYNTAX 2 |
| 68 | #define NOT_SYNTAX 3 |
| 69 | #define SET 4 |
| 70 | #define WORD_BOUNDARY 5 |
| 71 | #define NOT_WORD_BOUNDARY 6 |
| 72 | #define BEGINNING_OF_BUFFER 7 |
| 73 | #define END_OF_BUFFER 8 |
| 74 | |
| 75 | |
| 76 | static PcreObject * |
| 77 | newPcreObject(arg) |
| 78 | PyObject *arg; |
| 79 | { |
| 80 | PcreObject *self; |
| 81 | self = PyObject_NEW(PcreObject, &Pcre_Type); |
| 82 | if (self == NULL) |
| 83 | return NULL; |
| 84 | self->regex = NULL; |
| 85 | self->regex_extra = NULL; |
| 86 | return self; |
| 87 | } |
| 88 | |
| 89 | /* Pcre methods */ |
| 90 | |
| 91 | static void |
| 92 | PyPcre_dealloc(self) |
| 93 | PcreObject *self; |
| 94 | { |
| 95 | if (self->regex) free(self->regex); |
| 96 | if (self->regex_extra) free(self->regex_extra); |
| 97 | self->regex=NULL; |
| 98 | self->regex_extra=NULL; |
| 99 | PyMem_DEL(self); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | static PyObject * |
| 104 | PyPcre_exec(self, args) |
| 105 | PcreObject *self; |
| 106 | PyObject *args; |
| 107 | { |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 108 | char *string; |
| 109 | int stringlen, pos = 0, options=0, endpos = -1, i, count; |
| 110 | int offsets[100*2]; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 111 | PyObject *list; |
| 112 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 113 | if (!PyArg_ParseTuple(args, "s#|iiii", &string, &stringlen, &pos, &endpos, &options)) |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 114 | return NULL; |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 115 | if (endpos == -1) {endpos = stringlen;} |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 116 | count = pcre_exec(self->regex, self->regex_extra, |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 117 | (char *)string+pos, endpos - pos, options, |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 118 | offsets, sizeof(offsets)/sizeof(int) ); |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 119 | /* If an error occurred during the match, and an exception was raised, |
| 120 | just return NULL and leave the exception alone. The most likely |
| 121 | problem to cause this would be running out of memory for |
| 122 | the failure stack. */ |
| 123 | if (PyErr_Occurred()) |
| 124 | { |
| 125 | return NULL; |
| 126 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 127 | if (count==PCRE_ERROR_NOMATCH) {Py_INCREF(Py_None); return Py_None;} |
| 128 | if (count<0) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 129 | { |
| 130 | PyErr_SetObject(ErrorObject, Py_BuildValue("si", "Regex error", count)); |
| 131 | return NULL; |
| 132 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 133 | |
| 134 | list=PyList_New(self->num_groups+1); |
| 135 | if (list==NULL) return NULL; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 136 | for(i=0; i<=self->num_groups; i++) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 137 | { |
| 138 | PyObject *v; |
| 139 | int start=offsets[i*2], end=offsets[i*2+1]; |
| 140 | /* If the group wasn't affected by the match, return -1, -1 */ |
| 141 | if (start<0 || count<=i) |
| 142 | {start=end=-1;} |
| 143 | else |
| 144 | {start += pos; end +=pos;} |
| 145 | v=Py_BuildValue("ii", start, end); |
| 146 | if (v==NULL) {Py_DECREF(list); return NULL;} |
| 147 | PyList_SetItem(list, i, v); |
| 148 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 149 | return list; |
| 150 | } |
| 151 | |
| 152 | static PyMethodDef Pcre_methods[] = { |
| 153 | {"match", (PyCFunction)PyPcre_exec, 1}, |
| 154 | {NULL, NULL} /* sentinel */ |
| 155 | }; |
| 156 | |
| 157 | static PyObject * |
| 158 | PyPcre_getattr(self, name) |
| 159 | PcreObject *self; |
| 160 | char *name; |
| 161 | { |
| 162 | return Py_FindMethod(Pcre_methods, (PyObject *)self, name); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | staticforward PyTypeObject Pcre_Type = { |
| 167 | PyObject_HEAD_INIT(&PyType_Type) |
| 168 | 0, /*ob_size*/ |
| 169 | "Pcre", /*tp_name*/ |
| 170 | sizeof(PcreObject), /*tp_basicsize*/ |
| 171 | 0, /*tp_itemsize*/ |
| 172 | /* methods */ |
| 173 | (destructor)PyPcre_dealloc, /*tp_dealloc*/ |
| 174 | 0, /*tp_print*/ |
Guido van Rossum | cb4d303 | 1997-10-20 23:21:23 +0000 | [diff] [blame] | 175 | (getattrfunc)PyPcre_getattr, /*tp_getattr*/ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 176 | 0, /*tp_setattr*/ |
| 177 | 0, /*tp_compare*/ |
| 178 | 0, /*tp_repr*/ |
| 179 | 0, /*tp_as_number*/ |
| 180 | 0, /*tp_as_sequence*/ |
| 181 | 0, /*tp_as_mapping*/ |
| 182 | 0, /*tp_hash*/ |
| 183 | }; |
| 184 | /* --------------------------------------------------------------------- */ |
| 185 | |
| 186 | static PyObject * |
| 187 | PyPcre_compile(self, args) |
| 188 | PyObject *self; /* Not used */ |
| 189 | PyObject *args; |
| 190 | { |
| 191 | PcreObject *rv; |
| 192 | PyObject *dictionary; |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 193 | char *pattern, *newpattern; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 194 | char *error; |
| 195 | int num_zeros, i, j; |
| 196 | |
| 197 | int patternlen, options, erroroffset; |
| 198 | if (!PyArg_ParseTuple(args, "s#iO!", &pattern, &patternlen, &options, |
| 199 | &PyDict_Type, &dictionary)) |
| 200 | return NULL; |
| 201 | rv = newPcreObject(args); |
| 202 | if ( rv == NULL ) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 203 | return NULL; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 204 | |
| 205 | /* PCRE doesn't like having null bytes in its pattern, so we have to replace |
| 206 | any zeros in the string with the characters '\0'. */ |
| 207 | num_zeros=1; |
| 208 | for(i=0; i<patternlen; i++) { |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 209 | if (pattern[i]==0) num_zeros++; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 210 | } |
| 211 | newpattern=malloc(patternlen+num_zeros); |
| 212 | if (newpattern==NULL) { |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 213 | PyErr_SetString(PyExc_MemoryError, "can't allocate memory for new pattern"); |
| 214 | return NULL; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 215 | } |
| 216 | for (i=j=0; i<patternlen; i++, j++) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 217 | { |
| 218 | if (pattern[i]!=0) newpattern[j]=pattern[i]; |
| 219 | else { |
| 220 | newpattern[j++]='\\'; |
| 221 | newpattern[j] ='0'; |
| 222 | } |
| 223 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 224 | newpattern[j]='\0'; |
| 225 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 226 | rv->regex = pcre_compile((char*)newpattern, options, |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 227 | &error, &erroroffset, dictionary); |
| 228 | free(newpattern); |
| 229 | if (rv->regex==NULL) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 230 | { |
| 231 | PyMem_DEL(rv); |
| 232 | if (!PyErr_Occurred()) |
| 233 | { |
| 234 | PyErr_SetObject(ErrorObject, Py_BuildValue("si", error, erroroffset)); |
| 235 | } |
| 236 | return NULL; |
| 237 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 238 | rv->regex_extra=pcre_study(rv->regex, 0, &error); |
| 239 | if (rv->regex_extra==NULL && error!=NULL) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 240 | { |
| 241 | PyMem_DEL(rv); |
| 242 | PyErr_SetObject(ErrorObject, Py_BuildValue("si", error, 0)); |
| 243 | return NULL; |
| 244 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 245 | rv->num_groups = pcre_info(rv->regex, NULL, NULL); |
| 246 | if (rv->num_groups<0) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 247 | { |
| 248 | PyErr_SetObject(ErrorObject, Py_BuildValue("si", "Regex error", rv->num_groups)); |
| 249 | PyMem_DEL(rv); |
| 250 | return NULL; |
| 251 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 252 | return (PyObject *)rv; |
| 253 | } |
| 254 | |
| 255 | static PyObject * |
Guido van Rossum | c386107 | 1997-10-08 02:07:40 +0000 | [diff] [blame] | 256 | PyPcre_expand_escape(pattern, pattern_len, indexptr, typeptr) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 257 | unsigned char *pattern; |
| 258 | int pattern_len, *indexptr, *typeptr; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 259 | { |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 260 | unsigned char c; |
| 261 | int index = *indexptr; |
Guido van Rossum | c386107 | 1997-10-08 02:07:40 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 263 | if (pattern_len<=index) |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 264 | { |
| 265 | PyErr_SetString(ErrorObject, "escape ends too soon"); |
| 266 | return NULL; |
| 267 | } |
| 268 | c=pattern[index]; index++; |
| 269 | *typeptr=CHAR; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 271 | switch (c) |
| 272 | { |
| 273 | case('t'): |
| 274 | *indexptr=index; |
| 275 | return Py_BuildValue("c", (char)9); |
| 276 | break; |
| 277 | case('n'): |
| 278 | *indexptr = index; |
| 279 | return Py_BuildValue("c", (char)10); |
| 280 | break; |
| 281 | case('v'): |
| 282 | *indexptr = index; |
| 283 | return Py_BuildValue("c", (char)11); |
| 284 | break; |
| 285 | case('r'): |
| 286 | *indexptr = index; |
| 287 | return Py_BuildValue("c", (char)13); |
| 288 | break; |
| 289 | case('f'): |
| 290 | *indexptr = index; |
| 291 | return Py_BuildValue("c", (char)12); |
| 292 | break; |
| 293 | case('a'): |
| 294 | *indexptr = index; |
| 295 | return Py_BuildValue("c", (char)7); |
| 296 | break; |
| 297 | case('b'): |
| 298 | *indexptr=index; |
| 299 | return Py_BuildValue("c", (char)8); |
| 300 | break; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 301 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 302 | case('x'): |
| 303 | { |
| 304 | int x, ch, end; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 306 | x = 0; end = index; |
| 307 | while ( (end<pattern_len && pcre_ctypes[ pattern[end] ] & ctype_xdigit) != 0) |
| 308 | { |
| 309 | ch = pattern[end]; |
| 310 | x = x * 16 + pcre_lcc[ch] - |
| 311 | (((pcre_ctypes[ch] & ctype_digit) != 0)? '0' : 'W'); |
| 312 | x &= 255; |
| 313 | end++; |
| 314 | } |
| 315 | if (end==index) |
| 316 | { |
| 317 | PyErr_SetString(ErrorObject, "\\x must be followed by hex digits"); |
| 318 | return NULL; |
| 319 | } |
| 320 | *indexptr = end; |
| 321 | return Py_BuildValue("c", (char)x); |
| 322 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 323 | break; |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 324 | |
| 325 | case('E'): case('G'): case('L'): case('Q'): |
| 326 | case('U'): case('l'): case('u'): |
| 327 | { |
| 328 | char message[50]; |
| 329 | sprintf(message, "\\%c is not allowed", c); |
| 330 | PyErr_SetString(ErrorObject, message); |
| 331 | return NULL; |
| 332 | } |
| 333 | |
| 334 | case('g'): |
| 335 | { |
| 336 | int end, i; |
| 337 | if (pattern_len<=index) |
| 338 | { |
| 339 | PyErr_SetString(ErrorObject, "unfinished symbolic reference"); |
| 340 | return NULL; |
| 341 | } |
| 342 | if (pattern[index]!='<') |
| 343 | { |
| 344 | PyErr_SetString(ErrorObject, "missing < in symbolic reference"); |
| 345 | return NULL; |
| 346 | } |
| 347 | index++; |
| 348 | end=index; |
| 349 | while (end<pattern_len && pattern[end]!='>') |
| 350 | end++; |
| 351 | if (end==pattern_len) |
| 352 | { |
| 353 | PyErr_SetString(ErrorObject, "unfinished symbolic reference"); |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | if (index==end) /* Zero-length name */ |
| 358 | { |
| 359 | /* XXX should include the text of the reference */ |
| 360 | PyErr_SetString(ErrorObject, "zero-length symbolic reference"); |
| 361 | return NULL; |
| 362 | } |
| 363 | if (!(pcre_ctypes[pattern[index]] & ctype_word) /* First char. not alphanumeric */ |
| 364 | || (pcre_ctypes[pattern[index]] & ctype_digit) ) /* First char. a digit */ |
| 365 | { |
| 366 | /* XXX should include the text of the reference */ |
| 367 | PyErr_SetString(ErrorObject, "first character of symbolic reference not a letter or _"); |
| 368 | return NULL; |
| 369 | } |
| 370 | |
| 371 | for(i=index+1; i<end; i++) |
| 372 | { |
| 373 | if (!(pcre_ctypes[pattern[i]] & ctype_word) ) |
| 374 | { |
| 375 | /* XXX should include the text of the reference */ |
| 376 | PyErr_SetString(ErrorObject, "illegal symbolic reference"); |
| 377 | return NULL; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | *typeptr = MEMORY_REFERENCE; |
| 382 | *indexptr = end+1; |
| 383 | return Py_BuildValue("s#", pattern+index, end-index); |
| 384 | } |
| 385 | break; |
| 386 | |
| 387 | case('0'): |
| 388 | { |
| 389 | /* \0 always indicates an octal escape, so we consume up to 3 |
| 390 | characters, as long as they're all octal digits */ |
| 391 | int octval=0, i; |
| 392 | index--; |
| 393 | for(i=index; |
| 394 | i<=index+2 && i<pattern_len |
| 395 | && (pcre_ctypes[ pattern[i] ] & ctype_odigit ); |
| 396 | i++) |
| 397 | { |
| 398 | octval = octval * 8 + pattern[i] - '0'; |
| 399 | } |
| 400 | if (octval>255) |
| 401 | { |
| 402 | PyErr_SetString(ErrorObject, "octal value out of range"); |
| 403 | return NULL; |
| 404 | } |
| 405 | *indexptr = i; |
| 406 | return Py_BuildValue("c", (unsigned char)octval); |
| 407 | } |
| 408 | break; |
| 409 | case('1'): case('2'): case('3'): case('4'): |
| 410 | case('5'): case('6'): case('7'): case('8'): |
| 411 | case('9'): |
| 412 | { |
| 413 | /* Handle \?, where ? is from 1 through 9 */ |
| 414 | int value=0; |
| 415 | index--; |
| 416 | /* If it's at least a two-digit reference, like \34, it might |
| 417 | either be a 3-digit octal escape (\123) or a 2-digit |
| 418 | decimal memory reference (\34) */ |
| 419 | |
| 420 | if ( (index+1) <pattern_len && |
| 421 | (pcre_ctypes[ pattern[index+1] ] & ctype_digit) ) |
| 422 | { |
| 423 | if ( (index+2) <pattern_len && |
| 424 | (pcre_ctypes[ pattern[index+2] ] & ctype_odigit) && |
| 425 | (pcre_ctypes[ pattern[index+1] ] & ctype_odigit) && |
| 426 | (pcre_ctypes[ pattern[index ] ] & ctype_odigit) |
| 427 | ) |
| 428 | { |
| 429 | /* 3 octal digits */ |
| 430 | value= 8*8*(pattern[index ]-'0') + |
| 431 | 8*(pattern[index+1]-'0') + |
| 432 | (pattern[index+2]-'0'); |
| 433 | if (value>255) |
| 434 | { |
| 435 | PyErr_SetString(ErrorObject, "octal value out of range"); |
| 436 | return NULL; |
| 437 | } |
| 438 | *indexptr = index+3; |
| 439 | return Py_BuildValue("c", (unsigned char)value); |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | /* 2-digit form, so it's a memory reference */ |
| 444 | value= 10*(pattern[index ]-'0') + |
| 445 | (pattern[index+1]-'0'); |
| 446 | if (value<1 || EXTRACT_MAX<=value) |
| 447 | { |
| 448 | PyErr_SetString(ErrorObject, "memory reference out of range"); |
| 449 | return NULL; |
| 450 | } |
| 451 | *typeptr = MEMORY_REFERENCE; |
| 452 | *indexptr = index+2; |
| 453 | return Py_BuildValue("i", value); |
| 454 | } |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | /* Single-digit form, like \2, so it's a memory reference */ |
| 459 | *typeptr = MEMORY_REFERENCE; |
| 460 | *indexptr = index+1; |
| 461 | return Py_BuildValue("i", pattern[index]-'0'); |
| 462 | } |
| 463 | } |
| 464 | break; |
| 465 | |
| 466 | default: |
| 467 | *indexptr = index; |
| 468 | return Py_BuildValue("c", c); |
| 469 | break; |
| 470 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static PyObject * |
| 474 | PyPcre_expand(self, args) |
| 475 | PyObject *self; |
| 476 | PyObject *args; |
| 477 | { |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 478 | PyObject *results, *match_obj; |
| 479 | PyObject *repl_obj, *newstring; |
| 480 | unsigned char *repl; |
| 481 | int size, total_len, i, start, pos; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 482 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 483 | if (!PyArg_ParseTuple(args, "OS", &match_obj, &repl_obj)) |
| 484 | return NULL; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 485 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 486 | repl=(unsigned char *)PyString_AsString(repl_obj); |
| 487 | size=PyString_Size(repl_obj); |
| 488 | results=PyList_New(0); |
| 489 | if (results==NULL) return NULL; |
| 490 | for(start=total_len=i=0; i<size; i++) |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 491 | { |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 492 | if (repl[i]=='\\') |
| 493 | { |
| 494 | PyObject *value; |
| 495 | int escape_type; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 497 | if (start!=i) |
| 498 | { |
| 499 | PyList_Append(results, |
| 500 | PyString_FromStringAndSize((char *)repl+start, i-start)); |
| 501 | total_len += i-start; |
| 502 | } |
| 503 | i++; |
| 504 | value=PyPcre_expand_escape(repl, size, &i, &escape_type); |
| 505 | if (value==NULL) |
| 506 | { |
| 507 | /* PyPcre_expand_escape triggered an exception of some sort, |
| 508 | so just return */ |
| 509 | Py_DECREF(results); |
| 510 | return NULL; |
| 511 | } |
| 512 | switch (escape_type) |
| 513 | { |
| 514 | case (CHAR): |
| 515 | PyList_Append(results, value); |
| 516 | total_len += PyString_Size(value); |
| 517 | break; |
| 518 | case(MEMORY_REFERENCE): |
| 519 | { |
| 520 | PyObject *r, *tuple, *result; |
| 521 | r=PyObject_GetAttrString(match_obj, "group"); |
| 522 | tuple=PyTuple_New(1); |
| 523 | Py_INCREF(value); |
| 524 | PyTuple_SetItem(tuple, 0, value); |
| 525 | result=PyEval_CallObject(r, tuple); |
| 526 | Py_DECREF(r); Py_DECREF(tuple); |
| 527 | if (result==NULL) |
| 528 | { |
| 529 | /* The group() method trigged an exception of some sort */ |
| 530 | Py_DECREF(results); |
| 531 | Py_DECREF(value); |
| 532 | return NULL; |
| 533 | } |
| 534 | if (result==Py_None) |
| 535 | { |
| 536 | char message[50]; |
| 537 | sprintf(message, |
| 538 | "group did not contribute to the match"); |
| 539 | PyErr_SetString(ErrorObject, |
| 540 | message); |
| 541 | Py_DECREF(result); |
| 542 | Py_DECREF(value); |
| 543 | Py_DECREF(results); |
| 544 | return NULL; |
| 545 | } |
| 546 | /* typecheck that it's a string! */ |
| 547 | if (!PyString_Check(result)) |
| 548 | { |
| 549 | Py_DECREF(results); |
| 550 | Py_DECREF(result); |
| 551 | PyErr_SetString(ErrorObject, |
| 552 | "group() must return a string value for replacement"); |
| 553 | return NULL; |
| 554 | } |
| 555 | PyList_Append(results, result); |
| 556 | total_len += PyString_Size(result); |
| 557 | Py_DECREF(result); |
| 558 | } |
| 559 | break; |
| 560 | default: |
| 561 | Py_DECREF(results); |
| 562 | PyErr_SetString(ErrorObject, |
| 563 | "bad escape in replacement"); |
| 564 | return NULL; |
| 565 | } |
| 566 | Py_DECREF(value); |
| 567 | start=i; |
| 568 | i--; /* Decrement now, because the 'for' loop will increment it */ |
| 569 | } |
| 570 | } /* endif repl[i]!='\\' */ |
| 571 | |
| 572 | if (start!=i) |
| 573 | { |
| 574 | PyList_Append(results, PyString_FromStringAndSize((char *)repl+start, i-start)); |
| 575 | total_len += i-start; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 576 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 577 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 578 | /* Whew! Now we've constructed a list containing various pieces of |
| 579 | strings that will make up our final result. So, iterate over |
| 580 | the list concatenating them. A new string measuring total_len |
| 581 | bytes is allocated and filled in. */ |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 582 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 583 | newstring=PyString_FromStringAndSize(NULL, total_len); |
| 584 | if (newstring==NULL) |
| 585 | { |
| 586 | Py_DECREF(results); |
| 587 | return NULL; |
| 588 | } |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 589 | |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 590 | repl=(unsigned char *)PyString_AsString(newstring); |
| 591 | for (pos=i=0; i<PyList_Size(results); i++) |
| 592 | { |
| 593 | PyObject *item=PyList_GetItem(results, i); |
| 594 | memcpy(repl+pos, PyString_AsString(item), PyString_Size(item) ); |
| 595 | pos += PyString_Size(item); |
| 596 | } |
| 597 | Py_DECREF(results); |
| 598 | return newstring; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | |
| 602 | /* List of functions defined in the module */ |
| 603 | |
| 604 | static PyMethodDef pcre_methods[] = { |
| 605 | {"pcre_compile", PyPcre_compile, 1}, |
| 606 | {"pcre_expand", PyPcre_expand, 1}, |
| 607 | {NULL, NULL} /* sentinel */ |
| 608 | }; |
| 609 | |
| 610 | |
| 611 | /* |
| 612 | * Convenience routine to export an integer value. |
| 613 | * For simplicity, errors (which are unlikely anyway) are ignored. |
| 614 | */ |
| 615 | |
| 616 | static void |
| 617 | insint(d, name, value) |
| 618 | PyObject * d; |
| 619 | char * name; |
| 620 | int value; |
| 621 | { |
| 622 | PyObject *v = PyInt_FromLong((long) value); |
| 623 | if (v == NULL) { |
| 624 | /* Don't bother reporting this error */ |
| 625 | PyErr_Clear(); |
| 626 | } |
| 627 | else { |
| 628 | PyDict_SetItemString(d, name, v); |
| 629 | Py_DECREF(v); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | |
| 634 | /* Initialization function for the module (*must* be called initpcre) */ |
| 635 | |
| 636 | void |
| 637 | initpcre() |
| 638 | { |
| 639 | PyObject *m, *d; |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 640 | |
| 641 | /* Create the module and add the functions */ |
| 642 | m = Py_InitModule("pcre", pcre_methods); |
| 643 | |
| 644 | /* Add some symbolic constants to the module */ |
| 645 | d = PyModule_GetDict(m); |
| 646 | ErrorObject = PyString_FromString("pcre.error"); |
| 647 | PyDict_SetItemString(d, "error", ErrorObject); |
| 648 | |
| 649 | /* Insert the flags */ |
| 650 | insint(d, "IGNORECASE", PCRE_CASELESS); |
| 651 | insint(d, "ANCHORED", PCRE_ANCHORED); |
| 652 | insint(d, "MULTILINE", PCRE_MULTILINE); |
| 653 | insint(d, "DOTALL", PCRE_DOTALL); |
| 654 | insint(d, "VERBOSE", PCRE_EXTENDED); |
Guido van Rossum | 5070060 | 1997-12-08 17:15:20 +0000 | [diff] [blame] | 655 | insint(d, "LOCALE", PCRE_LOCALE); |
Guido van Rossum | 51b3aa3 | 1997-10-06 14:43:11 +0000 | [diff] [blame] | 656 | |
| 657 | /* Check for errors */ |
| 658 | if (PyErr_Occurred()) |
| 659 | Py_FatalError("can't initialize module pcre"); |
| 660 | } |
| 661 | |