Mercurial > octave-nkf
annotate scripts/miscellaneous/ls.m @ 14000:15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
* ls.m: Escape special characters such as ["'(] in filename arguments.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 05 Dec 2011 17:13:35 -0800 |
parents | 5859cf1d57a6 |
children | caa7439203f2 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2006-2011 John W. Eaton |
6115 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6115 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6115 | 18 |
19 ## -*- texinfo -*- | |
11547 | 20 ## @deftypefn {Command} {} ls options |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
21 ## List directory contents. For example: |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11547
diff
changeset
|
22 ## |
6115 | 23 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## @group |
6115 | 25 ## ls -l |
26 ## @print{} total 12 | |
27 ## @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m | |
28 ## @print{} -rw-r--r-- 1 jwe users 1315 Aug 17 23:14 bar.m | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @end group |
6115 | 30 ## @end example |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11547
diff
changeset
|
31 ## |
6115 | 32 ## The @code{dir} and @code{ls} commands are implemented by calling your |
33 ## system's directory listing command, so the available options may vary | |
34 ## from system to system. | |
35 ## @seealso{dir, stat, readdir, glob, filesep, ls_command} | |
11547 | 36 ## @end deftypefn |
6115 | 37 |
38 ## Author: jwe | |
39 | |
40 function retval = ls (varargin) | |
41 | |
42 global __ls_command__; | |
43 | |
44 if (isempty (__ls_command__) || ! ischar (__ls_command__)) | |
45 ## Initialize value for __ls_command__. | |
46 ls_command (); | |
47 endif | |
48 | |
49 if (iscellstr (varargin)) | |
50 | |
51 args = tilde_expand (varargin); | |
52 | |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
53 if (nargin > 0) |
14000
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
54 if (ispc () && ! isunix ()) |
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
55 ## shell (cmd.exe) on MinGW uses '^' as escape character |
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
56 args = regexprep (args{:}, '([^\w.*?])', '^$1'); |
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
57 else |
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
58 args = regexprep (args{:}, '([^\w.*?])', '\$1'); |
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
59 endif |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
60 else |
14000
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
61 args = ""; |
13900
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
62 endif |
5859cf1d57a6
quote filenames passed to the shell
John W. Eaton <jwe@octave.org>
parents:
13899
diff
changeset
|
63 |
14000
15400d5deb1c
ls.m: Fix handling of special characters in filename arguments (Bug #34950)
Rik <octave@nomad.inbox5.com>
parents:
13900
diff
changeset
|
64 cmd = sprintf ("%s %s", __ls_command__, args); |
6115 | 65 |
6943 | 66 if (page_screen_output () || nargout > 0) |
6115 | 67 |
68 [status, output] = system (cmd); | |
69 | |
70 if (status == 0) | |
10549 | 71 if (nargout == 0) |
72 puts (output); | |
73 else | |
11032
c9b0a75b02e8
Make all regexp in Octave compatible with both POSIX and PCRE.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
74 retval = strvcat (regexp (output, '\S+', 'match'){:}); |
10549 | 75 endif |
6115 | 76 else |
12634
d129be41f1e3
ls.m: Don't print backtrace information when command issues an error.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
77 error ("ls: command exited abnormally with status %d\n", status); |
6115 | 78 endif |
79 | |
80 else | |
81 ## Just let the output flow if the pager is off. That way the | |
82 ## output from things like "ls -R /" will show up immediately and | |
83 ## we won't have to buffer all the output. | |
84 system (cmd); | |
85 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11547
diff
changeset
|
86 |
6115 | 87 else |
88 error ("ls: expecting all arguments to be character strings"); | |
89 endif | |
90 | |
91 endfunction | |
7411 | 92 |
93 %!error ls (1); | |
94 |