| 1 |
/* |
| 2 |
* tclUtil.c -- |
| 3 |
* |
| 4 |
* This file contains utility functions 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 of |
| 12 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "tclInt.h" |
| 16 |
#include "tclParse.h" |
| 17 |
#include "tclStringTrim.h" |
| 18 |
#include <math.h> |
| 19 |
|
| 20 |
/* |
| 21 |
* The absolute pathname of the executable in which this Tcl library is |
| 22 |
* running. |
| 23 |
*/ |
| 24 |
|
| 25 |
static ProcessGlobalValue executableName = { |
| 26 |
0, 0, NULL, NULL, NULL, NULL, NULL |
| 27 |
}; |
| 28 |
|
| 29 |
/* |
| 30 |
* The following values are used in the flags arguments of Tcl*Scan*Element |
| 31 |
* and Tcl*Convert*Element. The values TCL_DONT_USE_BRACES and |
| 32 |
* TCL_DONT_QUOTE_HASH are defined in tcl.h, like so: |
| 33 |
* |
| 34 |
#define TCL_DONT_USE_BRACES 1 |
| 35 |
#define TCL_DONT_QUOTE_HASH 8 |
| 36 |
* |
| 37 |
* Those are public flag bits which callers of the public routines |
| 38 |
* Tcl_Convert*Element() can use to indicate: |
| 39 |
* |
| 40 |
* TCL_DONT_USE_BRACES - 1 means the caller is insisting that brace |
| 41 |
* quoting not be used when converting the list |
| 42 |
* element. |
| 43 |
* TCL_DONT_QUOTE_HASH - 1 means the caller insists that a leading hash |
| 44 |
* character ('#') should *not* be quoted. This |
| 45 |
* is appropriate when the caller can guarantee |
| 46 |
* the element is not the first element of a |
| 47 |
* list, so [eval] cannot mis-parse the element |
| 48 |
* as a comment. |
| 49 |
* |
| 50 |
* The remaining values which can be carried by the flags of these routines |
| 51 |
* are for internal use only. Make sure they do not overlap with the public |
| 52 |
* values above. |
| 53 |
* |
| 54 |
* The Tcl*Scan*Element() routines make a determination which of 4 modes of |
| 55 |
* conversion is most appropriate for Tcl*Convert*Element() to perform, and |
| 56 |
* sets two bits of the flags value to indicate the mode selected. |
| 57 |
* |
| 58 |
* CONVERT_NONE The element needs no quoting. Its literal string is |
| 59 |
* suitable as is. |
| 60 |
* CONVERT_BRACE The conversion should be enclosing the literal string |
| 61 |
* in braces. |
| 62 |
* CONVERT_ESCAPE The conversion should be using backslashes to escape |
| 63 |
* any characters in the string that require it. |
| 64 |
* CONVERT_MASK A mask value used to extract the conversion mode from |
| 65 |
* the flags argument. |
| 66 |
* Also indicates a strange conversion mode where all |
| 67 |
* special characters are escaped with backslashes |
| 68 |
* *except for braces*. This is a strange and unnecessary |
| 69 |
* case, but it's part of the historical way in which |
| 70 |
* lists have been formatted in Tcl. To experiment with |
| 71 |
* removing this case, set the value of COMPAT to 0. |
| 72 |
* |
| 73 |
* One last flag value is used only by callers of TclScanElement(). The flag |
| 74 |
* value produced by a call to Tcl*Scan*Element() will never leave this bit |
| 75 |
* set. |
| 76 |
* |
| 77 |
* CONVERT_ANY The caller of TclScanElement() declares it can make no |
| 78 |
* promise about what public flags will be passed to the |
| 79 |
* matching call of TclConvertElement(). As such, |
| 80 |
* TclScanElement() has to determine the worst case |
| 81 |
* destination buffer length over all possibilities, and |
| 82 |
* in other cases this means an overestimate of the |
| 83 |
* required size. |
| 84 |
* |
| 85 |
* For more details, see the comments on the Tcl*Scan*Element and |
| 86 |
* Tcl*Convert*Element routines. |
| 87 |
*/ |
| 88 |
|
| 89 |
#define COMPAT 1 |
| 90 |
#define CONVERT_NONE 0 |
| 91 |
#define CONVERT_BRACE 2 |
| 92 |
#define CONVERT_ESCAPE 4 |
| 93 |
#define CONVERT_MASK (CONVERT_BRACE | CONVERT_ESCAPE) |
| 94 |
#define CONVERT_ANY 16 |
| 95 |
|
| 96 |
/* |
| 97 |
* The following key is used by Tcl_PrintDouble and TclPrecTraceProc to |
| 98 |
* access the precision to be used for double formatting. |
| 99 |
*/ |
| 100 |
|
| 101 |
static Tcl_ThreadDataKey precisionKey; |
| 102 |
|
| 103 |
/* |
| 104 |
* Prototypes for functions defined later in this file. |
| 105 |
*/ |
| 106 |
|
| 107 |
static void ClearHash(Tcl_HashTable *tablePtr); |
| 108 |
static void FreeProcessGlobalValue(ClientData clientData); |
| 109 |
static void FreeThreadHash(ClientData clientData); |
| 110 |
static int GetEndOffsetFromObj(Tcl_Obj *objPtr, int endValue, |
| 111 |
int *indexPtr); |
| 112 |
static Tcl_HashTable * GetThreadHash(Tcl_ThreadDataKey *keyPtr); |
| 113 |
static int SetEndOffsetFromAny(Tcl_Interp *interp, |
| 114 |
Tcl_Obj *objPtr); |
| 115 |
static void UpdateStringOfEndOffset(Tcl_Obj *objPtr); |
| 116 |
static int FindElement(Tcl_Interp *interp, const char *string, |
| 117 |
int stringLength, const char *typeStr, |
| 118 |
const char *typeCode, const char **elementPtr, |
| 119 |
const char **nextPtr, int *sizePtr, |
| 120 |
int *literalPtr); |
| 121 |
/* |
| 122 |
* The following is the Tcl object type definition for an object that |
| 123 |
* represents a list index in the form, "end-offset". It is used as a |
| 124 |
* performance optimization in TclGetIntForIndex. The internal rep is an |
| 125 |
* integer, so no memory management is required for it. |
| 126 |
*/ |
| 127 |
|
| 128 |
const Tcl_ObjType tclEndOffsetType = { |
| 129 |
"end-offset", /* name */ |
| 130 |
NULL, /* freeIntRepProc */ |
| 131 |
NULL, /* dupIntRepProc */ |
| 132 |
UpdateStringOfEndOffset, /* updateStringProc */ |
| 133 |
SetEndOffsetFromAny |
| 134 |
}; |
| 135 |
|
| 136 |
/* |
| 137 |
* * STRING REPRESENTATION OF LISTS * * * |
| 138 |
* |
| 139 |
* The next several routines implement the conversions of strings to and from |
| 140 |
* Tcl lists. To understand their operation, the rules of parsing and |
| 141 |
* generating the string representation of lists must be known. Here we |
| 142 |
* describe them in one place. |
| 143 |
* |
| 144 |
* A list is made up of zero or more elements. Any string is a list if it is |
| 145 |
* made up of alternating substrings of element-separating ASCII whitespace |
| 146 |
* and properly formatted elements. |
| 147 |
* |
| 148 |
* The ASCII characters which can make up the whitespace between list elements |
| 149 |
* are: |
| 150 |
* |
| 151 |
* \u0009 \t TAB |
| 152 |
* \u000A \n NEWLINE |
| 153 |
* \u000B \v VERTICAL TAB |
| 154 |
* \u000C \f FORM FEED |
| 155 |
* \u000D \r CARRIAGE RETURN |
| 156 |
* \u0020 SPACE |
| 157 |
* |
| 158 |
* NOTE: differences between this and other places where Tcl defines a role |
| 159 |
* for "whitespace". |
| 160 |
* |
| 161 |
* * Unlike command parsing, here NEWLINE is just another whitespace |
| 162 |
* character; its role as a command terminator in a script has no |
| 163 |
* importance here. |
| 164 |
* |
| 165 |
* * Unlike command parsing, the BACKSLASH NEWLINE sequence is not |
| 166 |
* considered to be a whitespace character. |
| 167 |
* |
| 168 |
* * Other Unicode whitespace characters (recognized by [string is space] |
| 169 |
* or Tcl_UniCharIsSpace()) do not play any role as element separators |
| 170 |
* in Tcl lists. |
| 171 |
* |
| 172 |
* * The NUL byte ought not appear, as it is not in strings properly |
| 173 |
* encoded for Tcl, but if it is present, it is not treated as |
| 174 |
* separating whitespace, or a string terminator. It is just another |
| 175 |
* character in a list element. |
| 176 |
* |
| 177 |
* The interpretation of a formatted substring as a list element follows rules |
| 178 |
* similar to the parsing of the words of a command in a Tcl script. Backslash |
| 179 |
* substitution plays a key role, and is defined exactly as it is in command |
| 180 |
* parsing. The same routine, TclParseBackslash() is used in both command |
| 181 |
* parsing and list parsing. |
| 182 |
* |
| 183 |
* NOTE: This means that if and when backslash substitution rules ever change |
| 184 |
* for command parsing, the interpretation of strings as lists also changes. |
| 185 |
* |
| 186 |
* Backslash substitution replaces an "escape sequence" of one or more |
| 187 |
* characters starting with |
| 188 |
* \u005c \ BACKSLASH |
| 189 |
* with a single character. The one character escape sequence case happens only |
| 190 |
* when BACKSLASH is the last character in the string. In all other cases, the |
| 191 |
* escape sequence is at least two characters long. |
| 192 |
* |
| 193 |
* The formatted substrings are interpreted as element values according to the |
| 194 |
* following cases: |
| 195 |
* |
| 196 |
* * If the first character of a formatted substring is |
| 197 |
* \u007b { OPEN BRACE |
| 198 |
* then the end of the substring is the matching |
| 199 |
* \u007d } CLOSE BRACE |
| 200 |
* character, where matching is determined by counting nesting levels, and |
| 201 |
* not including any brace characters that are contained within a backslash |
| 202 |
* escape sequence in the nesting count. Having found the matching brace, |
| 203 |
* all characters between the braces are the string value of the element. |
| 204 |
* If no matching close brace is found before the end of the string, the |
| 205 |
* string is not a Tcl list. If the character following the close brace is |
| 206 |
* not an element separating whitespace character, or the end of the string, |
| 207 |
* then the string is not a Tcl list. |
| 208 |
* |
| 209 |
* NOTE: this differs from a brace-quoted word in the parsing of a Tcl |
| 210 |
* command only in its treatment of the backslash-newline sequence. In a |
| 211 |
* list element, the literal characters in the backslash-newline sequence |
| 212 |
* become part of the element value. In a script word, conversion to a |
| 213 |
* single SPACE character is done. |
| 214 |
* |
| 215 |
* NOTE: Most list element values can be represented by a formatted |
| 216 |
* substring using brace quoting. The exceptions are any element value that |
| 217 |
* includes an unbalanced brace not in a backslash escape sequence, and any |
| 218 |
* value that ends with a backslash not itself in a backslash escape |
| 219 |
* sequence. |
| 220 |
* |
| 221 |
* * If the first character of a formatted substring is |
| 222 |
* \u0022 " QUOTE |
| 223 |
* then the end of the substring is the next QUOTE character, not counting |
| 224 |
* any QUOTE characters that are contained within a backslash escape |
| 225 |
* sequence. If no next QUOTE is found before the end of the string, the |
| 226 |
* string is not a Tcl list. If the character following the closing QUOTE is |
| 227 |
* not an element separating whitespace character, or the end of the string, |
| 228 |
* then the string is not a Tcl list. Having found the limits of the |
| 229 |
* substring, the element value is produced by performing backslash |
| 230 |
* substitution on the character sequence between the open and close QUOTEs. |
| 231 |
* |
| 232 |
* NOTE: Any element value can be represented by this style of formatting, |
| 233 |
* given suitable choice of backslash escape sequences. |
| 234 |
* |
| 235 |
* * All other formatted substrings are terminated by the next element |
| 236 |
* separating whitespace character in the string. Having found the limits |
| 237 |
* of the substring, the element value is produced by performing backslash |
| 238 |
* substitution on it. |
| 239 |
* |
| 240 |
* NOTE: Any element value can be represented by this style of formatting, |
| 241 |
* given suitable choice of backslash escape sequences, with one exception. |
| 242 |
* The empty string cannot be represented as a list element without the use |
| 243 |
* of either braces or quotes to delimit it. |
| 244 |
* |
| 245 |
* This collection of parsing rules is implemented in the routine |
| 246 |
* FindElement(). |
| 247 |
* |
| 248 |
* In order to produce lists that can be parsed by these rules, we need the |
| 249 |
* ability to distinguish between characters that are part of a list element |
| 250 |
* value from characters providing syntax that define the structure of the |
| 251 |
* list. This means that our code that generates lists must at a minimum be |
| 252 |
* able to produce escape sequences for the 10 characters identified above |
| 253 |
* that have significance to a list parser. |
| 254 |
* |
| 255 |
* * * CANONICAL LISTS * * * * * |
| 256 |
* |
| 257 |
* In addition to the basic rules for parsing strings into Tcl lists, there |
| 258 |
* are additional properties to be met by the set of list values that are |
| 259 |
* generated by Tcl. Such list values are often said to be in "canonical |
| 260 |
* form": |
| 261 |
* |
| 262 |
* * When any canonical list is evaluated as a Tcl script, it is a script of |
| 263 |
* either zero commands (an empty list) or exactly one command. The command |
| 264 |
* word is exactly the first element of the list, and each argument word is |
| 265 |
* exactly one of the following elements of the list. This means that any |
| 266 |
* characters that have special meaning during script evaluation need |
| 267 |
* special treatment when canonical lists are produced: |
| 268 |
* |
| 269 |
* * Whitespace between elements may not include NEWLINE. |
| 270 |
* * The command terminating character, |
| 271 |
* \u003b ; SEMICOLON |
| 272 |
* must be BRACEd, QUOTEd, or escaped so that it does not terminate the |
| 273 |
* command prematurely. |
| 274 |
* * Any of the characters that begin substitutions in scripts, |
| 275 |
* \u0024 $ DOLLAR |
| 276 |
* \u005b [ OPEN BRACKET |
| 277 |
* \u005c \ BACKSLASH |
| 278 |
* need to be BRACEd or escaped. |
| 279 |
* * In any list where the first character of the first element is |
| 280 |
* \u0023 # HASH |
| 281 |
* that HASH character must be BRACEd, QUOTEd, or escaped so that it |
| 282 |
* does not convert the command into a comment. |
| 283 |
* * Any list element that contains the character sequence BACKSLASH |
| 284 |
* NEWLINE cannot be formatted with BRACEs. The BACKSLASH character |
| 285 |
* must be represented by an escape sequence, and unless QUOTEs are |
| 286 |
* used, the NEWLINE must be as well. |
| 287 |
* |
| 288 |
* * It is also guaranteed that one can use a canonical list as a building |
| 289 |
* block of a larger script within command substitution, as in this example: |
| 290 |
* set script "puts \[[list $cmd $arg]]"; eval $script |
| 291 |
* To support this usage, any appearance of the character |
| 292 |
* \u005d ] CLOSE BRACKET |
| 293 |
* in a list element must be BRACEd, QUOTEd, or escaped. |
| 294 |
* |
| 295 |
* * Finally it is guaranteed that enclosing a canonical list in braces |
| 296 |
* produces a new value that is also a canonical list. This new list has |
| 297 |
* length 1, and its only element is the original canonical list. This same |
| 298 |
* guarantee also makes it possible to construct scripts where an argument |
| 299 |
* word is given a list value by enclosing the canonical form of that list |
| 300 |
* in braces: |
| 301 |
* set script "puts {[list $one $two $three]}"; eval $script |
| 302 |
* This sort of coding was once fairly common, though it's become more |
| 303 |
* idiomatic to see the following instead: |
| 304 |
* set script [list puts [list $one $two $three]]; eval $script |
| 305 |
* In order to support this guarantee, every canonical list must have |
| 306 |
* balance when counting those braces that are not in escape sequences. |
| 307 |
* |
| 308 |
* Within these constraints, the canonical list generation routines |
| 309 |
* TclScanElement() and TclConvertElement() attempt to generate the string for |
| 310 |
* any list that is easiest to read. When an element value is itself |
| 311 |
* acceptable as the formatted substring, it is usually used (CONVERT_NONE). |
| 312 |
* When some quoting or escaping is required, use of BRACEs (CONVERT_BRACE) is |
| 313 |
* usually preferred over the use of escape sequences (CONVERT_ESCAPE). There |
| 314 |
* are some exceptions to both of these preferences for reasons of code |
| 315 |
* simplicity, efficiency, and continuation of historical habits. Canonical |
| 316 |
* lists never use the QUOTE formatting to delimit their elements because that |
| 317 |
* form of quoting does not nest, which makes construction of nested lists far |
| 318 |
* too much trouble. Canonical lists always use only a single SPACE character |
| 319 |
* for element-separating whitespace. |
| 320 |
* |
| 321 |
* * * FUTURE CONSIDERATIONS * * * |
| 322 |
* |
| 323 |
* When a list element requires quoting or escaping due to a CLOSE BRACKET |
| 324 |
* character or an internal QUOTE character, a strange formatting mode is |
| 325 |
* recommended. For example, if the value "a{b]c}d" is converted by the usual |
| 326 |
* modes: |
| 327 |
* |
| 328 |
* CONVERT_BRACE: a{b]c}d => {a{b]c}d} |
| 329 |
* CONVERT_ESCAPE: a{b]c}d => a\{b\]c\}d |
| 330 |
* |
| 331 |
* we get perfectly usable formatted list elements. However, this is not what |
| 332 |
* Tcl releases have been producing. Instead, we have: |
| 333 |
* |
| 334 |
* CONVERT_MASK: a{b]c}d => a{b\]c}d |
| 335 |
* |
| 336 |
* where the CLOSE BRACKET is escaped, but the BRACEs are not. The same effect |
| 337 |
* can be seen replacing ] with " in this example. There does not appear to be |
| 338 |
* any functional or aesthetic purpose for this strange additional mode. The |
| 339 |
* sole purpose I can see for preserving it is to keep generating the same |
| 340 |
* formatted lists programmers have become accustomed to, and perhaps written |
| 341 |
* tests to expect. That is, compatibility only. The additional code |
| 342 |
* complexity required to support this mode is significant. The lines of code |
| 343 |
* supporting it are delimited in the routines below with #if COMPAT |
| 344 |
* directives. This makes it easy to experiment with eliminating this |
| 345 |
* formatting mode simply with "#define COMPAT 0" above. I believe this is |
| 346 |
* worth considering. |
| 347 |
* |
| 348 |
* Another consideration is the treatment of QUOTE characters in list |
| 349 |
* elements. TclConvertElement() must have the ability to produce the escape |
| 350 |
* sequence \" so that when a list element begins with a QUOTE we do not |
| 351 |
* confuse that first character with a QUOTE used as list syntax to define |
| 352 |
* list structure. However, that is the only place where QUOTE characters need |
| 353 |
* quoting. In this way, handling QUOTE could really be much more like the way |
| 354 |
* we handle HASH which also needs quoting and escaping only in particular |
| 355 |
* situations. Following up this could increase the set of list elements that |
| 356 |
* can use the CONVERT_NONE formatting mode. |
| 357 |
* |
| 358 |
* More speculative is that the demands of canonical list form require brace |
| 359 |
* balance for the list as a whole, while the current implementation achieves |
| 360 |
* this by establishing brace balance for every element. |
| 361 |
* |
| 362 |
* Finally, a reminder that the rules for parsing and formatting lists are |
| 363 |
* closely tied together with the rules for parsing and evaluating scripts, |
| 364 |
* and will need to evolve in sync. |
| 365 |
*/ |
| 366 |
|
| 367 |
/* |
| 368 |
*---------------------------------------------------------------------- |
| 369 |
* |
| 370 |
* TclMaxListLength -- |
| 371 |
* |
| 372 |
* Given 'bytes' pointing to 'numBytes' bytes, scan through them and |
| 373 |
* count the number of whitespace runs that could be list element |
| 374 |
* separators. If 'numBytes' is -1, scan to the terminating '\0'. Not a |
| 375 |
* full list parser. Typically used to get a quick and dirty overestimate |
| 376 |
* of length size in order to allocate space for an actual list parser to |
| 377 |
* operate with. |
| 378 |
* |
| 379 |
* Results: |
| 380 |
* Returns the largest number of list elements that could possibly be in |
| 381 |
* this string, interpreted as a Tcl list. If 'endPtr' is not NULL, |
| 382 |
* writes a pointer to the end of the string scanned there. |
| 383 |
* |
| 384 |
* Side effects: |
| 385 |
* None. |
| 386 |
* |
| 387 |
*---------------------------------------------------------------------- |
| 388 |
*/ |
| 389 |
|
| 390 |
int |
| 391 |
TclMaxListLength( |
| 392 |
const char *bytes, |
| 393 |
int numBytes, |
| 394 |
const char **endPtr) |
| 395 |
{ |
| 396 |
int count = 0; |
| 397 |
|
| 398 |
if ((numBytes == 0) || ((numBytes == -1) && (*bytes == '\0'))) { |
| 399 |
/* Empty string case - quick exit */ |
| 400 |
goto done; |
| 401 |
} |
| 402 |
|
| 403 |
/* |
| 404 |
* No list element before leading white space. |
| 405 |
*/ |
| 406 |
|
| 407 |
count += 1 - TclIsSpaceProcM(*bytes); |
| 408 |
|
| 409 |
/* |
| 410 |
* Count white space runs as potential element separators. |
| 411 |
*/ |
| 412 |
|
| 413 |
while (numBytes) { |
| 414 |
if ((numBytes == -1) && (*bytes == '\0')) { |
| 415 |
break; |
| 416 |
} |
| 417 |
if (TclIsSpaceProcM(*bytes)) { |
| 418 |
/* |
| 419 |
* Space run started; bump count. |
| 420 |
*/ |
| 421 |
|
| 422 |
count++; |
| 423 |
do { |
| 424 |
bytes++; |
| 425 |
numBytes -= (numBytes != -1); |
| 426 |
} while (numBytes && TclIsSpaceProcM(*bytes)); |
| 427 |
if ((numBytes == 0) || ((numBytes == -1) && (*bytes == '\0'))) { |
| 428 |
break; |
| 429 |
} |
| 430 |
|
| 431 |
/* |
| 432 |
* (*bytes) is non-space; return to counting state. |
| 433 |
*/ |
| 434 |
} |
| 435 |
bytes++; |
| 436 |
numBytes -= (numBytes != -1); |
| 437 |
} |
| 438 |
|
| 439 |
/* |
| 440 |
* No list element following trailing white space. |
| 441 |
*/ |
| 442 |
|
| 443 |
count -= TclIsSpaceProcM(bytes[-1]); |
| 444 |
|
| 445 |
done: |
| 446 |
if (endPtr) { |
| 447 |
*endPtr = bytes; |
| 448 |
} |
| 449 |
return count; |
| 450 |
} |
| 451 |
|
| 452 |
/* |
| 453 |
*---------------------------------------------------------------------- |
| 454 |
* |
| 455 |
* TclFindElement -- |
| 456 |
* |
| 457 |
* Given a pointer into a Tcl list, locate the first (or next) element in |
| 458 |
* the list. |
| 459 |
* |
| 460 |
* Results: |
| 461 |
* The return value is normally TCL_OK, which means that the element was |
| 462 |
* successfully located. If TCL_ERROR is returned it means that list |
| 463 |
* didn't have proper list structure; the interp's result contains a more |
| 464 |
* detailed error message. |
| 465 |
* |
| 466 |
* If TCL_OK is returned, then *elementPtr will be set to point to the |
| 467 |
* first element of list, and *nextPtr will be set to point to the |
| 468 |
* character just after any white space following the last character |
| 469 |
* that's part of the element. If this is the last argument in the list, |
| 470 |
* then *nextPtr will point just after the last character in the list |
| 471 |
* (i.e., at the character at list+listLength). If sizePtr is non-NULL, |
| 472 |
* *sizePtr is filled in with the number of bytes in the element. If the |
| 473 |
* element is in braces, then *elementPtr will point to the character |
| 474 |
* after the opening brace and *sizePtr will not include either of the |
| 475 |
* braces. If there isn't an element in the list, *sizePtr will be zero, |
| 476 |
* and both *elementPtr and *nextPtr will point just after the last |
| 477 |
* character in the list. If literalPtr is non-NULL, *literalPtr is set |
| 478 |
* to a boolean value indicating whether the substring returned as the |
| 479 |
* values of **elementPtr and *sizePtr is the literal value of a list |
| 480 |
* element. If not, a call to TclCopyAndCollapse() is needed to produce |
| 481 |
* the actual value of the list element. Note: this function does NOT |
| 482 |
* collapse backslash sequences, but uses *literalPtr to tell callers |
| 483 |
* when it is required for them to do so. |
| 484 |
* |
| 485 |
* Side effects: |
| 486 |
* None. |
| 487 |
* |
| 488 |
*---------------------------------------------------------------------- |
| 489 |
*/ |
| 490 |
|
| 491 |
int |
| 492 |
TclFindElement( |
| 493 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. If |
| 494 |
* NULL, then no error message is left after |
| 495 |
* errors. */ |
| 496 |
const char *list, /* Points to the first byte of a string |
| 497 |
* containing a Tcl list with zero or more |
| 498 |
* elements (possibly in braces). */ |
| 499 |
int listLength, /* Number of bytes in the list's string. */ |
| 500 |
const char **elementPtr, /* Where to put address of first significant |
| 501 |
* character in first element of list. */ |
| 502 |
const char **nextPtr, /* Fill in with location of character just |
| 503 |
* after all white space following end of |
| 504 |
* argument (next arg or end of list). */ |
| 505 |
int *sizePtr, /* If non-zero, fill in with size of |
| 506 |
* element. */ |
| 507 |
int *literalPtr) /* If non-zero, fill in with non-zero/zero to |
| 508 |
* indicate that the substring of *sizePtr |
| 509 |
* bytes starting at **elementPtr is/is not |
| 510 |
* the literal list element and therefore |
| 511 |
* does not/does require a call to |
| 512 |
* TclCopyAndCollapse() by the caller. */ |
| 513 |
{ |
| 514 |
return FindElement(interp, list, listLength, "list", "LIST", elementPtr, |
| 515 |
nextPtr, sizePtr, literalPtr); |
| 516 |
} |
| 517 |
|
| 518 |
int |
| 519 |
TclFindDictElement( |
| 520 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. If |
| 521 |
* NULL, then no error message is left after |
| 522 |
* errors. */ |
| 523 |
const char *dict, /* Points to the first byte of a string |
| 524 |
* containing a Tcl dictionary with zero or |
| 525 |
* more keys and values (possibly in |
| 526 |
* braces). */ |
| 527 |
int dictLength, /* Number of bytes in the dict's string. */ |
| 528 |
const char **elementPtr, /* Where to put address of first significant |
| 529 |
* character in the first element (i.e., key |
| 530 |
* or value) of dict. */ |
| 531 |
const char **nextPtr, /* Fill in with location of character just |
| 532 |
* after all white space following end of |
| 533 |
* element (next arg or end of list). */ |
| 534 |
int *sizePtr, /* If non-zero, fill in with size of |
| 535 |
* element. */ |
| 536 |
int *literalPtr) /* If non-zero, fill in with non-zero/zero to |
| 537 |
* indicate that the substring of *sizePtr |
| 538 |
* bytes starting at **elementPtr is/is not |
| 539 |
* the literal key or value and therefore |
| 540 |
* does not/does require a call to |
| 541 |
* TclCopyAndCollapse() by the caller. */ |
| 542 |
{ |
| 543 |
return FindElement(interp, dict, dictLength, "dict", "DICTIONARY", |
| 544 |
elementPtr, nextPtr, sizePtr, literalPtr); |
| 545 |
} |
| 546 |
|
| 547 |
static int |
| 548 |
FindElement( |
| 549 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. If |
| 550 |
* NULL, then no error message is left after |
| 551 |
* errors. */ |
| 552 |
const char *string, /* Points to the first byte of a string |
| 553 |
* containing a Tcl list or dictionary with |
| 554 |
* zero or more elements (possibly in |
| 555 |
* braces). */ |
| 556 |
int stringLength, /* Number of bytes in the string. */ |
| 557 |
const char *typeStr, /* The name of the type of thing we are |
| 558 |
* parsing, for error messages. */ |
| 559 |
const char *typeCode, /* The type code for thing we are parsing, for |
| 560 |
* error messages. */ |
| 561 |
const char **elementPtr, /* Where to put address of first significant |
| 562 |
* character in first element. */ |
| 563 |
const char **nextPtr, /* Fill in with location of character just |
| 564 |
* after all white space following end of |
| 565 |
* argument (next arg or end of list/dict). */ |
| 566 |
int *sizePtr, /* If non-zero, fill in with size of |
| 567 |
* element. */ |
| 568 |
int *literalPtr) /* If non-zero, fill in with non-zero/zero to |
| 569 |
* indicate that the substring of *sizePtr |
| 570 |
* bytes starting at **elementPtr is/is not |
| 571 |
* the literal list/dict element and therefore |
| 572 |
* does not/does require a call to |
| 573 |
* TclCopyAndCollapse() by the caller. */ |
| 574 |
{ |
| 575 |
const char *p = string; |
| 576 |
const char *elemStart; /* Points to first byte of first element. */ |
| 577 |
const char *limit; /* Points just after list/dict's last byte. */ |
| 578 |
int openBraces = 0; /* Brace nesting level during parse. */ |
| 579 |
int inQuotes = 0; |
| 580 |
int size = 0; |
| 581 |
int numChars; |
| 582 |
int literal = 1; |
| 583 |
const char *p2; |
| 584 |
|
| 585 |
/* |
| 586 |
* Skim off leading white space and check for an opening brace or quote. |
| 587 |
* We treat embedded NULLs in the list/dict as bytes belonging to a list |
| 588 |
* element (or dictionary key or value). |
| 589 |
*/ |
| 590 |
|
| 591 |
limit = (string + stringLength); |
| 592 |
while ((p < limit) && (TclIsSpaceProcM(*p))) { |
| 593 |
p++; |
| 594 |
} |
| 595 |
if (p == limit) { /* no element found */ |
| 596 |
elemStart = limit; |
| 597 |
goto done; |
| 598 |
} |
| 599 |
|
| 600 |
if (*p == '{') { |
| 601 |
openBraces = 1; |
| 602 |
p++; |
| 603 |
} else if (*p == '"') { |
| 604 |
inQuotes = 1; |
| 605 |
p++; |
| 606 |
} |
| 607 |
elemStart = p; |
| 608 |
|
| 609 |
/* |
| 610 |
* Find element's end (a space, close brace, or the end of the string). |
| 611 |
*/ |
| 612 |
|
| 613 |
while (p < limit) { |
| 614 |
switch (*p) { |
| 615 |
/* |
| 616 |
* Open brace: don't treat specially unless the element is in |
| 617 |
* braces. In this case, keep a nesting count. |
| 618 |
*/ |
| 619 |
|
| 620 |
case '{': |
| 621 |
if (openBraces != 0) { |
| 622 |
openBraces++; |
| 623 |
} |
| 624 |
break; |
| 625 |
|
| 626 |
/* |
| 627 |
* Close brace: if element is in braces, keep nesting count and |
| 628 |
* quit when the last close brace is seen. |
| 629 |
*/ |
| 630 |
|
| 631 |
case '}': |
| 632 |
if (openBraces > 1) { |
| 633 |
openBraces--; |
| 634 |
} else if (openBraces == 1) { |
| 635 |
size = (p - elemStart); |
| 636 |
p++; |
| 637 |
if ((p >= limit) || TclIsSpaceProcM(*p)) { |
| 638 |
goto done; |
| 639 |
} |
| 640 |
|
| 641 |
/* |
| 642 |
* Garbage after the closing brace; return an error. |
| 643 |
*/ |
| 644 |
|
| 645 |
if (interp != NULL) { |
| 646 |
p2 = p; |
| 647 |
while ((p2 < limit) && (!TclIsSpaceProcM(*p2)) |
| 648 |
&& (p2 < p+20)) { |
| 649 |
p2++; |
| 650 |
} |
| 651 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 652 |
"%s element in braces followed by \"%.*s\" " |
| 653 |
"instead of space", typeStr, (int) (p2-p), p)); |
| 654 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", typeCode, "JUNK", |
| 655 |
(char *)NULL); |
| 656 |
} |
| 657 |
return TCL_ERROR; |
| 658 |
} |
| 659 |
break; |
| 660 |
|
| 661 |
/* |
| 662 |
* Backslash: skip over everything up to the end of the backslash |
| 663 |
* sequence. |
| 664 |
*/ |
| 665 |
|
| 666 |
case '\\': |
| 667 |
if (openBraces == 0) { |
| 668 |
/* |
| 669 |
* A backslash sequence not within a brace quoted element |
| 670 |
* means the value of the element is different from the |
| 671 |
* substring we are parsing. A call to TclCopyAndCollapse() is |
| 672 |
* needed to produce the element value. Inform the caller. |
| 673 |
*/ |
| 674 |
|
| 675 |
literal = 0; |
| 676 |
} |
| 677 |
TclParseBackslash(p, limit - p, &numChars, NULL); |
| 678 |
p += (numChars - 1); |
| 679 |
break; |
| 680 |
|
| 681 |
/* |
| 682 |
* Double-quote: if element is in quotes then terminate it. |
| 683 |
*/ |
| 684 |
|
| 685 |
case '"': |
| 686 |
if (inQuotes) { |
| 687 |
size = (p - elemStart); |
| 688 |
p++; |
| 689 |
if ((p >= limit) || TclIsSpaceProcM(*p)) { |
| 690 |
goto done; |
| 691 |
} |
| 692 |
|
| 693 |
/* |
| 694 |
* Garbage after the closing quote; return an error. |
| 695 |
*/ |
| 696 |
|
| 697 |
if (interp != NULL) { |
| 698 |
p2 = p; |
| 699 |
while ((p2 < limit) && (!TclIsSpaceProcM(*p2)) |
| 700 |
&& (p2 < p+20)) { |
| 701 |
p2++; |
| 702 |
} |
| 703 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 704 |
"%s element in quotes followed by \"%.*s\" " |
| 705 |
"instead of space", typeStr, (int) (p2-p), p)); |
| 706 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", typeCode, "JUNK", |
| 707 |
(char *)NULL); |
| 708 |
} |
| 709 |
return TCL_ERROR; |
| 710 |
} |
| 711 |
break; |
| 712 |
|
| 713 |
default: |
| 714 |
if (TclIsSpaceProcM(*p)) { |
| 715 |
/* |
| 716 |
* Space: ignore if element is in braces or quotes; |
| 717 |
* otherwise terminate element. |
| 718 |
*/ |
| 719 |
if ((openBraces == 0) && !inQuotes) { |
| 720 |
size = (p - elemStart); |
| 721 |
goto done; |
| 722 |
} |
| 723 |
} |
| 724 |
break; |
| 725 |
|
| 726 |
} |
| 727 |
p++; |
| 728 |
} |
| 729 |
|
| 730 |
/* |
| 731 |
* End of list/dict: terminate element. |
| 732 |
*/ |
| 733 |
|
| 734 |
if (p == limit) { |
| 735 |
if (openBraces != 0) { |
| 736 |
if (interp != NULL) { |
| 737 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 738 |
"unmatched open brace in %s", typeStr)); |
| 739 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", typeCode, "BRACE", |
| 740 |
(char *)NULL); |
| 741 |
} |
| 742 |
return TCL_ERROR; |
| 743 |
} else if (inQuotes) { |
| 744 |
if (interp != NULL) { |
| 745 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 746 |
"unmatched open quote in %s", typeStr)); |
| 747 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", typeCode, "QUOTE", |
| 748 |
(char *)NULL); |
| 749 |
} |
| 750 |
return TCL_ERROR; |
| 751 |
} |
| 752 |
size = (p - elemStart); |
| 753 |
} |
| 754 |
|
| 755 |
done: |
| 756 |
while ((p < limit) && (TclIsSpaceProcM(*p))) { |
| 757 |
p++; |
| 758 |
} |
| 759 |
*elementPtr = elemStart; |
| 760 |
*nextPtr = p; |
| 761 |
if (sizePtr != 0) { |
| 762 |
*sizePtr = size; |
| 763 |
} |
| 764 |
if (literalPtr != 0) { |
| 765 |
*literalPtr = literal; |
| 766 |
} |
| 767 |
return TCL_OK; |
| 768 |
} |
| 769 |
|
| 770 |
/* |
| 771 |
*---------------------------------------------------------------------- |
| 772 |
* |
| 773 |
* TclCopyAndCollapse -- |
| 774 |
* |
| 775 |
* Copy a string and substitute all backslash escape sequences |
| 776 |
* |
| 777 |
* Results: |
| 778 |
* Count bytes get copied from src to dst. Along the way, backslash |
| 779 |
* sequences are substituted in the copy. After scanning count bytes from |
| 780 |
* src, a null character is placed at the end of dst. Returns the number |
| 781 |
* of bytes that got written to dst. |
| 782 |
* |
| 783 |
* Side effects: |
| 784 |
* None. |
| 785 |
* |
| 786 |
*---------------------------------------------------------------------- |
| 787 |
*/ |
| 788 |
|
| 789 |
int |
| 790 |
TclCopyAndCollapse( |
| 791 |
int count, /* Number of byte to copy from src. */ |
| 792 |
const char *src, /* Copy from here... */ |
| 793 |
char *dst) /* ... to here. */ |
| 794 |
{ |
| 795 |
int newCount = 0; |
| 796 |
|
| 797 |
while (count > 0) { |
| 798 |
char c = *src; |
| 799 |
|
| 800 |
if (c == '\\') { |
| 801 |
int numRead; |
| 802 |
int backslashCount = TclParseBackslash(src, count, &numRead, dst); |
| 803 |
|
| 804 |
dst += backslashCount; |
| 805 |
newCount += backslashCount; |
| 806 |
src += numRead; |
| 807 |
count -= numRead; |
| 808 |
} else { |
| 809 |
*dst = c; |
| 810 |
dst++; |
| 811 |
newCount++; |
| 812 |
src++; |
| 813 |
count--; |
| 814 |
} |
| 815 |
} |
| 816 |
*dst = 0; |
| 817 |
return newCount; |
| 818 |
} |
| 819 |
|
| 820 |
/* |
| 821 |
*---------------------------------------------------------------------- |
| 822 |
* |
| 823 |
* Tcl_SplitList -- |
| 824 |
* |
| 825 |
* Splits a list up into its constituent fields. |
| 826 |
* |
| 827 |
* Results |
| 828 |
* The return value is normally TCL_OK, which means that the list was |
| 829 |
* successfully split up. If TCL_ERROR is returned, it means that "list" |
| 830 |
* didn't have proper list structure; the interp's result will contain a |
| 831 |
* more detailed error message. |
| 832 |
* |
| 833 |
* *argvPtr will be filled in with the address of an array whose elements |
| 834 |
* point to the elements of list, in order. *argcPtr will get filled in |
| 835 |
* with the number of valid elements in the array. A single block of |
| 836 |
* memory is dynamically allocated to hold both the argv array and a copy |
| 837 |
* of the list (with backslashes and braces removed in the standard way). |
| 838 |
* The caller must eventually free this memory by calling ckfree() on |
| 839 |
* *argvPtr. Note: *argvPtr and *argcPtr are only modified if the |
| 840 |
* function returns normally. |
| 841 |
* |
| 842 |
* Side effects: |
| 843 |
* Memory is allocated. |
| 844 |
* |
| 845 |
*---------------------------------------------------------------------- |
| 846 |
*/ |
| 847 |
|
| 848 |
int |
| 849 |
Tcl_SplitList( |
| 850 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. If |
| 851 |
* NULL, no error message is left. */ |
| 852 |
const char *list, /* Pointer to string with list structure. */ |
| 853 |
int *argcPtr, /* Pointer to location to fill in with the |
| 854 |
* number of elements in the list. */ |
| 855 |
const char ***argvPtr) /* Pointer to place to store pointer to array |
| 856 |
* of pointers to list elements. */ |
| 857 |
{ |
| 858 |
const char **argv, *end, *element; |
| 859 |
char *p; |
| 860 |
int length, size, i, result, elSize; |
| 861 |
|
| 862 |
/* |
| 863 |
* Allocate enough space to work in. A (const char *) for each (possible) |
| 864 |
* list element plus one more for terminating NULL, plus as many bytes as |
| 865 |
* in the original string value, plus one more for a terminating '\0'. |
| 866 |
* Space used to hold element separating white space in the original |
| 867 |
* string gets re-purposed to hold '\0' characters in the argv array. |
| 868 |
*/ |
| 869 |
|
| 870 |
size = TclMaxListLength(list, -1, &end) + 1; |
| 871 |
length = end - list; |
| 872 |
if (size >= (INT_MAX/(int)sizeof(char *)) |
| 873 |
|| length > (INT_MAX - 1 - (size * (int)sizeof(char *)))) { |
| 874 |
if (interp) { |
| 875 |
Tcl_SetResult(interp, |
| 876 |
"memory allocation limit exceeded", TCL_STATIC); |
| 877 |
} |
| 878 |
return TCL_ERROR; |
| 879 |
} |
| 880 |
argv = (const char **)ckalloc((size * sizeof(char *)) + length + 1); |
| 881 |
|
| 882 |
for (i = 0, p = ((char *) argv) + size*sizeof(char *); |
| 883 |
*list != 0; i++) { |
| 884 |
const char *prevList = list; |
| 885 |
int literal; |
| 886 |
|
| 887 |
result = TclFindElement(interp, list, length, &element, &list, |
| 888 |
&elSize, &literal); |
| 889 |
length -= (list - prevList); |
| 890 |
if (result != TCL_OK) { |
| 891 |
ckfree(argv); |
| 892 |
return result; |
| 893 |
} |
| 894 |
if (*element == 0) { |
| 895 |
break; |
| 896 |
} |
| 897 |
if (i >= size) { |
| 898 |
ckfree(argv); |
| 899 |
if (interp != NULL) { |
| 900 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 901 |
"internal error in Tcl_SplitList", -1)); |
| 902 |
Tcl_SetErrorCode(interp, "TCL", "INTERNAL", "Tcl_SplitList", |
| 903 |
(char *)NULL); |
| 904 |
} |
| 905 |
return TCL_ERROR; |
| 906 |
} |
| 907 |
argv[i] = p; |
| 908 |
if (literal) { |
| 909 |
memcpy(p, element, elSize); |
| 910 |
p += elSize; |
| 911 |
*p = 0; |
| 912 |
p++; |
| 913 |
} else { |
| 914 |
p += 1 + TclCopyAndCollapse(elSize, element, p); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
argv[i] = NULL; |
| 919 |
*argvPtr = argv; |
| 920 |
*argcPtr = i; |
| 921 |
return TCL_OK; |
| 922 |
} |
| 923 |
|
| 924 |
/* |
| 925 |
*---------------------------------------------------------------------- |
| 926 |
* |
| 927 |
* Tcl_ScanElement -- |
| 928 |
* |
| 929 |
* This function is a companion function to Tcl_ConvertElement. It scans |
| 930 |
* a string to see what needs to be done to it (e.g. add backslashes or |
| 931 |
* enclosing braces) to make the string into a valid Tcl list element. |
| 932 |
* |
| 933 |
* Results: |
| 934 |
* The return value is an overestimate of the number of bytes that will |
| 935 |
* be needed by Tcl_ConvertElement to produce a valid list element from |
| 936 |
* src. The word at *flagPtr is filled in with a value needed by |
| 937 |
* Tcl_ConvertElement when doing the actual conversion. |
| 938 |
* |
| 939 |
* Side effects: |
| 940 |
* None. |
| 941 |
* |
| 942 |
*---------------------------------------------------------------------- |
| 943 |
*/ |
| 944 |
|
| 945 |
int |
| 946 |
Tcl_ScanElement( |
| 947 |
const char *src, /* String to convert to list element. */ |
| 948 |
int *flagPtr) /* Where to store information to guide |
| 949 |
* Tcl_ConvertCountedElement. */ |
| 950 |
{ |
| 951 |
return Tcl_ScanCountedElement(src, -1, flagPtr); |
| 952 |
} |
| 953 |
|
| 954 |
/* |
| 955 |
*---------------------------------------------------------------------- |
| 956 |
* |
| 957 |
* Tcl_ScanCountedElement -- |
| 958 |
* |
| 959 |
* This function is a companion function to Tcl_ConvertCountedElement. It |
| 960 |
* scans a string to see what needs to be done to it (e.g. add |
| 961 |
* backslashes or enclosing braces) to make the string into a valid Tcl |
| 962 |
* list element. If length is -1, then the string is scanned from src up |
| 963 |
* to the first null byte. |
| 964 |
* |
| 965 |
* Results: |
| 966 |
* The return value is an overestimate of the number of bytes that will |
| 967 |
* be needed by Tcl_ConvertCountedElement to produce a valid list element |
| 968 |
* from src. The word at *flagPtr is filled in with a value needed by |
| 969 |
* Tcl_ConvertCountedElement when doing the actual conversion. |
| 970 |
* |
| 971 |
* Side effects: |
| 972 |
* None. |
| 973 |
* |
| 974 |
*---------------------------------------------------------------------- |
| 975 |
*/ |
| 976 |
|
| 977 |
int |
| 978 |
Tcl_ScanCountedElement( |
| 979 |
const char *src, /* String to convert to Tcl list element. */ |
| 980 |
int length, /* Number of bytes in src, or -1. */ |
| 981 |
int *flagPtr) /* Where to store information to guide |
| 982 |
* Tcl_ConvertElement. */ |
| 983 |
{ |
| 984 |
char flags = CONVERT_ANY; |
| 985 |
int numBytes = TclScanElement(src, length, &flags); |
| 986 |
|
| 987 |
*flagPtr = flags; |
| 988 |
return numBytes; |
| 989 |
} |
| 990 |
|
| 991 |
/* |
| 992 |
*---------------------------------------------------------------------- |
| 993 |
* |
| 994 |
* TclScanElement -- |
| 995 |
* |
| 996 |
* This function is a companion function to TclConvertElement. It scans a |
| 997 |
* string to see what needs to be done to it (e.g. add backslashes or |
| 998 |
* enclosing braces) to make the string into a valid Tcl list element. If |
| 999 |
* length is -1, then the string is scanned from src up to the first null |
| 1000 |
* byte. A NULL value for src is treated as an empty string. The incoming |
| 1001 |
* value of *flagPtr is a report from the caller what additional flags it |
| 1002 |
* will pass to TclConvertElement(). |
| 1003 |
* |
| 1004 |
* Results: |
| 1005 |
* The recommended formatting mode for the element is determined and a |
| 1006 |
* value is written to *flagPtr indicating that recommendation. This |
| 1007 |
* recommendation is combined with the incoming flag values in *flagPtr |
| 1008 |
* set by the caller to determine how many bytes will be needed by |
| 1009 |
* TclConvertElement() in which to write the formatted element following |
| 1010 |
* the recommendation modified by the flag values. This number of bytes |
| 1011 |
* is the return value of the routine. In some situations it may be an |
| 1012 |
* overestimate, but so long as the caller passes the same flags to |
| 1013 |
* TclConvertElement(), it will be large enough. |
| 1014 |
* |
| 1015 |
* Side effects: |
| 1016 |
* None. |
| 1017 |
* |
| 1018 |
*---------------------------------------------------------------------- |
| 1019 |
*/ |
| 1020 |
|
| 1021 |
unsigned int |
| 1022 |
TclScanElement( |
| 1023 |
const char *src, /* String to convert to Tcl list element. */ |
| 1024 |
int length, /* Number of bytes in src, or -1. */ |
| 1025 |
char *flagPtr) /* Where to store information to guide |
| 1026 |
* Tcl_ConvertElement. */ |
| 1027 |
{ |
| 1028 |
const char *p = src; |
| 1029 |
int nestingLevel = 0; /* Brace nesting count */ |
| 1030 |
int forbidNone = 0; /* Do not permit CONVERT_NONE mode. Something |
| 1031 |
* needs protection or escape. */ |
| 1032 |
int requireEscape = 0; /* Force use of CONVERT_ESCAPE mode. For some |
| 1033 |
* reason bare or brace-quoted form fails. */ |
| 1034 |
int extra = 0; /* Count of number of extra bytes needed for |
| 1035 |
* formatted element, assuming we use escape |
| 1036 |
* sequences in formatting. */ |
| 1037 |
unsigned int bytesNeeded; /* Buffer length computed to complete the |
| 1038 |
* element formatting in the selected mode. */ |
| 1039 |
#if COMPAT |
| 1040 |
int preferEscape = 0; /* Use preferences to track whether to use */ |
| 1041 |
int preferBrace = 0; /* CONVERT_MASK mode. */ |
| 1042 |
int braceCount = 0; /* Count of all braces '{' '}' seen. */ |
| 1043 |
#endif /* COMPAT */ |
| 1044 |
|
| 1045 |
if ((p == NULL) || (length == 0) || ((*p == '\0') && (length == -1))) { |
| 1046 |
/* |
| 1047 |
* Empty string element must be brace quoted. |
| 1048 |
*/ |
| 1049 |
|
| 1050 |
*flagPtr = CONVERT_BRACE; |
| 1051 |
return 2; |
| 1052 |
} |
| 1053 |
|
| 1054 |
#if COMPAT |
| 1055 |
/* |
| 1056 |
* We have an established history in TclConvertElement() when quoting |
| 1057 |
* because of a leading hash character to force what would be the |
| 1058 |
* CONVERT_MASK mode into the CONVERT_BRACE mode. That is, we format |
| 1059 |
* the element #{a"b} like this: |
| 1060 |
* {#{a"b}} |
| 1061 |
* and not like this: |
| 1062 |
* \#{a\"b} |
| 1063 |
* This is inconsistent with [list x{a"b}], but we will not change that now. |
| 1064 |
* Set that preference here so that we compute a tight size requirement. |
| 1065 |
*/ |
| 1066 |
if ((*src == '#') && !(*flagPtr & TCL_DONT_QUOTE_HASH)) { |
| 1067 |
preferBrace = 1; |
| 1068 |
} |
| 1069 |
#endif |
| 1070 |
|
| 1071 |
if ((*p == '{') || (*p == '"')) { |
| 1072 |
/* |
| 1073 |
* Must escape or protect so leading character of value is not |
| 1074 |
* misinterpreted as list element delimiting syntax. |
| 1075 |
*/ |
| 1076 |
|
| 1077 |
forbidNone = 1; |
| 1078 |
#if COMPAT |
| 1079 |
preferBrace = 1; |
| 1080 |
#endif /* COMPAT */ |
| 1081 |
} |
| 1082 |
|
| 1083 |
while (length) { |
| 1084 |
if (CHAR_TYPE(*p) != TYPE_NORMAL) { |
| 1085 |
switch (*p) { |
| 1086 |
case '{': /* TYPE_BRACE */ |
| 1087 |
#if COMPAT |
| 1088 |
braceCount++; |
| 1089 |
#endif /* COMPAT */ |
| 1090 |
extra++; /* Escape '{' => '\{' */ |
| 1091 |
nestingLevel++; |
| 1092 |
break; |
| 1093 |
case '}': /* TYPE_BRACE */ |
| 1094 |
#if COMPAT |
| 1095 |
braceCount++; |
| 1096 |
#endif /* COMPAT */ |
| 1097 |
extra++; /* Escape '}' => '\}' */ |
| 1098 |
nestingLevel--; |
| 1099 |
if (nestingLevel < 0) { |
| 1100 |
/* |
| 1101 |
* Unbalanced braces! Cannot format with brace quoting. |
| 1102 |
*/ |
| 1103 |
|
| 1104 |
requireEscape = 1; |
| 1105 |
} |
| 1106 |
break; |
| 1107 |
case ']': /* TYPE_CLOSE_BRACK */ |
| 1108 |
case '"': /* TYPE_SPACE */ |
| 1109 |
#if COMPAT |
| 1110 |
forbidNone = 1; |
| 1111 |
extra++; /* Escapes all just prepend a backslash */ |
| 1112 |
preferEscape = 1; |
| 1113 |
break; |
| 1114 |
#else |
| 1115 |
/* FLOW THROUGH */ |
| 1116 |
#endif /* COMPAT */ |
| 1117 |
case '[': /* TYPE_SUBS */ |
| 1118 |
case '$': /* TYPE_SUBS */ |
| 1119 |
case ';': /* TYPE_COMMAND_END */ |
| 1120 |
forbidNone = 1; |
| 1121 |
extra++; /* Escape sequences all one byte longer. */ |
| 1122 |
#if COMPAT |
| 1123 |
preferBrace = 1; |
| 1124 |
#endif /* COMPAT */ |
| 1125 |
break; |
| 1126 |
case '\\': /* TYPE_SUBS */ |
| 1127 |
extra++; /* Escape '\' => '\\' */ |
| 1128 |
if ((length == 1) || ((length == -1) && (p[1] == '\0'))) { |
| 1129 |
/* |
| 1130 |
* Final backslash. Cannot format with brace quoting. |
| 1131 |
*/ |
| 1132 |
|
| 1133 |
requireEscape = 1; |
| 1134 |
break; |
| 1135 |
} |
| 1136 |
if (p[1] == '\n') { |
| 1137 |
extra++; /* Escape newline => '\n', one byte longer */ |
| 1138 |
|
| 1139 |
/* |
| 1140 |
* Backslash newline sequence. Brace quoting not permitted. |
| 1141 |
*/ |
| 1142 |
|
| 1143 |
requireEscape = 1; |
| 1144 |
length -= (length > 0); |
| 1145 |
p++; |
| 1146 |
break; |
| 1147 |
} |
| 1148 |
if ((p[1] == '{') || (p[1] == '}') || (p[1] == '\\')) { |
| 1149 |
extra++; /* Escape sequences all one byte longer. */ |
| 1150 |
length -= (length > 0); |
| 1151 |
p++; |
| 1152 |
} |
| 1153 |
forbidNone = 1; |
| 1154 |
#if COMPAT |
| 1155 |
preferBrace = 1; |
| 1156 |
#endif /* COMPAT */ |
| 1157 |
break; |
| 1158 |
case '\0': /* TYPE_SUBS */ |
| 1159 |
if (length == -1) { |
| 1160 |
goto endOfString; |
| 1161 |
} |
| 1162 |
/* TODO: Panic on improper encoding? */ |
| 1163 |
break; |
| 1164 |
default: |
| 1165 |
if (TclIsSpaceProcM(*p)) { |
| 1166 |
forbidNone = 1; |
| 1167 |
extra++; /* Escape sequences all one byte longer. */ |
| 1168 |
#if COMPAT |
| 1169 |
preferBrace = 1; |
| 1170 |
#endif |
| 1171 |
} |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
length -= (length > 0); |
| 1176 |
p++; |
| 1177 |
} |
| 1178 |
|
| 1179 |
endOfString: |
| 1180 |
if (nestingLevel != 0) { |
| 1181 |
/* |
| 1182 |
* Unbalanced braces! Cannot format with brace quoting. |
| 1183 |
*/ |
| 1184 |
|
| 1185 |
requireEscape = 1; |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* |
| 1189 |
* We need at least as many bytes as are in the element value... |
| 1190 |
*/ |
| 1191 |
|
| 1192 |
bytesNeeded = p - src; |
| 1193 |
|
| 1194 |
if (requireEscape) { |
| 1195 |
/* |
| 1196 |
* We must use escape sequences. Add all the extra bytes needed to |
| 1197 |
* have room to create them. |
| 1198 |
*/ |
| 1199 |
|
| 1200 |
bytesNeeded += extra; |
| 1201 |
|
| 1202 |
/* |
| 1203 |
* Make room to escape leading #, if needed. |
| 1204 |
*/ |
| 1205 |
|
| 1206 |
if ((*src == '#') && !(*flagPtr & TCL_DONT_QUOTE_HASH)) { |
| 1207 |
bytesNeeded++; |
| 1208 |
} |
| 1209 |
*flagPtr = CONVERT_ESCAPE; |
| 1210 |
goto overflowCheck; |
| 1211 |
} |
| 1212 |
if (*flagPtr & CONVERT_ANY) { |
| 1213 |
/* |
| 1214 |
* The caller has not let us know what flags it will pass to |
| 1215 |
* TclConvertElement() so compute the max size we might need for any |
| 1216 |
* possible choice. Normally the formatting using escape sequences is |
| 1217 |
* the longer one, and a minimum "extra" value of 2 makes sure we |
| 1218 |
* don't request too small a buffer in those edge cases where that's |
| 1219 |
* not true. |
| 1220 |
*/ |
| 1221 |
|
| 1222 |
if (extra < 2) { |
| 1223 |
extra = 2; |
| 1224 |
} |
| 1225 |
*flagPtr &= ~CONVERT_ANY; |
| 1226 |
*flagPtr |= TCL_DONT_USE_BRACES; |
| 1227 |
} |
| 1228 |
if (forbidNone) { |
| 1229 |
/* |
| 1230 |
* We must request some form of quoting of escaping... |
| 1231 |
*/ |
| 1232 |
|
| 1233 |
#if COMPAT |
| 1234 |
if (preferEscape && !preferBrace) { |
| 1235 |
/* |
| 1236 |
* If we are quoting solely due to ] or internal " characters use |
| 1237 |
* the CONVERT_MASK mode where we escape all special characters |
| 1238 |
* except for braces. "extra" counted space needed to escape |
| 1239 |
* braces too, so subtract "braceCount" to get our actual needs. |
| 1240 |
*/ |
| 1241 |
|
| 1242 |
bytesNeeded += (extra - braceCount); |
| 1243 |
/* Make room to escape leading #, if needed. */ |
| 1244 |
if ((*src == '#') && !(*flagPtr & TCL_DONT_QUOTE_HASH)) { |
| 1245 |
bytesNeeded++; |
| 1246 |
} |
| 1247 |
|
| 1248 |
/* |
| 1249 |
* If the caller reports it will direct TclConvertElement() to |
| 1250 |
* use full escapes on the element, add back the bytes needed to |
| 1251 |
* escape the braces. |
| 1252 |
*/ |
| 1253 |
|
| 1254 |
if (*flagPtr & TCL_DONT_USE_BRACES) { |
| 1255 |
bytesNeeded += braceCount; |
| 1256 |
} |
| 1257 |
*flagPtr = CONVERT_MASK; |
| 1258 |
goto overflowCheck; |
| 1259 |
} |
| 1260 |
#endif /* COMPAT */ |
| 1261 |
if (*flagPtr & TCL_DONT_USE_BRACES) { |
| 1262 |
/* |
| 1263 |
* If the caller reports it will direct TclConvertElement() to |
| 1264 |
* use escapes, add the extra bytes needed to have room for them. |
| 1265 |
*/ |
| 1266 |
|
| 1267 |
bytesNeeded += extra; |
| 1268 |
|
| 1269 |
/* |
| 1270 |
* Make room to escape leading #, if needed. |
| 1271 |
*/ |
| 1272 |
|
| 1273 |
if ((*src == '#') && !(*flagPtr & TCL_DONT_QUOTE_HASH)) { |
| 1274 |
bytesNeeded++; |
| 1275 |
} |
| 1276 |
} else { |
| 1277 |
/* |
| 1278 |
* Add 2 bytes for room for the enclosing braces. |
| 1279 |
*/ |
| 1280 |
|
| 1281 |
bytesNeeded += 2; |
| 1282 |
} |
| 1283 |
*flagPtr = CONVERT_BRACE; |
| 1284 |
goto overflowCheck; |
| 1285 |
} |
| 1286 |
|
| 1287 |
/* |
| 1288 |
* So far, no need to quote or escape anything. |
| 1289 |
*/ |
| 1290 |
|
| 1291 |
if ((*src == '#') && !(*flagPtr & TCL_DONT_QUOTE_HASH)) { |
| 1292 |
/* |
| 1293 |
* If we need to quote a leading #, make room to enclose in braces. |
| 1294 |
*/ |
| 1295 |
|
| 1296 |
bytesNeeded += 2; |
| 1297 |
} |
| 1298 |
*flagPtr = CONVERT_NONE; |
| 1299 |
|
| 1300 |
overflowCheck: |
| 1301 |
if (bytesNeeded > INT_MAX) { |
| 1302 |
Tcl_Panic("TclScanElement: string length overflow"); |
| 1303 |
} |
| 1304 |
return bytesNeeded; |
| 1305 |
} |
| 1306 |
|
| 1307 |
/* |
| 1308 |
*---------------------------------------------------------------------- |
| 1309 |
* |
| 1310 |
* Tcl_ConvertElement -- |
| 1311 |
* |
| 1312 |
* This is a companion function to Tcl_ScanElement. Given the information |
| 1313 |
* produced by Tcl_ScanElement, this function converts a string to a list |
| 1314 |
* element equal to that string. |
| 1315 |
* |
| 1316 |
* Results: |
| 1317 |
* Information is copied to *dst in the form of a list element identical |
| 1318 |
* to src (i.e. if Tcl_SplitList is applied to dst it will produce a |
| 1319 |
* string identical to src). The return value is a count of the number of |
| 1320 |
* characters copied (not including the terminating NULL character). |
| 1321 |
* |
| 1322 |
* Side effects: |
| 1323 |
* None. |
| 1324 |
* |
| 1325 |
*---------------------------------------------------------------------- |
| 1326 |
*/ |
| 1327 |
|
| 1328 |
int |
| 1329 |
Tcl_ConvertElement( |
| 1330 |
const char *src, /* Source information for list element. */ |
| 1331 |
char *dst, /* Place to put list-ified element. */ |
| 1332 |
int flags) /* Flags produced by Tcl_ScanElement. */ |
| 1333 |
{ |
| 1334 |
return Tcl_ConvertCountedElement(src, -1, dst, flags); |
| 1335 |
} |
| 1336 |
|
| 1337 |
/* |
| 1338 |
*---------------------------------------------------------------------- |
| 1339 |
* |
| 1340 |
* Tcl_ConvertCountedElement -- |
| 1341 |
* |
| 1342 |
* This is a companion function to Tcl_ScanCountedElement. Given the |
| 1343 |
* information produced by Tcl_ScanCountedElement, this function converts |
| 1344 |
* a string to a list element equal to that string. |
| 1345 |
* |
| 1346 |
* Results: |
| 1347 |
* Information is copied to *dst in the form of a list element identical |
| 1348 |
* to src (i.e. if Tcl_SplitList is applied to dst it will produce a |
| 1349 |
* string identical to src). The return value is a count of the number of |
| 1350 |
* characters copied (not including the terminating NULL character). |
| 1351 |
* |
| 1352 |
* Side effects: |
| 1353 |
* None. |
| 1354 |
* |
| 1355 |
*---------------------------------------------------------------------- |
| 1356 |
*/ |
| 1357 |
|
| 1358 |
int |
| 1359 |
Tcl_ConvertCountedElement( |
| 1360 |
const char *src, /* Source information for list element. */ |
| 1361 |
int length, /* Number of bytes in src, or -1. */ |
| 1362 |
char *dst, /* Place to put list-ified element. */ |
| 1363 |
int flags) /* Flags produced by Tcl_ScanElement. */ |
| 1364 |
{ |
| 1365 |
int numBytes = TclConvertElement(src, length, dst, flags); |
| 1366 |
dst[numBytes] = '\0'; |
| 1367 |
return numBytes; |
| 1368 |
} |
| 1369 |
|
| 1370 |
/* |
| 1371 |
*---------------------------------------------------------------------- |
| 1372 |
* |
| 1373 |
* TclConvertElement -- |
| 1374 |
* |
| 1375 |
* This is a companion function to TclScanElement. Given the information |
| 1376 |
* produced by TclScanElement, this function converts a string to a list |
| 1377 |
* element equal to that string. |
| 1378 |
* |
| 1379 |
* Results: |
| 1380 |
* Information is copied to *dst in the form of a list element identical |
| 1381 |
* to src (i.e. if Tcl_SplitList is applied to dst it will produce a |
| 1382 |
* string identical to src). The return value is a count of the number of |
| 1383 |
* characters copied (not including the terminating NULL character). |
| 1384 |
* |
| 1385 |
* Side effects: |
| 1386 |
* None. |
| 1387 |
* |
| 1388 |
*---------------------------------------------------------------------- |
| 1389 |
*/ |
| 1390 |
|
| 1391 |
int |
| 1392 |
TclConvertElement( |
| 1393 |
const char *src, /* Source information for list element. */ |
| 1394 |
int length, /* Number of bytes in src, or -1. */ |
| 1395 |
char *dst, /* Place to put list-ified element. */ |
| 1396 |
int flags) /* Flags produced by Tcl_ScanElement. */ |
| 1397 |
{ |
| 1398 |
int conversion = flags & CONVERT_MASK; |
| 1399 |
char *p = dst; |
| 1400 |
|
| 1401 |
/* |
| 1402 |
* Let the caller demand we use escape sequences rather than braces. |
| 1403 |
*/ |
| 1404 |
|
| 1405 |
if ((flags & TCL_DONT_USE_BRACES) && (conversion & CONVERT_BRACE)) { |
| 1406 |
conversion = CONVERT_ESCAPE; |
| 1407 |
} |
| 1408 |
|
| 1409 |
/* |
| 1410 |
* No matter what the caller demands, empty string must be braced! |
| 1411 |
*/ |
| 1412 |
|
| 1413 |
if ((src == NULL) || (length == 0) || (*src == '\0' && length == -1)) { |
| 1414 |
src = tclEmptyStringRep; |
| 1415 |
length = 0; |
| 1416 |
conversion = CONVERT_BRACE; |
| 1417 |
} |
| 1418 |
|
| 1419 |
/* |
| 1420 |
* Escape leading hash as needed and requested. |
| 1421 |
*/ |
| 1422 |
|
| 1423 |
if ((*src == '#') && !(flags & TCL_DONT_QUOTE_HASH)) { |
| 1424 |
if (conversion == CONVERT_ESCAPE) { |
| 1425 |
p[0] = '\\'; |
| 1426 |
p[1] = '#'; |
| 1427 |
p += 2; |
| 1428 |
src++; |
| 1429 |
length -= (length > 0); |
| 1430 |
} else { |
| 1431 |
conversion = CONVERT_BRACE; |
| 1432 |
} |
| 1433 |
} |
| 1434 |
|
| 1435 |
/* |
| 1436 |
* No escape or quoting needed. Copy the literal string value. |
| 1437 |
*/ |
| 1438 |
|
| 1439 |
if (conversion == CONVERT_NONE) { |
| 1440 |
if (length == -1) { |
| 1441 |
/* TODO: INT_MAX overflow? */ |
| 1442 |
while (*src) { |
| 1443 |
*p++ = *src++; |
| 1444 |
} |
| 1445 |
return p - dst; |
| 1446 |
} else { |
| 1447 |
memcpy(dst, src, length); |
| 1448 |
return length; |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
/* |
| 1453 |
* Formatted string is original string enclosed in braces. |
| 1454 |
*/ |
| 1455 |
|
| 1456 |
if (conversion == CONVERT_BRACE) { |
| 1457 |
*p = '{'; |
| 1458 |
p++; |
| 1459 |
if (length == -1) { |
| 1460 |
/* TODO: INT_MAX overflow? */ |
| 1461 |
while (*src) { |
| 1462 |
*p++ = *src++; |
| 1463 |
} |
| 1464 |
} else { |
| 1465 |
memcpy(p, src, length); |
| 1466 |
p += length; |
| 1467 |
} |
| 1468 |
*p = '}'; |
| 1469 |
p++; |
| 1470 |
return p - dst; |
| 1471 |
} |
| 1472 |
|
| 1473 |
/* conversion == CONVERT_ESCAPE or CONVERT_MASK */ |
| 1474 |
|
| 1475 |
/* |
| 1476 |
* Formatted string is original string converted to escape sequences. |
| 1477 |
*/ |
| 1478 |
|
| 1479 |
for ( ; length; src++, length -= (length > 0)) { |
| 1480 |
switch (*src) { |
| 1481 |
case ']': |
| 1482 |
case '[': |
| 1483 |
case '$': |
| 1484 |
case ';': |
| 1485 |
case ' ': |
| 1486 |
case '\\': |
| 1487 |
case '"': |
| 1488 |
*p = '\\'; |
| 1489 |
p++; |
| 1490 |
break; |
| 1491 |
case '{': |
| 1492 |
case '}': |
| 1493 |
#if COMPAT |
| 1494 |
if (conversion == CONVERT_ESCAPE) |
| 1495 |
#endif /* COMPAT */ |
| 1496 |
{ |
| 1497 |
*p = '\\'; |
| 1498 |
p++; |
| 1499 |
} |
| 1500 |
break; |
| 1501 |
case '\f': |
| 1502 |
*p = '\\'; |
| 1503 |
p++; |
| 1504 |
*p = 'f'; |
| 1505 |
p++; |
| 1506 |
continue; |
| 1507 |
case '\n': |
| 1508 |
*p = '\\'; |
| 1509 |
p++; |
| 1510 |
*p = 'n'; |
| 1511 |
p++; |
| 1512 |
continue; |
| 1513 |
case '\r': |
| 1514 |
*p = '\\'; |
| 1515 |
p++; |
| 1516 |
*p = 'r'; |
| 1517 |
p++; |
| 1518 |
continue; |
| 1519 |
case '\t': |
| 1520 |
*p = '\\'; |
| 1521 |
p++; |
| 1522 |
*p = 't'; |
| 1523 |
p++; |
| 1524 |
continue; |
| 1525 |
case '\v': |
| 1526 |
*p = '\\'; |
| 1527 |
p++; |
| 1528 |
*p = 'v'; |
| 1529 |
p++; |
| 1530 |
continue; |
| 1531 |
case '\0': |
| 1532 |
if (length == -1) { |
| 1533 |
return p - dst; |
| 1534 |
} |
| 1535 |
|
| 1536 |
/* |
| 1537 |
* If we reach this point, there's an embedded NULL in the string |
| 1538 |
* range being processed, which should not happen when the |
| 1539 |
* encoding rules for Tcl strings are properly followed. If the |
| 1540 |
* day ever comes when we stop tolerating such things, this is |
| 1541 |
* where to put the Tcl_Panic(). |
| 1542 |
*/ |
| 1543 |
|
| 1544 |
break; |
| 1545 |
} |
| 1546 |
*p = *src; |
| 1547 |
p++; |
| 1548 |
} |
| 1549 |
return p - dst; |
| 1550 |
} |
| 1551 |
|
| 1552 |
/* |
| 1553 |
*---------------------------------------------------------------------- |
| 1554 |
* |
| 1555 |
* Tcl_Merge -- |
| 1556 |
* |
| 1557 |
* Given a collection of strings, merge them together into a single |
| 1558 |
* string that has proper Tcl list structured (i.e. Tcl_SplitList may be |
| 1559 |
* used to retrieve strings equal to the original elements, and Tcl_Eval |
| 1560 |
* will parse the string back into its original elements). |
| 1561 |
* |
| 1562 |
* Results: |
| 1563 |
* The return value is the address of a dynamically-allocated string |
| 1564 |
* containing the merged list. |
| 1565 |
* |
| 1566 |
* Side effects: |
| 1567 |
* None. |
| 1568 |
* |
| 1569 |
*---------------------------------------------------------------------- |
| 1570 |
*/ |
| 1571 |
|
| 1572 |
char * |
| 1573 |
Tcl_Merge( |
| 1574 |
int argc, /* How many strings to merge. */ |
| 1575 |
const char *const *argv) /* Array of string values. */ |
| 1576 |
{ |
| 1577 |
#define LOCAL_SIZE 64 |
| 1578 |
char localFlags[LOCAL_SIZE], *flagPtr = NULL; |
| 1579 |
int i; |
| 1580 |
unsigned int bytesNeeded = 0; |
| 1581 |
char *result, *dst; |
| 1582 |
|
| 1583 |
/* |
| 1584 |
* Handle empty list case first, so logic of the general case can be |
| 1585 |
* simpler. |
| 1586 |
*/ |
| 1587 |
|
| 1588 |
if (argc <= 0) { |
| 1589 |
if (argc < 0) { |
| 1590 |
Tcl_Panic("Tcl_Merge called with negative argc (%d)", argc); |
| 1591 |
} |
| 1592 |
result = (char *)ckalloc(1); |
| 1593 |
result[0] = '\0'; |
| 1594 |
return result; |
| 1595 |
} |
| 1596 |
|
| 1597 |
/* |
| 1598 |
* Pass 1: estimate space, gather flags. |
| 1599 |
*/ |
| 1600 |
|
| 1601 |
if (argc <= LOCAL_SIZE) { |
| 1602 |
flagPtr = localFlags; |
| 1603 |
} else { |
| 1604 |
flagPtr = (char *)ckalloc(argc); |
| 1605 |
} |
| 1606 |
for (i = 0; i < argc; i++) { |
| 1607 |
flagPtr[i] = ( i ? TCL_DONT_QUOTE_HASH : 0 ); |
| 1608 |
bytesNeeded += TclScanElement(argv[i], -1, &flagPtr[i]); |
| 1609 |
if (bytesNeeded > INT_MAX) { |
| 1610 |
Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX); |
| 1611 |
} |
| 1612 |
} |
| 1613 |
if (bytesNeeded + argc > INT_MAX + 1U) { |
| 1614 |
Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX); |
| 1615 |
} |
| 1616 |
bytesNeeded += argc; |
| 1617 |
|
| 1618 |
/* |
| 1619 |
* Pass two: copy into the result area. |
| 1620 |
*/ |
| 1621 |
|
| 1622 |
result = (char *)ckalloc(bytesNeeded); |
| 1623 |
dst = result; |
| 1624 |
for (i = 0; i < argc; i++) { |
| 1625 |
flagPtr[i] |= ( i ? TCL_DONT_QUOTE_HASH : 0 ); |
| 1626 |
dst += TclConvertElement(argv[i], -1, dst, flagPtr[i]); |
| 1627 |
*dst = ' '; |
| 1628 |
dst++; |
| 1629 |
} |
| 1630 |
dst[-1] = 0; |
| 1631 |
|
| 1632 |
if (flagPtr != localFlags) { |
| 1633 |
ckfree(flagPtr); |
| 1634 |
} |
| 1635 |
return result; |
| 1636 |
} |
| 1637 |
|
| 1638 |
/* |
| 1639 |
*---------------------------------------------------------------------- |
| 1640 |
* |
| 1641 |
* Tcl_Backslash -- |
| 1642 |
* |
| 1643 |
* Figure out how to handle a backslash sequence. |
| 1644 |
* |
| 1645 |
* Results: |
| 1646 |
* The return value is the character that should be substituted in place |
| 1647 |
* of the backslash sequence that starts at src. If readPtr isn't NULL |
| 1648 |
* then it is filled in with a count of the number of characters in the |
| 1649 |
* backslash sequence. |
| 1650 |
* |
| 1651 |
* Side effects: |
| 1652 |
* None. |
| 1653 |
* |
| 1654 |
*---------------------------------------------------------------------- |
| 1655 |
*/ |
| 1656 |
|
| 1657 |
char |
| 1658 |
Tcl_Backslash( |
| 1659 |
const char *src, /* Points to the backslash character of a |
| 1660 |
* backslash sequence. */ |
| 1661 |
int *readPtr) /* Fill in with number of characters read from |
| 1662 |
* src, unless NULL. */ |
| 1663 |
{ |
| 1664 |
char buf[4] = ""; |
| 1665 |
Tcl_UniChar ch = 0; |
| 1666 |
|
| 1667 |
Tcl_UtfBackslash(src, readPtr, buf); |
| 1668 |
TclUtfToUniChar(buf, &ch); |
| 1669 |
return (char) ch; |
| 1670 |
} |
| 1671 |
|
| 1672 |
/* |
| 1673 |
*---------------------------------------------------------------------- |
| 1674 |
* |
| 1675 |
* TclTrimRight -- |
| 1676 |
* Takes two counted strings in the Tcl encoding. Conceptually |
| 1677 |
* finds the sub string (offset) to trim from the right side of the |
| 1678 |
* first string all characters found in the second string. |
| 1679 |
* |
| 1680 |
* Results: |
| 1681 |
* The number of bytes to be removed from the end of the string. |
| 1682 |
* |
| 1683 |
* Side effects: |
| 1684 |
* None. |
| 1685 |
* |
| 1686 |
*---------------------------------------------------------------------- |
| 1687 |
*/ |
| 1688 |
|
| 1689 |
int |
| 1690 |
TclTrimRight( |
| 1691 |
const char *bytes, /* String to be trimmed... */ |
| 1692 |
int numBytes, /* ...and its length in bytes */ |
| 1693 |
/* Calls to TclUtfToUniChar() in this routine |
| 1694 |
* rely on (bytes[numBytes] == '\0'). */ |
| 1695 |
const char *trim, /* String of trim characters... */ |
| 1696 |
int numTrim) /* ...and its length in bytes */ |
| 1697 |
/* Calls to TclUtfToUniChar() in this routine |
| 1698 |
* rely on (trim[numTrim] == '\0'). */ |
| 1699 |
{ |
| 1700 |
const char *pp, *p = bytes + numBytes; |
| 1701 |
int ch1, ch2; |
| 1702 |
|
| 1703 |
/* Empty strings -> nothing to do */ |
| 1704 |
if ((numBytes == 0) || (numTrim == 0)) { |
| 1705 |
return 0; |
| 1706 |
} |
| 1707 |
|
| 1708 |
/* |
| 1709 |
* Outer loop: iterate over string to be trimmed. |
| 1710 |
*/ |
| 1711 |
|
| 1712 |
do { |
| 1713 |
const char *q = trim; |
| 1714 |
int pInc = 0, bytesLeft = numTrim; |
| 1715 |
|
| 1716 |
pp = TclUtfPrev(p, bytes); |
| 1717 |
#if TCL_UTF_MAX < 4 |
| 1718 |
pp = TclUtfPrev(pp, bytes); |
| 1719 |
#endif |
| 1720 |
do { |
| 1721 |
pp += pInc; |
| 1722 |
pInc = TclUtfToUCS4(pp, &ch1); |
| 1723 |
} while (pp + pInc < p); |
| 1724 |
|
| 1725 |
/* |
| 1726 |
* Inner loop: scan trim string for match to current character. |
| 1727 |
*/ |
| 1728 |
|
| 1729 |
do { |
| 1730 |
pInc = TclUtfToUCS4(q, &ch2); |
| 1731 |
|
| 1732 |
if (ch1 == ch2) { |
| 1733 |
break; |
| 1734 |
} |
| 1735 |
|
| 1736 |
q += pInc; |
| 1737 |
bytesLeft -= pInc; |
| 1738 |
} while (bytesLeft); |
| 1739 |
|
| 1740 |
if (bytesLeft == 0) { |
| 1741 |
/* |
| 1742 |
* No match; trim task done; *p is last non-trimmed char. |
| 1743 |
*/ |
| 1744 |
|
| 1745 |
break; |
| 1746 |
} |
| 1747 |
p = pp; |
| 1748 |
} while (p > bytes); |
| 1749 |
|
| 1750 |
return numBytes - (p - bytes); |
| 1751 |
} |
| 1752 |
|
| 1753 |
/* |
| 1754 |
*---------------------------------------------------------------------- |
| 1755 |
* |
| 1756 |
* TclTrimLeft -- |
| 1757 |
* |
| 1758 |
* Takes two counted strings in the Tcl encoding. Conceptually |
| 1759 |
* finds the sub string (offset) to trim from the left side of the |
| 1760 |
* first string all characters found in the second string. |
| 1761 |
* |
| 1762 |
* Results: |
| 1763 |
* The number of bytes to be removed from the start of the string. |
| 1764 |
* |
| 1765 |
* Side effects: |
| 1766 |
* None. |
| 1767 |
* |
| 1768 |
*---------------------------------------------------------------------- |
| 1769 |
*/ |
| 1770 |
|
| 1771 |
int |
| 1772 |
TclTrimLeft( |
| 1773 |
const char *bytes, /* String to be trimmed... */ |
| 1774 |
int numBytes, /* ...and its length in bytes */ |
| 1775 |
/* Calls to TclUtfToUniChar() in this routine |
| 1776 |
* rely on (bytes[numBytes] == '\0'). */ |
| 1777 |
const char *trim, /* String of trim characters... */ |
| 1778 |
int numTrim) /* ...and its length in bytes */ |
| 1779 |
/* Calls to TclUtfToUniChar() in this routine |
| 1780 |
* rely on (trim[numTrim] == '\0'). */ |
| 1781 |
{ |
| 1782 |
const char *p = bytes; |
| 1783 |
int ch1, ch2; |
| 1784 |
|
| 1785 |
/* Empty strings -> nothing to do */ |
| 1786 |
if ((numBytes == 0) || (numTrim == 0)) { |
| 1787 |
return 0; |
| 1788 |
} |
| 1789 |
|
| 1790 |
/* |
| 1791 |
* Outer loop: iterate over string to be trimmed. |
| 1792 |
*/ |
| 1793 |
|
| 1794 |
do { |
| 1795 |
int pInc = TclUtfToUCS4(p, &ch1); |
| 1796 |
const char *q = trim; |
| 1797 |
int bytesLeft = numTrim; |
| 1798 |
|
| 1799 |
/* |
| 1800 |
* Inner loop: scan trim string for match to current character. |
| 1801 |
*/ |
| 1802 |
|
| 1803 |
do { |
| 1804 |
int qInc = TclUtfToUCS4(q, &ch2); |
| 1805 |
|
| 1806 |
if (ch1 == ch2) { |
| 1807 |
break; |
| 1808 |
} |
| 1809 |
|
| 1810 |
q += qInc; |
| 1811 |
bytesLeft -= qInc; |
| 1812 |
} while (bytesLeft); |
| 1813 |
|
| 1814 |
if (bytesLeft == 0) { |
| 1815 |
/* |
| 1816 |
* No match; trim task done; *p is first non-trimmed char. |
| 1817 |
*/ |
| 1818 |
|
| 1819 |
break; |
| 1820 |
} |
| 1821 |
|
| 1822 |
p += pInc; |
| 1823 |
numBytes -= pInc; |
| 1824 |
} while (numBytes > 0); |
| 1825 |
|
| 1826 |
return p - bytes; |
| 1827 |
} |
| 1828 |
|
| 1829 |
/* |
| 1830 |
*---------------------------------------------------------------------- |
| 1831 |
* |
| 1832 |
* TclTrim -- |
| 1833 |
* Finds the sub string (offset) to trim from both sides of the |
| 1834 |
* first string all characters found in the second string. |
| 1835 |
* |
| 1836 |
* Results: |
| 1837 |
* The number of bytes to be removed from the start of the string |
| 1838 |
* |
| 1839 |
* Side effects: |
| 1840 |
* None. |
| 1841 |
* |
| 1842 |
*---------------------------------------------------------------------- |
| 1843 |
*/ |
| 1844 |
|
| 1845 |
int |
| 1846 |
TclTrim( |
| 1847 |
const char *bytes, /* String to be trimmed... */ |
| 1848 |
int numBytes, /* ...and its length in bytes */ |
| 1849 |
/* Calls in this routine |
| 1850 |
* rely on (bytes[numBytes] == '\0'). */ |
| 1851 |
const char *trim, /* String of trim characters... */ |
| 1852 |
int numTrim, /* ...and its length in bytes */ |
| 1853 |
/* Calls in this routine |
| 1854 |
* rely on (trim[numTrim] == '\0'). */ |
| 1855 |
int *trimRightPtr) /* Offset from the end of the string. */ |
| 1856 |
{ |
| 1857 |
int trimLeft = 0, trimRight = 0; |
| 1858 |
|
| 1859 |
/* Empty strings -> nothing to do */ |
| 1860 |
if ((numBytes > 0) && (numTrim > 0)) { |
| 1861 |
|
| 1862 |
/* When bytes is NUL-terminated, returns 0 <= trimLeft <= numBytes */ |
| 1863 |
trimLeft = TclTrimLeft(bytes, numBytes, trim, numTrim); |
| 1864 |
numBytes -= trimLeft; |
| 1865 |
|
| 1866 |
/* If we did not trim the whole string, it starts with a character |
| 1867 |
* that we will not trim. Skip over it. */ |
| 1868 |
if (numBytes > 0) { |
| 1869 |
int ch; |
| 1870 |
const char *first = bytes + trimLeft; |
| 1871 |
int pInc = TclUtfToUCS4(first, &ch); |
| 1872 |
first += pInc; |
| 1873 |
numBytes -= pInc; |
| 1874 |
|
| 1875 |
if (numBytes > 0) { |
| 1876 |
/* When bytes is NUL-terminated, returns |
| 1877 |
* 0 <= trimRight <= numBytes */ |
| 1878 |
trimRight = TclTrimRight(first, numBytes, trim, numTrim); |
| 1879 |
} |
| 1880 |
} |
| 1881 |
} |
| 1882 |
*trimRightPtr = trimRight; |
| 1883 |
return trimLeft; |
| 1884 |
} |
| 1885 |
|
| 1886 |
/* |
| 1887 |
*---------------------------------------------------------------------- |
| 1888 |
* |
| 1889 |
* Tcl_Concat -- |
| 1890 |
* |
| 1891 |
* Concatenate a set of strings into a single large string. |
| 1892 |
* |
| 1893 |
* Results: |
| 1894 |
* The return value is dynamically-allocated string containing a |
| 1895 |
* concatenation of all the strings in argv, with spaces between the |
| 1896 |
* original argv elements. |
| 1897 |
* |
| 1898 |
* Side effects: |
| 1899 |
* Memory is allocated for the result; the caller is responsible for |
| 1900 |
* freeing the memory. |
| 1901 |
* |
| 1902 |
*---------------------------------------------------------------------- |
| 1903 |
*/ |
| 1904 |
|
| 1905 |
/* The whitespace characters trimmed during [concat] operations */ |
| 1906 |
#define CONCAT_WS_SIZE (int) (sizeof(CONCAT_TRIM_SET "") - 1) |
| 1907 |
|
| 1908 |
char * |
| 1909 |
Tcl_Concat( |
| 1910 |
int argc, /* Number of strings to concatenate. */ |
| 1911 |
const char *const *argv) /* Array of strings to concatenate. */ |
| 1912 |
{ |
| 1913 |
int i, needSpace = 0, bytesNeeded = 0; |
| 1914 |
char *result, *p; |
| 1915 |
|
| 1916 |
/* |
| 1917 |
* Dispose of the empty result corner case first to simplify later code. |
| 1918 |
*/ |
| 1919 |
|
| 1920 |
if (argc == 0) { |
| 1921 |
result = (char *)ckalloc(1); |
| 1922 |
result[0] = '\0'; |
| 1923 |
return result; |
| 1924 |
} |
| 1925 |
|
| 1926 |
/* |
| 1927 |
* First allocate the result buffer at the size required. |
| 1928 |
*/ |
| 1929 |
|
| 1930 |
for (i = 0; i < argc; i++) { |
| 1931 |
bytesNeeded += strlen(argv[i]); |
| 1932 |
if (bytesNeeded < 0) { |
| 1933 |
Tcl_Panic("Tcl_Concat: max size of Tcl value exceeded"); |
| 1934 |
} |
| 1935 |
} |
| 1936 |
if (bytesNeeded + argc - 1 < 0) { |
| 1937 |
/* |
| 1938 |
* Panic test could be tighter, but not going to bother for this |
| 1939 |
* legacy routine. |
| 1940 |
*/ |
| 1941 |
|
| 1942 |
Tcl_Panic("Tcl_Concat: max size of Tcl value exceeded"); |
| 1943 |
} |
| 1944 |
|
| 1945 |
/* |
| 1946 |
* All element bytes + (argc - 1) spaces + 1 terminating NULL. |
| 1947 |
*/ |
| 1948 |
|
| 1949 |
result = (char *)ckalloc(bytesNeeded + argc); |
| 1950 |
|
| 1951 |
for (p = result, i = 0; i < argc; i++) { |
| 1952 |
int triml, trimr, elemLength; |
| 1953 |
const char *element; |
| 1954 |
|
| 1955 |
element = argv[i]; |
| 1956 |
elemLength = strlen(argv[i]); |
| 1957 |
|
| 1958 |
/* Trim away the leading/trailing whitespace. */ |
| 1959 |
triml = TclTrim(element, elemLength, CONCAT_TRIM_SET, |
| 1960 |
CONCAT_WS_SIZE, &trimr); |
| 1961 |
element += triml; |
| 1962 |
elemLength -= triml + trimr; |
| 1963 |
|
| 1964 |
/* Do not permit trimming to expose a final backslash character. */ |
| 1965 |
elemLength += trimr && (element[elemLength - 1] == '\\'); |
| 1966 |
|
| 1967 |
/* |
| 1968 |
* If we're left with empty element after trimming, do nothing. |
| 1969 |
*/ |
| 1970 |
|
| 1971 |
if (elemLength == 0) { |
| 1972 |
continue; |
| 1973 |
} |
| 1974 |
|
| 1975 |
/* |
| 1976 |
* Append to the result with space if needed. |
| 1977 |
*/ |
| 1978 |
|
| 1979 |
if (needSpace) { |
| 1980 |
*p++ = ' '; |
| 1981 |
} |
| 1982 |
memcpy(p, element, elemLength); |
| 1983 |
p += elemLength; |
| 1984 |
needSpace = 1; |
| 1985 |
} |
| 1986 |
*p = '\0'; |
| 1987 |
return result; |
| 1988 |
} |
| 1989 |
|
| 1990 |
/* |
| 1991 |
*---------------------------------------------------------------------- |
| 1992 |
* |
| 1993 |
* Tcl_ConcatObj -- |
| 1994 |
* |
| 1995 |
* Concatenate the strings from a set of objects into a single string |
| 1996 |
* object with spaces between the original strings. |
| 1997 |
* |
| 1998 |
* Results: |
| 1999 |
* The return value is a new string object containing a concatenation of |
| 2000 |
* the strings in objv. Its ref count is zero. |
| 2001 |
* |
| 2002 |
* Side effects: |
| 2003 |
* A new object is created. |
| 2004 |
* |
| 2005 |
*---------------------------------------------------------------------- |
| 2006 |
*/ |
| 2007 |
|
| 2008 |
Tcl_Obj * |
| 2009 |
Tcl_ConcatObj( |
| 2010 |
int objc, /* Number of objects to concatenate. */ |
| 2011 |
Tcl_Obj *const objv[]) /* Array of objects to concatenate. */ |
| 2012 |
{ |
| 2013 |
int i, elemLength, needSpace = 0, bytesNeeded = 0; |
| 2014 |
const char *element; |
| 2015 |
Tcl_Obj *objPtr, *resPtr; |
| 2016 |
|
| 2017 |
/* |
| 2018 |
* Check first to see if all the items are of list type or empty. If so, |
| 2019 |
* we will concat them together as lists, and return a list object. This |
| 2020 |
* is only valid when the lists are in canonical form. |
| 2021 |
*/ |
| 2022 |
|
| 2023 |
for (i = 0; i < objc; i++) { |
| 2024 |
int length; |
| 2025 |
|
| 2026 |
objPtr = objv[i]; |
| 2027 |
if (TclListObjIsCanonical(objPtr)) { |
| 2028 |
continue; |
| 2029 |
} |
| 2030 |
TclGetStringFromObj(objPtr, &length); |
| 2031 |
if (length > 0) { |
| 2032 |
break; |
| 2033 |
} |
| 2034 |
} |
| 2035 |
if (i == objc) { |
| 2036 |
resPtr = NULL; |
| 2037 |
for (i = 0; i < objc; i++) { |
| 2038 |
objPtr = objv[i]; |
| 2039 |
if (objPtr->bytes && objPtr->length == 0) { |
| 2040 |
continue; |
| 2041 |
} |
| 2042 |
if (resPtr) { |
| 2043 |
Tcl_Obj *elemPtr = NULL; |
| 2044 |
|
| 2045 |
Tcl_ListObjIndex(NULL, objPtr, 0, &elemPtr); |
| 2046 |
if (elemPtr == NULL) { |
| 2047 |
continue; |
| 2048 |
} |
| 2049 |
if (TclGetString(elemPtr)[0] == '#' || TCL_OK |
| 2050 |
!= Tcl_ListObjAppendList(NULL, resPtr, objPtr)) { |
| 2051 |
/* Abandon ship! */ |
| 2052 |
Tcl_DecrRefCount(resPtr); |
| 2053 |
goto slow; |
| 2054 |
} |
| 2055 |
} else { |
| 2056 |
resPtr = TclListObjCopy(NULL, objPtr); |
| 2057 |
} |
| 2058 |
} |
| 2059 |
if (!resPtr) { |
| 2060 |
TclNewObj(resPtr); |
| 2061 |
} |
| 2062 |
return resPtr; |
| 2063 |
} |
| 2064 |
|
| 2065 |
slow: |
| 2066 |
/* |
| 2067 |
* Something cannot be determined to be safe, so build the concatenation |
| 2068 |
* the slow way, using the string representations. |
| 2069 |
* |
| 2070 |
* First try to preallocate the size required. |
| 2071 |
*/ |
| 2072 |
|
| 2073 |
for (i = 0; i < objc; i++) { |
| 2074 |
element = TclGetStringFromObj(objv[i], &elemLength); |
| 2075 |
bytesNeeded += elemLength; |
| 2076 |
if (bytesNeeded < 0) { |
| 2077 |
break; |
| 2078 |
} |
| 2079 |
} |
| 2080 |
|
| 2081 |
/* |
| 2082 |
* Does not matter if this fails, will simply try later to build up the |
| 2083 |
* string with each Append reallocating as needed with the usual string |
| 2084 |
* append algorithm. When that fails it will report the error. |
| 2085 |
*/ |
| 2086 |
|
| 2087 |
TclNewObj(resPtr); |
| 2088 |
(void) Tcl_AttemptSetObjLength(resPtr, bytesNeeded + objc - 1); |
| 2089 |
Tcl_SetObjLength(resPtr, 0); |
| 2090 |
|
| 2091 |
for (i = 0; i < objc; i++) { |
| 2092 |
int triml, trimr; |
| 2093 |
|
| 2094 |
element = TclGetStringFromObj(objv[i], &elemLength); |
| 2095 |
|
| 2096 |
/* Trim away the leading/trailing whitespace. */ |
| 2097 |
triml = TclTrim(element, elemLength, CONCAT_TRIM_SET, |
| 2098 |
CONCAT_WS_SIZE, &trimr); |
| 2099 |
element += triml; |
| 2100 |
elemLength -= triml + trimr; |
| 2101 |
|
| 2102 |
/* Do not permit trimming to expose a final backslash character. */ |
| 2103 |
elemLength += trimr && (element[elemLength - 1] == '\\'); |
| 2104 |
|
| 2105 |
/* |
| 2106 |
* If we're left with empty element after trimming, do nothing. |
| 2107 |
*/ |
| 2108 |
|
| 2109 |
if (elemLength == 0) { |
| 2110 |
continue; |
| 2111 |
} |
| 2112 |
|
| 2113 |
/* |
| 2114 |
* Append to the result with space if needed. |
| 2115 |
*/ |
| 2116 |
|
| 2117 |
if (needSpace) { |
| 2118 |
Tcl_AppendToObj(resPtr, " ", 1); |
| 2119 |
} |
| 2120 |
Tcl_AppendToObj(resPtr, element, elemLength); |
| 2121 |
needSpace = 1; |
| 2122 |
} |
| 2123 |
return resPtr; |
| 2124 |
} |
| 2125 |
|
| 2126 |
/* |
| 2127 |
*---------------------------------------------------------------------- |
| 2128 |
* |
| 2129 |
* Tcl_StringMatch -- |
| 2130 |
* |
| 2131 |
* See if a particular string matches a particular pattern. |
| 2132 |
* |
| 2133 |
* Results: |
| 2134 |
* The return value is 1 if string matches pattern, and 0 otherwise. The |
| 2135 |
* matching operation permits the following special characters in the |
| 2136 |
* pattern: *?\[] (see the manual entry for details on what these mean). |
| 2137 |
* |
| 2138 |
* Side effects: |
| 2139 |
* None. |
| 2140 |
* |
| 2141 |
*---------------------------------------------------------------------- |
| 2142 |
*/ |
| 2143 |
|
| 2144 |
int |
| 2145 |
Tcl_StringMatch( |
| 2146 |
const char *str, /* String. */ |
| 2147 |
const char *pattern) /* Pattern, which may contain special |
| 2148 |
* characters. */ |
| 2149 |
{ |
| 2150 |
return Tcl_StringCaseMatch(str, pattern, 0); |
| 2151 |
} |
| 2152 |
|
| 2153 |
/* |
| 2154 |
*---------------------------------------------------------------------- |
| 2155 |
* |
| 2156 |
* Tcl_StringCaseMatch -- |
| 2157 |
* |
| 2158 |
* See if a particular string matches a particular pattern. Allows case |
| 2159 |
* insensitivity. |
| 2160 |
* |
| 2161 |
* Results: |
| 2162 |
* The return value is 1 if string matches pattern, and 0 otherwise. The |
| 2163 |
* matching operation permits the following special characters in the |
| 2164 |
* pattern: *?\[] (see the manual entry for details on what these mean). |
| 2165 |
* |
| 2166 |
* Side effects: |
| 2167 |
* None. |
| 2168 |
* |
| 2169 |
*---------------------------------------------------------------------- |
| 2170 |
*/ |
| 2171 |
|
| 2172 |
int |
| 2173 |
Tcl_StringCaseMatch( |
| 2174 |
const char *str, /* String. */ |
| 2175 |
const char *pattern, /* Pattern, which may contain special |
| 2176 |
* characters. */ |
| 2177 |
int nocase) /* 0 for case sensitive, 1 for insensitive */ |
| 2178 |
{ |
| 2179 |
int p, charLen; |
| 2180 |
int ch1 = 0, ch2 = 0; |
| 2181 |
|
| 2182 |
while (1) { |
| 2183 |
p = *pattern; |
| 2184 |
|
| 2185 |
/* |
| 2186 |
* See if we're at the end of both the pattern and the string. If so, |
| 2187 |
* we succeeded. If we're at the end of the pattern but not at the end |
| 2188 |
* of the string, we failed. |
| 2189 |
*/ |
| 2190 |
|
| 2191 |
if (p == '\0') { |
| 2192 |
return (*str == '\0'); |
| 2193 |
} |
| 2194 |
if ((*str == '\0') && (p != '*')) { |
| 2195 |
return 0; |
| 2196 |
} |
| 2197 |
|
| 2198 |
/* |
| 2199 |
* Check for a "*" as the next pattern character. It matches any |
| 2200 |
* substring. We handle this by calling ourselves recursively for each |
| 2201 |
* postfix of string, until either we match or we reach the end of the |
| 2202 |
* string. |
| 2203 |
*/ |
| 2204 |
|
| 2205 |
if (p == '*') { |
| 2206 |
/* |
| 2207 |
* Skip all successive *'s in the pattern |
| 2208 |
*/ |
| 2209 |
|
| 2210 |
while (*(++pattern) == '*') {} |
| 2211 |
p = *pattern; |
| 2212 |
if (p == '\0') { |
| 2213 |
return 1; |
| 2214 |
} |
| 2215 |
|
| 2216 |
/* |
| 2217 |
* This is a special case optimization for single-byte utf. |
| 2218 |
*/ |
| 2219 |
|
| 2220 |
if (UCHAR(*pattern) < 0x80) { |
| 2221 |
ch2 = (int) |
| 2222 |
(nocase ? tolower(UCHAR(*pattern)) : UCHAR(*pattern)); |
| 2223 |
} else { |
| 2224 |
TclUtfToUCS4(pattern, &ch2); |
| 2225 |
if (nocase) { |
| 2226 |
ch2 = TclUCS4ToLower(ch2); |
| 2227 |
} |
| 2228 |
} |
| 2229 |
|
| 2230 |
while (1) { |
| 2231 |
/* |
| 2232 |
* Optimization for matching - cruise through the string |
| 2233 |
* quickly if the next char in the pattern isn't a special |
| 2234 |
* character |
| 2235 |
*/ |
| 2236 |
|
| 2237 |
if ((p != '[') && (p != '?') && (p != '\\')) { |
| 2238 |
if (nocase) { |
| 2239 |
while (*str) { |
| 2240 |
charLen = TclUtfToUCS4(str, &ch1); |
| 2241 |
if (ch2==ch1 || ch2==TclUCS4ToLower(ch1)) { |
| 2242 |
break; |
| 2243 |
} |
| 2244 |
str += charLen; |
| 2245 |
} |
| 2246 |
} else { |
| 2247 |
/* |
| 2248 |
* There's no point in trying to make this code |
| 2249 |
* shorter, as the number of bytes you want to compare |
| 2250 |
* each time is non-constant. |
| 2251 |
*/ |
| 2252 |
|
| 2253 |
while (*str) { |
| 2254 |
charLen = TclUtfToUCS4(str, &ch1); |
| 2255 |
if (ch2 == ch1) { |
| 2256 |
break; |
| 2257 |
} |
| 2258 |
str += charLen; |
| 2259 |
} |
| 2260 |
} |
| 2261 |
} |
| 2262 |
if (Tcl_StringCaseMatch(str, pattern, nocase)) { |
| 2263 |
return 1; |
| 2264 |
} |
| 2265 |
if (*str == '\0') { |
| 2266 |
return 0; |
| 2267 |
} |
| 2268 |
str += TclUtfToUCS4(str, &ch1); |
| 2269 |
} |
| 2270 |
} |
| 2271 |
|
| 2272 |
/* |
| 2273 |
* Check for a "?" as the next pattern character. It matches any |
| 2274 |
* single character. |
| 2275 |
*/ |
| 2276 |
|
| 2277 |
if (p == '?') { |
| 2278 |
pattern++; |
| 2279 |
str += TclUtfToUCS4(str, &ch1); |
| 2280 |
continue; |
| 2281 |
} |
| 2282 |
|
| 2283 |
/* |
| 2284 |
* Check for a "[" as the next pattern character. It is followed by a |
| 2285 |
* list of characters that are acceptable, or by a range (two |
| 2286 |
* characters separated by "-"). |
| 2287 |
*/ |
| 2288 |
|
| 2289 |
if (p == '[') { |
| 2290 |
int startChar = 0, endChar = 0; |
| 2291 |
|
| 2292 |
pattern++; |
| 2293 |
if (UCHAR(*str) < 0x80) { |
| 2294 |
ch1 = (int) |
| 2295 |
(nocase ? tolower(UCHAR(*str)) : UCHAR(*str)); |
| 2296 |
str++; |
| 2297 |
} else { |
| 2298 |
str += TclUtfToUCS4(str, &ch1); |
| 2299 |
if (nocase) { |
| 2300 |
ch1 = TclUCS4ToLower(ch1); |
| 2301 |
} |
| 2302 |
} |
| 2303 |
while (1) { |
| 2304 |
if ((*pattern == ']') || (*pattern == '\0')) { |
| 2305 |
return 0; |
| 2306 |
} |
| 2307 |
if (UCHAR(*pattern) < 0x80) { |
| 2308 |
startChar = (int) (nocase |
| 2309 |
? tolower(UCHAR(*pattern)) : UCHAR(*pattern)); |
| 2310 |
pattern++; |
| 2311 |
} else { |
| 2312 |
pattern += TclUtfToUCS4(pattern, &startChar); |
| 2313 |
if (nocase) { |
| 2314 |
startChar = TclUCS4ToLower(startChar); |
| 2315 |
} |
| 2316 |
} |
| 2317 |
if (*pattern == '-') { |
| 2318 |
pattern++; |
| 2319 |
if (*pattern == '\0') { |
| 2320 |
return 0; |
| 2321 |
} |
| 2322 |
if (UCHAR(*pattern) < 0x80) { |
| 2323 |
endChar = (int) (nocase |
| 2324 |
? tolower(UCHAR(*pattern)) : UCHAR(*pattern)); |
| 2325 |
pattern++; |
| 2326 |
} else { |
| 2327 |
pattern += TclUtfToUCS4(pattern, &endChar); |
| 2328 |
if (nocase) { |
| 2329 |
endChar = TclUCS4ToLower(endChar); |
| 2330 |
} |
| 2331 |
} |
| 2332 |
if (((startChar <= ch1) && (ch1 <= endChar)) |
| 2333 |
|| ((endChar <= ch1) && (ch1 <= startChar))) { |
| 2334 |
/* |
| 2335 |
* Matches ranges of form [a-z] or [z-a]. |
| 2336 |
*/ |
| 2337 |
|
| 2338 |
break; |
| 2339 |
} |
| 2340 |
} else if (startChar == ch1) { |
| 2341 |
break; |
| 2342 |
} |
| 2343 |
} |
| 2344 |
/* If we reach here, we matched. Need to move past closing ] */ |
| 2345 |
while (*pattern != ']') { |
| 2346 |
if (*pattern == '\0') { |
| 2347 |
/* We ran out of pattern after matching something in |
| 2348 |
* (unclosed!) brackets. So long as we ran out of string |
| 2349 |
* at the same time, we have a match. Otherwise, not. */ |
| 2350 |
return (*str == '\0'); |
| 2351 |
} |
| 2352 |
pattern++; |
| 2353 |
} |
| 2354 |
pattern++; |
| 2355 |
continue; |
| 2356 |
} |
| 2357 |
|
| 2358 |
/* |
| 2359 |
* If the next pattern character is '\', just strip off the '\' so we |
| 2360 |
* do exact matching on the character that follows. |
| 2361 |
*/ |
| 2362 |
|
| 2363 |
if (p == '\\') { |
| 2364 |
pattern++; |
| 2365 |
if (*pattern == '\0') { |
| 2366 |
return 0; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
/* |
| 2371 |
* There's no special character. Just make sure that the next bytes of |
| 2372 |
* each string match. |
| 2373 |
*/ |
| 2374 |
|
| 2375 |
str += TclUtfToUCS4(str, &ch1); |
| 2376 |
pattern += TclUtfToUCS4(pattern, &ch2); |
| 2377 |
if (nocase) { |
| 2378 |
if (TclUCS4ToLower(ch1) != TclUCS4ToLower(ch2)) { |
| 2379 |
return 0; |
| 2380 |
} |
| 2381 |
} else if (ch1 != ch2) { |
| 2382 |
return 0; |
| 2383 |
} |
| 2384 |
} |
| 2385 |
} |
| 2386 |
|
| 2387 |
/* |
| 2388 |
*---------------------------------------------------------------------- |
| 2389 |
* |
| 2390 |
* TclByteArrayMatch -- |
| 2391 |
* |
| 2392 |
* See if a particular string matches a particular pattern. Does not |
| 2393 |
* allow for case insensitivity. |
| 2394 |
* Parallels tclUtf.c:TclUniCharMatch, adjusted for char* and sans nocase. |
| 2395 |
* |
| 2396 |
* Results: |
| 2397 |
* The return value is 1 if string matches pattern, and 0 otherwise. The |
| 2398 |
* matching operation permits the following special characters in the |
| 2399 |
* pattern: *?\[] (see the manual entry for details on what these mean). |
| 2400 |
* |
| 2401 |
* Side effects: |
| 2402 |
* None. |
| 2403 |
* |
| 2404 |
*---------------------------------------------------------------------- |
| 2405 |
*/ |
| 2406 |
|
| 2407 |
int |
| 2408 |
TclByteArrayMatch( |
| 2409 |
const unsigned char *string,/* String. */ |
| 2410 |
int strLen, /* Length of String */ |
| 2411 |
const unsigned char *pattern, |
| 2412 |
/* Pattern, which may contain special |
| 2413 |
* characters. */ |
| 2414 |
int ptnLen, /* Length of Pattern */ |
| 2415 |
int flags) |
| 2416 |
{ |
| 2417 |
const unsigned char *stringEnd, *patternEnd; |
| 2418 |
unsigned char p; |
| 2419 |
|
| 2420 |
stringEnd = string + strLen; |
| 2421 |
patternEnd = pattern + ptnLen; |
| 2422 |
|
| 2423 |
while (1) { |
| 2424 |
/* |
| 2425 |
* See if we're at the end of both the pattern and the string. If so, |
| 2426 |
* we succeeded. If we're at the end of the pattern but not at the end |
| 2427 |
* of the string, we failed. |
| 2428 |
*/ |
| 2429 |
|
| 2430 |
if (pattern == patternEnd) { |
| 2431 |
return (string == stringEnd); |
| 2432 |
} |
| 2433 |
p = *pattern; |
| 2434 |
if ((string == stringEnd) && (p != '*')) { |
| 2435 |
return 0; |
| 2436 |
} |
| 2437 |
|
| 2438 |
/* |
| 2439 |
* Check for a "*" as the next pattern character. It matches any |
| 2440 |
* substring. We handle this by skipping all the characters up to the |
| 2441 |
* next matching one in the pattern, and then calling ourselves |
| 2442 |
* recursively for each postfix of string, until either we match or we |
| 2443 |
* reach the end of the string. |
| 2444 |
*/ |
| 2445 |
|
| 2446 |
if (p == '*') { |
| 2447 |
/* |
| 2448 |
* Skip all successive *'s in the pattern. |
| 2449 |
*/ |
| 2450 |
|
| 2451 |
while ((++pattern < patternEnd) && (*pattern == '*')) { |
| 2452 |
/* empty body */ |
| 2453 |
} |
| 2454 |
if (pattern == patternEnd) { |
| 2455 |
return 1; |
| 2456 |
} |
| 2457 |
p = *pattern; |
| 2458 |
while (1) { |
| 2459 |
/* |
| 2460 |
* Optimization for matching - cruise through the string |
| 2461 |
* quickly if the next char in the pattern isn't a special |
| 2462 |
* character. |
| 2463 |
*/ |
| 2464 |
|
| 2465 |
if ((p != '[') && (p != '?') && (p != '\\')) { |
| 2466 |
while ((string < stringEnd) && (p != *string)) { |
| 2467 |
string++; |
| 2468 |
} |
| 2469 |
} |
| 2470 |
if (TclByteArrayMatch(string, stringEnd - string, |
| 2471 |
pattern, patternEnd - pattern, 0)) { |
| 2472 |
return 1; |
| 2473 |
} |
| 2474 |
if (string == stringEnd) { |
| 2475 |
return 0; |
| 2476 |
} |
| 2477 |
string++; |
| 2478 |
} |
| 2479 |
} |
| 2480 |
|
| 2481 |
/* |
| 2482 |
* Check for a "?" as the next pattern character. It matches any |
| 2483 |
* single character. |
| 2484 |
*/ |
| 2485 |
|
| 2486 |
if (p == '?') { |
| 2487 |
pattern++; |
| 2488 |
string++; |
| 2489 |
continue; |
| 2490 |
} |
| 2491 |
|
| 2492 |
/* |
| 2493 |
* Check for a "[" as the next pattern character. It is followed by a |
| 2494 |
* list of characters that are acceptable, or by a range (two |
| 2495 |
* characters separated by "-"). |
| 2496 |
*/ |
| 2497 |
|
| 2498 |
if (p == '[') { |
| 2499 |
unsigned char ch1, startChar, endChar; |
| 2500 |
|
| 2501 |
pattern++; |
| 2502 |
ch1 = *string; |
| 2503 |
string++; |
| 2504 |
while (1) { |
| 2505 |
if ((*pattern == ']') || (pattern == patternEnd)) { |
| 2506 |
return 0; |
| 2507 |
} |
| 2508 |
startChar = *pattern; |
| 2509 |
pattern++; |
| 2510 |
if (*pattern == '-') { |
| 2511 |
pattern++; |
| 2512 |
if (pattern == patternEnd) { |
| 2513 |
return 0; |
| 2514 |
} |
| 2515 |
endChar = *pattern; |
| 2516 |
pattern++; |
| 2517 |
if (((startChar <= ch1) && (ch1 <= endChar)) |
| 2518 |
|| ((endChar <= ch1) && (ch1 <= startChar))) { |
| 2519 |
/* |
| 2520 |
* Matches ranges of form [a-z] or [z-a]. |
| 2521 |
*/ |
| 2522 |
|
| 2523 |
break; |
| 2524 |
} |
| 2525 |
} else if (startChar == ch1) { |
| 2526 |
break; |
| 2527 |
} |
| 2528 |
} |
| 2529 |
while (*pattern != ']') { |
| 2530 |
if (pattern == patternEnd) { |
| 2531 |
pattern--; |
| 2532 |
break; |
| 2533 |
} |
| 2534 |
pattern++; |
| 2535 |
} |
| 2536 |
pattern++; |
| 2537 |
continue; |
| 2538 |
} |
| 2539 |
|
| 2540 |
/* |
| 2541 |
* If the next pattern character is '\', just strip off the '\' so we |
| 2542 |
* do exact matching on the character that follows. |
| 2543 |
*/ |
| 2544 |
|
| 2545 |
if (p == '\\') { |
| 2546 |
if (++pattern == patternEnd) { |
| 2547 |
return 0; |
| 2548 |
} |
| 2549 |
} |
| 2550 |
|
| 2551 |
/* |
| 2552 |
* There's no special character. Just make sure that the next bytes of |
| 2553 |
* each string match. |
| 2554 |
*/ |
| 2555 |
|
| 2556 |
if (*string != *pattern) { |
| 2557 |
return 0; |
| 2558 |
} |
| 2559 |
string++; |
| 2560 |
pattern++; |
| 2561 |
} |
| 2562 |
} |
| 2563 |
|
| 2564 |
/* |
| 2565 |
*---------------------------------------------------------------------- |
| 2566 |
* |
| 2567 |
* TclStringMatchObj -- |
| 2568 |
* |
| 2569 |
* See if a particular string matches a particular pattern. Allows case |
| 2570 |
* insensitivity. This is the generic multi-type handler for the various |
| 2571 |
* matching algorithms. |
| 2572 |
* |
| 2573 |
* Results: |
| 2574 |
* The return value is 1 if string matches pattern, and 0 otherwise. The |
| 2575 |
* matching operation permits the following special characters in the |
| 2576 |
* pattern: *?\[] (see the manual entry for details on what these mean). |
| 2577 |
* |
| 2578 |
* Side effects: |
| 2579 |
* None. |
| 2580 |
* |
| 2581 |
*---------------------------------------------------------------------- |
| 2582 |
*/ |
| 2583 |
|
| 2584 |
int |
| 2585 |
TclStringMatchObj( |
| 2586 |
Tcl_Obj *strObj, /* string object. */ |
| 2587 |
Tcl_Obj *ptnObj, /* pattern object. */ |
| 2588 |
int flags) /* Only TCL_MATCH_NOCASE should be passed, or |
| 2589 |
* 0. */ |
| 2590 |
{ |
| 2591 |
int match, length, plen; |
| 2592 |
|
| 2593 |
/* |
| 2594 |
* Promote based on the type of incoming object. |
| 2595 |
* XXX: Currently doesn't take advantage of exact-ness that |
| 2596 |
* XXX: TclReToGlob tells us about |
| 2597 |
trivial = nocase ? 0 : TclMatchIsTrivial(TclGetString(ptnObj)); |
| 2598 |
*/ |
| 2599 |
|
| 2600 |
if ((strObj->typePtr == &tclStringType) || (strObj->typePtr == NULL)) { |
| 2601 |
Tcl_UniChar *udata, *uptn; |
| 2602 |
|
| 2603 |
udata = Tcl_GetUnicodeFromObj(strObj, &length); |
| 2604 |
uptn = Tcl_GetUnicodeFromObj(ptnObj, &plen); |
| 2605 |
match = TclUniCharMatch(udata, length, uptn, plen, flags); |
| 2606 |
} else if (TclIsPureByteArray(strObj) && TclIsPureByteArray(ptnObj) |
| 2607 |
&& !flags) { |
| 2608 |
unsigned char *data, *ptn; |
| 2609 |
|
| 2610 |
data = Tcl_GetByteArrayFromObj(strObj, &length); |
| 2611 |
ptn = Tcl_GetByteArrayFromObj(ptnObj, &plen); |
| 2612 |
match = TclByteArrayMatch(data, length, ptn, plen, 0); |
| 2613 |
} else { |
| 2614 |
match = Tcl_StringCaseMatch(TclGetString(strObj), |
| 2615 |
TclGetString(ptnObj), flags); |
| 2616 |
} |
| 2617 |
return match; |
| 2618 |
} |
| 2619 |
|
| 2620 |
/* |
| 2621 |
*---------------------------------------------------------------------- |
| 2622 |
* |
| 2623 |
* Tcl_DStringInit -- |
| 2624 |
* |
| 2625 |
* Initializes a dynamic string, discarding any previous contents of the |
| 2626 |
* string (Tcl_DStringFree should have been called already if the dynamic |
| 2627 |
* string was previously in use). |
| 2628 |
* |
| 2629 |
* Results: |
| 2630 |
* None. |
| 2631 |
* |
| 2632 |
* Side effects: |
| 2633 |
* The dynamic string is initialized to be empty. |
| 2634 |
* |
| 2635 |
*---------------------------------------------------------------------- |
| 2636 |
*/ |
| 2637 |
|
| 2638 |
void |
| 2639 |
Tcl_DStringInit( |
| 2640 |
Tcl_DString *dsPtr) /* Pointer to structure for dynamic string. */ |
| 2641 |
{ |
| 2642 |
dsPtr->string = dsPtr->staticSpace; |
| 2643 |
dsPtr->length = 0; |
| 2644 |
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 2645 |
dsPtr->staticSpace[0] = '\0'; |
| 2646 |
} |
| 2647 |
|
| 2648 |
/* |
| 2649 |
*---------------------------------------------------------------------- |
| 2650 |
* |
| 2651 |
* Tcl_DStringAppend -- |
| 2652 |
* |
| 2653 |
* Append more bytes to the current value of a dynamic string. |
| 2654 |
* |
| 2655 |
* Results: |
| 2656 |
* The return value is a pointer to the dynamic string's new value. |
| 2657 |
* |
| 2658 |
* Side effects: |
| 2659 |
* Length bytes from "bytes" (or all of "bytes" if length is less than |
| 2660 |
* zero) are added to the current value of the string. Memory gets |
| 2661 |
* reallocated if needed to accommodate the string's new size. |
| 2662 |
* |
| 2663 |
*---------------------------------------------------------------------- |
| 2664 |
*/ |
| 2665 |
|
| 2666 |
char * |
| 2667 |
Tcl_DStringAppend( |
| 2668 |
Tcl_DString *dsPtr, /* Structure describing dynamic string. */ |
| 2669 |
const char *bytes, /* String to append. If length is -1 then this |
| 2670 |
* must be null-terminated. */ |
| 2671 |
int length) /* Number of bytes from "bytes" to append. If |
| 2672 |
* < 0, then append all of bytes, up to null |
| 2673 |
* at end. */ |
| 2674 |
{ |
| 2675 |
int newSize; |
| 2676 |
|
| 2677 |
if (length < 0) { |
| 2678 |
length = strlen(bytes); |
| 2679 |
} |
| 2680 |
newSize = length + dsPtr->length; |
| 2681 |
|
| 2682 |
/* |
| 2683 |
* Allocate a larger buffer for the string if the current one isn't large |
| 2684 |
* enough. Allocate extra space in the new buffer so that there will be |
| 2685 |
* room to grow before we have to allocate again. |
| 2686 |
*/ |
| 2687 |
|
| 2688 |
if (newSize >= dsPtr->spaceAvl) { |
| 2689 |
dsPtr->spaceAvl = newSize * 2; |
| 2690 |
if (dsPtr->string == dsPtr->staticSpace) { |
| 2691 |
char *newString = (char *)ckalloc(dsPtr->spaceAvl); |
| 2692 |
|
| 2693 |
memcpy(newString, dsPtr->string, dsPtr->length); |
| 2694 |
dsPtr->string = newString; |
| 2695 |
} else { |
| 2696 |
int offset = -1; |
| 2697 |
|
| 2698 |
/* See [16896d49fd] */ |
| 2699 |
if (bytes >= dsPtr->string |
| 2700 |
&& bytes <= dsPtr->string + dsPtr->length) { |
| 2701 |
offset = bytes - dsPtr->string; |
| 2702 |
} |
| 2703 |
|
| 2704 |
dsPtr->string = (char *)ckrealloc(dsPtr->string, dsPtr->spaceAvl); |
| 2705 |
|
| 2706 |
if (offset >= 0) { |
| 2707 |
bytes = dsPtr->string + offset; |
| 2708 |
} |
| 2709 |
} |
| 2710 |
} |
| 2711 |
|
| 2712 |
/* |
| 2713 |
* Copy the new string into the buffer at the end of the old one. |
| 2714 |
*/ |
| 2715 |
|
| 2716 |
memcpy(dsPtr->string + dsPtr->length, bytes, length); |
| 2717 |
dsPtr->length += length; |
| 2718 |
dsPtr->string[dsPtr->length] = '\0'; |
| 2719 |
return dsPtr->string; |
| 2720 |
} |
| 2721 |
|
| 2722 |
/* |
| 2723 |
*---------------------------------------------------------------------- |
| 2724 |
* |
| 2725 |
* TclDStringAppendObj, TclDStringAppendDString -- |
| 2726 |
* |
| 2727 |
* Simple wrappers round Tcl_DStringAppend that make it easier to append |
| 2728 |
* from particular sources of strings. |
| 2729 |
* |
| 2730 |
*---------------------------------------------------------------------- |
| 2731 |
*/ |
| 2732 |
|
| 2733 |
char * |
| 2734 |
TclDStringAppendObj( |
| 2735 |
Tcl_DString *dsPtr, |
| 2736 |
Tcl_Obj *objPtr) |
| 2737 |
{ |
| 2738 |
int length; |
| 2739 |
char *bytes = TclGetStringFromObj(objPtr, &length); |
| 2740 |
|
| 2741 |
return Tcl_DStringAppend(dsPtr, bytes, length); |
| 2742 |
} |
| 2743 |
|
| 2744 |
char * |
| 2745 |
TclDStringAppendDString( |
| 2746 |
Tcl_DString *dsPtr, |
| 2747 |
Tcl_DString *toAppendPtr) |
| 2748 |
{ |
| 2749 |
return Tcl_DStringAppend(dsPtr, Tcl_DStringValue(toAppendPtr), |
| 2750 |
Tcl_DStringLength(toAppendPtr)); |
| 2751 |
} |
| 2752 |
|
| 2753 |
/* |
| 2754 |
*---------------------------------------------------------------------- |
| 2755 |
* |
| 2756 |
* Tcl_DStringAppendElement -- |
| 2757 |
* |
| 2758 |
* Append a list element to the current value of a dynamic string. |
| 2759 |
* |
| 2760 |
* Results: |
| 2761 |
* The return value is a pointer to the dynamic string's new value. |
| 2762 |
* |
| 2763 |
* Side effects: |
| 2764 |
* String is reformatted as a list element and added to the current value |
| 2765 |
* of the string. Memory gets reallocated if needed to accommodate the |
| 2766 |
* string's new size. |
| 2767 |
* |
| 2768 |
*---------------------------------------------------------------------- |
| 2769 |
*/ |
| 2770 |
|
| 2771 |
char * |
| 2772 |
Tcl_DStringAppendElement( |
| 2773 |
Tcl_DString *dsPtr, /* Structure describing dynamic string. */ |
| 2774 |
const char *element) /* String to append. Must be |
| 2775 |
* null-terminated. */ |
| 2776 |
{ |
| 2777 |
char *dst = dsPtr->string + dsPtr->length; |
| 2778 |
int needSpace = TclNeedSpace(dsPtr->string, dst); |
| 2779 |
char flags = 0; |
| 2780 |
int quoteHash = 1, newSize; |
| 2781 |
|
| 2782 |
if (needSpace) { |
| 2783 |
/* |
| 2784 |
* If we need a space to separate the new element from something |
| 2785 |
* already ending the string, we're not appending the first element |
| 2786 |
* of any list, so we need not quote any leading hash character. |
| 2787 |
*/ |
| 2788 |
quoteHash = 0; |
| 2789 |
} else { |
| 2790 |
/* |
| 2791 |
* We don't need a space, maybe because there's some already there. |
| 2792 |
* Checking whether we might be appending a first element is a bit |
| 2793 |
* more involved. |
| 2794 |
* |
| 2795 |
* Backtrack over all whitespace. |
| 2796 |
*/ |
| 2797 |
while ((--dst >= dsPtr->string) && TclIsSpaceProcM(*dst)) { |
| 2798 |
} |
| 2799 |
|
| 2800 |
/* Call again without whitespace to confound things. */ |
| 2801 |
quoteHash = !TclNeedSpace(dsPtr->string, dst+1); |
| 2802 |
} |
| 2803 |
if (!quoteHash) { |
| 2804 |
flags |= TCL_DONT_QUOTE_HASH; |
| 2805 |
} |
| 2806 |
newSize = dsPtr->length + needSpace + TclScanElement(element, -1, &flags); |
| 2807 |
if (!quoteHash) { |
| 2808 |
flags |= TCL_DONT_QUOTE_HASH; |
| 2809 |
} |
| 2810 |
|
| 2811 |
/* |
| 2812 |
* Allocate a larger buffer for the string if the current one isn't large |
| 2813 |
* enough. Allocate extra space in the new buffer so that there will be |
| 2814 |
* room to grow before we have to allocate again. SPECIAL NOTE: must use |
| 2815 |
* memcpy, not strcpy, to copy the string to a larger buffer, since there |
| 2816 |
* may be embedded NULLs in the string in some cases. |
| 2817 |
*/ |
| 2818 |
|
| 2819 |
if (newSize >= dsPtr->spaceAvl) { |
| 2820 |
dsPtr->spaceAvl = newSize * 2; |
| 2821 |
if (dsPtr->string == dsPtr->staticSpace) { |
| 2822 |
char *newString = (char *)ckalloc(dsPtr->spaceAvl); |
| 2823 |
|
| 2824 |
memcpy(newString, dsPtr->string, dsPtr->length); |
| 2825 |
dsPtr->string = newString; |
| 2826 |
} else { |
| 2827 |
int offset = -1; |
| 2828 |
|
| 2829 |
/* See [16896d49fd] */ |
| 2830 |
if (element >= dsPtr->string |
| 2831 |
&& element <= dsPtr->string + dsPtr->length) { |
| 2832 |
offset = element - dsPtr->string; |
| 2833 |
} |
| 2834 |
|
| 2835 |
dsPtr->string = (char *)ckrealloc(dsPtr->string, dsPtr->spaceAvl); |
| 2836 |
|
| 2837 |
if (offset >= 0) { |
| 2838 |
element = dsPtr->string + offset; |
| 2839 |
} |
| 2840 |
} |
| 2841 |
} |
| 2842 |
dst = dsPtr->string + dsPtr->length; |
| 2843 |
|
| 2844 |
/* |
| 2845 |
* Convert the new string to a list element and copy it into the buffer at |
| 2846 |
* the end, with a space, if needed. |
| 2847 |
*/ |
| 2848 |
|
| 2849 |
if (needSpace) { |
| 2850 |
*dst = ' '; |
| 2851 |
dst++; |
| 2852 |
dsPtr->length++; |
| 2853 |
} |
| 2854 |
|
| 2855 |
dsPtr->length += TclConvertElement(element, -1, dst, flags); |
| 2856 |
dsPtr->string[dsPtr->length] = '\0'; |
| 2857 |
return dsPtr->string; |
| 2858 |
} |
| 2859 |
|
| 2860 |
/* |
| 2861 |
*---------------------------------------------------------------------- |
| 2862 |
* |
| 2863 |
* Tcl_DStringSetLength -- |
| 2864 |
* |
| 2865 |
* Change the length of a dynamic string. This can cause the string to |
| 2866 |
* either grow or shrink, depending on the value of length. |
| 2867 |
* |
| 2868 |
* Results: |
| 2869 |
* None. |
| 2870 |
* |
| 2871 |
* Side effects: |
| 2872 |
* The length of dsPtr is changed to length and a null byte is stored at |
| 2873 |
* that position in the string. If length is larger than the space |
| 2874 |
* allocated for dsPtr, then a panic occurs. |
| 2875 |
* |
| 2876 |
*---------------------------------------------------------------------- |
| 2877 |
*/ |
| 2878 |
|
| 2879 |
void |
| 2880 |
Tcl_DStringSetLength( |
| 2881 |
Tcl_DString *dsPtr, /* Structure describing dynamic string. */ |
| 2882 |
int length) /* New length for dynamic string. */ |
| 2883 |
{ |
| 2884 |
int newsize; |
| 2885 |
|
| 2886 |
if (length < 0) { |
| 2887 |
length = 0; |
| 2888 |
} |
| 2889 |
if (length >= dsPtr->spaceAvl) { |
| 2890 |
/* |
| 2891 |
* There are two interesting cases here. In the first case, the user |
| 2892 |
* may be trying to allocate a large buffer of a specific size. It |
| 2893 |
* would be wasteful to overallocate that buffer, so we just allocate |
| 2894 |
* enough for the requested size plus the trailing null byte. In the |
| 2895 |
* second case, we are growing the buffer incrementally, so we need |
| 2896 |
* behavior similar to Tcl_DStringAppend. The requested length will |
| 2897 |
* usually be a small delta above the current spaceAvl, so we'll end |
| 2898 |
* up doubling the old size. This won't grow the buffer quite as |
| 2899 |
* quickly, but it should be close enough. |
| 2900 |
*/ |
| 2901 |
|
| 2902 |
newsize = dsPtr->spaceAvl * 2; |
| 2903 |
if (length < newsize) { |
| 2904 |
dsPtr->spaceAvl = newsize; |
| 2905 |
} else { |
| 2906 |
dsPtr->spaceAvl = length + 1; |
| 2907 |
} |
| 2908 |
if (dsPtr->string == dsPtr->staticSpace) { |
| 2909 |
char *newString = (char *)ckalloc(dsPtr->spaceAvl); |
| 2910 |
|
| 2911 |
memcpy(newString, dsPtr->string, dsPtr->length); |
| 2912 |
dsPtr->string = newString; |
| 2913 |
} else { |
| 2914 |
dsPtr->string = (char *)ckrealloc(dsPtr->string, dsPtr->spaceAvl); |
| 2915 |
} |
| 2916 |
} |
| 2917 |
dsPtr->length = length; |
| 2918 |
dsPtr->string[length] = 0; |
| 2919 |
} |
| 2920 |
|
| 2921 |
/* |
| 2922 |
*---------------------------------------------------------------------- |
| 2923 |
* |
| 2924 |
* Tcl_DStringFree -- |
| 2925 |
* |
| 2926 |
* Frees up any memory allocated for the dynamic string and reinitializes |
| 2927 |
* the string to an empty state. |
| 2928 |
* |
| 2929 |
* Results: |
| 2930 |
* None. |
| 2931 |
* |
| 2932 |
* Side effects: |
| 2933 |
* The previous contents of the dynamic string are lost, and the new |
| 2934 |
* value is an empty string. |
| 2935 |
* |
| 2936 |
*---------------------------------------------------------------------- |
| 2937 |
*/ |
| 2938 |
|
| 2939 |
void |
| 2940 |
Tcl_DStringFree( |
| 2941 |
Tcl_DString *dsPtr) /* Structure describing dynamic string. */ |
| 2942 |
{ |
| 2943 |
if (dsPtr->string != dsPtr->staticSpace) { |
| 2944 |
ckfree(dsPtr->string); |
| 2945 |
} |
| 2946 |
dsPtr->string = dsPtr->staticSpace; |
| 2947 |
dsPtr->length = 0; |
| 2948 |
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 2949 |
dsPtr->staticSpace[0] = '\0'; |
| 2950 |
} |
| 2951 |
|
| 2952 |
/* |
| 2953 |
*---------------------------------------------------------------------- |
| 2954 |
* |
| 2955 |
* Tcl_DStringResult -- |
| 2956 |
* |
| 2957 |
* This function moves the value of a dynamic string into an interpreter |
| 2958 |
* as its string result. Afterwards, the dynamic string is reset to an |
| 2959 |
* empty string. |
| 2960 |
* |
| 2961 |
* Results: |
| 2962 |
* None. |
| 2963 |
* |
| 2964 |
* Side effects: |
| 2965 |
* The string is "moved" to interp's result, and any existing string |
| 2966 |
* result for interp is freed. dsPtr is reinitialized to an empty string. |
| 2967 |
* |
| 2968 |
*---------------------------------------------------------------------- |
| 2969 |
*/ |
| 2970 |
|
| 2971 |
void |
| 2972 |
Tcl_DStringResult( |
| 2973 |
Tcl_Interp *interp, /* Interpreter whose result is to be reset. */ |
| 2974 |
Tcl_DString *dsPtr) /* Dynamic string that is to become the |
| 2975 |
* result of interp. */ |
| 2976 |
{ |
| 2977 |
Tcl_ResetResult(interp); |
| 2978 |
Tcl_SetObjResult(interp, TclDStringToObj(dsPtr)); |
| 2979 |
} |
| 2980 |
|
| 2981 |
/* |
| 2982 |
*---------------------------------------------------------------------- |
| 2983 |
* |
| 2984 |
* Tcl_DStringGetResult -- |
| 2985 |
* |
| 2986 |
* This function moves an interpreter's result into a dynamic string. |
| 2987 |
* |
| 2988 |
* Results: |
| 2989 |
* None. |
| 2990 |
* |
| 2991 |
* Side effects: |
| 2992 |
* The interpreter's string result is cleared, and the previous contents |
| 2993 |
* of dsPtr are freed. |
| 2994 |
* |
| 2995 |
* If the string result is empty, the object result is moved to the |
| 2996 |
* string result, then the object result is reset. |
| 2997 |
* |
| 2998 |
*---------------------------------------------------------------------- |
| 2999 |
*/ |
| 3000 |
|
| 3001 |
void |
| 3002 |
Tcl_DStringGetResult( |
| 3003 |
Tcl_Interp *interp, /* Interpreter whose result is to be reset. */ |
| 3004 |
Tcl_DString *dsPtr) /* Dynamic string that is to become the result |
| 3005 |
* of interp. */ |
| 3006 |
{ |
| 3007 |
Interp *iPtr = (Interp *) interp; |
| 3008 |
|
| 3009 |
if (dsPtr->string != dsPtr->staticSpace) { |
| 3010 |
ckfree(dsPtr->string); |
| 3011 |
} |
| 3012 |
|
| 3013 |
/* |
| 3014 |
* Do more efficient transfer when we know the result is a Tcl_Obj. When |
| 3015 |
* there's no string result, we only have to deal with two cases: |
| 3016 |
* |
| 3017 |
* 1. When the string rep is the empty string, when we don't copy but |
| 3018 |
* instead use the staticSpace in the DString to hold an empty string. |
| 3019 |
|
| 3020 |
* 2. When the string rep is not there or there's a real string rep, when |
| 3021 |
* we use Tcl_GetString to fetch (or generate) the string rep - which |
| 3022 |
* we know to have been allocated with ckalloc() - and use it to |
| 3023 |
* populate the DString space. Then, we free the internal rep. and set |
| 3024 |
* the object's string representation back to the canonical empty |
| 3025 |
* string. |
| 3026 |
*/ |
| 3027 |
|
| 3028 |
if (!iPtr->result[0] && iPtr->objResultPtr |
| 3029 |
&& !Tcl_IsShared(iPtr->objResultPtr)) { |
| 3030 |
if (iPtr->objResultPtr->bytes == tclEmptyStringRep) { |
| 3031 |
dsPtr->string = dsPtr->staticSpace; |
| 3032 |
dsPtr->string[0] = 0; |
| 3033 |
dsPtr->length = 0; |
| 3034 |
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 3035 |
} else { |
| 3036 |
dsPtr->string = TclGetString(iPtr->objResultPtr); |
| 3037 |
dsPtr->length = iPtr->objResultPtr->length; |
| 3038 |
dsPtr->spaceAvl = dsPtr->length + 1; |
| 3039 |
TclFreeIntRep(iPtr->objResultPtr); |
| 3040 |
iPtr->objResultPtr->bytes = tclEmptyStringRep; |
| 3041 |
iPtr->objResultPtr->length = 0; |
| 3042 |
} |
| 3043 |
return; |
| 3044 |
} |
| 3045 |
|
| 3046 |
/* |
| 3047 |
* If the string result is empty, move the object result to the string |
| 3048 |
* result, then reset the object result. |
| 3049 |
*/ |
| 3050 |
|
| 3051 |
(void) Tcl_GetStringResult(interp); |
| 3052 |
|
| 3053 |
dsPtr->length = strlen(iPtr->result); |
| 3054 |
if (iPtr->freeProc != NULL) { |
| 3055 |
if (iPtr->freeProc == TCL_DYNAMIC) { |
| 3056 |
dsPtr->string = iPtr->result; |
| 3057 |
dsPtr->spaceAvl = dsPtr->length+1; |
| 3058 |
} else { |
| 3059 |
dsPtr->string = (char *)ckalloc(dsPtr->length+1); |
| 3060 |
memcpy(dsPtr->string, iPtr->result, dsPtr->length+1); |
| 3061 |
iPtr->freeProc(iPtr->result); |
| 3062 |
} |
| 3063 |
dsPtr->spaceAvl = dsPtr->length+1; |
| 3064 |
iPtr->freeProc = NULL; |
| 3065 |
} else { |
| 3066 |
if (dsPtr->length < TCL_DSTRING_STATIC_SIZE) { |
| 3067 |
dsPtr->string = dsPtr->staticSpace; |
| 3068 |
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 3069 |
} else { |
| 3070 |
dsPtr->string = (char *)ckalloc(dsPtr->length+1); |
| 3071 |
dsPtr->spaceAvl = dsPtr->length + 1; |
| 3072 |
} |
| 3073 |
memcpy(dsPtr->string, iPtr->result, dsPtr->length+1); |
| 3074 |
} |
| 3075 |
|
| 3076 |
iPtr->result = iPtr->resultSpace; |
| 3077 |
iPtr->resultSpace[0] = 0; |
| 3078 |
} |
| 3079 |
|
| 3080 |
/* |
| 3081 |
*---------------------------------------------------------------------- |
| 3082 |
* |
| 3083 |
* TclDStringToObj -- |
| 3084 |
* |
| 3085 |
* This function moves a dynamic string's contents to a new Tcl_Obj. Be |
| 3086 |
* aware that this function does *not* check that the encoding of the |
| 3087 |
* contents of the dynamic string is correct; this is the caller's |
| 3088 |
* responsibility to enforce. |
| 3089 |
* |
| 3090 |
* Results: |
| 3091 |
* The newly-allocated untyped (i.e., typePtr==NULL) Tcl_Obj with a |
| 3092 |
* reference count of zero. |
| 3093 |
* |
| 3094 |
* Side effects: |
| 3095 |
* The string is "moved" to the object. dsPtr is reinitialized to an |
| 3096 |
* empty string; it does not need to be Tcl_DStringFree'd after this if |
| 3097 |
* not used further. |
| 3098 |
* |
| 3099 |
*---------------------------------------------------------------------- |
| 3100 |
*/ |
| 3101 |
|
| 3102 |
Tcl_Obj * |
| 3103 |
TclDStringToObj( |
| 3104 |
Tcl_DString *dsPtr) |
| 3105 |
{ |
| 3106 |
Tcl_Obj *result; |
| 3107 |
|
| 3108 |
if (dsPtr->string == dsPtr->staticSpace) { |
| 3109 |
if (dsPtr->length == 0) { |
| 3110 |
TclNewObj(result); |
| 3111 |
} else { |
| 3112 |
/* |
| 3113 |
* Static buffer, so must copy. |
| 3114 |
*/ |
| 3115 |
|
| 3116 |
TclNewStringObj(result, dsPtr->string, dsPtr->length); |
| 3117 |
} |
| 3118 |
} else { |
| 3119 |
/* |
| 3120 |
* Dynamic buffer, so transfer ownership and reset. |
| 3121 |
*/ |
| 3122 |
|
| 3123 |
TclNewObj(result); |
| 3124 |
result->bytes = dsPtr->string; |
| 3125 |
result->length = dsPtr->length; |
| 3126 |
} |
| 3127 |
|
| 3128 |
/* |
| 3129 |
* Re-establish the DString as empty with no buffer allocated. |
| 3130 |
*/ |
| 3131 |
|
| 3132 |
dsPtr->string = dsPtr->staticSpace; |
| 3133 |
dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE; |
| 3134 |
dsPtr->length = 0; |
| 3135 |
dsPtr->staticSpace[0] = '\0'; |
| 3136 |
|
| 3137 |
return result; |
| 3138 |
} |
| 3139 |
|
| 3140 |
/* |
| 3141 |
*---------------------------------------------------------------------- |
| 3142 |
* |
| 3143 |
* Tcl_DStringStartSublist -- |
| 3144 |
* |
| 3145 |
* This function adds the necessary information to a dynamic string |
| 3146 |
* (e.g. " {") to start a sublist. Future element appends will be in the |
| 3147 |
* sublist rather than the main list. |
| 3148 |
* |
| 3149 |
* Results: |
| 3150 |
* None. |
| 3151 |
* |
| 3152 |
* Side effects: |
| 3153 |
* Characters get added to the dynamic string. |
| 3154 |
* |
| 3155 |
*---------------------------------------------------------------------- |
| 3156 |
*/ |
| 3157 |
|
| 3158 |
void |
| 3159 |
Tcl_DStringStartSublist( |
| 3160 |
Tcl_DString *dsPtr) /* Dynamic string. */ |
| 3161 |
{ |
| 3162 |
if (TclNeedSpace(dsPtr->string, dsPtr->string + dsPtr->length)) { |
| 3163 |
TclDStringAppendLiteral(dsPtr, " {"); |
| 3164 |
} else { |
| 3165 |
TclDStringAppendLiteral(dsPtr, "{"); |
| 3166 |
} |
| 3167 |
} |
| 3168 |
|
| 3169 |
/* |
| 3170 |
*---------------------------------------------------------------------- |
| 3171 |
* |
| 3172 |
* Tcl_DStringEndSublist -- |
| 3173 |
* |
| 3174 |
* This function adds the necessary characters to a dynamic string to end |
| 3175 |
* a sublist (e.g. "}"). Future element appends will be in the enclosing |
| 3176 |
* (sub)list rather than the current sublist. |
| 3177 |
* |
| 3178 |
* Results: |
| 3179 |
* None. |
| 3180 |
* |
| 3181 |
* Side effects: |
| 3182 |
* None. |
| 3183 |
* |
| 3184 |
*---------------------------------------------------------------------- |
| 3185 |
*/ |
| 3186 |
|
| 3187 |
void |
| 3188 |
Tcl_DStringEndSublist( |
| 3189 |
Tcl_DString *dsPtr) /* Dynamic string. */ |
| 3190 |
{ |
| 3191 |
TclDStringAppendLiteral(dsPtr, "}"); |
| 3192 |
} |
| 3193 |
|
| 3194 |
/* |
| 3195 |
*---------------------------------------------------------------------- |
| 3196 |
* |
| 3197 |
* Tcl_PrintDouble -- |
| 3198 |
* |
| 3199 |
* Given a floating-point value, this function converts it to an ASCII |
| 3200 |
* string using. |
| 3201 |
* |
| 3202 |
* Results: |
| 3203 |
* The ASCII equivalent of "value" is written at "dst". It is written |
| 3204 |
* using the current precision, and it is guaranteed to contain a decimal |
| 3205 |
* point or exponent, so that it looks like a floating-point value and |
| 3206 |
* not an integer. |
| 3207 |
* |
| 3208 |
* Side effects: |
| 3209 |
* None. |
| 3210 |
* |
| 3211 |
*---------------------------------------------------------------------- |
| 3212 |
*/ |
| 3213 |
|
| 3214 |
void |
| 3215 |
Tcl_PrintDouble( |
| 3216 |
Tcl_Interp *interp, /* Interpreter whose tcl_precision variable |
| 3217 |
* used to be used to control printing. It's |
| 3218 |
* ignored now. */ |
| 3219 |
double value, /* Value to print as string. */ |
| 3220 |
char *dst) /* Where to store converted value; must have |
| 3221 |
* at least TCL_DOUBLE_SPACE characters. */ |
| 3222 |
{ |
| 3223 |
char *p, c; |
| 3224 |
int exponent; |
| 3225 |
int signum; |
| 3226 |
char *digits; |
| 3227 |
char *end; |
| 3228 |
int *precisionPtr = (int *)Tcl_GetThreadData(&precisionKey, sizeof(int)); |
| 3229 |
|
| 3230 |
/* |
| 3231 |
* Handle NaN. |
| 3232 |
*/ |
| 3233 |
|
| 3234 |
if (TclIsNaN(value)) { |
| 3235 |
TclFormatNaN(value, dst); |
| 3236 |
return; |
| 3237 |
} |
| 3238 |
|
| 3239 |
/* |
| 3240 |
* Handle infinities. |
| 3241 |
*/ |
| 3242 |
|
| 3243 |
if (TclIsInfinite(value)) { |
| 3244 |
/* |
| 3245 |
* Remember to copy the terminating NUL too. |
| 3246 |
*/ |
| 3247 |
|
| 3248 |
if (value < 0) { |
| 3249 |
memcpy(dst, "-Inf", 5); |
| 3250 |
} else { |
| 3251 |
memcpy(dst, "Inf", 4); |
| 3252 |
} |
| 3253 |
return; |
| 3254 |
} |
| 3255 |
|
| 3256 |
/* |
| 3257 |
* Ordinary (normal and denormal) values. |
| 3258 |
*/ |
| 3259 |
|
| 3260 |
if (*precisionPtr == 0) { |
| 3261 |
digits = TclDoubleDigits(value, -1, TCL_DD_SHORTEST, |
| 3262 |
&exponent, &signum, &end); |
| 3263 |
} else { |
| 3264 |
/* |
| 3265 |
* There are at least two possible interpretations for tcl_precision. |
| 3266 |
* |
| 3267 |
* The first is, "choose the decimal representation having |
| 3268 |
* $tcl_precision digits of significance that is nearest to the given |
| 3269 |
* number, breaking ties by rounding to even, and then trimming |
| 3270 |
* trailing zeros." This gives the greatest possible precision in the |
| 3271 |
* decimal string, but offers the anomaly that [expr 0.1] will be |
| 3272 |
* "0.10000000000000001". |
| 3273 |
* |
| 3274 |
* The second is "choose the decimal representation having at most |
| 3275 |
* $tcl_precision digits of significance that is nearest to the given |
| 3276 |
* number. If no such representation converts exactly to the given |
| 3277 |
* number, choose the one that is closest, breaking ties by rounding |
| 3278 |
* to even. If more than one such representation converts exactly to |
| 3279 |
* the given number, choose the shortest, breaking ties in favour of |
| 3280 |
* the nearest, breaking remaining ties in favour of the one ending in |
| 3281 |
* an even digit." |
| 3282 |
* |
| 3283 |
* Tcl 8.4 implements the first of these, which gives rise to |
| 3284 |
* anomalies in formatting: |
| 3285 |
* |
| 3286 |
* % expr 0.1 |
| 3287 |
* 0.10000000000000001 |
| 3288 |
* % expr 0.01 |
| 3289 |
* 0.01 |
| 3290 |
* % expr 1e-7 |
| 3291 |
* 9.9999999999999995e-08 |
| 3292 |
* |
| 3293 |
* For human readability, it appears better to choose the second rule, |
| 3294 |
* and let [expr 0.1] return 0.1. But for 8.4 compatibility, we prefer |
| 3295 |
* the first (the recommended zero value for tcl_precision avoids the |
| 3296 |
* problem entirely). |
| 3297 |
* |
| 3298 |
* Uncomment TCL_DD_SHORTEN_FLAG in the next call to prefer the method |
| 3299 |
* that allows floating point values to be shortened if it can be done |
| 3300 |
* without loss of precision. |
| 3301 |
*/ |
| 3302 |
|
| 3303 |
digits = TclDoubleDigits(value, *precisionPtr, |
| 3304 |
TCL_DD_E_FORMAT /* | TCL_DD_SHORTEN_FLAG */, |
| 3305 |
&exponent, &signum, &end); |
| 3306 |
} |
| 3307 |
if (signum) { |
| 3308 |
*dst++ = '-'; |
| 3309 |
} |
| 3310 |
p = digits; |
| 3311 |
if (exponent < -4 || exponent > 16) { |
| 3312 |
/* |
| 3313 |
* E format for numbers < 1e-3 or >= 1e17. |
| 3314 |
*/ |
| 3315 |
|
| 3316 |
*dst++ = *p++; |
| 3317 |
c = *p; |
| 3318 |
if (c != '\0') { |
| 3319 |
*dst++ = '.'; |
| 3320 |
while (c != '\0') { |
| 3321 |
*dst++ = c; |
| 3322 |
c = *++p; |
| 3323 |
} |
| 3324 |
} |
| 3325 |
|
| 3326 |
/* |
| 3327 |
* Tcl 8.4 appears to format with at least a two-digit exponent; |
| 3328 |
* preserve that behaviour when tcl_precision != 0 |
| 3329 |
*/ |
| 3330 |
|
| 3331 |
if (*precisionPtr == 0) { |
| 3332 |
snprintf(dst, TCL_DOUBLE_SPACE, "e%+d", exponent); |
| 3333 |
} else { |
| 3334 |
snprintf(dst, TCL_DOUBLE_SPACE, "e%+03d", exponent); |
| 3335 |
} |
| 3336 |
} else { |
| 3337 |
/* |
| 3338 |
* F format for others. |
| 3339 |
*/ |
| 3340 |
|
| 3341 |
if (exponent < 0) { |
| 3342 |
*dst++ = '0'; |
| 3343 |
} |
| 3344 |
c = *p; |
| 3345 |
while (exponent-- >= 0) { |
| 3346 |
if (c != '\0') { |
| 3347 |
*dst++ = c; |
| 3348 |
c = *++p; |
| 3349 |
} else { |
| 3350 |
*dst++ = '0'; |
| 3351 |
} |
| 3352 |
} |
| 3353 |
*dst++ = '.'; |
| 3354 |
if (c == '\0') { |
| 3355 |
*dst++ = '0'; |
| 3356 |
} else { |
| 3357 |
while (++exponent < -1) { |
| 3358 |
*dst++ = '0'; |
| 3359 |
} |
| 3360 |
while (c != '\0') { |
| 3361 |
*dst++ = c; |
| 3362 |
c = *++p; |
| 3363 |
} |
| 3364 |
} |
| 3365 |
*dst++ = '\0'; |
| 3366 |
} |
| 3367 |
ckfree(digits); |
| 3368 |
} |
| 3369 |
|
| 3370 |
/* |
| 3371 |
*---------------------------------------------------------------------- |
| 3372 |
* |
| 3373 |
* TclPrecTraceProc -- |
| 3374 |
* |
| 3375 |
* This function is invoked whenever the variable "tcl_precision" is |
| 3376 |
* written. |
| 3377 |
* |
| 3378 |
* Results: |
| 3379 |
* Returns NULL if all went well, or an error message if the new value |
| 3380 |
* for the variable doesn't make sense. |
| 3381 |
* |
| 3382 |
* Side effects: |
| 3383 |
* If the new value doesn't make sense then this function undoes the |
| 3384 |
* effect of the variable modification. Otherwise it modifies the format |
| 3385 |
* string that's used by Tcl_PrintDouble. |
| 3386 |
* |
| 3387 |
*---------------------------------------------------------------------- |
| 3388 |
*/ |
| 3389 |
|
| 3390 |
char * |
| 3391 |
TclPrecTraceProc( |
| 3392 |
ClientData clientData, /* Not used. */ |
| 3393 |
Tcl_Interp *interp, /* Interpreter containing variable. */ |
| 3394 |
const char *name1, /* Name of variable. */ |
| 3395 |
const char *name2, /* Second part of variable name. */ |
| 3396 |
int flags) /* Information about what happened. */ |
| 3397 |
{ |
| 3398 |
Tcl_Obj *value; |
| 3399 |
int prec; |
| 3400 |
int *precisionPtr = (int *)Tcl_GetThreadData(&precisionKey, sizeof(int)); |
| 3401 |
|
| 3402 |
/* |
| 3403 |
* If the variable is unset, then recreate the trace. |
| 3404 |
*/ |
| 3405 |
|
| 3406 |
if (flags & TCL_TRACE_UNSETS) { |
| 3407 |
if ((flags & TCL_TRACE_DESTROYED) && !Tcl_InterpDeleted(interp)) { |
| 3408 |
Tcl_TraceVar2(interp, name1, name2, |
| 3409 |
TCL_GLOBAL_ONLY|TCL_TRACE_READS|TCL_TRACE_WRITES |
| 3410 |
|TCL_TRACE_UNSETS, TclPrecTraceProc, clientData); |
| 3411 |
} |
| 3412 |
return NULL; |
| 3413 |
} |
| 3414 |
|
| 3415 |
/* |
| 3416 |
* When the variable is read, reset its value from our shared value. This |
| 3417 |
* is needed in case the variable was modified in some other interpreter |
| 3418 |
* so that this interpreter's value is out of date. |
| 3419 |
*/ |
| 3420 |
|
| 3421 |
|
| 3422 |
if (flags & TCL_TRACE_READS) { |
| 3423 |
Tcl_SetVar2Ex(interp, name1, name2, Tcl_NewIntObj(*precisionPtr), |
| 3424 |
flags & TCL_GLOBAL_ONLY); |
| 3425 |
return NULL; |
| 3426 |
} |
| 3427 |
|
| 3428 |
/* |
| 3429 |
* The variable is being written. Check the new value and disallow it if |
| 3430 |
* it isn't reasonable or if this is a safe interpreter (we don't want |
| 3431 |
* safe interpreters messing up the precision of other interpreters). |
| 3432 |
*/ |
| 3433 |
|
| 3434 |
if (Tcl_IsSafe(interp)) { |
| 3435 |
return (char *) "can't modify precision from a safe interpreter"; |
| 3436 |
} |
| 3437 |
value = Tcl_GetVar2Ex(interp, name1, name2, flags & TCL_GLOBAL_ONLY); |
| 3438 |
if (value == NULL |
| 3439 |
|| Tcl_GetIntFromObj(NULL, value, &prec) != TCL_OK |
| 3440 |
|| prec < 0 || prec > TCL_MAX_PREC) { |
| 3441 |
return (char *) "improper value for precision"; |
| 3442 |
} |
| 3443 |
*precisionPtr = prec; |
| 3444 |
return NULL; |
| 3445 |
} |
| 3446 |
|
| 3447 |
/* |
| 3448 |
*---------------------------------------------------------------------- |
| 3449 |
* |
| 3450 |
* TclNeedSpace -- |
| 3451 |
* |
| 3452 |
* This function checks to see whether it is appropriate to add a space |
| 3453 |
* before appending a new list element to an existing string. |
| 3454 |
* |
| 3455 |
* Results: |
| 3456 |
* The return value is 1 if a space is appropriate, 0 otherwise. |
| 3457 |
* |
| 3458 |
* Side effects: |
| 3459 |
* None. |
| 3460 |
* |
| 3461 |
*---------------------------------------------------------------------- |
| 3462 |
*/ |
| 3463 |
|
| 3464 |
int |
| 3465 |
TclNeedSpace( |
| 3466 |
const char *start, /* First character in string. */ |
| 3467 |
const char *end) /* End of string (place where space will be |
| 3468 |
* added, if appropriate). */ |
| 3469 |
{ |
| 3470 |
/* |
| 3471 |
* A space is needed unless either: |
| 3472 |
* (a) we're at the start of the string, or |
| 3473 |
* |
| 3474 |
* (NOTE: This check is now absorbed into the loop below.) |
| 3475 |
* |
| 3476 |
|
| 3477 |
if (end == start) { |
| 3478 |
return 0; |
| 3479 |
} |
| 3480 |
|
| 3481 |
* |
| 3482 |
*/ |
| 3483 |
|
| 3484 |
/* |
| 3485 |
* (b) we're at the start of a nested list-element, quoted with an open |
| 3486 |
* curly brace; we can be nested arbitrarily deep, so long as the |
| 3487 |
* first curly brace starts an element, so backtrack over open curly |
| 3488 |
* braces that are trailing characters of the string; and |
| 3489 |
* |
| 3490 |
* (NOTE: Every character our parser is looking for is a proper |
| 3491 |
* single-byte encoding of an ASCII value. It does not accept |
| 3492 |
* overlong encodings. Given that, there's no benefit using |
| 3493 |
* Tcl_UtfPrev. If it would find what we seek, so would byte-by-byte |
| 3494 |
* backward scan. Save routine call overhead and risk of wrong |
| 3495 |
* results should the behavior of Tcl_UtfPrev change in unexpected ways. |
| 3496 |
* Reconsider this if we ever start treating non-ASCII Unicode |
| 3497 |
* characters as meaningful list syntax, expanded Unicode spaces as |
| 3498 |
* element separators, for example.) |
| 3499 |
* |
| 3500 |
|
| 3501 |
end = Tcl_UtfPrev(end, start); |
| 3502 |
while (*end == '{') { |
| 3503 |
if (end == start) { |
| 3504 |
return 0; |
| 3505 |
} |
| 3506 |
end = Tcl_UtfPrev(end, start); |
| 3507 |
} |
| 3508 |
|
| 3509 |
* |
| 3510 |
*/ |
| 3511 |
|
| 3512 |
while ((--end >= start) && (*end == '{')) { |
| 3513 |
} |
| 3514 |
if (end < start) { |
| 3515 |
return 0; |
| 3516 |
} |
| 3517 |
|
| 3518 |
/* |
| 3519 |
* (c) the trailing character of the string is already a list-element |
| 3520 |
* separator, Use the same testing routine as TclFindElement to |
| 3521 |
* enforce consistency. |
| 3522 |
*/ |
| 3523 |
|
| 3524 |
if (TclIsSpaceProcM(*end)) { |
| 3525 |
int result = 0; |
| 3526 |
|
| 3527 |
/* |
| 3528 |
* Trailing whitespace might be part of a backslash escape |
| 3529 |
* sequence. Handle that possibility. |
| 3530 |
*/ |
| 3531 |
|
| 3532 |
while ((--end >= start) && (*end == '\\')) { |
| 3533 |
result = !result; |
| 3534 |
} |
| 3535 |
return result; |
| 3536 |
} |
| 3537 |
return 1; |
| 3538 |
} |
| 3539 |
|
| 3540 |
/* |
| 3541 |
*---------------------------------------------------------------------- |
| 3542 |
* |
| 3543 |
* TclFormatInt -- |
| 3544 |
* |
| 3545 |
* This procedure formats an integer into a sequence of decimal digit |
| 3546 |
* characters in a buffer. If the integer is negative, a minus sign is |
| 3547 |
* inserted at the start of the buffer. A null character is inserted at |
| 3548 |
* the end of the formatted characters. It is the caller's responsibility |
| 3549 |
* to ensure that enough storage is available. This procedure has the |
| 3550 |
* effect of sprintf(buffer, "%ld", n) but is faster as proven in |
| 3551 |
* benchmarks. This is key to UpdateStringOfInt, which is a common path |
| 3552 |
* for a lot of code (e.g. int-indexed arrays). |
| 3553 |
* |
| 3554 |
* Results: |
| 3555 |
* An integer representing the number of characters formatted, not |
| 3556 |
* including the terminating \0. |
| 3557 |
* |
| 3558 |
* Side effects: |
| 3559 |
* The formatted characters are written into the storage pointer to by |
| 3560 |
* the "buffer" argument. |
| 3561 |
* |
| 3562 |
*---------------------------------------------------------------------- |
| 3563 |
*/ |
| 3564 |
|
| 3565 |
int |
| 3566 |
TclFormatInt( |
| 3567 |
char *buffer, /* Points to the storage into which the |
| 3568 |
* formatted characters are written. */ |
| 3569 |
long n) /* The integer to format. */ |
| 3570 |
{ |
| 3571 |
unsigned long intVal; |
| 3572 |
int i = 0; |
| 3573 |
int numFormatted, j; |
| 3574 |
static const char digits[] = "0123456789"; |
| 3575 |
|
| 3576 |
/* |
| 3577 |
* Generate the characters of the result backwards in the buffer. |
| 3578 |
*/ |
| 3579 |
|
| 3580 |
intVal = (n < 0 ? -(unsigned long)n : (unsigned long)n); |
| 3581 |
do { |
| 3582 |
buffer[i++] = digits[intVal % 10]; |
| 3583 |
intVal = intVal / 10; |
| 3584 |
} while (intVal > 0); |
| 3585 |
if (n < 0) { |
| 3586 |
buffer[i++] = '-'; |
| 3587 |
} |
| 3588 |
buffer[i] = '\0'; |
| 3589 |
numFormatted = i--; |
| 3590 |
|
| 3591 |
/* |
| 3592 |
* Now reverse the characters. |
| 3593 |
*/ |
| 3594 |
|
| 3595 |
for (j = 0; j < i; j++, i--) { |
| 3596 |
char tmp = buffer[i]; |
| 3597 |
|
| 3598 |
buffer[i] = buffer[j]; |
| 3599 |
buffer[j] = tmp; |
| 3600 |
} |
| 3601 |
return numFormatted; |
| 3602 |
} |
| 3603 |
|
| 3604 |
/* |
| 3605 |
*---------------------------------------------------------------------- |
| 3606 |
* |
| 3607 |
* TclGetIntForIndex -- |
| 3608 |
* |
| 3609 |
* Provides an integer corresponding to the list index held in a Tcl |
| 3610 |
* object. The string value 'objPtr' is expected have the format |
| 3611 |
* integer([+-]integer)? or end([+-]integer)?. |
| 3612 |
* |
| 3613 |
* Value |
| 3614 |
* TCL_OK |
| 3615 |
* |
| 3616 |
* The index is stored at the address given by by 'indexPtr'. If |
| 3617 |
* 'objPtr' has the value "end", the value stored is 'endValue'. |
| 3618 |
* |
| 3619 |
* TCL_ERROR |
| 3620 |
* |
| 3621 |
* The value of 'objPtr' does not have one of the expected formats. If |
| 3622 |
* 'interp' is non-NULL, an error message is left in the interpreter's |
| 3623 |
* result object. |
| 3624 |
* |
| 3625 |
* Effect |
| 3626 |
* |
| 3627 |
* The object referenced by 'objPtr' is converted, as needed, to an |
| 3628 |
* integer, wide integer, or end-based-index object. |
| 3629 |
* |
| 3630 |
*---------------------------------------------------------------------- |
| 3631 |
*/ |
| 3632 |
|
| 3633 |
int |
| 3634 |
TclGetIntForIndex( |
| 3635 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. If |
| 3636 |
* NULL, then no error message is left after |
| 3637 |
* errors. */ |
| 3638 |
Tcl_Obj *objPtr, /* Points to an object containing either "end" |
| 3639 |
* or an integer. */ |
| 3640 |
int endValue, /* The value to be stored at "indexPtr" if |
| 3641 |
* "objPtr" holds "end". */ |
| 3642 |
int *indexPtr) /* Location filled in with an integer |
| 3643 |
* representing an index. */ |
| 3644 |
{ |
| 3645 |
int length; |
| 3646 |
char *opPtr; |
| 3647 |
const char *bytes; |
| 3648 |
|
| 3649 |
if (TclGetIntFromObj(NULL, objPtr, indexPtr) == TCL_OK) { |
| 3650 |
return TCL_OK; |
| 3651 |
} |
| 3652 |
|
| 3653 |
if (GetEndOffsetFromObj(objPtr, endValue, indexPtr) == TCL_OK) { |
| 3654 |
return TCL_OK; |
| 3655 |
} |
| 3656 |
|
| 3657 |
bytes = TclGetStringFromObj(objPtr, &length); |
| 3658 |
|
| 3659 |
/* |
| 3660 |
* Leading whitespace is acceptable in an index. |
| 3661 |
*/ |
| 3662 |
|
| 3663 |
while (length && TclIsSpaceProcM(*bytes)) { |
| 3664 |
bytes++; |
| 3665 |
length--; |
| 3666 |
} |
| 3667 |
|
| 3668 |
if (TclParseNumber(NULL, NULL, NULL, bytes, length, (const char **)&opPtr, |
| 3669 |
TCL_PARSE_INTEGER_ONLY | TCL_PARSE_NO_WHITESPACE) == TCL_OK) { |
| 3670 |
int code, first, second; |
| 3671 |
char savedOp = *opPtr; |
| 3672 |
|
| 3673 |
if ((savedOp != '+') && (savedOp != '-')) { |
| 3674 |
goto parseError; |
| 3675 |
} |
| 3676 |
if (TclIsSpaceProcM(opPtr[1])) { |
| 3677 |
goto parseError; |
| 3678 |
} |
| 3679 |
*opPtr = '\0'; |
| 3680 |
code = Tcl_GetInt(interp, bytes, &first); |
| 3681 |
*opPtr = savedOp; |
| 3682 |
if (code == TCL_ERROR) { |
| 3683 |
goto parseError; |
| 3684 |
} |
| 3685 |
if (TCL_ERROR == Tcl_GetInt(interp, opPtr+1, &second)) { |
| 3686 |
goto parseError; |
| 3687 |
} |
| 3688 |
if (savedOp == '+') { |
| 3689 |
*indexPtr = first + second; |
| 3690 |
} else { |
| 3691 |
*indexPtr = first - second; |
| 3692 |
} |
| 3693 |
return TCL_OK; |
| 3694 |
} |
| 3695 |
|
| 3696 |
/* |
| 3697 |
* Report a parse error. |
| 3698 |
*/ |
| 3699 |
|
| 3700 |
parseError: |
| 3701 |
if (interp != NULL) { |
| 3702 |
bytes = TclGetString(objPtr); |
| 3703 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3704 |
"bad index \"%s\": must be integer?[+-]integer? or" |
| 3705 |
" end?[+-]integer?", bytes)); |
| 3706 |
if (!strncmp(bytes, "end-", 4)) { |
| 3707 |
bytes += 4; |
| 3708 |
} |
| 3709 |
TclCheckBadOctal(interp, bytes); |
| 3710 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", (char *)NULL); |
| 3711 |
} |
| 3712 |
|
| 3713 |
return TCL_ERROR; |
| 3714 |
} |
| 3715 |
|
| 3716 |
/* |
| 3717 |
*---------------------------------------------------------------------- |
| 3718 |
* |
| 3719 |
* UpdateStringOfEndOffset -- |
| 3720 |
* |
| 3721 |
* Update the string rep of a Tcl object holding an "end-offset" |
| 3722 |
* expression. |
| 3723 |
* |
| 3724 |
* Results: |
| 3725 |
* None. |
| 3726 |
* |
| 3727 |
* Side effects: |
| 3728 |
* Stores a valid string in the object's string rep. |
| 3729 |
* |
| 3730 |
* This function does NOT free any earlier string rep. If it is called on an |
| 3731 |
* object that already has a valid string rep, it will leak memory. |
| 3732 |
* |
| 3733 |
*---------------------------------------------------------------------- |
| 3734 |
*/ |
| 3735 |
|
| 3736 |
static void |
| 3737 |
UpdateStringOfEndOffset( |
| 3738 |
Tcl_Obj *objPtr) |
| 3739 |
{ |
| 3740 |
char buffer[TCL_INTEGER_SPACE + 5]; |
| 3741 |
int len = 3; |
| 3742 |
|
| 3743 |
memcpy(buffer, "end", 4); |
| 3744 |
if (objPtr->internalRep.longValue != 0) { |
| 3745 |
buffer[len++] = '-'; |
| 3746 |
len += TclFormatInt(buffer+len, |
| 3747 |
(long)(-(unsigned long)(objPtr->internalRep.longValue))); |
| 3748 |
} |
| 3749 |
objPtr->bytes = (char *)ckalloc(len+1); |
| 3750 |
memcpy(objPtr->bytes, buffer, len+1); |
| 3751 |
objPtr->length = len; |
| 3752 |
} |
| 3753 |
|
| 3754 |
/* |
| 3755 |
*---------------------------------------------------------------------- |
| 3756 |
* |
| 3757 |
* GetEndOffsetFromObj -- |
| 3758 |
* |
| 3759 |
* Look for a string of the form "end[+-]offset" and convert it to an |
| 3760 |
* internal representation holding the offset. |
| 3761 |
* |
| 3762 |
* Results: |
| 3763 |
* Tcl return code. |
| 3764 |
* |
| 3765 |
* Side effects: |
| 3766 |
* May store a Tcl_ObjType. |
| 3767 |
* |
| 3768 |
*---------------------------------------------------------------------- |
| 3769 |
*/ |
| 3770 |
|
| 3771 |
static int |
| 3772 |
GetEndOffsetFromObj( |
| 3773 |
Tcl_Obj *objPtr, /* Pointer to the object to parse */ |
| 3774 |
int endValue, /* The value to be stored at "indexPtr" if |
| 3775 |
* "objPtr" holds "end". */ |
| 3776 |
int *indexPtr) /* Location filled in with an integer |
| 3777 |
* representing an index. */ |
| 3778 |
{ |
| 3779 |
if (SetEndOffsetFromAny(NULL, objPtr) != TCL_OK) { |
| 3780 |
return TCL_ERROR; |
| 3781 |
} |
| 3782 |
|
| 3783 |
/* TODO: Handle overflow cases sensibly */ |
| 3784 |
*indexPtr = endValue + (int)objPtr->internalRep.longValue; |
| 3785 |
return TCL_OK; |
| 3786 |
} |
| 3787 |
|
| 3788 |
|
| 3789 |
/* |
| 3790 |
*---------------------------------------------------------------------- |
| 3791 |
* |
| 3792 |
* SetEndOffsetFromAny -- |
| 3793 |
* |
| 3794 |
* Look for a string of the form "end[+-]offset" and convert it to an |
| 3795 |
* internal representation holding the offset. |
| 3796 |
* |
| 3797 |
* Results: |
| 3798 |
* Returns TCL_OK if ok, TCL_ERROR if the string was badly formed. |
| 3799 |
* |
| 3800 |
* Side effects: |
| 3801 |
* If interp is not NULL, stores an error message in the interpreter |
| 3802 |
* result. |
| 3803 |
* |
| 3804 |
*---------------------------------------------------------------------- |
| 3805 |
*/ |
| 3806 |
|
| 3807 |
static int |
| 3808 |
SetEndOffsetFromAny( |
| 3809 |
Tcl_Interp *interp, /* Tcl interpreter or NULL */ |
| 3810 |
Tcl_Obj *objPtr) /* Pointer to the object to parse */ |
| 3811 |
{ |
| 3812 |
int offset; /* Offset in the "end-offset" expression */ |
| 3813 |
const char *bytes; /* String rep of the object */ |
| 3814 |
int length; /* Length of the object's string rep */ |
| 3815 |
|
| 3816 |
/* |
| 3817 |
* If it's already the right type, we're fine. |
| 3818 |
*/ |
| 3819 |
|
| 3820 |
if (objPtr->typePtr == &tclEndOffsetType) { |
| 3821 |
return TCL_OK; |
| 3822 |
} |
| 3823 |
|
| 3824 |
/* |
| 3825 |
* Check for a string rep of the right form. |
| 3826 |
*/ |
| 3827 |
|
| 3828 |
bytes = TclGetStringFromObj(objPtr, &length); |
| 3829 |
if ((*bytes != 'e') || (strncmp(bytes, "end", |
| 3830 |
(size_t)((length > 3) ? 3 : length)) != 0)) { |
| 3831 |
if (interp != NULL) { |
| 3832 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3833 |
"bad index \"%s\": must be end?[+-]integer?", bytes)); |
| 3834 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", (char *)NULL); |
| 3835 |
} |
| 3836 |
return TCL_ERROR; |
| 3837 |
} |
| 3838 |
|
| 3839 |
/* |
| 3840 |
* Convert the string rep. |
| 3841 |
*/ |
| 3842 |
|
| 3843 |
if (length <= 3) { |
| 3844 |
offset = 0; |
| 3845 |
} else if ((length > 4) && ((bytes[3] == '-') || (bytes[3] == '+'))) { |
| 3846 |
/* |
| 3847 |
* This is our limited string expression evaluator. Pass everything |
| 3848 |
* after "end-" to Tcl_GetInt, then reverse for offset. |
| 3849 |
*/ |
| 3850 |
|
| 3851 |
if (TclIsSpaceProcM(bytes[4])) { |
| 3852 |
goto badIndexFormat; |
| 3853 |
} |
| 3854 |
if (Tcl_GetInt(interp, bytes+4, &offset) != TCL_OK) { |
| 3855 |
return TCL_ERROR; |
| 3856 |
} |
| 3857 |
if (bytes[3] == '-') { |
| 3858 |
|
| 3859 |
offset = (int)(-(unsigned int)offset); |
| 3860 |
} |
| 3861 |
} else { |
| 3862 |
/* |
| 3863 |
* Conversion failed. Report the error. |
| 3864 |
*/ |
| 3865 |
|
| 3866 |
badIndexFormat: |
| 3867 |
if (interp != NULL) { |
| 3868 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3869 |
"bad index \"%s\": must be end?[+-]integer?", bytes)); |
| 3870 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", (char *)NULL); |
| 3871 |
} |
| 3872 |
return TCL_ERROR; |
| 3873 |
} |
| 3874 |
|
| 3875 |
/* |
| 3876 |
* The conversion succeeded. Free the old internal rep and set the new |
| 3877 |
* one. |
| 3878 |
*/ |
| 3879 |
|
| 3880 |
TclFreeIntRep(objPtr); |
| 3881 |
objPtr->internalRep.longValue = offset; |
| 3882 |
objPtr->typePtr = &tclEndOffsetType; |
| 3883 |
|
| 3884 |
return TCL_OK; |
| 3885 |
} |
| 3886 |
|
| 3887 |
/* |
| 3888 |
*---------------------------------------------------------------------- |
| 3889 |
* |
| 3890 |
* TclIndexEncode -- |
| 3891 |
* |
| 3892 |
* Parse objPtr to determine if it is an index value. Two cases |
| 3893 |
* are possible. The value objPtr might be parsed as an absolute |
| 3894 |
* index value in the C signed int range. Note that this includes |
| 3895 |
* index values that are integers as presented and it includes index |
| 3896 |
* arithmetic expressions. The absolute index values that can be |
| 3897 |
* directly meaningful as an index into either a list or a string are |
| 3898 |
* those integer values >= TCL_INDEX_START (0) |
| 3899 |
* and < TCL_INDEX_AFTER (INT_MAX). |
| 3900 |
* The largest string supported in Tcl 8 has bytelength INT_MAX. |
| 3901 |
* This means the largest supported character length is also INT_MAX, |
| 3902 |
* and the index of the last character in a string of length INT_MAX |
| 3903 |
* is INT_MAX-1. |
| 3904 |
* |
| 3905 |
* Any absolute index value parsed outside that range is encoded |
| 3906 |
* using the before and after values passed in by the |
| 3907 |
* caller as the encoding to use for indices that are either |
| 3908 |
* less than or greater than the usable index range. TCL_INDEX_AFTER |
| 3909 |
* is available as a good choice for most callers to use for |
| 3910 |
* after. Likewise, the value TCL_INDEX_BEFORE is good for |
| 3911 |
* most callers to use for before. Other values are possible |
| 3912 |
* when the caller knows it is helpful in producing its own behavior |
| 3913 |
* for indices before and after the indexed item. |
| 3914 |
* |
| 3915 |
* A token can also be parsed as an end-relative index expression. |
| 3916 |
* All end-relative expressions that indicate an index larger |
| 3917 |
* than end (end+2, end--5) point beyond the end of the indexed |
| 3918 |
* collection, and can be encoded as after. The end-relative |
| 3919 |
* expressions that indicate an index less than or equal to end |
| 3920 |
* are encoded relative to the value TCL_INDEX_END (-2). The |
| 3921 |
* index "end" is encoded as -2, down to the index "end-0x7FFFFFFE" |
| 3922 |
* which is encoded as INT_MIN. Since the largest index into a |
| 3923 |
* string possible in Tcl 8 is 0x7FFFFFFE, the interpretation of |
| 3924 |
* "end-0x7FFFFFFE" for that largest string would be 0. Thus, |
| 3925 |
* if the tokens "end-0x7FFFFFFF" or "end+-0x80000000" are parsed, |
| 3926 |
* they can be encoded with the before value. |
| 3927 |
* |
| 3928 |
* These details will require re-examination whenever string and |
| 3929 |
* list length limits are increased, but that will likely also |
| 3930 |
* mean a revised routine capable of returning Tcl_WideInt values. |
| 3931 |
* |
| 3932 |
* Returns: |
| 3933 |
* TCL_OK if parsing succeeded, and TCL_ERROR if it failed. |
| 3934 |
* |
| 3935 |
* Side effects: |
| 3936 |
* When TCL_OK is returned, the encoded index value is written |
| 3937 |
* to *indexPtr. |
| 3938 |
* |
| 3939 |
*---------------------------------------------------------------------- |
| 3940 |
*/ |
| 3941 |
|
| 3942 |
int |
| 3943 |
TclIndexEncode( |
| 3944 |
Tcl_Interp *interp, /* For error reporting, may be NULL */ |
| 3945 |
Tcl_Obj *objPtr, /* Index value to parse */ |
| 3946 |
int before, /* Value to return for index before beginning */ |
| 3947 |
int after, /* Value to return for index after end */ |
| 3948 |
int *indexPtr) /* Where to write the encoded answer, not NULL */ |
| 3949 |
{ |
| 3950 |
int idx; |
| 3951 |
|
| 3952 |
if (TCL_OK == TclGetIntFromObj(NULL, objPtr, &idx)) { |
| 3953 |
/* We parsed a value in the range INT_MIN...INT_MAX */ |
| 3954 |
integerEncode: |
| 3955 |
if (idx < TCL_INDEX_START) { |
| 3956 |
/* All negative absolute indices are "before the beginning" */ |
| 3957 |
idx = before; |
| 3958 |
} else if (idx == INT_MAX) { |
| 3959 |
/* This index value is always "after the end" */ |
| 3960 |
idx = after; |
| 3961 |
} |
| 3962 |
/* usual case, the absolute index value encodes itself */ |
| 3963 |
} else if (TCL_OK == GetEndOffsetFromObj(objPtr, 0, &idx)) { |
| 3964 |
/* |
| 3965 |
* We parsed an end+offset index value. |
| 3966 |
* idx holds the offset value in the range INT_MIN...INT_MAX. |
| 3967 |
*/ |
| 3968 |
if (idx > 0) { |
| 3969 |
/* |
| 3970 |
* All end+positive or end-negative expressions |
| 3971 |
* always indicate "after the end". |
| 3972 |
*/ |
| 3973 |
idx = after; |
| 3974 |
} else if (idx < INT_MIN - TCL_INDEX_END) { |
| 3975 |
/* These indices always indicate "before the beginning" */ |
| 3976 |
idx = before; |
| 3977 |
} else { |
| 3978 |
/* Encoded end-positive (or end+negative) are offset */ |
| 3979 |
idx += TCL_INDEX_END; |
| 3980 |
} |
| 3981 |
|
| 3982 |
/* TODO: Consider flag to suppress repeated end-offset parse. */ |
| 3983 |
} else if (TCL_OK == TclGetIntForIndexM(interp, objPtr, 0, &idx)) { |
| 3984 |
/* |
| 3985 |
* Only reach this case when the index value is a |
| 3986 |
* constant index arithmetic expression, and idx |
| 3987 |
* holds the result. Treat it the same as if it were |
| 3988 |
* parsed as an absolute integer value. |
| 3989 |
*/ |
| 3990 |
goto integerEncode; |
| 3991 |
} else { |
| 3992 |
return TCL_ERROR; |
| 3993 |
} |
| 3994 |
*indexPtr = idx; |
| 3995 |
return TCL_OK; |
| 3996 |
} |
| 3997 |
|
| 3998 |
/* |
| 3999 |
*---------------------------------------------------------------------- |
| 4000 |
* |
| 4001 |
* TclIndexDecode -- |
| 4002 |
* |
| 4003 |
* Decodes a value previously encoded by TclIndexEncode. The argument |
| 4004 |
* endValue indicates what value of "end" should be used in the |
| 4005 |
* decoding. |
| 4006 |
* |
| 4007 |
* Results: |
| 4008 |
* The decoded index value. |
| 4009 |
* |
| 4010 |
*---------------------------------------------------------------------- |
| 4011 |
*/ |
| 4012 |
|
| 4013 |
int |
| 4014 |
TclIndexDecode( |
| 4015 |
int encoded, /* Value to decode */ |
| 4016 |
int endValue) /* Meaning of "end" to use, > TCL_INDEX_END */ |
| 4017 |
{ |
| 4018 |
if (encoded <= TCL_INDEX_END) { |
| 4019 |
return (encoded - TCL_INDEX_END) + endValue; |
| 4020 |
} |
| 4021 |
return encoded; |
| 4022 |
} |
| 4023 |
|
| 4024 |
/* |
| 4025 |
*---------------------------------------------------------------------- |
| 4026 |
* |
| 4027 |
* TclCheckBadOctal -- |
| 4028 |
* |
| 4029 |
* This function checks for a bad octal value and appends a meaningful |
| 4030 |
* error to the interp's result. |
| 4031 |
* |
| 4032 |
* Results: |
| 4033 |
* 1 if the argument was a bad octal, else 0. |
| 4034 |
* |
| 4035 |
* Side effects: |
| 4036 |
* The interpreter's result is modified. |
| 4037 |
* |
| 4038 |
*---------------------------------------------------------------------- |
| 4039 |
*/ |
| 4040 |
|
| 4041 |
int |
| 4042 |
TclCheckBadOctal( |
| 4043 |
Tcl_Interp *interp, /* Interpreter to use for error reporting. If |
| 4044 |
* NULL, then no error message is left after |
| 4045 |
* errors. */ |
| 4046 |
const char *value) /* String to check. */ |
| 4047 |
{ |
| 4048 |
const char *p = value; |
| 4049 |
|
| 4050 |
/* |
| 4051 |
* A frequent mistake is invalid octal values due to an unwanted leading |
| 4052 |
* zero. Try to generate a meaningful error message. |
| 4053 |
*/ |
| 4054 |
|
| 4055 |
while (TclIsSpaceProcM(*p)) { |
| 4056 |
p++; |
| 4057 |
} |
| 4058 |
if (*p == '+' || *p == '-') { |
| 4059 |
p++; |
| 4060 |
} |
| 4061 |
if (*p == '0') { |
| 4062 |
if ((p[1] == 'o') || p[1] == 'O') { |
| 4063 |
p += 2; |
| 4064 |
} |
| 4065 |
while (isdigit(UCHAR(*p))) { /* INTL: digit. */ |
| 4066 |
p++; |
| 4067 |
} |
| 4068 |
while (TclIsSpaceProcM(*p)) { |
| 4069 |
p++; |
| 4070 |
} |
| 4071 |
if (*p == '\0') { |
| 4072 |
/* |
| 4073 |
* Reached end of string. |
| 4074 |
*/ |
| 4075 |
|
| 4076 |
if (interp != NULL) { |
| 4077 |
/* |
| 4078 |
* Don't reset the result here because we want this result to |
| 4079 |
* be added to an existing error message as extra info. |
| 4080 |
*/ |
| 4081 |
|
| 4082 |
Tcl_AppendToObj(Tcl_GetObjResult(interp), |
| 4083 |
" (looks like invalid octal number)", -1); |
| 4084 |
} |
| 4085 |
return 1; |
| 4086 |
} |
| 4087 |
} |
| 4088 |
return 0; |
| 4089 |
} |
| 4090 |
|
| 4091 |
/* |
| 4092 |
*---------------------------------------------------------------------- |
| 4093 |
* |
| 4094 |
* ClearHash -- |
| 4095 |
* |
| 4096 |
* Remove all the entries in the hash table *tablePtr. |
| 4097 |
* |
| 4098 |
*---------------------------------------------------------------------- |
| 4099 |
*/ |
| 4100 |
|
| 4101 |
static void |
| 4102 |
ClearHash( |
| 4103 |
Tcl_HashTable *tablePtr) |
| 4104 |
{ |
| 4105 |
Tcl_HashSearch search; |
| 4106 |
Tcl_HashEntry *hPtr; |
| 4107 |
|
| 4108 |
for (hPtr = Tcl_FirstHashEntry(tablePtr, &search); hPtr != NULL; |
| 4109 |
hPtr = Tcl_NextHashEntry(&search)) { |
| 4110 |
Tcl_Obj *objPtr = (Tcl_Obj *)Tcl_GetHashValue(hPtr); |
| 4111 |
|
| 4112 |
Tcl_DecrRefCount(objPtr); |
| 4113 |
Tcl_DeleteHashEntry(hPtr); |
| 4114 |
} |
| 4115 |
} |
| 4116 |
|
| 4117 |
/* |
| 4118 |
*---------------------------------------------------------------------- |
| 4119 |
* |
| 4120 |
* GetThreadHash -- |
| 4121 |
* |
| 4122 |
* Get a thread-specific (Tcl_HashTable *) associated with a thread data |
| 4123 |
* key. |
| 4124 |
* |
| 4125 |
* Results: |
| 4126 |
* The Tcl_HashTable * corresponding to *keyPtr. |
| 4127 |
* |
| 4128 |
* Side effects: |
| 4129 |
* The first call on a keyPtr in each thread creates a new Tcl_HashTable, |
| 4130 |
* and registers a thread exit handler to dispose of it. |
| 4131 |
* |
| 4132 |
*---------------------------------------------------------------------- |
| 4133 |
*/ |
| 4134 |
|
| 4135 |
static Tcl_HashTable * |
| 4136 |
GetThreadHash( |
| 4137 |
Tcl_ThreadDataKey *keyPtr) |
| 4138 |
{ |
| 4139 |
Tcl_HashTable **tablePtrPtr = |
| 4140 |
(Tcl_HashTable **)Tcl_GetThreadData(keyPtr, sizeof(Tcl_HashTable *)); |
| 4141 |
|
| 4142 |
if (NULL == *tablePtrPtr) { |
| 4143 |
*tablePtrPtr = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable)); |
| 4144 |
Tcl_CreateThreadExitHandler(FreeThreadHash, *tablePtrPtr); |
| 4145 |
Tcl_InitHashTable(*tablePtrPtr, TCL_ONE_WORD_KEYS); |
| 4146 |
} |
| 4147 |
return *tablePtrPtr; |
| 4148 |
} |
| 4149 |
|
| 4150 |
/* |
| 4151 |
*---------------------------------------------------------------------- |
| 4152 |
* |
| 4153 |
* FreeThreadHash -- |
| 4154 |
* |
| 4155 |
* Thread exit handler used by GetThreadHash to dispose of a thread hash |
| 4156 |
* table. |
| 4157 |
* |
| 4158 |
* Side effects: |
| 4159 |
* Frees a Tcl_HashTable. |
| 4160 |
* |
| 4161 |
*---------------------------------------------------------------------- |
| 4162 |
*/ |
| 4163 |
|
| 4164 |
static void |
| 4165 |
FreeThreadHash( |
| 4166 |
ClientData clientData) |
| 4167 |
{ |
| 4168 |
Tcl_HashTable *tablePtr = (Tcl_HashTable *)clientData; |
| 4169 |
|
| 4170 |
ClearHash(tablePtr); |
| 4171 |
Tcl_DeleteHashTable(tablePtr); |
| 4172 |
ckfree(tablePtr); |
| 4173 |
} |
| 4174 |
|
| 4175 |
/* |
| 4176 |
*---------------------------------------------------------------------- |
| 4177 |
* |
| 4178 |
* FreeProcessGlobalValue -- |
| 4179 |
* |
| 4180 |
* Exit handler used by Tcl(Set|Get)ProcessGlobalValue to cleanup a |
| 4181 |
* ProcessGlobalValue at exit. |
| 4182 |
* |
| 4183 |
*---------------------------------------------------------------------- |
| 4184 |
*/ |
| 4185 |
|
| 4186 |
static void |
| 4187 |
FreeProcessGlobalValue( |
| 4188 |
ClientData clientData) |
| 4189 |
{ |
| 4190 |
ProcessGlobalValue *pgvPtr = (ProcessGlobalValue *)clientData; |
| 4191 |
|
| 4192 |
pgvPtr->epoch++; |
| 4193 |
pgvPtr->numBytes = 0; |
| 4194 |
ckfree(pgvPtr->value); |
| 4195 |
pgvPtr->value = NULL; |
| 4196 |
if (pgvPtr->encoding) { |
| 4197 |
Tcl_FreeEncoding(pgvPtr->encoding); |
| 4198 |
pgvPtr->encoding = NULL; |
| 4199 |
} |
| 4200 |
Tcl_MutexFinalize(&pgvPtr->mutex); |
| 4201 |
} |
| 4202 |
|
| 4203 |
/* |
| 4204 |
*---------------------------------------------------------------------- |
| 4205 |
* |
| 4206 |
* TclSetProcessGlobalValue -- |
| 4207 |
* |
| 4208 |
* Utility routine to set a global value shared by all threads in the |
| 4209 |
* process while keeping a thread-local copy as well. |
| 4210 |
* |
| 4211 |
*---------------------------------------------------------------------- |
| 4212 |
*/ |
| 4213 |
|
| 4214 |
void |
| 4215 |
TclSetProcessGlobalValue( |
| 4216 |
ProcessGlobalValue *pgvPtr, |
| 4217 |
Tcl_Obj *newValue, |
| 4218 |
Tcl_Encoding encoding) |
| 4219 |
{ |
| 4220 |
const char *bytes; |
| 4221 |
Tcl_HashTable *cacheMap; |
| 4222 |
Tcl_HashEntry *hPtr; |
| 4223 |
int dummy; |
| 4224 |
Tcl_DString ds; |
| 4225 |
|
| 4226 |
Tcl_MutexLock(&pgvPtr->mutex); |
| 4227 |
|
| 4228 |
/* |
| 4229 |
* Fill the global string value. |
| 4230 |
*/ |
| 4231 |
|
| 4232 |
pgvPtr->epoch++; |
| 4233 |
if (NULL != pgvPtr->value) { |
| 4234 |
ckfree(pgvPtr->value); |
| 4235 |
} else { |
| 4236 |
Tcl_CreateExitHandler(FreeProcessGlobalValue, pgvPtr); |
| 4237 |
} |
| 4238 |
bytes = TclGetStringFromObj(newValue, &pgvPtr->numBytes); |
| 4239 |
Tcl_UtfToExternalDString(encoding, bytes, pgvPtr->numBytes, &ds); |
| 4240 |
pgvPtr->numBytes = Tcl_DStringLength(&ds); |
| 4241 |
pgvPtr->value = (char *)ckalloc(pgvPtr->numBytes + 1); |
| 4242 |
memcpy(pgvPtr->value, Tcl_DStringValue(&ds), pgvPtr->numBytes + 1); |
| 4243 |
Tcl_DStringFree(&ds); |
| 4244 |
if (pgvPtr->encoding) { |
| 4245 |
Tcl_FreeEncoding(pgvPtr->encoding); |
| 4246 |
} |
| 4247 |
pgvPtr->encoding = encoding; |
| 4248 |
|
| 4249 |
/* |
| 4250 |
* Fill the local thread copy directly with the Tcl_Obj value to avoid |
| 4251 |
* loss of the internalrep. Increment newValue refCount early to handle case |
| 4252 |
* where we set a PGV to itself. |
| 4253 |
*/ |
| 4254 |
|
| 4255 |
Tcl_IncrRefCount(newValue); |
| 4256 |
cacheMap = GetThreadHash(&pgvPtr->key); |
| 4257 |
ClearHash(cacheMap); |
| 4258 |
hPtr = Tcl_CreateHashEntry(cacheMap, INT2PTR(pgvPtr->epoch), &dummy); |
| 4259 |
Tcl_SetHashValue(hPtr, newValue); |
| 4260 |
Tcl_MutexUnlock(&pgvPtr->mutex); |
| 4261 |
} |
| 4262 |
|
| 4263 |
/* |
| 4264 |
*---------------------------------------------------------------------- |
| 4265 |
* |
| 4266 |
* TclGetProcessGlobalValue -- |
| 4267 |
* |
| 4268 |
* Retrieve a global value shared among all threads of the process, |
| 4269 |
* preferring a thread-local copy as long as it remains valid. |
| 4270 |
* |
| 4271 |
* Results: |
| 4272 |
* Returns a (Tcl_Obj *) that holds a copy of the global value. |
| 4273 |
* |
| 4274 |
*---------------------------------------------------------------------- |
| 4275 |
*/ |
| 4276 |
|
| 4277 |
Tcl_Obj * |
| 4278 |
TclGetProcessGlobalValue( |
| 4279 |
ProcessGlobalValue *pgvPtr) |
| 4280 |
{ |
| 4281 |
Tcl_Obj *value = NULL; |
| 4282 |
Tcl_HashTable *cacheMap; |
| 4283 |
Tcl_HashEntry *hPtr; |
| 4284 |
int epoch = pgvPtr->epoch; |
| 4285 |
Tcl_DString newValue; |
| 4286 |
|
| 4287 |
if (pgvPtr->encoding) { |
| 4288 |
Tcl_Encoding current = Tcl_GetEncoding(NULL, NULL); |
| 4289 |
|
| 4290 |
if (pgvPtr->encoding != current) { |
| 4291 |
/* |
| 4292 |
* The system encoding has changed since the global string value |
| 4293 |
* was saved. Convert the global value to be based on the new |
| 4294 |
* system encoding. |
| 4295 |
*/ |
| 4296 |
|
| 4297 |
Tcl_DString native; |
| 4298 |
|
| 4299 |
Tcl_MutexLock(&pgvPtr->mutex); |
| 4300 |
epoch = ++pgvPtr->epoch; |
| 4301 |
Tcl_UtfToExternalDString(pgvPtr->encoding, pgvPtr->value, |
| 4302 |
pgvPtr->numBytes, &native); |
| 4303 |
Tcl_ExternalToUtfDString(current, Tcl_DStringValue(&native), |
| 4304 |
Tcl_DStringLength(&native), &newValue); |
| 4305 |
Tcl_DStringFree(&native); |
| 4306 |
ckfree(pgvPtr->value); |
| 4307 |
pgvPtr->value = (char *)ckalloc(Tcl_DStringLength(&newValue) + 1); |
| 4308 |
memcpy(pgvPtr->value, Tcl_DStringValue(&newValue), |
| 4309 |
Tcl_DStringLength(&newValue) + 1); |
| 4310 |
Tcl_DStringFree(&newValue); |
| 4311 |
Tcl_FreeEncoding(pgvPtr->encoding); |
| 4312 |
pgvPtr->encoding = current; |
| 4313 |
Tcl_MutexUnlock(&pgvPtr->mutex); |
| 4314 |
} else { |
| 4315 |
Tcl_FreeEncoding(current); |
| 4316 |
} |
| 4317 |
} |
| 4318 |
cacheMap = GetThreadHash(&pgvPtr->key); |
| 4319 |
hPtr = Tcl_FindHashEntry(cacheMap, INT2PTR(epoch)); |
| 4320 |
if (NULL == hPtr) { |
| 4321 |
int dummy; |
| 4322 |
|
| 4323 |
/* |
| 4324 |
* No cache for the current epoch - must be a new one. |
| 4325 |
* |
| 4326 |
* First, clear the cacheMap, as anything in it must refer to some |
| 4327 |
* expired epoch. |
| 4328 |
*/ |
| 4329 |
|
| 4330 |
ClearHash(cacheMap); |
| 4331 |
|
| 4332 |
/* |
| 4333 |
* If no thread has set the shared value, call the initializer. |
| 4334 |
*/ |
| 4335 |
|
| 4336 |
Tcl_MutexLock(&pgvPtr->mutex); |
| 4337 |
if ((NULL == pgvPtr->value) && (pgvPtr->proc)) { |
| 4338 |
pgvPtr->epoch++; |
| 4339 |
pgvPtr->proc(&pgvPtr->value,&pgvPtr->numBytes,&pgvPtr->encoding); |
| 4340 |
if (pgvPtr->value == NULL) { |
| 4341 |
Tcl_Panic("PGV Initializer did not initialize"); |
| 4342 |
} |
| 4343 |
Tcl_CreateExitHandler(FreeProcessGlobalValue, pgvPtr); |
| 4344 |
} |
| 4345 |
|
| 4346 |
/* |
| 4347 |
* Store a copy of the shared value (but then in utf-8) |
| 4348 |
* in our epoch-indexed cache. |
| 4349 |
*/ |
| 4350 |
|
| 4351 |
Tcl_ExternalToUtfDString(NULL, pgvPtr->value, pgvPtr->numBytes, &newValue); |
| 4352 |
value = TclDStringToObj(&newValue); |
| 4353 |
hPtr = Tcl_CreateHashEntry(cacheMap, |
| 4354 |
INT2PTR(pgvPtr->epoch), &dummy); |
| 4355 |
Tcl_MutexUnlock(&pgvPtr->mutex); |
| 4356 |
Tcl_SetHashValue(hPtr, value); |
| 4357 |
Tcl_IncrRefCount(value); |
| 4358 |
} |
| 4359 |
return (Tcl_Obj *)Tcl_GetHashValue(hPtr); |
| 4360 |
} |
| 4361 |
|
| 4362 |
/* |
| 4363 |
*---------------------------------------------------------------------- |
| 4364 |
* |
| 4365 |
* TclSetObjNameOfExecutable -- |
| 4366 |
* |
| 4367 |
* This function stores the absolute pathname of the executable file |
| 4368 |
* (normally as computed by TclpFindExecutable). |
| 4369 |
* |
| 4370 |
* Results: |
| 4371 |
* None. |
| 4372 |
* |
| 4373 |
* Side effects: |
| 4374 |
* Stores the executable name. |
| 4375 |
* |
| 4376 |
*---------------------------------------------------------------------- |
| 4377 |
*/ |
| 4378 |
|
| 4379 |
void |
| 4380 |
TclSetObjNameOfExecutable( |
| 4381 |
Tcl_Obj *name, |
| 4382 |
Tcl_Encoding encoding) |
| 4383 |
{ |
| 4384 |
TclSetProcessGlobalValue(&executableName, name, encoding); |
| 4385 |
} |
| 4386 |
|
| 4387 |
/* |
| 4388 |
*---------------------------------------------------------------------- |
| 4389 |
* |
| 4390 |
* TclGetObjNameOfExecutable -- |
| 4391 |
* |
| 4392 |
* This function retrieves the absolute pathname of the application in |
| 4393 |
* which the Tcl library is running, usually as previously stored by |
| 4394 |
* TclpFindExecutable(). This function call is the C API equivalent to |
| 4395 |
* the "info nameofexecutable" command. |
| 4396 |
* |
| 4397 |
* Results: |
| 4398 |
* A pointer to an "fsPath" Tcl_Obj, or to an empty Tcl_Obj if the |
| 4399 |
* pathname of the application is unknown. |
| 4400 |
* |
| 4401 |
* Side effects: |
| 4402 |
* None. |
| 4403 |
* |
| 4404 |
*---------------------------------------------------------------------- |
| 4405 |
*/ |
| 4406 |
|
| 4407 |
Tcl_Obj * |
| 4408 |
TclGetObjNameOfExecutable(void) |
| 4409 |
{ |
| 4410 |
return TclGetProcessGlobalValue(&executableName); |
| 4411 |
} |
| 4412 |
|
| 4413 |
/* |
| 4414 |
*---------------------------------------------------------------------- |
| 4415 |
* |
| 4416 |
* Tcl_GetNameOfExecutable -- |
| 4417 |
* |
| 4418 |
* This function retrieves the absolute pathname of the application in |
| 4419 |
* which the Tcl library is running, and returns it in string form. |
| 4420 |
* |
| 4421 |
* The returned string belongs to Tcl and should be copied if the caller |
| 4422 |
* plans to keep it, to guard against it becoming invalid. |
| 4423 |
* |
| 4424 |
* Results: |
| 4425 |
* A pointer to the internal string or NULL if the internal full path |
| 4426 |
* name has not been computed or unknown. |
| 4427 |
* |
| 4428 |
* Side effects: |
| 4429 |
* None. |
| 4430 |
* |
| 4431 |
*---------------------------------------------------------------------- |
| 4432 |
*/ |
| 4433 |
|
| 4434 |
const char * |
| 4435 |
Tcl_GetNameOfExecutable(void) |
| 4436 |
{ |
| 4437 |
int numBytes; |
| 4438 |
const char *bytes = |
| 4439 |
Tcl_GetStringFromObj(TclGetObjNameOfExecutable(), &numBytes); |
| 4440 |
|
| 4441 |
if (numBytes == 0) { |
| 4442 |
return NULL; |
| 4443 |
} |
| 4444 |
return bytes; |
| 4445 |
} |
| 4446 |
|
| 4447 |
/* |
| 4448 |
*---------------------------------------------------------------------- |
| 4449 |
* |
| 4450 |
* TclpGetTime -- |
| 4451 |
* |
| 4452 |
* Deprecated synonym for Tcl_GetTime. This function is provided for the |
| 4453 |
* benefit of extensions written before Tcl_GetTime was exported from the |
| 4454 |
* library. |
| 4455 |
* |
| 4456 |
* Results: |
| 4457 |
* None. |
| 4458 |
* |
| 4459 |
* Side effects: |
| 4460 |
* Stores current time in the buffer designated by "timePtr" |
| 4461 |
* |
| 4462 |
*---------------------------------------------------------------------- |
| 4463 |
*/ |
| 4464 |
|
| 4465 |
void |
| 4466 |
TclpGetTime( |
| 4467 |
Tcl_Time *timePtr) |
| 4468 |
{ |
| 4469 |
Tcl_GetTime(timePtr); |
| 4470 |
} |
| 4471 |
|
| 4472 |
/* |
| 4473 |
*---------------------------------------------------------------------- |
| 4474 |
* |
| 4475 |
* TclGetPlatform -- |
| 4476 |
* |
| 4477 |
* This is a kludge that allows the test library to get access the |
| 4478 |
* internal tclPlatform variable. |
| 4479 |
* |
| 4480 |
* Results: |
| 4481 |
* Returns a pointer to the tclPlatform variable. |
| 4482 |
* |
| 4483 |
* Side effects: |
| 4484 |
* None. |
| 4485 |
* |
| 4486 |
*---------------------------------------------------------------------- |
| 4487 |
*/ |
| 4488 |
|
| 4489 |
TclPlatformType * |
| 4490 |
TclGetPlatform(void) |
| 4491 |
{ |
| 4492 |
return &tclPlatform; |
| 4493 |
} |
| 4494 |
|
| 4495 |
/* |
| 4496 |
*---------------------------------------------------------------------- |
| 4497 |
* |
| 4498 |
* TclReToGlob -- |
| 4499 |
* |
| 4500 |
* Attempt to convert a regular expression to an equivalent glob pattern. |
| 4501 |
* |
| 4502 |
* Results: |
| 4503 |
* Returns TCL_OK on success, TCL_ERROR on failure. If interp is not |
| 4504 |
* NULL, an error message is placed in the result. On success, the |
| 4505 |
* DString will contain an exact equivalent glob pattern. The caller is |
| 4506 |
* responsible for calling Tcl_DStringFree on success. If exactPtr is not |
| 4507 |
* NULL, it will be 1 if an exact match qualifies. |
| 4508 |
* |
| 4509 |
* Side effects: |
| 4510 |
* None. |
| 4511 |
* |
| 4512 |
*---------------------------------------------------------------------- |
| 4513 |
*/ |
| 4514 |
|
| 4515 |
int |
| 4516 |
TclReToGlob( |
| 4517 |
Tcl_Interp *interp, |
| 4518 |
const char *reStr, |
| 4519 |
int reStrLen, |
| 4520 |
Tcl_DString *dsPtr, |
| 4521 |
int *exactPtr, |
| 4522 |
int *quantifiersFoundPtr) |
| 4523 |
{ |
| 4524 |
int anchorLeft, anchorRight, lastIsStar, numStars; |
| 4525 |
char *dsStr, *dsStrStart; |
| 4526 |
const char *msg, *p, *strEnd, *code; |
| 4527 |
|
| 4528 |
strEnd = reStr + reStrLen; |
| 4529 |
Tcl_DStringInit(dsPtr); |
| 4530 |
if (quantifiersFoundPtr != NULL) { |
| 4531 |
*quantifiersFoundPtr = 0; |
| 4532 |
} |
| 4533 |
|
| 4534 |
/* |
| 4535 |
* "***=xxx" == "*xxx*", watch for glob-sensitive chars. |
| 4536 |
*/ |
| 4537 |
|
| 4538 |
if ((reStrLen >= 4) && (memcmp("***=", reStr, 4) == 0)) { |
| 4539 |
/* |
| 4540 |
* At most, the glob pattern has length 2*reStrLen + 2 to backslash |
| 4541 |
* escape every character and have * at each end. |
| 4542 |
*/ |
| 4543 |
|
| 4544 |
Tcl_DStringSetLength(dsPtr, reStrLen + 2); |
| 4545 |
dsStr = dsStrStart = Tcl_DStringValue(dsPtr); |
| 4546 |
*dsStr++ = '*'; |
| 4547 |
for (p = reStr + 4; p < strEnd; p++) { |
| 4548 |
switch (*p) { |
| 4549 |
case '\\': case '*': case '[': case ']': case '?': |
| 4550 |
/* Only add \ where necessary for glob */ |
| 4551 |
*dsStr++ = '\\'; |
| 4552 |
/* fall through */ |
| 4553 |
default: |
| 4554 |
*dsStr++ = *p; |
| 4555 |
break; |
| 4556 |
} |
| 4557 |
} |
| 4558 |
*dsStr++ = '*'; |
| 4559 |
Tcl_DStringSetLength(dsPtr, dsStr - dsStrStart); |
| 4560 |
if (exactPtr) { |
| 4561 |
*exactPtr = 0; |
| 4562 |
} |
| 4563 |
return TCL_OK; |
| 4564 |
} |
| 4565 |
|
| 4566 |
/* |
| 4567 |
* At most, the glob pattern has length reStrLen + 2 to account for |
| 4568 |
* possible * at each end. |
| 4569 |
*/ |
| 4570 |
|
| 4571 |
Tcl_DStringSetLength(dsPtr, reStrLen + 2); |
| 4572 |
dsStr = dsStrStart = Tcl_DStringValue(dsPtr); |
| 4573 |
|
| 4574 |
/* |
| 4575 |
* Check for anchored REs (ie ^foo$), so we can use string equal if |
| 4576 |
* possible. Do not alter the start of str so we can free it correctly. |
| 4577 |
* |
| 4578 |
* Keep track of the last char being an unescaped star to prevent multiple |
| 4579 |
* instances. Simpler than checking that the last star may be escaped. |
| 4580 |
*/ |
| 4581 |
|
| 4582 |
msg = NULL; |
| 4583 |
code = NULL; |
| 4584 |
p = reStr; |
| 4585 |
anchorRight = 0; |
| 4586 |
lastIsStar = 0; |
| 4587 |
numStars = 0; |
| 4588 |
|
| 4589 |
if (*p == '^') { |
| 4590 |
anchorLeft = 1; |
| 4591 |
p++; |
| 4592 |
} else { |
| 4593 |
anchorLeft = 0; |
| 4594 |
*dsStr++ = '*'; |
| 4595 |
lastIsStar = 1; |
| 4596 |
} |
| 4597 |
|
| 4598 |
for ( ; p < strEnd; p++) { |
| 4599 |
switch (*p) { |
| 4600 |
case '\\': |
| 4601 |
p++; |
| 4602 |
switch (*p) { |
| 4603 |
case 'a': |
| 4604 |
*dsStr++ = '\a'; |
| 4605 |
break; |
| 4606 |
case 'b': |
| 4607 |
*dsStr++ = '\b'; |
| 4608 |
break; |
| 4609 |
case 'f': |
| 4610 |
*dsStr++ = '\f'; |
| 4611 |
break; |
| 4612 |
case 'n': |
| 4613 |
*dsStr++ = '\n'; |
| 4614 |
break; |
| 4615 |
case 'r': |
| 4616 |
*dsStr++ = '\r'; |
| 4617 |
break; |
| 4618 |
case 't': |
| 4619 |
*dsStr++ = '\t'; |
| 4620 |
break; |
| 4621 |
case 'v': |
| 4622 |
*dsStr++ = '\v'; |
| 4623 |
break; |
| 4624 |
case 'B': case '\\': |
| 4625 |
*dsStr++ = '\\'; |
| 4626 |
*dsStr++ = '\\'; |
| 4627 |
anchorLeft = 0; /* prevent exact match */ |
| 4628 |
break; |
| 4629 |
case '*': case '[': case ']': case '?': |
| 4630 |
/* Only add \ where necessary for glob */ |
| 4631 |
*dsStr++ = '\\'; |
| 4632 |
anchorLeft = 0; /* prevent exact match */ |
| 4633 |
/* fall through */ |
| 4634 |
case '{': case '}': case '(': case ')': case '+': |
| 4635 |
case '.': case '|': case '^': case '$': |
| 4636 |
*dsStr++ = *p; |
| 4637 |
break; |
| 4638 |
default: |
| 4639 |
msg = "invalid escape sequence"; |
| 4640 |
code = "BADESCAPE"; |
| 4641 |
goto invalidGlob; |
| 4642 |
} |
| 4643 |
break; |
| 4644 |
case '.': |
| 4645 |
if (quantifiersFoundPtr != NULL) { |
| 4646 |
*quantifiersFoundPtr = 1; |
| 4647 |
} |
| 4648 |
anchorLeft = 0; /* prevent exact match */ |
| 4649 |
if (p+1 < strEnd) { |
| 4650 |
if (p[1] == '*') { |
| 4651 |
p++; |
| 4652 |
if (!lastIsStar) { |
| 4653 |
*dsStr++ = '*'; |
| 4654 |
lastIsStar = 1; |
| 4655 |
numStars++; |
| 4656 |
} |
| 4657 |
continue; |
| 4658 |
} else if (p[1] == '+') { |
| 4659 |
p++; |
| 4660 |
*dsStr++ = '?'; |
| 4661 |
*dsStr++ = '*'; |
| 4662 |
lastIsStar = 1; |
| 4663 |
numStars++; |
| 4664 |
continue; |
| 4665 |
} |
| 4666 |
} |
| 4667 |
*dsStr++ = '?'; |
| 4668 |
break; |
| 4669 |
case '$': |
| 4670 |
if (p+1 != strEnd) { |
| 4671 |
msg = "$ not anchor"; |
| 4672 |
code = "NONANCHOR"; |
| 4673 |
goto invalidGlob; |
| 4674 |
} |
| 4675 |
anchorRight = 1; |
| 4676 |
break; |
| 4677 |
case '*': case '+': case '?': case '|': case '^': |
| 4678 |
case '{': case '}': case '(': case ')': case '[': case ']': |
| 4679 |
msg = "unhandled RE special char"; |
| 4680 |
code = "UNHANDLED"; |
| 4681 |
goto invalidGlob; |
| 4682 |
default: |
| 4683 |
*dsStr++ = *p; |
| 4684 |
break; |
| 4685 |
} |
| 4686 |
lastIsStar = 0; |
| 4687 |
} |
| 4688 |
if (numStars > 1) { |
| 4689 |
/* |
| 4690 |
* Heuristic: if >1 non-anchoring *, the risk is large that glob |
| 4691 |
* matching is slower than the RE engine, so report invalid. |
| 4692 |
*/ |
| 4693 |
|
| 4694 |
msg = "excessive recursive glob backtrack potential"; |
| 4695 |
code = "OVERCOMPLEX"; |
| 4696 |
goto invalidGlob; |
| 4697 |
} |
| 4698 |
|
| 4699 |
if (!anchorRight && !lastIsStar) { |
| 4700 |
*dsStr++ = '*'; |
| 4701 |
} |
| 4702 |
Tcl_DStringSetLength(dsPtr, dsStr - dsStrStart); |
| 4703 |
|
| 4704 |
if (exactPtr) { |
| 4705 |
*exactPtr = (anchorLeft && anchorRight); |
| 4706 |
} |
| 4707 |
|
| 4708 |
return TCL_OK; |
| 4709 |
|
| 4710 |
invalidGlob: |
| 4711 |
if (interp != NULL) { |
| 4712 |
Tcl_SetObjResult(interp, Tcl_NewStringObj(msg, -1)); |
| 4713 |
Tcl_SetErrorCode(interp, "TCL", "RE2GLOB", code, (char *)NULL); |
| 4714 |
} |
| 4715 |
Tcl_DStringFree(dsPtr); |
| 4716 |
return TCL_ERROR; |
| 4717 |
} |
| 4718 |
|
| 4719 |
/* |
| 4720 |
* Local Variables: |
| 4721 |
* mode: c |
| 4722 |
* c-basic-offset: 4 |
| 4723 |
* fill-column: 78 |
| 4724 |
* End: |
| 4725 |
*/ |