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