GNU Octave  4.0.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-2015 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_data_dir;
78 std::string Voct_etc_dir;
79 std::string Voct_locale_dir;
80 
81 std::string Voct_file_dir;
82 std::string Vfcn_file_dir;
83 
84 std::string Vimage_dir;
85 
86 // The path that will be searched for programs that we execute.
87 // (--exec-path path)
88 static std::string VEXEC_PATH;
89 
90 // Name of the editor to be invoked by the edit_history command.
91 std::string VEDITOR;
92 
93 static std::string VIMAGE_PATH;
94 
96 std::string Vsite_defaults_file;
97 
99 
100 std::string
101 subst_octave_home (const std::string& s)
102 {
103  std::string retval;
104 
105  std::string prefix = OCTAVE_PREFIX;
106 
107  retval = s;
108 
109  if (Voctave_home != prefix)
110  {
111  octave_idx_type len = prefix.length ();
112 
113  if (s.substr (0, len) == prefix)
114  retval.replace (0, len, Voctave_home);
115  }
116 
117  if (file_ops::dir_sep_char () != '/')
118  std::replace (retval.begin (), retval.end (), '/',
120 
121  return retval;
122 }
123 
124 static void
126 {
127  std::string oh = octave_env::getenv ("OCTAVE_HOME");
128 
129  Voctave_home = oh.empty () ? std::string (OCTAVE_PREFIX) : oh;
130 }
131 
132 static void
134 {
136 }
137 
138 static void
140 {
142 }
143 
144 static void
146 {
148 }
149 
150 static void
152 {
154 }
155 
156 static void
158 {
160 }
161 
162 static void
164 {
166 }
167 
168 static void
170 {
172 }
173 
174 static void
176 {
178 }
179 
180 static void
182 {
184 }
185 
186 static void
188 {
190 }
191 
192 static void
194 {
196 }
197 
198 static void
200 {
202 }
203 
204 static void
206 {
208 }
209 
210 static void
212 {
214 }
215 
216 static void
218 {
220 }
221 
222 static void
224 {
226 }
227 
228 static void
230 {
232 }
233 
234 static void
236 {
238 }
239 
240 static void
242 {
244 }
245 
246 static void
248 {
250 }
251 
252 void
253 set_exec_path (const std::string& path_arg)
254 {
255  std::string tpath = path_arg;
256 
257  if (tpath.empty ())
258  tpath = octave_env::getenv ("OCTAVE_EXEC_PATH");
259 
260  if (tpath.empty ())
265  + Vbin_dir;
266 
267  VEXEC_PATH = tpath;
268 
269  // FIXME: should we really be modifying PATH in the environment?
270  // The way things are now, Octave will ignore directories set in the
271  // PATH with calls like
272  //
273  // setenv ("PATH", "/my/path");
274  //
275  // To fix this, I think Octave should be searching the combination of
276  // PATH and EXEC_PATH for programs that it executes instead of setting
277  // the PATH in the environment and relying on the shell to do the
278  // searching.
279 
280  // This is static so that even if set_exec_path is called more than
281  // once, shell_path is the original PATH from the environment,
282  // before we start modifying it.
283  static std::string shell_path = octave_env::getenv ("PATH");
284 
285  if (! shell_path.empty ())
286  tpath = shell_path + dir_path::path_sep_str () + tpath;
287 
288  octave_env::putenv ("PATH", tpath);
289 }
290 
291 void
292 set_image_path (const std::string& path)
293 {
294  VIMAGE_PATH = ".";
295 
296  std::string tpath = path;
297 
298  if (tpath.empty ())
299  tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH");
300 
301  if (! tpath.empty ())
302  VIMAGE_PATH += dir_path::path_sep_str () + tpath;
303 
304  tpath = genpath (Vimage_dir, "");
305 
306  if (! tpath.empty ())
307  VIMAGE_PATH += dir_path::path_sep_str () + tpath;
308 }
309 
310 static void
312 {
313  if (Vdoc_cache_file.empty ())
314  {
315  std::string def_file = subst_octave_home (OCTAVE_DOC_CACHE_FILE);
316 
317  std::string env_file = octave_env::getenv ("OCTAVE_DOC_CACHE_FILE");
318 
319  Vdoc_cache_file = env_file.empty () ? def_file : env_file;
320  }
321 }
322 
323 static void
325 {
326  if (Vtexi_macros_file.empty ())
327  {
328  std::string def_file = subst_octave_home (OCTAVE_TEXI_MACROS_FILE);
329 
330  std::string env_file = octave_env::getenv ("OCTAVE_TEXI_MACROS_FILE");
331 
332  Vtexi_macros_file = env_file.empty () ? def_file : env_file;
333  }
334 }
335 
336 static void
338 {
339  if (Vinfo_file.empty ())
340  {
341  std::string std_info_file = subst_octave_home (OCTAVE_INFOFILE);
342 
343  std::string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE");
344 
345  Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file;
346  }
347 }
348 
349 static void
351 {
352  if (Vinfo_program.empty ())
353  {
354  std::string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM");
355 
356  if (oct_info_prog.empty ())
357  Vinfo_program = "info";
358  else
359  Vinfo_program = std::string (oct_info_prog);
360  }
361 }
362 
363 static void
365 {
366  VEDITOR = "emacs";
367 
368  std::string env_editor = octave_env::getenv ("EDITOR");
369 
370  if (! env_editor.empty ())
371  VEDITOR = env_editor;
372 }
373 
374 static void
376 {
377  std::string lsf = octave_env::getenv ("OCTAVE_SITE_INITFILE");
378 
379  if (lsf.empty ())
380  {
383  Vlocal_site_defaults_file.append ("/octaverc");
384  }
385  else
387 }
388 
389 static void
391 {
392  std::string sf = octave_env::getenv ("OCTAVE_VERSION_INITFILE");
393 
394  if (sf.empty ())
395  {
397  Vsite_defaults_file.append ("/octaverc");
398  }
399  else
400  Vsite_defaults_file = sf;
401 }
402 
403 static void
405 {
406  if (Vbuilt_in_docstrings_file.empty ())
407  {
408  std::string df = octave_env::getenv ("OCTAVE_BUILT_IN_DOCSTRINGS_FILE");
409 
410  if (df.empty ())
412  = Voct_etc_dir + file_ops::dir_sep_str () + "built-in-docstrings";
413  else
415  }
416 }
417 
418 void
420 {
421  // OCTAVE_HOME must be set first!
422 
423  set_octave_home ();
424 
426 
428 
430 
432 
436 
440 
444 
448 
451 
453 
455 
456  set_exec_path ();
457 
458  set_image_path ();
459 
461 
463 
465 
467 
469 
471 
473 
475 }
476 
477 DEFUN (EDITOR, args, nargout,
478  "-*- texinfo -*-\n\
479 @deftypefn {Built-in Function} {@var{val} =} EDITOR ()\n\
480 @deftypefnx {Built-in Function} {@var{old_val} =} EDITOR (@var{new_val})\n\
481 @deftypefnx {Built-in Function} {} EDITOR (@var{new_val}, \"local\")\n\
482 Query or set the internal variable that specifies the default text editor.\n\
483 \n\
484 The default value is taken from the environment variable @w{@env{EDITOR}}\n\
485 when Octave starts. If the environment variable is not initialized,\n\
486 @w{@env{EDITOR}} will be set to @qcode{\"emacs\"}.\n\
487 \n\
488 When called from inside a function with the @qcode{\"local\"} option, the\n\
489 variable is changed locally for the function and any subroutines it calls.\n\
490 The original variable value is restored when exiting the function.\n\
491 \n\
492 @seealso{edit, edit_history}\n\
493 @end deftypefn")
494 {
496 }
497 
498 /*
499 %!test
500 %! orig_val = EDITOR ();
501 %! old_val = EDITOR ("X");
502 %! assert (orig_val, old_val);
503 %! assert (EDITOR (), "X");
504 %! EDITOR (orig_val);
505 %! assert (EDITOR (), orig_val);
506 
507 %!error (EDITOR (1, 2))
508 */
509 
510 DEFUN (EXEC_PATH, args, nargout,
511  "-*- texinfo -*-\n\
512 @deftypefn {Built-in Function} {@var{val} =} EXEC_PATH ()\n\
513 @deftypefnx {Built-in Function} {@var{old_val} =} EXEC_PATH (@var{new_val})\n\
514 @deftypefnx {Built-in Function} {} EXEC_PATH (@var{new_val}, \"local\")\n\
515 Query or set the internal variable that specifies a colon separated\n\
516 list of directories to append to the shell PATH when executing external\n\
517 programs.\n\
518 \n\
519 The initial value of is taken from the environment variable\n\
520 @w{@env{OCTAVE_EXEC_PATH}}, but that value can be overridden by the command\n\
521 line argument @option{--exec-path PATH}.\n\
522 \n\
523 When called from inside a function with the @qcode{\"local\"} option, the\n\
524 variable is changed locally for the function and any subroutines it calls.\n\
525 The original variable value is restored when exiting the function.\n\
526 \n\
527 @seealso{IMAGE_PATH, OCTAVE_HOME}\n\
528 @end deftypefn")
529 {
531 
532  if (args.length () > 0)
534 
535  return retval;
536 }
537 
538 /*
539 %!test
540 %! orig_val = EXEC_PATH ();
541 %! old_val = EXEC_PATH ("X");
542 %! assert (orig_val, old_val);
543 %! assert (EXEC_PATH (), "X");
544 %! EXEC_PATH (orig_val);
545 %! assert (EXEC_PATH (), orig_val);
546 
547 %!error (EXEC_PATH (1, 2))
548 */
549 
550 DEFUN (IMAGE_PATH, args, nargout,
551  "-*- texinfo -*-\n\
552 @deftypefn {Built-in Function} {@var{val} =} IMAGE_PATH ()\n\
553 @deftypefnx {Built-in Function} {@var{old_val} =} IMAGE_PATH (@var{new_val})\n\
554 @deftypefnx {Built-in Function} {} IMAGE_PATH (@var{new_val}, \"local\")\n\
555 Query or set the internal variable that specifies a colon separated\n\
556 list of directories in which to search for image files.\n\
557 \n\
558 When called from inside a function with the @qcode{\"local\"} option, the\n\
559 variable is changed locally for the function and any subroutines it calls.\n\
560 The original variable value is restored when exiting the function.\n\
561 \n\
562 @seealso{EXEC_PATH, OCTAVE_HOME}\n\
563 @end deftypefn")
564 {
565  return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (IMAGE_PATH);
566 }
567 
568 /*
569 %!test
570 %! orig_val = IMAGE_PATH ();
571 %! old_val = IMAGE_PATH ("X");
572 %! assert (orig_val, old_val);
573 %! assert (IMAGE_PATH (), "X");
574 %! IMAGE_PATH (orig_val);
575 %! assert (IMAGE_PATH (), orig_val);
576 
577 %!error (IMAGE_PATH (1, 2))
578 */
579 
580 DEFUN (OCTAVE_HOME, args, ,
581  "-*- texinfo -*-\n\
582 @deftypefn {Built-in Function} {} OCTAVE_HOME ()\n\
583 Return the name of the top-level Octave installation directory.\n\
584 @seealso{EXEC_PATH, IMAGE_PATH}\n\
585 @end deftypefn")
586 {
587  octave_value retval;
588 
589  if (args.length () == 0)
590  retval = Voctave_home;
591  else
592  print_usage ();
593 
594  return retval;
595 }
596 
597 /*
598 %!assert (ischar (OCTAVE_HOME ()))
599 %!error OCTAVE_HOME (1)
600 */
601 
602 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, ,
603  "-*- texinfo -*-\n\
604 @deftypefn {Built-in Function} {} OCTAVE_VERSION ()\n\
605 Return the version number of Octave as a string.\n\
606 @seealso{ver, version}\n\
607 @end deftypefn")
608 {
609  octave_value retval;
610 
611  int nargin = args.length ();
612 
613  if (nargin == 0)
614  retval = OCTAVE_VERSION;
615  else
616  print_usage ();
617 
618  return retval;
619 }
620 
621 /*
622 %!assert (ischar (OCTAVE_VERSION ()))
623 %!error OCTAVE_VERSION (1)
624 */
static void set_default_bin_dir(void)
Definition: defaults.cc:247
#define OCTAVE_FCNFILEDIR
Definition: defaults.h:68
void set_exec_path(const std::string &path_arg)
Definition: defaults.cc:253
std::string Vsite_defaults_file
Definition: defaults.cc:96
static void set_default_doc_cache_file(void)
Definition: defaults.cc:311
#define OCTAVE_DATADIR
Definition: defaults.h:48
std::string Vlocal_oct_file_dir
Definition: defaults.cc:71
std::string Vinfo_file
Definition: help.cc:82
#define OCTAVE_LOCALVEROCTFILEDIR
Definition: defaults.h:136
static std::string path_sep_str(void)
Definition: pathsearch.h:95
std::string Vlocal_fcn_file_dir
Definition: defaults.cc:75
std::string Vinfo_program
Definition: help.cc:86
OCTINTERP_API void print_usage(void)
Definition: defun.cc:51
std::string Vlocal_ver_fcn_file_dir
Definition: defaults.cc:73
#define OCTAVE_LOCALFCNFILEDIR
Definition: defaults.h:112
static void putenv(const std::string &name, const std::string &value)
Definition: oct-env.cc:245
static void set_default_local_arch_lib_dir(void)
Definition: defaults.cc:157
#define OCTAVE_OCTETCDIR
Definition: defaults.h:160
#define OCTAVE_LOCALSTARTUPFILEDIR
Definition: defaults.h:120
static void set_default_info_dir(void)
Definition: defaults.cc:133
std::string Vlocal_api_arch_lib_dir
Definition: defaults.cc:66
std::string Vdata_dir
Definition: defaults.cc:62
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:44
#define OCTAVE_LOCALARCHLIBDIR
Definition: defaults.h:108
#define OCTAVE_INFOFILE
Definition: defaults.h:84
std::string Vfcn_file_dir
Definition: defaults.cc:82
static void set_default_local_api_oct_file_dir(void)
Definition: defaults.cc:181
#define OCTAVE_STARTUPFILEDIR
Definition: defaults.h:184
#define OCTAVE_OCTFILEDIR
Definition: defaults.h:156
std::string Vbuilt_in_docstrings_file
Definition: defaults.cc:98
void set_image_path(const std::string &path)
Definition: defaults.cc:292
#define OCTAVE_ARCHLIBDIR
Definition: main.cc:50
std::string Vdoc_cache_file
Definition: help.cc:73
static void set_default_fcn_file_dir(void)
Definition: defaults.cc:211
static void set_default_arch_lib_dir(void)
Definition: defaults.cc:151
std::string Voct_data_dir
Definition: defaults.cc:77
static void set_default_oct_data_dir(void)
Definition: defaults.cc:223
static void set_default_local_oct_file_dir(void)
Definition: defaults.cc:187
static void set_default_oct_etc_dir(void)
Definition: defaults.cc:229
#define OCTAVE_PREFIX
Definition: main.cc:58
static void set_default_info_prog(void)
Definition: defaults.cc:350
static void set_default_local_api_fcn_file_dir(void)
Definition: defaults.cc:199
std::string Vlocal_arch_lib_dir
Definition: defaults.cc:65
static void replace(QString &text, const QRegExp &re, const QString &after)
Definition: parser.cc:566
std::string Vtexi_macros_file
Definition: help.cc:78
std::string Vlocal_api_fcn_file_dir
Definition: defaults.cc:74
static void set_default_libexec_dir(void)
Definition: defaults.cc:145
std::string Vinfo_dir
Definition: defaults.cc:61
std::string Vbin_dir
Definition: defaults.cc:60
std::string Voctave_home
Definition: defaults.cc:58
static void set_default_local_ver_fcn_file_dir(void)
Definition: defaults.cc:193
std::string Vlibexec_dir
Definition: defaults.cc:63
static void set_default_local_ver_arch_lib_dir(void)
Definition: defaults.cc:169
static std::string getenv(const std::string &name)
Definition: oct-env.cc:238
#define DEFUNX(name, fname, args_name, nargout_name, doc)
Definition: defun.h:52
#define OCTAVE_BINDIR
Definition: main.cc:54
std::string Vlocal_ver_arch_lib_dir
Definition: defaults.cc:67
#define OCTAVE_IMAGEDIR
Definition: defaults.h:72
static void set_site_defaults_file(void)
Definition: defaults.cc:390
std::string Vlocal_site_defaults_file
Definition: defaults.cc:95
static void set_default_local_fcn_file_dir(void)
Definition: defaults.cc:205
octave_idx_type length(void) const
Definition: ov.cc:1525
static void set_default_image_dir(void)
Definition: defaults.cc:217
#define OCTAVE_LOCALAPIFCNFILEDIR
Definition: defaults.h:100
static void set_default_texi_macros_file(void)
Definition: defaults.cc:324
std::string VEDITOR
Definition: defaults.cc:91
#define OCTAVE_LOCALOCTFILEDIR
Definition: defaults.h:116
#define OCTAVE_LOCALVERARCHLIBDIR
Definition: defaults.h:128
static void set_default_info_file(void)
Definition: defaults.cc:337
#define SET_NONEMPTY_INTERNAL_STRING_VARIABLE(NM)
Definition: variables.h:123
std::string Vlocal_ver_oct_file_dir
Definition: defaults.cc:69
#define OCTAVE_VERSION
Definition: main.cc:46
static void set_default_editor(void)
Definition: defaults.cc:364
#define OCTAVE_OCTDATADIR
Definition: defaults.h:152
static void set_octave_home(void)
Definition: defaults.cc:125
#define OCTAVE_LOCALAPIOCTFILEDIR
Definition: defaults.h:104
static void set_default_oct_locale_dir(void)
Definition: defaults.cc:235
std::string Voct_file_dir
Definition: defaults.cc:81
#define OCTAVE_OCTLOCALEDIR
Definition: defaults.h:164
#define OCTAVE_TEXI_MACROS_FILE
Definition: defaults.h:60
#define OCTAVE_DOC_CACHE_FILE
Definition: defaults.h:56
static void set_default_oct_file_dir(void)
Definition: defaults.cc:241
static void set_local_site_defaults_file(void)
Definition: defaults.cc:375
static std::string VIMAGE_PATH
Definition: defaults.cc:93
static void set_built_in_docstrings_file(void)
Definition: defaults.cc:404
std::string Voct_etc_dir
Definition: defaults.cc:78
static std::string VEXEC_PATH
Definition: defaults.cc:88
static void set_default_data_dir(void)
Definition: defaults.cc:139
#define OCTAVE_LOCALAPIARCHLIBDIR
Definition: defaults.h:124
static std::string dir_sep_str(void)
Definition: file-ops.h:63
void install_defaults(void)
Definition: defaults.cc:419
#define OCTAVE_LIBEXECDIR
Definition: defaults.h:92
std::string Varch_lib_dir
Definition: defaults.cc:64
std::string subst_octave_home(const std::string &s)
Definition: defaults.cc:101
std::string Voct_locale_dir
Definition: defaults.cc:79
static void set_default_local_api_arch_lib_dir(void)
Definition: defaults.cc:163
std::string genpath(const std::string &dirname, const string_vector &skip)
Definition: load-path.cc:2141
OCTAVE_EXPORT octave_value_list FOCTAVE_VERSION(const octave_value_list &args, int)
Definition: defaults.cc:607
static void set_default_local_ver_oct_file_dir(void)
Definition: defaults.cc:175
#define OCTAVE_LOCALVERFCNFILEDIR
Definition: defaults.h:132
std::string Vimage_dir
Definition: defaults.cc:84
static char dir_sep_char(void)
Definition: file-ops.h:58
std::string Vlocal_api_oct_file_dir
Definition: defaults.cc:70
#define OCTAVE_INFODIR
Definition: defaults.h:80