author | Claus Gittinger <cg@exept.de> |
Tue, 15 Aug 2000 16:34:33 +0200 | |
changeset 5523 | 86ad12f4afc9 |
parent 5481 | 680d6de80808 |
child 5624 | fd80180ce3e2 |
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 |
||
5388
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
14 |
|
269 | 15 |
NonPositionableExternalStream subclass:#PipeStream |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
16 |
instanceVariableNames:'commandString pid exitStatus exitSema exitAction' |
1295 | 17 |
classVariableNames:'BrokenPipeSignal' |
18 |
poolDictionaries:'' |
|
19 |
category:'Streams-External' |
|
88 | 20 |
! |
2 | 21 |
|
217 | 22 |
!PipeStream primitiveDefinitions! |
88 | 23 |
%{ |
437 | 24 |
|
4616 | 25 |
#if defined(WIN32) |
793 | 26 |
# undef UNIX_LIKE |
27 |
# define MSDOS_LIKE |
|
28 |
#endif |
|
29 |
||
88 | 30 |
#include <stdio.h> |
437 | 31 |
#define _STDIO_H_INCLUDED_ |
32 |
||
88 | 33 |
#include <errno.h> |
437 | 34 |
#define _ERRNO_H_INCLUDED_ |
35 |
||
88 | 36 |
#ifndef transputer |
37 |
# include <sys/types.h> |
|
38 |
# include <sys/stat.h> |
|
39 |
#endif |
|
230 | 40 |
|
41 |
/* |
|
42 |
* on some systems errno is a macro ... check for it here |
|
43 |
*/ |
|
44 |
#ifndef errno |
|
45 |
extern errno; |
|
46 |
#endif |
|
47 |
||
1663 | 48 |
#ifdef LINUX |
49 |
# define BUGGY_STDIO_LIB |
|
50 |
#endif |
|
51 |
||
88 | 52 |
%} |
180 | 53 |
! ! |
88 | 54 |
|
325 | 55 |
!PipeStream primitiveFunctions! |
56 |
%{ |
|
57 |
||
58 |
/* |
|
2925 | 59 |
* no longer needed - popen is useless ... |
60 |
*/ |
|
61 |
#undef NEED_POPEN_WITH_VFORK |
|
62 |
||
63 |
||
64 |
/* |
|
325 | 65 |
* some systems (i.e. ultrix) use fork; |
66 |
* were better off with a popen based on vfork ... |
|
67 |
*/ |
|
68 |
#ifdef NEED_POPEN_WITH_VFORK |
|
69 |
||
70 |
static int popen_pid = 0; |
|
71 |
||
72 |
FILE * |
|
73 |
popen(command, type) |
|
74 |
/* const */ char *command; |
|
75 |
/* const */ char *type; |
|
76 |
{ |
|
77 |
int pipes[2]; |
|
78 |
int itype = (strcmp(type, "w") == 0 ? 1 : 0); |
|
79 |
||
80 |
if (pipe(pipes) == -1) |
|
326 | 81 |
return NULL; |
325 | 82 |
|
83 |
switch (popen_pid = vfork()) { |
|
84 |
case -1: |
|
326 | 85 |
(void)close(pipes[0]); |
86 |
(void)close(pipes[1]); |
|
87 |
return NULL; |
|
325 | 88 |
|
89 |
case 0: |
|
326 | 90 |
if (itype) { |
91 |
dup2(pipes[0], fileno(stdin)); |
|
92 |
close(pipes[1]); |
|
93 |
} else { |
|
94 |
dup2(pipes[1], fileno(stdout)); |
|
95 |
close(pipes[0]); |
|
96 |
} |
|
97 |
execl("/bin/sh", "/bin/sh", "-c", command, 0); |
|
98 |
fprintf(stderr, "XRN Error: failed the execlp\n"); |
|
99 |
_exit(-1); |
|
100 |
/* NOTREACHED */ |
|
325 | 101 |
|
102 |
default: |
|
326 | 103 |
if (itype) { |
104 |
close(pipes[0]); |
|
105 |
return fdopen(pipes[1], "w"); |
|
106 |
} else { |
|
107 |
close(pipes[1]); |
|
108 |
return fdopen(pipes[0], "r"); |
|
109 |
} |
|
325 | 110 |
} |
111 |
} |
|
112 |
||
113 |
int |
|
114 |
pclose(str) |
|
115 |
FILE *str; |
|
116 |
{ |
|
117 |
int pd = 0; |
|
118 |
int status; |
|
119 |
int err; |
|
120 |
||
121 |
err = fclose(str); |
|
122 |
||
123 |
do { |
|
326 | 124 |
if ((pd = wait(&status)) == -1) |
125 |
{ |
|
126 |
err = EOF; |
|
127 |
break; |
|
128 |
} |
|
325 | 129 |
} while (pd != popen_pid); |
130 |
||
131 |
if (err == EOF) |
|
326 | 132 |
return -1; |
325 | 133 |
|
134 |
if (status) |
|
326 | 135 |
status >>= 8; /* exit status in high byte */ |
325 | 136 |
|
137 |
return status; |
|
138 |
} |
|
139 |
||
2925 | 140 |
#endif /* NEED_POPEN_WITH_VFORK */ |
325 | 141 |
|
142 |
%} |
|
143 |
! ! |
|
144 |
||
2266 | 145 |
!PipeStream class methodsFor:'documentation'! |
613 | 146 |
|
147 |
copyright |
|
148 |
" |
|
149 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
150 |
All Rights Reserved |
|
151 |
||
152 |
This software is furnished under a license and may be used |
|
153 |
only in accordance with the terms of that license and with the |
|
154 |
inclusion of the above copyright notice. This software may not |
|
155 |
be provided or otherwise made available to, or used by, any |
|
156 |
other person. No title to or ownership of the software is |
|
157 |
hereby transferred. |
|
158 |
" |
|
159 |
! |
|
160 |
||
161 |
documentation |
|
162 |
" |
|
163 |
Pipestreams allow reading or writing from/to a unix command. |
|
164 |
For example, to get a stream reading the output of an 'ls -l' |
|
165 |
command, a PipeStream can be created with: |
|
166 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
167 |
PipeStream readingFrom:'ls -l' |
613 | 168 |
|
169 |
the characters of the commands output can be read using the |
|
170 |
standard stream messages as next, nextLine etc. |
|
171 |
||
172 |
If a writing pipeStream is written to, after the command has finished, |
|
2966 | 173 |
UNIX will generate an error-signal (SIGPIPE), which will raise the BrokenPipeSignal. |
613 | 174 |
Thus, to handle this condition correctly, the following code is suggested: |
175 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
176 |
|p| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
177 |
p := PipeStream writingTo:'echo hello'. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
178 |
PipeStream brokenPipeSignal handle:[:ex | |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
179 |
'broken pipe' printNewline. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
180 |
p shutDown. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
181 |
ex return |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
182 |
] do:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
183 |
p nextPutLine:'oops'. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
184 |
'after write' printNewline. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
185 |
p close. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
186 |
'after close' printNewline |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
187 |
] |
613 | 188 |
|
1663 | 189 |
Notice, that iff the Stream is buffered, the Signal may occur some time after |
613 | 190 |
the write - or even at close time; to avoid a recursive signal in the exception |
2966 | 191 |
handler, a #shutDown is useful there; if you use close in the handler, this would |
192 |
try to send any buffered output to the pipe, leading to another brokenPipe exception. |
|
1295 | 193 |
|
1662
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
194 |
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
|
195 |
buggy (trying to restart the read ...) |
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
196 |
|
2966 | 197 |
Currently, no filtering pipeStreams (i.e. both reading AND writing) are provided. |
198 |
However, if you look at how things are setup, this can be implemented using the |
|
199 |
low level primitives #mapePipe and #executeCommand from the OS class protocol. |
|
200 |
||
1295 | 201 |
[author:] |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
202 |
Claus Gittinger |
2966 | 203 |
|
204 |
[see also:] |
|
205 |
ExternalStream FileStream Socket |
|
206 |
OperatingSystem |
|
613 | 207 |
" |
208 |
! ! |
|
209 |
||
2266 | 210 |
!PipeStream class methodsFor:'initialization'! |
2 | 211 |
|
212 |
initialize |
|
213 |
"setup the signal" |
|
214 |
||
57 | 215 |
BrokenPipeSignal isNil ifTrue:[ |
582
21f08116b28d
BrokenPipeSignal now a child of WriteErrorSignal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
216 |
BrokenPipeSignal := WriteErrorSignal newSignalMayProceed:true. |
159 | 217 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal. |
218 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'. |
|
57 | 219 |
] |
2 | 220 |
! ! |
221 |
||
2266 | 222 |
!PipeStream class methodsFor:'instance creation'! |
2 | 223 |
|
613 | 224 |
readingFrom:commandString |
225 |
"create and return a new pipeStream which can read from the unix command |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
226 |
given by command. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
227 |
The commands error output is send to my own error output." |
2 | 228 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
229 |
^ self |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
230 |
readingFrom:commandString |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
231 |
errorDisposition:#stderr |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
232 |
inDirectory:nil |
613 | 233 |
|
2966 | 234 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
235 |
PipeStream readingFrom:'ls -l'. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
236 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
237 |
|
2966 | 238 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
239 |
p := PipeStream readingFrom:'ls -l'. |
2966 | 240 |
Transcript showCR:p nextLine. |
241 |
p close |
|
242 |
" |
|
243 |
||
244 |
" |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
245 |
|s| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
246 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
247 |
(Delay forSeconds:2) wait. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
248 |
s shutDown |
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 |
|
2966 | 251 |
"vms: |
252 |
PipeStream readingFrom:'dir'. |
|
253 |
" |
|
254 |
||
255 |
" |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
256 |
|p| |
2966 | 257 |
p := PipeStream readingFrom:'dir'. |
258 |
Transcript showCR:p nextLine. |
|
259 |
p close |
|
260 |
" |
|
261 |
||
262 |
"msdos: |
|
263 |
PipeStream readingFrom:'dir'. |
|
264 |
" |
|
265 |
" |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
266 |
|p| |
2966 | 267 |
p := PipeStream readingFrom:'dir'. |
268 |
Transcript showCR:p nextLine. |
|
269 |
p close |
|
270 |
" |
|
271 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
272 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 273 |
! |
1 | 274 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
275 |
readingFrom:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
276 |
"similar to #readingFrom, but changes the directory while |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
277 |
executing the command. Use this if a command is to be |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
278 |
executed in another directory, to avoid any OS dependencies |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
279 |
in your code. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
280 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
281 |
#discard causes stderr to be discarded (/dev/null), |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
282 |
#inline causes it to be merged into the PipeStream and |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
283 |
#stderr causes it to be written to smalltalks own stderr. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
284 |
Nil is treated like #stderr" |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
285 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
286 |
^ self basicNew |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
287 |
openPipeFor:commandString |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
288 |
withMode:'r' |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
289 |
errorDisposition:errorDisposition |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
290 |
inDirectory:aDirectory |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
291 |
! |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
292 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
293 |
readingFrom:commandString inDirectory:aDirectory |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
294 |
"similar to #readingFrom, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
295 |
executing the command. Use this if a command is to be |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
296 |
executed in another directory, to avoid any OS dependencies |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
297 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
298 |
The commands error output is send to my own error output." |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
299 |
|
3563 | 300 |
^ self |
301 |
readingFrom:commandString |
|
302 |
errorDisposition:#stderr |
|
303 |
inDirectory:aDirectory |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
304 |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
305 |
"Modified: 24.9.1997 / 09:33:48 / stefan" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
306 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
307 |
|
2 | 308 |
writingTo:commandString |
1 | 309 |
"create and return a new pipeStream which can write to the unix command |
310 |
given by command." |
|
311 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
312 |
^ self |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
313 |
writingTo:commandString errorDisposition:#stderr inDirectory:nil |
1 | 314 |
|
2966 | 315 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
316 |
PipeStream writingTo:'sort' |
2966 | 317 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
318 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
319 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
320 |
writingTo:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
321 |
"similar to #writingTo, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
322 |
executing the command. Use this if a command is to be |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
323 |
executed in another directory, to avoid any OS dependencies |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
324 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
325 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
326 |
#discard causes stderr to be discarded (/dev/null), |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
327 |
#inline causes it to be written to smalltalks own stdout and |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
328 |
#stderr causes it to be written to smalltalks own stderr. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
329 |
Nil is treated like #stderr" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
330 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
331 |
^ self basicNew |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
332 |
openPipeFor:commandString |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
333 |
withMode:'w' |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
334 |
errorDisposition:errorDisposition |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
335 |
inDirectory:aDirectory |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
336 |
! |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
337 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
338 |
writingTo:commandString inDirectory:aDirectory |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
339 |
"create and return a new pipeStream which can write to the unix command |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
340 |
given by command. The command is executed in the given directory." |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
341 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
342 |
^ self |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
343 |
writingTo:commandString errorDisposition:#stderr inDirectory:aDirectory |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
344 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
345 |
"unix: |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
346 |
PipeStream writingTo:'sort' |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
347 |
" |
613 | 348 |
! ! |
349 |
||
2266 | 350 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 351 |
|
613 | 352 |
brokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
353 |
"return the signal used to handle SIGPIPE unix-signals. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
354 |
Since SIGPIPE is asynchronous, we can't decide which smalltalk process |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
355 |
should handle BrokenPipeSignal. So the system doesn't raise |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
356 |
BrokenPipeSignal for SIGPIPE any longer." |
1 | 357 |
|
613 | 358 |
^ BrokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
359 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
360 |
"Modified: 24.9.1997 / 09:43:23 / stefan" |
1 | 361 |
! ! |
362 |
||
99 | 363 |
!PipeStream methodsFor:'accessing'! |
364 |
||
365 |
commandString |
|
366 |
"return the command string" |
|
367 |
||
368 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
369 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
370 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
371 |
exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
372 |
"return exitStatus" |
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 |
^ exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
375 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
376 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
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 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
379 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
380 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
381 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
382 |
^ pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
383 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
384 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
99 | 385 |
! ! |
386 |
||
5243 | 387 |
!PipeStream methodsFor:'finalization'! |
388 |
||
389 |
disposed |
|
390 |
"redefined to avoid blocking in close." |
|
391 |
||
392 |
self shutDown |
|
393 |
! ! |
|
394 |
||
1 | 395 |
!PipeStream methodsFor:'instance release'! |
396 |
||
397 |
closeFile |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
398 |
"low level close |
230 | 399 |
This waits for the command to finish. |
400 |
Use shutDown for a fast (nonBlocking) close." |
|
1 | 401 |
|
3834
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
402 |
|tpid| |
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
403 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
404 |
filePointer notNil ifTrue:[ |
5456 | 405 |
super closeFile. |
406 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
407 |
self terminatePipeCommand. |
|
5388
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
408 |
]. |
3834
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
409 |
|
5388
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
410 |
filePointer := nil. |
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
411 |
pid notNil ifTrue:[ |
5456 | 412 |
"/ wait for the pipe-command to terminate. |
413 |
self waitForPipeCommandWithTimeout:nil. |
|
5388
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
414 |
]. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
415 |
]. |
3833
3ae90f8da01a
terminate process BEFORE closing the pipe under win32
Claus Gittinger <cg@exept.de>
parents:
3791
diff
changeset
|
416 |
|
3834
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
417 |
"Modified: / 12.9.1998 / 16:51:04 / cg" |
369 | 418 |
! |
419 |
||
420 |
closeFileDescriptor |
|
421 |
"alternative very low level close |
|
422 |
This closes the underlying OS-fileDescriptor |
|
423 |
- and will NOT write any buffered data to the stream. |
|
424 |
You have been warned." |
|
425 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
426 |
|action| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
427 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
428 |
%{ |
3296 | 429 |
#if !defined(transputer) |
369 | 430 |
OBJ fp; |
431 |
FILE *f; |
|
4616 | 432 |
extern close(); |
369 | 433 |
|
1133 | 434 |
if ((fp = __INST(filePointer)) != nil) { |
435 |
__INST(filePointer) = nil; |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
436 |
f = __FILEVal(fp); |
5203 | 437 |
if (@global(ExternalStream:FileOpenTrace) == true) { |
438 |
fprintf(stderr, "close [PipeStream] %x fd=%d\n", f, fileno(f)); |
|
439 |
} |
|
3744 | 440 |
#ifdef WIN32 |
4616 | 441 |
__STX_C_CALL1((void*)close, (void*)fileno(f)); |
3744 | 442 |
#else |
369 | 443 |
__BEGIN_INTERRUPTABLE__ |
444 |
close(fileno(f)); |
|
445 |
__END_INTERRUPTABLE__ |
|
3744 | 446 |
#endif |
369 | 447 |
} |
3296 | 448 |
#endif /* not transputer */ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
449 |
%}. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
450 |
exitAction notNil ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
451 |
action := exitAction. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
452 |
exitAction := nil. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
453 |
action value. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
454 |
] |
613 | 455 |
! |
456 |
||
457 |
shutDown |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
458 |
"close the Stream, ignoring any broken-pipe errors. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
459 |
Terminate the command" |
613 | 460 |
|
461 |
BrokenPipeSignal catch:[ |
|
5456 | 462 |
Lobby unregister:self. |
3833
3ae90f8da01a
terminate process BEFORE closing the pipe under win32
Claus Gittinger <cg@exept.de>
parents:
3791
diff
changeset
|
463 |
|
5456 | 464 |
"/ terminate first under windows. |
465 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
466 |
self terminatePipeCommand. |
|
467 |
]. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
468 |
|
5456 | 469 |
self closeFileDescriptor. |
3834
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
470 |
|
5456 | 471 |
"/ terminate last under unix. |
472 |
OperatingSystem isMSDOSlike ifFalse:[ |
|
473 |
self terminatePipeCommand. |
|
474 |
]. |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
475 |
|
613 | 476 |
] |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
477 |
|
3833
3ae90f8da01a
terminate process BEFORE closing the pipe under win32
Claus Gittinger <cg@exept.de>
parents:
3791
diff
changeset
|
478 |
"Modified: / 23.5.1996 / 09:15:41 / stefan" |
3834
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
479 |
"Modified: / 12.9.1998 / 16:50:18 / cg" |
5456 | 480 |
! |
481 |
||
482 |
terminatePipeCommand |
|
483 |
|tpid| |
|
484 |
||
485 |
(tpid := pid) notNil ifTrue:[ |
|
486 |
OperatingSystem terminateProcess:tpid. |
|
487 |
OperatingSystem terminateProcessGroup:tpid. |
|
488 |
pid := nil. |
|
489 |
]. |
|
490 |
||
491 |
! |
|
492 |
||
493 |
waitForPipeCommandWithTimeout:seconds |
|
494 |
"wait for the pipe command to terminate itself. |
|
495 |
Return true, if a timeout occurred." |
|
496 |
||
497 |
pid notNil ifTrue:[ |
|
498 |
[ |
|
499 |
pid notNil ifTrue:[ |
|
500 |
exitSema waitWithTimeout:seconds. |
|
501 |
] |
|
502 |
] valueUninterruptably |
|
503 |
]. |
|
504 |
^ pid notNil |
|
1 | 505 |
! ! |
506 |
||
507 |
!PipeStream methodsFor:'private'! |
|
508 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
509 |
exitAction:aBlock |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
510 |
"define a block to be evaluated when the pipe is closed. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
511 |
This is only used with VMS, to remove any temporary COM file. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
512 |
(see readingFrom:inDirectory:)" |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
513 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
514 |
exitAction := aBlock |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
515 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
516 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
517 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:err inDirectory:aDirectory |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
518 |
"open a pipe to the OS command in commandString; |
5481 | 519 |
rwMode may be 'r' or 'w'. |
520 |
errorDisposition controls where the stdErr output should go, |
|
521 |
and may be one of #discard, #inline or #stderr (default). |
|
522 |
#discard causes stderr to be discarded (/dev/null), |
|
523 |
#inline causes it to be written to smalltalks own stdout and |
|
524 |
#stderr causes it to be written to smalltalks own stderr. |
|
525 |
Nil is treated like #stderr" |
|
3296 | 526 |
|
527 |
|blocked pipeFdArray execFdArray execFd myFd shellAndArgs |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
528 |
shellPath shellArgs closeFdArray mbx mbxName |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
529 |
realCmd execDirectory tmpComFile nullOutput| |
3296 | 530 |
|
531 |
filePointer notNil ifTrue:[ |
|
5481 | 532 |
"the pipe was already open ... |
533 |
this should (can) not happen." |
|
534 |
^ self errorAlreadyOpen |
|
3296 | 535 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
536 |
|
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
537 |
rwMode = 'r' ifTrue:[ |
5481 | 538 |
mode := #readonly. didWrite := false. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
539 |
] ifFalse:[ |
5481 | 540 |
mode := #writeonly. didWrite := true. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
541 |
]. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
542 |
|
3296 | 543 |
lastErrorNumber := nil. |
544 |
exitStatus := nil. |
|
545 |
exitSema := Semaphore new name:'pipe exitSema'. |
|
546 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
547 |
realCmd := aCommandString. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
548 |
execDirectory := aDirectory. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
549 |
|
3791 | 550 |
OperatingSystem isVMSlike ifTrue:[ |
5481 | 551 |
"/ |
552 |
"/ the generated COM-file includes a 'set default' |
|
553 |
"/ |
|
554 |
tmpComFile := OperatingSystem createCOMFileForVMSCommand:aCommandString in:aDirectory. |
|
555 |
realCmd := '@' , tmpComFile osName. |
|
556 |
execDirectory := nil. |
|
4616 | 557 |
|
5481 | 558 |
mbx := OperatingSystem createMailBox. |
559 |
mbx isNil ifTrue:[ |
|
560 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
561 |
tmpComFile delete. |
|
562 |
^ self openError |
|
563 |
]. |
|
564 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
|
3296 | 565 |
|
5481 | 566 |
"/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR. |
567 |
shellPath := ''. |
|
568 |
shellArgs := realCmd. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
569 |
|
5481 | 570 |
rwMode = 'r' ifTrue:[ |
571 |
execFdArray := Array with:0 with:mbx with:2. |
|
572 |
(err == #inline or:[err == #stdout]) ifTrue:[ |
|
573 |
execFdArray at:3 put:mbx |
|
574 |
] |
|
575 |
] ifFalse:[ |
|
576 |
execFdArray := Array with:mbx with:1 with:2. |
|
577 |
(err == #inline or:[err == #stdout]) ifTrue:[ |
|
578 |
execFdArray at:3 put:1 |
|
579 |
] |
|
580 |
]. |
|
581 |
closeFdArray := nil. |
|
3296 | 582 |
] ifFalse:[ |
5481 | 583 |
OperatingSystem isUNIXlike ifTrue:[ |
584 |
aDirectory notNil ifTrue:[ |
|
585 |
"/ unix - prepend a 'cd' to the command |
|
586 |
realCmd := 'cd ' , aDirectory asFilename name, '; ' , aCommandString. |
|
587 |
] ifFalse:[ |
|
588 |
realCmd := aCommandString |
|
589 |
]. |
|
590 |
execDirectory := nil. |
|
591 |
]. |
|
3296 | 592 |
|
5481 | 593 |
pipeFdArray := OperatingSystem makePipe. |
594 |
pipeFdArray isNil ifTrue:[ |
|
595 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
596 |
^ self openError |
|
597 |
]. |
|
3296 | 598 |
|
5481 | 599 |
shellAndArgs := OperatingSystem commandAndArgsForOSCommand:realCmd. |
600 |
shellPath := shellAndArgs at:1. |
|
601 |
shellArgs := shellAndArgs at:2. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
602 |
|
5481 | 603 |
rwMode = 'r' ifTrue:[ |
604 |
myFd := pipeFdArray at:1. |
|
605 |
execFd := pipeFdArray at:2. |
|
606 |
execFdArray := Array with:0 with:execFd with:2. |
|
607 |
(err == #inline or:[err == #stdout]) ifTrue:[ |
|
608 |
execFdArray at:3 put:execFd |
|
609 |
] |
|
610 |
] ifFalse:[ |
|
611 |
myFd := pipeFdArray at:2. |
|
612 |
execFd := pipeFdArray at:1. |
|
613 |
execFdArray := Array with:execFd with:1 with:2. |
|
614 |
(err == #inline or:[err == #stdout]) ifTrue:[ |
|
615 |
execFdArray at:3 put:1 |
|
616 |
] |
|
617 |
]. |
|
618 |
closeFdArray := Array with:myFd. |
|
3296 | 619 |
]. |
620 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
621 |
err == #discard ifTrue:[ |
5481 | 622 |
nullOutput := Filename nullDevice writeStream. |
623 |
execFdArray at:3 put:nullOutput fileDescriptor |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
624 |
]. |
3296 | 625 |
|
626 |
"/ must block here, to avoid races due to early finishing |
|
627 |
"/ subprocesses ... |
|
628 |
||
629 |
blocked := OperatingSystem blockInterrupts. |
|
630 |
||
3563 | 631 |
pid := Processor |
5481 | 632 |
monitor:[ |
633 |
OperatingSystem |
|
634 |
exec:shellPath |
|
635 |
withArguments:shellArgs |
|
636 |
fileDescriptors:execFdArray |
|
637 |
closeDescriptors:closeFdArray |
|
638 |
fork:true |
|
639 |
newPgrp:true |
|
640 |
inDirectory:execDirectory. |
|
641 |
] |
|
642 |
action:[:status | |
|
643 |
status stillAlive ifFalse:[ |
|
644 |
exitStatus := status. |
|
645 |
OperatingSystem closePid:pid. |
|
646 |
pid := nil. |
|
647 |
exitSema signal. |
|
648 |
]. |
|
649 |
]. |
|
3296 | 650 |
|
3791 | 651 |
OperatingSystem isVMSlike ifFalse:[ |
5481 | 652 |
OperatingSystem closeFd:execFd. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
653 |
]. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
654 |
|
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
655 |
nullOutput notNil ifTrue:[ |
5481 | 656 |
nullOutput close |
3296 | 657 |
]. |
658 |
||
659 |
pid notNil ifTrue:[ |
|
5481 | 660 |
OperatingSystem isVMSlike ifTrue:[ |
661 |
"/ |
|
662 |
"/ reopen the mailbox as a file ... |
|
663 |
"/ |
|
664 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
|
665 |
mbxName notNil ifTrue:[ |
|
666 |
super open:mbxName withMode:rwMode. |
|
667 |
exitAction := [tmpComFile delete]. |
|
668 |
]. |
|
669 |
] ifFalse:[ |
|
670 |
self setFileDescriptor:myFd mode:rwMode. |
|
671 |
] |
|
3296 | 672 |
] ifFalse:[ |
5481 | 673 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
674 |
OperatingSystem isVMSlike ifTrue:[ |
|
675 |
OperatingSystem destroyMailBox:mbx. |
|
676 |
tmpComFile delete. |
|
677 |
] ifFalse:[ |
|
678 |
OperatingSystem closeFd:myFd. |
|
679 |
]. |
|
3296 | 680 |
]. |
681 |
||
682 |
blocked ifFalse:[ |
|
5481 | 683 |
OperatingSystem unblockInterrupts |
3296 | 684 |
]. |
685 |
||
686 |
lastErrorNumber notNil ifTrue:[ |
|
5481 | 687 |
" |
688 |
the pipe open failed for some reason ... |
|
689 |
... this may be either due to an invalid command string, |
|
690 |
or due to the system running out of memory (when forking |
|
691 |
the unix process) |
|
692 |
" |
|
693 |
exitAction value. |
|
694 |
^ self openError |
|
3296 | 695 |
]. |
696 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
697 |
commandString := realCmd. |
3296 | 698 |
%{ |
699 |
/* LINUX stdio is corrupt here ... */ |
|
3744 | 700 |
#if defined(BUGGY_STDIO_LIB) || defined(WIN32) |
3296 | 701 |
__INST(buffered) = false; |
702 |
#else |
|
703 |
__INST(buffered) = true; |
|
704 |
#endif |
|
705 |
%}. |
|
706 |
position := 1. |
|
707 |
hitEOF := false. |
|
708 |
binary := false. |
|
709 |
Lobby register:self. |
|
710 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
711 |
"Modified: / 23.4.1996 / 17:05:59 / stefan" |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
712 |
"Modified: / 28.1.1998 / 14:47:34 / md" |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
713 |
"Created: / 19.5.1999 / 12:28:54 / cg" |
1 | 714 |
! ! |
613 | 715 |
|
2266 | 716 |
!PipeStream class methodsFor:'documentation'! |
1295 | 717 |
|
718 |
version |
|
5481 | 719 |
^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.86 2000-07-25 14:13:57 cg Exp $' |
1295 | 720 |
! ! |
613 | 721 |
PipeStream initialize! |