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 { |
3631
|
102 ::fclose (octave_procbuf_list->f); |
2094
|
103 octave_procbuf_list = octave_procbuf_list->next; |
|
104 } |
|
105 |
|
106 execl ("/bin/sh", "sh", "-c", command, NULL); |
|
107 |
|
108 exit (127); |
|
109 } |
|
110 |
3174
|
111 if (Vkluge_procbuf_delay > 0) |
3308
|
112 octave_usleep (Vkluge_procbuf_delay); |
3174
|
113 |
2094
|
114 ::close (child_end); |
|
115 |
|
116 if (proc_pid < 0) |
|
117 { |
|
118 ::close (parent_end); |
|
119 return 0; |
|
120 } |
|
121 |
3632
|
122 f = ::fdopen (parent_end, (mode & std::ios::in) ? "r" : "w"); |
3631
|
123 |
|
124 open_p = true; |
2094
|
125 |
|
126 next = octave_procbuf_list; |
|
127 octave_procbuf_list = this; |
|
128 |
|
129 return this; |
|
130 |
|
131 #else |
|
132 |
|
133 return 0; |
|
134 |
|
135 #endif |
|
136 } |
|
137 |
3631
|
138 octave_procbuf * |
|
139 octave_procbuf::close (void) |
2094
|
140 { |
|
141 #if defined (HAVE_SYS_WAIT_H) |
|
142 |
|
143 pid_t wait_pid; |
|
144 |
|
145 int status = -1; |
|
146 |
|
147 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
148 *ptr != 0; |
|
149 ptr = &(*ptr)->next) |
|
150 { |
|
151 if (*ptr == this) |
|
152 { |
|
153 *ptr = (*ptr)->next; |
|
154 status = 0; |
|
155 break; |
|
156 } |
|
157 } |
|
158 |
3631
|
159 if (status == 0 && ::fclose (f) == 0) |
|
160 { |
|
161 using namespace std; |
3531
|
162 |
3631
|
163 do |
|
164 { |
|
165 wait_pid = ::waitpid (proc_pid, &wstatus, 0); |
|
166 } |
|
167 while (wait_pid == -1 && errno == EINTR); |
|
168 } |
2094
|
169 |
3631
|
170 open_p = false; |
2094
|
171 |
3631
|
172 return this; |
2094
|
173 |
|
174 #else |
|
175 |
3631
|
176 return 0; |
2094
|
177 |
|
178 #endif |
|
179 } |
|
180 |
3174
|
181 static int |
|
182 kluge_procbuf_delay (void) |
|
183 { |
|
184 double val; |
3447
|
185 if (builtin_real_scalar_variable ("__kluge_procbuf_delay__", val) |
3174
|
186 && ! xisnan (val)) |
|
187 { |
|
188 int ival = NINT (val); |
|
189 if (ival >= 0 && (double) ival == val) |
|
190 { |
|
191 Vkluge_procbuf_delay = ival; |
|
192 return 0; |
|
193 } |
|
194 } |
3447
|
195 gripe_invalid_value_specified ("__kluge_procbuf_delay__"); |
3174
|
196 return -1; |
|
197 } |
|
198 |
3195
|
199 void |
3174
|
200 symbols_of_oct_procbuf (void) |
|
201 { |
3447
|
202 DEFVAR (__kluge_procbuf_delay__, static_cast<double> (Vkluge_procbuf_delay), |
3258
|
203 kluge_procbuf_delay, |
3447
|
204 "-*- texinfo -*-\n\ |
|
205 @defvr __kluge_procbuf_delay__\n\ |
|
206 Number of microseconds to delay in the parent after forking.\n\ |
|
207 @end defvr"); |
|
208 |
3174
|
209 } |
|
210 |
2094
|
211 /* |
|
212 ;;; Local Variables: *** |
|
213 ;;; mode: C++ *** |
|
214 ;;; End: *** |
|
215 */ |