annotate scripts/miscellaneous/popen2.m @ 5899:82c38ce145a7

[project @ 2006-07-19 23:00:22 by jwe]
author jwe
date Wed, 19 Jul 2006 23:00:22 +0000
parents d6163c0effd5
children 34f96dd5441b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2847
8b262e771614 [project @ 1997-03-27 16:18:26 by jwe]
jwe
parents: 2325
diff changeset
1 ## Copyright (C) 1996, 1997 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
6 ## under the terms of the GNU General Public License as published by
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
7 ## the Free Software Foundation; either version 2, or (at your option)
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
8 ## any later version.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
15 ## You should have received a copy of the GNU General Public License
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2311
diff changeset
16 ## along with Octave; see the file COPYING. If not, write to the Free
5307
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5143
diff changeset
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
4c8a2e4e0717 [project @ 2005-04-26 19:24:27 by jwe]
jwe
parents: 5143
diff changeset
18 ## 02110-1301, USA.
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
19
3301
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
20 ## -*- texinfo -*-
3302
bac635def477 [project @ 1999-10-20 04:49:39 by jwe]
jwe
parents: 3301
diff changeset
21 ## @deftypefn {Function File} {[@var{in}, @var{out}, @var{pid}] =} popen2 (@var{command}, @var{args})
3301
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
22 ## Start a subprocess with two-way communication. The name of the process
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
23 ## is given by @var{command}, and @var{args} is an array of strings
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
24 ## containing options for the command. The file identifiers for the input
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
25 ## and output streams of the subprocess are returned in @var{in} and
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
26 ## @var{out}. If execution of the command is successful, @var{pid}
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
27 ## contains the process ID of the subprocess. Otherwise, @var{pid} is
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
28 ## @minus{}1.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
29 ##
3301
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
30 ## For example,
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
31 ##
3301
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
32 ## @example
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
33 ## @group
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
34 ## [in, out, pid] = popen2 ("sort", "-nr");
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
35 ## fputs (in, "these\nare\nsome\nstrings\n");
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
36 ## fclose (in);
5465
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
37 ## EAGAIN = errno ("EAGAIN");
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
38 ## done = false;
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
39 ## do
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
40 ## s = fgets (out);
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
41 ## if (ischar (s))
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
42 ## fputs (stdout, s);
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
43 ## elseif (errno () == EAGAIN)
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
44 ## sleep (0.1);
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
45 ## fclear (out);
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
46 ## else
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
47 ## done = true;
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
48 ## endif
d6163c0effd5 [project @ 2005-09-23 15:42:48 by jwe]
jwe
parents: 5443
diff changeset
49 ## until (done)
3301
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
50 ## fclose (out);
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
51 ## @print{} are
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
52 ## @print{} some
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
53 ## @print{} strings
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
54 ## @print{} these
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
55 ## @end group
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
56 ## @end example
02866242d3ae [project @ 1999-10-20 04:10:46 by jwe]
jwe
parents: 3156
diff changeset
57 ## @end deftypefn
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
58
2314
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
59 ## Author: jwe
949ab8eba8bc [project @ 1996-07-12 03:58:02 by jwe]
jwe
parents: 2313
diff changeset
60
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
61 function [in, out, pid] = popen2 (command, args)
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
62
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
63 in = -1;
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
64 out = -1;
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
65 pid = -1;
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
66
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
67 if (nargin == 1 || nargin == 2)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
68
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
69 if (nargin == 1)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
70 args = "";
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
71 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
72
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5307
diff changeset
73 if (ischar (command))
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
74
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
75 [stdin_pipe, stdin_status] = pipe ();
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
76 [stdout_pipe, stdout_status] = pipe ();
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
77
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
78 if (stdin_status == 0 && stdout_status == 0)
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
79
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
80 pid = fork ();
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
81
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
82 if (pid == 0)
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
83
5143
d6e99e773993 [project @ 2005-02-14 15:48:47 by jwe]
jwe
parents: 4013
diff changeset
84 ## In the child.
d6e99e773993 [project @ 2005-02-14 15:48:47 by jwe]
jwe
parents: 4013
diff changeset
85
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
86 fclose (nth (stdin_pipe, 2));
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
87 fclose (nth (stdout_pipe, 1));
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
88
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
89 dup2 (nth (stdin_pipe, 1), stdin);
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
90 fclose (nth (stdin_pipe, 1));
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
91
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
92 dup2 (nth (stdout_pipe, 2), stdout);
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
93 fclose (nth (stdout_pipe, 2));
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
94
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
95 if (exec (command, args) < 0)
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
96 error ("popen2: unable to start process `%s'", command);
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
97 exit (0);
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
98 endif
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
99
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
100 elseif (pid)
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
101
5143
d6e99e773993 [project @ 2005-02-14 15:48:47 by jwe]
jwe
parents: 4013
diff changeset
102 ## In the parent.
d6e99e773993 [project @ 2005-02-14 15:48:47 by jwe]
jwe
parents: 4013
diff changeset
103
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
104 fclose (nth (stdin_pipe, 1));
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
105 fclose (nth (stdout_pipe, 2));
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
106
4013
9b99d84ded1b [project @ 2002-08-01 20:36:15 by jwe]
jwe
parents: 3483
diff changeset
107 if (fcntl (nth (stdout_pipe, 1), F_SETFL, O_NONBLOCK) < 0)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
108 error ("popen2: error setting file mode");
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
109 else
3483
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
110 in = nth (stdin_pipe, 2);
1bb6e519a4a2 [project @ 2000-01-26 23:37:10 by jwe]
jwe
parents: 3426
diff changeset
111 out = nth (stdout_pipe, 1);
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
112 endif
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
113
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
114 elseif (pid < 0)
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
115 error ("popen2: fork failed -- unable to create child process");
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
116 endif
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
117 else
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3302
diff changeset
118 error ("popen2: pipe creation failed");
2079
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
119 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
120 else
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
121 error ("popen2: file name must be a string");
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
122 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
123 else
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
124 usage ("[in, out, pid] = popen2 (command, args)");
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
125 endif
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
126
6c1effd5dcea [project @ 1996-04-24 07:45:47 by jwe]
jwe
parents:
diff changeset
127 endfunction