Add doublequotes when special characters exist in launch script. Closes #420

This commit is contained in:
huangyuhui
2018-08-18 00:31:54 +08:00
committed by huanghongxun
parent 2e9c237367
commit 26a6a7d883
2 changed files with 28 additions and 23 deletions

View File

@@ -41,6 +41,12 @@ public final class CommandBuilder {
} }
} }
/**
* Parsing will ignore your manual escaping
*
* @param args commands
* @return this
*/
public CommandBuilder add(String... args) { public CommandBuilder add(String... args) {
for (String s : args) for (String s : args)
raw.add(new Item(s, true)); raw.add(new Item(s, true));
@@ -89,35 +95,27 @@ public final class CommandBuilder {
} }
private static String parseWindows(String s) { private static String parseWindows(String s) {
if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) String escape = " \t^&<>|";
if (s.charAt(0) != '"') { if (StringUtils.containsOne(s, escape.toCharArray()))
// The argument has not been quoted, add quotes. // The argument has not been quoted, add quotes.
return '"' + s.replace("\\", "\\\\").replace("\"", "\"\"") + '"'; return '"' + s
} else if (s.endsWith("\"")) { .replace("\\", "\\\\")
// The argument has already been quoted. .replace("\"", "\"\"")
return s; + '"';
} else {
// Unmatched quote for the argument.
throw new IllegalArgumentException();
}
else { else {
return s; return s;
} }
} }
private static String parseBash(String s) { private static String parseBash(String s) {
if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) String escaping = " \t\"!#$&'()*,;<=>?[\\]^`{|}~";
if (s.charAt(0) != '"') { String escaped = "\"$&`";
// The argument has not been quoted, add quotes. if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0 || StringUtils.containsOne(s, escaping.toCharArray())) {
return '"' + s.replace("\"", "\\\"") + '"'; // The argument has not been quoted, add quotes.
} else if (s.endsWith("\"")) { for (char ch : escaped.toCharArray())
// The argument has already been quoted. s = s.replace("" + ch, "\\" + ch);
return s; return '"' + s + '"';
} else { } else
// Unmatched quote for the argument.
throw new IllegalArgumentException();
}
else
return s; return s;
} }
} }

View File

@@ -176,6 +176,13 @@ public final class StringUtils {
return false; return false;
} }
public static boolean containsOne(String pattern, char... targets) {
for (char target : targets)
if (pattern.toLowerCase().indexOf(Character.toLowerCase(target)) >= 0)
return true;
return false;
}
public static List<String> tokenize(String str) { public static List<String> tokenize(String str) {
if (str == null) if (str == null)
return new LinkedList<>(); return new LinkedList<>();