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