1Tcl_Interp(3) Tcl Library Procedures Tcl_Interp(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_Interp - client-visible fields of interpreter structures
9
11 #include <tcl.h>
12
13 typedef struct {
14 char *result;
15 Tcl_FreeProc *freeProc;
16 int errorLine;
17 } Tcl_Interp;
18
19 typedef void Tcl_FreeProc(
20 char *blockPtr);
21______________________________________________________________________________
22
24 The Tcl_CreateInterp procedure returns a pointer to a Tcl_Interp struc‐
25 ture. Callers of Tcl_CreateInterp should use this pointer as an opaque
26 token, suitable for nothing other than passing back to other routines
27 in the Tcl interface. Accessing fields directly through the pointer as
28 described below is no longer supported. The supported public routines
29 Tcl_SetResult, Tcl_GetResult, Tcl_SetErrorLine, Tcl_GetErrorLine must
30 be used instead.
31
32 For legacy programs and extensions no longer being maintained, compiles
33 against the Tcl 8.6 header files are only possible with the compiler
34 directives
35 #define USE_INTERP_RESULT
36 and/or
37 #define USE_INTERP_ERRORLINE
38 depending on which fields of the Tcl_Interp struct are accessed. These
39 directives may be embedded in code or supplied via compiler options.
40
41 The result and freeProc fields are used to return results or error mes‐
42 sages from commands. This information is returned by command proce‐
43 dures back to Tcl_Eval, and by Tcl_Eval back to its callers. The
44 result field points to the string that represents the result or error
45 message, and the freeProc field tells how to dispose of the storage for
46 the string when it is not needed anymore. The easiest way for command
47 procedures to manipulate these fields is to call procedures like
48 Tcl_SetResult or Tcl_AppendResult; they will hide all the details of
49 managing the fields. The description below is for those procedures
50 that manipulate the fields directly.
51
52 Whenever a command procedure returns, it must ensure that the result
53 field of its interpreter points to the string being returned by the
54 command. The result field must always point to a valid string. If a
55 command wishes to return no result then interp->result should point to
56 an empty string. Normally, results are assumed to be statically allo‐
57 cated, which means that the contents will not change before the next
58 time Tcl_Eval is called or some other command procedure is invoked. In
59 this case, the freeProc field must be zero. Alternatively, a command
60 procedure may dynamically allocate its return value (e.g. using
61 Tcl_Alloc) and store a pointer to it in interp->result. In this case,
62 the command procedure must also set interp->freeProc to the address of
63 a procedure that can free the value, or TCL_DYNAMIC if the storage was
64 allocated directly by Tcl or by a call to Tcl_Alloc. If interp->freeP‐
65 roc is non-zero, then Tcl will call freeProc to free the space pointed
66 to by interp->result before it invokes the next command. If a client
67 procedure overwrites interp->result when interp->freeProc is non-zero,
68 then it is responsible for calling freeProc to free the old
69 interp->result (the Tcl_FreeResult macro should be used for this pur‐
70 pose).
71
72 FreeProc should have arguments and result that match the Tcl_FreeProc
73 declaration above: it receives a single argument which is a pointer to
74 the result value to free. In most applications TCL_DYNAMIC is the only
75 non-zero value ever used for freeProc. However, an application may
76 store a different procedure address in freeProc in order to use an
77 alternate memory allocator or in order to do other cleanup when the
78 result memory is freed.
79
80 As part of processing each command, Tcl_Eval initializes interp->result
81 and interp->freeProc just before calling the command procedure for the
82 command. The freeProc field will be initialized to zero, and
83 interp->result will point to an empty string. Commands that do not
84 return any value can simply leave the fields alone. Furthermore, the
85 empty string pointed to by result is actually part of an array of
86 TCL_RESULT_SIZE characters (approximately 200). If a command wishes to
87 return a short string, it can simply copy it to the area pointed to by
88 interp->result. Or, it can use the sprintf procedure to generate a
89 short result string at the location pointed to by interp->result.
90
91 It is a general convention in Tcl-based applications that the result of
92 an interpreter is normally in the initialized state described in the
93 previous paragraph. Procedures that manipulate an interpreter's result
94 (e.g. by returning an error) will generally assume that the result has
95 been initialized when the procedure is called. If such a procedure is
96 to be called after the result has been changed, then Tcl_ResetResult
97 should be called first to reset the result to its initialized state.
98 The direct use of interp->result is strongly deprecated (see Tcl_SetRe‐
99 sult).
100
101 The errorLine field is valid only after Tcl_Eval returns a TCL_ERROR
102 return code. In this situation the errorLine field identifies the line
103 number of the command being executed when the error occurred. The line
104 numbers are relative to the command being executed: 1 means the first
105 line of the command passed to Tcl_Eval, 2 means the second line, and so
106 on. The errorLine field is typically used in conjunction with
107 Tcl_AddErrorInfo to report information about where an error occurred.
108 ErrorLine should not normally be modified except by Tcl_Eval.
109
110
112 free, initialized, interpreter, malloc, result
113
114
115
116Tcl 7.5 Tcl_Interp(3)