/*********************************************************** Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI or Corporation for National Research Initiatives or CNRI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. While CWI is the initial source for this software, a modified version is made available by the Corporation for National Research Initiatives (CNRI) at the Internet address ftp://ftp.python.org. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ******************************************************************/ /* $Id$ */ /* Regular expression objects */ /* This uses Tatu Ylonen's copyleft-free reimplementation of GNU regular expressions */ #include "Python.h" #include #include "regexpr.h" static PyObject *ReopError; /* Exception */ #define IGNORECASE 0x01 #define MULTILINE 0x02 #define DOTALL 0x04 #define VERBOSE 0x08 #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 static PyObject * makeresult(regs, num_regs) struct re_registers *regs; int num_regs; { PyObject *v; int i; static PyObject *filler = NULL; if (filler == NULL) { filler = Py_BuildValue("(ii)", -1, -1); if (filler == NULL) return NULL; } v = PyTuple_New(num_regs); if (v == NULL) return NULL; for (i = 0; i < num_regs; i++) { int lo = regs->start[i]; int hi = regs->end[i]; PyObject *w; if (lo == -1 && hi == -1) { w = filler; Py_INCREF(w); } else w = Py_BuildValue("(ii)", lo, hi); if (w == NULL || PyTuple_SetItem(v, i, w) < 0) { Py_DECREF(v); return NULL; } } return v; } static PyObject * reop_match(self, args) PyObject *self; PyObject *args; { unsigned char *string; int fastmaplen, stringlen; int can_be_null, anchor, i; int flags, pos, result; struct re_pattern_buffer bufp; struct re_registers re_regs; PyObject *modules = NULL; PyObject *reopmodule = NULL; PyObject *reopdict = NULL; PyObject *casefold = NULL; if (!PyArg_Parse(args, "(s#iiis#is#i)", &(bufp.buffer), &(bufp.allocated), &(bufp.num_registers), &flags, &can_be_null, &(bufp.fastmap), &fastmaplen, &anchor, &string, &stringlen, &pos)) return NULL; /* XXX sanity-check the input data */ bufp.used=bufp.allocated; if (flags & IGNORECASE) { if ((modules = PyImport_GetModuleDict()) == NULL) return NULL; if ((reopmodule = PyDict_GetItemString(modules, "reop")) == NULL) return NULL; if ((reopdict = PyModule_GetDict(reopmodule)) == NULL) return NULL; if ((casefold = PyDict_GetItemString(reopdict, "casefold")) == NULL) return NULL; bufp.translate = (unsigned char*)PyString_AsString(casefold); } else bufp.translate=NULL; bufp.fastmap_accurate=1; bufp.can_be_null=can_be_null; bufp.uses_registers=1; bufp.anchor=anchor; for(i=0; i", which will be passed to eval() */ string[0]=string[length+3]='"'; string[1]='\\'; string[length+4]='\0'; memcpy(string+2, pattern+index-1, length+1); v=PyRun_String((char *)string, Py_eval_input, PyEval_GetGlobals(), PyEval_GetLocals()); free(string); /* The evaluation raised an exception */ if (v==NULL) return NULL; result=Py_BuildValue("iOi", CHAR, v, end); Py_DECREF(v); return result; } break; case('b'): if (context!=NORMAL) return Py_BuildValue("ici", CHAR, (char)8, index); else { unsigned char empty_string[1]; empty_string[0]='\0'; return Py_BuildValue("isi", WORD_BOUNDARY, empty_string, index); } break; case('B'): if (context!=NORMAL) return Py_BuildValue("ici", CHAR, 'B', index); else { unsigned char empty_string[1]; empty_string[0]='\0'; return Py_BuildValue("isi", NOT_WORD_BOUNDARY, empty_string, index); } break; case('A'): if (context!=NORMAL) return Py_BuildValue("ici", CHAR, 'A', index); else { unsigned char empty_string[1]; empty_string[0]='\0'; return Py_BuildValue("isi", BEGINNING_OF_BUFFER, empty_string, index); } break; case('Z'): if (context!=NORMAL) return Py_BuildValue("ici", CHAR, 'Z', index); else { unsigned char empty_string[1]; empty_string[0]='\0'; return Py_BuildValue("isi", END_OF_BUFFER, empty_string, index); } break; case('E'): case('G'): case('L'): case('Q'): case('U'): case('l'): case('u'): { char message[50]; sprintf(message, "\\%c is not allowed", c); PyErr_SetString(ReopError, message); return NULL; } case ('w'): if (context==NORMAL) return Py_BuildValue("iii", SYNTAX, Sword, index); if (context!=CHARCLASS) return Py_BuildValue("ici", CHAR, 'w', index); { /* context==CHARCLASS */ unsigned char set[256]; int i, j; for(i=j=0; i<256; i++) if (re_syntax_table[i] & Sword) { set[j++] = i; } return Py_BuildValue("is#i", SET, set, j, index); } break; case ('W'): if (context==NORMAL) return Py_BuildValue("iii", NOT_SYNTAX, Sword, index); if (context!=CHARCLASS) return Py_BuildValue("ici", CHAR, 'W', index); { /* context==CHARCLASS */ unsigned char set[256]; int i, j; for(i=j=0; i<256; i++) if (! (re_syntax_table[i] & Sword)) { set[j++] = i; } return Py_BuildValue("is#i", SET, set, j, index); } break; case ('s'): if (context==NORMAL) return Py_BuildValue("iii", SYNTAX, Swhitespace, index); if (context!=CHARCLASS) return Py_BuildValue("ici", CHAR, 's', index); { /* context==CHARCLASS */ unsigned char set[256]; int i, j; for(i=j=0; i<256; i++) if (re_syntax_table[i] & Swhitespace) { set[j++] = i; } return Py_BuildValue("is#i", SET, set, j, index); } break; case ('S'): if (context==NORMAL) return Py_BuildValue("iii", NOT_SYNTAX, Swhitespace, index); if (context!=CHARCLASS) return Py_BuildValue("ici", CHAR, 'S', index); { /* context==CHARCLASS */ unsigned char set[256]; int i, j; for(i=j=0; i<256; i++) if (! (re_syntax_table[i] & Swhitespace) ) { set[j++] = i; } return Py_BuildValue("is#i", SET, set, j, index); } break; case ('d'): if (context==NORMAL) return Py_BuildValue("iii", SYNTAX, Sdigit, index); if (context!=CHARCLASS) return Py_BuildValue("ici", CHAR, 'd', index); { /* context==CHARCLASS */ unsigned char set[256]; int i, j; for(i=j=0; i<256; i++) if (re_syntax_table[i] & Sdigit) { set[j++] = i; } return Py_BuildValue("is#i", SET, set, j, index); } break; case ('D'): if (context==NORMAL) return Py_BuildValue("iii", NOT_SYNTAX, Sdigit, index); if (context!=CHARCLASS) return Py_BuildValue("ici", CHAR, 'D', index); { /* context==CHARCLASS */ unsigned char set[256]; int i, j; for(i=j=0; i<256; i++) if ( !(re_syntax_table[i] & Sdigit) ) { set[j++] = i; } return Py_BuildValue("is#i", SET, set, j, index); } break; case('g'): { int end, valid, i; if (context!=REPLACEMENT) return Py_BuildValue("ici", CHAR, 'g', index); if (pattern_len<=index) { PyErr_SetString(ReopError, "unfinished symbolic reference"); return NULL; } if (pattern[index]!='<') { PyErr_SetString(ReopError, "missing < in symbolic reference"); return NULL; } index++; end=index; while (end255) { PyErr_SetString(ReopError, "octal value out of range"); return NULL; } return Py_BuildValue("ici", CHAR, (unsigned char)octval, i); } 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(ReopError, "octal value out of range"); return NULL; } return Py_BuildValue("ici", CHAR, (unsigned char)value, index+3); } else { /* 2-digit form, so it's a memory reference */ if (context==CHARCLASS) { PyErr_SetString(ReopError, "cannot reference a register from inside a character class"); return NULL; } value= 10*(pattern[index ]-'0') + (pattern[index+1]-'0'); if (value<1 || RE_NREGS<=value) { PyErr_SetString(ReopError, "memory reference out of range"); return NULL; } return Py_BuildValue("iii", MEMORY_REFERENCE, value, index+2); } } else { /* Single-digit form, like \2, so it's a memory reference */ if (context==CHARCLASS) { PyErr_SetString(ReopError, "cannot reference a register from inside a character class"); return NULL; } return Py_BuildValue("iii", MEMORY_REFERENCE, pattern[index]-'0', index+1); } } break; default: return Py_BuildValue("ici", CHAR, c, index); break; } } static PyObject * reop__expand(self, args) 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", &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; ire_patbuf, start, length, next, length-next, &pattern->re_regs); if (result < -1) { /* Erk... an error happened during the reop search */ Py_DECREF(newlist); PyErr_SetString(ReopError, "match failure"); return NULL; } if (next<=result) { int match_start=pattern->re_regs.start[0]; int oldmatch_end=match_end; match_end=pattern->re_regs.end[0]; if (match_start==match_end) { /* A zero-length match; increment to the next position */ next=result+1; match_end=oldmatch_end; continue; } /* Append the string up to the start of the match */ s=PyString_FromStringAndSize(start+oldmatch_end, match_start-oldmatch_end); if (!s) { Py_DECREF(newlist); return NULL; } PyList_Append(newlist, s); Py_DECREF(s); if (retain) { /* Append a string containing whatever matched */ s=PyString_FromStringAndSize(start+match_start, match_end-match_start); if (!s) { Py_DECREF(newlist); return NULL; } PyList_Append(newlist, s); Py_DECREF(s); } /* Update the pointer, and increment the count of splits */ next=match_end; count++; } } while (result!=-1 && !(maxsplit && maxsplit==count) && next