author | Jan Vrany <jan.vrany@labware.com> |
Wed, 22 Mar 2023 13:57:18 +0000 | |
branch | jv |
changeset 25445 | 1623217d2268 |
parent 24086 | 9018daacf498 |
permissions | -rw-r--r-- |
21210 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1 | 3 |
" |
5 | 4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 5 |
All Rights Reserved |
1 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
5388
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
14 |
"{ Package: 'stx:libbasic' }" |
bef4d52749a4
terminate command on close (also under unix)
Claus Gittinger <cg@exept.de>
parents:
5243
diff
changeset
|
15 |
|
17460 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
269 | 18 |
NonPositionableExternalStream subclass:#PipeStream |
21210 | 19 |
instanceVariableNames:'commandString osProcess' |
1295 | 20 |
classVariableNames:'BrokenPipeSignal' |
21 |
poolDictionaries:'' |
|
22 |
category:'Streams-External' |
|
88 | 23 |
! |
2 | 24 |
|
217 | 25 |
!PipeStream primitiveDefinitions! |
88 | 26 |
%{ |
17077 | 27 |
#include "stxOSDefs.h" |
437 | 28 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
29 |
#if defined(__win32__) |
793 | 30 |
# undef UNIX_LIKE |
31 |
# define MSDOS_LIKE |
|
32 |
#endif |
|
33 |
||
20298 | 34 |
#ifndef _STDIO_H_INCLUDED_ |
35 |
# include <stdio.h> |
|
36 |
# define _STDIO_H_INCLUDED_ |
|
37 |
#endif |
|
437 | 38 |
|
20298 | 39 |
#ifndef _ERRNO_H_INCLUDED_ |
40 |
# include <errno.h> |
|
41 |
# define _ERRNO_H_INCLUDED_ |
|
42 |
#endif |
|
437 | 43 |
|
88 | 44 |
#ifndef transputer |
45 |
# include <sys/types.h> |
|
46 |
# include <sys/stat.h> |
|
47 |
#endif |
|
230 | 48 |
|
49 |
/* |
|
50 |
* on some systems errno is a macro ... check for it here |
|
51 |
*/ |
|
52 |
#ifndef errno |
|
53 |
extern errno; |
|
54 |
#endif |
|
55 |
||
1663 | 56 |
#ifdef LINUX |
57 |
# define BUGGY_STDIO_LIB |
|
58 |
#endif |
|
59 |
||
88 | 60 |
%} |
180 | 61 |
! ! |
88 | 62 |
|
325 | 63 |
!PipeStream primitiveFunctions! |
64 |
%{ |
|
65 |
||
66 |
/* |
|
2925 | 67 |
* no longer needed - popen is useless ... |
68 |
*/ |
|
69 |
#undef NEED_POPEN_WITH_VFORK |
|
70 |
||
71 |
/* |
|
325 | 72 |
* some systems (i.e. ultrix) use fork; |
16953 | 73 |
* we're better off with a popen based on vfork ... |
325 | 74 |
*/ |
75 |
#ifdef NEED_POPEN_WITH_VFORK |
|
76 |
||
77 |
static int popen_pid = 0; |
|
78 |
||
79 |
FILE * |
|
80 |
popen(command, type) |
|
81 |
/* const */ char *command; |
|
82 |
/* const */ char *type; |
|
83 |
{ |
|
84 |
int pipes[2]; |
|
85 |
int itype = (strcmp(type, "w") == 0 ? 1 : 0); |
|
86 |
||
87 |
if (pipe(pipes) == -1) |
|
326 | 88 |
return NULL; |
325 | 89 |
|
90 |
switch (popen_pid = vfork()) { |
|
91 |
case -1: |
|
326 | 92 |
(void)close(pipes[0]); |
93 |
(void)close(pipes[1]); |
|
94 |
return NULL; |
|
325 | 95 |
|
96 |
case 0: |
|
326 | 97 |
if (itype) { |
98 |
dup2(pipes[0], fileno(stdin)); |
|
99 |
close(pipes[1]); |
|
100 |
} else { |
|
101 |
dup2(pipes[1], fileno(stdout)); |
|
102 |
close(pipes[0]); |
|
103 |
} |
|
104 |
execl("/bin/sh", "/bin/sh", "-c", command, 0); |
|
10408 | 105 |
console_fprintf(stderr, "PipeStream [warning]: execlp failed in popen\n"); |
326 | 106 |
_exit(-1); |
107 |
/* NOTREACHED */ |
|
325 | 108 |
|
109 |
default: |
|
326 | 110 |
if (itype) { |
111 |
close(pipes[0]); |
|
112 |
return fdopen(pipes[1], "w"); |
|
113 |
} else { |
|
114 |
close(pipes[1]); |
|
115 |
return fdopen(pipes[0], "r"); |
|
116 |
} |
|
325 | 117 |
} |
118 |
} |
|
119 |
||
120 |
int |
|
121 |
pclose(str) |
|
122 |
FILE *str; |
|
123 |
{ |
|
124 |
int pd = 0; |
|
125 |
int status; |
|
126 |
int err; |
|
127 |
||
128 |
err = fclose(str); |
|
129 |
||
130 |
do { |
|
326 | 131 |
if ((pd = wait(&status)) == -1) |
132 |
{ |
|
133 |
err = EOF; |
|
134 |
break; |
|
135 |
} |
|
325 | 136 |
} while (pd != popen_pid); |
137 |
||
138 |
if (err == EOF) |
|
326 | 139 |
return -1; |
325 | 140 |
|
141 |
if (status) |
|
326 | 142 |
status >>= 8; /* exit status in high byte */ |
325 | 143 |
|
144 |
return status; |
|
145 |
} |
|
146 |
||
2925 | 147 |
#endif /* NEED_POPEN_WITH_VFORK */ |
325 | 148 |
|
149 |
%} |
|
150 |
! ! |
|
151 |
||
2266 | 152 |
!PipeStream class methodsFor:'documentation'! |
613 | 153 |
|
154 |
copyright |
|
155 |
" |
|
156 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
157 |
All Rights Reserved |
|
158 |
||
159 |
This software is furnished under a license and may be used |
|
160 |
only in accordance with the terms of that license and with the |
|
161 |
inclusion of the above copyright notice. This software may not |
|
162 |
be provided or otherwise made available to, or used by, any |
|
163 |
other person. No title to or ownership of the software is |
|
164 |
hereby transferred. |
|
165 |
" |
|
166 |
! |
|
167 |
||
168 |
documentation |
|
169 |
" |
|
17460 | 170 |
Pipestreams allow reading or writing from/to a unix or dos command. |
613 | 171 |
For example, to get a stream reading the output of an 'ls -l' |
172 |
command, a PipeStream can be created with: |
|
173 |
||
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
174 |
PipeStream readingFrom:'ls -l' |
613 | 175 |
|
17460 | 176 |
the characters of the command's output can be read using the |
177 |
standard stream messages, such as next, nextLine etc. |
|
613 | 178 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
179 |
Example for writing to a command: |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
180 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
181 |
PipeStream writingTo:'cat >/tmp/x' |
613 | 182 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
183 |
Bidirectional pipestreams (supporting both reading an writing) may be used for filters: |
613 | 184 |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
185 |
PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/''' |
1295 | 186 |
|
1662
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
187 |
Buffered pipes do not work with Linux - the stdio library seems to be |
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
188 |
buggy (trying to restart the read ...) |
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
189 |
|
1295 | 190 |
[author:] |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
191 |
Claus Gittinger |
2966 | 192 |
|
193 |
[see also:] |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
194 |
ExternalStream FileStream Socket |
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
195 |
OperatingSystem |
613 | 196 |
" |
197 |
! ! |
|
198 |
||
2266 | 199 |
!PipeStream class methodsFor:'initialization'! |
2 | 200 |
|
201 |
initialize |
|
202 |
"setup the signal" |
|
203 |
||
57 | 204 |
BrokenPipeSignal isNil ifTrue:[ |
10408 | 205 |
BrokenPipeSignal := WriteError newSignalMayProceed:true. |
206 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal. |
|
207 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'. |
|
57 | 208 |
] |
2 | 209 |
! ! |
210 |
||
2266 | 211 |
!PipeStream class methodsFor:'instance creation'! |
2 | 212 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
213 |
bidirectionalFor:commandString |
10408 | 214 |
"create and return a new bidirectonal pipeStream which can both be written to |
215 |
and read from the unix command given by commandString. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
216 |
The commands error output is send to my own error output." |
2 | 217 |
|
10408 | 218 |
^ self |
219 |
bidirectionalFor:commandString |
|
220 |
errorDisposition:#stderr |
|
221 |
inDirectory:nil |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
222 |
|
2966 | 223 |
" |
10408 | 224 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
225 |
|
10408 | 226 |
p := PipeStream bidirectionalFor:'cat -u'. |
21210 | 227 |
p nextPutAll:'Wer ist der Bürgermeister von Wesel'; cr. |
10408 | 228 |
Transcript showCR:p nextLine. |
229 |
p close |
|
2966 | 230 |
" |
231 |
||
232 |
" |
|
10408 | 233 |
|p| |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
234 |
|
10408 | 235 |
p := PipeStream bidirectionalFor:'sed -e ''s/Hello/Greetings/'''. |
236 |
p nextPutAll:'Hello world'; cr. |
|
237 |
p shutDownOutput. |
|
238 |
Transcript showCR:p nextLine. |
|
239 |
p close |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
240 |
" |
9313 | 241 |
|
242 |
" |
|
10408 | 243 |
|p| |
9313 | 244 |
|
10408 | 245 |
p := PipeStream bidirectionalFor:'wc'. |
246 |
p nextPutAll:'Hello world'; cr. |
|
247 |
p shutDownOutput. |
|
248 |
Transcript showCR:p nextLine. |
|
249 |
p close |
|
9313 | 250 |
" |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
251 |
! |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
252 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
253 |
bidirectionalFor:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
10408 | 254 |
"create and return a new bidirectonal pipeStream which can both be written to |
255 |
and read from the unix command given by commandString. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
256 |
The directory will be changed to aDirectory while |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
257 |
executing the command. Use this if a command is to be |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
258 |
executed in another directory, to avoid any OS dependencies |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
259 |
in your code. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
260 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
261 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
10408 | 262 |
#discard causes stderr to be discarded (/dev/null), |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
263 |
#inline causes it to be written to smalltalks own stdout and |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
264 |
#stderr causes it to be written to smalltalks own stderr. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
265 |
Nil is treated like #stderr" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
266 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
267 |
^ self basicNew |
10408 | 268 |
openPipeFor:commandString |
269 |
withMode:#'r+' |
|
270 |
errorDisposition:errorDisposition |
|
271 |
inDirectory:aDirectory |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
272 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
273 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
274 |
readingFrom:commandString |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
275 |
"create and return a new pipeStream which can read from the unix command |
10408 | 276 |
given by commandString. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
277 |
The commands error output is send to my own error output." |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
278 |
|
10408 | 279 |
^ self |
21210 | 280 |
readingFrom:commandString |
281 |
errorDisposition:#stderr |
|
282 |
inDirectory:nil |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
283 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
284 |
"unix: |
21210 | 285 |
PipeStream readingFrom:'ls -l'. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
286 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
287 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
288 |
" |
21210 | 289 |
|p| |
290 |
||
291 |
p := PipeStream readingFrom:'ls -l'. |
|
292 |
Transcript showCR:p nextLine. |
|
293 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
294 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
295 |
|
21210 | 296 |
|
297 |
" |
|
298 |
|p| |
|
299 |
||
300 |
p := PipeStream readingFrom:'echo error >&2'. |
|
301 |
Transcript showCR:p nextLine. |
|
302 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
303 |
" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
304 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
305 |
" |
21210 | 306 |
|s| |
307 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
|
308 |
(Delay forSeconds:2) wait. |
|
309 |
s abortAndClose |
|
2966 | 310 |
" |
311 |
||
21210 | 312 |
" |
313 |
|p| |
|
314 |
p := PipeStream readingFrom:'dir'. |
|
315 |
Transcript showCR:p nextLine. |
|
316 |
p close |
|
317 |
" |
|
318 |
||
319 |
"Windows: |
|
320 |
PipeStream readingFrom:'dir'. |
|
2966 | 321 |
" |
322 |
" |
|
21210 | 323 |
|p| |
324 |
p := PipeStream readingFrom:'dir'. |
|
325 |
Transcript showCR:p nextLine. |
|
326 |
p close |
|
2966 | 327 |
" |
328 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
329 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 330 |
! |
1 | 331 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
332 |
readingFrom:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
333 |
"similar to #readingFrom, but changes the directory while |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
334 |
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
|
335 |
executed in another directory, to avoid any OS dependencies |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
336 |
in your code. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
337 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
10408 | 338 |
#discard causes stderr to be discarded (/dev/null), |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
339 |
#inline causes it to be merged into the PipeStream and |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
340 |
#stderr causes it to be written to smalltalks own stderr. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
341 |
Nil is treated like #stderr" |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
342 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
343 |
^ self basicNew |
21210 | 344 |
openPipeFor:commandString |
345 |
withMode:#r |
|
346 |
errorDisposition:errorDisposition |
|
347 |
inDirectory:aDirectory |
|
348 |
||
349 |
||
350 |
" |
|
351 |
|p| |
|
352 |
||
353 |
p := PipeStream readingFrom:'bla' errorDisposition:Transcript inDirectory:nil. |
|
354 |
Transcript showCR:p nextLine. |
|
355 |
p close |
|
356 |
" |
|
357 |
||
358 |
" |
|
359 |
|p| |
|
360 |
||
361 |
p := PipeStream readingFrom:'bla' errorDisposition:#inline inDirectory:nil. |
|
362 |
Transcript showCR:p nextLine. |
|
363 |
p close |
|
364 |
" |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
365 |
! |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
366 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
367 |
readingFrom:commandString inDirectory:aDirectory |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
368 |
"similar to #readingFrom, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
369 |
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
|
370 |
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
|
371 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
372 |
The commands error output is send to my own error output." |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
373 |
|
10408 | 374 |
^ self |
375 |
readingFrom:commandString |
|
376 |
errorDisposition:#stderr |
|
377 |
inDirectory:aDirectory |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
378 |
|
10408 | 379 |
" UNIX: |
380 |
|p| |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
381 |
|
10408 | 382 |
p := PipeStream readingFrom:'ls -l' inDirectory:'/etc'. |
383 |
Transcript showCR:p upToEnd. |
|
384 |
p close |
|
9193 | 385 |
" |
10408 | 386 |
"WINDOOF: |
387 |
|p| |
|
9193 | 388 |
|
10408 | 389 |
p := PipeStream readingFrom:'dir'. |
390 |
Transcript showCR:p upToEnd. |
|
391 |
p close |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
392 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
393 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
394 |
|
2 | 395 |
writingTo:commandString |
1 | 396 |
"create and return a new pipeStream which can write to the unix command |
397 |
given by command." |
|
398 |
||
10408 | 399 |
^ self |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
400 |
writingTo:commandString errorDisposition:#stderr inDirectory:nil |
1 | 401 |
|
2966 | 402 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
403 |
PipeStream writingTo:'sort' |
2966 | 404 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
405 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
406 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
407 |
writingTo:commandString errorDisposition:errorDisposition inDirectory:aDirectory |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
408 |
"similar to #writingTo, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
409 |
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
|
410 |
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
|
411 |
in your code. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
412 |
errorDisposition may be one of #discard, #inline or #stderr (default). |
10408 | 413 |
#discard causes stderr to be discarded (/dev/null), |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
414 |
#inline causes it to be written to smalltalks own stdout and |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
415 |
#stderr causes it to be written to smalltalks own stderr. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
416 |
Nil is treated like #stderr" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
417 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
418 |
^ self basicNew |
10408 | 419 |
openPipeFor:commandString |
420 |
withMode:#w |
|
421 |
errorDisposition:errorDisposition |
|
422 |
inDirectory:aDirectory |
|
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
423 |
! |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
424 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
425 |
writingTo:commandString inDirectory:aDirectory |
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
426 |
"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
|
427 |
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
|
428 |
|
10408 | 429 |
^ self |
430 |
writingTo:commandString errorDisposition:#stderr inDirectory:aDirectory |
|
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
431 |
|
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
432 |
"unix: |
10408 | 433 |
PipeStream writingTo:'sort' |
4526
a42dc8e09586
errorOpen renamed to errorAlreadyOpen
Claus Gittinger <cg@exept.de>
parents:
4202
diff
changeset
|
434 |
" |
613 | 435 |
! ! |
436 |
||
2266 | 437 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 438 |
|
613 | 439 |
brokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
440 |
"return the signal used to handle SIGPIPE unix-signals. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
441 |
Since SIGPIPE is asynchronous, we can't decide which smalltalk process |
10408 | 442 |
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
|
443 |
BrokenPipeSignal for SIGPIPE any longer." |
1 | 444 |
|
613 | 445 |
^ BrokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
446 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
447 |
"Modified: 24.9.1997 / 09:43:23 / stefan" |
1 | 448 |
! ! |
449 |
||
16579 | 450 |
!PipeStream class methodsFor:'utilities'! |
451 |
||
452 |
outputFromCommand:aCommand |
|
453 |
"open a pipe reading from aCommand and return the complete output as a string. |
|
18638 | 454 |
If the command cannot be executed, return nil. |
455 |
The command's current directory will be the smalltalk current directory." |
|
456 |
||
457 |
^ self outputFromCommand:aCommand inDirectory:nil |
|
458 |
||
459 |
" |
|
460 |
PipeStream outputFromCommand:'ls -l' |
|
461 |
" |
|
462 |
! |
|
463 |
||
464 |
outputFromCommand:aCommand inDirectory:aDirectoryOrNil |
|
465 |
"open a pipe reading from aCommand and return the complete output as a string. |
|
466 |
If the command cannot be executed, return nil. |
|
467 |
The current directory of the command will be aDirectoryOrNil or the smalltalk's current directory (if nil)" |
|
16579 | 468 |
|
469 |
|p cmdOutput| |
|
470 |
||
18638 | 471 |
p := self readingFrom:aCommand inDirectory:aDirectoryOrNil. |
16579 | 472 |
p isNil ifTrue:[^ nil]. |
473 |
||
474 |
[ |
|
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
475 |
cmdOutput := p contentsAsString. |
16579 | 476 |
] ensure:[ |
19860
324edacff5cc
unified cpu and os defines;
Claus Gittinger <cg@exept.de>
parents:
18785
diff
changeset
|
477 |
p close. |
16579 | 478 |
]. |
479 |
^ cmdOutput |
|
480 |
||
481 |
" |
|
18638 | 482 |
PipeStream outputFromCommand:'ls -l' inDirectory:nil |
483 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/' |
|
484 |
PipeStream outputFromCommand:'ls -l' inDirectory:'/etc' |
|
16579 | 485 |
" |
486 |
! ! |
|
487 |
||
99 | 488 |
!PipeStream methodsFor:'accessing'! |
489 |
||
490 |
commandString |
|
491 |
"return the command string" |
|
492 |
||
493 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
494 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
495 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
496 |
exitStatus |
10343 | 497 |
"return the exitStatus" |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
498 |
|
21210 | 499 |
osProcess isNil ifTrue:[ |
500 |
^ nil. |
|
501 |
]. |
|
502 |
^ osProcess exitStatus. |
|
503 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
504 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
505 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
506 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
507 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
508 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
509 |
|
21210 | 510 |
osProcess isNil ifTrue:[ |
511 |
^ nil. |
|
512 |
]. |
|
513 |
^ osProcess pid. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
514 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
515 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
24086 | 516 |
! |
517 |
||
518 |
setCommandString:aString |
|
519 |
"for OSProcess only (so we see the command in the streams monitor)" |
|
520 |
||
521 |
commandString := aString |
|
522 |
||
523 |
"Created: / 29-10-2018 / 15:14:24 / Claus Gittinger" |
|
99 | 524 |
! ! |
525 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
526 |
!PipeStream methodsFor:'closing'! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
527 |
|
20681 | 528 |
abortAndClose |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
529 |
"close the Stream and terminate the command" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
530 |
|
20031 | 531 |
self unregisterForFinalization. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
532 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
533 |
"terminate first under windows" |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
534 |
OperatingSystem isMSDOSlike ifTrue:[ |
20031 | 535 |
self terminatePipeCommand. |
21228 | 536 |
self closeFile. |
20031 | 537 |
^ self. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
538 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
539 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
540 |
"terminate last under unix" |
21228 | 541 |
self closeFile. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
542 |
self terminatePipeCommand. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
543 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
544 |
|
20681 | 545 |
close |
546 |
"low level close |
|
547 |
This waits for the command to finish. |
|
548 |
Use abortAndClose for a fast (nonBlocking) close." |
|
549 |
||
550 |
handle notNil ifTrue:[ |
|
551 |
super close. |
|
21210 | 552 |
"/ wait for the pipe-command to terminate. |
553 |
self waitForPipeCommandWithTimeout:nil. |
|
20681 | 554 |
]. |
555 |
||
556 |
"Modified: / 12.9.1998 / 16:51:04 / cg" |
|
557 |
! |
|
558 |
||
559 |
shutDown |
|
560 |
<resource: #obsolete> |
|
561 |
"this is a historic leftover kept for backward compatibility. |
|
20970 | 562 |
The name collides with the same name in Socket, which does |
20681 | 563 |
not hard terminate the connection." |
564 |
||
565 |
self abortAndClose. |
|
566 |
! |
|
567 |
||
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
568 |
shutDownOutput |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
569 |
"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
|
570 |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
571 |
|fd| |
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
572 |
|
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
573 |
self isOpen ifTrue:[ |
10408 | 574 |
fd := self fileDescriptor. |
575 |
fd notNil ifTrue:[ |
|
576 |
OperatingSystem shutdownBidirectionalPipeOutput:fd. |
|
577 |
]. |
|
8976
2ff451956d48
#shutDownOutput do nothing (ignore errors), if pipe is closed
Stefan Vogel <sv@exept.de>
parents:
8969
diff
changeset
|
578 |
]. |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
579 |
! ! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
580 |
|
5243 | 581 |
!PipeStream methodsFor:'finalization'! |
582 |
||
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
583 |
finalize |
5243 | 584 |
"redefined to avoid blocking in close." |
585 |
||
20681 | 586 |
self abortAndClose |
18785 | 587 |
! ! |
588 |
||
589 |
!PipeStream methodsFor:'private'! |
|
369 | 590 |
|
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
591 |
openPipeFor:aCommandString withMode:rwMode errorDisposition:errorDisposition inDirectory:aDirectory |
10408 | 592 |
"open a pipe to the OS command in commandString; |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
593 |
rwMode may be 'r' or 'w' or 'r+'. |
5481 | 594 |
errorDisposition controls where the stdErr output should go, |
595 |
and may be one of #discard, #inline or #stderr (default). |
|
10408 | 596 |
#discard causes stderr to be discarded (/dev/null), |
5481 | 597 |
#inline causes it to be written to smalltalks own stdout and |
598 |
#stderr causes it to be written to smalltalks own stderr. |
|
599 |
Nil is treated like #stderr" |
|
3296 | 600 |
|
21210 | 601 |
|pipeArray remotePipeEnd nullOutput errorNumber myPipeEnd result| |
3296 | 602 |
|
12395
1db7ea277c93
filePointer -> handle migration
Claus Gittinger <cg@exept.de>
parents:
11083
diff
changeset
|
603 |
handle notNil ifTrue:[ |
20099 | 604 |
"the pipe was already open ... |
605 |
this should (can) not happen." |
|
606 |
^ self errorAlreadyOpen |
|
3296 | 607 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
608 |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
609 |
rwMode = #r ifTrue:[ |
20099 | 610 |
mode := #readonly. didWrite := false. |
611 |
position := 0. "only reading - can keep track of position" |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
612 |
] ifFalse:[rwMode = #'r+' ifTrue:[ |
20099 | 613 |
mode := #readwrite. didWrite := true. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
614 |
] ifFalse:[ |
20099 | 615 |
mode := #writeonly. didWrite := true. |
616 |
position := 0. "only writing - can keep track of position" |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
617 |
]]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
618 |
|
3296 | 619 |
lastErrorNumber := nil. |
21210 | 620 |
commandString := aCommandString. |
621 |
"stdio lib does not work with blocking pipes and interrupts |
|
622 |
for WIN, Linux, Solaris and probably any other UNIX" |
|
623 |
buffered := false. |
|
624 |
hitEOF := false. |
|
625 |
binary := false. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
626 |
|
21210 | 627 |
osProcess := OSProcess new |
628 |
command:aCommandString; |
|
629 |
directory:aDirectory. |
|
4616 | 630 |
|
21210 | 631 |
mode == #readwrite ifTrue:[ |
21211 | 632 |
pipeArray := self class makeBidirectionalPipe. |
21210 | 633 |
pipeArray isNil ifTrue:[ |
20099 | 634 |
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber. |
635 |
^ self openError:errorNumber. |
|
636 |
]. |
|
21210 | 637 |
myPipeEnd := pipeArray at:1. |
638 |
remotePipeEnd := pipeArray at:2. |
|
639 |
osProcess inStream:remotePipeEnd. |
|
640 |
osProcess outStream:remotePipeEnd. |
|
641 |
] ifFalse:[ |
|
21211 | 642 |
pipeArray := self class makePipe. |
21210 | 643 |
pipeArray isNil ifTrue:[ |
644 |
lastErrorNumber := errorNumber := OperatingSystem currentErrorNumber. |
|
645 |
^ self openError:errorNumber. |
|
20099 | 646 |
]. |
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
647 |
|
21210 | 648 |
mode == #readonly ifTrue:[ |
649 |
"redirect stdout of subprocess to write to pipe" |
|
650 |
myPipeEnd := pipeArray at:1. |
|
651 |
remotePipeEnd := pipeArray at:2. |
|
652 |
osProcess outStream:remotePipeEnd. |
|
20099 | 653 |
] ifFalse:[ |
21210 | 654 |
"redirect stdin of subprocess to read from pipe" |
655 |
myPipeEnd := pipeArray at:2. |
|
656 |
remotePipeEnd := pipeArray at:1. |
|
657 |
osProcess inStream:remotePipeEnd. |
|
20099 | 658 |
]. |
3296 | 659 |
]. |
660 |
||
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
661 |
errorDisposition == #discard ifTrue:[ |
20099 | 662 |
nullOutput := Filename nullDevice writeStream. |
21210 | 663 |
osProcess errorStream:nullOutput. |
664 |
] ifFalse:[(errorDisposition == #inline or:[errorDisposition == #stdout]) ifTrue:[ |
|
665 |
osProcess errorStream:osProcess outStream. |
|
21211 | 666 |
] ifFalse:[(errorDisposition == #stderr or:[errorDisposition isNil]) ifTrue:[ |
21210 | 667 |
osProcess errorStream:Stderr. |
668 |
] ifFalse:[errorDisposition isStream ifTrue:[ |
|
669 |
osProcess errorStream:errorDisposition. |
|
670 |
]]]]. |
|
3296 | 671 |
|
21228 | 672 |
mode ~~ #readonly ifTrue:[ |
673 |
osProcess terminateActionBlock:[ |
|
674 |
"writing doesn't make sense - there is no reader any longer" |
|
675 |
mode == #readwrite ifTrue:[ |
|
676 |
"... but allow to read the rest of the command's output" |
|
677 |
self shutDownOutput. |
|
678 |
] ifFalse:[mode == #writeonly ifTrue:[ |
|
679 |
self closeFile. |
|
680 |
]]. |
|
681 |
]. |
|
682 |
]. |
|
8969
978613904d0b
For writing Pipestreams: close write side of filedescriptor, when
Stefan Vogel <sv@exept.de>
parents:
8968
diff
changeset
|
683 |
|
21210 | 684 |
result := osProcess startProcess. |
3296 | 685 |
|
7079
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
686 |
"subprocess has been created. |
07625dfffcbf
Cleanup filedescriptor closing on #exec:withArguments:....
Stefan Vogel <sv@exept.de>
parents:
7060
diff
changeset
|
687 |
close unused filedescriptors" |
21210 | 688 |
remotePipeEnd notNil ifTrue:[ |
689 |
remotePipeEnd close. |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
690 |
]. |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
691 |
nullOutput notNil ifTrue:[ |
20099 | 692 |
nullOutput close |
3296 | 693 |
]. |
694 |
||
21210 | 695 |
result ifTrue:[ |
20099 | 696 |
"successfull creation of subprocesss" |
21231 | 697 |
handle := myPipeEnd handle. |
698 |
handleType := myPipeEnd handleType. |
|
21210 | 699 |
myPipeEnd unregisterForFinalization. "make sure filedesciptor is not closed by finalizer" |
700 |
myPipeEnd := nil. |
|
3296 | 701 |
] ifFalse:[ |
21210 | 702 |
"the pipe open failed for some reason ... |
20099 | 703 |
... this may be either due to an invalid command string, |
704 |
or due to the system running out of memory (when forking |
|
21210 | 705 |
the unix process)" |
706 |
lastErrorNumber := OperatingSystem lastErrorNumber. |
|
707 |
myPipeEnd close. |
|
20099 | 708 |
^ self openError:lastErrorNumber. |
3296 | 709 |
]. |
710 |
||
18782 | 711 |
self registerForFinalization. |
3296 | 712 |
|
4202
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
713 |
"Modified: / 23.4.1996 / 17:05:59 / stefan" |
b9f85ebd2e38
generalized errorDisposition (for other OS's);
Claus Gittinger <cg@exept.de>
parents:
4125
diff
changeset
|
714 |
"Modified: / 28.1.1998 / 14:47:34 / md" |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
715 |
"Created: / 19.5.1999 / 12:28:54 / cg" |
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
716 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
717 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
718 |
terminatePipeCommand |
21210 | 719 |
osProcess notNil ifTrue:[ |
720 |
osProcess terminateGroup. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
721 |
]. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
722 |
! |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
723 |
|
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
724 |
waitForPipeCommandWithTimeout:seconds |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
725 |
"wait for the pipe command to terminate itself. |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
726 |
Return true, if a timeout occurred." |
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
727 |
|
21210 | 728 |
osProcess notNil ifTrue:[ |
729 |
^ osProcess finishSema waitWithTimeout:seconds. |
|
8968
879991b52630
Support for bidirectional PipeStreams
Stefan Vogel <sv@exept.de>
parents:
8520
diff
changeset
|
730 |
]. |
21210 | 731 |
^ false |
6439
0f841258ec4a
Use #finalize instead of #disposed
Stefan Vogel <sv@exept.de>
parents:
6267
diff
changeset
|
732 |
! ! |
613 | 733 |
|
2266 | 734 |
!PipeStream class methodsFor:'documentation'! |
1295 | 735 |
|
736 |
version |
|
18638 | 737 |
^ '$Header$' |
12712
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
738 |
! |
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
739 |
|
c1b3b5846541
Make most win32 wrap calls non-interruptable
Stefan Vogel <sv@exept.de>
parents:
12548
diff
changeset
|
740 |
version_CVS |
18638 | 741 |
^ '$Header$' |
1295 | 742 |
! ! |
7027 | 743 |
|
15359 | 744 |
|
613 | 745 |
PipeStream initialize! |