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