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