fix(launch): not escape spaces in exported shell script. Closes #1541.

This commit is contained in:
huanghongxun 2022-08-29 00:19:32 +08:00
parent a80fc34ef1
commit 3c3c28aaa5
2 changed files with 7 additions and 7 deletions

View File

@ -601,7 +601,7 @@ public class DefaultLauncher extends Launcher {
writer.write("set APPDATA=" + options.getGameDir().getAbsoluteFile().getParent());
writer.newLine();
for (Map.Entry<String, String> entry : getEnvVars().entrySet()) {
writer.write("set " + entry.getKey() + "=" + entry.getValue());
writer.write("set " + entry.getKey() + "=" + CommandBuilder.toBatchStringLiteral(entry.getValue()));
writer.newLine();
}
writer.newLine();
@ -610,7 +610,7 @@ public class DefaultLauncher extends Launcher {
writer.write("#!/usr/bin/env bash");
writer.newLine();
for (Map.Entry<String, String> entry : getEnvVars().entrySet()) {
writer.write("export " + entry.getKey() + "=" + entry.getValue());
writer.write("export " + entry.getKey() + "=" + CommandBuilder.toShellStringLiteral(entry.getValue()));
writer.newLine();
}
if (commandLine.tempNativeFolder != null) {

View File

@ -47,9 +47,9 @@ public final class CommandBuilder {
private String parse(String s) {
if (OperatingSystem.WINDOWS == os) {
return parseBatch(s);
return toBatchStringLiteral(s);
} else {
return parseShell(s);
return toShellStringLiteral(s);
}
}
@ -164,7 +164,7 @@ public final class CommandBuilder {
@Override
public String toString() {
return parse ? (OperatingSystem.WINDOWS == OperatingSystem.CURRENT_OS ? parseBatch(arg) : parseShell(arg)) : arg;
return parse ? (OperatingSystem.WINDOWS == OperatingSystem.CURRENT_OS ? toBatchStringLiteral(arg) : toShellStringLiteral(arg)) : arg;
}
}
@ -206,7 +206,7 @@ public final class CommandBuilder {
return true;
}
private static String parseBatch(String s) {
public static String toBatchStringLiteral(String s) {
String escape = " \t\"^&<>|";
if (StringUtils.containsOne(s, escape.toCharArray()))
// The argument has not been quoted, add quotes.
@ -219,7 +219,7 @@ public final class CommandBuilder {
}
}
private static String parseShell(String s) {
public static String toShellStringLiteral(String s) {
String escaping = " \t\"!#$&'()*,;<=>?[\\]^`{|}~";
String escaped = "\"$&`";
if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0 || StringUtils.containsOne(s, escaping.toCharArray())) {