2094
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005, 2006 |
|
4 2007 John W. Eaton |
2094
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2094
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2094
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cerrno> |
|
29 |
3503
|
30 #include <iostream> |
2094
|
31 |
|
32 #include <sys/types.h> |
|
33 #include <unistd.h> |
|
34 |
3174
|
35 #include "lo-mappers.h" |
|
36 #include "lo-utils.h" |
2094
|
37 #include "oct-procbuf.h" |
5453
|
38 #include "oct-syscalls.h" |
6726
|
39 #include "sysdep.h" |
3176
|
40 #include "variables.h" |
2094
|
41 |
3174
|
42 #include "defun.h" |
|
43 #include "gripes.h" |
3308
|
44 #include "utils.h" |
3174
|
45 |
2094
|
46 // This class is based on the procbuf class from libg++, written by |
|
47 // Per Bothner, Copyright (C) 1993 Free Software Foundation. |
|
48 |
|
49 static octave_procbuf *octave_procbuf_list = 0; |
|
50 |
6094
|
51 #ifndef BUFSIZ |
|
52 #define BUFSIZ 1024 |
|
53 #endif |
|
54 |
2094
|
55 octave_procbuf * |
|
56 octave_procbuf::open (const char *command, int mode) |
|
57 { |
6096
|
58 #if defined (__CYGWIN__) || defined (__MINGW32__) || defined (_MSC_VER) |
4472
|
59 |
|
60 if (is_open ()) |
|
61 return 0; |
|
62 |
6726
|
63 f = octave_popen (command, (mode & std::ios::in) ? "r" : "w"); |
4472
|
64 |
|
65 if (! f) |
|
66 return 0; |
|
67 |
|
68 // Oops... popen doesn't return the associated pid, so fake it for now |
|
69 |
|
70 proc_pid = 1; |
|
71 |
|
72 open_p = true; |
|
73 |
|
74 if (mode & std::ios::out) |
6094
|
75 ::setvbuf (f, 0, _IOLBF, BUFSIZ); |
4472
|
76 |
|
77 return this; |
|
78 |
|
79 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
80 |
|
81 int pipe_fds[2]; |
|
82 |
3544
|
83 volatile int child_std_end = (mode & std::ios::in) ? 1 : 0; |
3156
|
84 |
3147
|
85 volatile int parent_end, child_end; |
2094
|
86 |
|
87 if (is_open ()) |
|
88 return 0; |
|
89 |
|
90 if (pipe (pipe_fds) < 0) |
|
91 return 0; |
|
92 |
3544
|
93 if (mode & std::ios::in) |
2094
|
94 { |
|
95 parent_end = pipe_fds[0]; |
|
96 child_end = pipe_fds[1]; |
|
97 } |
|
98 else |
|
99 { |
|
100 parent_end = pipe_fds[1]; |
|
101 child_end = pipe_fds[0]; |
|
102 } |
|
103 |
3156
|
104 proc_pid = ::fork (); |
2094
|
105 |
|
106 if (proc_pid == 0) |
|
107 { |
|
108 ::close (parent_end); |
|
109 |
|
110 if (child_end != child_std_end) |
|
111 { |
3156
|
112 ::dup2 (child_end, child_std_end); |
2094
|
113 ::close (child_end); |
|
114 } |
|
115 |
|
116 while (octave_procbuf_list) |
|
117 { |
3644
|
118 FILE *fp = octave_procbuf_list->f; |
|
119 |
|
120 if (fp) |
|
121 { |
|
122 ::fclose (fp); |
|
123 fp = 0; |
|
124 } |
|
125 |
2094
|
126 octave_procbuf_list = octave_procbuf_list->next; |
|
127 } |
|
128 |
5510
|
129 execl ("/bin/sh", "sh", "-c", command, static_cast<void *> (0)); |
2094
|
130 |
|
131 exit (127); |
|
132 } |
|
133 |
|
134 ::close (child_end); |
|
135 |
|
136 if (proc_pid < 0) |
|
137 { |
|
138 ::close (parent_end); |
|
139 return 0; |
|
140 } |
|
141 |
3632
|
142 f = ::fdopen (parent_end, (mode & std::ios::in) ? "r" : "w"); |
3631
|
143 |
3649
|
144 if (mode & std::ios::out) |
6094
|
145 ::setvbuf (f, 0, _IOLBF, BUFSIZ); |
3649
|
146 |
3631
|
147 open_p = true; |
2094
|
148 |
|
149 next = octave_procbuf_list; |
|
150 octave_procbuf_list = this; |
|
151 |
|
152 return this; |
|
153 |
|
154 #else |
|
155 |
|
156 return 0; |
|
157 |
|
158 #endif |
|
159 } |
|
160 |
3631
|
161 octave_procbuf * |
|
162 octave_procbuf::close (void) |
2094
|
163 { |
6096
|
164 #if defined (__CYGWIN__) || defined (__MINGW32__) || defined (_MSC_VER) |
4472
|
165 |
|
166 if (f) |
|
167 { |
6726
|
168 wstatus = octave_pclose (f); |
4472
|
169 f = 0; |
|
170 } |
|
171 |
|
172 open_p = false; |
|
173 |
|
174 return this; |
|
175 |
|
176 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
177 |
3644
|
178 if (f) |
|
179 { |
|
180 pid_t wait_pid; |
2094
|
181 |
3644
|
182 int status = -1; |
|
183 |
|
184 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
185 *ptr != 0; |
|
186 ptr = &(*ptr)->next) |
2094
|
187 { |
3644
|
188 if (*ptr == this) |
|
189 { |
|
190 *ptr = (*ptr)->next; |
|
191 status = 0; |
|
192 break; |
|
193 } |
2094
|
194 } |
|
195 |
3644
|
196 if (status == 0 && ::fclose (f) == 0) |
|
197 { |
|
198 using namespace std; |
3531
|
199 |
3644
|
200 do |
|
201 { |
5453
|
202 wait_pid = octave_syscalls::waitpid (proc_pid, &wstatus, 0); |
3644
|
203 } |
|
204 while (wait_pid == -1 && errno == EINTR); |
3631
|
205 } |
3644
|
206 |
|
207 f = 0; |
3631
|
208 } |
2094
|
209 |
3631
|
210 open_p = false; |
2094
|
211 |
3631
|
212 return this; |
2094
|
213 |
|
214 #else |
|
215 |
3631
|
216 return 0; |
2094
|
217 |
|
218 #endif |
|
219 } |