| 1 |
'\" |
| 2 |
'\" Copyright (c) 1989-1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1997 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_CreateCommand 3 "" Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
Tcl_CreateCommand \- implement new commands in C |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
Tcl_Command |
| 18 |
\fBTcl_CreateCommand\fR(\fIinterp, cmdName, proc, clientData, deleteProc\fR) |
| 19 |
.SH ARGUMENTS |
| 20 |
.AS Tcl_CmdDeleteProc *deleteProc |
| 21 |
.AP Tcl_Interp *interp in |
| 22 |
Interpreter in which to create new command. |
| 23 |
.AP "const char" *cmdName in |
| 24 |
Name of command. |
| 25 |
.AP Tcl_CmdProc *proc in |
| 26 |
Implementation of new command: \fIproc\fR will be called whenever |
| 27 |
\fIcmdName\fR is invoked as a command. |
| 28 |
.AP ClientData clientData in |
| 29 |
Arbitrary one-word value to pass to \fIproc\fR and \fIdeleteProc\fR. |
| 30 |
.AP Tcl_CmdDeleteProc *deleteProc in |
| 31 |
Procedure to call before \fIcmdName\fR is deleted from the interpreter; |
| 32 |
allows for command-specific cleanup. If NULL, then no procedure is |
| 33 |
called before the command is deleted. |
| 34 |
.BE |
| 35 |
.SH DESCRIPTION |
| 36 |
.PP |
| 37 |
\fBTcl_CreateCommand\fR defines a new command in \fIinterp\fR and associates |
| 38 |
it with procedure \fIproc\fR such that whenever \fIcmdName\fR is |
| 39 |
invoked as a Tcl command (via a call to \fBTcl_Eval\fR) the Tcl interpreter |
| 40 |
will call \fIproc\fR to process the command. |
| 41 |
It differs from \fBTcl_CreateObjCommand\fR in that a new string-based |
| 42 |
command is defined; |
| 43 |
that is, a command procedure is defined that takes an array of |
| 44 |
argument strings instead of values. |
| 45 |
The value-based command procedures registered by \fBTcl_CreateObjCommand\fR |
| 46 |
can execute significantly faster than the string-based command procedures |
| 47 |
defined by \fBTcl_CreateCommand\fR. |
| 48 |
This is because they take Tcl values as arguments |
| 49 |
and those values can retain an internal representation that |
| 50 |
can be manipulated more efficiently. |
| 51 |
Also, Tcl's interpreter now uses values internally. |
| 52 |
In order to invoke a string-based command procedure |
| 53 |
registered by \fBTcl_CreateCommand\fR, |
| 54 |
it must generate and fetch a string representation |
| 55 |
from each argument value before the call. |
| 56 |
New commands should be defined using \fBTcl_CreateObjCommand\fR. |
| 57 |
We support \fBTcl_CreateCommand\fR for backwards compatibility. |
| 58 |
.PP |
| 59 |
The procedures \fBTcl_DeleteCommand\fR, \fBTcl_GetCommandInfo\fR, |
| 60 |
and \fBTcl_SetCommandInfo\fR are used in conjunction with |
| 61 |
\fBTcl_CreateCommand\fR. |
| 62 |
.PP |
| 63 |
\fBTcl_CreateCommand\fR will delete an existing command \fIcmdName\fR, |
| 64 |
if one is already associated with the interpreter. |
| 65 |
It returns a token that may be used to refer |
| 66 |
to the command in subsequent calls to \fBTcl_GetCommandName\fR. |
| 67 |
If \fIcmdName\fR contains any \fB::\fR namespace qualifiers, |
| 68 |
then the command is added to the specified namespace; |
| 69 |
otherwise the command is added to the global namespace. |
| 70 |
If \fBTcl_CreateCommand\fR is called for an interpreter that is in |
| 71 |
the process of being deleted, then it does not create a new command |
| 72 |
and it returns NULL. |
| 73 |
\fIProc\fR should have arguments and result that match the type |
| 74 |
\fBTcl_CmdProc\fR: |
| 75 |
.PP |
| 76 |
.CS |
| 77 |
typedef int \fBTcl_CmdProc\fR( |
| 78 |
ClientData \fIclientData\fR, |
| 79 |
Tcl_Interp *\fIinterp\fR, |
| 80 |
int \fIargc\fR, |
| 81 |
const char *\fIargv\fR[]); |
| 82 |
.CE |
| 83 |
.PP |
| 84 |
When \fIproc\fR is invoked the \fIclientData\fR and \fIinterp\fR |
| 85 |
parameters will be copies of the \fIclientData\fR and \fIinterp\fR |
| 86 |
arguments given to \fBTcl_CreateCommand\fR. |
| 87 |
Typically, \fIclientData\fR points to an application-specific |
| 88 |
data structure that describes what to do when the command procedure |
| 89 |
is invoked. \fIArgc\fR and \fIargv\fR describe the arguments to |
| 90 |
the command, \fIargc\fR giving the number of arguments (including |
| 91 |
the command name) and \fIargv\fR giving the values of the arguments |
| 92 |
as strings. The \fIargv\fR array will contain \fIargc\fR+1 values; |
| 93 |
the first \fIargc\fR values point to the argument strings, and the |
| 94 |
last value is NULL. |
| 95 |
Note that the argument strings should not be modified as they may |
| 96 |
point to constant strings or may be shared with other parts of the |
| 97 |
interpreter. |
| 98 |
.PP |
| 99 |
Note that the argument strings are encoded in normalized UTF-8 since |
| 100 |
version 8.1 of Tcl. |
| 101 |
.PP |
| 102 |
\fIProc\fR must return an integer code that is expected to be one of |
| 103 |
\fBTCL_OK\fR, \fBTCL_ERROR\fR, \fBTCL_RETURN\fR, \fBTCL_BREAK\fR, or |
| 104 |
\fBTCL_CONTINUE\fR. See the Tcl overview man page |
| 105 |
for details on what these codes mean. Most normal commands will only |
| 106 |
return \fBTCL_OK\fR or \fBTCL_ERROR\fR. In addition, \fIproc\fR must set |
| 107 |
the interpreter result; |
| 108 |
in the case of a \fBTCL_OK\fR return code this gives the result |
| 109 |
of the command, and in the case of \fBTCL_ERROR\fR it gives an error message. |
| 110 |
The \fBTcl_SetResult\fR procedure provides an easy interface for setting |
| 111 |
the return value; for complete details on how the interpreter result |
| 112 |
field is managed, see the \fBTcl_Interp\fR man page. |
| 113 |
Before invoking a command procedure, |
| 114 |
\fBTcl_Eval\fR sets the interpreter result to point to an empty string, |
| 115 |
so simple commands can return an empty result by doing nothing at all. |
| 116 |
.PP |
| 117 |
The contents of the \fIargv\fR array belong to Tcl and are not |
| 118 |
guaranteed to persist once \fIproc\fR returns: \fIproc\fR should |
| 119 |
not modify them, nor should it set the interpreter result to point |
| 120 |
anywhere within the \fIargv\fR values. |
| 121 |
Call \fBTcl_SetResult\fR with status \fBTCL_VOLATILE\fR if you want |
| 122 |
to return something from the \fIargv\fR array. |
| 123 |
.PP |
| 124 |
\fIDeleteProc\fR will be invoked when (if) \fIcmdName\fR is deleted. This can |
| 125 |
occur through a call to \fBTcl_DeleteCommand\fR or \fBTcl_DeleteInterp\fR, |
| 126 |
or by replacing \fIcmdName\fR in another call to \fBTcl_CreateCommand\fR. |
| 127 |
\fIDeleteProc\fR is invoked before the command is deleted, and gives the |
| 128 |
application an opportunity to release any structures associated |
| 129 |
with the command. \fIDeleteProc\fR should have arguments and |
| 130 |
result that match the type \fBTcl_CmdDeleteProc\fR: |
| 131 |
.PP |
| 132 |
.CS |
| 133 |
typedef void \fBTcl_CmdDeleteProc\fR( |
| 134 |
ClientData \fIclientData\fR); |
| 135 |
.CE |
| 136 |
.PP |
| 137 |
The \fIclientData\fR argument will be the same as the \fIclientData\fR |
| 138 |
argument passed to \fBTcl_CreateCommand\fR. |
| 139 |
.SH "SEE ALSO" |
| 140 |
Tcl_CreateObjCommand, Tcl_DeleteCommand, Tcl_GetCommandInfo, |
| 141 |
Tcl_SetCommandInfo, Tcl_GetCommandName, Tcl_SetObjResult |
| 142 |
.SH KEYWORDS |
| 143 |
bind, command, create, delete, interpreter, namespace |