stk/Tcl/tclUtil.c

3013 lines
78 KiB
C
Raw Normal View History

1996-09-27 06:29:02 -04:00
/*
* tclUtil.c --
*
* This file contains utility procedures that are used by many Tcl
* commands.
*
* Copyright (c) 1987-1993 The Regents of the University of California.
1998-04-10 06:59:06 -04:00
* Copyright (c) 1994-1997 Sun Microsystems, Inc.
1996-09-27 06:29:02 -04:00
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
1998-09-30 07:11:02 -04:00
* SCCS: @(#) tclUtil.c 1.2 98/06/12 16:49:08
1996-09-27 06:29:02 -04:00
*/
#include "tclInt.h"
#include "tclPort.h"
1998-09-30 07:11:02 -04:00
/*
* The following variable holds the full path name of the binary
* from which this application was executed, or NULL if it isn't
* know. The value of the variable is set by the procedure
* Tcl_FindExecutable. The storage space is dynamically allocated.
*/
char *tclExecutableName = NULL;
1996-09-27 06:29:02 -04:00
/*
* The following values are used in the flags returned by Tcl_ScanElement
* and used by Tcl_ConvertElement. The value TCL_DONT_USE_BRACES is also
* defined in tcl.h; make sure its value doesn't overlap with any of the
* values below.
*
* TCL_DONT_USE_BRACES - 1 means the string mustn't be enclosed in
* braces (e.g. it contains unmatched braces,
* or ends in a backslash character, or user
* just doesn't want braces); handle all
* special characters by adding backslashes.
* USE_BRACES - 1 means the string contains a special
* character that can be handled simply by
* enclosing the entire argument in braces.
* BRACES_UNMATCHED - 1 means that braces aren't properly matched
* in the argument.
*/
#define USE_BRACES 2
#define BRACES_UNMATCHED 4
1998-04-10 06:59:06 -04:00
/*
* The following values determine the precision used when converting
* floating-point values to strings. This information is linked to all
* of the tcl_precision variables in all interpreters via the procedure
* TclPrecTraceProc.
*
* NOTE: these variables are not thread-safe.
*/
1999-09-05 07:16:41 -04:00
#ifndef SCM_CODE
1998-04-10 06:59:06 -04:00
static char precisionString[10] = "12";
/* The string value of all the tcl_precision
* variables. */
1998-09-30 07:11:02 -04:00
#endif
1998-04-10 06:59:06 -04:00
static char precisionFormat[10] = "%.12g";
/* The format string actually used in calls
* to sprintf. */
1996-09-27 06:29:02 -04:00
/*
* Function prototypes for local procedures in this file:
*/
static void SetupAppendBuffer _ANSI_ARGS_((Interp *iPtr,
int newSpace));
/*
*----------------------------------------------------------------------
*
* TclFindElement --
*
* Given a pointer into a Tcl list, locate the first (or next)
* element in the list.
*
* Results:
* The return value is normally TCL_OK, which means that the
* element was successfully located. If TCL_ERROR is returned
* it means that list didn't have proper list structure;
* interp->result contains a more detailed error message.
*
1998-04-10 06:59:06 -04:00
* If TCL_OK is returned, then *elementPtr will be set to point to the
* first element of list, and *nextPtr will be set to point to the
* character just after any white space following the last character
* that's part of the element. If this is the last argument in the
* list, then *nextPtr will point just after the last character in the
* list (i.e., at the character at list+listLength). If sizePtr is
* non-NULL, *sizePtr is filled in with the number of characters in the
* element. If the element is in braces, then *elementPtr will point
* to the character after the opening brace and *sizePtr will not
* include either of the braces. If there isn't an element in the list,
* *sizePtr will be zero, and both *elementPtr and *termPtr will point
* just after the last character in the list. Note: this procedure does
* NOT collapse backslash sequences.
1996-09-27 06:29:02 -04:00
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
1998-04-10 06:59:06 -04:00
TclFindElement(interp, list, listLength, elementPtr, nextPtr, sizePtr,
bracePtr)
1996-09-27 06:29:02 -04:00
Tcl_Interp *interp; /* Interpreter to use for error reporting.
* If NULL, then no error message is left
* after errors. */
1998-04-10 06:59:06 -04:00
char *list; /* Points to the first byte of a string
* containing a Tcl list with zero or more
* elements (possibly in braces). */
int listLength; /* Number of bytes in the list's string. */
char **elementPtr; /* Where to put address of first significant
1996-09-27 06:29:02 -04:00
* character in first element of list. */
char **nextPtr; /* Fill in with location of character just
* after all white space following end of
1998-04-10 06:59:06 -04:00
* argument (next arg or end of list). */
1996-09-27 06:29:02 -04:00
int *sizePtr; /* If non-zero, fill in with size of
* element. */
1998-04-10 06:59:06 -04:00
int *bracePtr; /* If non-zero, fill in with non-zero/zero
1996-09-27 06:29:02 -04:00
* to indicate that arg was/wasn't
* in braces. */
{
1998-04-10 06:59:06 -04:00
char *p = list;
char *elemStart; /* Points to first byte of first element. */
char *limit; /* Points just after list's last byte. */
int openBraces = 0; /* Brace nesting level during parse. */
1996-09-27 06:29:02 -04:00
int inQuotes = 0;
1998-04-10 06:59:06 -04:00
int size = 0; /* Init. avoids compiler warning. */
int numChars;
char *p2;
1996-09-27 06:29:02 -04:00
/*
* Skim off leading white space and check for an opening brace or
1998-04-10 06:59:06 -04:00
* quote. We treat embedded NULLs in the list as bytes belonging to
* a list element. Note: use of "isascii" below and elsewhere in this
1996-09-27 06:29:02 -04:00
* procedure is a temporary hack (7/27/90) because Mx uses characters
1998-04-10 06:59:06 -04:00
* with the high-order bit set for some things. This should probably
1996-09-27 06:29:02 -04:00
* be changed back eventually, or all of Tcl should call isascii.
*/
1998-04-10 06:59:06 -04:00
limit = (list + listLength);
while ((p < limit) && (isspace(UCHAR(*p)))) {
p++;
}
if (p == limit) { /* no element found */
elemStart = limit;
goto done;
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
1999-09-05 07:16:41 -04:00
#ifdef SCM_CODE
1998-04-10 06:59:06 -04:00
if (*p == '(') {
1996-09-27 06:29:02 -04:00
#else
1998-04-10 06:59:06 -04:00
if (*p == '{') {
#endif
1996-09-27 06:29:02 -04:00
openBraces = 1;
1998-04-10 06:59:06 -04:00
p++;
} else if (*p == '"') {
1996-09-27 06:29:02 -04:00
inQuotes = 1;
1998-04-10 06:59:06 -04:00
p++;
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
elemStart = p;
1996-09-27 06:29:02 -04:00
if (bracePtr != 0) {
*bracePtr = openBraces;
}
/*
1998-04-10 06:59:06 -04:00
* Find element's end (a space, close brace, or the end of the string).
1996-09-27 06:29:02 -04:00
*/
1998-04-10 06:59:06 -04:00
while (p < limit) {
1996-09-27 06:29:02 -04:00
switch (*p) {
/*
1998-04-10 06:59:06 -04:00
* Open brace: don't treat specially unless the element is in
* braces. In this case, keep a nesting count.
1996-09-27 06:29:02 -04:00
*/
1999-09-05 07:16:41 -04:00
#ifdef SCM_CODE
1998-04-10 06:59:06 -04:00
case '(':
1996-09-27 06:29:02 -04:00
#else
1998-04-10 06:59:06 -04:00
case '{':
1996-09-27 06:29:02 -04:00
#endif
if (openBraces != 0) {
openBraces++;
}
break;
/*
1998-04-10 06:59:06 -04:00
* Close brace: if element is in braces, keep nesting count and
* quit when the last close brace is seen.
1996-09-27 06:29:02 -04:00
*/
1999-09-05 07:16:41 -04:00
#ifdef SCM_CODE
1996-09-27 06:29:02 -04:00
case ')':
#else
1998-04-10 06:59:06 -04:00
case '}':
1996-09-27 06:29:02 -04:00
#endif
1998-04-10 06:59:06 -04:00
if (openBraces > 1) {
openBraces--;
} else if (openBraces == 1) {
size = (p - elemStart);
1996-09-27 06:29:02 -04:00
p++;
1998-04-10 06:59:06 -04:00
if ((p >= limit) || isspace(UCHAR(*p))) {
1996-09-27 06:29:02 -04:00
goto done;
}
1998-04-10 06:59:06 -04:00
/*
* Garbage after the closing brace; return an error.
*/
1996-09-27 06:29:02 -04:00
if (interp != NULL) {
1998-04-10 06:59:06 -04:00
char buf[100];
p2 = p;
while ((p2 < limit) && (!isspace(UCHAR(*p2)))
&& (p2 < p+20)) {
p2++;
}
sprintf(buf,
1996-09-27 06:29:02 -04:00
"list element in braces followed by \"%.*s\" instead of space",
(int) (p2-p), p);
1998-04-10 06:59:06 -04:00
Tcl_SetResult(interp, buf, TCL_VOLATILE);
1996-09-27 06:29:02 -04:00
}
return TCL_ERROR;
}
break;
/*
* Backslash: skip over everything up to the end of the
* backslash sequence.
*/
case '\\': {
1998-04-10 06:59:06 -04:00
(void) Tcl_Backslash(p, &numChars);
p += (numChars - 1);
1996-09-27 06:29:02 -04:00
break;
}
/*
1998-04-10 06:59:06 -04:00
* Space: ignore if element is in braces or quotes; otherwise
1996-09-27 06:29:02 -04:00
* terminate element.
*/
case ' ':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
if ((openBraces == 0) && !inQuotes) {
1998-04-10 06:59:06 -04:00
size = (p - elemStart);
1996-09-27 06:29:02 -04:00
goto done;
}
break;
/*
1998-04-10 06:59:06 -04:00
* Double-quote: if element is in quotes then terminate it.
1996-09-27 06:29:02 -04:00
*/
case '"':
if (inQuotes) {
1998-04-10 06:59:06 -04:00
size = (p - elemStart);
1996-09-27 06:29:02 -04:00
p++;
1998-04-10 06:59:06 -04:00
if ((p >= limit) || isspace(UCHAR(*p))) {
1996-09-27 06:29:02 -04:00
goto done;
}
1998-04-10 06:59:06 -04:00
/*
* Garbage after the closing quote; return an error.
*/
1996-09-27 06:29:02 -04:00
if (interp != NULL) {
1998-04-10 06:59:06 -04:00
char buf[100];
p2 = p;
while ((p2 < limit) && (!isspace(UCHAR(*p2)))
&& (p2 < p+20)) {
p2++;
}
sprintf(buf,
"list element in quotes followed by \"%.*s\" %s",
(int) (p2-p), p, "instead of space");
Tcl_SetResult(interp, buf, TCL_VOLATILE);
1996-09-27 06:29:02 -04:00
}
return TCL_ERROR;
}
break;
1998-04-10 06:59:06 -04:00
}
p++;
}
1996-09-27 06:29:02 -04:00
1998-04-10 06:59:06 -04:00
/*
* End of list: terminate element.
*/
1996-09-27 06:29:02 -04:00
1998-04-10 06:59:06 -04:00
if (p == limit) {
if (openBraces != 0) {
if (interp != NULL) {
Tcl_SetResult(interp, "unmatched open brace in list",
TCL_STATIC);
}
return TCL_ERROR;
} else if (inQuotes) {
if (interp != NULL) {
Tcl_SetResult(interp, "unmatched open quote in list",
TCL_STATIC);
}
return TCL_ERROR;
1996-09-27 06:29:02 -04:00
}
1998-04-10 06:59:06 -04:00
size = (p - elemStart);
1996-09-27 06:29:02 -04:00
}
done:
1998-04-10 06:59:06 -04:00
while ((p < limit) && (isspace(UCHAR(*p)))) {
1996-09-27 06:29:02 -04:00
p++;
}
1998-04-10 06:59:06 -04:00
*elementPtr = elemStart;
1996-09-27 06:29:02 -04:00
*nextPtr = p;
if (sizePtr != 0) {
*sizePtr = size;
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* TclCopyAndCollapse --
*
* Copy a string and eliminate any backslashes that aren't in braces.
*
* Results:
1998-04-10 06:59:06 -04:00
* There is no return value. Count characters get copied from src to
* dst. Along the way, if backslash sequences are found outside braces,
* the backslashes are eliminated in the copy. After scanning count
* chars from source, a null character is placed at the end of dst.
* Returns the number of characters that got copied.
1996-09-27 06:29:02 -04:00
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
1998-04-10 06:59:06 -04:00
int
1996-09-27 06:29:02 -04:00
TclCopyAndCollapse(count, src, dst)
1998-04-10 06:59:06 -04:00
int count; /* Number of characters to copy from src. */
char *src; /* Copy from here... */
char *dst; /* ... to here. */
1996-09-27 06:29:02 -04:00
{
1998-04-10 06:59:06 -04:00
char c;
1996-09-27 06:29:02 -04:00
int numRead;
1998-04-10 06:59:06 -04:00
int newCount = 0;
1996-09-27 06:29:02 -04:00
1998-04-10 06:59:06 -04:00
for (c = *src; count > 0; src++, c = *src, count--) {
1996-09-27 06:29:02 -04:00
if (c == '\\') {
*dst = Tcl_Backslash(src, &numRead);
dst++;
src += numRead-1;
count -= numRead-1;
1998-04-10 06:59:06 -04:00
newCount++;
1996-09-27 06:29:02 -04:00
} else {
*dst = c;
dst++;
1998-04-10 06:59:06 -04:00
newCount++;
1996-09-27 06:29:02 -04:00
}
}
*dst = 0;
1998-04-10 06:59:06 -04:00
return newCount;
1996-09-27 06:29:02 -04:00
}
/*
*----------------------------------------------------------------------
*
* Tcl_SplitList --
*
* Splits a list up into its constituent fields.
*
* Results
* The return value is normally TCL_OK, which means that
* the list was successfully split up. If TCL_ERROR is
* returned, it means that "list" didn't have proper list
* structure; interp->result will contain a more detailed
* error message.
*
* *argvPtr will be filled in with the address of an array
* whose elements point to the elements of list, in order.
* *argcPtr will get filled in with the number of valid elements
* in the array. A single block of memory is dynamically allocated
* to hold both the argv array and a copy of the list (with
* backslashes and braces removed in the standard way).
* The caller must eventually free this memory by calling free()
* on *argvPtr. Note: *argvPtr and *argcPtr are only modified
* if the procedure returns normally.
*
* Side effects:
* Memory is allocated.
*
*----------------------------------------------------------------------
*/
int
Tcl_SplitList(interp, list, argcPtr, argvPtr)
Tcl_Interp *interp; /* Interpreter to use for error reporting.
1998-04-10 06:59:06 -04:00
* If NULL, no error message is left. */
1996-09-27 06:29:02 -04:00
char *list; /* Pointer to string with list structure. */
int *argcPtr; /* Pointer to location to fill in with
* the number of elements in the list. */
1998-04-10 06:59:06 -04:00
char ***argvPtr; /* Pointer to place to store pointer to
* array of pointers to list elements. */
1996-09-27 06:29:02 -04:00
{
char **argv;
1998-04-10 06:59:06 -04:00
char *p;
int length, size, i, result, elSize, brace;
1996-09-27 06:29:02 -04:00
char *element;
/*
* Figure out how much space to allocate. There must be enough
* space for both the array of pointers and also for a copy of
* the list. To estimate the number of pointers needed, count
* the number of space characters in the list.
*/
1999-09-05 07:16:41 -04:00
#ifdef SCM_CODE
1996-09-27 06:29:02 -04:00
{
char *q;
for (size = 1, p = q = list; *p != 0; p++) {
if (isspace(UCHAR(*p))) size++;
if (*p == ')') q = p;
}
/*
* Tcl considers strings of the form "( ... )" as quoted string
* (rather than lists). So if the string is of this form, open
* and close parenthesis are replaced by spaces
*/
if (q > list && *list == '(' && *q == ')') {
*list = *q = ' ';
}
}
#else
for (size = 1, p = list; *p != 0; p++) {
if (isspace(UCHAR(*p))) {
size++;
}
}
#endif
size++; /* Leave space for final NULL pointer. */
argv = (char **) ckalloc((unsigned)
((size * sizeof(char *)) + (p - list) + 1));
1998-04-10 06:59:06 -04:00
length = strlen(list);
1996-09-27 06:29:02 -04:00
for (i = 0, p = ((char *) argv) + size*sizeof(char *);
1998-04-10 06:59:06 -04:00
*list != 0; i++) {
char *prevList = list;
result = TclFindElement(interp, list, length, &element,
&list, &elSize, &brace);
length -= (list - prevList);
1996-09-27 06:29:02 -04:00
if (result != TCL_OK) {
ckfree((char *) argv);
return result;
}
if (*element == 0) {
break;
}
if (i >= size) {
ckfree((char *) argv);
if (interp != NULL) {
Tcl_SetResult(interp, "internal error in Tcl_SplitList",
TCL_STATIC);
}
return TCL_ERROR;
}
argv[i] = p;
if (brace) {
1998-04-10 06:59:06 -04:00
memcpy((VOID *) p, (VOID *) element, (size_t) elSize);
1996-09-27 06:29:02 -04:00
p += elSize;
*p = 0;
p++;
} else {
TclCopyAndCollapse(elSize, element, p);
p += elSize+1;
}
}
argv[i] = NULL;
*argvPtr = argv;
*argcPtr = i;
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_ScanElement --
*
* This procedure is a companion procedure to Tcl_ConvertElement.
1998-04-10 06:59:06 -04:00
* It scans a string to see what needs to be done to it (e.g. add
* backslashes or enclosing braces) to make the string into a
* valid Tcl list element.
1996-09-27 06:29:02 -04:00
*
* Results:
* The return value is an overestimate of the number of characters
* that will be needed by Tcl_ConvertElement to produce a valid
* list element from string. The word at *flagPtr is filled in
* with a value needed by Tcl_ConvertElement when doing the actual
* conversion.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ScanElement(string, flagPtr)
1998-04-10 06:59:06 -04:00
CONST char *string; /* String to convert to Tcl list element. */
int *flagPtr; /* Where to store information to guide
* Tcl_ConvertCountedElement. */
{
return Tcl_ScanCountedElement(string, -1, flagPtr);
}
/*
*----------------------------------------------------------------------
*
* Tcl_ScanCountedElement --
*
* This procedure is a companion procedure to
* Tcl_ConvertCountedElement. It scans a string to see what
* needs to be done to it (e.g. add backslashes or enclosing
* braces) to make the string into a valid Tcl list element.
* If length is -1, then the string is scanned up to the first
* null byte.
*
* Results:
* The return value is an overestimate of the number of characters
* that will be needed by Tcl_ConvertCountedElement to produce a
* valid list element from string. The word at *flagPtr is
* filled in with a value needed by Tcl_ConvertCountedElement
* when doing the actual conversion.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ScanCountedElement(string, length, flagPtr)
CONST char *string; /* String to convert to Tcl list element. */
int length; /* Number of bytes in string, or -1. */
1996-09-27 06:29:02 -04:00
int *flagPtr; /* Where to store information to guide
* Tcl_ConvertElement. */
{
int flags, nestingLevel;
1998-04-10 06:59:06 -04:00
CONST char *p, *lastChar;
1996-09-27 06:29:02 -04:00
/*
* This procedure and Tcl_ConvertElement together do two things:
*
* 1. They produce a proper list, one that will yield back the
* argument strings when evaluated or when disassembled with
* Tcl_SplitList. This is the most important thing.
*
* 2. They try to produce legible output, which means minimizing the
* use of backslashes (using braces instead). However, there are
* some situations where backslashes must be used (e.g. an element
1998-04-10 06:59:06 -04:00
* like "{abc": the leading brace will have to be backslashed.
* For each element, one of three things must be done:
1996-09-27 06:29:02 -04:00
*
1998-04-10 06:59:06 -04:00
* (a) Use the element as-is (it doesn't contain any special
1996-09-27 06:29:02 -04:00
* characters). This is the most desirable option.
*
* (b) Enclose the element in braces, but leave the contents alone.
* This happens if the element contains embedded space, or if it
* contains characters with special interpretation ($, [, ;, or \),
* or if it starts with a brace or double-quote, or if there are
* no characters in the element.
*
* (c) Don't enclose the element in braces, but add backslashes to
* prevent special interpretation of special characters. This is a
* last resort used when the argument would normally fall under case
* (b) but contains unmatched braces. It also occurs if the last
* character of the argument is a backslash or if the element contains
* a backslash followed by newline.
*
* The procedure figures out how many bytes will be needed to store
1998-04-10 06:59:06 -04:00
* the result (actually, it overestimates). It also collects information
1996-09-27 06:29:02 -04:00
* about the element in the form of a flags word.
1998-04-10 06:59:06 -04:00
*
* Note: list elements produced by this procedure and
* Tcl_ConvertCountedElement must have the property that they can be
* enclosing in curly braces to make sub-lists. This means, for
* example, that we must not leave unmatched curly braces in the
* resulting list element. This property is necessary in order for
* procedures like Tcl_DStringStartSublist to work.
1996-09-27 06:29:02 -04:00
*/
nestingLevel = 0;
1999-09-05 07:16:41 -04:00
#ifdef SCM_CODE
1996-09-27 06:29:02 -04:00
flags = TCL_DONT_USE_BRACES;
#else
flags = 0;
#endif
if (string == NULL) {
string = "";
}
1998-04-10 06:59:06 -04:00
if (length == -1) {
length = strlen(string);
}
lastChar = string + length;
1996-09-27 06:29:02 -04:00
p = string;
1998-04-10 06:59:06 -04:00
if ((p == lastChar) || (*p == '{') || (*p == '"')) {
1996-09-27 06:29:02 -04:00
flags |= USE_BRACES;
}
1998-04-10 06:59:06 -04:00
for ( ; p != lastChar; p++) {
1996-09-27 06:29:02 -04:00
switch (*p) {
case '{':
nestingLevel++;
break;
case '}':
nestingLevel--;
if (nestingLevel < 0) {
flags |= TCL_DONT_USE_BRACES|BRACES_UNMATCHED;
}
break;
1999-09-05 07:16:41 -04:00
#ifndef SCM_CODE
1996-09-27 06:29:02 -04:00
case '[':
case '$':
case ';':
case ' ':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
flags |= USE_BRACES;
break;
#endif
case '\\':
1998-04-10 06:59:06 -04:00
if ((p+1 == lastChar) || (p[1] == '\n')) {
flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED;
1996-09-27 06:29:02 -04:00
} else {
int size;
(void) Tcl_Backslash(p, &size);
p += size-1;
flags |= USE_BRACES;
}
break;
}
}
if (nestingLevel != 0) {
flags = TCL_DONT_USE_BRACES | BRACES_UNMATCHED;
}
*flagPtr = flags;
/*
* Allow enough space to backslash every character plus leave
* two spaces for braces.
*/
return 2*(p-string) + 2;
}
/*
*----------------------------------------------------------------------
*
* Tcl_ConvertElement --
*
1998-04-10 06:59:06 -04:00
* This is a companion procedure to Tcl_ScanElement. Given
* the information produced by Tcl_ScanElement, this procedure
* converts a string to a list element equal to that string.
1996-09-27 06:29:02 -04:00
*
* Results:
* Information is copied to *dst in the form of a list element
* identical to src (i.e. if Tcl_SplitList is applied to dst it
* will produce a string identical to src). The return value is
* a count of the number of characters copied (not including the
* terminating NULL character).
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ConvertElement(src, dst, flags)
1998-04-10 06:59:06 -04:00
CONST char *src; /* Source information for list element. */
1996-09-27 06:29:02 -04:00
char *dst; /* Place to put list-ified element. */
int flags; /* Flags produced by Tcl_ScanElement. */
{
1998-04-10 06:59:06 -04:00
return Tcl_ConvertCountedElement(src, -1, dst, flags);
}
/*
*----------------------------------------------------------------------
*
* Tcl_ConvertCountedElement --
*
* This is a companion procedure to Tcl_ScanCountedElement. Given
* the information produced by Tcl_ScanCountedElement, this
* procedure converts a string to a list element equal to that
* string.
*
* Results:
* Information is copied to *dst in the form of a list element
* identical to src (i.e. if Tcl_SplitList is applied to dst it
* will produce a string identical to src). The return value is
* a count of the number of characters copied (not including the
* terminating NULL character).
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
Tcl_ConvertCountedElement(src, length, dst, flags)
CONST char *src; /* Source information for list element. */
int length; /* Number of bytes in src, or -1. */
char *dst; /* Place to put list-ified element. */
int flags; /* Flags produced by Tcl_ScanElement. */
{
char *p = dst;
1999-09-05 07:16:41 -04:00
#ifndef SCM_CODE
1998-04-10 06:59:06 -04:00
CONST char *lastChar;
1998-09-30 07:11:02 -04:00
#endif
1996-09-27 06:29:02 -04:00
/*
* See the comment block at the beginning of the Tcl_ScanElement
* code for details of how this works.
*/
1998-04-10 06:59:06 -04:00
if (src && length == -1) {
length = strlen(src);
}
1999-09-05 07:16:41 -04:00
#ifdef SCM_CODE
1996-09-27 06:29:02 -04:00
if ((src == NULL) || (*src == 0)) {
p[0] = '\\';
p[1] = '0';
p[2] = 0;
return 2;
}
1999-09-05 07:16:41 -04:00
while ((*p++ = *src++)) /* Nothing */;
1996-09-27 06:29:02 -04:00
return p - dst - 1;
#else
1998-04-10 06:59:06 -04:00
if ((src == NULL) || (length == 0)) {
1996-09-27 06:29:02 -04:00
p[0] = '{';
p[1] = '}';
p[2] = 0;
return 2;
}
1998-04-10 06:59:06 -04:00
lastChar = src + length;
1996-09-27 06:29:02 -04:00
if ((flags & USE_BRACES) && !(flags & TCL_DONT_USE_BRACES)) {
*p = '{';
p++;
1998-04-10 06:59:06 -04:00
for ( ; src != lastChar; src++, p++) {
1996-09-27 06:29:02 -04:00
*p = *src;
}
*p = '}';
p++;
} else {
if (*src == '{') {
/*
* Can't have a leading brace unless the whole element is
* enclosed in braces. Add a backslash before the brace.
* Furthermore, this may destroy the balance between open
* and close braces, so set BRACES_UNMATCHED.
*/
p[0] = '\\';
p[1] = '{';
p += 2;
src++;
flags |= BRACES_UNMATCHED;
}
1998-04-10 06:59:06 -04:00
for (; src != lastChar; src++) {
1996-09-27 06:29:02 -04:00
switch (*src) {
case ']':
case '[':
case '$':
case ';':
case ' ':
case '\\':
case '"':
*p = '\\';
p++;
break;
case '{':
case '}':
/*
* It may not seem necessary to backslash braces, but
* it is. The reason for this is that the resulting
* list element may actually be an element of a sub-list
* enclosed in braces (e.g. if Tcl_DStringStartSublist
* has been invoked), so there may be a brace mismatch
* if the braces aren't backslashed.
*/
if (flags & BRACES_UNMATCHED) {
*p = '\\';
p++;
}
break;
case '\f':
*p = '\\';
p++;
*p = 'f';
p++;
continue;
case '\n':
*p = '\\';
p++;
*p = 'n';
p++;
continue;
case '\r':
*p = '\\';
p++;
*p = 'r';
p++;
continue;
case '\t':
*p = '\\';
p++;
*p = 't';
p++;
continue;
case '\v':
*p = '\\';
p++;
*p = 'v';
p++;
continue;
}
*p = *src;
p++;
}
}
*p = '\0';
return p-dst;
#endif
}
/*
*----------------------------------------------------------------------
*
* Tcl_Merge --
*
* Given a collection of strings, merge them together into a
* single string that has proper Tcl list structured (i.e.
* Tcl_SplitList may be used to retrieve strings equal to the
* original elements, and Tcl_Eval will parse the string back
* into its original elements).
*
* Results:
* The return value is the address of a dynamically-allocated
* string containing the merged list.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
char *
Tcl_Merge(argc, argv)
int argc; /* How many strings to merge. */
char **argv; /* Array of string values. */
{
# define LOCAL_SIZE 20
int localFlags[LOCAL_SIZE], *flagPtr;
int numChars;
char *result;
1998-04-10 06:59:06 -04:00