2 * \file environment.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
7 * \author João Luis M. Assirati
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
15 #include "support/environment.h"
17 #include "support/docstring.h"
18 #include "support/lstrings.h"
19 #include "support/os.h"
20 #include "support/debug.h"
22 #include <algorithm> // for remove
33 bool hasEnv(string const & name)
35 return getenv(name.c_str());
39 string const getEnv(string const & name)
41 // f.ex. what about error checking?
42 char const * const ch = getenv(name.c_str());
43 return ch ? to_utf8(from_local8bit(ch)) : string();
47 vector<string> const getEnvPath(string const & name)
49 string const env_var = getEnv(name);
50 string const separator(1, os::path_separator());
52 return getVectorFromString(env_var, separator);
56 bool setEnv(string const & name, string const & value)
58 // CHECK Look at and fix this.
59 // f.ex. what about error checking?
63 encoded = to_local8bit(from_utf8(value));
68 #if defined (HAVE_SETENV)
69 return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
70 #elif defined (HAVE_PUTENV)
71 // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
72 // the argument of putenv() needs to be static, because changing its
73 // value will change the environment. Therefore we need a different static
74 // storage for each variable.
76 static map<string, string> varmap;
77 varmap[name] = name + '=' + encoded;
78 return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
80 #error No environment-setting function has been defined.
86 void setEnvPath(string const & name, vector<string> const & env)
88 char const separator(os::path_separator());
90 vector<string>::const_iterator const begin = env.begin();
91 vector<string>::const_iterator const end = env.end();
92 vector<string>::const_iterator it = begin;
93 for (; it != end; ++it) {
96 ss << os::external_path(*it);
98 setEnv(name, ss.str());
102 void prependEnvPath(string const & name, string const & prefix)
104 string const separator(1, os::path_separator());
105 vector<string> reversed_tokens
106 = getVectorFromString(prefix, separator);
107 vector<string> env_var = getEnvPath(name);
109 // Prepend each new element to the list, removing identical elements
110 // that occur later in the list.
111 LYXERR(Debug::INIT, "Prepending \"" << prefix << "\" to PATH");
112 typedef vector<string>::const_reverse_iterator token_iterator;
113 token_iterator it = reversed_tokens.rbegin();
114 token_iterator const end = reversed_tokens.rend();
115 for (; it != end; ++it) {
116 vector<string>::iterator remove_it =
117 remove(env_var.begin(), env_var.end(), *it);
118 env_var.erase(remove_it, env_var.end());
119 env_var.insert(env_var.begin(), *it);
122 setEnvPath(name, env_var);
126 bool unsetEnv(string const & name)
128 #if defined(HAVE_UNSETENV)
129 // FIXME: does it leak?
130 return ::unsetenv(name.c_str()) == 0;
131 #elif defined(HAVE_PUTENV)
132 // This is OK with MSVC and MinGW at least.
133 // The argument of putenv() does not need to be a static variable in this
134 // case, since the variable is removed from the environment.
135 return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
137 #error No environment-unsetting function has been defined.
142 } // namespace support