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