Donal K. Fellows's Small C Extensions


Linked Tcl_Obj Variables

This zipped C source for Tcl8.0 (and later?) allows the linking of Tcl_Obj-containing variables at the Tcl and C levels in an efficient manner. Note that this code is extracted from my OO Extension which is why the functions have the prefixes they have, though this code is entirely self-contained...

Dkfobj_LinkVar(interp,varName,addr,read_only)

Links the named Tcl variable (varName) in the given interpreter (interp) to the C variable (addr) containing a pointer to a Tcl_Obj structure, marking the Tcl variable in question as being read-only if the boolean read_only is set.

Dkfobj_UnlinkVar(interp,varName)

Undoes the link made by Dkfobj_LinkVar above.

Dkfobj_UpdateLinkedVar(interp,varName)

When a C variable linked to by Dkfobj_LinkVar has been updated, the change should be reflected back to the Tcl level by calling this function.


Turn Procedures into Functions

This C source file (suitable for both Tcl7 and Tcl8) builds into an extension to allow the implementation of functions (as used in expressions) in terms of ordinary Tcl commands (typically a procedure.) (Sorry, I've not written the Makefile or a configure script - it should be pretty easy if you know what you are doing as I don't use any platform-specific features at all.

Examples:

intfunc foo 2 fooProc

declares a new function called foo that takes two arguments and that is implemented by a procedure called fooProc. The function returns an integer.

doublefunc bar 3 barProc

declares a new function called bar that takes three arguments and that is implemented by a procedure called barProc. The function returns a floating-point number.

Note that your procedures must return either an integer or a floating-point number, and the arguments will always be either integers or floating-point numbers - this is due to certain restrictions in the Tcl library.


Major/Minor Commands

This zipped C source forms the baginnings of some code to do major/minor commands as outlined in a news article I wrote. This code is still very raw and untested - it does compile very cleanly with gcc but I've not actually then developed anything that uses these functions. There is no bundled documentation, and what is here is rather sparse and incomplete.

There are four interesting (public) commands and one interesting (public) structure in the code:

Majorminor_CreateCommand

Allows you to create one of these things. This is also when you specify the description of the subcommand and the flags that apply to the subcommand selection (usually 0 but sometimes TCL_EXACT.) Returns a token which allows you to add blocks of subcommands to it.

Majorminor_GetCommandToken

Allows you to look up the token corresponding to a particular command so you can add (remove) blocks of subcommands to (from) it.

Majorminor_AddSubcommandSet

Adds a block of subcommands to the command specified by the token.

Majorminor_RemoveSubcommandSet

Removes a block of subcommands from the command specified by the token. The block must have been added previously.

The blocks of subcommands are specified as arrays of MajorMinor_Subcommand structures. These specify the name of the subcommand, the function to handle calls to the subcommand (just like with a normal command,) some data to pass to that subcommand, and a function to call if the overall command is deleted (the function is not called if the block of subcommands is deleted first.) The end of the block of subcommands is specified by making the name field NULL. The block of subcommands is assumed to be persistent; do not deallocate it while it is installed in the command or Bad Things Will Happen.

No global data is used anywhere in this code, but only interpreter-local data, so it stands a chance of being thread-safe in the sense of much of Tcl.

Back to the main index.


Donal. K. Fellows, Computer Science, University of Manchester, U.K. / Tcl Core Team