]> git.lyx.org Git - features.git/commitdiff
Do not replace nonexistent environment variables
authorEnrico Forestieri <forenr@lyx.org>
Mon, 4 Jan 2021 22:00:42 +0000 (23:00 +0100)
committerEnrico Forestieri <forenr@lyx.org>
Mon, 4 Jan 2021 22:00:42 +0000 (23:00 +0100)
References to environment variables embedded in a filename are expanded
and replaced by their value. However, if a variable does not exist, its
reference is simply erased from the filename, causing havoc (see #7801).

This has been like that since ever and cannot be changed, both for
backward compatibility and because this feature is currently used in
the Windows installer.

A possible backward compatible strategy is leaving as is the reference
to the environment variable (introduced by a $ sign) in the filename
if it does not exist. This is done in this patch, which also assumes
that an escape character is never used in a filename (inserting a $ in
the filename is easy, but I don't think one is able to easily insert
an escape character).

src/support/filetools.cpp

index a5168d4d4a58c81dbcb19c642cdda52c3d67cc8c..16e6da425a77dd4db8f1ed0f2e9cb8ee5e35816a 100644 (file)
@@ -683,8 +683,12 @@ string const onlyFileName(string const & fname)
 
 
 // Search the string for ${VAR} and $VAR and replace VAR using getenv.
+// If VAR does not exist, ${VAR} and $VAR are left as is in the string.
 string const replaceEnvironmentPath(string const & path)
 {
+       if (!contains(path, '$'))
+               return path;
+
        // ${VAR} is defined as
        // $\{[A-Za-z_][A-Za-z_0-9]*\}
        static string const envvar_br = "[$]\\{([A-Za-z_][A-Za-z_0-9]*)\\}";
@@ -702,14 +706,23 @@ string const replaceEnvironmentPath(string const & path)
                string result = path;
                while (1) {
                        smatch what;
+                       bool brackets = true;
                        if (!regex_match(result, what, envvar_br_re)) {
+                               brackets = false;
                                if (!regex_match(result, what, envvar_re))
                                        break;
                        }
                        string env_var = getEnv(what.str(2));
+                       if (env_var.empty()) {
+                               // temporarily use escape (0x1B) in place of $
+                               if (brackets)
+                                       env_var = "\e{" + what.str(2) + '}';
+                               else
+                                       env_var = "\e" + what.str(2);
+                       }
                        result = what.str(1) + env_var + what.str(3);
                }
-               return result;
+               return subst(result, '\e', '$');
        } catch (exception const & e) {
                LYXERR0("Something is very wrong: " << e.what());
                return path;