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