Proper double-quote and backslash escaping for windows command line arguments. Fixes #245.

db4
Doug Coleman 2011-10-11 12:21:04 -07:00
parent 871dfb9a3a
commit 63fa01fd4f
2 changed files with 39 additions and 1 deletions

View File

@ -18,6 +18,27 @@ IN: io.launcher.windows.tests
[ "\"\\\"hi you\\\"\"" ] [ { "\"hi you\"" } join-arguments ] unit-test
! Commented line -- what should appear on the command line
! \foo\\bar\\\bas\ -> \foo\\bar\\\bas\
[ "\\foo\\\\bar\\\\\\bas\\" ]
[ { "\\foo\\\\bar\\\\\\bas\\" } join-arguments ] unit-test
! \"foo"\\bar\\\bas\ -> \\\"foo\"\\bar\\\bas\
[ "\\\\\\\"foo\\\"\\\\bar\\\\\\bas\\" ]
[ { "\\\"foo\"\\\\bar\\\\\\bas\\" } join-arguments ] unit-test
! \foo\\"bar"\\\bas\ -> \foo\\\\\"bar\"\\\bas\
[ "\\foo\\\\\\\\\\\"bar\\\"\\\\\\bas\\" ]
[ { "\\foo\\\\\"bar\"\\\\\\bas\\" } join-arguments ] unit-test
! \foo\\bar\\\"bas"\ -> \foo\\bar\\\\\\\"bas\"\
[ "\\foo\\\\bar\\\\\\\\\\\\\\\"bas\\\"\\" ]
[ { "\\foo\\\\bar\\\\\\\"bas\"\\" } join-arguments ] unit-test
! \foo\\bar bar\\\bas\ -> "\foo\\bar bar\\\bas\\"
[ "\"\\foo\\\\bar bar\\\\\\bas\\\\\"" ]
[ { "\\foo\\\\bar bar\\\\\\bas\\" } join-arguments ] unit-test
[ ] [
<process>

View File

@ -58,9 +58,26 @@ TUPLE: CreateProcess-args
0 count-trailing-backslashes
2 * CHAR: \\ <repetition> append ;
! Find groups of \, groups of \ followed by ", or naked "
: escape-double-quote ( str -- newstr )
"\"" split "\\\"" join ;
[
{ [ drop CHAR: \ = ] [ nip "\\\"" member? ] } 2&&
] monotonic-split [
dup last CHAR: " = [
dup length 1 > [
! String of backslashes + double-quote
length 1 - 2 * CHAR: \ <repetition> "\\\"" append
] [
! Single double-quote
drop "\\\""
] if
] when
] map "" concat-as ;
! Naked double-quotes get a backslash before them
! Backslashes before a double-quote get doubled in the output
! If there's a space, double trailing backslashes and surround by quotes
! See http://msdn.microsoft.com/en-us/library/ms647232.aspx
: escape-argument ( str -- newstr )
escape-double-quote
CHAR: \s over member? [