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