Mercurial > hg > octave-jordi
annotate src/defaults.cc @ 8874:bd1b1fe9c6e9 ss-3-1-53
bump version info for snapshot
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 25 Feb 2009 18:35:47 -0500 |
parents | eace5649a8b5 |
children | eb63fbe60fab |
rev | line source |
---|---|
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 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
116 if (file_ops::dir_sep_char () != '/') |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
117 std::replace (retval.begin (), retval.end (), '/', |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
118 file_ops::dir_sep_char ()); |
6114 | 119 |
2203 | 120 return retval; |
121 } | |
122 | |
123 static void | |
124 set_octave_home (void) | |
125 { | |
3523 | 126 std::string oh = octave_env::getenv ("OCTAVE_HOME"); |
2203 | 127 |
3523 | 128 Voctave_home = oh.empty () ? std::string (OCTAVE_PREFIX) : oh; |
2203 | 129 } |
130 | |
131 static void | |
132 set_default_info_dir (void) | |
133 { | |
134 Vinfo_dir = subst_octave_home (OCTAVE_INFODIR); | |
135 } | |
136 | |
137 static void | |
3141 | 138 set_default_data_dir (void) |
139 { | |
140 Vdata_dir = subst_octave_home (OCTAVE_DATADIR); | |
141 } | |
142 | |
143 static void | |
144 set_default_libexec_dir (void) | |
145 { | |
146 Vlibexec_dir = subst_octave_home (OCTAVE_LIBEXECDIR); | |
147 } | |
148 | |
149 static void | |
2203 | 150 set_default_arch_lib_dir (void) |
151 { | |
152 Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR); | |
153 } | |
154 | |
155 static void | |
2439 | 156 set_default_local_arch_lib_dir (void) |
157 { | |
158 Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR); | |
159 } | |
160 | |
161 static void | |
5909 | 162 set_default_local_api_arch_lib_dir (void) |
163 { | |
164 Vlocal_api_arch_lib_dir = subst_octave_home (OCTAVE_LOCALAPIARCHLIBDIR); | |
165 } | |
166 | |
167 static void | |
3597 | 168 set_default_local_ver_arch_lib_dir (void) |
169 { | |
170 Vlocal_ver_arch_lib_dir = subst_octave_home (OCTAVE_LOCALVERARCHLIBDIR); | |
171 } | |
172 | |
173 static void | |
5814 | 174 set_default_local_ver_oct_file_dir (void) |
175 { | |
176 Vlocal_ver_oct_file_dir = subst_octave_home (OCTAVE_LOCALVEROCTFILEDIR); | |
177 } | |
178 | |
179 static void | |
180 set_default_local_api_oct_file_dir (void) | |
181 { | |
182 Vlocal_api_oct_file_dir = subst_octave_home (OCTAVE_LOCALAPIOCTFILEDIR); | |
183 } | |
184 | |
185 static void | |
186 set_default_local_oct_file_dir (void) | |
187 { | |
188 Vlocal_oct_file_dir = subst_octave_home (OCTAVE_LOCALOCTFILEDIR); | |
189 } | |
190 | |
191 static void | |
192 set_default_local_ver_fcn_file_dir (void) | |
193 { | |
194 Vlocal_ver_fcn_file_dir = subst_octave_home (OCTAVE_LOCALVERFCNFILEDIR); | |
195 } | |
196 | |
197 static void | |
198 set_default_local_api_fcn_file_dir (void) | |
199 { | |
200 Vlocal_api_fcn_file_dir = subst_octave_home (OCTAVE_LOCALAPIFCNFILEDIR); | |
201 } | |
202 | |
203 static void | |
204 set_default_local_fcn_file_dir (void) | |
205 { | |
206 Vlocal_fcn_file_dir = subst_octave_home (OCTAVE_LOCALFCNFILEDIR); | |
207 } | |
208 | |
209 static void | |
2203 | 210 set_default_fcn_file_dir (void) |
211 { | |
212 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR); | |
213 } | |
214 | |
215 static void | |
5814 | 216 set_default_image_dir (void) |
217 { | |
218 Vimage_dir = subst_octave_home (OCTAVE_IMAGEDIR); | |
219 } | |
220 | |
221 static void | |
5397 | 222 set_default_oct_file_dir (void) |
223 { | |
224 Voct_file_dir = subst_octave_home (OCTAVE_OCTFILEDIR); | |
225 } | |
226 | |
227 static void | |
2203 | 228 set_default_bin_dir (void) |
229 { | |
230 Vbin_dir = subst_octave_home (OCTAVE_BINDIR); | |
231 } | |
232 | |
5814 | 233 void |
234 set_exec_path (const std::string& path) | |
4447 | 235 { |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
236 VEXEC_PATH = Vlocal_ver_arch_lib_dir + dir_path::path_sep_str () |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
237 + Vlocal_api_arch_lib_dir + dir_path::path_sep_str () |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
238 + Vlocal_arch_lib_dir + dir_path::path_sep_str () |
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
239 + Varch_lib_dir + dir_path::path_sep_str () |
4447 | 240 + Vbin_dir; |
5814 | 241 |
242 // This is static so that even if set_exec_path is called more than | |
243 // once, shell_path is the original PATH from the environment, | |
244 // before we start modifying it. | |
245 static std::string shell_path = octave_env::getenv ("PATH"); | |
246 | |
247 if (! shell_path.empty ()) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
248 VEXEC_PATH += dir_path::path_sep_str () + shell_path; |
5814 | 249 |
250 std::string tpath = path; | |
251 | |
252 if (tpath.empty ()) | |
253 tpath = octave_env::getenv ("OCTAVE_EXEC_PATH"); | |
254 | |
255 if (! tpath.empty ()) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
256 VEXEC_PATH = tpath + dir_path::path_sep_str () + VEXEC_PATH; |
5814 | 257 |
258 octave_env::putenv ("PATH", VEXEC_PATH); | |
259 } | |
260 | |
261 void | |
262 set_image_path (const std::string& path) | |
263 { | |
264 VIMAGE_PATH = "."; | |
265 | |
266 std::string tpath = path; | |
267 | |
268 if (tpath.empty ()) | |
269 tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH"); | |
270 | |
271 if (! tpath.empty ()) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
272 VIMAGE_PATH += dir_path::path_sep_str () + tpath; |
5814 | 273 |
5832 | 274 tpath = genpath (Vimage_dir, ""); |
275 | |
276 if (! tpath.empty ()) | |
8008
4d13a7a2f6ab
dir_path: use singleton class for static data members
John W. Eaton <jwe@octave.org>
parents:
8007
diff
changeset
|
277 VIMAGE_PATH += dir_path::path_sep_str () + tpath; |
5814 | 278 } |
279 | |
2203 | 280 static void |
8865
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
281 set_default_doc_cache_file (void) |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
282 { |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
283 std::string def_file = subst_octave_home (OCTAVE_DOC_CACHE_FILE); |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
284 |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
285 std::string env_file = octave_env::getenv ("OCTAVE_DOC_CACHE_FILE"); |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
286 |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
287 Vdoc_cache_file = env_file.empty () ? def_file : env_file; |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
288 } |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
289 |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
290 static void |
2203 | 291 set_default_info_file (void) |
292 { | |
3523 | 293 std::string std_info_file = subst_octave_home (OCTAVE_INFOFILE); |
2512 | 294 |
3523 | 295 std::string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE"); |
2203 | 296 |
2926 | 297 Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file; |
2203 | 298 } |
299 | |
300 static void | |
301 set_default_info_prog (void) | |
302 { | |
3523 | 303 std::string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM"); |
2203 | 304 |
2926 | 305 if (oct_info_prog.empty ()) |
5794 | 306 Vinfo_program = "info"; |
2926 | 307 else |
5794 | 308 Vinfo_program = std::string (oct_info_prog); |
2203 | 309 } |
310 | |
4773 | 311 static void |
2203 | 312 set_default_editor (void) |
313 { | |
5794 | 314 VEDITOR = "emacs"; |
2203 | 315 |
3523 | 316 std::string env_editor = octave_env::getenv ("EDITOR"); |
2203 | 317 |
2926 | 318 if (! env_editor.empty ()) |
5794 | 319 VEDITOR = env_editor; |
2203 | 320 } |
321 | |
322 static void | |
323 set_local_site_defaults_file (void) | |
324 { | |
5781 | 325 std::string lsf = octave_env::getenv ("OCTAVE_LOCAL_SITE_INITFILE"); |
326 | |
327 if (lsf.empty ()) | |
328 { | |
329 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); | |
330 Vlocal_site_defaults_file.append ("/octaverc"); | |
331 } | |
332 else | |
333 Vlocal_site_defaults_file = lsf; | |
2203 | 334 } |
335 | |
336 static void | |
337 set_site_defaults_file (void) | |
338 { | |
5781 | 339 std::string sf = octave_env::getenv ("OCTAVE_SITE_INITFILE"); |
340 | |
341 if (sf.empty ()) | |
342 { | |
343 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR); | |
344 Vsite_defaults_file.append ("/octaverc"); | |
345 } | |
346 else | |
347 Vsite_defaults_file = sf; | |
2203 | 348 } |
349 | |
350 void | |
351 install_defaults (void) | |
352 { | |
353 // OCTAVE_HOME must be set first! | |
354 | |
355 set_octave_home (); | |
356 | |
357 set_default_info_dir (); | |
358 | |
3141 | 359 set_default_data_dir (); |
360 | |
361 set_default_libexec_dir (); | |
362 | |
2203 | 363 set_default_arch_lib_dir (); |
364 | |
5909 | 365 set_default_local_ver_arch_lib_dir (); |
366 set_default_local_api_arch_lib_dir (); | |
2439 | 367 set_default_local_arch_lib_dir (); |
368 | |
5814 | 369 set_default_local_ver_oct_file_dir (); |
370 set_default_local_api_oct_file_dir (); | |
371 set_default_local_oct_file_dir (); | |
2203 | 372 |
5814 | 373 set_default_local_ver_fcn_file_dir (); |
374 set_default_local_api_fcn_file_dir (); | |
375 set_default_local_fcn_file_dir (); | |
376 | |
377 set_default_fcn_file_dir (); | |
5397 | 378 set_default_oct_file_dir (); |
379 | |
5814 | 380 set_default_image_dir (); |
381 | |
2203 | 382 set_default_bin_dir (); |
383 | |
5814 | 384 set_exec_path (); |
4447 | 385 |
5814 | 386 set_image_path (); |
2203 | 387 |
8865
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
388 set_default_doc_cache_file (); |
eace5649a8b5
set default value for doc_cache_file variable
John W. Eaton <jwe@octave.org>
parents:
8008
diff
changeset
|
389 |
2203 | 390 set_default_info_file (); |
391 | |
392 set_default_info_prog (); | |
393 | |
394 set_default_editor (); | |
395 | |
396 set_local_site_defaults_file (); | |
397 | |
398 set_site_defaults_file (); | |
399 } | |
400 | |
5794 | 401 DEFUN (EDITOR, args, nargout, |
402 "-*- texinfo -*-\n\ | |
403 @deftypefn {Built-in Function} {@var{val} =} EDITOR ()\n\ | |
404 @deftypefnx {Built-in Function} {@var{old_val} =} EDITOR (@var{new_val})\n\ | |
405 Query or set the internal variable that specifies the editor to\n\ | |
406 use with the @code{edit_history} command. If the environment\n\ | |
407 variable @code{EDITOR} is set when Octave starts, its\n\ | |
408 value is used as the default. Otherwise, @code{EDITOR} is set to\n\ | |
409 @code{\"emacs\"}.\n\ | |
410 @seealso{edit_history}\n\ | |
411 @end deftypefn") | |
2203 | 412 { |
5794 | 413 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (EDITOR); |
2203 | 414 } |
415 | |
5794 | 416 DEFUN (EXEC_PATH, args, nargout, |
417 "-*- texinfo -*-\n\ | |
418 @deftypefn {Built-in Function} {@var{val} =} EXEC_PATH ()\n\ | |
419 @deftypefnx {Built-in Function} {@var{old_val} =} EXEC_PATH (@var{new_val})\n\ | |
420 Query or set the internal variable that specifies a colon separated\n\ | |
421 list of directories to search when executing external programs.\n\ | |
422 Its initial value is taken from the environment variable\n\ | |
423 @code{OCTAVE_EXEC_PATH} (if it exists) or @code{PATH}, but that\n\ | |
424 value can be overridden by the command line argument\n\ | |
5814 | 425 @code{--exec-path PATH}. At startup, an additional set of\n\ |
426 directories (including the shell PATH) is appended to the path\n\ | |
427 specified in the environment or on the command line. If you use\n\ | |
428 the @code{EXEC_PATH} function to modify the path, you should take\n\ | |
429 care to preserve these additional directories.\n\ | |
5794 | 430 @end deftypefn") |
4217 | 431 { |
5794 | 432 std::string saved_exec_path = VEXEC_PATH; |
4217 | 433 |
5794 | 434 octave_value retval = SET_NONEMPTY_INTERNAL_STRING_VARIABLE (EXEC_PATH); |
4217 | 435 |
5794 | 436 if (VEXEC_PATH != saved_exec_path) |
5814 | 437 octave_env::putenv ("PATH", VEXEC_PATH); |
2203 | 438 |
5794 | 439 return retval; |
2203 | 440 } |
441 | |
5814 | 442 DEFUN (IMAGE_PATH, args, nargout, |
5794 | 443 "-*- texinfo -*-\n\ |
5814 | 444 @deftypefn {Built-in Function} {@var{val} =} IMAGE_PATH ()\n\ |
445 @deftypefnx {Built-in Function} {@var{old_val} =} IMAGE_PATH (@var{new_val})\n\ | |
5794 | 446 Query or set the internal variable that specifies a colon separated\n\ |
447 list of directories in which to search for image files.\n\ | |
448 @end deftypefn") | |
449 { | |
5814 | 450 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (IMAGE_PATH); |
5794 | 451 } |
452 | |
5780 | 453 DEFUN (OCTAVE_HOME, args, , |
454 "-*- texinfo -*-\n\ | |
455 @deftypefn {Built-in Function} {} OCTAVE_HOME ()\n\ | |
456 Return the name of the top-level Octave installation directory.\n\ | |
457 @end deftypefn") | |
458 { | |
459 octave_value retval; | |
2831 | 460 |
5780 | 461 if (args.length () == 0) |
462 retval = Voctave_home; | |
463 else | |
5823 | 464 print_usage (); |
5780 | 465 |
466 return retval; | |
5749 | 467 } |
3446 | 468 |
5749 | 469 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, , |
470 "-*- texinfo -*-\n\ | |
471 @deftypefn {Built-in Function} {} OCTAVE_VERSION ()\n\ | |
472 Return the version number of Octave, as a string.\n\ | |
473 @end deftypefn") | |
474 { | |
475 octave_value retval; | |
476 | |
477 int nargin = args.length (); | |
478 | |
479 if (nargin == 0) | |
480 retval = OCTAVE_VERSION; | |
481 else | |
5823 | 482 print_usage (); |
5749 | 483 |
484 return retval; | |
2203 | 485 } |
486 | |
487 /* | |
488 ;;; Local Variables: *** | |
489 ;;; mode: C++ *** | |
490 ;;; End: *** | |
491 */ |