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