Mercurial > hg > octave-lyh
comparison src/syscalls.cc @ 6321:363a2f8c9e97
[project @ 2007-02-16 20:26:23 by dbateman]
author | dbateman |
---|---|
date | Fri, 16 Feb 2007 20:27:46 +0000 |
parents | 44f24cf66b95 |
children | 723a32c8ee10 |
comparison
equal
deleted
inserted
replaced
6320:6338017166e4 | 6321:363a2f8c9e97 |
---|---|
58 #include "oct-stdstrm.h" | 58 #include "oct-stdstrm.h" |
59 #include "oct-stream.h" | 59 #include "oct-stream.h" |
60 #include "sysdep.h" | 60 #include "sysdep.h" |
61 #include "utils.h" | 61 #include "utils.h" |
62 #include "variables.h" | 62 #include "variables.h" |
63 #include "input.h" | |
63 | 64 |
64 static Octave_map | 65 static Octave_map |
65 mk_stat_map (const file_stat& fs) | 66 mk_stat_map (const file_stat& fs) |
66 { | 67 { |
67 Octave_map m; | 68 Octave_map m; |
217 else | 218 else |
218 print_usage (); | 219 print_usage (); |
219 | 220 |
220 return retval; | 221 return retval; |
221 } | 222 } |
223 | |
224 DEFUN (popen2, args, , | |
225 "-*- texinfo -*-\n\ | |
226 @deftypefn {Function File} {[@var{in}, @var{out}, @var{pid}] =} popen2 (@var{command}, @var{args})\n\ | |
227 Start a subprocess with two-way communication. The name of the process\n\ | |
228 is given by @var{command}, and @var{args} is an array of strings\n\ | |
229 containing options for the command. The file identifiers for the input\n\ | |
230 and output streams of the subprocess are returned in @var{in} and\n\ | |
231 @var{out}. If execution of the command is successful, @var{pid}\n\ | |
232 contains the process ID of the subprocess. Otherwise, @var{pid} is\n\ | |
233 @minus{}1.\n\ | |
234 \n\ | |
235 For example,\n\ | |
236 \n\ | |
237 @example\n\ | |
238 @group\n\ | |
239 [in, out, pid] = popen2 (\"sort\", \"-nr\");\n\ | |
240 fputs (in, \"these\\nare\\nsome\\nstrings\\n\");\n\ | |
241 fclose (in);\n\ | |
242 EAGAIN = errno (\"EAGAIN\");\n\ | |
243 done = false;\n\ | |
244 do\n\ | |
245 s = fgets (out);\n\ | |
246 if (ischar (s))\n\ | |
247 fputs (stdout, s);\n\ | |
248 elseif (errno () == EAGAIN)\n\ | |
249 sleep (0.1);\n\ | |
250 fclear (out);\n\ | |
251 else\n\ | |
252 done = true;\n\ | |
253 endif\n\ | |
254 until (done)\n\ | |
255 fclose (out);\n\ | |
256 @print{} are\n\ | |
257 @print{} some\n\ | |
258 @print{} strings\n\ | |
259 @print{} these\n\ | |
260 @end group\n\ | |
261 @end example\n\ | |
262 @end deftypefn") | |
263 { | |
264 octave_value_list retval; | |
265 | |
266 retval(2) = -1; | |
267 retval(1) = Matrix (); | |
268 retval(0) = Matrix (); | |
269 | |
270 int nargin = args.length (); | |
271 | |
272 if (nargin >= 1 && nargin <= 3) | |
273 { | |
274 std::string exec_file = args(0).string_value(); | |
275 | |
276 if (! error_state) | |
277 { | |
278 string_vector arg_list; | |
279 | |
280 if (nargin >= 2) | |
281 { | |
282 string_vector tmp = args(1).all_strings (); | |
283 | |
284 if (! error_state) | |
285 { | |
286 int len = tmp.length (); | |
287 | |
288 arg_list.resize (len + 1); | |
289 | |
290 arg_list[0] = exec_file; | |
291 | |
292 for (int i = 0; i < len; i++) | |
293 arg_list[i+1] = tmp[i]; | |
294 } | |
295 else | |
296 error ("popen2: arguments must be character strings"); | |
297 } | |
298 else | |
299 { | |
300 arg_list.resize (1); | |
301 | |
302 arg_list[0] = exec_file; | |
303 } | |
304 | |
305 if (! error_state) | |
306 { | |
307 bool sync_mode = (nargin == 3 ? args(2).bool_value() : false); | |
308 | |
309 if (! error_state) | |
310 { | |
311 int fildes[2]; | |
312 std::string msg; | |
313 pid_t pid; | |
314 | |
315 pid = octave_syscalls::popen2 (exec_file, arg_list, sync_mode, fildes, msg, interactive); | |
316 if (pid >= 0) | |
317 { | |
318 FILE *ifile = fdopen (fildes[1], "r"); | |
319 FILE *ofile = fdopen (fildes[0], "w"); | |
320 | |
321 std::string nm; | |
322 | |
323 octave_stream is = octave_stdiostream::create (nm, ifile, | |
324 std::ios::in); | |
325 | |
326 octave_stream os = octave_stdiostream::create (nm, ofile, | |
327 std::ios::out); | |
328 | |
329 Cell file_ids (1, 2); | |
330 | |
331 retval(0) = octave_stream_list::insert (os); | |
332 retval(1) = octave_stream_list::insert (is); | |
333 retval(2) = pid; | |
334 } | |
335 else | |
336 error (msg.c_str ()); | |
337 } | |
338 } | |
339 else | |
340 error ("popen2: arguments must be character strings"); | |
341 } | |
342 else | |
343 error ("popen2: first argument must be a string"); | |
344 } | |
345 else | |
346 print_usage (); | |
347 | |
348 return retval; | |
349 } | |
350 | |
351 /* | |
352 | |
353 %!test | |
354 %! if (isunix()) | |
355 %! [in, out, pid] = popen2 ("sort", "-nr"); | |
356 %! EAGAIN = errno ("EAGAIN"); | |
357 %! else | |
358 %! [in, out, pid] = popen2 ("sort", "/R", 1); | |
359 %! EAGAIN = errno ("EINVAL"); | |
360 %! endif | |
361 %! fputs (in, "these\nare\nsome\nstrings\n"); | |
362 %! fclose (in); | |
363 %! done = false; | |
364 %! str = {}; | |
365 %! idx = 0; | |
366 %! do | |
367 %! if (!isunix()) | |
368 %! errno (0); | |
369 %! endif | |
370 %! s = fgets (out); | |
371 %! if (ischar (s)) | |
372 %! idx++; | |
373 %! str{idx} = s; | |
374 %! elseif (errno () == EAGAIN) | |
375 %! sleep (0.1); | |
376 %! fclear (out); | |
377 %! else | |
378 %! done = true; | |
379 %! endif | |
380 %! until (done) | |
381 %! fclose (out); | |
382 %! assert(str,{"these\n","strings\n","some\n","are\n"}) | |
383 | |
384 */ | |
222 | 385 |
223 DEFUN (fcntl, args, , | 386 DEFUN (fcntl, args, , |
224 "-*- texinfo -*-\n\ | 387 "-*- texinfo -*-\n\ |
225 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} fcntl (@var{fid}, @var{request}, @var{arg})\n\ | 388 @deftypefn {Built-in Function} {[@var{err}, @var{msg}] =} fcntl (@var{fid}, @var{request}, @var{arg})\n\ |
226 Change the properties of the open file @var{fid}. The following values\n\ | 389 Change the properties of the open file @var{fid}. The following values\n\ |
642 return retval; | 805 return retval; |
643 } | 806 } |
644 | 807 |
645 DEFUN (pipe, args, , | 808 DEFUN (pipe, args, , |
646 "-*- texinfo -*-\n\ | 809 "-*- texinfo -*-\n\ |
647 @deftypefn {Built-in Function} {[@var{file_ids}, @var{err}, @var{msg}] =} pipe ()\n\ | 810 @deftypefn {Built-in Function} {[@var{read_fd}, @var{write_fd}, @var{err}, @var{msg}] =} pipe ()\n\ |
648 Create a pipe and return the vector @var{file_ids}, which corresponding\n\ | 811 Create a pipe and return the reading and writing ends of the pipe\n\ |
649 to the reading and writing ends of the pipe.\n\ | 812 into @var{read_fd} and @var{write_fd} respectively.\n\ |
650 \n\ | 813 \n\ |
651 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ | 814 If successful, @var{err} is 0 and @var{msg} is an empty string.\n\ |
652 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ | 815 Otherwise, @var{err} is nonzero and @var{msg} contains a\n\ |
653 system-dependent error message.\n\ | 816 system-dependent error message.\n\ |
654 @end deftypefn") | 817 @end deftypefn") |
655 { | 818 { |
656 octave_value_list retval; | 819 octave_value_list retval; |
657 | 820 |
658 retval(2) = std::string (); | 821 retval(3) = std::string (); |
822 retval(2) = -1; | |
659 retval(1) = -1; | 823 retval(1) = -1; |
660 retval(0) = Matrix (); | 824 retval(0) = -1; |
661 | 825 |
662 int nargin = args.length (); | 826 int nargin = args.length (); |
663 | 827 |
664 if (nargin == 0) | 828 if (nargin == 0) |
665 { | 829 { |
668 std::string msg; | 832 std::string msg; |
669 | 833 |
670 int status = octave_syscalls::pipe (fid, msg); | 834 int status = octave_syscalls::pipe (fid, msg); |
671 | 835 |
672 if (status < 0) | 836 if (status < 0) |
673 retval(2) = msg; | 837 retval(3) = msg; |
674 else | 838 else |
675 { | 839 { |
676 FILE *ifile = fdopen (fid[0], "r"); | 840 FILE *ifile = fdopen (fid[0], "r"); |
677 FILE *ofile = fdopen (fid[1], "w"); | 841 FILE *ofile = fdopen (fid[1], "w"); |
678 | 842 |
682 std::ios::in); | 846 std::ios::in); |
683 | 847 |
684 octave_stream os = octave_stdiostream::create (nm, ofile, | 848 octave_stream os = octave_stdiostream::create (nm, ofile, |
685 std::ios::out); | 849 std::ios::out); |
686 | 850 |
687 octave_value_list file_ids; | 851 retval(1) = octave_stream_list::insert (os); |
688 | 852 retval(0) = octave_stream_list::insert (is); |
689 file_ids(1) = octave_stream_list::insert (os); | 853 |
690 file_ids(0) = octave_stream_list::insert (is); | 854 retval(2) = status; |
691 | |
692 retval(1) = status; | |
693 retval(0) = octave_value (file_ids); | |
694 } | 855 } |
695 } | 856 } |
696 else | 857 else |
697 print_usage (); | 858 print_usage (); |
698 | 859 |