From 51ed172d5d18f682203aa28a546beda5cafa9e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sat, 27 Oct 2018 21:25:11 +0200 Subject: Only quote output when necessary --- sdn.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'sdn.cpp') diff --git a/sdn.cpp b/sdn.cpp index aece1fc..e880ed5 100644 --- a/sdn.cpp +++ b/sdn.cpp @@ -106,7 +106,19 @@ fun split (const string &s, const string &sep) -> vector { vector result; split (s, sep, result); return result; } +fun needs_shell_quoting (const string &v) -> bool { + // IEEE Std 1003.1 sh + the exclamation mark because of csh/bash + // history expansion, implicitly also the NUL character + for (auto c : v) + if (strchr ("|&;<>()$`\\\"' \t\n" "*?[#˜=%" "!", c)) + return true; + return false; +} + fun shell_escape (const string &v) -> string { + if (!needs_shell_quoting (v)) + return v; + string result; for (auto c : v) if (c == '\'') -- cgit v1.2.3