defaults.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <cstdlib>
00028 
00029 #include <algorithm>
00030 #include <iostream>
00031 #include <string>
00032 
00033 #include <sys/types.h>
00034 #include <unistd.h>
00035 
00036 #include "dir-ops.h"
00037 #include "oct-env.h"
00038 #include "file-stat.h"
00039 #include "pathsearch.h"
00040 #include "str-vec.h"
00041 
00042 #include <defaults.h>
00043 #include "defun.h"
00044 #include "error.h"
00045 #include "file-ops.h"
00046 #include "gripes.h"
00047 #include "help.h"
00048 #include "input.h"
00049 #include "load-path.h"
00050 #include "oct-obj.h"
00051 #include "ov.h"
00052 #include "parse.h"
00053 #include "toplev.h"
00054 #include "unwind-prot.h"
00055 #include "variables.h"
00056 #include <version.h>
00057 
00058 std::string Voctave_home;
00059 
00060 std::string Vbin_dir;
00061 std::string Vinfo_dir;
00062 std::string Vdata_dir;
00063 std::string Vlibexec_dir;
00064 std::string Varch_lib_dir;
00065 std::string Vlocal_arch_lib_dir;
00066 std::string Vlocal_api_arch_lib_dir;
00067 std::string Vlocal_ver_arch_lib_dir;
00068 
00069 std::string Vlocal_ver_oct_file_dir;
00070 std::string Vlocal_api_oct_file_dir;
00071 std::string Vlocal_oct_file_dir;
00072 
00073 std::string Vlocal_ver_fcn_file_dir;
00074 std::string Vlocal_api_fcn_file_dir;
00075 std::string Vlocal_fcn_file_dir;
00076 
00077 std::string Voct_file_dir;
00078 std::string Vfcn_file_dir;
00079 
00080 std::string Vimage_dir;
00081 
00082 // The path that will be searched for programs that we execute.
00083 // (--exec-path path)
00084 static std::string VEXEC_PATH;
00085 
00086 // Name of the editor to be invoked by the edit_history command.
00087 std::string VEDITOR;
00088 
00089 static std::string VIMAGE_PATH;
00090 
00091 std::string Vlocal_site_defaults_file;
00092 std::string Vsite_defaults_file;
00093 
00094 std::string
00095 subst_octave_home (const std::string& s)
00096 {
00097   std::string retval;
00098 
00099   std::string prefix = OCTAVE_PREFIX;
00100 
00101   retval = s;
00102 
00103   if (Voctave_home != prefix)
00104     {
00105       octave_idx_type len = prefix.length ();
00106 
00107       if (s.substr (0, len) == prefix)
00108         retval.replace (0, len, Voctave_home);
00109     }
00110 
00111   if (file_ops::dir_sep_char () != '/')
00112     std::replace (retval.begin (), retval.end (), '/',
00113                   file_ops::dir_sep_char ());
00114 
00115   return retval;
00116 }
00117 
00118 static void
00119 set_octave_home (void)
00120 {
00121   std::string oh = octave_env::getenv ("OCTAVE_HOME");
00122 
00123   Voctave_home = oh.empty () ? std::string (OCTAVE_PREFIX) : oh;
00124 }
00125 
00126 static void
00127 set_default_info_dir (void)
00128 {
00129   Vinfo_dir = subst_octave_home (OCTAVE_INFODIR);
00130 }
00131 
00132 static void
00133 set_default_data_dir (void)
00134 {
00135   Vdata_dir = subst_octave_home (OCTAVE_DATADIR);
00136 }
00137 
00138 static void
00139 set_default_libexec_dir (void)
00140 {
00141   Vlibexec_dir = subst_octave_home (OCTAVE_LIBEXECDIR);
00142 }
00143 
00144 static void
00145 set_default_arch_lib_dir (void)
00146 {
00147   Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR);
00148 }
00149 
00150 static void
00151 set_default_local_arch_lib_dir (void)
00152 {
00153   Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR);
00154 }
00155 
00156 static void
00157 set_default_local_api_arch_lib_dir (void)
00158 {
00159   Vlocal_api_arch_lib_dir = subst_octave_home (OCTAVE_LOCALAPIARCHLIBDIR);
00160 }
00161 
00162 static void
00163 set_default_local_ver_arch_lib_dir (void)
00164 {
00165   Vlocal_ver_arch_lib_dir = subst_octave_home (OCTAVE_LOCALVERARCHLIBDIR);
00166 }
00167 
00168 static void
00169 set_default_local_ver_oct_file_dir (void)
00170 {
00171   Vlocal_ver_oct_file_dir = subst_octave_home (OCTAVE_LOCALVEROCTFILEDIR);
00172 }
00173 
00174 static void
00175 set_default_local_api_oct_file_dir (void)
00176 {
00177   Vlocal_api_oct_file_dir = subst_octave_home (OCTAVE_LOCALAPIOCTFILEDIR);
00178 }
00179 
00180 static void
00181 set_default_local_oct_file_dir (void)
00182 {
00183   Vlocal_oct_file_dir = subst_octave_home (OCTAVE_LOCALOCTFILEDIR);
00184 }
00185 
00186 static void
00187 set_default_local_ver_fcn_file_dir (void)
00188 {
00189   Vlocal_ver_fcn_file_dir = subst_octave_home (OCTAVE_LOCALVERFCNFILEDIR);
00190 }
00191 
00192 static void
00193 set_default_local_api_fcn_file_dir (void)
00194 {
00195   Vlocal_api_fcn_file_dir = subst_octave_home (OCTAVE_LOCALAPIFCNFILEDIR);
00196 }
00197 
00198 static void
00199 set_default_local_fcn_file_dir (void)
00200 {
00201   Vlocal_fcn_file_dir = subst_octave_home (OCTAVE_LOCALFCNFILEDIR);
00202 }
00203 
00204 static void
00205 set_default_fcn_file_dir (void)
00206 {
00207   Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR);
00208 }
00209 
00210 static void
00211 set_default_image_dir (void)
00212 {
00213   Vimage_dir = subst_octave_home (OCTAVE_IMAGEDIR);
00214 }
00215 
00216 static void
00217 set_default_oct_file_dir (void)
00218 {
00219   Voct_file_dir = subst_octave_home (OCTAVE_OCTFILEDIR);
00220 }
00221 
00222 static void
00223 set_default_bin_dir (void)
00224 {
00225   Vbin_dir = subst_octave_home (OCTAVE_BINDIR);
00226 }
00227 
00228 void
00229 set_exec_path (const std::string& path_arg)
00230 {
00231   std::string tpath = path_arg;
00232 
00233   if (tpath.empty ())
00234     tpath = octave_env::getenv ("OCTAVE_EXEC_PATH");
00235 
00236   if (tpath.empty ())
00237     tpath = Vlocal_ver_arch_lib_dir + dir_path::path_sep_str ()
00238       + Vlocal_api_arch_lib_dir + dir_path::path_sep_str ()
00239       + Vlocal_arch_lib_dir + dir_path::path_sep_str ()
00240       + Varch_lib_dir + dir_path::path_sep_str ()
00241       + Vbin_dir;
00242 
00243   VEXEC_PATH = tpath;
00244 
00245   // FIXME -- should we really be modifying PATH in the environment?
00246   // The way things are now, Octave will ignore directories set in the
00247   // PATH with calls like
00248   //
00249   //   setenv ("PATH", "/my/path");
00250   //
00251   // To fix this, I think Octave should be searching the combination of
00252   // PATH and EXEC_PATH for programs that it executes instead of setting
00253   // the PATH in the environment and relying on the shell to do the
00254   // searching.
00255 
00256   // This is static so that even if set_exec_path is called more than
00257   // once, shell_path is the original PATH from the environment,
00258   // before we start modifying it.
00259   static std::string shell_path = octave_env::getenv ("PATH");
00260 
00261   if (! shell_path.empty ())
00262     tpath = shell_path + dir_path::path_sep_str () + tpath;
00263 
00264   octave_env::putenv ("PATH", tpath);
00265 }
00266 
00267 void
00268 set_image_path (const std::string& path)
00269 {
00270   VIMAGE_PATH = ".";
00271 
00272   std::string tpath = path;
00273 
00274   if (tpath.empty ())
00275     tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH");
00276 
00277   if (! tpath.empty ())
00278     VIMAGE_PATH += dir_path::path_sep_str () + tpath;
00279 
00280   tpath = genpath (Vimage_dir, "");
00281 
00282   if (! tpath.empty ())
00283     VIMAGE_PATH += dir_path::path_sep_str () + tpath;
00284 }
00285 
00286 static void
00287 set_default_doc_cache_file (void)
00288 {
00289   std::string def_file = subst_octave_home (OCTAVE_DOC_CACHE_FILE);
00290 
00291   std::string env_file = octave_env::getenv ("OCTAVE_DOC_CACHE_FILE");
00292 
00293   Vdoc_cache_file = env_file.empty () ? def_file : env_file;
00294 }
00295 
00296 static void
00297 set_default_info_file (void)
00298 {
00299   std::string std_info_file = subst_octave_home (OCTAVE_INFOFILE);
00300 
00301   std::string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE");
00302 
00303   Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file;
00304 }
00305 
00306 static void
00307 set_default_info_prog (void)
00308 {
00309   std::string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM");
00310 
00311   if (oct_info_prog.empty ())
00312     Vinfo_program = "info";
00313   else
00314     Vinfo_program = std::string (oct_info_prog);
00315 }
00316 
00317 static void
00318 set_default_editor (void)
00319 {
00320   VEDITOR = "emacs";
00321 
00322   std::string env_editor = octave_env::getenv ("EDITOR");
00323 
00324   if (! env_editor.empty ())
00325     VEDITOR = env_editor;
00326 }
00327 
00328 static void
00329 set_local_site_defaults_file (void)
00330 {
00331   std::string lsf = octave_env::getenv ("OCTAVE_SITE_INITFILE");
00332 
00333   if (lsf.empty ())
00334     {
00335       Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR);
00336       Vlocal_site_defaults_file.append ("/octaverc");
00337     }
00338   else
00339     Vlocal_site_defaults_file = lsf;
00340 }
00341 
00342 static void
00343 set_site_defaults_file (void)
00344 {
00345   std::string sf = octave_env::getenv ("OCTAVE_VERSION_INITFILE");
00346 
00347   if (sf.empty ())
00348     {
00349       Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR);
00350       Vsite_defaults_file.append ("/octaverc");
00351     }
00352   else
00353     Vsite_defaults_file = sf;
00354 }
00355 
00356 void
00357 install_defaults (void)
00358 {
00359   // OCTAVE_HOME must be set first!
00360 
00361   set_octave_home ();
00362 
00363   set_default_info_dir ();
00364 
00365   set_default_data_dir ();
00366 
00367   set_default_libexec_dir ();
00368 
00369   set_default_arch_lib_dir ();
00370 
00371   set_default_local_ver_arch_lib_dir ();
00372   set_default_local_api_arch_lib_dir ();
00373   set_default_local_arch_lib_dir ();
00374 
00375   set_default_local_ver_oct_file_dir ();
00376   set_default_local_api_oct_file_dir ();
00377   set_default_local_oct_file_dir ();
00378 
00379   set_default_local_ver_fcn_file_dir ();
00380   set_default_local_api_fcn_file_dir ();
00381   set_default_local_fcn_file_dir ();
00382 
00383   set_default_fcn_file_dir ();
00384   set_default_oct_file_dir ();
00385 
00386   set_default_image_dir ();
00387 
00388   set_default_bin_dir ();
00389 
00390   set_exec_path ();
00391 
00392   set_image_path ();
00393 
00394   set_default_doc_cache_file ();
00395 
00396   set_default_info_file ();
00397 
00398   set_default_info_prog ();
00399 
00400   set_default_editor ();
00401 
00402   set_local_site_defaults_file ();
00403 
00404   set_site_defaults_file ();
00405 }
00406 
00407 DEFUN (EDITOR, args, nargout,
00408   "-*- texinfo -*-\n\
00409 @deftypefn  {Built-in Function} {@var{val} =} EDITOR ()\n\
00410 @deftypefnx {Built-in Function} {@var{old_val} =} EDITOR (@var{new_val})\n\
00411 @deftypefnx {Built-in Function} {} EDITOR (@var{new_val}, \"local\")\n\
00412 Query or set the internal variable that specifies the editor to\n\
00413 use with the @code{edit_history} command.  The default value is taken from\n\
00414 the environment variable @w{@env{EDITOR}} when Octave starts.  If the\n\
00415 environment variable is not initialized, @w{@env{EDITOR}} will be set to\n\
00416 @code{\"emacs\"}.\n\
00417 \n\
00418 When called from inside a function with the \"local\" option, the variable is\n\
00419 changed locally for the function and any subroutines it calls.  The original\n\
00420 variable value is restored when exiting the function.\n\
00421 @seealso{edit_history}\n\
00422 @end deftypefn")
00423 {
00424   return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (EDITOR);
00425 }
00426 
00427 /*
00428 %!error (EDITOR (1, 2));
00429 %!test
00430 %! orig_val = EDITOR ();
00431 %! old_val = EDITOR ("X");
00432 %! assert (orig_val, old_val);
00433 %! assert (EDITOR (), "X");
00434 %! EDITOR (orig_val);
00435 %! assert (EDITOR (), orig_val);
00436 */
00437 
00438 DEFUN (EXEC_PATH, args, nargout,
00439   "-*- texinfo -*-\n\
00440 @deftypefn  {Built-in Function} {@var{val} =} EXEC_PATH ()\n\
00441 @deftypefnx {Built-in Function} {@var{old_val} =} EXEC_PATH (@var{new_val})\n\
00442 @deftypefnx {Built-in Function} {} EXEC_PATH (@var{new_val}, \"local\")\n\
00443 Query or set the internal variable that specifies a colon separated\n\
00444 list of directories to append to the shell PATH when executing external\n\
00445 programs.  The initial value of is taken from the environment variable\n\
00446 @w{@env{OCTAVE_EXEC_PATH}}, but that value can be overridden by\n\
00447 the command line argument @option{--exec-path PATH}.\n\
00448 \n\
00449 When called from inside a function with the \"local\" option, the variable is\n\
00450 changed locally for the function and any subroutines it calls.  The original\n\
00451 variable value is restored when exiting the function.\n\
00452 @end deftypefn")
00453 {
00454   octave_value retval = SET_NONEMPTY_INTERNAL_STRING_VARIABLE (EXEC_PATH);
00455 
00456   if (args.length () > 0)
00457     set_exec_path (VEXEC_PATH);
00458 
00459   return retval;
00460 }
00461 
00462 /*
00463 %!error (EXEC_PATH (1, 2));
00464 %!test
00465 %! orig_val = EXEC_PATH ();
00466 %! old_val = EXEC_PATH ("X");
00467 %! assert (orig_val, old_val);
00468 %! assert (EXEC_PATH (), "X");
00469 %! EXEC_PATH (orig_val);
00470 %! assert (EXEC_PATH (), orig_val);
00471 */
00472 
00473 DEFUN (IMAGE_PATH, args, nargout,
00474   "-*- texinfo -*-\n\
00475 @deftypefn  {Built-in Function} {@var{val} =} IMAGE_PATH ()\n\
00476 @deftypefnx {Built-in Function} {@var{old_val} =} IMAGE_PATH (@var{new_val})\n\
00477 @deftypefnx {Built-in Function} {} IMAGE_PATH (@var{new_val}, \"local\")\n\
00478 Query or set the internal variable that specifies a colon separated\n\
00479 list of directories in which to search for image files.\n\
00480 \n\
00481 When called from inside a function with the \"local\" option, the variable is\n\
00482 changed locally for the function and any subroutines it calls.  The original\n\
00483 variable value is restored when exiting the function.\n\
00484 @end deftypefn")
00485 {
00486   return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (IMAGE_PATH);
00487 }
00488 
00489 /*
00490 %!error (IMAGE_PATH (1, 2));
00491 %!test
00492 %! orig_val = IMAGE_PATH ();
00493 %! old_val = IMAGE_PATH ("X");
00494 %! assert (orig_val, old_val);
00495 %! assert (IMAGE_PATH (), "X");
00496 %! IMAGE_PATH (orig_val);
00497 %! assert (IMAGE_PATH (), orig_val);
00498 */
00499 
00500 DEFUN (OCTAVE_HOME, args, ,
00501   "-*- texinfo -*-\n\
00502 @deftypefn {Built-in Function} {} OCTAVE_HOME ()\n\
00503 Return the name of the top-level Octave installation directory.\n\
00504 @end deftypefn")
00505 {
00506   octave_value retval;
00507 
00508   if (args.length () == 0)
00509     retval = Voctave_home;
00510   else
00511     print_usage ();
00512 
00513   return retval;
00514 }
00515 
00516 /*
00517 %!error OCTAVE_HOME (1);
00518 %!assert (ischar (OCTAVE_HOME ()));
00519 */
00520 
00521 DEFUNX ("OCTAVE_VERSION", FOCTAVE_VERSION, args, ,
00522   "-*- texinfo -*-\n\
00523 @deftypefn {Built-in Function} {} OCTAVE_VERSION ()\n\
00524 Return the version number of Octave, as a string.\n\
00525 @end deftypefn")
00526 {
00527   octave_value retval;
00528 
00529   int nargin = args.length ();
00530 
00531   if (nargin == 0)
00532     retval = OCTAVE_VERSION;
00533   else
00534     print_usage ();
00535 
00536   return retval;
00537 }
00538 
00539 /*
00540 %!error OCTAVE_VERSION (1);
00541 %!assert (ischar (OCTAVE_VERSION ()));
00542 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines