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