3028
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
Object subclass:#TerminalSession
|
|
4 |
instanceVariableNames:'inStream outStream errStream readerProcess shellPid shellCommand
|
|
5 |
shellDirectory readerDelay pluggableCheckBeforeReadAction
|
3038
|
6 |
pluggableProcessInputAction execFDArray stxToStdinPipe
|
3040
|
7 |
stdOutToStxPipe pty ptyName'
|
3028
|
8 |
classVariableNames:'Debug'
|
|
9 |
poolDictionaries:''
|
|
10 |
category:'Views-TerminalViews'
|
|
11 |
!
|
|
12 |
|
|
13 |
!TerminalSession class methodsFor:'documentation'!
|
|
14 |
|
|
15 |
documentation
|
|
16 |
"
|
|
17 |
This keeps the state and API to interact with another program
|
|
18 |
via a terminal session. Under Unix, a pseudo-tty connection
|
|
19 |
is used; other operating systems might use other mechanisms.
|
|
20 |
This is (currently) mostly used by the TerminalView application,
|
|
21 |
but can be used whereever more control is needed than a simple pipe
|
|
22 |
offers (such as terminal emulation, window size, CTRL-c support etc.)
|
|
23 |
|
|
24 |
Extracted from TerminalView, which is refactored, once this is stable.
|
|
25 |
|
|
26 |
"
|
|
27 |
! !
|
|
28 |
|
|
29 |
!TerminalSession class methodsFor:'initialization'!
|
|
30 |
|
|
31 |
initialize
|
|
32 |
|
|
33 |
Debug := false.
|
|
34 |
|
|
35 |
"
|
|
36 |
self initialize
|
|
37 |
"
|
|
38 |
! !
|
|
39 |
|
|
40 |
!TerminalSession methodsFor:'accessing'!
|
|
41 |
|
|
42 |
errStream
|
|
43 |
^ errStream
|
|
44 |
!
|
|
45 |
|
|
46 |
inStream
|
|
47 |
^ inStream
|
|
48 |
!
|
|
49 |
|
|
50 |
outStream
|
|
51 |
^ outStream
|
|
52 |
!
|
|
53 |
|
|
54 |
pluggableCheckBeforeReadAction:something
|
|
55 |
pluggableCheckBeforeReadAction := something.
|
|
56 |
!
|
|
57 |
|
|
58 |
pluggableProcessInputAction:something
|
|
59 |
pluggableProcessInputAction := something.
|
|
60 |
!
|
|
61 |
|
3039
|
62 |
pty
|
|
63 |
^ pty
|
|
64 |
!
|
|
65 |
|
3040
|
66 |
ptyName
|
|
67 |
^ ptyName
|
|
68 |
!
|
|
69 |
|
3028
|
70 |
shellCommand
|
|
71 |
^ shellCommand
|
|
72 |
!
|
|
73 |
|
|
74 |
shellDirectory
|
|
75 |
^ shellDirectory
|
|
76 |
!
|
|
77 |
|
|
78 |
shellPid
|
|
79 |
^ shellPid
|
|
80 |
! !
|
|
81 |
|
|
82 |
!TerminalSession methodsFor:'initialization & release'!
|
|
83 |
|
|
84 |
closeDownShell
|
|
85 |
"shut down my shell process and stop the background reader thread."
|
|
86 |
|
|
87 |
|pid|
|
|
88 |
|
|
89 |
(pid := shellPid) notNil ifTrue:[
|
|
90 |
Debug ifTrue:[
|
|
91 |
Transcript print:'killing shell pid='; showCR:pid.
|
|
92 |
].
|
|
93 |
OperatingSystem terminateProcessGroup:pid.
|
|
94 |
OperatingSystem terminateProcess:pid.
|
|
95 |
Delay waitForSeconds:1.
|
|
96 |
shellPid notNil ifTrue:[
|
|
97 |
OperatingSystem isMSWINDOWSlike ifFalse:[
|
|
98 |
OperatingSystem killProcessGroup:pid.
|
|
99 |
].
|
|
100 |
OperatingSystem killProcess:pid.
|
|
101 |
shellPid := nil.
|
|
102 |
].
|
|
103 |
OperatingSystem closePid:pid.
|
|
104 |
].
|
|
105 |
|
|
106 |
"Modified: / 5.5.1999 / 18:43:02 / cg"
|
|
107 |
!
|
|
108 |
|
|
109 |
closeStreams
|
3037
|
110 |
self stopReaderProcess.
|
|
111 |
|
3028
|
112 |
inStream notNil ifTrue:[
|
|
113 |
inStream isStream ifTrue:[inStream close].
|
|
114 |
inStream := nil
|
|
115 |
].
|
|
116 |
outStream notNil ifTrue:[
|
|
117 |
outStream close.
|
|
118 |
outStream := nil
|
|
119 |
].
|
|
120 |
errStream notNil ifTrue:[
|
|
121 |
errStream close.
|
|
122 |
errStream := nil
|
|
123 |
].
|
|
124 |
!
|
|
125 |
|
3038
|
126 |
createTerminalConnectionAndSetupWith:setupBlock
|
|
127 |
"create a terminal conenction (pseudo terminal or pipe)"
|
|
128 |
|
3040
|
129 |
|slaveFD master slave ptyTriple|
|
3038
|
130 |
|
|
131 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
132 |
"use two pipes to COMMAND.COM"
|
|
133 |
stxToStdinPipe := NonPositionableExternalStream makePipe.
|
|
134 |
stxToStdinPipe isNil ifTrue:[
|
|
135 |
self error:(self class classResources string:'Could not create pipe to COMMAND.COM.') mayProceed:true.
|
|
136 |
^ self.
|
|
137 |
].
|
|
138 |
|
|
139 |
stdOutToStxPipe := NonPositionableExternalStream makePipe.
|
|
140 |
stdOutToStxPipe isNil ifTrue:[
|
|
141 |
self error:(self class classResources classResources string:'Could not create pipe from COMMAND.COM.') mayProceed:true.
|
|
142 |
^ self.
|
|
143 |
].
|
|
144 |
|
|
145 |
"/ pipe readSide is p at:1;
|
|
146 |
"/ writeSide is p at:2
|
|
147 |
|
|
148 |
slaveFD := (stdOutToStxPipe at:2) fileDescriptor.
|
|
149 |
execFDArray := Array
|
|
150 |
with:(stxToStdinPipe at:1) fileDescriptor "stdin"
|
|
151 |
with:slaveFD "stdout"
|
|
152 |
with:slaveFD. "stderr"
|
|
153 |
|
|
154 |
outStream := stdOutToStxPipe at:1.
|
|
155 |
inStream := stxToStdinPipe at:2.
|
|
156 |
] ifFalse:[
|
|
157 |
"Use a pseudo-tty"
|
3040
|
158 |
ptyTriple := OperatingSystem makePTY.
|
|
159 |
ptyTriple isNil ifTrue:[
|
3038
|
160 |
self warn:'Cannot open pty.'.
|
|
161 |
^ self.
|
|
162 |
].
|
|
163 |
|
3040
|
164 |
ptyName := ptyTriple at:3.
|
3039
|
165 |
|
3038
|
166 |
"/ pty at:1 is the master;
|
|
167 |
"/ pty at:2 is the slave
|
3040
|
168 |
master := NonPositionableExternalStream forReadWriteToFileDescriptor:(ptyTriple at:1).
|
3039
|
169 |
master buffered:false.
|
|
170 |
|
3040
|
171 |
slave := NonPositionableExternalStream forReadWriteToFileDescriptor:(ptyTriple at:2).
|
|
172 |
slave buffered:false.
|
|
173 |
|
|
174 |
pty := { master . slave }.
|
|
175 |
|
3039
|
176 |
inStream := outStream := master.
|
3038
|
177 |
setupBlock value.
|
|
178 |
"/ fork a shell process on the slave-side
|
3040
|
179 |
slaveFD := (ptyTriple at:2).
|
3038
|
180 |
execFDArray := Array with:slaveFD with:slaveFD with:slaveFD.
|
|
181 |
].
|
|
182 |
!
|
|
183 |
|
3028
|
184 |
killShell
|
|
185 |
"shut down my shell process and stop the background reader thread."
|
|
186 |
|
|
187 |
|pid|
|
|
188 |
|
|
189 |
(pid := shellPid) notNil ifTrue:[
|
|
190 |
Debug ifTrue:[
|
|
191 |
Transcript show:'killing shell pid='; showCR:pid.
|
|
192 |
].
|
|
193 |
OperatingSystem terminateProcessGroup:pid.
|
|
194 |
OperatingSystem terminateProcess:pid.
|
|
195 |
Delay waitForSeconds:1.
|
|
196 |
shellPid notNil ifTrue:[
|
|
197 |
OperatingSystem isMSWINDOWSlike ifFalse:[
|
|
198 |
OperatingSystem killProcessGroup:pid.
|
|
199 |
].
|
|
200 |
OperatingSystem killProcess:pid.
|
|
201 |
shellPid := nil.
|
|
202 |
].
|
|
203 |
OperatingSystem closePid:pid.
|
|
204 |
].
|
|
205 |
!
|
|
206 |
|
|
207 |
reinitialize
|
|
208 |
shellPid := nil.
|
|
209 |
inStream := outStream := errStream := nil.
|
|
210 |
!
|
|
211 |
|
|
212 |
startCommand:aCommand in:aDirectory environment:envIn setupTerminalWith:setupBlock terminatedAction:terminatedAction
|
|
213 |
"start a command on a pseudo terminal. If the command arg is nil,
|
|
214 |
a shell is started. If aDirectory is not nil, the command is
|
|
215 |
executed in that directory.
|
|
216 |
Also fork a reader process, to read the shell's output and
|
|
217 |
tell me, whenever something arrives"
|
|
218 |
|
3038
|
219 |
|blocked exitStatus
|
|
220 |
cmd shell args env shellAndArgs|
|
3028
|
221 |
|
|
222 |
shellCommand := aCommand.
|
|
223 |
shellDirectory := aDirectory.
|
|
224 |
|
3038
|
225 |
self createTerminalConnectionAndSetupWith:setupBlock.
|
3028
|
226 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
227 |
shellAndArgs := OperatingSystem commandAndArgsForOSCommand:aCommand.
|
|
228 |
shell := shellAndArgs at:1.
|
|
229 |
args := (shellAndArgs at:2) ? ''.
|
|
230 |
] ifFalse:[
|
|
231 |
aCommand isNil ifTrue:[
|
|
232 |
shell := OperatingSystem getEnvironment:'SHELL'.
|
|
233 |
shell size == 0 ifTrue:[
|
|
234 |
shell := '/bin/sh'.
|
|
235 |
].
|
|
236 |
cmd := shell asFilename baseName.
|
|
237 |
args := (Array with:cmd).
|
|
238 |
] ifFalse:[
|
|
239 |
shell := '/bin/sh'.
|
|
240 |
args := (Array with:'sh' with:'-c' with:aCommand).
|
|
241 |
].
|
|
242 |
env := Dictionary new.
|
|
243 |
env declareAllFrom:envIn.
|
|
244 |
env at:'SHELL' put:shell.
|
|
245 |
].
|
|
246 |
|
|
247 |
blocked := OperatingSystem blockInterrupts.
|
|
248 |
|
|
249 |
shellPid := Processor
|
|
250 |
monitor:[
|
|
251 |
OperatingSystem
|
|
252 |
exec:shell
|
|
253 |
withArguments:args
|
|
254 |
environment:env
|
3038
|
255 |
fileDescriptors:execFDArray
|
3028
|
256 |
fork:true
|
|
257 |
newPgrp:true
|
|
258 |
inDirectory:aDirectory.
|
|
259 |
]
|
|
260 |
action:[:status |
|
|
261 |
Debug ifTrue:[
|
|
262 |
Transcript show:'pid:'; showCR:status pid.
|
|
263 |
Transcript show:'status:'; showCR:status status.
|
|
264 |
Transcript show:'code:'; showCR:status code.
|
|
265 |
Transcript show:'core:'; showCR:status core.
|
|
266 |
].
|
|
267 |
status stillAlive ifFalse:[
|
|
268 |
exitStatus := status.
|
|
269 |
OperatingSystem closePid:shellPid.
|
|
270 |
shellPid := nil.
|
|
271 |
terminatedAction value
|
|
272 |
].
|
|
273 |
].
|
|
274 |
|
|
275 |
blocked ifFalse:[
|
|
276 |
OperatingSystem unblockInterrupts
|
|
277 |
].
|
|
278 |
|
|
279 |
"close the slave side of the pty/pipes (only used by the child)"
|
|
280 |
pty notNil ifTrue:[
|
|
281 |
(pty at:2) close.
|
|
282 |
].
|
|
283 |
|
|
284 |
stdOutToStxPipe notNil ifTrue:[
|
|
285 |
(stdOutToStxPipe at:2) close.
|
|
286 |
(stxToStdinPipe at:1) close.
|
|
287 |
].
|
|
288 |
|
|
289 |
shellPid isNil ifTrue:[
|
3069
|
290 |
"/ self warn:'Cannot start shell'.
|
3028
|
291 |
outStream close.
|
|
292 |
inStream close.
|
|
293 |
inStream := outStream := nil.
|
|
294 |
].
|
3069
|
295 |
^ shellPid
|
3028
|
296 |
|
3069
|
297 |
"Created: / 20-07-1998 / 18:19:32 / cg"
|
|
298 |
"Modified: / 01-08-2013 / 20:38:37 / cg"
|
3028
|
299 |
! !
|
|
300 |
|
|
301 |
!TerminalSession methodsFor:'input / output'!
|
|
302 |
|
|
303 |
paste:someText
|
|
304 |
"paste - redefined to send the chars to the shell instead
|
|
305 |
of pasting into the view"
|
|
306 |
|
|
307 |
|s nLines|
|
|
308 |
|
|
309 |
s := someText.
|
|
310 |
s isString ifTrue:[
|
|
311 |
s := s asStringCollection
|
|
312 |
] ifFalse:[
|
|
313 |
(s isKindOf:StringCollection) ifFalse:[
|
|
314 |
self warn:'selection (' , s class name , ') is not convertable to Text'.
|
|
315 |
^ self
|
|
316 |
]
|
|
317 |
].
|
|
318 |
(nLines := s size) == 0 ifTrue:[^ self].
|
|
319 |
(nLines == 1 and:[(s at:1) size == 0]) ifTrue:[^ self].
|
|
320 |
s keysAndValuesDo:[:idx :line |
|
|
321 |
line notNil ifTrue:[inStream nextPutAll:line].
|
|
322 |
idx ~~ nLines ifTrue:[
|
|
323 |
self sendLineEnd.
|
|
324 |
]
|
|
325 |
].
|
|
326 |
|
|
327 |
"Modified: / 12.6.1998 / 22:12:47 / cg"
|
|
328 |
!
|
|
329 |
|
|
330 |
sendLine:aString
|
|
331 |
inStream nextPutAll:aString.
|
|
332 |
self sendLineEnd
|
|
333 |
!
|
|
334 |
|
|
335 |
sendLineEnd
|
|
336 |
OperatingSystem isMSDOSlike ifTrue:[
|
|
337 |
inStream nextPut:Character return.
|
|
338 |
inStream nextPut:Character linefeed.
|
|
339 |
] ifFalse:[
|
|
340 |
inStream nextPut:Character return.
|
|
341 |
].
|
|
342 |
! !
|
|
343 |
|
|
344 |
!TerminalSession methodsFor:'misc'!
|
|
345 |
|
|
346 |
defineWindowSizeLines:numberOfLines columns:numberOfColumns
|
|
347 |
| delta prevNumCols prevNumLines|
|
|
348 |
|
|
349 |
"/ any idea, how to do this under windows ?
|
|
350 |
|
|
351 |
OperatingSystem isUNIXlike ifTrue:[
|
|
352 |
"/
|
|
353 |
"/ tell the pty;
|
|
354 |
"/ tell the shell;
|
|
355 |
"/
|
|
356 |
(inStream notNil
|
|
357 |
and:[inStream isExternalStream
|
|
358 |
and:[inStream isOpen]]) ifTrue:[
|
|
359 |
Debug ifTrue:[
|
|
360 |
Transcript showCR:'TerminalSession [info]: changed len to ', numberOfLines printString.
|
|
361 |
].
|
|
362 |
(OperatingSystem
|
|
363 |
setWindowSizeOnFileDescriptor:inStream fileDescriptor
|
|
364 |
width:numberOfColumns
|
|
365 |
height:numberOfLines) ifFalse:[
|
|
366 |
Debug ifTrue:[
|
|
367 |
Transcript showCR:'TerminalSession [warning]: cannot change windowSize'.
|
|
368 |
].
|
|
369 |
].
|
|
370 |
|
|
371 |
].
|
|
372 |
shellPid notNil ifTrue:[
|
|
373 |
OperatingSystem sendSignal:OperatingSystem sigWINCH to:shellPid
|
|
374 |
]
|
|
375 |
].
|
|
376 |
|
|
377 |
"Created: / 11.6.1998 / 22:51:39 / cg"
|
|
378 |
"Modified: / 5.5.1999 / 19:45:09 / cg"
|
|
379 |
!
|
|
380 |
|
|
381 |
sendInterruptSignal
|
|
382 |
"send an INT-signal to the shell (UNIX only)"
|
|
383 |
|
|
384 |
shellPid notNil ifTrue:[
|
|
385 |
OperatingSystem sendSignal:(OperatingSystem sigINT) to:shellPid negated.
|
|
386 |
].
|
|
387 |
|
|
388 |
"Modified: / 10.6.1998 / 17:49:49 / cg"
|
|
389 |
!
|
|
390 |
|
|
391 |
sendKillSignal
|
|
392 |
"send a KILL-signal to the shell (UNIX only)"
|
|
393 |
|
|
394 |
shellPid notNil ifTrue:[
|
|
395 |
OperatingSystem sendSignal:(OperatingSystem sigKILL) to:shellPid negated.
|
|
396 |
]
|
|
397 |
! !
|
|
398 |
|
|
399 |
!TerminalSession methodsFor:'reader process'!
|
|
400 |
|
|
401 |
readAnyAvailableData
|
|
402 |
"read data from the stream,
|
|
403 |
and sends me #processInput:n: events if something arrived.
|
|
404 |
Returns the amount of data read."
|
|
405 |
|
|
406 |
|buffer n bufferSize|
|
|
407 |
|
|
408 |
outStream isNil ifTrue:[^ 0]. "/ already closed
|
|
409 |
|
|
410 |
bufferSize := 256.
|
|
411 |
buffer := String new:bufferSize.
|
|
412 |
|
|
413 |
ExternalStream readErrorSignal handle:[:ex |
|
|
414 |
n := 0
|
|
415 |
] do:[
|
|
416 |
n := outStream nextAvailableBytes:bufferSize into:buffer startingAt:1.
|
|
417 |
n > 0 ifTrue:[
|
|
418 |
pluggableProcessInputAction notNil ifTrue:[
|
|
419 |
pluggableProcessInputAction value:buffer value:n.
|
|
420 |
]
|
|
421 |
].
|
|
422 |
].
|
|
423 |
^ n
|
|
424 |
!
|
|
425 |
|
|
426 |
readerProcessLoop
|
|
427 |
"look for the session's output"
|
|
428 |
|
|
429 |
StreamError handle:[:ex |
|
|
430 |
Transcript show:'Terminal(PTY-reader) [error]: '; showCR:ex description.
|
|
431 |
] do:[
|
|
432 |
[true] whileTrue:[
|
|
433 |
AbortOperationRequest handle:[:ex |
|
|
434 |
] do:[
|
|
435 |
|n sensor|
|
|
436 |
|
|
437 |
readerDelay notNil ifTrue:[ Delay waitForSeconds:readerDelay].
|
|
438 |
outStream readWait.
|
|
439 |
|
|
440 |
(pluggableCheckBeforeReadAction isNil
|
|
441 |
or:[pluggableCheckBeforeReadAction value]) ifTrue:[
|
|
442 |
n := self readAnyAvailableData.
|
|
443 |
n == 0 ifTrue:[
|
|
444 |
"/ Windows IPC has a bug - it always
|
|
445 |
"/ returns 0 (when the command is idle)
|
|
446 |
"/ and says it's at the end (sigh)
|
|
447 |
|
|
448 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
449 |
Delay waitForSeconds:0.1
|
|
450 |
] ifFalse:[
|
|
451 |
outStream atEnd ifTrue:[
|
|
452 |
outStream close. outStream := nil.
|
|
453 |
inStream close. inStream := nil.
|
|
454 |
Processor activeProcess terminate.
|
|
455 |
] ifFalse:[
|
|
456 |
"/ this should not happen.
|
|
457 |
|
|
458 |
Delay waitForSeconds:0.1
|
|
459 |
]
|
|
460 |
].
|
|
461 |
]
|
|
462 |
]
|
|
463 |
]
|
|
464 |
]
|
|
465 |
]
|
|
466 |
!
|
|
467 |
|
|
468 |
startReaderProcess
|
|
469 |
"Start a reader process, which looks for the commands output,
|
|
470 |
and sends me #processInput:n: events whenever something arrives."
|
|
471 |
|
|
472 |
readerProcess isNil ifTrue:[
|
|
473 |
readerProcess := [
|
|
474 |
[
|
|
475 |
self readerProcessLoop.
|
|
476 |
] ifCurtailed:[
|
|
477 |
readerProcess := nil
|
|
478 |
]
|
|
479 |
] fork. "/ forkAt:9.
|
|
480 |
readerProcess name:'pty reader'.
|
|
481 |
]
|
|
482 |
|
|
483 |
"
|
|
484 |
VT100TerminalView openShell
|
|
485 |
"
|
|
486 |
|
|
487 |
"Modified: / 5.5.1999 / 17:58:02 / cg"
|
|
488 |
"Modified: / 28.1.2002 / 21:10:13 / micha"
|
|
489 |
!
|
|
490 |
|
|
491 |
stopReaderProcess
|
|
492 |
"stop the background reader thread"
|
|
493 |
|
3037
|
494 |
|p|
|
|
495 |
|
|
496 |
(p := readerProcess) notNil ifTrue:[
|
|
497 |
readerProcess := nil.
|
|
498 |
p terminate.
|
3028
|
499 |
"/ give it a chance to really terminate
|
|
500 |
Processor yield.
|
|
501 |
].
|
|
502 |
! !
|
|
503 |
|
|
504 |
!TerminalSession class methodsFor:'documentation'!
|
|
505 |
|
|
506 |
version
|
3069
|
507 |
^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.6 2013-08-01 18:47:02 cg Exp $'
|
3028
|
508 |
!
|
|
509 |
|
|
510 |
version_CVS
|
3069
|
511 |
^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.6 2013-08-01 18:47:02 cg Exp $'
|
3028
|
512 |
! !
|
|
513 |
|
|
514 |
|
|
515 |
TerminalSession initialize!
|