4245
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
667
|
3 |
"
|
|
4 |
COPYRIGHT (c) 1998 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
3906
|
14 |
"{ Package: 'stx:libbasic2' }"
|
667
|
15 |
|
3906
|
16 |
"{ NameSpace: Smalltalk }"
|
667
|
17 |
|
|
18 |
PipeStream subclass:#UnixPTYStream
|
|
19 |
instanceVariableNames:''
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'OS-Unix'
|
|
23 |
!
|
|
24 |
|
|
25 |
!UnixPTYStream class methodsFor:'documentation'!
|
|
26 |
|
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 1998 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
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 |
|
|
42 |
documentation
|
|
43 |
"
|
|
44 |
These are much like PipeStreams, but allow bi-directional communication
|
|
45 |
with a Unix command. (i.e. everything written to the PTYStream is seen
|
|
46 |
by the commands standard-input, everything written by the command to its
|
|
47 |
stdErr or stdOut can be read from me.
|
|
48 |
|
|
49 |
In addition, sending control characters (such as INTR or QUIT),
|
|
50 |
will be handled by the command as a signal (unless the command changed
|
|
51 |
its standard input to raw mode).
|
|
52 |
|
|
53 |
[author:]
|
|
54 |
Claus Gittinger
|
|
55 |
|
|
56 |
[see also:]
|
|
57 |
TerminalView
|
|
58 |
PipeStream ExternalStream FileStream Socket
|
|
59 |
OperatingSystem
|
|
60 |
"
|
|
61 |
|
|
62 |
!
|
|
63 |
|
|
64 |
examples
|
|
65 |
"
|
|
66 |
that one is not special (could be done with a PipeStream):
|
|
67 |
[exBegin]
|
|
68 |
|pty|
|
|
69 |
|
|
70 |
pty := UnixPTYStream to:'ls -l'.
|
|
71 |
[pty atEnd] whileFalse:[
|
|
72 |
Transcript showCR:(pty nextLine).
|
|
73 |
].
|
|
74 |
pty close.
|
|
75 |
[exEnd]
|
668
|
76 |
|
|
77 |
|
|
78 |
prove (done with a PipeStream):
|
667
|
79 |
[exBegin]
|
|
80 |
|pty|
|
|
81 |
|
|
82 |
pty := PipeStream readingFrom:'ls -l'.
|
|
83 |
[pty atEnd] whileFalse:[
|
|
84 |
Transcript showCR:(pty nextLine).
|
|
85 |
].
|
|
86 |
pty close.
|
|
87 |
[exEnd]
|
668
|
88 |
|
667
|
89 |
|
668
|
90 |
but that one is not possible with a PipeStream
|
|
91 |
(simulating an editor session):
|
667
|
92 |
[exBegin]
|
|
93 |
|pty|
|
|
94 |
|
|
95 |
pty := UnixPTYStream to:'ed'.
|
|
96 |
[
|
|
97 |
[pty atEnd] whileFalse:[
|
|
98 |
Transcript showCR:(pty nextLine).
|
|
99 |
].
|
|
100 |
pty close.
|
|
101 |
] forkAt:9.
|
|
102 |
|
3991
|
103 |
pty nextPutLine:'r Make.proto'.
|
667
|
104 |
pty nextPutLine:'1,2d'.
|
3991
|
105 |
pty nextPutLine:'1,$s/#/+++++++/'.
|
667
|
106 |
pty nextPutLine:'w xxx'.
|
|
107 |
pty nextPutLine:'q'.
|
|
108 |
[exEnd]
|
|
109 |
|
668
|
110 |
|
|
111 |
and that one is even better ...
|
|
112 |
(simulating a login session):
|
|
113 |
[exBegin]
|
|
114 |
|pty password command|
|
|
115 |
|
3991
|
116 |
pty := UnixPTYStream to:'ssh 127.0.0.1'.
|
668
|
117 |
[
|
|
118 |
[pty atEnd] whileFalse:[
|
|
119 |
Transcript show:(pty next).
|
|
120 |
].
|
|
121 |
pty close.
|
|
122 |
] forkAt:9.
|
|
123 |
|
669
|
124 |
password := Dialog requestPassword:'password'.
|
668
|
125 |
pty nextPutLine:password.
|
|
126 |
command := Dialog request:'command'.
|
|
127 |
pty nextPutLine:command.
|
670
|
128 |
pty nextPutLine:'exit'.
|
668
|
129 |
[exEnd]
|
|
130 |
|
667
|
131 |
"
|
|
132 |
! !
|
|
133 |
|
|
134 |
!UnixPTYStream class methodsFor:'instance creation'!
|
|
135 |
|
|
136 |
to:commandString
|
|
137 |
"create and return a new ptyStream which can read/write to the unix command
|
|
138 |
given by commandString."
|
|
139 |
|
|
140 |
^ (self basicNew) to:commandString
|
|
141 |
|
|
142 |
"unix:
|
|
143 |
UnixPTYStream to:'sh'
|
|
144 |
"
|
|
145 |
|
|
146 |
"Modified: / 9.7.1998 / 18:26:31 / cg"
|
|
147 |
! !
|
|
148 |
|
|
149 |
!UnixPTYStream class methodsFor:'blocked instance creation'!
|
|
150 |
|
|
151 |
readingFrom:commandString
|
|
152 |
^ self shouldNotImplement
|
|
153 |
|
|
154 |
"Created: / 9.7.1998 / 18:25:09 / cg"
|
|
155 |
"Modified: / 9.7.1998 / 18:25:34 / cg"
|
|
156 |
!
|
|
157 |
|
|
158 |
readingFrom:commandString errorDisposition:handleError inDirectory:aDirectory
|
|
159 |
^ self shouldNotImplement
|
|
160 |
|
|
161 |
"Modified: / 9.7.1998 / 18:25:31 / cg"
|
|
162 |
!
|
|
163 |
|
|
164 |
readingFrom:commandString inDirectory:aDirectory
|
|
165 |
^ self shouldNotImplement
|
|
166 |
|
|
167 |
"Created: / 9.7.1998 / 18:25:38 / cg"
|
|
168 |
!
|
|
169 |
|
|
170 |
writingTo:commandString
|
|
171 |
^ self shouldNotImplement
|
|
172 |
|
|
173 |
"Created: / 9.7.1998 / 18:25:42 / cg"
|
|
174 |
!
|
|
175 |
|
|
176 |
writingTo:commandString inDirectory:aDirectory
|
|
177 |
^ self shouldNotImplement
|
|
178 |
|
|
179 |
"Created: / 9.7.1998 / 18:25:46 / cg"
|
|
180 |
! !
|
|
181 |
|
|
182 |
!UnixPTYStream methodsFor:'private'!
|
|
183 |
|
3991
|
184 |
openPTYFor:aCommandString withMode:openMode inDirectory:aDirectory
|
667
|
185 |
"open a pty to the unix command in commandString"
|
|
186 |
|
4245
|
187 |
|ptyFdArray slaveFd masterFd env remotePipeEnd result|
|
667
|
188 |
|
3906
|
189 |
handle notNil ifTrue:[
|
667
|
190 |
"the pipe was already open ...
|
|
191 |
this should (can) not happen."
|
3990
|
192 |
^ self errorAlreadyOpen
|
667
|
193 |
].
|
3906
|
194 |
|
667
|
195 |
lastErrorNumber := nil.
|
4245
|
196 |
"stdio lib does not work with blocking pipes and interrupts
|
|
197 |
for WIN, Linux, Solaris and probably any other UNIX"
|
|
198 |
buffered := false.
|
|
199 |
hitEOF := false.
|
|
200 |
binary := false.
|
3906
|
201 |
|
4245
|
202 |
ptyFdArray := OperatingSystem makePTYPair.
|
|
203 |
ptyFdArray isNil ifTrue:[
|
|
204 |
lastErrorNumber := OperatingSystem lastErrorNumber.
|
|
205 |
^ self openError:lastErrorNumber.
|
|
206 |
].
|
667
|
207 |
|
4245
|
208 |
masterFd := ptyFdArray at:1.
|
|
209 |
slaveFd := ptyFdArray at:2.
|
667
|
210 |
|
4245
|
211 |
remotePipeEnd := self class forFileDescriptor:slaveFd mode:#readwrite buffered:false handleType:#pipeFilePointer.
|
667
|
212 |
|
|
213 |
env := Dictionary new.
|
3990
|
214 |
env at:'TERM' put:'dumb'.
|
4245
|
215 |
env at:'SHELL' put:'/bin/sh'.
|
667
|
216 |
|
4245
|
217 |
osProcess := OSProcess new
|
4558
|
218 |
command:aCommandString directory:aDirectory;
|
4245
|
219 |
environment:env;
|
|
220 |
inStream:remotePipeEnd;
|
|
221 |
outStream:remotePipeEnd;
|
|
222 |
errorStream:remotePipeEnd.
|
667
|
223 |
|
4245
|
224 |
result := osProcess startProcess.
|
|
225 |
|
|
226 |
remotePipeEnd notNil ifTrue:[
|
|
227 |
remotePipeEnd close.
|
667
|
228 |
].
|
|
229 |
|
4245
|
230 |
result ifTrue:[
|
|
231 |
self setFileHandle:masterFd mode:openMode.
|
667
|
232 |
] ifFalse:[
|
4245
|
233 |
"the pipe open failed for some reason ...
|
|
234 |
... this may be either due to an invalid command string,
|
|
235 |
or due to the system running out of memory (when forking
|
|
236 |
the unix process)"
|
|
237 |
lastErrorNumber := OperatingSystem lastErrorNumber.
|
|
238 |
OperatingSystem closeFd:masterFd.
|
|
239 |
^ self openError:lastErrorNumber.
|
667
|
240 |
].
|
|
241 |
|
4245
|
242 |
self registerForFinalization.
|
667
|
243 |
|
|
244 |
"Created: / 9.7.1998 / 20:21:42 / cg"
|
|
245 |
"Modified: / 9.7.1998 / 20:28:31 / cg"
|
|
246 |
!
|
|
247 |
|
|
248 |
to:command
|
|
249 |
"setup the receiver to read/write to command"
|
|
250 |
|
|
251 |
mode := #readwrite. didWrite := true.
|
|
252 |
^ self openPTYFor:command withMode:ReadWriteMode inDirectory:nil
|
|
253 |
|
|
254 |
"Created: / 9.7.1998 / 18:27:40 / cg"
|
|
255 |
"Modified: / 9.7.1998 / 20:22:39 / cg"
|
|
256 |
! !
|
|
257 |
|
|
258 |
!UnixPTYStream methodsFor:'testing'!
|
|
259 |
|
|
260 |
atEnd
|
|
261 |
ReadErrorSignal handle:[:ex |
|
|
262 |
ex return
|
|
263 |
] do:[
|
|
264 |
^ super atEnd.
|
|
265 |
].
|
|
266 |
^ true
|
|
267 |
|
|
268 |
"Created: / 9.7.1998 / 20:29:03 / cg"
|
|
269 |
"Modified: / 9.7.1998 / 20:29:48 / cg"
|
|
270 |
! !
|
|
271 |
|
|
272 |
!UnixPTYStream class methodsFor:'documentation'!
|
|
273 |
|
|
274 |
version
|
3906
|
275 |
^ '$Header$'
|
3991
|
276 |
!
|
|
277 |
|
|
278 |
version_CVS
|
|
279 |
^ '$Header$'
|
667
|
280 |
! !
|
3906
|
281 |
|