]> git.lyx.org Git - lyx.git/blob - src/support/environment.cpp
Fix bugs #6078 and #9364
[lyx.git] / src / support / environment.cpp
1 /**
2  * \file environment.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author João Luis M. Assirati
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "support/environment.h"
16
17 #include "support/docstring.h"
18 #include "support/os.h"
19
20 #include <boost/tokenizer.hpp>
21
22 #include <cstdlib>
23 #include <map>
24 #include <sstream>
25
26 using namespace std;
27
28 namespace lyx {
29 namespace support {
30
31
32 bool hasEnv(string const & name)
33 {
34         return getenv(name.c_str());
35 }
36
37
38 string const getEnv(string const & name)
39 {
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();
43 }
44
45
46 vector<string> const getEnvPath(string const & name)
47 {
48         typedef boost::char_separator<char> Separator;
49         typedef boost::tokenizer<Separator> Tokenizer;
50
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();
56
57         vector<string> vars;
58         for (; it != end; ++it)
59                 vars.push_back(os::internal_path(*it));
60
61         return vars;
62 }
63
64
65 bool setEnv(string const & name, string const & value)
66 {
67         // CHECK Look at and fix this.
68         // f.ex. what about error checking?
69
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         // FIXME THREAD
79         static map<string, string> varmap;
80         varmap[name] = name + '=' + encoded;
81         return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
82 #else
83 #error No environment-setting function has been defined.
84 #endif
85         return false;
86 }
87
88
89 void setEnvPath(string const & name, vector<string> const & env)
90 {
91         char const separator(os::path_separator());
92         ostringstream ss;
93         vector<string>::const_iterator const begin = env.begin();
94         vector<string>::const_iterator const end = env.end();
95         vector<string>::const_iterator it = begin;
96         for (; it != end; ++it) {
97                 if (it != begin)
98                         ss << separator;
99                 ss << os::external_path(*it);
100         }
101         setEnv(name, ss.str());
102 }
103
104
105 void prependEnvPath(string const & name, string const & prefix)
106 {
107         vector<string> env_var = getEnvPath(name);
108
109         typedef boost::char_separator<char> Separator;
110         typedef boost::tokenizer<Separator> Tokenizer;
111
112         Separator const separator(string(1, os::path_separator()).c_str());
113
114         // Prepend each new element to the list, removing identical elements
115         // that occur later in the list.
116         Tokenizer const tokens(prefix, separator);
117         vector<string> reversed_tokens(tokens.begin(), tokens.end());
118
119         typedef vector<string>::const_reverse_iterator token_iterator;
120         token_iterator it = reversed_tokens.rbegin();
121         token_iterator const end = reversed_tokens.rend();
122         for (; it != end; ++it) {
123                 vector<string>::iterator remove_it =
124                         remove(env_var.begin(), env_var.end(), *it);
125                 env_var.erase(remove_it, env_var.end());
126                 env_var.insert(env_var.begin(), *it);
127         }
128
129         setEnvPath(name, env_var);
130 }
131
132
133 bool unsetEnv(string const & name)
134 {
135 #if defined(HAVE_UNSETENV)
136         // FIXME: does it leak?
137         return ::unsetenv(name.c_str()) == 0;
138 #elif defined(HAVE_PUTENV)
139         // This is OK with MSVC and MinGW at least.
140         // The argument of putenv() does not need to be a static variable in this
141         // case, since the variable is removed from the environment.
142         return ::putenv(const_cast<char*>((name + "=").c_str())) == 0;
143 #else
144 #error No environment-unsetting function has been defined.
145 #endif
146 }
147
148
149 } // namespace support
150 } // namespace lyx