1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
1
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
269
|
13 |
NonPositionableExternalStream subclass:#PipeStream
|
49
|
14 |
instanceVariableNames:'commandString'
|
57
|
15 |
classVariableNames:'BrokenPipeSignal'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams-External'
|
|
18 |
!
|
|
19 |
|
|
20 |
PipeStream comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
22 |
All Rights Reserved
|
93
|
23 |
|
362
|
24 |
$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.26 1995-07-22 19:23:52 claus Exp $
|
1
|
25 |
'!
|
|
26 |
|
88
|
27 |
!PipeStream class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
159
|
32 |
All Rights Reserved
|
1
|
33 |
|
88
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
362
|
45 |
$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.26 1995-07-22 19:23:52 claus Exp $
|
88
|
46 |
"
|
|
47 |
!
|
2
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
255
|
51 |
Pipestreams allow reading or writing from/to a unix command.
|
|
52 |
For example, to get a stream reading the output of an 'ls -l'
|
|
53 |
command, a PipeStream can be created with:
|
2
|
54 |
|
159
|
55 |
PipeStream readingFrom:'ls -l'
|
2
|
56 |
|
255
|
57 |
the characters of the commands output can be read using the
|
|
58 |
standard stream messages as next, nextLine etc.
|
2
|
59 |
|
255
|
60 |
If a writing pipeStream is written to, after the command has finished,
|
|
61 |
UNIX will generate an error-signal (SIGPIPE), which will raise the
|
|
62 |
BrokenPipeSignal.
|
|
63 |
Thus, to handle this condition correctly, the following code is suggested:
|
2
|
64 |
|
159
|
65 |
|p|
|
|
66 |
p := PipeStream writingTo:'echo hello'.
|
|
67 |
PipeStream brokenPipeSignal handle:[:ex |
|
|
68 |
'broken pipe' printNewline.
|
|
69 |
p shutDown.
|
|
70 |
ex return
|
|
71 |
] do:[
|
|
72 |
p nextPutLine:'oops'.
|
|
73 |
'after write' printNewline.
|
|
74 |
p close.
|
|
75 |
'after close' printNewline
|
|
76 |
]
|
57
|
77 |
|
255
|
78 |
Notice, that if the Stream is buffered, the Signal may occur some time after
|
|
79 |
the write - or even at close time; to avoid a recursive signal in the exception
|
|
80 |
handler, a shutDown is useful there.
|
2
|
81 |
"
|
|
82 |
! !
|
|
83 |
|
217
|
84 |
!PipeStream primitiveDefinitions!
|
180
|
85 |
|
88
|
86 |
%{
|
|
87 |
#include <stdio.h>
|
|
88 |
#include <errno.h>
|
|
89 |
#ifndef transputer
|
|
90 |
# include <sys/types.h>
|
|
91 |
# include <sys/stat.h>
|
|
92 |
#endif
|
230
|
93 |
|
|
94 |
/*
|
|
95 |
* on some systems errno is a macro ... check for it here
|
|
96 |
*/
|
|
97 |
#ifndef errno
|
|
98 |
extern errno;
|
|
99 |
#endif
|
|
100 |
|
88
|
101 |
%}
|
180
|
102 |
! !
|
88
|
103 |
|
325
|
104 |
!PipeStream primitiveFunctions!
|
|
105 |
|
|
106 |
%{
|
|
107 |
|
|
108 |
/*
|
|
109 |
* some systems (i.e. ultrix) use fork;
|
|
110 |
* were better off with a popen based on vfork ...
|
|
111 |
*/
|
|
112 |
#ifdef NEED_POPEN_WITH_VFORK
|
|
113 |
|
|
114 |
static int popen_pid = 0;
|
|
115 |
|
|
116 |
FILE *
|
|
117 |
popen(command, type)
|
|
118 |
/* const */ char *command;
|
|
119 |
/* const */ char *type;
|
|
120 |
{
|
|
121 |
int pipes[2];
|
|
122 |
int itype = (strcmp(type, "w") == 0 ? 1 : 0);
|
|
123 |
|
|
124 |
if (pipe(pipes) == -1)
|
326
|
125 |
return NULL;
|
325
|
126 |
|
|
127 |
switch (popen_pid = vfork()) {
|
|
128 |
case -1:
|
326
|
129 |
(void)close(pipes[0]);
|
|
130 |
(void)close(pipes[1]);
|
|
131 |
return NULL;
|
325
|
132 |
|
|
133 |
case 0:
|
326
|
134 |
if (itype) {
|
|
135 |
dup2(pipes[0], fileno(stdin));
|
|
136 |
close(pipes[1]);
|
|
137 |
} else {
|
|
138 |
dup2(pipes[1], fileno(stdout));
|
|
139 |
close(pipes[0]);
|
|
140 |
}
|
|
141 |
execl("/bin/sh", "/bin/sh", "-c", command, 0);
|
|
142 |
fprintf(stderr, "XRN Error: failed the execlp\n");
|
|
143 |
_exit(-1);
|
|
144 |
/* NOTREACHED */
|
325
|
145 |
|
|
146 |
default:
|
326
|
147 |
if (itype) {
|
|
148 |
close(pipes[0]);
|
|
149 |
return fdopen(pipes[1], "w");
|
|
150 |
} else {
|
|
151 |
close(pipes[1]);
|
|
152 |
return fdopen(pipes[0], "r");
|
|
153 |
}
|
325
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
int
|
|
158 |
pclose(str)
|
|
159 |
FILE *str;
|
|
160 |
{
|
|
161 |
int pd = 0;
|
|
162 |
int status;
|
|
163 |
int err;
|
|
164 |
|
|
165 |
err = fclose(str);
|
|
166 |
|
|
167 |
do {
|
326
|
168 |
if ((pd = wait(&status)) == -1)
|
|
169 |
{
|
|
170 |
err = EOF;
|
|
171 |
break;
|
|
172 |
}
|
325
|
173 |
} while (pd != popen_pid);
|
|
174 |
|
|
175 |
if (err == EOF)
|
326
|
176 |
return -1;
|
325
|
177 |
|
|
178 |
if (status)
|
326
|
179 |
status >>= 8; /* exit status in high byte */
|
325
|
180 |
|
|
181 |
return status;
|
|
182 |
}
|
|
183 |
|
|
184 |
#endif
|
|
185 |
|
|
186 |
%}
|
|
187 |
! !
|
|
188 |
|
2
|
189 |
!PipeStream class methodsFor:'initialization'!
|
|
190 |
|
|
191 |
initialize
|
|
192 |
"setup the signal"
|
|
193 |
|
57
|
194 |
BrokenPipeSignal isNil ifTrue:[
|
302
|
195 |
BrokenPipeSignal := ErrorSignal newSignalMayProceed:true.
|
159
|
196 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal.
|
|
197 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
|
57
|
198 |
]
|
2
|
199 |
! !
|
|
200 |
|
345
|
201 |
!PipeStream class methodsFor:'Signal constants'!
|
2
|
202 |
|
57
|
203 |
brokenPipeSignal
|
2
|
204 |
"return the signal used to handle SIGPIPE unix-signals"
|
|
205 |
|
57
|
206 |
^ BrokenPipeSignal
|
2
|
207 |
! !
|
|
208 |
|
1
|
209 |
!PipeStream class methodsFor:'instance creation'!
|
|
210 |
|
2
|
211 |
writingTo:commandString
|
1
|
212 |
"create and return a new pipeStream which can write to the unix command
|
|
213 |
given by command."
|
|
214 |
|
2
|
215 |
^ (self basicNew) writingTo:commandString
|
1
|
216 |
|
|
217 |
"PipeStream writingTo:'sort'"
|
|
218 |
!
|
|
219 |
|
2
|
220 |
readingFrom:commandString
|
1
|
221 |
"create and return a new pipeStream which can read from the unix command
|
|
222 |
given by command."
|
|
223 |
|
2
|
224 |
^ (self basicNew) readingFrom:commandString
|
1
|
225 |
|
2
|
226 |
"PipeStream readingFrom:'ls -l'"
|
1
|
227 |
! !
|
|
228 |
|
99
|
229 |
!PipeStream methodsFor:'accessing'!
|
|
230 |
|
|
231 |
commandString
|
|
232 |
"return the command string"
|
|
233 |
|
|
234 |
^ commandString
|
|
235 |
! !
|
|
236 |
|
1
|
237 |
!PipeStream methodsFor:'instance release'!
|
|
238 |
|
230
|
239 |
disposed
|
|
240 |
"redefined to avoid blocking in close."
|
|
241 |
|
|
242 |
self shutDown
|
|
243 |
!
|
|
244 |
|
57
|
245 |
shutDown
|
|
246 |
"close the Stream, ignoring any broken-pipe errors"
|
|
247 |
|
230
|
248 |
BrokenPipeSignal catch:[self closeFileDescriptor]
|
57
|
249 |
!
|
|
250 |
|
1
|
251 |
closeFile
|
230
|
252 |
"low level close - redefined since we close a pipe here.
|
|
253 |
This waits for the command to finish.
|
|
254 |
Use shutDown for a fast (nonBlocking) close."
|
1
|
255 |
|
13
|
256 |
%{ /* UNLIMITEDSTACK */
|
1
|
257 |
#ifndef transputer
|
360
|
258 |
OBJ fp;
|
13
|
259 |
|
360
|
260 |
if ((fp = _INST(filePointer)) != nil) {
|
|
261 |
_INST(filePointer) = nil;
|
159
|
262 |
/*
|
|
263 |
* allow interrupt even when blocking here ...
|
|
264 |
*/
|
362
|
265 |
__BEGIN_INTERRUPTABLE__
|
360
|
266 |
pclose(MKFD(fp));
|
362
|
267 |
__END_INTERRUPTABLE__
|
13
|
268 |
}
|
1
|
269 |
#endif
|
|
270 |
%}
|
|
271 |
! !
|
|
272 |
|
|
273 |
!PipeStream methodsFor:'private'!
|
|
274 |
|
159
|
275 |
XXatEnd
|
93
|
276 |
"return true, if position is at end"
|
|
277 |
|
|
278 |
%{ /* NOCONTEXT */
|
|
279 |
#ifdef IRIX5
|
|
280 |
FILE *f;
|
|
281 |
OBJ t;
|
|
282 |
OBJ _true = true;
|
|
283 |
int c;
|
|
284 |
|
|
285 |
if (_INST(hitEOF) == _true) {
|
159
|
286 |
RETURN (_true);
|
93
|
287 |
}
|
|
288 |
if ((t = _INST(filePointer)) != nil) {
|
159
|
289 |
f = MKFD(t);
|
|
290 |
if (feof(f)) {
|
|
291 |
_INST(hitEOF) = true;
|
|
292 |
RETURN (true);
|
|
293 |
}
|
|
294 |
RETURN ( false );
|
93
|
295 |
}
|
|
296 |
#endif
|
|
297 |
%}
|
|
298 |
.
|
|
299 |
^ super atEnd
|
|
300 |
! !
|
|
301 |
|
|
302 |
!PipeStream methodsFor:'private'!
|
|
303 |
|
49
|
304 |
openPipeFor:aCommandString withMode:mode
|
255
|
305 |
"open a pipe to the unix command in aCcommandString;
|
|
306 |
mode may be 'r' or 'w'"
|
1
|
307 |
|
|
308 |
|retVal|
|
|
309 |
|
326
|
310 |
filePointer notNil ifTrue:[
|
|
311 |
"the pipe was already open ...
|
|
312 |
this should (can) not happen."
|
|
313 |
^ self errorOpen
|
|
314 |
].
|
255
|
315 |
|
13
|
316 |
%{ /* STACK: 32000 */
|
1
|
317 |
#ifndef transputer
|
255
|
318 |
FILE *f;
|
|
319 |
|
|
320 |
_INST(lastErrorNumber) = nil;
|
1
|
321 |
|
255
|
322 |
if (__isString(aCommandString) && __isString(mode)) {
|
362
|
323 |
__BEGIN_INTERRUPTABLE__
|
255
|
324 |
do {
|
13
|
325 |
#ifdef LINUX
|
255
|
326 |
/* LINUX returns a non-NULL f even when interrupted */
|
|
327 |
errno = 0;
|
|
328 |
f = (FILE *)popen((char *) _stringVal(aCommandString),
|
|
329 |
(char *) _stringVal(mode));
|
|
330 |
if (errno == EINTR)
|
|
331 |
f = NULL;
|
13
|
332 |
#else
|
255
|
333 |
f = (FILE *)popen((char *) _stringVal(aCommandString),
|
|
334 |
(char *) _stringVal(mode));
|
13
|
335 |
#endif
|
255
|
336 |
} while ((f == NULL) && (errno == EINTR));
|
362
|
337 |
__END_INTERRUPTABLE__
|
|
338 |
|
255
|
339 |
if (f == NULL) {
|
|
340 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
|
341 |
} else {
|
|
342 |
_INST(filePointer) = MKOBJ(f);
|
|
343 |
retVal = self;
|
159
|
344 |
}
|
1
|
345 |
}
|
|
346 |
#endif
|
255
|
347 |
%}.
|
1
|
348 |
retVal notNil ifTrue:[
|
159
|
349 |
commandString := aCommandString.
|
|
350 |
buffered := true.
|
|
351 |
hitEOF := false.
|
|
352 |
Lobby register:self
|
1
|
353 |
].
|
255
|
354 |
lastErrorNumber notNil ifTrue:[
|
|
355 |
"
|
308
|
356 |
the pipe open failed for some reason ...
|
|
357 |
... this may be either due to an invalid command string,
|
|
358 |
or due to the system running out of memory (when forking
|
|
359 |
the unix process)
|
255
|
360 |
"
|
|
361 |
^ self openError
|
|
362 |
].
|
1
|
363 |
^ retVal
|
|
364 |
!
|
|
365 |
|
|
366 |
readingFrom:command
|
|
367 |
"setup the receiver to read from command"
|
|
368 |
|
326
|
369 |
mode := #readonly. didWrite := false.
|
1
|
370 |
^ self openPipeFor:command withMode:'r'
|
|
371 |
!
|
|
372 |
|
|
373 |
writingTo:command
|
|
374 |
"setup the receiver to write to command"
|
|
375 |
|
326
|
376 |
mode := #writeonly. didWrite := true.
|
1
|
377 |
^ self openPipeFor:command withMode:'w'
|
|
378 |
! !
|