]> git.lyx.org Git - lyx.git/blob - src/support/environment.cpp
Add comments about putenv() usage
[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         static map<string, string> varmap;
79         varmap[name] = name + '=' + encoded;
80         return ::putenv(const_cast<char*>(varmap[name].c_str())) == 0;
81 #else
82 #error No environment-setting function has been defined.
83 #endif
84         return false;
85 }
86
87
88 void setEnvPath(string const & name, vector<string> const & env)
89 {
90         char const separator(os::path_separator());
91         ostringstream ss;
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) {
96                 if (it != begin)
97                         ss << separator;
98                 ss << os::external_path(*it);
99         }
100         setEnv(name, ss.str());
101 }
102
103
104 void prependEnvPath(string const & name, string const & prefix)
105 {
106         vector<string> env_var = getEnvPath(name);
107
108         typedef boost::char_separator<char> Separator;
109         typedef boost::tokenizer<Separator> Tokenizer;
110
111         Separator const separator(string(1, os::path_separator()).c_str());
112
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());
117
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);
126         }
127
128         setEnvPath(name, env_var);
129 }
130
131
132 bool unsetEnv(string const & name)
133 {
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;
142 #else
143 #error No environment-unsetting function has been defined.
144 #endif
145 }
146
147
148 } // namespace support
149 } // namespace lyx