]> git.lyx.org Git - lyx.git/blob - src/support/environment.C
Detect mode_t for safe use of chmod, and for scons/msvc
[lyx.git] / src / support / environment.C
1 /**
2  * \file environment.C
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 #include "support/os.h"
17
18 #include <boost/tokenizer.hpp>
19
20 #include <cstdlib>
21 #include <map>
22 #include <sstream>
23
24 using std::string;
25 using std::vector;
26
27
28 namespace lyx {
29 namespace support {
30
31 string const getEnv(string const & envname)
32 {
33         // f.ex. what about error checking?
34         char const * const ch = getenv(envname.c_str());
35         string const envstr = !ch ? "" : ch;
36         return envstr;
37 }
38
39
40 vector<string> const getEnvPath(string const & name)
41 {
42         typedef boost::char_separator<char> Separator;
43         typedef boost::tokenizer<Separator> Tokenizer;
44
45         string const env_var = getEnv(name);
46         Separator const separator(string(1, os::path_separator()).c_str());
47         Tokenizer const tokens(env_var, separator);
48         Tokenizer::const_iterator it = tokens.begin();
49         Tokenizer::const_iterator const end = tokens.end();
50
51         std::vector<string> vars;
52         for (; it != end; ++it)
53                 vars.push_back(os::internal_path(*it));
54
55         return vars;
56 }
57
58
59 bool setEnv(string const & name, string const & value)
60 {
61         // CHECK Look at and fix this.
62         // f.ex. what about error checking?
63
64 #if defined (HAVE_SETENV)
65         int const retval = ::setenv(name.c_str(), value.c_str(), true);
66
67 #elif defined (HAVE_PUTENV)
68         static std::map<string, char *> varmap;
69
70         string envstr = name + '=' + value;
71         char * newptr = new char[envstr.size() + 1];
72         envstr.copy(newptr, envstr.length());
73         newptr[envstr.length()] = '\0';
74         int const retval = ::putenv(newptr);
75
76         char * oldptr = varmap[name];
77         if (oldptr)
78                 delete oldptr;
79         varmap[name] = newptr;
80
81 #else
82 #error No environment-setting function has been defined.
83 #endif
84         return retval == 0;
85 }
86
87
88 void setEnvPath(string const & name, vector<string> const & env)
89 {
90         char const separator(os::path_separator());
91         std::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 #if defined(__CYGWIN__) || defined(__CYGWIN32__)
99                 // On cygwin, os::external_path returns either posix or
100                 // pseudo-win style paths, but here we always need posix style.
101                 // This fixes bug 2344.
102                 ss << os::internal_path(*it);
103 #else
104                 ss << os::external_path(*it);
105 #endif
106         }
107         setEnv(name, ss.str());
108 }
109
110
111 void prependEnvPath(string const & name, string const & prefix)
112 {
113         vector<string> env_var = getEnvPath(name);
114
115         typedef boost::char_separator<char> Separator;
116         typedef boost::tokenizer<Separator> Tokenizer;
117
118         Separator const separator(string(1, os::path_separator()).c_str());
119
120         // Prepend each new element to the list, removing identical elements
121         // that occur later in the list.
122         Tokenizer const tokens(prefix, separator);
123         vector<string> reversed_tokens(tokens.begin(), tokens.end());
124
125         typedef vector<string>::const_reverse_iterator token_iterator;
126         token_iterator it = reversed_tokens.rbegin();
127         token_iterator const end = reversed_tokens.rend();
128         for (; it != end; ++it) {
129                 vector<string>::iterator remove_it =
130                         std::remove(env_var.begin(), env_var.end(), *it);
131                 env_var.erase(remove_it, env_var.end());
132                 env_var.insert(env_var.begin(), *it);
133         }
134
135         setEnvPath(name, env_var);
136 }
137
138 } // namespace support
139 } // namespace lyx