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