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