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