author | Stefan Vogel <sv@exept.de> |
Tue, 22 May 2018 10:48:26 +0200 | |
changeset 22982 | c91015b9e2e3 |
parent 22980 | 8ea9d4f9983a |
child 22991 | 761301acd1aa |
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 |
||
22982 | 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 |
|
22982 | 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 |
|
22982 | 185 |
PipeStream bidirectionalFor:'sed -u -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:] |
22982 | 191 |
Claus Gittinger |
2966 | 192 |
|
193 |
[see also:] |
|
22982 | 194 |
ExternalStream FileStream Socket |
195 |
OperatingSystem |
|
613 | 196 |
" |
22975 | 197 |
! |
198 |
||
199 |
examples |
|
200 |
" |
|
201 |
reading: |
|
202 |
[exBegin] |
|
203 |
|p output| |
|
204 |
||
205 |
p := PipeStream readingFrom:'ls -l'. |
|
206 |
output := p upToEnd. |
|
207 |
p close. |
|
208 |
Transcript showCR:output |
|
209 |
[exEnd] |
|
210 |
||
211 |
bidirectional: |
|
212 |
[exBegin] |
|
213 |
|p| |
|
214 |
||
22982 | 215 |
p := PipeStream bidirectionalFor:'sed -u -e s/Hello/Greetings/'. |
22975 | 216 |
p nextPutLine:'bla'; flush. |
217 |
Transcript showCR:p nextLine. |
|
218 |
p nextPutLine:'foo Hello'. |
|
219 |
Transcript showCR:p nextLine. |
|
220 |
p nextPutLine:'bar'. |
|
221 |
Transcript showCR:p nextLine. |
|
222 |
p close. |
|
223 |
[exEnd] |
|
224 |
||
225 |
error output is on my stderr: |
|
226 |
[exBegin] |
|
227 |
|p| |
|
228 |
||
229 |
p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2'. |
|
230 |
Transcript showCR:p upToEnd. |
|
231 |
p close. |
|
232 |
[exEnd] |
|
233 |
||
234 |
error output is included: |
|
235 |
[exBegin] |
|
236 |
|p| |
|
237 |
||
238 |
p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2' errorDisposition:#stdout. |
|
239 |
Transcript showCR:p upToEnd. |
|
240 |
p close. |
|
241 |
[exEnd] |
|
242 |
||
243 |
error output is separate: |
|
244 |
[exBegin] |
|
245 |
|p errStream| |
|
246 |
||
247 |
errStream := '' writeStream. |
|
248 |
p := PipeStream readingFrom:'echo hello1; echo error>&2; echo hello2' errorDisposition:errStream. |
|
249 |
Transcript showCR:'output:'; showCR:p upToEnd; cr. |
|
250 |
p close. |
|
251 |
Transcript showCR:'error:'; showCR:errStream contents; cr. |
|
252 |
[exEnd] |
|
253 |
" |
|
613 | 254 |
! ! |
255 |
||
2266 | 256 |
!PipeStream class methodsFor:'initialization'! |
2 | 257 |
|
258 |
initialize |
|
259 |
"setup the signal" |
|
260 |
||
57 | 261 |
BrokenPipeSignal isNil ifTrue:[ |
10408 | 262 |
BrokenPipeSignal := WriteError newSignalMayProceed:true. |
263 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal. |
|
264 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'. |
|
57 | 265 |
] |
2 | 266 |
! ! |
267 |
||
2266 | 268 |
!PipeStream class methodsFor:'instance creation'! |
2 | 269 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
270 |
bidirectionalFor:commandString |
10408 | 271 |
"create and return a new bidirectonal pipeStream which can both be written to |
272 |
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
|
273 |
The commands error output is send to my own error output." |
2 | 274 |
|
10408 | 275 |
^ self |
22982 | 276 |
bidirectionalFor:commandString |
277 |
errorDisposition:#stderr |
|
278 |
inDirectory:nil |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
279 |
|
2966 | 280 |
" |
22982 | 281 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
282 |
|
22982 | 283 |
p := PipeStream bidirectionalFor:'cat -u'. |
284 |
p nextPutAll:'Wer ist der Bürgermeister von Wesel'; cr. |
|
285 |
Transcript showCR:p nextLine. |
|
286 |
p close |
|
2966 | 287 |
" |
288 |
||
289 |
" |
|
22982 | 290 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
291 |
|
22982 | 292 |
p := PipeStream bidirectionalFor:'sed -u -e ''s/Hello/Greetings/'''. |
293 |
p nextPutAll:'Hello world'; cr. |
|
294 |
p shutDownOutput. |
|
295 |
Transcript showCR:p nextLine. |
|
296 |
p close |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
297 |
" |
9313 | 298 |
|
299 |
" |
|
22982 | 300 |
|p| |
9313 | 301 |
|
22982 | 302 |
p := PipeStream bidirectionalFor:'wc'. |
303 |
p nextPutAll:'Hello world'; cr. |
|
304 |
p shutDownOutput. |
|
305 |
Transcript showCR:p nextLine. |
|
306 |
p close |
|
9313 | 307 |
" |
22982 | 308 |
|
309 |
"Modified (comment): / 22-05-2018 / 10:46:32 / Stefan Vogel" |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
310 |
! |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
311 |
|
22974 | 312 |
bidirectionalFor:commandString errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectory |
10408 | 313 |
"create and return a new bidirectonal pipeStream which can both be written to |
314 |
and read from the unix command given by commandString. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
315 |
The directory will be changed to aDirectory while |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
316 |
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
|
317 |
executed in another directory, to avoid any OS dependencies |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
318 |
in your code. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
319 |
|
22974 | 320 |
errorDisposition may be a stream or one of #discard, #inline or #stderr (default). |
10408 | 321 |
#discard causes stderr to be discarded (/dev/null), |
22974 | 322 |
#inline causes it to be written to smalltalk's own stdout |
323 |
#stderr causes it to be written to smalltalk's own stderr. |
|
324 |
a stream causes stderr to be sent to that stream (internal or external) |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
325 |
Nil is treated like #stderr" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
326 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
327 |
^ self basicNew |
22974 | 328 |
openPipeFor:commandString |
329 |
withMode:#'r+' |
|
330 |
errorDisposition:errorDispositionSymbolOrStream |
|
331 |
inDirectory:aDirectory |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
332 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
333 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
334 |
readingFrom:commandString |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
335 |
"create and return a new pipeStream which can read from the unix command |
10408 | 336 |
given by commandString. |
22974 | 337 |
The command's error output is send to my own error output." |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
338 |
|
10408 | 339 |
^ self |
21210 | 340 |
readingFrom:commandString |
341 |
errorDisposition:#stderr |
|
342 |
inDirectory:nil |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
343 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
344 |
"unix: |
21210 | 345 |
PipeStream readingFrom:'ls -l'. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
346 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
347 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
348 |
" |
21210 | 349 |
|p| |
350 |
||
351 |
p := PipeStream readingFrom:'ls -l'. |
|
352 |
Transcript showCR:p nextLine. |
|
353 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
354 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
355 |
|
21210 | 356 |
|
357 |
" |
|
358 |
|p| |
|
359 |
||
360 |
p := PipeStream readingFrom:'echo error >&2'. |
|
361 |
Transcript showCR:p nextLine. |
|
362 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
363 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
364 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
365 |
" |
21210 | 366 |
|s| |
367 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
|
368 |
(Delay forSeconds:2) wait. |
|
369 |
s abortAndClose |
|
2966 | 370 |
" |
371 |
||
21210 | 372 |
" |
373 |
|p| |
|
374 |
p := PipeStream readingFrom:'dir'. |
|
375 |
Transcript showCR:p nextLine. |
|
376 |
p close |
|
377 |
" |
|
378 |
||
379 |
"Windows: |
|
380 |
PipeStream readingFrom:'dir'. |
|
2966 | 381 |
" |
382 |
" |
|
21210 | 383 |
|p| |
384 |
p := PipeStream readingFrom:'dir'. |
|
385 |
Transcript showCR:p nextLine. |
|
386 |
p close |
|
2966 | 387 |
" |
388 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
389 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 390 |
! |
1 | 391 |
|
22794 | 392 |
readingFrom:commandString environment:aShellEnvironmentOrNil |
393 |
"create and return a new pipeStream which can read from the unix command |
|
394 |
given by commandString. |
|
22974 | 395 |
The command's error output is send to my own error output." |
22794 | 396 |
|
397 |
^ self |
|
398 |
readingFrom:commandString |
|
399 |
errorDisposition:#stderr |
|
400 |
environment:aShellEnvironmentOrNil |
|
401 |
inDirectory:nil |
|
402 |
||
403 |
"unix: |
|
404 |
PipeStream readingFrom:'ls -l'. |
|
405 |
" |
|
406 |
||
407 |
" |
|
408 |
|p| |
|
409 |
||
410 |
p := PipeStream readingFrom:'ls -l'. |
|
411 |
Transcript showCR:p nextLine. |
|
412 |
p close |
|
413 |
" |
|
414 |
||
415 |
||
416 |
" |
|
417 |
|p| |
|
418 |
||
419 |
p := PipeStream readingFrom:'echo error >&2'. |
|
420 |
Transcript showCR:p nextLine. |
|
421 |
p close |
|
422 |
" |
|
423 |
||
424 |
" |
|
425 |
|s| |
|
426 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
|
427 |
(Delay forSeconds:2) wait. |
|
428 |
s abortAndClose |
|
429 |
" |
|
430 |
||
431 |
" |
|
432 |
|p| |
|
433 |
p := PipeStream readingFrom:'dir'. |
|
434 |
Transcript showCR:p nextLine. |
|
435 |
p close |
|
436 |
" |
|
437 |
||
438 |
"Windows: |
|
439 |
PipeStream readingFrom:'dir'. |
|
440 |
" |
|
441 |
" |
|
442 |
|p| |
|
443 |
p := PipeStream readingFrom:'dir'. |
|
444 |
Transcript showCR:p nextLine. |
|
445 |
p close |
|
446 |
" |
|
447 |
||
448 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
|
449 |
! |
|
450 |
||
451 |
readingFrom:commandString environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
452 |
"similar to #readingFrom, but changes the directory while |
|
453 |
executing the command. Use this if a command is to be |
|
454 |
executed in another directory, to avoid any OS dependencies |
|
455 |
in your code. |
|
22974 | 456 |
The command's error output is send to my own error output." |
22794 | 457 |
|
458 |
^ self |
|
459 |
readingFrom:commandString |
|
460 |
errorDisposition:#stderr |
|
461 |
environment:aShellEnvironmentOrNil |
|
462 |
inDirectory:aDirectory |
|
463 |
||
464 |
" UNIX: |
|
465 |
|p| |
|
466 |
||
467 |
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'. |
|
468 |
Transcript showCR:p upToEnd. |
|
469 |
p close |
|
470 |
" |
|
471 |
"WINDOOF: |
|
472 |
|p| |
|
473 |
||
474 |
p := PipeStream readingFrom:'dir'. |
|
475 |
Transcript showCR:p upToEnd. |
|
476 |
p close |
|
477 |
" |
|
478 |
! |
|
479 |
||
22975 | 480 |
readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream |
481 |
"create and return a new pipeStream which can read from the unix command |
|
482 |
given by commandString. |
|
483 |
errorDisposition may be a stream or one of #discard, #inline or #stderr (default). |
|
484 |
#discard causes stderr to be discarded (/dev/null), |
|
485 |
#inline causes it to be merged into the PipeStream and |
|
486 |
#stderr causes it to be written to smalltalk's own stderr. |
|
487 |
a stream causes stderr to be sent to that stream (internal or external) |
|
488 |
Nil is treated like #stderr" |
|
489 |
||
490 |
^ self |
|
491 |
readingFrom:commandString |
|
492 |
errorDisposition:errorDispositionSymbolOrStream |
|
493 |
inDirectory:nil |
|
494 |
||
495 |
"unix: |
|
496 |
PipeStream readingFrom:'ls -l'. |
|
497 |
" |
|
498 |
||
499 |
" |
|
500 |
|p| |
|
501 |
||
502 |
p := PipeStream readingFrom:'ls -l'. |
|
503 |
Transcript showCR:p nextLine. |
|
504 |
p close |
|
505 |
" |
|
506 |
||
507 |
||
508 |
" |
|
509 |
|p| |
|
510 |
||
511 |
p := PipeStream readingFrom:'echo error >&2'. |
|
512 |
Transcript showCR:p nextLine. |
|
513 |
p close |
|
514 |
" |
|
515 |
||
516 |
" |
|
517 |
|s| |
|
518 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
|
519 |
(Delay forSeconds:2) wait. |
|
520 |
s abortAndClose |
|
521 |
" |
|
522 |
||
523 |
" |
|
524 |
|p| |
|
525 |
p := PipeStream readingFrom:'dir'. |
|
526 |
Transcript showCR:p nextLine. |
|
527 |
p close |
|
528 |
" |
|
529 |
||
530 |
"Windows: |
|
531 |
PipeStream readingFrom:'dir'. |
|
532 |
" |
|
533 |
" |
|
534 |
|p| |
|
535 |
p := PipeStream readingFrom:'dir'. |
|
536 |
Transcript showCR:p nextLine. |
|
537 |
p close |
|
538 |
" |
|
539 |
||
540 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
|
541 |
! |
|
542 |
||
22974 | 543 |
readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream environment:aShellEnvironmentOrNil inDirectory:aDirectory |
22794 | 544 |
"similar to #readingFrom, but changes the directory while |
545 |
executing the command. Use this if a command is to be |
|
546 |
executed in another directory, to avoid any OS dependencies |
|
547 |
in your code. |
|
22974 | 548 |
errorDisposition may be a stream or one of #discard, #inline or #stderr (default). |
22794 | 549 |
#discard causes stderr to be discarded (/dev/null), |
550 |
#inline causes it to be merged into the PipeStream and |
|
22974 | 551 |
#stderr causes it to be written to smalltalk's own stderr. |
552 |
a stream causes stderr to be sent to that stream (internal or external) |
|
22794 | 553 |
Nil is treated like #stderr" |
554 |
||
555 |
^ self basicNew |
|
556 |
openPipeFor:commandString |
|
557 |
withMode:#r |
|
22974 | 558 |
errorDisposition:errorDispositionSymbolOrStream |
22794 | 559 |
environment:aShellEnvironmentOrNil |
560 |
inDirectory:aDirectory |
|
561 |
||
562 |
||
563 |
" |
|
564 |
|p| |
|
565 |
||
566 |
p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil. |
|
567 |
Transcript showCR:p nextLine. |
|
568 |
p close |
|
569 |
" |
|
570 |
||
571 |
" |
|
572 |
|p| |
|
573 |
||
574 |
p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil. |
|
575 |
Transcript showCR:p nextLine. |
|
576 |
p close |
|
577 |
" |
|
578 |
! |
|
579 |
||
22974 | 580 |
readingFrom:commandString errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectory |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
581 |
"similar to #readingFrom, but changes the directory while |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
582 |
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
|
583 |
executed in another directory, to avoid any OS dependencies |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
584 |
in your code. |
22974 | 585 |
errorDisposition may be a stream or one of #discard, #inline or #stderr (default). |
22794 | 586 |
#discard causes stderr to be discarded (/dev/null), |
587 |
#inline causes it to be merged into the PipeStream and |
|
22974 | 588 |
#stderr causes it to be written to smalltalk's own stderr. |
589 |
a stream causes stderr to be sent to that stream (internal or external) |
|
22794 | 590 |
Nil is treated like #stderr" |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
591 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
592 |
^ self basicNew |
21210 | 593 |
openPipeFor:commandString |
594 |
withMode:#r |
|
22974 | 595 |
errorDisposition:errorDispositionSymbolOrStream |
21210 | 596 |
inDirectory:aDirectory |
597 |
||
598 |
||
599 |
" |
|
600 |
|p| |
|
601 |
||
602 |
p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil. |
|
603 |
Transcript showCR:p nextLine. |
|
604 |
p close |
|
605 |
" |
|
606 |
||
607 |
" |
|
608 |
|p| |
|
609 |
||
610 |
p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil. |
|
611 |
Transcript showCR:p nextLine. |
|
612 |
p close |
|
613 |
" |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
614 |
! |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
615 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
616 |
readingFrom:commandString inDirectory:aDirectory |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
617 |
"similar to #readingFrom, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
618 |
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
|
619 |
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
|
620 |
in your code. |
22974 | 621 |
The command's 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
|
622 |
|
10408 | 623 |
^ self |
22974 | 624 |
readingFrom:commandString |
625 |
errorDisposition:#stderr |
|
626 |
inDirectory:aDirectory |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
627 |
|
10408 | 628 |
" UNIX: |
22974 | 629 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
630 |
|
22974 | 631 |
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'. |
632 |
Transcript showCR:p upToEnd. |
|
633 |
p close |
|
9193 | 634 |
" |
10408 | 635 |
"WINDOOF: |
22974 | 636 |
|p| |
9193 | 637 |
|
22974 | 638 |
p := PipeStream readingFrom:'dir'. |
639 |
Transcript showCR:p upToEnd. |
|
640 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
641 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
642 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
643 |
|
2 | 644 |
writingTo:commandString |
1 | 645 |
"create and return a new pipeStream which can write to the unix command |
646 |
given by command." |
|
647 |
||
10408 | 648 |
^ self |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
649 |
writingTo:commandString errorDisposition:#stderr inDirectory:nil |
1 | 650 |
|
2966 | 651 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
652 |
PipeStream writingTo:'sort' |
2966 | 653 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
654 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
655 |
|
22794 | 656 |
writingTo:commandString environment:aShellEnvironmentOrNil |
657 |
"create and return a new pipeStream which can write to the unix command |
|
658 |
given by command." |
|
659 |
||
660 |
^ self |
|
661 |
writingTo:commandString |
|
662 |
errorDisposition:#stderr |
|
663 |
environment:aShellEnvironmentOrNil |
|
664 |
inDirectory:nil |
|
665 |
||
666 |
"unix: |
|
667 |
PipeStream writingTo:'sort' |
|
668 |
" |
|
669 |
! |
|
670 |
||
671 |
writingTo:commandString environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
672 |
"create and return a new pipeStream which can write to the unix command |
|
673 |
given by commandString. The command is executed in the given directory." |
|
674 |
||
675 |
^ self |
|
676 |
writingTo:commandString |
|
677 |
errorDisposition:#stderr |
|
678 |
environment:aShellEnvironmentOrNil |
|
679 |
inDirectory:aDirectory |
|
680 |
||
681 |
"unix: |
|
682 |
PipeStream writingTo:'sort' |
|
683 |
" |
|
684 |
! |
|
685 |
||
22974 | 686 |
writingTo:commandString errorDisposition:errorDispositionSymbolOrStream environment:aShellEnvironmentOrNil inDirectory:aDirectory |
22794 | 687 |
"similar to #writingTo, but changes the directory while |
688 |
executing the command. Use this if a command is to be |
|
689 |
executed in another directory, to avoid any OS dependencies |
|
690 |
in your code. |
|
22974 | 691 |
errorDisposition may be a stream or one of #discard, #inline or #stderr (default). |
22794 | 692 |
#discard causes stderr to be discarded (/dev/null), |
22974 | 693 |
#inline causes it to be written to smalltalk's own stdout |
694 |
#stderr causes it to be written to smalltalk's own stderr. |
|
22794 | 695 |
Nil is treated like #stderr" |
696 |
||
697 |
^ self basicNew |
|
698 |
openPipeFor:commandString |
|
699 |
withMode:#w |
|
22974 | 700 |
errorDisposition:errorDispositionSymbolOrStream |
22794 | 701 |
environment:aShellEnvironmentOrNil |
702 |
inDirectory:aDirectory |
|
703 |
! |
|
704 |
||
22974 | 705 |
writingTo:commandString errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectory |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
706 |
"similar to #writingTo, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
707 |
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
|
708 |
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
|
709 |
in your code. |
22974 | 710 |
errorDisposition may be a stream or one of #discard, #inline or #stderr (default). |
22794 | 711 |
#discard causes stderr to be discarded (/dev/null), |
22974 | 712 |
#inline causes it to be written to smalltalk's own stdout |
713 |
#stderr causes it to be written to smalltalk's own stderr. |
|
22794 | 714 |
Nil is treated like #stderr" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
715 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
716 |
^ self basicNew |
22794 | 717 |
openPipeFor:commandString |
718 |
withMode:#w |
|
22974 | 719 |
errorDisposition:errorDispositionSymbolOrStream |
22794 | 720 |
inDirectory:aDirectory |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
721 |
! |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
722 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
723 |
writingTo:commandString inDirectory:aDirectory |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
724 |
"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
|
725 |
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
|
726 |
|
10408 | 727 |
^ self |
22794 | 728 |
writingTo:commandString |
729 |
errorDisposition:#stderr |
|
730 |
inDirectory:aDirectory |
|
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
731 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
732 |
"unix: |
22794 | 733 |
PipeStream writingTo:'sort' |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
734 |
" |
613 | 735 |
! ! |
736 |
||
2266 | 737 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 738 |
|
613 | 739 |
brokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
740 |
"return the signal used to handle SIGPIPE unix-signals. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
741 |
Since SIGPIPE is asynchronous, we can't decide which smalltalk process |
10408 | 742 |
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
|
743 |
BrokenPipeSignal for SIGPIPE any longer." |
1 | 744 |
|
613 | 745 |
^ BrokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
746 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
747 |
"Modified: 24.9.1997 / 09:43:23 / stefan" |
1 | 748 |
! ! |
749 |
||
16579 | 750 |
!PipeStream class methodsFor:'utilities'! |
751 |
||
752 |
outputFromCommand:aCommand |
|
753 |
"open a pipe reading from aCommand and return the complete output as a string. |
|
18638 | 754 |
If the command cannot be executed, return nil. |
755 |
The command's current directory will be the smalltalk current directory." |
|
756 |
||
757 |
^ self outputFromCommand:aCommand inDirectory:nil |
|
758 |
||
759 |
" |
|
760 |
PipeStream outputFromCommand:'ls -l' |
|
761 |
" |
|
762 |
! |
|
763 |
||
22974 | 764 |
outputFromCommand:aCommand errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectoryOrNil |
765 |
"open a pipe reading from aCommand and return the complete output as a string. |
|
766 |
If the command cannot be executed, return nil. |
|
767 |
The current directory of the command will be aDirectoryOrNil |
|
768 |
or the smalltalk's current directory (if nil). |
|
769 |
errorDisposition may be a stream or one of #discard, #inline or #stderr (default). |
|
770 |
#discard causes stderr to be discarded (/dev/null), |
|
771 |
#inline causes it to be merged into the PipeStream and |
|
772 |
#stderr causes it to be written to smalltalk's own stderr. |
|
773 |
a stream causes stderr to be sent to that stream (internal or external) |
|
774 |
Nil is treated like #stderr" |
|
775 |
||
776 |
|p cmdOutput| |
|
777 |
||
778 |
p := self readingFrom:aCommand errorDisposition:errorDispositionSymbolOrStream inDirectory:aDirectoryOrNil. |
|
779 |
p isNil ifTrue:[^ nil]. |
|
780 |
||
781 |
[ |
|
782 |
cmdOutput := p contentsAsString. |
|
783 |
] ensure:[ |
|
784 |
p close. |
|
785 |
]. |
|
786 |
^ cmdOutput |
|
787 |
||
788 |
" |
|
789 |
PipeStream outputFromCommand:'ls -l' inDirectory:nil |
|
790 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/' |
|
791 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/etc' |
|
792 |
" |
|
793 |
! |
|
794 |
||
18638 | 795 |
outputFromCommand:aCommand inDirectory:aDirectoryOrNil |
796 |
"open a pipe reading from aCommand and return the complete output as a string. |
|
797 |
If the command cannot be executed, return nil. |
|
22974 | 798 |
The current directory of the command will be aDirectoryOrNil |
799 |
or the smalltalk's current directory (if nil). |
|
800 |
Any stderr output will be returned in the returned cmdOutput." |
|
16579 | 801 |
|
802 |
|p cmdOutput| |
|
803 |
||
18638 | 804 |
p := self readingFrom:aCommand inDirectory:aDirectoryOrNil. |
16579 | 805 |
p isNil ifTrue:[^ nil]. |
806 |
||
807 |
[ |
|
22974 | 808 |
cmdOutput := p contentsAsString. |
16579 | 809 |
] ensure:[ |
22974 | 810 |
p close. |
16579 | 811 |
]. |
812 |
^ cmdOutput |
|
813 |
||
814 |
" |
|
18638 | 815 |
PipeStream outputFromCommand:'ls -l' inDirectory:nil |
816 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/' |
|
817 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/etc' |
|
16579 | 818 |
" |
819 |
! ! |
|
820 |
||
99 | 821 |
!PipeStream methodsFor:'accessing'! |
822 |
||
823 |
commandString |
|
824 |
"return the command string" |
|
825 |
||
826 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
827 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
828 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
829 |
exitStatus |
10343 | 830 |
"return the exitStatus" |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
831 |
|
21210 | 832 |
osProcess isNil ifTrue:[ |
833 |
^ nil. |
|
834 |
]. |
|
835 |
^ osProcess exitStatus. |
|
836 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
837 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
838 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
839 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
840 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
841 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
842 |
|
21210 | 843 |
osProcess isNil ifTrue:[ |
844 |
^ nil. |
|
845 |
]. |
|
846 |
^ osProcess pid. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
847 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
848 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
99 | 849 |
! ! |
850 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
851 |
!PipeStream methodsFor:'closing'! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
852 |
|
20681 | 853 |
abortAndClose |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
854 |
"close the Stream and terminate the command" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
855 |
|
20031 | 856 |
self unregisterForFinalization. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
857 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
858 |
"terminate first under windows" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
859 |
OperatingSystem isMSDOSlike ifTrue:[ |
20031 | 860 |
self terminatePipeCommand. |
21228 | 861 |
self closeFile. |
20031 | 862 |
^ self. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
863 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
864 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
865 |
"terminate last under unix" |
21228 | 866 |
self closeFile. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
867 |
self terminatePipeCommand. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
868 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
869 |
|
20681 | 870 |
close |
871 |
"low level close |
|
872 |
This waits for the command to finish. |
|
873 |
Use abortAndClose for a fast (nonBlocking) close." |
|
874 |
||
22696 | 875 |
self isOpen ifTrue:[ |
20681 | 876 |
super close. |
21210 | 877 |
"/ wait for the pipe-command to terminate. |
878 |
self waitForPipeCommandWithTimeout:nil. |
|
20681 | 879 |
]. |
880 |
||
22696 | 881 |
"Modified: / 12-09-1998 / 16:51:04 / cg" |
882 |
"Modified: / 23-04-2018 / 18:25:04 / stefan" |
|
20681 | 883 |
! |
884 |
||
885 |
shutDown |
|
886 |
<resource: #obsolete> |
|
887 |
"this is a historic leftover kept for backward compatibility. |
|
20970 | 888 |
The name collides with the same name in Socket, which does |
20681 | 889 |
not hard terminate the connection." |
890 |
||
891 |
self abortAndClose. |
|
892 |
! |
|
893 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
894 |
shutDownOutput |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
895 |
"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
|
896 |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
897 |
|fd| |
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
898 |
|
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
899 |
self isOpen ifTrue:[ |
10408 | 900 |
fd := self fileDescriptor. |
901 |
fd notNil ifTrue:[ |
|
902 |
OperatingSystem shutdownBidirectionalPipeOutput:fd. |
|
903 |
]. |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
904 |
]. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
905 |
! ! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
906 |
|
5243 | 907 |
!PipeStream methodsFor:'finalization'! |
908 |
||
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
909 |
finalize |
5243 | 910 |
"redefined to avoid blocking in close." |
911 |
||
20681 | 912 |
self abortAndClose |
18785 | 913 |
! ! |
914 |
||
915 |
!PipeStream methodsFor:'private'! |
|
369 | 916 |
|
22794 | 917 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition |
918 |
environment:aShellEnvironmentOrNil inDirectory:aDirectory |
|
919 |
||
10408 | 920 |
"open a pipe to the OS command in commandString; |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
921 |
rwMode may be 'r' or 'w' or 'r+'. |
22794 | 922 |
|
5481 | 923 |
errorDisposition controls where the stdErr output should go, |
924 |
and may be one of #discard, #inline or #stderr (default). |
|
22794 | 925 |
#discard causes stderr to be discarded (/dev/null), |
926 |
#inline causes it to be written to smalltalks own stdout and |
|
927 |
#stderr causes it to be written to smalltalks own stderr. |
|
928 |
Nil is treated like #stderr." |
|
3296 | 929 |
|
21210 | 930 |
|pipeArray remotePipeEnd nullOutput errorNumber myPipeEnd result| |
3296 | 931 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11083
diff
changeset
|
932 |
handle notNil ifTrue:[ |
20099 | 933 |
"the pipe was already open ... |
934 |
this should (can) not happen." |
|
935 |
^ self errorAlreadyOpen |
|
3296 | 936 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
937 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
938 |
rwMode = #r ifTrue:[ |
20099 | 939 |
mode := #readonly. didWrite := false. |
940 |
position := 0. "only reading - can keep track of position" |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
941 |
] ifFalse:[rwMode = #'r+' ifTrue:[ |
20099 | 942 |
mode := #readwrite. didWrite := true. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
943 |
] ifFalse:[ |
20099 | 944 |
mode := #writeonly. didWrite := true. |
945 |
position := 0. "only writing - can keep track of position" |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
946 |
]]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
947 |
|
3296 | 948 |
lastErrorNumber := nil. |
21210 | 949 |
commandString := aCommandString. |
950 |
"stdio lib does not work with blocking pipes and interrupts |
|
951 |
for WIN, Linux, Solaris and probably any other UNIX" |
|
952 |
buffered := false. |
|
953 |
hitEOF := false. |
|
954 |
binary := false. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
955 |
|
21791 | 956 |
osProcess := OSProcess new |
22440 | 957 |
command:aCommandString directory:aDirectory. |
22794 | 958 |
aShellEnvironmentOrNil notNil ifTrue:[ |
959 |
osProcess environment:aShellEnvironmentOrNil |
|
960 |
]. |
|
4616 | 961 |
|
21210 | 962 |
mode == #readwrite ifTrue:[ |
21211 | 963 |
pipeArray := self class makeBidirectionalPipe. |
21210 | 964 |
pipeArray isNil ifTrue:[ |
20099 | 965 |
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber. |
966 |
^ self openError:errorNumber. |
|
967 |
]. |
|
21210 | 968 |
myPipeEnd := pipeArray at:1. |
969 |
remotePipeEnd := pipeArray at:2. |
|
970 |
osProcess inStream:remotePipeEnd. |
|
971 |
osProcess outStream:remotePipeEnd. |
|
972 |
] ifFalse:[ |
|
21211 | 973 |
pipeArray := self class makePipe. |
21210 | 974 |
pipeArray isNil ifTrue:[ |
975 |
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber. |
|
976 |
^ self openError:errorNumber. |
|
20099 | 977 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
978 |
|
21210 | 979 |
mode == #readonly ifTrue:[ |
980 |
"redirect stdout of subprocess to write to pipe" |
|
981 |
myPipeEnd := pipeArray at:1. |
|
982 |
remotePipeEnd := pipeArray at:2. |
|
983 |
osProcess outStream:remotePipeEnd. |
|
20099 | 984 |
] ifFalse:[ |
21210 | 985 |
"redirect stdin of subprocess to read from pipe" |
986 |
myPipeEnd := pipeArray at:2. |
|
987 |
remotePipeEnd := pipeArray at:1. |
|
988 |
osProcess inStream:remotePipeEnd. |
|
20099 | 989 |
]. |
3296 | 990 |
]. |
991 |
||
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
992 |
errorDisposition == #discard ifTrue:[ |
20099 | 993 |
nullOutput := Filename nullDevice writeStream. |
21210 | 994 |
osProcess errorStream:nullOutput. |
995 |
] ifFalse:[(errorDisposition == #inline or:[errorDisposition == #stdout]) ifTrue:[ |
|
996 |
osProcess errorStream:osProcess outStream. |
|
21211 | 997 |
] ifFalse:[(errorDisposition == #stderr or:[errorDisposition isNil]) ifTrue:[ |
21210 | 998 |
osProcess errorStream:Stderr. |
999 |
] ifFalse:[errorDisposition isStream ifTrue:[ |
|
1000 |
osProcess errorStream:errorDisposition. |
|
1001 |
]]]]. |
|
3296 | 1002 |
|
21228 | 1003 |
mode ~~ #readonly ifTrue:[ |
1004 |
osProcess terminateActionBlock:[ |
|
1005 |
"writing doesn't make sense - there is no reader any longer" |
|
1006 |
mode == #readwrite ifTrue:[ |
|
1007 |
"... but allow to read the rest of the command's output" |
|
1008 |
self shutDownOutput. |
|
1009 |
] ifFalse:[mode == #writeonly ifTrue:[ |
|
1010 |
self closeFile. |
|
1011 |
]]. |
|
1012 |
]. |
|
1013 |
]. |
|
8969
978613904d0b
For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents:
8968
diff
changeset
|
1014 |
|
21210 | 1015 |
result := osProcess startProcess. |
3296 | 1016 |
|
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
1017 |
"subprocess has been created. |
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
1018 |
close unused filedescriptors" |
21210 | 1019 |
remotePipeEnd notNil ifTrue:[ |
1020 |
remotePipeEnd close. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
1021 |
]. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
1022 |
nullOutput notNil ifTrue:[ |
20099 | 1023 |
nullOutput close |
3296 | 1024 |
]. |
1025 |
||
21210 | 1026 |
result ifTrue:[ |
21791 | 1027 |
"successful creation of subprocess" |
21231 | 1028 |
handle := myPipeEnd handle. |
1029 |
handleType := myPipeEnd handleType. |
|
22507 | 1030 |
myPipeEnd unregisterForFinalization. "make sure filedescriptor is not closed by finalizer" |
21210 | 1031 |
myPipeEnd := nil. |
3296 | 1032 |
] ifFalse:[ |
21210 | 1033 |
"the pipe open failed for some reason ... |
20099 | 1034 |
... this may be either due to an invalid command string, |
1035 |
or due to the system running out of memory (when forking |
|
21210 | 1036 |
the unix process)" |
1037 |
lastErrorNumber := OperatingSystem lastErrorNumber. |
|
1038 |
myPipeEnd close. |
|
20099 | 1039 |
^ self openError:lastErrorNumber. |
3296 | 1040 |
]. |
1041 |
||
18782 | 1042 |
self registerForFinalization. |
3296 | 1043 |
|
21791 | 1044 |
"Modified: / 23-04-1996 / 17:05:59 / stefan" |
1045 |
"Modified: / 28-01-1998 / 14:47:34 / md" |
|
1046 |
"Created: / 19-05-1999 / 12:28:54 / cg" |
|
22507 | 1047 |
"Modified (comment): / 25-01-2018 / 19:40:27 / mawalch" |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1048 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1049 |
|
22794 | 1050 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition inDirectory:aDirectory |
1051 |
"open a pipe to the OS command in commandString; |
|
1052 |
rwMode may be 'r' or 'w' or 'r+'. |
|
1053 |
errorDisposition controls where the stdErr output should go, |
|
1054 |
and may be one of #discard, #inline or #stderr (default). |
|
1055 |
#discard causes stderr to be discarded (/dev/null), |
|
1056 |
#inline causes it to be written to smalltalks own stdout and |
|
1057 |
#stderr causes it to be written to smalltalks own stderr. |
|
1058 |
Nil is treated like #stderr" |
|
1059 |
||
1060 |
^ self |
|
1061 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition |
|
1062 |
environment:nil inDirectory:aDirectory |
|
1063 |
! |
|
1064 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1065 |
terminatePipeCommand |
21210 | 1066 |
osProcess notNil ifTrue:[ |
1067 |
osProcess terminateGroup. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1068 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1069 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1070 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1071 |
waitForPipeCommandWithTimeout:seconds |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1072 |
"wait for the pipe command to terminate itself. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1073 |
Return true, if a timeout occurred." |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1074 |
|
21210 | 1075 |
osProcess notNil ifTrue:[ |
1076 |
^ osProcess finishSema waitWithTimeout:seconds. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
1077 |
]. |
21210 | 1078 |
^ false |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
1079 |
! ! |
613 | 1080 |
|
22980 | 1081 |
!PipeStream methodsFor:'testing'! |
1082 |
||
1083 |
isPipeStream |
|
1084 |
^ true |
|
1085 |
! ! |
|
1086 |
||
2266 | 1087 |
!PipeStream class methodsFor:'documentation'! |
1295 | 1088 |
|
1089 |
version |
|
18638 | 1090 |
^ '$Header$' |
12712
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
1091 |
! |
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
1092 |
|
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
1093 |
version_CVS |
18638 | 1094 |
^ '$Header$' |
1295 | 1095 |
! ! |
7027 | 1096 |
|
15359 | 1097 |
|
613 | 1098 |
PipeStream initialize! |