Skip to content

Commit

Permalink
replace shell_escape_windows with shell_escape_wincmd and escape_micr…
Browse files Browse the repository at this point in the history
…osoft_c_args from PR JuliaLang#34111
  • Loading branch information
mgkuhn committed Dec 23, 2019
1 parent 57dae26 commit a0449ae
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 78 deletions.
3 changes: 2 additions & 1 deletion stdlib/Distributed/src/Distributed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import Base: getindex, wait, put!, take!, fetch, isready, push!, length,
using Base: Process, Semaphore, JLOptions, buffer_writes, @sync_add,
VERSION_STRING, binding_module, atexit, julia_exename,
julia_cmd, AsyncGenerator, acquire, release, invokelatest,
shell_escape_posixly, uv_error, something, notnothing, isbuffered
shell_escape_posixly, shell_escape_wincmd, escape_microsoft_c_args,
uv_error, something, notnothing, isbuffered
using Base.Threads: Event

using Serialization, Sockets
Expand Down
78 changes: 1 addition & 77 deletions stdlib/Distributed/src/managers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ function launch_on_machine(manager::SSHManager, machine, cnt, params, launched,

any(c -> c == '"', exename) && throw(ArgumentError("invalid exename"))

remotecmd = shell_escape_windows(exename, exeflags...)
remotecmd = shell_escape_wincmd(escape_microsoft_c_args(exename, exeflags...))
if dir !== nothing && dir != ""
any(c -> c == '"', dir) && throw(ArgumentError("invalid dir"))
remotecmd = "pushd \"$(dir)\" && $remotecmd"
Expand Down Expand Up @@ -268,82 +268,6 @@ function launch_on_machine(manager::SSHManager, machine, cnt, params, launched,
end


function print_shell_escaped_windows(io, args::AbstractString...)::Nothing
first = true
quoted = false
for arg in args
first || write(io, ' ')
first = false
function isword(c::AbstractChar)
return 'a' <= c <= 'z' || 'A' <= c <= 'Z' || '0' <= c <= '9' ||
c == '\\' || c == '.' || c == '_' || c == '-' || c == ':' ||
c == '/' || c == '=' || c == '"' || c == '%'
end
quotes = !all(isword, arg) || arg == ""
if quotes; write(io, '"'); quoted = !quoted; end
backslashes = 0
for c in arg
if c == '\\'
backslashes += 1
else
if c == '"'
backslashes = backslashes * 2 + 1
quoted = !quoted
end
for j=1:backslashes
write(io, '\\')
end
backslashes = 0
isword(c) || quoted || write(io, '^') # for cmd.exe
write(io, c)
end
end
if quotes; backslashes *= 2; end
for j=1:backslashes
write(io, '\\')
end
if quotes; write(io, '"'); quoted = !quoted; end
end
end

"""
shell_escape_windows(args::AbstractString...)::String
Convert the collection of strings `args` into a Windows command line.
Windows passes the entire command line as a single string to the
application (unlike POSIX systems, where the shell splits the command
line into a list of arguments). Many Windows API applications
(including julia.exe), use the conventions of the [Microsoft C
runtime](https://docs.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments)
to split that command line into a list of strings.
This function implements the inverse of such a C runtime command-line
parser. It joins command-line arguments to be passed to a Windows
C/C++/Julia application into a command line, escaping or quoting meta
characters such as space, double quotes and backslash where needed.
In addition, this function also escapes meta characters processed by
`cmd.exe`: it places a ^ in front of any potential metacharacter that
follows an even number of quotation marks on the command line.
The percent sign (`%`) is not escaped such that shell variable
references (like `%USER%`) can still be substituted by `cmd.exe`.
Input strings should avoid ASCII control characters, as many of these
cannot be escaped (e.g., NUL, CR, LF).
# Example
```jldoctest
julia> println(Distributed.shell_escape_windows("a b", "\\"", "\\\\\\"", "\\\\", "\\\\ \\\\", "c"))
\"a b\" \\\" \\\\\\\" \\ \"\\ \\\\\" c
```
"""
shell_escape_windows(args::AbstractString...) = sprint(print_shell_escaped_windows, args...,
sizehint = (sum(length.(args)) + 3*length(args)))


function manage(manager::SSHManager, id::Integer, config::WorkerConfig, op::Symbol)
if op === :interrupt
ospid = config.ospid
Expand Down

0 comments on commit a0449ae

Please sign in to comment.