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 |
|
29 #include <iostream.h> |
|
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 |
|
38 #include "oct-procbuf.h" |
|
39 #include "syswait.h" |
|
40 |
|
41 // This class is based on the procbuf class from libg++, written by |
|
42 // Per Bothner, Copyright (C) 1993 Free Software Foundation. |
|
43 // |
|
44 // It should work with the filebuf class from libg++, but it might not |
|
45 // work with others since it depends on being able to get at the |
|
46 // underlying file descriptor with filebuf::fd(), which is not |
|
47 // standard. |
|
48 |
|
49 static octave_procbuf *octave_procbuf_list = 0; |
|
50 |
|
51 octave_procbuf * |
|
52 octave_procbuf::open (const char *command, int mode) |
|
53 { |
|
54 #if defined (HAVE_SYS_WAIT_H) |
|
55 |
|
56 int pipe_fds[2]; |
|
57 |
3156
|
58 volatile int child_std_end = (mode & ios::in) ? 1 : 0; |
|
59 |
3147
|
60 volatile int parent_end, child_end; |
2094
|
61 |
|
62 if (is_open ()) |
|
63 return 0; |
|
64 |
|
65 if (pipe (pipe_fds) < 0) |
|
66 return 0; |
|
67 |
|
68 if (mode & ios::in) |
|
69 { |
|
70 parent_end = pipe_fds[0]; |
|
71 child_end = pipe_fds[1]; |
|
72 } |
|
73 else |
|
74 { |
|
75 parent_end = pipe_fds[1]; |
|
76 child_end = pipe_fds[0]; |
|
77 } |
|
78 |
3156
|
79 proc_pid = ::fork (); |
2094
|
80 |
|
81 if (proc_pid == 0) |
|
82 { |
|
83 ::close (parent_end); |
|
84 |
|
85 if (child_end != child_std_end) |
|
86 { |
3156
|
87 ::dup2 (child_end, child_std_end); |
2094
|
88 ::close (child_end); |
|
89 } |
|
90 |
|
91 while (octave_procbuf_list) |
|
92 { |
|
93 ::close (octave_procbuf_list->fd ()); |
|
94 octave_procbuf_list = octave_procbuf_list->next; |
|
95 } |
|
96 |
|
97 execl ("/bin/sh", "sh", "-c", command, NULL); |
|
98 |
|
99 exit (127); |
|
100 } |
|
101 |
|
102 ::close (child_end); |
|
103 |
|
104 if (proc_pid < 0) |
|
105 { |
|
106 ::close (parent_end); |
|
107 return 0; |
|
108 } |
|
109 |
|
110 attach (parent_end); |
|
111 |
|
112 next = octave_procbuf_list; |
|
113 octave_procbuf_list = this; |
|
114 |
|
115 return this; |
|
116 |
|
117 #else |
|
118 |
|
119 return 0; |
|
120 |
|
121 #endif |
|
122 } |
|
123 |
|
124 int |
|
125 octave_procbuf::sys_close (void) |
|
126 { |
|
127 #if defined (HAVE_SYS_WAIT_H) |
|
128 |
|
129 int wstatus; |
|
130 |
|
131 pid_t wait_pid; |
|
132 |
|
133 int status = -1; |
|
134 |
|
135 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
136 *ptr != 0; |
|
137 ptr = &(*ptr)->next) |
|
138 { |
|
139 if (*ptr == this) |
|
140 { |
|
141 *ptr = (*ptr)->next; |
|
142 status = 0; |
|
143 break; |
|
144 } |
|
145 } |
|
146 |
|
147 if (status < 0 || ::close (fd ()) < 0) |
|
148 return -1; |
|
149 |
|
150 do |
|
151 { |
3156
|
152 wait_pid = ::waitpid (proc_pid, &wstatus, 0); |
2094
|
153 } |
|
154 while (wait_pid == -1 && errno == EINTR); |
|
155 |
|
156 if (wait_pid == -1) |
|
157 return -1; |
|
158 |
|
159 return wstatus; |
|
160 |
|
161 #else |
|
162 |
|
163 return -1; |
|
164 |
|
165 #endif |
|
166 } |
|
167 |
|
168 /* |
|
169 ;;; Local Variables: *** |
|
170 ;;; mode: C++ *** |
|
171 ;;; End: *** |
|
172 */ |