Skip to content
Tcl Source Code Artifact [9e48b1125f]
Login

Artifact [9e48b1125f]

Artifact 9e48b1125f6ddc2a34f3baa9c74e6129e964489690ec7e7315707d961c800a61:



CommandName: Tcl_CreateCommand ManualSection: 3 Version: unknown TclPart: Tcl TclDescription: Tcl Library Procedures Links:

Name

Tcl_CreateCommand - implement new commands in C

Synopsis

::: {.synopsis} ::: #include <tcl.h> [Tcl_Command]{.ret} [Tcl_CreateCommand]{.lit} [interp, cmdName, proc, clientData, deleteProc]{.arg} :::

Arguments

.AP Tcl_Interp *interp in Interpreter in which to create new command. .AP "const char" *cmdName in Name of command. .AP Tcl_CmdProc *proc in Implementation of new command: fIprocfR will be called whenever fIcmdNamefR is invoked as a command. .AP void *clientData in Arbitrary one-word value to pass to fIprocfR and fIdeleteProcfR. .AP Tcl_CmdDeleteProc *deleteProc in Procedure to call before fIcmdNamefR is deleted from the interpreter; allows for command-specific cleanup. If NULL, then no procedure is called before the command is deleted.

Description

Tcl_CreateCommand defines a new command in fIinterpfR and associates it with procedure fIprocfR such that whenever fIcmdNamefR is invoked as a Tcl command (via a call to fBTcl_EvalfR) the Tcl interpreter will call fIprocfR to process the command. It differs from fBTcl_CreateObjCommandfR in that a new string-based command is defined; that is, a command procedure is defined that takes an array of argument strings instead of values. The value-based command procedures registered by fBTcl_CreateObjCommandfR can execute significantly faster than the string-based command procedures defined by fBTcl_CreateCommandfR. This is because they take Tcl values as arguments and those values can retain an internal representation that can be manipulated more efficiently. Also, Tcl's interpreter now uses values internally. In order to invoke a string-based command procedure registered by fBTcl_CreateCommandfR, it must generate and fetch a string representation from each argument value before the call. New commands should be defined using fBTcl_CreateObjCommandfR. We support fBTcl_CreateCommandfR for backwards compatibility.

The procedures fBTcl_DeleteCommandfR, fBTcl_GetCommandInfofR, and fBTcl_SetCommandInfofR are used in conjunction with fBTcl_CreateCommandfR.

Tcl_CreateCommand will delete an existing command fIcmdNamefR, if one is already associated with the interpreter. It returns a token that may be used to refer to the command in subsequent calls to fBTcl_GetCommandNamefR. If fIcmdNamefR contains any fB::fR namespace qualifiers, then the command is added to the specified namespace; otherwise the command is added to the global namespace. If fBTcl_CreateCommandfR is called for an interpreter that is in the process of being deleted, then it does not create a new command and it returns NULL. fIProcfR should have arguments and result that match the type fBTcl_CmdProcfR:

typedef int Tcl_CmdProc(
        void *clientData,
        Tcl_Interp *interp,
        int argc,
        const char *argv[]);

When fIprocfR is invoked the fIclientDatafR and fIinterpfR parameters will be copies of the fIclientDatafR and fIinterpfR arguments given to fBTcl_CreateCommandfR. Typically, fIclientDatafR points to an application-specific data structure that describes what to do when the command procedure is invoked. fIArgcfR and fIargvfR describe the arguments to the command, fIargcfR giving the number of arguments (including the command name) and fIargvfR giving the values of the arguments as strings. The fIargvfR array will contain fIargcfR+1 values; the first fIargcfR values point to the argument strings, and the last value is NULL.

Note that the argument strings should not be modified as they may point to constant strings or may be shared with other parts of the interpreter. Note also that the argument strings are encoded in normalized UTF-8 since version 8.1 of Tcl.

Proc must return an integer code that is expected to be one of fBTCL_OKfR, fBTCL_ERRORfR, fBTCL_RETURNfR, fBTCL_BREAKfR, or fBTCL_CONTINUEfR. See the fBreturnfR man page for details on what these codes mean and the use of extended values for an extension's private use. Most normal commands will only return fBTCL_OKfR or fBTCL_ERRORfR.

In addition, fIprocfR must set the interpreter result; in the case of a fBTCL_OKfR return code this gives the result of the command, and in the case of fBTCL_ERRORfR it gives an error message. The fBTcl_SetResultfR procedure provides an easy interface for setting the return value; for complete details on how the interpreter result field is managed, see the fBTcl_InterpfR man page. Before invoking a command procedure, fBTcl_EvalfR sets the interpreter result to point to an empty string, so simple commands can return an empty result by doing nothing at all.

The contents of the fIargvfR array belong to Tcl and are not guaranteed to persist once fIprocfR returns: fIprocfR should not modify them, nor should it set the interpreter result to point anywhere within the fIargvfR values. Call fBTcl_SetResultfR with status fBTCL_VOLATILEfR if you want to return something from the fIargvfR array.

DeleteProc will be invoked when (if) fIcmdNamefR is deleted. This can occur through a call to fBTcl_DeleteCommandfR or fBTcl_DeleteInterpfR, or by replacing fIcmdNamefR in another call to fBTcl_CreateCommandfR. fIDeleteProcfR is invoked before the command is deleted, and gives the application an opportunity to release any structures associated with the command. fIDeleteProcfR should have arguments and result that match the type fBTcl_CmdDeleteProcfR:

typedef void Tcl_CmdDeleteProc(
        void *clientData);

The fIclientDatafR argument will be the same as the fIclientDatafR argument passed to fBTcl_CreateCommandfR.