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" |
|
42 #include "syswait.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 |
|
57 octave_procbuf * |
|
58 octave_procbuf::open (const char *command, int mode) |
|
59 { |
4472
|
60 #if defined (__CYGWIN32__) |
|
61 |
|
62 if (is_open ()) |
|
63 return 0; |
|
64 |
|
65 f = popen (command, (mode & std::ios::in) ? "r" : "w"); |
|
66 |
|
67 if (! f) |
|
68 return 0; |
|
69 |
|
70 // Oops... popen doesn't return the associated pid, so fake it for now |
|
71 |
|
72 proc_pid = 1; |
|
73 |
|
74 open_p = true; |
|
75 |
|
76 if (mode & std::ios::out) |
|
77 ::setvbuf (f, 0, _IOLBF, 0); |
|
78 |
|
79 return this; |
|
80 |
|
81 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
82 |
|
83 int pipe_fds[2]; |
|
84 |
3544
|
85 volatile int child_std_end = (mode & std::ios::in) ? 1 : 0; |
3156
|
86 |
3147
|
87 volatile int parent_end, child_end; |
2094
|
88 |
|
89 if (is_open ()) |
|
90 return 0; |
|
91 |
|
92 if (pipe (pipe_fds) < 0) |
|
93 return 0; |
|
94 |
3544
|
95 if (mode & std::ios::in) |
2094
|
96 { |
|
97 parent_end = pipe_fds[0]; |
|
98 child_end = pipe_fds[1]; |
|
99 } |
|
100 else |
|
101 { |
|
102 parent_end = pipe_fds[1]; |
|
103 child_end = pipe_fds[0]; |
|
104 } |
|
105 |
3156
|
106 proc_pid = ::fork (); |
2094
|
107 |
|
108 if (proc_pid == 0) |
|
109 { |
|
110 ::close (parent_end); |
|
111 |
|
112 if (child_end != child_std_end) |
|
113 { |
3156
|
114 ::dup2 (child_end, child_std_end); |
2094
|
115 ::close (child_end); |
|
116 } |
|
117 |
|
118 while (octave_procbuf_list) |
|
119 { |
3644
|
120 FILE *fp = octave_procbuf_list->f; |
|
121 |
|
122 if (fp) |
|
123 { |
|
124 ::fclose (fp); |
|
125 fp = 0; |
|
126 } |
|
127 |
2094
|
128 octave_procbuf_list = octave_procbuf_list->next; |
|
129 } |
|
130 |
3956
|
131 execl ("/bin/sh", "sh", "-c", command, 0); |
2094
|
132 |
|
133 exit (127); |
|
134 } |
|
135 |
3174
|
136 if (Vkluge_procbuf_delay > 0) |
3308
|
137 octave_usleep (Vkluge_procbuf_delay); |
3174
|
138 |
2094
|
139 ::close (child_end); |
|
140 |
|
141 if (proc_pid < 0) |
|
142 { |
|
143 ::close (parent_end); |
|
144 return 0; |
|
145 } |
|
146 |
3632
|
147 f = ::fdopen (parent_end, (mode & std::ios::in) ? "r" : "w"); |
3631
|
148 |
3649
|
149 if (mode & std::ios::out) |
|
150 ::setvbuf (f, 0, _IOLBF, 0); |
|
151 |
3631
|
152 open_p = true; |
2094
|
153 |
|
154 next = octave_procbuf_list; |
|
155 octave_procbuf_list = this; |
|
156 |
|
157 return this; |
|
158 |
|
159 #else |
|
160 |
|
161 return 0; |
|
162 |
|
163 #endif |
|
164 } |
|
165 |
3631
|
166 octave_procbuf * |
|
167 octave_procbuf::close (void) |
2094
|
168 { |
4472
|
169 #if defined (__CYGWIN32__) |
|
170 |
|
171 if (f) |
|
172 { |
|
173 wstatus = ::pclose (f); |
|
174 f = 0; |
|
175 } |
|
176 |
|
177 open_p = false; |
|
178 |
|
179 return this; |
|
180 |
|
181 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
182 |
3644
|
183 if (f) |
|
184 { |
|
185 pid_t wait_pid; |
2094
|
186 |
3644
|
187 int status = -1; |
|
188 |
|
189 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
190 *ptr != 0; |
|
191 ptr = &(*ptr)->next) |
2094
|
192 { |
3644
|
193 if (*ptr == this) |
|
194 { |
|
195 *ptr = (*ptr)->next; |
|
196 status = 0; |
|
197 break; |
|
198 } |
2094
|
199 } |
|
200 |
3644
|
201 if (status == 0 && ::fclose (f) == 0) |
|
202 { |
|
203 using namespace std; |
3531
|
204 |
3644
|
205 do |
|
206 { |
|
207 wait_pid = ::waitpid (proc_pid, &wstatus, 0); |
|
208 } |
|
209 while (wait_pid == -1 && errno == EINTR); |
3631
|
210 } |
3644
|
211 |
|
212 f = 0; |
3631
|
213 } |
2094
|
214 |
3631
|
215 open_p = false; |
2094
|
216 |
3631
|
217 return this; |
2094
|
218 |
|
219 #else |
|
220 |
3631
|
221 return 0; |
2094
|
222 |
|
223 #endif |
|
224 } |
|
225 |
3174
|
226 static int |
|
227 kluge_procbuf_delay (void) |
|
228 { |
|
229 double val; |
3447
|
230 if (builtin_real_scalar_variable ("__kluge_procbuf_delay__", val) |
3174
|
231 && ! xisnan (val)) |
|
232 { |
|
233 int ival = NINT (val); |
|
234 if (ival >= 0 && (double) ival == val) |
|
235 { |
|
236 Vkluge_procbuf_delay = ival; |
|
237 return 0; |
|
238 } |
|
239 } |
3447
|
240 gripe_invalid_value_specified ("__kluge_procbuf_delay__"); |
3174
|
241 return -1; |
|
242 } |
|
243 |
3195
|
244 void |
3174
|
245 symbols_of_oct_procbuf (void) |
|
246 { |
4233
|
247 DEFVAR (__kluge_procbuf_delay__, Vkluge_procbuf_delay, kluge_procbuf_delay, |
3447
|
248 "-*- texinfo -*-\n\ |
|
249 @defvr __kluge_procbuf_delay__\n\ |
|
250 Number of microseconds to delay in the parent after forking.\n\ |
|
251 @end defvr"); |
|
252 |
3174
|
253 } |
|
254 |
2094
|
255 /* |
|
256 ;;; Local Variables: *** |
|
257 ;;; mode: C++ *** |
|
258 ;;; End: *** |
|
259 */ |