1 /* 2 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. 3 * 4 * Development of this software was funded, in part, by Cray Research Inc., 5 * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics 6 * Corporation, none of whom are responsible for the results. The author 7 * thanks all of them. 8 * 9 * Redistribution and use in source and binary forms - with or without 10 * modification - are permitted for any purpose, provided that redistributions 11 * in source form retain this entire copyright notice and indicate the origin 12 * and nature of any modifications. 13 * 14 * I'd appreciate being given credit for this package in the documentation of 15 * software which uses it, but that is not a requirement. 16 * 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 20 * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Headers if any. 31 */ 32 33 #include "regex.h" 34 35 /* 36 * Overrides for regguts.h definitions, if any. 37 */ 38 39 #define MALLOC(n) (void*)(attemptckalloc(n)) 40 #define FREE(p) ckfree((void*)(p)) 41 #define REALLOC(p,n) (void*)(attemptckrealloc((void*)(p),n)) 42 43 /* 44 * Do not insert extras between the "begin" and "end" lines - this chunk is 45 * automatically extracted to be fitted into regex.h. 46 */ 47 48 /* --- begin --- */ 49 /* Ensure certain things don't sneak in from system headers. */ 50 #ifdef __REG_WIDE_T 51 #undef __REG_WIDE_T 52 #endif 53 #ifdef __REG_WIDE_COMPILE 54 #undef __REG_WIDE_COMPILE 55 #endif 56 #ifdef __REG_WIDE_EXEC 57 #undef __REG_WIDE_EXEC 58 #endif 59 #ifdef __REG_REGOFF_T 60 #undef __REG_REGOFF_T 61 #endif 62 #ifdef __REG_NOFRONT 63 #undef __REG_NOFRONT 64 #endif 65 #ifdef __REG_NOCHAR 66 #undef __REG_NOCHAR 67 #endif 68 /* Interface types */ 69 #define __REG_WIDE_T Tcl_UniChar 70 #define __REG_REGOFF_T long /* Not really right, but good enough... */ 71 /* Names and declarations */ 72 #define __REG_WIDE_COMPILE TclReComp 73 #define __REG_WIDE_EXEC TclReExec 74 #define __REG_NOFRONT /* Don't want regcomp() and regexec() */ 75 #define __REG_NOCHAR /* Or the char versions */ 76 #define regfree TclReFree 77 #define regerror TclReError 78 /* --- end --- */ 79 80 /* 81 * Internal character type and related. 82 */ 83 84 typedef Tcl_UniChar chr; /* The type itself. */ 85 typedef int pchr; /* What it promotes to. */ 86 typedef unsigned uchr; /* Unsigned type that will hold a chr. */ 87 typedef int celt; /* Type to hold chr, or NOCELT */ 88 #define NOCELT (-1) /* Celt value which is not valid chr */ 89 #define CHR(c) (UCHAR(c)) /* Turn char literal into chr literal */ 90 #define DIGITVAL(c) ((c)-'0') /* Turn chr digit into its value */ 91 #if TCL_UTF_MAX > 3 92 #define CHRBITS 32 /* Bits in a chr; must not use sizeof */ 93 #define CHR_MIN 0x00000000 /* Smallest and largest chr; the value */ 94 #define CHR_MAX 0x10FFFF /* CHR_MAX-CHR_MIN+1 should fit in uchr */ 95 #else 96 #define CHRBITS 16 /* Bits in a chr; must not use sizeof */ 97 #define CHR_MIN 0x0000 /* Smallest and largest chr; the value */ 98 #define CHR_MAX 0xFFFF /* CHR_MAX-CHR_MIN+1 should fit in uchr */ 99 #endif 100 101 /* 102 * Functions operating on chr. 103 */ 104 105 #define iscalnum(x) Tcl_UniCharIsAlnum(x) 106 #define iscalpha(x) Tcl_UniCharIsAlpha(x) 107 #define iscdigit(x) Tcl_UniCharIsDigit(x) 108 #define iscspace(x) Tcl_UniCharIsSpace(x) 109 110 /* 111 * Name the external functions. 112 */ 113 114 #define compile TclReComp 115 #define exec TclReExec 116 117 /* 118 & Enable/disable debugging code (by whether REG_DEBUG is defined or not). 119 */ 120 121 #if 0 /* No debug unless requested by makefile. */ 122 #define REG_DEBUG /* */ 123 #endif 124 125 /* 126 * Method of allocating a local workspace. We used a thread-specific data 127 * space to store this because the regular expression engine is never 128 * reentered from the same thread; it doesn't make any callbacks. 129 */ 130 131 #if 1 132 #define AllocVars(vPtr) \ 133 static Tcl_ThreadDataKey varsKey; \ 134 struct vars *vPtr = (struct vars *) \ 135 Tcl_GetThreadData(&varsKey, sizeof(struct vars)) 136 #else 137 /* 138 * This strategy for allocating workspace is "more proper" in some sense, but 139 * quite a bit slower. Using TSD (as above) leads to code that is quite a bit 140 * faster in practice (measured!) 141 */ 142 #define AllocVars(vPtr) \ 143 struct vars *vPtr = (struct vars *) MALLOC(sizeof(struct vars)) 144 #define FreeVars(vPtr) \ 145 FREE(vPtr) 146 #endif 147 148 /* 149 * Local Variables: 150 * mode: c 151 * c-basic-offset: 4 152 * fill-column: 78 153 * End: 154 */ 155