From b432fc82c78bb890eca5938c7754950f089c7abe Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 23 Sep 2024 14:36:20 -0400 Subject: Add shell escaping for arrays of text --- environment.c | 1 + stdlib/shell.c | 11 +++++++++++ stdlib/shell.h | 1 + 3 files changed, 13 insertions(+) diff --git a/environment.c b/environment.c index 33b30c69..96c68c59 100644 --- a/environment.c +++ b/environment.c @@ -304,6 +304,7 @@ env_t *new_compilation_unit(CORD *libname) {"by_line", "Shell$by_line", "func(command:Shell)->(func()->Text?)?"}, {"escape_int", "Int$value_as_text", "func(i:Int)->Shell"}, {"escape_text", "Shell$escape_text", "func(text:Text)->Shell"}, + {"escape_text_array", "Shell$escape_text_array", "func(texts:[Text])->Shell"}, {"run_bytes", "Shell$run", "func(command:Shell)->[Byte]?"}, {"run", "Shell$run", "func(command:Shell)->Text?"}, )}, diff --git a/stdlib/shell.c b/stdlib/shell.c index 977fa512..28c85fb1 100644 --- a/stdlib/shell.c +++ b/stdlib/shell.c @@ -33,6 +33,17 @@ public Shell_t Shell$escape_text(Text_t text) return (Text_t){.length=shell_graphemes.length, .tag=TEXT_GRAPHEMES, .graphemes=shell_graphemes.data}; } +public Shell_t Shell$escape_text_array(Array_t texts) +{ + Array_t all_escaped = {}; + for (int64_t i = 0; i < texts.length; i++) { + Text_t raw = *(Text_t*)(texts.data + i*texts.stride); + Text_t escaped = Shell$escape_text(raw); + Array$insert(&all_escaped, &escaped, I(0), sizeof(Text_t)); + } + return Text$join(Text(" "), all_escaped); +} + public OptionalArray_t Shell$run_bytes(Shell_t command) { const char *cmd_str = Text$as_c_string(command); diff --git a/stdlib/shell.h b/stdlib/shell.h index 16d0b10d..14a086ef 100644 --- a/stdlib/shell.h +++ b/stdlib/shell.h @@ -17,6 +17,7 @@ OptionalClosure_t Shell$by_line(Shell_t command); Shell_t Shell$escape_text(Text_t text); +Shell_t Shell$escape_text_array(Array_t texts); OptionalArray_t Shell$run_bytes(Shell_t command); OptionalText_t Shell$run(Shell_t command); -- cgit v1.2.3