1Tcl_CreateObjCommand(3) Tcl Library Procedures Tcl_CreateObjCommand(3)
2
3
4
5______________________________________________________________________________
6
8 Tcl_CreateObjCommand, Tcl_DeleteCommand, Tcl_DeleteCommandFromToken,
9 Tcl_GetCommandInfo, Tcl_GetCommandInfoFromToken, Tcl_SetCommandInfo,
10 Tcl_SetCommandInfoFromToken, Tcl_GetCommandName, Tcl_GetCommandFull‐
11 Name, Tcl_GetCommandFromObj - implement new commands in C
12
14 #include <tcl.h>
15
16 Tcl_Command
17 Tcl_CreateObjCommand(interp, cmdName, proc, clientData, deleteProc)
18
19 int
20 Tcl_DeleteCommand(interp, cmdName)
21
22 int
23 Tcl_DeleteCommandFromToken(interp, token)
24
25 int
26 Tcl_GetCommandInfo(interp, cmdName, infoPtr)
27
28 int
29 Tcl_SetCommandInfo(interp, cmdName, infoPtr)
30
31 int
32 Tcl_GetCommandInfoFromToken(token, infoPtr)
33
34 int
35 Tcl_SetCommandInfoFromToken(token, infoPtr)
36
37 const char *
38 Tcl_GetCommandName(interp, token)
39
40 void
41 Tcl_GetCommandFullName(interp, token, objPtr)
42
43 Tcl_Command
44 Tcl_GetCommandFromObj(interp, objPtr)
45
47 Tcl_Interp *interp (in) Interpreter in which to
48 create a new command or
49 that contains a command.
50
51 char *cmdName (in) Name of command.
52
53 Tcl_ObjCmdProc *proc (in) Implementation of the new
54 command: proc will be
55 called whenever cmdName is
56 invoked as a command.
57
58 ClientData clientData (in) Arbitrary one-word value to
59 pass to proc and
60 deleteProc.
61
62 Tcl_CmdDeleteProc *deleteProc (in) Procedure to call before
63 cmdName is deleted from the
64 interpreter; allows for
65 command-specific cleanup.
66 If NULL, then no procedure
67 is called before the com‐
68 mand is deleted.
69
70 Tcl_Command token (in) Token for command, returned
71 by previous call to
72 Tcl_CreateObjCommand. The
73 command must not have been
74 deleted.
75
76 Tcl_CmdInfo *infoPtr (in/out) Pointer to structure con‐
77 taining various information
78 about a Tcl command.
79
80 Tcl_Obj *objPtr (in) Object containing the name
81 of a Tcl command.
82_________________________________________________________________
83
85 Tcl_CreateObjCommand defines a new command in interp and associates it
86 with procedure proc such that whenever name is invoked as a Tcl command
87 (e.g., via a call to Tcl_EvalObjEx) the Tcl interpreter will call proc
88 to process the command.
89
90 Tcl_CreateObjCommand deletes any existing command name already associ‐
91 ated with the interpreter (however see below for an exception where the
92 existing command is not deleted). It returns a token that may be used
93 to refer to the command in subsequent calls to Tcl_GetCommandName. If
94 name contains any :: namespace qualifiers, then the command is added to
95 the specified namespace; otherwise the command is added to the global
96 namespace. If Tcl_CreateObjCommand is called for an interpreter that
97 is in the process of being deleted, then it does not create a new com‐
98 mand and it returns NULL. proc should have arguments and result that
99 match the type Tcl_ObjCmdProc:
100 typedef int Tcl_ObjCmdProc(
101 ClientData clientData,
102 Tcl_Interp *interp,
103 int objc,
104 Tcl_Obj *const objv[]);
105 When proc is invoked, the clientData and interp parameters will be
106 copies of the clientData and interp arguments given to Tcl_CreateObj‐
107 Command. Typically, clientData points to an application-specific data
108 structure that describes what to do when the command procedure is
109 invoked. Objc and objv describe the arguments to the command, objc giv‐
110 ing the number of argument objects (including the command name) and
111 objv giving the values of the arguments. The objv array will contain
112 objc values, pointing to the argument objects. Unlike argv[argv] used
113 in a string-based command procedure, objv[objc] will not contain NULL.
114
115 Additionally, when proc is invoked, it must not modify the contents of
116 the objv array by assigning new pointer values to any element of the
117 array (for example, objv[2] = NULL) because this will cause memory to
118 be lost and the runtime stack to be corrupted. The const in the decla‐
119 ration of objv will cause ANSI-compliant compilers to report any such
120 attempted assignment as an error. However, it is acceptable to modify
121 the internal representation of any individual object argument. For
122 instance, the user may call Tcl_GetIntFromObj on objv[2] to obtain the
123 integer representation of that object; that call may change the type of
124 the object that objv[2] points at, but will not change where objv[2]
125 points.
126
127 proc must return an integer code that is either TCL_OK, TCL_ERROR,
128 TCL_RETURN, TCL_BREAK, or TCL_CONTINUE. See the Tcl overview man page
129 for details on what these codes mean. Most normal commands will only
130 return TCL_OK or TCL_ERROR. In addition, if proc needs to return a
131 non-empty result, it can call Tcl_SetObjResult to set the interpreter's
132 result. In the case of a TCL_OK return code this gives the result of
133 the command, and in the case of TCL_ERROR this gives an error message.
134 Before invoking a command procedure, Tcl_EvalObjEx sets interpreter's
135 result to point to an object representing an empty string, so simple
136 commands can return an empty result by doing nothing at all.
137
138 The contents of the objv array belong to Tcl and are not guaranteed to
139 persist once proc returns: proc should not modify them. Call Tcl_SetO‐
140 bjResult if you want to return something from the objv array.
141
142 Ordinarily, Tcl_CreateObjCommand deletes any existing command name
143 already associated with the interpreter. However, if the existing com‐
144 mand was created by a previous call to Tcl_CreateCommand, Tcl_CreateOb‐
145 jCommand does not delete the command but instead arranges for the Tcl
146 interpreter to call the Tcl_ObjCmdProc proc in the future. The old
147 string-based Tcl_CmdProc associated with the command is retained and
148 its address can be obtained by subsequent Tcl_GetCommandInfo calls.
149 This is done for backwards compatibility.
150
151 DeleteProc will be invoked when (if) name is deleted. This can occur
152 through a call to Tcl_DeleteCommand, Tcl_DeleteCommandFromToken, or
153 Tcl_DeleteInterp, or by replacing name in another call to Tcl_CreateOb‐
154 jCommand. DeleteProc is invoked before the command is deleted, and
155 gives the application an opportunity to release any structures associ‐
156 ated with the command. DeleteProc should have arguments and result
157 that match the type Tcl_CmdDeleteProc:
158 typedef void Tcl_CmdDeleteProc(
159 ClientData clientData);
160 The clientData argument will be the same as the clientData argument
161 passed to Tcl_CreateObjCommand.
162
163 Tcl_DeleteCommand deletes a command from a command interpreter. Once
164 the call completes, attempts to invoke cmdName in interp will result in
165 errors. If cmdName is not bound as a command in interp then
166 Tcl_DeleteCommand does nothing and returns -1; otherwise it returns 0.
167 There are no restrictions on cmdName: it may refer to a built-in com‐
168 mand, an application-specific command, or a Tcl procedure. If name
169 contains any :: namespace qualifiers, the command is deleted from the
170 specified namespace.
171
172 Given a token returned by Tcl_CreateObjCommand, Tcl_DeleteCommandFrom‐
173 Token deletes the command from a command interpreter. It will delete a
174 command even if that command has been renamed. Once the call com‐
175 pletes, attempts to invoke the command in interp will result in errors.
176 If the command corresponding to token has already been deleted from
177 interp then Tcl_DeleteCommand does nothing and returns -1; otherwise it
178 returns 0.
179
180 Tcl_GetCommandInfo checks to see whether its cmdName argument exists as
181 a command in interp. cmdName may include :: namespace qualifiers to
182 identify a command in a particular namespace. If the command is not
183 found, then it returns 0. Otherwise it places information about the
184 command in the Tcl_CmdInfo structure pointed to by infoPtr and returns
185 1. A Tcl_CmdInfo structure has the following fields:
186 typedef struct Tcl_CmdInfo {
187 int isNativeObjectProc;
188 Tcl_ObjCmdProc *objProc;
189 ClientData objClientData;
190 Tcl_CmdProc *proc;
191 ClientData clientData;
192 Tcl_CmdDeleteProc *deleteProc;
193 ClientData deleteData;
194 Tcl_Namespace *namespacePtr;
195 } Tcl_CmdInfo;
196 The isNativeObjectProc field has the value 1 if Tcl_CreateObjCommand
197 was called to register the command; it is 0 if only Tcl_CreateCommand
198 was called. It allows a program to determine whether it is faster to
199 call objProc or proc: objProc is normally faster if isNativeObjectProc
200 has the value 1. The fields objProc and objClientData have the same
201 meaning as the proc and clientData arguments to Tcl_CreateObjCommand;
202 they hold information about the object-based command procedure that the
203 Tcl interpreter calls to implement the command. The fields proc and
204 clientData hold information about the string-based command procedure
205 that implements the command. If Tcl_CreateCommand was called for this
206 command, this is the procedure passed to it; otherwise, this is a com‐
207 patibility procedure registered by Tcl_CreateObjCommand that simply
208 calls the command's object-based procedure after converting its string
209 arguments to Tcl objects. The field deleteData is the ClientData value
210 to pass to deleteProc; it is normally the same as clientData but may
211 be set independently using the Tcl_SetCommandInfo procedure. The field
212 namespacePtr holds a pointer to the Tcl_Namespace that contains the
213 command.
214
215 Tcl_GetCommandInfoFromToken is identical to Tcl_GetCommandInfo except
216 that it uses a command token returned from Tcl_CreateObjCommand in
217 place of the command name. If the token parameter is NULL, it returns
218 0; otherwise, it returns 1 and fills in the structure designated by
219 infoPtr.
220
221 Tcl_SetCommandInfo is used to modify the procedures and ClientData val‐
222 ues associated with a command. Its cmdName argument is the name of a
223 command in interp. cmdName may include :: namespace qualifiers to
224 identify a command in a particular namespace. If this command does not
225 exist then Tcl_SetCommandInfo returns 0. Otherwise, it copies the
226 information from *infoPtr to Tcl's internal structure for the command
227 and returns 1.
228
229 Tcl_SetCommandInfoFromToken is identical to Tcl_SetCommandInfo except
230 that it takes a command token as returned by Tcl_CreateObjCommand
231 instead of the command name. If the token parameter is NULL, it
232 returns 0. Otherwise, it copies the information from *infoPtr to Tcl's
233 internal structure for the command and returns 1.
234
235 Note that Tcl_SetCommandInfo and Tcl_SetCommandInfoFromToken both allow
236 the ClientData for a command's deletion procedure to be given a differ‐
237 ent value than the ClientData for its command procedure.
238
239 Note that neither Tcl_SetCommandInfo nor Tcl_SetCommandInfoFromToken
240 will change a command's namespace. Use Tcl_Eval to call the rename
241 command to do that.
242
243 Tcl_GetCommandName provides a mechanism for tracking commands that have
244 been renamed. Given a token returned by Tcl_CreateObjCommand when the
245 command was created, Tcl_GetCommandName returns the string name of the
246 command. If the command has been renamed since it was created, then
247 Tcl_GetCommandName returns the current name. This name does not
248 include any :: namespace qualifiers. The command corresponding to
249 token must not have been deleted. The string returned by Tcl_GetCom‐
250 mandName is in dynamic memory owned by Tcl and is only guaranteed to
251 retain its value as long as the command is not deleted or renamed;
252 callers should copy the string if they need to keep it for a long time.
253
254 Tcl_GetCommandFullName produces the fully qualified name of a command
255 from a command token. The name, including all namespace prefixes, is
256 appended to the object specified by objPtr.
257
258 Tcl_GetCommandFromObj returns a token for the command specified by the
259 name in a Tcl_Obj. The command name is resolved relative to the cur‐
260 rent namespace. Returns NULL if the command is not found.
261
263 Tcl_CreateCommand, Tcl_ResetResult, Tcl_SetObjResult
264
265
267 bind, command, create, delete, namespace, object
268
269
270
271Tcl 8.0 Tcl_CreateObjCommand(3)