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.
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));
35 *----------------------------------------------------------------------
39 * This is the main program for the application.
42 * None: Tcl_Main never returns here, so this procedure never
46 * Whatever the application does.
48 *----------------------------------------------------------------------
53 int argc; /* Number of command-line arguments. */
54 char **argv; /* Values of command-line arguments. */
57 * The following #if block allows you to change the AppInit
58 * function by using a #define of TCL_LOCAL_APPINIT instead
59 * of rewriting this entire file. The #if checks for that
60 * #define and uses Tcl_AppInit if it doesn't exist.
63 #ifndef TCL_LOCAL_APPINIT
64 #define TCL_LOCAL_APPINIT Tcl_AppInit
66 extern int TCL_LOCAL_APPINIT _ANSI_ARGS_((Tcl_Interp *interp));
69 * The following #if block allows you to change how Tcl finds the startup
70 * script, prime the library or encoding paths, fiddle with the argv,
71 * etc., without needing to rewrite Tcl_Main()
74 #ifdef TCL_LOCAL_MAIN_HOOK
75 extern int TCL_LOCAL_MAIN_HOOK _ANSI_ARGS_((int *argc, char ***argv));
78 char buffer[MAX_PATH +1];
81 * Set up the default locale to be standard "C" locale so parsing
82 * is performed correctly.
85 setlocale(LC_ALL, "C");
86 setargv(&argc, &argv);
89 * Replace argv[0] with full pathname of executable, and forward
90 * slashes substituted for backslashes.
93 GetModuleFileName(NULL, buffer, sizeof(buffer));
95 for (p = buffer; *p != '\0'; p++) {
101 #ifdef TCL_LOCAL_MAIN_HOOK
102 TCL_LOCAL_MAIN_HOOK(&argc, &argv);
105 Tcl_Main(argc, argv, TCL_LOCAL_APPINIT);
107 return 0; /* Needed only to prevent compiler warning. */
112 *----------------------------------------------------------------------
116 * This procedure performs application-specific initialization.
117 * Most applications, especially those that incorporate additional
118 * packages, will have their own version of this procedure.
121 * Returns a standard Tcl completion code, and leaves an error
122 * message in the interp's result if an error occurs.
125 * Depends on the startup script.
127 *----------------------------------------------------------------------
132 Tcl_Interp *interp; /* Interpreter for application. */
134 if (Tcl_Init(interp) == TCL_ERROR) {
139 if (Tcltest_Init(interp) == TCL_ERROR) {
142 Tcl_StaticPackage(interp, "Tcltest", Tcltest_Init,
143 (Tcl_PackageInitProc *) NULL);
144 if (TclObjTest_Init(interp) == TCL_ERROR) {
148 if (TclThread_Init(interp) == TCL_ERROR) {
152 if (Procbodytest_Init(interp) == TCL_ERROR) {
155 Tcl_StaticPackage(interp, "procbodytest", Procbodytest_Init,
156 Procbodytest_SafeInit);
157 #endif /* TCL_TEST */
160 * Call the init procedures for included packages. Each call should
163 * if (Mod_Init(interp) == TCL_ERROR) {
167 * where "Mod" is the name of the module.
171 * Call Tcl_CreateCommand for application-specific commands, if
172 * they weren't already created by the init procedures called above.
176 * Specify a user-specific startup file to invoke if the application
177 * is run interactively. Typically the startup file is "~/.apprc"
178 * where "app" is the name of the application. If this line is deleted
179 * then no user-specific startup file will be run under any conditions.
182 Tcl_SetVar(interp, "tcl_rcFileName", "~/tclshrc.tcl", TCL_GLOBAL_ONLY);
187 *-------------------------------------------------------------------------
191 * Parse the Windows command line string into argc/argv. Done here
192 * because we don't trust the builtin argument parser in crt0.
193 * Windows applications are responsible for breaking their command
194 * line into arguments.
196 * 2N backslashes + quote -> N backslashes + begin quoted string
197 * 2N + 1 backslashes + quote -> literal
198 * N backslashes + non-quote -> literal
199 * quote + quote in a quoted string -> single quote
200 * quote + quote not in quoted string -> empty string
201 * quote -> begin quoted string
204 * Fills argcPtr with the number of arguments and argvPtr with the
205 * array of arguments.
210 *--------------------------------------------------------------------------
214 setargv(argcPtr, argvPtr)
215 int *argcPtr; /* Filled with number of argument strings. */
216 char ***argvPtr; /* Filled with argument strings (malloc'd). */
218 char *cmdLine, *p, *arg, *argSpace;
220 int argc, size, inquote, copy, slashes;
222 cmdLine = GetCommandLine(); /* INTL: BUG */
225 * Precompute an overly pessimistic guess at the number of arguments
226 * in the command line by counting non-space spans.
230 for (p = cmdLine; *p != '\0'; p++) {
231 if ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
233 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
241 argSpace = (char *) Tcl_Alloc(
242 (unsigned) (size * sizeof(char *) + strlen(cmdLine) + 1));
243 argv = (char **) argSpace;
244 argSpace += size * sizeof(char *);
248 for (argc = 0; argc < size; argc++) {
249 argv[argc] = arg = argSpace;
250 while ((*p == ' ') || (*p == '\t')) { /* INTL: ISO space. */
266 if ((slashes & 1) == 0) {
268 if ((inquote) && (p[1] == '"')) {
285 || (!inquote && ((*p == ' ') || (*p == '\t')))) { /* INTL: ISO space. */