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 // It should work with the filebuf class from libg++, but it might not |
|
59 // work with others since it depends on being able to get at the |
|
60 // underlying file descriptor with filebuf::fd(), which is not |
|
61 // standard. |
|
62 |
|
63 static octave_procbuf *octave_procbuf_list = 0; |
|
64 |
|
65 octave_procbuf * |
|
66 octave_procbuf::open (const char *command, int mode) |
|
67 { |
|
68 #if defined (HAVE_SYS_WAIT_H) |
|
69 |
|
70 int pipe_fds[2]; |
|
71 |
3156
|
72 volatile int child_std_end = (mode & ios::in) ? 1 : 0; |
|
73 |
3147
|
74 volatile int parent_end, child_end; |
2094
|
75 |
|
76 if (is_open ()) |
|
77 return 0; |
|
78 |
|
79 if (pipe (pipe_fds) < 0) |
|
80 return 0; |
|
81 |
|
82 if (mode & ios::in) |
|
83 { |
|
84 parent_end = pipe_fds[0]; |
|
85 child_end = pipe_fds[1]; |
|
86 } |
|
87 else |
|
88 { |
|
89 parent_end = pipe_fds[1]; |
|
90 child_end = pipe_fds[0]; |
|
91 } |
|
92 |
3156
|
93 proc_pid = ::fork (); |
2094
|
94 |
|
95 if (proc_pid == 0) |
|
96 { |
|
97 ::close (parent_end); |
|
98 |
|
99 if (child_end != child_std_end) |
|
100 { |
3156
|
101 ::dup2 (child_end, child_std_end); |
2094
|
102 ::close (child_end); |
|
103 } |
|
104 |
|
105 while (octave_procbuf_list) |
|
106 { |
|
107 ::close (octave_procbuf_list->fd ()); |
|
108 octave_procbuf_list = octave_procbuf_list->next; |
|
109 } |
|
110 |
|
111 execl ("/bin/sh", "sh", "-c", command, NULL); |
|
112 |
|
113 exit (127); |
|
114 } |
|
115 |
3174
|
116 if (Vkluge_procbuf_delay > 0) |
3308
|
117 octave_usleep (Vkluge_procbuf_delay); |
3174
|
118 |
2094
|
119 ::close (child_end); |
|
120 |
|
121 if (proc_pid < 0) |
|
122 { |
|
123 ::close (parent_end); |
|
124 return 0; |
|
125 } |
|
126 |
|
127 attach (parent_end); |
|
128 |
|
129 next = octave_procbuf_list; |
|
130 octave_procbuf_list = this; |
|
131 |
|
132 return this; |
|
133 |
|
134 #else |
|
135 |
|
136 return 0; |
|
137 |
|
138 #endif |
|
139 } |
|
140 |
|
141 int |
|
142 octave_procbuf::sys_close (void) |
|
143 { |
|
144 #if defined (HAVE_SYS_WAIT_H) |
|
145 |
|
146 pid_t wait_pid; |
|
147 |
|
148 int status = -1; |
|
149 |
|
150 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
151 *ptr != 0; |
|
152 ptr = &(*ptr)->next) |
|
153 { |
|
154 if (*ptr == this) |
|
155 { |
|
156 *ptr = (*ptr)->next; |
|
157 status = 0; |
|
158 break; |
|
159 } |
|
160 } |
|
161 |
|
162 if (status < 0 || ::close (fd ()) < 0) |
|
163 return -1; |
|
164 |
3531
|
165 { |
|
166 using namespace std; |
|
167 |
|
168 do |
|
169 { |
|
170 wait_pid = ::waitpid (proc_pid, &wstatus, 0); |
|
171 } |
|
172 while (wait_pid == -1 && errno == EINTR); |
|
173 } |
2094
|
174 |
|
175 if (wait_pid == -1) |
|
176 return -1; |
|
177 |
|
178 return wstatus; |
|
179 |
|
180 #else |
|
181 |
|
182 return -1; |
|
183 |
|
184 #endif |
|
185 } |
|
186 |
3174
|
187 static int |
|
188 kluge_procbuf_delay (void) |
|
189 { |
|
190 double val; |
3447
|
191 if (builtin_real_scalar_variable ("__kluge_procbuf_delay__", val) |
3174
|
192 && ! xisnan (val)) |
|
193 { |
|
194 int ival = NINT (val); |
|
195 if (ival >= 0 && (double) ival == val) |
|
196 { |
|
197 Vkluge_procbuf_delay = ival; |
|
198 return 0; |
|
199 } |
|
200 } |
3447
|
201 gripe_invalid_value_specified ("__kluge_procbuf_delay__"); |
3174
|
202 return -1; |
|
203 } |
|
204 |
3195
|
205 void |
3174
|
206 symbols_of_oct_procbuf (void) |
|
207 { |
3447
|
208 DEFVAR (__kluge_procbuf_delay__, static_cast<double> (Vkluge_procbuf_delay), |
3258
|
209 kluge_procbuf_delay, |
3447
|
210 "-*- texinfo -*-\n\ |
|
211 @defvr __kluge_procbuf_delay__\n\ |
|
212 Number of microseconds to delay in the parent after forking.\n\ |
|
213 @end defvr"); |
|
214 |
3174
|
215 } |
|
216 |
2094
|
217 /* |
|
218 ;;; Local Variables: *** |
|
219 ;;; mode: C++ *** |
|
220 ;;; End: *** |
|
221 */ |