2313
|
1 ## Copyright (C) 1996 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
2079
|
19 |
2311
|
20 ## usage: [IN, OUT, PID] = popen2 (COMMAND, ARGS) |
|
21 ## |
|
22 ## Start a subprocess with two-way communication. COMMAND specifies |
|
23 ## the name of the command to start. ARGS is an array of strings |
|
24 ## containing options for COMMAND. IN and out are the file ids of the |
|
25 ## input and streams for the subprocess, and PID is the process id of |
|
26 ## the subprocess, or -1 if COMMAND could not be executed. |
|
27 ## |
|
28 ## Example: |
|
29 ## |
|
30 ## [in, out, pid] = popen2 ("sort", "-nr"); |
|
31 ## fputs (in, "these\n"); |
|
32 ## fputs (in, "are\n"); |
|
33 ## fputs (in, "some\n"); |
|
34 ## fputs (in, "strings\n"); |
|
35 ## fclose (in); |
|
36 ## while (isstr (s = fgets (out))) |
|
37 ## fputs (stdout, s); |
|
38 ## endwhile |
|
39 ## fclose (out); |
2079
|
40 |
2311
|
41 function [in, out, pid] = popen2 (command, args) |
2079
|
42 |
|
43 in = -1; |
|
44 out = -1; |
|
45 pid = -1; |
|
46 |
|
47 if (nargin == 1 || nargin == 2) |
|
48 |
|
49 if (nargin == 1) |
|
50 args = ""; |
|
51 endif |
|
52 |
|
53 if (isstr (command)) |
|
54 |
|
55 [stdin_pipe, stdin_status] = pipe (); |
|
56 [stdout_pipe, stdout_status] = pipe (); |
|
57 |
|
58 if (stdin_status == 0 && stdout_status == 0) |
|
59 |
|
60 pid = fork (); |
|
61 |
|
62 if (pid == 0) |
|
63 |
|
64 fclose (stdin_pipe (2)); |
|
65 fclose (stdout_pipe (1)); |
|
66 |
|
67 dup2 (stdin_pipe (1), stdin); |
|
68 fclose (stdin_pipe (1)); |
|
69 |
|
70 dup2 (stdout_pipe (2), stdout); |
|
71 fclose (stdout_pipe (2)); |
|
72 |
|
73 if (exec (command, args) < 0) |
|
74 error ("popen2: unable to start process `%s'", command); |
|
75 exit (0); |
|
76 endif |
|
77 |
|
78 elseif (pid) |
|
79 |
|
80 fclose (stdin_pipe (1)); |
|
81 fclose (stdout_pipe (2)); |
|
82 |
|
83 if (fcntl (stdout_pipe (1), __F_SETFL__, __O_NONBLOCK__) < 0) |
|
84 error ("popen2: error setting file mode"); |
|
85 else |
|
86 in = stdin_pipe (2); |
|
87 out = stdout_pipe (1); |
|
88 endif |
|
89 |
|
90 elseif (pid < 0) |
|
91 error ("popen2: fork failed -- unable to create child process"); |
|
92 endif |
|
93 else |
|
94 error ("popen2: pipe creation failed"); |
|
95 endif |
|
96 else |
|
97 error ("popen2: file name must be a string"); |
|
98 endif |
|
99 else |
|
100 usage ("[in, out, pid] = popen2 (command, args)"); |
|
101 endif |
|
102 |
|
103 endfunction |