Mercurial > octave
annotate liboctave/system/oct-env.cc @ 21202:f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
* Array-C.cc, Array-b.cc, Array-ch.cc, Array-d.cc, Array-f.cc, Array-fC.cc,
Array-i.cc, Array-idx-vec.cc, Array-s.cc, Array-str.cc, Array-util.cc,
Array-voidp.cc, Array.cc, CColVector.cc, CDiagMatrix.cc, CMatrix.cc,
CNDArray.cc, CRowVector.cc, CSparse.cc, CSparse.h, DiagArray2.cc, MArray-C.cc,
MArray-d.cc, MArray-f.cc, MArray-fC.cc, MArray-i.cc, MArray-s.cc, MArray.cc,
MDiagArray2.cc, MSparse-C.cc, MSparse-d.cc, MSparse.h, MatrixType.cc,
PermMatrix.cc, Range.cc, Sparse-C.cc, Sparse-b.cc, Sparse-d.cc, Sparse.cc,
boolMatrix.cc, boolNDArray.cc, boolSparse.cc, chMatrix.cc, chNDArray.cc,
dColVector.cc, dDiagMatrix.cc, dMatrix.cc, dNDArray.cc, dRowVector.cc,
dSparse.cc, dSparse.h, dim-vector.cc, fCColVector.cc, fCDiagMatrix.cc,
fCMatrix.cc, fCNDArray.cc, fCRowVector.cc, fColVector.cc, fDiagMatrix.cc,
fMatrix.cc, fNDArray.cc, fRowVector.cc, idx-vector.cc, int16NDArray.cc,
int32NDArray.cc, int64NDArray.cc, int8NDArray.cc, intNDArray.cc,
uint16NDArray.cc, uint32NDArray.cc, uint64NDArray.cc, uint8NDArray.cc,
blaswrap.c, cquit.c, f77-extern.cc, f77-fcn.c, f77-fcn.h, lo-error.c, quit.cc,
quit.h, CmplxAEPBAL.cc, CmplxCHOL.cc, CmplxGEPBAL.cc, CmplxHESS.cc, CmplxLU.cc,
CmplxQR.cc, CmplxQRP.cc, CmplxSCHUR.cc, CmplxSVD.cc, CollocWt.cc, DASPK.cc,
DASRT.cc, DASSL.cc, EIG.cc, LSODE.cc, ODES.cc, Quad.cc, base-lu.cc, base-qr.cc,
dbleAEPBAL.cc, dbleCHOL.cc, dbleGEPBAL.cc, dbleHESS.cc, dbleLU.cc, dbleQR.cc,
dbleQRP.cc, dbleSCHUR.cc, dbleSVD.cc, eigs-base.cc, fCmplxAEPBAL.cc,
fCmplxCHOL.cc, fCmplxGEPBAL.cc, fCmplxHESS.cc, fCmplxLU.cc, fCmplxQR.cc,
fCmplxQRP.cc, fCmplxSCHUR.cc, fCmplxSVD.cc, fEIG.cc, floatAEPBAL.cc,
floatCHOL.cc, floatGEPBAL.cc, floatHESS.cc, floatLU.cc, floatQR.cc,
floatQRP.cc, floatSCHUR.cc, floatSVD.cc, lo-mappers.cc, lo-specfun.cc,
oct-convn.cc, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-rand.cc,
oct-spparms.cc, randgamma.c, randmtzig.c, randpoisson.c, sparse-chol.cc,
sparse-dmsolve.cc, sparse-lu.cc, sparse-qr.cc, mx-defs.h, dir-ops.cc,
file-ops.cc, file-stat.cc, lo-sysdep.cc, mach-info.cc, oct-env.cc,
oct-group.cc, oct-openmp.h, oct-passwd.cc, oct-syscalls.cc, oct-time.cc,
oct-uname.cc, pathlen.h, sysdir.h, syswait.h, cmd-edit.cc, cmd-hist.cc,
data-conv.cc, f2c-main.c, glob-match.cc, lo-array-errwarn.cc,
lo-array-gripes.cc, lo-cutils.c, lo-cutils.h, lo-ieee.cc, lo-math.h,
lo-regexp.cc, lo-utils.cc, oct-base64.cc, oct-glob.cc, oct-inttypes.cc,
oct-inttypes.h, oct-locbuf.cc, oct-mutex.cc, oct-refcount.h, oct-rl-edit.c,
oct-rl-hist.c, oct-shlib.cc, oct-sort.cc, pathsearch.cc, singleton-cleanup.cc,
sparse-sort.cc, sparse-util.cc, statdefs.h, str-vec.cc, unwind-prot.cc,
url-transfer.cc, display-available.h, main-cli.cc, main-gui.cc, main.in.cc,
mkoctfile.in.cc, octave-config.in.cc, shared-fcns.h:
indent #ifdef blocks in liboctave and src directories.
author | Rik <rik@octave.org> |
---|---|
date | Sat, 06 Feb 2016 06:40:13 -0800 |
parents | 7cac4e7458f2 |
children | 40de9f8f23a6 |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19597
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 | |
21202
f7121e111991
maint: indent #ifdef blocks in liboctave and src directories.
Rik <rik@octave.org>
parents:
21136
diff
changeset
|
40 # include <config.h> |
2926 | 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) | |
21136
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
93 (*current_liboctave_error_handler) |
7cac4e7458f2
maint: clean up code around calls to current_liboctave_error_handler.
Rik <rik@octave.org>
parents:
21017
diff
changeset
|
94 ("unable to create current working directory object!"); |
2926 | 95 |
96 return retval; | |
97 } | |
98 | |
3504 | 99 std::string |
100 octave_env::polite_directory_format (const std::string& name) | |
2926 | 101 { |
102 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
103 ? instance->do_polite_directory_format (name) : ""; |
2926 | 104 } |
105 | |
106 bool | |
3504 | 107 octave_env::absolute_pathname (const std::string& s) |
2926 | 108 { |
109 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 ? instance->do_absolute_pathname (s) : false; |
2926 | 111 } |
112 | |
6838 | 113 bool |
114 octave_env::rooted_relative_pathname (const std::string& s) | |
115 { | |
116 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
117 ? instance->do_rooted_relative_pathname (s) : false; |
6838 | 118 } |
119 | |
3504 | 120 std::string |
121 octave_env::base_pathname (const std::string& s) | |
2926 | 122 { |
123 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
124 ? instance->do_base_pathname (s) : ""; |
2926 | 125 } |
126 | |
3504 | 127 std::string |
128 octave_env::make_absolute (const std::string& s, const std::string& dot_path) | |
2926 | 129 { |
130 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
131 ? instance->do_make_absolute (s, dot_path) : ""; |
2926 | 132 } |
133 | |
3504 | 134 std::string |
10250 | 135 octave_env::get_current_directory () |
2926 | 136 { |
137 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
138 ? instance->do_getcwd () : ""; |
2926 | 139 } |
140 | |
3504 | 141 std::string |
2926 | 142 octave_env::get_home_directory () |
143 { | |
144 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
145 ? instance->do_get_home_directory () : ""; |
2926 | 146 } |
147 | |
3504 | 148 std::string |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
149 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
|
150 { |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
151 return (instance_ok ()) |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
152 ? instance->do_get_temp_directory () : ""; |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
153 } |
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 std::string |
2926 | 156 octave_env::get_program_name (void) |
157 { | |
158 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
159 ? instance->prog_name : ""; |
2926 | 160 } |
161 | |
3504 | 162 std::string |
2926 | 163 octave_env::get_program_invocation_name (void) |
164 { | |
165 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
166 ? instance->prog_invocation_name : ""; |
2926 | 167 } |
168 | |
169 void | |
3504 | 170 octave_env::set_program_name (const std::string& s) |
2926 | 171 { |
172 if (instance_ok ()) | |
173 instance->do_set_program_name (s); | |
174 } | |
175 | |
3504 | 176 std::string |
2926 | 177 octave_env::get_user_name (void) |
178 { | |
179 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
180 ? instance->do_get_user_name () : ""; |
2926 | 181 } |
182 | |
3504 | 183 std::string |
2926 | 184 octave_env::get_host_name (void) |
185 { | |
186 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
187 ? instance->do_get_host_name () : ""; |
2926 | 188 } |
189 | |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
190 std::string |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
191 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
|
192 { |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
193 std::string tempd; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
194 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
195 #if defined (__MINGW32__) || defined (_MSC_VER) |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19459
diff
changeset
|
196 |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
197 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
|
198 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
199 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
200 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
|
201 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
202 #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
|
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 = P_tmpdir; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
205 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
206 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
207 // 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
|
208 // 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
|
209 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
|
210 tempd = "c:\\temp"; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
211 |
20945
9d9270e2f98f
eliminate comments after preprocessor conditionals
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
212 #else |
19458
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
213 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
214 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
|
215 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
216 #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
|
217 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
218 tempd = P_tmpdir; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
219 #else |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
220 if (tempd.empty ()) |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
221 tempd = "/tmp"; |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
222 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
223 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
224 #endif |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
225 |
d8fd3842a507
Use gnulib gen_tempname to create temporary names (Bug #43872).
John Donoghue <john.donoghue@ieee.org>
parents:
17769
diff
changeset
|
226 return tempd; |
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 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
229 // FIXME: this leaves no way to distinguish between a |
2926 | 230 // variable that is not set and one that is set to the empty string. |
231 // Is this a problem? | |
232 | |
3504 | 233 std::string |
234 octave_env::getenv (const std::string& name) | |
2926 | 235 { |
236 return (instance_ok ()) | |
21017
93748bcaec17
maint: Replace emtpy 'std::string ()' calls with "".
Rik <rik@octave.org>
parents:
20945
diff
changeset
|
237 ? instance->do_getenv (name) : ""; |
2926 | 238 } |
239 | |
240 void | |
3504 | 241 octave_env::putenv (const std::string& name, const std::string& value) |
2926 | 242 { |
243 octave_putenv (name, value); | |
244 } | |
245 | |
246 bool | |
5489 | 247 octave_env::have_x11_display (void) |
248 { | |
249 std::string display = getenv ("DISPLAY"); | |
250 | |
251 return ! display.empty (); | |
252 } | |
253 | |
254 bool | |
3504 | 255 octave_env::chdir (const std::string& newdir) |
2926 | 256 { |
257 return (instance_ok ()) | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
258 ? instance->do_chdir (newdir) : false; |
2926 | 259 } |
260 | |
261 void | |
3504 | 262 octave_env::do_set_program_name (const std::string& s) const |
2926 | 263 { |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
264 // For gnulib. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
265 ::set_program_name (s.c_str ()); |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
266 |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
267 // 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
|
268 prog_invocation_name = program_name; |
2926 | 269 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
270 size_t pos |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
271 = prog_invocation_name.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 272 |
10278
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
273 // Also keep a shortened version of the program name. |
4a278982c0fe
use gnulib progname module
John W. Eaton <jwe@octave.org>
parents:
10250
diff
changeset
|
274 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
|
275 ? prog_invocation_name : prog_invocation_name.substr (pos+1); |
2926 | 276 } |
277 | |
278 // 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
|
279 // same as $HOME, then replace that with '~'. |
2926 | 280 |
3504 | 281 std::string |
282 octave_env::do_polite_directory_format (const std::string& name) const | |
2926 | 283 { |
3504 | 284 std::string retval; |
2926 | 285 |
3504 | 286 std::string home_dir = do_get_home_directory (); |
2926 | 287 |
288 size_t len = home_dir.length (); | |
289 | |
3516 | 290 if (len > 1 && home_dir == name.substr (0, len) |
4097 | 291 && (name.length () == len || file_ops::is_dir_sep (name[len]))) |
2926 | 292 { |
293 retval = "~"; | |
294 retval.append (name.substr (len)); | |
295 } | |
296 else | |
297 retval = name; | |
298 | |
299 return retval; | |
300 } | |
301 | |
302 bool | |
3504 | 303 octave_env::do_absolute_pathname (const std::string& s) const |
2926 | 304 { |
4087 | 305 size_t len = s.length (); |
306 | |
307 if (len == 0) | |
308 return false; | |
2926 | 309 |
4097 | 310 if (file_ops::is_dir_sep (s[0])) |
2926 | 311 return true; |
312 | |
4101 | 313 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) |
4088 | 314 if ((len == 2 && isalpha (s[0]) && s[1] == ':') |
4097 | 315 || (len > 2 && isalpha (s[0]) && s[1] == ':' |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
316 && file_ops::is_dir_sep (s[2]))) |
4087 | 317 return true; |
318 #endif | |
319 | |
2926 | 320 return false; |
321 } | |
322 | |
6838 | 323 bool |
324 octave_env::do_rooted_relative_pathname (const std::string& s) const | |
325 { | |
326 size_t len = s.length (); | |
327 | |
328 if (len == 0) | |
329 return false; | |
330 | |
331 if (len == 1 && s[0] == '.') | |
332 return true; | |
333 | |
334 if (len > 1 && s[0] == '.' && file_ops::is_dir_sep (s[1])) | |
335 return true; | |
336 | |
337 if (len == 2 && s[0] == '.' && s[1] == '.') | |
338 return true; | |
339 | |
340 if (len > 2 && s[0] == '.' && s[1] == '.' && file_ops::is_dir_sep (s[2])) | |
341 return true; | |
342 | |
343 return false; | |
344 } | |
345 | |
15466
d174210ce1ec
use ' instead of ` in error messages, warnings and most comments
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
346 // Return the 'basename' of the pathname in STRING (the stuff after |
4097 | 347 // the last directory separator). If STRING is not a full pathname, |
348 // simply return it. | |
2926 | 349 |
3504 | 350 std::string |
351 octave_env::do_base_pathname (const std::string& s) const | |
2926 | 352 { |
7609
7e6002d15d4d
octave_env::do_base_pathname: handle rooted relativel names
John W. Eaton <jwe@octave.org>
parents:
7048
diff
changeset
|
353 if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s))) |
2926 | 354 return s; |
355 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
356 size_t pos = s.find_last_of (file_ops::dir_sep_chars ()); |
2926 | 357 |
8021 | 358 if (pos == std::string::npos) |
2926 | 359 return s; |
360 else | |
361 return s.substr (pos+1); | |
362 } | |
363 | |
364 // Turn STRING (a pathname) into an absolute pathname, assuming that | |
4097 | 365 // DOT_PATH contains the symbolic location of the current directory. |
2926 | 366 |
3504 | 367 std::string |
368 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
|
369 const std::string& dot_path) const |
2926 | 370 { |
4087 | 371 if (dot_path.empty () || s.empty () || do_absolute_pathname (s)) |
2926 | 372 return s; |
373 | |
4097 | 374 std::string current_dir = dot_path; |
2926 | 375 |
4097 | 376 if (current_dir.empty ()) |
377 current_dir = do_getcwd (); | |
2926 | 378 |
4097 | 379 size_t pos = current_dir.length () - 1; |
2926 | 380 |
4097 | 381 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
|
382 current_dir.append (file_ops::dir_sep_str ()); |
4097 | 383 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
384 // FIXME: this is probably not correct for all systems. |
2926 | 385 |
386 size_t i = 0; | |
387 size_t slen = s.length (); | |
388 | |
389 while (i < slen) | |
390 { | |
391 if (s[i] == '.') | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
392 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
393 if (i + 1 == slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
394 return current_dir; |
2926 | 395 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
396 if (file_ops::is_dir_sep (s[i+1])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
397 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
398 i += 2; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
399 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
400 } |
2926 | 401 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
402 if (s[i+1] == '.' |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
403 && (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
|
404 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
405 i += 2; |
2926 | 406 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
407 if (i != slen) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
408 i++; |
2926 | 409 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
410 pathname_backup (current_dir, 1); |
2926 | 411 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
412 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
413 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
414 } |
2926 | 415 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
416 size_t tmp = s.find_first_of (file_ops::dir_sep_chars (), i); |
2926 | 417 |
8021 | 418 if (tmp == std::string::npos) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
419 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
420 current_dir.append (s, i, tmp-i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
421 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
422 } |
2926 | 423 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
424 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
425 current_dir.append (s, i, tmp-i+1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
426 i = tmp + 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
427 } |
2926 | 428 } |
429 | |
4097 | 430 return current_dir; |
2926 | 431 } |
432 | |
4097 | 433 // Return a string which is the current working directory. |
2926 | 434 |
3504 | 435 std::string |
4097 | 436 octave_env::do_getcwd () const |
2926 | 437 { |
438 if (! follow_symbolic_links) | |
439 current_directory = ""; | |
440 | |
441 if (verbatim_pwd || current_directory.empty ()) | |
442 current_directory = ::octave_getcwd (); | |
443 | |
444 return current_directory; | |
445 } | |
446 | |
447 // This value is not cached because it can change while Octave is | |
448 // running. | |
449 | |
3504 | 450 std::string |
2926 | 451 octave_env::do_get_home_directory (void) const |
452 { | |
3504 | 453 std::string hd = do_getenv ("HOME"); |
2926 | 454 |
6096 | 455 #if defined (__MINGW32__) || defined (_MSC_VER) |
456 // Maybe we are started directly from cmd.exe. | |
5451 | 457 if (hd.empty ()) |
5454 | 458 { |
459 std::string drv = do_getenv ("HOMEDRIVE"); | |
460 if (drv.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
461 hd = do_getenv ("HOMEPATH"); |
5454 | 462 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
463 hd = drv + do_getenv ("HOMEPATH"); |
5454 | 464 } |
5451 | 465 #endif |
466 | |
2947 | 467 if (hd.empty ()) |
468 { | |
469 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); | |
470 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
8006
diff
changeset
|
471 hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str ()); |
2947 | 472 } |
473 | |
474 return hd; | |
2926 | 475 } |
476 | |
3504 | 477 std::string |
2926 | 478 octave_env::do_get_user_name (void) const |
479 { | |
480 if (user_name.empty ()) | |
481 { | |
2947 | 482 octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ()); |
2926 | 483 |
3504 | 484 user_name = pw ? pw.name () : std::string ("unknown"); |
2926 | 485 } |
486 | |
487 return user_name; | |
488 } | |
489 | |
3504 | 490 std::string |
2926 | 491 octave_env::do_get_host_name (void) const |
492 { | |
493 if (host_name.empty ()) | |
494 { | |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
495 char hostname[1024]; |
2926 | 496 |
11006
aca961a3f387
provide gethostname function
John W. Eaton <jwe@octave.org>
parents:
10463
diff
changeset
|
497 int status = gnulib::gethostname (hostname, 1023); |
2926 | 498 |
3185 | 499 host_name = (status < 0) ? "unknown" : hostname; |
2926 | 500 } |
501 | |
502 return host_name; | |
503 } | |
504 | |
3504 | 505 std::string |
506 octave_env::do_getenv (const std::string& name) const | |
2926 | 507 { |
508 char *value = ::getenv (name.c_str ()); | |
509 | |
510 return value ? value : ""; | |
511 } | |
512 | |
513 // Do the work of changing to the directory NEWDIR. Handle symbolic | |
514 // link following, etc. | |
515 | |
516 bool | |
3504 | 517 octave_env::do_chdir (const std::string& newdir) |
2926 | 518 { |
519 bool retval = false; | |
520 | |
3504 | 521 std::string tmp; |
2926 | 522 |
523 if (follow_symbolic_links) | |
524 { | |
525 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
526 do_getcwd (); |
2926 | 527 |
528 if (current_directory.empty ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
529 tmp = newdir; |
2926 | 530 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
531 tmp = do_make_absolute (newdir, current_directory); |
2926 | 532 |
4097 | 533 // Get rid of trailing directory separator. |
2926 | 534 |
535 size_t len = tmp.length (); | |
536 | |
537 if (len > 1) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
538 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
539 if (file_ops::is_dir_sep (tmp[--len])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
540 tmp.resize (len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
541 } |
2926 | 542 |
543 if (! ::octave_chdir (tmp)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
544 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
545 current_directory = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
546 retval = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10278
diff
changeset
|
547 } |
2926 | 548 } |
549 else | |
550 retval = (! ::octave_chdir (newdir)); | |
551 | |
552 return retval; | |
553 } | |
554 | |
555 // Remove the last N directories from PATH. | |
556 | |
557 void | |
3504 | 558 octave_env::pathname_backup (std::string& path, int n) const |
2926 | 559 { |
560 if (path.empty ()) | |
561 return; | |
562 | |
563 size_t i = path.length () - 1; | |
564 | |
565 while (n--) | |
566 { | |
4097 | 567 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
|
568 i--; |
2926 | 569 |
4097 | 570 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
|
571 i--; |
2926 | 572 |
573 i++; | |
574 } | |
575 | |
576 path.resize (i); | |
577 } | |
578 | |
579 void | |
580 octave_env::error (int err_num) const | |
581 { | |
10411 | 582 (*current_liboctave_error_handler) ("%s", gnulib::strerror (err_num)); |
2926 | 583 } |
584 | |
585 void | |
3504 | 586 octave_env::error (const std::string& s) const |
2926 | 587 { |
588 (*current_liboctave_error_handler) ("%s", s.c_str ()); | |
589 } |