Skip to content

Commit

Permalink
Simplify batch arg quoting by ignoring invalid characters
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Sep 23, 2024
1 parent c87f557 commit 197ac56
Showing 1 changed file with 9 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@ private Path writeArgLineFile(
}

private void quoteShellArg(StringBuilder sb, String arg) {
// POSIX file names can be a bit more complicated and may need escaping
// in certain edge cases to remain valid.
sb.append('\'');
for (var i = 0; i < arg.length(); ++i) {
var c = arg.charAt(i);
Expand All @@ -374,38 +376,16 @@ private void quoteShellArg(StringBuilder sb, String arg) {
break;
}
}

sb.append('\'');
}

private void quoteBatchArg(StringBuilder sb, String arg) {
sb.append('"');

for (var i = 0; i < arg.length(); ++i) {
var c = arg.charAt(i);

switch (c) {
case '"':
sb.append("\"\"\"");
break;
case '%':
sb.append("%%");
break;
case '\r':
case '\t':
case '^':
case '&':
case '<':
case '>':
case '|':
sb.append('^').append(c);
break;
default:
sb.append(c);
break;
}
}

sb.append('"');
// All the escapable characters in batch files other than quotes
// are considered to be illegal characters in Windows file names,
// so we can make the assumption that we don't need to change much
// here.
sb.append('"')
.append(arg.replaceAll("\"", "\"\"\""))
.append('"');
}
}

0 comments on commit 197ac56

Please sign in to comment.