| 1 |
'\" |
| 2 |
'\" Copyright (c) 1989-1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 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_SetResult 3 8.6 Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
Tcl_SetObjResult, Tcl_GetObjResult, Tcl_SetResult, Tcl_GetStringResult, Tcl_AppendResult, Tcl_AppendResultVA, Tcl_AppendElement, Tcl_ResetResult, Tcl_TransferResult, Tcl_FreeResult \- manipulate Tcl result |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
\fBTcl_SetObjResult\fR(\fIinterp, objPtr\fR) |
| 18 |
.sp |
| 19 |
Tcl_Obj * |
| 20 |
\fBTcl_GetObjResult\fR(\fIinterp\fR) |
| 21 |
.sp |
| 22 |
\fBTcl_SetResult\fR(\fIinterp, result, freeProc\fR) |
| 23 |
.sp |
| 24 |
const char * |
| 25 |
\fBTcl_GetStringResult\fR(\fIinterp\fR) |
| 26 |
.sp |
| 27 |
\fBTcl_AppendResult\fR(\fIinterp, result, result, ... , \fB(char *)NULL\fR) |
| 28 |
.sp |
| 29 |
\fBTcl_AppendResultVA\fR(\fIinterp, argList\fR) |
| 30 |
.sp |
| 31 |
\fBTcl_ResetResult\fR(\fIinterp\fR) |
| 32 |
.sp |
| 33 |
.VS 8.6 |
| 34 |
\fBTcl_TransferResult\fR(\fIsourceInterp, code, targetInterp\fR) |
| 35 |
.VE 8.6 |
| 36 |
.sp |
| 37 |
\fBTcl_AppendElement\fR(\fIinterp, element\fR) |
| 38 |
.sp |
| 39 |
\fBTcl_FreeResult\fR(\fIinterp\fR) |
| 40 |
.fi |
| 41 |
.SH ARGUMENTS |
| 42 |
.AS Tcl_FreeProc sourceInterp out |
| 43 |
.AP Tcl_Interp *interp out |
| 44 |
Interpreter whose result is to be modified or read. |
| 45 |
.AP Tcl_Obj *objPtr in |
| 46 |
Tcl value to become result for \fIinterp\fR. |
| 47 |
.AP char *result in |
| 48 |
String value to become result for \fIinterp\fR or to be |
| 49 |
appended to the existing result. |
| 50 |
.AP "const char" *element in |
| 51 |
String value to append as a list element |
| 52 |
to the existing result of \fIinterp\fR. |
| 53 |
.AP Tcl_FreeProc *freeProc in |
| 54 |
Address of procedure to call to release storage at |
| 55 |
\fIresult\fR, or \fBTCL_STATIC\fR, \fBTCL_DYNAMIC\fR, or |
| 56 |
\fBTCL_VOLATILE\fR. |
| 57 |
.AP va_list argList in |
| 58 |
An argument list which must have been initialized using |
| 59 |
\fBva_start\fR, and cleared using \fBva_end\fR. |
| 60 |
.AP Tcl_Interp *sourceInterp in |
| 61 |
.VS 8.6 |
| 62 |
Interpreter that the result and return options should be transferred from. |
| 63 |
.VE 8.6 |
| 64 |
.AP Tcl_Interp *targetInterp in |
| 65 |
.VS 8.6 |
| 66 |
Interpreter that the result and return options should be transferred to. |
| 67 |
.VE 8.6 |
| 68 |
.AP int code in |
| 69 |
.VS 8.6 |
| 70 |
Return code value that controls transfer of return options. |
| 71 |
.VE 8.6 |
| 72 |
.BE |
| 73 |
.SH DESCRIPTION |
| 74 |
.PP |
| 75 |
The procedures described here are utilities for manipulating the |
| 76 |
result value in a Tcl interpreter. |
| 77 |
The interpreter result may be either a Tcl value or a string. |
| 78 |
For example, \fBTcl_SetObjResult\fR and \fBTcl_SetResult\fR |
| 79 |
set the interpreter result to, respectively, a value and a string. |
| 80 |
Similarly, \fBTcl_GetObjResult\fR and \fBTcl_GetStringResult\fR |
| 81 |
return the interpreter result as a value and as a string. |
| 82 |
The procedures always keep the string and value forms |
| 83 |
of the interpreter result consistent. |
| 84 |
For example, if \fBTcl_SetObjResult\fR is called to set |
| 85 |
the result to a value, |
| 86 |
then \fBTcl_GetStringResult\fR is called, |
| 87 |
it will return the value's string representation. |
| 88 |
.PP |
| 89 |
\fBTcl_SetObjResult\fR |
| 90 |
arranges for \fIobjPtr\fR to be the result for \fIinterp\fR, |
| 91 |
replacing any existing result. |
| 92 |
The result is left pointing to the value |
| 93 |
referenced by \fIobjPtr\fR. |
| 94 |
\fIobjPtr\fR's reference count is incremented |
| 95 |
since there is now a new reference to it from \fIinterp\fR. |
| 96 |
The reference count for any old result value |
| 97 |
is decremented and the old result value is freed if no |
| 98 |
references to it remain. |
| 99 |
.PP |
| 100 |
\fBTcl_GetObjResult\fR returns the result for \fIinterp\fR as a value. |
| 101 |
The value's reference count is not incremented; |
| 102 |
if the caller needs to retain a long-term pointer to the value |
| 103 |
they should use \fBTcl_IncrRefCount\fR to increment its reference count |
| 104 |
in order to keep it from being freed too early or accidentally changed. |
| 105 |
.PP |
| 106 |
\fBTcl_SetResult\fR |
| 107 |
arranges for \fIresult\fR to be the result for the current Tcl |
| 108 |
command in \fIinterp\fR, replacing any existing result. |
| 109 |
The \fIfreeProc\fR argument specifies how to manage the storage |
| 110 |
for the \fIresult\fR argument; |
| 111 |
it is discussed in the section |
| 112 |
\fBTHE TCL_FREEPROC ARGUMENT TO TCL_SETRESULT\fR below. |
| 113 |
If \fIresult\fR is \fBNULL\fR, then \fIfreeProc\fR is ignored |
| 114 |
and \fBTcl_SetResult\fR |
| 115 |
re-initializes \fIinterp\fR's result to point to an empty string. |
| 116 |
.PP |
| 117 |
\fBTcl_GetStringResult\fR returns the result for \fIinterp\fR as a string. |
| 118 |
If the result was set to a value by a \fBTcl_SetObjResult\fR call, |
| 119 |
the value form will be converted to a string and returned. |
| 120 |
If the value's string representation contains null bytes, |
| 121 |
this conversion will lose information. |
| 122 |
For this reason, programmers are encouraged to |
| 123 |
write their code to use the new value API procedures |
| 124 |
and to call \fBTcl_GetObjResult\fR instead. |
| 125 |
.PP |
| 126 |
\fBTcl_ResetResult\fR clears the result for \fIinterp\fR |
| 127 |
and leaves the result in its normal empty initialized state. |
| 128 |
If the result is a value, |
| 129 |
its reference count is decremented and the result is left |
| 130 |
pointing to an unshared value representing an empty string. |
| 131 |
If the result is a dynamically allocated string, its memory is free*d |
| 132 |
and the result is left as a empty string. |
| 133 |
\fBTcl_ResetResult\fR also clears the error state managed by |
| 134 |
\fBTcl_AddErrorInfo\fR, \fBTcl_AddObjErrorInfo\fR, |
| 135 |
and \fBTcl_SetErrorCode\fR. |
| 136 |
.PP |
| 137 |
\fBTcl_AppendResult\fR makes it easy to build up Tcl results in pieces. |
| 138 |
It takes each of its \fIresult\fR arguments and appends them in order |
| 139 |
to the current result associated with \fIinterp\fR. |
| 140 |
If the result is in its initialized empty state (e.g. a command procedure |
| 141 |
was just invoked or \fBTcl_ResetResult\fR was just called), |
| 142 |
then \fBTcl_AppendResult\fR sets the result to the concatenation of |
| 143 |
its \fIresult\fR arguments. |
| 144 |
\fBTcl_AppendResult\fR may be called repeatedly as additional pieces |
| 145 |
of the result are produced. |
| 146 |
\fBTcl_AppendResult\fR takes care of all the |
| 147 |
storage management issues associated with managing \fIinterp\fR's |
| 148 |
result, such as allocating a larger result area if necessary. |
| 149 |
It also manages conversion to and from the \fIresult\fR field of the |
| 150 |
\fIinterp\fR so as to handle backward-compatibility with old-style |
| 151 |
extensions. |
| 152 |
Any number of \fIresult\fR arguments may be passed in a single |
| 153 |
call; the last argument in the list must be (char *)NULL. |
| 154 |
.PP |
| 155 |
\fBTcl_AppendResultVA\fR is the same as \fBTcl_AppendResult\fR except that |
| 156 |
instead of taking a variable number of arguments it takes an argument list. |
| 157 |
.PP |
| 158 |
.VS 8.6 |
| 159 |
\fBTcl_TransferResult\fR transfers interpreter state from \fIsourceInterp\fR |
| 160 |
to \fItargetInterp\fR. The two interpreters must have been created in the |
| 161 |
same thread. If \fIsourceInterp\fR and \fItargetInterp\fR are the same, |
| 162 |
nothing is done. Otherwise, \fBTcl_TransferResult\fR moves the result |
| 163 |
from \fIsourceInterp\fR to \fItargetInterp\fR, and resets the result |
| 164 |
in \fIsourceInterp\fR. It also moves the return options dictionary as |
| 165 |
controlled by the return code value \fIcode\fR in the same manner |
| 166 |
as \fBTcl_GetReturnOptions\fR. |
| 167 |
.VE 8.6 |
| 168 |
.SH "DEPRECATED INTERFACES" |
| 169 |
.SS "OLD STRING PROCEDURES" |
| 170 |
.PP |
| 171 |
Use of the following procedures is deprecated |
| 172 |
since they manipulate the Tcl result as a string. |
| 173 |
Procedures such as \fBTcl_SetObjResult\fR |
| 174 |
that manipulate the result as a value |
| 175 |
can be significantly more efficient. |
| 176 |
.PP |
| 177 |
\fBTcl_AppendElement\fR is similar to \fBTcl_AppendResult\fR in |
| 178 |
that it allows results to be built up in pieces. |
| 179 |
However, \fBTcl_AppendElement\fR takes only a single \fIelement\fR |
| 180 |
argument and it appends that argument to the current result |
| 181 |
as a proper Tcl list element. |
| 182 |
\fBTcl_AppendElement\fR adds backslashes or braces if necessary |
| 183 |
to ensure that \fIinterp\fR's result can be parsed as a list and that |
| 184 |
\fIelement\fR will be extracted as a single element. |
| 185 |
Under normal conditions, \fBTcl_AppendElement\fR will add a space |
| 186 |
character to \fIinterp\fR's result just before adding the new |
| 187 |
list element, so that the list elements in the result are properly |
| 188 |
separated. |
| 189 |
However if the new list element is the first in a list or sub-list |
| 190 |
(i.e. \fIinterp\fR's current result is empty, or consists of the |
| 191 |
single character |
| 192 |
.QW { , |
| 193 |
or ends in the characters |
| 194 |
.QW " {" ) |
| 195 |
then no space is added. |
| 196 |
.PP |
| 197 |
\fBTcl_FreeResult\fR performs part of the work |
| 198 |
of \fBTcl_ResetResult\fR. |
| 199 |
It frees up the memory associated with \fIinterp\fR's result. |
| 200 |
It also sets \fIinterp->freeProc\fR to zero, but does not |
| 201 |
change \fIinterp->result\fR or clear error state. |
| 202 |
\fBTcl_FreeResult\fR is most commonly used when a procedure |
| 203 |
is about to replace one result value with another. |
| 204 |
.SS "DIRECT ACCESS TO INTERP->RESULT" |
| 205 |
.PP |
| 206 |
It used to be legal for programs to |
| 207 |
directly read and write \fIinterp->result\fR |
| 208 |
to manipulate the interpreter result. The Tcl headers no longer |
| 209 |
permit this access by default, and C code still doing this must |
| 210 |
be updated to use supported routines \fBTcl_GetObjResult\fR, |
| 211 |
\fBTcl_GetStringResult\fR, \fBTcl_SetObjResult\fR, and \fBTcl_SetResult\fR. |
| 212 |
As a migration aid, access can be restored with the compiler directive |
| 213 |
.CS |
| 214 |
#define USE_INTERP_RESULT |
| 215 |
.CE |
| 216 |
but this is meant only to offer life support to otherwise dead code. |
| 217 |
.SH "THE TCL_FREEPROC ARGUMENT TO TCL_SETRESULT" |
| 218 |
.PP |
| 219 |
\fBTcl_SetResult\fR's \fIfreeProc\fR argument specifies how |
| 220 |
the Tcl system is to manage the storage for the \fIresult\fR argument. |
| 221 |
If \fBTcl_SetResult\fR or \fBTcl_SetObjResult\fR are called |
| 222 |
at a time when \fIinterp\fR holds a string result, |
| 223 |
they do whatever is necessary to dispose of the old string result |
| 224 |
(see the \fBTcl_Interp\fR manual entry for details on this). |
| 225 |
.PP |
| 226 |
If \fIfreeProc\fR is \fBTCL_STATIC\fR it means that \fIresult\fR |
| 227 |
refers to an area of static storage that is guaranteed not to be |
| 228 |
modified until at least the next call to \fBTcl_Eval\fR. |
| 229 |
If \fIfreeProc\fR |
| 230 |
is \fBTCL_DYNAMIC\fR it means that \fIresult\fR was allocated with a call |
| 231 |
to \fBTcl_Alloc\fR and is now the property of the Tcl system. |
| 232 |
\fBTcl_SetResult\fR will arrange for the string's storage to be |
| 233 |
released by calling \fBTcl_Free\fR when it is no longer needed. |
| 234 |
If \fIfreeProc\fR is \fBTCL_VOLATILE\fR it means that \fIresult\fR |
| 235 |
points to an area of memory that is likely to be overwritten when |
| 236 |
\fBTcl_SetResult\fR returns (e.g. it points to something in a stack frame). |
| 237 |
In this case \fBTcl_SetResult\fR will make a copy of the string in |
| 238 |
dynamically allocated storage and arrange for the copy to be the |
| 239 |
result for the current Tcl command. |
| 240 |
.PP |
| 241 |
If \fIfreeProc\fR is not one of the values \fBTCL_STATIC\fR, |
| 242 |
\fBTCL_DYNAMIC\fR, and \fBTCL_VOLATILE\fR, then it is the address |
| 243 |
of a procedure that Tcl should call to free the string. |
| 244 |
This allows applications to use non-standard storage allocators. |
| 245 |
When Tcl no longer needs the storage for the string, it will |
| 246 |
call \fIfreeProc\fR. \fIFreeProc\fR should have arguments and |
| 247 |
result that match the type \fBTcl_FreeProc\fR: |
| 248 |
.PP |
| 249 |
.CS |
| 250 |
typedef void \fBTcl_FreeProc\fR( |
| 251 |
char *\fIblockPtr\fR); |
| 252 |
.CE |
| 253 |
.PP |
| 254 |
When \fIfreeProc\fR is called, its \fIblockPtr\fR will be set to |
| 255 |
the value of \fIresult\fR passed to \fBTcl_SetResult\fR. |
| 256 |
.SH "SEE ALSO" |
| 257 |
Tcl_AddErrorInfo, Tcl_CreateObjCommand, Tcl_SetErrorCode, Tcl_Interp, |
| 258 |
Tcl_GetReturnOptions |
| 259 |
.SH KEYWORDS |
| 260 |
append, command, element, list, value, result, return value, interpreter |