Mercurial > hg > octave-nkf
annotate liboctave/oct-env.cc @ 13983:7dd7cccf0757
clean up memory allocated for singletons before exit
* singleton-cleanup.h, singleton-cleanup.cc: New files.
* liboctave/Makefile.am (INCS, LIBOCTAVE_CXX_SOURCES):
Add them to the lists.
* toplev.cc (clean_up_and_exit): Call singleton_cleanup_list::cleanup.
* debug.h, debug.cc (bp_table::instance_ok): Move definition to
debug.cc.
* coment-list.h (octave_comment_buffer::~octave_comment_buffer):
Define destructor for class.
* ov-typeinfo.h (octave_value_typeinfo::~octave_value_typeinfo):
Likewise.
* cmd-edit.h (command_editor::cleanup_instance): New function.
* cmd-hist.h (command_history::cleanup_instance): New function.
* file-ops.h (file_ops::cleanup_instance): New function.
* mach-info.h (oct_mach_info::cleanup_instance): New function.
* oct-env.h (octave_env::cleanup_instance): New function.
* oct-fftw.h (octave_fftw_planner::cleanup_instance): New function.
* oct-rand.h (octave_rand::cleanup_instance): New function.
* oct-spparms.h (octave_sparse_params::cleanup_instance):
New function.
* pathsearch.h (static_members::cleanup_instance): New function.
* comment-list.h (octave_comment_buffer::cleanup_instance):
New function.
* debug.h (bp_table::cleanup_instance): New function.
* display.h (display_info::cleanup_instance): New function.
* dynamic-ld.cc (octave_shlib_list::cleanup_instance,
octave_mex_file_list::cleanup_instance): New functions.
* dynamic-ld.h (octave_dynamic_loader::cleanup_instance):
New function.
* load-path.h (load_path::cleanup_instance): New function.
* oct-stream.h (octave_stream_list::cleanup_instance): New function.
* ov-typeinfo.h (octave_value_typeinfo::cleanup_instance):
New function.
* pager.h, pager.cc (octave_pager_stream::instance_ok,
octave_pager_stream::cleanup_instance): New functions.
(octave_diary_stream::instance_ok,
octave_diary_stream::cleanup_instance): New functions.
* sighandlers.h (octave_child_list::cleanup_instance): New function.
* toplev.h (octave_call_stack
* pager.cc (octave_pager_stream::stream, octave_diary_stream::stream):
Use instance_ok to create instance.
* toplev.h (octave_call_stack::cleanup_instance): New function.
* cmd-edit.cc (command_editor::instance_ok):
Register cleanup function.
* cmd-hist.cc (command_history::instance_ok): Likewise.
* file-ops.cc (file_ops::instance_ok): Likewise.
* mach-info.cc (oct_mach_info::instance_ok): Likewise.
* oct-env.cc (octave_env::instance_ok): Likewise.
* oct-fftw.cc (octave_fftw_planner::instance_ok): Likewise.
* oct-rand.cc (octave_rand::instance_ok): Likewise.
* oct-spparms.cc (octave_sparse_params::instance_ok): Likewise.
* pathsearch.cc (dir_path::static_members::instance_ok): Likewise.
* comment-list.cc (comment_list::instance_ok): Likewise.
* debug.cc (bp_table::instance_ok): Likewise.
* display.cc (display_info::instance_ok): Likewise.
* dynamic-ld.cc (octave_shlib_list::instance_ok,
octave_mex_file_list::instance_ok, octave_dynamic_loader): Likewise.
* load-path.cc (load_path::instance_ok): Likewise.
* oct-stream.cc (octave_stream_list::instance_ok): Likewise.
* ov-typeinfo.cc (octave_value_typeinfo::instance_ok): Likewise.
* sighandlers.cc (octave_child_list::instance_ok): Likewise.
* symtab.h, symtab.cc (symbol_table::scope_id::create_instance):
New function.
* symtab.h (symbol_table::scope_id::instance_ok): Call create_instance.
* toplev.h, toplev.cc (octave_call_stack::create_instance):
New function.
* toplev.cc (octave_call_stack::instance_ok): Call create_instance.
* pager.h, pager.cc (octave_pager_stream::set_diary_skip,
octave_pager_stream::flush_current_contents_to_diary): Now static.
octave_pager_stream::do_set_diary_skip,
octave_pager_stream::do_flush_current_contents_to_diary):
New functions.
(octave_pager_stream::stream): Return std::ostream&, not
octave_pager_stream&. If instance creation fails, return std::cout.
(octave_diary_stream::stream): Return std::ostream&, not
octave_diary_stream&. If instance creation fails, return std::cout.
(octave_pager_stream::do_reset, octave_diary_stream::do_reset):
Use instance_ok to create instance.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 03 Dec 2011 04:34:17 -0500 |
parents | 12df7854fa7c |
children | 72c96de7a403 |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1996-2011 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 ()) | |
3504 | 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 ()) | |
114 ? instance->do_absolute_pathname (s) : false; | |
115 } | |
116 | |
6838 | 117 bool |
118 octave_env::rooted_relative_pathname (const std::string& s) | |
119 { | |
120 return (instance_ok ()) | |
121 ? instance->do_rooted_relative_pathname (s) : false; | |
122 } | |
123 | |
3504 | 124 std::string |
125 octave_env::base_pathname (const std::string& s) | |
2926 | 126 { |
127 return (instance_ok ()) | |
3504 | 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 ()) | |
3504 | 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 ()) | |
3504 | 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 ()) | |
3504 | 149 ? instance->do_get_home_directory () : std::string (); |
2926 | 150 } |
151 | |
3504 | 152 std::string |
2926 | 153 octave_env::get_program_name (void) |
154 { | |
155 return (instance_ok ()) | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
156 ? instance->prog_name : std::string (); |
2926 | 157 } |
158 | |
3504 | 159 std::string |
2926 | 160 octave_env::get_program_invocation_name (void) |
161 { | |
162 return (instance_ok ()) | |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
163 ? instance->prog_invocation_name : std::string (); |
2926 | 164 } |
165 | |
166 void | |
3504 | 167 octave_env::set_program_name (const std::string& s) |
2926 | 168 { |
169 if (instance_ok ()) | |
170 instance->do_set_program_name (s); | |
171 } | |
172 | |
3504 | 173 std::string |
2926 | 174 octave_env::get_user_name (void) |
175 { | |
176 return (instance_ok ()) | |
3504 | 177 ? instance->do_get_user_name () : std::string (); |
2926 | 178 } |
179 | |
3504 | 180 std::string |
2926 | 181 octave_env::get_host_name (void) |
182 { | |
183 return (instance_ok ()) | |
3504 | 184 ? instance->do_get_host_name () : std::string (); |
2926 | 185 } |
186 | |
5775 | 187 // FIXME -- this leaves no way to distinguish between a |
2926 | 188 // variable that is not set and one that is set to the empty string. |
189 // Is this a problem? | |
190 | |
3504 | 191 std::string |
192 octave_env::getenv (const std::string& name) | |
2926 | 193 { |
194 return (instance_ok ()) | |
3504 | 195 ? instance->do_getenv (name) : std::string (); |
2926 | 196 } |
197 | |
198 void | |
3504 | 199 octave_env::putenv (const std::string& name, const std::string& value) |
2926 | 200 { |
201 octave_putenv (name, value); | |
202 } | |
203 | |
204 bool | |
5489 | 205 octave_env::have_x11_display (void) |
206 { | |
207 std::string display = getenv ("DISPLAY"); | |
208 | |
209 return ! display.empty (); | |
210 } | |
211 | |
212 bool | |
3504 | 213 octave_env::chdir (const std::string& newdir) |
2926 | 214 { |
215 return (instance_ok ()) | |
216 ? instance->do_chdir (newdir) : false; | |
217 } | |
218 | |
219 void | |
3504 | 220 octave_env::do_set_program_name (const std::string& s) const |
2926 | 221 { |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
222 // For gnulib. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
223 ::set_program_name (s.c_str ()); |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
224 |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
225 // 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
|
226 prog_invocation_name = program_name; |
2926 | 227 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
228 size_t pos |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
229 = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 230 |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
231 // Also keep a shortened version of the program name. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
232 prog_name = (pos == std::string::npos) |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
233 ? prog_invocation_name : prog_invocation_name.substr (pos+1); |
2926 | 234 } |
235 | |
236 // Return a pretty pathname. If the first part of the pathname is the | |
237 // same as $HOME, then replace that with `~'. | |
238 | |
3504 | 239 std::string |
240 octave_env::do_polite_directory_format (const std::string& name) const | |
2926 | 241 { |
3504 | 242 std::string retval; |
2926 | 243 |
3504 | 244 std::string home_dir = do_get_home_directory (); |
2926 | 245 |
246 size_t len = home_dir.length (); | |
247 | |
3516 | 248 if (len > 1 && home_dir == name.substr (0, len) |
4097 | 249 && (name.length () == len || file_ops::is_dir_sep (name[len]))) |
2926 | 250 { |
251 retval = "~"; | |
252 retval.append (name.substr (len)); | |
253 } | |
254 else | |
255 retval = name; | |
256 | |
257 return retval; | |
258 } | |
259 | |
260 bool | |
3504 | 261 octave_env::do_absolute_pathname (const std::string& s) const |
2926 | 262 { |
4087 | 263 size_t len = s.length (); |
264 | |
265 if (len == 0) | |
266 return false; | |
2926 | 267 |
4097 | 268 if (file_ops::is_dir_sep (s[0])) |
2926 | 269 return true; |
270 | |
4101 | 271 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
4088 | 272 if ((len == 2 && isalpha (s[0]) && s[1] == ':') |
4097 | 273 || (len > 2 && isalpha (s[0]) && s[1] == ':' |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
274 && file_ops::is_dir_sep (s[2]))) |
4087 | 275 return true; |
276 #endif | |
277 | |
2926 | 278 return false; |
279 } | |
280 | |
6838 | 281 bool |
282 octave_env::do_rooted_relative_pathname (const std::string& s) const | |
283 { | |
284 size_t len = s.length (); | |
285 | |
286 if (len == 0) | |
287 return false; | |
288 | |
289 if (len == 1 && s[0] == '.') | |
290 return true; | |
291 | |
292 if (len > 1 && s[0] == '.' && file_ops::is_dir_sep (s[1])) | |
293 return true; | |
294 | |
295 if (len == 2 && s[0] == '.' && s[1] == '.') | |
296 return true; | |
297 | |
298 if (len > 2 && s[0] == '.' && s[1] == '.' && file_ops::is_dir_sep (s[2])) | |
299 return true; | |
300 | |
301 return false; | |
302 } | |
303 | |
2926 | 304 // Return the `basename' of the pathname in STRING (the stuff after |
4097 | 305 // the last directory separator). If STRING is not a full pathname, |
306 // simply return it. | |
2926 | 307 |
3504 | 308 std::string |
309 octave_env::do_base_pathname (const std::string& s) const | |
2926 | 310 { |
7609
7e6002d15d4d
octave_env::do_base_pathname: handle rooted relativel names
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
311 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s))) |
2926 | 312 return s; |
313 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
314 size_t pos = s.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 315 |
8021 | 316 if (pos == std::string::npos) |
2926 | 317 return s; |
318 else | |
319 return s.substr (pos+1); | |
320 } | |
321 | |
322 // Turn STRING (a pathname) into an absolute pathname, assuming that | |
4097 | 323 // DOT_PATH contains the symbolic location of the current directory. |
2926 | 324 |
3504 | 325 std::string |
326 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
|
327 const std::string& dot_path) const |
2926 | 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 } |