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