Debian Codegraph @

pTk/mTk/generic/tkMain.c

raw symbols SCIP
1 /*
2 * tkMain.c --
3 *
4 * This file contains a generic main program for Tk-based applications.
5 * It can be used as-is for many applications, just by supplying a
6 * different appInitProc procedure for each specific application.
7 * Or, it can be used as a template for creating new main programs
8 * for Tk applications.
9 *
10 * Copyright (c) 1990-1994 The Regents of the University of California.
11 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
12 *
13 * See the file "license.terms" for information on usage and redistribution
14 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 *
16 * RCS: @(#) $Id: tkMain.c,v 1.15 2002/12/13 16:54:35 dgp Exp $
17 */
18
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <tcl.h>
23 #include <tclInt.h>
24 #include <tk.h>
25 #include "tkInt.h"
26 #ifdef NO_STDLIB_H
27 # include "../compat/stdlib.h"
28 #else
29 # include <stdlib.h>
30 #endif
31 #ifdef __WIN32__
32 #include "tkWinInt.h"
33 #endif
34
35
36 typedef struct ThreadSpecificData {
37 Tcl_Interp *interp; /* Interpreter for this thread. */
38 Tcl_DString command; /* Used to assemble lines of terminal input
39 * into Tcl commands. */
40 Tcl_DString line; /* Used to read the next line from the
41 * terminal input. */
42 int tty; /* Non-zero means standard input is a
43 * terminal-like device. Zero means it's
44 * a file. */
45 } ThreadSpecificData;
46 static Tcl_ThreadDataKey dataKey;
47
48 /*
49 * Declarations for various library procedures and variables (don't want
50 * to include tkInt.h or tkPort.h here, because people might copy this
51 * file out of the Tk source directory to make their own modified versions).
52 * Note: don't declare "exit" here even though a declaration is really
53 * needed, because it will conflict with a declaration elsewhere on
54 * some systems.
55 */
56
57 #if !defined(__WIN32__) && !defined(_WIN32)
58 #if !defined(MAC_TCL)
59 extern int isatty _ANSI_ARGS_((int fd));
60 #else
61 #include <unistd.h>
62 #endif
63 extern char * strrchr _ANSI_ARGS_((CONST char *string, int c));
64 #endif
65
66 /*
67 * Forward declarations for procedures defined later in this file.
68 */
69
70 static void Prompt _ANSI_ARGS_((Tcl_Interp *interp, int partial));
71 static void StdinProc _ANSI_ARGS_((ClientData clientData,
72 int mask));
73
74 /*
75 *----------------------------------------------------------------------
76 *
77 * Tk_MainEx --
78 *
79 * Main program for Wish and most other Tk-based applications.
80 *
81 * Results:
82 * None. This procedure never returns (it exits the process when
83 * it's done.
84 *
85 * Side effects:
86 * This procedure initializes the Tk world and then starts
87 * interpreting commands; almost anything could happen, depending
88 * on the script being interpreted.
89 *
90 *----------------------------------------------------------------------
91 */
92 void
93 Tk_MainEx(argc, argv, appInitProc, interp)
94 int argc; /* Number of arguments. */
95 char **argv; /* Array of argument strings. */
96 Tcl_AppInitProc *appInitProc; /* Application-specific initialization
97 * procedure to call after most
98 * initialization but before starting
99 * to execute commands. */
100 Tcl_Interp *interp;
101 {
102 char *args;
103 char buf[TCL_INTEGER_SPACE];
104 int code;
105 size_t length;
106 Tcl_Channel inChannel, outChannel;
107 Tcl_DString argString;
108 ThreadSpecificData *tsdPtr;
109 #ifdef __WIN32__
110 HANDLE handle;
111 #endif
112
113 /*
114 * Ensure that we are getting the matching version of Tcl. This is
115 * really only an issue when Tk is loaded dynamically.
116 */
117
118 if (Tcl_InitStubs(interp, TCL_VERSION, 1) == NULL) {
119 abort();
120 }
121
122 tsdPtr = (ThreadSpecificData *)
123 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
124
125 Tcl_FindExecutable(argv[0]);
126 tsdPtr->interp = interp;
127
128 #if (defined(__WIN32__) || defined(MAC_TCL))
129 Tk_InitConsoleChannels(interp);
130 #endif
131
132 #ifdef TCL_MEM_DEBUG
133 Tcl_InitMemory(interp);
134 #endif
135
136 /*
137 * Parse command-line arguments. A leading "-file" argument is
138 * ignored (a historical relic from the distant past). If the
139 * next argument doesn't start with a "-" then strip it off and
140 * use it as the name of a script file to process.
141 */
142
143 if (argc > 1) {
144 length = strlen(argv[1]);
145 if ((length >= 2) && (strncmp(argv[1], "-file", length) == 0)) {
146 argc--;
147 argv++;
148 }
149 }
150 if (TclGetStartupScriptFileName() == NULL) {
151 if ((argc > 1) && (argv[1][0] != '-')) {
152 TclSetStartupScriptFileName(argv[1]);
153 argc--;
154 argv++;
155 }
156 }
157
158 /*
159 * Make command-line arguments available in the Tcl variables "argc"
160 * and "argv".
161 */
162
163 args = Tcl_Merge(argc-1, (CONST char **)argv+1);
164 Tcl_ExternalToUtfDString(NULL, args, -1, &argString);
165 Tcl_SetVar(interp, "argv", Tcl_DStringValue(&argString), TCL_GLOBAL_ONLY);
166 Tcl_DStringFree(&argString);
167 ckfree(args);
168 sprintf(buf, "%d", argc-1);
169
170 if (TclGetStartupScriptFileName() == NULL) {
171 Tcl_ExternalToUtfDString(NULL, argv[0], -1, &argString);
172 } else {
173 TclSetStartupScriptFileName(Tcl_ExternalToUtfDString(NULL,
174 TclGetStartupScriptFileName(), -1, &argString));
175 }
176 Tcl_SetVar(interp, "argc", buf, TCL_GLOBAL_ONLY);
177 Tcl_SetVar(interp, "argv0", Tcl_DStringValue(&argString), TCL_GLOBAL_ONLY);
178
179 /*
180 * Set the "tcl_interactive" variable.
181 */
182
183 /*
184 * For now, under Windows, we assume we are not running as a console mode
185 * app, so we need to use the GUI console. In order to enable this, we
186 * always claim to be running on a tty. This probably isn't the right
187 * way to do it.
188 */
189
190 #ifdef __WIN32__
191 handle = GetStdHandle(STD_INPUT_HANDLE);
192
193 if ((handle == INVALID_HANDLE_VALUE) || (handle == 0)
194 || (GetFileType(handle) == FILE_TYPE_UNKNOWN)) {
195 /*
196 * If it's a bad or closed handle, then it's been connected
197 * to a wish console window.
198 */
199
200 tsdPtr->tty = 1;
201 } else if (GetFileType(handle) == FILE_TYPE_CHAR) {
202 /*
203 * A character file handle is a tty by definition.
204 */
205
206 tsdPtr->tty = 1;
207 } else {
208 tsdPtr->tty = 0;
209 }
210
211 #else
212 tsdPtr->tty = isatty(0);
213 #endif
214 Tcl_SetVar(interp, "tcl_interactive",
215 ((TclGetStartupScriptFileName() == NULL)
216 && tsdPtr->tty) ? "1" : "0", TCL_GLOBAL_ONLY);
217
218 /*
219 * Invoke application-specific initialization.
220 */
221
222 if ((*appInitProc)(interp) != TCL_OK) {
223 TkpDisplayWarning(Tcl_GetStringResult(interp),
224 "Application initialization failed");
225 }
226
227 /*
228 * Invoke the script specified on the command line, if any.
229 */
230
231 if (TclGetStartupScriptFileName() != NULL) {
232 Tcl_ResetResult(interp);
233 code = Tcl_EvalFile(interp, TclGetStartupScriptFileName());
234 if (code != TCL_OK) {
235 /*
236 * The following statement guarantees that the errorInfo
237 * variable is set properly.
238 */
239
240 Tcl_AddErrorInfo(interp, "");
241 TkpDisplayWarning(Tcl_GetVar(interp, "errorInfo",
242 TCL_GLOBAL_ONLY), "Error in startup script");
243 Tcl_DeleteInterp(interp);
244 Tcl_Exit(1);
245 }
246 tsdPtr->tty = 0;
247 } else {
248
249 /*
250 * Evaluate the .rc file, if one has been specified.
251 */
252
253 Tcl_SourceRCFile(interp);
254
255 /*
256 * Establish a channel handler for stdin.
257 */
258
259 inChannel = Tcl_GetStdChannel(TCL_STDIN);
260 if (inChannel) {
261 Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc,
262 (ClientData) inChannel);
263 }
264 if (tsdPtr->tty) {
265 Prompt(interp, 0);
266 }
267 }
268 Tcl_DStringFree(&argString);
269
270 outChannel = Tcl_GetStdChannel(TCL_STDOUT);
271 if (outChannel) {
272 Tcl_Flush(outChannel);
273 }
274 Tcl_DStringInit(&tsdPtr->command);
275 Tcl_DStringInit(&tsdPtr->line);
276 Tcl_ResetResult(interp);
277
278 /*
279 * Loop infinitely, waiting for commands to execute. When there
280 * are no windows left, Tk_MainLoop returns and we exit.
281 */
282
283 Tk_MainLoop();
284 Tcl_DeleteInterp(interp);
285 Tcl_Exit(0);
286 }
287
288 /*
289 *----------------------------------------------------------------------
290 *
291 * StdinProc --
292 *
293 * This procedure is invoked by the event dispatcher whenever
294 * standard input becomes readable. It grabs the next line of
295 * input characters, adds them to a command being assembled, and
296 * executes the command if it's complete.
297 *
298 * Results:
299 * None.
300 *
301 * Side effects:
302 * Could be almost arbitrary, depending on the command that's
303 * typed.
304 *
305 *----------------------------------------------------------------------
306 */
307
308 /* ARGSUSED */
309 static void
310 StdinProc(clientData, mask)
311 ClientData clientData; /* Not used. */
312 int mask; /* Not used. */
313 {
314 static int gotPartial = 0;
315 char *cmd;
316 int code, count;
317 Tcl_Channel chan = (Tcl_Channel) clientData;
318 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
319 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
320 Tcl_Interp *interp = tsdPtr->interp;
321
322 count = Tcl_Gets(chan, &tsdPtr->line);
323
324 if (count < 0) {
325 if (!gotPartial) {
326 if (tsdPtr->tty) {
327 Tcl_Exit(0);
328 } else {
329 Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan);
330 }
331 return;
332 }
333 }
334
335 (void) Tcl_DStringAppend(&tsdPtr->command, Tcl_DStringValue(
336 &tsdPtr->line), -1);
337 cmd = Tcl_DStringAppend(&tsdPtr->command, "\n", -1);
338 Tcl_DStringFree(&tsdPtr->line);
339 if (!Tcl_CommandComplete(cmd)) {
340 gotPartial = 1;
341 goto prompt;
342 }
343 gotPartial = 0;
344
345 /*
346 * Disable the stdin channel handler while evaluating the command;
347 * otherwise if the command re-enters the event loop we might
348 * process commands from stdin before the current command is
349 * finished. Among other things, this will trash the text of the
350 * command being evaluated.
351 */
352
353 Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan);
354 code = Tcl_RecordAndEval(interp, cmd, TCL_EVAL_GLOBAL);
355
356 chan = Tcl_GetStdChannel(TCL_STDIN);
357 if (chan) {
358 Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc,
359 (ClientData) chan);
360 }
361 Tcl_DStringFree(&tsdPtr->command);
362 if (Tcl_GetStringResult(interp)[0] != '\0') {
363 if ((code != TCL_OK) || (tsdPtr->tty)) {
364 chan = Tcl_GetStdChannel(TCL_STDOUT);
365 if (chan) {
366 Tcl_WriteObj(chan, Tcl_GetObjResult(interp));
367 Tcl_WriteChars(chan, "\n", 1);
368 }
369 }
370 }
371
372 /*
373 * Output a prompt.
374 */
375
376 prompt:
377 if (tsdPtr->tty) {
378 Prompt(interp, gotPartial);
379 }
380 Tcl_ResetResult(interp);
381 }
382
383 /*
384 *----------------------------------------------------------------------
385 *
386 * Prompt --
387 *
388 * Issue a prompt on standard output, or invoke a script
389 * to issue the prompt.
390 *
391 * Results:
392 * None.
393 *
394 * Side effects:
395 * A prompt gets output, and a Tcl script may be evaluated
396 * in interp.
397 *
398 *----------------------------------------------------------------------
399 */
400
401 static void
402 Prompt(interp, partial)
403 Tcl_Interp *interp; /* Interpreter to use for prompting. */
404 int partial; /* Non-zero means there already
405 * exists a partial command, so use
406 * the secondary prompt. */
407 {
408 Tcl_Obj *promptCmd;
409 int code;
410 Tcl_Channel outChannel, errChannel;
411
412 promptCmd = Tcl_GetVar2Ex(interp,
413 partial ? "tcl_prompt2" : "tcl_prompt1", NULL, TCL_GLOBAL_ONLY);
414 if (promptCmd == NULL) {
415 defaultPrompt:
416 if (!partial) {
417
418 /*
419 * We must check that outChannel is a real channel - it
420 * is possible that someone has transferred stdout out of
421 * this interpreter with "interp transfer".
422 */
423
424 outChannel = Tcl_GetChannel(interp, "stdout", NULL);
425 if (outChannel != (Tcl_Channel) NULL) {
426 Tcl_WriteChars(outChannel, "% ", 2);
427 }
428 }
429 } else {
430 code = Tcl_EvalObjEx(interp, promptCmd, TCL_EVAL_GLOBAL);
431 if (code != TCL_OK) {
432 Tcl_AddErrorInfo(interp,
433 "\n (script that generates prompt)");
434 /*
435 * We must check that errChannel is a real channel - it
436 * is possible that someone has transferred stderr out of
437 * this interpreter with "interp transfer".
438 */
439
440 errChannel = Tcl_GetChannel(interp, "stderr", NULL);
441 if (errChannel != (Tcl_Channel) NULL) {
442 Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp));
443 Tcl_WriteChars(errChannel, "\n", 1);
444 }
445 goto defaultPrompt;
446 }
447 }
448 outChannel = Tcl_GetChannel(interp, "stdout", NULL);
449 if (outChannel != (Tcl_Channel) NULL) {
450 Tcl_Flush(outChannel);
451 }
452 }