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