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