Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added tcl to libs

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