1 ## Copyright (C) 2005-2012 Bill Denney
2 ## Copyright (C) 2007-2009 Ben Abbott
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {@var{val} =} pathdef ()
22 ## Return the default path for Octave.
23 ## The path information is extracted from one of three sources.
24 ## In order of preference, those are;
27 ## @item @file{~/.octaverc}
29 ## @item @file{<octave-home>/@dots{}/<version>/m/startup/octaverc}
31 ## @item Octave's path prior to changes by any octaverc.
33 ## @seealso{path, addpath, rmpath, genpath, savepath, pathsep}
36 function val = pathdef ()
38 ## Locate the site octaverc file.
39 pathdir = octave_config_info ("localstartupfiledir");
40 site_octaverc = fullfile (pathdir, "octaverc");
42 ## Locate the user ~\.octaverc file.
43 user_octaverc = fullfile ("~", ".octaverc");
45 ## Extract the specified paths from the site and user octaverc"s.
46 site_path = __extractpath__ (site_octaverc);
47 if (exist (user_octaverc, "file"))
48 user_path = __extractpath__ (user_octaverc);
53 ## A path definition in the user octaverc has precedence over the
56 if (! isempty (user_path))
58 elseif (! isempty (site_path))
61 val = __pathorig__ ();
66 ## Extact the path information from the script/function @var{file},
67 ## created by @file{savepath.m}. If @var{file} is omitted,
68 ## @file{~/.octaverc} is used. If successful, @code{__extractpath__}
69 ## returns the path specified in @var{file}.
71 ## Author: Ben Abbott <bpabbott@mac.com>
73 function specifiedpath = __extractpath__ (savefile)
75 ## The majority of this code was borrowed from savepath.m.
76 ## FIXME -- is there some way to share the common parts instead of
79 beginstring = "## Begin savepath auto-created section, do not edit";
80 endstring = "## End savepath auto-created section";
83 savefile = tilde_expand ("~/.octaverc");
86 ## Parse the file if it exists to see if we should replace a section
87 ## or create a section.
91 if (exist (savefile) == 2)
92 ## read in all lines of the file
93 [fid, msg] = fopen (savefile, "rt");
95 error ("__extractpath__: could not open savefile, %s: %s", savefile, msg);
100 result = fgetl (fid);
101 if (isnumeric (result))
102 ## End at the end of file.
106 filelines{linenum} = result;
107 ## Find the first and last lines if they exist in the file.
108 if (strcmp (result, beginstring))
109 startline = linenum + 1;
110 elseif (strcmp (result, endstring))
111 endline = linenum - 1;
115 unwind_protect_cleanup
116 closeread = fclose (fid);
118 error ("__extractpath__: could not close savefile after reading, %s",
124 ## Extract the path specifiation.
125 if (startline > endline || (startline > 0 && endline == 0))
126 error ("__extractpath__: unable to parse file, %s", savefile);
127 elseif (startline > 0)
128 ## Undo doubling of single quote characters performed by savepath.
129 specifiedpath = strrep (regexprep (cstrcat (filelines(startline:endline){:}),
130 " *path *\\('(.*)'\\); *", "$1"),