From 739c709334bccbd1e2bcd6c379044ef46819f470 Mon Sep 17 00:00:00 2001 From: Anton Makeev Date: Sun, 16 Jun 2013 18:56:34 +0200 Subject: [PATCH] Platform: handle escaped whitespaces in command lines --- .../configurations/CommandLineTokenizer.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/platform/util/src/com/intellij/execution/configurations/CommandLineTokenizer.java b/platform/util/src/com/intellij/execution/configurations/CommandLineTokenizer.java index 751cbbf4a988..5f7aa2a5037b 100644 --- a/platform/util/src/com/intellij/execution/configurations/CommandLineTokenizer.java +++ b/platform/util/src/com/intellij/execution/configurations/CommandLineTokenizer.java @@ -5,7 +5,7 @@ import java.util.List; import java.util.StringTokenizer; /** - * Splits input String to tokens being aware of quoted tokens, + * Splits input String to tokens being aware of quoted tokens ("foo bar") and escaped spaces (foo\ bar), * usually used for splitting command line to separate arguments that may contain space symbols. * Escaped symbols are not handled so there's no way to get token that itself contains quotation mark. */ @@ -15,9 +15,15 @@ public class CommandLineTokenizer extends StringTokenizer { // keep source level 1.4 private List myTokens = new ArrayList(); private int myCurrentToken = 0; + private boolean myHandleEscapedWhitespaces = false; public CommandLineTokenizer(String str) { + this(str, false); + } + + public CommandLineTokenizer(String str, boolean handleEscapedWhitespaces) { super(str, DEFAULT_DELIMITERS, true); + myHandleEscapedWhitespaces = handleEscapedWhitespaces; parseTokens(); } @@ -78,8 +84,18 @@ public class CommandLineTokenizer extends StringTokenizer { buffer.append(nextToken.substring(0, i)); nextToken = nextToken.substring(i + 1); } - buffer.append(nextToken); - if (quotationMarks % 2 == 1 && super.hasMoreTokens()) { + + boolean isEscapedWhitespace = false; + if (myHandleEscapedWhitespaces && quotationMarks == 0 && nextToken.endsWith("\\") && super.hasMoreTokens()) { + isEscapedWhitespace = true; + buffer.append(nextToken.substring(0, nextToken.length() - 1)); + buffer.append(super.nextToken()); + } + else { + buffer.append(nextToken); + } + + if ((isEscapedWhitespace || quotationMarks % 2 == 1) && super.hasMoreTokens()) { nextToken = super.nextToken(); } else { nextToken = null;