author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 11083 | 988c0b2e8a26 |
child 17734 | 406b1590afe8 |
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 |
" |
|
5388
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
12 |
"{ Package: 'stx:libbasic' }" |
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
13 |
|
269 | 14 |
NonPositionableExternalStream subclass:#PipeStream |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
15 |
instanceVariableNames:'commandString pid exitStatus exitSema exitAction' |
1295 | 16 |
classVariableNames:'BrokenPipeSignal' |
17 |
poolDictionaries:'' |
|
18 |
category:'Streams-External' |
|
88 | 19 |
! |
2 | 20 |
|
217 | 21 |
!PipeStream primitiveDefinitions! |
88 | 22 |
%{ |
437 | 23 |
|
4616 | 24 |
#if defined(WIN32) |
793 | 25 |
# undef UNIX_LIKE |
26 |
# define MSDOS_LIKE |
|
27 |
#endif |
|
28 |
||
88 | 29 |
#include <stdio.h> |
437 | 30 |
#define _STDIO_H_INCLUDED_ |
31 |
||
88 | 32 |
#include <errno.h> |
437 | 33 |
#define _ERRNO_H_INCLUDED_ |
34 |
||
88 | 35 |
#ifndef transputer |
36 |
# include <sys/types.h> |
|
37 |
# include <sys/stat.h> |
|
38 |
#endif |
|
230 | 39 |
|
40 |
/* |
|
41 |
* on some systems errno is a macro ... check for it here |
|
42 |
*/ |
|
43 |
#ifndef errno |
|
44 |
extern errno; |
|
45 |
#endif |
|
46 |
||
1663 | 47 |
#ifdef LINUX |
48 |
# define BUGGY_STDIO_LIB |
|
49 |
#endif |
|
50 |
||
88 | 51 |
%} |
180 | 52 |
! ! |
88 | 53 |
|
325 | 54 |
!PipeStream primitiveFunctions! |
55 |
%{ |
|
56 |
||
57 |
/* |
|
2925 | 58 |
* no longer needed - popen is useless ... |
59 |
*/ |
|
60 |
#undef NEED_POPEN_WITH_VFORK |
|
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); |
|
10408 | 96 |
console_fprintf(stderr, "PipeStream [warning]: execlp failed in popen\n"); |
326 | 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 |
||
10408 | 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 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
170 |
Example for writing to a command: |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
171 |
|
10408 | 172 |
PipeStream writingTo:'cat >/tmp/x' |
613 | 173 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
174 |
Bidirectional pipestreams (supporting both reading an writing) may be used for filters: |
613 | 175 |
|
10408 | 176 |
PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/''' |
1295 | 177 |
|
1662
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
178 |
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
|
179 |
buggy (trying to restart the read ...) |
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
180 |
|
1295 | 181 |
[author:] |
10408 | 182 |
Claus Gittinger |
2966 | 183 |
|
184 |
[see also:] |
|
10408 | 185 |
ExternalStream FileStream Socket |
186 |
OperatingSystem |
|
613 | 187 |
" |
188 |
! ! |
|
189 |
||
2266 | 190 |
!PipeStream class methodsFor:'initialization'! |
2 | 191 |
|
192 |
initialize |
|
193 |
"setup the signal" |
|
194 |
||
57 | 195 |
BrokenPipeSignal isNil ifTrue:[ |
10408 | 196 |
BrokenPipeSignal := WriteError newSignalMayProceed:true. |
197 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal. |
|
198 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'. |
|
57 | 199 |
] |
2 | 200 |
! ! |
201 |
||
2266 | 202 |
!PipeStream class methodsFor:'instance creation'! |
2 | 203 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
204 |
bidirectionalFor:commandString |
10408 | 205 |
"create and return a new bidirectonal pipeStream which can both be written to |
206 |
and read from the unix command given by commandString. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
207 |
The commands error output is send to my own error output." |
2 | 208 |
|
10408 | 209 |
^ self |
210 |
bidirectionalFor:commandString |
|
211 |
errorDisposition:#stderr |
|
212 |
inDirectory:nil |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
213 |
|
2966 | 214 |
" |
10408 | 215 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
216 |
|
10408 | 217 |
p := PipeStream bidirectionalFor:'cat -u'. |
218 |
p nextPutAll:'Wer ist der Bürgermeister von Wesel'; cr. |
|
219 |
Transcript showCR:p nextLine. |
|
220 |
p close |
|
2966 | 221 |
" |
222 |
||
223 |
" |
|
10408 | 224 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
225 |
|
10408 | 226 |
p := PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/'''. |
227 |
p nextPutAll:'Hello world'; cr. |
|
228 |
p shutDownOutput. |
|
229 |
Transcript showCR:p nextLine. |
|
230 |
p close |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
231 |
" |
9313 | 232 |
|
233 |
" |
|
10408 | 234 |
|p| |
9313 | 235 |
|
10408 | 236 |
p := PipeStream bidirectionalFor:'wc'. |
237 |
p nextPutAll:'Hello world'; cr. |
|
238 |
p shutDownOutput. |
|
239 |
Transcript showCR:p nextLine. |
|
240 |
p close |
|
9313 | 241 |
" |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
242 |
! |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
243 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
244 |
bidirectionalFor:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
10408 | 245 |
"create and return a new bidirectonal pipeStream which can both be written to |
246 |
and read from the unix command given by commandString. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
247 |
The directory will be changed to aDirectory while |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
248 |
executing the command. Use this if a command is to be |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
249 |
executed in another directory, to avoid any OS dependencies |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
250 |
in your code. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
251 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
252 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
10408 | 253 |
#discard causes stderr to be discarded (/dev/null), |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
254 |
#inline causes it to be written to smalltalks own stdout and |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
255 |
#stderr causes it to be written to smalltalks own stderr. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
256 |
Nil is treated like #stderr" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
257 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
258 |
^ self basicNew |
10408 | 259 |
openPipeFor:commandString |
260 |
withMode:#'r+' |
|
261 |
errorDisposition:errorDisposition |
|
262 |
inDirectory:aDirectory |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
263 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
264 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
265 |
readingFrom:commandString |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
266 |
"create and return a new pipeStream which can read from the unix command |
10408 | 267 |
given by commandString. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
268 |
The commands error output is send to my own error output." |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
269 |
|
10408 | 270 |
^ self |
271 |
readingFrom:commandString |
|
272 |
errorDisposition:#stderr |
|
273 |
inDirectory:nil |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
274 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
275 |
"unix: |
10408 | 276 |
PipeStream readingFrom:'ls -l'. |
2966 | 277 |
" |
278 |
||
279 |
" |
|
10408 | 280 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
281 |
|
10408 | 282 |
p := PipeStream readingFrom:'ls -l'. |
283 |
Transcript showCR:p nextLine. |
|
284 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
285 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
286 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
287 |
" |
10408 | 288 |
|s| |
289 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
|
290 |
(Delay forSeconds:2) wait. |
|
291 |
s shutDown |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
292 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
293 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
294 |
"vms: |
10408 | 295 |
PipeStream readingFrom:'dir'. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
296 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
297 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
298 |
" |
10408 | 299 |
|p| |
300 |
p := PipeStream readingFrom:'dir'. |
|
301 |
Transcript showCR:p nextLine. |
|
302 |
p close |
|
2966 | 303 |
" |
304 |
||
305 |
"msdos: |
|
10408 | 306 |
PipeStream readingFrom:'dir'. |
2966 | 307 |
" |
308 |
" |
|
10408 | 309 |
|p| |
310 |
p := PipeStream readingFrom:'dir'. |
|
311 |
Transcript showCR:p nextLine. |
|
312 |
p close |
|
2966 | 313 |
" |
314 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
315 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 316 |
! |
1 | 317 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
318 |
readingFrom:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
319 |
"similar to #readingFrom, but changes the directory while |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
320 |
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
|
321 |
executed in another directory, to avoid any OS dependencies |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
322 |
in your code. |
4202
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). |
10408 | 324 |
#discard causes stderr to be discarded (/dev/null), |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
325 |
#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
|
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" |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
328 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
329 |
^ self basicNew |
10408 | 330 |
openPipeFor:commandString |
331 |
withMode:#r |
|
332 |
errorDisposition:errorDisposition |
|
333 |
inDirectory:aDirectory |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
334 |
! |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
335 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
336 |
readingFrom:commandString inDirectory:aDirectory |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
337 |
"similar to #readingFrom, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
338 |
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
|
339 |
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
|
340 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
341 |
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
|
342 |
|
10408 | 343 |
^ self |
344 |
readingFrom:commandString |
|
345 |
errorDisposition:#stderr |
|
346 |
inDirectory:aDirectory |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
347 |
|
10408 | 348 |
" UNIX: |
349 |
|p| |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
350 |
|
10408 | 351 |
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'. |
352 |
Transcript showCR:p upToEnd. |
|
353 |
p close |
|
9193 | 354 |
" |
10408 | 355 |
"WINDOOF: |
356 |
|p| |
|
9193 | 357 |
|
10408 | 358 |
p := PipeStream readingFrom:'dir'. |
359 |
Transcript showCR:p upToEnd. |
|
360 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
361 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
362 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
363 |
|
2 | 364 |
writingTo:commandString |
1 | 365 |
"create and return a new pipeStream which can write to the unix command |
366 |
given by command." |
|
367 |
||
10408 | 368 |
^ self |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
369 |
writingTo:commandString errorDisposition:#stderr inDirectory:nil |
1 | 370 |
|
2966 | 371 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
372 |
PipeStream writingTo:'sort' |
2966 | 373 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
374 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
375 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
376 |
writingTo:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
377 |
"similar to #writingTo, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
378 |
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
|
379 |
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
|
380 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
381 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
10408 | 382 |
#discard causes stderr to be discarded (/dev/null), |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
383 |
#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
|
384 |
#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
|
385 |
Nil is treated like #stderr" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
386 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
387 |
^ self basicNew |
10408 | 388 |
openPipeFor:commandString |
389 |
withMode:#w |
|
390 |
errorDisposition:errorDisposition |
|
391 |
inDirectory:aDirectory |
|
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
392 |
! |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
393 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
394 |
writingTo:commandString inDirectory:aDirectory |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
395 |
"create and return a new pipeStream which can write to the unix command |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
396 |
given by commandString. The command is executed in the given directory." |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
397 |
|
10408 | 398 |
^ self |
399 |
writingTo:commandString errorDisposition:#stderr inDirectory:aDirectory |
|
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
400 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
401 |
"unix: |
10408 | 402 |
PipeStream writingTo:'sort' |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
403 |
" |
613 | 404 |
! ! |
405 |
||
2266 | 406 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 407 |
|
613 | 408 |
brokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
409 |
"return the signal used to handle SIGPIPE unix-signals. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
410 |
Since SIGPIPE is asynchronous, we can't decide which smalltalk process |
10408 | 411 |
should handle BrokenPipeSignal. So the system doesn't raise |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
412 |
BrokenPipeSignal for SIGPIPE any longer." |
1 | 413 |
|
613 | 414 |
^ BrokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
415 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
416 |
"Modified: 24.9.1997 / 09:43:23 / stefan" |
1 | 417 |
! ! |
418 |
||
99 | 419 |
!PipeStream methodsFor:'accessing'! |
420 |
||
421 |
commandString |
|
422 |
"return the command string" |
|
423 |
||
424 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
425 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
426 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
427 |
exitStatus |
10343 | 428 |
"return the exitStatus" |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
429 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
430 |
^ exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
431 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
432 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
433 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
434 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
435 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
436 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
437 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
438 |
^ pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
439 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
440 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
99 | 441 |
! ! |
442 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
443 |
!PipeStream methodsFor:'closing'! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
444 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
445 |
shutDown |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
446 |
"close the Stream and terminate the command" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
447 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
448 |
Lobby unregister:self. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
449 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
450 |
"terminate first under windows" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
451 |
OperatingSystem isMSDOSlike ifTrue:[ |
10408 | 452 |
self terminatePipeCommand. |
453 |
self closeFileDescriptor. |
|
454 |
^ self. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
455 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
456 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
457 |
"terminate last under unix" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
458 |
self closeFileDescriptor. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
459 |
self terminatePipeCommand. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
460 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
461 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
462 |
shutDownOutput |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
463 |
"signal to the pipestream's command, that no more data will be sent" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
464 |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
465 |
|fd| |
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
466 |
|
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
467 |
self isOpen ifTrue:[ |
10408 | 468 |
fd := self fileDescriptor. |
469 |
fd notNil ifTrue:[ |
|
470 |
OperatingSystem shutdownBidirectionalPipeOutput:fd. |
|
471 |
]. |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
472 |
]. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
473 |
! ! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
474 |
|
5243 | 475 |
!PipeStream methodsFor:'finalization'! |
476 |
||
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
477 |
finalize |
5243 | 478 |
"redefined to avoid blocking in close." |
479 |
||
480 |
self shutDown |
|
481 |
! ! |
|
482 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
483 |
!PipeStream protectedMethodsFor:'private'! |
1 | 484 |
|
485 |
closeFile |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
486 |
"low level close |
10408 | 487 |
This waits for the command to finish. |
230 | 488 |
Use shutDown for a fast (nonBlocking) close." |
1 | 489 |
|
7027 | 490 |
filePointer notNil ifTrue:[ |
10408 | 491 |
super closeFile. |
492 |
OperatingSystem isMSDOSlike ifTrue:[ |
|
493 |
self terminatePipeCommand. |
|
494 |
]. |
|
3834
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
495 |
|
10408 | 496 |
filePointer := nil. |
497 |
pid notNil ifTrue:[ |
|
498 |
"/ wait for the pipe-command to terminate. |
|
499 |
self waitForPipeCommandWithTimeout:nil. |
|
500 |
]. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
501 |
]. |
3833
3ae90f8da01a
terminate process BEFORE closing the pipe under win32
Claus Gittinger <cg@exept.de>
parents:
3791
diff
changeset
|
502 |
|
3834
93ec07316a98
oops - must nil pid when terminating the process
Claus Gittinger <cg@exept.de>
parents:
3833
diff
changeset
|
503 |
"Modified: / 12.9.1998 / 16:51:04 / cg" |
369 | 504 |
! |
505 |
||
506 |
closeFileDescriptor |
|
10408 | 507 |
"alternative very low level close |
508 |
This closes the underlying OS-fileDescriptor |
|
369 | 509 |
- and will NOT write any buffered data to the stream. |
510 |
You have been warned." |
|
511 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
512 |
|action| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
513 |
|
10408 | 514 |
%{ |
3296 | 515 |
#if !defined(transputer) |
369 | 516 |
OBJ fp; |
517 |
FILE *f; |
|
4616 | 518 |
extern close(); |
6267
10016cc16511
pass name of API/C wrap function
Claus Gittinger <cg@exept.de>
parents:
5880
diff
changeset
|
519 |
int retVal; |
369 | 520 |
|
1133 | 521 |
if ((fp = __INST(filePointer)) != nil) { |
10408 | 522 |
__INST(filePointer) = nil; |
523 |
f = __FILEVal(fp); |
|
524 |
if (@global(FileOpenTrace) == true) { |
|
525 |
console_fprintf(stderr, "close [PipeStream] %x fd=%d\n", f, fileno(f)); |
|
526 |
} |
|
3744 | 527 |
#ifdef WIN32 |
10408 | 528 |
do { |
529 |
__threadErrno = 0; |
|
530 |
retVal = __STX_C_CALL1( "close", (void*)close, (void*)fileno(f) ); |
|
531 |
} while ((retVal < 0) && (__threadErrno == EINTR)); |
|
3744 | 532 |
#else |
10408 | 533 |
__BEGIN_INTERRUPTABLE__ |
534 |
close(fileno(f)); |
|
535 |
__END_INTERRUPTABLE__ |
|
3744 | 536 |
#endif |
369 | 537 |
} |
3296 | 538 |
#endif /* not transputer */ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
539 |
%}. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
540 |
exitAction notNil ifTrue:[ |
10408 | 541 |
action := exitAction. |
542 |
exitAction := nil. |
|
543 |
action value. |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
544 |
] |
8520 | 545 |
! ! |
546 |
||
1 | 547 |
!PipeStream methodsFor:'private'! |
548 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
549 |
exitAction:aBlock |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
550 |
"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
|
551 |
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
|
552 |
(see readingFrom:inDirectory:)" |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
553 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
554 |
exitAction := aBlock |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
555 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
556 |
|
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
557 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition inDirectory:aDirectory |
10408 | 558 |
"open a pipe to the OS command in commandString; |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
559 |
rwMode may be 'r' or 'w' or 'r+'. |
5481 | 560 |
errorDisposition controls where the stdErr output should go, |
561 |
and may be one of #discard, #inline or #stderr (default). |
|
10408 | 562 |
#discard causes stderr to be discarded (/dev/null), |
5481 | 563 |
#inline causes it to be written to smalltalks own stdout and |
564 |
#stderr causes it to be written to smalltalks own stderr. |
|
565 |
Nil is treated like #stderr" |
|
3296 | 566 |
|
567 |
|blocked pipeFdArray execFdArray execFd myFd shellAndArgs |
|
10408 | 568 |
shellPath shellArgs mbx mbxName |
10363
5cf689c14989
Fix race condition in PipeStream.
Stefan Vogel <sv@exept.de>
parents:
10343
diff
changeset
|
569 |
realCmd execDirectory tmpComFile nullOutput resultPid| |
3296 | 570 |
|
571 |
filePointer notNil ifTrue:[ |
|
11083 | 572 |
"the pipe was already open ... |
573 |
this should (can) not happen." |
|
574 |
^ self errorAlreadyOpen |
|
3296 | 575 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
576 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
577 |
rwMode = #r ifTrue:[ |
11083 | 578 |
mode := #readonly. didWrite := false. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
579 |
] ifFalse:[rwMode = #'r+' ifTrue:[ |
11083 | 580 |
mode := #readwrite. didWrite := true. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
581 |
] ifFalse:[ |
11083 | 582 |
mode := #writeonly. didWrite := true. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
583 |
]]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
584 |
|
3296 | 585 |
lastErrorNumber := nil. |
586 |
exitStatus := nil. |
|
587 |
exitSema := Semaphore new name:'pipe exitSema'. |
|
588 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
589 |
realCmd := aCommandString. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
590 |
execDirectory := aDirectory. |
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
591 |
execFdArray := #(0 1 2) copy. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
592 |
|
3791 | 593 |
OperatingSystem isVMSlike ifTrue:[ |
11083 | 594 |
"/ |
595 |
"/ the generated COM-file includes a 'set default' |
|
596 |
"/ |
|
597 |
tmpComFile := OperatingSystem createCOMFileForVMSCommand:aCommandString in:aDirectory. |
|
598 |
realCmd := '@' , tmpComFile osName. |
|
599 |
execDirectory := nil. |
|
4616 | 600 |
|
11083 | 601 |
mbx := OperatingSystem createMailBox. |
602 |
mbx isNil ifTrue:[ |
|
603 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
604 |
tmpComFile delete. |
|
605 |
^ self openError |
|
606 |
]. |
|
607 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
|
3296 | 608 |
|
11083 | 609 |
"/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR. |
610 |
shellPath := ''. |
|
611 |
shellArgs := realCmd. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
612 |
|
11083 | 613 |
rwMode = #r ifTrue:[ |
614 |
"redirect stdout of subprocess to write to mailbox" |
|
615 |
execFdArray at:2 put:mbx. |
|
616 |
] ifFalse:[ |
|
617 |
"redirect stdin of subprocess to read from mailbox" |
|
618 |
execFdArray at:1 put:mbx. |
|
619 |
]. |
|
3296 | 620 |
] ifFalse:[ |
11083 | 621 |
shellAndArgs := OperatingSystem commandAndArgsForOSCommand:realCmd. |
622 |
shellPath := shellAndArgs at:1. |
|
623 |
shellArgs := shellAndArgs at:2. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
624 |
|
11083 | 625 |
mode == #readwrite ifTrue:[ |
626 |
pipeFdArray := OperatingSystem makeBidirectionalPipe. |
|
627 |
pipeFdArray isNil ifTrue:[ |
|
628 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
629 |
^ self openError |
|
630 |
]. |
|
631 |
myFd := pipeFdArray at:1. |
|
632 |
execFd := pipeFdArray at:2. |
|
633 |
execFdArray at:1 put:execFd. |
|
634 |
execFdArray at:2 put:execFd. |
|
635 |
] ifFalse:[ |
|
636 |
pipeFdArray := OperatingSystem makePipe. |
|
637 |
pipeFdArray isNil ifTrue:[ |
|
638 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
639 |
^ self openError |
|
640 |
]. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
641 |
|
11083 | 642 |
mode == #readonly ifTrue:[ |
643 |
"redirect stdout of subprocess to write to pipe" |
|
644 |
myFd := pipeFdArray at:1. |
|
645 |
execFd := pipeFdArray at:2. |
|
646 |
execFdArray at:2 put:execFd. |
|
647 |
] ifFalse:[ |
|
648 |
"redirect stdin of subprocess to read from pipe" |
|
649 |
myFd := pipeFdArray at:2. |
|
650 |
execFd := pipeFdArray at:1. |
|
651 |
execFdArray at:1 put:execFd. |
|
652 |
]. |
|
653 |
]. |
|
3296 | 654 |
]. |
655 |
||
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
656 |
errorDisposition == #discard ifTrue:[ |
11083 | 657 |
nullOutput := Filename nullDevice writeStream. |
658 |
execFdArray at:3 put:nullOutput fileDescriptor |
|
659 |
] ifFalse:[ |
|
660 |
(errorDisposition == #inline or:[errorDisposition == #stdout]) ifTrue:[ |
|
661 |
execFdArray at:3 put:1 |
|
662 |
] ifFalse:[ |
|
663 |
"/ errorDisposition isStream ifTrue:[ |
|
664 |
"/self halt. |
|
665 |
"/ ]. |
|
666 |
]. |
|
667 |
]. |
|
3296 | 668 |
|
669 |
"/ must block here, to avoid races due to early finishing |
|
670 |
"/ subprocesses ... |
|
671 |
||
672 |
blocked := OperatingSystem blockInterrupts. |
|
673 |
||
10363
5cf689c14989
Fix race condition in PipeStream.
Stefan Vogel <sv@exept.de>
parents:
10343
diff
changeset
|
674 |
"beware: pid may change if subprocess is fast" |
10408 | 675 |
pid := resultPid := Processor |
11083 | 676 |
monitor:[ |
677 |
OperatingSystem |
|
678 |
exec:shellPath |
|
679 |
withArguments:shellArgs |
|
680 |
environment:nil |
|
681 |
fileDescriptors:execFdArray |
|
682 |
fork:true |
|
683 |
newPgrp:true |
|
684 |
inDirectory:execDirectory. |
|
685 |
] |
|
686 |
action:[:status | |
|
687 |
status stillAlive ifFalse:[ |
|
688 |
exitStatus := status. |
|
8969
978613904d0b
For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents:
8968
diff
changeset
|
689 |
|
11083 | 690 |
"writing doesn't make sense - there is no reader any longer" |
691 |
mode == #readwrite ifTrue:[ |
|
692 |
"... but allow to read the rest of the command's output" |
|
693 |
self shutDownOutput. |
|
694 |
] ifFalse:[mode == #writeonly ifTrue:[ |
|
695 |
self closeFileDescriptor. |
|
696 |
]]. |
|
10408 | 697 |
|
11083 | 698 |
OperatingSystem closePid:pid. |
699 |
pid := nil. |
|
700 |
exitSema signal. |
|
701 |
]. |
|
702 |
]. |
|
3296 | 703 |
|
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
704 |
"subprocess has been created. |
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
705 |
close unused filedescriptors" |
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
706 |
|
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
707 |
execFd notNil ifTrue:[ |
11083 | 708 |
OperatingSystem closeFd:execFd. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
709 |
]. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
710 |
|
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
711 |
nullOutput notNil ifTrue:[ |
11083 | 712 |
nullOutput close |
3296 | 713 |
]. |
714 |
||
10363
5cf689c14989
Fix race condition in PipeStream.
Stefan Vogel <sv@exept.de>
parents:
10343
diff
changeset
|
715 |
resultPid notNil ifTrue:[ |
11083 | 716 |
"successfull creation of subprocesss" |
717 |
OperatingSystem isVMSlike ifTrue:[ |
|
718 |
"/ |
|
719 |
"/ reopen the mailbox as a file ... |
|
720 |
"/ |
|
721 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
|
722 |
mbxName notNil ifTrue:[ |
|
723 |
super open:mbxName withMode:rwMode. |
|
724 |
exitAction := [tmpComFile delete]. |
|
725 |
]. |
|
726 |
] ifFalse:[ |
|
727 |
self setFileDescriptor:myFd mode:rwMode. |
|
728 |
] |
|
3296 | 729 |
] ifFalse:[ |
11083 | 730 |
"creation of subprocesss failed" |
731 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
732 |
OperatingSystem isVMSlike ifTrue:[ |
|
733 |
OperatingSystem destroyMailBox:mbx. |
|
734 |
tmpComFile delete. |
|
735 |
] ifFalse:[ |
|
736 |
OperatingSystem closeFd:myFd. |
|
737 |
]. |
|
3296 | 738 |
]. |
739 |
||
740 |
blocked ifFalse:[ |
|
11083 | 741 |
OperatingSystem unblockInterrupts |
3296 | 742 |
]. |
743 |
||
10363
5cf689c14989
Fix race condition in PipeStream.
Stefan Vogel <sv@exept.de>
parents:
10343
diff
changeset
|
744 |
(resultPid isNil or:[lastErrorNumber notNil]) ifTrue:[ |
11083 | 745 |
" |
746 |
the pipe open failed for some reason ... |
|
747 |
... this may be either due to an invalid command string, |
|
748 |
or due to the system running out of memory (when forking |
|
749 |
the unix process) |
|
750 |
" |
|
751 |
exitAction value. |
|
752 |
^ self openError |
|
3296 | 753 |
]. |
754 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
755 |
commandString := realCmd. |
5673
58ebbb50cd3a
Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents:
5624
diff
changeset
|
756 |
|
58ebbb50cd3a
Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents:
5624
diff
changeset
|
757 |
"stdio lib does not work with blocking pipes and interrupts |
58ebbb50cd3a
Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents:
5624
diff
changeset
|
758 |
for WIN, Linux, Solaris and probably any other UNIX" |
58ebbb50cd3a
Set to unbuffered, sincestdio does not work with pipes.
Stefan Vogel <sv@exept.de>
parents:
5624
diff
changeset
|
759 |
buffered := false. |
7114 | 760 |
position := ZeroPosition. |
3296 | 761 |
hitEOF := false. |
762 |
binary := false. |
|
763 |
Lobby register:self. |
|
764 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
765 |
"Modified: / 23.4.1996 / 17:05:59 / stefan" |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
766 |
"Modified: / 28.1.1998 / 14:47:34 / md" |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
767 |
"Created: / 19.5.1999 / 12:28:54 / cg" |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
768 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
769 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
770 |
terminatePipeCommand |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
771 |
|tpid| |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
772 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
773 |
(tpid := pid) notNil ifTrue:[ |
10408 | 774 |
OperatingSystem terminateProcessGroup:tpid. |
775 |
OperatingSystem terminateProcess:tpid. |
|
776 |
OperatingSystem closePid:tpid. |
|
777 |
pid := nil. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
778 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
779 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
780 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
781 |
waitForPipeCommandWithTimeout:seconds |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
782 |
"wait for the pipe command to terminate itself. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
783 |
Return true, if a timeout occurred." |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
784 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
785 |
pid notNil ifTrue:[ |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
786 |
[ |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
787 |
pid notNil ifTrue:[ |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
788 |
exitSema waitWithTimeout:seconds. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
789 |
] |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
790 |
] valueUninterruptably |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
791 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
792 |
^ pid notNil |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
793 |
! ! |
613 | 794 |
|
2266 | 795 |
!PipeStream class methodsFor:'documentation'! |
1295 | 796 |
|
797 |
version |
|
17711 | 798 |
^ '$Id: PipeStream.st 10447 2009-06-14 13:09:55Z vranyj1 $' |
1295 | 799 |
! ! |
7027 | 800 |
|
613 | 801 |
PipeStream initialize! |