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