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