summaryrefslogtreecommitdiffstats
path: root/stream.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-09-01 20:36:52 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-09-01 20:36:52 -0700
commita5d5536fcf2fe616b8c0941df0bc92f3bd9761ba (patch)
tree59d522eb4ec987ed034707d2644827cb4bc051e8 /stream.c
parent9c0e5743188588ece98197880a3f1174170dc563 (diff)
downloadtxr-a5d5536fcf2fe616b8c0941df0bc92f3bd9761ba.tar.gz
txr-a5d5536fcf2fe616b8c0941df0bc92f3bd9761ba.tar.bz2
txr-a5d5536fcf2fe616b8c0941df0bc92f3bd9761ba.zip
New functions for shell escaping.
* stream.c (sh_esc, sh_esc_all, sh_esc_dq, sh_esc_sq): New static functions. (stream_init): sh-esc, sh-esc-all, sh-esc-dq, sh-esc-sq: Intrinsics registered. * tests/018/sh-esc.tl: New file. * txr.1: Documented.
Diffstat (limited to 'stream.c')
-rw-r--r--stream.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/stream.c b/stream.c
index f948e988..02aec9ee 100644
--- a/stream.c
+++ b/stream.c
@@ -4838,6 +4838,26 @@ static val sh(val command)
#endif
+static val sh_esc(val string)
+{
+ return str_esc(lit("|&;<>()$`\\\"' \t\n*?[#~"), chr('\\'), string);
+}
+
+static val sh_esc_all(val string)
+{
+ return str_esc(lit("|&;<>()$`\\\"' \t\n*?[#~=%"), chr('\\'), string);
+}
+
+static val sh_esc_dq(val string)
+{
+ return str_esc(lit("$`\\\"\n"), chr('\\'), string);
+}
+
+static val sh_esc_sq(val string)
+{
+ return str_esc(lit("'"), lit("'\\'"), string);
+}
+
val remove_path(val path, val throw_on_error)
{
val self = lit("remove-path");
@@ -5556,6 +5576,10 @@ void stream_init(void)
reg_fun(intern(lit("sh"), user_package), func_n1(sh));
reg_fun(intern(lit("run"), user_package), func_n2o(run, 1));
#endif
+ reg_fun(intern(lit("sh-esc"), user_package), func_n1(sh_esc));
+ reg_fun(intern(lit("sh-esc-all"), user_package), func_n1(sh_esc_all));
+ reg_fun(intern(lit("sh-esc-dq"), user_package), func_n1(sh_esc_dq));
+ reg_fun(intern(lit("sh-esc-sq"), user_package), func_n1(sh_esc_sq));
reg_fun(intern(lit("remove-path"), user_package), func_n2o(remove_path, 1));
reg_fun(intern(lit("rename-path"), user_package), func_n2(rename_path));
reg_fun(intern(lit("open-files"), user_package), func_n3o(open_files, 1));