author | Claus Gittinger <cg@exept.de> |
Tue, 08 May 2018 12:35:12 +0200 | |
changeset 22794 | eda5d0a964e5 |
parent 22696 | 7b933d672ea3 |
child 22974 | f1df0b1303ab |
permissions | -rw-r--r-- |
22696 | 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 |
21210 | 19 |
instanceVariableNames:'commandString osProcess' |
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'. |
22696 | 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 |
21210 | 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: |
21210 | 285 |
PipeStream readingFrom:'ls -l'. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
286 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
287 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
288 |
" |
21210 | 289 |
|p| |
290 |
||
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 |
|
21210 | 296 |
|
297 |
" |
|
298 |
|p| |
|
299 |
||
300 |
p := PipeStream readingFrom:'echo error >&2'. |
|
301 |
Transcript showCR:p nextLine. |
|
302 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
303 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
304 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
305 |
" |
21210 | 306 |
|s| |
307 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
|
308 |
(Delay forSeconds:2) wait. |
|
309 |
s abortAndClose |
|
2966 | 310 |
" |
311 |
||
21210 | 312 |
" |
313 |
|p| |
|
314 |
p := PipeStream readingFrom:'dir'. |
|
315 |
Transcript showCR:p nextLine. |
|
316 |
p close |
|
317 |
" |
|
318 |
||
319 |
"Windows: |
|
320 |
PipeStream readingFrom:'dir'. |
|
2966 | 321 |
" |
322 |
" |
|
21210 | 323 |
|p| |
324 |
p := PipeStream readingFrom:'dir'. |
|
325 |
Transcript showCR:p nextLine. |
|
326 |
p close |
|
2966 | 327 |
" |
328 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
329 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 330 |
! |
1 | 331 |
|
22794 | 332 |
readingFrom:commandString environment:aShellEnvironmentOrNil |
333 |
"create and return a new pipeStream which can read from the unix command |
|
334 |
given by commandString. |
|
335 |
The commands error output is send to my own error output." |
|
336 |
||
337 |
^ self |
|
338 |
readingFrom:commandString |
|
339 |
errorDisposition:#stderr |
|
340 |
environment:aShellEnvironmentOrNil |
|
341 |
inDirectory:nil |
|
342 |
||
343 |
"unix: |
|
344 |
PipeStream readingFrom:'ls -l'. |
|
345 |
" |
|
346 |
||
347 |
" |
|
348 |
|p| |
|
349 |
||
350 |
p := PipeStream readingFrom:'ls -l'. |
|
351 |
Transcript showCR:p nextLine. |
|
352 |
p close |
|
353 |
" |
|
354 |
||
355 |
||
356 |
" |
|
357 |
|p| |
|
358 |
||
359 |
p := PipeStream readingFrom:'echo error >&2'. |
|
360 |
Transcript showCR:p nextLine. |
|
361 |
p close |
|
362 |
" |
|
363 |
||
364 |
" |
|
365 |
|s| |
|
366 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
|
367 |
(Delay forSeconds:2) wait. |
|
368 |
s abortAndClose |
|
369 |
" |
|
370 |
||
371 |
" |
|
372 |
|p| |
|
373 |
p := PipeStream readingFrom:'dir'. |
|
374 |
Transcript showCR:p nextLine. |
|
375 |
p close |
|
376 |
" |
|
377 |
||
378 |
"Windows: |
|
379 |
PipeStream readingFrom:'dir'. |
|
380 |
" |
|
381 |
" |
|
382 |
|p| |
|
383 |
p := PipeStream readingFrom:'dir'. |
|
384 |
Transcript showCR:p nextLine. |
|
385 |
p close |
|
386 |
" |
|
387 |
||
388 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
|
389 |
! |
|
390 |
||
391 |
readingFrom:commandString environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
392 |
"similar to #readingFrom, but changes the directory while |
|
393 |
executing the command. Use this if a command is to be |
|
394 |
executed in another directory, to avoid any OS dependencies |
|
395 |
in your code. |
|
396 |
The commands error output is send to my own error output." |
|
397 |
||
398 |
^ self |
|
399 |
readingFrom:commandString |
|
400 |
errorDisposition:#stderr |
|
401 |
environment:aShellEnvironmentOrNil |
|
402 |
inDirectory:aDirectory |
|
403 |
||
404 |
" UNIX: |
|
405 |
|p| |
|
406 |
||
407 |
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'. |
|
408 |
Transcript showCR:p upToEnd. |
|
409 |
p close |
|
410 |
" |
|
411 |
"WINDOOF: |
|
412 |
|p| |
|
413 |
||
414 |
p := PipeStream readingFrom:'dir'. |
|
415 |
Transcript showCR:p upToEnd. |
|
416 |
p close |
|
417 |
" |
|
418 |
! |
|
419 |
||
420 |
readingFrom:commandString errorDisposition:errorDisposition environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
421 |
"similar to #readingFrom, but changes the directory while |
|
422 |
executing the command. Use this if a command is to be |
|
423 |
executed in another directory, to avoid any OS dependencies |
|
424 |
in your code. |
|
425 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
|
426 |
#discard causes stderr to be discarded (/dev/null), |
|
427 |
#inline causes it to be merged into the PipeStream and |
|
428 |
#stderr causes it to be written to smalltalks own stderr. |
|
429 |
Nil is treated like #stderr" |
|
430 |
||
431 |
^ self basicNew |
|
432 |
openPipeFor:commandString |
|
433 |
withMode:#r |
|
434 |
errorDisposition:errorDisposition |
|
435 |
environment:aShellEnvironmentOrNil |
|
436 |
inDirectory:aDirectory |
|
437 |
||
438 |
||
439 |
" |
|
440 |
|p| |
|
441 |
||
442 |
p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil. |
|
443 |
Transcript showCR:p nextLine. |
|
444 |
p close |
|
445 |
" |
|
446 |
||
447 |
" |
|
448 |
|p| |
|
449 |
||
450 |
p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil. |
|
451 |
Transcript showCR:p nextLine. |
|
452 |
p close |
|
453 |
" |
|
454 |
! |
|
455 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
456 |
readingFrom:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
457 |
"similar to #readingFrom, but changes the directory while |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
458 |
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
|
459 |
executed in another directory, to avoid any OS dependencies |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
460 |
in your code. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
461 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
22794 | 462 |
#discard causes stderr to be discarded (/dev/null), |
463 |
#inline causes it to be merged into the PipeStream and |
|
464 |
#stderr causes it to be written to smalltalks own stderr. |
|
465 |
Nil is treated like #stderr" |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
466 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
467 |
^ self basicNew |
21210 | 468 |
openPipeFor:commandString |
469 |
withMode:#r |
|
470 |
errorDisposition:errorDisposition |
|
471 |
inDirectory:aDirectory |
|
472 |
||
473 |
||
474 |
" |
|
475 |
|p| |
|
476 |
||
477 |
p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil. |
|
478 |
Transcript showCR:p nextLine. |
|
479 |
p close |
|
480 |
" |
|
481 |
||
482 |
" |
|
483 |
|p| |
|
484 |
||
485 |
p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil. |
|
486 |
Transcript showCR:p nextLine. |
|
487 |
p close |
|
488 |
" |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
489 |
! |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
490 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
491 |
readingFrom:commandString inDirectory:aDirectory |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
492 |
"similar to #readingFrom, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
493 |
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
|
494 |
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
|
495 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
496 |
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
|
497 |
|
10408 | 498 |
^ self |
499 |
readingFrom:commandString |
|
500 |
errorDisposition:#stderr |
|
501 |
inDirectory:aDirectory |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
502 |
|
10408 | 503 |
" UNIX: |
504 |
|p| |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
505 |
|
10408 | 506 |
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'. |
507 |
Transcript showCR:p upToEnd. |
|
508 |
p close |
|
9193 | 509 |
" |
10408 | 510 |
"WINDOOF: |
511 |
|p| |
|
9193 | 512 |
|
10408 | 513 |
p := PipeStream readingFrom:'dir'. |
514 |
Transcript showCR:p upToEnd. |
|
515 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
516 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
517 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
518 |
|
2 | 519 |
writingTo:commandString |
1 | 520 |
"create and return a new pipeStream which can write to the unix command |
521 |
given by command." |
|
522 |
||
10408 | 523 |
^ self |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
524 |
writingTo:commandString errorDisposition:#stderr inDirectory:nil |
1 | 525 |
|
2966 | 526 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
527 |
PipeStream writingTo:'sort' |
2966 | 528 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
529 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
530 |
|
22794 | 531 |
writingTo:commandString environment:aShellEnvironmentOrNil |
532 |
"create and return a new pipeStream which can write to the unix command |
|
533 |
given by command." |
|
534 |
||
535 |
^ self |
|
536 |
writingTo:commandString |
|
537 |
errorDisposition:#stderr |
|
538 |
environment:aShellEnvironmentOrNil |
|
539 |
inDirectory:nil |
|
540 |
||
541 |
"unix: |
|
542 |
PipeStream writingTo:'sort' |
|
543 |
" |
|
544 |
! |
|
545 |
||
546 |
writingTo:commandString environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
547 |
"create and return a new pipeStream which can write to the unix command |
|
548 |
given by commandString. The command is executed in the given directory." |
|
549 |
||
550 |
^ self |
|
551 |
writingTo:commandString |
|
552 |
errorDisposition:#stderr |
|
553 |
environment:aShellEnvironmentOrNil |
|
554 |
inDirectory:aDirectory |
|
555 |
||
556 |
"unix: |
|
557 |
PipeStream writingTo:'sort' |
|
558 |
" |
|
559 |
! |
|
560 |
||
561 |
writingTo:commandString errorDisposition:errorDisposition environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
562 |
"similar to #writingTo, but changes the directory while |
|
563 |
executing the command. Use this if a command is to be |
|
564 |
executed in another directory, to avoid any OS dependencies |
|
565 |
in your code. |
|
566 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
|
567 |
#discard causes stderr to be discarded (/dev/null), |
|
568 |
#inline causes it to be written to smalltalks own stdout and |
|
569 |
#stderr causes it to be written to smalltalks own stderr. |
|
570 |
Nil is treated like #stderr" |
|
571 |
||
572 |
^ self basicNew |
|
573 |
openPipeFor:commandString |
|
574 |
withMode:#w |
|
575 |
errorDisposition:errorDisposition |
|
576 |
environment:aShellEnvironmentOrNil |
|
577 |
inDirectory:aDirectory |
|
578 |
! |
|
579 |
||
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
580 |
writingTo:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
581 |
"similar to #writingTo, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
582 |
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
|
583 |
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
|
584 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
585 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
22794 | 586 |
#discard causes stderr to be discarded (/dev/null), |
587 |
#inline causes it to be written to smalltalks own stdout and |
|
588 |
#stderr causes it to be written to smalltalks own stderr. |
|
589 |
Nil is treated like #stderr" |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
590 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
591 |
^ self basicNew |
22794 | 592 |
openPipeFor:commandString |
593 |
withMode:#w |
|
594 |
errorDisposition:errorDisposition |
|
595 |
inDirectory:aDirectory |
|
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
596 |
! |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
597 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
598 |
writingTo:commandString inDirectory:aDirectory |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
599 |
"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
|
600 |
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
|
601 |
|
10408 | 602 |
^ self |
22794 | 603 |
writingTo:commandString |
604 |
errorDisposition:#stderr |
|
605 |
inDirectory:aDirectory |
|
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
606 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
607 |
"unix: |
22794 | 608 |
PipeStream writingTo:'sort' |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
609 |
" |
613 | 610 |
! ! |
611 |
||
2266 | 612 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 613 |
|
613 | 614 |
brokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
615 |
"return the signal used to handle SIGPIPE unix-signals. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
616 |
Since SIGPIPE is asynchronous, we can't decide which smalltalk process |
10408 | 617 |
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
|
618 |
BrokenPipeSignal for SIGPIPE any longer." |
1 | 619 |
|
613 | 620 |
^ BrokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
621 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
622 |
"Modified: 24.9.1997 / 09:43:23 / stefan" |
1 | 623 |
! ! |
624 |
||
16579 | 625 |
!PipeStream class methodsFor:'utilities'! |
626 |
||
627 |
outputFromCommand:aCommand |
|
628 |
"open a pipe reading from aCommand and return the complete output as a string. |
|
18638 | 629 |
If the command cannot be executed, return nil. |
630 |
The command's current directory will be the smalltalk current directory." |
|
631 |
||
632 |
^ self outputFromCommand:aCommand inDirectory:nil |
|
633 |
||
634 |
" |
|
635 |
PipeStream outputFromCommand:'ls -l' |
|
636 |
" |
|
637 |
! |
|
638 |
||
639 |
outputFromCommand:aCommand inDirectory:aDirectoryOrNil |
|
640 |
"open a pipe reading from aCommand and return the complete output as a string. |
|
641 |
If the command cannot be executed, return nil. |
|
642 |
The current directory of the command will be aDirectoryOrNil or the smalltalk's current directory (if nil)" |
|
16579 | 643 |
|
644 |
|p cmdOutput| |
|
645 |
||
18638 | 646 |
p := self readingFrom:aCommand inDirectory:aDirectoryOrNil. |
16579 | 647 |
p isNil ifTrue:[^ nil]. |
648 |
||
649 |
[ |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
650 |
cmdOutput := p contentsAsString. |
16579 | 651 |
] ensure:[ |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
652 |
p close. |
16579 | 653 |
]. |
654 |
^ cmdOutput |
|
655 |
||
656 |
" |
|
18638 | 657 |
PipeStream outputFromCommand:'ls -l' inDirectory:nil |
658 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/' |
|
659 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/etc' |
|
16579 | 660 |
" |
661 |
! ! |
|
662 |
||
99 | 663 |
!PipeStream methodsFor:'accessing'! |
664 |
||
665 |
commandString |
|
666 |
"return the command string" |
|
667 |
||
668 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
669 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
670 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
671 |
exitStatus |
10343 | 672 |
"return the exitStatus" |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
673 |
|
21210 | 674 |
osProcess isNil ifTrue:[ |
675 |
^ nil. |
|
676 |
]. |
|
677 |
^ osProcess exitStatus. |
|
678 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
679 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
680 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
681 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
682 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
683 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
684 |
|
21210 | 685 |
osProcess isNil ifTrue:[ |
686 |
^ nil. |
|
687 |
]. |
|
688 |
^ osProcess pid. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
689 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
690 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
99 | 691 |
! ! |
692 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
693 |
!PipeStream methodsFor:'closing'! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
694 |
|
20681 | 695 |
abortAndClose |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
696 |
"close the Stream and terminate the command" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
697 |
|
20031 | 698 |
self unregisterForFinalization. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
699 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
700 |
"terminate first under windows" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
701 |
OperatingSystem isMSDOSlike ifTrue:[ |
20031 | 702 |
self terminatePipeCommand. |
21228 | 703 |
self closeFile. |
20031 | 704 |
^ self. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
705 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
706 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
707 |
"terminate last under unix" |
21228 | 708 |
self closeFile. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
709 |
self terminatePipeCommand. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
710 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
711 |
|
20681 | 712 |
close |
713 |
"low level close |
|
714 |
This waits for the command to finish. |
|
715 |
Use abortAndClose for a fast (nonBlocking) close." |
|
716 |
||
22696 | 717 |
self isOpen ifTrue:[ |
20681 | 718 |
super close. |
21210 | 719 |
"/ wait for the pipe-command to terminate. |
720 |
self waitForPipeCommandWithTimeout:nil. |
|
20681 | 721 |
]. |
722 |
||
22696 | 723 |
"Modified: / 12-09-1998 / 16:51:04 / cg" |
724 |
"Modified: / 23-04-2018 / 18:25:04 / stefan" |
|
20681 | 725 |
! |
726 |
||
727 |
shutDown |
|
728 |
<resource: #obsolete> |
|
729 |
"this is a historic leftover kept for backward compatibility. |
|
20970 | 730 |
The name collides with the same name in Socket, which does |
20681 | 731 |
not hard terminate the connection." |
732 |
||
733 |
self abortAndClose. |
|
734 |
! |
|
735 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
736 |
shutDownOutput |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
737 |
"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
|
738 |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
739 |
|fd| |
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
740 |
|
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
741 |
self isOpen ifTrue:[ |
10408 | 742 |
fd := self fileDescriptor. |
743 |
fd notNil ifTrue:[ |
|
744 |
OperatingSystem shutdownBidirectionalPipeOutput:fd. |
|
745 |
]. |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
746 |
]. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
747 |
! ! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
748 |
|
5243 | 749 |
!PipeStream methodsFor:'finalization'! |
750 |
||
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
751 |
finalize |
5243 | 752 |
"redefined to avoid blocking in close." |
753 |
||
20681 | 754 |
self abortAndClose |
18785 | 755 |
! ! |
756 |
||
757 |
!PipeStream methodsFor:'private'! |
|
369 | 758 |
|
22794 | 759 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition |
760 |
environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
761 |
||
10408 | 762 |
"open a pipe to the OS command in commandString; |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
763 |
rwMode may be 'r' or 'w' or 'r+'. |
22794 | 764 |
|
5481 | 765 |
errorDisposition controls where the stdErr output should go, |
766 |
and may be one of #discard, #inline or #stderr (default). |
|
22794 | 767 |
#discard causes stderr to be discarded (/dev/null), |
768 |
#inline causes it to be written to smalltalks own stdout and |
|
769 |
#stderr causes it to be written to smalltalks own stderr. |
|
770 |
Nil is treated like #stderr." |
|
3296 | 771 |
|
21210 | 772 |
|pipeArray remotePipeEnd nullOutput errorNumber myPipeEnd result| |
3296 | 773 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11083
diff
changeset
|
774 |
handle notNil ifTrue:[ |
20099 | 775 |
"the pipe was already open ... |
776 |
this should (can) not happen." |
|
777 |
^ self errorAlreadyOpen |
|
3296 | 778 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
779 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
780 |
rwMode = #r ifTrue:[ |
20099 | 781 |
mode := #readonly. didWrite := false. |
782 |
position := 0. "only reading - can keep track of position" |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
783 |
] ifFalse:[rwMode = #'r+' ifTrue:[ |
20099 | 784 |
mode := #readwrite. didWrite := true. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
785 |
] ifFalse:[ |
20099 | 786 |
mode := #writeonly. didWrite := true. |
787 |
position := 0. "only writing - can keep track of position" |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
788 |
]]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
789 |
|
3296 | 790 |
lastErrorNumber := nil. |
21210 | 791 |
commandString := aCommandString. |
792 |
"stdio lib does not work with blocking pipes and interrupts |
|
793 |
for WIN, Linux, Solaris and probably any other UNIX" |
|
794 |
buffered := false. |
|
795 |
hitEOF := false. |
|
796 |
binary := false. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
797 |
|
21791 | 798 |
osProcess := OSProcess new |
22440 | 799 |
command:aCommandString directory:aDirectory. |
22794 | 800 |
aShellEnvironmentOrNil notNil ifTrue:[ |
801 |
osProcess environment:aShellEnvironmentOrNil |
|
802 |
]. |
|
4616 | 803 |
|
21210 | 804 |
mode == #readwrite ifTrue:[ |
21211 | 805 |
pipeArray := self class makeBidirectionalPipe. |
21210 | 806 |
pipeArray isNil ifTrue:[ |
20099 | 807 |
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber. |
808 |
^ self openError:errorNumber. |
|
809 |
]. |
|
21210 | 810 |
myPipeEnd := pipeArray at:1. |
811 |
remotePipeEnd := pipeArray at:2. |
|
812 |
osProcess inStream:remotePipeEnd. |
|
813 |
osProcess outStream:remotePipeEnd. |
|
814 |
] ifFalse:[ |
|
21211 | 815 |
pipeArray := self class makePipe. |
21210 | 816 |
pipeArray isNil ifTrue:[ |
817 |
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber. |
|
818 |
^ self openError:errorNumber. |
|
20099 | 819 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
820 |
|
21210 | 821 |
mode == #readonly ifTrue:[ |
822 |
"redirect stdout of subprocess to write to pipe" |
|
823 |
myPipeEnd := pipeArray at:1. |
|
824 |
remotePipeEnd := pipeArray at:2. |
|
825 |
osProcess outStream:remotePipeEnd. |
|
20099 | 826 |
] ifFalse:[ |
21210 | 827 |
"redirect stdin of subprocess to read from pipe" |
828 |
myPipeEnd := pipeArray at:2. |
|
829 |
remotePipeEnd := pipeArray at:1. |
|
830 |
osProcess inStream:remotePipeEnd. |
|
20099 | 831 |
]. |
3296 | 832 |
]. |
833 |
||
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
834 |
errorDisposition == #discard ifTrue:[ |
20099 | 835 |
nullOutput := Filename nullDevice writeStream. |
21210 | 836 |
osProcess errorStream:nullOutput. |
837 |
] ifFalse:[(errorDisposition == #inline or:[errorDisposition == #stdout]) ifTrue:[ |
|
838 |
osProcess errorStream:osProcess outStream. |
|
21211 | 839 |
] ifFalse:[(errorDisposition == #stderr or:[errorDisposition isNil]) ifTrue:[ |
21210 | 840 |
osProcess errorStream:Stderr. |
841 |
] ifFalse:[errorDisposition isStream ifTrue:[ |
|
842 |
osProcess errorStream:errorDisposition. |
|
843 |
]]]]. |
|
3296 | 844 |
|
21228 | 845 |
mode ~~ #readonly ifTrue:[ |
846 |
osProcess terminateActionBlock:[ |
|
847 |
"writing doesn't make sense - there is no reader any longer" |
|
848 |
mode == #readwrite ifTrue:[ |
|
849 |
"... but allow to read the rest of the command's output" |
|
850 |
self shutDownOutput. |
|
851 |
] ifFalse:[mode == #writeonly ifTrue:[ |
|
852 |
self closeFile. |
|
853 |
]]. |
|
854 |
]. |
|
855 |
]. |
|
8969
978613904d0b
For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents:
8968
diff
changeset
|
856 |
|
21210 | 857 |
result := osProcess startProcess. |
3296 | 858 |
|
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
859 |
"subprocess has been created. |
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
860 |
close unused filedescriptors" |
21210 | 861 |
remotePipeEnd notNil ifTrue:[ |
862 |
remotePipeEnd close. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
863 |
]. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
864 |
nullOutput notNil ifTrue:[ |
20099 | 865 |
nullOutput close |
3296 | 866 |
]. |
867 |
||
21210 | 868 |
result ifTrue:[ |
21791 | 869 |
"successful creation of subprocess" |
21231 | 870 |
handle := myPipeEnd handle. |
871 |
handleType := myPipeEnd handleType. |
|
22507 | 872 |
myPipeEnd unregisterForFinalization. "make sure filedescriptor is not closed by finalizer" |
21210 | 873 |
myPipeEnd := nil. |
3296 | 874 |
] ifFalse:[ |
21210 | 875 |
"the pipe open failed for some reason ... |
20099 | 876 |
... this may be either due to an invalid command string, |
877 |
or due to the system running out of memory (when forking |
|
21210 | 878 |
the unix process)" |
879 |
lastErrorNumber := OperatingSystem lastErrorNumber. |
|
880 |
myPipeEnd close. |
|
20099 | 881 |
^ self openError:lastErrorNumber. |
3296 | 882 |
]. |
883 |
||
18782 | 884 |
self registerForFinalization. |
3296 | 885 |
|
21791 | 886 |
"Modified: / 23-04-1996 / 17:05:59 / stefan" |
887 |
"Modified: / 28-01-1998 / 14:47:34 / md" |
|
888 |
"Created: / 19-05-1999 / 12:28:54 / cg" |
|
22507 | 889 |
"Modified (comment): / 25-01-2018 / 19:40:27 / mawalch" |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
890 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
891 |
|
22794 | 892 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition inDirectory:aDirectory |
893 |
"open a pipe to the OS command in commandString; |
|
894 |
rwMode may be 'r' or 'w' or 'r+'. |
|
895 |
errorDisposition controls where the stdErr output should go, |
|
896 |
and may be one of #discard, #inline or #stderr (default). |
|
897 |
#discard causes stderr to be discarded (/dev/null), |
|
898 |
#inline causes it to be written to smalltalks own stdout and |
|
899 |
#stderr causes it to be written to smalltalks own stderr. |
|
900 |
Nil is treated like #stderr" |
|
901 |
||
902 |
^ self |
|
903 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition |
|
904 |
environment:nil inDirectory:aDirectory |
|
905 |
! |
|
906 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
907 |
terminatePipeCommand |
21210 | 908 |
osProcess notNil ifTrue:[ |
909 |
osProcess terminateGroup. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
910 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
911 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
912 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
913 |
waitForPipeCommandWithTimeout:seconds |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
914 |
"wait for the pipe command to terminate itself. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
915 |
Return true, if a timeout occurred." |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
916 |
|
21210 | 917 |
osProcess notNil ifTrue:[ |
918 |
^ osProcess finishSema waitWithTimeout:seconds. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
919 |
]. |
21210 | 920 |
^ false |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
921 |
! ! |
613 | 922 |
|
2266 | 923 |
!PipeStream class methodsFor:'documentation'! |
1295 | 924 |
|
925 |
version |
|
18638 | 926 |
^ '$Header$' |
12712
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
927 |
! |
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
928 |
|
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
929 |
version_CVS |
18638 | 930 |
^ '$Header$' |
1295 | 931 |
! ! |
7027 | 932 |
|
15359 | 933 |
|
613 | 934 |
PipeStream initialize! |