1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1989-1993 The Regents of the University of California.
|
3 |
|
|
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
|
4 |
|
|
'\"
|
5 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
6 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
7 |
|
|
'\"
|
8 |
|
|
'\" RCS: @(#) $Id: SplitList.3,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH Tcl_SplitList 3 7.5 Tcl "Tcl Library Procedures"
|
12 |
|
|
.BS
|
13 |
|
|
.SH NAME
|
14 |
|
|
Tcl_SplitList, Tcl_Merge, Tcl_ScanElement, Tcl_ConvertElement \- manipulate Tcl lists
|
15 |
|
|
.SH SYNOPSIS
|
16 |
|
|
.nf
|
17 |
|
|
\fB#include \fR
|
18 |
|
|
.sp
|
19 |
|
|
int
|
20 |
|
|
\fBTcl_SplitList\fR(\fIinterp, list, argcPtr, argvPtr\fR)
|
21 |
|
|
.sp
|
22 |
|
|
char *
|
23 |
|
|
\fBTcl_Merge\fR(\fIargc, argv\fR)
|
24 |
|
|
.sp
|
25 |
|
|
int
|
26 |
|
|
\fBTcl_ScanElement\fR(\fIsrc, flagsPtr\fR)
|
27 |
|
|
.VS
|
28 |
|
|
.sp
|
29 |
|
|
int
|
30 |
|
|
\fBTcl_ScanCountedElement\fR(\fIsrc, length, flagsPtr\fR)
|
31 |
|
|
.VE
|
32 |
|
|
.sp
|
33 |
|
|
int
|
34 |
|
|
\fBTcl_ConvertElement\fR(\fIsrc, dst, flags\fR)
|
35 |
|
|
.VS
|
36 |
|
|
.sp
|
37 |
|
|
int
|
38 |
|
|
\fBTcl_ConvertCountedElement\fR(\fIsrc, length, dst, flags\fR)
|
39 |
|
|
.VE
|
40 |
|
|
.SH ARGUMENTS
|
41 |
|
|
.AS Tcl_Interp ***argvPtr
|
42 |
|
|
.AP Tcl_Interp *interp out
|
43 |
|
|
.VS
|
44 |
|
|
Interpreter to use for error reporting. If NULL, then no error message
|
45 |
|
|
is left.
|
46 |
|
|
.VE
|
47 |
|
|
.AP char *list in
|
48 |
|
|
Pointer to a string with proper list structure.
|
49 |
|
|
.AP int *argcPtr out
|
50 |
|
|
Filled in with number of elements in \fIlist\fR.
|
51 |
|
|
.AP char ***argvPtr out
|
52 |
|
|
\fI*argvPtr\fR will be filled in with the address of an array of
|
53 |
|
|
pointers to the strings that are the extracted elements of \fIlist\fR.
|
54 |
|
|
There will be \fI*argcPtr\fR valid entries in the array, followed by
|
55 |
|
|
a NULL entry.
|
56 |
|
|
.AP int argc in
|
57 |
|
|
Number of elements in \fIargv\fR.
|
58 |
|
|
.AP char **argv in
|
59 |
|
|
Array of strings to merge together into a single list.
|
60 |
|
|
Each string will become a separate element of the list.
|
61 |
|
|
.AP char *src in
|
62 |
|
|
String that is to become an element of a list.
|
63 |
|
|
.AP int *flagsPtr in
|
64 |
|
|
Pointer to word to fill in with information about \fIsrc\fR.
|
65 |
|
|
The value of *\fIflagsPtr\fR must be passed to \fBTcl_ConvertElement\fR.
|
66 |
|
|
.VS
|
67 |
|
|
.AP int length in
|
68 |
|
|
Number of bytes in string \fIsrc\fR.
|
69 |
|
|
.VE
|
70 |
|
|
.AP char *dst in
|
71 |
|
|
Place to copy converted list element. Must contain enough characters
|
72 |
|
|
to hold converted string.
|
73 |
|
|
.AP int flags in
|
74 |
|
|
Information about \fIsrc\fR. Must be value returned by previous
|
75 |
|
|
call to \fBTcl_ScanElement\fR, possibly OR-ed
|
76 |
|
|
with \fBTCL_DONT_USE_BRACES\fR.
|
77 |
|
|
.BE
|
78 |
|
|
|
79 |
|
|
.SH DESCRIPTION
|
80 |
|
|
.PP
|
81 |
|
|
These procedures may be used to disassemble and reassemble Tcl lists.
|
82 |
|
|
\fBTcl_SplitList\fR breaks a list up into its constituent elements,
|
83 |
|
|
returning an array of pointers to the elements using
|
84 |
|
|
\fIargcPtr\fR and \fIargvPtr\fR.
|
85 |
|
|
While extracting the arguments, \fBTcl_SplitList\fR obeys the usual
|
86 |
|
|
rules for backslash substitutions and braces. The area of
|
87 |
|
|
memory pointed to by \fI*argvPtr\fR is dynamically allocated; in
|
88 |
|
|
addition to the array of pointers, it
|
89 |
|
|
also holds copies of all the list elements. It is the caller's
|
90 |
|
|
responsibility to free up all of this storage.
|
91 |
|
|
For example, suppose that you have called \fBTcl_SplitList\fR with
|
92 |
|
|
the following code:
|
93 |
|
|
.CS
|
94 |
|
|
int argc, code;
|
95 |
|
|
char *string;
|
96 |
|
|
char **argv;
|
97 |
|
|
\&...
|
98 |
|
|
code = Tcl_SplitList(interp, string, &argc, &argv);
|
99 |
|
|
.CE
|
100 |
|
|
Then you should eventually free the storage with a call like the
|
101 |
|
|
following:
|
102 |
|
|
.VS
|
103 |
|
|
.CS
|
104 |
|
|
Tcl_Free((char *) argv);
|
105 |
|
|
.CE
|
106 |
|
|
.VE
|
107 |
|
|
.PP
|
108 |
|
|
\fBTcl_SplitList\fR normally returns \fBTCL_OK\fR, which means the list was
|
109 |
|
|
successfully parsed.
|
110 |
|
|
If there was a syntax error in \fIlist\fR, then \fBTCL_ERROR\fR is returned
|
111 |
|
|
and \fIinterp->result\fR will point to an error message describing the
|
112 |
|
|
.VS
|
113 |
|
|
problem (if \fIinterp\fR was not NULL).
|
114 |
|
|
.VE
|
115 |
|
|
If \fBTCL_ERROR\fR is returned then no memory is allocated and \fI*argvPtr\fR
|
116 |
|
|
is not modified.
|
117 |
|
|
.PP
|
118 |
|
|
\fBTcl_Merge\fR is the inverse of \fBTcl_SplitList\fR: it
|
119 |
|
|
takes a collection of strings given by \fIargc\fR
|
120 |
|
|
and \fIargv\fR and generates a result string
|
121 |
|
|
that has proper list structure.
|
122 |
|
|
This means that commands like \fBindex\fR may be used to
|
123 |
|
|
extract the original elements again.
|
124 |
|
|
In addition, if the result of \fBTcl_Merge\fR is passed to \fBTcl_Eval\fR,
|
125 |
|
|
it will be parsed into \fIargc\fR words whose values will
|
126 |
|
|
be the same as the \fIargv\fR strings passed to \fBTcl_Merge\fR.
|
127 |
|
|
\fBTcl_Merge\fR will modify the list elements with braces and/or
|
128 |
|
|
backslashes in order to produce proper Tcl list structure.
|
129 |
|
|
.VS
|
130 |
|
|
The result string is dynamically allocated
|
131 |
|
|
using \fBTcl_Alloc\fR; the caller must eventually release the space
|
132 |
|
|
using \fBTcl_Free\fR.
|
133 |
|
|
.VE
|
134 |
|
|
.PP
|
135 |
|
|
If the result of \fBTcl_Merge\fR is passed to \fBTcl_SplitList\fR,
|
136 |
|
|
the elements returned by \fBTcl_SplitList\fR will be identical to
|
137 |
|
|
those passed into \fBTcl_Merge\fR.
|
138 |
|
|
However, the converse is not true: if \fBTcl_SplitList\fR
|
139 |
|
|
is passed a given string, and the resulting \fIargc\fR and
|
140 |
|
|
\fIargv\fR are passed to \fBTcl_Merge\fR, the resulting string
|
141 |
|
|
may not be the same as the original string passed to \fBTcl_SplitList\fR.
|
142 |
|
|
This is because \fBTcl_Merge\fR may use backslashes and braces
|
143 |
|
|
differently than the original string.
|
144 |
|
|
.PP
|
145 |
|
|
\fBTcl_ScanElement\fR and \fBTcl_ConvertElement\fR are the
|
146 |
|
|
procedures that do all of the real work of \fBTcl_Merge\fR.
|
147 |
|
|
\fBTcl_ScanElement\fR scans its \fIsrc\fR argument
|
148 |
|
|
and determines how to use backslashes and braces
|
149 |
|
|
when converting it to a list element.
|
150 |
|
|
It returns an overestimate of the number of characters
|
151 |
|
|
required to represent \fIsrc\fR as a list element, and
|
152 |
|
|
it stores information in \fI*flagsPtr\fR that is needed
|
153 |
|
|
by \fBTcl_ConvertElement\fR.
|
154 |
|
|
.PP
|
155 |
|
|
\fBTcl_ConvertElement\fR is a companion procedure to \fBTcl_ScanElement\fR.
|
156 |
|
|
It does the actual work of converting a string to a list element.
|
157 |
|
|
Its \fIflags\fR argument must be the same as the value returned
|
158 |
|
|
by \fBTcl_ScanElement\fR.
|
159 |
|
|
\fBTcl_ConvertElement\fR writes a proper list element to memory
|
160 |
|
|
starting at *\fIdst\fR and returns a count of the total number
|
161 |
|
|
of characters written, which will be no more than the result
|
162 |
|
|
returned by \fBTcl_ScanElement\fR.
|
163 |
|
|
\fBTcl_ConvertElement\fR writes out only the actual list element
|
164 |
|
|
without any leading or trailing spaces: it is up to the caller to
|
165 |
|
|
include spaces between adjacent list elements.
|
166 |
|
|
.PP
|
167 |
|
|
\fBTcl_ConvertElement\fR uses one of two different approaches to
|
168 |
|
|
handle the special characters in \fIsrc\fR. Wherever possible, it
|
169 |
|
|
handles special characters by surrounding the string with braces.
|
170 |
|
|
This produces clean-looking output, but can't be used in some situations,
|
171 |
|
|
such as when \fIsrc\fR contains unmatched braces.
|
172 |
|
|
In these situations, \fBTcl_ConvertElement\fR handles special
|
173 |
|
|
characters by generating backslash sequences for them.
|
174 |
|
|
The caller may insist on the second approach by OR-ing the
|
175 |
|
|
flag value returned by \fBTcl_ScanElement\fR with
|
176 |
|
|
\fBTCL_DONT_USE_BRACES\fR.
|
177 |
|
|
Although this will produce an uglier result, it is useful in some
|
178 |
|
|
special situations, such as when \fBTcl_ConvertElement\fR is being
|
179 |
|
|
used to generate a portion of an argument for a Tcl command.
|
180 |
|
|
In this case, surrounding \fIsrc\fR with curly braces would cause
|
181 |
|
|
the command not to be parsed correctly.
|
182 |
|
|
.PP
|
183 |
|
|
.VS
|
184 |
|
|
\fBTcl_ScanCountedElement\fR and \fBTcl_ConvertCountedElement\fR are
|
185 |
|
|
the same as \fBTcl_ScanElement\fR and \fBTcl_ConvertElement\fR, except
|
186 |
|
|
the length of string \fIsrc\fR is specified by the \fIlength\fR
|
187 |
|
|
argument, and the string may contain embedded nulls.
|
188 |
|
|
.VE
|
189 |
|
|
|
190 |
|
|
.SH KEYWORDS
|
191 |
|
|
backslash, convert, element, list, merge, split, strings
|