Skip to content

Commit

Permalink
shell_parse: deprecate unescaped chars "#{}()[]<>|&*?~;" in commands
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jan 3, 2017
1 parent b3dfd0d commit 4c271fc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions base/managers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function launch_on_machine(manager::SSHManager, machine, cnt, params, launched,
cmd = `cd $dir '&&' $tval $exename $exeflags`

# shell login (-l) with string command (-c) to launch julia process
cmd = `sh -l -c $(shell_escape(cmd))`
cmd = `sh -l -c $(shell_escape(cmd, special = ""))`

# remote launch with ssh with given ssh flags / host / port information
# -T → disable pseudo-terminal allocation
Expand All @@ -195,7 +195,7 @@ function launch_on_machine(manager::SSHManager, machine, cnt, params, launched,
# forwarded connections are causing collisions
# -n → Redirects stdin from /dev/null (actually, prevents reading from stdin).
# Used when running ssh in the background.
cmd = `ssh -T -a -x -o ClearAllForwardings=yes -n $sshflags $host $(shell_escape(cmd))`
cmd = `ssh -T -a -x -o ClearAllForwardings=yes -n $sshflags $host $(shell_escape(cmd, special = ""))`

# launch the remote Julia process

Expand Down
3 changes: 2 additions & 1 deletion base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ end
hash(x::AndCmds, h::UInt) = hash(x.a, hash(x.b, h))
==(x::AndCmds, y::AndCmds) = x.a == y.a && x.b == y.b

shell_escape(cmd::Cmd) = shell_escape(cmd.exec...)
shell_escape(cmd::Cmd; special::AbstractString=shell_special) =
shell_escape(cmd.exec..., special=special)

function show(io::IO, cmd::Cmd)
print_env = cmd.env !== nothing
Expand Down
22 changes: 15 additions & 7 deletions base/shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## shell-like command parsing ##

const shell_special = "#{}()[]<>|&*?~;"

function shell_parse(str::AbstractString, interpolate::Bool=true)
s = lstrip(str)
# strips the end but respects the space when the string ends with "\\ "
Expand Down Expand Up @@ -92,6 +94,8 @@ function shell_parse(str::AbstractString, interpolate::Bool=true)
update_arg(s[i:j-1]); i = k
c, k = next(s,k)
end
elseif !in_single_quotes && !in_double_quotes && c in shell_special
depwarn("special characters \"$shell_special\" should now be quoted in commands", :shell_parse)
end
j = k
end
Expand Down Expand Up @@ -122,14 +126,14 @@ function shell_split(s::AbstractString)
args
end

function print_shell_word(io::IO, word::AbstractString)
function print_shell_word(io::IO, word::AbstractString, special::AbstractString = shell_special)
if isempty(word)
print(io, "''")
end
has_single = false
has_special = false
for c in word
if isspace(c) || c=='\\' || c=='\'' || c=='"' || c=='$'
if isspace(c) || c=='\\' || c=='\'' || c=='"' || c=='$' || c in special
has_special = true
if c == '\''
has_single = true
Expand All @@ -152,13 +156,17 @@ function print_shell_word(io::IO, word::AbstractString)
end
end

function print_shell_escaped(io::IO, cmd::AbstractString, args::AbstractString...)
print_shell_word(io, cmd)
function print_shell_escaped(
io::IO, cmd::AbstractString, args::AbstractString...;
special::AbstractString=shell_special
)
print_shell_word(io, cmd, special)
for arg in args
print(io, ' ')
print_shell_word(io, arg)
print_shell_word(io, arg, special)
end
end
print_shell_escaped(io::IO) = nothing
print_shell_escaped(io::IO; special::String=shell_special) = nothing

shell_escape(args::AbstractString...) = sprint(print_shell_escaped, args...)
shell_escape(args::AbstractString...; special::AbstractString=shell_special) =
sprint(io->print_shell_escaped(io, args..., special=special))

0 comments on commit 4c271fc

Please sign in to comment.