GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
defaults.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
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
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
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
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cstdlib>
28 
29 #include <algorithm>
30 #include <iostream>
31 #include <string>
32 
33 #include <sys/types.h>
34 #include <unistd.h>
35 
36 #include "dir-ops.h"
37 #include "oct-env.h"
38 #include "file-stat.h"
39 #include "pathsearch.h"
40 #include "str-vec.h"
41 
42 #include <defaults.h>
43 #include "defun.h"
44 #include "error.h"
45 #include "file-ops.h"
46 #include "gripes.h"
47 #include "help.h"
48 #include "input.h"
49 #include "load-path.h"
50 #include "oct-obj.h"
51 #include "ov.h"
52 #include "parse.h"
53 #include "toplev.h"
54 #include "unwind-prot.h"
55 #include "variables.h"
56 #include <version.h>
57 
58 std::string Voctave_home;
59 
60 std::string Vbin_dir;
61 std::string Vinfo_dir;
62 std::string Vdata_dir;
63 std::string Vlibexec_dir;
64 std::string Varch_lib_dir;
65 std::string Vlocal_arch_lib_dir;
68 
71 std::string Vlocal_oct_file_dir;
72 
75 std::string Vlocal_fcn_file_dir;
76 
77 std::string Voct_etc_dir;
78 std::string Voct_locale_dir;
79 
80 std::string Voct_file_dir;
81 std::string Vfcn_file_dir;
82 
83 std::string Vimage_dir;
84 
85 // The path that will be searched for programs that we execute.
86 // (--exec-path path)
87 static std::string VEXEC_PATH;
88 
89 // Name of the editor to be invoked by the edit_history command.
90 std::string VEDITOR;
91 
92 static std::string VIMAGE_PATH;
93 
95 std::string Vsite_defaults_file;
96 
98 
99 std::string
100 subst_octave_home (const std::string& s)
101 {
102  std::string retval;
103 
104  std::string prefix = OCTAVE_PREFIX;
105 
106  retval = s;
107 
108  if (Voctave_home != prefix)
109  {
110  octave_idx_type len = prefix.length ();
111 
112  if (s.substr (0, len) == prefix)
113  retval.replace (0, len, Voctave_home);
114  }
115 
116  if (file_ops::dir_sep_char () != '/')
117  std::replace (retval.begin (), retval.end (), '/',
119 
120  return retval;
121 }
122 
123 static void
125 {
126  std::string oh = octave_env::getenv ("OCTAVE_HOME");
127 
128  Voctave_home = oh.empty () ? std::string (OCTAVE_PREFIX) : oh;
129 }
130 
131 static void
133 {
135 }
136 
137 static void
139 {
141 }
142 
143 static void
145 {
147 }
148 
149 static void
151 {
153 }
154 
155 static void
157 {
159 }
160 
161 static void
163 {
165 }
166 
167 static void
169 {
171 }
172 
173 static void
175 {
177 }
178 
179 static void
181 {
183 }
184 
185 static void
187 {
189 }
190 
191 static void
193 {
195 }
196 
197 static void
199 {
201 }
202 
203 static void
205 {
207 }
208 
209 static void
211 {
213 }
214 
215 static void
217 {
219 }
220 
221 static void
223 {
225 }
226 
227 static void
229 {
231 }
232 
233 static void
235 {
237 }
238 
239 static void
241 {
243 }
244 
245 void
246 set_exec_path (const std::string& path_arg)
247 {
248  std::string tpath = path_arg;
249 
250  if (tpath.empty ())
251  tpath = octave_env::getenv ("OCTAVE_EXEC_PATH");
252 
253  if (tpath.empty ())
258  + Vbin_dir;
259 
260  VEXEC_PATH = tpath;
261 
262  // FIXME: should we really be modifying PATH in the environment?
263  // The way things are now, Octave will ignore directories set in the
264  // PATH with calls like
265  //
266  // setenv ("PATH", "/my/path");
267  //
268  // To fix this, I think Octave should be searching the combination of
269  // PATH and EXEC_PATH for programs that it executes instead of setting
270  // the PATH in the environment and relying on the shell to do the
271  // searching.
272 
273  // This is static so that even if set_exec_path is called more than
274  // once, shell_path is the original PATH from the environment,
275  // before we start modifying it.
276  static std::string shell_path = octave_env::getenv ("PATH");
277 
278  if (! shell_path.empty ())
279  tpath = shell_path + dir_path::path_sep_str () + tpath;
280 
281  octave_env::putenv ("PATH", tpath);
282 }
283 
284 void
285 set_image_path (const std::string& path)
286 {
287  VIMAGE_PATH = ".";
288 
289  std::string tpath = path;
290 
291  if (tpath.empty ())
292  tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH");
293 
294  if (! tpath.empty ())
295  VIMAGE_PATH += dir_path::path_sep_str () + tpath;
296 
297  tpath = genpath (Vimage_dir, "");
298 
299  if (! tpath.empty ())
300  VIMAGE_PATH += dir_path::path_sep_str () + tpath;
301 }
302 
303 static void
305 {
306  if (Vdoc_cache_file.empty ())
307  {
308  std::string def_file = subst_octave_home (OCTAVE_DOC_CACHE_FILE);
309 
310  std::string env_file = octave_env::getenv ("OCTAVE_DOC_CACHE_FILE");
311 
312  Vdoc_cache_file = env_file.empty () ? def_file : env_file;
313  }
314 }
315 
316 static void
318 {
319  if (Vtexi_macros_file.empty ())
320  {
321  std::string def_file = subst_octave_home (OCTAVE_TEXI_MACROS_FILE);
322 
323  std::string env_file = octave_env::getenv ("OCTAVE_TEXI_MACROS_FILE");
324 
325  Vtexi_macros_file = env_file.empty () ? def_file : env_file;
326  }
327 }
328 
329 static void
331 {
332  if (Vinfo_file.empty ())
333  {
334  std::string std_info_file = subst_octave_home (OCTAVE_INFOFILE);
335 
336  std::string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE");
337 
338  Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file;
339  }
340 }
341 
342 static void
344 {
345  if (Vinfo_program.empty ())
346  {
347  std::string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM");
348 
349  if (oct_info_prog.empty ())
350  Vinfo_program = "info";
351  else
352  Vinfo_program = std::string (oct_info_prog);
353  }
354 }
355 
356 static void
358 {
359  VEDITOR = "emacs";
360 
361  std::string env_editor = octave_env::getenv ("EDITOR");
362 
363  if (! env_editor.empty ())
364  VEDITOR = env_editor;
365 }
366 
367 static void
369 {
370  std::string lsf = octave_env::getenv ("OCTAVE_SITE_INITFILE");
371 
372  if (lsf.empty ())
373  {
376  Vlocal_site_defaults_file.append ("/octaverc");
377  }
378  else
380 }
381 
382 static void
384 {
385  std::string sf = octave_env::getenv ("OCTAVE_VERSION_INITFILE");
386 
387  if (sf.empty ())
388  {
390  Vsite_defaults_file.append ("/octaverc");
391  }
392  else
393  Vsite_defaults_file = sf;
394 }
395 
396 static void
398 {
399  if (Vbuilt_in_docstrings_file.empty ())
400  {
401  std::string df = octave_env::getenv ("OCTAVE_BUILT_IN_DOCSTRINGS_FILE");
402 
403  if (df.empty ())
405  = Voct_etc_dir + file_ops::dir_sep_str () + "built-in-docstrings";
406  else
408  }
409 }
410 
411 void
413 {
414  // OCTAVE_HOME must be set first!
415 
416  set_octave_home ();
417 
419 
421 
423 
425 
429 
433 
437 
440 
443 
445 
447 
448  set_exec_path ();
449 
450  set_image_path ();
451 
453 
455 
457 
459 
461 
463 
465 
467 }
468 
469 DEFUN (EDITOR, args, nargout,
470  "-*- texinfo -*-\n\
471 @deftypefn {Built-in Function} {@var{val} =} EDITOR ()\n\
472 @deftypefnx {Built-in Function} {@var{old_val} =} EDITOR (@var{new_val})\n\
473 @deftypefnx {Built-in Function} {} EDITOR (@var{new_val}, \"local\")\n\
474 Query or set the internal variable that specifies the default text editor.\n\
475 \n\
476 The default value is taken from the environment variable @w{@env{EDITOR}}\n\
477 when Octave starts. If the\n\
478 environment variable is not initialized, @w{@env{EDITOR}} will be set to\n\
479 @qcode{\"emacs\"}.\n\
480 \n\
481 When called from inside a function with the @qcode{\"local\"} option, the\n\
482 variable is changed locally for the function and any subroutines it calls. \n\
483 The original variable value is restored when exiting the function.\n\
484 \n\
485 @seealso{edit, edit_history}\n\
486 @end deftypefn")
487 {
489 }
490 
491 /*
492 %!test
493 %! orig_val = EDITOR ();
494 %! old_val = EDITOR ("X");
495 %! assert (orig_val, old_val);
496 %! assert (EDITOR (), "X");
497 %! EDITOR (orig_val);
498 %! assert (EDITOR (), orig_val);
499 
500 %!error (EDITOR (1, 2))
501 */
502 
503 DEFUN (EXEC_PATH, args, nargout,
504  "-*- texinfo -*-\n\
505 @deftypefn {Built-in Function} {@var{val} =} EXEC_PATH ()\n\
506 @deftypefnx {Built-in Function} {@var{old_val} =} EXEC_PATH (@var{new_val})\n\
507 @deftypefnx {Built-in Function} {} EXEC_PATH (@var{new_val}, \"local\")\n\
508 Query or set the internal variable that specifies a colon separated\n\
509 list of directories to append to the shell PATH when executing external\n\
510 programs. The initial value of is taken from the environment variable\n\
511 @w{@env{OCTAVE_EXEC_PATH}}, but that value can be overridden by\n\
512 the command line argument @option{--exec-path PATH}.\n\
513 \n\
514 When called from inside a function with the @qcode{\"local\"} option, the\n\
515 variable is changed locally for the function and any subroutines it calls. \n\
516 The original variable value is restored when exiting the function.\n\
517 \n\
518 @seealso{IMAGE_PATH, OCTAVE_HOME}\n\
519 @end deftypefn")
520 {
522 
523  if (args.length () > 0)
525 
526  return retval;
527 }
528 
529 /*
530 %!test
531 %! orig_val = EXEC_PATH ();
532 %! old_val = EXEC_PATH ("X");
533 %! assert (orig_val, old_val);
534 %! assert (EXEC_PATH (), "X");
535 %! EXEC_PATH (orig_val);
536 %! assert (EXEC_PATH (), orig_val);
537 
538 %!error (EXEC_PATH (1, 2))
539 */
540 
541 DEFUN (IMAGE_PATH, args, nargout,
542  "-*- texinfo -*-\n\
543 @deftypefn {Built-in Function} {@var{val} =} IMAGE_PATH ()\n\
544 @deftypefnx {Built-in Function} {@var{old_val} =} IMAGE_PATH (@var{new_val})\n\
545 @deftypefnx {Built-in Function} {} IMAGE_PATH (@var{new_val}, \"local\")\n\
546 Query or set the internal variable that specifies a colon separated\n\
547 list of directories in which to search for image files.\n\
548 \n\
549 When called from inside a function with the @qcode{\"local\"} option, the\n\
550 variable is changed locally for the function and any subroutines it calls. \n\
551 The original variable value is restored when exiting the function.\n\
552 \n\
553 @seealso{EXEC_PATH, OCTAVE_HOME}\n\
554 @end deftypefn")
555 {
556  return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (IMAGE_PATH);
557 }
558 
559 /*
560 %!test
561 %! orig_val = IMAGE_PATH ();
562 %! old_val = IMAGE_PATH ("X");
563 %! assert (orig_val, old_val);
564 %! assert (IMAGE_PATH (), "X");
565 %! IMAGE_PATH (orig_val);
566 %! assert (IMAGE_PATH (), orig_val);
567 
568 %!error (IMAGE_PATH (1, 2))
569 */
570 
571 DEFUN (OCTAVE_HOME, args, ,
572  "-*- texinfo -*-\n\
573 @deftypefn {Built-in Function} {} OCTAVE_HOME ()\n\
574 Return the name of the top-level Octave installation directory.\n\
575 \n\
576 @seealso{EXEC_PATH, IMAGE_PATH}\n\
577 @end deftypefn")
578 {
579  octave_value retval;
580 
581  if (args.length () == 0)
582  retval = Voctave_home;
583  else
584  print_usage ();
585 
586  return retval;
587 }
588 
589 /*
590 %!assert (ischar (OCTAVE_HOME ()))
591 %!error OCTAVE_HOME (1)
592 */
593 
594 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, ,
595  "-*- texinfo -*-\n\
596 @deftypefn {Built-in Function} {} OCTAVE_VERSION ()\n\
597 Return the version number of Octave, as a string.\n\
598 @end deftypefn")
599 {
600  octave_value retval;
601 
602  int nargin = args.length ();
603 
604  if (nargin == 0)
605  retval = OCTAVE_VERSION;
606  else
607  print_usage ();
608 
609  return retval;
610 }
611 
612 /*
613 %!assert (ischar (OCTAVE_VERSION ()))
614 %!error OCTAVE_VERSION (1)
615 */