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