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