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" |
5453
|
42 #include "oct-syscalls.h" |
3176
|
43 #include "variables.h" |
2094
|
44 |
3174
|
45 #include "defun.h" |
|
46 #include "gripes.h" |
3308
|
47 #include "utils.h" |
3174
|
48 |
2094
|
49 // This class is based on the procbuf class from libg++, written by |
|
50 // Per Bothner, Copyright (C) 1993 Free Software Foundation. |
|
51 |
|
52 static octave_procbuf *octave_procbuf_list = 0; |
|
53 |
5621
|
54 #if defined (__CYGWIN__) |
5451
|
55 #define W32POPEN popen |
|
56 #define W32PCLOSE pclose |
|
57 #elif defined (__MINGW32__) |
|
58 #define W32POPEN _popen |
|
59 #define W32PCLOSE _pclose |
|
60 #endif |
|
61 |
2094
|
62 octave_procbuf * |
|
63 octave_procbuf::open (const char *command, int mode) |
|
64 { |
5621
|
65 #if defined (__CYGWIN__) || defined (__MINGW32__) |
4472
|
66 |
|
67 if (is_open ()) |
|
68 return 0; |
|
69 |
5451
|
70 f = ::W32POPEN (command, (mode & std::ios::in) ? "r" : "w"); |
4472
|
71 |
|
72 if (! f) |
|
73 return 0; |
|
74 |
|
75 // Oops... popen doesn't return the associated pid, so fake it for now |
|
76 |
|
77 proc_pid = 1; |
|
78 |
|
79 open_p = true; |
|
80 |
|
81 if (mode & std::ios::out) |
|
82 ::setvbuf (f, 0, _IOLBF, 0); |
|
83 |
|
84 return this; |
|
85 |
|
86 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
87 |
|
88 int pipe_fds[2]; |
|
89 |
3544
|
90 volatile int child_std_end = (mode & std::ios::in) ? 1 : 0; |
3156
|
91 |
3147
|
92 volatile int parent_end, child_end; |
2094
|
93 |
|
94 if (is_open ()) |
|
95 return 0; |
|
96 |
|
97 if (pipe (pipe_fds) < 0) |
|
98 return 0; |
|
99 |
3544
|
100 if (mode & std::ios::in) |
2094
|
101 { |
|
102 parent_end = pipe_fds[0]; |
|
103 child_end = pipe_fds[1]; |
|
104 } |
|
105 else |
|
106 { |
|
107 parent_end = pipe_fds[1]; |
|
108 child_end = pipe_fds[0]; |
|
109 } |
|
110 |
3156
|
111 proc_pid = ::fork (); |
2094
|
112 |
|
113 if (proc_pid == 0) |
|
114 { |
|
115 ::close (parent_end); |
|
116 |
|
117 if (child_end != child_std_end) |
|
118 { |
3156
|
119 ::dup2 (child_end, child_std_end); |
2094
|
120 ::close (child_end); |
|
121 } |
|
122 |
|
123 while (octave_procbuf_list) |
|
124 { |
3644
|
125 FILE *fp = octave_procbuf_list->f; |
|
126 |
|
127 if (fp) |
|
128 { |
|
129 ::fclose (fp); |
|
130 fp = 0; |
|
131 } |
|
132 |
2094
|
133 octave_procbuf_list = octave_procbuf_list->next; |
|
134 } |
|
135 |
5510
|
136 execl ("/bin/sh", "sh", "-c", command, static_cast<void *> (0)); |
2094
|
137 |
|
138 exit (127); |
|
139 } |
|
140 |
|
141 ::close (child_end); |
|
142 |
|
143 if (proc_pid < 0) |
|
144 { |
|
145 ::close (parent_end); |
|
146 return 0; |
|
147 } |
|
148 |
3632
|
149 f = ::fdopen (parent_end, (mode & std::ios::in) ? "r" : "w"); |
3631
|
150 |
3649
|
151 if (mode & std::ios::out) |
|
152 ::setvbuf (f, 0, _IOLBF, 0); |
|
153 |
3631
|
154 open_p = true; |
2094
|
155 |
|
156 next = octave_procbuf_list; |
|
157 octave_procbuf_list = this; |
|
158 |
|
159 return this; |
|
160 |
|
161 #else |
|
162 |
|
163 return 0; |
|
164 |
|
165 #endif |
|
166 } |
|
167 |
3631
|
168 octave_procbuf * |
|
169 octave_procbuf::close (void) |
2094
|
170 { |
5451
|
171 |
|
172 |
5621
|
173 #if defined (__CYGWIN__) || defined (__MINGW32__) |
4472
|
174 |
|
175 if (f) |
|
176 { |
5451
|
177 wstatus = ::W32PCLOSE (f); |
4472
|
178 f = 0; |
|
179 } |
|
180 |
|
181 open_p = false; |
|
182 |
|
183 return this; |
|
184 |
|
185 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
186 |
3644
|
187 if (f) |
|
188 { |
|
189 pid_t wait_pid; |
2094
|
190 |
3644
|
191 int status = -1; |
|
192 |
|
193 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
194 *ptr != 0; |
|
195 ptr = &(*ptr)->next) |
2094
|
196 { |
3644
|
197 if (*ptr == this) |
|
198 { |
|
199 *ptr = (*ptr)->next; |
|
200 status = 0; |
|
201 break; |
|
202 } |
2094
|
203 } |
|
204 |
3644
|
205 if (status == 0 && ::fclose (f) == 0) |
|
206 { |
|
207 using namespace std; |
3531
|
208 |
3644
|
209 do |
|
210 { |
5453
|
211 wait_pid = octave_syscalls::waitpid (proc_pid, &wstatus, 0); |
3644
|
212 } |
|
213 while (wait_pid == -1 && errno == EINTR); |
3631
|
214 } |
3644
|
215 |
|
216 f = 0; |
3631
|
217 } |
2094
|
218 |
3631
|
219 open_p = false; |
2094
|
220 |
3631
|
221 return this; |
2094
|
222 |
|
223 #else |
|
224 |
3631
|
225 return 0; |
2094
|
226 |
|
227 #endif |
|
228 } |
|
229 |
|
230 /* |
|
231 ;;; Local Variables: *** |
|
232 ;;; mode: C++ *** |
|
233 ;;; End: *** |
|
234 */ |