Mercurial > octave
annotate libinterp/corefcn/environment.cc @ 25440:893344cee100
rename defaults source files to installation-data
* installation-data.h, installation-data.cc: New files, adapted from
defaults.h and defaults.cc. Include installation-data.h instead of
defaults.h, but only in files that require it.
* defaults.h: Preserve for backward compatibility. Define deprecated
functions as inline.
* libinterp/corefcn/module.mk: Update.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 08 Jun 2018 15:43:36 -0400 |
parents | 69fc8935020b |
children | 627d6bde9b8d |
rev | line source |
---|---|
23719 | 1 /* |
2 | |
25054
6652d3823428
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
3 Copyright (C) 2017-2018 John W. Eaton |
23719 | 4 |
5 This file is part of Octave. | |
6 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23719
diff
changeset
|
7 Octave is free software: you can redistribute it and/or modify it |
23719 | 8 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23719
diff
changeset
|
9 the Free Software Foundation, either version 3 of the License, or |
23719 | 10 (at your option) any later version. |
11 | |
12 Octave is distributed in the hope that it will be useful, but | |
13 WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 GNU General Public License for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23719
diff
changeset
|
19 <https://www.gnu.org/licenses/>. |
23719 | 20 |
21 */ | |
22 | |
23 #if defined (HAVE_CONFIG_H) | |
24 # include "config.h" | |
25 #endif | |
26 | |
27 #include <string> | |
28 | |
29 #include "dir-ops.h" | |
30 #include "oct-env.h" | |
31 #include "file-stat.h" | |
32 #include "pathsearch.h" | |
33 #include "str-vec.h" | |
34 | |
35 #include "defun.h" | |
36 #include "environment.h" | |
25440
893344cee100
rename defaults source files to installation-data
John W. Eaton <jwe@octave.org>
parents:
25439
diff
changeset
|
37 #include "installation-data.h" |
23719 | 38 #include "interpreter.h" |
39 #include "variables.h" | |
40 | |
41 static void append_to_shell_path (const std::string& exec_path) | |
42 { | |
43 // FIXME: should there be a way to remove a previous setting from | |
44 // PATH? | |
45 | |
46 if (exec_path.empty ()) | |
47 return; | |
48 | |
49 // FIXME: should we really be modifying PATH in the environment? | |
50 | |
51 std::string shell_path = octave::sys::env::getenv ("PATH"); | |
52 | |
53 if (shell_path.empty ()) | |
54 octave::sys::env::putenv ("PATH", exec_path); | |
55 else | |
56 { | |
57 // If PATH doesn't already have exec_path, append it. | |
58 // FIXME: should we search for the elements individually, and | |
59 // only append those that are missing? | |
60 | |
25103
078b795c5219
maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
61 std::string path_sep = octave::directory_path::path_sep_str (); |
23719 | 62 |
25103
078b795c5219
maint: style check C++ ahead of 4.4 release.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
63 if (shell_path.find (exec_path) == std::string::npos) |
23719 | 64 octave::sys::env::putenv ("PATH", shell_path + path_sep + exec_path); |
65 } | |
66 } | |
67 | |
68 namespace octave | |
69 { | |
70 octave_value | |
71 environment::editor (const octave_value_list& args, int nargout) | |
72 { | |
73 return set_internal_variable (m_editor, args, nargout, "EDITOR", false); | |
74 } | |
75 | |
76 | |
77 octave_value | |
78 environment::exec_path (const octave_value_list& args, int nargout) | |
79 { | |
80 octave_value retval | |
81 = set_internal_variable (m_exec_path, args, nargout, "EXEC_PATH", false); | |
82 | |
83 append_to_shell_path (m_exec_path); | |
84 | |
85 return retval; | |
86 } | |
87 | |
88 std::string environment::exec_path (const std::string& path) | |
89 { | |
90 std::string old_val = set (m_exec_path, path); | |
91 | |
92 append_to_shell_path (m_exec_path); | |
93 | |
94 return old_val; | |
95 } | |
96 | |
97 octave_value | |
98 environment::image_path (const octave_value_list& args, int nargout) | |
99 { | |
100 return set_internal_variable (m_image_path, args, nargout, "IMAGE_PATH", | |
101 false); | |
102 } | |
103 | |
104 std::string environment::init_editor (void) | |
105 { | |
106 std::string retval = "emacs"; | |
107 | |
25336
389757b7b6af
eliminate redundant octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents:
25103
diff
changeset
|
108 std::string env_editor = sys::env::getenv ("EDITOR"); |
23719 | 109 |
110 if (! env_editor.empty ()) | |
111 retval = env_editor; | |
112 | |
113 return retval; | |
114 } | |
115 | |
25439
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
116 std::string environment::init_exec_path (interpreter& interp) |
23719 | 117 { |
25336
389757b7b6af
eliminate redundant octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents:
25103
diff
changeset
|
118 std::string exec_path = sys::env::getenv ("OCTAVE_EXEC_PATH"); |
23719 | 119 |
25336
389757b7b6af
eliminate redundant octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents:
25103
diff
changeset
|
120 std::string path_sep = directory_path::path_sep_str (); |
23719 | 121 |
122 if (exec_path.empty ()) | |
25439
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
123 { |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
124 installation_data& inst_data = interp.get_installation_data (); |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
125 |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
126 exec_path = (inst_data.local_ver_arch_lib_dir () + path_sep |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
127 + inst_data.local_api_arch_lib_dir () + path_sep |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
128 + inst_data.local_arch_lib_dir () + path_sep |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
129 + inst_data.arch_lib_dir () + path_sep |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
130 + inst_data.bin_dir ()); |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
131 |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
132 } |
23719 | 133 |
134 append_to_shell_path (exec_path); | |
135 | |
136 return exec_path; | |
137 } | |
138 | |
25439
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
139 std::string environment::init_image_path (interpreter& interp) |
23719 | 140 { |
141 std::string image_path = "."; | |
142 | |
25336
389757b7b6af
eliminate redundant octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents:
25103
diff
changeset
|
143 std::string path_sep = directory_path::path_sep_str (); |
23719 | 144 |
25336
389757b7b6af
eliminate redundant octave:: namespace tags
John W. Eaton <jwe@octave.org>
parents:
25103
diff
changeset
|
145 std::string env_path = sys::env::getenv ("OCTAVE_IMAGE_PATH"); |
23719 | 146 |
147 if (! env_path.empty ()) | |
148 image_path += path_sep + env_path; | |
149 | |
25439
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
150 installation_data& inst_data = interp.get_installation_data (); |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
151 |
69fc8935020b
move some static configuration variables inside a class
John W. Eaton <jwe@octave.org>
parents:
25336
diff
changeset
|
152 std::string gen_path = genpath (inst_data.image_dir (), ""); |
23719 | 153 |
154 if (! gen_path.empty ()) | |
155 image_path += path_sep + gen_path; | |
156 | |
157 return image_path; | |
158 } | |
159 } | |
160 | |
161 DEFMETHOD (EDITOR, interp, args, nargout, | |
162 doc: /* -*- texinfo -*- | |
163 @deftypefn {} {@var{val} =} EDITOR () | |
164 @deftypefnx {} {@var{old_val} =} EDITOR (@var{new_val}) | |
165 @deftypefnx {} {} EDITOR (@var{new_val}, "local") | |
166 Query or set the internal variable that specifies the default text editor. | |
167 | |
168 The default value is taken from the environment variable @w{@env{EDITOR}} | |
169 when Octave starts. If the environment variable is not initialized, | |
170 @w{@env{EDITOR}} will be set to @qcode{"emacs"}. | |
171 | |
172 When called from inside a function with the @qcode{"local"} option, the | |
173 variable is changed locally for the function and any subroutines it calls. | |
174 The original variable value is restored when exiting the function. | |
175 | |
176 @seealso{edit, edit_history} | |
177 @end deftypefn */) | |
178 { | |
179 octave::environment& env = interp.get_environment (); | |
180 | |
181 return env.editor (args, nargout); | |
182 } | |
183 | |
184 /* | |
185 %!test | |
186 %! orig_val = EDITOR (); | |
187 %! old_val = EDITOR ("X"); | |
188 %! assert (orig_val, old_val); | |
189 %! assert (EDITOR (), "X"); | |
190 %! EDITOR (orig_val); | |
191 %! assert (EDITOR (), orig_val); | |
192 | |
193 %!error (EDITOR (1, 2)) | |
194 */ | |
195 | |
196 DEFMETHOD (EXEC_PATH, interp, args, nargout, | |
197 doc: /* -*- texinfo -*- | |
198 @deftypefn {} {@var{val} =} EXEC_PATH () | |
199 @deftypefnx {} {@var{old_val} =} EXEC_PATH (@var{new_val}) | |
200 @deftypefnx {} {} EXEC_PATH (@var{new_val}, "local") | |
201 Query or set the internal variable that specifies a colon separated | |
202 list of directories to append to the shell PATH when executing external | |
203 programs. | |
204 | |
205 The initial value of is taken from the environment variable | |
206 @w{@env{OCTAVE_EXEC_PATH}}, but that value can be overridden by the command | |
207 line argument @option{--exec-path PATH}. | |
208 | |
209 When called from inside a function with the @qcode{"local"} option, the | |
210 variable is changed locally for the function and any subroutines it calls. | |
211 The original variable value is restored when exiting the function. | |
212 | |
213 @seealso{IMAGE_PATH, OCTAVE_HOME, OCTAVE_EXEC_HOME} | |
214 @end deftypefn */) | |
215 { | |
216 octave::environment& env = interp.get_environment (); | |
217 | |
218 return env.exec_path (args, nargout); | |
219 } | |
220 | |
221 /* | |
222 %!test | |
223 %! orig_val = EXEC_PATH (); | |
224 %! old_val = EXEC_PATH ("X"); | |
225 %! assert (orig_val, old_val); | |
226 %! assert (EXEC_PATH (), "X"); | |
227 %! EXEC_PATH (orig_val); | |
228 %! assert (EXEC_PATH (), orig_val); | |
229 | |
230 %!error (EXEC_PATH (1, 2)) | |
231 */ | |
232 | |
233 DEFMETHOD (IMAGE_PATH, interp, args, nargout, | |
234 doc: /* -*- texinfo -*- | |
235 @deftypefn {} {@var{val} =} IMAGE_PATH () | |
236 @deftypefnx {} {@var{old_val} =} IMAGE_PATH (@var{new_val}) | |
237 @deftypefnx {} {} IMAGE_PATH (@var{new_val}, "local") | |
238 Query or set the internal variable that specifies a colon separated | |
239 list of directories in which to search for image files. | |
240 | |
241 When called from inside a function with the @qcode{"local"} option, the | |
242 variable is changed locally for the function and any subroutines it calls. | |
243 The original variable value is restored when exiting the function. | |
244 | |
245 @seealso{EXEC_PATH, OCTAVE_HOME, OCTAVE_EXEC_HOME} | |
246 @end deftypefn */) | |
247 { | |
248 octave::environment& env = interp.get_environment (); | |
249 | |
250 return env.image_path (args, nargout); | |
251 } | |
252 | |
253 /* | |
254 %!test | |
255 %! orig_val = IMAGE_PATH (); | |
256 %! old_val = IMAGE_PATH ("X"); | |
257 %! assert (orig_val, old_val); | |
258 %! assert (IMAGE_PATH (), "X"); | |
259 %! IMAGE_PATH (orig_val); | |
260 %! assert (IMAGE_PATH (), orig_val); | |
261 | |
262 %!error (IMAGE_PATH (1, 2)) | |
263 */ |