author | Stefan Vogel <sv@exept.de> |
Wed, 24 Sep 1997 17:11:32 +0200 | |
changeset 2985 | eff9e86dd2f9 |
parent 2976 | dcb3a0f7f2f9 |
child 2989 | c96b2a254daa |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
159 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
269 | 13 |
NonPositionableExternalStream subclass:#PipeStream |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
14 |
instanceVariableNames:'commandString pid exitStatus exitSema exitAction' |
1295 | 15 |
classVariableNames:'BrokenPipeSignal' |
16 |
poolDictionaries:'' |
|
17 |
category:'Streams-External' |
|
88 | 18 |
! |
2 | 19 |
|
217 | 20 |
!PipeStream primitiveDefinitions! |
88 | 21 |
%{ |
437 | 22 |
|
793 | 23 |
#if defined(NT) || defined(WIN32) || defined(MSDOS) |
24 |
# undef UNIX_LIKE |
|
25 |
# define MSDOS_LIKE |
|
26 |
#endif |
|
27 |
||
2896 | 28 |
#if defined(__openVMS__) |
29 |
# undef __new |
|
30 |
#endif |
|
31 |
||
88 | 32 |
#include <stdio.h> |
437 | 33 |
#define _STDIO_H_INCLUDED_ |
34 |
||
88 | 35 |
#include <errno.h> |
437 | 36 |
#define _ERRNO_H_INCLUDED_ |
37 |
||
88 | 38 |
#ifndef transputer |
39 |
# include <sys/types.h> |
|
40 |
# include <sys/stat.h> |
|
41 |
#endif |
|
230 | 42 |
|
43 |
/* |
|
44 |
* on some systems errno is a macro ... check for it here |
|
45 |
*/ |
|
46 |
#ifndef errno |
|
47 |
extern errno; |
|
48 |
#endif |
|
49 |
||
1663 | 50 |
#ifdef LINUX |
51 |
# define BUGGY_STDIO_LIB |
|
52 |
#endif |
|
53 |
||
88 | 54 |
%} |
180 | 55 |
! ! |
88 | 56 |
|
325 | 57 |
!PipeStream primitiveFunctions! |
58 |
%{ |
|
59 |
||
60 |
/* |
|
2925 | 61 |
* no longer needed - popen is useless ... |
62 |
*/ |
|
63 |
#undef NEED_POPEN_WITH_VFORK |
|
64 |
||
65 |
||
66 |
/* |
|
325 | 67 |
* some systems (i.e. ultrix) use fork; |
68 |
* were better off with a popen based on vfork ... |
|
69 |
*/ |
|
70 |
#ifdef NEED_POPEN_WITH_VFORK |
|
71 |
||
72 |
static int popen_pid = 0; |
|
73 |
||
74 |
FILE * |
|
75 |
popen(command, type) |
|
76 |
/* const */ char *command; |
|
77 |
/* const */ char *type; |
|
78 |
{ |
|
79 |
int pipes[2]; |
|
80 |
int itype = (strcmp(type, "w") == 0 ? 1 : 0); |
|
81 |
||
82 |
if (pipe(pipes) == -1) |
|
326 | 83 |
return NULL; |
325 | 84 |
|
85 |
switch (popen_pid = vfork()) { |
|
86 |
case -1: |
|
326 | 87 |
(void)close(pipes[0]); |
88 |
(void)close(pipes[1]); |
|
89 |
return NULL; |
|
325 | 90 |
|
91 |
case 0: |
|
326 | 92 |
if (itype) { |
93 |
dup2(pipes[0], fileno(stdin)); |
|
94 |
close(pipes[1]); |
|
95 |
} else { |
|
96 |
dup2(pipes[1], fileno(stdout)); |
|
97 |
close(pipes[0]); |
|
98 |
} |
|
99 |
execl("/bin/sh", "/bin/sh", "-c", command, 0); |
|
100 |
fprintf(stderr, "XRN Error: failed the execlp\n"); |
|
101 |
_exit(-1); |
|
102 |
/* NOTREACHED */ |
|
325 | 103 |
|
104 |
default: |
|
326 | 105 |
if (itype) { |
106 |
close(pipes[0]); |
|
107 |
return fdopen(pipes[1], "w"); |
|
108 |
} else { |
|
109 |
close(pipes[1]); |
|
110 |
return fdopen(pipes[0], "r"); |
|
111 |
} |
|
325 | 112 |
} |
113 |
} |
|
114 |
||
115 |
int |
|
116 |
pclose(str) |
|
117 |
FILE *str; |
|
118 |
{ |
|
119 |
int pd = 0; |
|
120 |
int status; |
|
121 |
int err; |
|
122 |
||
123 |
err = fclose(str); |
|
124 |
||
125 |
do { |
|
326 | 126 |
if ((pd = wait(&status)) == -1) |
127 |
{ |
|
128 |
err = EOF; |
|
129 |
break; |
|
130 |
} |
|
325 | 131 |
} while (pd != popen_pid); |
132 |
||
133 |
if (err == EOF) |
|
326 | 134 |
return -1; |
325 | 135 |
|
136 |
if (status) |
|
326 | 137 |
status >>= 8; /* exit status in high byte */ |
325 | 138 |
|
139 |
return status; |
|
140 |
} |
|
141 |
||
2925 | 142 |
#endif /* NEED_POPEN_WITH_VFORK */ |
325 | 143 |
|
144 |
%} |
|
145 |
! ! |
|
146 |
||
2266 | 147 |
!PipeStream class methodsFor:'documentation'! |
613 | 148 |
|
149 |
copyright |
|
150 |
" |
|
151 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
152 |
All Rights Reserved |
|
153 |
||
154 |
This software is furnished under a license and may be used |
|
155 |
only in accordance with the terms of that license and with the |
|
156 |
inclusion of the above copyright notice. This software may not |
|
157 |
be provided or otherwise made available to, or used by, any |
|
158 |
other person. No title to or ownership of the software is |
|
159 |
hereby transferred. |
|
160 |
" |
|
161 |
! |
|
162 |
||
163 |
documentation |
|
164 |
" |
|
165 |
Pipestreams allow reading or writing from/to a unix command. |
|
166 |
For example, to get a stream reading the output of an 'ls -l' |
|
167 |
command, a PipeStream can be created with: |
|
168 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
169 |
PipeStream readingFrom:'ls -l' |
613 | 170 |
|
171 |
the characters of the commands output can be read using the |
|
172 |
standard stream messages as next, nextLine etc. |
|
173 |
||
174 |
If a writing pipeStream is written to, after the command has finished, |
|
2966 | 175 |
UNIX will generate an error-signal (SIGPIPE), which will raise the BrokenPipeSignal. |
613 | 176 |
Thus, to handle this condition correctly, the following code is suggested: |
177 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
178 |
|p| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
179 |
p := PipeStream writingTo:'echo hello'. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
180 |
PipeStream brokenPipeSignal handle:[:ex | |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
181 |
'broken pipe' printNewline. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
182 |
p shutDown. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
183 |
ex return |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
184 |
] do:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
185 |
p nextPutLine:'oops'. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
186 |
'after write' printNewline. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
187 |
p close. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
188 |
'after close' printNewline |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
189 |
] |
613 | 190 |
|
1663 | 191 |
Notice, that iff the Stream is buffered, the Signal may occur some time after |
613 | 192 |
the write - or even at close time; to avoid a recursive signal in the exception |
2966 | 193 |
handler, a #shutDown is useful there; if you use close in the handler, this would |
194 |
try to send any buffered output to the pipe, leading to another brokenPipe exception. |
|
1295 | 195 |
|
1662
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
196 |
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
|
197 |
buggy (trying to restart the read ...) |
ce26ca3d837c
linux stdio does not work with buffered pipes
Claus Gittinger <cg@exept.de>
parents:
1648
diff
changeset
|
198 |
|
2966 | 199 |
Currently, no filtering pipeStreams (i.e. both reading AND writing) are provided. |
200 |
However, if you look at how things are setup, this can be implemented using the |
|
201 |
low level primitives #mapePipe and #executeCommand from the OS class protocol. |
|
202 |
||
1295 | 203 |
[author:] |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
204 |
Claus Gittinger |
2966 | 205 |
|
206 |
[see also:] |
|
207 |
ExternalStream FileStream Socket |
|
208 |
OperatingSystem |
|
613 | 209 |
" |
210 |
! ! |
|
211 |
||
2266 | 212 |
!PipeStream class methodsFor:'initialization'! |
2 | 213 |
|
214 |
initialize |
|
215 |
"setup the signal" |
|
216 |
||
57 | 217 |
BrokenPipeSignal isNil ifTrue:[ |
582
21f08116b28d
BrokenPipeSignal now a child of WriteErrorSignal
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
218 |
BrokenPipeSignal := WriteErrorSignal newSignalMayProceed:true. |
159 | 219 |
BrokenPipeSignal nameClass:self message:#brokenPipeSignal. |
220 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'. |
|
57 | 221 |
] |
2 | 222 |
! ! |
223 |
||
2266 | 224 |
!PipeStream class methodsFor:'instance creation'! |
2 | 225 |
|
613 | 226 |
readingFrom:commandString |
227 |
"create and return a new pipeStream which can read from the unix command |
|
228 |
given by command." |
|
2 | 229 |
|
613 | 230 |
^ (self basicNew) readingFrom:commandString |
231 |
||
2966 | 232 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
233 |
PipeStream readingFrom:'ls -l'. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
234 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
235 |
|
2966 | 236 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
237 |
p := PipeStream readingFrom:'ls -l'. |
2966 | 238 |
Transcript showCR:p nextLine. |
239 |
p close |
|
240 |
" |
|
241 |
||
242 |
" |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
243 |
|s| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
244 |
s := PipeStream readingFrom:'sh -c sleep\ 600'. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
245 |
(Delay forSeconds:2) wait. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
246 |
s shutDown |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
247 |
" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
248 |
|
2966 | 249 |
"vms: |
250 |
PipeStream readingFrom:'dir'. |
|
251 |
" |
|
252 |
||
253 |
" |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
254 |
|p| |
2966 | 255 |
p := PipeStream readingFrom:'dir'. |
256 |
Transcript showCR:p nextLine. |
|
257 |
p close |
|
258 |
" |
|
259 |
||
260 |
"msdos: |
|
261 |
PipeStream readingFrom:'dir'. |
|
262 |
" |
|
263 |
" |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
264 |
|p| |
2966 | 265 |
p := PipeStream readingFrom:'dir'. |
266 |
Transcript showCR:p nextLine. |
|
267 |
p close |
|
268 |
" |
|
269 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
270 |
"Modified: 24.4.1996 / 09:09:25 / stefan" |
613 | 271 |
! |
1 | 272 |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
273 |
readingFrom:commandString errorDisposition:handleError inDirectory:aDirectory |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
274 |
"similar to #readingFrom, but changes the directory while |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
275 |
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
|
276 |
executed in another directory, to avoid any OS dependencies |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
277 |
in your code. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
278 |
handleError may be one of #discard #inline #stderr (default), which causes |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
279 |
stderr to me discarded (/dev/null), merged into the PipeStream or written |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
280 |
to smalltalks stderr." |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
281 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
282 |
|cmd tmpComFile pipe| |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
283 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
284 |
(OperatingSystem platformName == #vms) ifTrue:[ |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
285 |
tmpComFile := OperatingSystem createCOMFileForVMSCommand:commandString in:aDirectory. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
286 |
cmd := '@' , tmpComFile osName. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
287 |
pipe := self readingFrom:cmd. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
288 |
pipe notNil ifTrue:[ |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
289 |
pipe exitAction:[tmpComFile delete]. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
290 |
]. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
291 |
^ pipe |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
292 |
]. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
293 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
294 |
"/ unix - prepend a 'cd' to the command |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
295 |
cmd := 'cd ' , aDirectory asFilename pathName, '; ' , commandString. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
296 |
handleError == #discard ifTrue:[ |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
297 |
cmd := cmd, ' 2>/dev/null'. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
298 |
] ifFalse:[handleError == #inline ifTrue:[ |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
299 |
cmd := cmd, ' 2>&1'. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
300 |
]]. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
301 |
^ self readingFrom:cmd |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
302 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
303 |
"Created: 24.9.1997 / 09:32:35 / stefan" |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
304 |
! |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
305 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
306 |
readingFrom:commandString inDirectory:aDirectory |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
307 |
"similar to #readingFrom, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
308 |
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
|
309 |
executed in another directory, to avoid any OS dependencies |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
310 |
in your code." |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
311 |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
312 |
^ self readingFrom:commandString errorDisposition:#stderr inDirectory:aDirectory |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
313 |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
314 |
"Modified: 24.9.1997 / 09:33:48 / stefan" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
315 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
316 |
|
2 | 317 |
writingTo:commandString |
1 | 318 |
"create and return a new pipeStream which can write to the unix command |
319 |
given by command." |
|
320 |
||
2 | 321 |
^ (self basicNew) writingTo:commandString |
1 | 322 |
|
2966 | 323 |
"unix: |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
324 |
PipeStream writingTo:'sort' |
2966 | 325 |
" |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
326 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
327 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
328 |
writingTo:commandString inDirectory:aDirectory |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
329 |
"similar to #writingTo, but changes the directory while |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
330 |
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
|
331 |
executed in another directory, to avoid any OS dependencies |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
332 |
in your code." |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
333 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
334 |
|cmd tmpComFile pipe| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
335 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
336 |
(OperatingSystem platformName == #vms) ifTrue:[ |
2976 | 337 |
tmpComFile := OperatingSystem createCOMFileForVMSCommand:commandString in:aDirectory. |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
338 |
cmd := '@' , tmpComFile osName. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
339 |
pipe := self writingTo:cmd. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
340 |
pipe notNil ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
341 |
pipe exitAction:[tmpComFile delete]. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
342 |
]. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
343 |
^ pipe |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
344 |
]. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
345 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
346 |
"/ unix - prepend a 'cd' to the command |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
347 |
cmd := 'cd ' , aDirectory asFilename pathName, '; ' , commandString. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
348 |
^ self writingTo:cmd |
613 | 349 |
! ! |
350 |
||
2266 | 351 |
!PipeStream class methodsFor:'Signal constants'! |
1 | 352 |
|
613 | 353 |
brokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
354 |
"return the signal used to handle SIGPIPE unix-signals. |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
355 |
Since SIGPIPE is asynchronous, we can't decide which smalltalk process |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
356 |
should handle BrokenPipeSignal. So the system doesn't raise |
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
357 |
BrokenPipeSignal for SIGPIPE any longer." |
1 | 358 |
|
613 | 359 |
^ BrokenPipeSignal |
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
360 |
|
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
361 |
"Modified: 24.9.1997 / 09:43:23 / stefan" |
1 | 362 |
! ! |
363 |
||
99 | 364 |
!PipeStream methodsFor:'accessing'! |
365 |
||
366 |
commandString |
|
367 |
"return the command string" |
|
368 |
||
369 |
^ commandString |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
370 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
371 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
372 |
exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
373 |
"return exitStatus" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
374 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
375 |
^ exitStatus |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
376 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
377 |
"Created: 28.12.1995 / 14:54:41 / stefan" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
378 |
! |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
379 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
380 |
pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
381 |
"return pid" |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
382 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
383 |
^ pid |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
384 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
385 |
"Created: 28.12.1995 / 14:54:30 / stefan" |
99 | 386 |
! ! |
387 |
||
1 | 388 |
!PipeStream methodsFor:'instance release'! |
389 |
||
390 |
closeFile |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
391 |
"low level close |
230 | 392 |
This waits for the command to finish. |
393 |
Use shutDown for a fast (nonBlocking) close." |
|
1 | 394 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
395 |
filePointer notNil ifTrue:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
396 |
super closeFile. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
397 |
filePointer := nil. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
398 |
pid notNil ifTrue:[ |
2119
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
399 |
[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
400 |
pid notNil ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
401 |
exitSema wait. |
2119
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
402 |
] |
3bb34bc533e6
care for a child to finish right in close
Claus Gittinger <cg@exept.de>
parents:
1984
diff
changeset
|
403 |
] valueUninterruptably |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
404 |
]. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
405 |
]. |
369 | 406 |
! |
407 |
||
408 |
closeFileDescriptor |
|
409 |
"alternative very low level close |
|
410 |
This closes the underlying OS-fileDescriptor |
|
411 |
- and will NOT write any buffered data to the stream. |
|
412 |
You have been warned." |
|
413 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
414 |
|action| |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
415 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
416 |
%{ |
793 | 417 |
#if !defined(transputer) && !defined(MSDOS_LIKE) |
369 | 418 |
OBJ fp; |
419 |
FILE *f; |
|
420 |
||
1133 | 421 |
if ((fp = __INST(filePointer)) != nil) { |
422 |
__INST(filePointer) = nil; |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
423 |
f = __FILEVal(fp); |
369 | 424 |
__BEGIN_INTERRUPTABLE__ |
425 |
close(fileno(f)); |
|
426 |
__END_INTERRUPTABLE__ |
|
427 |
} |
|
793 | 428 |
#endif /* not transputer && not MSDOS_LIKE */ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
429 |
%}. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
430 |
exitAction notNil ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
431 |
action := exitAction. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
432 |
exitAction := nil. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
433 |
action value. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
434 |
] |
613 | 435 |
! |
436 |
||
437 |
disposed |
|
438 |
"redefined to avoid blocking in close." |
|
439 |
||
440 |
self shutDown |
|
441 |
! |
|
442 |
||
443 |
shutDown |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
444 |
"close the Stream, ignoring any broken-pipe errors. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
445 |
Terminate the command" |
613 | 446 |
|
447 |
BrokenPipeSignal catch:[ |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
448 |
|tpid| |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
449 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
450 |
Lobby unregister:self. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
451 |
self closeFileDescriptor. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
452 |
tpid := pid. "copy pid to avoid race" |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
453 |
tpid notNil ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
454 |
"/ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
455 |
"/ Terminate both the process and group, just in case the |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
456 |
"/ operating system does not support process groups. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
457 |
"/ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
458 |
OperatingSystem terminateProcess:tpid. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
459 |
OperatingSystem terminateProcessGroup:tpid. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
460 |
pid := nil. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
461 |
]. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
462 |
|
613 | 463 |
] |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
464 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
465 |
"Modified: 23.5.1996 / 09:15:41 / stefan" |
1 | 466 |
! ! |
467 |
||
468 |
!PipeStream methodsFor:'private'! |
|
469 |
||
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
470 |
exitAction:aBlock |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
471 |
"define a block to be evaluated when the pipe is closed. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
472 |
This is only used with VMS, to remove any temporary COM file. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
473 |
(see readingFrom:inDirectory:)" |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
474 |
|
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
475 |
exitAction := aBlock |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
476 |
! |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
477 |
|
49 | 478 |
openPipeFor:aCommandString withMode:mode |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
479 |
"open a pipe to the unix command in commandString; |
255 | 480 |
mode may be 'r' or 'w'" |
1 | 481 |
|
2966 | 482 |
|blocked pipeFdArray execFdArray execFd myFd |
483 |
osType shellPath shellArgs closeFdArray mbx mbxName| |
|
1 | 484 |
|
326 | 485 |
filePointer notNil ifTrue:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
486 |
"the pipe was already open ... |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
487 |
this should (can) not happen." |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
488 |
^ self errorOpen |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
489 |
]. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
490 |
lastErrorNumber := nil. |
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
491 |
exitStatus := nil. |
2266 | 492 |
exitSema := Semaphore new name:'pipe exitSema'. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
493 |
|
2966 | 494 |
osType := OperatingSystem platformName. |
495 |
osType == #vms ifTrue:[ |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
496 |
mbx := OperatingSystem createMailBox. |
2966 | 497 |
mbx isNil ifTrue:[ |
498 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
|
499 |
^ self openError |
|
500 |
]. |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
501 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
2973 | 502 |
"/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR. |
2966 | 503 |
shellPath := ''. |
504 |
shellArgs := aCommandString. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
505 |
|
2966 | 506 |
mode = 'r' ifTrue:[ |
507 |
execFdArray := Array with:0 with:mbx with:2. |
|
508 |
] ifFalse:[ |
|
509 |
execFdArray := Array with:mbx with:1 with:2. |
|
510 |
]. |
|
511 |
closeFdArray := nil. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
512 |
] ifFalse:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
513 |
pipeFdArray := OperatingSystem makePipe. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
514 |
pipeFdArray isNil ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
515 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
516 |
^ self openError |
2966 | 517 |
]. |
255 | 518 |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
519 |
osType == #unix ifTrue:[ |
2966 | 520 |
shellPath := '/bin/sh'. |
521 |
shellArgs := Array with:'sh' with:'-c' with:aCommandString. |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
522 |
] ifFalse:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
523 |
osType == #win32 ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
524 |
shellPath := 'C:\WINNT\System32\cmd /c'. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
525 |
shellArgs := aCommandString. |
2925 | 526 |
] ifFalse:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
527 |
OperatingSystem closeFd:execFd; closeFd:myFd. |
2925 | 528 |
"/ |
529 |
"/ dont know how to do it ... |
|
530 |
"/ |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
531 |
^ self openError |
2925 | 532 |
] |
2966 | 533 |
]. |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
534 |
mode = 'r' ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
535 |
execFd := pipeFdArray at:2. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
536 |
execFdArray := Array with:0 with:execFd with:2. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
537 |
myFd := pipeFdArray at:1. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
538 |
] ifFalse:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
539 |
execFd := pipeFdArray at:1. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
540 |
execFdArray := Array with:execFd with:1 with:2. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
541 |
myFd := pipeFdArray at:2. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
542 |
]. |
2966 | 543 |
closeFdArray := Array with:myFd. |
2598 | 544 |
]. |
545 |
||
2966 | 546 |
|
2598 | 547 |
"/ must block here, to avoid races due to early finishing |
548 |
"/ subprocesses ... |
|
549 |
||
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
550 |
blocked := OperatingSystem blockInterrupts. |
2598 | 551 |
|
552 |
pid := Processor |
|
553 |
monitor:[ |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
554 |
OperatingSystem |
2598 | 555 |
exec:shellPath |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
556 |
withArguments:shellArgs |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
557 |
fileDescriptors:execFdArray |
2966 | 558 |
closeDescriptors:closeFdArray |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
559 |
fork:true |
2598 | 560 |
newPgrp:true. |
561 |
] |
|
562 |
action:[:status | |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
563 |
status stillAlive ifFalse:[ |
2598 | 564 |
exitStatus := status. |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
565 |
pid := nil. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
566 |
exitSema signal. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
567 |
]. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
568 |
]. |
2598 | 569 |
|
2966 | 570 |
(osType ~~ #vms) ifTrue:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
571 |
OperatingSystem closeFd:execFd. |
2966 | 572 |
]. |
573 |
||
2498
eb0bad72f05e
Fixe race when waiting for SIGCHLD.
Stefan Vogel <sv@exept.de>
parents:
2266
diff
changeset
|
574 |
pid notNil ifTrue:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
575 |
(osType == #win32) ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
576 |
self setFileHandle:myFd mode:mode |
2598 | 577 |
] ifFalse:[ |
2966 | 578 |
(osType == #vms) ifTrue:[ |
579 |
"/ |
|
580 |
"/ reopen the mailbox as a file ... |
|
581 |
"/ |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
582 |
mbxName := OperatingSystem mailBoxNameOf:mbx. |
2966 | 583 |
mbxName notNil ifTrue:[ |
584 |
super open:mbxName withMode:mode |
|
585 |
]. |
|
586 |
] ifFalse:[ |
|
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
587 |
self setFileDescriptor:myFd mode:mode. |
2966 | 588 |
] |
2598 | 589 |
] |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
590 |
] ifFalse:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
591 |
lastErrorNumber := OperatingSystem currentErrorNumber. |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
592 |
osType ~~ #vms ifTrue:[ |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
593 |
OperatingSystem closeFd:myFd. |
2966 | 594 |
] ifFalse:[ |
595 |
OperatingSystem destroyMailBox:mbx |
|
596 |
]. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
597 |
]. |
2598 | 598 |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
599 |
blocked ifFalse:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
600 |
OperatingSystem unblockInterrupts |
1 | 601 |
]. |
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
602 |
|
2961 | 603 |
lastErrorNumber notNil ifTrue:[ |
2969
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
604 |
" |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
605 |
the pipe open failed for some reason ... |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
606 |
... this may be either due to an invalid command string, |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
607 |
or due to the system running out of memory (when forking |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
608 |
the unix process) |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
609 |
" |
1fcf6dfb6004
VMS pipes in another directory
Claus Gittinger <cg@exept.de>
parents:
2966
diff
changeset
|
610 |
^ self openError |
255 | 611 |
]. |
2961 | 612 |
|
613 |
commandString := aCommandString. |
|
614 |
%{ |
|
615 |
/* LINUX stdio is corrupt here ... */ |
|
616 |
#ifdef BUGGY_STDIO_LIB |
|
617 |
__INST(buffered) = false; |
|
618 |
#else |
|
619 |
__INST(buffered) = true; |
|
620 |
#endif |
|
621 |
%}. |
|
2966 | 622 |
position := 1. |
2961 | 623 |
hitEOF := false. |
624 |
binary := false. |
|
625 |
Lobby register:self. |
|
1648
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
626 |
|
faa891d2c1b0
Use OperatingSystem executeCommand:... instead of popen().
Stefan Vogel <sv@exept.de>
parents:
1295
diff
changeset
|
627 |
"Modified: 23.4.1996 / 17:05:59 / stefan" |
1 | 628 |
! |
629 |
||
630 |
readingFrom:command |
|
631 |
"setup the receiver to read from command" |
|
632 |
||
326 | 633 |
mode := #readonly. didWrite := false. |
1 | 634 |
^ self openPipeFor:command withMode:'r' |
635 |
! |
|
636 |
||
637 |
writingTo:command |
|
638 |
"setup the receiver to write to command" |
|
639 |
||
326 | 640 |
mode := #writeonly. didWrite := true. |
1 | 641 |
^ self openPipeFor:command withMode:'w' |
642 |
! ! |
|
613 | 643 |
|
2266 | 644 |
!PipeStream class methodsFor:'documentation'! |
1295 | 645 |
|
646 |
version |
|
2985
eff9e86dd2f9
New method #readingFrom:errorDisposition:inDirectory.
Stefan Vogel <sv@exept.de>
parents:
2976
diff
changeset
|
647 |
^ '$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.64 1997-09-24 15:11:32 stefan Exp $' |
1295 | 648 |
! ! |
613 | 649 |
PipeStream initialize! |