Debian Codegraph @

doc/SplitList.3

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