Tcl_CreateCommand(3)

πŸ’– Tcl_CreateCommand β€” bestie, let's make a whole new command in C πŸ’–

okay so this is the Tcl Library Procedures page for Tcl_CreateCommand, and I'm gonna walk you through it like we're texting at 1am, except every single flag and detail is staying EXACTLY where it is. no cap. 🫢


✨ SYNOPSIS ✨

#include <tcl.h>

Tcl_Command
Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)

πŸŽ€ ARGUMENTS πŸŽ€

  • Tcl_Interp *interp (in) β€” the interpreter you're dropping this new command into. her house, her rules. 🏠
  • const char *cmdName (in) β€” the name of the command. what you're gonna type to summon her. ✨
  • Tcl_CmdProc *proc (in) β€” the actual implementation of your new command: proc gets called every single time cmdName is invoked as a command. main character behavior fr.
  • void *clientData (in) β€” any arbitrary one-word value you want, and it gets passed along to both proc and deleteProc. it's like a little gift you hand off. 🎁
  • Tcl_CmdDeleteProc *deleteProc (in) β€” the procedure that gets called before cmdName is deleted from the interpreter. this is your cleanup era, command-specific and everything. if it's NULL, then literally nothing gets called before the command is deleted. no goodbye, just gone. πŸ’€

🌸 DESCRIPTION 🌸

Tcl_CreateCommand defines a new command inside interp and links it up with the procedure proc, so that whenever cmdName is invoked as a Tcl command (like, via a call to Tcl_Eval), the Tcl interpreter will call proc to handle it. 🫑

Here's the tea though: this is different from Tcl_CreateObjCommand, because Tcl_CreateCommand defines a string-based command. translation? you're defining a command procedure that takes an array of argument strings instead of values. and bestie, that matters, because the value-based command procedures you register with Tcl_CreateObjCommand can run significantly faster than the string-based ones from Tcl_CreateCommand. why? because they take Tcl values as arguments, and those values can keep an internal representation that's way more efficient to manipulate. also, Tcl's interpreter now uses values internally β€” so to invoke a string-based command procedure registered by Tcl_CreateCommand, it has to generate and fetch a string representation from every single argument value before the call. it's giving extra steps. 😭

New commands should be defined using Tcl_CreateObjCommand. Tcl_CreateCommand is supported for backwards compatibility only. periodt.

The procedures Tcl_DeleteCommand, Tcl_GetCommandInfo, and Tcl_SetCommandInfo are used in conjunction with Tcl_CreateCommand. they're the friend group. πŸ’…

Tcl_CreateCommand will delete an existing command cmdName if one is already associated with the interpreter. it returns a token that you can use to refer to the command in subsequent calls to Tcl_GetCommandName. πŸ“Œ

If cmdName contains any :: namespace qualifiers, then the command gets added to the specified namespace; otherwise the command is added to the global namespace. and if Tcl_CreateCommand is called for an interpreter that's in the process of being deleted, then it does not create a new command and it returns NULL. nope, not today. πŸ™ˆ

Proc should have arguments and a result that match the type Tcl_CmdProc:

typedef int Tcl_CmdProc(
        void *clientData,
        Tcl_Interp *interp,
        int argc,
        const char *argv[]);

When proc is invoked, the clientData and interp parameters will be copies of the clientData and interp arguments you gave to Tcl_CreateCommand. typically, clientData points to an application-specific data structure that describes what to do when the command procedure is invoked. Argc and argv describe the arguments to the command β€” argc gives the number of arguments (including the command name) and argv gives the values of the arguments as strings. the argv array will contain argc+1 values: the first argc values point to the argument strings, and the last value is NULL. 🎯

Note: the argument strings should not be modified, because they may point to constant strings or may be shared with other parts of the interpreter. also note that the argument strings are encoded in normalized UTF-8 since version 8.1 of Tcl.

Proc must return an integer code that's expected to be one of TCL_OK, TCL_ERROR, TCL_RETURN, TCL_BREAK, or TCL_CONTINUE. see the return man page for details on what these codes mean and for the use of extended values for an extension's private use. most normal commands will only ever return TCL_OK or TCL_ERROR. keep it simple bestie. ✨

In addition, proc must set the interpreter result. in the case of a TCL_OK return code, this gives the result of the command; in the case of TCL_ERROR, it gives an error message. the Tcl_SetResult procedure provides an easy interface for setting the return value; for complete details on how the interpreter result field is managed, see the Tcl_Interp man page. before invoking a command procedure, Tcl_Eval sets the interpreter result to point to an empty string, so simple commands can return an empty result by doing absolutely nothing at all. slay, minimal effort. πŸ’…

The contents of the argv array belong to Tcl and are not guaranteed to persist once proc returns: proc should not modify them, and it should not set the interpreter result to point anywhere within the argv values. if you want to return something from the argv array, call Tcl_SetResult with status TCL_VOLATILE. 🌊

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

typedef void Tcl_CmdDeleteProc(
        void *clientData);

The clientData argument will be the same as the clientData argument you passed to Tcl_CreateCommand. same little gift, back again. 🎁


πŸ‘‘ SEE ALSO πŸ‘‘

  • Tcl_CreateObjCommand
  • Tcl_DeleteCommand
  • Tcl_GetCommandInfo
  • Tcl_SetCommandInfo
  • Tcl_GetCommandName
  • Tcl_SetObjResult

⭐ KEYWORDS ⭐

bind, command, create, delete, interpreter, namespace


and that's the whole page, bestie!! πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

this was the Tcl manual, page Tcl_CreateCommand(3) β€” brought to you by Tcl, from the Tcl Library Procedures collection. go forth and register commands the value-based way (aka Tcl_CreateObjCommand) unless you're living in the backwards-compatibility timeline. mwah. 😘

πŸ“¦ where this page comes from