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