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