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/os.h"
20 #include <boost/tokenizer.hpp>
32 bool hasEnv(string const & name)
34 return getenv(name.c_str());
38 string const getEnv(string const & name)
40 // f.ex. what about error checking?
41 char const * const ch = getenv(name.c_str());
42 return ch ? to_utf8(from_local8bit(ch)) : string();
46 vector<string> const getEnvPath(string const & name)
48 typedef boost::char_separator<char> Separator;
49 typedef boost::tokenizer<Separator> Tokenizer;
51 string const env_var = getEnv(name);
52 Separator const separator(string(1, os::path_separator()).c_str());
53 Tokenizer const tokens(env_var, separator);
54 Tokenizer::const_iterator it = tokens.begin();
55 Tokenizer::const_iterator const end = tokens.end();
58 for (; it != end; ++it)
59 vars.push_back(os::internal_path(*it));
65 bool setEnv(string const & name, string const & value)
67 // CHECK Look at and fix this.
68 // f.ex. what about error checking?
70 string const encoded = to_local8bit(from_utf8(value));
71 #if defined (HAVE_SETENV)
72 return ::setenv(name.c_str(), encoded.c_str(), 1) == 0;
73 #elif defined (HAVE_PUTENV)
74 // According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
75 // the argument of putenv() needs to be static, because changing its
76 // value will change the environment. Therefore we need a different static
77 // storage for each variable.
78 static map<string, string> varmap;
79 varmap[name] = name + '=' + encoded;
80 return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
82 #error No environment-setting function has been defined.
88 void setEnvPath(string const & name, vector<string> const & env)
90 char const separator(os::path_separator());
92 vector<string>::const_iterator const begin = env.begin();
93 vector<string>::const_iterator const end = env.end();
94 vector<string>::const_iterator it = begin;
95 for (; it != end; ++it) {
98 ss << os::external_path(*it);
100 setEnv(name, ss.str());
104 void prependEnvPath(string const & name, string const & prefix)
106 vector<string> env_var = getEnvPath(name);
108 typedef boost::char_separator<char> Separator;
109 typedef boost::tokenizer<Separator> Tokenizer;
111 Separator const separator(string(1, os::path_separator()).c_str());
113 // Prepend each new element to the list, removing identical elements
114 // that occur later in the list.
115 Tokenizer const tokens(prefix, separator);
116 vector<string> reversed_tokens(tokens.begin(), tokens.end());
118 typedef vector<string>::const_reverse_iterator token_iterator;
119 token_iterator it = reversed_tokens.rbegin();
120 token_iterator const end = reversed_tokens.rend();
121 for (; it != end; ++it) {
122 vector<string>::iterator remove_it =
123 remove(env_var.begin(), env_var.end(), *it);
124 env_var.erase(remove_it, env_var.end());
125 env_var.insert(env_var.begin(), *it);
128 setEnvPath(name, env_var);
132 bool unsetEnv(string const & name)
134 #if defined(HAVE_UNSETENV)
135 // FIXME: does it leak?
136 return ::unsetenv(name.c_str()) == 0;
137 #elif defined(HAVE_PUTENV)
138 // This is OK with MSVC and MinGW at least.
139 // FIXME: According to http://pubs.opengroup.org/onlinepubs/9699919799/functions/putenv.html
140 // the argument of putenv() needs to be static, see setEnv().
141 return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
143 #error No environment-unsetting function has been defined.
148 } // namespace support