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