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

View File

@@ -176,6 +176,13 @@ public final class StringUtils {
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) {
if (str == null)
return new LinkedList<>();