subprocess-win: clarify argument escaping logic

This bit always seemed confusing to me.
This commit is contained in:
James Ross-Gowan 2015-03-24 15:40:35 +11:00
parent 54cc610fde
commit 603a0f733f
1 changed files with 10 additions and 10 deletions

View File

@ -54,22 +54,22 @@ static void write_arg(bstr *cmdline, char *arg)
for (int pos = 0; arg[pos]; pos++) {
switch (arg[pos]) {
case '\\':
// Count backslashes that appear in a row
// Count consecutive backslashes
num_slashes++;
break;
case '"':
// Write the argument up to the point before the quote
bstr_xappend(NULL, cmdline, (struct bstr){arg, pos});
// Double preceding slashes
for (int i = 0; i < num_slashes; i++)
bstr_xappend(NULL, cmdline, bstr0("\\"));
// Escape the following quote
bstr_xappend(NULL, cmdline, bstr0("\\"));
arg += pos;
pos = 0;
// Double backslashes preceding the quote
for (int i = 0; i < num_slashes; i++)
bstr_xappend(NULL, cmdline, bstr0("\\"));
num_slashes = 0;
// Escape the quote itself
bstr_xappend(NULL, cmdline, bstr0("\\"));
break;
default:
num_slashes = 0;
@ -79,7 +79,7 @@ static void write_arg(bstr *cmdline, char *arg)
// Write the rest of the argument
bstr_xappend(NULL, cmdline, bstr0(arg));
// Double slashes that appear at the end of the string
// Double backslashes at the end of the argument
for (int i = 0; i < num_slashes; i++)
bstr_xappend(NULL, cmdline, bstr0("\\"));