1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 
50 namespace octave
51 {
52   namespace config
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
prepend_home_dir(const std::string & hd,const std::string & s)62     prepend_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 
get_octave_home(void)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 
get_octave_exec_home(void)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 
get_local_site_defaults_file(void)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 
get_site_defaults_file(void)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 
prepend_octave_home(const std::string & s)125     std::string prepend_octave_home (const std::string& s)
126     {
127       return prepend_home_dir (octave_home (), s);
128     }
129 
prepend_octave_exec_home(const std::string & s)130     std::string prepend_octave_exec_home (const std::string& s)
131     {
132       return prepend_home_dir (octave_exec_home (), s);
133     }
134 
canonical_host_type(void)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 
release(void)143     std::string release (void)
144     {
145       static const std::string s_octave_release = OCTAVE_RELEASE;
146 
147       return s_octave_release;
148     }
149 
default_pager(void)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 
octave_home(void)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 
octave_exec_home(void)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 
bin_dir(void)171     std::string bin_dir (void)
172     {
173       static const std::string s_bin_dir
174         = prepend_octave_exec_home (OCTAVE_BINDIR);
175 
176       return s_bin_dir;
177     }
178 
data_dir(void)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 
dataroot_dir(void)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 
include_dir(void)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 
lib_dir(void)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 
libexec_dir(void)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 
arch_lib_dir(void)219     std::string arch_lib_dir (void)
220     {
221       static const std::string s_arch_lib_dir
222         = prepend_octave_exec_home (OCTAVE_ARCHLIBDIR);
223 
224       return s_arch_lib_dir;
225     }
226 
info_dir(void)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 
local_ver_arch_lib_dir(void)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 
local_api_arch_lib_dir(void)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 
local_arch_lib_dir(void)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 
local_ver_oct_file_dir(void)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 
local_api_oct_file_dir(void)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 
local_oct_file_dir(void)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 
oct_file_dir(void)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 
local_ver_fcn_file_dir(void)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 
local_api_fcn_file_dir(void)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 
local_fcn_file_dir(void)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 
fcn_file_dir(void)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 
oct_data_dir(void)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 
oct_doc_dir(void)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 
oct_etc_dir(void)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 
oct_fonts_dir(void)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 
oct_include_dir(void)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 
oct_lib_dir(void)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 
oct_locale_dir(void)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 
oct_tests_dir(void)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 
man_dir(void)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 
man1_dir(void)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 
man1_ext(void)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 
image_dir(void)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 
local_startupfile_dir(void)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 
startupfile_dir(void)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 
local_site_defaults_file(void)434     std::string local_site_defaults_file (void)
435     {
436       static const std::string s_local_site_defaults_file
437         = get_local_site_defaults_file ();
438 
439       return s_local_site_defaults_file;
440     }
441 
site_defaults_file(void)442     std::string site_defaults_file (void)
443     {
444       static const std::string s_site_defaults_file
445         = get_site_defaults_file ();
446 
447       return s_site_defaults_file;
448     }
449   }
450 }
451 
452 
453 DEFUN (OCTAVE_HOME, args, ,
454        doc: /* -*- texinfo -*-
455 @deftypefn {} {} OCTAVE_HOME ()
456 Return the name of the top-level Octave installation directory.
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 (octave::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 {} {} OCTAVE_EXEC_HOME ()
475 Return the name of the top-level Octave installation directory for
476 architecture-dependent files.  If not specified separately, the value
477 is the same as OCTAVE_HOME@.  OCTAVE_EXEC_HOME corresponds to the
478 configuration variable @var{exec_prefix}.
479 @seealso{EXEC_PATH, IMAGE_PATH, OCTAVE_HOME}
480 @end deftypefn */)
481 {
482   if (args.length () != 0)
483     print_usage ();
484 
485   return ovl (octave::config::octave_exec_home ());
486 }
487 
488 /*
489 %!assert (ischar (OCTAVE_EXEC_HOME ()))
490 %!error OCTAVE_EXEC_HOME (1)
491 */
492 
493 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, ,
494         doc: /* -*- texinfo -*-
495 @deftypefn {} {} OCTAVE_VERSION ()
496 Return the version number of Octave as a string.
497 @seealso{ver, version}
498 @end deftypefn */)
499 {
500   if (args.length () != 0)
501     print_usage ();
502 
503   return ovl (OCTAVE_VERSION);
504 }
505 
506 /*
507 %!assert (ischar (OCTAVE_VERSION ()))
508 %!error OCTAVE_VERSION (1)
509 */
510