2926
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 /* |
|
24 |
|
25 The functions listed below were adapted from a similar functions |
|
26 from GNU Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 |
|
27 Free Software Foundation, Inc. |
|
28 |
|
29 octave_env::do_absolute_pathname |
|
30 octave_env::do_base_pathname |
|
31 octave_env::do_chdir |
|
32 octave_env::do_getcwd |
|
33 octave_env::do_make_absolute |
|
34 octave_env::do_polite_directory_format |
|
35 octave_env::pathname_backup |
|
36 |
|
37 */ |
|
38 |
|
39 #ifdef HAVE_CONFIG_H |
|
40 #include <config.h> |
|
41 #endif |
|
42 |
|
43 #include <cstdlib> |
3504
|
44 #include <string> |
2926
|
45 |
|
46 #ifdef HAVE_UNISTD_H |
|
47 #ifdef HAVE_SYS_TYPES_H |
|
48 #include <sys/types.h> |
|
49 #endif |
|
50 #include <unistd.h> |
|
51 #endif |
|
52 |
|
53 #include "lo-error.h" |
|
54 #include "lo-sysdep.h" |
|
55 #include "lo-utils.h" |
|
56 #include "oct-env.h" |
2934
|
57 #include "oct-passwd.h" |
2947
|
58 #include "oct-syscalls.h" |
2926
|
59 |
|
60 octave_env::octave_env (void) |
|
61 : follow_symbolic_links (true), verbatim_pwd (true), |
|
62 current_directory (), program_name (), program_invocation_name (), |
|
63 user_name (), host_name () |
|
64 { |
|
65 // Get a real value for the current directory. |
|
66 do_getcwd (); |
|
67 |
|
68 // Etc. |
|
69 do_get_user_name (); |
|
70 |
|
71 do_get_host_name (); |
|
72 } |
|
73 |
|
74 octave_env *octave_env::instance = 0; |
|
75 |
|
76 bool |
|
77 octave_env::instance_ok (void) |
|
78 { |
|
79 bool retval = true; |
|
80 |
|
81 if (! instance) |
|
82 instance = new octave_env (); |
|
83 |
|
84 if (! instance) |
|
85 { |
|
86 (*current_liboctave_error_handler) |
|
87 ("unable to create current working directoy object!"); |
|
88 |
|
89 retval = false; |
|
90 } |
|
91 |
|
92 return retval; |
|
93 } |
|
94 |
3504
|
95 std::string |
|
96 octave_env::polite_directory_format (const std::string& name) |
2926
|
97 { |
|
98 return (instance_ok ()) |
3504
|
99 ? instance->do_polite_directory_format (name) : std::string (); |
2926
|
100 } |
|
101 |
|
102 bool |
3504
|
103 octave_env::absolute_pathname (const std::string& s) |
2926
|
104 { |
|
105 return (instance_ok ()) |
|
106 ? instance->do_absolute_pathname (s) : false; |
|
107 } |
|
108 |
3504
|
109 std::string |
|
110 octave_env::base_pathname (const std::string& s) |
2926
|
111 { |
|
112 return (instance_ok ()) |
3504
|
113 ? instance->do_base_pathname (s) : std::string (); |
2926
|
114 } |
|
115 |
3504
|
116 std::string |
|
117 octave_env::make_absolute (const std::string& s, const std::string& dot_path) |
2926
|
118 { |
|
119 return (instance_ok ()) |
3504
|
120 ? instance->do_make_absolute (s, dot_path) : std::string (); |
2926
|
121 } |
|
122 |
3504
|
123 std::string |
2926
|
124 octave_env::getcwd () |
|
125 { |
|
126 return (instance_ok ()) |
3504
|
127 ? instance->do_getcwd () : std::string (); |
2926
|
128 } |
|
129 |
3504
|
130 std::string |
2926
|
131 octave_env::get_home_directory () |
|
132 { |
|
133 return (instance_ok ()) |
3504
|
134 ? instance->do_get_home_directory () : std::string (); |
2926
|
135 } |
|
136 |
3504
|
137 std::string |
2926
|
138 octave_env::get_program_name (void) |
|
139 { |
|
140 return (instance_ok ()) |
3504
|
141 ? instance->program_name : std::string (); |
2926
|
142 } |
|
143 |
3504
|
144 std::string |
2926
|
145 octave_env::get_program_invocation_name (void) |
|
146 { |
|
147 return (instance_ok ()) |
3504
|
148 ? instance->program_invocation_name : std::string (); |
2926
|
149 } |
|
150 |
|
151 void |
3504
|
152 octave_env::set_program_name (const std::string& s) |
2926
|
153 { |
|
154 if (instance_ok ()) |
|
155 instance->do_set_program_name (s); |
|
156 } |
|
157 |
3504
|
158 std::string |
2926
|
159 octave_env::get_user_name (void) |
|
160 { |
|
161 return (instance_ok ()) |
3504
|
162 ? instance->do_get_user_name () : std::string (); |
2926
|
163 } |
|
164 |
3504
|
165 std::string |
2926
|
166 octave_env::get_host_name (void) |
|
167 { |
|
168 return (instance_ok ()) |
3504
|
169 ? instance->do_get_host_name () : std::string (); |
2926
|
170 } |
|
171 |
|
172 // XXX FIXME XXX -- this leaves no way to distinguish between a |
|
173 // variable that is not set and one that is set to the empty string. |
|
174 // Is this a problem? |
|
175 |
3504
|
176 std::string |
|
177 octave_env::getenv (const std::string& name) |
2926
|
178 { |
|
179 return (instance_ok ()) |
3504
|
180 ? instance->do_getenv (name) : std::string (); |
2926
|
181 } |
|
182 |
|
183 void |
3504
|
184 octave_env::putenv (const std::string& name, const std::string& value) |
2926
|
185 { |
|
186 octave_putenv (name, value); |
|
187 } |
|
188 |
|
189 bool |
3504
|
190 octave_env::chdir (const std::string& newdir) |
2926
|
191 { |
|
192 return (instance_ok ()) |
|
193 ? instance->do_chdir (newdir) : false; |
|
194 } |
|
195 |
|
196 void |
3504
|
197 octave_env::do_set_program_name (const std::string& s) const |
2926
|
198 { |
|
199 program_invocation_name = s; |
|
200 |
|
201 size_t pos = program_invocation_name.rfind ('/'); |
|
202 |
|
203 program_name = (pos == NPOS) |
|
204 ? program_invocation_name : program_invocation_name.substr (pos+1); |
|
205 } |
|
206 |
|
207 // Return a pretty pathname. If the first part of the pathname is the |
|
208 // same as $HOME, then replace that with `~'. |
|
209 |
3504
|
210 std::string |
|
211 octave_env::do_polite_directory_format (const std::string& name) const |
2926
|
212 { |
3504
|
213 std::string retval; |
2926
|
214 |
3504
|
215 std::string home_dir = do_get_home_directory (); |
2926
|
216 |
|
217 size_t len = home_dir.length (); |
|
218 |
|
219 if (len > 1 && home_dir.compare (name, 0, len) == 0 |
|
220 && (name.length () == len || name[len] == '/')) |
|
221 { |
|
222 retval = "~"; |
|
223 retval.append (name.substr (len)); |
|
224 } |
|
225 else |
|
226 retval = name; |
|
227 |
|
228 return retval; |
|
229 } |
|
230 |
|
231 // Return 1 if STRING contains an absolute pathname, else 0. |
|
232 |
|
233 bool |
3504
|
234 octave_env::do_absolute_pathname (const std::string& s) const |
2926
|
235 { |
|
236 if (s.empty ()) |
|
237 return 0; |
|
238 |
|
239 if (s[0] == '/') |
|
240 return true; |
|
241 |
|
242 if (s[0] == '.') |
|
243 { |
|
244 if (s[1] == '\0' || s[1] == '/') |
|
245 return true; |
|
246 |
|
247 if (s[1] == '.') |
|
248 if (s[2] == '\0' || s[2] == '/') |
|
249 return true; |
|
250 } |
|
251 |
|
252 return false; |
|
253 } |
|
254 |
|
255 // Return the `basename' of the pathname in STRING (the stuff after |
|
256 // the last '/'). If STRING is not a full pathname, simply return it. |
|
257 |
3504
|
258 std::string |
|
259 octave_env::do_base_pathname (const std::string& s) const |
2926
|
260 { |
|
261 if (! do_absolute_pathname (s)) |
|
262 return s; |
|
263 |
|
264 size_t pos = s.rfind ('/'); |
|
265 |
|
266 if (pos == NPOS) |
|
267 return s; |
|
268 else |
|
269 return s.substr (pos+1); |
|
270 } |
|
271 |
|
272 // Turn STRING (a pathname) into an absolute pathname, assuming that |
|
273 // DOT_PATH contains the symbolic location of '.'. |
|
274 |
3504
|
275 std::string |
|
276 octave_env::do_make_absolute (const std::string& s, |
|
277 const std::string& dot_path) const |
2926
|
278 { |
|
279 #if defined (__EMX__) |
|
280 if (s.length () > 1 && s[1] == ':') |
|
281 return s; |
|
282 #endif |
|
283 |
|
284 if (dot_path.empty () || s[0] == '/' || s.empty ()) |
|
285 return s; |
|
286 |
3504
|
287 std::string current_path = dot_path; |
2926
|
288 |
|
289 if (current_path.empty ()) |
|
290 current_path = "./"; |
|
291 |
|
292 size_t pos = current_path.length () - 1; |
|
293 |
|
294 if (current_path[pos] != '/') |
|
295 current_path.append ("/"); |
|
296 |
|
297 size_t i = 0; |
|
298 size_t slen = s.length (); |
|
299 |
|
300 while (i < slen) |
|
301 { |
|
302 if (s[i] == '.') |
|
303 { |
|
304 if (i + 1 == slen) |
|
305 return current_path; |
|
306 |
|
307 if (s[i+1] == '/') |
|
308 { |
|
309 i += 2; |
|
310 continue; |
|
311 } |
|
312 |
|
313 if (s[i+1] == '.' && (i + 2 == slen || s[i+2] == '/')) |
|
314 { |
|
315 i += 2; |
|
316 |
|
317 if (i != slen) |
|
318 i++; |
|
319 |
|
320 pathname_backup (current_path, 1); |
|
321 |
|
322 continue; |
|
323 } |
|
324 } |
|
325 |
|
326 size_t tmp = s.find ('/', i); |
|
327 |
|
328 if (tmp == NPOS) |
|
329 { |
|
330 current_path.append (s, i, tmp-i); |
|
331 break; |
|
332 } |
|
333 else |
|
334 { |
|
335 current_path.append (s, i, tmp-i+1); |
|
336 i = tmp + 1; |
|
337 } |
|
338 } |
|
339 |
|
340 return current_path; |
|
341 } |
|
342 |
|
343 // Return a consed string which is the current working directory. |
|
344 |
3504
|
345 std::string |
2926
|
346 octave_env::do_getcwd () |
|
347 { |
|
348 if (! follow_symbolic_links) |
|
349 current_directory = ""; |
|
350 |
|
351 if (verbatim_pwd || current_directory.empty ()) |
|
352 current_directory = ::octave_getcwd (); |
|
353 |
|
354 return current_directory; |
|
355 } |
|
356 |
|
357 // This value is not cached because it can change while Octave is |
|
358 // running. |
|
359 |
3504
|
360 std::string |
2926
|
361 octave_env::do_get_home_directory (void) const |
|
362 { |
3504
|
363 std::string hd = do_getenv ("HOME"); |
2926
|
364 |
2947
|
365 if (hd.empty ()) |
|
366 { |
|
367 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
|
368 |
3504
|
369 hd = pw ? pw.dir () : std::string ("/"); |
2947
|
370 } |
|
371 |
|
372 return hd; |
2926
|
373 } |
|
374 |
3504
|
375 std::string |
2926
|
376 octave_env::do_get_user_name (void) const |
|
377 { |
|
378 // XXX FIXME XXX -- is it possible for this to change while Octave |
|
379 // is running? |
|
380 |
|
381 if (user_name.empty ()) |
|
382 { |
2947
|
383 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
2926
|
384 |
3504
|
385 user_name = pw ? pw.name () : std::string ("unknown"); |
2926
|
386 } |
|
387 |
|
388 return user_name; |
|
389 } |
|
390 |
3504
|
391 std::string |
2926
|
392 octave_env::do_get_host_name (void) const |
|
393 { |
|
394 // XXX FIXME XXX -- is it possible for this to change while Octave |
|
395 // is running? |
|
396 |
|
397 if (host_name.empty ()) |
|
398 { |
|
399 char hostname[256]; |
|
400 |
|
401 int status = gethostname (hostname, 255); |
|
402 |
3185
|
403 host_name = (status < 0) ? "unknown" : hostname; |
2926
|
404 } |
|
405 |
|
406 return host_name; |
|
407 } |
|
408 |
3504
|
409 std::string |
|
410 octave_env::do_getenv (const std::string& name) const |
2926
|
411 { |
|
412 char *value = ::getenv (name.c_str ()); |
|
413 |
|
414 return value ? value : ""; |
|
415 } |
|
416 |
|
417 // Do the work of changing to the directory NEWDIR. Handle symbolic |
|
418 // link following, etc. |
|
419 |
|
420 bool |
3504
|
421 octave_env::do_chdir (const std::string& newdir) |
2926
|
422 { |
|
423 bool retval = false; |
|
424 |
3504
|
425 std::string tmp; |
2926
|
426 |
|
427 if (follow_symbolic_links) |
|
428 { |
|
429 if (current_directory.empty ()) |
|
430 do_getcwd (); |
|
431 |
|
432 if (current_directory.empty ()) |
|
433 tmp = newdir; |
|
434 else |
|
435 tmp = do_make_absolute (newdir, current_directory); |
|
436 |
|
437 // Get rid of trailing `/'. |
|
438 |
|
439 size_t len = tmp.length (); |
|
440 |
|
441 if (len > 1) |
|
442 { |
|
443 if (tmp[--len] == '/') |
|
444 tmp.resize (len); |
|
445 } |
|
446 |
|
447 if (! ::octave_chdir (tmp)) |
|
448 { |
|
449 current_directory = tmp; |
|
450 retval = true; |
|
451 } |
|
452 } |
|
453 else |
|
454 retval = (! ::octave_chdir (newdir)); |
|
455 |
|
456 return retval; |
|
457 } |
|
458 |
|
459 // Remove the last N directories from PATH. |
|
460 |
|
461 void |
3504
|
462 octave_env::pathname_backup (std::string& path, int n) const |
2926
|
463 { |
|
464 if (path.empty ()) |
|
465 return; |
|
466 |
|
467 size_t i = path.length () - 1; |
|
468 |
|
469 while (n--) |
|
470 { |
|
471 while (path[i] == '/' && i > 0) |
|
472 i--; |
|
473 |
|
474 while (path[i] != '/' && i > 0) |
|
475 i--; |
|
476 |
|
477 i++; |
|
478 } |
|
479 |
|
480 path.resize (i); |
|
481 } |
|
482 |
|
483 void |
|
484 octave_env::error (int err_num) const |
|
485 { |
|
486 (*current_liboctave_error_handler) ("%s", strerror (err_num)); |
|
487 } |
|
488 |
|
489 void |
3504
|
490 octave_env::error (const std::string& s) const |
2926
|
491 { |
|
492 (*current_liboctave_error_handler) ("%s", s.c_str ()); |
|
493 } |
|
494 |
|
495 /* |
|
496 ;;; Local Variables: *** |
|
497 ;;; mode: C++ *** |
|
498 ;;; End: *** |
|
499 */ |