Skip to content

Commit

Permalink
feat: Support escaping dollar signs in configuration values
Browse files Browse the repository at this point in the history
  • Loading branch information
jrosdahl committed Jul 2, 2023
1 parent 3c7fa42 commit c92b683
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
10 changes: 9 additions & 1 deletion doc/MANUAL.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -474,6 +482,7 @@ expand_environment_variables(const std::string& str)
}
++right;
}

result += left;
return result;
}
Expand Down
4 changes: 3 additions & 1 deletion unittest/test_Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit c92b683

Please sign in to comment.