# HG changeset patch # User jwe # Date 1147291810 0 # Node ID 296cefb48d7ed9c6d357c34a718887e5c106dd97 # Parent c86a550a91c0eba7f6a4f3b3f106c1ddc5653694 [project @ 2006-05-10 20:10:10 by jwe] diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,5 +1,8 @@ 2006-05-10 John W. Eaton + * path/addpath.m, path/rmpath.m: Improve compatibility. + * path/setpath.m: Delete. + * pkg/pkg.m: New file. 2006-05-09 Keith Goodman diff --git a/scripts/path/addpath.m b/scripts/path/addpath.m --- a/scripts/path/addpath.m +++ b/scripts/path/addpath.m @@ -18,22 +18,13 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} addpath (@var{dir1}, @dots{}) -## Prepend @var{dir1}, @dots{} to the current @code{LOADPATH}. -## If the directory is already in the path, it is moved to the specified -## location, prepending by default. -## -## @example -## addpath (dir1, "-end", dir2, "-begin", dir3, "-END", dir4, "-BEGIN", dir5) -## @result{} Prepend dir1, dir3 and dir5 and append dir2 and dir4. -## @end example -## -## An error will be returned if the string is not a directory, the -## directory doesn't exist or you don't have read access to it. -## -## BUG: This function can't add directories called @samp{-end} or -## @samp{-begin} (case insensitively). -## -## @seealso{LOADPATH, rmpath, savepath, setpath} +## @deftypefnx {Function File} {} addpath (@var{dir1}, @dots{}, @var{option}) +## Add @var{dir1}, @dots{} to the current @code{LOADPATH}. If +## @var{option} is @samp{"-begin"} or 0 (the default), prepend the +## directory name to the current path. If @var{option} is @samp{"-end"} +## or 1, append the directory name to the current path. +## Directories added to the path must exist. +## @seealso{path, rmpath, savepath, pathsep} ## @end deftypefn ## Author: Etienne Grossmann @@ -44,86 +35,78 @@ function ret = addpath (varargin) if (nargout > 0) - path = varargin{1}; - varargin = varargin(2:end); - else - path = LOADPATH; + ret = path (); endif - dir = ""; - if (length (varargin) > 0) - append = 0; - switch varargin{end} - case { 0, "0", "-begin", "-BEGIN" } - varargin = varargin(1:end-1); - case { 1, "1", "-end", "-END" } - varargin = varargin(1:end-1); - append = 1; - endswitch + nargs = nargin (); + + if (nargs > 0) - psep = pathsep(); + append = false; + option = varargin{end}; + if (ischar (option)) + if (strcmpi (option, "-end")) + append = true; + nargs--; + elseif (strcmpi (option, "-begin")) + nargs--; + endif + elseif (option == 1) + append = true; + endif - ## Avoid duplicates by stripping pre-existing entries - path = rmpath (path, varargin{:}); + psep = pathsep (); - ## Check if the directories are valid - for arg = 1:length (varargin) - p = varargin{arg}; - if (nargout == 0 && ! isempty (p)) - [s, err, m] = stat (p); - if (err != 0) - warning ("addpath %s : %s\n", p, m); + xpath = cellstr (split (path (), psep)); + n_path_elts = length (xpath); + for i = 1:n_path_elts + tmp = xpath{i}; + tmp = regexprep (tmp, "//+", "/"); + tmp = regexprep (tmp, "/$", ""); + xpath{i,1} = xpath{i}; + xpath{i,2} = tmp; + endfor + + for i = 1:nargs + dir_elts = cellstr (split (varargin{i}, psep)); + n_dir_elts = length (dir_elts); + for j = 1:n_dir_elts + dir = regexprep (dir_elts{j}, "//+", "/"); + dir = regexprep (dir, "/$", ""); + [s, status, msg] = stat (dir); + if (status != 0) + warning ("addpath: %s: %s", dir, msg); continue; - elseif (index (s.modestr, "d") != 1) - warning ("addpath %s : not a directory (mode=%s)\n", p, s.modestr); - continue; - elseif (! (s.modestr(8) == "r" - || (getgid == s.gid && s.modestr(5) == "r") - || (getuid == s.uid && s.modestr(2) == "r"))) - warning ("addpath %s : not readable (mode=%s)\n", p, s.modestr); + elseif (! S_ISDIR (s.mode)) + warning ("addpath: %s: not a directory", dir); continue; endif - endif - dir = sprintf ("%s%s%s", dir, psep, p); + elt_found = false; + for k = n_path_elts:-1:1 + if (strcmp (dir, xpath{k,2})) + xpath(k,:) = []; + n_path_elts--; + elt_found = true; + endif + endfor + if (append) + xpath = [xpath; {dir_elts{j}, dir}]; + else + xpath = [{dir_elts{j}, dir}; xpath]; + endif + endfor endfor - - ## Add the directories to the current path - if (! isempty (dir)) - dir = dir(2:end); - if (isempty (path) && ! isempty (dir)) - path = dir; - else - if strcmp (path, psep), path = ""; end - if append - path = sprintf ("%s%s%s", path, psep, dir); - else - path = sprintf ("%s%s%s", dir, psep, path); - endif - endif - endif - endif - if nargout - ret = path; - else - LOADPATH = path; - endif + xpath{:,2} = psep; + xpath = xpath'; -endfunction + tmp = strcat (xpath{:}); + tmp(end) = ""; -%!assert(addpath('','hello'),'hello'); -%!assert(addpath('','hello','world'),['hello',pathsep(),'world']) -%!assert(addpath(pathsep(),'hello'),['hello',pathsep()]); -%!assert(addpath(pathsep(),'hello','-end'),[pathsep(),'hello']); -%!assert(addpath('hello','hello'),'hello'); -%!assert(addpath('hello','world'),['world',pathsep(),'hello']) -%!assert(addpath('hello','world','-end'),['hello',pathsep(),'world']) -%!assert(addpath(['hello',pathsep()],'world','-end'),['hello',pathsep(),pathsep(),'world']) -%!assert(addpath(['hello',pathsep()],'hello','world','-end'),[pathsep(),'hello',pathsep(),'world']) + tmp = strrep (tmp, DEFAULT_LOADPATH (), ""); + + path (tmp); -%!assert(addpath('',''),pathsep()) -%!assert(addpath(pathsep(),''),pathsep()) -%!assert(addpath('hello',''),[pathsep(),'hello']) -%!assert(addpath(['hello',pathsep(),'world'],''),[pathsep(),'hello',pathsep(),'world']) -%!assert(addpath(['hello',pathsep(),'world',pathsep()],''),[pathsep(),'hello',pathsep(),'world']) -%!assert(addpath(['hello',pathsep(),pathsep(),'world'],''),[pathsep(),'hello',pathsep(),'world']) + endif + +endfunction diff --git a/scripts/path/path.m b/scripts/path/path.m --- a/scripts/path/path.m +++ b/scripts/path/path.m @@ -32,7 +32,7 @@ ## and also return it. ## ## No checks are made for duplicate elements. -## @seealso{pathsep} +## @seealso{addpath, rmpath, savepath, pathsep} ## @end deftypefn ## Author: jwe diff --git a/scripts/path/rmpath.m b/scripts/path/rmpath.m --- a/scripts/path/rmpath.m +++ b/scripts/path/rmpath.m @@ -18,7 +18,7 @@ ## @deftypefn {Function File} {} rmpath (@var{dir1}, @dots{}) ## Remove @var{dir1}, @dots{} from the current @code{LOADPATH}. ## -## @seealso{LOADPATH, addpath, savepath, setpath} +## @seealso{path, addpath, savepath, pathsep} ## @end deftypefn ## Author: Etienne Grossmann @@ -27,97 +27,50 @@ function ret = rmpath (varargin) - if (nargout == 0) - path = LOADPATH; - else - path = varargin{1}; + if (nargout > 0) + ret = path (); endif - psep = pathsep(); - - strip_system_path = 0; - for arg = nargout + 1:length (varargin) - p = varargin{arg}; - lp = length (p); - - ## "" is the system path - if (lp == 0) - strip_system_path = 1; - endif - - ## strip "...:p:..." -> "...:..." - lo = 0 ; - while (lo != length (path)) # Loop while I can substitute - lo = length (path); - path = strrep (path, sprintf("%s%s%s", psep, p, psep), psep); - endwhile + psep = pathsep (); - ## strip "p:..." and "...:p" -> "..." - if (length (path) > lp+1 && - strcmp (path(1:lp+1), sprintf ("%s%s", p, psep))) - path = path(lp+2:end); - endif - if (length (path) > lp+1 && - strcmp (path(end-lp:end), sprintf ("%s%s", psep, p))) - path = path(1:end-lp-1); - endif - - ## strip "p:" and ":p" -> ":" - if (length (path) == lp+1 - && (strcmp (path, sprintf ("%s%s", p, psep)) - || strcmp (path, sprintf ("%s%s", psep, p)))) - path = psep; - endif - - ## strip "p" -> "" - if (length (path) == lp && strcmp (path, p)) - path = ""; - endif - + xpath = cellstr (split (path (), psep)); + n_path_elts = length (xpath); + for i = 1:n_path_elts + tmp = xpath{i}; + tmp = regexprep (tmp, "//+", "/"); + tmp = regexprep (tmp, "/$", ""); + xpath{i,1} = xpath{i}; + xpath{i,2} = tmp; endfor - if (strip_system_path && strcmp (path, psep)) - path = ""; - endif + for i = 1:nargin + dir_elts = cellstr (split (varargin{i}, psep)); + n_dir_elts = length (dir_elts); + for j = 1:n_dir_elts + dir = regexprep (dir_elts{j}, "//+", "/"); + dir = regexprep (dir, "/$", ""); + elt_found = false; + for k = n_path_elts:-1:1 + if (strcmp (dir, xpath{k,2})) + xpath(k,:) = []; + n_path_elts--; + elt_found = true; + endif + endfor + if (! elt_found) + warning ("rmpath: %s: not found", dir); + endif + endfor + endfor - if (nargout > 0) - ret = path; - elseif (! strcmp (LOADPATH, path)) - LOADPATH = path; - endif + xpath{:,2} = psep; + xpath = xpath'; + + tmp = strcat (xpath{:}); + tmp(end) = ""; + + tmp = strrep (tmp, DEFAULT_LOADPATH (), ""); + + path (tmp); endfunction - -%!assert(rmpath(pathsep(),''),''); -%!assert(rmpath(['hello',pathsep()],''),'hello'); -%!assert(rmpath(['hello',pathsep(),'world'],''),['hello',pathsep(),'world']); -%!assert(rmpath([pathsep(),'hello',pathsep(),'world'],''),['hello',pathsep(),'world']); -%!assert(rmpath([pathsep(),'hello',pathsep(),'world',pathsep()],''),['hello',pathsep(),'world']); -%!assert(rmpath([pathsep(),'hello',pathsep(),pathsep(),'world',pathsep()],''),['hello',pathsep(),'world']); - -%!assert(rmpath('hello','hello'),''); -%!assert(rmpath([pathsep,'hello'],'hello'),pathsep()); -%!assert(rmpath(['hello',pathsep()],'hello'),pathsep()); -%!assert(rmpath(['hello',pathsep(),'hello'],'hello'),''); -%!assert(rmpath(['hello',pathsep(),'hello',pathsep(),'hello'],'hello'),''); -%!assert(rmpath(['hello',pathsep(),'hello',pathsep(),'hello',pathsep(),'hello'],'hello'),''); -%!assert(rmpath([pathsep(),'hello',pathsep(),'hello'],'hello'),pathsep()); -%!assert(rmpath(['hello',pathsep(),'hello',pathsep()],'hello'),pathsep()); -%!assert(rmpath('hello','world'),'hello'); -%!assert(rmpath([pathsep(),'hello'],'','hello'),''); -%!assert(rmpath([pathsep(),'hello'],'hello',''),''); - -%!assert(rmpath(['hello',pathsep(),'world'],'hello','world'),''); -%!assert(rmpath(['hello',pathsep(),'world',pathsep()],'hello','world'),pathsep()); -%!assert(rmpath([pathsep(),'hello',pathsep(),'world',pathsep()],'hello','world'),pathsep()); - -%!assert(rmpath(['hello',pathsep(),'world'],'','hello','world'),''); -%!assert(rmpath(['hello',pathsep(),'world',pathsep()],'','hello','world'),''); -%!assert(rmpath([pathsep(),'hello',pathsep(),'world',pathsep()],'','hello','world'),''); - -%!assert(rmpath(['hello',pathsep(),'world'],'hello'),'world'); -%!assert(rmpath(['hello',pathsep(),'world'],'world'),'hello'); -%!assert(rmpath(['hello',pathsep(),'world',pathsep()],'hello'),['world',pathsep()]); -%!assert(rmpath(['hello',pathsep(),'world',pathsep()],'world'),['hello',pathsep()]); -%!assert(rmpath([pathsep(),'hello',pathsep(),'world',pathsep()],'hello'),[pathsep(),'world',pathsep()]); -%!assert(rmpath([pathsep(),'hello',pathsep(),'world',pathsep()],'world'),[pathsep(),'hello',pathsep()]); diff --git a/scripts/path/savepath.m b/scripts/path/savepath.m --- a/scripts/path/savepath.m +++ b/scripts/path/savepath.m @@ -19,13 +19,10 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} savepath (@var{file}) -## This function saves the current @code{LOADPATH} to your personal -## default initilization file or optionally the @var{file} that you -## specify. -## -## It will return 0 if it was successful. -## -## @seealso{LOADPATH, addpath, rmpath, setpath} +## Save the current function search path to @var{file}. If @var{file} +## is omitted, @file{~/.octaverc} is used. If successful, +## @code{savepath} returns 0. +## @seealso{path, addpath, rmpath, pathsep} ## @end deftypefn ## Author: Bill Denney @@ -111,8 +108,8 @@ fprintf (fid, "%s\n", pre{i}) endfor - fprintf (fid, "%s\n setpath (\"%s\");\n%s\n", - beginstring, LOADPATH, endstring); + fprintf (fid, "%s\n path (\"%s\");\n%s\n", + beginstring, path (), endstring); for i = 1:length (post) fprintf (fid, "%s\n", post{i}); diff --git a/scripts/path/setpath.m b/scripts/path/setpath.m deleted file mode 100644 --- a/scripts/path/setpath.m +++ /dev/null @@ -1,43 +0,0 @@ -## Copyright (C) 2006 John W. Eaton -## -## This program is free software; you can redistribute it and/or modify -## it under the terms of the GNU General Public License as published by -## the Free Software Foundation; either version 2 of the License, or -## (at your option) any later version. -## -## This program is distributed in the hope that it will be useful, -## but WITHOUT ANY WARRANTY; without even the implied warranty of -## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -## GNU General Public License for more details. -## -## You should have received a copy of the GNU General Public License -## along with this program; if not, write to the Free Software -## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -## -*- texinfo -*- -## @deftypefn {Function File} @var{opath} setpath (@var{npath}) -## Set @code{LOADPATH} to @var{path} and return the previous value. -## -## @seealso{LOADPATH, addpath, rmpath, savepath} -## @end deftypefn - -## PKGADD: mark_as_command setpath - -function opath = setpath (npath) - - if (nargin == 1) - if (nargout > 0) - opath = LOADPATH; - endif - ## FIXME -- perhaps validate elements of npath to make sure - ## they are existing directories? - if (ischar (npath)) - LOADPATH = npath; - else - error ("setpath: expecting argument to be a character string"); - endif - else - usage ("opath = setpath (npath)"); - endif - -endfunction