From 2d5b1b24bf70a498e4c0b378704cfdb6471cc699 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 25 Oct 2017 13:01:50 +0200 Subject: Pass lists/attrsets to bash as (associative) arrays --- src/libutil/util.cc | 10 ++++++++++ src/libutil/util.hh | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'src/libutil') diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 3c98a61f9e50..9346d5dc4cf8 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -1142,6 +1142,16 @@ std::string toLower(const std::string & s) } +std::string shellEscape(const std::string & s) +{ + std::string r = "'"; + for (auto & i : s) + if (i == '\'') r += "'\\''"; else r += i; + r += '\''; + return r; +} + + void ignoreException() { try { diff --git a/src/libutil/util.hh b/src/libutil/util.hh index 6a66576e96ce..fccf5d854800 100644 --- a/src/libutil/util.hh +++ b/src/libutil/util.hh @@ -352,10 +352,8 @@ bool hasSuffix(const string & s, const string & suffix); std::string toLower(const std::string & s); -/* Escape a string that contains octal-encoded escape codes such as - used in /etc/fstab and /proc/mounts (e.g. "foo\040bar" decodes to - "foo bar"). */ -string decodeOctalEscaped(const string & s); +/* Escape a string as a shell word. */ +std::string shellEscape(const std::string & s); /* Exception handling in destructors: print an error message, then -- cgit 1.4.1