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'
|
2
|
15 |
classVariableNames:'PipeSignal'
|
1
|
16 |
poolDictionaries:''
|
|
17 |
category:'Streams-External'
|
|
18 |
!
|
|
19 |
|
|
20 |
PipeStream comment:'
|
|
21 |
|
5
|
22 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
1
|
23 |
All Rights Reserved
|
|
24 |
|
49
|
25 |
$Header: /cvs/stx/stx/libbasic/PipeStream.st,v 1.8 1994-02-05 12:22:43 claus Exp $
|
1
|
26 |
'!
|
|
27 |
|
|
28 |
%{
|
|
29 |
#include <stdio.h>
|
10
|
30 |
#include <errno.h>
|
1
|
31 |
#ifndef transputer
|
|
32 |
# include <sys/types.h>
|
|
33 |
# include <sys/stat.h>
|
|
34 |
#endif
|
|
35 |
%}
|
|
36 |
|
2
|
37 |
!PipeStream class methodsFor:'documentation'!
|
|
38 |
|
|
39 |
documentation
|
|
40 |
"
|
|
41 |
Pipestreams allow reading or writing from/to a unix command.
|
|
42 |
For example, to get a stream reading the output of an 'ls -l'
|
|
43 |
command, a PipeStream can be created with:
|
|
44 |
|
|
45 |
PipeStream readingFrom:'ls -l'
|
|
46 |
|
|
47 |
the characters of the commands output can be read using the
|
|
48 |
standard stream messages as next, nextLine etc.
|
|
49 |
|
|
50 |
If a writing pipeStream is written to, after the command has finished,
|
|
51 |
UNIX will generate an error-signal (SIGPIPE), which will raise a
|
|
52 |
PipeSignal. Thus, to handle this condition correctly, the following
|
|
53 |
code is suggested:
|
|
54 |
|
|
55 |
p := PipeStream writingTo:' ... '.
|
|
56 |
PipeString pipeSignal handle:[:ex |
|
|
57 |
....
|
|
58 |
any cleanup action
|
|
59 |
] do:[
|
|
60 |
p nextPutLine:' ... '
|
|
61 |
]
|
|
62 |
"
|
|
63 |
! !
|
|
64 |
|
|
65 |
!PipeStream class methodsFor:'initialization'!
|
|
66 |
|
|
67 |
initialize
|
|
68 |
"setup the signal"
|
|
69 |
|
|
70 |
PipeSignal := (Signal new) mayProceed:true.
|
|
71 |
PipeSignal notifierString:'write on a pipe with no one to read'.
|
|
72 |
! !
|
|
73 |
|
|
74 |
!PipeStream class methodsFor:'signal access'!
|
|
75 |
|
|
76 |
pipeSignal
|
|
77 |
"return the signal used to handle SIGPIPE unix-signals"
|
|
78 |
|
|
79 |
^ PipeSignal
|
|
80 |
! !
|
|
81 |
|
1
|
82 |
!PipeStream class methodsFor:'instance creation'!
|
|
83 |
|
2
|
84 |
writingTo:commandString
|
1
|
85 |
"create and return a new pipeStream which can write to the unix command
|
|
86 |
given by command."
|
|
87 |
|
2
|
88 |
^ (self basicNew) writingTo:commandString
|
1
|
89 |
|
|
90 |
"PipeStream writingTo:'sort'"
|
|
91 |
!
|
|
92 |
|
2
|
93 |
readingFrom:commandString
|
1
|
94 |
"create and return a new pipeStream which can read from the unix command
|
|
95 |
given by command."
|
|
96 |
|
2
|
97 |
^ (self basicNew) readingFrom:commandString
|
1
|
98 |
|
2
|
99 |
"PipeStream readingFrom:'ls -l'"
|
1
|
100 |
! !
|
|
101 |
|
|
102 |
!PipeStream methodsFor:'instance release'!
|
|
103 |
|
|
104 |
closeFile
|
|
105 |
"low level close - redefined since we close a pipe here"
|
|
106 |
|
13
|
107 |
%{ /* UNLIMITEDSTACK */
|
1
|
108 |
#ifndef transputer
|
13
|
109 |
extern int _immediateInterrupt;
|
|
110 |
int savInt;
|
|
111 |
|
|
112 |
if (_INST(filePointer) != nil) {
|
|
113 |
savInt = _immediateInterrupt;
|
|
114 |
_immediateInterrupt = 1;
|
|
115 |
pclose(MKFD(_INST(filePointer)));
|
|
116 |
_immediateInterrupt = savInt;
|
|
117 |
}
|
1
|
118 |
#endif
|
|
119 |
%}
|
|
120 |
! !
|
|
121 |
|
|
122 |
!PipeStream methodsFor:'private'!
|
|
123 |
|
49
|
124 |
openPipeFor:aCommandString withMode:mode
|
|
125 |
"open a pipe to the unix command in aCcommandString; mode may be 'r' or 'w'"
|
1
|
126 |
|
|
127 |
|retVal|
|
|
128 |
|
13
|
129 |
%{ /* STACK: 32000 */
|
1
|
130 |
#ifndef transputer
|
|
131 |
{
|
|
132 |
FILE *f;
|
|
133 |
extern OBJ ErrorNumber;
|
|
134 |
extern errno;
|
2
|
135 |
extern int _immediateInterrupt;
|
49
|
136 |
int savInt;
|
1
|
137 |
|
49
|
138 |
if (_isString(aCommandString) && _isString(mode)) {
|
|
139 |
savInt = _immediateInterrupt;
|
2
|
140 |
_immediateInterrupt = 1;
|
49
|
141 |
do {
|
13
|
142 |
#ifdef LINUX
|
49
|
143 |
/* LINUX returns a non-NULL f even when interrupted */
|
|
144 |
errno = 0;
|
|
145 |
f = (FILE *)popen((char *) _stringVal(aCommandString),
|
|
146 |
(char *) _stringVal(mode));
|
|
147 |
if (errno == EINTR)
|
|
148 |
f = NULL;
|
13
|
149 |
#else
|
49
|
150 |
f = (FILE *)popen((char *) _stringVal(aCommandString),
|
10
|
151 |
(char *) _stringVal(mode));
|
13
|
152 |
#endif
|
49
|
153 |
} while ((f == NULL) && (errno == EINTR));
|
10
|
154 |
_immediateInterrupt = savInt;
|
1
|
155 |
if (f == NULL) {
|
|
156 |
ErrorNumber = _MKSMALLINT(errno);
|
|
157 |
} else {
|
|
158 |
_INST(filePointer) = MKOBJ(f);
|
|
159 |
retVal = self;
|
|
160 |
}
|
|
161 |
}
|
|
162 |
}
|
|
163 |
#endif
|
|
164 |
%}
|
|
165 |
.
|
|
166 |
retVal notNil ifTrue:[
|
49
|
167 |
commandString := aCommandString.
|
|
168 |
buffered := true.
|
32
|
169 |
Lobby register:self
|
1
|
170 |
].
|
|
171 |
^ retVal
|
|
172 |
!
|
|
173 |
|
|
174 |
readingFrom:command
|
|
175 |
"setup the receiver to read from command"
|
|
176 |
|
|
177 |
self readonly.
|
|
178 |
^ self openPipeFor:command withMode:'r'
|
|
179 |
!
|
|
180 |
|
|
181 |
writingTo:command
|
|
182 |
"setup the receiver to write to command"
|
|
183 |
|
|
184 |
self writeonly.
|
|
185 |
^ self openPipeFor:command withMode:'w'
|
|
186 |
! !
|