Mercurial > hg > octave-nkf
annotate liboctave/system/oct-env.cc @ 20811:a22d8a2eb0e5
fix adaptive strategy in ode solvers.
* script/ode/ode45.m: remove unused option OutputSave
* script/ode/private/integrate_adaptive.m: rewrite algorithm
to be more compatible.
* script/ode/private/runge_kutta_45_dorpri.m: use kahan summation
for time increment.
author | Carlo de Falco <carlo.defalco@polimi.it> |
---|---|
date | Sun, 11 Oct 2015 18:44:58 +0200 |
parents | 4197fc428c7d |
children |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19794
diff
changeset
|
3 Copyright (C) 1996-2015 John W. Eaton |
2926 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
2926 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
2926 | 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 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 */ |
2926 | 38 |
39 #ifdef HAVE_CONFIG_H | |
40 #include <config.h> | |
41 #endif | |
42 | |
4093 | 43 #include <cctype> |
2926 | 44 #include <cstdlib> |
10463
bbe99b2a5ba7
undo recent gnulib-related changes
John W. Eaton <jwe@octave.org>
parents:
10447
diff
changeset
|
45 #include <cstring> |
4093 | 46 |
3504 | 47 #include <string> |
2926 | 48 |
49 #include <sys/types.h> | |
50 #include <unistd.h> | |
51 | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
52 #include "progname.h" |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
53 |
4097 | 54 #include "file-ops.h" |
2926 | 55 #include "lo-error.h" |
56 #include "lo-sysdep.h" | |
57 #include "lo-utils.h" | |
58 #include "oct-env.h" | |
2934 | 59 #include "oct-passwd.h" |
2947 | 60 #include "oct-syscalls.h" |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
61 #include "singleton-cleanup.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) | |
13983
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
85 { |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
86 instance = new octave_env (); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
87 |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
88 if (instance) |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
89 singleton_cleanup_list::add (cleanup_instance); |
7dd7cccf0757
clean up memory allocated for singletons before exit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
90 } |
2926 | 91 |
92 if (! instance) | |
93 { | |
94 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
95 ("unable to create current working directory object!"); |
2926 | 96 |
97 retval = false; | |
98 } | |
99 | |
100 return retval; | |
101 } | |
102 | |
3504 | 103 std::string |
104 octave_env::polite_directory_format (const std::string& name) | |
2926 | 105 { |
106 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 ? instance->do_polite_directory_format (name) : std::string (); |
2926 | 108 } |
109 | |
110 bool | |
3504 | 111 octave_env::absolute_pathname (const std::string& s) |
2926 | 112 { |
113 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 ? instance->do_absolute_pathname (s) : false; |
2926 | 115 } |
116 | |
6838 | 117 bool |
118 octave_env::rooted_relative_pathname (const std::string& s) | |
119 { | |
120 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
121 ? instance->do_rooted_relative_pathname (s) : false; |
6838 | 122 } |
123 | |
3504 | 124 std::string |
125 octave_env::base_pathname (const std::string& s) | |
2926 | 126 { |
127 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 ? instance->do_base_pathname (s) : std::string (); |
2926 | 129 } |
130 | |
3504 | 131 std::string |
132 octave_env::make_absolute (const std::string& s, const std::string& dot_path) | |
2926 | 133 { |
134 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
135 ? instance->do_make_absolute (s, dot_path) : std::string (); |
2926 | 136 } |
137 | |
3504 | 138 std::string |
10250 | 139 octave_env::get_current_directory () |
2926 | 140 { |
141 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
142 ? instance->do_getcwd () : std::string (); |
2926 | 143 } |
144 | |
3504 | 145 std::string |
2926 | 146 octave_env::get_home_directory () |
147 { | |
148 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
149 ? instance->do_get_home_directory () : std::string (); |
2926 | 150 } |
151 | |
3504 | 152 std::string |
19655
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
153 octave_env::get_temp_directory () |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
154 { |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
155 return (instance_ok ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
156 ? instance->do_get_temp_directory () : std::string (); |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
157 } |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
158 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
159 std::string |
2926 | 160 octave_env::get_program_name (void) |
161 { | |
162 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
163 ? instance->prog_name : std::string (); |
2926 | 164 } |
165 | |
3504 | 166 std::string |
2926 | 167 octave_env::get_program_invocation_name (void) |
168 { | |
169 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
170 ? instance->prog_invocation_name : std::string (); |
2926 | 171 } |
172 | |
173 void | |
3504 | 174 octave_env::set_program_name (const std::string& s) |
2926 | 175 { |
176 if (instance_ok ()) | |
177 instance->do_set_program_name (s); | |
178 } | |
179 | |
3504 | 180 std::string |
2926 | 181 octave_env::get_user_name (void) |
182 { | |
183 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
184 ? instance->do_get_user_name () : std::string (); |
2926 | 185 } |
186 | |
3504 | 187 std::string |
2926 | 188 octave_env::get_host_name (void) |
189 { | |
190 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
191 ? instance->do_get_host_name () : std::string (); |
2926 | 192 } |
193 | |
19655
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
194 std::string |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
195 octave_env::do_get_temp_directory (void) const |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
196 { |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
197 std::string tempd; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
198 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
199 #if defined (__MINGW32__) || defined (_MSC_VER) |
19794
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19656
diff
changeset
|
200 |
19655
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
201 tempd = do_getenv ("TEMP"); |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
202 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
203 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
204 tempd = do_getenv ("TMP"); |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
205 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
206 #if defined (P_tmpdir) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
207 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
208 tempd = P_tmpdir; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
209 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
210 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
211 // Some versions of MinGW and MSVC either don't define P_tmpdir, or |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
212 // define it to a single backslash. In such cases just use C:\temp. |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
213 if (tempd.empty () || tempd == "\\") |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
214 tempd = "c:\\temp"; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
215 |
19656
f765fea3ca14
Fix compile warning about comment at end of line in cset d8fd3842a507.
Rik <rik@octave.org>
parents:
19655
diff
changeset
|
216 #else // Unix-like OS |
19655
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
217 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
218 tempd = do_getenv ("TMP"); |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
219 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
220 #if defined (P_tmpdir) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
221 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
222 tempd = P_tmpdir; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
223 #else |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
224 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
225 tempd = "/tmp"; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
226 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
227 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
228 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
229 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
230 return tempd; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
231 } |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
232 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
233 // FIXME: this leaves no way to distinguish between a |
2926 | 234 // variable that is not set and one that is set to the empty string. |
235 // Is this a problem? | |
236 | |
3504 | 237 std::string |
238 octave_env::getenv (const std::string& name) | |
2926 | 239 { |
240 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
241 ? instance->do_getenv (name) : std::string (); |
2926 | 242 } |
243 | |
244 void | |
3504 | 245 octave_env::putenv (const std::string& name, const std::string& value) |
2926 | 246 { |
247 octave_putenv (name, value); | |
248 } | |
249 | |
250 bool | |
5489 | 251 octave_env::have_x11_display (void) |
252 { | |
253 std::string display = getenv ("DISPLAY"); | |
254 | |
255 return ! display.empty (); | |
256 } | |
257 | |
258 bool | |
3504 | 259 octave_env::chdir (const std::string& newdir) |
2926 | 260 { |
261 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
262 ? instance->do_chdir (newdir) : false; |
2926 | 263 } |
264 | |
265 void | |
3504 | 266 octave_env::do_set_program_name (const std::string& s) const |
2926 | 267 { |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
268 // For gnulib. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
269 ::set_program_name (s.c_str ()); |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
270 |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
271 // 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
|
272 prog_invocation_name = program_name; |
2926 | 273 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
274 size_t pos |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
275 = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 276 |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
277 // Also keep a shortened version of the program name. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
278 prog_name = (pos == std::string::npos) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
279 ? prog_invocation_name : prog_invocation_name.substr (pos+1); |
2926 | 280 } |
281 | |
282 // Return a pretty pathname. If the first part of the pathname is the | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
283 // same as $HOME, then replace that with '~'. |
2926 | 284 |
3504 | 285 std::string |
286 octave_env::do_polite_directory_format (const std::string& name) const | |
2926 | 287 { |
3504 | 288 std::string retval; |
2926 | 289 |
3504 | 290 std::string home_dir = do_get_home_directory (); |
2926 | 291 |
292 size_t len = home_dir.length (); | |
293 | |
3516 | 294 if (len > 1 && home_dir == name.substr (0, len) |
4097 | 295 && (name.length () == len || file_ops::is_dir_sep (name[len]))) |
2926 | 296 { |
297 retval = "~"; | |
298 retval.append (name.substr (len)); | |
299 } | |
300 else | |
301 retval = name; | |
302 | |
303 return retval; | |
304 } | |
305 | |
306 bool | |
3504 | 307 octave_env::do_absolute_pathname (const std::string& s) const |
2926 | 308 { |
4087 | 309 size_t len = s.length (); |
310 | |
311 if (len == 0) | |
312 return false; | |
2926 | 313 |
4097 | 314 if (file_ops::is_dir_sep (s[0])) |
2926 | 315 return true; |
316 | |
4101 | 317 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
4088 | 318 if ((len == 2 && isalpha (s[0]) && s[1] == ':') |
4097 | 319 || (len > 2 && isalpha (s[0]) && s[1] == ':' |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
320 && file_ops::is_dir_sep (s[2]))) |
4087 | 321 return true; |
322 #endif | |
323 | |
2926 | 324 return false; |
325 } | |
326 | |
6838 | 327 bool |
328 octave_env::do_rooted_relative_pathname (const std::string& s) const | |
329 { | |
330 size_t len = s.length (); | |
331 | |
332 if (len == 0) | |
333 return false; | |
334 | |
335 if (len == 1 && s[0] == '.') | |
336 return true; | |
337 | |
338 if (len > 1 && s[0] == '.' && file_ops::is_dir_sep (s[1])) | |
339 return true; | |
340 | |
341 if (len == 2 && s[0] == '.' && s[1] == '.') | |
342 return true; | |
343 | |
344 if (len > 2 && s[0] == '.' && s[1] == '.' && file_ops::is_dir_sep (s[2])) | |
345 return true; | |
346 | |
347 return false; | |
348 } | |
349 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
350 // Return the 'basename' of the pathname in STRING (the stuff after |
4097 | 351 // the last directory separator). If STRING is not a full pathname, |
352 // simply return it. | |
2926 | 353 |
3504 | 354 std::string |
355 octave_env::do_base_pathname (const std::string& s) const | |
2926 | 356 { |
7609
7e6002d15d4d
octave_env::do_base_pathname: handle rooted relativel names
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
357 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s))) |
2926 | 358 return s; |
359 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
360 size_t pos = s.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 361 |
8021 | 362 if (pos == std::string::npos) |
2926 | 363 return s; |
364 else | |
365 return s.substr (pos+1); | |
366 } | |
367 | |
368 // Turn STRING (a pathname) into an absolute pathname, assuming that | |
4097 | 369 // DOT_PATH contains the symbolic location of the current directory. |
2926 | 370 |
3504 | 371 std::string |
372 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
|
373 const std::string& dot_path) const |
2926 | 374 { |
4087 | 375 if (dot_path.empty () || s.empty () || do_absolute_pathname (s)) |
2926 | 376 return s; |
377 | |
4097 | 378 std::string current_dir = dot_path; |
2926 | 379 |
4097 | 380 if (current_dir.empty ()) |
381 current_dir = do_getcwd (); | |
2926 | 382 |
4097 | 383 size_t pos = current_dir.length () - 1; |
2926 | 384 |
4097 | 385 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
|
386 current_dir.append (file_ops::dir_sep_str ()); |
4097 | 387 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
388 // FIXME: this is probably not correct for all systems. |
2926 | 389 |
390 size_t i = 0; | |
391 size_t slen = s.length (); | |
392 | |
393 while (i < slen) | |
394 { | |
395 if (s[i] == '.') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
396 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
397 if (i + 1 == slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
398 return current_dir; |
2926 | 399 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
400 if (file_ops::is_dir_sep (s[i+1])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
401 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
402 i += 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
403 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
404 } |
2926 | 405 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
406 if (s[i+1] == '.' |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
407 && (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
|
408 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
409 i += 2; |
2926 | 410 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
411 if (i != slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
412 i++; |
2926 | 413 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
414 pathname_backup (current_dir, 1); |
2926 | 415 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
416 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
417 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
418 } |
2926 | 419 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
420 size_t tmp = s.find_first_of (file_ops::dir_sep_chars (), i); |
2926 | 421 |
8021 | 422 if (tmp == std::string::npos) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
423 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
424 current_dir.append (s, i, tmp-i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
425 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
426 } |
2926 | 427 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
428 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
429 current_dir.append (s, i, tmp-i+1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
430 i = tmp + 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
431 } |
2926 | 432 } |
433 | |
4097 | 434 return current_dir; |
2926 | 435 } |
436 | |
4097 | 437 // Return a string which is the current working directory. |
2926 | 438 |
3504 | 439 std::string |
4097 | 440 octave_env::do_getcwd () const |
2926 | 441 { |
442 if (! follow_symbolic_links) | |
443 current_directory = ""; | |
444 | |
445 if (verbatim_pwd || current_directory.empty ()) | |
446 current_directory = ::octave_getcwd (); | |
447 | |
448 return current_directory; | |
449 } | |
450 | |
451 // This value is not cached because it can change while Octave is | |
452 // running. | |
453 | |
3504 | 454 std::string |
2926 | 455 octave_env::do_get_home_directory (void) const |
456 { | |
3504 | 457 std::string hd = do_getenv ("HOME"); |
2926 | 458 |
6096 | 459 #if defined (__MINGW32__) || defined (_MSC_VER) |
460 // Maybe we are started directly from cmd.exe. | |
5451 | 461 if (hd.empty ()) |
5454 | 462 { |
463 std::string drv = do_getenv ("HOMEDRIVE"); | |
464 if (drv.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
465 hd = do_getenv ("HOMEPATH"); |
5454 | 466 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
467 hd = drv + do_getenv ("HOMEPATH"); |
5454 | 468 } |
5451 | 469 #endif |
470 | |
2947 | 471 if (hd.empty ()) |
472 { | |
473 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); | |
474 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
475 hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str ()); |
2947 | 476 } |
477 | |
478 return hd; | |
2926 | 479 } |
480 | |
3504 | 481 std::string |
2926 | 482 octave_env::do_get_user_name (void) const |
483 { | |
484 if (user_name.empty ()) | |
485 { | |
2947 | 486 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
2926 | 487 |
3504 | 488 user_name = pw ? pw.name () : std::string ("unknown"); |
2926 | 489 } |
490 | |
491 return user_name; | |
492 } | |
493 | |
3504 | 494 std::string |
2926 | 495 octave_env::do_get_host_name (void) const |
496 { | |
497 if (host_name.empty ()) | |
498 { | |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
499 char hostname[1024]; |
2926 | 500 |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
501 int status = gnulib::gethostname (hostname, 1023); |
2926 | 502 |
3185 | 503 host_name = (status < 0) ? "unknown" : hostname; |
2926 | 504 } |
505 | |
506 return host_name; | |
507 } | |
508 | |
3504 | 509 std::string |
510 octave_env::do_getenv (const std::string& name) const | |
2926 | 511 { |
512 char *value = ::getenv (name.c_str ()); | |
513 | |
514 return value ? value : ""; | |
515 } | |
516 | |
517 // Do the work of changing to the directory NEWDIR. Handle symbolic | |
518 // link following, etc. | |
519 | |
520 bool | |
3504 | 521 octave_env::do_chdir (const std::string& newdir) |
2926 | 522 { |
523 bool retval = false; | |
524 | |
3504 | 525 std::string tmp; |
2926 | 526 |
527 if (follow_symbolic_links) | |
528 { | |
529 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
530 do_getcwd (); |
2926 | 531 |
532 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
533 tmp = newdir; |
2926 | 534 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
535 tmp = do_make_absolute (newdir, current_directory); |
2926 | 536 |
4097 | 537 // Get rid of trailing directory separator. |
2926 | 538 |
539 size_t len = tmp.length (); | |
540 | |
541 if (len > 1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
542 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
543 if (file_ops::is_dir_sep (tmp[--len])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
544 tmp.resize (len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
545 } |
2926 | 546 |
547 if (! ::octave_chdir (tmp)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
548 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
549 current_directory = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
550 retval = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
551 } |
2926 | 552 } |
553 else | |
554 retval = (! ::octave_chdir (newdir)); | |
555 | |
556 return retval; | |
557 } | |
558 | |
559 // Remove the last N directories from PATH. | |
560 | |
561 void | |
3504 | 562 octave_env::pathname_backup (std::string& path, int n) const |
2926 | 563 { |
564 if (path.empty ()) | |
565 return; | |
566 | |
567 size_t i = path.length () - 1; | |
568 | |
569 while (n--) | |
570 { | |
4097 | 571 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
|
572 i--; |
2926 | 573 |
4097 | 574 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
|
575 i--; |
2926 | 576 |
577 i++; | |
578 } | |
579 | |
580 path.resize (i); | |
581 } | |
582 | |
583 void | |
584 octave_env::error (int err_num) const | |
585 { | |
10411 | 586 (*current_liboctave_error_handler) ("%s", gnulib::strerror (err_num)); |
2926 | 587 } |
588 | |
589 void | |
3504 | 590 octave_env::error (const std::string& s) const |
2926 | 591 { |
592 (*current_liboctave_error_handler) ("%s", s.c_str ()); | |
593 } |