2
votes

I have a C application that can execute TCL scripts, and many custom TCL commands that are implemented in C and registered with Tcl_CreateObjCommand. In the implementation of a command I want to find out a filename of the TCL script that has called my command. I.e. something like "info script" but called from C (I don't need a full path, just the filename would be sufficient). The only parameters that I have in my command are

Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]

clientData is NULL.

So is there a C function where I can pass just Tcl_Interp* and get the filename of the TCL script? Or if it doesn't exist, is it safe to call Tcl_Eval* functions from an implementation of a TCL command, and thus it will be an indirectly recursive call.

1

1 Answers

3
votes

The API for getting that information is just:

if (Tcl_Eval(interp, "info script") == TCL_OK) {
    const char *scriptFile = Tcl_GetString(Tcl_GetObjResult(interp));
    // Will be OK until you evaluate another script; copy if necessary
    // ...
} else {
    // Something bad happened; unlikely for just “info script” in a normal interp
}

There's no formal C API for getting the executing script name, as it isn't required all that often. The string produced will be whatever was passed to the source command (or to Tcl_EvalFile() or its relations).

In general, yes, it's safe to call Tcl_Eval from Tcl commands implemented in C. How do you think a command like if or while or for is implemented?