523
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
523
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
523
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
523
|
25 #endif |
|
26 |
1341
|
27 #include <cerrno> |
|
28 #include <cstdio> |
|
29 #include <cstddef> |
|
30 #include <cstdlib> |
|
31 #include <cstring> |
|
32 |
3503
|
33 #include <strstream> |
1728
|
34 #include <string> |
|
35 |
1832
|
36 #ifdef HAVE_UNISTD_H |
2442
|
37 #ifdef HAVE_SYS_TYPES_H |
1832
|
38 #include <sys/types.h> |
2442
|
39 #endif |
1832
|
40 #include <unistd.h> |
|
41 #endif |
|
42 |
2926
|
43 #include "file-ops.h" |
|
44 #include "file-stat.h" |
|
45 #include "glob-match.h" |
|
46 #include "oct-env.h" |
1755
|
47 #include "str-vec.h" |
|
48 |
1355
|
49 #include "defun.h" |
1781
|
50 #include "dir-ops.h" |
1355
|
51 #include "dirfns.h" |
|
52 #include "error.h" |
1402
|
53 #include "gripes.h" |
1750
|
54 #include "oct-obj.h" |
1355
|
55 #include "pager.h" |
|
56 #include "procstream.h" |
1750
|
57 #include "pt-plot.h" |
1355
|
58 #include "sysdep.h" |
1750
|
59 #include "toplev.h" |
1449
|
60 #include "unwind-prot.h" |
523
|
61 #include "utils.h" |
1742
|
62 #include "variables.h" |
523
|
63 |
2926
|
64 // XXX FIXME XXX -- changing the plotter directory should be handled |
|
65 // by registering a function for octave_env::chdir to call so that |
|
66 // this function can be eliminated. |
523
|
67 |
1328
|
68 static int |
3523
|
69 octave_change_to_directory (const std::string& newdir) |
1328
|
70 { |
2926
|
71 int cd_ok = octave_env::chdir (newdir); |
1328
|
72 |
|
73 if (cd_ok) |
|
74 do_external_plotter_cd (newdir); |
|
75 else |
1755
|
76 error ("%s: %s", newdir.c_str (), strerror (errno)); |
1328
|
77 |
|
78 return cd_ok; |
|
79 } |
|
80 |
1957
|
81 DEFUN_TEXT (cd, args, , |
3301
|
82 "-*- texinfo -*-\n\ |
|
83 @deffn {Command} cd dir\n\ |
|
84 @deffnx {Command} chdir dir\n\ |
|
85 Change the current working directory to @var{dir}. If @var{dir} is\n\ |
|
86 omitted, the current directory is changed to the users home\n\ |
|
87 directory. For example,\n\ |
523
|
88 \n\ |
3301
|
89 @example\n\ |
|
90 cd ~/octave\n\ |
|
91 @end example\n\ |
|
92 \n\ |
|
93 @noindent\n\ |
|
94 Changes the current working directory to @file{~/octave}. If the\n\ |
|
95 directory does not exist, an error message is printed and the working\n\ |
|
96 directory is not changed.\n\ |
|
97 @end deffn") |
523
|
98 { |
2086
|
99 octave_value_list retval; |
523
|
100 |
1755
|
101 int argc = args.length () + 1; |
|
102 |
1965
|
103 string_vector argv = args.make_argv ("cd"); |
1755
|
104 |
|
105 if (error_state) |
|
106 return retval; |
523
|
107 |
|
108 if (argc > 1) |
|
109 { |
3523
|
110 std::string dirname = file_ops::tilde_expand (argv[1]); |
523
|
111 |
1750
|
112 if (dirname.length () > 0 |
1755
|
113 && ! octave_change_to_directory (dirname)) |
523
|
114 { |
|
115 return retval; |
|
116 } |
|
117 } |
|
118 else |
|
119 { |
3523
|
120 std::string home_dir = octave_env::get_home_directory (); |
2926
|
121 |
|
122 if (home_dir.empty () || ! octave_change_to_directory (home_dir)) |
|
123 return retval; |
523
|
124 } |
|
125 |
|
126 return retval; |
|
127 } |
|
128 |
611
|
129 DEFALIAS (chdir, cd); |
|
130 |
661
|
131 // Get a directory listing. |
|
132 |
1965
|
133 static void |
|
134 cleanup_iprocstream (void *p) |
|
135 { |
2800
|
136 delete static_cast <iprocstream *> (p); |
1965
|
137 } |
|
138 |
1957
|
139 DEFUN_TEXT (ls, args, , |
3301
|
140 "-*- texinfo -*-\n\ |
|
141 @deffn {Command} ls options\n\ |
|
142 @deffnx {Command} dir options\n\ |
|
143 List directory contents. For example,\n\ |
523
|
144 \n\ |
3301
|
145 @example\n\ |
|
146 ls -l\n\ |
|
147 @print{} total 12\n\ |
|
148 @print{} -rw-r--r-- 1 jwe users 4488 Aug 19 04:02 foo.m\n\ |
|
149 @print{} -rw-r--r-- 1 jwe users 1315 Aug 17 23:14 bar.m\n\ |
|
150 @end example\n\ |
|
151 \n\ |
|
152 The @code{dir} and @code{ls} commands are implemented by calling your\n\ |
|
153 system's directory listing command, so the available options may vary\n\ |
|
154 from system to system.\n\ |
|
155 @end deffn") |
523
|
156 { |
2086
|
157 octave_value_list retval; |
523
|
158 |
1755
|
159 int argc = args.length () + 1; |
|
160 |
1965
|
161 string_vector argv = args.make_argv ("ls"); |
1755
|
162 |
|
163 if (error_state) |
|
164 return retval; |
523
|
165 |
3523
|
166 std::ostrstream ls_buf; |
523
|
167 |
|
168 ls_buf << "ls -C "; |
|
169 for (int i = 1; i < argc; i++) |
2926
|
170 ls_buf << file_ops::tilde_expand (argv[i]) << " "; |
523
|
171 |
|
172 ls_buf << ends; |
1449
|
173 char *ls_command = ls_buf.str (); |
523
|
174 |
1449
|
175 iprocstream *cmd = new iprocstream (ls_command); |
523
|
176 |
1469
|
177 delete [] ls_command; |
|
178 |
2985
|
179 unwind_protect::add (cleanup_iprocstream, cmd); |
523
|
180 |
3147
|
181 // XXX FIXME XXX -- sometimes, the subprocess hasn't written |
|
182 // anything before we try to read from the procstream. The kluge |
|
183 // below (simply waiting and trying again) is ugly, but it seems to |
|
184 // work, at least most of the time. It could probably still fail if |
|
185 // the subprocess hasn't started writing after the snooze. Isn't |
|
186 // there a better way? If there is, you should also fix the code |
|
187 // for the system function in toplev.cc. |
|
188 |
1449
|
189 if (cmd && *cmd) |
|
190 { |
3147
|
191 char ch; |
|
192 |
|
193 if (cmd->get (ch)) |
|
194 octave_stdout << ch; |
|
195 else |
|
196 { |
|
197 cmd->clear (); |
|
198 |
3308
|
199 octave_usleep (100); |
3147
|
200 } |
|
201 |
|
202 while (cmd->get (ch)) |
|
203 octave_stdout << ch; |
1449
|
204 } |
|
205 else |
|
206 error ("couldn't start process for ls!"); |
523
|
207 |
2985
|
208 unwind_protect::run (); |
523
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
549
|
213 DEFALIAS (dir, ls); |
|
214 |
1957
|
215 DEFUN (pwd, , nargout, |
3301
|
216 "-*- texinfo -*-\n\ |
|
217 @deftypefn {Built-in Function} {} pwd ()\n\ |
|
218 Return the current working directory.\n\ |
|
219 @end deftypefn") |
523
|
220 { |
2086
|
221 octave_value_list retval; |
523
|
222 |
3523
|
223 std::string directory = octave_env::getcwd (); |
523
|
224 |
2926
|
225 if (directory.empty ()) |
|
226 warning ("pwd: can't find working directory!"); |
523
|
227 else |
|
228 { |
1592
|
229 if (nargout == 0) |
2095
|
230 octave_stdout << directory << "\n"; |
1592
|
231 else |
|
232 retval = directory; |
523
|
233 } |
1592
|
234 |
523
|
235 return retval; |
|
236 } |
|
237 |
1957
|
238 DEFUN (readdir, args, , |
3301
|
239 "-*- texinfo -*-\n\ |
|
240 @deftypefn {Built-in Function} {[@var{files}, @var{err}, @var{msg}] =} readdir (@var{dir})\n\ |
|
241 Return names of the files in the directory @var{dir} as an array of\n\ |
|
242 strings. If an error occurs, return an empty matrix in @var{files}.\n\ |
1389
|
243 \n\ |
3301
|
244 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
245 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
246 system-dependent error message.\n\ |
|
247 @end deftypefn") |
1389
|
248 { |
2086
|
249 octave_value_list retval; |
1389
|
250 |
3523
|
251 retval(2) = std::string (); |
2669
|
252 retval(1) = -1.0; |
|
253 retval(0) = Matrix (); |
|
254 |
1401
|
255 if (args.length () == 1) |
1389
|
256 { |
3523
|
257 std::string dirname = args(0).string_value (); |
1389
|
258 |
1401
|
259 if (error_state) |
1781
|
260 gripe_wrong_type_arg ("readdir", args(0)); |
1401
|
261 else |
|
262 { |
2926
|
263 dir_entry dir (file_ops::tilde_expand (dirname)); |
1389
|
264 |
1401
|
265 if (dir) |
1389
|
266 { |
1781
|
267 string_vector dirlist = dir.read (); |
|
268 retval(0) = dirlist.qsort (); |
2669
|
269 retval(1) = 0.0; |
1401
|
270 } |
|
271 else |
|
272 { |
2669
|
273 retval(2) = dir.error (); |
1401
|
274 } |
1389
|
275 } |
|
276 } |
|
277 else |
|
278 print_usage ("readdir"); |
|
279 |
1401
|
280 return retval; |
|
281 } |
|
282 |
|
283 // XXX FIXME XXX -- should probably also allow second arg to specify |
|
284 // mode. |
|
285 |
1957
|
286 DEFUN (mkdir, args, , |
3301
|
287 "-*- texinfo -*-\n\ |
|
288 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} mkdir (@var{dir})\\n\ |
|
289 Create a directory named @var{dir}.\n\ |
1401
|
290 \n\ |
3301
|
291 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
292 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
293 system-dependent error message.\n\ |
|
294 @end deftypefn") |
1401
|
295 { |
2086
|
296 octave_value_list retval; |
1401
|
297 |
3523
|
298 retval(1) = std::string (); |
2669
|
299 retval(0) = -1.0; |
1401
|
300 |
|
301 if (args.length () == 1) |
|
302 { |
3523
|
303 std::string dirname = args(0).string_value (); |
1401
|
304 |
|
305 if (error_state) |
1402
|
306 gripe_wrong_type_arg ("mkdir", args(0)); |
1489
|
307 else |
1401
|
308 { |
3523
|
309 std::string msg; |
2669
|
310 |
2926
|
311 int status = file_ops::mkdir (file_ops::tilde_expand (dirname), |
|
312 0777, msg); |
1489
|
313 |
2800
|
314 retval(0) = static_cast<double> (status); |
2669
|
315 |
|
316 if (status < 0) |
|
317 retval(1) = msg; |
1401
|
318 } |
|
319 } |
|
320 else |
|
321 print_usage ("mkdir"); |
|
322 |
|
323 return retval; |
|
324 } |
|
325 |
1957
|
326 DEFUN (rmdir, args, , |
3301
|
327 "-*- texinfo -*-\n\ |
|
328 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} rmdir (@var{dir})\n\ |
|
329 Remove the directory named @var{dir}.\n\ |
1401
|
330 \n\ |
3301
|
331 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
332 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
333 system-dependent error message.\n\ |
|
334 @end deftypefn") |
1401
|
335 { |
2086
|
336 octave_value_list retval; |
1401
|
337 |
3523
|
338 retval(1) = std::string (); |
2669
|
339 retval(0) = -1.0; |
1401
|
340 |
|
341 if (args.length () == 1) |
|
342 { |
3523
|
343 std::string dirname = args(0).string_value (); |
1401
|
344 |
|
345 if (error_state) |
1402
|
346 gripe_wrong_type_arg ("rmdir", args(0)); |
1489
|
347 else |
1401
|
348 { |
3523
|
349 std::string msg; |
2669
|
350 |
2926
|
351 int status = file_ops::rmdir (file_ops::tilde_expand (dirname), msg); |
1489
|
352 |
2800
|
353 retval(0) = static_cast<double> (status); |
2669
|
354 |
|
355 if (status < 0) |
|
356 retval(1) = msg; |
1401
|
357 } |
|
358 } |
1389
|
359 else |
1401
|
360 print_usage ("rmdir"); |
|
361 |
|
362 return retval; |
|
363 } |
|
364 |
1957
|
365 DEFUN (rename, args, , |
3301
|
366 "-*- texinfo -*-\n\ |
|
367 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} rename (@var{old}, @var{new})\n\ |
|
368 Change the name of file @var{old} to @var{new}.\n\ |
1401
|
369 \n\ |
3301
|
370 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
|
371 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
|
372 system-dependent error message.\n\ |
|
373 @end deftypefn") |
1401
|
374 { |
2086
|
375 octave_value_list retval; |
1401
|
376 |
3523
|
377 retval(1) = std::string (); |
2669
|
378 retval(0) = -1.0; |
1401
|
379 |
|
380 if (args.length () == 2) |
|
381 { |
3523
|
382 std::string from = args(0).string_value (); |
1728
|
383 |
1401
|
384 if (error_state) |
1402
|
385 gripe_wrong_type_arg ("rename", args(0)); |
|
386 else |
1401
|
387 { |
3523
|
388 std::string to = args(1).string_value (); |
1728
|
389 |
1402
|
390 if (error_state) |
|
391 gripe_wrong_type_arg ("rename", args(1)); |
2669
|
392 else |
1402
|
393 { |
3523
|
394 std::string msg; |
2669
|
395 |
2926
|
396 int status = file_ops::rename (from, to, msg); |
2669
|
397 |
2800
|
398 retval(0) = static_cast<double> (status); |
2669
|
399 |
|
400 if (status < 0) |
|
401 retval(1) = msg; |
1402
|
402 } |
1401
|
403 } |
|
404 } |
|
405 else |
|
406 print_usage ("rename"); |
|
407 |
1389
|
408 return retval; |
|
409 } |
|
410 |
2495
|
411 DEFUN (glob, args, , |
3301
|
412 "-*- texinfo -*-\n\ |
|
413 @deftypefn {Built-in Function} {} glob (@var{pattern})\n\ |
|
414 Given an array of strings in @var{pattern}, return the list of file\n\ |
|
415 names that any of them, or an empty string if no patterns match. Tilde\n\ |
|
416 expansion is performed on each of the patterns before looking for\n\ |
|
417 matching file names. For example,\n\ |
2495
|
418 \n\ |
3301
|
419 @example\n\ |
|
420 @group\n\ |
|
421 glob (\"/vm*\")\n\ |
|
422 @result{} \"/vmlinuz\"\n\ |
|
423 @end group\n\ |
|
424 @end example\n\ |
|
425 \n\ |
|
426 Note that multiple values are returned in a string matrix with the fill\n\ |
|
427 character set to ASCII NUL.\n\ |
|
428 @end deftypefn") |
2495
|
429 { |
|
430 octave_value retval; |
|
431 |
|
432 if (args.length () == 1) |
|
433 { |
|
434 string_vector pat = args(0).all_strings (); |
|
435 |
|
436 if (error_state) |
|
437 gripe_wrong_type_arg ("glob", args(0)); |
|
438 else |
|
439 { |
2926
|
440 glob_match pattern (file_ops::tilde_expand (pat)); |
2495
|
441 |
|
442 string_vector list = pattern.glob (); |
|
443 |
|
444 if (list.empty ()) |
|
445 retval = ""; |
|
446 else |
|
447 retval = list; |
|
448 } |
|
449 } |
|
450 else |
|
451 print_usage ("glob"); |
|
452 |
|
453 return retval; |
|
454 } |
|
455 |
2496
|
456 DEFUN (fnmatch, args, , |
3301
|
457 "-*- texinfo -*-\n\ |
|
458 @deftypefn {Built-in Function} {} fnmatch (@var{pattern}, @var{string})\n\ |
|
459 Return 1 or zero for each element of @var{string} that matches any of\n\ |
|
460 the elements of the string array @var{pattern}, using the rules of\n\ |
|
461 filename pattern matching. For example,\n\ |
2496
|
462 \n\ |
3301
|
463 @example\n\ |
|
464 @group\n\ |
|
465 fnmatch (\"a*b\", [\"ab\"; \"axyzb\"; \"xyzab\"])\n\ |
|
466 @result{} [ 1; 1; 0 ]\n\ |
|
467 @end group\n\ |
|
468 @end example\n\ |
|
469 @end deftypefn") |
2496
|
470 { |
|
471 octave_value retval; |
|
472 |
|
473 if (args.length () == 2) |
|
474 { |
|
475 string_vector pat = args(0).all_strings (); |
|
476 string_vector str = args(1).all_strings (); |
|
477 |
|
478 if (error_state) |
|
479 gripe_wrong_type_arg ("fnmatch", args(0)); |
|
480 else |
|
481 { |
2926
|
482 glob_match pattern (file_ops::tilde_expand (pat)); |
2496
|
483 |
|
484 Array<bool> tmp = pattern.match (str); |
|
485 |
|
486 int n = tmp.length (); |
|
487 |
|
488 ColumnVector result (n); |
|
489 |
|
490 for (int i = 0; i < n; i++) |
|
491 result(i) = tmp(i); |
|
492 |
3418
|
493 retval = result; |
2496
|
494 } |
|
495 } |
|
496 else |
|
497 print_usage ("fnmatch"); |
|
498 |
|
499 return retval; |
|
500 } |
|
501 |
523
|
502 /* |
|
503 ;;; Local Variables: *** |
|
504 ;;; mode: C++ *** |
|
505 ;;; End: *** |
|
506 */ |