Debian Codegraph @

generic/bltWinMain.c

raw symbols SCIP
1
2 /*
3 * bltWinMain.c --
4 *
5 * Main entry point for wish and other Tk-based applications.
6 *
7 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * SCCS: @(#) winMain.c 1.37 98/01/20 22:47:06
13 */
14
15 #include "bltInt.h"
16 #include <locale.h>
17
18 /*
19 * Forward declarations for procedures defined later in this file:
20 */
21
22 static void setargv _ANSI_ARGS_((int *argcPtr, char ***argvPtr));
23
24 #if (TCL_VERSION_NUMBER >= _VERSION(8,2,0))
25 static BOOL consoleRequired = TRUE;
26 #endif
27
28 extern EXPORT int Blt_Init(Tcl_Interp *interp);
29 extern EXPORT int Blt_SafeInit(Tcl_Interp *interp);
30 static Tcl_AppInitProc AppInit;
31
32
33 /*
34 *-------------------------------------------------------------------------
35 *
36 * setargv --
37 *
38 * Parse the Windows command line string into argc/argv. Done here
39 * because we don't trust the builtin argument parser in crt0.
40 * Windows applications are responsible for breaking their command
41 * line into arguments.
42 *
43 * 2N backslashes + quote -> N backslashes + begin quoted string
44 * 2N + 1 backslashes + quote -> literal
45 * N backslashes + non-quote -> literal
46 * quote + quote in a quoted string -> single quote
47 * quote + quote not in quoted string -> empty string
48 * quote -> begin quoted string
49 *
50 * Results:
51 * Fills argcPtr with the number of arguments and argvPtr with the
52 * array of arguments.
53 *
54 * Side effects:
55 * Memory allocated.
56 *
57 *--------------------------------------------------------------------------
58 */
59
60 static void
61 setargv(
62 int *argcPtr, /* Filled with number of argument strings. */
63 char ***argvPtr)
64 { /* Filled with argument strings (malloc'd). */
65 char *cmdLine, *p, *arg, *argSpace;
66 char **argv;
67 int argc, size, inquote, copy, slashes;
68
69 cmdLine = GetCommandLine(); /* INTL: BUG */
70
71 /*
72 * Precompute an overly pessimistic guess at the number of arguments
73 * in the command line by counting non-space spans.
74 */
75
76 size = 2;
77 for (p = cmdLine; *p != '\0'; p++) {
78 if ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
79 size++;
80 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
81 p++;
82 }
83 if (*p == '\0') {
84 break;
85 }
86 }
87 }
88 argSpace = (char *)Tcl_Alloc(
89 (unsigned)(size * sizeof(char *) + strlen(cmdLine) + 1));
90 argv = (char **)argSpace;
91 argSpace += size * sizeof(char *);
92 size--;
93
94 p = cmdLine;
95 for (argc = 0; argc < size; argc++) {
96 argv[argc] = arg = argSpace;
97 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
98 p++;
99 }
100 if (*p == '\0') {
101 break;
102 }
103 inquote = 0;
104 slashes = 0;
105 while (1) {
106 copy = 1;
107 while (*p == '\\') {
108 slashes++;
109 p++;
110 }
111 if (*p == '"') {
112 if ((slashes & 1) == 0) {
113 copy = 0;
114 if ((inquote) && (p[1] == '"')) {
115 p++;
116 copy = 1;
117 } else {
118 inquote = !inquote;
119 }
120 }
121 slashes >>= 1;
122 }
123 while (slashes) {
124 *arg = '\\';
125 arg++;
126 slashes--;
127 }
128
129 if ((*p == '\0') || (!inquote && ((*p == ' ') ||
130 (*p == '\t')))) { /* INTL: ISO space. */
131 break;
132 }
133 if (copy != 0) {
134 *arg = *p;
135 arg++;
136 }
137 p++;
138 }
139 *arg = '\0';
140 argSpace = arg + 1;
141 }
142 argv[argc] = NULL;
143
144 *argcPtr = argc;
145 *argvPtr = argv;
146 }
147
148
149 #ifdef TCL_ONLY
150
151 /*
152 *----------------------------------------------------------------------
153 *
154 * main --
155 *
156 * This is the main program for the application.
157 *
158 * Results:
159 * None: Tcl_Main never returns here, so this procedure never
160 * returns either.
161 *
162 * Side effects:
163 * Whatever the application does.
164 *
165 *----------------------------------------------------------------------
166 */
167 int
168 main(argc, argv)
169 int argc; /* Number of command-line arguments. */
170 char **argv; /* Values of command-line arguments. */
171 {
172 char buffer[MAX_PATH +1];
173 char *p;
174 /*
175 * Set up the default locale to be standard "C" locale so parsing
176 * is performed correctly.
177 */
178
179 setlocale(LC_ALL, "C");
180 setargv(&argc, &argv);
181
182 /*
183 * Replace argv[0] with full pathname of executable, and forward
184 * slashes substituted for backslashes.
185 */
186
187 GetModuleFileName(NULL, buffer, sizeof(buffer));
188 argv[0] = buffer;
189 for (p = buffer; *p != '\0'; p++) {
190 if (*p == '\\') {
191 *p = '/';
192 }
193 }
194 Tcl_Main(argc, argv, AppInit);
195 return 0; /* Needed only to prevent compiler warning. */
196 }
197
198 #else /* TCL_ONLY */
199
200 #if (TK_VERSION_NUMBER < _VERSION(8,2,0))
201 /*
202 * The following declarations refer to internal Tk routines. These
203 * interfaces are available for use, but are not supported.
204 */
205 extern void TkConsoleCreate(void);
206 extern int TkConsoleInit(Tcl_Interp *interp);
207
208 #endif /* TK_VERSION_NUMBER < 8.2.0 */
209
210
211 /*
212 *----------------------------------------------------------------------
213 *
214 * WishPanic --
215 *
216 * Display a message and exit.
217 *
218 * Results:
219 * None.
220 *
221 * Side effects:
222 * Exits the program.
223 *
224 *----------------------------------------------------------------------
225 */
226
227 void
228 WishPanic
229 TCL_VARARGS_DEF(char *, arg1)
230 {
231 va_list argList;
232 char buf[1024];
233 CONST char *format;
234
235 format = TCL_VARARGS_START(char *, arg1, argList);
236 vsprintf(buf, format, argList);
237
238 MessageBeep(MB_ICONEXCLAMATION);
239 MessageBox(NULL, buf, "Fatal Error in Wish",
240 MB_ICONSTOP | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
241 #if defined(_MSC_VER) || defined(__BORLANDC__)
242 DebugBreak();
243 #ifdef notdef /* Panics shouldn't cause exceptions.
244 * Simply let the program exit. */
245 _asm {
246 int 3
247 }
248 #endif
249 #endif /* _MSC_VER || __BORLANDC__ */
250 ExitProcess(1);
251 }
252
253 /*
254 *----------------------------------------------------------------------
255 *
256 * WinMain --
257 *
258 * Main entry point from Windows.
259 *
260 * Results:
261 * Returns false if initialization fails, otherwise it never
262 * returns.
263 *
264 * Side effects:
265 * Just about anything, since from here we call arbitrary Tcl code.
266 *
267 *----------------------------------------------------------------------
268 */
269
270 int APIENTRY
271 WinMain(
272 HINSTANCE hInstance,
273 HINSTANCE hPrevInstance,
274 LPSTR lpszCmdLine,
275 int nCmdShow)
276 {
277 char **argv;
278 int argc;
279
280 Tcl_SetPanicProc(WishPanic);
281
282 /*
283 * Set up the default locale to be standard "C" locale so parsing
284 * is performed correctly.
285 */
286
287 setlocale(LC_ALL, "C");
288 setargv(&argc, &argv);
289
290 /*
291 * Increase the application queue size from default value of 8.
292 * At the default value, cross application SendMessage of WM_KILLFOCUS
293 * will fail because the handler will not be able to do a PostMessage!
294 * This is only needed for Windows 3.x, since NT dynamically expands
295 * the queue.
296 */
297
298 SetMessageQueue(64);
299
300 /*
301 * Create the console channels and install them as the standard
302 * channels. All I/O will be discarded until TkConsoleInit is
303 * called to attach the console to a text widget.
304 */
305 #if (TCL_VERSION_NUMBER >= _VERSION(8,2,0))
306 consoleRequired = TRUE;
307 #else
308 TkConsoleCreate();
309 #endif
310 Tk_Main(argc, argv, AppInit);
311 return 1;
312 }
313
314 #endif /* TCL_ONLY */
315
316
317 /*
318 *----------------------------------------------------------------------
319 *
320 * AppInit --
321 *
322 * This procedure performs application-specific initialization.
323 * Most applications, especially those that incorporate additional
324 * packages, will have their own version of this procedure.
325 *
326 * Results:
327 * Returns a standard Tcl completion code, and leaves an error
328 * message in the interp's result if an error occurs.
329 *
330 * Side effects:
331 * Depends on the startup script.
332 *
333 *----------------------------------------------------------------------
334 */
335
336 #ifdef TCL_ONLY
337 static int
338 AppInit(Tcl_Interp *interp)
339 { /* Interpreter for application. */
340 #ifdef TCLLIBPATH
341 /*
342 * It seems that some distributions of Tcl don't compile-in a
343 * default location of the library. This causes Tcl_Init to fail
344 * if bltwish and bltsh are moved to another directory. The
345 * workaround is to set the magic variable "tclDefaultLibrary".
346 */
347 Tcl_SetVar(interp, "tclDefaultLibrary", TCLLIBPATH, TCL_GLOBAL_ONLY);
348 #endif
349 if (Tcl_Init(interp) == TCL_ERROR) {
350 return TCL_ERROR;
351 }
352 if (Blt_Init(interp) == TCL_ERROR) {
353 return TCL_ERROR;
354 }
355 Tcl_StaticPackage(interp, "Blt", Blt_Init, Blt_SafeInit);
356 Tcl_SetVar(interp, "tcl_rcFileName", "~/tclshrc.tcl", TCL_GLOBAL_ONLY);
357 return TCL_OK;
358 }
359
360 #else
361
362 static int
363 AppInit(Tcl_Interp *interp)
364 { /* Interpreter for application. */
365 #ifdef TCLLIBPATH
366 /*
367 * It seems that some distributions of Tcl don't compile-in a
368 * default location of the library. This causes Tcl_Init to fail
369 * if bltwish and bltsh are moved to another directory. The
370 * workaround is to set the magic variable "tclDefaultLibrary".
371 */
372 Tcl_SetVar(interp, "tclDefaultLibrary", TCLLIBPATH, TCL_GLOBAL_ONLY);
373 #endif
374 if (Tcl_Init(interp) == TCL_ERROR) {
375 goto error;
376 }
377 if (Tk_Init(interp) == TCL_ERROR) {
378 goto error;
379 }
380 Tcl_StaticPackage(interp, "Tk", Tk_Init, Tk_SafeInit);
381 if (Blt_Init(interp) == TCL_ERROR) {
382 goto error;
383 }
384 Tcl_StaticPackage(interp, "Blt", Blt_Init, Blt_SafeInit);
385
386 /*
387 * Initialize the console only if we are running as an interactive
388 * application.
389 */
390 #if (TCL_VERSION_NUMBER >= _VERSION(8,2,0))
391 if (consoleRequired) {
392 if (Tk_CreateConsoleWindow(interp) == TCL_ERROR) {
393 goto error;
394 }
395 }
396 #else
397 /*
398 * Initialize the console only if we are running as an interactive
399 * application.
400 */
401 if (TkConsoleInit(interp) == TCL_ERROR) {
402 goto error;
403 }
404 #endif /* TCL_VERSION_NUMBER >= 8.2.0 */
405 Tcl_SetVar(interp, "tcl_rcFileName", "~/wishrc.tcl", TCL_GLOBAL_ONLY);
406 return TCL_OK;
407
408 error:
409 /* WishPanic(Tcl_GetStringResult(interp)); */
410 return TCL_ERROR;
411 }
412
413 #endif /* TCL_ONLY */