Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/CrtObjCmd.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 12.9 KB
Line 
1'\"
2'\" Copyright (c) 1996-1997 Sun Microsystems, Inc.
3'\"
4'\" See the file "license.terms" for information on usage and redistribution
5'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6'\"
7'\" RCS: @(#) $Id: CrtObjCmd.3,v 1.17 2007/12/13 15:22:30 dgp Exp $
8'\"
9.so man.macros
10.TH Tcl_CreateObjCommand 3 8.0 Tcl "Tcl Library Procedures"
11.BS
12.SH NAME
13Tcl_CreateObjCommand, Tcl_DeleteCommand, Tcl_DeleteCommandFromToken, Tcl_GetCommandInfo, Tcl_GetCommandInfoFromToken, Tcl_SetCommandInfo, Tcl_SetCommandInfoFromToken, Tcl_GetCommandName, Tcl_GetCommandFullName, Tcl_GetCommandFromObj \- implement new commands in C
14.SH SYNOPSIS
15.nf
16\fB#include <tcl.h>\fR
17.sp
18Tcl_Command
19\fBTcl_CreateObjCommand\fR(\fIinterp, cmdName, proc, clientData, deleteProc\fR)
20.sp
21int
22\fBTcl_DeleteCommand\fR(\fIinterp, cmdName\fR)
23.sp
24int
25\fBTcl_DeleteCommandFromToken\fR(\fIinterp, token\fR)
26.sp
27int
28\fBTcl_GetCommandInfo\fR(\fIinterp, cmdName, infoPtr\fR)
29.sp
30int
31\fBTcl_SetCommandInfo\fR(\fIinterp, cmdName, infoPtr\fR)
32.sp
33int
34\fBTcl_GetCommandInfoFromToken\fR(\fItoken, infoPtr\fR)
35.sp
36int
37\fBTcl_SetCommandInfoFromToken\fR(\fItoken, infoPtr\fR)
38.sp
39const char *
40\fBTcl_GetCommandName\fR(\fIinterp, token\fR)
41.sp
42void
43\fBTcl_GetCommandFullName\fR(\fIinterp, token, objPtr\fR)
44.sp
45Tcl_Command
46\fBTcl_GetCommandFromObj\fR(\fIinterp, objPtr\fR)
47.SH ARGUMENTS
48.AS Tcl_CmdDeleteProc *deleteProc in/out
49.AP Tcl_Interp *interp in
50Interpreter in which to create a new command or that contains a command.
51.AP char *cmdName in
52Name of command.
53.AP Tcl_ObjCmdProc *proc in
54Implementation of the new command: \fIproc\fR will be called whenever
55\fIcmdName\fR is invoked as a command.
56.AP ClientData clientData in
57Arbitrary one-word value to pass to \fIproc\fR and \fIdeleteProc\fR.
58.AP Tcl_CmdDeleteProc *deleteProc in
59Procedure to call before \fIcmdName\fR is deleted from the interpreter;
60allows for command-specific cleanup. If NULL, then no procedure is
61called before the command is deleted.
62.AP Tcl_Command token in
63Token for command, returned by previous call to \fBTcl_CreateObjCommand\fR.
64The command must not have been deleted.
65.AP Tcl_CmdInfo *infoPtr in/out
66Pointer to structure containing various information about a
67Tcl command.
68.AP Tcl_Obj *objPtr in
69Object containing the name of a Tcl command.
70.BE
71.SH DESCRIPTION
72.PP
73\fBTcl_CreateObjCommand\fR defines a new command in \fIinterp\fR
74and associates it with procedure \fIproc\fR
75such that whenever \fIname\fR is
76invoked as a Tcl command (e.g., via a call to \fBTcl_EvalObjEx\fR)
77the Tcl interpreter will call \fIproc\fR to process the command.
78.PP
79\fBTcl_CreateObjCommand\fR deletes any existing command
80\fIname\fR already associated with the interpreter
81(however see below for an exception where the existing command
82is not deleted).
83It returns a token that may be used to refer
84to the command in subsequent calls to \fBTcl_GetCommandName\fR.
85If \fIname\fR contains any \fB::\fR namespace qualifiers,
86then the command is added to the specified namespace;
87otherwise the command is added to the global namespace.
88If \fBTcl_CreateObjCommand\fR is called for an interpreter that is in
89the process of being deleted, then it does not create a new command
90and it returns NULL.
91\fIproc\fR should have arguments and result that match the type
92\fBTcl_ObjCmdProc\fR:
93.CS
94typedef int Tcl_ObjCmdProc(
95        ClientData \fIclientData\fR,
96        Tcl_Interp *\fIinterp\fR,
97        int \fIobjc\fR,
98        Tcl_Obj *const \fIobjv\fR[]);
99.CE
100When \fIproc\fR is invoked, the \fIclientData\fR and \fIinterp\fR parameters
101will be copies of the \fIclientData\fR and \fIinterp\fR arguments given to
102\fBTcl_CreateObjCommand\fR.  Typically, \fIclientData\fR points to an
103application-specific data structure that describes what to do when the
104command procedure is invoked. \fIObjc\fR and \fIobjv\fR describe the
105arguments to the command, \fIobjc\fR giving the number of argument objects
106(including the command name) and \fIobjv\fR giving the values of the
107arguments.  The \fIobjv\fR array will contain \fIobjc\fR values, pointing to
108the argument objects.  Unlike \fIargv\fR[\fIargv\fR] used in a
109string-based command procedure, \fIobjv\fR[\fIobjc\fR] will not contain NULL.
110.PP
111Additionally, when \fIproc\fR is invoked, it must not modify the contents
112of the \fIobjv\fR array by assigning new pointer values to any element of the
113array (for example, \fIobjv\fR[\fB2\fR] = \fBNULL\fR) because this will
114cause memory to be lost and the runtime stack to be corrupted.  The
115\fBconst\fR in the declaration of \fIobjv\fR will cause ANSI-compliant
116compilers to report any such attempted assignment as an error.  However,
117it is acceptable to modify the internal representation of any individual
118object argument.  For instance, the user may call
119\fBTcl_GetIntFromObj\fR on \fIobjv\fR[\fB2\fR] to obtain the integer
120representation of that object; that call may change the type of the object
121that \fIobjv\fR[\fB2\fR] points at, but will not change where
122\fIobjv\fR[\fB2\fR] points.
123.PP
124\fIproc\fR must return an integer code that is either \fBTCL_OK\fR,
125\fBTCL_ERROR\fR, \fBTCL_RETURN\fR, \fBTCL_BREAK\fR, or \fBTCL_CONTINUE\fR.
126See the Tcl overview man page
127for details on what these codes mean.  Most normal commands will only
128return \fBTCL_OK\fR or \fBTCL_ERROR\fR.
129In addition, if \fIproc\fR needs to return a non-empty result,
130it can call \fBTcl_SetObjResult\fR to set the interpreter's result.
131In the case of a \fBTCL_OK\fR return code this gives the result
132of the command,
133and in the case of \fBTCL_ERROR\fR this gives an error message.
134Before invoking a command procedure,
135\fBTcl_EvalObjEx\fR sets interpreter's result to
136point to an object representing an empty string, so simple
137commands can return an empty result by doing nothing at all.
138.PP
139The contents of the \fIobjv\fR array belong to Tcl and are not
140guaranteed to persist once \fIproc\fR returns: \fIproc\fR should
141not modify them.
142Call \fBTcl_SetObjResult\fR if you want
143to return something from the \fIobjv\fR array.
144.PP
145Ordinarily, \fBTcl_CreateObjCommand\fR deletes any existing command
146\fIname\fR already associated with the interpreter.
147However, if the existing command was created by a previous call to
148\fBTcl_CreateCommand\fR,
149\fBTcl_CreateObjCommand\fR does not delete the command
150but instead arranges for the Tcl interpreter to call the
151\fBTcl_ObjCmdProc\fR \fIproc\fR in the future.
152The old string-based \fBTcl_CmdProc\fR associated with the command
153is retained and its address can be obtained by subsequent
154\fBTcl_GetCommandInfo\fR calls. This is done for backwards compatibility.
155.PP
156\fIDeleteProc\fR will be invoked when (if) \fIname\fR is deleted.
157This can occur through a call to \fBTcl_DeleteCommand\fR,
158\fBTcl_DeleteCommandFromToken\fR, or \fBTcl_DeleteInterp\fR,
159or by replacing \fIname\fR in another call to \fBTcl_CreateObjCommand\fR.
160\fIDeleteProc\fR is invoked before the command is deleted, and gives the
161application an opportunity to release any structures associated
162with the command.  \fIDeleteProc\fR should have arguments and
163result that match the type \fBTcl_CmdDeleteProc\fR:
164.CS
165typedef void Tcl_CmdDeleteProc(
166        ClientData \fIclientData\fR);
167.CE
168The \fIclientData\fR argument will be the same as the \fIclientData\fR
169argument passed to \fBTcl_CreateObjCommand\fR.
170.PP
171\fBTcl_DeleteCommand\fR deletes a command from a command interpreter.
172Once the call completes, attempts to invoke \fIcmdName\fR in
173\fIinterp\fR will result in errors.
174If \fIcmdName\fR is not bound as a command in \fIinterp\fR then
175\fBTcl_DeleteCommand\fR does nothing and returns -1;  otherwise
176it returns 0.
177There are no restrictions on \fIcmdName\fR:  it may refer to
178a built-in command, an application-specific command, or a Tcl procedure.
179If \fIname\fR contains any \fB::\fR namespace qualifiers,
180the command is deleted from the specified namespace.
181.PP
182Given a token returned by \fBTcl_CreateObjCommand\fR,
183\fBTcl_DeleteCommandFromToken\fR deletes the command
184from a command interpreter.
185It will delete a command even if that command has been renamed.
186Once the call completes, attempts to invoke the command in
187\fIinterp\fR will result in errors.
188If the command corresponding to \fItoken\fR
189has already been deleted from \fIinterp\fR then
190\fBTcl_DeleteCommand\fR does nothing and returns -1;
191otherwise it returns 0.
192.PP
193\fBTcl_GetCommandInfo\fR checks to see whether its \fIcmdName\fR argument
194exists as a command in \fIinterp\fR.
195\fIcmdName\fR may include \fB::\fR namespace qualifiers
196to identify a command in a particular namespace.
197If the command is not found, then it returns 0.
198Otherwise it places information about the command
199in the \fBTcl_CmdInfo\fR structure
200pointed to by \fIinfoPtr\fR and returns 1.
201A \fBTcl_CmdInfo\fR structure has the following fields:
202.CS
203typedef struct Tcl_CmdInfo {
204    int \fIisNativeObjectProc\fR;
205    Tcl_ObjCmdProc *\fIobjProc\fR;
206    ClientData \fIobjClientData\fR;
207    Tcl_CmdProc *\fIproc\fR;
208    ClientData \fIclientData\fR;
209    Tcl_CmdDeleteProc *\fIdeleteProc\fR;
210    ClientData \fIdeleteData\fR;
211    Tcl_Namespace *\fInamespacePtr\fR;
212} Tcl_CmdInfo;
213.CE
214The \fIisNativeObjectProc\fR field has the value 1
215if \fBTcl_CreateObjCommand\fR was called to register the command;
216it is 0 if only \fBTcl_CreateCommand\fR was called.
217It allows a program to determine whether it is faster to
218call \fIobjProc\fR or \fIproc\fR:
219\fIobjProc\fR is normally faster
220if \fIisNativeObjectProc\fR has the value 1.
221The fields \fIobjProc\fR and \fIobjClientData\fR
222have the same meaning as the \fIproc\fR and \fIclientData\fR
223arguments to \fBTcl_CreateObjCommand\fR;
224they hold information about the object-based command procedure
225that the Tcl interpreter calls to implement the command.
226The fields \fIproc\fR and \fIclientData\fR
227hold information about the string-based command procedure
228that implements the command.
229If \fBTcl_CreateCommand\fR was called for this command,
230this is the procedure passed to it;
231otherwise, this is a compatibility procedure
232registered by \fBTcl_CreateObjCommand\fR
233that simply calls the command's
234object-based procedure after converting its string arguments to Tcl objects.
235The field \fIdeleteData\fR is the ClientData value
236to pass to \fIdeleteProc\fR;  it is normally the same as
237\fIclientData\fR but may be set independently using the
238\fBTcl_SetCommandInfo\fR procedure.
239The field \fInamespacePtr\fR holds a pointer to the
240Tcl_Namespace that contains the command.
241.PP
242\fBTcl_GetCommandInfoFromToken\fR is identical to
243\fBTcl_GetCommandInfo\fR except that it uses a command token returned
244from \fBTcl_CreateObjCommand\fR in place of the command name.  If the
245\fItoken\fR parameter is NULL, it returns 0; otherwise, it returns 1
246and fills in the structure designated by \fIinfoPtr\fR.
247.PP
248\fBTcl_SetCommandInfo\fR is used to modify the procedures and
249ClientData values associated with a command.
250Its \fIcmdName\fR argument is the name of a command in \fIinterp\fR.
251\fIcmdName\fR may include \fB::\fR namespace qualifiers
252to identify a command in a particular namespace.
253If this command does not exist then \fBTcl_SetCommandInfo\fR returns 0.
254Otherwise, it copies the information from \fI*infoPtr\fR to
255Tcl's internal structure for the command and returns 1.
256.PP
257\fBTcl_SetCommandInfoFromToken\fR is identical to
258\fBTcl_SetCommandInfo\fR except that it takes a command token as
259returned by \fBTcl_CreateObjCommand\fR instead of the command name.
260If the \fItoken\fR parameter is NULL, it returns 0.  Otherwise, it
261copies the information from \fI*infoPtr\fR to Tcl's internal structure
262for the command and returns 1.
263.PP
264Note that \fBTcl_SetCommandInfo\fR and
265\fBTcl_SetCommandInfoFromToken\fR both allow the ClientData for a
266command's deletion procedure to be given a different value than the
267ClientData for its command procedure.
268.PP
269Note that neither \fBTcl_SetCommandInfo\fR nor
270\fBTcl_SetCommandInfoFromToken\fR will change a command's namespace.
271Use \fBTcl_Eval\fR to call the \fBrename\fR command to do that.
272.PP
273\fBTcl_GetCommandName\fR provides a mechanism for tracking commands
274that have been renamed.
275Given a token returned by \fBTcl_CreateObjCommand\fR
276when the command was created, \fBTcl_GetCommandName\fR returns the
277string name of the command.  If the command has been renamed since it
278was created, then \fBTcl_GetCommandName\fR returns the current name.
279This name does not include any \fB::\fR namespace qualifiers.
280The command corresponding to \fItoken\fR must not have been deleted.
281The string returned by \fBTcl_GetCommandName\fR is in dynamic memory
282owned by Tcl and is only guaranteed to retain its value as long as the
283command is not deleted or renamed;  callers should copy the string if
284they need to keep it for a long time.
285.PP
286\fBTcl_GetCommandFullName\fR produces the fully qualified name
287of a command from a command token. 
288The name, including all namespace prefixes,
289is appended to the object specified by \fIobjPtr\fR.
290.PP
291\fBTcl_GetCommandFromObj\fR returns a token for the command
292specified by the name in a \fBTcl_Obj\fR.
293The command name is resolved relative to the current namespace.
294Returns NULL if the command is not found.
295.SH "SEE ALSO"
296Tcl_CreateCommand, Tcl_ResetResult, Tcl_SetObjResult
297
298.SH KEYWORDS
299bind, command, create, delete, namespace, object
Note: See TracBrowser for help on using the repository browser.