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