Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell_escape_posixly: fix handling of empty argument #30636

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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