author | Claus Gittinger <cg@exept.de> |
Sun, 21 Sep 1997 00:10:22 +0200 | |
changeset 2961 | a188727d0619 |
parent 2953 | f0b3a1f4596e |
child 2966 | 856dfc8a294a |
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 |
||
2896 | 28 |
#if defined(__openVMS__) |
29 |
# undef __new |
|
30 |
#endif |
|
31 |
||
88 | 32 |
#include <stdio.h> |
437 | 33 |
#define _STDIO_H_INCLUDED_ |
34 |
||
88 | 35 |
#include <errno.h> |
437 | 36 |
#define _ERRNO_H_INCLUDED_ |
37 |
||
88 | 38 |
#ifndef transputer |
39 |
# include <sys/types.h> |
|
40 |
# include <sys/stat.h> |
|
41 |
#endif |
|
230 | 42 |
|
43 |
/* |
|
44 |
* on some systems errno is a macro ... check for it here |
|
45 |
*/ |
|
46 |
#ifndef errno |
|
47 |
extern errno; |
|
48 |
#endif |
|
49 |
||
1663 | 50 |
#ifdef LINUX |
51 |
# define BUGGY_STDIO_LIB |
|
52 |
#endif |
|
53 |
||
88 | 54 |
%} |
180 | 55 |
! ! |
88 | 56 |
|
325 | 57 |
!PipeStream primitiveFunctions! |
58 |
%{ |
|
59 |
||
60 |
/* |
|
2925 | 61 |
* no longer needed - popen is useless ... |
62 |
*/ |
|
63 |
#undef NEED_POPEN_WITH_VFORK |
|
64 |
||
65 |
||
66 |
/* |
|
325 | 67 |
* some systems (i.e. ultrix) use fork; |
68 |
* were better off with a popen based on vfork ... |
|
69 |
*/ |
|
70 |
#ifdef NEED_POPEN_WITH_VFORK |
|
71 |
||
72 |
static int popen_pid = 0; |
|
73 |
||
74 |
FILE * |
|
75 |
popen(command, type) |
|
76 |
/* const */ char *command; |
|
77 |
/* const */ char *type; |
|
78 |
{ |
|
79 |
int pipes[2]; |
|
80 |
int itype = (strcmp(type, "w") == 0 ? 1 : 0); |
|
81 |
||
82 |
if (pipe(pipes) == -1) |
|
326 | 83 |
return NULL; |
325 | 84 |
|
85 |
switch (popen_pid = vfork()) { |
|
86 |
case -1: |
|
326 | 87 |
(void)close(pipes[0]); |
88 |
(void)close(pipes[1]); |
|
89 |
return NULL; |
|
325 | 90 |
|
91 |
case 0: |
|
326 | 92 |
if (itype) { |
93 |
dup2(pipes[0], fileno(stdin)); |
|
94 |
close(pipes[1]); |
|
95 |
} else { |
|
96 |
dup2(pipes[1], fileno(stdout)); |
|
97 |
close(pipes[0]); |
|
98 |
} |
|
99 |
execl("/bin/sh", "/bin/sh", "-c", command, 0); |
|
100 |
fprintf(stderr, "XRN Error: failed the execlp\n"); |
|
101 |
_exit(-1); |
|
102 |
/* NOTREACHED */ |
|
325 | 103 |
|
104 |
default: |
|
326 | 105 |
if (itype) { |
106 |
close(pipes[0]); |
|
107 |
return fdopen(pipes[1], "w"); |
|
108 |
} else { |
|
109 |
close(pipes[1]); |
|
110 |
return fdopen(pipes[0], "r"); |
|
111 |
} |
|
325 | 112 |
} |
113 |
} |
|
114 |
||
115 |
int |
|
116 |
pclose(str) |
|
117 |
FILE *str; |
|
118 |
{ |
|
119 |
int pd = 0; |
|
120 |
int status; |
|
121 |
int err; |
|
122 |
||
123 |
err = fclose(str); |
|
124 |
||
125 |
do { |
|
326 | 126 |
if ((pd = wait(&status)) == -1) |
127 |
{ |
|
128 |
err = EOF; |
|
129 |
break; |
|
130 |
} |
|
325 | 131 |
} while (pd != popen_pid); |
132 |
||
133 |
if (err == EOF) |
|
326 | 134 |
return -1; |
325 | 135 |
|
136 |
if (status) |
|
326 | 137 |
status >>= 8; /* exit status in high byte */ |
325 | 138 |
|
139 |
return status; |
|
140 |
} |
|
141 |
||
2925 | 142 |
#endif /* NEED_POPEN_WITH_VFORK */ |
325 | 143 |
|
144 |
%} |
|
145 |
! ! |
|
146 |
||
2266 | 147 |
!PipeStream class methodsFor:'documentation'! |
613 | 148 |
|
149 |
copyright |
|
150 |
" |
|
151 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
152 |
All Rights Reserved |
|
153 |
||
154 |
This software is furnished under a license and may be used |
|
155 |
only in accordance with the terms of that license and with the |
|
156 |
inclusion of the above copyright notice. This software may not |
|
157 |
be provided or otherwise made available to, or used by, any |
|
158 |
other person. No title to or ownership of the software is |
|
159 |
hereby transferred. |
|
160 |
" |
|
161 |
! |
|
162 |
||
163 |
documentation |
|
164 |
" |
|
165 |
Pipestreams allow reading or writing from/to a unix command. |
|
166 |
For example, to get a stream reading the output of an 'ls -l' |
|
167 |
command, a PipeStream can be created with: |
|
168 |
||
1295 | 169 |
PipeStream readingFrom:'ls -l' |
613 | 170 |
|
171 |
the characters of the commands output can be read using the |
|
172 |
standard stream messages as next, nextLine etc. |
|
173 |
||
174 |
If a writing pipeStream is written to, after the command has finished, |
|
175 |
UNIX will generate an error-signal (SIGPIPE), which will raise the |
|
176 |
BrokenPipeSignal. |
|
177 |
Thus, to handle this condition correctly, the following code is suggested: |
|
178 |
||
1295 | 179 |
|p| |
180 |
p := PipeStream writingTo:'echo hello'. |
|
181 |
PipeStream brokenPipeSignal handle:[:ex | |
|
182 |
'broken pipe' printNewline. |
|
183 |
p shutDown. |
|
184 |
ex return |
|
185 |
] do:[ |
|
186 |
p nextPutLine:'oops'. |
|
187 |
'after write' printNewline. |
|
188 |
p close. |
|
189 |
'after close' printNewline |
|
190 |
] |
|
613 | 191 |
|
1663 | 192 |
Notice, that iff the Stream is buffered, the Signal may occur some time after |
613 | 193 |
the write - or even at close time; to avoid a recursive signal in the exception |
194 |
handler, a shutDown is useful there. |
|
1295 | 195 |
|
1662
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
196 |
Buffered pipes do not work with Linux - the stdio library seems to be |
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
197 |
buggy (trying to restart the read ...) |
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
198 |
|
1295 | 199 |
[author:] |
200 |
Claus Gittinger |
|
613 | 201 |
" |
202 |
! ! |
|
203 |
||
2266 | 204 |
!PipeStream class methodsFor:'initialization'! |
2 | 205 |
|
206 |
initialize |
|
207 |
"setup the signal" |
|
208 |
||
57 | 209 |
BrokenPipeSignal isNil ifTrue:[ |
582
21f08116b28d
BrokenPipeSignal now a child of WriteErrorSignal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
210 |
BrokenPipeSignal := WriteErrorSignal newSignalMayProceed:true. |
159 | 211 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal. |
212 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'. |
|
57 | 213 |
] |
2 | 214 |
! ! |
215 |
||
2266 | 216 |
!PipeStream class methodsFor:'instance creation'! |
2 | 217 |
|
613 | 218 |
readingFrom:commandString |
219 |
"create and return a new pipeStream which can read from the unix command |
|
220 |
given by command." |
|
2 | 221 |
|
613 | 222 |
^ (self basicNew) readingFrom:commandString |
223 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
224 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
225 |
PipeStream readingFrom:'ls -l' |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
226 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
227 |
|s| |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
228 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
229 |
(Delay forSeconds:2) wait. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
230 |
s shutDown |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
231 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
232 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
233 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 234 |
! |
1 | 235 |
|
2 | 236 |
writingTo:commandString |
1 | 237 |
"create and return a new pipeStream which can write to the unix command |
238 |
given by command." |
|
239 |
||
2 | 240 |
^ (self basicNew) writingTo:commandString |
1 | 241 |
|
242 |
"PipeStream writingTo:'sort'" |
|
613 | 243 |
! ! |
244 |
||
2266 | 245 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 246 |
|
613 | 247 |
brokenPipeSignal |
248 |
"return the signal used to handle SIGPIPE unix-signals" |
|
1 | 249 |
|
613 | 250 |
^ BrokenPipeSignal |
1 | 251 |
! ! |
252 |
||
99 | 253 |
!PipeStream methodsFor:'accessing'! |
254 |
||
255 |
commandString |
|
256 |
"return the command string" |
|
257 |
||
258 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
259 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
260 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
261 |
exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
262 |
"return exitStatus" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
263 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
264 |
^ exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
265 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
266 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
267 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
268 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
269 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
270 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
271 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
272 |
^ pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
273 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
274 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
99 | 275 |
! ! |
276 |
||
1 | 277 |
!PipeStream methodsFor:'instance release'! |
278 |
||
279 |
closeFile |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
280 |
"low level close |
230 | 281 |
This waits for the command to finish. |
282 |
Use shutDown for a fast (nonBlocking) close." |
|
1 | 283 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
284 |
filePointer notNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
285 |
super closeFile. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
286 |
filePointer := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
287 |
pid notNil ifTrue:[ |
2119
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
288 |
[ |
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
289 |
pid notNil ifTrue:[ |
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
290 |
exitSema wait. |
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
291 |
] |
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
292 |
] valueUninterruptably |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
293 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
294 |
]. |
369 | 295 |
! |
296 |
||
297 |
closeFileDescriptor |
|
298 |
"alternative very low level close |
|
299 |
This closes the underlying OS-fileDescriptor |
|
300 |
- and will NOT write any buffered data to the stream. |
|
301 |
You have been warned." |
|
302 |
||
303 |
%{ /* NOCONTEXT */ |
|
793 | 304 |
#if !defined(transputer) && !defined(MSDOS_LIKE) |
369 | 305 |
|
306 |
OBJ fp; |
|
307 |
FILE *f; |
|
308 |
||
1133 | 309 |
if ((fp = __INST(filePointer)) != nil) { |
310 |
__INST(filePointer) = nil; |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
311 |
f = __FILEVal(fp); |
369 | 312 |
__BEGIN_INTERRUPTABLE__ |
313 |
close(fileno(f)); |
|
314 |
__END_INTERRUPTABLE__ |
|
315 |
} |
|
793 | 316 |
#endif /* not transputer && not MSDOS_LIKE */ |
369 | 317 |
%} |
613 | 318 |
! |
319 |
||
320 |
disposed |
|
321 |
"redefined to avoid blocking in close." |
|
322 |
||
323 |
self shutDown |
|
324 |
! |
|
325 |
||
326 |
shutDown |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
327 |
"close the Stream, ignoring any broken-pipe errors. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
328 |
Terminate the command" |
613 | 329 |
|
330 |
BrokenPipeSignal catch:[ |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
331 |
|tpid| |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
332 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
333 |
Lobby unregister:self. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
334 |
self closeFileDescriptor. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
335 |
tpid := pid. "copy pid to avoid race" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
336 |
tpid notNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
337 |
"/ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
338 |
"/ 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
|
339 |
"/ operating system does not support process groups. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
340 |
"/ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
341 |
OperatingSystem terminateProcess:tpid. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
342 |
OperatingSystem terminateProcessGroup:tpid. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
343 |
pid := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
344 |
]. |
613 | 345 |
] |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
346 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
347 |
"Modified: 23.5.1996 / 09:15:41 / stefan" |
1 | 348 |
! ! |
349 |
||
350 |
!PipeStream methodsFor:'private'! |
|
351 |
||
49 | 352 |
openPipeFor:aCommandString withMode:mode |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
353 |
"open a pipe to the unix command in commandString; |
255 | 354 |
mode may be 'r' or 'w'" |
1 | 355 |
|
2598 | 356 |
|blocked pipeFdArray execFdArray execFd myFd osType |
357 |
shellPath shellArgs| |
|
1 | 358 |
|
326 | 359 |
filePointer notNil ifTrue:[ |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
360 |
"the pipe was already open ... |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
361 |
this should (can) not happen." |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
362 |
^ self errorOpen |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
363 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
364 |
lastErrorNumber := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
365 |
exitStatus := nil. |
2266 | 366 |
exitSema := Semaphore new name:'pipe exitSema'. |
1648
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 |
pipeFdArray := OperatingSystem makePipe. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
369 |
pipeFdArray isNil ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
370 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
371 |
^ self openError |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
372 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
373 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
374 |
mode = 'r' ifTrue:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
375 |
execFd := pipeFdArray at:2. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
376 |
execFdArray := Array with:0 with:execFd with:2. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
377 |
myFd := pipeFdArray at:1. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
378 |
] ifFalse:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
379 |
execFd := pipeFdArray at:1. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
380 |
execFdArray := Array with:execFd with:1 with:2. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
381 |
myFd := pipeFdArray at:2. |
326 | 382 |
]. |
255 | 383 |
|
2925 | 384 |
osType := OperatingSystem platformName. |
385 |
osType == #unix ifTrue:[ |
|
2598 | 386 |
shellPath := '/bin/sh'. |
387 |
shellArgs := Array with:'sh' with:'-c' with:aCommandString. |
|
2925 | 388 |
] ifFalse:[ |
389 |
osType == #win32 ifTrue:[ |
|
390 |
shellPath := 'C:\WINNT\System32\cmd /c'. |
|
391 |
shellArgs := aCommandString. |
|
392 |
] ifFalse:[ |
|
393 |
osType == #vms ifTrue:[ |
|
394 |
shellPath := 'DCL - ignores shellPath'. |
|
395 |
shellArgs := aCommandString. |
|
396 |
] ifFalse:[ |
|
2953 | 397 |
OperatingSystem closeFd:execFd; closeFd:myFd. |
2925 | 398 |
"/ |
399 |
"/ dont know how to do it ... |
|
400 |
"/ |
|
401 |
^ self openError |
|
402 |
] |
|
403 |
] |
|
2598 | 404 |
]. |
405 |
||
406 |
"/ must block here, to avoid races due to early finishing |
|
407 |
"/ subprocesses ... |
|
408 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
409 |
blocked := OperatingSystem blockInterrupts. |
2598 | 410 |
|
411 |
pid := Processor |
|
412 |
monitor:[ |
|
413 |
OperatingSystem |
|
414 |
exec:shellPath |
|
415 |
withArguments:shellArgs |
|
416 |
fileDescriptors:execFdArray |
|
417 |
closeDescriptors:(Array with:myFd) |
|
418 |
fork:true |
|
419 |
newPgrp:true. |
|
420 |
] |
|
421 |
action:[:status | |
|
422 |
status stillAlive ifFalse:[ |
|
423 |
exitStatus := status. |
|
424 |
pid := nil. |
|
425 |
exitSema signal. |
|
426 |
]. |
|
427 |
]. |
|
428 |
||
2498
eb0bad72f05e
Fixe race when waiting for SIGCHLD.
Stefan Vogel <sv@exept.de>
parents:
2266
diff
changeset
|
429 |
OperatingSystem closeFd:execFd. |
eb0bad72f05e
Fixe race when waiting for SIGCHLD.
Stefan Vogel <sv@exept.de>
parents:
2266
diff
changeset
|
430 |
pid notNil ifTrue:[ |
2925 | 431 |
(osType == #win32) ifTrue:[ |
2598 | 432 |
self setFileHandle:myFd mode:mode |
433 |
] ifFalse:[ |
|
434 |
self setFileDescriptor:myFd mode:mode. |
|
435 |
] |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
436 |
] ifFalse:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
437 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
438 |
OperatingSystem closeFd:myFd. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
439 |
]. |
2598 | 440 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
441 |
blocked ifFalse:[ |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
442 |
OperatingSystem unblockInterrupts |
1 | 443 |
]. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
444 |
|
2961 | 445 |
lastErrorNumber notNil ifTrue:[ |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
446 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
447 |
the pipe open failed for some reason ... |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
448 |
... 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
|
449 |
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
|
450 |
the unix process) |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
451 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
452 |
^ self openError |
255 | 453 |
]. |
2961 | 454 |
|
455 |
commandString := aCommandString. |
|
456 |
%{ |
|
457 |
/* LINUX stdio is corrupt here ... */ |
|
458 |
#ifdef BUGGY_STDIO_LIB |
|
459 |
__INST(buffered) = false; |
|
460 |
#else |
|
461 |
__INST(buffered) = true; |
|
462 |
#endif |
|
463 |
%}. |
|
464 |
hitEOF := false. |
|
465 |
binary := false. |
|
466 |
Lobby register:self. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
467 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
468 |
"Modified: 23.4.1996 / 17:05:59 / stefan" |
1 | 469 |
! |
470 |
||
471 |
readingFrom:command |
|
472 |
"setup the receiver to read from command" |
|
473 |
||
326 | 474 |
mode := #readonly. didWrite := false. |
1 | 475 |
^ self openPipeFor:command withMode:'r' |
476 |
! |
|
477 |
||
478 |
writingTo:command |
|
479 |
"setup the receiver to write to command" |
|
480 |
||
326 | 481 |
mode := #writeonly. didWrite := true. |
1 | 482 |
^ self openPipeFor:command withMode:'w' |
483 |
! ! |
|
613 | 484 |
|
2266 | 485 |
!PipeStream class methodsFor:'documentation'! |
1295 | 486 |
|
487 |
version |
|
2961 | 488 |
^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.57 1997-09-20 22:10:22 cg Exp $' |
1295 | 489 |
! ! |
613 | 490 |
PipeStream initialize! |