| 1 | /* |
| 2 | * tclUtil.c -- |
| 3 | * |
| 4 | * This file contains utility procedures that are used by many Tcl |
| 5 | * commands. |
| 6 | * |
| 7 | * Copyright (c) 1987-1993 The Regents of the University of California. |
| 8 | * Copyright (c) 1994-1998 Sun Microsystems, Inc. |
| 9 | * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved. |
| 10 | * |
| 11 | * See the file "license.terms" for information on usage and redistribution |
| 12 | * of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 | * |
| 14 | * RCS: @(#) $Id: tclUtil.c,v 1.36.2.4 2003/08/27 21:31:53 dgp Exp $ |
| 15 | */ |
| 16 | |
| 17 | #include "tkPort.h" |
| 18 | #include "tkInt.h" |
| 19 | |
| 20 | /* |
| 21 | * The following variable holds the full path name of the binary |
| 22 | * from which this application was executed, or NULL if it isn't |
| 23 | * know. The value of the variable is set by the procedure |
| 24 | * Tcl_FindExecutable. The storage space is dynamically allocated. |
| 25 | */ |
| 26 | |
| 27 | char *tclExecutableName = NULL; |
| 28 | char *tclNativeExecutableName = NULL; |
| 29 | |
| 30 | /* |
| 31 | * The following values are used in the flags returned by Tcl_ScanElement |
| 32 | * and used by Tcl_ConvertElement. The value TCL_DONT_USE_BRACES is also |
| 33 | * defined in tcl.h; make sure its value doesn't overlap with any of the |
| 34 | * values below. |
| 35 | * |
| 36 | * TCL_DONT_USE_BRACES - 1 means the string mustn't be enclosed in |
| 37 | * braces (e.g. it contains unmatched braces, |
| 38 | * or ends in a backslash character, or user |
| 39 | * just doesn't want braces); handle all |
| 40 | * special characters by adding backslashes. |
| 41 | * USE_BRACES - 1 means the string contains a special |
| 42 | * character that can be handled simply by |
| 43 | * enclosing the entire argument in braces. |
| 44 | * BRACES_UNMATCHED - 1 means that braces aren't properly matched |
| 45 | * in the argument. |
| 46 | */ |
| 47 | |
| 48 | #define USE_BRACES 2 |
| 49 | #define BRACES_UNMATCHED 4 |
| 50 | |
| 51 | /* |
| 52 | * The following values determine the precision used when converting |
| 53 | * floating-point values to strings. This information is linked to all |
| 54 | * of the tcl_precision variables in all interpreters via the procedure |
| 55 | * TclPrecTraceProc. |
| 56 | */ |
| 57 | |
| 58 | static char precisionString[10] = "12"; |
| 59 | /* The string value of all the tcl_precision |
| 60 | * variables. */ |
| 61 | static char precisionFormat[10] = "%.12g"; |
| 62 | /* The format string actually used in calls |
| 63 | * to sprintf. */ |
| 64 | TCL_DECLARE_MUTEX(precisionMutex) |
| 65 | |
| 66 | /* |
| 67 | * Prototypes for procedures defined later in this file. |
| 68 | */ |
| 69 | |
| 70 | static void UpdateStringOfEndOffset _ANSI_ARGS_((Tcl_Obj* objPtr)); |
| 71 | static int SetEndOffsetFromAny _ANSI_ARGS_((Tcl_Interp* interp, |
| 72 | Tcl_Obj* objPtr)); |
| 73 | |
| 74 | /* |
| 75 | * The following is the Tcl object type definition for an object |
| 76 | * that represents a list index in the form, "end-offset". It is |
| 77 | * used as a performance optimization in TclGetIntForIndex. The |
| 78 | * internal rep is an integer, so no memory management is required |
| 79 | * for it. |
| 80 | */ |
| 81 | |
| 82 | Tcl_ObjType tclEndOffsetType = { |
| 83 | "end-offset", /* name */ |
| 84 | (Tcl_FreeInternalRepProc*) NULL, /* freeIntRepProc */ |
| 85 | (Tcl_DupInternalRepProc*) NULL, /* dupIntRepProc */ |
| 86 | UpdateStringOfEndOffset, /* updateStringProc */ |
| 87 | SetEndOffsetFromAny |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | *---------------------------------------------------------------------- |
| 93 | * |
| 94 | * TclFindElement -- |
| 95 | * |
| 96 | * Given a pointer into a Tcl list, locate the first (or next) |
| 97 | * element in the list. |
| 98 | * |
| 99 | * Results: |
| 100 | * The return value is normally TCL_OK, which means that the |
| 101 | * element was successfully located. If TCL_ERROR is returned |
| 102 | * it means that list didn't have proper list structure; |
| 103 | * the interp's result contains a more detailed error message. |
| 104 | * |
| 105 | * If TCL_OK is returned, then *elementPtr will be set to point to the |
| 106 | * first element of list, and *nextPtr will be set to point to the |
| 107 | * character just after any white space following the last character |
| 108 | * that's part of the element. If this is the last argument in the |
| 109 | * list, then *nextPtr will point just after the last character in the |
| 110 | * list (i.e., at the character at list+listLength). If sizePtr is |
| 111 | * non-NULL, *sizePtr is filled in with the number of characters in the |
| 112 | * element. If the element is in braces, then *elementPtr will point |
| 113 | * to the character after the opening brace and *sizePtr will not |
| 114 | * include either of the braces. If there isn't an element in the list, |
| 115 | * *sizePtr will be zero, and both *elementPtr and *termPtr will point |
| 116 | * just after the last character in the list. Note: this procedure does |
| 117 | * NOT collapse backslash sequences. |
| 118 | * |
| 119 | * Side effects: |
| 120 | * None. |
| 121 | * |
| 122 | *---------------------------------------------------------------------- |
| 123 | */ |
| 124 | |
| 125 | int |
| 126 | TclFindElement(interp, list, listLength, elementPtr, nextPtr, sizePtr, |
| 127 | bracePtr) |
| 128 | Tcl_Interp *interp; /* Interpreter to use for error reporting. |
| 129 | * If NULL, then no error message is left |
| 130 | * after errors. */ |
| 131 | CONST char *list; /* Points to the first byte of a string |
| 132 | * containing a Tcl list with zero or more |
| 133 | * elements (possibly in braces). */ |
| 134 | int listLength; /* Number of bytes in the list's string. */ |
| 135 | CONST char **elementPtr; /* Where to put address of first significant |
| 136 | * character in first element of list. */ |
| 137 | CONST char **nextPtr; /* Fill in with location of character just |
| 138 | * after all white space following end of |
| 139 | * argument (next arg or end of list). */ |
| 140 | int *sizePtr; /* If non-zero, fill in with size of |
| 141 | * element. */ |
| 142 | int *bracePtr; /* If non-zero, fill in with non-zero/zero |
| 143 | * to indicate that arg was/wasn't |
| 144 | * in braces. */ |
| 145 | { |
| 146 | CONST char *p = list; |
| 147 | CONST char *elemStart; /* Points to first byte of first element. */ |
| 148 | CONST char *limit; /* Points just after list's last byte. */ |
| 149 | int openBraces = 0; /* Brace nesting level during parse. */ |
| 150 | int inQuotes = 0; |
| 151 | int size = 0; /* lint. */ |
| 152 | int numChars; |
| 153 | CONST char *p2; |
| 154 | |
| 155 | /* |
| 156 | * Skim off leading white space and check for an opening brace or |
| 157 | * quote. We treat embedded NULLs in the list as bytes belonging to |
| 158 | * a list element. |
| 159 | */ |
| 160 | |
| 161 | limit = (list + listLength); |
| 162 | while ((p < limit) && (isspace(UCHAR(*p)))) { /* INTL: ISO space. */ |
| 163 | p++; |
| 164 | } |
| 165 | if (p == limit) { /* no element found */ |
| 166 | elemStart = limit; |
| 167 | goto done; |
| 168 | } |
| 169 | |
| 170 | if (*p == '{') { |
| 171 | openBraces = 1; |
| 172 | p++; |
| 173 | } else if (*p == '"') { |
| 174 | inQuotes = 1; |
| 175 | p++; |
| 176 | } |
| 177 | elemStart = p; |
| 178 | if (bracePtr != 0) { |
| 179 | *bracePtr = openBraces; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Find element's end (a space, close brace, or the end of the string). |
| 184 | */ |
| 185 | |
| 186 | while (p < limit) { |
| 187 | switch (*p) { |
| 188 | |
| 189 | /* |
| 190 | * Open brace: don't treat specially unless the element is in |
| 191 | * braces. In this case, keep a nesting count. |
| 192 | */ |
| 193 | |
| 194 | case '{': |
| 195 | if (openBraces != 0) { |
| 196 | openBraces++; |
| 197 | } |
| 198 | break; |
| 199 | |
| 200 | /* |
| 201 | * Close brace: if element is in braces, keep nesting count and |
| 202 | * quit when the last close brace is seen. |
| 203 | */ |
| 204 | |
| 205 | case '}': |
| 206 | if (openBraces > 1) { |
| 207 | openBraces--; |
| 208 | } else if (openBraces == 1) { |
| 209 | size = (p - elemStart); |
| 210 | p++; |
| 211 | if ((p >= limit) |
| 212 | || isspace(UCHAR(*p))) { /* INTL: ISO space. */ |
| 213 | goto done; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Garbage after the closing brace; return an error. |
| 218 | */ |
| 219 | |
| 220 | if (interp != NULL) { |
| 221 | char buf[100]; |
| 222 | |
| 223 | p2 = p; |
| 224 | while ((p2 < limit) |
| 225 | && (!isspace(UCHAR(*p2))) /* INTL: ISO space. */ |
| 226 | && (p2 < p+20)) { |
| 227 | p2++; |
| 228 | } |
| 229 | sprintf(buf, |
| 230 | "list element in braces followed by \"%.*s\" instead of space", |
| 231 | (int) (p2-p), p); |
| 232 | Tcl_SetResult(interp, buf, TCL_VOLATILE); |
| 233 | } |
| 234 | return TCL_ERROR; |
| 235 | } |
| 236 | break; |
| 237 | |
| 238 | /* |
| 239 | * Backslash: skip over everything up to the end of the |
| 240 | * backslash sequence. |
| 241 | */ |
| 242 | |
| 243 | case '\\': { |
| 244 | Tcl_UtfBackslash(p, &numChars, NULL); |
| 245 | p += (numChars - 1); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Space: ignore if element is in braces or quotes; otherwise |
| 251 | * terminate element. |
| 252 | */ |
| 253 | |
| 254 | case ' ': |
| 255 | case '\f': |
| 256 | case '\n': |
| 257 | case '\r': |
| 258 | case '\t': |
| 259 | case '\v': |
| 260 | if ((openBraces == 0) && !inQuotes) { |
| 261 | size = (p - elemStart); |
| 262 | goto done; |
| 263 | } |
| 264 | break; |
| 265 | |
| 266 | /* |
| 267 | * Double-quote: if element is in quotes then terminate it. |
| 268 | */ |
| 269 | |
| 270 | case '"': |
| 271 | if (inQuotes) { |
| 272 | size = (p - elemStart); |
| 273 | p++; |
| 274 | if ((p >= limit) |
| 275 | || isspace(UCHAR(*p))) { /* INTL: ISO space */ |
| 276 | goto done; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Garbage after the closing quote; return an error. |
| 281 | */ |
| 282 | |
| 283 | if (interp != NULL) { |
| 284 | char buf[100]; |
| 285 | |
| 286 | p2 = p; |
| 287 | while ((p2 < limit) |
| 288 | && (!isspace(UCHAR(*p2))) /* INTL: ISO space */ |
| 289 | && (p2 < p+20)) { |
| 290 | p2++; |
| 291 | } |
| 292 | sprintf(buf, |
| 293 | "list element in quotes followed by \"%.*s\" %s", |
| 294 | (int) (p2-p), p, "instead of space"); |
| 295 | Tcl_SetResult(interp, buf, TCL_VOLATILE); |
| 296 | } |
| 297 | return TCL_ERROR; |
| 298 | } |
| 299 | break; |
| 300 | } |
| 301 | p++; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | /* |
| 306 | * End of list: terminate element. |
| 307 | */ |
| 308 | |
| 309 | if (p == limit) { |
| 310 | if (openBraces != 0) { |
| 311 | if (interp != NULL) { |
| 312 | Tcl_SetResult(interp, "unmatched open brace in list", |
| 313 | TCL_STATIC); |
| 314 | } |
| 315 | return TCL_ERROR; |
| 316 | } else if (inQuotes) { |
| 317 | if (interp != NULL) { |
| 318 | Tcl_SetResult(interp, "unmatched open quote in list", |
| 319 | TCL_STATIC); |
| 320 | } |
| 321 | return TCL_ERROR; |
| 322 | } |
| 323 | size = (p - elemStart); |
| 324 | } |
| 325 | |
| 326 | done: |
| 327 | while ((p < limit) && (isspace(UCHAR(*p)))) { /* INTL: ISO space. */ |
| 328 | p++; |
| 329 | } |
| 330 | *elementPtr = elemStart; |
| 331 | *nextPtr = p; |
| 332 | if (sizePtr != 0) { |
| 333 | *sizePtr = size; |
| 334 | } |
| 335 | return TCL_OK; |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | *---------------------------------------------------------------------- |
| 340 | * |
| 341 | * TclCopyAndCollapse -- |
| 342 | * |
| 343 | * Copy a string and eliminate any backslashes that aren't in braces. |
| 344 | * |
| 345 | * Results: |
| 346 | * Count characters get copied from src to dst. Along the way, if |
| 347 | * backslash sequences are found outside braces, the backslashes are |
| 348 | * eliminated in the copy. After scanning count chars from source, a |
| 349 | * null character is placed at the end of dst. Returns the number |
| 350 | * of characters that got copied. |
| 351 | * |
| 352 | * Side effects: |
| 353 | * None. |
| 354 | * |
| 355 | *---------------------------------------------------------------------- |
| 356 | */ |
| 357 | |
| 358 | int |
| 359 | TclCopyAndCollapse(count, src, dst) |
| 360 | int count; /* Number of characters to copy from src. */ |
| 361 | CONST char *src; /* Copy from here... */ |
| 362 | char *dst; /* ... to here. */ |
| 363 | { |
| 364 | register char c; |
| 365 | int numRead; |
| 366 | int newCount = 0; |
| 367 | int backslashCount; |
| 368 | |
| 369 | for (c = *src; count > 0; src++, c = *src, count--) { |
| 370 | if (c == '\\') { |
| 371 | backslashCount = Tcl_UtfBackslash(src, &numRead, dst); |
| 372 | dst += backslashCount; |
| 373 | newCount += backslashCount; |
| 374 | src += numRead-1; |
| 375 | count -= numRead-1; |
| 376 | } else { |
| 377 | *dst = c; |
| 378 | dst++; |
| 379 | newCount++; |
| 380 | } |
| 381 | } |
| 382 | *dst = 0; |
| 383 | return newCount; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | *---------------------------------------------------------------------- |
| 388 | * |
| 389 | * Tcl_SplitList -- |
| 390 | * |
| 391 | * Splits a list up into its constituent fields. |
| 392 | * |
| 393 | * Results |
| 394 | * The return value is normally TCL_OK, which means that |
| 395 | * the list was successfully split up. If TCL_ERROR is |
| 396 | * returned, it means that "list" didn't have proper list |
| 397 | * structure; the interp's result will contain a more detailed |
| 398 | * error message. |
| 399 | * |
| 400 | * *argvPtr will be filled in with the address of an array |
| 401 | * whose elements point to the elements of list, in order. |
| 402 | * *argcPtr will get filled in with the number of valid elements |
| 403 | * in the array. A single block of memory is dynamically allocated |
| 404 | * to hold both the argv array and a copy of the list (with |
| 405 | * backslashes and braces removed in the standard way). |
| 406 | * The caller must eventually free this memory by calling free() |
| 407 | * on *argvPtr. Note: *argvPtr and *argcPtr are only modified |
| 408 | * if the procedure returns normally. |
| 409 | * |
| 410 | * Side effects: |
| 411 | * Memory is allocated. |
| 412 | * |
| 413 | *---------------------------------------------------------------------- |
| 414 | */ |
| 415 | |
| 416 | int |
| 417 | Tcl_SplitList(interp, list, argcPtr, argvPtr) |
| 418 | Tcl_Interp *interp; /* Interpreter to use for error reporting. |
| 419 | * If NULL, no error message is left. */ |
| 420 | CONST char *list; /* Pointer to string with list structure. */ |
| 421 | int *argcPtr; /* Pointer to location to fill in with |
| 422 | * the number of elements in the list. */ |
| 423 | CONST char ***argvPtr; /* Pointer to place to store pointer to |
| 424 | * array of pointers to list elements. */ |
| 425 | { |
| 426 | CONST char **argv; |
| 427 | CONST char *l; |
| 428 | char *p; |
| 429 | int length, size, i, result, elSize, brace; |
| 430 | CONST char *element; |
| 431 | |
| 432 | /* |
| 433 | * Figure out how much space to allocate. There must be enough |
| 434 | * space for both the array of pointers and also for a copy of |
| 435 | * the list. To estimate the number of pointers needed, count |
| 436 | * the number of space characters in the list. |
| 437 | */ |
| 438 | |
| 439 | for (size = 1, l = list; *l != 0; l++) { |
| 440 | if (isspace(UCHAR(*l))) { /* INTL: ISO space. */ |
| 441 | size++; |
| 442 | } |
| 443 | } |
| 444 | size++; /* Leave space for final NULL pointer. */ |
| 445 | argv = (CONST char **) ckalloc((unsigned) |
| 446 | ((size * sizeof(char *)) + (l - list) + 1)); |
| 447 | length = strlen(list); |
| 448 | for (i = 0, p = ((char *) argv) + size*sizeof(char *); |
| 449 | *list != 0; i++) { |
| 450 | CONST char *prevList = list; |
| 451 | |
| 452 | result = TclFindElement(interp, list, length, &element, |
| 453 | &list, &elSize, &brace); |
| 454 | length -= (list - prevList); |
| 455 | if (result != TCL_OK) { |
| 456 | ckfree((char *) argv); |
| 457 | return result; |
| 458 | } |
| 459 | if (*element == 0) { |
| 460 | break; |
| 461 | } |
| 462 | if (i >= size) { |
| 463 | ckfree((char *) argv); |
| 464 | if (interp != NULL) { |
| 465 | Tcl_SetResult(interp, "internal error in Tcl_SplitList", |
| 466 | TCL_STATIC); |
| 467 | } |
| 468 | return TCL_ERROR; |
| 469 | } |
| 470 | argv[i] = p; |
| 471 | if (brace) { |
| 472 | memcpy((VOID *) p, (VOID *) element, (size_t) elSize); |
| 473 | p += elSize; |
| 474 | *p = 0; |
| 475 | p++; |
| 476 | } else { |
| 477 | TclCopyAndCollapse(elSize, element, p); |
| 478 | p += elSize+1; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | argv[i] = NULL; |
| 483 | *argvPtr = argv; |
| 484 | *argcPtr = i; |
| 485 | return TCL_OK; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | *---------------------------------------------------------------------- |
| 490 | * |
| 491 | * Tcl_ScanElement -- |
| 492 | * |
| 493 | * This procedure is a companion procedure to Tcl_ConvertElement. |
| 494 | * It scans a string to see what needs to be done to it (e.g. add |
| 495 | * backslashes or enclosing braces) to make the string into a |
| 496 | * valid Tcl list element. |
| 497 | * |
| 498 | * Results: |
| 499 | * The return value is an overestimate of the number of characters |
| 500 | * that will be needed by Tcl_ConvertElement to produce a valid |
| 501 | * list element from string. The word at *flagPtr is filled in |
| 502 | * with a value needed by Tcl_ConvertElement when doing the actual |
| 503 | * conversion. |
| 504 | * |
| 505 | * Side effects: |
| 506 | * None. |
| 507 | * |
| 508 | *---------------------------------------------------------------------- |
| 509 | */ |
| 510 | |
| 511 | int |
| 512 | Tcl_ScanElement(string, flagPtr) |
| 513 | register CONST char *string; /* String to convert to list element. */ |
| 514 | register int *flagPtr; /* Where to store information to guide |
| 515 | * Tcl_ConvertCountedElement. */ |
| 516 | { |
| 517 | return Tcl_ScanCountedElement(string, -1, flagPtr); |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | *---------------------------------------------------------------------- |
| 522 | * |
| 523 | * Tcl_ScanCountedElement -- |
| 524 | * |
| 525 | * This procedure is a companion procedure to |
| 526 | * Tcl_ConvertCountedElement. It scans a string to see what |
| 527 | * needs to be done to it (e.g. add backslashes or enclosing |
| 528 | * braces) to make the string into a valid Tcl list element. |
| 529 | * If length is -1, then the string is scanned up to the first |
| 530 | * null byte. |
| 531 | * |
| 532 | * Results: |
| 533 | * The return value is an overestimate of the number of characters |
| 534 | * that will be needed by Tcl_ConvertCountedElement to produce a |
| 535 | * valid list element from string. The word at *flagPtr is |
| 536 | * filled in with a value needed by Tcl_ConvertCountedElement |
| 537 | * when doing the actual conversion. |
| 538 | * |
| 539 | * Side effects: |
| 540 | * None. |
| 541 | * |
| 542 | *---------------------------------------------------------------------- |
| 543 | */ |
| 544 | |
| 545 | int |
| 546 | Tcl_ScanCountedElement(string, length, flagPtr) |
| 547 | CONST char *string; /* String to convert to Tcl list element. */ |
| 548 | int length; /* Number of bytes in string, or -1. */ |
| 549 | int *flagPtr; /* Where to store information to guide |
| 550 | * Tcl_ConvertElement. */ |
| 551 | { |
| 552 | int flags, nestingLevel; |
| 553 | register CONST char *p, *lastChar; |
| 554 | |
| 555 | /* |
| 556 | * This procedure and Tcl_ConvertElement together do two things: |
| 557 | * |
| 558 | * 1. They produce a proper list, one that will yield back the |
| 559 | * argument strings when evaluated or when disassembled with |
| 560 | * Tcl_SplitList. This is the most important thing. |
| 561 | * |
| 562 | * 2. They try to produce legible output, which means minimizing the |
| 563 | * use of backslashes (using braces instead). However, there are |
| 564 | * some situations where backslashes must be used (e.g. an element |
| 565 | * like "{abc": the leading brace will have to be backslashed. |
| 566 | * For each element, one of three things must be done: |
| 567 | * |
| 568 | * (a) Use the element as-is (it doesn't contain any special |
| 569 | * characters). This is the most desirable option. |
| 570 | * |
| 571 | * (b) Enclose the element in braces, but leave the contents alone. |
| 572 | * This happens if the element contains embedded space, or if it |
| 573 | * contains characters with special interpretation ($, [, ;, or \), |
| 574 | * or if it starts with a brace or double-quote, or if there are |
| 575 | * no characters in the element. |
| 576 | * |
| 577 | * (c) Don't enclose the element in braces, but add backslashes to |
| 578 | * prevent special interpretation of special characters. This is a |
| 579 | * last resort used when the argument would normally fall under case |
| 580 | * (b) but contains unmatched braces. It also occurs if the last |
| 581 | * character of the argument is a backslash or if the element contains |
| 582 | * a backslash followed by newline. |
| 583 | * |
| 584 | * The procedure figures out how many bytes will be needed to store |
| 585 | * the result (actually, it overestimates). It also collects information |
| 586 | * about the element in the form of a flags word. |
| 587 | * |
| 588 | * Note: list elements produced by this procedure and |
| 589 | * Tcl_ConvertCountedElement must have the property that they can be |
| 590 | * enclosing in curly braces to make sub-lists. This means, for |
| 591 | * example, that we must not leave unmatched curly braces in the |
| 592 | * resulting list element. This property is necessary in order for |
| 593 | * procedures like Tcl_DStringStartSublist to work. |
| 594 | */ |
| 595 | |
| 596 | nestingLevel = 0; |
| 597 | flags = 0; |
| 598 | if (string == NULL) { |
| 599 | string = ""; |
| 600 | } |
| 601 | if (length == -1) { |
| 602 | length = strlen(string); |
| 603 | } |
| 604 | lastChar = string + length; |
| 605 | p = string; |
| 606 | if ((p == lastChar) || (*p == '{') || (*p == '"')) { |
| 607 | flags |= USE_BRACES; |
| 608 | } |
| 609 | for ( ; p < lastChar; p++) { |
| 610 | switch (*p) { |
| 611 | case '{': |
| 612 | nestingLevel++; |
| 613 | break; |
| 614 | case '}': |
| 615 | nestingLevel--; |
| 616 | if (nestingLevel < 0) { |
| 617 | flags |= TCL_DONT_USE_BRACES|BRACES_UNMATCHED; |
| 618 | } |
| 619 | break; |
| 620 | case '[': |
| 621 | case '$': |
| 622 | case ';': |
| 623 | case ' ': |
| 624 | case '\f': |
| 625 | case '\n': |
| 626 | case '\r': |
| 627 | case '\t': |
| 628 | case '\v': |
| 629 | flags |= USE_BRACES; |
| 630 | break; |
| 631 | case '\\': |
| 632 | if ((p+1 == lastChar) || (p[1] == '\n')) { |
| 633 | flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED; |
| 634 | } else { |
| 635 | int size; |
| 636 | |
| 637 | Tcl_UtfBackslash(p, &size, NULL); |
| 638 | p += size-1; |
| 639 | flags |= USE_BRACES; |
| 640 | } |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | if (nestingLevel != 0) { |
| 645 | flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED; |
| 646 | } |
| 647 | *flagPtr = flags; |
| 648 | |
| 649 | /* |
| 650 | * Allow enough space to backslash every character plus leave |
| 651 | * two spaces for braces. |
| 652 | */ |
| 653 | |
| 654 | return 2*(p-string) + 2; |
| 655 | } |
| 656 | |
| 657 | /* |
| 658 | *---------------------------------------------------------------------- |
| 659 | * |
| 660 | * Tcl_ConvertElement -- |
| 661 | * |
| 662 | * This is a companion procedure to Tcl_ScanElement. Given |
| 663 | * the information produced by Tcl_ScanElement, this procedure |
| 664 | * converts a string to a list element equal to that string. |
| 665 | * |
| 666 | * Results: |
| 667 | * Information is copied to *dst in the form of a list element |
| 668 | * identical to src (i.e. if Tcl_SplitList is applied to dst it |
| 669 | * will produce a string identical to src). The return value is |
| 670 | * a count of the number of characters copied (not including the |
| 671 | * terminating NULL character). |
| 672 | * |
| 673 | * Side effects: |
| 674 | * None. |
| 675 | * |
| 676 | *---------------------------------------------------------------------- |
| 677 | */ |
| 678 | |
| 679 | int |
| 680 | Tcl_ConvertElement(src, dst, flags) |
| 681 | register CONST char *src; /* Source information for list element. */ |
| 682 | register char *dst; /* Place to put list-ified element. */ |
| 683 | register int flags; /* Flags produced by Tcl_ScanElement. */ |
| 684 | { |
| 685 | return Tcl_ConvertCountedElement(src, -1, dst, flags); |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | *---------------------------------------------------------------------- |
| 690 | * |
| 691 | * Tcl_ConvertCountedElement -- |
| 692 | * |
| 693 | * This is a companion procedure to Tcl_ScanCountedElement. Given |
| 694 | * the information produced by Tcl_ScanCountedElement, this |
| 695 | * procedure converts a string to a list element equal to that |
| 696 | * string. |
| 697 | * |
| 698 | * Results: |
| 699 | * Information is copied to *dst in the form of a list element |
| 700 | * identical to src (i.e. if Tcl_SplitList is applied to dst it |
| 701 | * will produce a string identical to src). The return value is |
| 702 | * a count of the number of characters copied (not including the |
| 703 | * terminating NULL character). |
| 704 | * |
| 705 | * Side effects: |
| 706 | * None. |
| 707 | * |
| 708 | *---------------------------------------------------------------------- |
| 709 | */ |
| 710 | |
| 711 | int |
| 712 | Tcl_ConvertCountedElement(src, length, dst, flags) |
| 713 | register CONST char *src; /* Source information for list element. */ |
| 714 | int length; /* Number of bytes in src, or -1. */ |
| 715 | char *dst; /* Place to put list-ified element. */ |
| 716 | int flags; /* Flags produced by Tcl_ScanElement. */ |
| 717 | { |
| 718 | register char *p = dst; |
| 719 | register CONST char *lastChar; |
| 720 | |
| 721 | /* |
| 722 | * See the comment block at the beginning of the Tcl_ScanElement |
| 723 | * code for details of how this works. |
| 724 | */ |
| 725 | |
| 726 | if (src && length == -1) { |
| 727 | length = strlen(src); |
| 728 | } |
| 729 | if ((src == NULL) || (length == 0)) { |
| 730 | p[0] = '{'; |
| 731 | p[1] = '}'; |
| 732 | p[2] = 0; |
| 733 | return 2; |
| 734 | } |
| 735 | lastChar = src + length; |
| 736 | if ((flags & USE_BRACES) && !(flags & TCL_DONT_USE_BRACES)) { |
| 737 | *p = '{'; |
| 738 | p++; |
| 739 | for ( ; src != lastChar; src++, p++) { |
| 740 | *p = *src; |
| 741 | } |
| 742 | *p = '}'; |
| 743 | p++; |
| 744 | } else { |
| 745 | if (*src == '{') { |
| 746 | /* |
| 747 | * Can't have a leading brace unless the whole element is |
| 748 | * enclosed in braces. Add a backslash before the brace. |
| 749 | * Furthermore, this may destroy the balance between open |
| 750 | * and close braces, so set BRACES_UNMATCHED. |
| 751 | */ |
| 752 | |
| 753 | p[0] = '\\'; |
| 754 | p[1] = '{'; |
| 755 | p += 2; |
| 756 | src++; |
| 757 | flags |= BRACES_UNMATCHED; |
| 758 | } |
| 759 | for (; src != lastChar; src++) { |
| 760 | switch (*src) { |
| 761 | case ']': |
| 762 | case '[': |
| 763 | case '$': |
| 764 | case ';': |
| 765 | case ' ': |
| 766 | case '\\': |
| 767 | case '"': |
| 768 | *p = '\\'; |
| 769 | p++; |
| 770 | break; |
| 771 | case '{': |
| 772 | case '}': |
| 773 | /* |
| 774 | * It may not seem necessary to backslash braces, but |
| 775 | * it is. The reason for this is that the resulting |
| 776 | * list element may actually be an element of a sub-list |
| 777 | * enclosed in braces (e.g. if Tcl_DStringStartSublist |
| 778 | * has been invoked), so there may be a brace mismatch |
| 779 | * if the braces aren't backslashed. |
| 780 | */ |
| 781 | |
| 782 | if (flags & BRACES_UNMATCHED) { |
| 783 | *p = '\\'; |
| 784 | p++; |
| 785 | } |
| 786 | break; |
| 787 | case '\f': |
| 788 | *p = '\\'; |
| 789 | p++; |
| 790 | *p = 'f'; |
| 791 | p++; |
| 792 | continue; |
| 793 | case '\n': |
| 794 | *p = '\\'; |
| 795 | p++; |
| 796 | *p = 'n'; |
| 797 | p++; |
| 798 | continue; |
| 799 | case '\r': |
| 800 | *p = '\\'; |
| 801 | p++; |
| 802 | *p = 'r'; |
| 803 | p++; |
| 804 | continue; |
| 805 | case '\t': |
| 806 | *p = '\\'; |
| 807 | p++; |
| 808 | *p = 't'; |
| 809 | p++; |
| 810 | continue; |
| 811 | case '\v': |
| 812 | *p = '\\'; |
| 813 | p++; |
| 814 | *p = 'v'; |
| 815 | p++; |
| 816 | continue; |
| 817 | } |
| 818 | *p = *src; |
| 819 | p++; |
| 820 | } |
| 821 | } |
| 822 | *p = '\0'; |
| 823 | return p-dst; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | *---------------------------------------------------------------------- |
| 828 | * |
| 829 | * Tcl_Merge -- |
| 830 | * |
| 831 | * Given a collection of strings, merge them together into a |
| 832 | * single string that has proper Tcl list structured (i.e. |
| 833 | * Tcl_SplitList may be used to retrieve strings equal to the |
| 834 | * original elements, and Tcl_Eval will parse the string back |
| 835 | * into its original elements). |
| 836 | * |
| 837 | * Results: |
| 838 | * The return value is the address of a dynamically-allocated |
| 839 | * string containing the merged list. |
| 840 | * |
| 841 | * Side effects: |
| 842 | * None. |
| 843 | * |
| 844 | *---------------------------------------------------------------------- |
| 845 | */ |
| 846 | |
| 847 | char * |
| 848 | Tcl_Merge(argc, argv) |
| 849 | int argc; /* How many strings to merge. */ |
| 850 | CONST char * CONST *argv; /* Array of string values. */ |
| 851 | { |
| 852 | # define LOCAL_SIZE 20 |
| 853 | int localFlags[LOCAL_SIZE], *flagPtr; |
| 854 | int numChars; |
| 855 | char *result; |
| 856 | char *dst; |
| 857 | int i; |
| 858 | |
| 859 | /* |
| 860 | * Pass 1: estimate space, gather flags. |
| 861 | */ |
| 862 | |
| 863 | if (argc <= LOCAL_SIZE) { |
| 864 | flagPtr = localFlags; |
| 865 | } else { |
| 866 | flagPtr = (int *) ckalloc((unsigned) argc*sizeof(int)); |
| 867 | } |
| 868 | numChars = 1; |
| 869 | for (i = 0; i < argc; i++) { |
| 870 | numChars += Tcl_ScanElement(argv[i], &flagPtr[i]) + 1; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * Pass two: copy into the result area. |
| 875 | */ |
| 876 | |
| 877 | result = (char *) ckalloc((unsigned) numChars); |
| 878 | dst = result; |
| 879 | for (i = 0; i < argc; i++) { |
| 880 | numChars = Tcl_ConvertElement(argv[i], dst, flagPtr[i]); |
| 881 | dst += numChars; |
| 882 | *dst = ' '; |
| 883 | dst++; |
| 884 | } |
| 885 | if (dst == result) { |
| 886 | *dst = 0; |
| 887 | } else { |
| 888 | dst[-1] = 0; |
| 889 | } |
| 890 | |
| 891 | if (flagPtr != localFlags) { |
| 892 | ckfree((char *) flagPtr); |
| 893 | } |
| 894 | return result; |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | *---------------------------------------------------------------------- |
| 899 | * |
| 900 | * Tcl_Backslash -- |
| 901 | * |
| 902 | * Figure out how to handle a backslash sequence. |
| 903 | * |
| 904 | * Results: |
| 905 | * The return value is the character that should be substituted |
| 906 | * in place of the backslash sequence that starts at src. If |
| 907 | * readPtr isn't NULL then it is filled in with a count of the |
| 908 | * number of characters in the backslash sequence. |
| 909 | * |
| 910 | * Side effects: |
| 911 | * None. |
| 912 | * |
| 913 | *---------------------------------------------------------------------- |
| 914 | */ |
| 915 | |
| 916 | char |
| 917 | Tcl_Backslash(src, readPtr) |
| 918 | CONST char *src; /* Points to the backslash character of |
| 919 | * a backslash sequence. */ |
| 920 | int *readPtr; /* Fill in with number of characters read |
| 921 | * from src, unless NULL. */ |
| 922 | { |
| 923 | char buf[TCL_UTF_MAX]; |
| 924 | Tcl_UniChar ch; |
| 925 | |
| 926 | Tcl_UtfBackslash(src, readPtr, buf); |
| 927 | TclUtfToUniChar(buf, &ch); |
| 928 | return (char) ch; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | *---------------------------------------------------------------------- |
| 933 | * |
| 934 | * Tcl_Concat -- |
| 935 | * |
| 936 | * Concatenate a set of strings into a single large string. |
| 937 | * |
| 938 | * Results: |
| 939 | * The return value is dynamically-allocated string containing |
| 940 | * a concatenation of all the strings in argv, with spaces between |
| 941 | * the original argv elements. |
| 942 | * |
| 943 | * Side effects: |
| 944 | * Memory is allocated for the result; the caller is responsible |
| 945 | * for freeing the memory. |
| 946 | * |
| 947 | *---------------------------------------------------------------------- |
| 948 | */ |
| 949 | |
| 950 | char * |
| 951 | Tcl_Concat(argc, argv) |
| 952 | int argc; /* Number of strings to concatenate. */ |
| 953 | CONST char * CONST *argv; /* Array of strings to concatenate. */ |
| 954 | { |
| 955 | int totalSize, i; |
| 956 | char *p; |
| 957 | char *result; |
| 958 | |
| 959 | for (totalSize = 1, i = 0; i < argc; i++) { |
| 960 | totalSize += strlen(argv[i]) + 1; |
| 961 | } |
| 962 | result = (char *) ckalloc((unsigned) totalSize); |
| 963 | if (argc == 0) { |
| 964 | *result = '\0'; |
| 965 | return result; |
| 966 | } |
| 967 | for (p = result, i = 0; i < argc; i++) { |
| 968 | CONST char *element; |
| 969 | int length; |
| 970 | |
| 971 | /* |
| 972 | * Clip white space off the front and back of the string |
| 973 | * to generate a neater result, and ignore any empty |
| 974 | * elements. |
| 975 | */ |
| 976 | |
| 977 | element = argv[i]; |
| 978 | while (isspace(UCHAR(*element))) { /* INTL: ISO space. */ |
| 979 | element++; |
| 980 | } |
| 981 | for (length = strlen(element); |
| 982 | (length > 0) |
| 983 | && (isspace(UCHAR(element[length-1]))) /* INTL: ISO space. */ |
| 984 | && ((length < 2) || (element[length-2] != '\\')); |
| 985 | length--) { |
| 986 | /* Null loop body. */ |
| 987 | } |
| 988 | if (length == 0) { |
| 989 | continue; |
| 990 | } |
| 991 | memcpy((VOID *) p, (VOID *) element, (size_t) length); |
| 992 | p += length; |
| 993 | *p = ' '; |
| 994 | p++; |
| 995 | } |
| 996 | if (p != result) { |
| 997 | p[-1] = 0; |
| 998 | } else { |
| 999 | *p = 0; |
| 1000 | } |
| 1001 | return result; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | *---------------------------------------------------------------------- |
| 1006 | * |
| 1007 | * Tcl_ConcatObj -- |
| 1008 | * |
| 1009 | * Concatenate the strings from a set of objects into a single string |
| 1010 | * object with spaces between the original strings. |
| 1011 | * |
| 1012 | * Results: |
| 1013 | * The return value is a new string object containing a concatenation |
| 1014 | * of the strings in objv. Its ref count is zero. |
| 1015 | * |
| 1016 | * Side effects: |
| 1017 | * A new object is created. |
| 1018 | * |
| 1019 | *---------------------------------------------------------------------- |
| 1020 | */ |
| 1021 | |
| 1022 | Tcl_Obj * |
| 1023 | Tcl_ConcatObj(objc, objv) |
| 1024 | int objc; /* Number of objects to concatenate. */ |
| 1025 | Tcl_Obj *CONST objv[]; /* Array of objects to concatenate. */ |
| 1026 | { |
| 1027 | int allocSize, finalSize, length, elemLength, i; |
| 1028 | char *p; |
| 1029 | char *element; |
| 1030 | char *concatStr; |
| 1031 | Tcl_Obj *objPtr; |
| 1032 | |
| 1033 | /* |
| 1034 | * Check first to see if all the items are of list type. If so, |
| 1035 | * we will concat them together as lists, and return a list object. |
| 1036 | * This is only valid when the lists have no current string |
| 1037 | * representation, since we don't know what the original type was. |
| 1038 | * An original string rep may have lost some whitespace info when |
| 1039 | * converted which could be important. |
| 1040 | */ |
| 1041 | for (i = 0; i < objc; i++) { |
| 1042 | objPtr = objv[i]; |
| 1043 | if ((objPtr->typePtr != &tclListType) || (objPtr->bytes != NULL)) { |
| 1044 | break; |
| 1045 | } |
| 1046 | } |
| 1047 | if (i == objc) { |
| 1048 | Tcl_Obj **listv; |
| 1049 | int listc; |
| 1050 | |
| 1051 | objPtr = Tcl_NewListObj(0, NULL); |
| 1052 | for (i = 0; i < objc; i++) { |
| 1053 | /* |
| 1054 | * Tcl_ListObjAppendList could be used here, but this saves |
| 1055 | * us a bit of type checking (since we've already done it) |
| 1056 | * Use of INT_MAX tells us to always put the new stuff on |
| 1057 | * the end. It will be set right in Tcl_ListObjReplace. |
| 1058 | */ |
| 1059 | Tcl_ListObjGetElements(NULL, objv[i], &listc, &listv); |
| 1060 | Tcl_ListObjReplace(NULL, objPtr, INT_MAX, 0, listc, listv); |
| 1061 | } |
| 1062 | return objPtr; |
| 1063 | } |
| 1064 | |
| 1065 | allocSize = 0; |
| 1066 | for (i = 0; i < objc; i++) { |
| 1067 | objPtr = objv[i]; |
| 1068 | element = Tcl_GetStringFromObj(objPtr, &length); |
| 1069 | if ((element != NULL) && (length > 0)) { |
| 1070 | allocSize += (length + 1); |
| 1071 | } |
| 1072 | } |
| 1073 | if (allocSize == 0) { |
| 1074 | allocSize = 1; /* enough for the NULL byte at end */ |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Allocate storage for the concatenated result. Note that allocSize |
| 1079 | * is one more than the total number of characters, and so includes |
| 1080 | * room for the terminating NULL byte. |
| 1081 | */ |
| 1082 | |
| 1083 | concatStr = (char *) ckalloc((unsigned) allocSize); |
| 1084 | |
| 1085 | /* |
| 1086 | * Now concatenate the elements. Clip white space off the front and back |
| 1087 | * to generate a neater result, and ignore any empty elements. Also put |
| 1088 | * a null byte at the end. |
| 1089 | */ |
| 1090 | |
| 1091 | finalSize = 0; |
| 1092 | if (objc == 0) { |
| 1093 | *concatStr = '\0'; |
| 1094 | } else { |
| 1095 | p = concatStr; |
| 1096 | for (i = 0; i < objc; i++) { |
| 1097 | objPtr = objv[i]; |
| 1098 | element = Tcl_GetStringFromObj(objPtr, &elemLength); |
| 1099 | while ((elemLength > 0) && (UCHAR(*element) < 127) |
| 1100 | && isspace(UCHAR(*element))) { /* INTL: ISO C space. */ |
| 1101 | element++; |
| 1102 | elemLength--; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * Trim trailing white space. But, be careful not to trim |
| 1107 | * a space character if it is preceded by a backslash: in |
| 1108 | * this case it could be significant. |
| 1109 | */ |
| 1110 | |
| 1111 | while ((elemLength > 0) && (UCHAR(element[elemLength-1]) < 127) |
| 1112 | && isspace(UCHAR(element[elemLength-1])) /* INTL: ISO C space. */ |
| 1113 | && ((elemLength < 2) || (element[elemLength-2] != '\\'))) { |
| 1114 | elemLength--; |
| 1115 | } |
| 1116 | if (elemLength == 0) { |
| 1117 | continue; /* nothing left of this element */ |
| 1118 | } |
| 1119 | memcpy((VOID *) p, (VOID *) element, (size_t) elemLength); |
| 1120 | p += elemLength; |
| 1121 | *p = ' '; |
| 1122 | p++; |
| 1123 | finalSize += (elemLength + 1); |
| 1124 | } |
| 1125 | if (p != concatStr) { |
| 1126 | p[-1] = 0; |
| 1127 | finalSize -= 1; /* we overwrote the final ' ' */ |
| 1128 | } else { |
| 1129 | *p = 0; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | TclNewObj(objPtr); |
| 1134 | objPtr->bytes = concatStr; |
| 1135 | objPtr->length = finalSize; |
| 1136 | return objPtr; |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | *---------------------------------------------------------------------- |
| 1141 | * |
| 1142 | * Tcl_StringMatch -- |
| 1143 | * |
| 1144 | * See if a particular string matches a particular pattern. |
| 1145 | * |
| 1146 | * Results: |
| 1147 | * The return value is 1 if string matches pattern, and |
| 1148 | * 0 otherwise. The matching operation permits the following |
| 1149 | * special characters in the pattern: *?\[] (see the manual |
| 1150 | * entry for details on what these mean). |
| 1151 | * |
| 1152 | * Side effects: |
| 1153 | * None. |
| 1154 | * |
| 1155 | *---------------------------------------------------------------------- |
| 1156 | */ |
| 1157 | |
| 1158 | int |
| 1159 | Tcl_StringMatch(string, pattern) |
| 1160 | CONST char *string; /* String. */ |
| 1161 | CONST char *pattern; /* Pattern, which may contain special |
| 1162 | * characters. */ |
| 1163 | { |
| 1164 | return Tcl_StringCaseMatch(string, pattern, 0); |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | *---------------------------------------------------------------------- |
| 1169 | * |
| 1170 | * Tcl_StringCaseMatch -- |
| 1171 | * |
| 1172 | * See if a particular string matches a particular pattern. |
| 1173 | * Allows case insensitivity. |
| 1174 | * |
| 1175 | * Results: |
| 1176 | * The return value is 1 if string matches pattern, and |
| 1177 | * 0 otherwise. The matching operation permits the following |
| 1178 | * special characters in the pattern: *?\[] (see the manual |
| 1179 | * entry for details on what these mean). |
| 1180 | * |
| 1181 | * Side effects: |
| 1182 | * None. |
| 1183 | * |
| 1184 | *---------------------------------------------------------------------- |
| 1185 | */ |
| 1186 | |
| 1187 | int |
| 1188 | Tcl_StringCaseMatch(string, pattern, nocase) |
| 1189 | CONST char *string; /* String. */ |
| 1190 | CONST char *pattern; /* Pattern, which may contain special |
| 1191 | * characters. */ |
| 1192 | int nocase; /* 0 for case sensitive, 1 for insensitive */ |
| 1193 | { |
| 1194 | int p, charLen; |
| 1195 | CONST char *pstart = pattern; |
| 1196 | Tcl_UniChar ch1, ch2; |
| 1197 | |
| 1198 | while (1) { |
| 1199 | p = *pattern; |
| 1200 | |
| 1201 | /* |
| 1202 | * See if we're at the end of both the pattern and the string. If |
| 1203 | * so, we succeeded. If we're at the end of the pattern but not at |
| 1204 | * the end of the string, we failed. |
| 1205 | */ |
| 1206 | |
| 1207 | if (p == '\0') { |
| 1208 | return (*string == '\0'); |
| 1209 | } |
| 1210 | if ((*string == '\0') && (p != '*')) { |
| 1211 | return 0; |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | * Check for a "*" as the next pattern character. It matches |
| 1216 | * any substring. We handle this by calling ourselves |
| 1217 | * recursively for each postfix of string, until either we |
| 1218 | * match or we reach the end of the string. |
| 1219 | */ |
| 1220 | |
| 1221 | if (p == '*') { |
| 1222 | /* |
| 1223 | * Skip all successive *'s in the pattern |
| 1224 | */ |
| 1225 | while (*(++pattern) == '*') {} |
| 1226 | p = *pattern; |
| 1227 | if (p == '\0') { |
| 1228 | return 1; |
| 1229 | } |
| 1230 | /* |
| 1231 | * This is a special case optimization for single-byte utf. |
| 1232 | */ |
| 1233 | if (UCHAR(*pattern) < 0x80) { |
| 1234 | ch2 = (Tcl_UniChar) |
| 1235 | (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern)); |
| 1236 | } else { |
| 1237 | Tcl_UtfToUniChar(pattern, &ch2); |
| 1238 | if (nocase) { |
| 1239 | ch2 = Tcl_UniCharToLower(ch2); |
| 1240 | } |
| 1241 | } |
| 1242 | while (1) { |
| 1243 | /* |
| 1244 | * Optimization for matching - cruise through the string |
| 1245 | * quickly if the next char in the pattern isn't a special |
| 1246 | * character |
| 1247 | */ |
| 1248 | if ((p != '[') && (p != '?') && (p != '\\')) { |
| 1249 | if (nocase) { |
| 1250 | while (*string) { |
| 1251 | charLen = TclUtfToUniChar(string, &ch1); |
| 1252 | if (ch2==ch1 || ch2==Tcl_UniCharToLower(ch1)) { |
| 1253 | break; |
| 1254 | } |
| 1255 | string += charLen; |
| 1256 | } |
| 1257 | } else { |
| 1258 | /* |
| 1259 | * There's no point in trying to make this code |
| 1260 | * shorter, as the number of bytes you want to |
| 1261 | * compare each time is non-constant. |
| 1262 | */ |
| 1263 | while (*string) { |
| 1264 | charLen = TclUtfToUniChar(string, &ch1); |
| 1265 | if (ch2 == ch1) { |
| 1266 | break; |
| 1267 | } |
| 1268 | string += charLen; |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | if (Tcl_StringCaseMatch(string, pattern, nocase)) { |
| 1273 | return 1; |
| 1274 | } |
| 1275 | if (*string == '\0') { |
| 1276 | return 0; |
| 1277 | } |
| 1278 | string += TclUtfToUniChar(string, &ch1); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Check for a "?" as the next pattern character. It matches |
| 1284 | * any single character. |
| 1285 | */ |
| 1286 | |
| 1287 | if (p == '?') { |
| 1288 | pattern++; |
| 1289 | string += TclUtfToUniChar(string, &ch1); |
| 1290 | continue; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * Check for a "[" as the next pattern character. It is followed |
| 1295 | * by a list of characters that are acceptable, or by a range |
| 1296 | * (two characters separated by "-"). |
| 1297 | */ |
| 1298 | |
| 1299 | if (p == '[') { |
| 1300 | Tcl_UniChar startChar, endChar; |
| 1301 | |
| 1302 | pattern++; |
| 1303 | if (UCHAR(*string) < 0x80) { |
| 1304 | ch1 = (Tcl_UniChar) |
| 1305 | (nocase ? tolower(UCHAR(*string)) : UCHAR(*string)); |
| 1306 | string++; |
| 1307 | } else { |
| 1308 | string += Tcl_UtfToUniChar(string, &ch1); |
| 1309 | if (nocase) { |
| 1310 | ch1 = Tcl_UniCharToLower(ch1); |
| 1311 | } |
| 1312 | } |
| 1313 | while (1) { |
| 1314 | if ((*pattern == ']') || (*pattern == '\0')) { |
| 1315 | return 0; |
| 1316 | } |
| 1317 | if (UCHAR(*pattern) < 0x80) { |
| 1318 | startChar = (Tcl_UniChar) |
| 1319 | (nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern)); |
| 1320 | pattern++; |
| 1321 | } else { |
| 1322 | pattern += Tcl_UtfToUniChar(pattern, &startChar); |
| 1323 | if (nocase) { |
| 1324 | startChar = Tcl_UniCharToLower(startChar); |
| 1325 | } |
| 1326 | } |
| 1327 | if (*pattern == '-') { |
| 1328 | pattern++; |
| 1329 | if (*pattern == '\0') { |
| 1330 | return 0; |
| 1331 | } |
| 1332 | if (UCHAR(*pattern) < 0x80) { |
| 1333 | endChar = (Tcl_UniChar) |
| 1334 | (nocase ? tolower(UCHAR(*pattern)) |
| 1335 | : UCHAR(*pattern)); |
| 1336 | pattern++; |
| 1337 | } else { |
| 1338 | pattern += Tcl_UtfToUniChar(pattern, &endChar); |
| 1339 | if (nocase) { |
| 1340 | endChar = Tcl_UniCharToLower(endChar); |
| 1341 | } |
| 1342 | } |
| 1343 | if (((startChar <= ch1) && (ch1 <= endChar)) |
| 1344 | || ((endChar <= ch1) && (ch1 <= startChar))) { |
| 1345 | /* |
| 1346 | * Matches ranges of form [a-z] or [z-a]. |
| 1347 | */ |
| 1348 | |
| 1349 | break; |
| 1350 | } |
| 1351 | } else if (startChar == ch1) { |
| 1352 | break; |
| 1353 | } |
| 1354 | } |
| 1355 | while (*pattern != ']') { |
| 1356 | if (*pattern == '\0') { |
| 1357 | pattern = Tcl_UtfPrev(pattern, pstart); |
| 1358 | break; |
| 1359 | } |
| 1360 | pattern++; |
| 1361 | } |
| 1362 | pattern++; |
| 1363 | continue; |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * If the next pattern character is '\', just strip off the '\' |
| 1368 | * so we do exact matching on the character that follows. |
| 1369 | */ |
| 1370 | |
| 1371 | if (p == '\\') { |
| 1372 | pattern++; |
| 1373 | if (*pattern == '\0') { |
| 1374 | return 0; |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * There's no special character. Just make sure that the next |
| 1380 | * bytes of each string match. |
| 1381 | */ |
| 1382 | |
| 1383 | string += TclUtfToUniChar(string, &ch1); |
| 1384 | pattern += TclUtfToUniChar(pattern, &ch2); |
| 1385 | if (nocase) { |
| 1386 | if (Tcl_UniCharToLower(ch1) != Tcl_UniCharToLower(ch2)) { |
| 1387 | return 0; |
| 1388 | } |
| 1389 | } else if (ch1 != ch2) { |
| 1390 | return 0; |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | *---------------------------------------------------------------------- |
| 1397 | * |
| 1398 | * Tcl_DStringInit -- |
| 1399 | * |
| 1400 | * Initializes a dynamic string, discarding any previous contents |
| 1401 | * of the string (Tcl_DStringFree should have been called already |
| 1402 | * if the dynamic string was previously in use). |
| 1403 | * |
| 1404 | * Results: |
| 1405 | * None. |
| 1406 | * |
| 1407 | * Side effects: |
| 1408 | * The dynamic string is initialized to be empty. |
| 1409 | * |
| 1410 | *---------------------------------------------------------------------- |
| 1411 | */ |
| 1412 | |
| 1413 | void |
| 1414 | Tcl_DStringInit(dsPtr) |
| 1415 | Tcl_DString *dsPtr; /* Pointer to structure for dynamic string. */ |
| 1416 | { |
| 1417 | dsPtr->string = dsPtr->staticSpace; |
| 1418 | dsPtr->length = 0; |
| 1419 | dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 1420 | dsPtr->staticSpace[0] = '\0'; |
| 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | *---------------------------------------------------------------------- |
| 1425 | * |
| 1426 | * Tcl_DStringAppend -- |
| 1427 | * |
| 1428 | * Append more characters to the current value of a dynamic string. |
| 1429 | * |
| 1430 | * Results: |
| 1431 | * The return value is a pointer to the dynamic string's new value. |
| 1432 | * |
| 1433 | * Side effects: |
| 1434 | * Length bytes from string (or all of string if length is less |
| 1435 | * than zero) are added to the current value of the string. Memory |
| 1436 | * gets reallocated if needed to accomodate the string's new size. |
| 1437 | * |
| 1438 | *---------------------------------------------------------------------- |
| 1439 | */ |
| 1440 | |
| 1441 | char * |
| 1442 | Tcl_DStringAppend(dsPtr, string, length) |
| 1443 | Tcl_DString *dsPtr; /* Structure describing dynamic string. */ |
| 1444 | CONST char *string; /* String to append. If length is -1 then |
| 1445 | * this must be null-terminated. */ |
| 1446 | int length; /* Number of characters from string to |
| 1447 | * append. If < 0, then append all of string, |
| 1448 | * up to null at end. */ |
| 1449 | { |
| 1450 | int newSize; |
| 1451 | char *dst; |
| 1452 | CONST char *end; |
| 1453 | |
| 1454 | if (length < 0) { |
| 1455 | length = strlen(string); |
| 1456 | } |
| 1457 | newSize = length + dsPtr->length; |
| 1458 | |
| 1459 | /* |
| 1460 | * Allocate a larger buffer for the string if the current one isn't |
| 1461 | * large enough. Allocate extra space in the new buffer so that there |
| 1462 | * will be room to grow before we have to allocate again. |
| 1463 | */ |
| 1464 | |
| 1465 | if (newSize >= dsPtr->spaceAvl) { |
| 1466 | dsPtr->spaceAvl = newSize * 2; |
| 1467 | if (dsPtr->string == dsPtr->staticSpace) { |
| 1468 | char *newString; |
| 1469 | |
| 1470 | newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl); |
| 1471 | memcpy((VOID *) newString, (VOID *) dsPtr->string, |
| 1472 | (size_t) dsPtr->length); |
| 1473 | dsPtr->string = newString; |
| 1474 | } else { |
| 1475 | dsPtr->string = (char *) ckrealloc((VOID *) dsPtr->string, |
| 1476 | (size_t) dsPtr->spaceAvl); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * Copy the new string into the buffer at the end of the old |
| 1482 | * one. |
| 1483 | */ |
| 1484 | |
| 1485 | for (dst = dsPtr->string + dsPtr->length, end = string+length; |
| 1486 | string < end; string++, dst++) { |
| 1487 | *dst = *string; |
| 1488 | } |
| 1489 | *dst = '\0'; |
| 1490 | dsPtr->length += length; |
| 1491 | return dsPtr->string; |
| 1492 | } |
| 1493 | |
| 1494 | /* |
| 1495 | *---------------------------------------------------------------------- |
| 1496 | * |
| 1497 | * Tcl_DStringAppendElement -- |
| 1498 | * |
| 1499 | * Append a list element to the current value of a dynamic string. |
| 1500 | * |
| 1501 | * Results: |
| 1502 | * The return value is a pointer to the dynamic string's new value. |
| 1503 | * |
| 1504 | * Side effects: |
| 1505 | * String is reformatted as a list element and added to the current |
| 1506 | * value of the string. Memory gets reallocated if needed to |
| 1507 | * accomodate the string's new size. |
| 1508 | * |
| 1509 | *---------------------------------------------------------------------- |
| 1510 | */ |
| 1511 | |
| 1512 | char * |
| 1513 | Tcl_DStringAppendElement(dsPtr, string) |
| 1514 | Tcl_DString *dsPtr; /* Structure describing dynamic string. */ |
| 1515 | CONST char *string; /* String to append. Must be |
| 1516 | * null-terminated. */ |
| 1517 | { |
| 1518 | int newSize, flags, strSize; |
| 1519 | char *dst; |
| 1520 | |
| 1521 | strSize = ((string == NULL) ? 0 : strlen(string)); |
| 1522 | newSize = Tcl_ScanCountedElement(string, strSize, &flags) |
| 1523 | + dsPtr->length + 1; |
| 1524 | |
| 1525 | /* |
| 1526 | * Allocate a larger buffer for the string if the current one isn't |
| 1527 | * large enough. Allocate extra space in the new buffer so that there |
| 1528 | * will be room to grow before we have to allocate again. |
| 1529 | * SPECIAL NOTE: must use memcpy, not strcpy, to copy the string |
| 1530 | * to a larger buffer, since there may be embedded NULLs in the |
| 1531 | * string in some cases. |
| 1532 | */ |
| 1533 | |
| 1534 | if (newSize >= dsPtr->spaceAvl) { |
| 1535 | dsPtr->spaceAvl = newSize * 2; |
| 1536 | if (dsPtr->string == dsPtr->staticSpace) { |
| 1537 | char *newString; |
| 1538 | |
| 1539 | newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl); |
| 1540 | memcpy((VOID *) newString, (VOID *) dsPtr->string, |
| 1541 | (size_t) dsPtr->length); |
| 1542 | dsPtr->string = newString; |
| 1543 | } else { |
| 1544 | dsPtr->string = (char *) ckrealloc((VOID *) dsPtr->string, |
| 1545 | (size_t) dsPtr->spaceAvl); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * Convert the new string to a list element and copy it into the |
| 1551 | * buffer at the end, with a space, if needed. |
| 1552 | */ |
| 1553 | |
| 1554 | dst = dsPtr->string + dsPtr->length; |
| 1555 | if (TclNeedSpace(dsPtr->string, dst)) { |
| 1556 | *dst = ' '; |
| 1557 | dst++; |
| 1558 | dsPtr->length++; |
| 1559 | } |
| 1560 | dsPtr->length += Tcl_ConvertCountedElement(string, strSize, dst, flags); |
| 1561 | return dsPtr->string; |
| 1562 | } |
| 1563 | |
| 1564 | /* |
| 1565 | *---------------------------------------------------------------------- |
| 1566 | * |
| 1567 | * Tcl_DStringSetLength -- |
| 1568 | * |
| 1569 | * Change the length of a dynamic string. This can cause the |
| 1570 | * string to either grow or shrink, depending on the value of |
| 1571 | * length. |
| 1572 | * |
| 1573 | * Results: |
| 1574 | * None. |
| 1575 | * |
| 1576 | * Side effects: |
| 1577 | * The length of dsPtr is changed to length and a null byte is |
| 1578 | * stored at that position in the string. If length is larger |
| 1579 | * than the space allocated for dsPtr, then a panic occurs. |
| 1580 | * |
| 1581 | *---------------------------------------------------------------------- |
| 1582 | */ |
| 1583 | |
| 1584 | void |
| 1585 | Tcl_DStringSetLength(dsPtr, length) |
| 1586 | Tcl_DString *dsPtr; /* Structure describing dynamic string. */ |
| 1587 | int length; /* New length for dynamic string. */ |
| 1588 | { |
| 1589 | int newsize; |
| 1590 | |
| 1591 | if (length < 0) { |
| 1592 | length = 0; |
| 1593 | } |
| 1594 | if (length >= dsPtr->spaceAvl) { |
| 1595 | /* |
| 1596 | * There are two interesting cases here. In the first case, the user |
| 1597 | * may be trying to allocate a large buffer of a specific size. It |
| 1598 | * would be wasteful to overallocate that buffer, so we just allocate |
| 1599 | * enough for the requested size plus the trailing null byte. In the |
| 1600 | * second case, we are growing the buffer incrementally, so we need |
| 1601 | * behavior similar to Tcl_DStringAppend. The requested length will |
| 1602 | * usually be a small delta above the current spaceAvl, so we'll end up |
| 1603 | * doubling the old size. This won't grow the buffer quite as quickly, |
| 1604 | * but it should be close enough. |
| 1605 | */ |
| 1606 | |
| 1607 | newsize = dsPtr->spaceAvl * 2; |
| 1608 | if (length < newsize) { |
| 1609 | dsPtr->spaceAvl = newsize; |
| 1610 | } else { |
| 1611 | dsPtr->spaceAvl = length + 1; |
| 1612 | } |
| 1613 | if (dsPtr->string == dsPtr->staticSpace) { |
| 1614 | char *newString; |
| 1615 | |
| 1616 | newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl); |
| 1617 | memcpy((VOID *) newString, (VOID *) dsPtr->string, |
| 1618 | (size_t) dsPtr->length); |
| 1619 | dsPtr->string = newString; |
| 1620 | } else { |
| 1621 | dsPtr->string = (char *) ckrealloc((VOID *) dsPtr->string, |
| 1622 | (size_t) dsPtr->spaceAvl); |
| 1623 | } |
| 1624 | } |
| 1625 | dsPtr->length = length; |
| 1626 | dsPtr->string[length] = 0; |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | *---------------------------------------------------------------------- |
| 1631 | * |
| 1632 | * Tcl_DStringFree -- |
| 1633 | * |
| 1634 | * Frees up any memory allocated for the dynamic string and |
| 1635 | * reinitializes the string to an empty state. |
| 1636 | * |
| 1637 | * Results: |
| 1638 | * None. |
| 1639 | * |
| 1640 | * Side effects: |
| 1641 | * The previous contents of the dynamic string are lost, and |
| 1642 | * the new value is an empty string. |
| 1643 | * |
| 1644 | *---------------------------------------------------------------------- */ |
| 1645 | |
| 1646 | void |
| 1647 | Tcl_DStringFree(dsPtr) |
| 1648 | Tcl_DString *dsPtr; /* Structure describing dynamic string. */ |
| 1649 | { |
| 1650 | if (dsPtr->string != dsPtr->staticSpace) { |
| 1651 | ckfree(dsPtr->string); |
| 1652 | } |
| 1653 | dsPtr->string = dsPtr->staticSpace; |
| 1654 | dsPtr->length = 0; |
| 1655 | dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 1656 | dsPtr->staticSpace[0] = '\0'; |
| 1657 | } |
| 1658 | |
| 1659 | /* |
| 1660 | *---------------------------------------------------------------------- |
| 1661 | * |
| 1662 | * Tcl_DStringResult -- |
| 1663 | * |
| 1664 | * This procedure moves the value of a dynamic string into an |
| 1665 | * interpreter as its string result. Afterwards, the dynamic string |
| 1666 | * is reset to an empty string. |
| 1667 | * |
| 1668 | * Results: |
| 1669 | * None. |
| 1670 | * |
| 1671 | * Side effects: |
| 1672 | * The string is "moved" to interp's result, and any existing |
| 1673 | * string result for interp is freed. dsPtr is reinitialized to |
| 1674 | * an empty string. |
| 1675 | * |
| 1676 | *---------------------------------------------------------------------- |
| 1677 | */ |
| 1678 | |
| 1679 | void |
| 1680 | Tcl_DStringResult(interp, dsPtr) |
| 1681 | Tcl_Interp *interp; /* Interpreter whose result is to be reset. */ |
| 1682 | Tcl_DString *dsPtr; /* Dynamic string that is to become the |
| 1683 | * result of interp. */ |
| 1684 | { |
| 1685 | Tcl_ResetResult(interp); |
| 1686 | |
| 1687 | if (dsPtr->string != dsPtr->staticSpace) { |
| 1688 | interp->result = dsPtr->string; |
| 1689 | interp->freeProc = TCL_DYNAMIC; |
| 1690 | } else if (dsPtr->length < TCL_RESULT_SIZE) { |
| 1691 | interp->result = ((Interp *) interp)->resultSpace; |
| 1692 | strcpy(interp->result, dsPtr->string); |
| 1693 | } else { |
| 1694 | Tcl_SetResult(interp, dsPtr->string, TCL_VOLATILE); |
| 1695 | } |
| 1696 | |
| 1697 | dsPtr->string = dsPtr->staticSpace; |
| 1698 | dsPtr->length = 0; |
| 1699 | dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 1700 | dsPtr->staticSpace[0] = '\0'; |
| 1701 | } |
| 1702 | |
| 1703 | /* |
| 1704 | *---------------------------------------------------------------------- |
| 1705 | * |
| 1706 | * Tcl_DStringGetResult -- |
| 1707 | * |
| 1708 | * This procedure moves an interpreter's result into a dynamic string. |
| 1709 | * |
| 1710 | * Results: |
| 1711 | * None. |
| 1712 | * |
| 1713 | * Side effects: |
| 1714 | * The interpreter's string result is cleared, and the previous |
| 1715 | * contents of dsPtr are freed. |
| 1716 | * |
| 1717 | * If the string result is empty, the object result is moved to the |
| 1718 | * string result, then the object result is reset. |
| 1719 | * |
| 1720 | *---------------------------------------------------------------------- |
| 1721 | */ |
| 1722 | |
| 1723 | void |
| 1724 | Tcl_DStringGetResult(interp, dsPtr) |
| 1725 | Tcl_Interp *interp; /* Interpreter whose result is to be reset. */ |
| 1726 | Tcl_DString *dsPtr; /* Dynamic string that is to become the |
| 1727 | * result of interp. */ |
| 1728 | { |
| 1729 | Interp *iPtr = (Interp *) interp; |
| 1730 | |
| 1731 | if (dsPtr->string != dsPtr->staticSpace) { |
| 1732 | ckfree(dsPtr->string); |
| 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | * If the string result is empty, move the object result to the |
| 1737 | * string result, then reset the object result. |
| 1738 | */ |
| 1739 | |
| 1740 | if (*(iPtr->result) == 0) { |
| 1741 | Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)), |
| 1742 | TCL_VOLATILE); |
| 1743 | } |
| 1744 | |
| 1745 | dsPtr->length = strlen(iPtr->result); |
| 1746 | if (iPtr->freeProc != NULL) { |
| 1747 | if (iPtr->freeProc == TCL_DYNAMIC) { |
| 1748 | dsPtr->string = iPtr->result; |
| 1749 | dsPtr->spaceAvl = dsPtr->length+1; |
| 1750 | } else { |
| 1751 | dsPtr->string = (char *) ckalloc((unsigned) (dsPtr->length+1)); |
| 1752 | strcpy(dsPtr->string, iPtr->result); |
| 1753 | (*iPtr->freeProc)(iPtr->result); |
| 1754 | } |
| 1755 | dsPtr->spaceAvl = dsPtr->length+1; |
| 1756 | iPtr->freeProc = NULL; |
| 1757 | } else { |
| 1758 | if (dsPtr->length < TCL_DSTRING_STATIC_SIZE) { |
| 1759 | dsPtr->string = dsPtr->staticSpace; |
| 1760 | dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 1761 | } else { |
| 1762 | dsPtr->string = (char *) ckalloc((unsigned) (dsPtr->length + 1)); |
| 1763 | dsPtr->spaceAvl = dsPtr->length + 1; |
| 1764 | } |
| 1765 | strcpy(dsPtr->string, iPtr->result); |
| 1766 | } |
| 1767 | |
| 1768 | iPtr->result = iPtr->resultSpace; |
| 1769 | iPtr->resultSpace[0] = 0; |
| 1770 | } |
| 1771 | |
| 1772 | /* |
| 1773 | *---------------------------------------------------------------------- |
| 1774 | * |
| 1775 | * Tcl_DStringStartSublist -- |
| 1776 | * |
| 1777 | * This procedure adds the necessary information to a dynamic |
| 1778 | * string (e.g. " {" to start a sublist. Future element |
| 1779 | * appends will be in the sublist rather than the main list. |
| 1780 | * |
| 1781 | * Results: |
| 1782 | * None. |
| 1783 | * |
| 1784 | * Side effects: |
| 1785 | * Characters get added to the dynamic string. |
| 1786 | * |
| 1787 | *---------------------------------------------------------------------- |
| 1788 | */ |
| 1789 | |
| 1790 | void |
| 1791 | Tcl_DStringStartSublist(dsPtr) |
| 1792 | Tcl_DString *dsPtr; /* Dynamic string. */ |
| 1793 | { |
| 1794 | if (TclNeedSpace(dsPtr->string, dsPtr->string + dsPtr->length)) { |
| 1795 | Tcl_DStringAppend(dsPtr, " {", -1); |
| 1796 | } else { |
| 1797 | Tcl_DStringAppend(dsPtr, "{", -1); |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | /* |
| 1802 | *---------------------------------------------------------------------- |
| 1803 | * |
| 1804 | * Tcl_DStringEndSublist -- |
| 1805 | * |
| 1806 | * This procedure adds the necessary characters to a dynamic |
| 1807 | * string to end a sublist (e.g. "}"). Future element appends |
| 1808 | * will be in the enclosing (sub)list rather than the current |
| 1809 | * sublist. |
| 1810 | * |
| 1811 | * Results: |
| 1812 | * None. |
| 1813 | * |
| 1814 | * Side effects: |
| 1815 | * None. |
| 1816 | * |
| 1817 | *---------------------------------------------------------------------- |
| 1818 | */ |
| 1819 | |
| 1820 | void |
| 1821 | Tcl_DStringEndSublist(dsPtr) |
| 1822 | Tcl_DString *dsPtr; /* Dynamic string. */ |
| 1823 | { |
| 1824 | Tcl_DStringAppend(dsPtr, "}", -1); |
| 1825 | } |
| 1826 | |
| 1827 | /* |
| 1828 | *---------------------------------------------------------------------- |
| 1829 | * |
| 1830 | * Tcl_PrintDouble -- |
| 1831 | * |
| 1832 | * Given a floating-point value, this procedure converts it to |
| 1833 | * an ASCII string using. |
| 1834 | * |
| 1835 | * Results: |
| 1836 | * The ASCII equivalent of "value" is written at "dst". It is |
| 1837 | * written using the current precision, and it is guaranteed to |
| 1838 | * contain a decimal point or exponent, so that it looks like |
| 1839 | * a floating-point value and not an integer. |
| 1840 | * |
| 1841 | * Side effects: |
| 1842 | * None. |
| 1843 | * |
| 1844 | *---------------------------------------------------------------------- |
| 1845 | */ |
| 1846 | |
| 1847 | void |
| 1848 | Tcl_PrintDouble(interp, value, dst) |
| 1849 | Tcl_Interp *interp; /* Interpreter whose tcl_precision |
| 1850 | * variable used to be used to control |
| 1851 | * printing. It's ignored now. */ |
| 1852 | double value; /* Value to print as string. */ |
| 1853 | char *dst; /* Where to store converted value; |
| 1854 | * must have at least TCL_DOUBLE_SPACE |
| 1855 | * characters. */ |
| 1856 | { |
| 1857 | char *p, c; |
| 1858 | Tcl_UniChar ch; |
| 1859 | |
| 1860 | Tcl_MutexLock(&precisionMutex); |
| 1861 | sprintf(dst, precisionFormat, value); |
| 1862 | Tcl_MutexUnlock(&precisionMutex); |
| 1863 | |
| 1864 | /* |
| 1865 | * If the ASCII result looks like an integer, add ".0" so that it |
| 1866 | * doesn't look like an integer anymore. This prevents floating-point |
| 1867 | * values from being converted to integers unintentionally. |
| 1868 | * Check for ASCII specifically to speed up the function. |
| 1869 | */ |
| 1870 | |
| 1871 | for (p = dst; *p != 0; ) { |
| 1872 | if (UCHAR(*p) < 0x80) { |
| 1873 | c = *p++; |
| 1874 | } else { |
| 1875 | p += Tcl_UtfToUniChar(p, &ch); |
| 1876 | c = UCHAR(ch); |
| 1877 | } |
| 1878 | if ((c == '.') || isalpha(UCHAR(c))) { /* INTL: ISO only. */ |
| 1879 | return; |
| 1880 | } |
| 1881 | } |
| 1882 | p[0] = '.'; |
| 1883 | p[1] = '0'; |
| 1884 | p[2] = 0; |
| 1885 | } |
| 1886 | |
| 1887 | /* |
| 1888 | *---------------------------------------------------------------------- |
| 1889 | * |
| 1890 | * TclPrecTraceProc -- |
| 1891 | * |
| 1892 | * This procedure is invoked whenever the variable "tcl_precision" |
| 1893 | * is written. |
| 1894 | * |
| 1895 | * Results: |
| 1896 | * Returns NULL if all went well, or an error message if the |
| 1897 | * new value for the variable doesn't make sense. |
| 1898 | * |
| 1899 | * Side effects: |
| 1900 | * If the new value doesn't make sense then this procedure |
| 1901 | * undoes the effect of the variable modification. Otherwise |
| 1902 | * it modifies the format string that's used by Tcl_PrintDouble. |
| 1903 | * |
| 1904 | *---------------------------------------------------------------------- |
| 1905 | */ |
| 1906 | |
| 1907 | /* ARGSUSED */ |
| 1908 | char * |
| 1909 | TclPrecTraceProc(clientData, interp, name1, name2, flags) |
| 1910 | ClientData clientData; /* Not used. */ |
| 1911 | Tcl_Interp *interp; /* Interpreter containing variable. */ |
| 1912 | CONST char *name1; /* Name of variable. */ |
| 1913 | CONST char *name2; /* Second part of variable name. */ |
| 1914 | int flags; /* Information about what happened. */ |
| 1915 | { |
| 1916 | CONST char *value; |
| 1917 | char *end; |
| 1918 | int prec; |
| 1919 | |
| 1920 | /* |
| 1921 | * If the variable is unset, then recreate the trace. |
| 1922 | */ |
| 1923 | |
| 1924 | if (flags & TCL_TRACE_UNSETS) { |
| 1925 | if ((flags & TCL_TRACE_DESTROYED) && !(flags & TCL_INTERP_DESTROYED)) { |
| 1926 | Tcl_TraceVar2(interp, name1, name2, |
| 1927 | TCL_GLOBAL_ONLY|TCL_TRACE_READS|TCL_TRACE_WRITES |
| 1928 | |TCL_TRACE_UNSETS, TclPrecTraceProc, clientData); |
| 1929 | } |
| 1930 | return (char *) NULL; |
| 1931 | } |
| 1932 | |
| 1933 | /* |
| 1934 | * When the variable is read, reset its value from our shared |
| 1935 | * value. This is needed in case the variable was modified in |
| 1936 | * some other interpreter so that this interpreter's value is |
| 1937 | * out of date. |
| 1938 | */ |
| 1939 | |
| 1940 | Tcl_MutexLock(&precisionMutex); |
| 1941 | |
| 1942 | if (flags & TCL_TRACE_READS) { |
| 1943 | Tcl_SetVar2(interp, name1, name2, precisionString, |
| 1944 | flags & TCL_GLOBAL_ONLY); |
| 1945 | Tcl_MutexUnlock(&precisionMutex); |
| 1946 | return (char *) NULL; |
| 1947 | } |
| 1948 | |
| 1949 | /* |
| 1950 | * The variable is being written. Check the new value and disallow |
| 1951 | * it if it isn't reasonable or if this is a safe interpreter (we |
| 1952 | * don't want safe interpreters messing up the precision of other |
| 1953 | * interpreters). |
| 1954 | */ |
| 1955 | |
| 1956 | if (Tcl_IsSafe(interp)) { |
| 1957 | Tcl_SetVar2(interp, name1, name2, precisionString, |
| 1958 | flags & TCL_GLOBAL_ONLY); |
| 1959 | Tcl_MutexUnlock(&precisionMutex); |
| 1960 | return "can't modify precision from a safe interpreter"; |
| 1961 | } |
| 1962 | value = Tcl_GetVar2(interp, name1, name2, flags & TCL_GLOBAL_ONLY); |
| 1963 | if (value == NULL) { |
| 1964 | value = ""; |
| 1965 | } |
| 1966 | prec = strtoul(value, &end, 10); |
| 1967 | if ((prec <= 0) || (prec > TCL_MAX_PREC) || (prec > 100) || |
| 1968 | (end == value) || (*end != 0)) { |
| 1969 | Tcl_SetVar2(interp, name1, name2, precisionString, |
| 1970 | flags & TCL_GLOBAL_ONLY); |
| 1971 | Tcl_MutexUnlock(&precisionMutex); |
| 1972 | return "improper value for precision"; |
| 1973 | } |
| 1974 | TclFormatInt(precisionString, prec); |
| 1975 | sprintf(precisionFormat, "%%.%dg", prec); |
| 1976 | Tcl_MutexUnlock(&precisionMutex); |
| 1977 | return (char *) NULL; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | *---------------------------------------------------------------------- |
| 1982 | * |
| 1983 | * TclNeedSpace -- |
| 1984 | * |
| 1985 | * This procedure checks to see whether it is appropriate to |
| 1986 | * add a space before appending a new list element to an |
| 1987 | * existing string. |
| 1988 | * |
| 1989 | * Results: |
| 1990 | * The return value is 1 if a space is appropriate, 0 otherwise. |
| 1991 | * |
| 1992 | * Side effects: |
| 1993 | * None. |
| 1994 | * |
| 1995 | *---------------------------------------------------------------------- |
| 1996 | */ |
| 1997 | |
| 1998 | int |
| 1999 | TclNeedSpace(start, end) |
| 2000 | CONST char *start; /* First character in string. */ |
| 2001 | CONST char *end; /* End of string (place where space will |
| 2002 | * be added, if appropriate). */ |
| 2003 | { |
| 2004 | /* |
| 2005 | * A space is needed unless either |
| 2006 | * (a) we're at the start of the string, or |
| 2007 | */ |
| 2008 | if (end == start) { |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * (b) we're at the start of a nested list-element, quoted with an |
| 2014 | * open curly brace; we can be nested arbitrarily deep, so long |
| 2015 | * as the first curly brace starts an element, so backtrack over |
| 2016 | * open curly braces that are trailing characters of the string; and |
| 2017 | */ |
| 2018 | |
| 2019 | end = Tcl_UtfPrev(end, start); |
| 2020 | while (*end == '{') { |
| 2021 | if (end == start) { |
| 2022 | return 0; |
| 2023 | } |
| 2024 | end = Tcl_UtfPrev(end, start); |
| 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | * (c) the trailing character of the string is already a list-element |
| 2029 | * separator (according to TclFindElement); that is, one of these |
| 2030 | * characters: |
| 2031 | * \u0009 \t TAB |
| 2032 | * \u000A \n NEWLINE |
| 2033 | * \u000B \v VERTICAL TAB |
| 2034 | * \u000C \f FORM FEED |
| 2035 | * \u000D \r CARRIAGE RETURN |
| 2036 | * \u0020 SPACE |
| 2037 | * with the condition that the penultimate character is not a |
| 2038 | * backslash. |
| 2039 | */ |
| 2040 | |
| 2041 | if (*end > 0x20) { |
| 2042 | /* |
| 2043 | * Performance tweak. All ASCII spaces are <= 0x20. So get |
| 2044 | * a quick answer for most characters before comparing against |
| 2045 | * all spaces in the switch below. |
| 2046 | * |
| 2047 | * NOTE: Remove this if other Unicode spaces ever get accepted |
| 2048 | * as list-element separators. |
| 2049 | */ |
| 2050 | return 1; |
| 2051 | } |
| 2052 | switch (*end) { |
| 2053 | case ' ': |
| 2054 | case '\t': |
| 2055 | case '\n': |
| 2056 | case '\r': |
| 2057 | case '\v': |
| 2058 | case '\f': |
| 2059 | if ((end == start) || (end[-1] != '\\')) { |
| 2060 | return 0; |
| 2061 | } |
| 2062 | } |
| 2063 | return 1; |
| 2064 | } |
| 2065 | |
| 2066 | /* |
| 2067 | *---------------------------------------------------------------------- |
| 2068 | * |
| 2069 | * TclFormatInt -- |
| 2070 | * |
| 2071 | * This procedure formats an integer into a sequence of decimal digit |
| 2072 | * characters in a buffer. If the integer is negative, a minus sign is |
| 2073 | * inserted at the start of the buffer. A null character is inserted at |
| 2074 | * the end of the formatted characters. It is the caller's |
| 2075 | * responsibility to ensure that enough storage is available. This |
| 2076 | * procedure has the effect of sprintf(buffer, "%d", n) but is faster. |
| 2077 | * |
| 2078 | * Results: |
| 2079 | * An integer representing the number of characters formatted, not |
| 2080 | * including the terminating \0. |
| 2081 | * |
| 2082 | * Side effects: |
| 2083 | * The formatted characters are written into the storage pointer to |
| 2084 | * by the "buffer" argument. |
| 2085 | * |
| 2086 | *---------------------------------------------------------------------- |
| 2087 | */ |
| 2088 | |
| 2089 | int |
| 2090 | TclFormatInt(buffer, n) |
| 2091 | char *buffer; /* Points to the storage into which the |
| 2092 | * formatted characters are written. */ |
| 2093 | long n; /* The integer to format. */ |
| 2094 | { |
| 2095 | long intVal; |
| 2096 | int i; |
| 2097 | int numFormatted, j; |
| 2098 | char *digits = "0123456789"; |
| 2099 | |
| 2100 | /* |
| 2101 | * Check first whether "n" is zero. |
| 2102 | */ |
| 2103 | |
| 2104 | if (n == 0) { |
| 2105 | buffer[0] = '0'; |
| 2106 | buffer[1] = 0; |
| 2107 | return 1; |
| 2108 | } |
| 2109 | |
| 2110 | /* |
| 2111 | * Check whether "n" is the maximum negative value. This is |
| 2112 | * -2^(m-1) for an m-bit word, and has no positive equivalent; |
| 2113 | * negating it produces the same value. |
| 2114 | */ |
| 2115 | |
| 2116 | if (n == -n) { |
| 2117 | sprintf(buffer, "%ld", n); |
| 2118 | return strlen(buffer); |
| 2119 | } |
| 2120 | |
| 2121 | /* |
| 2122 | * Generate the characters of the result backwards in the buffer. |
| 2123 | */ |
| 2124 | |
| 2125 | intVal = (n < 0? -n : n); |
| 2126 | i = 0; |
| 2127 | buffer[0] = '\0'; |
| 2128 | do { |
| 2129 | i++; |
| 2130 | buffer[i] = digits[intVal % 10]; |
| 2131 | intVal = intVal/10; |
| 2132 | } while (intVal > 0); |
| 2133 | if (n < 0) { |
| 2134 | i++; |
| 2135 | buffer[i] = '-'; |
| 2136 | } |
| 2137 | numFormatted = i; |
| 2138 | |
| 2139 | /* |
| 2140 | * Now reverse the characters. |
| 2141 | */ |
| 2142 | |
| 2143 | for (j = 0; j < i; j++, i--) { |
| 2144 | char tmp = buffer[i]; |
| 2145 | buffer[i] = buffer[j]; |
| 2146 | buffer[j] = tmp; |
| 2147 | } |
| 2148 | return numFormatted; |
| 2149 | } |
| 2150 | |
| 2151 | /* |
| 2152 | *---------------------------------------------------------------------- |
| 2153 | * |
| 2154 | * TclLooksLikeInt -- |
| 2155 | * |
| 2156 | * This procedure decides whether the leading characters of a |
| 2157 | * string look like an integer or something else (such as a |
| 2158 | * floating-point number or string). |
| 2159 | * |
| 2160 | * Results: |
| 2161 | * The return value is 1 if the leading characters of p look |
| 2162 | * like a valid Tcl integer. If they look like a floating-point |
| 2163 | * number (e.g. "e01" or "2.4"), or if they don't look like a |
| 2164 | * number at all, then 0 is returned. |
| 2165 | * |
| 2166 | * Side effects: |
| 2167 | * None. |
| 2168 | * |
| 2169 | *---------------------------------------------------------------------- |
| 2170 | */ |
| 2171 | |
| 2172 | int |
| 2173 | TclLooksLikeInt(bytes, length) |
| 2174 | register CONST char *bytes; /* Points to first byte of the string. */ |
| 2175 | int length; /* Number of bytes in the string. If < 0 |
| 2176 | * bytes up to the first null byte are |
| 2177 | * considered (if they may appear in an |
| 2178 | * integer). */ |
| 2179 | { |
| 2180 | register CONST char *p; |
| 2181 | |
| 2182 | if ((bytes == NULL) && (length > 0)) { |
| 2183 | Tcl_Panic("TclLooksLikeInt: cannot scan %d bytes from NULL", length); |
| 2184 | } |
| 2185 | |
| 2186 | if (length < 0) { |
| 2187 | length = (bytes? strlen(bytes) : 0); |
| 2188 | } |
| 2189 | |
| 2190 | p = bytes; |
| 2191 | while (length && isspace(UCHAR(*p))) { /* INTL: ISO space. */ |
| 2192 | length--; p++; |
| 2193 | } |
| 2194 | if (length == 0) { |
| 2195 | return 0; |
| 2196 | } |
| 2197 | if ((*p == '+') || (*p == '-')) { |
| 2198 | p++; length--; |
| 2199 | } |
| 2200 | |
| 2201 | return (0 != TclParseInteger(p, length)); |
| 2202 | } |
| 2203 | |
| 2204 | /* |
| 2205 | *---------------------------------------------------------------------- |
| 2206 | * |
| 2207 | * TclGetIntForIndex -- |
| 2208 | * |
| 2209 | * This procedure returns an integer corresponding to the list index |
| 2210 | * held in a Tcl object. The Tcl object's value is expected to be |
| 2211 | * either an integer or a string of the form "end([+-]integer)?". |
| 2212 | * |
| 2213 | * Results: |
| 2214 | * The return value is normally TCL_OK, which means that the index was |
| 2215 | * successfully stored into the location referenced by "indexPtr". If |
| 2216 | * the Tcl object referenced by "objPtr" has the value "end", the |
| 2217 | * value stored is "endValue". If "objPtr"s values is not of the form |
| 2218 | * "end([+-]integer)?" and |
| 2219 | * can not be converted to an integer, TCL_ERROR is returned and, if |
| 2220 | * "interp" is non-NULL, an error message is left in the interpreter's |
| 2221 | * result object. |
| 2222 | * |
| 2223 | * Side effects: |
| 2224 | * The object referenced by "objPtr" might be converted to an |
| 2225 | * integer, wide integer, or end-based-index object. |
| 2226 | * |
| 2227 | *---------------------------------------------------------------------- |
| 2228 | */ |
| 2229 | |
| 2230 | int |
| 2231 | TclGetIntForIndex(interp, objPtr, endValue, indexPtr) |
| 2232 | Tcl_Interp *interp; /* Interpreter to use for error reporting. |
| 2233 | * If NULL, then no error message is left |
| 2234 | * after errors. */ |
| 2235 | Tcl_Obj *objPtr; /* Points to an object containing either |
| 2236 | * "end" or an integer. */ |
| 2237 | int endValue; /* The value to be stored at "indexPtr" if |
| 2238 | * "objPtr" holds "end". */ |
| 2239 | int *indexPtr; /* Location filled in with an integer |
| 2240 | * representing an index. */ |
| 2241 | { |
| 2242 | char *bytes; |
| 2243 | int offset; |
| 2244 | Tcl_WideInt wideOffset; |
| 2245 | |
| 2246 | /* |
| 2247 | * If the object is already an integer, use it. |
| 2248 | */ |
| 2249 | |
| 2250 | if (objPtr->typePtr == &tclIntType) { |
| 2251 | *indexPtr = (int)objPtr->internalRep.longValue; |
| 2252 | return TCL_OK; |
| 2253 | } |
| 2254 | |
| 2255 | /* |
| 2256 | * If the object is already a wide-int, and it is not out of range |
| 2257 | * for an integer, use it. [Bug #526717] |
| 2258 | */ |
| 2259 | if (objPtr->typePtr == &tclWideIntType) { |
| 2260 | TclGetWide(wideOffset,objPtr); |
| 2261 | if (wideOffset >= Tcl_LongAsWide(INT_MIN) |
| 2262 | && wideOffset <= Tcl_LongAsWide(INT_MAX)) { |
| 2263 | *indexPtr = (int) Tcl_WideAsLong(wideOffset); |
| 2264 | return TCL_OK; |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | if (SetEndOffsetFromAny(NULL, objPtr) == TCL_OK) { |
| 2269 | /* |
| 2270 | * If the object is already an offset from the end of the |
| 2271 | * list, or can be converted to one, use it. |
| 2272 | */ |
| 2273 | |
| 2274 | *indexPtr = endValue + objPtr->internalRep.longValue; |
| 2275 | |
| 2276 | } else if (Tcl_GetWideIntFromObj(NULL, objPtr, &wideOffset) == TCL_OK) { |
| 2277 | /* |
| 2278 | * If the object can be converted to a wide integer, use |
| 2279 | * that. [Bug #526717] |
| 2280 | */ |
| 2281 | |
| 2282 | offset = (int) Tcl_WideAsLong(wideOffset); |
| 2283 | if (Tcl_LongAsWide(offset) == wideOffset) { |
| 2284 | /* |
| 2285 | * But it is representable as a narrow integer, so we |
| 2286 | * prefer that (so preserving old behaviour in the |
| 2287 | * majority of cases.) |
| 2288 | */ |
| 2289 | objPtr->typePtr = &tclIntType; |
| 2290 | objPtr->internalRep.longValue = offset; |
| 2291 | } |
| 2292 | *indexPtr = offset; |
| 2293 | |
| 2294 | } else { |
| 2295 | /* |
| 2296 | * Report a parse error. |
| 2297 | */ |
| 2298 | |
| 2299 | if (interp != NULL) { |
| 2300 | bytes = Tcl_GetString(objPtr); |
| 2301 | /* |
| 2302 | * The result might not be empty; this resets it which |
| 2303 | * should be both a cheap operation, and of little problem |
| 2304 | * because this is an error-generation path anyway. |
| 2305 | */ |
| 2306 | Tcl_ResetResult(interp); |
| 2307 | Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 2308 | "bad index \"", bytes, |
| 2309 | "\": must be integer or end?-integer?", |
| 2310 | (char *) NULL); |
| 2311 | if (!strncmp(bytes, "end-", 3)) { |
| 2312 | bytes += 3; |
| 2313 | } |
| 2314 | TclCheckBadOctal(interp, bytes); |
| 2315 | } |
| 2316 | |
| 2317 | return TCL_ERROR; |
| 2318 | } |
| 2319 | |
| 2320 | return TCL_OK; |
| 2321 | } |
| 2322 | |
| 2323 | /* |
| 2324 | *---------------------------------------------------------------------- |
| 2325 | * |
| 2326 | * UpdateStringOfEndOffset -- |
| 2327 | * |
| 2328 | * Update the string rep of a Tcl object holding an "end-offset" |
| 2329 | * expression. |
| 2330 | * |
| 2331 | * Results: |
| 2332 | * None. |
| 2333 | * |
| 2334 | * Side effects: |
| 2335 | * Stores a valid string in the object's string rep. |
| 2336 | * |
| 2337 | * This procedure does NOT free any earlier string rep. If it is |
| 2338 | * called on an object that already has a valid string rep, it will |
| 2339 | * leak memory. |
| 2340 | * |
| 2341 | *---------------------------------------------------------------------- |
| 2342 | */ |
| 2343 | |
| 2344 | static void |
| 2345 | UpdateStringOfEndOffset(objPtr) |
| 2346 | register Tcl_Obj* objPtr; |
| 2347 | { |
| 2348 | char buffer[TCL_INTEGER_SPACE + sizeof("end") + 1]; |
| 2349 | register int len; |
| 2350 | |
| 2351 | strcpy(buffer, "end"); |
| 2352 | len = sizeof("end") - 1; |
| 2353 | if (objPtr->internalRep.longValue != 0) { |
| 2354 | buffer[len++] = '-'; |
| 2355 | len += TclFormatInt(buffer+len, -(objPtr->internalRep.longValue)); |
| 2356 | } |
| 2357 | objPtr->bytes = ckalloc((unsigned) (len+1)); |
| 2358 | strcpy(objPtr->bytes, buffer); |
| 2359 | objPtr->length = len; |
| 2360 | } |
| 2361 | |
| 2362 | /* |
| 2363 | *---------------------------------------------------------------------- |
| 2364 | * |
| 2365 | * SetEndOffsetFromAny -- |
| 2366 | * |
| 2367 | * Look for a string of the form "end-offset" and convert it |
| 2368 | * to an internal representation holding the offset. |
| 2369 | * |
| 2370 | * Results: |
| 2371 | * Returns TCL_OK if ok, TCL_ERROR if the string was badly formed. |
| 2372 | * |
| 2373 | * Side effects: |
| 2374 | * If interp is not NULL, stores an error message in the |
| 2375 | * interpreter result. |
| 2376 | * |
| 2377 | *---------------------------------------------------------------------- |
| 2378 | */ |
| 2379 | |
| 2380 | static int |
| 2381 | SetEndOffsetFromAny(interp, objPtr) |
| 2382 | Tcl_Interp* interp; /* Tcl interpreter or NULL */ |
| 2383 | Tcl_Obj* objPtr; /* Pointer to the object to parse */ |
| 2384 | { |
| 2385 | int offset; /* Offset in the "end-offset" expression */ |
| 2386 | Tcl_ObjType* oldTypePtr = objPtr->typePtr; |
| 2387 | /* Old internal rep type of the object */ |
| 2388 | register char* bytes; /* String rep of the object */ |
| 2389 | int length; /* Length of the object's string rep */ |
| 2390 | |
| 2391 | /* If it's already the right type, we're fine. */ |
| 2392 | |
| 2393 | if (objPtr->typePtr == &tclEndOffsetType) { |
| 2394 | return TCL_OK; |
| 2395 | } |
| 2396 | |
| 2397 | /* Check for a string rep of the right form. */ |
| 2398 | |
| 2399 | bytes = Tcl_GetStringFromObj(objPtr, &length); |
| 2400 | if ((*bytes != 'e') || (strncmp(bytes, "end", |
| 2401 | (size_t)((length > 3) ? 3 : length)) != 0)) { |
| 2402 | if (interp != NULL) { |
| 2403 | Tcl_ResetResult(interp); |
| 2404 | Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 2405 | "bad index \"", bytes, |
| 2406 | "\": must be end?-integer?", |
| 2407 | (char*) NULL); |
| 2408 | } |
| 2409 | return TCL_ERROR; |
| 2410 | } |
| 2411 | |
| 2412 | /* Convert the string rep */ |
| 2413 | |
| 2414 | if (length <= 3) { |
| 2415 | offset = 0; |
| 2416 | } else if ((length > 4) && (bytes[3] == '-')) { |
| 2417 | /* |
| 2418 | * This is our limited string expression evaluator. Pass everything |
| 2419 | * after "end-" to Tcl_GetInt, then reverse for offset. |
| 2420 | */ |
| 2421 | if (Tcl_GetInt(interp, bytes+4, &offset) != TCL_OK) { |
| 2422 | return TCL_ERROR; |
| 2423 | } |
| 2424 | offset = -offset; |
| 2425 | } else { |
| 2426 | /* |
| 2427 | * Conversion failed. Report the error. |
| 2428 | */ |
| 2429 | if (interp != NULL) { |
| 2430 | Tcl_ResetResult(interp); |
| 2431 | Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), |
| 2432 | "bad index \"", bytes, |
| 2433 | "\": must be integer or end?-integer?", |
| 2434 | (char *) NULL); |
| 2435 | } |
| 2436 | return TCL_ERROR; |
| 2437 | } |
| 2438 | |
| 2439 | /* |
| 2440 | * The conversion succeeded. Free the old internal rep and set |
| 2441 | * the new one. |
| 2442 | */ |
| 2443 | |
| 2444 | if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) { |
| 2445 | oldTypePtr->freeIntRepProc(objPtr); |
| 2446 | } |
| 2447 | |
| 2448 | objPtr->internalRep.longValue = offset; |
| 2449 | objPtr->typePtr = &tclEndOffsetType; |
| 2450 | |
| 2451 | return TCL_OK; |
| 2452 | } |
| 2453 | |
| 2454 | /* |
| 2455 | *---------------------------------------------------------------------- |
| 2456 | * |
| 2457 | * TclCheckBadOctal -- |
| 2458 | * |
| 2459 | * This procedure checks for a bad octal value and appends a |
| 2460 | * meaningful error to the interp's result. |
| 2461 | * |
| 2462 | * Results: |
| 2463 | * 1 if the argument was a bad octal, else 0. |
| 2464 | * |
| 2465 | * Side effects: |
| 2466 | * The interpreter's result is modified. |
| 2467 | * |
| 2468 | *---------------------------------------------------------------------- |
| 2469 | */ |
| 2470 | |
| 2471 | int |
| 2472 | TclCheckBadOctal(interp, value) |
| 2473 | Tcl_Interp *interp; /* Interpreter to use for error reporting. |
| 2474 | * If NULL, then no error message is left |
| 2475 | * after errors. */ |
| 2476 | CONST char *value; /* String to check. */ |
| 2477 | { |
| 2478 | register CONST char *p = value; |
| 2479 | |
| 2480 | /* |
| 2481 | * A frequent mistake is invalid octal values due to an unwanted |
| 2482 | * leading zero. Try to generate a meaningful error message. |
| 2483 | */ |
| 2484 | |
| 2485 | while (isspace(UCHAR(*p))) { /* INTL: ISO space. */ |
| 2486 | p++; |
| 2487 | } |
| 2488 | if (*p == '+' || *p == '-') { |
| 2489 | p++; |
| 2490 | } |
| 2491 | if (*p == '0') { |
| 2492 | while (isdigit(UCHAR(*p))) { /* INTL: digit. */ |
| 2493 | p++; |
| 2494 | } |
| 2495 | while (isspace(UCHAR(*p))) { /* INTL: ISO space. */ |
| 2496 | p++; |
| 2497 | } |
| 2498 | if (*p == '\0') { |
| 2499 | /* Reached end of string */ |
| 2500 | if (interp != NULL) { |
| 2501 | /* |
| 2502 | * Don't reset the result here because we want this result |
| 2503 | * to be added to an existing error message as extra info. |
| 2504 | */ |
| 2505 | Tcl_AppendResult(interp, " (looks like invalid octal number)", |
| 2506 | (char *) NULL); |
| 2507 | } |
| 2508 | return 1; |
| 2509 | } |
| 2510 | } |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
| 2514 | /* |
| 2515 | *---------------------------------------------------------------------- |
| 2516 | * |
| 2517 | * Tcl_GetNameOfExecutable -- |
| 2518 | * |
| 2519 | * This procedure simply returns a pointer to the internal full |
| 2520 | * path name of the executable file as computed by |
| 2521 | * Tcl_FindExecutable. This procedure call is the C API |
| 2522 | * equivalent to the "info nameofexecutable" command. |
| 2523 | * |
| 2524 | * Results: |
| 2525 | * A pointer to the internal string or NULL if the internal full |
| 2526 | * path name has not been computed or unknown. |
| 2527 | * |
| 2528 | * Side effects: |
| 2529 | * The object referenced by "objPtr" might be converted to an |
| 2530 | * integer object. |
| 2531 | * |
| 2532 | *---------------------------------------------------------------------- |
| 2533 | */ |
| 2534 | |
| 2535 | CONST char * |
| 2536 | Tcl_GetNameOfExecutable() |
| 2537 | { |
| 2538 | return tclExecutableName; |
| 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | *---------------------------------------------------------------------- |
| 2543 | * |
| 2544 | * TclpGetTime -- |
| 2545 | * |
| 2546 | * Deprecated synonym for Tcl_GetTime. |
| 2547 | * |
| 2548 | * Results: |
| 2549 | * None. |
| 2550 | * |
| 2551 | * Side effects: |
| 2552 | * Stores current time in the buffer designated by "timePtr" |
| 2553 | * |
| 2554 | * This procedure is provided for the benefit of extensions written |
| 2555 | * before Tcl_GetTime was exported from the library. |
| 2556 | * |
| 2557 | *---------------------------------------------------------------------- |
| 2558 | */ |
| 2559 | |
| 2560 | void |
| 2561 | TclpGetTime(timePtr) |
| 2562 | Tcl_Time* timePtr; |
| 2563 | { |
| 2564 | Tcl_GetTime(timePtr); |
| 2565 | } |