| 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_Interp 3 7.5 Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
Tcl_Interp \- client-visible fields of interpreter structures |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
typedef struct { |
| 18 |
char *\fIresult\fR; |
| 19 |
Tcl_FreeProc *\fIfreeProc\fR; |
| 20 |
int \fIerrorLine\fR; |
| 21 |
} \fBTcl_Interp\fR; |
| 22 |
|
| 23 |
typedef void \fBTcl_FreeProc\fR( |
| 24 |
char *\fIblockPtr\fR); |
| 25 |
.BE |
| 26 |
.SH DESCRIPTION |
| 27 |
.PP |
| 28 |
The \fBTcl_CreateInterp\fR procedure returns a pointer to a Tcl_Interp |
| 29 |
structure. Callers of \fBTcl_CreateInterp\fR should use this pointer |
| 30 |
as an opaque token, suitable for nothing other than passing back to |
| 31 |
other routines in the Tcl interface. Accessing fields directly through |
| 32 |
the pointer as described below is no longer supported. The supported |
| 33 |
public routines \fBTcl_SetResult\fR, \fBTcl_GetResult\fR, |
| 34 |
\fBTcl_SetErrorLine\fR, \fBTcl_GetErrorLine\fR must be used instead. |
| 35 |
.PP |
| 36 |
For legacy programs and extensions no longer being maintained, compiles |
| 37 |
against the Tcl 8.6 header files are only possible with the compiler |
| 38 |
directives |
| 39 |
.CS |
| 40 |
#define USE_INTERP_RESULT |
| 41 |
.CE |
| 42 |
and/or |
| 43 |
.CS |
| 44 |
#define USE_INTERP_ERRORLINE |
| 45 |
.CE |
| 46 |
depending on which fields of the \fBTcl_Interp\fR struct are accessed. |
| 47 |
These directives may be embedded in code or supplied via compiler options. |
| 48 |
.PP |
| 49 |
The \fIresult\fR and \fIfreeProc\fR fields are used to return |
| 50 |
results or error messages from commands. |
| 51 |
This information is returned by command procedures back to \fBTcl_Eval\fR, |
| 52 |
and by \fBTcl_Eval\fR back to its callers. |
| 53 |
The \fIresult\fR field points to the string that represents the |
| 54 |
result or error message, and the \fIfreeProc\fR field tells how |
| 55 |
to dispose of the storage for the string when it is not needed anymore. |
| 56 |
The easiest way for command procedures to manipulate these |
| 57 |
fields is to call procedures like \fBTcl_SetResult\fR |
| 58 |
or \fBTcl_AppendResult\fR; they |
| 59 |
will hide all the details of managing the fields. |
| 60 |
The description below is for those procedures that manipulate the |
| 61 |
fields directly. |
| 62 |
.PP |
| 63 |
Whenever a command procedure returns, it must ensure |
| 64 |
that the \fIresult\fR field of its interpreter points to the string |
| 65 |
being returned by the command. |
| 66 |
The \fIresult\fR field must always point to a valid string. |
| 67 |
If a command wishes to return no result then \fIinterp->result\fR |
| 68 |
should point to an empty string. |
| 69 |
Normally, results are assumed to be statically allocated, |
| 70 |
which means that the contents will not change before the next time |
| 71 |
\fBTcl_Eval\fR is called or some other command procedure is invoked. |
| 72 |
In this case, the \fIfreeProc\fR field must be zero. |
| 73 |
Alternatively, a command procedure may dynamically |
| 74 |
allocate its return value (e.g. using \fBTcl_Alloc\fR) |
| 75 |
and store a pointer to it in \fIinterp->result\fR. |
| 76 |
In this case, the command procedure must also set \fIinterp->freeProc\fR |
| 77 |
to the address of a procedure that can free the value, or \fBTCL_DYNAMIC\fR |
| 78 |
if the storage was allocated directly by Tcl or by a call to |
| 79 |
\fBTcl_Alloc\fR. |
| 80 |
If \fIinterp->freeProc\fR is non-zero, then Tcl will call \fIfreeProc\fR |
| 81 |
to free the space pointed to by \fIinterp->result\fR before it |
| 82 |
invokes the next command. |
| 83 |
If a client procedure overwrites \fIinterp->result\fR when |
| 84 |
\fIinterp->freeProc\fR is non-zero, then it is responsible for calling |
| 85 |
\fIfreeProc\fR to free the old \fIinterp->result\fR (the \fBTcl_FreeResult\fR |
| 86 |
macro should be used for this purpose). |
| 87 |
.PP |
| 88 |
\fIFreeProc\fR should have arguments and result that match the |
| 89 |
\fBTcl_FreeProc\fR declaration above: it receives a single |
| 90 |
argument which is a pointer to the result value to free. |
| 91 |
In most applications \fBTCL_DYNAMIC\fR is the only non-zero value ever |
| 92 |
used for \fIfreeProc\fR. |
| 93 |
However, an application may store a different procedure address |
| 94 |
in \fIfreeProc\fR in order to use an alternate memory allocator |
| 95 |
or in order to do other cleanup when the result memory is freed. |
| 96 |
.PP |
| 97 |
As part of processing each command, \fBTcl_Eval\fR initializes |
| 98 |
\fIinterp->result\fR |
| 99 |
and \fIinterp->freeProc\fR just before calling the command procedure for |
| 100 |
the command. The \fIfreeProc\fR field will be initialized to zero, |
| 101 |
and \fIinterp->result\fR will point to an empty string. Commands that |
| 102 |
do not return any value can simply leave the fields alone. |
| 103 |
Furthermore, the empty string pointed to by \fIresult\fR is actually |
| 104 |
part of an array of \fBTCL_RESULT_SIZE\fR characters (approximately 200). |
| 105 |
If a command wishes to return a short string, it can simply copy |
| 106 |
it to the area pointed to by \fIinterp->result\fR. Or, it can use |
| 107 |
the sprintf procedure to generate a short result string at the location |
| 108 |
pointed to by \fIinterp->result\fR. |
| 109 |
.PP |
| 110 |
It is a general convention in Tcl-based applications that the result |
| 111 |
of an interpreter is normally in the initialized state described |
| 112 |
in the previous paragraph. |
| 113 |
Procedures that manipulate an interpreter's result (e.g. by |
| 114 |
returning an error) will generally assume that the result |
| 115 |
has been initialized when the procedure is called. |
| 116 |
If such a procedure is to be called after the result has been |
| 117 |
changed, then \fBTcl_ResetResult\fR should be called first to |
| 118 |
reset the result to its initialized state. The direct use of |
| 119 |
\fIinterp->result\fR is strongly deprecated (see \fBTcl_SetResult\fR). |
| 120 |
.PP |
| 121 |
The \fIerrorLine\fR |
| 122 |
field is valid only after \fBTcl_Eval\fR returns |
| 123 |
a \fBTCL_ERROR\fR return code. In this situation the \fIerrorLine\fR |
| 124 |
field identifies the line number of the command being executed when |
| 125 |
the error occurred. The line numbers are relative to the command |
| 126 |
being executed: 1 means the first line of the command passed to |
| 127 |
\fBTcl_Eval\fR, 2 means the second line, and so on. |
| 128 |
The \fIerrorLine\fR field is typically used in conjunction with |
| 129 |
\fBTcl_AddErrorInfo\fR to report information about where an error |
| 130 |
occurred. |
| 131 |
\fIErrorLine\fR should not normally be modified except by \fBTcl_Eval\fR. |
| 132 |
|
| 133 |
.SH KEYWORDS |
| 134 |
free, initialized, interpreter, malloc, result |