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