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