Debian Codegraph @

doc/Tcl_Main.3

raw
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 .TH Tcl_Main 3 8.4 Tcl "Tcl Library Procedures"
10 .so man.macros
11 .BS
12 .SH NAME
13 Tcl_Main, Tcl_MainEx, Tcl_MainExW, Tcl_SetStartupScript, Tcl_GetStartupScript, Tcl_SetMainLoop \- main program, startup script, and event loop definition for Tcl-based applications
14 .SH SYNOPSIS
15 .nf
16 \fB#include <tcl.h>\fR
17 .sp
18 \fBTcl_Main\fR(\fIargc, argv, appInitProc\fR)
19 .sp
20 \fBTcl_MainEx\fR(\fIargc, charargv, appInitProc, interp\fR)
21 .sp
22 \fBTcl_MainExW\fR(\fIargc, wideargv, appInitProc, interp\fR)
23 .sp
24 \fBTcl_SetStartupScript\fR(\fIpath, encoding\fR)
25 .sp
26 Tcl_Obj *
27 \fBTcl_GetStartupScript\fR(\fIencodingPtr\fR)
28 .sp
29 \fBTcl_SetMainLoop\fR(\fImainLoopProc\fR)
30 .SH ARGUMENTS
31 .AS Tcl_MainLoopProc *mainLoopProc
32 .AP int argc in
33 Number of elements in \fIargv\fR.
34 .AP char *argv[] in
35 Array of strings containing command-line arguments. On Windows, when
36 using -DUNICODE, the parameter type changes to wchar_t *.
37 .AP char *charargv[] in
38 As argv, but does not change type to wchar_t.
39 .AP char *wideargv[] in
40 As argv, but type is always wchar_t.
41 .AP Tcl_AppInitProc *appInitProc in
42 Address of an application-specific initialization procedure.
43 The value for this argument is usually \fBTcl_AppInit\fR.
44 .AP Tcl_Obj *path in
45 Name of file to use as startup script, or NULL.
46 .AP "const char" *encoding in
47 Encoding of file to use as startup script, or NULL.
48 .AP "const char" **encodingPtr out
49 If non-NULL, location to write a copy of the (const char *)
50 pointing to the encoding name.
51 .AP Tcl_MainLoopProc *mainLoopProc in
52 Address of an application-specific event loop procedure.
53 .AP Tcl_Interp *interp in
54 Already created Tcl Interpreter.
55 .BE
56 .SH DESCRIPTION
57 .PP
58 \fBTcl_Main\fR can serve as the main program for Tcl-based shell
59 applications. A
60 .QW "shell application"
61 is a program
62 like tclsh or wish that supports both interactive interpretation
63 of Tcl and evaluation of a script contained in a file given as
64 a command line argument. \fBTcl_Main\fR is offered as a convenience
65 to developers of shell applications, so they do not have to
66 reproduce all of the code for proper initialization of the Tcl
67 library and interactive shell operation. Other styles of embedding
68 Tcl in an application are not supported by \fBTcl_Main\fR. Those
69 must be achieved by calling lower level functions in the Tcl library
70 directly.
71 .PP
72 The \fBTcl_Main\fR function has been offered by the Tcl library
73 since release Tcl 7.4. In older releases of Tcl, the Tcl library
74 itself defined a function \fBmain\fR, but that lacks flexibility
75 of embedding style and having a function \fBmain\fR in a library
76 (particularly a shared library) causes problems on many systems.
77 Having \fBmain\fR in the Tcl library would also make it hard to use
78 Tcl in C++ programs, since C++ programs must have special C++
79 \fBmain\fR functions.
80 .PP
81 Normally each shell application contains a small \fBmain\fR function
82 that does nothing but invoke \fBTcl_Main\fR.
83 \fBTcl_Main\fR then does all the work of creating and running a
84 \fBtclsh\fR-like application.
85 .PP
86 \fBTcl_Main\fR is not provided by the public interface of Tcl's
87 stub library. Programs that call \fBTcl_Main\fR must be linked
88 against the standard Tcl library. Extensions (stub-enabled or
89 not) are not intended to call \fBTcl_Main\fR.
90 .PP
91 \fBTcl_Main\fR is not thread-safe. It should only be called by
92 a single main thread of a multi-threaded application. This
93 restriction is not a problem with normal use described above.
94 .PP
95 \fBTcl_Main\fR and therefore all applications based upon it, like
96 \fBtclsh\fR, use \fBTcl_GetStdChannel\fR to initialize the standard
97 channels to their default values. See \fBTcl_StandardChannels\fR for
98 more information.
99 .PP
100 \fBTcl_Main\fR supports two modes of operation, depending on
101 whether the filename and encoding of a startup script has been
102 established. The routines \fBTcl_SetStartupScript\fR and
103 \fBTcl_GetStartupScript\fR are the tools for controlling this
104 configuration of \fBTcl_Main\fR.
105 .PP
106 \fBTcl_SetStartupScript\fR registers the value \fIpath\fR
107 as the name of the file for \fBTcl_Main\fR to evaluate as
108 its startup script. The value \fIencoding\fR is Tcl's name
109 for the encoding used to store the text in that file. A
110 value of \fBNULL\fR for \fIencoding\fR is a signal to use
111 the system encoding. A value of \fBNULL\fR for \fIpath\fR
112 erases any existing registration so that \fBTcl_Main\fR
113 will not evaluate any startup script.
114 .PP
115 \fBTcl_GetStartupScript\fR queries the registered file name
116 and encoding set by the most recent \fBTcl_SetStartupScript\fR
117 call in the same thread. The stored file name is returned,
118 and the stored encoding name is written to space pointed to
119 by \fIencodingPtr\fR, when that is not NULL.
120 .PP
121 The file name and encoding values managed by the routines
122 \fBTcl_SetStartupScript\fR and \fBTcl_GetStartupScript\fR
123 are stored per-thread. Although the storage and retrieval
124 functions of these routines work in any thread, only those
125 calls in the same main thread as \fBTcl_Main\fR can have
126 any influence on it.
127 .PP
128 The caller of \fBTcl_Main\fR may call \fBTcl_SetStartupScript\fR
129 first to establish its desired startup script. If \fBTcl_Main\fR
130 finds that no such startup script has been established, it consults
131 the first few arguments in \fIargv\fR. If they match
132 ?\fB\-encoding \fIname\fR? \fIfileName\fR,
133 where \fIfileName\fR does not begin with the character \fI\-\fR,
134 then \fIfileName\fR is taken to be the name of a file containing
135 a \fIstartup script\fR, and \fIname\fR is taken to be the name
136 of the encoding of the contents of that file. \fBTcl_Main\fR
137 then calls \fBTcl_SetStartupScript\fR with these values.
138 .PP
139 \fBTcl_Main\fR then defines in its main interpreter
140 the Tcl variables \fIargc\fR, \fIargv\fR, \fIargv0\fR, and
141 \fItcl_interactive\fR, as described in the documentation for \fBtclsh\fR.
142 .PP
143 When it has finished its own initialization, but before it processes
144 commands, \fBTcl_Main\fR calls the procedure given by the
145 \fIappInitProc\fR argument. This procedure provides a
146 .QW hook
147 for the application to perform its own initialization of the interpreter
148 created by \fBTcl_Main\fR, such as defining application-specific
149 commands. The application initialization routine might also
150 call \fBTcl_SetStartupScript\fR to (re-)set the file and encoding
151 to be used as a startup script. The procedure must have an interface
152 that matches the type \fBTcl_AppInitProc\fR:
153 .PP
154 .CS
155 typedef int \fBTcl_AppInitProc\fR(
156 Tcl_Interp *\fIinterp\fR);
157 .CE
158 .PP
159 \fIAppInitProc\fR is almost always a pointer to \fBTcl_AppInit\fR; for more
160 details on this procedure, see the documentation for \fBTcl_AppInit\fR.
161 .PP
162 When the \fIappInitProc\fR is finished, \fBTcl_Main\fR calls
163 \fBTcl_GetStartupScript\fR to determine what startup script has
164 been requested, if any. If a startup script has been provided,
165 \fBTcl_Main\fR attempts to evaluate it. Otherwise, interactive
166 mode begins with examination of the variable \fItcl_rcFileName\fR
167 in the main interpreter. If that variable exists and holds the
168 name of a readable file, the contents of that file are evaluated
169 in the main interpreter. Then interactive operations begin,
170 with prompts and command evaluation results written to the standard
171 output channel, and commands read from the standard input channel
172 and then evaluated. The prompts written to the standard output
173 channel may be customized by defining the Tcl variables \fItcl_prompt1\fR
174 and \fItcl_prompt2\fR as described in the documentation for \fBtclsh\fR.
175 The prompts and command evaluation results are written to the standard
176 output channel only if the Tcl variable \fItcl_interactive\fR in the
177 main interpreter holds a non-zero integer value.
178 .PP
179 \fBTcl_SetMainLoop\fR allows setting an event loop procedure to be run.
180 This allows, for example, Tk to be dynamically loaded and set its event
181 loop. The event loop will run following the startup script. If you
182 are in interactive mode, setting the main loop procedure will cause the
183 prompt to become fileevent based and then the loop procedure is called.
184 When the loop procedure returns in interactive mode, interactive operation
185 will continue.
186 The main loop procedure must have an interface that matches the type
187 \fBTcl_MainLoopProc\fR:
188 .PP
189 .CS
190 typedef void \fBTcl_MainLoopProc\fR(void);
191 .CE
192 .PP
193 \fBTcl_Main\fR does not return. Normally a program based on
194 \fBTcl_Main\fR will terminate when the \fBexit\fR command is
195 evaluated. In interactive mode, if an EOF or channel error
196 is encountered on the standard input channel, then \fBTcl_Main\fR
197 itself will evaluate the \fBexit\fR command after the main loop
198 procedure (if any) returns. In non-interactive mode, after
199 \fBTcl_Main\fR evaluates the startup script, and the main loop
200 procedure (if any) returns, \fBTcl_Main\fR will also evaluate
201 the \fBexit\fR command.
202 .SH "SEE ALSO"
203 tclsh(1), Tcl_GetStdChannel(3), Tcl_StandardChannels(3), Tcl_AppInit(3),
204 exit(n), encoding(n)
205 .SH KEYWORDS
206 application-specific initialization, command-line arguments, main program