]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Support escaping dollar signs in configuration values
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 2 Jul 2023 08:44:16 +0000 (10:44 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 2 Jul 2023 09:28:26 +0000 (11:28 +0200)
doc/MANUAL.adoc
src/Util.cpp
unittest/test_Util.cpp

index 722c9d9a3c521eb5abd2d761f70a086aff3495c9..4d3c6686c806c0c02a8b7caadf2808621afa12b0 100644 (file)
@@ -371,6 +371,15 @@ See also the <<config_cache_dir,*cache_dir*>> 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
index 6ba1b3431d090df0764a38fd95f54a5d94863fd7..e00502fa541a4c4da4c1fbc775bdaa62f78556ac 100644 (file)
@@ -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;
 }
index fd02076b756fb7eecfd58fed54e0c3902d191d33..ae320edf3d073b64f03d19f8c61a94a9b322585b 100644 (file)
@@ -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");