Tcl_Main(3tcl)
๐ Tcl_Main(3tcl) ๐
okay bestie, so this is a page from the Tcl Library Procedures manual โ yes, the whole Tcl_Main family โ and it's literally the blueprint for how you build your own little tclsh-flavored baby. Let's get into it. ๐
โจ NAME โจ
Tcl_Main, Tcl_MainEx, Tcl_MainExW, Tcl_SetStartupScript, Tcl_GetStartupScript, Tcl_SetMainLoop โ main program, startup script, and event loop definition for Tcl-based applications ๐ฆ
๐ธ SYNOPSIS ๐ธ
#include <tcl.h>
Tcl_Main(argc, argv, appInitProc)
Tcl_MainEx(argc, charargv, appInitProc, interp)
Tcl_MainExW(argc, wideargv, appInitProc, interp)
Tcl_SetStartupScript(path, encoding)
Tcl_Obj *
Tcl_GetStartupScript(encodingPtr)
Tcl_SetMainLoop(mainLoopProc)
๐ ARGUMENTS ๐
Tcl_Size argc(in) โ how many things are chillin' inargv. the count, the vibe check number. ๐ขchar *argv[](in) โ array of strings holding your command-line arguments. heads up tho: on Windows, when you're using-DUNICODE, the parameter type switches up towchar_t *. ๐char *charargv[](in) โ same deal asargv, except it does not change type towchar_t. it stays char, no cap.char *wideargv[](in) โ also likeargv, but the type is alwayswchar_t. wide and proud. ๐Tcl_AppInitProc *appInitProc(in) โ the address of an application-specific init procedure. and like, the value here is usuallyTcl_AppInit. that's the icon, that's the moment. โจTcl_Obj *path(in) โ name of the file to use as the startup script, orNULL. ๐const char *encoding(in) โ encoding of the file to use as the startup script, orNULL. ๐บconst char **encodingPtr(out) โ if it's non-NULL, this is where we write a copy of the(const char *)pointing at the encoding name.Tcl_MainLoopProc *mainLoopProc(in) โ address of an application-specific event loop procedure. the loop-de-loop. ๐Tcl_Interp *interp(in) โ a Tcl interpreter that's already been created. you brought it, you own it. ๐ซถ
๐ DESCRIPTION ๐
So Tcl_Main can straight up serve as the main program for Tcl-based shell applications. and what's a "shell application" bestie? it's a program like tclsh or wish that supports both interactive interpretation of Tcl and evaluation of a script sitting in a file that got passed as a command line argument. two modes, one queen. ๐
Tcl_Main exists as a little convenience for developers of shell applications so they don't have to reproduce every single line of code for proper initialization of the Tcl library and interactive shell operation. it's the pre-built, no-assembly-required option. ๐ง
ngl tho โ other styles of embedding Tcl in an application are NOT supported byTcl_Main. if that's your situation, you have to achieve it by calling lower level functions in the Tcl library directly.Tcl_Mainis not the girl for that job. ๐
The Tcl_Main function has been offered by the Tcl library since release Tcl 7.4. in older releases of Tcl, the Tcl library itself defined a function main, but that lacks flexibility of embedding style, and having a function main in a library (particularly a shared library) causes problems on many systems. having main in the Tcl library would also make it hard to use Tcl in C++ programs, since C++ programs must have special C++ main functions. so yeah, we retired that era. periodt. ๐
Normally each shell application contains a small main function that does nothing but invoke Tcl_Main. Tcl_Main then does all the work of creating and running a tclsh-like application. it's giving "you do the bare minimum, I do the rest." ๐
okay important plot twist: Tcl_Main is not provided by the public interface of Tcl's stub library. Programs that call Tcl_Main must be linked against the standard Tcl library. If the standard Tcl library is a dll (so, not a static .lib/.a), then the program must be linked against the stub library as well. And extensions (stub-enabled or not) are not intended to call Tcl_Main. not for them. ๐
also: Tcl_Main is not thread-safe. it should only be called by a single main thread of a multi-threaded application. the good news is this restriction is not a problem with normal use described above, so like, breathe. ๐ฎโ๐จ
Tcl_Main โ and therefore all applications based upon it, like tclsh โ use Tcl_GetStdChannel to initialize the standard channels to their default values. See Tcl_StandardChannels for more info. ๐
Tcl_Main supports two modes of operation, depending on whether the filename and encoding of a startup script has been established. The routines Tcl_SetStartupScript and Tcl_GetStartupScript are the tools for controlling this configuration of Tcl_Main. ๐ ๏ธ
๐ the startup script situation ๐
Tcl_SetStartupScript registers the value path as the name of the file for Tcl_Main to evaluate as its startup script. The value encoding is Tcl's name for the encoding used to store the text in that file. A value of NULL for encoding is a signal to use the system encoding. A value of NULL for path erases any existing registration so that Tcl_Main will not evaluate any startup script. ๐งน
Tcl_GetStartupScript queries the registered file name and encoding set by the most recent Tcl_SetStartupScript call in the same thread. The stored file name is returned, and the stored encoding name is written to space pointed to by encodingPtr, when that is not NULL. ๐ฏ
The file name and encoding values managed by the routines Tcl_SetStartupScript and Tcl_GetStartupScript are stored per-thread. Although the storage and retrieval functions of these routines work in any thread, only those calls in the same main thread as Tcl_Main can have any influence on it. so like, wrong thread = no influence. iykyk. ๐งต
๐ฆ how the main character flow actually goes ๐ฆ
The caller of Tcl_Main may call Tcl_SetStartupScript first to establish its desired startup script. If Tcl_Main finds that no such startup script has been established, it consults the first few arguments in argv. If they match ?-encoding name? fileName, where fileName does not begin with the character -, then fileName is taken to be the name of a file containing a startup script, and name is taken to be the name of the encoding of the contents of that file. Tcl_Main then calls Tcl_SetStartupScript with these values. ๐
Tcl_Main then defines in its main interpreter the Tcl variables argc, argv, argv0, and tcl_interactive, as described in the documentation for tclsh. ๐
When it has finished its own initialization, but before it processes commands, Tcl_Main calls the procedure given by the appInitProc argument. This procedure provides a "hook" for the application to perform its own initialization of the interpreter created by Tcl_Main, such as defining application-specific commands. The application initialization routine might also call Tcl_SetStartupScript to (re-)set the file and encoding to be used as a startup script. The procedure must have an interface that matches the type Tcl_AppInitProc:
typedef int Tcl_AppInitProc(
Tcl_Interp *interp);
AppInitProc is almost always a pointer to Tcl_AppInit; for more details on this procedure, see the documentation for Tcl_AppInit. ๐ซ
When the appInitProc is finished, Tcl_Main calls Tcl_GetStartupScript to determine what startup script has been requested, if any. If a startup script has been provided, Tcl_Main attempts to evaluate it. Otherwise, interactive mode begins with examination of the variable tcl_rcFileName in the main interpreter. If that variable exists and holds the name of a readable file, the contents of that file are evaluated in the main interpreter. Then interactive operations begin, with prompts and command evaluation results written to the standard output channel, and commands read from the standard input channel and then evaluated. The prompts written to the standard output channel may be customized by defining the Tcl variables tcl_prompt1 and tcl_prompt2 as described in the documentation for tclsh. The prompts and command evaluation results are written to the standard output channel only if the Tcl variable tcl_interactive in the main interpreter holds a non-zero integer value. โจ
๐ Tcl_SetMainLoop ๐
Tcl_SetMainLoop allows setting an event loop procedure to be run. This allows, for example, Tk to be dynamically loaded and set its event loop. The event loop will run following the startup script. If you are in interactive mode, setting the main loop procedure will cause the prompt to become fileevent based and then the loop procedure is called. When the loop procedure returns in interactive mode, interactive operation will continue. The main loop procedure must have an interface that matches the type Tcl_MainLoopProc:
typedef void Tcl_MainLoopProc(void);
๐ the ending (it doesn't return, bestie) ๐
Tcl_Main does not return. Normally a program based on Tcl_Main will terminate when the exit command is evaluated. In interactive mode, if an EOF or channel error is encountered on the standard input channel, then Tcl_Main itself will evaluate the exit command after the main loop procedure (if any) returns. In non-interactive mode, after Tcl_Main evaluates the startup script, and the main loop procedure (if any) returns, Tcl_Main will also evaluate the exit command. ๐ช
Tcl_Main can not be used in stub-enabled extensions.
The difference between Tcl_MainEx and Tcl_MainExW is that the arguments are passed as characters or wide characters. When used in stub-enabled embedders, the stubs table must be first initialized using one of Tcl_InitSubsystems, Tcl_SetPanicProc, Tcl_FindExecutable or TclZipfs_AppHook. ๐ง
๐ REFERENCE COUNT MANAGEMENT ๐
Tcl_SetStartupScripttakes a value (orNULL) for itspathargument, and will increment the reference count of it. ๐Tcl_GetStartupScriptreturns a value with reference count at least 1, orNULL. ItsencodingPtris also used (if non-NULL) to return a value with a reference count at least 1, orNULL. In both cases, the owner of the values is the current thread. ๐งต
๐ SEE ALSO ๐
tclsh(1), Tcl_GetStdChannel(3tcl), Tcl_StandardChannels(3tcl), Tcl_AppInit(3tcl), exit(3tcl), encoding(3tcl)
โญ KEYWORDS โญ
application-specific initialization, command-line arguments, main program
and that's the whole page bestie, we ate and left absolutely zero crumbs ๐ โจ
Tcl 9.0 ยท Tcl_Main(3tcl) โ mwah, go build something iconic ๐ซถ๐๐
๐ฆ where this page comes from
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2000 Ajuba Solutions
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms