From: Joel Rosdahl Date: Sun, 2 Jul 2023 08:44:16 +0000 (+0200) Subject: feat: Support escaping dollar signs in configuration values X-Git-Tag: v4.9~155 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c92b683b3a2ab1215fb4d9a99e3f42400562d3c2;p=thirdparty%2Fccache.git feat: Support escaping dollar signs in configuration values --- diff --git a/doc/MANUAL.adoc b/doc/MANUAL.adoc index 722c9d9a3..4d3c6686c 100644 --- a/doc/MANUAL.adoc +++ b/doc/MANUAL.adoc @@ -371,6 +371,15 @@ See also the <> configuration option for how the cache directory location is determined. +=== Configuration value syntax + +All configuration values support expansion of environment variables. The syntax +is similar to POSIX shell syntax: `$VAR` or `${VAR}`. Both variants will expand +to the value of the environment variable `VAR`. + +Two consecutive dollar signs (`$$`) will expand to a single dollar sign (`$`). + + === Configuration file syntax Configuration files are in a simple "`key = value`" format, one option per @@ -382,7 +391,6 @@ is whitespace surrounding keys and values. Example: max_size = 10G ------------------------------------------------------------------------------- - === Boolean values Some configuration options are boolean values (i.e. truth values). In a diff --git a/src/Util.cpp b/src/Util.cpp index 6ba1b3431..e00502fa5 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -439,10 +439,18 @@ expand_environment_variables(const std::string& str) std::string result; const char* left = str.c_str(); const char* right = left; + while (*right) { if (*right == '$') { result.append(left, right - left); + if (*(right + 1) == '$') { + result += '$'; + right += 2; + left = right; + continue; + } + left = right + 1; bool curly = *left == '{'; if (curly) { @@ -474,6 +482,7 @@ expand_environment_variables(const std::string& str) } ++right; } + result += left; return result; } diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index fd02076b7..ae320edf3 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -187,7 +187,9 @@ TEST_CASE("Util::expand_environment_variables") CHECK(Util::expand_environment_variables("") == ""); CHECK(Util::expand_environment_variables("$FOO") == "bar"); - CHECK(Util::expand_environment_variables("$") == "$"); + CHECK(Util::expand_environment_variables("$$FOO") == "$FOO"); + CHECK(Util::expand_environment_variables("$$$FOO") == "$bar"); + CHECK(Util::expand_environment_variables("$ $$ $") == "$ $ $"); CHECK(Util::expand_environment_variables("$FOO $FOO:$FOO") == "bar bar:bar"); CHECK(Util::expand_environment_variables("x$FOO") == "xbar"); CHECK(Util::expand_environment_variables("${FOO}x") == "barx");