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