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