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