Tcl_CreateAlias(3tcl)

โœจ NAME โœจ

okay so bestie, this whole page is about one iconic squad of functions: Tcl_IsSafe, Tcl_CreateChild, Tcl_GetChild, Tcl_GetParent, Tcl_GetInterpPath, Tcl_CreateAlias, Tcl_CreateAliasObj, Tcl_GetAliasObj, Tcl_ExposeCommand, and Tcl_HideCommand. They all live in the Tcl Library Procedures and theyโ€™re here to help you manage multiple Tcl interpreters, aliases and hidden commands. This is the Tcl_CreateAlias(3tcl) manual page, and itโ€™s giving very much โ€œI run a whole multiverse of interpretersโ€ energy. ๐Ÿฆ‹


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <tcl.h>

int
Tcl_IsSafe(interp)

Tcl_Interp *
Tcl_CreateChild(interp, name, isSafe)

Tcl_Interp *
Tcl_GetChild(interp, name)

Tcl_Interp *
Tcl_GetParent(interp)

int
Tcl_GetInterpPath(interp, childInterp)

int
Tcl_CreateAlias(childInterp, childCmd, targetInterp, targetCmd, argc, argv)

int
Tcl_CreateAliasObj(childInterp, childCmd, targetInterp, targetCmd, objc, objv)

int
Tcl_GetAliasObj(interp, childCmd, targetInterpPtr, targetCmdPtr, objcPtr, objvPtr)

int
Tcl_ExposeCommand(interp, hiddenCmdName, cmdName)

int
Tcl_HideCommand(interp, cmdName, hiddenCmdName)

๐Ÿ’… ARGUMENTS ๐Ÿ’…

  • Tcl_Interp *interp (in) โ€” the interpreter where you want to run the specified command. Like, the main characterโ€™s home base. ๐Ÿ 
  • const char *name (in) โ€” the name of the child interpreter youโ€™re creating or manipulating. Gotta name your babies. ๐Ÿ‘‘
  • int isSafe (in) โ€” if this is non-zero, you get a โ€œsafeโ€ child thatโ€™s suitable for running untrusted code. If itโ€™s zero, you get a trusted child. Choose wisely, bestie. ๐Ÿ”’
  • Tcl_Interp *childInterp (in) โ€” the interpreter used to create the source command for an alias (peep the description below for the full tea). โ˜•
  • const char *childCmd (in) โ€” the name of the source command for the alias. Itโ€™s the command that gets aliased. โœจ
  • Tcl_Interp *targetInterp (in) โ€” the interpreter that contains the target command for an alias. So, where the real magic happens. ๐ŸŽฏ
  • const char *targetCmd (in) โ€” the name of the target command for the alias, living inside targetInterp. ๐Ÿ‘ฏ
  • Tcl_Size argc (in) โ€” how many additional arguments youโ€™re passing to the alias command. Count โ€˜em up. ๐Ÿ”ข
  • const char *const *argv (in) โ€” a vector of strings: the additional arguments to pass to the alias command. This storage is owned by the caller. So you brought it, you manage it. ๐ŸŽ’
  • Tcl_Size objc (in) โ€” count of additional value arguments to pass to the aliased command. Same vibes as argc, but for objects. ๐Ÿ“ฆ
  • Tcl_Obj **objv (in) โ€” a vector of Tcl_Obj structures, the additional value arguments to pass to the aliased command. This storage is owned by the caller too. You keep it, you clean it. ๐Ÿงน
  • Tcl_Interp **targetInterpPtr (in) โ€” pointer to a location where weโ€™ll store the address of the interpreter where a target command is defined for an alias. Itโ€™s like a little mailbox for an interpreter address. ๐Ÿ“ฌ
  • const char **targetCmdPtr (out) โ€” pointer to a location where weโ€™ll store the address of the name of the target command for an alias. Another mailbox, but for a command name. ๐Ÿ“ฎ
  • Tcl_Size | int *objcPtr (out) โ€” pointer to a location where weโ€™ll store the count of additional value arguments to be passed to the alias. The location is in storage owned by the caller. If it points to a variable whose type is not Tcl_Size, a compiler warning will be generated. If your extension is compiled with -DTCL_8_API, this function will return TCL_ERROR for aliases with more than INT_MAX value arguments โ€” otherwise, expect it to crash ๐Ÿ’€. No cap, thatโ€™s the warning.
  • Tcl_Obj ***objvPtr (out) โ€” pointer to a location where weโ€™ll store a vector of Tcl_Obj structures: the additional arguments to pass to an alias command. The location is in storage owned by the caller, but the vector of Tcl_Obj structures is owned by the called function. So like, you get the address, they own the goodies. ๐Ÿซถ
  • const char *cmdName (in) โ€” the name of an exposed command to hide or create. Itโ€™s the commandโ€™s public-facing name. ๐ŸŒŸ
  • const char *hiddenCmdName (in) โ€” the name under which a hidden command is stored, and with which it can be exposed or invoked. Itโ€™s the secret identity. ๐Ÿ•ต๏ธโ€โ™€๏ธ

thatโ€™s part 1, bestie โ€” more Tcl interpreter chaos is coming in part 2, so donโ€™t go anywhere ๐Ÿ’–

๐Ÿ’ซ DESCRIPTION ๐Ÿ’ซ

Okay bestie, listen up, because these procedures are literally the VIP pass into Tcl's multiple interpreter facility from inside C programs. They let you manage a whole lil hierarchy of interpreters, plus handle aliases โ€” and an alias is just a command that, when you invoke it in one interpreter, runs a command over in another interpreter. Iconic crossover behavior, fr.

The return value for any of these procedures that returns an int is either TCL_OK or TCL_ERROR. And if it hands you back TCL_ERROR? Then the interpreter's result is holding an error message. No cap, go read it.


  • Tcl_CreateChild โ€” makes a brand new interpreter as a child of interp. It also creates a child command named name in interp, which is how interp gets to manipulate the new baby child. If isSafe is zero, the command creates a trusted child, where Tcl code has access to all the Tcl commands. If isSafe is 1, you get a โ€œsafeโ€ child, where Tcl code only gets access to the set of Tcl commands defined as โ€œSafe Tclโ€ โ€” peep the manual entry for the Tcl interp command for the details. And if creating that new child interpreter failed? You get NULL. ๐Ÿ’€
  • Tcl_IsSafe โ€” returns 1 if interp is โ€œsafeโ€ (meaning it was created with the TCL_SAFE_INTERPRETER flag specified), and 0 otherwise. Simple as.
  • Tcl_GetChild โ€” hands you back a pointer to a child interpreter of interp. The child is identified by name. If no such child interpreter exists, you get NULL. ๐Ÿ™ˆ
  • Tcl_GetParent โ€” hands you back a pointer to the parent interpreter of interp. If interp has no parent (it's a top-level interpreter, main character energy), then NULL comes back.
  • Tcl_GetInterpPath โ€” stores in the result of interp the relative path between interp and childInterp; and heads up, childInterp must be a child of interp. If the computation of the relative path goes well, TCL_OK is returned. Otherwise TCL_ERROR comes back and an error message gets stored as the result of interp.
  • Tcl_CreateAlias โ€” creates a command named childCmd in childInterp that, when invoked, will cause targetCmd to be invoked in targetInterp. The arguments specified by the strings contained in argv are always prepended to any arguments supplied in the invocation of childCmd, and passed along to targetCmd. Returns TCL_OK if it succeeds, or TCL_ERROR if it fails โ€” and in that failure case, an error message is left in the value result of childInterp. Also, bestie, plot twist: there are no restrictions on the ancestry relationship (as created by Tcl_CreateChild) between childInterp and targetInterp. Any two interpreters can be used, with zero restrictions on how they're related. Slay.
  • Tcl_CreateAliasObj โ€” same vibe as Tcl_CreateAlias, except it takes a vector of values to pass as additional arguments instead of a vector of strings. Small change, big difference. โœจ
  • Tcl_GetAliasObj โ€” returns info in the form of a pointer to a vector of Tcl_Obj structures about an alias aliasName in interp. Any of the result fields can be NULL, in which case the corresponding datum is just not returned. If a result field is non-NULL, the address indicated gets set to the corresponding datum. For example, if targetCmdPtr is non-NULL, it gets set to a pointer to the string containing the name of the target command. ๐ŸŽ€
  • Tcl_ExposeCommand โ€” moves the command named hiddenCmdName from the set of hidden commands over to the set of exposed commands, putting it under the name cmdName. hiddenCmdName must be the name of an existing hidden command, or the operation returns TCL_ERROR and leaves an error message as the result of interp. If an exposed command named cmdName already exists, the operation returns TCL_ERROR and leaves an error message as the result of interp. If everything goes right, it returns TCL_OK. After executing this command, attempts to use cmdName in any script evaluation mechanism will again succeed. Welcome back to the spotlight, babe. ๐Ÿ‘‘
  • Tcl_HideCommand โ€” moves the command named cmdName from the set of exposed commands to the set of hidden commands, under the name hiddenCmdName. cmdName must be the name of an existing exposed command, or the operation returns TCL_ERROR and leaves an error message as the result of interp. Currently both cmdName and hiddenCmdName must not contain namespace qualifiers, or the operation returns TCL_ERROR and leaves an error message as the result of interp. Also, cmdName will be looked up in the global namespace, and not relative to the current namespace โ€” even if the current namespace is not the global one. Sneaky but true. And if a hidden command whose name is hiddenCmdName already exists, the operation also returns TCL_ERROR and an error message is left as the result of interp. If it succeeds, it returns TCL_OK. After executing this command, attempts to use cmdName in any script evaluation mechanism will fail. Gone. Ghosted. ๐Ÿ‘ป
If you want the deets on the Tcl interface to multiple interpreters, go see interp(3tcl).

๐Ÿซถ REFERENCE COUNT MANAGEMENT ๐Ÿซถ

Okay this part is important, don't skip it:

  • Tcl_CreateAliasObj increments the reference counts of the values in its objv argument. (And that reference lasts the same length of time as the owning alias.) So it's holding onto those values, periodt.
  • Tcl_GetAliasObj returns โ€” via its objvPtr argument โ€” a pointer to values that it holds a reference to. Keep that in mind and you won't get got. ๐Ÿ’…

๐ŸŒˆ SEE ALSO ๐ŸŒˆ


โญ KEYWORDS โญ

alias, command, exposed commands, hidden commands, interpreter, invoke, parent, child


And that's the whole tea, bestie ๐Ÿ’– this was Tcl_CreateAlias(3tcl) from Tcl 7.6, dated the same way the page itself signs off โ€” go forth and manage those interpreters like the icon you are, mwah ๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from