1
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
3 |
All Rights Reserved
|
|
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 |
|
|
13 |
NonPositionableExternalStream subclass:#PipeStream
|
49
|
14 |
instanceVariableNames:'commandString'
|
57
|
15 |
classVariableNames:'BrokenPipeSignal'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams-External'
|
|
18 |
!
|
|
19 |
|
|
20 |
PipeStream comment:'
|
5
|
21 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
22 |
All Rights Reserved
|
|
23 |
'!
|
|
24 |
|
88
|
25 |
!PipeStream class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
|
30 |
All Rights Reserved
|
1
|
31 |
|
88
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
|
41 |
version
|
|
42 |
"
|
|
43 |
$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.11 1994-06-02 16:21:16 claus Exp $
|
|
44 |
"
|
|
45 |
!
|
2
|
46 |
|
|
47 |
documentation
|
|
48 |
"
|
|
49 |
Pipestreams allow reading or writing from/to a unix command.
|
|
50 |
For example, to get a stream reading the output of an 'ls -l'
|
|
51 |
command, a PipeStream can be created with:
|
|
52 |
|
|
53 |
PipeStream readingFrom:'ls -l'
|
|
54 |
|
|
55 |
the characters of the commands output can be read using the
|
|
56 |
standard stream messages as next, nextLine etc.
|
|
57 |
|
|
58 |
If a writing pipeStream is written to, after the command has finished,
|
57
|
59 |
UNIX will generate an error-signal (SIGPIPE), which will raise the
|
|
60 |
BrokenPipeSignal.
|
|
61 |
Thus, to handle this condition correctly, the following code is suggested:
|
2
|
62 |
|
57
|
63 |
|p|
|
|
64 |
p := PipeStream writingTo:'echo hello'.
|
|
65 |
PipeStream brokenPipeSignal handle:[:ex |
|
|
66 |
'broken pipe' printNewline.
|
|
67 |
p shutDown.
|
|
68 |
ex return
|
2
|
69 |
] do:[
|
57
|
70 |
p nextPutLine:'oops'.
|
|
71 |
'after write' printNewline.
|
|
72 |
p close.
|
|
73 |
'after close' printNewline
|
2
|
74 |
]
|
57
|
75 |
|
|
76 |
Notice, that if the Stream is buffered, the Signal may occur some time after
|
|
77 |
the write - or even at close time; to avoid a recursive signal in the exception
|
|
78 |
handler, a shutDown is useful there.
|
2
|
79 |
"
|
|
80 |
! !
|
|
81 |
|
88
|
82 |
%{
|
|
83 |
#include <stdio.h>
|
|
84 |
#include <errno.h>
|
|
85 |
#ifndef transputer
|
|
86 |
# include <sys/types.h>
|
|
87 |
# include <sys/stat.h>
|
|
88 |
#endif
|
|
89 |
%}
|
|
90 |
|
2
|
91 |
!PipeStream class methodsFor:'initialization'!
|
|
92 |
|
|
93 |
initialize
|
|
94 |
"setup the signal"
|
|
95 |
|
57
|
96 |
BrokenPipeSignal isNil ifTrue:[
|
|
97 |
BrokenPipeSignal := (Signal new) mayProceed:true.
|
|
98 |
BrokenPipeSignal notifierString:'write on a pipe with no one to read'.
|
|
99 |
]
|
2
|
100 |
! !
|
|
101 |
|
|
102 |
!PipeStream class methodsFor:'signal access'!
|
|
103 |
|
57
|
104 |
brokenPipeSignal
|
2
|
105 |
"return the signal used to handle SIGPIPE unix-signals"
|
|
106 |
|
57
|
107 |
^ BrokenPipeSignal
|
2
|
108 |
! !
|
|
109 |
|
1
|
110 |
!PipeStream class methodsFor:'instance creation'!
|
|
111 |
|
2
|
112 |
writingTo:commandString
|
1
|
113 |
"create and return a new pipeStream which can write to the unix command
|
|
114 |
given by command."
|
|
115 |
|
2
|
116 |
^ (self basicNew) writingTo:commandString
|
1
|
117 |
|
|
118 |
"PipeStream writingTo:'sort'"
|
|
119 |
!
|
|
120 |
|
2
|
121 |
readingFrom:commandString
|
1
|
122 |
"create and return a new pipeStream which can read from the unix command
|
|
123 |
given by command."
|
|
124 |
|
2
|
125 |
^ (self basicNew) readingFrom:commandString
|
1
|
126 |
|
2
|
127 |
"PipeStream readingFrom:'ls -l'"
|
1
|
128 |
! !
|
|
129 |
|
|
130 |
!PipeStream methodsFor:'instance release'!
|
|
131 |
|
57
|
132 |
shutDown
|
|
133 |
"close the Stream, ignoring any broken-pipe errors"
|
|
134 |
|
|
135 |
BrokenPipeSignal catch:[self closeFile]
|
|
136 |
!
|
|
137 |
|
1
|
138 |
closeFile
|
|
139 |
"low level close - redefined since we close a pipe here"
|
|
140 |
|
13
|
141 |
%{ /* UNLIMITEDSTACK */
|
1
|
142 |
#ifndef transputer
|
13
|
143 |
extern int _immediateInterrupt;
|
|
144 |
int savInt;
|
|
145 |
|
|
146 |
if (_INST(filePointer) != nil) {
|
|
147 |
savInt = _immediateInterrupt;
|
57
|
148 |
/*
|
|
149 |
* allow interrupt even when blocking here ...
|
|
150 |
*/
|
13
|
151 |
_immediateInterrupt = 1;
|
|
152 |
pclose(MKFD(_INST(filePointer)));
|
|
153 |
_immediateInterrupt = savInt;
|
|
154 |
}
|
1
|
155 |
#endif
|
|
156 |
%}
|
|
157 |
! !
|
|
158 |
|
|
159 |
!PipeStream methodsFor:'private'!
|
|
160 |
|
49
|
161 |
openPipeFor:aCommandString withMode:mode
|
|
162 |
"open a pipe to the unix command in aCcommandString; mode may be 'r' or 'w'"
|
1
|
163 |
|
|
164 |
|retVal|
|
|
165 |
|
13
|
166 |
%{ /* STACK: 32000 */
|
1
|
167 |
#ifndef transputer
|
|
168 |
{
|
|
169 |
FILE *f;
|
|
170 |
extern errno;
|
2
|
171 |
extern int _immediateInterrupt;
|
49
|
172 |
int savInt;
|
1
|
173 |
|
57
|
174 |
if (__isString(aCommandString) && __isString(mode)) {
|
49
|
175 |
savInt = _immediateInterrupt;
|
2
|
176 |
_immediateInterrupt = 1;
|
49
|
177 |
do {
|
13
|
178 |
#ifdef LINUX
|
49
|
179 |
/* LINUX returns a non-NULL f even when interrupted */
|
|
180 |
errno = 0;
|
|
181 |
f = (FILE *)popen((char *) _stringVal(aCommandString),
|
|
182 |
(char *) _stringVal(mode));
|
|
183 |
if (errno == EINTR)
|
|
184 |
f = NULL;
|
13
|
185 |
#else
|
49
|
186 |
f = (FILE *)popen((char *) _stringVal(aCommandString),
|
10
|
187 |
(char *) _stringVal(mode));
|
13
|
188 |
#endif
|
49
|
189 |
} while ((f == NULL) && (errno == EINTR));
|
10
|
190 |
_immediateInterrupt = savInt;
|
1
|
191 |
if (f == NULL) {
|
85
|
192 |
ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
|
1
|
193 |
} else {
|
|
194 |
_INST(filePointer) = MKOBJ(f);
|
|
195 |
retVal = self;
|
|
196 |
}
|
|
197 |
}
|
|
198 |
}
|
|
199 |
#endif
|
|
200 |
%}
|
|
201 |
.
|
|
202 |
retVal notNil ifTrue:[
|
49
|
203 |
commandString := aCommandString.
|
|
204 |
buffered := true.
|
32
|
205 |
Lobby register:self
|
1
|
206 |
].
|
|
207 |
^ retVal
|
|
208 |
!
|
|
209 |
|
|
210 |
readingFrom:command
|
|
211 |
"setup the receiver to read from command"
|
|
212 |
|
|
213 |
self readonly.
|
|
214 |
^ self openPipeFor:command withMode:'r'
|
|
215 |
!
|
|
216 |
|
|
217 |
writingTo:command
|
|
218 |
"setup the receiver to write to command"
|
|
219 |
|
|
220 |
self writeonly.
|
|
221 |
^ self openPipeFor:command withMode:'w'
|
|
222 |
! !
|