NAME
Tcl_Interp - client-visible fields of interpreter structures

SYNOPSIS
#include <tcl.h>

typedef struct {
	char *result;
	int dynamic;
	int errorLine;
} Tcl_Interp;

DESCRIPTION

The Tcl_CreateInterp procedure returns a pointer to a Tcl_Interp structure. This pointer is then passed into other Tcl procedures to process commands in the interpreter and perform other operations on the interpreter. Interpreter structures contain many many fields that are used by Tcl, but only three that may be accessed by clients: result and dynamic. These fields are used by Tcl command procedures to return strings that form part of the result of each command. When Tcl_Eval returns, the string pointed to be the result field will be used by Tcl_Eval's caller as a return value or error message.

The easiest way for command procedures to manipulate the result and dynamic fields is to call Tcl_Return; Tcl_Return will hide all the details of managing these fields. The description below is for those procedures that manipulate the fields directly.

Whenever a command procedure returns, it must ensure that the result field of its interpreter points to the string being returned by the command. Normally, these strings are assumed to be statically allocated; in this case, the dynamic field must be zero. As an alternative, a command procedure may dynamically allocate its return value and store a pointer to it in interp->result. In this case, the command procedure must also set interp->dynamic to non-zero. If interp->dynamic is non-zero, then Tcl will free the space pointed to by interp->result before it invokes the next command. If a client procedure overwrites interp->result field when interp->dynamic is non-zero, then it is responsible for freeing the old interp->result. Once again, if clients use the Tcl_Result procedure to manage these fields, they need not worry about these issues.

As part of processing each command, Tcl_Eval initializes interp->result and interp->dynamic just before calling the command procedure for the command. The dynamic field will be initialized to zero, and interp->result will point to an empty string. Commands that do not return any value can simply leave the fields alone. Furthermore, the empty string pointed to by result is actually part of an array of TCL_RESULT_SIZE characters (approximately 200). If a command wishes to return a short string, it can simply copy it to the area pointed to by interp->result. Or, it can use the sprintf procedure to generate a short result string at the location pointed to by interp->result.

If a command procedure calls a lower-level procedure that sets interp->result and interp->dynamic (such as a recursive instance of Tcl_Eval), then the command procedure must reset interp->result if it wishes to return a value different than that returned by the lower-level procedure. As part of resetting interp->result, it must free the space if interp->dynamic is set. Once again, the easiest way to make sure this gets done right is to call Tcl_Result.

The errorLine field is valid only after Tcl_Eval returns a TCL_ERROR return code. In this situation the errorLine field identifies the line number of the command being executed when the error occurred. The line numbers are relative to the command being executed: 1 means the first line of the command passed to Tcl_Eval, 2 means the second line, and so on. ErrorLine should not normally be modified except by Tcl_Eval.

KEYWORDS
dynamic, interpreter, result