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