Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public static String[] translateCommandline(String toProcess) throws Exception {
final int inQuote = 1;
final int inDoubleQuote = 2;
int state = normal;
StringTokenizer tok = new StringTokenizer(toProcess, "\"\' ", true);
StringTokenizer tok = new StringTokenizer(toProcess, "\"\' \t\n\r", true);
Vector<String> v = new Vector<String>();
StringBuilder current = new StringBuilder();

Expand All @@ -382,7 +382,10 @@ public static String[] translateCommandline(String toProcess) throws Exception {
state = inQuote;
} else if ("\"".equals(nextTok)) {
state = inDoubleQuote;
} else if (" ".equals(nextTok)) {
} else if (" ".equals(nextTok)
|| "\t".equals(nextTok)
|| "\n".equals(nextTok)
|| "\r".equals(nextTok)) {
if (current.length() != 0) {
v.addElement(current.toString());
current.setLength(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ void translateCommandline() throws Exception {
assertCmdLineArgs(new String[] {"foo", " ' ", "bar"}, "foo \" ' \" bar");
}

@Test
void translateCommandlineSplitsAtTabsAndLineBreaks() throws Exception {
assertCmdLineArgs(new String[] {"foo", "bar"}, "foo\tbar");
assertCmdLineArgs(new String[] {"foo", "bar"}, "foo\nbar");
assertCmdLineArgs(new String[] {"foo", "bar"}, "foo\rbar");
assertCmdLineArgs(new String[] {"foo", "bar"}, "foo\r\nbar");
assertCmdLineArgs(new String[] {"foo", "bar", "baz"}, " \t foo \t\n bar\r\n\tbaz \n");
assertCmdLineArgs(new String[] {}, " \t\r\n ");
}

@Test
void translateCommandlineKeepsWhitespaceInsideQuotes() throws Exception {
assertCmdLineArgs(new String[] {"foo", "a\tb\nc\r", "bar"}, "foo \"a\tb\nc\r\" bar");
assertCmdLineArgs(new String[] {"foo", "a\tb\nc\r", "bar"}, "foo 'a\tb\nc\r' bar");
assertCmdLineArgs(new String[] {"a\tb", "c"}, "'a\tb'\tc");
}

private void assertCmdLineArgs(String[] expected, String cmdLine) throws Exception {
String[] actual = CommandLineUtils.translateCommandline(cmdLine);
assertNotNull(actual);
Expand Down
Loading