| 1 |
'\" |
| 2 |
'\" Copyright (c) 1989-1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 Sun Microsystems, Inc. |
| 4 |
'\" Copyright (c) 2000 Scriptics Corporation. |
| 5 |
'\" |
| 6 |
'\" See the file "license.terms" for information on usage and redistribution |
| 7 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 8 |
'\" |
| 9 |
.TH Tcl_Eval 3 8.1 Tcl "Tcl Library Procedures" |
| 10 |
.so man.macros |
| 11 |
.BS |
| 12 |
.SH NAME |
| 13 |
Tcl_EvalObjEx, Tcl_EvalFile, Tcl_EvalObjv, Tcl_Eval, Tcl_EvalEx, Tcl_GlobalEval, Tcl_GlobalEvalObj, Tcl_VarEval, Tcl_VarEvalVA \- execute Tcl scripts |
| 14 |
.SH SYNOPSIS |
| 15 |
.nf |
| 16 |
\fB#include <tcl.h>\fR |
| 17 |
.sp |
| 18 |
int |
| 19 |
\fBTcl_EvalObjEx\fR(\fIinterp, objPtr, flags\fR) |
| 20 |
.sp |
| 21 |
int |
| 22 |
\fBTcl_EvalFile\fR(\fIinterp, fileName\fR) |
| 23 |
.sp |
| 24 |
int |
| 25 |
\fBTcl_EvalObjv\fR(\fIinterp, objc, objv, flags\fR) |
| 26 |
.sp |
| 27 |
int |
| 28 |
\fBTcl_Eval\fR(\fIinterp, script\fR) |
| 29 |
.sp |
| 30 |
int |
| 31 |
\fBTcl_EvalEx\fR(\fIinterp, script, numBytes, flags\fR) |
| 32 |
.sp |
| 33 |
int |
| 34 |
\fBTcl_GlobalEval\fR(\fIinterp, script\fR) |
| 35 |
.sp |
| 36 |
int |
| 37 |
\fBTcl_GlobalEvalObj\fR(\fIinterp, objPtr\fR) |
| 38 |
.sp |
| 39 |
int |
| 40 |
\fBTcl_VarEval\fR(\fIinterp, part, part, ... \fB(char *)NULL\fR) |
| 41 |
.sp |
| 42 |
int |
| 43 |
\fBTcl_VarEvalVA\fR(\fIinterp, argList\fR) |
| 44 |
.SH ARGUMENTS |
| 45 |
.AS Tcl_Interp **termPtr |
| 46 |
.AP Tcl_Interp *interp in |
| 47 |
Interpreter in which to execute the script. The interpreter's result is |
| 48 |
modified to hold the result or error message from the script. |
| 49 |
.AP Tcl_Obj *objPtr in |
| 50 |
A Tcl value containing the script to execute. |
| 51 |
.AP int flags in |
| 52 |
OR'ed combination of flag bits that specify additional options. |
| 53 |
\fBTCL_EVAL_GLOBAL\fR and \fBTCL_EVAL_DIRECT\fR are currently supported. |
| 54 |
.AP "const char" *fileName in |
| 55 |
Name of a file containing a Tcl script. |
| 56 |
.AP int objc in |
| 57 |
The number of values in the array pointed to by \fIobjv\fR; |
| 58 |
this is also the number of words in the command. |
| 59 |
.AP Tcl_Obj **objv in |
| 60 |
Points to an array of pointers to values; each value holds the |
| 61 |
value of a single word in the command to execute. |
| 62 |
.AP int numBytes in |
| 63 |
The number of bytes in \fIscript\fR, not including any |
| 64 |
null terminating character. If \-1, then all characters up to the |
| 65 |
first null byte are used. |
| 66 |
.AP "const char" *script in |
| 67 |
Points to first byte of script to execute (null-terminated and UTF-8). |
| 68 |
.AP "const char" *part in |
| 69 |
String forming part of a Tcl script. |
| 70 |
.AP va_list argList in |
| 71 |
An argument list which must have been initialized using |
| 72 |
\fBva_start\fR, and cleared using \fBva_end\fR. |
| 73 |
.BE |
| 74 |
|
| 75 |
.SH DESCRIPTION |
| 76 |
.PP |
| 77 |
The procedures described here are invoked to execute Tcl scripts in |
| 78 |
various forms. |
| 79 |
\fBTcl_EvalObjEx\fR is the core procedure and is used by many of the others. |
| 80 |
It executes the commands in the script stored in \fIobjPtr\fR |
| 81 |
until either an error occurs or the end of the script is reached. |
| 82 |
If this is the first time \fIobjPtr\fR has been executed, |
| 83 |
its commands are compiled into bytecode instructions |
| 84 |
which are then executed. The |
| 85 |
bytecodes are saved in \fIobjPtr\fR so that the compilation step |
| 86 |
can be skipped if the value is evaluated again in the future. |
| 87 |
.PP |
| 88 |
The return value from \fBTcl_EvalObjEx\fR (and all the other procedures |
| 89 |
described here) is a Tcl completion code with |
| 90 |
one of the values \fBTCL_OK\fR, \fBTCL_ERROR\fR, \fBTCL_RETURN\fR, |
| 91 |
\fBTCL_BREAK\fR, or \fBTCL_CONTINUE\fR, or possibly some other |
| 92 |
integer value originating in an extension. |
| 93 |
In addition, a result value or error message is left in \fIinterp\fR's |
| 94 |
result; it can be retrieved using \fBTcl_GetObjResult\fR. |
| 95 |
.PP |
| 96 |
\fBTcl_EvalFile\fR reads the file given by \fIfileName\fR and evaluates |
| 97 |
its contents as a Tcl script. It returns the same information as |
| 98 |
\fBTcl_EvalObjEx\fR. |
| 99 |
If the file could not be read then a Tcl error is returned to describe |
| 100 |
why the file could not be read. |
| 101 |
The eofchar for files is |
| 102 |
.QW \e32 |
| 103 |
(^Z) for all platforms. If you require a |
| 104 |
.QW ^Z |
| 105 |
in code for string comparison, you can use |
| 106 |
.QW \e032 |
| 107 |
or |
| 108 |
.QW \eu001a , |
| 109 |
which will be safely substituted by the Tcl interpreter into |
| 110 |
.QW ^Z . |
| 111 |
.PP |
| 112 |
\fBTcl_EvalObjv\fR executes a single preparsed command instead of a |
| 113 |
script. The \fIobjc\fR and \fIobjv\fR arguments contain the values |
| 114 |
of the words for the Tcl command, one word in each value in |
| 115 |
\fIobjv\fR. \fBTcl_EvalObjv\fR evaluates the command and returns |
| 116 |
a completion code and result just like \fBTcl_EvalObjEx\fR. |
| 117 |
The caller of \fBTcl_EvalObjv\fR has to manage the reference count of the |
| 118 |
elements of \fIobjv\fR, insuring that the values are valid until |
| 119 |
\fBTcl_EvalObjv\fR returns. |
| 120 |
.PP |
| 121 |
\fBTcl_Eval\fR is similar to \fBTcl_EvalObjEx\fR except that the script to |
| 122 |
be executed is supplied as a string instead of a value and no compilation |
| 123 |
occurs. The string should be a proper UTF-8 string as converted by |
| 124 |
\fBTcl_ExternalToUtfDString\fR or \fBTcl_ExternalToUtf\fR when it is known |
| 125 |
to possibly contain upper ASCII characters whose possible combinations |
| 126 |
might be a UTF-8 special code. The string is parsed and executed directly |
| 127 |
(using \fBTcl_EvalObjv\fR) instead of compiling it and executing the |
| 128 |
bytecodes. In situations where it is known that the script will never be |
| 129 |
executed again, \fBTcl_Eval\fR may be faster than \fBTcl_EvalObjEx\fR. |
| 130 |
\fBTcl_Eval\fR returns a completion code and result just like |
| 131 |
\fBTcl_EvalObjEx\fR. Note: for backward compatibility with versions before |
| 132 |
Tcl 8.0, \fBTcl_Eval\fR copies the value result in \fIinterp\fR to |
| 133 |
\fIinterp->result\fR (use is deprecated) where it can be accessed directly. |
| 134 |
This makes \fBTcl_Eval\fR somewhat slower than \fBTcl_EvalEx\fR, which |
| 135 |
does not do the copy. |
| 136 |
.PP |
| 137 |
\fBTcl_EvalEx\fR is an extended version of \fBTcl_Eval\fR that takes |
| 138 |
additional arguments \fInumBytes\fR and \fIflags\fR. For the |
| 139 |
efficiency reason given above, \fBTcl_EvalEx\fR is generally preferred |
| 140 |
over \fBTcl_Eval\fR. |
| 141 |
.PP |
| 142 |
\fBTcl_GlobalEval\fR and \fBTcl_GlobalEvalObj\fR are older procedures |
| 143 |
that are now deprecated. They are similar to \fBTcl_EvalEx\fR and |
| 144 |
\fBTcl_EvalObjEx\fR except that the script is evaluated in the global |
| 145 |
namespace and its variable context consists of global variables only |
| 146 |
(it ignores any Tcl procedures that are active). These functions are |
| 147 |
equivalent to using the \fBTCL_EVAL_GLOBAL\fR flag (see below). |
| 148 |
.PP |
| 149 |
\fBTcl_VarEval\fR takes any number of string arguments |
| 150 |
of any length, concatenates them into a single string, |
| 151 |
then calls \fBTcl_Eval\fR to execute that string as a Tcl command. |
| 152 |
It returns the result of the command and also modifies |
| 153 |
\fIinterp->result\fR in the same way as \fBTcl_Eval\fR. |
| 154 |
The last argument to \fBTcl_VarEval\fR must be (char *)NULL to indicate the end |
| 155 |
of arguments. |
| 156 |
.PP |
| 157 |
\fBTcl_VarEvalVA\fR is the same as \fBTcl_VarEval\fR except that |
| 158 |
instead of taking a variable number of arguments it takes an argument |
| 159 |
list. \fBTcl_VarEvalVA\fR is now deprecated. |
| 160 |
|
| 161 |
.SH "FLAG BITS" |
| 162 |
.PP |
| 163 |
Any OR'ed combination of the following values may be used for the |
| 164 |
\fIflags\fR argument to procedures such as \fBTcl_EvalObjEx\fR: |
| 165 |
.TP 23 |
| 166 |
\fBTCL_EVAL_DIRECT\fR |
| 167 |
. |
| 168 |
This flag is only used by \fBTcl_EvalObjEx\fR; it is ignored by |
| 169 |
other procedures. If this flag bit is set, the script is not |
| 170 |
compiled to bytecodes; instead it is executed directly |
| 171 |
as is done by \fBTcl_EvalEx\fR. The |
| 172 |
\fBTCL_EVAL_DIRECT\fR flag is useful in situations where the |
| 173 |
contents of a value are going to change immediately, so the |
| 174 |
bytecodes will not be reused in a future execution. In this case, |
| 175 |
it is faster to execute the script directly. |
| 176 |
.TP 23 |
| 177 |
\fBTCL_EVAL_GLOBAL\fR |
| 178 |
. |
| 179 |
If this flag is set, the script is evaluated in the global namespace instead of |
| 180 |
the current namespace and its variable context consists of global variables |
| 181 |
only (it ignores any Tcl procedures that are active). |
| 182 |
.\" TODO: document TCL_EVAL_INVOKE and TCL_EVAL_NOERR. |
| 183 |
|
| 184 |
.SH "MISCELLANEOUS DETAILS" |
| 185 |
.PP |
| 186 |
During the processing of a Tcl command it is legal to make nested |
| 187 |
calls to evaluate other commands (this is how procedures and |
| 188 |
some control structures are implemented). |
| 189 |
If a code other than \fBTCL_OK\fR is returned |
| 190 |
from a nested \fBTcl_EvalObjEx\fR invocation, |
| 191 |
then the caller should normally return immediately, |
| 192 |
passing that same return code back to its caller, |
| 193 |
and so on until the top-level application is reached. |
| 194 |
A few commands, like \fBfor\fR, will check for certain |
| 195 |
return codes, like \fBTCL_BREAK\fR and \fBTCL_CONTINUE\fR, and process them |
| 196 |
specially without returning. |
| 197 |
.PP |
| 198 |
\fBTcl_EvalObjEx\fR keeps track of how many nested \fBTcl_EvalObjEx\fR |
| 199 |
invocations are in progress for \fIinterp\fR. |
| 200 |
If a code of \fBTCL_RETURN\fR, \fBTCL_BREAK\fR, or \fBTCL_CONTINUE\fR is |
| 201 |
about to be returned from the topmost \fBTcl_EvalObjEx\fR |
| 202 |
invocation for \fIinterp\fR, |
| 203 |
it converts the return code to \fBTCL_ERROR\fR |
| 204 |
and sets \fIinterp\fR's result to an error message indicating that |
| 205 |
the \fBreturn\fR, \fBbreak\fR, or \fBcontinue\fR command was |
| 206 |
invoked in an inappropriate place. |
| 207 |
This means that top-level applications should never see a return code |
| 208 |
from \fBTcl_EvalObjEx\fR other than \fBTCL_OK\fR or \fBTCL_ERROR\fR. |
| 209 |
|
| 210 |
.SH KEYWORDS |
| 211 |
execute, file, global, result, script, value |