Debian Codegraph @

win/tclAppInit.c

raw
1 /*
2 * tclAppInit.c --
3 *
4 * Provides a default version of the main program and Tcl_AppInit
5 * procedure for tclsh and other Tcl-based applications (without Tk).
6 * Note that this program must be built in Win32 console mode to work
7 * properly.
8 *
9 * Copyright (c) 1993 The Regents of the University of California.
10 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
11 * Copyright (c) 1998-1999 Scriptics Corporation.
12 *
13 * See the file "license.terms" for information on usage and redistribution of
14 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 */
16
17 #include "tcl.h"
18 #define WIN32_LEAN_AND_MEAN
19 #define STRICT /* See MSDN Article Q83456 */
20 #include <windows.h>
21 #undef STRICT
22 #undef WIN32_LEAN_AND_MEAN
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <tchar.h>
26 #if TCL_MAJOR_VERSION < 9
27 # define Tcl_LibraryInitProc Tcl_PackageInitProc
28 # define Tcl_StaticLibrary Tcl_StaticPackage
29 #endif
30
31 #ifdef TCL_TEST
32 extern Tcl_LibraryInitProc Tcltest_Init;
33 extern Tcl_LibraryInitProc Tcltest_SafeInit;
34 #endif /* TCL_TEST */
35
36 #if defined(STATIC_BUILD) && defined(TCL_USE_STATIC_PACKAGES) && TCL_USE_STATIC_PACKAGES
37 extern Tcl_LibraryInitProc Registry_Init;
38 extern Tcl_LibraryInitProc Dde_Init;
39 extern Tcl_LibraryInitProc Dde_SafeInit;
40 #endif
41
42 #if defined(__GNUC__) || defined(TCL_BROKEN_MAINARGS)
43 int _CRT_glob = 0;
44 #endif /* __GNUC__ || TCL_BROKEN_MAINARGS */
45 #ifdef TCL_BROKEN_MAINARGS
46 static void setargv(int *argcPtr, TCHAR ***argvPtr);
47 #endif /* TCL_BROKEN_MAINARGS */
48
49 /*
50 * The following #if block allows you to change the AppInit function by using
51 * a #define of TCL_LOCAL_APPINIT instead of rewriting this entire file. The
52 * #if checks for that #define and uses Tcl_AppInit if it does not exist.
53 */
54
55 #ifndef TCL_LOCAL_APPINIT
56 #define TCL_LOCAL_APPINIT Tcl_AppInit
57 #endif
58 #ifndef MODULE_SCOPE
59 # define MODULE_SCOPE extern
60 #endif
61 MODULE_SCOPE int TCL_LOCAL_APPINIT(Tcl_Interp *);
62
63 /*
64 * The following #if block allows you to change how Tcl finds the startup
65 * script, prime the library or encoding paths, fiddle with the argv, etc.,
66 * without needing to rewrite Tcl_Main()
67 */
68
69 #ifdef TCL_LOCAL_MAIN_HOOK
70 MODULE_SCOPE int TCL_LOCAL_MAIN_HOOK(int *argc, TCHAR ***argv);
71 #endif
72
73 /*
74 *----------------------------------------------------------------------
75 *
76 * main --
77 *
78 * This is the main program for the application.
79 *
80 * Results:
81 * None: Tcl_Main never returns here, so this procedure never returns
82 * either.
83 *
84 * Side effects:
85 * Just about anything, since from here we call arbitrary Tcl code.
86 *
87 *----------------------------------------------------------------------
88 */
89
90 #ifdef TCL_BROKEN_MAINARGS
91 int
92 main(
93 int argc, /* Number of command-line arguments. */
94 char **argv1) /* Not used. */
95 {
96 TCHAR **argv;
97 #else
98 int
99 _tmain(
100 int argc, /* Number of command-line arguments. */
101 TCHAR *argv[]) /* Values of command-line arguments. */
102 {
103 #endif
104 TCHAR *p;
105
106 /*
107 * Set up the default locale to be standard "C" locale so parsing is
108 * performed correctly.
109 */
110
111 setlocale(LC_ALL, "C");
112
113 #ifdef TCL_BROKEN_MAINARGS
114 /*
115 * Get our args from the c-runtime. Ignore command line.
116 */
117
118 (void)argv1;
119 setargv(&argc, &argv);
120 #endif
121
122 /*
123 * Forward slashes substituted for backslashes.
124 */
125
126 for (p = argv[0]; *p != '\0'; p++) {
127 if (*p == '\\') {
128 *p = '/';
129 }
130 }
131
132 #ifdef TCL_LOCAL_MAIN_HOOK
133 TCL_LOCAL_MAIN_HOOK(&argc, &argv);
134 #elif TCL_MAJOR_VERSION > 8 && (!defined(_WIN32) || defined(UNICODE))
135 /* New in Tcl 9.0. This doesn't work on Windows without UNICODE */
136 TclZipfs_AppHook(&argc, &argv);
137 #endif
138
139 Tcl_Main(argc, argv, TCL_LOCAL_APPINIT);
140 return 0; /* Needed only to prevent compiler warning. */
141 }
142
143 /*
144 *----------------------------------------------------------------------
145 *
146 * Tcl_AppInit --
147 *
148 * This procedure performs application-specific initialization. Most
149 * applications, especially those that incorporate additional packages,
150 * will have their own version of this procedure.
151 *
152 * Results:
153 * Returns a standard Tcl completion code, and leaves an error message in
154 * the interp's result if an error occurs.
155 *
156 * Side effects:
157 * Depends on the startup script.
158 *
159 *----------------------------------------------------------------------
160 */
161
162 int
163 Tcl_AppInit(
164 Tcl_Interp *interp) /* Interpreter for application. */
165 {
166 if ((Tcl_Init)(interp) == TCL_ERROR) {
167 return TCL_ERROR;
168 }
169
170 #if defined(STATIC_BUILD) && defined(TCL_USE_STATIC_PACKAGES) && TCL_USE_STATIC_PACKAGES
171 if (Registry_Init(interp) == TCL_ERROR) {
172 return TCL_ERROR;
173 }
174 Tcl_StaticLibrary(interp, "Registry", Registry_Init, NULL);
175
176 if (Dde_Init(interp) == TCL_ERROR) {
177 return TCL_ERROR;
178 }
179 Tcl_StaticLibrary(interp, "Dde", Dde_Init, Dde_SafeInit);
180 #endif
181
182 #ifdef TCL_TEST
183 if (Tcltest_Init(interp) == TCL_ERROR) {
184 return TCL_ERROR;
185 }
186 Tcl_StaticLibrary(interp, "Tcltest", Tcltest_Init, Tcltest_SafeInit);
187 #endif /* TCL_TEST */
188
189 /*
190 * Call the init procedures for included packages. Each call should look
191 * like this:
192 *
193 * if (Mod_Init(interp) == TCL_ERROR) {
194 * return TCL_ERROR;
195 * }
196 *
197 * where "Mod" is the name of the module. (Dynamically-loadable packages
198 * should have the same entry-point name.)
199 */
200
201 /*
202 * Call Tcl_CreateCommand for application-specific commands, if they
203 * weren't already created by the init procedures called above.
204 */
205
206 /*
207 * Specify a user-specific startup file to invoke if the application is
208 * run interactively. Typically the startup file is "~/.apprc" where "app"
209 * is the name of the application. If this line is deleted then no
210 * user-specific startup file will be run under any conditions.
211 */
212
213 (Tcl_ObjSetVar2)(interp, Tcl_NewStringObj("tcl_rcFileName", -1), NULL,
214 Tcl_NewStringObj("~/tclshrc.tcl", -1), TCL_GLOBAL_ONLY);
215 return TCL_OK;
216 }
217
218 /*
219 *-------------------------------------------------------------------------
220 *
221 * setargv --
222 *
223 * Parse the Windows command line string into argc/argv. Done here
224 * because we don't trust the builtin argument parser in crt0. Windows
225 * applications are responsible for breaking their command line into
226 * arguments.
227 *
228 * 2N backslashes + quote -> N backslashes + begin quoted string
229 * 2N + 1 backslashes + quote -> literal
230 * N backslashes + non-quote -> literal
231 * quote + quote in a quoted string -> single quote
232 * quote + quote not in quoted string -> empty string
233 * quote -> begin quoted string
234 *
235 * Results:
236 * Fills argcPtr with the number of arguments and argvPtr with the array
237 * of arguments.
238 *
239 * Side effects:
240 * Memory allocated.
241 *
242 *--------------------------------------------------------------------------
243 */
244
245 #ifdef TCL_BROKEN_MAINARGS
246 static void
247 setargv(
248 int *argcPtr, /* Filled with number of argument strings. */
249 TCHAR ***argvPtr) /* Filled with argument strings (malloc'd). */
250 {
251 TCHAR *cmdLine, *p, *arg, *argSpace;
252 TCHAR **argv;
253 int argc, size, inquote, copy, slashes;
254
255 cmdLine = GetCommandLine();
256
257 /*
258 * Precompute an overly pessimistic guess at the number of arguments in
259 * the command line by counting non-space spans.
260 */
261
262 size = 2;
263 for (p = cmdLine; *p != '\0'; p++) {
264 if ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
265 size++;
266 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
267 p++;
268 }
269 if (*p == '\0') {
270 break;
271 }
272 }
273 }
274
275 /* Make sure we don't call ckalloc through the (not yet initialized) stub table */
276 #undef Tcl_Alloc
277 #undef Tcl_DbCkalloc
278
279 argSpace = (TCHAR *)ckalloc(size * sizeof(char *)
280 + (_tcslen(cmdLine) * sizeof(TCHAR)) + sizeof(TCHAR));
281 argv = (TCHAR **) argSpace;
282 argSpace += size * (sizeof(char *)/sizeof(TCHAR));
283 size--;
284
285 p = cmdLine;
286 for (argc = 0; argc < size; argc++) {
287 argv[argc] = arg = argSpace;
288 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
289 p++;
290 }
291 if (*p == '\0') {
292 break;
293 }
294
295 inquote = 0;
296 slashes = 0;
297 while (1) {
298 copy = 1;
299 while (*p == '\\') {
300 slashes++;
301 p++;
302 }
303 if (*p == '"') {
304 if ((slashes & 1) == 0) {
305 copy = 0;
306 if ((inquote) && (p[1] == '"')) {
307 p++;
308 copy = 1;
309 } else {
310 inquote = !inquote;
311 }
312 }
313 slashes >>= 1;
314 }
315
316 while (slashes) {
317 *arg = '\\';
318 arg++;
319 slashes--;
320 }
321
322 if ((*p == '\0') || (!inquote &&
323 ((*p == ' ') || (*p == '\t')))) { /* INTL: ISO space. */
324 break;
325 }
326 if (copy != 0) {
327 *arg = *p;
328 arg++;
329 }
330 p++;
331 }
332 *arg = '\0';
333 argSpace = arg + 1;
334 }
335 argv[argc] = NULL;
336
337 *argcPtr = argc;
338 *argvPtr = argv;
339 }
340 #endif /* TCL_BROKEN_MAINARGS */
341
342 /*
343 * Local Variables:
344 * mode: c
345 * c-basic-offset: 4
346 * fill-column: 78
347 * End:
348 */