changeset 5804:296cefb48d7e

[project @ 2006-05-10 20:10:10 by jwe]
author jwe
date Wed, 10 May 2006 20:10:10 +0000
parents c86a550a91c0
children 5bfb24f90bdd
files scripts/ChangeLog scripts/path/addpath.m scripts/path/path.m scripts/path/rmpath.m scripts/path/savepath.m scripts/path/setpath.m
diffstat 6 files changed, 119 insertions(+), 226 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Wed May 10 18:15:19 2006 +0000
+++ b/scripts/ChangeLog	Wed May 10 20:10:10 2006 +0000
@@ -1,5 +1,8 @@
 2006-05-10  John W. Eaton  <jwe@octave.org>
 
+	* path/addpath.m, path/rmpath.m: Improve compatibility.
+	* path/setpath.m: Delete.
+
 	* pkg/pkg.m: New file.
 
 2006-05-09  Keith Goodman  <kwgoodman@gmail.com>
--- a/scripts/path/addpath.m	Wed May 10 18:15:19 2006 +0000
+++ b/scripts/path/addpath.m	Wed May 10 20:10:10 2006 +0000
@@ -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 <etienne@cs.uky.edu>
@@ -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
--- a/scripts/path/path.m	Wed May 10 18:15:19 2006 +0000
+++ b/scripts/path/path.m	Wed May 10 20:10:10 2006 +0000
@@ -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
--- a/scripts/path/rmpath.m	Wed May 10 18:15:19 2006 +0000
+++ b/scripts/path/rmpath.m	Wed May 10 20:10:10 2006 +0000
@@ -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 <etienne@cs.uky.edu>
@@ -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()]);
--- a/scripts/path/savepath.m	Wed May 10 18:15:19 2006 +0000
+++ b/scripts/path/savepath.m	Wed May 10 20:10:10 2006 +0000
@@ -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 <bill@givebillmoney.com>
@@ -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});
--- a/scripts/path/setpath.m	Wed May 10 18:15:19 2006 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -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