2094
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005, 2006 |
|
4 2007 John W. Eaton |
2094
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2094
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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" |
6726
|
43 #include "sysdep.h" |
3176
|
44 #include "variables.h" |
2094
|
45 |
3174
|
46 #include "defun.h" |
|
47 #include "gripes.h" |
3308
|
48 #include "utils.h" |
3174
|
49 |
2094
|
50 // This class is based on the procbuf class from libg++, written by |
|
51 // Per Bothner, Copyright (C) 1993 Free Software Foundation. |
|
52 |
|
53 static octave_procbuf *octave_procbuf_list = 0; |
|
54 |
6094
|
55 #ifndef BUFSIZ |
|
56 #define BUFSIZ 1024 |
|
57 #endif |
|
58 |
2094
|
59 octave_procbuf * |
|
60 octave_procbuf::open (const char *command, int mode) |
|
61 { |
6096
|
62 #if defined (__CYGWIN__) || defined (__MINGW32__) || defined (_MSC_VER) |
4472
|
63 |
|
64 if (is_open ()) |
|
65 return 0; |
|
66 |
6726
|
67 f = octave_popen (command, (mode & std::ios::in) ? "r" : "w"); |
4472
|
68 |
|
69 if (! f) |
|
70 return 0; |
|
71 |
|
72 // Oops... popen doesn't return the associated pid, so fake it for now |
|
73 |
|
74 proc_pid = 1; |
|
75 |
|
76 open_p = true; |
|
77 |
|
78 if (mode & std::ios::out) |
6094
|
79 ::setvbuf (f, 0, _IOLBF, BUFSIZ); |
4472
|
80 |
|
81 return this; |
|
82 |
|
83 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
84 |
|
85 int pipe_fds[2]; |
|
86 |
3544
|
87 volatile int child_std_end = (mode & std::ios::in) ? 1 : 0; |
3156
|
88 |
3147
|
89 volatile int parent_end, child_end; |
2094
|
90 |
|
91 if (is_open ()) |
|
92 return 0; |
|
93 |
|
94 if (pipe (pipe_fds) < 0) |
|
95 return 0; |
|
96 |
3544
|
97 if (mode & std::ios::in) |
2094
|
98 { |
|
99 parent_end = pipe_fds[0]; |
|
100 child_end = pipe_fds[1]; |
|
101 } |
|
102 else |
|
103 { |
|
104 parent_end = pipe_fds[1]; |
|
105 child_end = pipe_fds[0]; |
|
106 } |
|
107 |
3156
|
108 proc_pid = ::fork (); |
2094
|
109 |
|
110 if (proc_pid == 0) |
|
111 { |
|
112 ::close (parent_end); |
|
113 |
|
114 if (child_end != child_std_end) |
|
115 { |
3156
|
116 ::dup2 (child_end, child_std_end); |
2094
|
117 ::close (child_end); |
|
118 } |
|
119 |
|
120 while (octave_procbuf_list) |
|
121 { |
3644
|
122 FILE *fp = octave_procbuf_list->f; |
|
123 |
|
124 if (fp) |
|
125 { |
|
126 ::fclose (fp); |
|
127 fp = 0; |
|
128 } |
|
129 |
2094
|
130 octave_procbuf_list = octave_procbuf_list->next; |
|
131 } |
|
132 |
5510
|
133 execl ("/bin/sh", "sh", "-c", command, static_cast<void *> (0)); |
2094
|
134 |
|
135 exit (127); |
|
136 } |
|
137 |
|
138 ::close (child_end); |
|
139 |
|
140 if (proc_pid < 0) |
|
141 { |
|
142 ::close (parent_end); |
|
143 return 0; |
|
144 } |
|
145 |
3632
|
146 f = ::fdopen (parent_end, (mode & std::ios::in) ? "r" : "w"); |
3631
|
147 |
3649
|
148 if (mode & std::ios::out) |
6094
|
149 ::setvbuf (f, 0, _IOLBF, BUFSIZ); |
3649
|
150 |
3631
|
151 open_p = true; |
2094
|
152 |
|
153 next = octave_procbuf_list; |
|
154 octave_procbuf_list = this; |
|
155 |
|
156 return this; |
|
157 |
|
158 #else |
|
159 |
|
160 return 0; |
|
161 |
|
162 #endif |
|
163 } |
|
164 |
3631
|
165 octave_procbuf * |
|
166 octave_procbuf::close (void) |
2094
|
167 { |
6096
|
168 #if defined (__CYGWIN__) || defined (__MINGW32__) || defined (_MSC_VER) |
4472
|
169 |
|
170 if (f) |
|
171 { |
6726
|
172 wstatus = octave_pclose (f); |
4472
|
173 f = 0; |
|
174 } |
|
175 |
|
176 open_p = false; |
|
177 |
|
178 return this; |
|
179 |
|
180 #elif defined (HAVE_SYS_WAIT_H) |
2094
|
181 |
3644
|
182 if (f) |
|
183 { |
|
184 pid_t wait_pid; |
2094
|
185 |
3644
|
186 int status = -1; |
|
187 |
|
188 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
189 *ptr != 0; |
|
190 ptr = &(*ptr)->next) |
2094
|
191 { |
3644
|
192 if (*ptr == this) |
|
193 { |
|
194 *ptr = (*ptr)->next; |
|
195 status = 0; |
|
196 break; |
|
197 } |
2094
|
198 } |
|
199 |
3644
|
200 if (status == 0 && ::fclose (f) == 0) |
|
201 { |
|
202 using namespace std; |
3531
|
203 |
3644
|
204 do |
|
205 { |
5453
|
206 wait_pid = octave_syscalls::waitpid (proc_pid, &wstatus, 0); |
3644
|
207 } |
|
208 while (wait_pid == -1 && errno == EINTR); |
3631
|
209 } |
3644
|
210 |
|
211 f = 0; |
3631
|
212 } |
2094
|
213 |
3631
|
214 open_p = false; |
2094
|
215 |
3631
|
216 return this; |
2094
|
217 |
|
218 #else |
|
219 |
3631
|
220 return 0; |
2094
|
221 |
|
222 #endif |
|
223 } |
|
224 |
|
225 /* |
|
226 ;;; Local Variables: *** |
|
227 ;;; mode: C++ *** |
|
228 ;;; End: *** |
|
229 */ |