sal/osl/all/log.cxx |   38 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

New commits:
commit ef28f693351411c0d1651196b99e501acba7e7d5
Author:     Stephan Bergmann <stephan.bergm...@allotropia.de>
AuthorDate: Tue Mar 12 14:15:32 2024 +0100
Commit:     Stephan Bergmann <stephan.bergm...@allotropia.de>
CommitDate: Wed Mar 13 14:06:13 2024 +0100

    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.bergm...@allotropia.de>

diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx
index a8b86e3e7e79..3f92e9a62ec9 100644
--- a/sal/osl/all/log.cxx
+++ b/sal/osl/all/log.cxx
@@ -113,7 +113,43 @@ char const* setEnvFromLoggingIniFile(const char* env, 
const char* key)
             std::string_view aKey(sLine.data(), 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('
+                        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;
         }

Reply via email to