Index: trunk/src/kmk/kmkbuiltin/redirect.c
===================================================================
--- trunk/src/kmk/kmkbuiltin/redirect.c	(revision 2745)
+++ trunk/src/kmk/kmkbuiltin/redirect.c	(revision 2747)
@@ -60,8 +60,11 @@
  * Arguments": http://msdn.microsoft.com/en-us/library/a1y7w461.aspx
  *
- * @param   argc        The argument count.
- * @param   argv        The argument vector.
+ * @param   argc                The argument count.
+ * @param   argv                The argument vector.
+ * @param   fWatcomBrainDamage  Set if we're catering for wcc, wcc386 or similar
+ *                              OpenWatcom tools.  They seem to follow some
+ *                              ancient or home made quoting convention.
  */
-static void quoteArguments(int argc, char **argv)
+static void quoteArguments(int argc, char **argv, int fWatcomBrainDamage)
 {
     int i;
@@ -71,17 +74,18 @@
         size_t      cchOrg    = strlen(pszOrg);
         const char *pszQuotes = (const char *)memchr(pszOrg, '"', cchOrg);
+        const char *pszProblem = NULL;
         if (   pszQuotes
             || cchOrg == 0
-            || memchr(pszOrg, ' ', cchOrg)
-            || memchr(pszOrg, '\t', cchOrg)
-            || memchr(pszOrg, '\n', cchOrg)
-            || memchr(pszOrg, '\r', cchOrg)
-            || memchr(pszOrg, '&', cchOrg)
-            || memchr(pszOrg, '>', cchOrg)
-            || memchr(pszOrg, '<', cchOrg)
-            || memchr(pszOrg, '|', cchOrg)
-            || memchr(pszOrg, '%', cchOrg)
-            || memchr(pszOrg, '\'', cchOrg)
-            || memchr(pszOrg, '=', cchOrg)
+            || (pszProblem = (const char *)memchr(pszOrg, ' ',  cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '\t', cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '\n', cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '\r', cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '&',  cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '>',  cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '<',  cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '|',  cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '%',  cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '\'', cchOrg)) != NULL
+            || (pszProblem = (const char *)memchr(pszOrg, '=',  cchOrg)) != NULL
             )
         {
@@ -92,4 +96,30 @@
 
             argv[i] = pszNew;
+
+            /* Watcom does not grok "-i=c:\program files\watcom\h", it thing
+               it's a source specification. The quote must follow the equal. */
+            if (fWatcomBrainDamage)
+            {
+                size_t cchUnquoted  = 0;
+                if (pszOrg[0] == '@') /* Response file quoting: @"file name.rsp" */
+                    cchUnquoted = 1;
+                else if (pszOrg[0] == '-' || pszOrg[0] == '/') /* Switch quoting. */
+                {
+                    const char *pszNeedQuoting = (const char *)memchr(pszOrg, '=', cchOrg);
+                    if (   pszNeedQuoting == NULL
+                        || (uintptr_t)pszNeedQuoting > (uintptr_t)(pszProblem ? pszProblem : pszQuotes))
+                        pszNeedQuoting = pszProblem ? pszProblem : pszQuotes;
+                    else
+                        pszNeedQuoting++;
+                    cchUnquoted = pszNeedQuoting - pszOrg;
+                }
+                if (cchUnquoted)
+                {
+                    memcpy(pszNew, pszOrg, cchUnquoted);
+                    pszNew += cchUnquoted;
+                    pszOrg += cchUnquoted;
+                    cchOrg -= cchUnquoted;
+                }
+            }
 
             *pszNew++ = '"';
@@ -188,5 +218,5 @@
 {
     fprintf(pOut,
-            "usage: %s [-[rwa+tb]<fd> <file>] [-c<fd>] [-Z] [-E <var=val>] [-C <dir>] -- <program> [args]\n"
+            "usage: %s [-[rwa+tb]<fd> <file>] [-c<fd>] [-Z] [-E <var=val>] [-C <dir>] [--wcc-brain-damage] -- <program> [args]\n"
             "   or: %s --help\n"
             "   or: %s --version\n"
@@ -208,4 +238,7 @@
             "effect, so be careful where you put it.\n"
             "\n"
+            "The --wcc-brain-damage switch is to work around wcc and wcc386 (Open Watcom)\n"
+            "not following normal quoting conventions on Windows, OS/2, and DOS.\n"
+            "\n"
             "This command was originally just a quick hack to avoid invoking the shell\n"
             "on Windows (cygwin) where forking is very expensive and has exhibited\n"
@@ -226,4 +259,5 @@
     FILE *pStdErr = stderr;
     FILE *pStdOut = stdout;
+    int fWatcomBrainDamage = 0;
 
     /*
@@ -262,4 +296,9 @@
                 else if (!strcmp(psz, "-close"))
                     psz = "c";
+                else if (!strcmp(psz, "-wcc-brain-damage"))
+                {
+                    fWatcomBrainDamage = 1;
+                    continue;
+                }
             }
 
@@ -710,5 +749,5 @@
 
     /* MSC is a PITA since it refuses to quote the arguments... */
-    quoteArguments(argc - i, &argv[i]);
+    quoteArguments(argc - i, &argv[i], fWatcomBrainDamage);
     rc = _spawnvp(_P_WAIT, argv[i], &argv[i]);
     if (rc == -1 && pStdErr)
