Mercurial > hg > octave-lyh
annotate liboctave/oct-env.cc @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | aca961a3f387 |
children | 2f29b765c0ef |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2005, 2006, |
8920 | 4 2007, 2008 John W. Eaton |
2926 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2926 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
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> |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
46 #include <cstring> |
4093 | 47 |
3504 | 48 #include <string> |
2926 | 49 |
50 #include <sys/types.h> | |
51 #include <unistd.h> | |
52 | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
53 #include "progname.h" |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
54 |
4097 | 55 #include "file-ops.h" |
2926 | 56 #include "lo-error.h" |
57 #include "lo-sysdep.h" | |
58 #include "lo-utils.h" | |
59 #include "oct-env.h" | |
2934 | 60 #include "oct-passwd.h" |
2947 | 61 #include "oct-syscalls.h" |
2926 | 62 |
63 octave_env::octave_env (void) | |
64 : follow_symbolic_links (true), verbatim_pwd (true), | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
65 current_directory (), prog_name (), prog_invocation_name (), |
2926 | 66 user_name (), host_name () |
67 { | |
68 // Get a real value for the current directory. | |
69 do_getcwd (); | |
70 | |
71 // Etc. | |
72 do_get_user_name (); | |
73 | |
74 do_get_host_name (); | |
75 } | |
76 | |
77 octave_env *octave_env::instance = 0; | |
78 | |
79 bool | |
80 octave_env::instance_ok (void) | |
81 { | |
82 bool retval = true; | |
83 | |
84 if (! instance) | |
85 instance = new octave_env (); | |
86 | |
87 if (! instance) | |
88 { | |
89 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
90 ("unable to create current working directory object!"); |
2926 | 91 |
92 retval = false; | |
93 } | |
94 | |
95 return retval; | |
96 } | |
97 | |
3504 | 98 std::string |
99 octave_env::polite_directory_format (const std::string& name) | |
2926 | 100 { |
101 return (instance_ok ()) | |
3504 | 102 ? instance->do_polite_directory_format (name) : std::string (); |
2926 | 103 } |
104 | |
105 bool | |
3504 | 106 octave_env::absolute_pathname (const std::string& s) |
2926 | 107 { |
108 return (instance_ok ()) | |
109 ? instance->do_absolute_pathname (s) : false; | |
110 } | |
111 | |
6838 | 112 bool |
113 octave_env::rooted_relative_pathname (const std::string& s) | |
114 { | |
115 return (instance_ok ()) | |
116 ? instance->do_rooted_relative_pathname (s) : false; | |
117 } | |
118 | |
3504 | 119 std::string |
120 octave_env::base_pathname (const std::string& s) | |
2926 | 121 { |
122 return (instance_ok ()) | |
3504 | 123 ? instance->do_base_pathname (s) : std::string (); |
2926 | 124 } |
125 | |
3504 | 126 std::string |
127 octave_env::make_absolute (const std::string& s, const std::string& dot_path) | |
2926 | 128 { |
129 return (instance_ok ()) | |
3504 | 130 ? instance->do_make_absolute (s, dot_path) : std::string (); |
2926 | 131 } |
132 | |
3504 | 133 std::string |
10250 | 134 octave_env::get_current_directory () |
2926 | 135 { |
136 return (instance_ok ()) | |
3504 | 137 ? instance->do_getcwd () : std::string (); |
2926 | 138 } |
139 | |
3504 | 140 std::string |
2926 | 141 octave_env::get_home_directory () |
142 { | |
143 return (instance_ok ()) | |
3504 | 144 ? instance->do_get_home_directory () : std::string (); |
2926 | 145 } |
146 | |
3504 | 147 std::string |
2926 | 148 octave_env::get_program_name (void) |
149 { | |
150 return (instance_ok ()) | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
151 ? instance->prog_name : std::string (); |
2926 | 152 } |
153 | |
3504 | 154 std::string |
2926 | 155 octave_env::get_program_invocation_name (void) |
156 { | |
157 return (instance_ok ()) | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
158 ? instance->prog_invocation_name : std::string (); |
2926 | 159 } |
160 | |
161 void | |
3504 | 162 octave_env::set_program_name (const std::string& s) |
2926 | 163 { |
164 if (instance_ok ()) | |
165 instance->do_set_program_name (s); | |
166 } | |
167 | |
3504 | 168 std::string |
2926 | 169 octave_env::get_user_name (void) |
170 { | |
171 return (instance_ok ()) | |
3504 | 172 ? instance->do_get_user_name () : std::string (); |
2926 | 173 } |
174 | |
3504 | 175 std::string |
2926 | 176 octave_env::get_host_name (void) |
177 { | |
178 return (instance_ok ()) | |
3504 | 179 ? instance->do_get_host_name () : std::string (); |
2926 | 180 } |
181 | |
5775 | 182 // FIXME -- this leaves no way to distinguish between a |
2926 | 183 // variable that is not set and one that is set to the empty string. |
184 // Is this a problem? | |
185 | |
3504 | 186 std::string |
187 octave_env::getenv (const std::string& name) | |
2926 | 188 { |
189 return (instance_ok ()) | |
3504 | 190 ? instance->do_getenv (name) : std::string (); |
2926 | 191 } |
192 | |
193 void | |
3504 | 194 octave_env::putenv (const std::string& name, const std::string& value) |
2926 | 195 { |
196 octave_putenv (name, value); | |
197 } | |
198 | |
199 bool | |
5489 | 200 octave_env::have_x11_display (void) |
201 { | |
202 std::string display = getenv ("DISPLAY"); | |
203 | |
204 return ! display.empty (); | |
205 } | |
206 | |
207 bool | |
3504 | 208 octave_env::chdir (const std::string& newdir) |
2926 | 209 { |
210 return (instance_ok ()) | |
211 ? instance->do_chdir (newdir) : false; | |
212 } | |
213 | |
214 void | |
3504 | 215 octave_env::do_set_program_name (const std::string& s) const |
2926 | 216 { |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
217 // For gnulib. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
218 ::set_program_name (s.c_str ()); |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
219 |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
220 // Let gnulib strip off things like the "lt-" prefix from libtool. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
221 prog_invocation_name = program_name; |
2926 | 222 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
223 size_t pos |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
224 = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 225 |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
226 // Also keep a shortened version of the program name. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
227 prog_name = (pos == std::string::npos) |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
228 ? prog_invocation_name : prog_invocation_name.substr (pos+1); |
2926 | 229 } |
230 | |
231 // Return a pretty pathname. If the first part of the pathname is the | |
232 // same as $HOME, then replace that with `~'. | |
233 | |
3504 | 234 std::string |
235 octave_env::do_polite_directory_format (const std::string& name) const | |
2926 | 236 { |
3504 | 237 std::string retval; |
2926 | 238 |
3504 | 239 std::string home_dir = do_get_home_directory (); |
2926 | 240 |
241 size_t len = home_dir.length (); | |
242 | |
3516 | 243 if (len > 1 && home_dir == name.substr (0, len) |
4097 | 244 && (name.length () == len || file_ops::is_dir_sep (name[len]))) |
2926 | 245 { |
246 retval = "~"; | |
247 retval.append (name.substr (len)); | |
248 } | |
249 else | |
250 retval = name; | |
251 | |
252 return retval; | |
253 } | |
254 | |
255 bool | |
3504 | 256 octave_env::do_absolute_pathname (const std::string& s) const |
2926 | 257 { |
4087 | 258 size_t len = s.length (); |
259 | |
260 if (len == 0) | |
261 return false; | |
2926 | 262 |
4097 | 263 if (file_ops::is_dir_sep (s[0])) |
2926 | 264 return true; |
265 | |
4101 | 266 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
4088 | 267 if ((len == 2 && isalpha (s[0]) && s[1] == ':') |
4097 | 268 || (len > 2 && isalpha (s[0]) && s[1] == ':' |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
269 && file_ops::is_dir_sep (s[2]))) |
4087 | 270 return true; |
271 #endif | |
272 | |
2926 | 273 return false; |
274 } | |
275 | |
6838 | 276 bool |
277 octave_env::do_rooted_relative_pathname (const std::string& s) const | |
278 { | |
279 size_t len = s.length (); | |
280 | |
281 if (len == 0) | |
282 return false; | |
283 | |
284 if (len == 1 && s[0] == '.') | |
285 return true; | |
286 | |
287 if (len > 1 && s[0] == '.' && file_ops::is_dir_sep (s[1])) | |
288 return true; | |
289 | |
290 if (len == 2 && s[0] == '.' && s[1] == '.') | |
291 return true; | |
292 | |
293 if (len > 2 && s[0] == '.' && s[1] == '.' && file_ops::is_dir_sep (s[2])) | |
294 return true; | |
295 | |
296 return false; | |
297 } | |
298 | |
2926 | 299 // Return the `basename' of the pathname in STRING (the stuff after |
4097 | 300 // the last directory separator). If STRING is not a full pathname, |
301 // simply return it. | |
2926 | 302 |
3504 | 303 std::string |
304 octave_env::do_base_pathname (const std::string& s) const | |
2926 | 305 { |
7609
7e6002d15d4d
octave_env::do_base_pathname: handle rooted relativel names
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
306 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s))) |
2926 | 307 return s; |
308 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
309 size_t pos = s.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 310 |
8021 | 311 if (pos == std::string::npos) |
2926 | 312 return s; |
313 else | |
314 return s.substr (pos+1); | |
315 } | |
316 | |
317 // Turn STRING (a pathname) into an absolute pathname, assuming that | |
4097 | 318 // DOT_PATH contains the symbolic location of the current directory. |
2926 | 319 |
3504 | 320 std::string |
321 octave_env::do_make_absolute (const std::string& s, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
322 const std::string& dot_path) const |
2926 | 323 { |
324 #if defined (__EMX__) | |
325 if (s.length () > 1 && s[1] == ':') | |
326 return s; | |
327 #endif | |
328 | |
4087 | 329 if (dot_path.empty () || s.empty () || do_absolute_pathname (s)) |
2926 | 330 return s; |
331 | |
4097 | 332 std::string current_dir = dot_path; |
2926 | 333 |
4097 | 334 if (current_dir.empty ()) |
335 current_dir = do_getcwd (); | |
2926 | 336 |
4097 | 337 size_t pos = current_dir.length () - 1; |
2926 | 338 |
4097 | 339 if (! file_ops::is_dir_sep (current_dir[pos])) |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
340 current_dir.append (file_ops::dir_sep_str ()); |
4097 | 341 |
5775 | 342 // FIXME -- this is probably not correct for all systems. |
2926 | 343 |
344 size_t i = 0; | |
345 size_t slen = s.length (); | |
346 | |
347 while (i < slen) | |
348 { | |
349 if (s[i] == '.') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
350 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
351 if (i + 1 == slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
352 return current_dir; |
2926 | 353 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
354 if (file_ops::is_dir_sep (s[i+1])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
355 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
356 i += 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
357 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
358 } |
2926 | 359 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
360 if (s[i+1] == '.' |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
361 && (i + 2 == slen || file_ops::is_dir_sep (s[i+2]))) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
362 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
363 i += 2; |
2926 | 364 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
365 if (i != slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
366 i++; |
2926 | 367 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
368 pathname_backup (current_dir, 1); |
2926 | 369 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
370 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
371 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
372 } |
2926 | 373 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
374 size_t tmp = s.find_first_of (file_ops::dir_sep_chars (), i); |
2926 | 375 |
8021 | 376 if (tmp == std::string::npos) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
377 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
378 current_dir.append (s, i, tmp-i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
379 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
380 } |
2926 | 381 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
382 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
383 current_dir.append (s, i, tmp-i+1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
384 i = tmp + 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
385 } |
2926 | 386 } |
387 | |
4097 | 388 return current_dir; |
2926 | 389 } |
390 | |
4097 | 391 // Return a string which is the current working directory. |
2926 | 392 |
3504 | 393 std::string |
4097 | 394 octave_env::do_getcwd () const |
2926 | 395 { |
396 if (! follow_symbolic_links) | |
397 current_directory = ""; | |
398 | |
399 if (verbatim_pwd || current_directory.empty ()) | |
400 current_directory = ::octave_getcwd (); | |
401 | |
402 return current_directory; | |
403 } | |
404 | |
405 // This value is not cached because it can change while Octave is | |
406 // running. | |
407 | |
3504 | 408 std::string |
2926 | 409 octave_env::do_get_home_directory (void) const |
410 { | |
3504 | 411 std::string hd = do_getenv ("HOME"); |
2926 | 412 |
6096 | 413 #if defined (__MINGW32__) || defined (_MSC_VER) |
414 // Maybe we are started directly from cmd.exe. | |
5451 | 415 if (hd.empty ()) |
5454 | 416 { |
417 std::string drv = do_getenv ("HOMEDRIVE"); | |
418 if (drv.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
419 hd = do_getenv ("HOMEPATH"); |
5454 | 420 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
421 hd = drv + do_getenv ("HOMEPATH"); |
5454 | 422 } |
5451 | 423 #endif |
424 | |
2947 | 425 if (hd.empty ()) |
426 { | |
427 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); | |
428 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
429 hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str ()); |
2947 | 430 } |
431 | |
432 return hd; | |
2926 | 433 } |
434 | |
3504 | 435 std::string |
2926 | 436 octave_env::do_get_user_name (void) const |
437 { | |
438 if (user_name.empty ()) | |
439 { | |
2947 | 440 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
2926 | 441 |
3504 | 442 user_name = pw ? pw.name () : std::string ("unknown"); |
2926 | 443 } |
444 | |
445 return user_name; | |
446 } | |
447 | |
3504 | 448 std::string |
2926 | 449 octave_env::do_get_host_name (void) const |
450 { | |
451 if (host_name.empty ()) | |
452 { | |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
453 char hostname[1024]; |
2926 | 454 |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
455 int status = gnulib::gethostname (hostname, 1023); |
2926 | 456 |
3185 | 457 host_name = (status < 0) ? "unknown" : hostname; |
2926 | 458 } |
459 | |
460 return host_name; | |
461 } | |
462 | |
3504 | 463 std::string |
464 octave_env::do_getenv (const std::string& name) const | |
2926 | 465 { |
466 char *value = ::getenv (name.c_str ()); | |
467 | |
468 return value ? value : ""; | |
469 } | |
470 | |
471 // Do the work of changing to the directory NEWDIR. Handle symbolic | |
472 // link following, etc. | |
473 | |
474 bool | |
3504 | 475 octave_env::do_chdir (const std::string& newdir) |
2926 | 476 { |
477 bool retval = false; | |
478 | |
3504 | 479 std::string tmp; |
2926 | 480 |
481 if (follow_symbolic_links) | |
482 { | |
483 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
484 do_getcwd (); |
2926 | 485 |
486 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
487 tmp = newdir; |
2926 | 488 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
489 tmp = do_make_absolute (newdir, current_directory); |
2926 | 490 |
4097 | 491 // Get rid of trailing directory separator. |
2926 | 492 |
493 size_t len = tmp.length (); | |
494 | |
495 if (len > 1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
496 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
497 if (file_ops::is_dir_sep (tmp[--len])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
498 tmp.resize (len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
499 } |
2926 | 500 |
501 if (! ::octave_chdir (tmp)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
502 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
503 current_directory = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
504 retval = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
505 } |
2926 | 506 } |
507 else | |
508 retval = (! ::octave_chdir (newdir)); | |
509 | |
510 return retval; | |
511 } | |
512 | |
513 // Remove the last N directories from PATH. | |
514 | |
515 void | |
3504 | 516 octave_env::pathname_backup (std::string& path, int n) const |
2926 | 517 { |
518 if (path.empty ()) | |
519 return; | |
520 | |
521 size_t i = path.length () - 1; | |
522 | |
523 while (n--) | |
524 { | |
4097 | 525 while (file_ops::is_dir_sep (path[i]) && i > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
526 i--; |
2926 | 527 |
4097 | 528 while (! file_ops::is_dir_sep (path[i]) && i > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
529 i--; |
2926 | 530 |
531 i++; | |
532 } | |
533 | |
534 path.resize (i); | |
535 } | |
536 | |
537 void | |
538 octave_env::error (int err_num) const | |
539 { | |
10411 | 540 (*current_liboctave_error_handler) ("%s", gnulib::strerror (err_num)); |
2926 | 541 } |
542 | |
543 void | |
3504 | 544 octave_env::error (const std::string& s) const |
2926 | 545 { |
546 (*current_liboctave_error_handler) ("%s", s.c_str ()); | |
547 } |