2094
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2094
|
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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 #ifdef HAVE_UNISTD_H |
2442
|
33 #ifdef HAVE_SYS_TYPES_H |
2094
|
34 #include <sys/types.h> |
2442
|
35 #endif |
2094
|
36 #include <unistd.h> |
|
37 #endif |
|
38 |
3174
|
39 #include "lo-mappers.h" |
|
40 #include "lo-utils.h" |
2094
|
41 #include "oct-procbuf.h" |
5453
|
42 #include "oct-syscalls.h" |
3176
|
43 #include "variables.h" |
2094
|
44 |
3174
|
45 #include "defun.h" |
|
46 #include "gripes.h" |
3308
|
47 #include "utils.h" |
3174
|
48 |
|
49 // Number of microseconds to delay in the parent after forking. |
|
50 static int Vkluge_procbuf_delay = 0; |
|
51 |
2094
|
52 // This class is based on the procbuf class from libg++, written by |
|
53 // Per Bothner, Copyright (C) 1993 Free Software Foundation. |
|
54 |
|
55 static octave_procbuf *octave_procbuf_list = 0; |
|
56 |
5451
|
57 #if defined (__CYGWIN32__) |
|
58 #define W32POPEN popen |
|
59 #define W32PCLOSE pclose |
|
60 #elif defined (__MINGW32__) |
|
61 #define W32POPEN _popen |
|
62 #define W32PCLOSE _pclose |
|
63 #endif |
|
64 |
2094
|
65 octave_procbuf * |
|
66 octave_procbuf::open (const char *command, int mode) |
|
67 { |
5451
|
68 #if defined (__CYGWIN32__) || defined (__MINGW32__) |
4472
|
69 |
|
70 if (is_open ()) |
|
71 return 0; |
|
72 |
5451
|
73 f = ::W32POPEN (command, (mode & std::ios::in) ? "r" : "w"); |
4472
|
74 |
|
75 if (! f) |
|
76 return 0; |
|
77 |
|
78 // Oops... popen doesn't return the associated pid, so fake it for now |
|
79 |
|
80 proc_pid = 1; |
|
81 |
|
82 open_p = true; |
|
83 |
|
84 if (mode & std::ios::out) |
|
85 ::setvbuf (f, 0, _IOLBF, 0); |
|
86 |
|
87 return this; |
|
88 |
|
89 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
90 |
|
91 int pipe_fds[2]; |
|
92 |
3544
|
93 volatile int child_std_end = (mode & std::ios::in) ? 1 : 0; |
3156
|
94 |
3147
|
95 volatile int parent_end, child_end; |
2094
|
96 |
|
97 if (is_open ()) |
|
98 return 0; |
|
99 |
|
100 if (pipe (pipe_fds) < 0) |
|
101 return 0; |
|
102 |
3544
|
103 if (mode & std::ios::in) |
2094
|
104 { |
|
105 parent_end = pipe_fds[0]; |
|
106 child_end = pipe_fds[1]; |
|
107 } |
|
108 else |
|
109 { |
|
110 parent_end = pipe_fds[1]; |
|
111 child_end = pipe_fds[0]; |
|
112 } |
|
113 |
3156
|
114 proc_pid = ::fork (); |
2094
|
115 |
|
116 if (proc_pid == 0) |
|
117 { |
|
118 ::close (parent_end); |
|
119 |
|
120 if (child_end != child_std_end) |
|
121 { |
3156
|
122 ::dup2 (child_end, child_std_end); |
2094
|
123 ::close (child_end); |
|
124 } |
|
125 |
|
126 while (octave_procbuf_list) |
|
127 { |
3644
|
128 FILE *fp = octave_procbuf_list->f; |
|
129 |
|
130 if (fp) |
|
131 { |
|
132 ::fclose (fp); |
|
133 fp = 0; |
|
134 } |
|
135 |
2094
|
136 octave_procbuf_list = octave_procbuf_list->next; |
|
137 } |
|
138 |
5510
|
139 execl ("/bin/sh", "sh", "-c", command, static_cast<void *> (0)); |
2094
|
140 |
|
141 exit (127); |
|
142 } |
|
143 |
3174
|
144 if (Vkluge_procbuf_delay > 0) |
3308
|
145 octave_usleep (Vkluge_procbuf_delay); |
3174
|
146 |
2094
|
147 ::close (child_end); |
|
148 |
|
149 if (proc_pid < 0) |
|
150 { |
|
151 ::close (parent_end); |
|
152 return 0; |
|
153 } |
|
154 |
3632
|
155 f = ::fdopen (parent_end, (mode & std::ios::in) ? "r" : "w"); |
3631
|
156 |
3649
|
157 if (mode & std::ios::out) |
|
158 ::setvbuf (f, 0, _IOLBF, 0); |
|
159 |
3631
|
160 open_p = true; |
2094
|
161 |
|
162 next = octave_procbuf_list; |
|
163 octave_procbuf_list = this; |
|
164 |
|
165 return this; |
|
166 |
|
167 #else |
|
168 |
|
169 return 0; |
|
170 |
|
171 #endif |
|
172 } |
|
173 |
3631
|
174 octave_procbuf * |
|
175 octave_procbuf::close (void) |
2094
|
176 { |
5451
|
177 |
|
178 |
|
179 #if defined (__CYGWIN32__) || defined (__MINGW32__) |
4472
|
180 |
|
181 if (f) |
|
182 { |
5451
|
183 wstatus = ::W32PCLOSE (f); |
4472
|
184 f = 0; |
|
185 } |
|
186 |
|
187 open_p = false; |
|
188 |
|
189 return this; |
|
190 |
|
191 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
192 |
3644
|
193 if (f) |
|
194 { |
|
195 pid_t wait_pid; |
2094
|
196 |
3644
|
197 int status = -1; |
|
198 |
|
199 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
200 *ptr != 0; |
|
201 ptr = &(*ptr)->next) |
2094
|
202 { |
3644
|
203 if (*ptr == this) |
|
204 { |
|
205 *ptr = (*ptr)->next; |
|
206 status = 0; |
|
207 break; |
|
208 } |
2094
|
209 } |
|
210 |
3644
|
211 if (status == 0 && ::fclose (f) == 0) |
|
212 { |
|
213 using namespace std; |
3531
|
214 |
3644
|
215 do |
|
216 { |
5453
|
217 wait_pid = octave_syscalls::waitpid (proc_pid, &wstatus, 0); |
3644
|
218 } |
|
219 while (wait_pid == -1 && errno == EINTR); |
3631
|
220 } |
3644
|
221 |
|
222 f = 0; |
3631
|
223 } |
2094
|
224 |
3631
|
225 open_p = false; |
2094
|
226 |
3631
|
227 return this; |
2094
|
228 |
|
229 #else |
|
230 |
3631
|
231 return 0; |
2094
|
232 |
|
233 #endif |
|
234 } |
|
235 |
3174
|
236 static int |
|
237 kluge_procbuf_delay (void) |
|
238 { |
|
239 double val; |
3447
|
240 if (builtin_real_scalar_variable ("__kluge_procbuf_delay__", val) |
3174
|
241 && ! xisnan (val)) |
|
242 { |
|
243 int ival = NINT (val); |
|
244 if (ival >= 0 && (double) ival == val) |
|
245 { |
|
246 Vkluge_procbuf_delay = ival; |
|
247 return 0; |
|
248 } |
|
249 } |
3447
|
250 gripe_invalid_value_specified ("__kluge_procbuf_delay__"); |
3174
|
251 return -1; |
|
252 } |
|
253 |
3195
|
254 void |
3174
|
255 symbols_of_oct_procbuf (void) |
|
256 { |
4233
|
257 DEFVAR (__kluge_procbuf_delay__, Vkluge_procbuf_delay, kluge_procbuf_delay, |
3447
|
258 "-*- texinfo -*-\n\ |
|
259 @defvr __kluge_procbuf_delay__\n\ |
|
260 Number of microseconds to delay in the parent after forking.\n\ |
|
261 @end defvr"); |
|
262 |
3174
|
263 } |
|
264 |
2094
|
265 /* |
|
266 ;;; Local Variables: *** |
|
267 ;;; mode: C++ *** |
|
268 ;;; End: *** |
|
269 */ |