Skip to content

Commit

Permalink
Add parse_arguments function and replace the naive split on space
Browse files Browse the repository at this point in the history
  • Loading branch information
BKaperick committed Dec 17, 2023
1 parent 85a57ac commit a58e161
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/DaemonMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,45 @@ function serverRun(run, sock, shared, print_stack, fname, args, reviser)

end

"""
parse_arguments(args_str::String)
Parse the argument string handling quoted arguments, and escaped quotes correctly.
# Parameters
- shared: string of arguments separated by one or many spaces
"""
function parse_arguments(args_str::String)
args_out = []
quotes = Set(['\'','"'])
whitespace = Set([' '])
escape_chars = Set(['\\'])
in_quote = false
escaped = false
current = ""
for c in collect(args_str)
if ~escaped && c in quotes
in_quote = ~in_quote
elseif c in whitespace
if in_quote
current = string(current, c)
elseif length(current) > 0
push!(args_out, current)
current = ""
end
elseif ~(c in escape_chars)
current = string(current, c)
end
escaped = false
if c in escape_chars
escaped = true
end
end
push!(args_out, current)
return args_out
end

"""
serverRunFile(sock, shared)
Expand All @@ -389,7 +428,7 @@ function serverRunFile(sock, shared, print_stack, reviser)
dir = readline(sock)
fname = readline(sock)
args_str = readline(sock)
args = split(args_str, " ")
args = parse_arguments(args_str)

if !isempty(args) && isempty(args[1])
empty!(args)
Expand Down Expand Up @@ -581,7 +620,7 @@ function runargs(port=PORT)
println(stderr, "Error: file '$(ARGS[1])' doest not exist")
exit(1)
end

result = runfile(ARGS[1], args=ARGS[2:end], port=port)
exit(result)
end
Expand Down

0 comments on commit a58e161

Please sign in to comment.