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 |
4101
|
204 size_t pos = program_invocation_name.find_last_of (file_ops::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 (file_ops::is_dir_sep (s[0])) |
2926
|
243 return true; |
|
244 |
4101
|
245 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
4088
|
246 if ((len == 2 && isalpha (s[0]) && s[1] == ':') |
4097
|
247 || (len > 2 && isalpha (s[0]) && s[1] == ':' |
|
248 && file_ops::is_dir_sep (s[2]))) |
4087
|
249 return true; |
|
250 #endif |
|
251 |
2926
|
252 return false; |
|
253 } |
|
254 |
|
255 // Return the `basename' of the pathname in STRING (the stuff after |
4097
|
256 // the last directory separator). If STRING is not a full pathname, |
|
257 // simply return it. |
2926
|
258 |
3504
|
259 std::string |
|
260 octave_env::do_base_pathname (const std::string& s) const |
2926
|
261 { |
|
262 if (! do_absolute_pathname (s)) |
|
263 return s; |
|
264 |
4101
|
265 size_t pos = s.find_last_of (file_ops::dir_sep_chars); |
2926
|
266 |
|
267 if (pos == NPOS) |
|
268 return s; |
|
269 else |
|
270 return s.substr (pos+1); |
|
271 } |
|
272 |
|
273 // Turn STRING (a pathname) into an absolute pathname, assuming that |
4097
|
274 // DOT_PATH contains the symbolic location of the current directory. |
2926
|
275 |
3504
|
276 std::string |
|
277 octave_env::do_make_absolute (const std::string& s, |
|
278 const std::string& dot_path) const |
2926
|
279 { |
|
280 #if defined (__EMX__) |
|
281 if (s.length () > 1 && s[1] == ':') |
|
282 return s; |
|
283 #endif |
|
284 |
4087
|
285 if (dot_path.empty () || s.empty () || do_absolute_pathname (s)) |
2926
|
286 return s; |
|
287 |
4097
|
288 std::string current_dir = dot_path; |
2926
|
289 |
4097
|
290 if (current_dir.empty ()) |
|
291 current_dir = do_getcwd (); |
2926
|
292 |
4097
|
293 size_t pos = current_dir.length () - 1; |
2926
|
294 |
4097
|
295 if (! file_ops::is_dir_sep (current_dir[pos])) |
4101
|
296 current_dir.append (file_ops::dir_sep_str); |
4097
|
297 |
|
298 // XXX FIXME XXX -- this is probably not correct for all systems. |
2926
|
299 |
|
300 size_t i = 0; |
|
301 size_t slen = s.length (); |
|
302 |
|
303 while (i < slen) |
|
304 { |
|
305 if (s[i] == '.') |
|
306 { |
|
307 if (i + 1 == slen) |
4097
|
308 return current_dir; |
2926
|
309 |
4097
|
310 if (file_ops::is_dir_sep (s[i+1])) |
2926
|
311 { |
|
312 i += 2; |
|
313 continue; |
|
314 } |
|
315 |
4097
|
316 if (s[i+1] == '.' |
|
317 && (i + 2 == slen || file_ops::is_dir_sep (s[i+2]))) |
2926
|
318 { |
|
319 i += 2; |
|
320 |
|
321 if (i != slen) |
|
322 i++; |
|
323 |
4097
|
324 pathname_backup (current_dir, 1); |
2926
|
325 |
|
326 continue; |
|
327 } |
|
328 } |
|
329 |
4101
|
330 size_t tmp = s.find_first_of (file_ops::dir_sep_chars, i); |
2926
|
331 |
|
332 if (tmp == NPOS) |
|
333 { |
4097
|
334 current_dir.append (s, i, tmp-i); |
2926
|
335 break; |
|
336 } |
|
337 else |
|
338 { |
4097
|
339 current_dir.append (s, i, tmp-i+1); |
2926
|
340 i = tmp + 1; |
|
341 } |
|
342 } |
|
343 |
4097
|
344 return current_dir; |
2926
|
345 } |
|
346 |
4097
|
347 // Return a string which is the current working directory. |
2926
|
348 |
3504
|
349 std::string |
4097
|
350 octave_env::do_getcwd () const |
2926
|
351 { |
|
352 if (! follow_symbolic_links) |
|
353 current_directory = ""; |
|
354 |
|
355 if (verbatim_pwd || current_directory.empty ()) |
|
356 current_directory = ::octave_getcwd (); |
|
357 |
|
358 return current_directory; |
|
359 } |
|
360 |
|
361 // This value is not cached because it can change while Octave is |
|
362 // running. |
|
363 |
3504
|
364 std::string |
2926
|
365 octave_env::do_get_home_directory (void) const |
|
366 { |
3504
|
367 std::string hd = do_getenv ("HOME"); |
2926
|
368 |
2947
|
369 if (hd.empty ()) |
|
370 { |
|
371 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
|
372 |
4101
|
373 hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str); |
2947
|
374 } |
|
375 |
|
376 return hd; |
2926
|
377 } |
|
378 |
3504
|
379 std::string |
2926
|
380 octave_env::do_get_user_name (void) const |
|
381 { |
|
382 // XXX FIXME XXX -- is it possible for this to change while Octave |
|
383 // is running? |
|
384 |
|
385 if (user_name.empty ()) |
|
386 { |
2947
|
387 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
2926
|
388 |
3504
|
389 user_name = pw ? pw.name () : std::string ("unknown"); |
2926
|
390 } |
|
391 |
|
392 return user_name; |
|
393 } |
|
394 |
3504
|
395 std::string |
2926
|
396 octave_env::do_get_host_name (void) const |
|
397 { |
|
398 // XXX FIXME XXX -- is it possible for this to change while Octave |
|
399 // is running? |
|
400 |
|
401 if (host_name.empty ()) |
|
402 { |
|
403 char hostname[256]; |
|
404 |
3803
|
405 int status = octave_gethostname (hostname, 255); |
2926
|
406 |
3185
|
407 host_name = (status < 0) ? "unknown" : hostname; |
2926
|
408 } |
|
409 |
|
410 return host_name; |
|
411 } |
|
412 |
3504
|
413 std::string |
|
414 octave_env::do_getenv (const std::string& name) const |
2926
|
415 { |
|
416 char *value = ::getenv (name.c_str ()); |
|
417 |
|
418 return value ? value : ""; |
|
419 } |
|
420 |
|
421 // Do the work of changing to the directory NEWDIR. Handle symbolic |
|
422 // link following, etc. |
|
423 |
|
424 bool |
3504
|
425 octave_env::do_chdir (const std::string& newdir) |
2926
|
426 { |
|
427 bool retval = false; |
|
428 |
3504
|
429 std::string tmp; |
2926
|
430 |
|
431 if (follow_symbolic_links) |
|
432 { |
|
433 if (current_directory.empty ()) |
|
434 do_getcwd (); |
|
435 |
|
436 if (current_directory.empty ()) |
|
437 tmp = newdir; |
|
438 else |
|
439 tmp = do_make_absolute (newdir, current_directory); |
|
440 |
4097
|
441 // Get rid of trailing directory separator. |
2926
|
442 |
|
443 size_t len = tmp.length (); |
|
444 |
|
445 if (len > 1) |
|
446 { |
4097
|
447 if (file_ops::is_dir_sep (tmp[--len])) |
2926
|
448 tmp.resize (len); |
|
449 } |
|
450 |
|
451 if (! ::octave_chdir (tmp)) |
|
452 { |
|
453 current_directory = tmp; |
|
454 retval = true; |
|
455 } |
|
456 } |
|
457 else |
|
458 retval = (! ::octave_chdir (newdir)); |
|
459 |
|
460 return retval; |
|
461 } |
|
462 |
|
463 // Remove the last N directories from PATH. |
|
464 |
|
465 void |
3504
|
466 octave_env::pathname_backup (std::string& path, int n) const |
2926
|
467 { |
|
468 if (path.empty ()) |
|
469 return; |
|
470 |
|
471 size_t i = path.length () - 1; |
|
472 |
|
473 while (n--) |
|
474 { |
4097
|
475 while (file_ops::is_dir_sep (path[i]) && i > 0) |
2926
|
476 i--; |
|
477 |
4097
|
478 while (! file_ops::is_dir_sep (path[i]) && i > 0) |
2926
|
479 i--; |
|
480 |
|
481 i++; |
|
482 } |
|
483 |
|
484 path.resize (i); |
|
485 } |
|
486 |
|
487 void |
|
488 octave_env::error (int err_num) const |
|
489 { |
|
490 (*current_liboctave_error_handler) ("%s", strerror (err_num)); |
|
491 } |
|
492 |
|
493 void |
3504
|
494 octave_env::error (const std::string& s) const |
2926
|
495 { |
|
496 (*current_liboctave_error_handler) ("%s", s.c_str ()); |
|
497 } |
|
498 |
|
499 /* |
|
500 ;;; Local Variables: *** |
|
501 ;;; mode: C++ *** |
|
502 ;;; End: *** |
|
503 */ |