GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
defaults.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <cstdlib>
31 
32 #include <algorithm>
33 #include <string>
34 
35 #include "dir-ops.h"
36 #include "file-ops.h"
37 #include "oct-env.h"
38 
39 #include "defaults.h"
40 #include "defun.h"
41 #include "error.h"
42 #include "file-ops.h"
43 #include "ovl.h"
44 #include "ov.h"
45 #include "variables.h"
46 #include "version.h"
47 
48 #include "default-defs.h"
49 
51 
53 
54 // Variables that name directories or files are substituted into source
55 // files with "${prefix}/" stripped from the beginning of the string.
56 
57 // All configure variables of this form should be specified as absolute
58 // directory names. The only ones that should not be absolute here are
59 // ones that have had "${prefix}/" or "${exec_prefix} stripped.
60 
61 static std::string
62 prepend_home_dir (const std::string& hd, const std::string& s)
63 {
64  std::string retval = s;
65 
67 
68  if (! sys::env::absolute_pathname (retval))
69  retval = hd + dir_sep_char + s;
70 
71  if (dir_sep_char != '/')
72  std::replace (retval.begin (), retval.end (), '/', dir_sep_char);
73 
74  return retval;
75 }
76 
77 static std::string
78 get_octave_home ()
79 {
80  std::string op = OCTAVE_PREFIX;
81 
82  std::string oh = sys::env::getenv ("OCTAVE_HOME");
83 
84  // If OCTAVE_HOME is set in the environment, use that. Otherwise,
85  // default to ${prefix} from configure.
86 
87  return oh.empty () ? op : oh;
88 }
89 
90 static std::string
91 get_octave_exec_home ()
92 {
93  std::string op = OCTAVE_PREFIX;
94  std::string oep = OCTAVE_EXEC_PREFIX;
95 
96  std::string oh = sys::env::getenv ("OCTAVE_HOME");
97  std::string oeh = sys::env::getenv ("OCTAVE_EXEC_HOME");
98 
99  // If OCTAVE_EXEC_HOME is set in the environment, use that.
100  // Otherwise, if ${prefix} and ${exec_prefix} from configure are set
101  // to the same value, use OCTAVE_HOME from the environment if it is set.
102  // Otherwise, default to ${exec_prefix} from configure.
103 
104  if (! oeh.empty ())
105  return oeh;
106 
107  if (op == oep && ! oh.empty ())
108  return oh;
109 
110  return oep;
111 }
112 
113 static std::string
114 get_local_site_defaults_file ()
115 {
116  std::string lsf = sys::env::getenv ("OCTAVE_SITE_INITFILE");
117 
118  return lsf.empty () ? local_startupfile_dir () + "/octaverc" : lsf;
119 }
120 
121 static std::string
122 get_site_defaults_file ()
123 {
124  std::string sf = sys::env::getenv ("OCTAVE_VERSION_INITFILE");
125 
126  return sf.empty () ? startupfile_dir () + "/octaverc" : sf;
127 }
128 
129 std::string
130 prepend_octave_home (const std::string& s)
131 {
132  return prepend_home_dir (octave_home (), s);
133 }
134 
135 std::string
136 prepend_octave_exec_home (const std::string& s)
137 {
138  return prepend_home_dir (octave_exec_home (), s);
139 }
140 
141 std::string
143 {
144  static const std::string s_canonical_host_type
146 
147  return s_canonical_host_type;
148 }
149 
150 std::string
152 {
153  static const std::string s_octave_release = OCTAVE_RELEASE;
154 
155  return s_octave_release;
156 }
157 
158 std::string
160 {
161  static const std::string s_default_pager = OCTAVE_DEFAULT_PAGER;
162 
163  return s_default_pager;
164 }
165 
166 std::string
168 {
169  static const std::string s_octave_home = get_octave_home ();
170 
171  return s_octave_home;
172 }
173 
174 std::string
176 {
177  static const std::string s_octave_exec_home = get_octave_exec_home ();
178 
179  return s_octave_exec_home;
180 }
181 
182 std::string
184 {
185  static const std::string s_bin_dir
187 
188  return s_bin_dir;
189 }
190 
191 std::string
193 {
194  static const std::string s_data_dir
196 
197  return s_data_dir;
198 }
199 
200 std::string
202 {
203  static const std::string s_dataroot_dir
205 
206  return s_dataroot_dir;
207 }
208 
209 std::string
211 {
212  static const std::string s_include_dir
214 
215  return s_include_dir;
216 }
217 
218 std::string
220 {
221  static const std::string s_lib_dir
223 
224  return s_lib_dir;
225 }
226 
227 std::string
229 {
230  static const std::string s_libexec_dir
232 
233  return s_libexec_dir;
234 }
235 
236 std::string
238 {
239  static const std::string s_arch_lib_dir
241 
242  return s_arch_lib_dir;
243 }
244 
245 std::string
247 {
248  static const std::string s_info_dir
250 
251  return s_info_dir;
252 }
253 
254 std::string
256 {
257  static const std::string s_local_ver_arch_lib_dir
259 
260  return s_local_ver_arch_lib_dir;
261 }
262 
263 std::string
265 {
266  static const std::string s_local_api_arch_lib_dir
268 
269  return s_local_api_arch_lib_dir;
270 }
271 
272 std::string
274 {
275  static const std::string s_local_arch_lib_dir
277 
278  return s_local_arch_lib_dir;
279 }
280 
281 std::string
283 {
284  static const std::string s_local_ver_oct_file_dir
286 
287  return s_local_ver_oct_file_dir;
288 }
289 
290 std::string
292 {
293  static const std::string s_local_api_oct_file_dir
295 
296  return s_local_api_oct_file_dir;
297 }
298 
299 std::string
301 {
302  static const std::string s_local_oct_file_dir
304 
305  return s_local_oct_file_dir;
306 }
307 
308 std::string
310 {
311  static const std::string s_oct_file_dir
313 
314  return s_oct_file_dir;
315 }
316 
317 std::string
319 {
320  static const std::string s_local_ver_fcn_file_dir
322 
323  return s_local_ver_fcn_file_dir;
324 }
325 
326 std::string
328 {
329  static const std::string s_local_api_fcn_file_dir
331 
332  return s_local_api_fcn_file_dir;
333 }
334 
335 std::string
337 {
338  static const std::string s_local_fcn_file_dir
340 
341  return s_local_fcn_file_dir;
342 }
343 
344 std::string
346 {
347  static const std::string s_fcn_file_dir
349 
350  return s_fcn_file_dir;
351 }
352 
353 std::string
355 {
356  static const std::string s_oct_data_dir
358 
359  return s_oct_data_dir;
360 }
361 
362 std::string
364 {
365  static const std::string s_oct_doc_dir
367 
368  return s_oct_doc_dir;
369 }
370 
371 std::string
373 {
374  static const std::string s_oct_etc_dir
376 
377  return s_oct_etc_dir;
378 }
379 
380 std::string
382 {
383  static const std::string s_oct_fonts_dir
385 
386  return s_oct_fonts_dir;
387 }
388 
389 std::string
391 {
392  static const std::string s_oct_include_dir
394 
395  return s_oct_include_dir;
396 }
397 
398 std::string
400 {
401  static const std::string s_oct_lib_dir
403 
404  return s_oct_lib_dir;
405 }
406 
407 std::string
409 {
410  static const std::string s_oct_locale_dir
412 
413  return s_oct_locale_dir;
414 }
415 
416 std::string
418 {
419  static const std::string s_oct_tests_dir
421 
422  return s_oct_tests_dir;
423 }
424 
425 std::string
427 {
428  static const std::string s_man_dir
430 
431  return s_man_dir;
432 }
433 
434 std::string
436 {
437  static const std::string s_man1_dir
439 
440  return s_man1_dir;
441 }
442 
443 std::string
445 {
446  static const std::string s_man1_ext = OCTAVE_MAN1EXT;
447 
448  return s_man1_ext;
449 }
450 
451 std::string
453 {
454  static const std::string s_image_dir
456 
457  return s_image_dir;
458 }
459 
460 std::string
462 {
463  static const std::string s_local_startupfile_dir
465 
466  return s_local_startupfile_dir;
467 }
468 
469 std::string
471 {
472  static const std::string s_startupfile_dir
474 
475  return s_startupfile_dir;
476 }
477 
478 std::string
480 {
481  static const std::string s_local_site_defaults_file
482  = get_local_site_defaults_file ();
483 
484  return s_local_site_defaults_file;
485 }
486 
487 std::string
489 {
490  static const std::string s_site_defaults_file
491  = get_site_defaults_file ();
492 
493  return s_site_defaults_file;
494 }
495 
496 OCTAVE_END_NAMESPACE(config)
497 
498 DEFUN (OCTAVE_HOME, args, ,
499  doc: /* -*- texinfo -*-
500 @deftypefn {} {@var{dir} =} OCTAVE_HOME ()
501 Return the name of the top-level Octave installation directory.
502 
503 OCTAVE_HOME corresponds to the configuration variable @var{prefix}.
504 @seealso{EXEC_PATH, IMAGE_PATH, OCTAVE_EXEC_HOME}
505 @end deftypefn */)
506 {
507  if (args.length () != 0)
508  print_usage ();
509 
510  return ovl (config::octave_home ());
511 }
512 
513 /*
514 %!assert (ischar (OCTAVE_HOME ()))
515 %!error OCTAVE_HOME (1)
516 */
517 
518 DEFUN (OCTAVE_EXEC_HOME, args, ,
519  doc: /* -*- texinfo -*-
520 @deftypefn {} {@var{dir} =} OCTAVE_EXEC_HOME ()
521 Return the name of the top-level Octave installation directory for
522 architecture-dependent files.
523 
524 If not specified separately, the value is the same as OCTAVE_HOME@.
525 OCTAVE_EXEC_HOME corresponds to the configuration variable @var{exec_prefix}.
526 @seealso{EXEC_PATH, IMAGE_PATH, OCTAVE_HOME}
527 @end deftypefn */)
528 {
529  if (args.length () != 0)
530  print_usage ();
531 
532  return ovl (config::octave_exec_home ());
533 }
534 
535 /*
536 %!assert (ischar (OCTAVE_EXEC_HOME ()))
537 %!error OCTAVE_EXEC_HOME (1)
538 */
539 
540 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, ,
541  doc: /* -*- texinfo -*-
542 @deftypefn {} {@var{verstr} =} OCTAVE_VERSION ()
543 Return the version number of Octave as a string.
544 @seealso{ver, version}
545 @end deftypefn */)
546 {
547  if (args.length () != 0)
548  print_usage ();
549 
550  return ovl (OCTAVE_VERSION);
551 }
552 
553 /*
554 %!assert (ischar (OCTAVE_VERSION ()))
555 %!error OCTAVE_VERSION (1)
556 */
557 
558 DEFUN (user_config_dir, args, ,
559  doc: /* -*- texinfo -*-
560 @deftypefn {} {cfg_dir =} user_config_dir ()
561 Return the (platform-specific) directory for user configuration.
562 @seealso{user_data_dir}
563 @end deftypefn */)
564 {
565  if (args.length () != 0)
566  print_usage ();
567 
568  return ovl (sys::env::get_user_config_directory ());
569 }
570 
571 /*
572 %!assert (ischar (user_config_dir ()))
573 %!error user_config_dir (1)
574 */
575 
576 DEFUN (user_data_dir, args, ,
577  doc: /* -*- texinfo -*-
578 @deftypefn {} {data_dir =} user_data_dir ()
579 Return the (platform-specific) directory for user data.
580 @seealso{user_config_dir}
581 @end deftypefn */)
582 {
583  if (args.length () != 0)
584  print_usage ();
585 
586  return ovl (sys::env::get_user_data_directory ());
587 }
588 
589 /*
590 %!assert (ischar (user_data_dir ()))
591 %!error user_data_dir (1)
592 */
593 
594 OCTAVE_END_NAMESPACE(octave)
octave_value_list FOCTAVE_VERSION(const octave_value_list &=octave_value_list(), int=0)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_OCTLIBDIR
Definition: default-defs.h:140
#define OCTAVE_LOCALSTARTUPFILEDIR
Definition: default-defs.h:172
#define OCTAVE_LOCALVERFCNFILEDIR
Definition: default-defs.h:104
#define OCTAVE_OCTINCLUDEDIR
Definition: default-defs.h:136
#define OCTAVE_LOCALAPIOCTFILEDIR
Definition: default-defs.h:92
#define OCTAVE_OCTDOCDIR
Definition: default-defs.h:124
#define OCTAVE_LIBEXECDIR
Definition: default-defs.h:68
#define OCTAVE_LOCALVEROCTFILEDIR
Definition: default-defs.h:88
#define OCTAVE_OCTFONTSDIR
Definition: default-defs.h:132
#define OCTAVE_OCTETCDIR
Definition: default-defs.h:128
#define OCTAVE_OCTDATADIR
Definition: default-defs.h:120
#define OCTAVE_OCTLOCALEDIR
Definition: default-defs.h:144
#define OCTAVE_FCNFILEDIR
Definition: default-defs.h:116
#define OCTAVE_MAN1EXT
Definition: default-defs.h:164
#define OCTAVE_LOCALOCTFILEDIR
Definition: default-defs.h:96
#define OCTAVE_LIBDIR
Definition: default-defs.h:64
#define OCTAVE_MANDIR
Definition: default-defs.h:156
#define OCTAVE_LOCALAPIARCHLIBDIR
Definition: default-defs.h:76
#define OCTAVE_STARTUPFILEDIR
Definition: default-defs.h:176
#define OCTAVE_INFODIR
Definition: default-defs.h:152
#define OCTAVE_INCLUDEDIR
Definition: default-defs.h:60
#define OCTAVE_OCTTESTSDIR
Definition: default-defs.h:148
#define OCTAVE_DEFAULT_PAGER
Definition: default-defs.h:36
#define OCTAVE_OCTFILEDIR
Definition: default-defs.h:100
#define OCTAVE_LOCALARCHLIBDIR
Definition: default-defs.h:80
#define OCTAVE_LOCALVERARCHLIBDIR
Definition: default-defs.h:72
#define OCTAVE_IMAGEDIR
Definition: default-defs.h:168
#define OCTAVE_LOCALFCNFILEDIR
Definition: default-defs.h:112
#define OCTAVE_LOCALAPIFCNFILEDIR
Definition: default-defs.h:108
#define OCTAVE_MAN1DIR
Definition: default-defs.h:160
#define OCTAVE_DATADIR
Definition: default-defs.h:52
#define OCTAVE_DATAROOTDIR
Definition: default-defs.h:56
#define OCTAVE_RELEASE
Definition: default-defs.h:32
std::string startupfile_dir()
std::string man_dir()
std::string dataroot_dir()
std::string oct_doc_dir()
std::string default_pager()
std::string local_ver_arch_lib_dir()
std::string local_oct_file_dir()
std::string prepend_octave_home(const std::string &s)
std::string local_startupfile_dir()
std::string octave_exec_home()
std::string canonical_host_type()
std::string oct_fonts_dir()
std::string lib_dir()
std::string local_fcn_file_dir()
std::string include_dir()
std::string local_api_oct_file_dir()
std::string local_arch_lib_dir()
std::string info_dir()
std::string oct_tests_dir()
std::string fcn_file_dir()
std::string site_defaults_file()
std::string bin_dir()
std::string local_api_fcn_file_dir()
std::string local_ver_fcn_file_dir()
std::string man1_dir()
std::string octave_home()
std::string oct_etc_dir()
std::string prepend_octave_exec_home(const std::string &s)
std::string local_ver_oct_file_dir()
std::string local_api_arch_lib_dir()
std::string man1_ext()
std::string libexec_dir()
std::string image_dir()
std::string local_site_defaults_file()
std::string oct_locale_dir()
std::string oct_file_dir()
std::string oct_data_dir()
std::string release()
std::string oct_include_dir()
std::string oct_lib_dir()
std::string data_dir()
std::string arch_lib_dir()
void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
#define DEFUNX(name, fname, args_name, nargout_name, doc)
Macro to define a builtin function with certain internal name.
Definition: defun.h:85
char dir_sep_char()
#define OCTAVE_BINDIR
Definition: main.cc:71
#define OCTAVE_PREFIX
Definition: main.cc:75
#define OCTAVE_ARCHLIBDIR
Definition: main.cc:67
#define OCTAVE_VERSION
Definition: main.cc:63
#define OCTAVE_EXEC_PREFIX
Definition: main.cc:79
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219
#define OCTAVE_CANONICAL_HOST_TYPE
Definition: version.h:53