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 |
3147
|
58 volatile int parent_end, child_end; |
2094
|
59 |
|
60 if (is_open ()) |
|
61 return 0; |
|
62 |
|
63 if (pipe (pipe_fds) < 0) |
|
64 return 0; |
|
65 |
|
66 if (mode & ios::in) |
|
67 { |
|
68 parent_end = pipe_fds[0]; |
|
69 child_end = pipe_fds[1]; |
|
70 } |
|
71 else |
|
72 { |
|
73 parent_end = pipe_fds[1]; |
|
74 child_end = pipe_fds[0]; |
|
75 } |
|
76 |
3147
|
77 #if defined HAVE_VFORK |
|
78 proc_pid = vfork (); |
|
79 #else |
2094
|
80 proc_pid = fork (); |
3147
|
81 #endif |
2094
|
82 |
|
83 if (proc_pid == 0) |
|
84 { |
|
85 int child_std_end = (mode & ios::in) ? 1 : 0; |
|
86 |
|
87 ::close (parent_end); |
|
88 |
|
89 if (child_end != child_std_end) |
|
90 { |
|
91 dup2 (child_end, child_std_end); |
|
92 ::close (child_end); |
|
93 } |
|
94 |
|
95 while (octave_procbuf_list) |
|
96 { |
|
97 ::close (octave_procbuf_list->fd ()); |
|
98 octave_procbuf_list = octave_procbuf_list->next; |
|
99 } |
|
100 |
|
101 execl ("/bin/sh", "sh", "-c", command, NULL); |
|
102 |
|
103 exit (127); |
|
104 } |
|
105 |
|
106 ::close (child_end); |
|
107 |
|
108 if (proc_pid < 0) |
|
109 { |
|
110 ::close (parent_end); |
|
111 return 0; |
|
112 } |
|
113 |
|
114 attach (parent_end); |
|
115 |
|
116 next = octave_procbuf_list; |
|
117 octave_procbuf_list = this; |
|
118 |
|
119 return this; |
|
120 |
|
121 #else |
|
122 |
|
123 return 0; |
|
124 |
|
125 #endif |
|
126 } |
|
127 |
|
128 int |
|
129 octave_procbuf::sys_close (void) |
|
130 { |
|
131 #if defined (HAVE_SYS_WAIT_H) |
|
132 |
|
133 int wstatus; |
|
134 |
|
135 pid_t wait_pid; |
|
136 |
|
137 int status = -1; |
|
138 |
|
139 for (octave_procbuf **ptr = &octave_procbuf_list; |
|
140 *ptr != 0; |
|
141 ptr = &(*ptr)->next) |
|
142 { |
|
143 if (*ptr == this) |
|
144 { |
|
145 *ptr = (*ptr)->next; |
|
146 status = 0; |
|
147 break; |
|
148 } |
|
149 } |
|
150 |
|
151 if (status < 0 || ::close (fd ()) < 0) |
|
152 return -1; |
|
153 |
|
154 do |
|
155 { |
|
156 wait_pid = waitpid (proc_pid, &wstatus, 0); |
|
157 } |
|
158 while (wait_pid == -1 && errno == EINTR); |
|
159 |
|
160 if (wait_pid == -1) |
|
161 return -1; |
|
162 |
|
163 return wstatus; |
|
164 |
|
165 #else |
|
166 |
|
167 return -1; |
|
168 |
|
169 #endif |
|
170 } |
|
171 |
|
172 /* |
|
173 ;;; Local Variables: *** |
|
174 ;;; mode: C++ *** |
|
175 ;;; End: *** |
|
176 */ |