Mercurial > octave
annotate scripts/miscellaneous/what.m @ 27903:f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
* what.m: Add isfile() or isfolder() checks to make sure that names have
the correct identity.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 03 Jan 2020 09:43:43 -0800 |
parents | 32f4a15c77c7 |
children | 121d33ab44bc |
rev | line source |
---|---|
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25781
diff
changeset
|
1 ## Copyright (C) 2007-2019 David Bateman |
7050 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
5 ## Octave is free software: you can redistribute it and/or modify it |
7050 | 6 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
7 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
8 ## (at your option) any later version. |
7050 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
13 ## GNU General Public License for more details. |
7050 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
17 ## <https://www.gnu.org/licenses/>. |
7050 | 18 |
19 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
20 ## @deftypefn {} {} what |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
21 ## @deftypefnx {} {} what @var{dir} |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20164
diff
changeset
|
22 ## @deftypefnx {} {w =} what (@var{dir}) |
19183 | 23 ## List the Octave specific files in directory @var{dir}. |
24 ## | |
25 ## If @var{dir} is not specified then the current directory is used. | |
26 ## | |
27 ## If a return argument is requested, the files found are returned in the | |
28 ## structure @var{w}. The structure contains the following fields: | |
29 ## | |
30 ## @table @asis | |
31 ## @item path | |
32 ## Full path to directory @var{dir} | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
33 ## |
19183 | 34 ## @item m |
35 ## Cell array of m-files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
36 ## |
19183 | 37 ## @item mat |
38 ## Cell array of mat files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
39 ## |
19183 | 40 ## @item mex |
41 ## Cell array of mex files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
42 ## |
19183 | 43 ## @item oct |
44 ## Cell array of oct files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
45 ## |
19183 | 46 ## @item mdl |
47 ## Cell array of mdl files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
48 ## |
19183 | 49 ## @item slx |
50 ## Cell array of slx files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
51 ## |
19183 | 52 ## @item p |
53 ## Cell array of p-files | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
54 ## |
19183 | 55 ## @item classes |
56 ## Cell array of class directories (@file{@@@var{classname}/}) | |
19405
cbce5d1bcaf9
doc: Periodic grammarcheck of documentation.
Rik <rik@octave.org>
parents:
19183
diff
changeset
|
57 ## |
19183 | 58 ## @item packages |
59 ## Cell array of package directories (@file{+@var{pkgname}/}) | |
60 ## @end table | |
61 ## | |
20164
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
62 ## Compatibility Note: Octave does not support mdl, slx, and p files; nor does |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
63 ## it support package directories. @code{what} will always return an empty |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
64 ## list for these categories. |
19183 | 65 ## @seealso{which, ls, exist} |
7050 | 66 ## @end deftypefn |
67 | |
19183 | 68 function retval = what (dir) |
69 | |
70 if (nargin > 1) | |
71 print_usage (); | |
72 endif | |
7050 | 73 |
74 if (nargin == 0) | |
19183 | 75 dir = pwd (); |
7050 | 76 else |
19183 | 77 dtmp = canonicalize_file_name (dir); |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 if (isempty (dtmp)) |
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
79 ## Search for directory name in path |
19183 | 80 if (dir(end) == '/' || dir(end) == '\') |
81 dir(end) = []; | |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
82 endif |
19183 | 83 dtmp = dir_in_loadpath (dir); |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
84 if (isempty (dtmp)) |
19183 | 85 error ("what: could not find the directory %s", dir); |
18060
427412d40f1a
what.m: Workaround file separator issues in code (bug #40726).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
86 endif |
7050 | 87 endif |
19183 | 88 dir = dtmp; |
7050 | 89 endif |
90 | |
19183 | 91 files = readdir (dir); |
92 w.path = dir; | |
7050 | 93 w.m = cell (0, 1); |
19183 | 94 w.mat = cell (0, 1); |
7050 | 95 w.mex = cell (0, 1); |
96 w.oct = cell (0, 1); | |
97 w.mdl = cell (0, 1); | |
19183 | 98 w.slx = cell (0, 1); |
7050 | 99 w.p = cell (0, 1); |
100 w.classes = cell (0, 1); | |
19183 | 101 w.packages = cell (0, 1); |
7050 | 102 |
103 for i = 1 : length (files) | |
27902
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
104 nm = files{i}; |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
105 |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
106 if (strcmp (nm, ".") || strcmp (nm, "..")) |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
107 continue; # Ignore . and .. |
7050 | 108 endif |
27902
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
109 |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
110 ## mdl, slx, and p are ignored (no if test) since they are not implemented |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
111 [~, f, e] = fileparts (nm); |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
112 if (strcmp (e, ".m")) |
27903
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
113 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
114 w.m{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
115 endif |
27902
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
116 elseif (strcmp (e, ".mat")) |
27903
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
117 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
118 w.mat{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
119 endif |
27902
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
120 elseif (strcmp (e, ".oct")) |
27903
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
121 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
122 w.oct{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
123 endif |
27902
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
124 elseif (strcmp (e, mexext ())) |
27903
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
125 if (isfile (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
126 w.mex{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
127 endif |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
128 elseif (nm(1) == "@") |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
129 if (isfolder (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
130 w.classes{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
131 endif |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
132 elseif (nm(1) == "+") |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
133 if (isfolder (fullfile (dir, nm))) |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
134 w.packages{end+1} = nm; |
f7279dd915d8
what.m: Check file / folder status before returning results (bug #57531).
Rik <rik@octave.org>
parents:
27902
diff
changeset
|
135 endif |
27902
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
136 endif |
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
137 |
7050 | 138 endfor |
139 | |
140 if (nargout == 0) | |
141 __display_filenames__ ("M-files in directory", w.path, w.m); | |
19183 | 142 __display_filenames__ ("\nMAT-files in directory", w.path, w.mat); |
7050 | 143 __display_filenames__ ("\nMEX-files in directory", w.path, w.mex); |
144 __display_filenames__ ("\nOCT-files in directory", w.path, w.oct); | |
145 __display_filenames__ ("\nClasses in directory", w.path, w.classes); | |
27902
32f4a15c77c7
what.m: Detect and report on packages (bug #57530)
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
146 __display_filenames__ ("\nPackages in directory", w.path, w.packages); |
7050 | 147 else |
19183 | 148 retval = w; |
7050 | 149 endif |
19183 | 150 |
7050 | 151 endfunction |
152 | |
153 function __display_filenames__ (msg, p, f) | |
19183 | 154 |
7050 | 155 if (length (f) > 0) |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
156 printf ("%s %s:\n\n", msg, p); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
157 |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12668
diff
changeset
|
158 maxlen = max (cellfun ("length", f)); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 ncols = max (1, floor (terminal_size ()(2) / (maxlen + 3))); |
19183 | 160 fmt = sprintf (" %%-%ds", maxlen); |
161 fmt = repmat (fmt, [1, ncols]); | |
162 fmt = [fmt "\n"]; | |
7050 | 163 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
164 nrows = ceil (length (f) / ncols); |
7050 | 165 for i = 1 : nrows |
21568
3d60ed163b70
maint: Eliminate bad spacing around '='.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
166 args = f(i:nrows:end); |
7050 | 167 if (length (args) < ncols) |
19183 | 168 args(end+1 : ncols) = {""}; |
7050 | 169 endif |
21634
96518f623c91
Backed out changeset dcf8922b724b
Mike Miller <mtmiller@octave.org>
parents:
21633
diff
changeset
|
170 printf (fmt, args{:}); |
7050 | 171 endfor |
172 endif | |
19183 | 173 |
7050 | 174 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16724
diff
changeset
|
175 |
19183 | 176 |
177 %!test | |
178 %! w = what (); | |
179 %! assert (w.path, pwd); | |
180 %! assert (fieldnames (w), {"path"; "m"; "mat"; "mex"; "oct"; "mdl"; "slx"; | |
181 %! "p"; "classes"; "packages"}); | |
182 | |
183 %!error what (1, 2) |