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