os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/win/tclAppInit.c
Update contrib.
4 * Provides a default version of the main program and Tcl_AppInit
5 * procedure for Tcl applications (without Tk). Note that this
6 * program must be built in Win32 console mode to work properly.
8 * Copyright (c) 1996-1997 by Sun Microsystems, Inc.
9 * Copyright (c) 1998-1999 by Scriptics Corporation.
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 * RCS: @(#) $Id: tclAppInit.c,v 1.11.2.3 2007/03/19 17:06:26 dgp Exp $
22 extern int Procbodytest_Init _ANSI_ARGS_((Tcl_Interp *interp));
23 extern int Procbodytest_SafeInit _ANSI_ARGS_((Tcl_Interp *interp));
24 extern int Tcltest_Init _ANSI_ARGS_((Tcl_Interp *interp));
25 extern int TclObjTest_Init _ANSI_ARGS_((Tcl_Interp *interp));
27 extern int TclThread_Init _ANSI_ARGS_((Tcl_Interp *interp));
31 static void setargv _ANSI_ARGS_((int *argcPtr, char ***argvPtr));
32 static BOOL __stdcall sigHandler (DWORD fdwCtrlType);
33 static Tcl_AsyncProc asyncExit;
34 static void AppInitExitHandler(ClientData clientData);
36 static char ** argvSave = NULL;
37 static Tcl_AsyncHandler exitToken = NULL;
38 static DWORD exitErrorCode = 0;
42 *----------------------------------------------------------------------
46 * This is the main program for the application.
49 * None: Tcl_Main never returns here, so this procedure never
53 * Whatever the application does.
55 *----------------------------------------------------------------------
60 int argc; /* Number of command-line arguments. */
61 char **argv; /* Values of command-line arguments. */
64 * The following #if block allows you to change the AppInit
65 * function by using a #define of TCL_LOCAL_APPINIT instead
66 * of rewriting this entire file. The #if checks for that
67 * #define and uses Tcl_AppInit if it doesn't exist.
70 #ifndef TCL_LOCAL_APPINIT
71 #define TCL_LOCAL_APPINIT Tcl_AppInit
73 extern int TCL_LOCAL_APPINIT _ANSI_ARGS_((Tcl_Interp *interp));
76 * The following #if block allows you to change how Tcl finds the startup
77 * script, prime the library or encoding paths, fiddle with the argv,
78 * etc., without needing to rewrite Tcl_Main()
81 #ifdef TCL_LOCAL_MAIN_HOOK
82 extern int TCL_LOCAL_MAIN_HOOK _ANSI_ARGS_((int *argc, char ***argv));
85 char buffer[MAX_PATH +1];
88 * Set up the default locale to be standard "C" locale so parsing
89 * is performed correctly.
92 setlocale(LC_ALL, "C");
93 setargv(&argc, &argv);
96 * Save this for later, so we can free it.
101 * Replace argv[0] with full pathname of executable, and forward
102 * slashes substituted for backslashes.
105 GetModuleFileName(NULL, buffer, sizeof(buffer));
107 for (p = buffer; *p != '\0'; p++) {
113 #ifdef TCL_LOCAL_MAIN_HOOK
114 TCL_LOCAL_MAIN_HOOK(&argc, &argv);
117 Tcl_Main(argc, argv, TCL_LOCAL_APPINIT);
119 return 0; /* Needed only to prevent compiler warning. */
124 *----------------------------------------------------------------------
128 * This procedure performs application-specific initialization.
129 * Most applications, especially those that incorporate additional
130 * packages, will have their own version of this procedure.
133 * Returns a standard Tcl completion code, and leaves an error
134 * message in the interp's result if an error occurs.
137 * Depends on the startup script.
139 *----------------------------------------------------------------------
144 Tcl_Interp *interp; /* Interpreter for application. */
146 if (Tcl_Init(interp) == TCL_ERROR) {
151 * Install a signal handler to the win32 console tclsh is running in.
153 SetConsoleCtrlHandler(sigHandler, TRUE);
154 exitToken = Tcl_AsyncCreate(asyncExit, NULL);
157 * This exit handler will be used to free the
158 * resources allocated in this file.
160 Tcl_CreateExitHandler(AppInitExitHandler, NULL);
163 if (Tcltest_Init(interp) == TCL_ERROR) {
166 Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
167 (Tcl_PackageInitProc *) NULL);
168 if (TclObjTest_Init(interp) == TCL_ERROR) {
172 if (TclThread_Init(interp) == TCL_ERROR) {
176 if (Procbodytest_Init(interp) == TCL_ERROR) {
179 Tcl_StaticPackage(interp, "procbodytest", Procbodytest_Init,
180 Procbodytest_SafeInit);
181 #endif /* TCL_TEST */
183 #if defined(STATIC_BUILD) && defined(TCL_USE_STATIC_PACKAGES)
185 extern Tcl_PackageInitProc Registry_Init;
186 extern Tcl_PackageInitProc Dde_Init;
188 if (Registry_Init(interp) == TCL_ERROR) {
191 Tcl_StaticPackage(interp, "registry", Registry_Init, NULL);
193 if (Dde_Init(interp) == TCL_ERROR) {
196 Tcl_StaticPackage(interp, "dde", Dde_Init, NULL);
201 * Call the init procedures for included packages. Each call should
204 * if (Mod_Init(interp) == TCL_ERROR) {
208 * where "Mod" is the name of the module.
212 * Call Tcl_CreateCommand for application-specific commands, if
213 * they weren't already created by the init procedures called above.
217 * Specify a user-specific startup file to invoke if the application
218 * is run interactively. Typically the startup file is "~/.apprc"
219 * where "app" is the name of the application. If this line is deleted
220 * then no user-specific startup file will be run under any conditions.
223 Tcl_SetVar(interp, "tcl_rcFileName", "~/tclshrc.tcl", TCL_GLOBAL_ONLY);
228 *----------------------------------------------------------------------
230 * AppInitExitHandler --
232 * This function is called to cleanup the app init resources before
239 * Frees the saved argv and deletes the async exit handler.
241 *----------------------------------------------------------------------
246 ClientData clientData)
248 if (argvSave != NULL) {
249 ckfree((char *)argvSave);
253 if (exitToken != NULL) {
255 * This should be safe to do even if we
256 * are in an async exit right now.
258 Tcl_AsyncDelete(exitToken);
264 *-------------------------------------------------------------------------
268 * Parse the Windows command line string into argc/argv. Done here
269 * because we don't trust the builtin argument parser in crt0.
270 * Windows applications are responsible for breaking their command
271 * line into arguments.
273 * 2N backslashes + quote -> N backslashes + begin quoted string
274 * 2N + 1 backslashes + quote -> literal
275 * N backslashes + non-quote -> literal
276 * quote + quote in a quoted string -> single quote
277 * quote + quote not in quoted string -> empty string
278 * quote -> begin quoted string
281 * Fills argcPtr with the number of arguments and argvPtr with the
282 * array of arguments.
287 *--------------------------------------------------------------------------
291 setargv(argcPtr, argvPtr)
292 int *argcPtr; /* Filled with number of argument strings. */
293 char ***argvPtr; /* Filled with argument strings (malloc'd). */
295 char *cmdLine, *p, *arg, *argSpace;
297 int argc, size, inquote, copy, slashes;
299 cmdLine = GetCommandLine(); /* INTL: BUG */
302 * Precompute an overly pessimistic guess at the number of arguments
303 * in the command line by counting non-space spans.
307 for (p = cmdLine; *p != '\0'; p++) {
308 if ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
310 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
318 argSpace = (char *) ckalloc(
319 (unsigned) (size * sizeof(char *) + strlen(cmdLine) + 1));
320 argv = (char **) argSpace;
321 argSpace += size * sizeof(char *);
325 for (argc = 0; argc < size; argc++) {
326 argv[argc] = arg = argSpace;
327 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
343 if ((slashes & 1) == 0) {
345 if ((inquote) && (p[1] == '"')) {
362 || (!inquote && ((*p == ' ') || (*p == '\t')))) { /* INTL: ISO space. */
381 *----------------------------------------------------------------------
385 * The AsyncProc for the exitToken.
388 * doesn't actually return.
391 * tclsh cleanly exits.
393 *----------------------------------------------------------------------
397 asyncExit (ClientData clientData, Tcl_Interp *interp, int code)
399 Tcl_Exit((int)exitErrorCode);
406 *----------------------------------------------------------------------
410 * Signal handler for the Win32 OS. Catches Ctrl+C, Ctrl+Break and
411 * other exits. This is needed so tclsh can do it's real clean-up
412 * and not an unclean crash terminate.
418 * Effects the way the app exits from a signal. This is an
419 * operating system supplied thread and unsafe to call ANY
420 * Tcl commands except for Tcl_AsyncMark.
422 *----------------------------------------------------------------------
426 sigHandler(DWORD fdwCtrlType)
431 /* Async token must have been destroyed, punt gracefully. */
436 * If Tcl is currently executing some bytecode or in the eventloop,
437 * this will cause Tcl to enter asyncExit at the next command
440 exitErrorCode = fdwCtrlType;
441 Tcl_AsyncMark(exitToken);
444 * This will cause Tcl_Gets in Tcl_Main() to drop-out with an <EOF>
445 * should it be blocked on input and our Tcl_AsyncMark didn't grab
446 * the attention of the interpreter.
448 hStdIn = GetStdHandle(STD_INPUT_HANDLE);
453 /* indicate to the OS not to call the default terminator */