Only quote output when necessary

This commit is contained in:
Přemysl Eric Janouch 2018-10-27 21:25:11 +02:00
parent ffa5754b59
commit 51ed172d5d
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 12 additions and 0 deletions

12
sdn.cpp
View File

@ -106,7 +106,19 @@ fun split (const string &s, const string &sep) -> vector<string> {
vector<string> result; split (s, sep, result); return result; 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 { fun shell_escape (const string &v) -> string {
if (!needs_shell_quoting (v))
return v;
string result; string result;
for (auto c : v) for (auto c : v)
if (c == '\'') if (c == '\'')