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