From a58e161e09feaefa8586d1d25e0cc7a6c36d8834 Mon Sep 17 00:00:00 2001 From: bkaperick Date: Sun, 17 Dec 2023 11:26:02 -0500 Subject: [PATCH] Add parse_arguments function and replace the naive split on space --- src/DaemonMode.jl | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/DaemonMode.jl b/src/DaemonMode.jl index 1126886..30d53c0 100644 --- a/src/DaemonMode.jl +++ b/src/DaemonMode.jl @@ -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) @@ -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) @@ -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