1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
2 | * |
---|
3 | * schroot is free software: you can redistribute it and/or modify it |
---|
4 | * under the terms of the GNU General Public License as published by |
---|
5 | * the Free Software Foundation, either version 3 of the License, or |
---|
6 | * (at your option) any later version. |
---|
7 | * |
---|
8 | * schroot is distributed in the hope that it will be useful, but |
---|
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | * General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU General Public License |
---|
14 | * along with this program. If not, see |
---|
15 | * <http://www.gnu.org/licenses/>. |
---|
16 | * |
---|
17 | *********************************************************************/ |
---|
18 | |
---|
19 | #ifndef SBUILD_ENVIRONMENT_H |
---|
20 | #define SBUILD_ENVIRONMENT_H |
---|
21 | |
---|
22 | #include <sbuild/sbuild-log.h> |
---|
23 | #include <sbuild/sbuild-parse-value.h> |
---|
24 | #include <sbuild/sbuild-regex.h> |
---|
25 | |
---|
26 | #include <map> |
---|
27 | #include <string> |
---|
28 | #include <sstream> |
---|
29 | |
---|
30 | #include <boost/format.hpp> |
---|
31 | |
---|
32 | namespace sbuild |
---|
33 | { |
---|
34 | |
---|
35 | /** |
---|
36 | * Container of environment variables. |
---|
37 | */ |
---|
38 | class environment : public std::map<std::string, std::string> |
---|
39 | { |
---|
40 | public: |
---|
41 | using std::map<std::string, std::string>::value_type; |
---|
42 | |
---|
43 | /// The constructor. |
---|
44 | environment (); |
---|
45 | |
---|
46 | /** |
---|
47 | * The constructor. |
---|
48 | * |
---|
49 | * @param environment the environment to set. |
---|
50 | */ |
---|
51 | environment (char **environment); |
---|
52 | |
---|
53 | /// The destructor. |
---|
54 | ~environment (); |
---|
55 | |
---|
56 | /** |
---|
57 | * Set environment filter. If the environment variable name |
---|
58 | * matches the regex when add() is called, addition will be . |
---|
59 | * |
---|
60 | * The default filter is to allow all strings. |
---|
61 | * |
---|
62 | * If the regex contains errors, an exception will be thrown. |
---|
63 | * |
---|
64 | * @param filter the filter regex. |
---|
65 | */ |
---|
66 | void |
---|
67 | set_filter (regex const& filter); |
---|
68 | |
---|
69 | /** |
---|
70 | * Get environment filter. |
---|
71 | * |
---|
72 | * @returns the filter regex. |
---|
73 | */ |
---|
74 | regex const& |
---|
75 | get_filter () const; |
---|
76 | |
---|
77 | /** |
---|
78 | * Add environment variables. Any existing variables sharing the |
---|
79 | * name of a new value will be replaced. |
---|
80 | * |
---|
81 | * @param environment the environment variables to add. This is a |
---|
82 | * null-terminated array of pointers to char. |
---|
83 | */ |
---|
84 | void |
---|
85 | add (char **environment); |
---|
86 | |
---|
87 | /** |
---|
88 | * Add environment variables. Any existing variables sharing the |
---|
89 | * name of a new value will be replaced. |
---|
90 | * |
---|
91 | * @param environment the environment variables to add. |
---|
92 | */ |
---|
93 | void |
---|
94 | add (environment const& environment); |
---|
95 | |
---|
96 | /** |
---|
97 | * Add environment variable. Any existing variable sharing the |
---|
98 | * name will be replaced. |
---|
99 | * |
---|
100 | * @param value the environment variable to add. |
---|
101 | */ |
---|
102 | void |
---|
103 | add (value_type const& value); |
---|
104 | |
---|
105 | /** |
---|
106 | * Add environment variable. Any existing variable sharing the |
---|
107 | * name will be replaced. |
---|
108 | * |
---|
109 | * @param name the environment variable name |
---|
110 | * @param value the environment variable value to add. |
---|
111 | */ |
---|
112 | void |
---|
113 | add (std::string const& name, |
---|
114 | std::string const& value) |
---|
115 | { |
---|
116 | add(std::make_pair(name, value)); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Add environment variable. Any existing variable sharing the |
---|
121 | * name will be replaced. |
---|
122 | * |
---|
123 | * @param name the environment variable name |
---|
124 | * @param value the environment variable value to add. |
---|
125 | */ |
---|
126 | template<typename T> |
---|
127 | void |
---|
128 | add (std::string const& name, |
---|
129 | T const& value) |
---|
130 | { |
---|
131 | std::ostringstream varstring; |
---|
132 | varstring.imbue(std::locale::classic()); |
---|
133 | varstring << std::boolalpha << value; |
---|
134 | add(std::make_pair(name, varstring.str())); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Add environment variable. Any existing variable sharing the |
---|
139 | * name will be replaced. |
---|
140 | * |
---|
141 | * @param value the environment variable to add. This is a string |
---|
142 | * in the form key=value. |
---|
143 | */ |
---|
144 | void |
---|
145 | add (std::string const& value); |
---|
146 | |
---|
147 | /** |
---|
148 | * Remove environment variables. Any variables sharing the names |
---|
149 | * of a specified value will be removed. |
---|
150 | * |
---|
151 | * @param environment the environment variables to remove. This |
---|
152 | * is a null-terminated array of pointers to char. |
---|
153 | */ |
---|
154 | void |
---|
155 | remove (char **environment); |
---|
156 | |
---|
157 | /** |
---|
158 | * Remove environment variables. Any variables sharing the names |
---|
159 | * of a specified value will be removed. |
---|
160 | * |
---|
161 | * @param environment the environment variables to remove. |
---|
162 | */ |
---|
163 | void |
---|
164 | remove (environment const& environment); |
---|
165 | |
---|
166 | /** |
---|
167 | * Remove environment variable. Any variable sharing the name |
---|
168 | * of the specified value will be removed. |
---|
169 | * |
---|
170 | * @param value the environment variable to remove. |
---|
171 | */ |
---|
172 | void |
---|
173 | remove (std::string const& value); |
---|
174 | |
---|
175 | /** |
---|
176 | * Remove environment variable. Any variable sharing the name |
---|
177 | * of the specified value will be removed. |
---|
178 | * |
---|
179 | * @param value the environment variable to remove. |
---|
180 | */ |
---|
181 | void |
---|
182 | remove (value_type const& value); |
---|
183 | |
---|
184 | /** |
---|
185 | * Get the value of an environment variable. |
---|
186 | * |
---|
187 | * @param name the name of the environment variable. |
---|
188 | * @param value the variable to store the value in on success. |
---|
189 | * @returns true on success, false if the variable does not exist, |
---|
190 | * or there is a parse error. |
---|
191 | */ |
---|
192 | template <typename T> |
---|
193 | bool |
---|
194 | get (std::string const& name, |
---|
195 | T& value) const |
---|
196 | { |
---|
197 | log_debug(DEBUG_INFO) << "Getting environment variable=" << name |
---|
198 | << std::endl; |
---|
199 | const_iterator pos = find(name); |
---|
200 | if (pos != end()) |
---|
201 | { |
---|
202 | try |
---|
203 | { |
---|
204 | parse_value(pos->second, value); |
---|
205 | return true; |
---|
206 | } |
---|
207 | catch (parse_value_error const& e) |
---|
208 | { |
---|
209 | log_warning() << boost::format("%1%: %2%\n") |
---|
210 | % name % e.what(); |
---|
211 | return false; |
---|
212 | } |
---|
213 | } |
---|
214 | log_debug(DEBUG_NOTICE) << "name not found: " << name << std::endl; |
---|
215 | return false; |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | * Get the evironment variables as a string vector. This form is |
---|
220 | * suitable for use as an envp argument with execve, for example. |
---|
221 | * |
---|
222 | * @returns a newly-allocated string vector. This is allocated |
---|
223 | * with new, and should be freed with strv_delete(). |
---|
224 | */ |
---|
225 | char ** |
---|
226 | get_strv () const; |
---|
227 | |
---|
228 | /** |
---|
229 | * Add variables to the environment. |
---|
230 | * |
---|
231 | * @param rhs the values to add. |
---|
232 | * @returns the modified environment. |
---|
233 | */ |
---|
234 | template <typename T> |
---|
235 | environment& |
---|
236 | operator += (T const& rhs) |
---|
237 | { |
---|
238 | add(rhs); |
---|
239 | return *this; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Remove variables from the environment. |
---|
244 | * |
---|
245 | * @param rhs the values to remove. |
---|
246 | * @returns the modified environment. |
---|
247 | */ |
---|
248 | template <typename T> |
---|
249 | environment& |
---|
250 | operator -= (T const& rhs) |
---|
251 | { |
---|
252 | remove(rhs); |
---|
253 | return *this; |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * Add variables to the environment. |
---|
258 | * |
---|
259 | * @param lhs the environment to add to. |
---|
260 | * @param rhs the values to add. |
---|
261 | * @returns the new environment. |
---|
262 | */ |
---|
263 | template <typename T> |
---|
264 | friend environment |
---|
265 | operator + (environment const& lhs, |
---|
266 | T const& rhs) |
---|
267 | { |
---|
268 | environment ret(lhs); |
---|
269 | ret += rhs; |
---|
270 | return ret; |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * Remove variables from the environment. |
---|
275 | * |
---|
276 | * @param lhs the environment to remove from. |
---|
277 | * @param rhs the values to remove. |
---|
278 | * @returns the new environment. |
---|
279 | */ |
---|
280 | template <typename T> |
---|
281 | friend environment |
---|
282 | operator - (environment const& lhs, |
---|
283 | T const& rhs) |
---|
284 | { |
---|
285 | environment ret(lhs); |
---|
286 | ret -= rhs; |
---|
287 | return ret; |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * Output the environment to an ostream. |
---|
292 | * |
---|
293 | * @param stream the stream to output to. |
---|
294 | * @param rhs the environment to output. |
---|
295 | * @returns the stream. |
---|
296 | */ |
---|
297 | template <class charT, class traits> |
---|
298 | friend |
---|
299 | std::basic_ostream<charT,traits>& |
---|
300 | operator << (std::basic_ostream<charT,traits>& stream, |
---|
301 | environment const& rhs) |
---|
302 | { |
---|
303 | for (environment::const_iterator pos = rhs.begin(); |
---|
304 | pos != rhs.end(); |
---|
305 | ++pos) |
---|
306 | { |
---|
307 | stream << pos->first << '=' << pos->second << '\n'; |
---|
308 | } |
---|
309 | |
---|
310 | return stream; |
---|
311 | } |
---|
312 | |
---|
313 | private: |
---|
314 | /// Filter regex. |
---|
315 | regex filter; |
---|
316 | }; |
---|
317 | |
---|
318 | } |
---|
319 | |
---|
320 | #endif /* SBUILD_ENVIRONMENT_H */ |
---|
321 | |
---|
322 | /* |
---|
323 | * Local Variables: |
---|
324 | * mode:C++ |
---|
325 | * End: |
---|
326 | */ |
---|