1Tcl_CreateCommand(3)        Tcl Library Procedures        Tcl_CreateCommand(3)
2
3
4
5______________________________________________________________________________
6

NAME

8       Tcl_CreateCommand - implement new commands in C
9

SYNOPSIS

11       #include <tcl.h>
12
13       Tcl_Command
14       Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
15

ARGUMENTS

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

DESCRIPTION

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

SEE ALSO

126       Tcl_CreateObjCommand, Tcl_DeleteCommand,  Tcl_GetCommandInfo,  Tcl_Set‐
127       CommandInfo, Tcl_GetCommandName, Tcl_SetObjResult
128

KEYWORDS

130       bind, command, create, delete, interpreter, namespace
131
132
133
134Tcl                                                       Tcl_CreateCommand(3)
Impressum