# HG changeset patch
# User Ben Fritz <fritzophrenic@gmail.com>
# Date 1321679027 21600
# Branch 2html-dev
# Node ID e0cb6adbc064738ff60601a0f8a3dd6bd9eed713
# Parent  dfaee202b2b362a37e17d048e7606fc527628e29
Problem: cmd.exe strips first and last quote from external commands
Solution: set default value of shellxquote and shellcmdflag to add extra quotes
which are OK to strip off

diff -r dfaee202b2b3 -r e0cb6adbc064 runtime/doc/options.txt
--- a/runtime/doc/options.txt	Thu Nov 10 22:51:46 2011 -0600
+++ b/runtime/doc/options.txt	Fri Nov 18 23:03:47 2011 -0600
@@ -5894,8 +5894,10 @@
 	security reasons.
 
 						*'shellcmdflag'* *'shcf'*
-'shellcmdflag' 'shcf'	string	(default: "-c", MS-DOS and Win32, when 'shell'
-					does not contain "sh" somewhere: "/c")
+'shellcmdflag' 'shcf'	string	(default: "-c";
+                                 Win32, when 'shell' is cmd.exe: "/s /c";
+                                 MS-DOS and Win32, when 'shell' neither is
+                                 cmd.exe nor contains "sh" somewhere: "/c")
 			global
 			{not in Vi}
 	Flag passed to the shell to execute "!" and ":!" commands; e.g.,
@@ -6036,8 +6038,8 @@
 
 						*'shellxquote'* *'sxq'*
 'shellxquote' 'sxq'	string	(default: "";
-					for Win32, when 'shell' contains "sh"
-					somewhere: "\""
+                                        for Win32, when 'shell' is cmd.exe or
+                                        contains "sh" somewhere: "\""
 					for Unix, when using system(): "\"")
 			global
 			{not in Vi}
@@ -6045,11 +6047,12 @@
 	the "!" and ":!" commands.  Includes the redirection.  See
 	'shellquote' to exclude the redirection.  It's probably not useful
 	to set both options.
-	This is an empty string by default.  Known to be useful for
-	third-party shells when using the Win32 version, such as the MKS Korn
-	Shell or bash, where it should be "\"".  The default is adjusted
-	according the value of 'shell', to reduce the need to set this option
-	by the user.  See |dos-shell|.
+        This is an empty string by default on most systems, but is known to be
+        useful for on Win32 version, either for cmd.exe which automatically
+        strips off the first and last quote on a command, or 3rd-party shells
+        such as the MKS Korn Shell or bash, where it should be "\"".  The
+        default is adjusted according the value of 'shell', to reduce the need
+        to set this option by the user.  See |dos-shell|.
 	This option cannot be set from a |modeline| or in the |sandbox|, for
 	security reasons.
 
@@ -8111,4 +8114,4 @@
 	screen.  When non-zero, characters are sent to the terminal one by
 	one.  For MS-DOS pcterm this does not work.  For debugging purposes.
 
- vim:tw=78:ts=8:ft=help:norl:
+ vim:tw=78:ts=8:ft=help:norl:ai:
diff -r dfaee202b2b3 -r e0cb6adbc064 runtime/doc/todo.txt
--- a/runtime/doc/todo.txt	Thu Nov 10 22:51:46 2011 -0600
+++ b/runtime/doc/todo.txt	Fri Nov 18 23:03:47 2011 -0600
@@ -1085,12 +1085,6 @@
 
 Win32: patch for fullscreen mode. (Liushaolin, 2008 April 17)
 
-Win32: When 'shell' is cmd.exe this command fails:
-	echo system('"c:/path/echo.exe" "foo bar"')
-Should we set the default for 'shellxquote' to a double quote, when 'shell'
-contains "cmd" in the tail?  (Benjamin Fritz, 2008 Oct 13)
-Also set 'shellcmdflag' to include /s.
-
 Win32: When there is 4 Gbyte of memory mch_avail_mem() doesn't work properly.
 Unfinished patch by Jelle Geerts, 2008 Aug 24.
 Let mch_avail_mem() return Kbyte instead?
diff -r dfaee202b2b3 -r e0cb6adbc064 src/option.c
--- a/src/option.c	Thu Nov 10 22:51:46 2011 -0600
+++ b/src/option.c	Fri Nov 18 23:03:47 2011 -0600
@@ -3883,7 +3883,9 @@
 
 #if defined(MSDOS) || defined(WIN3264) || defined(OS2)
     /*
-     * Set 'shellcmdflag and 'shellquote' depending on the 'shell' option.
+     * Set 'shellcmdflag', 'shellxquote', and 'shellquote' depending on the
+     * 'shell' option.
+     *
      * This is done after other initializations, where 'shell' might have been
      * set, but only if they have not been set before.  Default for p_shcf is
      * "/c", for p_shq is "".  For "sh" like  shells it is changed here to
@@ -3920,6 +3922,40 @@
 #  endif
 # endif
     }
+    /* cmd.exe on Windows will automatically strip the first and last quote
+     * given on the command line, e.g. most of the time things like:
+     *   cmd /c "my path/to/echo" "my args to echo"
+     * become:
+     *   my path/to/echo" "my args to echo
+     * when executed.
+     *
+     * To avoid this, use the /s argument in addition to /c to force the
+     * stripping behavior, and also set shellxquote to automatically surround
+     * the entire command in quotes (which get stripped as noted).
+     */
+    else if (strstr((char *)gettail(p_sh), "cmd.exe") != NULL)
+    {
+	int	idx3;
+
+	/* set shellxquote default to add the quotes to be stripped */
+	idx3 = findoption((char_u *)"sxq");
+	if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
+	{
+	    p_sxq = (char_u *)"\"";
+	    options[idx3].def_val[VI_DEFAULT] = p_sxq;
+	}
+
+	/* set shellcmdflag default to always strip the quotes, note the order
+	 * between /s and /c is important or cmd.exe will treat the /s as part
+	 * of the command to be executed.
+	 */
+	idx3 = findoption((char_u *)"shcf");
+	if (idx3 >= 0 && !(options[idx3].flags & P_WAS_SET))
+	{
+	    p_shcf = (char_u *)"/s /c";
+	    options[idx3].def_val[VI_DEFAULT] = p_shcf;
+	}
+    }
 #endif
 
 #ifdef FEAT_TITLE
