From 03b701c9a53650a8a9dc6ac3f16bcb0b7024ce10 Mon Sep 17 00:00:00 2001 From: Jonah Graham Date: Mon, 14 Nov 2016 23:42:55 +0000 Subject: [PATCH] Bug 494246: prepare command line arguments properly for Windows GDB Includes splitting out and expanding CommandLineArgsTest from LaunchConfigurationAndRestartTest. Change-Id: I19fa97a847d908c1c780ca767cf688f26a51d684 Signed-off-by: Jonah Graham --- .../eclipse/cdt/utils/CommandLineUtil.java | 164 +++++++++++- .../command/commands/MIGDBSetArgs.java | 51 +--- .../dsf/gdb/tests/CommandLineArgsTest.java | 243 ++++++++++++++++++ .../LaunchConfigurationAndRestartTest.java | 114 +------- .../cdt/tests/dsf/gdb/tests/SuiteGdb.java | 1 + 5 files changed, 421 insertions(+), 152 deletions(-) create mode 100644 dsf-gdb/org.eclipse.cdt.tests.dsf.gdb/src/org/eclipse/cdt/tests/dsf/gdb/tests/CommandLineArgsTest.java diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CommandLineUtil.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CommandLineUtil.java index 77d19531d5a..4928b7e1f38 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CommandLineUtil.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CommandLineUtil.java @@ -24,14 +24,18 @@ import org.eclipse.osgi.service.environment.Constants; */ public class CommandLineUtil { - public static String[] argumentsToArray(String line) { + private static boolean isWindows() { boolean osWin; try { osWin = Platform.getOS().equals(Constants.OS_WIN32); } catch (Exception e) { osWin = false; } - if (osWin) { + return osWin; + } + + public static String[] argumentsToArray(String line) { + if (isWindows()) { return argumentsToArrayWindowsStyle(line); } else { return argumentsToArrayUnixStyle(line); @@ -260,4 +264,160 @@ public class CommandLineUtil { } return aList.toArray(new String[aList.size()]); } + + /** + * Converts argument array to a string suitable for passing to Bash like: + * + * This process reverses {@link #argumentsToArray(String)}, but does not + * restore the exact same results. + * + * @param args + * the arguments to convert and escape + * @param encodeNewline + * true if newline (\r or + * \n) should be encoded + * + * @return args suitable for passing to some process that decodes the string + * into an argument array + * @since 6.2 + */ + public static String argumentsToString(String[] args, boolean encodeNewline) { + if (isWindows()) { + return argumentsToStringWindowsCreateProcess(args, encodeNewline); + } else { + // XXX: Bug 507568: We are currently using incorrect assumption that + // shell is always bash. AFAIK this is only problematic when + // encoding newlines + return argumentsToStringBash(args, encodeNewline); + } + + } + + /** + * Converts argument array to a string suitable for passing to Bash like: + * + *
+	 * /bin/bash -c <args>
+	 * 
+ * + * In this case the arguments array passed to exec or equivalent will be: + * + *
+	 * argv[0] = "/bin/bash"
+	 * argv[1] = "-c"
+	 * argv[2] = argumentsToStringBashStyle(argumentsAsArray)
+	 * 
+ * + * Replace and concatenate all occurrences of: + *