Proper double-quote and backslash escaping for windows command line arguments. Fixes #245.
parent
871dfb9a3a
commit
63fa01fd4f
|
@ -18,6 +18,27 @@ IN: io.launcher.windows.tests
|
||||||
|
|
||||||
[ "\"\\\"hi you\\\"\"" ] [ { "\"hi you\"" } join-arguments ] unit-test
|
[ "\"\\\"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>
|
<process>
|
||||||
|
|
|
@ -58,9 +58,26 @@ TUPLE: CreateProcess-args
|
||||||
0 count-trailing-backslashes
|
0 count-trailing-backslashes
|
||||||
2 * CHAR: \\ <repetition> append ;
|
2 * CHAR: \\ <repetition> append ;
|
||||||
|
|
||||||
|
! Find groups of \, groups of \ followed by ", or naked "
|
||||||
: escape-double-quote ( str -- newstr )
|
: 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-argument ( str -- newstr )
|
||||||
escape-double-quote
|
escape-double-quote
|
||||||
CHAR: \s over member? [
|
CHAR: \s over member? [
|
||||||
|
|
Loading…
Reference in New Issue