Tcl Library Procedures

NAME

Tcl_CreateObjCommand - implement new commands in C

SYNOPSIS

#include <tcl.h>

Tcl_Command
Tcl_CreateObjCommand(interp, name, length, proc, clientData, deleteProc)

ARGUMENTS

Tcl_Interp *interp (in)
Interpreter in which to create new command.

char *name (in)
Points to first byte of the name of the command. The name may contain embedded null bytes.

int length (in)
Number of bytes in the command's name. If negative, bytes up to the first null byte are used.

Tcl_ObjCmdProc *proc (in)
Implementation of the new command: proc will be called whenever name is invoked as a command.

ClientData clientData (in)
Arbitrary one-word value to pass to proc and deleteProc.

Tcl_CmdDeleteProc *deleteProc (in)
Procedure to call before name is deleted from the interpreter; allows for command-specific cleanup. If NULL, then no procedure is called before the command is deleted.

DESCRIPTION

Tcl_CreateObjCommand defines a new command in interp and associates it with procedure proc such that whenever name is invoked as a Tcl command (via a call to Tcl_Eval) the Tcl interpreter will call proc to process the command. It differs from Tcl_CreateCommand in that a new object-based command is defined; that is, a command procedure is defined that takes an array of argument objects instead of strings. The object-based command procedures registered by Tcl_CreateObjCommand can execute significantly faster than the string-based command procedures defined by Tcl_CreateCommand. This is because they take Tcl objects as arguments and those objects can retain an internal representation that can be manipulated more efficiently. Also, Tcl's interpreter now uses objects internally. In order to invoke a string-based command procedure registered by Tcl_CreateCommand, it must generate and fetch a string representation from each argument object before the call and create a new Tcl object to hold the string result returned by the string-based command procedure. New commands should be defined using Tcl_CreateObjCommand. We support Tcl_CreateCommand for backwards compatibility.

The procedures Tcl_DeleteCommand, Tcl_GetCommandInfo, and Tcl_SetCommandInfo are used in conjunction with both Tcl_CreateObjCommand and Tcl_CreateCommand.

Tcl_CreateObjCommand will delete any command name already associated with the interpreter. It returns a token that may be used to refer to the command in subsequent calls to Tcl_GetCommandName. If Tcl_CreateObjCommand is called for an interpreter that is in the process of being deleted, then it does not create a new command and it returns NULL. ObjProc should have arguments and result that match the type Tcl_ObjCmdProc:

typedef int Tcl_ObjCmdProc(
       ClientData clientData,
       Tcl_Interp *interp,
       int objc,
       Tcl_Obj *objv[]);
When proc is invoked the clientData and interp parameters will be copies of the clientData and interp arguments given to Tcl_CreateObjCommand. Typically, clientData points to an application-specific data structure that describes what to do when the command procedure is invoked. Objc and objv describe the arguments to the command, objc giving the number of argument objects (including the command name) and objv giving the values of the arguments as objects. The objv array will contain objc+1 values; the first objc values point to the argument objects, and the last value is NULL.

ObjProc must return an integer code that is either TCL_OK, TCL_ERROR, TCL_RETURN, TCL_BREAK, or TCL_CONTINUE. See the Tcl overview man page for details on what these codes mean. Most normal commands will only return TCL_OK or TCL_ERROR. In addition, proc must set interp->objResult to point to an object; in the case of a TCL_OK return code this gives the result of the command, and in the case of TCL_ERROR it gives an error message. The Tcl_GetObjResult and Tcl_SetObjResult procedures provide an easy interface for getting and setting the result object; for details about how the interp->objResult field is managed, see the man page for Tcl_SetObjResult. Before invoking a command procedure, Tcl_Eval sets interp->objResult to point to an object representing an empty string, so simple commands can return an empty result by doing nothing at all.

The contents of the objv array belong to Tcl and are not guaranteed to persist once proc returns: proc should not modify them, nor should it set interp->objResult to point to one of the objv values. Call Tcl_SetObjResult if you want to return something from the objv array.

DeleteProc will be invoked when (if) name is deleted. This can occur through a call to Tcl_DeleteCommand or Tcl_DeleteInterp, or by replacing name in another call to Tcl_CreateObjCommand or Tcl_CreateCommand. DeleteProc is invoked before the command is deleted, and gives the application an opportunity to release any structures associated with the command. DeleteProc should have arguments and result that match the type Tcl_CmdDeleteProc:

typedef void Tcl_CmdDeleteProc(ClientData clientData);
The clientData argument will be the same as the clientData argument passed to Tcl_CreateObjCommand.

SEE ALSO

Tcl_CreateCommand, Tcl_DeleteCommand, Tcl_GetCommandInfo, Tcl_SetCommandInfo, Tcl_SetObjResult, Tcl_ResetObjResult

KEYWORDS

bind, command, create, delete, object

Last change: 8.0

[ tcl8.0a1 | tk8.0a1 | X-ref ]

Copyright © 1989-1994 The Regents of the University of California.
Copyright © 1994-1996 Sun Microsystems, Inc.