2203
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2203
|
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. |
2203
|
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/>. |
2203
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cstdlib> |
|
28 |
6114
|
29 #include <algorithm> |
3503
|
30 #include <iostream> |
2203
|
31 #include <string> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
2442
|
34 #ifdef HAVE_SYS_TYPES_H |
2203
|
35 #include <sys/types.h> |
2442
|
36 #endif |
2203
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
5814
|
40 #include "dir-ops.h" |
2926
|
41 #include "oct-env.h" |
4217
|
42 #include "file-stat.h" |
3174
|
43 #include "pathsearch.h" |
4217
|
44 #include "str-vec.h" |
2926
|
45 |
2492
|
46 #include <defaults.h> |
2203
|
47 #include "defun.h" |
|
48 #include "error.h" |
3040
|
49 #include "file-ops.h" |
2203
|
50 #include "gripes.h" |
|
51 #include "help.h" |
5395
|
52 #include "input.h" |
5832
|
53 #include "load-path.h" |
3185
|
54 #include "oct-obj.h" |
2390
|
55 #include "ov.h" |
4217
|
56 #include "parse.h" |
2203
|
57 #include "toplev.h" |
4217
|
58 #include "unwind-prot.h" |
2203
|
59 #include "variables.h" |
2492
|
60 #include <version.h> |
2203
|
61 |
3523
|
62 std::string Voctave_home; |
2203
|
63 |
3523
|
64 std::string Vbin_dir; |
|
65 std::string Vinfo_dir; |
|
66 std::string Vdata_dir; |
|
67 std::string Vlibexec_dir; |
|
68 std::string Varch_lib_dir; |
|
69 std::string Vlocal_arch_lib_dir; |
5909
|
70 std::string Vlocal_api_arch_lib_dir; |
3597
|
71 std::string Vlocal_ver_arch_lib_dir; |
5814
|
72 |
|
73 std::string Vlocal_ver_oct_file_dir; |
|
74 std::string Vlocal_api_oct_file_dir; |
|
75 std::string Vlocal_oct_file_dir; |
2203
|
76 |
5814
|
77 std::string Vlocal_ver_fcn_file_dir; |
|
78 std::string Vlocal_api_fcn_file_dir; |
|
79 std::string Vlocal_fcn_file_dir; |
|
80 |
|
81 std::string Voct_file_dir; |
|
82 std::string Vfcn_file_dir; |
|
83 |
|
84 std::string Vimage_dir; |
4447
|
85 |
2203
|
86 // The path that will be searched for programs that we execute. |
|
87 // (--exec-path path) |
5794
|
88 static std::string VEXEC_PATH; |
2203
|
89 |
|
90 // Name of the editor to be invoked by the edit_history command. |
5794
|
91 std::string VEDITOR; |
2203
|
92 |
5814
|
93 static std::string VIMAGE_PATH; |
2203
|
94 |
3523
|
95 std::string Vlocal_site_defaults_file; |
|
96 std::string Vsite_defaults_file; |
2203
|
97 |
6274
|
98 std::string |
3523
|
99 subst_octave_home (const std::string& s) |
2203
|
100 { |
3523
|
101 std::string retval; |
2203
|
102 |
3523
|
103 std::string prefix = OCTAVE_PREFIX; |
2203
|
104 |
|
105 retval = s; |
|
106 |
|
107 if (Voctave_home != prefix) |
|
108 { |
5275
|
109 octave_idx_type len = prefix.length (); |
6276
|
110 |
|
111 if (s.substr (0, len) == prefix) |
|
112 retval.replace (0, len, Voctave_home); |
2203
|
113 } |
|
114 |
6114
|
115 if (file_ops::dir_sep_char != '/') |
|
116 std::replace (retval.begin (), retval.end (), '/', file_ops::dir_sep_char); |
|
117 |
2203
|
118 return retval; |
|
119 } |
|
120 |
|
121 static void |
|
122 set_octave_home (void) |
|
123 { |
3523
|
124 std::string oh = octave_env::getenv ("OCTAVE_HOME"); |
2203
|
125 |
3523
|
126 Voctave_home = oh.empty () ? std::string (OCTAVE_PREFIX) : oh; |
2203
|
127 } |
|
128 |
|
129 static void |
|
130 set_default_info_dir (void) |
|
131 { |
|
132 Vinfo_dir = subst_octave_home (OCTAVE_INFODIR); |
|
133 } |
|
134 |
|
135 static void |
3141
|
136 set_default_data_dir (void) |
|
137 { |
|
138 Vdata_dir = subst_octave_home (OCTAVE_DATADIR); |
|
139 } |
|
140 |
|
141 static void |
|
142 set_default_libexec_dir (void) |
|
143 { |
|
144 Vlibexec_dir = subst_octave_home (OCTAVE_LIBEXECDIR); |
|
145 } |
|
146 |
|
147 static void |
2203
|
148 set_default_arch_lib_dir (void) |
|
149 { |
|
150 Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR); |
|
151 } |
|
152 |
|
153 static void |
2439
|
154 set_default_local_arch_lib_dir (void) |
|
155 { |
|
156 Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR); |
|
157 } |
|
158 |
|
159 static void |
5909
|
160 set_default_local_api_arch_lib_dir (void) |
|
161 { |
|
162 Vlocal_api_arch_lib_dir = subst_octave_home (OCTAVE_LOCALAPIARCHLIBDIR); |
|
163 } |
|
164 |
|
165 static void |
3597
|
166 set_default_local_ver_arch_lib_dir (void) |
|
167 { |
|
168 Vlocal_ver_arch_lib_dir = subst_octave_home (OCTAVE_LOCALVERARCHLIBDIR); |
|
169 } |
|
170 |
|
171 static void |
5814
|
172 set_default_local_ver_oct_file_dir (void) |
|
173 { |
|
174 Vlocal_ver_oct_file_dir = subst_octave_home (OCTAVE_LOCALVEROCTFILEDIR); |
|
175 } |
|
176 |
|
177 static void |
|
178 set_default_local_api_oct_file_dir (void) |
|
179 { |
|
180 Vlocal_api_oct_file_dir = subst_octave_home (OCTAVE_LOCALAPIOCTFILEDIR); |
|
181 } |
|
182 |
|
183 static void |
|
184 set_default_local_oct_file_dir (void) |
|
185 { |
|
186 Vlocal_oct_file_dir = subst_octave_home (OCTAVE_LOCALOCTFILEDIR); |
|
187 } |
|
188 |
|
189 static void |
|
190 set_default_local_ver_fcn_file_dir (void) |
|
191 { |
|
192 Vlocal_ver_fcn_file_dir = subst_octave_home (OCTAVE_LOCALVERFCNFILEDIR); |
|
193 } |
|
194 |
|
195 static void |
|
196 set_default_local_api_fcn_file_dir (void) |
|
197 { |
|
198 Vlocal_api_fcn_file_dir = subst_octave_home (OCTAVE_LOCALAPIFCNFILEDIR); |
|
199 } |
|
200 |
|
201 static void |
|
202 set_default_local_fcn_file_dir (void) |
|
203 { |
|
204 Vlocal_fcn_file_dir = subst_octave_home (OCTAVE_LOCALFCNFILEDIR); |
|
205 } |
|
206 |
|
207 static void |
2203
|
208 set_default_fcn_file_dir (void) |
|
209 { |
|
210 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR); |
|
211 } |
|
212 |
|
213 static void |
5814
|
214 set_default_image_dir (void) |
|
215 { |
|
216 Vimage_dir = subst_octave_home (OCTAVE_IMAGEDIR); |
|
217 } |
|
218 |
|
219 static void |
5397
|
220 set_default_oct_file_dir (void) |
|
221 { |
|
222 Voct_file_dir = subst_octave_home (OCTAVE_OCTFILEDIR); |
|
223 } |
|
224 |
|
225 static void |
2203
|
226 set_default_bin_dir (void) |
|
227 { |
|
228 Vbin_dir = subst_octave_home (OCTAVE_BINDIR); |
|
229 } |
|
230 |
5814
|
231 void |
|
232 set_exec_path (const std::string& path) |
4447
|
233 { |
5814
|
234 VEXEC_PATH = Vlocal_ver_arch_lib_dir + dir_path::path_sep_str |
5909
|
235 + Vlocal_api_arch_lib_dir + dir_path::path_sep_str |
5777
|
236 + Vlocal_arch_lib_dir + dir_path::path_sep_str |
|
237 + Varch_lib_dir + dir_path::path_sep_str |
4447
|
238 + Vbin_dir; |
5814
|
239 |
|
240 // This is static so that even if set_exec_path is called more than |
|
241 // once, shell_path is the original PATH from the environment, |
|
242 // before we start modifying it. |
|
243 static std::string shell_path = octave_env::getenv ("PATH"); |
|
244 |
|
245 if (! shell_path.empty ()) |
|
246 VEXEC_PATH += dir_path::path_sep_str + shell_path; |
|
247 |
|
248 std::string tpath = path; |
|
249 |
|
250 if (tpath.empty ()) |
|
251 tpath = octave_env::getenv ("OCTAVE_EXEC_PATH"); |
|
252 |
|
253 if (! tpath.empty ()) |
|
254 VEXEC_PATH = tpath + dir_path::path_sep_str + VEXEC_PATH; |
|
255 |
|
256 octave_env::putenv ("PATH", VEXEC_PATH); |
|
257 } |
|
258 |
|
259 void |
|
260 set_image_path (const std::string& path) |
|
261 { |
|
262 VIMAGE_PATH = "."; |
|
263 |
|
264 std::string tpath = path; |
|
265 |
|
266 if (tpath.empty ()) |
|
267 tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH"); |
|
268 |
|
269 if (! tpath.empty ()) |
|
270 VIMAGE_PATH += dir_path::path_sep_str + tpath; |
|
271 |
5832
|
272 tpath = genpath (Vimage_dir, ""); |
|
273 |
|
274 if (! tpath.empty ()) |
|
275 VIMAGE_PATH += dir_path::path_sep_str + tpath; |
5814
|
276 } |
|
277 |
2203
|
278 static void |
|
279 set_default_info_file (void) |
|
280 { |
3523
|
281 std::string std_info_file = subst_octave_home (OCTAVE_INFOFILE); |
2512
|
282 |
3523
|
283 std::string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE"); |
2203
|
284 |
2926
|
285 Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file; |
2203
|
286 } |
|
287 |
|
288 static void |
|
289 set_default_info_prog (void) |
|
290 { |
3523
|
291 std::string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM"); |
2203
|
292 |
2926
|
293 if (oct_info_prog.empty ()) |
5794
|
294 Vinfo_program = "info"; |
2926
|
295 else |
5794
|
296 Vinfo_program = std::string (oct_info_prog); |
2203
|
297 } |
|
298 |
4773
|
299 static void |
2203
|
300 set_default_editor (void) |
|
301 { |
5794
|
302 VEDITOR = "emacs"; |
2203
|
303 |
3523
|
304 std::string env_editor = octave_env::getenv ("EDITOR"); |
2203
|
305 |
2926
|
306 if (! env_editor.empty ()) |
5794
|
307 VEDITOR = env_editor; |
2203
|
308 } |
|
309 |
|
310 static void |
|
311 set_local_site_defaults_file (void) |
|
312 { |
5781
|
313 std::string lsf = octave_env::getenv ("OCTAVE_LOCAL_SITE_INITFILE"); |
|
314 |
|
315 if (lsf.empty ()) |
|
316 { |
|
317 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); |
|
318 Vlocal_site_defaults_file.append ("/octaverc"); |
|
319 } |
|
320 else |
|
321 Vlocal_site_defaults_file = lsf; |
2203
|
322 } |
|
323 |
|
324 static void |
|
325 set_site_defaults_file (void) |
|
326 { |
5781
|
327 std::string sf = octave_env::getenv ("OCTAVE_SITE_INITFILE"); |
|
328 |
|
329 if (sf.empty ()) |
|
330 { |
|
331 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
332 Vsite_defaults_file.append ("/octaverc"); |
|
333 } |
|
334 else |
|
335 Vsite_defaults_file = sf; |
2203
|
336 } |
|
337 |
|
338 void |
|
339 install_defaults (void) |
|
340 { |
|
341 // OCTAVE_HOME must be set first! |
|
342 |
|
343 set_octave_home (); |
|
344 |
|
345 set_default_info_dir (); |
|
346 |
3141
|
347 set_default_data_dir (); |
|
348 |
|
349 set_default_libexec_dir (); |
|
350 |
2203
|
351 set_default_arch_lib_dir (); |
|
352 |
5909
|
353 set_default_local_ver_arch_lib_dir (); |
|
354 set_default_local_api_arch_lib_dir (); |
2439
|
355 set_default_local_arch_lib_dir (); |
|
356 |
5814
|
357 set_default_local_ver_oct_file_dir (); |
|
358 set_default_local_api_oct_file_dir (); |
|
359 set_default_local_oct_file_dir (); |
2203
|
360 |
5814
|
361 set_default_local_ver_fcn_file_dir (); |
|
362 set_default_local_api_fcn_file_dir (); |
|
363 set_default_local_fcn_file_dir (); |
|
364 |
|
365 set_default_fcn_file_dir (); |
5397
|
366 set_default_oct_file_dir (); |
|
367 |
5814
|
368 set_default_image_dir (); |
|
369 |
2203
|
370 set_default_bin_dir (); |
|
371 |
5814
|
372 set_exec_path (); |
4447
|
373 |
5814
|
374 set_image_path (); |
2203
|
375 |
|
376 set_default_info_file (); |
|
377 |
|
378 set_default_info_prog (); |
|
379 |
|
380 set_default_editor (); |
|
381 |
|
382 set_local_site_defaults_file (); |
|
383 |
|
384 set_site_defaults_file (); |
|
385 } |
|
386 |
5794
|
387 DEFUN (EDITOR, args, nargout, |
|
388 "-*- texinfo -*-\n\ |
|
389 @deftypefn {Built-in Function} {@var{val} =} EDITOR ()\n\ |
|
390 @deftypefnx {Built-in Function} {@var{old_val} =} EDITOR (@var{new_val})\n\ |
|
391 Query or set the internal variable that specifies the editor to\n\ |
|
392 use with the @code{edit_history} command. If the environment\n\ |
|
393 variable @code{EDITOR} is set when Octave starts, its\n\ |
|
394 value is used as the default. Otherwise, @code{EDITOR} is set to\n\ |
|
395 @code{\"emacs\"}.\n\ |
|
396 @seealso{edit_history}\n\ |
|
397 @end deftypefn") |
2203
|
398 { |
5794
|
399 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (EDITOR); |
2203
|
400 } |
|
401 |
5794
|
402 DEFUN (EXEC_PATH, args, nargout, |
|
403 "-*- texinfo -*-\n\ |
|
404 @deftypefn {Built-in Function} {@var{val} =} EXEC_PATH ()\n\ |
|
405 @deftypefnx {Built-in Function} {@var{old_val} =} EXEC_PATH (@var{new_val})\n\ |
|
406 Query or set the internal variable that specifies a colon separated\n\ |
|
407 list of directories to search when executing external programs.\n\ |
|
408 Its initial value is taken from the environment variable\n\ |
|
409 @code{OCTAVE_EXEC_PATH} (if it exists) or @code{PATH}, but that\n\ |
|
410 value can be overridden by the command line argument\n\ |
5814
|
411 @code{--exec-path PATH}. At startup, an additional set of\n\ |
|
412 directories (including the shell PATH) is appended to the path\n\ |
|
413 specified in the environment or on the command line. If you use\n\ |
|
414 the @code{EXEC_PATH} function to modify the path, you should take\n\ |
|
415 care to preserve these additional directories.\n\ |
5794
|
416 @end deftypefn") |
4217
|
417 { |
5794
|
418 std::string saved_exec_path = VEXEC_PATH; |
4217
|
419 |
5794
|
420 octave_value retval = SET_NONEMPTY_INTERNAL_STRING_VARIABLE (EXEC_PATH); |
4217
|
421 |
5794
|
422 if (VEXEC_PATH != saved_exec_path) |
5814
|
423 octave_env::putenv ("PATH", VEXEC_PATH); |
2203
|
424 |
5794
|
425 return retval; |
2203
|
426 } |
|
427 |
5814
|
428 DEFUN (IMAGE_PATH, args, nargout, |
5794
|
429 "-*- texinfo -*-\n\ |
5814
|
430 @deftypefn {Built-in Function} {@var{val} =} IMAGE_PATH ()\n\ |
|
431 @deftypefnx {Built-in Function} {@var{old_val} =} IMAGE_PATH (@var{new_val})\n\ |
5794
|
432 Query or set the internal variable that specifies a colon separated\n\ |
|
433 list of directories in which to search for image files.\n\ |
|
434 @end deftypefn") |
|
435 { |
5814
|
436 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (IMAGE_PATH); |
5794
|
437 } |
|
438 |
5780
|
439 DEFUN (OCTAVE_HOME, args, , |
|
440 "-*- texinfo -*-\n\ |
|
441 @deftypefn {Built-in Function} {} OCTAVE_HOME ()\n\ |
|
442 Return the name of the top-level Octave installation directory.\n\ |
|
443 @end deftypefn") |
|
444 { |
|
445 octave_value retval; |
2831
|
446 |
5780
|
447 if (args.length () == 0) |
|
448 retval = Voctave_home; |
|
449 else |
5823
|
450 print_usage (); |
5780
|
451 |
|
452 return retval; |
5749
|
453 } |
3446
|
454 |
5749
|
455 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, , |
|
456 "-*- texinfo -*-\n\ |
|
457 @deftypefn {Built-in Function} {} OCTAVE_VERSION ()\n\ |
|
458 Return the version number of Octave, as a string.\n\ |
|
459 @end deftypefn") |
|
460 { |
|
461 octave_value retval; |
|
462 |
|
463 int nargin = args.length (); |
|
464 |
|
465 if (nargin == 0) |
|
466 retval = OCTAVE_VERSION; |
|
467 else |
5823
|
468 print_usage (); |
5749
|
469 |
|
470 return retval; |
2203
|
471 } |
|
472 |
|
473 /* |
|
474 ;;; Local Variables: *** |
|
475 ;;; mode: C++ *** |
|
476 ;;; End: *** |
|
477 */ |