aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p@janouch.name>2018-10-27 21:25:11 +0200
committerPřemysl Janouch <p@janouch.name>2018-10-27 21:26:11 +0200
commit51ed172d5d18f682203aa28a546beda5cafa9e3b (patch)
tree39ff4ebe09a0e8b3e13f7fa22c37529b5934db90
parentffa5754b591db293db0ddb87ad8b8d4c5c88320e (diff)
downloadsdn-51ed172d5d18f682203aa28a546beda5cafa9e3b.tar.gz
sdn-51ed172d5d18f682203aa28a546beda5cafa9e3b.tar.xz
sdn-51ed172d5d18f682203aa28a546beda5cafa9e3b.zip
Only quote output when necessary
-rw-r--r--sdn.cpp12
1 files changed, 12 insertions, 0 deletions
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<string> {
vector<string> 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 == '\'')