From a58e161e09feaefa8586d1d25e0cc7a6c36d8834 Mon Sep 17 00:00:00 2001 From: bkaperick Date: Sun, 17 Dec 2023 11:26:02 -0500 Subject: [PATCH 1/4] 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 From 6eaa259f3c0a80d73ca494014c1ae14165bb49e5 Mon Sep 17 00:00:00 2001 From: bkaperick Date: Sun, 17 Dec 2023 11:45:05 -0500 Subject: [PATCH 2/4] We also need to wrap each arg in quotes when sent to socket --- src/DaemonMode.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/DaemonMode.jl b/src/DaemonMode.jl index 30d53c0..7c6f5b5 100644 --- a/src/DaemonMode.jl +++ b/src/DaemonMode.jl @@ -249,7 +249,7 @@ end function serverRun(run, sock, shared, print_stack, fname, args, reviser) error = false - + println("args = $args") try reviser() @@ -429,6 +429,7 @@ function serverRunFile(sock, shared, print_stack, reviser) fname = readline(sock) args_str = readline(sock) args = parse_arguments(args_str) + println("parsing args: $args_str => $args") if !isempty(args) && isempty(args[1]) empty!(args) @@ -547,7 +548,7 @@ function runfile(fname::AbstractString; args=String[], port = PORT, output=stdou println(sock, token_runfile) println(sock, pwd()) println(sock, fcompletename) - println(sock, join(args, " ")) + println(sock, string("\"", join(args, "\" \""), "\"")) line = readline(sock) token_size = length(token_ok_end) From ae21596241e6606c756596a6f7aeb9d73dbabdf4 Mon Sep 17 00:00:00 2001 From: bkaperick Date: Sun, 17 Dec 2023 12:13:38 -0500 Subject: [PATCH 3/4] Added test --- test/test_argparse.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 test/test_argparse.jl diff --git a/test/test_argparse.jl b/test/test_argparse.jl new file mode 100644 index 0000000..5abc7ca --- /dev/null +++ b/test/test_argparse.jl @@ -0,0 +1,10 @@ +using Test +using DaemonMode + +@testset "Test argument parsing" begin + @test DaemonMode.parse_arguments("arg1 \"arg2 is in quotes\" arg3") == ["arg1", "arg2 is in quotes", "arg3"] + + @test DaemonMode.parse_arguments("arg1 arg2 \"arg3 is in quotes\"") == ["arg1", "arg2", "arg3 is in quotes"] + + @test DaemonMode.parse_arguments("arg1 arg2 \"arg3 has escaped \\\"quotes\\\"") == ["arg1", "arg2", "arg3 has escaped \"quotes\""] +end From 1e41546a29fc450acbbfef85f7fa2e87b51fabc9 Mon Sep 17 00:00:00 2001 From: bkaperick Date: Sun, 17 Dec 2023 12:16:58 -0500 Subject: [PATCH 4/4] Clean --- src/DaemonMode.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/DaemonMode.jl b/src/DaemonMode.jl index 7c6f5b5..69ee10e 100644 --- a/src/DaemonMode.jl +++ b/src/DaemonMode.jl @@ -249,7 +249,7 @@ end function serverRun(run, sock, shared, print_stack, fname, args, reviser) error = false - println("args = $args") + try reviser() @@ -429,7 +429,6 @@ function serverRunFile(sock, shared, print_stack, reviser) fname = readline(sock) args_str = readline(sock) args = parse_arguments(args_str) - println("parsing args: $args_str => $args") if !isempty(args) && isempty(args[1]) empty!(args) @@ -621,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