Bug 387504 comment 13: Bugs in program argument parsing (compared to command line)

Reverted special treatment of Windows arguments in DebugPlugin.exec(..)
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractVMRunner.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractVMRunner.java
index 8f6a1aa..6631513 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractVMRunner.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/AbstractVMRunner.java
@@ -18,6 +18,7 @@
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.variables.IStringVariableManager;
 import org.eclipse.core.variables.VariablesPlugin;
@@ -26,6 +27,7 @@
 import org.eclipse.debug.core.model.IProcess;
 import org.eclipse.jdt.internal.launching.LaunchingMessages;
 import org.eclipse.jdt.internal.launching.LaunchingPlugin;
+import org.eclipse.osgi.service.environment.Constants;
 
 /**
  * Abstract implementation of a VM runner.
@@ -69,6 +71,7 @@
 	 * @see DebugPlugin#exec(String[], File)
 	 */
 	protected Process exec(String[] cmdLine, File workingDirectory) throws CoreException {
+		cmdLine = quoteWindowsArgs(cmdLine);
 		return DebugPlugin.exec(cmdLine, workingDirectory);
 	}
 	
@@ -84,8 +87,43 @@
 	 * @see DebugPlugin#exec(String[], File, String[])
 	 */
 	protected Process exec(String[] cmdLine, File workingDirectory, String[] envp) throws CoreException {
+		cmdLine = quoteWindowsArgs(cmdLine);
 		return DebugPlugin.exec(cmdLine, workingDirectory, envp);
 	}	
+
+	private static String[] quoteWindowsArgs(String[] cmdLine) {
+		// see https://bugs.eclipse.org/387504 , workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6511002
+		if (Platform.getOS().equals(Constants.OS_WIN32)) {
+			String[] winCmdLine = new String[cmdLine.length];
+			for (int i = 0; i < cmdLine.length; i++) {
+				winCmdLine[i] = winQuote(cmdLine[i]);
+			}
+			cmdLine = winCmdLine;
+		}
+		return cmdLine;
+	}
+	
+	
+	private static boolean needsQuoting(String s) {
+		int len = s.length();
+		if (len == 0) // empty string has to be quoted
+			return true;
+		for (int i = 0; i < len; i++) {
+			switch (s.charAt(i)) {
+				case ' ': case '\t': case '\\': case '"':
+					return true;
+			}
+		}
+		return false;
+	}
+
+	private static String winQuote(String s) {
+		if (! needsQuoting(s))
+			return s;
+		s = s.replaceAll("([\\\\]*)\"", "$1$1\\\\\""); //$NON-NLS-1$ //$NON-NLS-2$
+		s = s.replaceAll("([\\\\]*)\\z", "$1$1"); //$NON-NLS-1$ //$NON-NLS-2$
+		return "\"" + s + "\""; //$NON-NLS-1$ //$NON-NLS-2$
+	}
 	
 	/**
 	 * Returns the given array of strings as a single space-delimited string.