annotate scripts/path/addpath.m @ 5732:17d87fbd7010

[project @ 2006-04-04 17:50:46 by jwe]
author jwe
date Tue, 04 Apr 2006 17:50:46 +0000
parents
children 6b345b4961ca
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5732
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2005 Bill Denney
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
2 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
6 ## (at your option) any later version.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
7 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
11 ## GNU General Public License for more details.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
12 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
14 ## along with this program; if not, write to the Free Software
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
16 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
17 ## Based on code Copyright (C) 2000 Etienne Grossmann
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
18
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
20 ## @deftypefn {Function File} {} addpath(dir1, ...)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
21 ## Prepends @code{dir1}, @code{...} to the current @code{LOADPATH}.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
22 ## If the directory is already in the path, it will place it where you
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
23 ## specify in the path (defaulting to prepending it).
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
24 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
25 ## @example
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
26 ## addpath(dir1,'-end',dir2,'-begin',dir3,'-END',dir4,'-BEGIN',dir5)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
27 ## @result{} Prepends dir1, dir3 and dir5 and appends dir2 and dir4.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
28 ## @end example
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
29 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
30 ## An error will be returned if the string is not a directory, the
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
31 ## directory doesn't exist or you don't have read access to it.
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
32 ##
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
33 ## BUG: This function can't add directories called @code{-end} or
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
34 ## @code{-begin} (case insensitively).
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
35 ## @end deftypefn
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
36
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
37 ## Author: Etienne Grossmann <etienne@cs.uky.edu>
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
38 ## Modified-By: Bill Denney <bill@givebillmoney.com>
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
39 ## Last modified: June 2005
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
40
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
41 ##PKGADD: mark_as_command addpath
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
42
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
43 function ret = addpath (varargin)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
44
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
45 if (nargout > 0)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
46 path = varargin{1};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
47 varargin = varargin(2:end);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
48 else
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
49 path = LOADPATH;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
50 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
51
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
52 dir = '';
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
53 if (length (varargin) > 0)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
54 append = 0;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
55 switch varargin{end}
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
56 case { 0, '0', '-begin', '-BEGIN' }
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
57 varargin = varargin(1:end-1);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
58 case { 1, '1', '-end', '-END' }
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
59 varargin = varargin(1:end-1);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
60 append = 1;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
61 endswitch
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
62
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
63 ## Avoid duplicates by stripping pre-existing entries
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
64 path = rmpath (path, varargin{:});
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
65
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
66 ## Check if the directories are valid
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
67 for arg = 1:length (varargin)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
68 p = varargin{arg};
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
69 if (nargout == 0 && ! isempty (p))
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
70 [s, err, m] = stat (p);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
71 if (err ~= 0)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
72 warning ("addpath %s : %s\n", p, m);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
73 continue;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
74 elseif (index (s.modestr, "d") != 1)
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
75 warning ("addpath %s : not a directory (mode=%s)\n", p, s.modestr);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
76 continue;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
77 elseif ! ((s.modestr(8) == 'r') || ...
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
78 ((getgid == s.gid) && (s.modestr(5) == 'r')) || ...
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
79 ((getuid == s.uid) && (s.modestr(2) == 'r')))
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
80 warning ("addpath %s : not readable (mode=%s)\n", p, s.modestr);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
81 continue;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
82 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
83 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
84 dir = sprintf ("%s:%s", dir, p);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
85 endfor
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
86
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
87 ## Add the directories to the current path
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
88 if (! isempty (dir))
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
89 dir = dir(2:end);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
90 if (isempty (path) && ! isempty (dir))
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
91 path = dir;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
92 else
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
93 if strcmp (path, ':'), path = ''; end
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
94 if append
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
95 path = sprintf ("%s:%s", path, dir);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
96 else
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
97 path = sprintf ("%s:%s", dir, path);
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
98 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
99 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
100 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
101 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
102
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
103 if nargout
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
104 ret = path;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
105 else
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
106 LOADPATH = path;
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
107 endif
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
108
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
109 endfunction
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
110
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
111 %!assert(addpath('','hello'),'hello');
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
112 %!assert(addpath('','hello','world'),'hello:world')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
113 %!assert(addpath(':','hello'),'hello:');
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
114 %!assert(addpath(':','hello','-end'),':hello');
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
115 %!assert(addpath('hello','hello'),'hello');
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
116 %!assert(addpath('hello','world'),'world:hello')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
117 %!assert(addpath('hello','world','-end'),'hello:world')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
118 %!assert(addpath('hello:','world','-end'),'hello::world')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
119 %!assert(addpath('hello:','hello','world','-end'),':hello:world')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
120
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
121 %!assert(addpath('',''),':')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
122 %!assert(addpath(':',''),':')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
123 %!assert(addpath('hello',''),':hello')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
124 %!assert(addpath('hello:world',''),':hello:world')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
125 %!assert(addpath('hello:world:',''),':hello:world')
17d87fbd7010 [project @ 2006-04-04 17:50:46 by jwe]
jwe
parents:
diff changeset
126 %!assert(addpath('hello::world',''),':hello:world')