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