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