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 readWait.
|
|
96 |
[pty atEnd] whileFalse:[
|
|
97 |
Transcript showCR:(pty nextLine).
|
|
98 |
pty readWait.
|
|
99 |
].
|
|
100 |
pty close.
|
|
101 |
] forkAt:9.
|
|
102 |
|
|
103 |
pty nextPutLine:'r Makefile'.
|
|
104 |
pty nextPutLine:'1,2d'.
|
|
105 |
pty nextPutLine:'$d'.
|
|
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 |
|
|
116 |
pty := UnixPTYStream to:'rlogin ' , OperatingSystem getHostName.
|
|
117 |
[
|
|
118 |
pty readWait.
|
|
119 |
[pty atEnd] whileFalse:[
|
|
120 |
Transcript show:(pty next).
|
|
121 |
pty readWait.
|
|
122 |
].
|
|
123 |
pty close.
|
|
124 |
] forkAt:9.
|
|
125 |
|
669
|
126 |
password := Dialog requestPassword:'password'.
|
668
|
127 |
pty nextPutLine:password.
|
|
128 |
command := Dialog request:'command'.
|
|
129 |
pty nextPutLine:command.
|
670
|
130 |
pty nextPutLine:'exit'.
|
668
|
131 |
[exEnd]
|
|
132 |
|
667
|
133 |
"
|
|
134 |
! !
|
|
135 |
|
|
136 |
!UnixPTYStream class methodsFor:'instance creation'!
|
|
137 |
|
|
138 |
to:commandString
|
|
139 |
"create and return a new ptyStream which can read/write to the unix command
|
|
140 |
given by commandString."
|
|
141 |
|
|
142 |
^ (self basicNew) to:commandString
|
|
143 |
|
|
144 |
"unix:
|
|
145 |
UnixPTYStream to:'sh'
|
|
146 |
"
|
|
147 |
|
|
148 |
"Modified: / 9.7.1998 / 18:26:31 / cg"
|
|
149 |
! !
|
|
150 |
|
|
151 |
!UnixPTYStream class methodsFor:'blocked instance creation'!
|
|
152 |
|
|
153 |
readingFrom:commandString
|
|
154 |
^ self shouldNotImplement
|
|
155 |
|
|
156 |
"Created: / 9.7.1998 / 18:25:09 / cg"
|
|
157 |
"Modified: / 9.7.1998 / 18:25:34 / cg"
|
|
158 |
!
|
|
159 |
|
|
160 |
readingFrom:commandString errorDisposition:handleError inDirectory:aDirectory
|
|
161 |
^ self shouldNotImplement
|
|
162 |
|
|
163 |
"Modified: / 9.7.1998 / 18:25:31 / cg"
|
|
164 |
!
|
|
165 |
|
|
166 |
readingFrom:commandString inDirectory:aDirectory
|
|
167 |
^ self shouldNotImplement
|
|
168 |
|
|
169 |
"Created: / 9.7.1998 / 18:25:38 / cg"
|
|
170 |
!
|
|
171 |
|
|
172 |
writingTo:commandString
|
|
173 |
^ self shouldNotImplement
|
|
174 |
|
|
175 |
"Created: / 9.7.1998 / 18:25:42 / cg"
|
|
176 |
!
|
|
177 |
|
|
178 |
writingTo:commandString inDirectory:aDirectory
|
|
179 |
^ self shouldNotImplement
|
|
180 |
|
|
181 |
"Created: / 9.7.1998 / 18:25:46 / cg"
|
|
182 |
! !
|
|
183 |
|
|
184 |
!UnixPTYStream methodsFor:'private'!
|
|
185 |
|
3990
|
186 |
openPTYFor:aCommandString withMode:mode inDirectory:aDirectory
|
667
|
187 |
"open a pty to the unix command in commandString"
|
|
188 |
|
|
189 |
|blocked ptyFdArray execFdArray slaveFd masterFd shellAndArgs
|
3990
|
190 |
shellPath shellArgs mbx mbxName env|
|
667
|
191 |
|
3906
|
192 |
handle notNil ifTrue:[
|
667
|
193 |
"the pipe was already open ...
|
|
194 |
this should (can) not happen."
|
3990
|
195 |
^ self errorAlreadyOpen
|
667
|
196 |
].
|
3906
|
197 |
|
667
|
198 |
lastErrorNumber := nil.
|
|
199 |
exitStatus := nil.
|
|
200 |
exitSema := Semaphore new name:'pty exitSema'.
|
3906
|
201 |
|
687
|
202 |
OperatingSystem isVMSlike ifTrue:[
|
667
|
203 |
mbx := OperatingSystem createMailBox.
|
|
204 |
mbx isNil ifTrue:[
|
|
205 |
lastErrorNumber := OperatingSystem currentErrorNumber.
|
|
206 |
^ self openError
|
|
207 |
].
|
|
208 |
mbxName := OperatingSystem mailBoxNameOf:mbx.
|
|
209 |
"/ 'mailBox is ' print. mbx print. ' name is ' print. mbxName printCR.
|
|
210 |
|
|
211 |
shellPath := ''.
|
|
212 |
shellArgs := aCommandString.
|
|
213 |
|
|
214 |
execFdArray := Array with:mbx with:mbx with:mbx.
|
|
215 |
] ifFalse:[
|
|
216 |
ptyFdArray := OperatingSystem makePTYPair.
|
|
217 |
ptyFdArray isNil ifTrue:[
|
|
218 |
lastErrorNumber := OperatingSystem currentErrorNumber.
|
|
219 |
^ self openError
|
|
220 |
].
|
|
221 |
|
|
222 |
shellAndArgs := OperatingSystem commandAndArgsForOSCommand:aCommandString.
|
|
223 |
shellPath := shellAndArgs at:1.
|
|
224 |
shellArgs := shellAndArgs at:2.
|
|
225 |
|
|
226 |
masterFd := ptyFdArray at:1.
|
|
227 |
slaveFd := ptyFdArray at:2.
|
|
228 |
execFdArray := Array with:slaveFd with:slaveFd with:slaveFd.
|
|
229 |
].
|
|
230 |
|
|
231 |
env := Dictionary new.
|
3990
|
232 |
env at:'TERM' put:'dumb'.
|
667
|
233 |
env at:'SHELL' put:shellPath.
|
|
234 |
|
|
235 |
"/ must block here, to avoid races due to early finishing
|
|
236 |
"/ subprocesses ...
|
|
237 |
|
|
238 |
blocked := OperatingSystem blockInterrupts.
|
|
239 |
|
|
240 |
pid := Processor
|
|
241 |
monitor:[
|
|
242 |
OperatingSystem
|
|
243 |
exec:shellPath
|
|
244 |
withArguments:shellArgs
|
|
245 |
environment:env
|
|
246 |
fileDescriptors:execFdArray
|
|
247 |
fork:true
|
|
248 |
newPgrp:true
|
3990
|
249 |
inDirectory:aDirectory.
|
667
|
250 |
]
|
|
251 |
action:[:status |
|
|
252 |
status stillAlive ifFalse:[
|
|
253 |
exitStatus := status.
|
|
254 |
OperatingSystem closePid:pid.
|
|
255 |
pid := nil.
|
|
256 |
exitSema signal.
|
|
257 |
].
|
|
258 |
].
|
|
259 |
|
3906
|
260 |
(OperatingSystem isVMSlike) ifFalse:[
|
667
|
261 |
OperatingSystem closeFd:slaveFd.
|
|
262 |
].
|
|
263 |
|
|
264 |
pid notNil ifTrue:[
|
3990
|
265 |
(OperatingSystem isVMSlike) ifTrue:[
|
|
266 |
"/
|
|
267 |
"/ reopen the mailbox as a file ...
|
|
268 |
"/
|
|
269 |
mbxName := OperatingSystem mailBoxNameOf:mbx.
|
|
270 |
mbxName notNil ifTrue:[
|
|
271 |
super open:mbxName withMode:mode
|
|
272 |
].
|
667
|
273 |
] ifFalse:[
|
3990
|
274 |
"/ (OperatingSystem isMSWINDOWSlike) ifTrue:[
|
|
275 |
"/ self setFileHandle:masterFd mode:mode
|
|
276 |
"/ ] ifFalse:[
|
|
277 |
|
|
278 |
self setFileDescriptor:masterFd mode:mode.
|
667
|
279 |
]
|
|
280 |
] ifFalse:[
|
|
281 |
lastErrorNumber := OperatingSystem currentErrorNumber.
|
3906
|
282 |
OperatingSystem isVMSlike ifTrue:[
|
667
|
283 |
OperatingSystem destroyMailBox:mbx
|
|
284 |
] ifFalse:[
|
|
285 |
OperatingSystem closeFd:masterFd.
|
|
286 |
].
|
|
287 |
].
|
|
288 |
|
|
289 |
blocked ifFalse:[
|
|
290 |
OperatingSystem unblockInterrupts
|
|
291 |
].
|
|
292 |
|
|
293 |
lastErrorNumber notNil ifTrue:[
|
|
294 |
"
|
|
295 |
the pipe open failed for some reason ...
|
|
296 |
... this may be either due to an invalid command string,
|
|
297 |
or due to the system running out of memory (when forking
|
|
298 |
the unix process)
|
|
299 |
"
|
|
300 |
^ self openError
|
|
301 |
].
|
|
302 |
|
|
303 |
commandString := aCommandString.
|
|
304 |
buffered := false.
|
|
305 |
|
3990
|
306 |
position := 0.
|
667
|
307 |
hitEOF := false.
|
|
308 |
binary := false.
|
|
309 |
Lobby register:self.
|
|
310 |
|
|
311 |
"Created: / 9.7.1998 / 20:21:42 / cg"
|
|
312 |
"Modified: / 9.7.1998 / 20:28:31 / cg"
|
|
313 |
!
|
|
314 |
|
|
315 |
to:command
|
|
316 |
"setup the receiver to read/write to command"
|
|
317 |
|
|
318 |
mode := #readwrite. didWrite := true.
|
|
319 |
^ self openPTYFor:command withMode:ReadWriteMode inDirectory:nil
|
|
320 |
|
|
321 |
"Created: / 9.7.1998 / 18:27:40 / cg"
|
|
322 |
"Modified: / 9.7.1998 / 20:22:39 / cg"
|
|
323 |
! !
|
|
324 |
|
|
325 |
!UnixPTYStream methodsFor:'testing'!
|
|
326 |
|
|
327 |
atEnd
|
|
328 |
ReadErrorSignal handle:[:ex |
|
|
329 |
ex return
|
|
330 |
] do:[
|
|
331 |
^ super atEnd.
|
|
332 |
].
|
|
333 |
^ true
|
|
334 |
|
|
335 |
"Created: / 9.7.1998 / 20:29:03 / cg"
|
|
336 |
"Modified: / 9.7.1998 / 20:29:48 / cg"
|
|
337 |
! !
|
|
338 |
|
|
339 |
!UnixPTYStream class methodsFor:'documentation'!
|
|
340 |
|
|
341 |
version
|
3906
|
342 |
^ '$Header$'
|
667
|
343 |
! !
|
3906
|
344 |
|