PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/SN-NG4.1/tcl/generic/tclUtil.c

https://gitlab.com/OpenSourceMirror/sourcenav
C | 1891 lines | 1085 code | 121 blank | 685 comment | 256 complexity | 1c21720d556f43228b7bf9b7a51e3f22 MD5 | raw file
  1. /*
  2. * tclUtil.c --
  3. *
  4. * This file contains utility procedures that are used by many Tcl
  5. * commands.
  6. *
  7. * Copyright (c) 1987-1993 The Regents of the University of California.
  8. * Copyright (c) 1994-1998 Sun Microsystems, Inc.
  9. *
  10. * See the file "license.terms" for information on usage and redistribution
  11. * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. *
  13. * RCS: @(#) $Id$
  14. */
  15. #include "tclInt.h"
  16. #include "tclPort.h"
  17. /*
  18. * The following variable holds the full path name of the binary
  19. * from which this application was executed, or NULL if it isn't
  20. * know. The value of the variable is set by the procedure
  21. * Tcl_FindExecutable. The storage space is dynamically allocated.
  22. */
  23. char *tclExecutableName = NULL;
  24. char *tclNativeExecutableName = NULL;
  25. /*
  26. * The following values are used in the flags returned by Tcl_ScanElement
  27. * and used by Tcl_ConvertElement. The value TCL_DONT_USE_BRACES is also
  28. * defined in tcl.h; make sure its value doesn't overlap with any of the
  29. * values below.
  30. *
  31. * TCL_DONT_USE_BRACES - 1 means the string mustn't be enclosed in
  32. * braces (e.g. it contains unmatched braces,
  33. * or ends in a backslash character, or user
  34. * just doesn't want braces); handle all
  35. * special characters by adding backslashes.
  36. * USE_BRACES - 1 means the string contains a special
  37. * character that can be handled simply by
  38. * enclosing the entire argument in braces.
  39. * BRACES_UNMATCHED - 1 means that braces aren't properly matched
  40. * in the argument.
  41. */
  42. #define USE_BRACES 2
  43. #define BRACES_UNMATCHED 4
  44. /*
  45. * The following values determine the precision used when converting
  46. * floating-point values to strings. This information is linked to all
  47. * of the tcl_precision variables in all interpreters via the procedure
  48. * TclPrecTraceProc.
  49. */
  50. static char precisionString[10] = "12";
  51. /* The string value of all the tcl_precision
  52. * variables. */
  53. static char precisionFormat[10] = "%.12g";
  54. /* The format string actually used in calls
  55. * to sprintf. */
  56. TCL_DECLARE_MUTEX(precisionMutex)
  57. /*
  58. *----------------------------------------------------------------------
  59. *
  60. * TclFindElement --
  61. *
  62. * Given a pointer into a Tcl list, locate the first (or next)
  63. * element in the list.
  64. *
  65. * Results:
  66. * The return value is normally TCL_OK, which means that the
  67. * element was successfully located. If TCL_ERROR is returned
  68. * it means that list didn't have proper list structure;
  69. * the interp's result contains a more detailed error message.
  70. *
  71. * If TCL_OK is returned, then *elementPtr will be set to point to the
  72. * first element of list, and *nextPtr will be set to point to the
  73. * character just after any white space following the last character
  74. * that's part of the element. If this is the last argument in the
  75. * list, then *nextPtr will point just after the last character in the
  76. * list (i.e., at the character at list+listLength). If sizePtr is
  77. * non-NULL, *sizePtr is filled in with the number of characters in the
  78. * element. If the element is in braces, then *elementPtr will point
  79. * to the character after the opening brace and *sizePtr will not
  80. * include either of the braces. If there isn't an element in the list,
  81. * *sizePtr will be zero, and both *elementPtr and *termPtr will point
  82. * just after the last character in the list. Note: this procedure does
  83. * NOT collapse backslash sequences.
  84. *
  85. * Side effects:
  86. * None.
  87. *
  88. *----------------------------------------------------------------------
  89. */
  90. int
  91. TclFindElement(interp, list, listLength, elementPtr, nextPtr, sizePtr,
  92. bracePtr)
  93. Tcl_Interp *interp; /* Interpreter to use for error reporting.
  94. * If NULL, then no error message is left
  95. * after errors. */
  96. CONST char *list; /* Points to the first byte of a string
  97. * containing a Tcl list with zero or more
  98. * elements (possibly in braces). */
  99. int listLength; /* Number of bytes in the list's string. */
  100. CONST char **elementPtr; /* Where to put address of first significant
  101. * character in first element of list. */
  102. CONST char **nextPtr; /* Fill in with location of character just
  103. * after all white space following end of
  104. * argument (next arg or end of list). */
  105. int *sizePtr; /* If non-zero, fill in with size of
  106. * element. */
  107. int *bracePtr; /* If non-zero, fill in with non-zero/zero
  108. * to indicate that arg was/wasn't
  109. * in braces. */
  110. {
  111. CONST char *p = list;
  112. CONST char *elemStart; /* Points to first byte of first element. */
  113. CONST char *limit; /* Points just after list's last byte. */
  114. int openBraces = 0; /* Brace nesting level during parse. */
  115. int inQuotes = 0;
  116. int size = 0; /* lint. */
  117. int numChars;
  118. CONST char *p2;
  119. /*
  120. * Skim off leading white space and check for an opening brace or
  121. * quote. We treat embedded NULLs in the list as bytes belonging to
  122. * a list element.
  123. */
  124. limit = (list + listLength);
  125. while ((p < limit) && (isspace(UCHAR(*p)))) { /* INTL: ISO space. */
  126. p++;
  127. }
  128. if (p == limit) { /* no element found */
  129. elemStart = limit;
  130. goto done;
  131. }
  132. if (*p == '{') {
  133. openBraces = 1;
  134. p++;
  135. } else if (*p == '"') {
  136. inQuotes = 1;
  137. p++;
  138. }
  139. elemStart = p;
  140. if (bracePtr != 0) {
  141. *bracePtr = openBraces;
  142. }
  143. /*
  144. * Find element's end (a space, close brace, or the end of the string).
  145. */
  146. while (p < limit) {
  147. switch (*p) {
  148. /*
  149. * Open brace: don't treat specially unless the element is in
  150. * braces. In this case, keep a nesting count.
  151. */
  152. case '{':
  153. if (openBraces != 0) {
  154. openBraces++;
  155. }
  156. break;
  157. /*
  158. * Close brace: if element is in braces, keep nesting count and
  159. * quit when the last close brace is seen.
  160. */
  161. case '}':
  162. if (openBraces > 1) {
  163. openBraces--;
  164. } else if (openBraces == 1) {
  165. size = (p - elemStart);
  166. p++;
  167. if ((p >= limit)
  168. || isspace(UCHAR(*p))) { /* INTL: ISO space. */
  169. goto done;
  170. }
  171. /*
  172. * Garbage after the closing brace; return an error.
  173. */
  174. if (interp != NULL) {
  175. char buf[100];
  176. p2 = p;
  177. while ((p2 < limit)
  178. && (!isspace(UCHAR(*p2))) /* INTL: ISO space. */
  179. && (p2 < p+20)) {
  180. p2++;
  181. }
  182. sprintf(buf,
  183. "list element in braces followed by \"%.*s\" instead of space",
  184. (int) (p2-p), p);
  185. Tcl_SetResult(interp, buf, TCL_VOLATILE);
  186. }
  187. return TCL_ERROR;
  188. }
  189. break;
  190. /*
  191. * Backslash: skip over everything up to the end of the
  192. * backslash sequence.
  193. */
  194. case '\\': {
  195. Tcl_UtfBackslash(p, &numChars, NULL);
  196. p += (numChars - 1);
  197. break;
  198. }
  199. /*
  200. * Space: ignore if element is in braces or quotes; otherwise
  201. * terminate element.
  202. */
  203. case ' ':
  204. case '\f':
  205. case '\n':
  206. case '\r':
  207. case '\t':
  208. case '\v':
  209. if ((openBraces == 0) && !inQuotes) {
  210. size = (p - elemStart);
  211. goto done;
  212. }
  213. break;
  214. /*
  215. * Double-quote: if element is in quotes then terminate it.
  216. */
  217. case '"':
  218. if (inQuotes) {
  219. size = (p - elemStart);
  220. p++;
  221. if ((p >= limit)
  222. || isspace(UCHAR(*p))) { /* INTL: ISO space */
  223. goto done;
  224. }
  225. /*
  226. * Garbage after the closing quote; return an error.
  227. */
  228. if (interp != NULL) {
  229. char buf[100];
  230. p2 = p;
  231. while ((p2 < limit)
  232. && (!isspace(UCHAR(*p2))) /* INTL: ISO space */
  233. && (p2 < p+20)) {
  234. p2++;
  235. }
  236. sprintf(buf,
  237. "list element in quotes followed by \"%.*s\" %s",
  238. (int) (p2-p), p, "instead of space");
  239. Tcl_SetResult(interp, buf, TCL_VOLATILE);
  240. }
  241. return TCL_ERROR;
  242. }
  243. break;
  244. }
  245. p++;
  246. }
  247. /*
  248. * End of list: terminate element.
  249. */
  250. if (p == limit) {
  251. if (openBraces != 0) {
  252. if (interp != NULL) {
  253. Tcl_SetResult(interp, "unmatched open brace in list",
  254. TCL_STATIC);
  255. }
  256. return TCL_ERROR;
  257. } else if (inQuotes) {
  258. if (interp != NULL) {
  259. Tcl_SetResult(interp, "unmatched open quote in list",
  260. TCL_STATIC);
  261. }
  262. return TCL_ERROR;
  263. }
  264. size = (p - elemStart);
  265. }
  266. done:
  267. while ((p < limit) && (isspace(UCHAR(*p)))) { /* INTL: ISO space. */
  268. p++;
  269. }
  270. *elementPtr = elemStart;
  271. *nextPtr = p;
  272. if (sizePtr != 0) {
  273. *sizePtr = size;
  274. }
  275. return TCL_OK;
  276. }
  277. /*
  278. *----------------------------------------------------------------------
  279. *
  280. * TclCopyAndCollapse --
  281. *
  282. * Copy a string and eliminate any backslashes that aren't in braces.
  283. *
  284. * Results:
  285. * There is no return value. Count characters get copied from src to
  286. * dst. Along the way, if backslash sequences are found outside braces,
  287. * the backslashes are eliminated in the copy. After scanning count
  288. * chars from source, a null character is placed at the end of dst.
  289. * Returns the number of characters that got copied.
  290. *
  291. * Side effects:
  292. * None.
  293. *
  294. *----------------------------------------------------------------------
  295. */
  296. int
  297. TclCopyAndCollapse(count, src, dst)
  298. int count; /* Number of characters to copy from src. */
  299. CONST char *src; /* Copy from here... */
  300. char *dst; /* ... to here. */
  301. {
  302. register char c;
  303. int numRead;
  304. int newCount = 0;
  305. int backslashCount;
  306. for (c = *src; count > 0; src++, c = *src, count--) {
  307. if (c == '\\') {
  308. backslashCount = Tcl_UtfBackslash(src, &numRead, dst);
  309. dst += backslashCount;
  310. newCount += backslashCount;
  311. src += numRead-1;
  312. count -= numRead-1;
  313. } else {
  314. *dst = c;
  315. dst++;
  316. newCount++;
  317. }
  318. }
  319. *dst = 0;
  320. return newCount;
  321. }
  322. /*
  323. *----------------------------------------------------------------------
  324. *
  325. * Tcl_SplitList --
  326. *
  327. * Splits a list up into its constituent fields.
  328. *
  329. * Results
  330. * The return value is normally TCL_OK, which means that
  331. * the list was successfully split up. If TCL_ERROR is
  332. * returned, it means that "list" didn't have proper list
  333. * structure; the interp's result will contain a more detailed
  334. * error message.
  335. *
  336. * *argvPtr will be filled in with the address of an array
  337. * whose elements point to the elements of list, in order.
  338. * *argcPtr will get filled in with the number of valid elements
  339. * in the array. A single block of memory is dynamically allocated
  340. * to hold both the argv array and a copy of the list (with
  341. * backslashes and braces removed in the standard way).
  342. * The caller must eventually free this memory by calling free()
  343. * on *argvPtr. Note: *argvPtr and *argcPtr are only modified
  344. * if the procedure returns normally.
  345. *
  346. * Side effects:
  347. * Memory is allocated.
  348. *
  349. *----------------------------------------------------------------------
  350. */
  351. int
  352. Tcl_SplitList(interp, list, argcPtr, argvPtr)
  353. Tcl_Interp *interp; /* Interpreter to use for error reporting.
  354. * If NULL, no error message is left. */
  355. CONST char *list; /* Pointer to string with list structure. */
  356. int *argcPtr; /* Pointer to location to fill in with
  357. * the number of elements in the list. */
  358. char ***argvPtr; /* Pointer to place to store pointer to
  359. * array of pointers to list elements. */
  360. {
  361. char **argv;
  362. CONST char *l;
  363. char *p;
  364. int length, size, i, result, elSize, brace;
  365. CONST char *element;
  366. /*
  367. * Figure out how much space to allocate. There must be enough
  368. * space for both the array of pointers and also for a copy of
  369. * the list. To estimate the number of pointers needed, count
  370. * the number of space characters in the list.
  371. */
  372. for (size = 1, l = list; *l != 0; l++) {
  373. if (isspace(UCHAR(*l))) { /* INTL: ISO space. */
  374. size++;
  375. }
  376. }
  377. size++; /* Leave space for final NULL pointer. */
  378. argv = (char **) ckalloc((unsigned)
  379. ((size * sizeof(char *)) + (l - list) + 1));
  380. length = strlen(list);
  381. for (i = 0, p = ((char *) argv) + size*sizeof(char *);
  382. *list != 0; i++) {
  383. CONST char *prevList = list;
  384. result = TclFindElement(interp, list, length, &element,
  385. &list, &elSize, &brace);
  386. length -= (list - prevList);
  387. if (result != TCL_OK) {
  388. ckfree((char *) argv);
  389. return result;
  390. }
  391. if (*element == 0) {
  392. break;
  393. }
  394. if (i >= size) {
  395. ckfree((char *) argv);
  396. if (interp != NULL) {
  397. Tcl_SetResult(interp, "internal error in Tcl_SplitList",
  398. TCL_STATIC);
  399. }
  400. return TCL_ERROR;
  401. }
  402. argv[i] = p;
  403. if (brace) {
  404. memcpy((VOID *) p, (VOID *) element, (size_t) elSize);
  405. p += elSize;
  406. *p = 0;
  407. p++;
  408. } else {
  409. TclCopyAndCollapse(elSize, element, p);
  410. p += elSize+1;
  411. }
  412. }
  413. argv[i] = NULL;
  414. *argvPtr = argv;
  415. *argcPtr = i;
  416. return TCL_OK;
  417. }
  418. /*
  419. *----------------------------------------------------------------------
  420. *
  421. * Tcl_ScanElement --
  422. *
  423. * This procedure is a companion procedure to Tcl_ConvertElement.
  424. * It scans a string to see what needs to be done to it (e.g. add
  425. * backslashes or enclosing braces) to make the string into a
  426. * valid Tcl list element.
  427. *
  428. * Results:
  429. * The return value is an overestimate of the number of characters
  430. * that will be needed by Tcl_ConvertElement to produce a valid
  431. * list element from string. The word at *flagPtr is filled in
  432. * with a value needed by Tcl_ConvertElement when doing the actual
  433. * conversion.
  434. *
  435. * Side effects:
  436. * None.
  437. *
  438. *----------------------------------------------------------------------
  439. */
  440. int
  441. Tcl_ScanElement(string, flagPtr)
  442. register CONST char *string; /* String to convert to list element. */
  443. register int *flagPtr; /* Where to store information to guide
  444. * Tcl_ConvertCountedElement. */
  445. {
  446. return Tcl_ScanCountedElement(string, -1, flagPtr);
  447. }
  448. /*
  449. *----------------------------------------------------------------------
  450. *
  451. * Tcl_ScanCountedElement --
  452. *
  453. * This procedure is a companion procedure to
  454. * Tcl_ConvertCountedElement. It scans a string to see what
  455. * needs to be done to it (e.g. add backslashes or enclosing
  456. * braces) to make the string into a valid Tcl list element.
  457. * If length is -1, then the string is scanned up to the first
  458. * null byte.
  459. *
  460. * Results:
  461. * The return value is an overestimate of the number of characters
  462. * that will be needed by Tcl_ConvertCountedElement to produce a
  463. * valid list element from string. The word at *flagPtr is
  464. * filled in with a value needed by Tcl_ConvertCountedElement
  465. * when doing the actual conversion.
  466. *
  467. * Side effects:
  468. * None.
  469. *
  470. *----------------------------------------------------------------------
  471. */
  472. int
  473. Tcl_ScanCountedElement(string, length, flagPtr)
  474. CONST char *string; /* String to convert to Tcl list element. */
  475. int length; /* Number of bytes in string, or -1. */
  476. int *flagPtr; /* Where to store information to guide
  477. * Tcl_ConvertElement. */
  478. {
  479. int flags, nestingLevel;
  480. register CONST char *p, *lastChar;
  481. /*
  482. * This procedure and Tcl_ConvertElement together do two things:
  483. *
  484. * 1. They produce a proper list, one that will yield back the
  485. * argument strings when evaluated or when disassembled with
  486. * Tcl_SplitList. This is the most important thing.
  487. *
  488. * 2. They try to produce legible output, which means minimizing the
  489. * use of backslashes (using braces instead). However, there are
  490. * some situations where backslashes must be used (e.g. an element
  491. * like "{abc": the leading brace will have to be backslashed.
  492. * For each element, one of three things must be done:
  493. *
  494. * (a) Use the element as-is (it doesn't contain any special
  495. * characters). This is the most desirable option.
  496. *
  497. * (b) Enclose the element in braces, but leave the contents alone.
  498. * This happens if the element contains embedded space, or if it
  499. * contains characters with special interpretation ($, [, ;, or \),
  500. * or if it starts with a brace or double-quote, or if there are
  501. * no characters in the element.
  502. *
  503. * (c) Don't enclose the element in braces, but add backslashes to
  504. * prevent special interpretation of special characters. This is a
  505. * last resort used when the argument would normally fall under case
  506. * (b) but contains unmatched braces. It also occurs if the last
  507. * character of the argument is a backslash or if the element contains
  508. * a backslash followed by newline.
  509. *
  510. * The procedure figures out how many bytes will be needed to store
  511. * the result (actually, it overestimates). It also collects information
  512. * about the element in the form of a flags word.
  513. *
  514. * Note: list elements produced by this procedure and
  515. * Tcl_ConvertCountedElement must have the property that they can be
  516. * enclosing in curly braces to make sub-lists. This means, for
  517. * example, that we must not leave unmatched curly braces in the
  518. * resulting list element. This property is necessary in order for
  519. * procedures like Tcl_DStringStartSublist to work.
  520. */
  521. nestingLevel = 0;
  522. flags = 0;
  523. if (string == NULL) {
  524. string = "";
  525. }
  526. if (length == -1) {
  527. length = strlen(string);
  528. }
  529. lastChar = string + length;
  530. p = string;
  531. if ((p == lastChar) || (*p == '{') || (*p == '"')) {
  532. flags |= USE_BRACES;
  533. }
  534. for ( ; p < lastChar; p++) {
  535. switch (*p) {
  536. case '{':
  537. nestingLevel++;
  538. break;
  539. case '}':
  540. nestingLevel--;
  541. if (nestingLevel < 0) {
  542. flags |= TCL_DONT_USE_BRACES|BRACES_UNMATCHED;
  543. }
  544. break;
  545. case '[':
  546. case '$':
  547. case ';':
  548. case ' ':
  549. case '\f':
  550. case '\n':
  551. case '\r':
  552. case '\t':
  553. case '\v':
  554. flags |= USE_BRACES;
  555. break;
  556. case '\\':
  557. if ((p+1 == lastChar) || (p[1] == '\n')) {
  558. flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED;
  559. } else {
  560. int size;
  561. Tcl_UtfBackslash(p, &size, NULL);
  562. p += size-1;
  563. flags |= USE_BRACES;
  564. }
  565. break;
  566. }
  567. }
  568. if (nestingLevel != 0) {
  569. flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED;
  570. }
  571. *flagPtr = flags;
  572. /*
  573. * Allow enough space to backslash every character plus leave
  574. * two spaces for braces.
  575. */
  576. return 2*(p-string) + 2;
  577. }
  578. /*
  579. *----------------------------------------------------------------------
  580. *
  581. * Tcl_ConvertElement --
  582. *
  583. * This is a companion procedure to Tcl_ScanElement. Given
  584. * the information produced by Tcl_ScanElement, this procedure
  585. * converts a string to a list element equal to that string.
  586. *
  587. * Results:
  588. * Information is copied to *dst in the form of a list element
  589. * identical to src (i.e. if Tcl_SplitList is applied to dst it
  590. * will produce a string identical to src). The return value is
  591. * a count of the number of characters copied (not including the
  592. * terminating NULL character).
  593. *
  594. * Side effects:
  595. * None.
  596. *
  597. *----------------------------------------------------------------------
  598. */
  599. int
  600. Tcl_ConvertElement(src, dst, flags)
  601. register CONST char *src; /* Source information for list element. */
  602. register char *dst; /* Place to put list-ified element. */
  603. register int flags; /* Flags produced by Tcl_ScanElement. */
  604. {
  605. return Tcl_ConvertCountedElement(src, -1, dst, flags);
  606. }
  607. /*
  608. *----------------------------------------------------------------------
  609. *
  610. * Tcl_ConvertCountedElement --
  611. *
  612. * This is a companion procedure to Tcl_ScanCountedElement. Given
  613. * the information produced by Tcl_ScanCountedElement, this
  614. * procedure converts a string to a list element equal to that
  615. * string.
  616. *
  617. * Results:
  618. * Information is copied to *dst in the form of a list element
  619. * identical to src (i.e. if Tcl_SplitList is applied to dst it
  620. * will produce a string identical to src). The return value is
  621. * a count of the number of characters copied (not including the
  622. * terminating NULL character).
  623. *
  624. * Side effects:
  625. * None.
  626. *
  627. *----------------------------------------------------------------------
  628. */
  629. int
  630. Tcl_ConvertCountedElement(src, length, dst, flags)
  631. register CONST char *src; /* Source information for list element. */
  632. int length; /* Number of bytes in src, or -1. */
  633. char *dst; /* Place to put list-ified element. */
  634. int flags; /* Flags produced by Tcl_ScanElement. */
  635. {
  636. register char *p = dst;
  637. register CONST char *lastChar;
  638. /*
  639. * See the comment block at the beginning of the Tcl_ScanElement
  640. * code for details of how this works.
  641. */
  642. if (src && length == -1) {
  643. length = strlen(src);
  644. }
  645. if ((src == NULL) || (length == 0)) {
  646. p[0] = '{';
  647. p[1] = '}';
  648. p[2] = 0;
  649. return 2;
  650. }
  651. lastChar = src + length;
  652. if ((flags & USE_BRACES) && !(flags & TCL_DONT_USE_BRACES)) {
  653. *p = '{';
  654. p++;
  655. for ( ; src != lastChar; src++, p++) {
  656. *p = *src;
  657. }
  658. *p = '}';
  659. p++;
  660. } else {
  661. if (*src == '{') {
  662. /*
  663. * Can't have a leading brace unless the whole element is
  664. * enclosed in braces. Add a backslash before the brace.
  665. * Furthermore, this may destroy the balance between open
  666. * and close braces, so set BRACES_UNMATCHED.
  667. */
  668. p[0] = '\\';
  669. p[1] = '{';
  670. p += 2;
  671. src++;
  672. flags |= BRACES_UNMATCHED;
  673. }
  674. for (; src != lastChar; src++) {
  675. switch (*src) {
  676. case ']':
  677. case '[':
  678. case '$':
  679. case ';':
  680. case ' ':
  681. case '\\':
  682. case '"':
  683. *p = '\\';
  684. p++;
  685. break;
  686. case '{':
  687. case '}':
  688. /*
  689. * It may not seem necessary to backslash braces, but
  690. * it is. The reason for this is that the resulting
  691. * list element may actually be an element of a sub-list
  692. * enclosed in braces (e.g. if Tcl_DStringStartSublist
  693. * has been invoked), so there may be a brace mismatch
  694. * if the braces aren't backslashed.
  695. */
  696. if (flags & BRACES_UNMATCHED) {
  697. *p = '\\';
  698. p++;
  699. }
  700. break;
  701. case '\f':
  702. *p = '\\';
  703. p++;
  704. *p = 'f';
  705. p++;
  706. continue;
  707. case '\n':
  708. *p = '\\';
  709. p++;
  710. *p = 'n';
  711. p++;
  712. continue;
  713. case '\r':
  714. *p = '\\';
  715. p++;
  716. *p = 'r';
  717. p++;
  718. continue;
  719. case '\t':
  720. *p = '\\';
  721. p++;
  722. *p = 't';
  723. p++;
  724. continue;
  725. case '\v':
  726. *p = '\\';
  727. p++;
  728. *p = 'v';
  729. p++;
  730. continue;
  731. }
  732. *p = *src;
  733. p++;
  734. }
  735. }
  736. *p = '\0';
  737. return p-dst;
  738. }
  739. /*
  740. *----------------------------------------------------------------------
  741. *
  742. * Tcl_Merge --
  743. *
  744. * Given a collection of strings, merge them together into a
  745. * single string that has proper Tcl list structured (i.e.
  746. * Tcl_SplitList may be used to retrieve strings equal to the
  747. * original elements, and Tcl_Eval will parse the string back
  748. * into its original elements).
  749. *
  750. * Results:
  751. * The return value is the address of a dynamically-allocated
  752. * string containing the merged list.
  753. *
  754. * Side effects:
  755. * None.
  756. *
  757. *----------------------------------------------------------------------
  758. */
  759. char *
  760. Tcl_Merge(argc, argv)
  761. int argc; /* How many strings to merge. */
  762. char **argv; /* Array of string values. */
  763. {
  764. # define LOCAL_SIZE 20
  765. int localFlags[LOCAL_SIZE], *flagPtr;
  766. int numChars;
  767. char *result;
  768. char *dst;
  769. int i;
  770. /*
  771. * Pass 1: estimate space, gather flags.
  772. */
  773. if (argc <= LOCAL_SIZE) {
  774. flagPtr = localFlags;
  775. } else {
  776. flagPtr = (int *) ckalloc((unsigned) argc*sizeof(int));
  777. }
  778. numChars = 1;
  779. for (i = 0; i < argc; i++) {
  780. numChars += Tcl_ScanElement(argv[i], &flagPtr[i]) + 1;
  781. }
  782. /*
  783. * Pass two: copy into the result area.
  784. */
  785. result = (char *) ckalloc((unsigned) numChars);
  786. dst = result;
  787. for (i = 0; i < argc; i++) {
  788. numChars = Tcl_ConvertElement(argv[i], dst, flagPtr[i]);
  789. dst += numChars;
  790. *dst = ' ';
  791. dst++;
  792. }
  793. if (dst == result) {
  794. *dst = 0;
  795. } else {
  796. dst[-1] = 0;
  797. }
  798. if (flagPtr != localFlags) {
  799. ckfree((char *) flagPtr);
  800. }
  801. return result;
  802. }
  803. /*
  804. *----------------------------------------------------------------------
  805. *
  806. * Tcl_Backslash --
  807. *
  808. * Figure out how to handle a backslash sequence.
  809. *
  810. * Results:
  811. * The return value is the character that should be substituted
  812. * in place of the backslash sequence that starts at src. If
  813. * readPtr isn't NULL then it is filled in with a count of the
  814. * number of characters in the backslash sequence.
  815. *
  816. * Side effects:
  817. * None.
  818. *
  819. *----------------------------------------------------------------------
  820. */
  821. char
  822. Tcl_Backslash(src, readPtr)
  823. CONST char *src; /* Points to the backslash character of
  824. * a backslash sequence. */
  825. int *readPtr; /* Fill in with number of characters read
  826. * from src, unless NULL. */
  827. {
  828. char buf[TCL_UTF_MAX];
  829. Tcl_UniChar ch;
  830. Tcl_UtfBackslash(src, readPtr, buf);
  831. Tcl_UtfToUniChar(buf, &ch);
  832. return (char) ch;
  833. }
  834. /*
  835. *----------------------------------------------------------------------
  836. *
  837. * Tcl_Concat --
  838. *
  839. * Concatenate a set of strings into a single large string.
  840. *
  841. * Results:
  842. * The return value is dynamically-allocated string containing
  843. * a concatenation of all the strings in argv, with spaces between
  844. * the original argv elements.
  845. *
  846. * Side effects:
  847. * Memory is allocated for the result; the caller is responsible
  848. * for freeing the memory.
  849. *
  850. *----------------------------------------------------------------------
  851. */
  852. char *
  853. Tcl_Concat(argc, argv)
  854. int argc; /* Number of strings to concatenate. */
  855. char **argv; /* Array of strings to concatenate. */
  856. {
  857. int totalSize, i;
  858. char *p;
  859. char *result;
  860. for (totalSize = 1, i = 0; i < argc; i++) {
  861. totalSize += strlen(argv[i]) + 1;
  862. }
  863. result = (char *) ckalloc((unsigned) totalSize);
  864. if (argc == 0) {
  865. *result = '\0';
  866. return result;
  867. }
  868. for (p = result, i = 0; i < argc; i++) {
  869. char *element;
  870. int length;
  871. /*
  872. * Clip white space off the front and back of the string
  873. * to generate a neater result, and ignore any empty
  874. * elements.
  875. */
  876. element = argv[i];
  877. while (isspace(UCHAR(*element))) { /* INTL: ISO space. */
  878. element++;
  879. }
  880. for (length = strlen(element);
  881. (length > 0)
  882. && (isspace(UCHAR(element[length-1]))) /* INTL: ISO space. */
  883. && ((length < 2) || (element[length-2] != '\\'));
  884. length--) {
  885. /* Null loop body. */
  886. }
  887. if (length == 0) {
  888. continue;
  889. }
  890. memcpy((VOID *) p, (VOID *) element, (size_t) length);
  891. p += length;
  892. *p = ' ';
  893. p++;
  894. }
  895. if (p != result) {
  896. p[-1] = 0;
  897. } else {
  898. *p = 0;
  899. }
  900. return result;
  901. }
  902. /*
  903. *----------------------------------------------------------------------
  904. *
  905. * Tcl_ConcatObj --
  906. *
  907. * Concatenate the strings from a set of objects into a single string
  908. * object with spaces between the original strings.
  909. *
  910. * Results:
  911. * The return value is a new string object containing a concatenation
  912. * of the strings in objv. Its ref count is zero.
  913. *
  914. * Side effects:
  915. * A new object is created.
  916. *
  917. *----------------------------------------------------------------------
  918. */
  919. Tcl_Obj *
  920. Tcl_ConcatObj(objc, objv)
  921. int objc; /* Number of objects to concatenate. */
  922. Tcl_Obj *CONST objv[]; /* Array of objects to concatenate. */
  923. {
  924. int allocSize, finalSize, length, elemLength, i;
  925. char *p;
  926. char *element;
  927. char *concatStr;
  928. Tcl_Obj *objPtr;
  929. /*
  930. * Check first to see if all the items are of list type. If so,
  931. * we will concat them together as lists, and return a list object.
  932. * This is only valid when the lists have no current string
  933. * representation, since we don't know what the original type was.
  934. * An original string rep may have lost some whitespace info when
  935. * converted which could be important.
  936. */
  937. for (i = 0; i < objc; i++) {
  938. objPtr = objv[i];
  939. if ((objPtr->typePtr != &tclListType) || (objPtr->bytes != NULL)) {
  940. break;
  941. }
  942. }
  943. if (i == objc) {
  944. Tcl_Obj **listv;
  945. int listc;
  946. objPtr = Tcl_NewListObj(0, NULL);
  947. for (i = 0; i < objc; i++) {
  948. /*
  949. * Tcl_ListObjAppendList could be used here, but this saves
  950. * us a bit of type checking (since we've already done it)
  951. * Use of INT_MAX tells us to always put the new stuff on
  952. * the end. It will be set right in Tcl_ListObjReplace.
  953. */
  954. Tcl_ListObjGetElements(NULL, objv[i], &listc, &listv);
  955. Tcl_ListObjReplace(NULL, objPtr, INT_MAX, 0, listc, listv);
  956. }
  957. return objPtr;
  958. }
  959. allocSize = 0;
  960. for (i = 0; i < objc; i++) {
  961. objPtr = objv[i];
  962. element = Tcl_GetStringFromObj(objPtr, &length);
  963. if ((element != NULL) && (length > 0)) {
  964. allocSize += (length + 1);
  965. }
  966. }
  967. if (allocSize == 0) {
  968. allocSize = 1; /* enough for the NULL byte at end */
  969. }
  970. /*
  971. * Allocate storage for the concatenated result. Note that allocSize
  972. * is one more than the total number of characters, and so includes
  973. * room for the terminating NULL byte.
  974. */
  975. concatStr = (char *) ckalloc((unsigned) allocSize);
  976. /*
  977. * Now concatenate the elements. Clip white space off the front and back
  978. * to generate a neater result, and ignore any empty elements. Also put
  979. * a null byte at the end.
  980. */
  981. finalSize = 0;
  982. if (objc == 0) {
  983. *concatStr = '\0';
  984. } else {
  985. p = concatStr;
  986. for (i = 0; i < objc; i++) {
  987. objPtr = objv[i];
  988. element = Tcl_GetStringFromObj(objPtr, &elemLength);
  989. while ((elemLength > 0)
  990. && (isspace(UCHAR(*element)))) { /* INTL: ISO space. */
  991. element++;
  992. elemLength--;
  993. }
  994. /*
  995. * Trim trailing white space. But, be careful not to trim
  996. * a space character if it is preceded by a backslash: in
  997. * this case it could be significant.
  998. */
  999. while ((elemLength > 0)
  1000. && isspace(UCHAR(element[elemLength-1])) /* INTL: ISO space. */
  1001. && ((elemLength < 2) || (element[elemLength-2] != '\\'))) {
  1002. elemLength--;
  1003. }
  1004. if (elemLength == 0) {
  1005. continue; /* nothing left of this element */
  1006. }
  1007. memcpy((VOID *) p, (VOID *) element, (size_t) elemLength);
  1008. p += elemLength;
  1009. *p = ' ';
  1010. p++;
  1011. finalSize += (elemLength + 1);
  1012. }
  1013. if (p != concatStr) {
  1014. p[-1] = 0;
  1015. finalSize -= 1; /* we overwrote the final ' ' */
  1016. } else {
  1017. *p = 0;
  1018. }
  1019. }
  1020. TclNewObj(objPtr);
  1021. objPtr->bytes = concatStr;
  1022. objPtr->length = finalSize;
  1023. return objPtr;
  1024. }
  1025. /*
  1026. *----------------------------------------------------------------------
  1027. *
  1028. * Tcl_StringMatch --
  1029. *
  1030. * See if a particular string matches a particular pattern.
  1031. *
  1032. * Results:
  1033. * The return value is 1 if string matches pattern, and
  1034. * 0 otherwise. The matching operation permits the following
  1035. * special characters in the pattern: *?\[] (see the manual
  1036. * entry for details on what these mean).
  1037. *
  1038. * Side effects:
  1039. * None.
  1040. *
  1041. *----------------------------------------------------------------------
  1042. */
  1043. int
  1044. Tcl_StringMatch(string, pattern)
  1045. CONST char *string; /* String. */
  1046. CONST char *pattern; /* Pattern, which may contain special
  1047. * characters. */
  1048. {
  1049. int p, s;
  1050. CONST char *pstart = pattern;
  1051. while (1) {
  1052. p = *pattern;
  1053. s = *string;
  1054. /*
  1055. * See if we're at the end of both the pattern and the string. If
  1056. * so, we succeeded. If we're at the end of the pattern but not at
  1057. * the end of the string, we failed.
  1058. */
  1059. if (p == '\0') {
  1060. if (s == '\0') {
  1061. return 1;
  1062. } else {
  1063. return 0;
  1064. }
  1065. }
  1066. if ((s == '\0') && (p != '*')) {
  1067. return 0;
  1068. }
  1069. /* Check for a "*" as the next pattern character. It matches
  1070. * any substring. We handle this by calling ourselves
  1071. * recursively for each postfix of string, until either we
  1072. * match or we reach the end of the string.
  1073. */
  1074. if (p == '*') {
  1075. pattern++;
  1076. if (*pattern == '\0') {
  1077. return 1;
  1078. }
  1079. while (1) {
  1080. if (Tcl_StringMatch(string, pattern)) {
  1081. return 1;
  1082. }
  1083. if (*string == '\0') {
  1084. return 0;
  1085. }
  1086. string++;
  1087. }
  1088. }
  1089. /* Check for a "?" as the next pattern character. It matches
  1090. * any single character.
  1091. */
  1092. if (p == '?') {
  1093. Tcl_UniChar ch;
  1094. pattern++;
  1095. string += Tcl_UtfToUniChar(string, &ch);
  1096. continue;
  1097. }
  1098. /* Check for a "[" as the next pattern character. It is followed
  1099. * by a list of characters that are acceptable, or by a range
  1100. * (two characters separated by "-").
  1101. */
  1102. if (p == '[') {
  1103. Tcl_UniChar ch, startChar, endChar;
  1104. pattern++;
  1105. string += Tcl_UtfToUniChar(string, &ch);
  1106. while (1) {
  1107. if ((*pattern == ']') || (*pattern == '\0')) {
  1108. return 0;
  1109. }
  1110. pattern += Tcl_UtfToUniChar(pattern, &startChar);
  1111. if (*pattern == '-') {
  1112. pattern++;
  1113. if (*pattern == '\0') {
  1114. return 0;
  1115. }
  1116. pattern += Tcl_UtfToUniChar(pattern, &endChar);
  1117. if (((startChar <= ch) && (ch <= endChar))
  1118. || ((endChar <= ch) && (ch <= startChar))) {
  1119. /*
  1120. * Matches ranges of form [a-z] or [z-a].
  1121. */
  1122. break;
  1123. }
  1124. } else if (startChar == ch) {
  1125. break;
  1126. }
  1127. }
  1128. while (*pattern != ']') {
  1129. if (*pattern == '\0') {
  1130. pattern = Tcl_UtfPrev(pattern, pstart);
  1131. break;
  1132. }
  1133. pattern++;
  1134. }
  1135. pattern++;
  1136. continue;
  1137. }
  1138. /* If the next pattern character is '\', just strip off the '\'
  1139. * so we do exact matching on the character that follows.
  1140. */
  1141. if (p == '\\') {
  1142. pattern++;
  1143. p = *pattern;
  1144. if (p == '\0') {
  1145. return 0;
  1146. }
  1147. }
  1148. /* There's no special character. Just make sure that the next
  1149. * bytes of each string match.
  1150. */
  1151. if (s != p) {
  1152. return 0;
  1153. }
  1154. pattern++;
  1155. string++;
  1156. }
  1157. }
  1158. /*
  1159. *----------------------------------------------------------------------
  1160. *
  1161. * Tcl_StringCaseMatch --
  1162. *
  1163. * See if a particular string matches a particular pattern.
  1164. * Allows case insensitivity.
  1165. *
  1166. * Results:
  1167. * The return value is 1 if string matches pattern, and
  1168. * 0 otherwise. The matching operation permits the following
  1169. * special characters in the pattern: *?\[] (see the manual
  1170. * entry for details on what these mean).
  1171. *
  1172. * Side effects:
  1173. * None.
  1174. *
  1175. *----------------------------------------------------------------------
  1176. */
  1177. int
  1178. Tcl_StringCaseMatch(string, pattern, nocase)
  1179. CONST char *string; /* String. */
  1180. CONST char *pattern; /* Pattern, which may contain special
  1181. * characters. */
  1182. int nocase; /* 0 for case sensitive, 1 for insensitive */
  1183. {
  1184. int p, s;
  1185. CONST char *pstart = pattern;
  1186. Tcl_UniChar ch1, ch2;
  1187. while (1) {
  1188. p = *pattern;
  1189. s = *string;
  1190. /*
  1191. * See if we're at the end of both the pattern and the string. If
  1192. * so, we succeeded. If we're at the end of the pattern but not at
  1193. * the end of the string, we failed.
  1194. */
  1195. if (p == '\0') {
  1196. return (s == '\0');
  1197. }
  1198. if ((s == '\0') && (p != '*')) {
  1199. return 0;
  1200. }
  1201. /* Check for a "*" as the next pattern character. It matches
  1202. * any substring. We handle this by calling ourselves
  1203. * recursively for each postfix of string, until either we
  1204. * match or we reach the end of the string.
  1205. */
  1206. if (p == '*') {
  1207. pattern++;
  1208. if (*pattern == '\0') {
  1209. return 1;
  1210. }
  1211. while (1) {
  1212. if (Tcl_StringCaseMatch(string, pattern, nocase)) {
  1213. return 1;
  1214. }
  1215. if (*string == '\0') {
  1216. return 0;
  1217. }
  1218. string++;
  1219. }
  1220. }
  1221. /* Check for a "?" as the next pattern character. It matches
  1222. * any single character.
  1223. */
  1224. if (p == '?') {
  1225. pattern++;
  1226. string += Tcl_UtfToUniChar(string, &ch1);
  1227. continue;
  1228. }
  1229. /* Check for a "[" as the next pattern character. It is followed
  1230. * by a list of characters that are acceptable, or by a range
  1231. * (two characters separated by "-").
  1232. */
  1233. if (p == '[') {
  1234. Tcl_UniChar startChar, endChar;
  1235. pattern++;
  1236. string += Tcl_UtfToUniChar(string, &ch1);
  1237. if (nocase) {
  1238. ch1 = Tcl_UniCharToLower(ch1);
  1239. }
  1240. while (1) {
  1241. if ((*pattern == ']') || (*pattern == '\0')) {
  1242. return 0;
  1243. }
  1244. pattern += Tcl_UtfToUniChar(pattern, &startChar);
  1245. if (nocase) {
  1246. startChar = Tcl_UniCharToLower(startChar);
  1247. }
  1248. if (*pattern == '-') {
  1249. pattern++;
  1250. if (*pattern == '\0') {
  1251. return 0;
  1252. }
  1253. pattern += Tcl_UtfToUniChar(pattern, &endChar);
  1254. if (nocase) {
  1255. endChar = Tcl_UniCharToLower(endChar);
  1256. }
  1257. if (((startChar <= ch1) && (ch1 <= endChar))
  1258. || ((endChar <= ch1) && (ch1 <= startChar))) {
  1259. /*
  1260. * Matches ranges of form [a-z] or [z-a].
  1261. */
  1262. break;
  1263. }
  1264. } else if (startChar == ch1) {
  1265. break;
  1266. }
  1267. }
  1268. while (*pattern != ']') {
  1269. if (*pattern == '\0') {
  1270. pattern = Tcl_UtfPrev(pattern, pstart);
  1271. break;
  1272. }
  1273. pattern++;
  1274. }
  1275. pattern++;
  1276. continue;
  1277. }
  1278. /* If the next pattern character is '\', just strip off the '\'
  1279. * so we do exact matching on the character that follows.
  1280. */
  1281. if (p == '\\') {
  1282. pattern++;
  1283. p = *pattern;
  1284. if (p == '\0') {
  1285. return 0;
  1286. }
  1287. }
  1288. /* There's no special character. Just make sure that the next
  1289. * bytes of each string match.
  1290. */
  1291. string += Tcl_UtfToUniChar(string, &ch1);
  1292. pattern += Tcl_UtfToUniChar(pattern, &ch2);
  1293. if (nocase) {
  1294. if (Tcl_UniCharToLower(ch1) != Tcl_UniCharToLower(ch2)) {
  1295. return 0;
  1296. }
  1297. } else if (ch1 != ch2) {
  1298. return 0;
  1299. }
  1300. }
  1301. }
  1302. /*
  1303. *----------------------------------------------------------------------
  1304. *
  1305. * Tcl_DStringInit --
  1306. *
  1307. * Initializes a dynamic string, discarding any previous contents
  1308. * of the string (Tcl_DStringFree should have been called already
  1309. * if the dynamic string was previously in use).
  1310. *
  1311. * Results:
  1312. * None.
  1313. *
  1314. * Side effects:
  1315. * The dynamic string is initialized to be empty.
  1316. *
  1317. *----------------------------------------------------------------------
  1318. */
  1319. void
  1320. Tcl_DStringInit(dsPtr)
  1321. Tcl_DString *dsPtr; /* Pointer to structure for dynamic string. */
  1322. {
  1323. dsPtr->string = dsPtr->staticSpace;
  1324. dsPtr->length = 0;
  1325. dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
  1326. dsPtr->staticSpace[0] = '\0';
  1327. }
  1328. /*
  1329. *----------------------------------------------------------------------
  1330. *
  1331. * Tcl_DStringAppend --
  1332. *
  1333. * Append more characters to the current value of a dynamic string.
  1334. *
  1335. * Results:
  1336. * The return value is a pointer to the dynamic string's new value.
  1337. *
  1338. * Side effects:
  1339. * Length bytes from string (or all of string if length is less
  1340. * than zero) are added to the current value of the string. Memory
  1341. * gets reallocated if needed to accomodate the string's new size.
  1342. *
  1343. *----------------------------------------------------------------------
  1344. */
  1345. char *
  1346. Tcl_DStringAppend(dsPtr, string, length)
  1347. Tcl_DString *dsPtr; /* Structure describing dynamic string. */
  1348. CONST char *string; /* String to append. If length is -1 then
  1349. * this must be null-terminated. */
  1350. int length; /* Number of characters from string to
  1351. * append. If < 0, then append all of string,
  1352. * up to null at end. */
  1353. {
  1354. int newSize;
  1355. char *dst;
  1356. CONST char *end;
  1357. if (length < 0) {
  1358. length = strlen(string);
  1359. }
  1360. newSize = length + dsPtr->length;
  1361. /*
  1362. * Allocate a larger buffer for the string if the current one isn't
  1363. * large enough. Allocate extra space in the new buffer so that there
  1364. * will be room to grow before we have to allocate again.
  1365. */
  1366. if (newSize >= dsPtr->spaceAvl) {
  1367. dsPtr->spaceAvl = newSize * 2;
  1368. if (dsPtr->string == dsPtr->staticSpace) {
  1369. char *newString;
  1370. newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl);
  1371. memcpy((VOID *) newString, (VOID *) dsPtr->string,
  1372. (size_t) dsPtr->length);
  1373. dsPtr->string = newString;
  1374. } else {
  1375. dsPtr->string = (char *) ckrealloc((VOID *) dsPtr->string,
  1376. (size_t) dsPtr->spaceAvl);
  1377. }
  1378. }
  1379. /*
  1380. * Copy the new string into the buffer at the end of the old
  1381. * one.
  1382. */
  1383. for (dst = dsPtr->string + dsPtr->length, end = string+length;
  1384. string < end; string++, dst++) {
  1385. *dst = *string;
  1386. }
  1387. *dst = '\0';
  1388. dsPtr->length += length;
  1389. return dsPtr->string;
  1390. }
  1391. /*
  1392. *----------------------------------------------------------------------
  1393. *
  1394. * Tcl_DStringAppendElement --
  1395. *
  1396. * Append a list element to the current value of a dynamic string.
  1397. *
  1398. * Results:
  1399. * The return value is a pointer to the dynamic string's new value.
  1400. *
  1401. * Side effects:
  1402. * String is reformatted as a list element and added to the current
  1403. * value of the string. Memory gets reallocated if needed to
  1404. * accomodate the string's new size.
  1405. *
  1406. *----------------------------------------------------------------------
  1407. */
  1408. char *
  1409. Tcl_DStringAppendElement(dsPtr, string)
  1410. Tcl_DString *dsPtr; /* Structure describing dynamic string. */
  1411. CONST char *string; /* String to append. Must be
  1412. * null-terminated. */
  1413. {
  1414. int newSize, flags;
  1415. char *dst;
  1416. newSize = Tcl_ScanElement(string, &flags) + dsPtr->length + 1;
  1417. /*
  1418. * Allocate a larger buffer for the string if the current one isn't
  1419. * large enough. Allocate extra space in the new buffer so that there
  1420. * will be room to grow before we have to allocate again.
  1421. * SPECIAL NOTE: must use memcpy, not strcpy, to copy the string
  1422. * to a larger buffer, since there may be embedded NULLs in the
  1423. * string in some cases.
  1424. */
  1425. if (newSize >= dsPtr->spaceAvl) {
  1426. dsPtr->spaceAvl = newSize * 2;
  1427. if (dsPtr->string == dsPtr->staticSpace) {
  1428. char *newString;
  1429. newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl);
  1430. memcpy((VOID *) newString, (VOID *) dsPtr->string,
  1431. (size_t) dsPtr->length);
  1432. dsPtr->string = newString;
  1433. } else {
  1434. dsPtr->string = (char *) ckrealloc((VOID *) dsPtr->string,
  1435. (size_t) dsPtr->spaceAvl);
  1436. }
  1437. }
  1438. /*
  1439. * Convert the new string to a list element and copy it into the
  1440. * buffer at the end, with a space, if needed.
  1441. */
  1442. dst = dsPtr->string + dsPtr->length;
  1443. if (TclNeedSpace(dsPtr->string, dst)) {
  1444. *dst = ' ';
  1445. dst++;
  1446. dsPtr->length++;
  1447. }
  1448. dsPtr->length += Tcl_ConvertElement(string, dst, flags);
  1449. return dsPtr->string;
  1450. }
  1451. /*
  1452. *----------------------------------------------------------------------
  1453. *
  1454. * Tcl_DStringSetLength --
  1455. *
  1456. * Change the length of a dynamic string. This can cause the
  1457. * string to either grow or shrink, depending on the value of
  1458. * length.
  1459. *
  1460. * Results:
  1461. * None.
  1462. *
  1463. * Side effects:
  1464. * The length of dsPtr is changed to length and a null byte is
  1465. * stored at that position in the string. If length is larger
  1466. * than the space allocated for dsPtr, then a panic occurs.
  1467. *
  1468. *----------------------------------------------------------------------
  1469. */
  1470. void
  1471. Tcl_DStringSetLength(dsPtr, length)
  1472. Tcl_DString *dsPtr; /* Structure describing dynamic string. */
  1473. int length; /* New length for dynamic string. */
  1474. {
  1475. int newsize;
  1476. if (length < 0) {
  1477. length = 0;
  1478. }
  1479. if (length >= dsPtr->spaceAvl) {
  1480. /*
  1481. * There are two interesting cases here. In the first case, the user
  1482. * may be trying to allocate a large buffer of a specific size. It
  1483. * would be wasteful to overallocate that buffer, so we just allocate
  1484. * enough for the requested size plus the trailing null byte. In the
  1485. * second case, we are growing the buffer incrementally, so we need
  1486. * behavior similar to Tcl_DStringAppend. The requested length will
  1487. * usually be a small delta above the current spaceAvl, so we'll end up
  1488. * doubling the old size. This won't grow the buffer quite as quickly,
  1489. * but it should be close enough.
  1490. */
  1491. newsize = dsPtr->spaceAvl * 2;
  1492. if (length < newsize) {
  1493. dsPtr->spaceAvl = newsize;
  1494. } else {
  1495. dsPtr->spaceAvl = length + 1;
  1496. }
  1497. if (dsPtr->string == dsPtr->staticSpace) {
  1498. char *newString;
  1499. newString = (char *) ckalloc((unsigned) dsPtr->spaceAvl);
  1500. memcpy((VOID *) newString, (VOID *) dsPtr->string,
  1501. (size_t) dsPtr->length);
  1502. dsPtr->string = newString;
  1503. } else {
  1504. dsPtr->string = (char *) ckrealloc((VOID *) dsPtr->string,
  1505. (size_t) dsPtr->spaceAvl);
  1506. }
  1507. }
  1508. dsPtr->length = length;
  1509. dsPtr->string[length] = 0;
  1510. }
  1511. /*
  1512. *----------------------------------------------------------------------
  1513. *
  1514. * Tcl_DStringFree --
  1515. *
  1516. * Frees up any memory allocated for the dynamic string and
  1517. * reinitializes the string to an empty state.
  1518. *
  1519. * Results:
  1520. * None.
  1521. *
  1522. * Side effects:
  1523. * The previous contents of the dynamic string are lost, and
  1524. * the new value is an empty string.
  1525. *
  1526. *---------------------------------------------------------------------- */
  1527. void
  1528. Tcl_DStringFree(dsPtr)
  1529. Tcl_DString *dsPtr; /* Structure describing dynamic string. */
  1530. {
  1531. if (dsPtr->string != dsPtr->staticSpace) {
  1532. ckfree(dsPtr->string);
  1533. }
  1534. dsPtr->string = dsPtr->staticSpace;
  1535. dsPtr->length = 0;
  1536. dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
  1537. dsPtr->staticSpace[0] = '\0';
  1538. }
  1539. /*
  1540. *----------------------------------------------------------------------
  1541. *
  1542. * Tcl_DStringResult --
  1543. *
  1544. * This procedure moves the value of a dynamic string into an
  1545. * interpreter as its string result. Afterwards, the dynamic string
  1546. * is reset to an empty string.
  1547. *
  1548. * Results:
  1549. * None.
  1550. *
  1551. * Side effects:
  1552. * The string is "moved" to interp's result, and any existing
  1553. * string result for interp is freed. dsPtr is reinitialized to
  1554. * an empty string.
  1555. *
  1556. *----------------------------------------------------------------------
  1557. */
  1558. void
  1559. Tcl_DStringResult(interp, dsPtr)
  1560. Tcl_Interp *interp; /* Interpreter whose result is to be reset. */
  1561. Tcl_DString *dsPtr; /* Dynamic string that is to become the
  1562. * result of interp. */
  1563. {
  1564. Tcl_ResetResult(interp);
  1565. if (dsPtr->string != dsPtr->staticSpace) {
  1566. interp->result = dsPtr->string;
  1567. interp->freeProc = TCL_DYNAMIC;
  1568. } else if (dsPtr->length < TCL_RESULT_SIZE) {
  1569. interp->result = ((Interp *) interp)->resultSpace;
  1570. strcpy(interp->result, dsPtr->string);
  1571. } else {
  1572. Tcl_SetResult(interp, dsPtr->string, TCL_VOLATILE);
  1573. }
  1574. dsPtr->string = dsPtr->staticSpace;
  1575. dsPtr->length = 0;
  1576. dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
  1577. dsPtr->staticSpace[0] = '\0';
  1578. }
  1579. /*
  1580. *----------------------------------------------------------------------
  1581. *
  1582. * Tcl_DStringGetResult --
  1583. *
  1584. * This procedure moves an interpreter's result into a dynamic string.
  1585. *
  1586. * Results:
  1587. * None.
  1588. *
  1589. * Side effects:
  1590. * The interpreter's string result is cleared, and the previous
  1591. * contents of dsPtr are freed.
  1592. *
  1593. * If the string result is empty, the object result is moved to the
  1594. * string result, then the object result is reset.
  1595. *
  1596. *----------------------------------------------------------------------
  1597. */
  1598. void
  1599. Tcl_DStringGetResult(interp, dsPtr)
  1600. Tcl_Interp *interp; /* Interpreter whose result is to be reset. */
  1601. Tcl_DString *dsPtr; /* Dynamic string that is to become the
  1602. * result of interp. */
  1603. {
  1604. Interp *iPtr = (Interp *) interp;
  1605. if (dsPtr->string != dsPtr->staticSpace) {
  1606. ckfree(dsPtr->string);
  1607. }
  1608. /*
  1609. * If the string result is empty, move the object result to the
  1610. * string result, then reset the object result.
  1611. */
  1612. if (*(iPtr->result) == 0) {
  1613. Tcl_SetResult(interp, TclGetString(Tcl_GetObjResult(interp)),
  1614. TCL_VOLATILE);
  1615. }
  1616. dsPtr->length = strlen(iPtr->result);
  1617. if (iPtr->freeProc != NULL) {
  1618. if ((iPtr->freeProc == TCL_DYNAMIC)
  1619. || (iPtr->freeProc == (Tcl_FreeProc *) free)) {
  1620. dsPtr->string = iPtr->result;
  1621. dsPtr->spaceAvl = dsPtr->length+1;
  1622. } else {
  1623. dsPtr->string = (char *) ckalloc((unsigned) (dsPtr->length+1));
  1624. strcpy(dsPtr->string, iPtr->result);
  1625. (*iPtr->freeProc)(iPtr->result);
  1626. }
  1627. dsPtr->spaceAvl = dsPtr->length+1;
  1628. iPtr->freeProc = NULL;
  1629. } else {
  1630. if (dsPtr->length < TCL_DSTRING_STATIC_SIZE) {
  1631. dsPtr->string = dsPtr->staticSpace;
  1632. dsPtr->spaceAvl = TCL_DSTRING_STATIC_SIZE;
  1633. } else {
  1634. dsPtr->string = (char *) ckalloc((unsigned) (dsPtr->length + 1));
  1635. dsPtr->spaceAvl = dsPtr->length + 1;
  1636. }
  1637. strcpy(dsPtr->string, iPtr->result);
  1638. }
  1639. iPtr->result = iPtr->resultSpace;
  1640. iPtr->resultSpace[0] = 0;
  1641. }
  1642. /*
  1643. *----------------------------------------------------------------------
  1644. *
  1645. * Tcl_DStringStartSublist --
  1646. *
  1647. * This procedure adds the necessary information to a dynamic
  1648. * string (e.g. " {" to start a sublist. Future element
  1649. * appends will be in the sublist rather than the main list.
  1650. *
  1651. * Results:
  1652. * None.
  1653. *
  1654. * Side effects:
  1655. * Characters get added to the dynamic string.
  1656. *
  1657. *----------------------------------------------------------------------
  1658. */
  1659. void
  1660. Tcl_DStringStartSublist(dsPtr)
  1661. Tcl_DString *dsPtr; /* Dynamic string. */
  1662. {
  1663. if (TclNeedSpace(dsPtr->string, dsPtr->string + dsPtr->length)) {
  1664. Tcl_DStringAppend(dsPtr, " {", -1);
  1665. } else {
  1666. Tcl_DStringAppend(dsPtr, "{", -1);
  1667. }
  1668. }
  1669. /*
  1670. *----------------------------------------------------------------------
  1671. *
  1672. * Tcl_DStringEndSublist --
  1673. *
  1674. * This procedure adds the necessary characters to a dynamic
  1675. * string to end a sublist (e.g. "}"). Future element appends
  1676. * will be in the enclosing (sub)list rather than the current
  1677. * sublist.
  1678. *
  1679. * Results:
  1680. * None.
  1681. *
  1682. * Side effects:
  1683. * None.
  1684. *
  1685. *----------------------------------------------------------------------
  1686. */
  1687. void
  1688. Tcl_DStringEndSublist(dsPtr)
  1689. Tcl_DString *dsPtr; /* Dynamic string. */
  1690. {
  1691. Tcl_DStringAppend(dsPtr, "}", -1);
  1692. }
  1693. /*
  1694. *----------------------------------------------------------------------
  1695. *
  1696. * Tcl_PrintDouble --
  1697. *
  1698. * Given a floating-point value, this procedure converts it to
  1699. * an ASCII string using.
  1700. *
  1701. * Results:
  1702. * The ASCII equivalent of "value" is written at "dst". It is
  1703. * written using the current precision, and it is guaranteed to
  1704. * contain a decimal point or exponent, so that it looks like
  1705. * a floating-point value and not an integer.
  1706. *
  1707. * Side effects:
  1708. * None.
  1709. *
  1710. *----------------------------------------------------------------------
  1711. */
  1712. void
  1713. Tcl_PrintDouble(interp, value, dst)
  1714. Tcl_Interp *interp; /* Interpreter whose tcl_precision
  1715. * variable used to be used to control
  1716. * printing. It's ignored now. */
  1717. double value; /* Value to print as string. */
  1718. char *dst; /* Where to store converted value;
  1719. * must have at least TCL_DOUBLE_SPACE
  1720. * characters. */
  1721. {
  1722. char *p, c;
  1723. Tcl_UniChar ch;
  1724. Tcl_MutexLock(&pr