Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/Tcl_Main.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 6.6 KB
Line 
1'\"
2'\" Copyright (c) 1994 The Regents of the University of California.
3'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4'\" Copyright (c) 2000 Ajuba Solutions.
5'\"
6'\" See the file "license.terms" for information on usage and redistribution
7'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8'\"
9'\" RCS: @(#) $Id: Tcl_Main.3,v 1.16 2007/12/13 15:22:32 dgp Exp $
10'\"
11.so man.macros
12.TH Tcl_Main 3 8.4 Tcl "Tcl Library Procedures"
13.BS
14.SH NAME
15Tcl_Main, Tcl_SetMainLoop \- main program and event loop definition for Tcl-based applications
16.SH SYNOPSIS
17.nf
18\fB#include <tcl.h>\fR
19.sp
20\fBTcl_Main\fR(\fIargc, argv, appInitProc\fR)
21.sp
22\fBTcl_SetMainLoop\fR(\fImainLoopProc\fR)
23.SH ARGUMENTS
24.AS Tcl_MainLoopProc *mainLoopProc
25.AP int argc in
26Number of elements in \fIargv\fR.
27.AP char *argv[] in
28Array of strings containing command-line arguments.
29.AP Tcl_AppInitProc *appInitProc in
30Address of an application-specific initialization procedure.
31The value for this argument is usually \fBTcl_AppInit\fR.
32.AP Tcl_MainLoopProc *mainLoopProc in
33Address of an application-specific event loop procedure.
34.BE
35
36.SH DESCRIPTION
37.PP
38\fBTcl_Main\fR can serve as the main program for Tcl-based shell
39applications.  A
40.QW "shell application"
41is a program
42like tclsh or wish that supports both interactive interpretation
43of Tcl and evaluation of a script contained in a file given as
44a command line argument.  \fBTcl_Main\fR is offered as a convenience
45to developers of shell applications, so they do not have to
46reproduce all of the code for proper initialization of the Tcl
47library and interactive shell operation.  Other styles of embedding
48Tcl in an application are not supported by \fBTcl_Main\fR.  Those
49must be achieved by calling lower level functions in the Tcl library
50directly.
51
52The \fBTcl_Main\fR function has been offered by the Tcl library
53since release Tcl 7.4.  In older releases of Tcl, the Tcl library
54itself defined a function \fBmain\fR, but that lacks flexibility
55of embedding style and having a function \fBmain\fR in a library
56(particularly a shared library) causes problems on many systems.
57Having \fBmain\fR in the Tcl library would also make it hard to use
58Tcl in C++ programs, since C++ programs must have special C++
59\fBmain\fR functions.
60.PP
61Normally each shell application contains a small \fBmain\fR function
62that does nothing but invoke \fBTcl_Main\fR.
63\fBTcl_Main\fR then does all the work of creating and running a
64\fBtclsh\fR-like application.
65.PP
66\fBTcl_Main\fR is not provided by the public interface of Tcl's
67stub library.  Programs that call \fBTcl_Main\fR must be linked
68against the standard Tcl library.  Extensions (stub-enabled or
69not) are not intended to call \fBTcl_Main\fR.
70.PP
71\fBTcl_Main\fR is not thread-safe.  It should only be called by
72a single master thread of a multi-threaded application.  This
73restriction is not a problem with normal use described above.
74.PP
75\fBTcl_Main\fR and therefore all applications based upon it, like
76\fBtclsh\fR, use \fBTcl_GetStdChannel\fR to initialize the standard
77channels to their default values. See \fBTcl_StandardChannels\fR for
78more information.
79.PP
80\fBTcl_Main\fR supports two modes of operation, depending on the
81values of \fIargc\fR and \fIargv\fR.  If the first few arguments
82in \fIargv\fR match ?\fB\-encoding \fIname\fR? ?\fIfileName\fR?,
83where \fIfileName\fR does not begin with the character \fI\-\fR,
84then \fIfileName\fR is taken to be the name of a file containing
85a \fIstartup script\fR, and \fIname\fR is taken to be the name
86of the encoding of the contents of that file, which \fBTcl_Main\fR
87will attempt to evaluate.  Otherwise, \fBTcl_Main\fR will enter an
88interactive mode.
89.PP
90In either mode, \fBTcl_Main\fR will define in its master interpreter
91the Tcl variables \fIargc\fR, \fIargv\fR, \fIargv0\fR, and
92\fItcl_interactive\fR, as described in the documentation for \fBtclsh\fR.
93.PP
94When it has finished its own initialization, but before it processes
95commands, \fBTcl_Main\fR calls the procedure given by the
96\fIappInitProc\fR argument.  This procedure provides a
97.QW hook
98for the application to perform its own initialization of the interpreter
99created by \fBTcl_Main\fR, such as defining application-specific
100commands.  The procedure must have an interface that matches the
101type \fBTcl_AppInitProc\fR:
102.CS
103typedef int Tcl_AppInitProc(Tcl_Interp *\fIinterp\fR);
104.CE
105
106\fIAppInitProc\fR is almost always a pointer to \fBTcl_AppInit\fR; for more
107details on this procedure, see the documentation for \fBTcl_AppInit\fR.
108.PP
109When the \fIappInitProc\fR is finished, \fBTcl_Main\fR enters one
110of its two modes.  If a startup script has been provided, \fBTcl_Main\fR
111attempts to evaluate it.  Otherwise, interactive mode begins with
112examination of the variable \fItcl_rcFileName\fR in the master
113interpreter.  If that variable exists and holds the name of a readable
114file, the contents of that file are evaluated in the master interpreter.
115Then interactive operations begin,
116with prompts and command evaluation results written to the standard
117output channel, and commands read from the standard input channel
118and then evaluated.  The prompts written to the standard output
119channel may be customized by defining the Tcl variables \fItcl_prompt1\fR
120and \fItcl_prompt2\fR as described in the documentation for \fBtclsh\fR.
121The prompts and command evaluation results are written to the standard
122output channel only if the Tcl variable \fItcl_interactive\fR in the
123master interpreter holds a non-zero integer value.
124.PP
125\fBTcl_SetMainLoop\fR allows setting an event loop procedure to be run.
126This allows, for example, Tk to be dynamically loaded and set its event
127loop.  The event loop will run following the startup script.  If you
128are in interactive mode, setting the main loop procedure will cause the
129prompt to become fileevent based and then the loop procedure is called.
130When the loop procedure returns in interactive mode, interactive operation
131will continue.
132The main loop procedure must have an interface that matches the type
133\fBTcl_MainLoopProc\fR:
134.CS
135typedef void Tcl_MainLoopProc(void);
136.CE
137.PP
138\fBTcl_Main\fR does not return.  Normally a program based on
139\fBTcl_Main\fR will terminate when the \fBexit\fR command is
140evaluated.  In interactive mode, if an EOF or channel error
141is encountered on the standard input channel, then \fBTcl_Main\fR
142itself will evaluate the \fBexit\fR command after the main loop
143procedure (if any) returns.  In non-interactive mode, after
144\fBTcl_Main\fR evaluates the startup script, and the main loop
145procedure (if any) returns, \fBTcl_Main\fR will also evaluate
146the \fBexit\fR command.
147
148.SH "SEE ALSO"
149tclsh(1), Tcl_GetStdChannel(3), Tcl_StandardChannels(3), Tcl_AppInit(3),
150exit(n)
151
152.SH KEYWORDS
153application-specific initialization, command-line arguments, main program
Note: See TracBrowser for help on using the repository browser.