Mercurial > hg > octave-nkf
annotate src/oct-procbuf.cc @ 10884:cc2bc3f46cd4
fix assignment bug with lazy indices
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 11 Aug 2010 10:55:31 +0200 |
parents | 479cc8a0a846 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
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 #include <sys/types.h> | |
33 #include <unistd.h> | |
34 | |
3174 | 35 #include "lo-mappers.h" |
36 #include "lo-utils.h" | |
2094 | 37 #include "oct-procbuf.h" |
5453 | 38 #include "oct-syscalls.h" |
6726 | 39 #include "sysdep.h" |
3176 | 40 #include "variables.h" |
2094 | 41 |
3174 | 42 #include "defun.h" |
43 #include "gripes.h" | |
3308 | 44 #include "utils.h" |
3174 | 45 |
2094 | 46 // This class is based on the procbuf class from libg++, written by |
47 // Per Bothner, Copyright (C) 1993 Free Software Foundation. | |
48 | |
49 static octave_procbuf *octave_procbuf_list = 0; | |
50 | |
6094 | 51 #ifndef BUFSIZ |
52 #define BUFSIZ 1024 | |
53 #endif | |
54 | |
2094 | 55 octave_procbuf * |
56 octave_procbuf::open (const char *command, int mode) | |
57 { | |
6096 | 58 #if defined (__CYGWIN__) || defined (__MINGW32__) || defined (_MSC_VER) |
4472 | 59 |
60 if (is_open ()) | |
61 return 0; | |
62 | |
6726 | 63 f = octave_popen (command, (mode & std::ios::in) ? "r" : "w"); |
4472 | 64 |
65 if (! f) | |
66 return 0; | |
67 | |
68 // Oops... popen doesn't return the associated pid, so fake it for now | |
69 | |
70 proc_pid = 1; | |
71 | |
72 open_p = true; | |
73 | |
74 if (mode & std::ios::out) | |
6094 | 75 ::setvbuf (f, 0, _IOLBF, BUFSIZ); |
4472 | 76 |
77 return this; | |
78 | |
79 #elif defined (HAVE_SYS_WAIT_H) | |
2094 | 80 |
81 int pipe_fds[2]; | |
82 | |
3544 | 83 volatile int child_std_end = (mode & std::ios::in) ? 1 : 0; |
3156 | 84 |
3147 | 85 volatile int parent_end, child_end; |
2094 | 86 |
87 if (is_open ()) | |
88 return 0; | |
89 | |
90 if (pipe (pipe_fds) < 0) | |
91 return 0; | |
92 | |
3544 | 93 if (mode & std::ios::in) |
2094 | 94 { |
95 parent_end = pipe_fds[0]; | |
96 child_end = pipe_fds[1]; | |
97 } | |
98 else | |
99 { | |
100 parent_end = pipe_fds[1]; | |
101 child_end = pipe_fds[0]; | |
102 } | |
103 | |
3156 | 104 proc_pid = ::fork (); |
2094 | 105 |
106 if (proc_pid == 0) | |
107 { | |
10411 | 108 gnulib::close (parent_end); |
2094 | 109 |
110 if (child_end != child_std_end) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
111 { |
10411 | 112 gnulib::dup2 (child_end, child_std_end); |
113 gnulib::close (child_end); | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
114 } |
2094 | 115 |
116 while (octave_procbuf_list) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
117 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
118 FILE *fp = octave_procbuf_list->f; |
3644 | 119 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
120 if (fp) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
121 { |
10411 | 122 gnulib::fclose (fp); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
123 fp = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
124 } |
3644 | 125 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
126 octave_procbuf_list = octave_procbuf_list->next; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
127 } |
2094 | 128 |
5510 | 129 execl ("/bin/sh", "sh", "-c", command, static_cast<void *> (0)); |
2094 | 130 |
131 exit (127); | |
132 } | |
133 | |
10411 | 134 gnulib::close (child_end); |
2094 | 135 |
136 if (proc_pid < 0) | |
137 { | |
10411 | 138 gnulib::close (parent_end); |
2094 | 139 return 0; |
140 } | |
141 | |
3632 | 142 f = ::fdopen (parent_end, (mode & std::ios::in) ? "r" : "w"); |
3631 | 143 |
3649 | 144 if (mode & std::ios::out) |
6094 | 145 ::setvbuf (f, 0, _IOLBF, BUFSIZ); |
3649 | 146 |
3631 | 147 open_p = true; |
2094 | 148 |
149 next = octave_procbuf_list; | |
150 octave_procbuf_list = this; | |
151 | |
152 return this; | |
153 | |
154 #else | |
155 | |
156 return 0; | |
157 | |
158 #endif | |
159 } | |
160 | |
3631 | 161 octave_procbuf * |
162 octave_procbuf::close (void) | |
2094 | 163 { |
6096 | 164 #if defined (__CYGWIN__) || defined (__MINGW32__) || defined (_MSC_VER) |
4472 | 165 |
166 if (f) | |
167 { | |
6726 | 168 wstatus = octave_pclose (f); |
4472 | 169 f = 0; |
170 } | |
171 | |
172 open_p = false; | |
173 | |
174 return this; | |
175 | |
176 #elif defined (HAVE_SYS_WAIT_H) | |
2094 | 177 |
3644 | 178 if (f) |
179 { | |
180 pid_t wait_pid; | |
2094 | 181 |
3644 | 182 int status = -1; |
183 | |
184 for (octave_procbuf **ptr = &octave_procbuf_list; | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
185 *ptr != 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
186 ptr = &(*ptr)->next) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
187 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
188 if (*ptr == this) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
189 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
190 *ptr = (*ptr)->next; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
191 status = 0; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
192 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
193 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
194 } |
2094 | 195 |
10411 | 196 if (status == 0 && gnulib::fclose (f) == 0) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
197 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
198 using namespace std; |
3531 | 199 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
200 do |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
201 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
202 wait_pid = octave_syscalls::waitpid (proc_pid, &wstatus, 0); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
203 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
204 while (wait_pid == -1 && errno == EINTR); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10182
diff
changeset
|
205 } |
3644 | 206 |
207 f = 0; | |
3631 | 208 } |
2094 | 209 |
3631 | 210 open_p = false; |
2094 | 211 |
3631 | 212 return this; |
2094 | 213 |
214 #else | |
215 | |
3631 | 216 return 0; |
2094 | 217 |
218 #endif | |
219 } |