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