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