1Tcl_SetResult(3) Tcl Library Procedures Tcl_SetResult(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_SetObjResult, Tcl_GetObjResult, Tcl_SetResult, Tcl_GetStringResult,
9 Tcl_AppendResult, Tcl_AppendResultVA, Tcl_AppendElement, Tcl_ResetRe‐
10 sult, Tcl_TransferResult, Tcl_FreeResult - manipulate Tcl result
11
13 #include <tcl.h>
14
15 Tcl_SetObjResult(interp, objPtr)
16
17 Tcl_Obj *
18 Tcl_GetObjResult(interp)
19
20 Tcl_SetResult(interp, result, freeProc)
21
22 const char *
23 Tcl_GetStringResult(interp)
24
25 Tcl_AppendResult(interp, result, result, ... , (char *) NULL)
26
27 Tcl_AppendResultVA(interp, argList)
28
29 Tcl_ResetResult(interp)
30
31 Tcl_TransferResult(sourceInterp, result, targetInterp) │
32
33 Tcl_AppendElement(interp, element)
34
35 Tcl_FreeResult(interp)
36
38 Tcl_Interp *interp (out) Interpreter whose result is to
39 be modified or read.
40
41 Tcl_Obj *objPtr (in) Tcl value to become result for
42 interp.
43
44 char *result (in) String value to become result
45 for interp or to be appended to
46 the existing result.
47
48 const char *element (in) String value to append as a
49 list element to the existing
50 result of interp.
51
52 Tcl_FreeProc *freeProc (in) Address of procedure to call to
53 release storage at result, or
54 TCL_STATIC, TCL_DYNAMIC, or
55 TCL_VOLATILE.
56
57 va_list argList (in) An argument list which must
58 have been initialized using
59 va_start, and cleared using
60 va_end.
61
62 Tcl_Interp *sourceInterp (in) Interpreter that the result and │
63 error information should be │
64 copied from.
65
66 Tcl_Interp *targetInterp (in) Interpreter that the result and │
67 error information should be │
68 copied to.
69
70 int result (in) If TCL_OK, only copy the │
71 result. If TCL_ERROR, copy the │
72 error information as well.
73______________________________________________________________________________
74
76 The procedures described here are utilities for manipulating the result
77 value in a Tcl interpreter. The interpreter result may be either a Tcl
78 value or a string. For example, Tcl_SetObjResult and Tcl_SetResult set
79 the interpreter result to, respectively, a value and a string. Simi‐
80 larly, Tcl_GetObjResult and Tcl_GetStringResult return the interpreter
81 result as a value and as a string. The procedures always keep the
82 string and value forms of the interpreter result consistent. For exam‐
83 ple, if Tcl_SetObjResult is called to set the result to a value, then
84 Tcl_GetStringResult is called, it will return the value's string repre‐
85 sentation.
86
87 Tcl_SetObjResult arranges for objPtr to be the result for interp,
88 replacing any existing result. The result is left pointing to the
89 value referenced by objPtr. objPtr's reference count is incremented
90 since there is now a new reference to it from interp. The reference
91 count for any old result value is decremented and the old result value
92 is freed if no references to it remain.
93
94 Tcl_GetObjResult returns the result for interp as a value. The value's
95 reference count is not incremented; if the caller needs to retain a
96 long-term pointer to the value they should use Tcl_IncrRefCount to
97 increment its reference count in order to keep it from being freed too
98 early or accidentally changed.
99
100 Tcl_SetResult arranges for result to be the result for the current Tcl
101 command in interp, replacing any existing result. The freeProc argu‐
102 ment specifies how to manage the storage for the result argument; it is
103 discussed in the section THE TCL_FREEPROC ARGUMENT TO TCL_SETRESULT
104 below. If result is NULL, then freeProc is ignored and Tcl_SetResult
105 re-initializes interp's result to point to an empty string.
106
107 Tcl_GetStringResult returns the result for interp as a string. If the
108 result was set to a value by a Tcl_SetObjResult call, the value form
109 will be converted to a string and returned. If the value's string rep‐
110 resentation contains null bytes, this conversion will lose information.
111 For this reason, programmers are encouraged to write their code to use
112 the new value API procedures and to call Tcl_GetObjResult instead.
113
114 Tcl_ResetResult clears the result for interp and leaves the result in
115 its normal empty initialized state. If the result is a value, its ref‐
116 erence count is decremented and the result is left pointing to an
117 unshared value representing an empty string. If the result is a dynam‐
118 ically allocated string, its memory is free*d and the result is left as
119 a empty string. Tcl_ResetResult also clears the error state managed by
120 Tcl_AddErrorInfo, Tcl_AddObjErrorInfo, and Tcl_SetErrorCode.
121
122 Tcl_AppendResult makes it easy to build up Tcl results in pieces. It
123 takes each of its result arguments and appends them in order to the
124 current result associated with interp. If the result is in its ini‐
125 tialized empty state (e.g. a command procedure was just invoked or
126 Tcl_ResetResult was just called), then Tcl_AppendResult sets the result
127 to the concatenation of its result arguments. Tcl_AppendResult may be
128 called repeatedly as additional pieces of the result are produced.
129 Tcl_AppendResult takes care of all the storage management issues asso‐
130 ciated with managing interp's result, such as allocating a larger
131 result area if necessary. It also manages conversion to and from the
132 result field of the interp so as to handle backward-compatibility with
133 old-style extensions. Any number of result arguments may be passed in
134 a single call; the last argument in the list must be a NULL pointer.
135
136 Tcl_AppendResultVA is the same as Tcl_AppendResult except that instead
137 of taking a variable number of arguments it takes an argument list.
138
139 Tcl_TransferResult moves a result from one interpreter to another, │
140 optionally (dependent on the result parameter) including the error │
141 information dictionary as well. The interpreters must be in the same │
142 thread. The source interpreter will have its result reset by this │
143 operation.
144
146 OLD STRING PROCEDURES
147 Use of the following procedures is deprecated since they manipulate the
148 Tcl result as a string. Procedures such as Tcl_SetObjResult that
149 manipulate the result as a value can be significantly more efficient.
150
151 Tcl_AppendElement is similar to Tcl_AppendResult in that it allows
152 results to be built up in pieces. However, Tcl_AppendElement takes
153 only a single element argument and it appends that argument to the cur‐
154 rent result as a proper Tcl list element. Tcl_AppendElement adds back‐
155 slashes or braces if necessary to ensure that interp's result can be
156 parsed as a list and that element will be extracted as a single ele‐
157 ment. Under normal conditions, Tcl_AppendElement will add a space
158 character to interp's result just before adding the new list element,
159 so that the list elements in the result are properly separated. How‐
160 ever if the new list element is the first in a list or sub-list (i.e.
161 interp's current result is empty, or consists of the single character
162 “{”, or ends in the characters “ {”) then no space is added.
163
164 Tcl_FreeResult performs part of the work of Tcl_ResetResult. It frees
165 up the memory associated with interp's result. It also sets
166 interp->freeProc to zero, but does not change interp->result or clear
167 error state. Tcl_FreeResult is most commonly used when a procedure is
168 about to replace one result value with another.
169
170 DIRECT ACCESS TO INTERP->RESULT
171 It used to be legal for programs to directly read and write
172 interp->result to manipulate the interpreter result. The Tcl headers
173 no longer permit this access by default, and C code still doing this
174 must be updated to use supported routines Tcl_GetObjResult, Tcl_Get‐
175 StringResult, Tcl_SetObjResult, and Tcl_SetResult. As a migration aid,
176 access can be restored with the compiler directive
177 #define USE_INTERP_RESULT
178 but this is meant only to offer life support to otherwise dead code.
179
181 Tcl_SetResult's freeProc argument specifies how the Tcl system is to
182 manage the storage for the result argument. If Tcl_SetResult or
183 Tcl_SetObjResult are called at a time when interp holds a string
184 result, they do whatever is necessary to dispose of the old string
185 result (see the Tcl_Interp manual entry for details on this).
186
187 If freeProc is TCL_STATIC it means that result refers to an area of
188 static storage that is guaranteed not to be modified until at least the
189 next call to Tcl_Eval. If freeProc is TCL_DYNAMIC it means that result
190 was allocated with a call to Tcl_Alloc and is now the property of the
191 Tcl system. Tcl_SetResult will arrange for the string's storage to be
192 released by calling Tcl_Free when it is no longer needed. If freeProc
193 is TCL_VOLATILE it means that result points to an area of memory that
194 is likely to be overwritten when Tcl_SetResult returns (e.g. it points
195 to something in a stack frame). In this case Tcl_SetResult will make a
196 copy of the string in dynamically allocated storage and arrange for the
197 copy to be the result for the current Tcl command.
198
199 If freeProc is not one of the values TCL_STATIC, TCL_DYNAMIC, and
200 TCL_VOLATILE, then it is the address of a procedure that Tcl should
201 call to free the string. This allows applications to use non-standard
202 storage allocators. When Tcl no longer needs the storage for the
203 string, it will call freeProc. FreeProc should have arguments and
204 result that match the type Tcl_FreeProc:
205
206 typedef void Tcl_FreeProc(
207 char *blockPtr);
208
209 When freeProc is called, its blockPtr will be set to the value of
210 result passed to Tcl_SetResult.
211
213 Tcl_AddErrorInfo, Tcl_CreateObjCommand, Tcl_SetErrorCode, Tcl_Interp
214
216 append, command, element, list, value, result, return value, inter‐
217 preter
218
219
220
221Tcl 8.0 Tcl_SetResult(3)