summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Bergmann <stephan.bergmann@allotropia.de>2024-03-12 14:15:32 +0100
committerStephan Bergmann <stephan.bergmann@allotropia.de>2024-03-13 17:28:43 +0100
commit798d530daec69d6f81ad4ee10709c2c91d16dab3 (patch)
tree5303b32b2b191e53ec8ab85fd5d51edff888af6f
parent391b620507d137f50aad2fe84d42583b62761e28 (diff)
Support environment variable expansion in logging.ini
The syntax is a stripped-down subset of what is available for variable expansion in bootstrap ini-files: It only supports "${name}", to be replaced with the contents of an environment variable of that name (which itself is not expanded further). If no such environment variable exists, it is replaced with the empty string. If the name contains a NUL character, or if there is a syntax error (i.e., missing opening or closing curly brace), the corresponding text is left unchanged. Also, backslash quoting is supported to allow for verbatim, uninterpreted "${name}" content. But logging.ini is typically used to specify Windows-style LogFilePath=C:\log.txt pathnames containing backslashes, so restrict backslash quoting to just "\$" (replaced with "$") and "\\" (replaced with "\"), and leave all other backslashes alone. (It is not possible to reuse the bootstrap macro expansion code here, as that would cause recursive dependencies. Therefore, this only implements this stripped-down subset.) Change-Id: I8c949a1637a7f14e0672a9cc1c79014edfa336bd Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164759 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <stephan.bergmann@allotropia.de> (cherry picked from commit ef28f693351411c0d1651196b99e501acba7e7d5) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164743 Tested-by: allotropia jenkins <jenkins@allotropia.de>
-rw-r--r--sal/osl/all/log.cxx38
1 files changed, 37 insertions, 1 deletions
diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx
index 39ea42d7e130..84f88f259daa 100644
--- a/sal/osl/all/log.cxx
+++ b/sal/osl/all/log.cxx
@@ -116,7 +116,43 @@ char const* setEnvFromLoggingIniFile(const char* env, const char* key)
aKey = sLine.substr(0, n);
if (aKey != sWantedKey)
continue;
- _putenv_s(env, sLine.substr(n+1, sLine.length()).c_str());
+ std::string value(sLine, n+1, sLine.length());
+ for (std::size_t i = 0;;) {
+ i = value.find_first_of("\\$", i);
+ if (i == std::string::npos) {
+ break;
+ }
+ if (value[i] == '\\') {
+ if (i == value.size() - 1 || (value[i + 1] != '\\' && value[i + 1] != '$')) {
+ ++i;
+ continue;
+ }
+ value.erase(i, 1);
+ ++i;
+ } else {
+ if (i == value.size() - 1 || value[i + 1] != '{') {
+ ++i;
+ continue;
+ }
+ std::size_t i2 = value.find('}', i + 2);
+ if (i2 == std::string::npos) {
+ break;
+ }
+ std::string name(value, i + 2, i2 - (i + 2));
+ if (name.find('\0') != std::string::npos) {
+ i = i2 + 1;
+ continue;
+ }
+ char const * p = std::getenv(name.c_str());
+ if (p == nullptr) {
+ value.erase(i, i2 + 1 - i);
+ } else {
+ value.replace(i, i2 + 1 - i, p);
+ i += std::strlen(p);
+ }
+ }
+ }
+ _putenv_s(env, value.c_str());
sResult = std::getenv(env);
break;
}