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" |
1389
|
55 #include "help.h" |
1750
|
56 #include "oct-obj.h" |
1355
|
57 #include "pager.h" |
|
58 #include "procstream.h" |
1750
|
59 #include "pt-plot.h" |
1355
|
60 #include "sysdep.h" |
1750
|
61 #include "toplev.h" |
1449
|
62 #include "unwind-prot.h" |
523
|
63 #include "utils.h" |
1742
|
64 #include "variables.h" |
523
|
65 |
2926
|
66 // XXX FIXME XXX -- changing the plotter directory should be handled |
|
67 // by registering a function for octave_env::chdir to call so that |
|
68 // this function can be eliminated. |
523
|
69 |
1328
|
70 static int |
1755
|
71 octave_change_to_directory (const string& newdir) |
1328
|
72 { |
2926
|
73 int cd_ok = octave_env::chdir (newdir); |
1328
|
74 |
|
75 if (cd_ok) |
|
76 do_external_plotter_cd (newdir); |
|
77 else |
1755
|
78 error ("%s: %s", newdir.c_str (), strerror (errno)); |
1328
|
79 |
|
80 return cd_ok; |
|
81 } |
|
82 |
1957
|
83 DEFUN_TEXT (cd, args, , |
523
|
84 "cd [dir]\n\ |
|
85 \n\ |
|
86 change current working directory\n\ |
|
87 if no arguments are given, the current directory is changed to the\n\ |
|
88 users home directory") |
|
89 { |
2086
|
90 octave_value_list retval; |
523
|
91 |
1755
|
92 int argc = args.length () + 1; |
|
93 |
1965
|
94 string_vector argv = args.make_argv ("cd"); |
1755
|
95 |
|
96 if (error_state) |
|
97 return retval; |
523
|
98 |
|
99 if (argc > 1) |
|
100 { |
2926
|
101 string dirname = file_ops::tilde_expand (argv[1]); |
523
|
102 |
1750
|
103 if (dirname.length () > 0 |
1755
|
104 && ! octave_change_to_directory (dirname)) |
523
|
105 { |
|
106 return retval; |
|
107 } |
|
108 } |
|
109 else |
|
110 { |
2926
|
111 string home_dir = octave_env::get_home_directory (); |
|
112 |
|
113 if (home_dir.empty () || ! octave_change_to_directory (home_dir)) |
|
114 return retval; |
523
|
115 } |
|
116 |
|
117 return retval; |
|
118 } |
|
119 |
611
|
120 DEFALIAS (chdir, cd); |
|
121 |
661
|
122 // Get a directory listing. |
|
123 |
1965
|
124 static void |
|
125 cleanup_iprocstream (void *p) |
|
126 { |
2800
|
127 delete static_cast <iprocstream *> (p); |
1965
|
128 } |
|
129 |
1957
|
130 DEFUN_TEXT (ls, args, , |
523
|
131 "ls [options]\n\ |
|
132 \n\ |
|
133 print a directory listing") |
|
134 { |
2086
|
135 octave_value_list retval; |
523
|
136 |
1755
|
137 int argc = args.length () + 1; |
|
138 |
1965
|
139 string_vector argv = args.make_argv ("ls"); |
1755
|
140 |
|
141 if (error_state) |
|
142 return retval; |
523
|
143 |
|
144 ostrstream ls_buf; |
|
145 |
|
146 ls_buf << "ls -C "; |
|
147 for (int i = 1; i < argc; i++) |
2926
|
148 ls_buf << file_ops::tilde_expand (argv[i]) << " "; |
523
|
149 |
|
150 ls_buf << ends; |
1449
|
151 char *ls_command = ls_buf.str (); |
523
|
152 |
1449
|
153 iprocstream *cmd = new iprocstream (ls_command); |
523
|
154 |
1469
|
155 delete [] ls_command; |
|
156 |
1449
|
157 add_unwind_protect (cleanup_iprocstream, cmd); |
523
|
158 |
1449
|
159 if (cmd && *cmd) |
|
160 { |
|
161 int ch; |
|
162 while ((ch = cmd->get ()) != EOF) |
2095
|
163 octave_stdout << (char) ch; |
1449
|
164 } |
|
165 else |
|
166 error ("couldn't start process for ls!"); |
523
|
167 |
1449
|
168 run_unwind_protect (); |
523
|
169 |
|
170 return retval; |
|
171 } |
|
172 |
549
|
173 DEFALIAS (dir, ls); |
|
174 |
1957
|
175 DEFUN (pwd, , nargout, |
523
|
176 "pwd (): print current working directory") |
|
177 { |
2086
|
178 octave_value_list retval; |
523
|
179 |
2926
|
180 string directory = octave_env::getcwd (); |
523
|
181 |
2926
|
182 if (directory.empty ()) |
|
183 warning ("pwd: can't find working directory!"); |
523
|
184 else |
|
185 { |
1592
|
186 if (nargout == 0) |
2095
|
187 octave_stdout << directory << "\n"; |
1592
|
188 else |
|
189 retval = directory; |
523
|
190 } |
1592
|
191 |
523
|
192 return retval; |
|
193 } |
|
194 |
1957
|
195 DEFUN (readdir, args, , |
2669
|
196 "[FILES, STATUS, MSG] = readdir (NAME)\n\ |
1389
|
197 \n\ |
2802
|
198 Return an array of strings containing the list of all files in the\n\ |
2669
|
199 named directory in FILES, or an empty matrix if an error occurs\n\ |
|
200 \n\ |
|
201 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
202 STATUS is nonzero and MSG contains a system-dependent error message.") |
1389
|
203 { |
2086
|
204 octave_value_list retval; |
1389
|
205 |
2669
|
206 retval(2) = string (); |
|
207 retval(1) = -1.0; |
|
208 retval(0) = Matrix (); |
|
209 |
1401
|
210 if (args.length () == 1) |
1389
|
211 { |
1755
|
212 string dirname = args(0).string_value (); |
1389
|
213 |
1401
|
214 if (error_state) |
1781
|
215 gripe_wrong_type_arg ("readdir", args(0)); |
1401
|
216 else |
|
217 { |
2926
|
218 dir_entry dir (file_ops::tilde_expand (dirname)); |
1389
|
219 |
1401
|
220 if (dir) |
1389
|
221 { |
1781
|
222 string_vector dirlist = dir.read (); |
|
223 retval(0) = dirlist.qsort (); |
2669
|
224 retval(1) = 0.0; |
1401
|
225 } |
|
226 else |
|
227 { |
2669
|
228 retval(2) = dir.error (); |
1401
|
229 } |
1389
|
230 } |
|
231 } |
|
232 else |
|
233 print_usage ("readdir"); |
|
234 |
1401
|
235 return retval; |
|
236 } |
|
237 |
|
238 // XXX FIXME XXX -- should probably also allow second arg to specify |
|
239 // mode. |
|
240 |
1957
|
241 DEFUN (mkdir, args, , |
2669
|
242 "[STATUS, MSG] = mkdir (NAME)\n\ |
1401
|
243 \n\ |
2669
|
244 Create the directory named by NAME.\n\ |
|
245 \n\ |
|
246 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
247 STATUS is nonzero and MSG contains a system-dependent error message.") |
1401
|
248 { |
2086
|
249 octave_value_list retval; |
1401
|
250 |
2669
|
251 retval(1) = string (); |
|
252 retval(0) = -1.0; |
1401
|
253 |
|
254 if (args.length () == 1) |
|
255 { |
1755
|
256 string dirname = args(0).string_value (); |
1401
|
257 |
|
258 if (error_state) |
1402
|
259 gripe_wrong_type_arg ("mkdir", args(0)); |
1489
|
260 else |
1401
|
261 { |
2669
|
262 string msg; |
|
263 |
2926
|
264 int status = file_ops::mkdir (file_ops::tilde_expand (dirname), |
|
265 0777, msg); |
1489
|
266 |
2800
|
267 retval(0) = static_cast<double> (status); |
2669
|
268 |
|
269 if (status < 0) |
|
270 retval(1) = msg; |
1401
|
271 } |
|
272 } |
|
273 else |
|
274 print_usage ("mkdir"); |
|
275 |
|
276 return retval; |
|
277 } |
|
278 |
1957
|
279 DEFUN (rmdir, args, , |
2669
|
280 "[STATUS, MSG] = rmdir (NAME)\n\ |
1401
|
281 \n\ |
2669
|
282 Remove the directory named by NAME.\n\ |
|
283 \n\ |
|
284 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
285 STATUS is nonzero and MSG contains a system-dependent error message.") |
1401
|
286 { |
2086
|
287 octave_value_list retval; |
1401
|
288 |
2669
|
289 retval(1) = string (); |
|
290 retval(0) = -1.0; |
1401
|
291 |
|
292 if (args.length () == 1) |
|
293 { |
1755
|
294 string dirname = args(0).string_value (); |
1401
|
295 |
|
296 if (error_state) |
1402
|
297 gripe_wrong_type_arg ("rmdir", args(0)); |
1489
|
298 else |
1401
|
299 { |
2669
|
300 string msg; |
|
301 |
2926
|
302 int status = file_ops::rmdir (file_ops::tilde_expand (dirname), msg); |
1489
|
303 |
2800
|
304 retval(0) = static_cast<double> (status); |
2669
|
305 |
|
306 if (status < 0) |
|
307 retval(1) = msg; |
1401
|
308 } |
|
309 } |
1389
|
310 else |
1401
|
311 print_usage ("rmdir"); |
|
312 |
|
313 return retval; |
|
314 } |
|
315 |
1957
|
316 DEFUN (rename, args, , |
2669
|
317 "[STATUS, MSG] = rename (FROM, TO)\n\ |
1401
|
318 \n\ |
2669
|
319 Rename a file.\n\ |
|
320 \n\ |
|
321 If successful, STATUS is 0 and MSG is an empty string. Otherwise,\n\ |
|
322 STATUS is nonzero and MSG contains a system-dependent error message.") |
1401
|
323 { |
2086
|
324 octave_value_list retval; |
1401
|
325 |
2669
|
326 retval(1) = string (); |
|
327 retval(0) = -1.0; |
1401
|
328 |
|
329 if (args.length () == 2) |
|
330 { |
1755
|
331 string from = args(0).string_value (); |
1728
|
332 |
1401
|
333 if (error_state) |
1402
|
334 gripe_wrong_type_arg ("rename", args(0)); |
|
335 else |
1401
|
336 { |
1755
|
337 string to = args(1).string_value (); |
1728
|
338 |
1402
|
339 if (error_state) |
|
340 gripe_wrong_type_arg ("rename", args(1)); |
2669
|
341 else |
1402
|
342 { |
2669
|
343 string msg; |
|
344 |
2926
|
345 int status = file_ops::rename (from, to, msg); |
2669
|
346 |
2800
|
347 retval(0) = static_cast<double> (status); |
2669
|
348 |
|
349 if (status < 0) |
|
350 retval(1) = msg; |
1402
|
351 } |
1401
|
352 } |
|
353 } |
|
354 else |
|
355 print_usage ("rename"); |
|
356 |
1389
|
357 return retval; |
|
358 } |
|
359 |
2495
|
360 DEFUN (glob, args, , |
|
361 "glob (PATTERN)\n\ |
|
362 \n\ |
2496
|
363 Given an array of strings in PATTERN, return the list of file names\n\ |
|
364 that any of them, or an empty string if no patterns match. Tilde\n\ |
|
365 expansion is performed on each of the patterns before looking for\n\ |
2495
|
366 matching file names.") |
|
367 { |
|
368 octave_value retval; |
|
369 |
|
370 if (args.length () == 1) |
|
371 { |
|
372 string_vector pat = args(0).all_strings (); |
|
373 |
|
374 if (error_state) |
|
375 gripe_wrong_type_arg ("glob", args(0)); |
|
376 else |
|
377 { |
2926
|
378 glob_match pattern (file_ops::tilde_expand (pat)); |
2495
|
379 |
|
380 string_vector list = pattern.glob (); |
|
381 |
|
382 if (list.empty ()) |
|
383 retval = ""; |
|
384 else |
|
385 retval = list; |
|
386 } |
|
387 } |
|
388 else |
|
389 print_usage ("glob"); |
|
390 |
|
391 return retval; |
|
392 } |
|
393 |
2496
|
394 DEFUN (fnmatch, args, , |
|
395 "fnmatch (PATTERN, STRING)\n\ |
|
396 \n\ |
|
397 Return 1 or zero for each element of STRING that matches any of the\n\ |
|
398 elements of the string array PATTERN, using the rules of filename\n\ |
|
399 pattern matching.") |
|
400 { |
|
401 octave_value retval; |
|
402 |
|
403 if (args.length () == 2) |
|
404 { |
|
405 string_vector pat = args(0).all_strings (); |
|
406 string_vector str = args(1).all_strings (); |
|
407 |
|
408 if (error_state) |
|
409 gripe_wrong_type_arg ("fnmatch", args(0)); |
|
410 else |
|
411 { |
2926
|
412 glob_match pattern (file_ops::tilde_expand (pat)); |
2496
|
413 |
|
414 Array<bool> tmp = pattern.match (str); |
|
415 |
|
416 int n = tmp.length (); |
|
417 |
|
418 ColumnVector result (n); |
|
419 |
|
420 for (int i = 0; i < n; i++) |
|
421 result(i) = tmp(i); |
|
422 |
|
423 retval = octave_value (result, true); |
|
424 } |
|
425 } |
|
426 else |
|
427 print_usage ("fnmatch"); |
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
523
|
432 /* |
|
433 ;;; Local Variables: *** |
|
434 ;;; mode: C++ *** |
|
435 ;;; End: *** |
|
436 */ |