Skip to content

Commit

Permalink
shell_escape_posixly: fix handling of empty argument (JuliaLang#30636)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash authored Jan 8, 2019
1 parent 6f4eb43 commit 6b108ec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function show(io::IO, cmd::Cmd)
print_env && (print(io, ","); show(io, cmd.env))
print_dir && (print(io, "; dir="); show(io, cmd.dir))
(print_dir || print_env) && print(io, ")")
nothing
end

function show(io::IO, cmds::Union{OrCmds,ErrOrCmds})
Expand Down
16 changes: 9 additions & 7 deletions base/shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ function shell_split(s::AbstractString)
end

function print_shell_word(io::IO, word::AbstractString, special::AbstractString = "")
if isempty(word)
print(io, "''")
end
has_single = false
has_special = false
for c in word
Expand All @@ -141,7 +138,9 @@ function print_shell_word(io::IO, word::AbstractString, special::AbstractString
end
end
end
if !has_special
if isempty(word)
print(io, "''")
elseif !has_special
print(io, word)
elseif !has_single
print(io, '\'', word, '\'')
Expand All @@ -155,6 +154,7 @@ function print_shell_word(io::IO, word::AbstractString, special::AbstractString
end
print(io, '"')
end
nothing
end

function print_shell_escaped(io::IO, cmd::AbstractString, args::AbstractString...;
Expand Down Expand Up @@ -186,7 +186,7 @@ julia> Base.shell_escape("echo", "this", "&&", "that")
```
"""
shell_escape(args::AbstractString...; special::AbstractString="") =
sprint(io->print_shell_escaped(io, args..., special=special))
sprint((io, args...) -> print_shell_escaped(io, args..., special=special), args...)


function print_shell_escaped_posixly(io::IO, args::AbstractString...)
Expand Down Expand Up @@ -215,7 +215,9 @@ function print_shell_escaped_posixly(io::IO, args::AbstractString...)
end
return true
end
if all(isword, arg)
if isempty(arg)
print(io, "''")
elseif all(isword, arg)
have_single && (arg = replace(arg, '\'' => "\\'"))
have_double && (arg = replace(arg, '"' => "\\\""))
print(io, arg)
Expand Down Expand Up @@ -243,4 +245,4 @@ julia> Base.shell_escape_posixly("echo", "this", "&&", "that")
```
"""
shell_escape_posixly(args::AbstractString...) =
sprint(io->print_shell_escaped_posixly(io, args...))
sprint(print_shell_escaped_posixly, args...)
8 changes: 4 additions & 4 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,13 @@ end

# Test shell_escape printing quoting
# Backticks should automatically quote where necessary
let cmd = ["foo bar", "baz", "a'b", "a\"b", "a\"b\"c", "-L/usr/+", "a=b", "``", "\$", "&&", "z"]
let cmd = ["foo bar", "baz", "a'b", "a\"b", "a\"b\"c", "-L/usr/+", "a=b", "``", "\$", "&&", "", "z"]
@test string(`$cmd`) ==
"""`'foo bar' baz "a'b" 'a"b' 'a"b"c' -L/usr/+ a=b \\`\\` '\$' '&&' z`"""
"""`'foo bar' baz "a'b" 'a"b' 'a"b"c' -L/usr/+ a=b \\`\\` '\$' '&&' '' z`"""
@test Base.shell_escape(`$cmd`) ==
"""'foo bar' baz "a'b" 'a"b' 'a"b"c' -L/usr/+ a=b `` '\$' && z"""
"""'foo bar' baz "a'b" 'a"b' 'a"b"c' -L/usr/+ a=b `` '\$' && '' z"""
@test Base.shell_escape_posixly(`$cmd`) ==
"""'foo bar' baz a\\'b a\\"b 'a"b"c' -L/usr/+ a=b '``' '\$' '&&' z"""
"""'foo bar' baz a\\'b a\\"b 'a"b"c' -L/usr/+ a=b '``' '\$' '&&' '' z"""
end
let cmd = ["foo=bar", "baz"]
@test string(`$cmd`) == "`foo=bar baz`"
Expand Down

0 comments on commit 6b108ec

Please sign in to comment.