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