| 1 |
/* |
| 2 |
* tclRegexp.c -- |
| 3 |
* |
| 4 |
* This file contains the public interfaces to the Tcl regular expression |
| 5 |
* mechanism. |
| 6 |
* |
| 7 |
* Copyright (c) 1998 by Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 1998-1999 by Scriptics Corporation. |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclInt.h" |
| 15 |
#include "tclRegexp.h" |
| 16 |
|
| 17 |
/* |
| 18 |
*---------------------------------------------------------------------- |
| 19 |
* The routines in this file use Henry Spencer's regular expression package |
| 20 |
* contained in the following additional source files: |
| 21 |
* |
| 22 |
* regc_color.c regc_cvec.c regc_lex.c |
| 23 |
* regc_nfa.c regcomp.c regcustom.h |
| 24 |
* rege_dfa.c regerror.c regerrs.h |
| 25 |
* regex.h regexec.c regfree.c |
| 26 |
* regfronts.c regguts.h |
| 27 |
* |
| 28 |
* Copyright (c) 1998 Henry Spencer. All rights reserved. |
| 29 |
* |
| 30 |
* Development of this software was funded, in part, by Cray Research Inc., |
| 31 |
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics |
| 32 |
* Corporation, none of whom are responsible for the results. The author |
| 33 |
* thanks all of them. |
| 34 |
* |
| 35 |
* Redistribution and use in source and binary forms -- with or without |
| 36 |
* modification -- are permitted for any purpose, provided that |
| 37 |
* redistributions in source form retain this entire copyright notice and |
| 38 |
* indicate the origin and nature of any modifications. |
| 39 |
* |
| 40 |
* I'd appreciate being given credit for this package in the documentation of |
| 41 |
* software which uses it, but that is not a requirement. |
| 42 |
* |
| 43 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 44 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 45 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 46 |
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 47 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 48 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 49 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 50 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 51 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 52 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 53 |
* |
| 54 |
* *** NOTE: this code has been altered slightly for use in Tcl: *** |
| 55 |
* *** 1. Names have been changed, e.g. from re_comp to *** |
| 56 |
* *** TclRegComp, to avoid clashes with other *** |
| 57 |
* *** regexp implementations used by applications. *** |
| 58 |
*/ |
| 59 |
|
| 60 |
/* |
| 61 |
* Thread local storage used to maintain a per-thread cache of compiled |
| 62 |
* regular expressions. |
| 63 |
*/ |
| 64 |
|
| 65 |
#define NUM_REGEXPS 30 |
| 66 |
|
| 67 |
typedef struct { |
| 68 |
int initialized; /* Set to 1 when the module is initialized. */ |
| 69 |
char *patterns[NUM_REGEXPS];/* Strings corresponding to compiled regular |
| 70 |
* expression patterns. NULL means that this |
| 71 |
* slot isn't used. Malloc-ed. */ |
| 72 |
int patLengths[NUM_REGEXPS];/* Number of non-null characters in |
| 73 |
* corresponding entry in patterns. -1 means |
| 74 |
* entry isn't used. */ |
| 75 |
struct TclRegexp *regexps[NUM_REGEXPS]; |
| 76 |
/* Compiled forms of above strings. Also |
| 77 |
* malloc-ed, or NULL if not in use yet. */ |
| 78 |
} ThreadSpecificData; |
| 79 |
|
| 80 |
static Tcl_ThreadDataKey dataKey; |
| 81 |
|
| 82 |
/* |
| 83 |
* Declarations for functions used only in this file. |
| 84 |
*/ |
| 85 |
|
| 86 |
static TclRegexp * CompileRegexp(Tcl_Interp *interp, const char *pattern, |
| 87 |
int length, int flags); |
| 88 |
static void DupRegexpInternalRep(Tcl_Obj *srcPtr, |
| 89 |
Tcl_Obj *copyPtr); |
| 90 |
static void FinalizeRegexp(ClientData clientData); |
| 91 |
static void FreeRegexp(TclRegexp *regexpPtr); |
| 92 |
static void FreeRegexpInternalRep(Tcl_Obj *objPtr); |
| 93 |
static int RegExpExecUniChar(Tcl_Interp *interp, Tcl_RegExp re, |
| 94 |
const Tcl_UniChar *uniString, int numChars, |
| 95 |
int nmatches, int flags); |
| 96 |
static int SetRegexpFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr); |
| 97 |
|
| 98 |
/* |
| 99 |
* The regular expression Tcl object type. This serves as a cache of the |
| 100 |
* compiled form of the regular expression. |
| 101 |
*/ |
| 102 |
|
| 103 |
const Tcl_ObjType tclRegexpType = { |
| 104 |
"regexp", /* name */ |
| 105 |
FreeRegexpInternalRep, /* freeIntRepProc */ |
| 106 |
DupRegexpInternalRep, /* dupIntRepProc */ |
| 107 |
NULL, /* updateStringProc */ |
| 108 |
SetRegexpFromAny /* setFromAnyProc */ |
| 109 |
}; |
| 110 |
|
| 111 |
/* |
| 112 |
*---------------------------------------------------------------------- |
| 113 |
* |
| 114 |
* Tcl_RegExpCompile -- |
| 115 |
* |
| 116 |
* Compile a regular expression into a form suitable for fast matching. |
| 117 |
* This function is DEPRECATED in favor of the object version of the |
| 118 |
* command. |
| 119 |
* |
| 120 |
* Results: |
| 121 |
* The return value is a pointer to the compiled form of string, suitable |
| 122 |
* for passing to Tcl_RegExpExec. This compiled form is only valid up |
| 123 |
* until the next call to this function, so don't keep these around for a |
| 124 |
* long time! If an error occurred while compiling the pattern, then NULL |
| 125 |
* is returned and an error message is left in the interp's result. |
| 126 |
* |
| 127 |
* Side effects: |
| 128 |
* Updates the cache of compiled regexps. |
| 129 |
* |
| 130 |
*---------------------------------------------------------------------- |
| 131 |
*/ |
| 132 |
|
| 133 |
Tcl_RegExp |
| 134 |
Tcl_RegExpCompile( |
| 135 |
Tcl_Interp *interp, /* For use in error reporting and to access |
| 136 |
* the interp regexp cache. */ |
| 137 |
const char *pattern) /* String for which to produce compiled |
| 138 |
* regular expression. */ |
| 139 |
{ |
| 140 |
return (Tcl_RegExp) CompileRegexp(interp, pattern, (int) strlen(pattern), |
| 141 |
REG_ADVANCED); |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
*---------------------------------------------------------------------- |
| 146 |
* |
| 147 |
* Tcl_RegExpExec -- |
| 148 |
* |
| 149 |
* Execute the regular expression matcher using a compiled form of a |
| 150 |
* regular expression and save information about any match that is found. |
| 151 |
* |
| 152 |
* Results: |
| 153 |
* If an error occurs during the matching operation then -1 is returned |
| 154 |
* and the interp's result contains an error message. Otherwise the |
| 155 |
* return value is 1 if a matching range is found and 0 if there is no |
| 156 |
* matching range. |
| 157 |
* |
| 158 |
* Side effects: |
| 159 |
* None. |
| 160 |
* |
| 161 |
*---------------------------------------------------------------------- |
| 162 |
*/ |
| 163 |
|
| 164 |
int |
| 165 |
Tcl_RegExpExec( |
| 166 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. */ |
| 167 |
Tcl_RegExp re, /* Compiled regular expression; must have been |
| 168 |
* returned by previous call to |
| 169 |
* Tcl_GetRegExpFromObj. */ |
| 170 |
const char *text, /* Text against which to match re. */ |
| 171 |
const char *start) /* If text is part of a larger string, this |
| 172 |
* identifies beginning of larger string, so |
| 173 |
* that "^" won't match. */ |
| 174 |
{ |
| 175 |
int flags, result, numChars; |
| 176 |
TclRegexp *regexp = (TclRegexp *) re; |
| 177 |
Tcl_DString ds; |
| 178 |
const Tcl_UniChar *ustr; |
| 179 |
|
| 180 |
/* |
| 181 |
* If the starting point is offset from the beginning of the buffer, then |
| 182 |
* we need to tell the regexp engine not to match "^". |
| 183 |
*/ |
| 184 |
|
| 185 |
if (text > start) { |
| 186 |
flags = REG_NOTBOL; |
| 187 |
} else { |
| 188 |
flags = 0; |
| 189 |
} |
| 190 |
|
| 191 |
/* |
| 192 |
* Remember the string for use by Tcl_RegExpRange(). |
| 193 |
*/ |
| 194 |
|
| 195 |
regexp->string = text; |
| 196 |
regexp->objPtr = NULL; |
| 197 |
|
| 198 |
/* |
| 199 |
* Convert the string to Unicode and perform the match. |
| 200 |
*/ |
| 201 |
|
| 202 |
Tcl_DStringInit(&ds); |
| 203 |
ustr = Tcl_UtfToUniCharDString(text, -1, &ds); |
| 204 |
numChars = Tcl_DStringLength(&ds) / sizeof(Tcl_UniChar); |
| 205 |
result = RegExpExecUniChar(interp, re, ustr, numChars, -1 /* nmatches */, |
| 206 |
flags); |
| 207 |
Tcl_DStringFree(&ds); |
| 208 |
|
| 209 |
return result; |
| 210 |
} |
| 211 |
|
| 212 |
/* |
| 213 |
*--------------------------------------------------------------------------- |
| 214 |
* |
| 215 |
* Tcl_RegExpRange -- |
| 216 |
* |
| 217 |
* Returns pointers describing the range of a regular expression match, |
| 218 |
* or one of the subranges within the match. |
| 219 |
* |
| 220 |
* Results: |
| 221 |
* The variables at *startPtr and *endPtr are modified to hold the |
| 222 |
* addresses of the endpoints of the range given by index. If the |
| 223 |
* specified range doesn't exist then NULLs are returned. |
| 224 |
* |
| 225 |
* Side effects: |
| 226 |
* None. |
| 227 |
* |
| 228 |
*--------------------------------------------------------------------------- |
| 229 |
*/ |
| 230 |
|
| 231 |
void |
| 232 |
Tcl_RegExpRange( |
| 233 |
Tcl_RegExp re, /* Compiled regular expression that has been |
| 234 |
* passed to Tcl_RegExpExec. */ |
| 235 |
int index, /* 0 means give the range of the entire match, |
| 236 |
* > 0 means give the range of a matching |
| 237 |
* subrange. */ |
| 238 |
const char **startPtr, /* Store address of first character in |
| 239 |
* (sub-)range here. */ |
| 240 |
const char **endPtr) /* Store address of character just after last |
| 241 |
* in (sub-)range here. */ |
| 242 |
{ |
| 243 |
TclRegexp *regexpPtr = (TclRegexp *) re; |
| 244 |
const char *string; |
| 245 |
|
| 246 |
if ((size_t) index > regexpPtr->re.re_nsub) { |
| 247 |
*startPtr = *endPtr = NULL; |
| 248 |
} else if (regexpPtr->matches[index].rm_so == -1) { |
| 249 |
*startPtr = *endPtr = NULL; |
| 250 |
} else { |
| 251 |
if (regexpPtr->objPtr) { |
| 252 |
string = TclGetString(regexpPtr->objPtr); |
| 253 |
} else { |
| 254 |
string = regexpPtr->string; |
| 255 |
} |
| 256 |
*startPtr = Tcl_UtfAtIndex(string, regexpPtr->matches[index].rm_so); |
| 257 |
*endPtr = Tcl_UtfAtIndex(string, regexpPtr->matches[index].rm_eo); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
*--------------------------------------------------------------------------- |
| 263 |
* |
| 264 |
* RegExpExecUniChar -- |
| 265 |
* |
| 266 |
* Execute the regular expression matcher using a compiled form of a |
| 267 |
* regular expression and save information about any match that is found. |
| 268 |
* |
| 269 |
* Results: |
| 270 |
* If an error occurs during the matching operation then -1 is returned |
| 271 |
* and an error message is left in interp's result. Otherwise the return |
| 272 |
* value is 1 if a matching range was found or 0 if there was no matching |
| 273 |
* range. |
| 274 |
* |
| 275 |
* Side effects: |
| 276 |
* None. |
| 277 |
* |
| 278 |
*---------------------------------------------------------------------- |
| 279 |
*/ |
| 280 |
|
| 281 |
static int |
| 282 |
RegExpExecUniChar( |
| 283 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. */ |
| 284 |
Tcl_RegExp re, /* Compiled regular expression; returned by a |
| 285 |
* previous call to Tcl_GetRegExpFromObj */ |
| 286 |
const Tcl_UniChar *wString, /* String against which to match re. */ |
| 287 |
int numChars, /* Length of Tcl_UniChar string (must be |
| 288 |
* >=0). */ |
| 289 |
int nmatches, /* How many subexpression matches (counting |
| 290 |
* the whole match as subexpression 0) are of |
| 291 |
* interest. -1 means "don't know". */ |
| 292 |
int flags) /* Regular expression flags. */ |
| 293 |
{ |
| 294 |
int status; |
| 295 |
TclRegexp *regexpPtr = (TclRegexp *) re; |
| 296 |
size_t last = regexpPtr->re.re_nsub + 1; |
| 297 |
size_t nm = last; |
| 298 |
|
| 299 |
if (nmatches >= 0 && (size_t) nmatches < nm) { |
| 300 |
nm = (size_t) nmatches; |
| 301 |
} |
| 302 |
|
| 303 |
status = TclReExec(®expPtr->re, wString, (size_t) numChars, |
| 304 |
®expPtr->details, nm, regexpPtr->matches, flags); |
| 305 |
|
| 306 |
/* |
| 307 |
* Check for errors. |
| 308 |
*/ |
| 309 |
|
| 310 |
if (status != REG_OKAY) { |
| 311 |
if (status == REG_NOMATCH) { |
| 312 |
return 0; |
| 313 |
} |
| 314 |
if (interp != NULL) { |
| 315 |
TclRegError(interp, "error while matching regular expression: ", |
| 316 |
status); |
| 317 |
} |
| 318 |
return -1; |
| 319 |
} |
| 320 |
return 1; |
| 321 |
} |
| 322 |
|
| 323 |
/* |
| 324 |
*--------------------------------------------------------------------------- |
| 325 |
* |
| 326 |
* TclRegExpRangeUniChar -- |
| 327 |
* |
| 328 |
* Returns pointers describing the range of a regular expression match, |
| 329 |
* or one of the subranges within the match, or the hypothetical range |
| 330 |
* represented by the rm_extend field of the rm_detail_t. |
| 331 |
* |
| 332 |
* Results: |
| 333 |
* The variables at *startPtr and *endPtr are modified to hold the |
| 334 |
* offsets of the endpoints of the range given by index. If the specified |
| 335 |
* range doesn't exist then -1s are supplied. |
| 336 |
* |
| 337 |
* Side effects: |
| 338 |
* None. |
| 339 |
* |
| 340 |
*--------------------------------------------------------------------------- |
| 341 |
*/ |
| 342 |
|
| 343 |
void |
| 344 |
TclRegExpRangeUniChar( |
| 345 |
Tcl_RegExp re, /* Compiled regular expression that has been |
| 346 |
* passed to Tcl_RegExpExec. */ |
| 347 |
int index, /* 0 means give the range of the entire match, |
| 348 |
* > 0 means give the range of a matching |
| 349 |
* subrange, -1 means the range of the |
| 350 |
* rm_extend field. */ |
| 351 |
int *startPtr, /* Store address of first character in |
| 352 |
* (sub-)range here. */ |
| 353 |
int *endPtr) /* Store address of character just after last |
| 354 |
* in (sub-)range here. */ |
| 355 |
{ |
| 356 |
TclRegexp *regexpPtr = (TclRegexp *) re; |
| 357 |
|
| 358 |
if ((regexpPtr->flags®_EXPECT) && (index == -1)) { |
| 359 |
*startPtr = regexpPtr->details.rm_extend.rm_so; |
| 360 |
*endPtr = regexpPtr->details.rm_extend.rm_eo; |
| 361 |
} else if ((size_t) index > regexpPtr->re.re_nsub) { |
| 362 |
*startPtr = -1; |
| 363 |
*endPtr = -1; |
| 364 |
} else { |
| 365 |
*startPtr = regexpPtr->matches[index].rm_so; |
| 366 |
*endPtr = regexpPtr->matches[index].rm_eo; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
/* |
| 371 |
*---------------------------------------------------------------------- |
| 372 |
* |
| 373 |
* Tcl_RegExpMatch -- |
| 374 |
* |
| 375 |
* See if a string matches a regular expression. |
| 376 |
* |
| 377 |
* Results: |
| 378 |
* If an error occurs during the matching operation then -1 is returned |
| 379 |
* and the interp's result contains an error message. Otherwise the |
| 380 |
* return value is 1 if "text" matches "pattern" and 0 otherwise. |
| 381 |
* |
| 382 |
* Side effects: |
| 383 |
* None. |
| 384 |
* |
| 385 |
*---------------------------------------------------------------------- |
| 386 |
*/ |
| 387 |
|
| 388 |
int |
| 389 |
Tcl_RegExpMatch( |
| 390 |
Tcl_Interp *interp, /* Used for error reporting. May be NULL. */ |
| 391 |
const char *text, /* Text to search for pattern matches. */ |
| 392 |
const char *pattern) /* Regular expression to match against text. */ |
| 393 |
{ |
| 394 |
Tcl_RegExp re = Tcl_RegExpCompile(interp, pattern); |
| 395 |
|
| 396 |
if (re == NULL) { |
| 397 |
return -1; |
| 398 |
} |
| 399 |
return Tcl_RegExpExec(interp, re, text, text); |
| 400 |
} |
| 401 |
|
| 402 |
/* |
| 403 |
*---------------------------------------------------------------------- |
| 404 |
* |
| 405 |
* Tcl_RegExpExecObj -- |
| 406 |
* |
| 407 |
* Execute a precompiled regexp against the given object. |
| 408 |
* |
| 409 |
* Results: |
| 410 |
* If an error occurs during the matching operation then -1 is returned |
| 411 |
* and the interp's result contains an error message. Otherwise the |
| 412 |
* return value is 1 if "string" matches "pattern" and 0 otherwise. |
| 413 |
* |
| 414 |
* Side effects: |
| 415 |
* Converts the object to a Unicode object. |
| 416 |
* |
| 417 |
*---------------------------------------------------------------------- |
| 418 |
*/ |
| 419 |
|
| 420 |
int |
| 421 |
Tcl_RegExpExecObj( |
| 422 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. */ |
| 423 |
Tcl_RegExp re, /* Compiled regular expression; must have been |
| 424 |
* returned by previous call to |
| 425 |
* Tcl_GetRegExpFromObj. */ |
| 426 |
Tcl_Obj *textObj, /* Text against which to match re. */ |
| 427 |
int offset, /* Character index that marks where matching |
| 428 |
* should begin. */ |
| 429 |
int nmatches, /* How many subexpression matches (counting |
| 430 |
* the whole match as subexpression 0) are of |
| 431 |
* interest. -1 means all of them. */ |
| 432 |
int flags) /* Regular expression execution flags. */ |
| 433 |
{ |
| 434 |
TclRegexp *regexpPtr = (TclRegexp *) re; |
| 435 |
Tcl_UniChar *udata; |
| 436 |
int length; |
| 437 |
int reflags = regexpPtr->flags; |
| 438 |
#define TCL_REG_GLOBOK_FLAGS \ |
| 439 |
(TCL_REG_ADVANCED | TCL_REG_NOSUB | TCL_REG_NOCASE) |
| 440 |
|
| 441 |
/* |
| 442 |
* Take advantage of the equivalent glob pattern, if one exists. |
| 443 |
* This is possible based only on the right mix of incoming flags (0) |
| 444 |
* and regexp compile flags. |
| 445 |
*/ |
| 446 |
if ((offset == 0) && (nmatches == 0) && (flags == 0) |
| 447 |
&& !(reflags & ~TCL_REG_GLOBOK_FLAGS) |
| 448 |
&& (regexpPtr->globObjPtr != NULL)) { |
| 449 |
int nocase = (reflags & TCL_REG_NOCASE) ? TCL_MATCH_NOCASE : 0; |
| 450 |
|
| 451 |
/* |
| 452 |
* Pass to TclStringMatchObj for obj-specific handling. |
| 453 |
* XXX: Currently doesn't take advantage of exact-ness that |
| 454 |
* XXX: TclReToGlob tells us about |
| 455 |
*/ |
| 456 |
|
| 457 |
return TclStringMatchObj(textObj, regexpPtr->globObjPtr, nocase); |
| 458 |
} |
| 459 |
|
| 460 |
/* |
| 461 |
* Save the target object so we can extract strings from it later. |
| 462 |
*/ |
| 463 |
|
| 464 |
regexpPtr->string = NULL; |
| 465 |
regexpPtr->objPtr = textObj; |
| 466 |
|
| 467 |
udata = Tcl_GetUnicodeFromObj(textObj, &length); |
| 468 |
|
| 469 |
if (offset > length) { |
| 470 |
offset = length; |
| 471 |
} |
| 472 |
udata += offset; |
| 473 |
length -= offset; |
| 474 |
|
| 475 |
return RegExpExecUniChar(interp, re, udata, length, nmatches, flags); |
| 476 |
} |
| 477 |
|
| 478 |
/* |
| 479 |
*---------------------------------------------------------------------- |
| 480 |
* |
| 481 |
* Tcl_RegExpMatchObj -- |
| 482 |
* |
| 483 |
* See if an object matches a regular expression. |
| 484 |
* |
| 485 |
* Results: |
| 486 |
* If an error occurs during the matching operation then -1 is returned |
| 487 |
* and the interp's result contains an error message. Otherwise the |
| 488 |
* return value is 1 if "text" matches "pattern" and 0 otherwise. |
| 489 |
* |
| 490 |
* Side effects: |
| 491 |
* Changes the internal rep of the pattern and string objects. |
| 492 |
* |
| 493 |
*---------------------------------------------------------------------- |
| 494 |
*/ |
| 495 |
|
| 496 |
int |
| 497 |
Tcl_RegExpMatchObj( |
| 498 |
Tcl_Interp *interp, /* Used for error reporting. May be NULL. */ |
| 499 |
Tcl_Obj *textObj, /* Object containing the String to search. */ |
| 500 |
Tcl_Obj *patternObj) /* Regular expression to match against |
| 501 |
* string. */ |
| 502 |
{ |
| 503 |
Tcl_RegExp re; |
| 504 |
|
| 505 |
/* |
| 506 |
* For performance reasons, first try compiling the RE without support for |
| 507 |
* subexpressions. On failure, try again without TCL_REG_NOSUB in case the |
| 508 |
* RE has backreferences in it. Closely related to [Bug 1366683]. If this |
| 509 |
* still fails, an error message will be left in the interpreter. |
| 510 |
*/ |
| 511 |
|
| 512 |
if (!(re = Tcl_GetRegExpFromObj(interp, patternObj, |
| 513 |
TCL_REG_ADVANCED | TCL_REG_NOSUB)) |
| 514 |
&& !(re = Tcl_GetRegExpFromObj(interp, patternObj, TCL_REG_ADVANCED))) { |
| 515 |
return -1; |
| 516 |
} |
| 517 |
return Tcl_RegExpExecObj(interp, re, textObj, 0 /* offset */, |
| 518 |
0 /* nmatches */, 0 /* flags */); |
| 519 |
} |
| 520 |
|
| 521 |
/* |
| 522 |
*---------------------------------------------------------------------- |
| 523 |
* |
| 524 |
* Tcl_RegExpGetInfo -- |
| 525 |
* |
| 526 |
* Retrieve information about the current match. |
| 527 |
* |
| 528 |
* Results: |
| 529 |
* None. |
| 530 |
* |
| 531 |
* Side effects: |
| 532 |
* None. |
| 533 |
* |
| 534 |
*---------------------------------------------------------------------- |
| 535 |
*/ |
| 536 |
|
| 537 |
void |
| 538 |
Tcl_RegExpGetInfo( |
| 539 |
Tcl_RegExp regexp, /* Pattern from which to get subexpressions. */ |
| 540 |
Tcl_RegExpInfo *infoPtr) /* Match information is stored here. */ |
| 541 |
{ |
| 542 |
TclRegexp *regexpPtr = (TclRegexp *) regexp; |
| 543 |
|
| 544 |
infoPtr->nsubs = regexpPtr->re.re_nsub; |
| 545 |
infoPtr->matches = (Tcl_RegExpIndices *) regexpPtr->matches; |
| 546 |
infoPtr->extendStart = regexpPtr->details.rm_extend.rm_so; |
| 547 |
} |
| 548 |
|
| 549 |
/* |
| 550 |
*---------------------------------------------------------------------- |
| 551 |
* |
| 552 |
* Tcl_GetRegExpFromObj -- |
| 553 |
* |
| 554 |
* Compile a regular expression into a form suitable for fast matching. |
| 555 |
* This function caches the result in a Tcl_Obj. |
| 556 |
* |
| 557 |
* Results: |
| 558 |
* The return value is a pointer to the compiled form of string, suitable |
| 559 |
* for passing to Tcl_RegExpExec. If an error occurred while compiling |
| 560 |
* the pattern, then NULL is returned and an error message is left in the |
| 561 |
* interp's result. |
| 562 |
* |
| 563 |
* Side effects: |
| 564 |
* Updates the native rep of the Tcl_Obj. |
| 565 |
* |
| 566 |
*---------------------------------------------------------------------- |
| 567 |
*/ |
| 568 |
|
| 569 |
Tcl_RegExp |
| 570 |
Tcl_GetRegExpFromObj( |
| 571 |
Tcl_Interp *interp, /* For use in error reporting, and to access |
| 572 |
* the interp regexp cache. */ |
| 573 |
Tcl_Obj *objPtr, /* Object whose string rep contains regular |
| 574 |
* expression pattern. Internal rep will be |
| 575 |
* changed to compiled form of this regular |
| 576 |
* expression. */ |
| 577 |
int flags) /* Regular expression compilation flags. */ |
| 578 |
{ |
| 579 |
int length; |
| 580 |
TclRegexp *regexpPtr; |
| 581 |
const char *pattern; |
| 582 |
|
| 583 |
/* |
| 584 |
* This is OK because we only actually interpret this value properly as a |
| 585 |
* TclRegexp* when the type is tclRegexpType. |
| 586 |
*/ |
| 587 |
|
| 588 |
regexpPtr = objPtr->internalRep.twoPtrValue.ptr1; |
| 589 |
|
| 590 |
if ((objPtr->typePtr != &tclRegexpType) || (regexpPtr->flags != flags)) { |
| 591 |
pattern = TclGetStringFromObj(objPtr, &length); |
| 592 |
|
| 593 |
regexpPtr = CompileRegexp(interp, pattern, length, flags); |
| 594 |
if (regexpPtr == NULL) { |
| 595 |
return NULL; |
| 596 |
} |
| 597 |
|
| 598 |
/* |
| 599 |
* Add a reference to the regexp so it will persist even if it is |
| 600 |
* pushed out of the current thread's regexp cache. This reference |
| 601 |
* will be removed when the object's internal rep is freed. |
| 602 |
*/ |
| 603 |
|
| 604 |
regexpPtr->refCount++; |
| 605 |
|
| 606 |
/* |
| 607 |
* Free the old representation and set our type. |
| 608 |
*/ |
| 609 |
|
| 610 |
TclFreeIntRep(objPtr); |
| 611 |
objPtr->internalRep.twoPtrValue.ptr1 = regexpPtr; |
| 612 |
objPtr->typePtr = &tclRegexpType; |
| 613 |
} |
| 614 |
return (Tcl_RegExp) regexpPtr; |
| 615 |
} |
| 616 |
|
| 617 |
/* |
| 618 |
*---------------------------------------------------------------------- |
| 619 |
* |
| 620 |
* TclRegAbout -- |
| 621 |
* |
| 622 |
* Return information about a compiled regular expression. |
| 623 |
* |
| 624 |
* Results: |
| 625 |
* The return value is -1 for failure, 0 for success, although at the |
| 626 |
* moment there's nothing that could fail. On success, a list is left in |
| 627 |
* the interp's result: first element is the subexpression count, second |
| 628 |
* is a list of re_info bit names. |
| 629 |
* |
| 630 |
* Side effects: |
| 631 |
* None. |
| 632 |
* |
| 633 |
*---------------------------------------------------------------------- |
| 634 |
*/ |
| 635 |
|
| 636 |
int |
| 637 |
TclRegAbout( |
| 638 |
Tcl_Interp *interp, /* For use in variable assignment. */ |
| 639 |
Tcl_RegExp re) /* The compiled regular expression. */ |
| 640 |
{ |
| 641 |
TclRegexp *regexpPtr = (TclRegexp *) re; |
| 642 |
struct infoname { |
| 643 |
int bit; |
| 644 |
const char *text; |
| 645 |
}; |
| 646 |
static const struct infoname infonames[] = { |
| 647 |
{REG_UBACKREF, "REG_UBACKREF"}, |
| 648 |
{REG_ULOOKAHEAD, "REG_ULOOKAHEAD"}, |
| 649 |
{REG_UBOUNDS, "REG_UBOUNDS"}, |
| 650 |
{REG_UBRACES, "REG_UBRACES"}, |
| 651 |
{REG_UBSALNUM, "REG_UBSALNUM"}, |
| 652 |
{REG_UPBOTCH, "REG_UPBOTCH"}, |
| 653 |
{REG_UBBS, "REG_UBBS"}, |
| 654 |
{REG_UNONPOSIX, "REG_UNONPOSIX"}, |
| 655 |
{REG_UUNSPEC, "REG_UUNSPEC"}, |
| 656 |
{REG_UUNPORT, "REG_UUNPORT"}, |
| 657 |
{REG_ULOCALE, "REG_ULOCALE"}, |
| 658 |
{REG_UEMPTYMATCH, "REG_UEMPTYMATCH"}, |
| 659 |
{REG_UIMPOSSIBLE, "REG_UIMPOSSIBLE"}, |
| 660 |
{REG_USHORTEST, "REG_USHORTEST"}, |
| 661 |
{0, NULL} |
| 662 |
}; |
| 663 |
const struct infoname *inf; |
| 664 |
Tcl_Obj *infoObj, *resultObj; |
| 665 |
|
| 666 |
/* |
| 667 |
* The reset here guarantees that the interpreter result is empty and |
| 668 |
* unshared. This means that we can use Tcl_ListObjAppendElement on the |
| 669 |
* result object quite safely. |
| 670 |
*/ |
| 671 |
|
| 672 |
Tcl_ResetResult(interp); |
| 673 |
|
| 674 |
/* |
| 675 |
* Assume that there will never be more than INT_MAX subexpressions. This |
| 676 |
* is a pretty reasonable assumption; the RE engine doesn't scale _that_ |
| 677 |
* well and Tcl has other limits that constrain things as well... |
| 678 |
*/ |
| 679 |
|
| 680 |
TclNewObj(resultObj); |
| 681 |
Tcl_ListObjAppendElement(NULL, resultObj, |
| 682 |
Tcl_NewIntObj((int) regexpPtr->re.re_nsub)); |
| 683 |
|
| 684 |
/* |
| 685 |
* Now append a list of all the bit-flags set for the RE. |
| 686 |
*/ |
| 687 |
|
| 688 |
TclNewObj(infoObj); |
| 689 |
for (inf=infonames ; inf->bit != 0 ; inf++) { |
| 690 |
if (regexpPtr->re.re_info & inf->bit) { |
| 691 |
Tcl_ListObjAppendElement(NULL, infoObj, |
| 692 |
Tcl_NewStringObj(inf->text, -1)); |
| 693 |
} |
| 694 |
} |
| 695 |
Tcl_ListObjAppendElement(NULL, resultObj, infoObj); |
| 696 |
Tcl_SetObjResult(interp, resultObj); |
| 697 |
|
| 698 |
return 0; |
| 699 |
} |
| 700 |
|
| 701 |
/* |
| 702 |
*---------------------------------------------------------------------- |
| 703 |
* |
| 704 |
* TclRegError -- |
| 705 |
* |
| 706 |
* Generate an error message based on the regexp status code. |
| 707 |
* |
| 708 |
* Results: |
| 709 |
* Places an error in the interpreter. |
| 710 |
* |
| 711 |
* Side effects: |
| 712 |
* Sets errorCode as well. |
| 713 |
* |
| 714 |
*---------------------------------------------------------------------- |
| 715 |
*/ |
| 716 |
|
| 717 |
void |
| 718 |
TclRegError( |
| 719 |
Tcl_Interp *interp, /* Interpreter for error reporting. */ |
| 720 |
const char *msg, /* Message to prepend to error. */ |
| 721 |
int status) /* Status code to report. */ |
| 722 |
{ |
| 723 |
char buf[100]; /* ample in practice */ |
| 724 |
char cbuf[TCL_INTEGER_SPACE]; |
| 725 |
size_t n; |
| 726 |
const char *p; |
| 727 |
|
| 728 |
Tcl_ResetResult(interp); |
| 729 |
n = TclReError(status, buf, sizeof(buf)); |
| 730 |
p = (n > sizeof(buf)) ? "..." : ""; |
| 731 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf("%s%s%s", msg, buf, p)); |
| 732 |
|
| 733 |
snprintf(cbuf, sizeof(cbuf), "%d", status); |
| 734 |
(void) TclReError(REG_ITOA, cbuf, sizeof(cbuf)); |
| 735 |
Tcl_SetErrorCode(interp, "REGEXP", cbuf, buf, (char *)NULL); |
| 736 |
} |
| 737 |
|
| 738 |
/* |
| 739 |
*---------------------------------------------------------------------- |
| 740 |
* |
| 741 |
* FreeRegexpInternalRep -- |
| 742 |
* |
| 743 |
* Deallocate the storage associated with a regexp object's internal |
| 744 |
* representation. |
| 745 |
* |
| 746 |
* Results: |
| 747 |
* None. |
| 748 |
* |
| 749 |
* Side effects: |
| 750 |
* Frees the compiled regular expression. |
| 751 |
* |
| 752 |
*---------------------------------------------------------------------- |
| 753 |
*/ |
| 754 |
|
| 755 |
static void |
| 756 |
FreeRegexpInternalRep( |
| 757 |
Tcl_Obj *objPtr) /* Regexp object with internal rep to free. */ |
| 758 |
{ |
| 759 |
TclRegexp *regexpRepPtr = objPtr->internalRep.twoPtrValue.ptr1; |
| 760 |
|
| 761 |
/* |
| 762 |
* If this is the last reference to the regexp, free it. |
| 763 |
*/ |
| 764 |
|
| 765 |
if (regexpRepPtr->refCount-- <= 1) { |
| 766 |
FreeRegexp(regexpRepPtr); |
| 767 |
} |
| 768 |
objPtr->typePtr = NULL; |
| 769 |
} |
| 770 |
|
| 771 |
/* |
| 772 |
*---------------------------------------------------------------------- |
| 773 |
* |
| 774 |
* DupRegexpInternalRep -- |
| 775 |
* |
| 776 |
* We copy the reference to the compiled regexp and bump its reference |
| 777 |
* count. |
| 778 |
* |
| 779 |
* Results: |
| 780 |
* None. |
| 781 |
* |
| 782 |
* Side effects: |
| 783 |
* Increments the reference count of the regexp. |
| 784 |
* |
| 785 |
*---------------------------------------------------------------------- |
| 786 |
*/ |
| 787 |
|
| 788 |
static void |
| 789 |
DupRegexpInternalRep( |
| 790 |
Tcl_Obj *srcPtr, /* Object with internal rep to copy. */ |
| 791 |
Tcl_Obj *copyPtr) /* Object with internal rep to set. */ |
| 792 |
{ |
| 793 |
TclRegexp *regexpPtr = srcPtr->internalRep.twoPtrValue.ptr1; |
| 794 |
|
| 795 |
regexpPtr->refCount++; |
| 796 |
copyPtr->internalRep.twoPtrValue.ptr1 = srcPtr->internalRep.twoPtrValue.ptr1; |
| 797 |
copyPtr->typePtr = &tclRegexpType; |
| 798 |
} |
| 799 |
|
| 800 |
/* |
| 801 |
*---------------------------------------------------------------------- |
| 802 |
* |
| 803 |
* SetRegexpFromAny -- |
| 804 |
* |
| 805 |
* Attempt to generate a compiled regular expression for the Tcl object |
| 806 |
* "objPtr". |
| 807 |
* |
| 808 |
* Results: |
| 809 |
* The return value is TCL_OK or TCL_ERROR. If an error occurs during |
| 810 |
* conversion, an error message is left in the interpreter's result |
| 811 |
* unless "interp" is NULL. |
| 812 |
* |
| 813 |
* Side effects: |
| 814 |
* If no error occurs, a regular expression is stored as "objPtr"s |
| 815 |
* internal representation. |
| 816 |
* |
| 817 |
*---------------------------------------------------------------------- |
| 818 |
*/ |
| 819 |
|
| 820 |
static int |
| 821 |
SetRegexpFromAny( |
| 822 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 823 |
Tcl_Obj *objPtr) /* The object to convert. */ |
| 824 |
{ |
| 825 |
if (Tcl_GetRegExpFromObj(interp, objPtr, REG_ADVANCED) == NULL) { |
| 826 |
return TCL_ERROR; |
| 827 |
} |
| 828 |
return TCL_OK; |
| 829 |
} |
| 830 |
|
| 831 |
/* |
| 832 |
*--------------------------------------------------------------------------- |
| 833 |
* |
| 834 |
* CompileRegexp -- |
| 835 |
* |
| 836 |
* Attempt to compile the given regexp pattern. If the compiled regular |
| 837 |
* expression can be found in the per-thread cache, it will be used |
| 838 |
* instead of compiling a new copy. |
| 839 |
* |
| 840 |
* Results: |
| 841 |
* The return value is a pointer to a newly allocated TclRegexp that |
| 842 |
* represents the compiled pattern, or NULL if the pattern could not be |
| 843 |
* compiled. If NULL is returned, an error message is left in the |
| 844 |
* interp's result. |
| 845 |
* |
| 846 |
* Side effects: |
| 847 |
* The thread-local regexp cache is updated and a new TclRegexp may be |
| 848 |
* allocated. |
| 849 |
* |
| 850 |
*---------------------------------------------------------------------- |
| 851 |
*/ |
| 852 |
|
| 853 |
static TclRegexp * |
| 854 |
CompileRegexp( |
| 855 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 856 |
const char *string, /* The regexp to compile (UTF-8). */ |
| 857 |
int length, /* The length of the string in bytes. */ |
| 858 |
int flags) /* Compilation flags. */ |
| 859 |
{ |
| 860 |
TclRegexp *regexpPtr; |
| 861 |
const Tcl_UniChar *uniString; |
| 862 |
int numChars, status, i, exact; |
| 863 |
Tcl_DString stringBuf; |
| 864 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 865 |
|
| 866 |
if (!tsdPtr->initialized) { |
| 867 |
tsdPtr->initialized = 1; |
| 868 |
Tcl_CreateThreadExitHandler(FinalizeRegexp, NULL); |
| 869 |
} |
| 870 |
|
| 871 |
/* |
| 872 |
* This routine maintains a second-level regular expression cache in |
| 873 |
* addition to the per-object regexp cache. The per-thread cache is needed |
| 874 |
* to handle the case where for various reasons the object is lost between |
| 875 |
* invocations of the regexp command, but the literal pattern is the same. |
| 876 |
*/ |
| 877 |
|
| 878 |
/* |
| 879 |
* Check the per-thread compiled regexp cache. We can only reuse a regexp |
| 880 |
* if it has the same pattern and the same flags. |
| 881 |
*/ |
| 882 |
|
| 883 |
for (i = 0; (i < NUM_REGEXPS) && (tsdPtr->patterns[i] != NULL); i++) { |
| 884 |
if ((length == tsdPtr->patLengths[i]) |
| 885 |
&& (tsdPtr->regexps[i]->flags == flags) |
| 886 |
&& (strcmp(string, tsdPtr->patterns[i]) == 0)) { |
| 887 |
/* |
| 888 |
* Move the matched pattern to the first slot in the cache and |
| 889 |
* shift the other patterns down one position. |
| 890 |
*/ |
| 891 |
|
| 892 |
if (i != 0) { |
| 893 |
int j; |
| 894 |
char *cachedString; |
| 895 |
|
| 896 |
cachedString = tsdPtr->patterns[i]; |
| 897 |
regexpPtr = tsdPtr->regexps[i]; |
| 898 |
for (j = i-1; j >= 0; j--) { |
| 899 |
tsdPtr->patterns[j+1] = tsdPtr->patterns[j]; |
| 900 |
tsdPtr->patLengths[j+1] = tsdPtr->patLengths[j]; |
| 901 |
tsdPtr->regexps[j+1] = tsdPtr->regexps[j]; |
| 902 |
} |
| 903 |
tsdPtr->patterns[0] = cachedString; |
| 904 |
tsdPtr->patLengths[0] = length; |
| 905 |
tsdPtr->regexps[0] = regexpPtr; |
| 906 |
} |
| 907 |
return tsdPtr->regexps[0]; |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
/* |
| 912 |
* This is a new expression, so compile it and add it to the cache. |
| 913 |
*/ |
| 914 |
|
| 915 |
regexpPtr = (TclRegexp*)ckalloc(sizeof(TclRegexp)); |
| 916 |
regexpPtr->objPtr = NULL; |
| 917 |
regexpPtr->string = NULL; |
| 918 |
regexpPtr->details.rm_extend.rm_so = -1; |
| 919 |
regexpPtr->details.rm_extend.rm_eo = -1; |
| 920 |
|
| 921 |
/* |
| 922 |
* Get the up-to-date string representation and map to unicode. |
| 923 |
*/ |
| 924 |
|
| 925 |
Tcl_DStringInit(&stringBuf); |
| 926 |
uniString = Tcl_UtfToUniCharDString(string, length, &stringBuf); |
| 927 |
numChars = Tcl_DStringLength(&stringBuf) / sizeof(Tcl_UniChar); |
| 928 |
|
| 929 |
/* |
| 930 |
* Compile the string and check for errors. |
| 931 |
*/ |
| 932 |
|
| 933 |
regexpPtr->flags = flags; |
| 934 |
status = TclReComp(®expPtr->re, uniString, (size_t) numChars, flags); |
| 935 |
Tcl_DStringFree(&stringBuf); |
| 936 |
|
| 937 |
if (status != REG_OKAY) { |
| 938 |
/* |
| 939 |
* Clean up and report errors in the interpreter, if possible. |
| 940 |
*/ |
| 941 |
|
| 942 |
ckfree(regexpPtr); |
| 943 |
if (interp) { |
| 944 |
TclRegError(interp, |
| 945 |
"couldn't compile regular expression pattern: ", status); |
| 946 |
} |
| 947 |
return NULL; |
| 948 |
} |
| 949 |
|
| 950 |
/* |
| 951 |
* Convert RE to a glob pattern equivalent, if any, and cache it. If this |
| 952 |
* is not possible, then globObjPtr will be NULL. This is used by |
| 953 |
* Tcl_RegExpExecObj to optionally do a fast match (avoids RE engine). |
| 954 |
*/ |
| 955 |
|
| 956 |
if (TclReToGlob(NULL, string, length, &stringBuf, &exact, |
| 957 |
NULL) == TCL_OK) { |
| 958 |
regexpPtr->globObjPtr = TclDStringToObj(&stringBuf); |
| 959 |
Tcl_IncrRefCount(regexpPtr->globObjPtr); |
| 960 |
} else { |
| 961 |
regexpPtr->globObjPtr = NULL; |
| 962 |
} |
| 963 |
|
| 964 |
/* |
| 965 |
* Allocate enough space for all of the subexpressions, plus one extra for |
| 966 |
* the entire pattern. |
| 967 |
*/ |
| 968 |
|
| 969 |
regexpPtr->matches = |
| 970 |
(regmatch_t*)ckalloc(sizeof(regmatch_t) * (regexpPtr->re.re_nsub + 1)); |
| 971 |
|
| 972 |
/* |
| 973 |
* Initialize the refcount to one initially, since it is in the cache. |
| 974 |
*/ |
| 975 |
|
| 976 |
regexpPtr->refCount = 1; |
| 977 |
|
| 978 |
/* |
| 979 |
* Free the last regexp, if necessary, and make room at the head of the |
| 980 |
* list for the new regexp. |
| 981 |
*/ |
| 982 |
|
| 983 |
if (tsdPtr->patterns[NUM_REGEXPS-1] != NULL) { |
| 984 |
TclRegexp *oldRegexpPtr = tsdPtr->regexps[NUM_REGEXPS-1]; |
| 985 |
|
| 986 |
if (oldRegexpPtr->refCount-- <= 1) { |
| 987 |
FreeRegexp(oldRegexpPtr); |
| 988 |
} |
| 989 |
ckfree(tsdPtr->patterns[NUM_REGEXPS-1]); |
| 990 |
} |
| 991 |
for (i = NUM_REGEXPS - 2; i >= 0; i--) { |
| 992 |
tsdPtr->patterns[i+1] = tsdPtr->patterns[i]; |
| 993 |
tsdPtr->patLengths[i+1] = tsdPtr->patLengths[i]; |
| 994 |
tsdPtr->regexps[i+1] = tsdPtr->regexps[i]; |
| 995 |
} |
| 996 |
tsdPtr->patterns[0] = (char *)ckalloc(length + 1); |
| 997 |
memcpy(tsdPtr->patterns[0], string, length + 1); |
| 998 |
tsdPtr->patLengths[0] = length; |
| 999 |
tsdPtr->regexps[0] = regexpPtr; |
| 1000 |
|
| 1001 |
return regexpPtr; |
| 1002 |
} |
| 1003 |
|
| 1004 |
/* |
| 1005 |
*---------------------------------------------------------------------- |
| 1006 |
* |
| 1007 |
* FreeRegexp -- |
| 1008 |
* |
| 1009 |
* Release the storage associated with a TclRegexp. |
| 1010 |
* |
| 1011 |
* Results: |
| 1012 |
* None. |
| 1013 |
* |
| 1014 |
* Side effects: |
| 1015 |
* None. |
| 1016 |
* |
| 1017 |
*---------------------------------------------------------------------- |
| 1018 |
*/ |
| 1019 |
|
| 1020 |
static void |
| 1021 |
FreeRegexp( |
| 1022 |
TclRegexp *regexpPtr) /* Compiled regular expression to free. */ |
| 1023 |
{ |
| 1024 |
TclReFree(®expPtr->re); |
| 1025 |
if (regexpPtr->globObjPtr) { |
| 1026 |
TclDecrRefCount(regexpPtr->globObjPtr); |
| 1027 |
} |
| 1028 |
if (regexpPtr->matches) { |
| 1029 |
ckfree(regexpPtr->matches); |
| 1030 |
} |
| 1031 |
ckfree(regexpPtr); |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* |
| 1035 |
*---------------------------------------------------------------------- |
| 1036 |
* |
| 1037 |
* FinalizeRegexp -- |
| 1038 |
* |
| 1039 |
* Release the storage associated with the per-thread regexp cache. |
| 1040 |
* |
| 1041 |
* Results: |
| 1042 |
* None. |
| 1043 |
* |
| 1044 |
* Side effects: |
| 1045 |
* None. |
| 1046 |
* |
| 1047 |
*---------------------------------------------------------------------- |
| 1048 |
*/ |
| 1049 |
|
| 1050 |
static void |
| 1051 |
FinalizeRegexp( |
| 1052 |
ClientData clientData) /* Not used. */ |
| 1053 |
{ |
| 1054 |
int i; |
| 1055 |
TclRegexp *regexpPtr; |
| 1056 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1057 |
|
| 1058 |
for (i = 0; (i < NUM_REGEXPS) && (tsdPtr->patterns[i] != NULL); i++) { |
| 1059 |
regexpPtr = tsdPtr->regexps[i]; |
| 1060 |
if (regexpPtr->refCount-- <= 1) { |
| 1061 |
FreeRegexp(regexpPtr); |
| 1062 |
} |
| 1063 |
ckfree(tsdPtr->patterns[i]); |
| 1064 |
tsdPtr->patterns[i] = NULL; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* |
| 1068 |
* We may find ourselves reinitialized if another finalization routine |
| 1069 |
* invokes regexps. |
| 1070 |
*/ |
| 1071 |
|
| 1072 |
tsdPtr->initialized = 0; |
| 1073 |
} |
| 1074 |
|
| 1075 |
/* |
| 1076 |
* Local Variables: |
| 1077 |
* mode: c |
| 1078 |
* c-basic-offset: 4 |
| 1079 |
* fill-column: 78 |
| 1080 |
* End: |
| 1081 |
*/ |