GNU Octave  8.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-2023 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 get_octave_home (void)
78 {
79  std::string op = OCTAVE_PREFIX;
80 
81  std::string oh = sys::env::getenv ("OCTAVE_HOME");
82 
83  // If OCTAVE_HOME is set in the environment, use that. Otherwise,
84  // default to ${prefix} from configure.
85 
86  return oh.empty () ? op : oh;
87 }
88 
89 static std::string get_octave_exec_home (void)
90 {
91  std::string op = OCTAVE_PREFIX;
92  std::string oep = OCTAVE_EXEC_PREFIX;
93 
94  std::string oh = sys::env::getenv ("OCTAVE_HOME");
95  std::string oeh = sys::env::getenv ("OCTAVE_EXEC_HOME");
96 
97  // If OCTAVE_EXEC_HOME is set in the environment, use that.
98  // Otherwise, if ${prefix} and ${exec_prefix} from configure are set
99  // to the same value, use OCTAVE_HOME from the environment if it is set.
100  // Otherwise, default to ${exec_prefix} from configure.
101 
102  if (! oeh.empty ())
103  return oeh;
104 
105  if (op == oep && ! oh.empty ())
106  return oh;
107 
108  return oep;
109 }
110 
111 static std::string get_local_site_defaults_file (void)
112 {
113  std::string lsf = sys::env::getenv ("OCTAVE_SITE_INITFILE");
114 
115  return lsf.empty () ? local_startupfile_dir () + "/octaverc" : lsf;
116 }
117 
118 static std::string get_site_defaults_file (void)
119 {
120  std::string sf = sys::env::getenv ("OCTAVE_VERSION_INITFILE");
121 
122  return sf.empty () ? startupfile_dir () + "/octaverc" : sf;
123 }
124 
125 std::string prepend_octave_home (const std::string& s)
126 {
127  return prepend_home_dir (octave_home (), s);
128 }
129 
130 std::string prepend_octave_exec_home (const std::string& s)
131 {
132  return prepend_home_dir (octave_exec_home (), s);
133 }
134 
135 std::string canonical_host_type (void)
136 {
137  static const std::string s_canonical_host_type
138  = OCTAVE_CANONICAL_HOST_TYPE;
139 
140  return s_canonical_host_type;
141 }
142 
143 std::string release (void)
144 {
145  static const std::string s_octave_release = OCTAVE_RELEASE;
146 
147  return s_octave_release;
148 }
149 
150 std::string default_pager (void)
151 {
152  static const std::string s_default_pager = OCTAVE_DEFAULT_PAGER;
153 
154  return s_default_pager;
155 }
156 
157 std::string octave_home (void)
158 {
159  static const std::string s_octave_home = get_octave_home ();
160 
161  return s_octave_home;
162 }
163 
164 std::string octave_exec_home (void)
165 {
166  static const std::string s_octave_exec_home = get_octave_exec_home ();
167 
168  return s_octave_exec_home;
169 }
170 
171 std::string bin_dir (void)
172 {
173  static const std::string s_bin_dir
175 
176  return s_bin_dir;
177 }
178 
179 std::string data_dir (void)
180 {
181  static const std::string s_data_dir
182  = prepend_octave_home (OCTAVE_DATADIR);
183 
184  return s_data_dir;
185 }
186 
187 std::string dataroot_dir (void)
188 {
189  static const std::string s_dataroot_dir
190  = prepend_octave_home (OCTAVE_DATAROOTDIR);
191 
192  return s_dataroot_dir;
193 }
194 
195 std::string include_dir (void)
196 {
197  static const std::string s_include_dir
198  = prepend_octave_home (OCTAVE_INCLUDEDIR);
199 
200  return s_include_dir;
201 }
202 
203 std::string lib_dir (void)
204 {
205  static const std::string s_lib_dir
206  = prepend_octave_exec_home (OCTAVE_LIBDIR);
207 
208  return s_lib_dir;
209 }
210 
211 std::string libexec_dir (void)
212 {
213  static const std::string s_libexec_dir
214  = prepend_octave_exec_home (OCTAVE_LIBEXECDIR);
215 
216  return s_libexec_dir;
217 }
218 
219 std::string arch_lib_dir (void)
220 {
221  static const std::string s_arch_lib_dir
223 
224  return s_arch_lib_dir;
225 }
226 
227 std::string info_dir (void)
228 {
229  static const std::string s_info_dir
230  = prepend_octave_exec_home (OCTAVE_INFODIR);
231 
232  return s_info_dir;
233 }
234 
235 std::string local_ver_arch_lib_dir (void)
236 {
237  static const std::string s_local_ver_arch_lib_dir
238  = prepend_octave_exec_home (OCTAVE_LOCALVERARCHLIBDIR);
239 
240  return s_local_ver_arch_lib_dir;
241 }
242 
243 std::string local_api_arch_lib_dir (void)
244 {
245  static const std::string s_local_api_arch_lib_dir
246  = prepend_octave_exec_home (OCTAVE_LOCALAPIARCHLIBDIR);
247 
248  return s_local_api_arch_lib_dir;
249 }
250 
251 std::string local_arch_lib_dir (void)
252 {
253  static const std::string s_local_arch_lib_dir
254  = prepend_octave_exec_home (OCTAVE_LOCALARCHLIBDIR);
255 
256  return s_local_arch_lib_dir;
257 }
258 
259 std::string local_ver_oct_file_dir (void)
260 {
261  static const std::string s_local_ver_oct_file_dir
262  = prepend_octave_exec_home (OCTAVE_LOCALVEROCTFILEDIR);
263 
264  return s_local_ver_oct_file_dir;
265 }
266 
267 std::string local_api_oct_file_dir (void)
268 {
269  static const std::string s_local_api_oct_file_dir
270  = prepend_octave_exec_home (OCTAVE_LOCALAPIOCTFILEDIR);
271 
272  return s_local_api_oct_file_dir;
273 }
274 
275 std::string local_oct_file_dir (void)
276 {
277  static const std::string s_local_oct_file_dir
278  = prepend_octave_exec_home (OCTAVE_LOCALOCTFILEDIR);
279 
280  return s_local_oct_file_dir;
281 }
282 
283 std::string oct_file_dir (void)
284 {
285  static const std::string s_oct_file_dir
286  = prepend_octave_exec_home (OCTAVE_OCTFILEDIR);
287 
288  return s_oct_file_dir;
289 }
290 
291 std::string local_ver_fcn_file_dir (void)
292 {
293  static const std::string s_local_ver_fcn_file_dir
294  = prepend_octave_home (OCTAVE_LOCALVERFCNFILEDIR);
295 
296  return s_local_ver_fcn_file_dir;
297 }
298 
299 std::string local_api_fcn_file_dir (void)
300 {
301  static const std::string s_local_api_fcn_file_dir
302  = prepend_octave_home (OCTAVE_LOCALAPIFCNFILEDIR);
303 
304  return s_local_api_fcn_file_dir;
305 }
306 
307 std::string local_fcn_file_dir (void)
308 {
309  static const std::string s_local_fcn_file_dir
310  = prepend_octave_home (OCTAVE_LOCALFCNFILEDIR);
311 
312  return s_local_fcn_file_dir;
313 }
314 
315 std::string fcn_file_dir (void)
316 {
317  static const std::string s_fcn_file_dir
318  = prepend_octave_home (OCTAVE_FCNFILEDIR);
319 
320  return s_fcn_file_dir;
321 }
322 
323 std::string oct_data_dir (void)
324 {
325  static const std::string s_oct_data_dir
326  = prepend_octave_home (OCTAVE_OCTDATADIR);
327 
328  return s_oct_data_dir;
329 }
330 
331 std::string oct_doc_dir (void)
332 {
333  static const std::string s_oct_doc_dir
334  = prepend_octave_home (OCTAVE_OCTDOCDIR);
335 
336  return s_oct_doc_dir;
337 }
338 
339 std::string oct_etc_dir (void)
340 {
341  static const std::string s_oct_etc_dir
342  = prepend_octave_home (OCTAVE_OCTETCDIR);
343 
344  return s_oct_etc_dir;
345 }
346 
347 std::string oct_fonts_dir (void)
348 {
349  static const std::string s_oct_fonts_dir
350  = prepend_octave_home (OCTAVE_OCTFONTSDIR);
351 
352  return s_oct_fonts_dir;
353 }
354 
355 std::string oct_include_dir (void)
356 {
357  static const std::string s_oct_include_dir
358  = prepend_octave_home (OCTAVE_OCTINCLUDEDIR);
359 
360  return s_oct_include_dir;
361 }
362 
363 std::string oct_lib_dir (void)
364 {
365  static const std::string s_oct_lib_dir
366  = prepend_octave_exec_home (OCTAVE_OCTLIBDIR);
367 
368  return s_oct_lib_dir;
369 }
370 
371 std::string oct_locale_dir (void)
372 {
373  static const std::string s_oct_locale_dir
374  = prepend_octave_home (OCTAVE_OCTLOCALEDIR);
375 
376  return s_oct_locale_dir;
377 }
378 
379 std::string oct_tests_dir (void)
380 {
381  static const std::string s_oct_tests_dir
382  = prepend_octave_home (OCTAVE_OCTTESTSDIR);
383 
384  return s_oct_tests_dir;
385 }
386 
387 std::string man_dir (void)
388 {
389  static const std::string s_man_dir
390  = prepend_octave_home (OCTAVE_MANDIR);
391 
392  return s_man_dir;
393 }
394 
395 std::string man1_dir (void)
396 {
397  static const std::string s_man1_dir
398  = prepend_octave_home (OCTAVE_MAN1DIR);
399 
400  return s_man1_dir;
401 }
402 
403 std::string man1_ext (void)
404 {
405  static const std::string s_man1_ext = OCTAVE_MAN1EXT;
406 
407  return s_man1_ext;
408 }
409 
410 std::string image_dir (void)
411 {
412  static const std::string s_image_dir
413  = prepend_octave_home (OCTAVE_IMAGEDIR);
414 
415  return s_image_dir;
416 }
417 
418 std::string local_startupfile_dir (void)
419 {
420  static const std::string s_local_startupfile_dir
421  = prepend_octave_home (OCTAVE_LOCALSTARTUPFILEDIR);
422 
423  return s_local_startupfile_dir;
424 }
425 
426 std::string startupfile_dir (void)
427 {
428  static const std::string s_startupfile_dir
429  = prepend_octave_home (OCTAVE_STARTUPFILEDIR);
430 
431  return s_startupfile_dir;
432 }
433 
434 std::string local_site_defaults_file (void)
435 {
436  static const std::string s_local_site_defaults_file
438 
439  return s_local_site_defaults_file;
440 }
441 
442 std::string site_defaults_file (void)
443 {
444  static const std::string s_site_defaults_file
446 
447  return s_site_defaults_file;
448 }
449 
450 OCTAVE_END_NAMESPACE(config)
451 
452 DEFUN (OCTAVE_HOME, args, ,
453  doc: /* -*- texinfo -*-
454 @deftypefn {} {@var{dir} =} OCTAVE_HOME ()
455 Return the name of the top-level Octave installation directory.
456 
457 OCTAVE_HOME corresponds to the configuration variable @var{prefix}.
458 @seealso{EXEC_PATH, IMAGE_PATH, OCTAVE_EXEC_HOME}
459 @end deftypefn */)
460 {
461  if (args.length () != 0)
462  print_usage ();
463 
464  return ovl (config::octave_home ());
465 }
466 
467 /*
468 %!assert (ischar (OCTAVE_HOME ()))
469 %!error OCTAVE_HOME (1)
470 */
471 
472 DEFUN (OCTAVE_EXEC_HOME, args, ,
473  doc: /* -*- texinfo -*-
474 @deftypefn {} {@var{dir} =} OCTAVE_EXEC_HOME ()
475 Return the name of the top-level Octave installation directory for
476 architecture-dependent files.
477 
478 If not specified separately, the value is the same as OCTAVE_HOME@.
479 OCTAVE_EXEC_HOME corresponds to the configuration variable @var{exec_prefix}.
480 @seealso{EXEC_PATH, IMAGE_PATH, OCTAVE_HOME}
481 @end deftypefn */)
482 {
483  if (args.length () != 0)
484  print_usage ();
485 
486  return ovl (config::octave_exec_home ());
487 }
488 
489 /*
490 %!assert (ischar (OCTAVE_EXEC_HOME ()))
491 %!error OCTAVE_EXEC_HOME (1)
492 */
493 
494 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, ,
495  doc: /* -*- texinfo -*-
496 @deftypefn {} {@var{verstr} =} OCTAVE_VERSION ()
497 Return the version number of Octave as a string.
498 @seealso{ver, version}
499 @end deftypefn */)
500 {
501  if (args.length () != 0)
502  print_usage ();
503 
504  return ovl (OCTAVE_VERSION);
505 }
506 
507 /*
508 %!assert (ischar (OCTAVE_VERSION ()))
509 %!error OCTAVE_VERSION (1)
510 */
511 
512 DEFUN (user_config_dir, args, ,
513  doc: /* -*- texinfo -*-
514 @deftypefn {} {cfg_dir =} user_config_dir ()
515 Return the (platform-specific) directory for user configuration.
516 @seealso{user_data_dir}
517 @end deftypefn */)
518 {
519  if (args.length () != 0)
520  print_usage ();
521 
522  return ovl (sys::env::get_user_config_directory ());
523 }
524 
525 /*
526 %!assert (ischar (user_config_dir ()))
527 %!error user_config_dir (1)
528 */
529 
530 DEFUN (user_data_dir, args, ,
531  doc: /* -*- texinfo -*-
532 @deftypefn {} {data_dir =} user_data_dir ()
533 Return the (platform-specific) directory for user data.
534 @seealso{user_config_dir}
535 @end deftypefn */)
536 {
537  if (args.length () != 0)
538  print_usage ();
539 
540  return ovl (sys::env::get_user_data_directory ());
541 }
542 
543 /*
544 %!assert (ischar (user_data_dir ()))
545 %!error user_data_dir (1)
546 */
547 
OCTAVE_END_NAMESPACE(octave)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::string local_fcn_file_dir(void)
Definition: defaults.cc:307
std::string oct_tests_dir(void)
Definition: defaults.cc:379
std::string local_ver_oct_file_dir(void)
Definition: defaults.cc:259
std::string canonical_host_type(void)
Definition: defaults.cc:135
static std::string get_octave_home(void)
Definition: defaults.cc:77
std::string local_api_fcn_file_dir(void)
Definition: defaults.cc:299
std::string man1_dir(void)
Definition: defaults.cc:395
std::string startupfile_dir(void)
Definition: defaults.cc:426
std::string site_defaults_file(void)
Definition: defaults.cc:442
std::string default_pager(void)
Definition: defaults.cc:150
std::string oct_doc_dir(void)
Definition: defaults.cc:331
std::string octave_exec_home(void)
Definition: defaults.cc:164
std::string image_dir(void)
Definition: defaults.cc:410
std::string local_oct_file_dir(void)
Definition: defaults.cc:275
std::string octave_home(void)
Definition: defaults.cc:157
std::string fcn_file_dir(void)
Definition: defaults.cc:315
std::string prepend_octave_home(const std::string &s)
Definition: defaults.cc:125
std::string man_dir(void)
Definition: defaults.cc:387
std::string include_dir(void)
Definition: defaults.cc:195
std::string oct_locale_dir(void)
Definition: defaults.cc:371
std::string oct_file_dir(void)
Definition: defaults.cc:283
std::string data_dir(void)
Definition: defaults.cc:179
OCTAVE_EXPORT octave_value_list FOCTAVE_VERSION(const octave_value_list &args, int)
Definition: defaults.cc:499
std::string dataroot_dir(void)
Definition: defaults.cc:187
std::string man1_ext(void)
Definition: defaults.cc:403
static std::string get_site_defaults_file(void)
Definition: defaults.cc:118
std::string libexec_dir(void)
Definition: defaults.cc:211
std::string local_startupfile_dir(void)
Definition: defaults.cc:418
std::string info_dir(void)
Definition: defaults.cc:227
std::string oct_etc_dir(void)
Definition: defaults.cc:339
std::string local_arch_lib_dir(void)
Definition: defaults.cc:251
std::string local_ver_arch_lib_dir(void)
Definition: defaults.cc:235
std::string lib_dir(void)
Definition: defaults.cc:203
std::string local_api_arch_lib_dir(void)
Definition: defaults.cc:243
std::string oct_include_dir(void)
Definition: defaults.cc:355
std::string prepend_octave_exec_home(const std::string &s)
Definition: defaults.cc:130
std::string release(void)
Definition: defaults.cc:143
std::string oct_data_dir(void)
Definition: defaults.cc:323
static std::string get_octave_exec_home(void)
Definition: defaults.cc:89
std::string bin_dir(void)
Definition: defaults.cc:171
std::string local_site_defaults_file(void)
Definition: defaults.cc:434
std::string local_api_oct_file_dir(void)
Definition: defaults.cc:267
std::string local_ver_fcn_file_dir(void)
Definition: defaults.cc:291
std::string arch_lib_dir(void)
Definition: defaults.cc:219
static std::string prepend_home_dir(const std::string &hd, const std::string &s)
Definition: defaults.cc:62
static std::string get_local_site_defaults_file(void)
Definition: defaults.cc:111
std::string oct_lib_dir(void)
Definition: defaults.cc:363
std::string oct_fonts_dir(void)
Definition: defaults.cc:347
OCTINTERP_API 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
#define OCTAVE_BINDIR
Definition: main.in.cc:71
#define OCTAVE_PREFIX
Definition: main.in.cc:75
#define OCTAVE_ARCHLIBDIR
Definition: main.in.cc:67
#define OCTAVE_VERSION
Definition: main.in.cc:63
#define OCTAVE_EXEC_PREFIX
Definition: main.in.cc:79
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
static bool absolute_pathname(const std::string &s)
Definition: shared-fcns.h:148
static const char dir_sep_char
Definition: shared-fcns.h:90