From 21254079488f4212f41c0c2b3acbe29e44bcae3d Mon Sep 17 00:00:00 2001 From: nijtmans Date: Mon, 31 May 2010 13:47:32 +0000 Subject: Add command line processing for CYGWIN, backported from trunk. --- ChangeLog | 2 + win/winMain.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8112893..f29cfe2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ * generic/tkMain.c: Fix CYGWIN warning: "fd_set and assiciated.macros have been defined in sys/types. This may cause runtime problems with W32" + * win/winMain.c: Add command line processing for CYGWIN, backported + from trunk. 2010-05-20 Donal K. Fellows diff --git a/win/winMain.c b/win/winMain.c index 254d194..273cf0f 100644 --- a/win/winMain.c +++ b/win/winMain.c @@ -9,7 +9,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: winMain.c,v 1.26 2007/12/13 15:28:56 dgp Exp $ + * RCS: @(#) $Id: winMain.c,v 1.26.2.1 2010/05/31 13:47:33 nijtmans Exp $ */ #include "tkInt.h" @@ -33,6 +33,10 @@ static void WishPanic(CONST char *format, ...); extern int Tktest_Init(Tcl_Interp *interp); #endif /* TK_TEST */ +#if defined(__CYGWIN__) +static void setargv(int *argcPtr, char ***argvPtr); +#endif /* __CYGWIN__ */ + static BOOL consoleRequired = TRUE; /* @@ -104,8 +108,12 @@ WinMain( * Get our args from the c-runtime. Ignore lpszCmdLine. */ +#if defined(__CYGWIN__) + setargv(&argc, &argv); +#else argc = __argc; argv = __argv; +#endif /* * Forward slashes substituted for backslashes. @@ -284,7 +292,127 @@ main( return 0; } #endif /* !__GNUC__ || TK_TEST */ - + + +/* + *------------------------------------------------------------------------- + * + * 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. + * + *-------------------------------------------------------------------------- + */ + +#if defined(__CYGWIN__) +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 *) ckalloc( + (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; +} +#endif /* __CYGWIN__ */ + /* * Local Variables: * mode: c -- cgit v0.12