4 * Main entry point for cwsh.
6 * Copyright (c) 1995 Sun Microsystems, Inc.
7 * Copyright (c) 1999 Christian Werner
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * RCS: @(#) $Id: winMain.c,v 1.1 2006-02-24 18:59:53 vitus Exp $
16 #define WIN32_LEAN_AND_MEAN
18 #undef WIN32_LEAN_AND_MEAN
23 * Forward declarations for procedures defined later in this file:
26 static void setargv _ANSI_ARGS_((int *argcPtr, char ***argvPtr));
27 static void CwshPanic _ANSI_ARGS_(TCL_VARARGS(char *,format));
31 *----------------------------------------------------------------------
35 * Main entry point from Windows.
38 * Returns false if initialization fails, otherwise it never
42 * Just about anything, since from here we call arbitrary Tcl code.
44 *----------------------------------------------------------------------
48 WinMain(hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
50 HINSTANCE hPrevInstance;
56 char buffer[MAX_PATH];
58 Tcl_SetPanicProc(CwshPanic);
64 if (!AllocConsole()) {
65 CwshPanic("Error allocating console");
67 freopen("CONIN$", "r", stdin);
68 freopen("CONOUT$", "w", stdout);
69 freopen("CONOUT$", "w", stderr);
72 * Set up the default locale to be standard "C" locale so parsing
73 * is performed correctly.
76 setlocale(LC_ALL, "C");
79 * Increase the application queue size from default value of 8.
80 * At the default value, cross application SendMessage of WM_KILLFOCUS
81 * will fail because the handler will not be able to do a PostMessage!
82 * This is only needed for Windows 3.x, since NT dynamically expands
87 setargv(&argc, &argv);
90 * Replace argv[0] with full pathname of executable, and forward
91 * slashes substituted for backslashes.
94 GetModuleFileName(NULL, buffer, sizeof(buffer));
96 for (p = buffer; *p != '\0'; p++) {
102 Ck_Main(argc, argv, Tcl_AppInit);
107 *----------------------------------------------------------------------
111 * This procedure performs application-specific initialization.
112 * Most applications, especially those that incorporate additional
113 * packages, will have their own version of this procedure.
116 * Returns a standard Tcl completion code, and leaves an error
117 * message in interp->result if an error occurs.
120 * Depends on the startup script.
122 *----------------------------------------------------------------------
127 Tcl_Interp *interp; /* Interpreter for application. */
129 if (Tcl_Init(interp) == TCL_ERROR) {
132 if (Ck_Init(interp) == TCL_ERROR) {
135 #if (TCL_MAJOR_VERSION == 7) && (TCL_MINOR_VERSION <= 4)
136 tcl_RcFileName = "~/.cwshrc";
138 Tcl_StaticPackage(interp, "Ck", Ck_Init, (Tcl_PackageInitProc *) NULL);
139 Tcl_SetVar(interp, "tcl_rcFileName", "~/.cwshrc", TCL_GLOBAL_ONLY);
144 CwshPanic(interp->result);
149 *----------------------------------------------------------------------
153 * Display a message and exit.
161 *----------------------------------------------------------------------
165 CwshPanic TCL_VARARGS_DEF(char *,arg1)
171 format = TCL_VARARGS_START(char *,arg1,argList);
172 vsprintf(buf, format, argList);
174 MessageBeep(MB_ICONEXCLAMATION);
175 MessageBox(NULL, buf, "Fatal Error in CWSH",
176 MB_ICONSTOP | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
183 *-------------------------------------------------------------------------
187 * Parse the Windows command line string into argc/argv. Done here
188 * because we don't trust the builtin argument parser in crt0.
189 * Windows applications are responsible for breaking their command
190 * line into arguments.
192 * 2N backslashes + quote -> N backslashes + begin quoted string
193 * 2N + 1 backslashes + quote -> literal
194 * N backslashes + non-quote -> literal
195 * quote + quote in a quoted string -> single quote
196 * quote + quote not in quoted string -> empty string
197 * quote -> begin quoted string
200 * Fills argcPtr with the number of arguments and argvPtr with the
201 * array of arguments.
206 *--------------------------------------------------------------------------
210 setargv(argcPtr, argvPtr)
211 int *argcPtr; /* Filled with number of argument strings. */
212 char ***argvPtr; /* Filled with argument strings (malloc'd). */
214 char *cmdLine, *p, *arg, *argSpace;
216 int argc, size, inquote, copy, slashes;
218 cmdLine = GetCommandLine();
221 * Precompute an overly pessimistic guess at the number of arguments
222 * in the command line by counting non-space spans.
226 for (p = cmdLine; *p != '\0'; p++) {
229 while (isspace(*p)) {
237 argSpace = (char *) ckalloc((unsigned) (size * sizeof(char *)
238 + strlen(cmdLine) + 1));
239 argv = (char **) argSpace;
240 argSpace += size * sizeof(char *);
244 for (argc = 0; argc < size; argc++) {
245 argv[argc] = arg = argSpace;
246 while (isspace(*p)) {
262 if ((slashes & 1) == 0) {
264 if ((inquote) && (p[1] == '"')) {
280 if ((*p == '\0') || (!inquote && isspace(*p))) {