author | Stefan Vogel <sv@exept.de> |
Mon, 09 Sep 1996 12:30:27 +0200 | |
changeset 1648 | faa891d2c1b0 |
parent 1295 | 83f594f05c52 |
child 1662 | ce26ca3d837c |
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 |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
14 |
instanceVariableNames:'commandString pid exitStatus exitSema' |
1295 | 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 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
133 |
!PipeStream class methodsFor:'documentation'! |
613 | 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 |
||
1295 | 155 |
PipeStream readingFrom:'ls -l' |
613 | 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 |
||
1295 | 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 |
] |
|
613 | 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. |
|
1295 | 181 |
|
182 |
[author:] |
|
183 |
Claus Gittinger |
|
613 | 184 |
" |
185 |
! ! |
|
186 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
187 |
!PipeStream class methodsFor:'initialization'! |
2 | 188 |
|
189 |
initialize |
|
190 |
"setup the signal" |
|
191 |
||
57 | 192 |
BrokenPipeSignal isNil ifTrue:[ |
582
21f08116b28d
BrokenPipeSignal now a child of WriteErrorSignal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
193 |
BrokenPipeSignal := WriteErrorSignal newSignalMayProceed:true. |
159 | 194 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal. |
195 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'. |
|
57 | 196 |
] |
2 | 197 |
! ! |
198 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
199 |
!PipeStream class methodsFor:'instance creation'! |
2 | 200 |
|
613 | 201 |
readingFrom:commandString |
202 |
"create and return a new pipeStream which can read from the unix command |
|
203 |
given by command." |
|
2 | 204 |
|
613 | 205 |
^ (self basicNew) readingFrom:commandString |
206 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
207 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
208 |
PipeStream readingFrom:'ls -l' |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
209 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
210 |
|s| |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
211 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
212 |
(Delay forSeconds:2) wait. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
213 |
s shutDown |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
214 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
215 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
216 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 217 |
! |
1 | 218 |
|
2 | 219 |
writingTo:commandString |
1 | 220 |
"create and return a new pipeStream which can write to the unix command |
221 |
given by command." |
|
222 |
||
2 | 223 |
^ (self basicNew) writingTo:commandString |
1 | 224 |
|
225 |
"PipeStream writingTo:'sort'" |
|
613 | 226 |
! ! |
227 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
228 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 229 |
|
613 | 230 |
brokenPipeSignal |
231 |
"return the signal used to handle SIGPIPE unix-signals" |
|
1 | 232 |
|
613 | 233 |
^ BrokenPipeSignal |
1 | 234 |
! ! |
235 |
||
99 | 236 |
!PipeStream methodsFor:'accessing'! |
237 |
||
238 |
commandString |
|
239 |
"return the command string" |
|
240 |
||
241 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
242 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
243 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
244 |
exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
245 |
"return exitStatus" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
246 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
247 |
^ exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
248 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
249 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
250 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
251 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
252 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
253 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
254 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
255 |
^ pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
256 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
257 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
99 | 258 |
! ! |
259 |
||
1 | 260 |
!PipeStream methodsFor:'instance release'! |
261 |
||
262 |
closeFile |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
263 |
"low level close |
230 | 264 |
This waits for the command to finish. |
265 |
Use shutDown for a fast (nonBlocking) close." |
|
1 | 266 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
267 |
filePointer notNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
268 |
super closeFile. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
269 |
filePointer := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
270 |
pid notNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
271 |
exitSema wait. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
272 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
273 |
]. |
369 | 274 |
! |
275 |
||
276 |
closeFileDescriptor |
|
277 |
"alternative very low level close |
|
278 |
This closes the underlying OS-fileDescriptor |
|
279 |
- and will NOT write any buffered data to the stream. |
|
280 |
You have been warned." |
|
281 |
||
282 |
%{ /* NOCONTEXT */ |
|
793 | 283 |
#if !defined(transputer) && !defined(MSDOS_LIKE) |
369 | 284 |
|
285 |
OBJ fp; |
|
286 |
FILE *f; |
|
287 |
||
1133 | 288 |
if ((fp = __INST(filePointer)) != nil) { |
289 |
__INST(filePointer) = nil; |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
290 |
f = __FILEVal(fp); |
369 | 291 |
__BEGIN_INTERRUPTABLE__ |
292 |
close(fileno(f)); |
|
293 |
__END_INTERRUPTABLE__ |
|
294 |
} |
|
793 | 295 |
#endif /* not transputer && not MSDOS_LIKE */ |
369 | 296 |
%} |
613 | 297 |
! |
298 |
||
299 |
disposed |
|
300 |
"redefined to avoid blocking in close." |
|
301 |
||
302 |
self shutDown |
|
303 |
! |
|
304 |
||
305 |
shutDown |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
306 |
"close the Stream, ignoring any broken-pipe errors. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
307 |
Terminate the command" |
613 | 308 |
|
309 |
BrokenPipeSignal catch:[ |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
310 |
|tpid| |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
311 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
312 |
Lobby unregister:self. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
313 |
self closeFileDescriptor. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
314 |
tpid := pid. "copy pid to avoid race" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
315 |
tpid notNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
316 |
"/ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
317 |
"/ Terminate both the process and group, just in case the |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
318 |
"/ operating system does not support process groups. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
319 |
"/ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
320 |
OperatingSystem terminateProcess:tpid. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
321 |
OperatingSystem terminateProcessGroup:tpid. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
322 |
pid := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
323 |
]. |
613 | 324 |
] |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
325 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
326 |
"Modified: 23.5.1996 / 09:15:41 / stefan" |
1 | 327 |
! ! |
328 |
||
329 |
!PipeStream methodsFor:'private'! |
|
330 |
||
379 | 331 |
atEnd |
93 | 332 |
"return true, if position is at end" |
333 |
||
334 |
%{ /* NOCONTEXT */ |
|
335 |
FILE *f; |
|
336 |
OBJ t; |
|
337 |
OBJ _true = true; |
|
338 |
int c; |
|
339 |
||
1133 | 340 |
if (__INST(hitEOF) == _true) { |
159 | 341 |
RETURN (_true); |
93 | 342 |
} |
1133 | 343 |
if ((t = __INST(filePointer)) != nil) { |
475
b57530aa1b0a
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
437
diff
changeset
|
344 |
f = __FILEVal(t); |
159 | 345 |
if (feof(f)) { |
1133 | 346 |
__INST(hitEOF) = true; |
159 | 347 |
RETURN (true); |
348 |
} |
|
401 | 349 |
clearerr(f); |
159 | 350 |
RETURN ( false ); |
93 | 351 |
} |
352 |
%} |
|
353 |
. |
|
354 |
^ super atEnd |
|
613 | 355 |
! |
93 | 356 |
|
49 | 357 |
openPipeFor:aCommandString withMode:mode |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
358 |
"open a pipe to the unix command in commandString; |
255 | 359 |
mode may be 'r' or 'w'" |
1 | 360 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
361 |
|blocked pipeFdArray execFdArray execFd myFd| |
1 | 362 |
|
326 | 363 |
filePointer notNil ifTrue:[ |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
364 |
"the pipe was already open ... |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
365 |
this should (can) not happen." |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
366 |
^ self errorOpen |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
367 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
368 |
lastErrorNumber := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
369 |
exitStatus := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
370 |
exitSema := Semaphore new. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
371 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
372 |
pipeFdArray := OperatingSystem makePipe. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
373 |
pipeFdArray isNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
374 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
375 |
^ self openError |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
376 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
377 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
378 |
mode = 'r' ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
379 |
execFd := pipeFdArray at:2. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
380 |
execFdArray := Array with:0 with:execFd with:2. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
381 |
myFd := pipeFdArray at:1. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
382 |
] ifFalse:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
383 |
execFd := pipeFdArray at:1. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
384 |
execFdArray := Array with:execFd with:1 with:2. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
385 |
myFd := pipeFdArray at:2. |
326 | 386 |
]. |
255 | 387 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
388 |
blocked := OperatingSystem blockInterrupts. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
389 |
pid := OperatingSystem exec:'/bin/sh' |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
390 |
withArguments:(Array with:'sh' with:'-c' with:aCommandString) |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
391 |
fileDescriptors:execFdArray |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
392 |
closeDescriptors:(Array with:myFd) |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
393 |
fork:true |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
394 |
newPgrp:true. |
362 | 395 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
396 |
OperatingSystem closeFd:execFd. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
397 |
pid > 0 ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
398 |
self fileDescriptor:myFd withMode:mode. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
399 |
Processor monitorPid:pid action:[ :stat | |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
400 |
exitStatus := stat. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
401 |
pid := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
402 |
exitSema signal. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
403 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
404 |
] ifFalse:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
405 |
pid := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
406 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
407 |
OperatingSystem closeFd:myFd. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
408 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
409 |
blocked ifFalse:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
410 |
OperatingSystem unblockInterrupts |
1 | 411 |
]. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
412 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
413 |
lastErrorNumber isNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
414 |
commandString := aCommandString. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
415 |
buffered := true. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
416 |
hitEOF := false. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
417 |
binary := false. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
418 |
Lobby register:self |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
419 |
] ifFalse:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
420 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
421 |
the pipe open failed for some reason ... |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
422 |
... this may be either due to an invalid command string, |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
423 |
or due to the system running out of memory (when forking |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
424 |
the unix process) |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
425 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
426 |
^ self openError |
255 | 427 |
]. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
428 |
^ self |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
429 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
430 |
"Modified: 23.4.1996 / 17:05:59 / stefan" |
1 | 431 |
! |
432 |
||
433 |
readingFrom:command |
|
434 |
"setup the receiver to read from command" |
|
435 |
||
326 | 436 |
mode := #readonly. didWrite := false. |
1 | 437 |
^ self openPipeFor:command withMode:'r' |
438 |
! |
|
439 |
||
440 |
writingTo:command |
|
441 |
"setup the receiver to write to command" |
|
442 |
||
326 | 443 |
mode := #writeonly. didWrite := true. |
1 | 444 |
^ self openPipeFor:command withMode:'w' |
445 |
! ! |
|
613 | 446 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
447 |
!PipeStream class methodsFor:'documentation'! |
1295 | 448 |
|
449 |
version |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
450 |
^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.44 1996-09-09 10:30:27 stefan Exp $' |
1295 | 451 |
! ! |
613 | 452 |
PipeStream initialize! |