1Tcl_CreateCommand(3) Tcl Library Procedures Tcl_CreateCommand(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_CreateCommand - implement new commands in C
9
11 #include <tcl.h>
12
13 Tcl_Command
14 Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
15
17 Tcl_Interp *interp (in) Interpreter in which to
18 create new command.
19
20 const char *cmdName (in) Name of command.
21
22 Tcl_CmdProc *proc (in) Implementation of new com‐
23 mand: proc will be called
24 whenever cmdName is invoked
25 as a command.
26
27 ClientData clientData (in) Arbitrary one-word value to
28 pass to proc and
29 deleteProc.
30
31 Tcl_CmdDeleteProc *deleteProc (in) Procedure to call before
32 cmdName is deleted from the
33 interpreter; allows for
34 command-specific cleanup.
35 If NULL, then no procedure
36 is called before the com‐
37 mand is deleted.
38_________________________________________________________________
39
40
42 Tcl_CreateCommand defines a new command in interp and associates it
43 with procedure proc such that whenever cmdName is invoked as a Tcl com‐
44 mand (via a call to Tcl_Eval) the Tcl interpreter will call proc to
45 process the command. It differs from Tcl_CreateObjCommand in that a
46 new string-based command is defined; that is, a command procedure is
47 defined that takes an array of argument strings instead of objects.
48 The object-based command procedures registered by Tcl_CreateObjCommand
49 can execute significantly faster than the string-based command proce‐
50 dures defined by Tcl_CreateCommand. This is because they take Tcl
51 objects as arguments and those objects can retain an internal represen‐
52 tation that can be manipulated more efficiently. Also, Tcl's inter‐
53 preter now uses objects internally. In order to invoke a string-based
54 command procedure registered by Tcl_CreateCommand, it must generate and
55 fetch a string representation from each argument object before the call
56 and create a new Tcl object to hold the string result returned by the
57 string-based command procedure. New commands should be defined using
58 Tcl_CreateObjCommand. We support Tcl_CreateCommand for backwards com‐
59 patibility.
60
61 The procedures Tcl_DeleteCommand, Tcl_GetCommandInfo, and Tcl_SetCom‐
62 mandInfo are used in conjunction with Tcl_CreateCommand.
63
64 Tcl_CreateCommand will delete an existing command cmdName, if one is
65 already associated with the interpreter. It returns a token that may
66 be used to refer to the command in subsequent calls to Tcl_GetCommand‐
67 Name. If cmdName contains any :: namespace qualifiers, then the com‐
68 mand is added to the specified namespace; otherwise the command is
69 added to the global namespace. If Tcl_CreateCommand is called for an
70 interpreter that is in the process of being deleted, then it does not
71 create a new command and it returns NULL. Proc should have arguments
72 and result that match the type Tcl_CmdProc:
73 typedef int Tcl_CmdProc(
74 ClientData clientData,
75 Tcl_Interp *interp,
76 int argc,
77 const char *argv[]);
78 When proc is invoked the clientData and interp parameters will be
79 copies of the clientData and interp arguments given to Tcl_CreateCom‐
80 mand. Typically, clientData points to an application-specific data
81 structure that describes what to do when the command procedure is
82 invoked. Argc and argv describe the arguments to the command, argc
83 giving the number of arguments (including the command name) and argv
84 giving the values of the arguments as strings. The argv array will
85 contain argc+1 values; the first argc values point to the argument
86 strings, and the last value is NULL. Note that the argument strings
87 should not be modified as they may point to constant strings or may be
88 shared with other parts of the interpreter.
89
90 Note that the argument strings are encoded in normalized UTF-8 since
91 version 8.1 of Tcl.
92
93 Proc must return an integer code that is expected to be one of TCL_OK,
94 TCL_ERROR, TCL_RETURN, TCL_BREAK, or TCL_CONTINUE. See the Tcl over‐
95 view man page for details on what these codes mean. Most normal com‐
96 mands will only return TCL_OK or TCL_ERROR. In addition, proc must set
97 the interpreter result to point to a string value; in the case of a
98 TCL_OK return code this gives the result of the command, and in the
99 case of TCL_ERROR it gives an error message. The Tcl_SetResult proce‐
100 dure provides an easy interface for setting the return value; for com‐
101 plete details on how the interpreter result field is managed, see the
102 Tcl_Interp man page. Before invoking a command procedure, Tcl_Eval
103 sets the interpreter result to point to an empty string, so simple com‐
104 mands can return an empty result by doing nothing at all.
105
106 The contents of the argv array belong to Tcl and are not guaranteed to
107 persist once proc returns: proc should not modify them, nor should it
108 set the interpreter result to point anywhere within the argv values.
109 Call Tcl_SetResult with status TCL_VOLATILE if you want to return some‐
110 thing from the argv array.
111
112 DeleteProc will be invoked when (if) cmdName is deleted. This can
113 occur through a call to Tcl_DeleteCommand or Tcl_DeleteInterp, or by
114 replacing cmdName in another call to Tcl_CreateCommand. DeleteProc is
115 invoked before the command is deleted, and gives the application an
116 opportunity to release any structures associated with the command.
117 DeleteProc should have arguments and result that match the type
118 Tcl_CmdDeleteProc:
119 typedef void Tcl_CmdDeleteProc(
120 ClientData clientData);
121 The clientData argument will be the same as the clientData argument
122 passed to Tcl_CreateCommand.
123
124
126 Tcl_CreateObjCommand, Tcl_DeleteCommand, Tcl_GetCommandInfo, Tcl_Set‐
127 CommandInfo, Tcl_GetCommandName, Tcl_SetObjResult
128
129
131 bind, command, create, delete, interpreter, namespace
132
133
134
135Tcl Tcl_CreateCommand(3)