/* Pcre objects */ #include "Python.h" #ifndef Py_eval_input /* For Python 1.4, graminit.h has to be explicitly included */ #include "graminit.h" #define Py_eval_input eval_input #endif #ifndef FOR_PYTHON #define FOR_PYTHON #endif #include "pcre.h" #include "pcre-int.h" static PyObject *ErrorObject; typedef struct { PyObject_HEAD pcre *regex; pcre_extra *regex_extra; int num_groups; } PcreObject; staticforward PyTypeObject Pcre_Type; #define PcreObject_Check(v) ((v)->ob_type == &Pcre_Type) #define NORMAL 0 #define CHARCLASS 1 #define REPLACEMENT 2 #define CHAR 0 #define MEMORY_REFERENCE 1 #define SYNTAX 2 #define NOT_SYNTAX 3 #define SET 4 #define WORD_BOUNDARY 5 #define NOT_WORD_BOUNDARY 6 #define BEGINNING_OF_BUFFER 7 #define END_OF_BUFFER 8 #define STRING 9 static PcreObject * newPcreObject(PyObject *args) { PcreObject *self; self = PyObject_New(PcreObject, &Pcre_Type); if (self == NULL) return NULL; self->regex = NULL; self->regex_extra = NULL; return self; } /* Pcre methods */ static void PyPcre_dealloc(PcreObject *self) { if (self->regex) (pcre_free)(self->regex); if (self->regex_extra) (pcre_free)(self->regex_extra); PyObject_Del(self); } static PyObject * PyPcre_exec(PcreObject *self, PyObject *args) { char *string; int stringlen, pos = 0, options=0, endpos = -1, i, count; int offsets[100*2]; PyObject *list; if (!PyArg_ParseTuple(args, "t#|iii:match", &string, &stringlen, &pos, &endpos, &options)) return NULL; if (endpos == -1) {endpos = stringlen;} count = pcre_exec(self->regex, self->regex_extra, string, endpos, pos, options, offsets, sizeof(offsets)/sizeof(int) ); /* If an error occurred during the match, and an exception was raised, just return NULL and leave the exception alone. The most likely problem to cause this would be running out of memory for the failure stack. */ if (PyErr_Occurred()) { return NULL; } if (count==PCRE_ERROR_NOMATCH) {Py_INCREF(Py_None); return Py_None;} if (count<0) { PyObject *errval = Py_BuildValue("si", "Regex execution error", count); PyErr_SetObject(ErrorObject, errval); Py_XDECREF(errval); return NULL; } list=PyList_New(self->num_groups+1); if (list==NULL) return NULL; for(i=0; i<=self->num_groups; i++) { PyObject *v; int start=offsets[i*2], end=offsets[i*2+1]; /* If the group wasn't affected by the match, return -1, -1 */ if (start<0 || count<=i) {start=end=-1;} v=Py_BuildValue("ii", start, end); if (v==NULL) {Py_DECREF(list); return NULL;} PyList_SetItem(list, i, v); } return list; } static PyMethodDef Pcre_methods[] = { {"match", (PyCFunction)PyPcre_exec, 1}, {NULL, NULL} /* sentinel */ }; static PyObject * PyPcre_getattr(PcreObject *self, char *name) { return Py_FindMethod(Pcre_methods, (PyObject *)self, name); } staticforward PyTypeObject Pcre_Type = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ "Pcre", /*tp_name*/ sizeof(PcreObject), /*tp_basicsize*/ 0, /*tp_itemsize*/ /* methods */ (destructor)PyPcre_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ (getattrfunc)PyPcre_getattr, /*tp_getattr*/ 0, /*tp_setattr*/ 0, /*tp_compare*/ 0, /*tp_repr*/ 0, /*tp_as_number*/ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash*/ }; /* --------------------------------------------------------------------- */ static PyObject * PyPcre_compile(PyObject *self, PyObject *args) { PcreObject *rv; PyObject *dictionary; char *pattern; const char *error; int options, erroroffset; if (!PyArg_ParseTuple(args, "siO!:pcre_compile", &pattern, &options, &PyDict_Type, &dictionary)) return NULL; rv = newPcreObject(args); if ( rv == NULL ) return NULL; rv->regex = pcre_compile((char*)pattern, options, &error, &erroroffset, dictionary); if (rv->regex==NULL) { Py_DECREF(rv); if (!PyErr_Occurred()) { PyObject *errval = Py_BuildValue("si", error, erroroffset); PyErr_SetObject(ErrorObject, errval); Py_XDECREF(errval); } return NULL; } rv->regex_extra=pcre_study(rv->regex, 0, &error); if (rv->regex_extra==NULL && error!=NULL) { PyObject *errval = Py_BuildValue("si", error, 0); Py_DECREF(rv); PyErr_SetObject(ErrorObject, errval); Py_XDECREF(errval); return NULL; } rv->num_groups = pcre_info(rv->regex, NULL, NULL); if (rv->num_groups<0) { PyObject *errval = Py_BuildValue("si", error, rv->num_groups); PyErr_SetObject(ErrorObject, errval); Py_XDECREF(errval); Py_DECREF(rv); return NULL; } return (PyObject *)rv; } static PyObject * PyPcre_expand_escape(unsigned char *pattern, int pattern_len, int *indexptr, int *typeptr) { unsigned char c; int index = *indexptr; if (pattern_len<=index) { PyErr_SetString(ErrorObject, "escape ends too soon"); return NULL; } c=pattern[index]; index++; *typeptr=CHAR; switch (c) { case('t'): *indexptr=index; return Py_BuildValue("c", (char)9); case('n'): *indexptr = index; return Py_BuildValue("c", (char)10); case('v'): *indexptr = index; return Py_BuildValue("c", (char)11); case('r'): *indexptr = index; return Py_BuildValue("c", (char)13); case('f'): *indexptr = index; return Py_BuildValue("c", (char)12); case('a'): *indexptr = index; return Py_BuildValue("c", (char)7); case('b'): *indexptr=index; return Py_BuildValue("c", (char)8); case('\\'): *indexptr=index; return Py_BuildValue("c", '\\'); case('x'): { int x, ch, end; x = 0; end = index; while ( (end starting with digit"); return NULL; } else {group_num = group_num * 10 + pattern[i] - '0';} if (!(pcre_ctypes[pattern[i]] & ctype_word) ) { /* XXX should include the text of the reference */ PyErr_SetString(ErrorObject, "illegal symbolic reference"); return NULL; } } *typeptr = MEMORY_REFERENCE; *indexptr = end+1; /* If it's a number, return the integer value of the group */ if (is_number) return Py_BuildValue("i", group_num); /* Otherwise, return a string containing the group name */ return Py_BuildValue("s#", pattern+index, end-index); } break; case('0'): { /* \0 always indicates an octal escape, so we consume up to 3 characters, as long as they're all octal digits */ int octval=0, i; index--; for(i=index; i<=index+2 && i255) { PyErr_SetString(ErrorObject, "octal value out of range"); return NULL; } *indexptr = i; return Py_BuildValue("c", (unsigned char)octval); } break; case('1'): case('2'): case('3'): case('4'): case('5'): case('6'): case('7'): case('8'): case('9'): { /* Handle \?, where ? is from 1 through 9 */ int value=0; index--; /* If it's at least a two-digit reference, like \34, it might either be a 3-digit octal escape (\123) or a 2-digit decimal memory reference (\34) */ if ( (index+1) 255) { PyErr_SetString(ErrorObject, "octal value out of range"); return NULL; } *indexptr = index+3; return Py_BuildValue("c", (unsigned char)value); } else { /* 2-digit form, so it's a memory reference */ value= 10*(pattern[index ]-'0') + (pattern[index+1]-'0'); if (value<1 || EXTRACT_MAX<=value) { PyErr_SetString(ErrorObject, "memory reference out of range"); return NULL; } *typeptr = MEMORY_REFERENCE; *indexptr = index+2; return Py_BuildValue("i", value); } } else { /* Single-digit form, like \2, so it's a memory reference */ *typeptr = MEMORY_REFERENCE; *indexptr = index+1; return Py_BuildValue("i", pattern[index]-'0'); } } break; default: /* It's some unknown escape like \s, so return a string containing \s */ *typeptr = STRING; *indexptr = index; return Py_BuildValue("s#", pattern+index-2, 2); } } static PyObject * PyPcre_expand(PyObject *self, PyObject *args) { PyObject *results, *match_obj; PyObject *repl_obj, *newstring; unsigned char *repl; int size, total_len, i, start, pos; if (!PyArg_ParseTuple(args, "OS:pcre_expand", &match_obj, &repl_obj)) return NULL; repl=(unsigned char *)PyString_AsString(repl_obj); size=PyString_Size(repl_obj); results=PyList_New(0); if (results==NULL) return NULL; for(start=total_len=i=0; i