914
|
1 |
TextCollector subclass:#TerminalView
|
|
2 |
instanceVariableNames:'inStream outStream readerProcess shellPid kbdSequences
|
|
3 |
escapeSequenceTree currentSequence currentTree kbdMap
|
|
4 |
escapeLeadingChars'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Views-TerminalViews'
|
|
8 |
!
|
|
9 |
|
|
10 |
!TerminalView class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
916
|
14 |
I provide terminal functionality, by forking a command interpreter
|
|
15 |
and comunicating with it via a pty.
|
|
16 |
I am abstract - concrete terminal characteristics are defined
|
|
17 |
by concrete subclasses (see VT52TerminalView).
|
914
|
18 |
|
|
19 |
[author:]
|
|
20 |
Claus Gittinger
|
915
|
21 |
|
|
22 |
[start with:]
|
|
23 |
VT52TerminalView openShell
|
914
|
24 |
"
|
|
25 |
! !
|
|
26 |
|
|
27 |
!TerminalView class methodsFor:'testing'!
|
|
28 |
|
|
29 |
openDummy
|
|
30 |
|in vt52|
|
|
31 |
|
|
32 |
vt52 := self new.
|
|
33 |
in := ForwardingStream on:''.
|
|
34 |
|
|
35 |
in fwdStream:vt52.
|
|
36 |
vt52 inStream:in.
|
|
37 |
vt52 outStream:in.
|
|
38 |
vt52 open
|
|
39 |
|
|
40 |
"
|
|
41 |
self openDummy
|
|
42 |
"
|
|
43 |
!
|
|
44 |
|
|
45 |
openShell
|
|
46 |
|in top scr vt52|
|
|
47 |
|
|
48 |
top := StandardSystemView new.
|
|
49 |
scr := HVScrollableView for:self in:top.
|
917
|
50 |
scr autoHideHorizontalScrollBar:true.
|
914
|
51 |
scr origin:0.0@0.0 corner:1.0@1.0.
|
|
52 |
vt52 := scr scrolledView.
|
|
53 |
|
|
54 |
vt52 startShell.
|
917
|
55 |
|
|
56 |
top extent:(scr preferredExtent).
|
914
|
57 |
top open
|
|
58 |
|
|
59 |
"
|
|
60 |
VT52TerminalView openShell
|
|
61 |
"
|
|
62 |
! !
|
|
63 |
|
|
64 |
!TerminalView methodsFor:'accessing'!
|
|
65 |
|
|
66 |
inStream
|
|
67 |
"return the value of the instance variable 'inStream' (automatically generated)"
|
|
68 |
|
|
69 |
^ inStream!
|
|
70 |
|
|
71 |
inStream:something
|
|
72 |
"set the value of the instance variable 'inStream' (automatically generated)"
|
|
73 |
|
|
74 |
inStream := something.!
|
|
75 |
|
|
76 |
outStream
|
|
77 |
"return the value of the instance variable 'outStream' (automatically generated)"
|
|
78 |
|
|
79 |
^ outStream!
|
|
80 |
|
|
81 |
outStream:something
|
|
82 |
"set the value of the instance variable 'outStream' (automatically generated)"
|
|
83 |
|
|
84 |
outStream := something.! !
|
|
85 |
|
|
86 |
!TerminalView methodsFor:'cursor handling'!
|
|
87 |
|
|
88 |
cursorCol:col
|
|
89 |
"check of col is a valid cursor position; return a new col-nr if not.
|
|
90 |
Here, the linelength is enforced"
|
|
91 |
|
|
92 |
^ super cursorCol:(col min:80)
|
|
93 |
|
|
94 |
!
|
|
95 |
|
|
96 |
cursorLine:l col:col
|
|
97 |
"check of col is a valid cursor position; return a new col-nr if not.
|
|
98 |
Here, the linelength is enforced"
|
|
99 |
|
|
100 |
^ super cursorLine:l col:(col min:80)
|
|
101 |
|
|
102 |
!
|
|
103 |
|
|
104 |
validateCursorCol:col inLine:line
|
|
105 |
"check of col is a valid cursor position; return a new col-nr if not.
|
|
106 |
Here, the linelength is enforced"
|
|
107 |
|
|
108 |
^ col min:80
|
|
109 |
|
|
110 |
! !
|
|
111 |
|
|
112 |
!TerminalView methodsFor:'event handling'!
|
|
113 |
|
|
114 |
keyPress:aKey x:x y:y
|
|
115 |
|rest rawKey seq|
|
|
116 |
|
|
117 |
inStream isNil ifTrue:[^ self].
|
|
118 |
|
|
119 |
Transcript showCR:'----'; show:'key:' ; showCR:aKey printString.
|
|
120 |
|
|
121 |
aKey isCharacter ifTrue:[
|
|
122 |
"/ send it down to inStream ...
|
|
123 |
inStream nextPut:aKey.
|
|
124 |
^ self
|
|
125 |
].
|
|
126 |
|
|
127 |
seq := kbdSequences at:aKey ifAbsent:nil.
|
|
128 |
seq notNil ifTrue:[
|
|
129 |
Transcript show:'->' ; showCR:seq storeString.
|
|
130 |
inStream nextPutAll:(seq withEscapes).
|
|
131 |
^ self
|
|
132 |
].
|
|
133 |
|
|
134 |
rawKey := device keyboardMap keyAtValue:aKey ifAbsent:aKey.
|
|
135 |
seq := kbdSequences at:rawKey ifAbsent:nil.
|
|
136 |
seq notNil ifTrue:[
|
|
137 |
Transcript show:'->' ; showCR:seq storeString.
|
|
138 |
inStream nextPutAll:(seq withEscapes).
|
|
139 |
^ self
|
|
140 |
].
|
|
141 |
|
|
142 |
(rawKey startsWith:'Ctrl') ifTrue:[
|
|
143 |
rest := rawKey copyFrom:5.
|
|
144 |
rest size == 1 ifTrue:[
|
|
145 |
rest := rest at:1.
|
|
146 |
(rest >= $a and:[rest <= $z]) ifTrue:[
|
|
147 |
Transcript show:'->' ; showCR:(Character value:(rest - $a + 1)) storeString.
|
|
148 |
inStream nextPut:(Character value:(rest - $a + 1)).
|
|
149 |
^ self
|
|
150 |
].
|
|
151 |
(rest >= $A and:[rest <= $Z]) ifTrue:[
|
|
152 |
Transcript show:'->' ; showCR:(Character value:(rest - $a + 1)) storeString.
|
|
153 |
inStream nextPut:(Character value:(rest - $A + 1)).
|
|
154 |
^ self
|
|
155 |
].
|
|
156 |
]
|
|
157 |
].
|
|
158 |
|
|
159 |
Transcript show:'ignored: '; showCR:rawKey.
|
|
160 |
|
|
161 |
"/ super keyPress:aKey x:x y:y
|
|
162 |
!
|
|
163 |
|
|
164 |
shellTerminated
|
|
165 |
self warn:'shell terminated'.
|
|
166 |
self closeDownShell.
|
|
167 |
! !
|
|
168 |
|
|
169 |
!TerminalView methodsFor:'functions'!
|
|
170 |
|
|
171 |
doBackspace
|
|
172 |
self cursorLeft.
|
|
173 |
self replaceCharAtCursor:(Character space).
|
|
174 |
self cursorLeft.
|
|
175 |
|
|
176 |
!
|
|
177 |
|
|
178 |
doCursorDown
|
|
179 |
super cursorDown
|
|
180 |
!
|
|
181 |
|
|
182 |
doCursorHome
|
|
183 |
super cursorHome
|
|
184 |
!
|
|
185 |
|
|
186 |
doCursorLeft
|
|
187 |
super cursorLeft
|
|
188 |
!
|
|
189 |
|
|
190 |
doCursorNewLine
|
|
191 |
super cursorDown
|
|
192 |
!
|
|
193 |
|
|
194 |
doCursorReturn
|
|
195 |
super cursorToBeginOfLine
|
|
196 |
!
|
|
197 |
|
|
198 |
doCursorRight
|
|
199 |
super cursorRight
|
|
200 |
!
|
|
201 |
|
|
202 |
doCursorUp
|
|
203 |
super cursorUp
|
|
204 |
! !
|
|
205 |
|
|
206 |
!TerminalView methodsFor:'initialization'!
|
|
207 |
|
|
208 |
closeDownShell
|
|
209 |
shellPid notNil ifTrue:[
|
|
210 |
OperatingSystem terminateProcess:shellPid.
|
|
211 |
OperatingSystem terminateProcessGroup:shellPid.
|
|
212 |
shellPid := nil.
|
|
213 |
].
|
|
214 |
|
|
215 |
readerProcess notNil ifTrue:[
|
|
216 |
readerProcess terminate.
|
|
217 |
readerProcess := nil
|
|
218 |
].
|
|
219 |
inStream notNil ifTrue:[
|
|
220 |
inStream close.
|
|
221 |
inStream := nil
|
|
222 |
].
|
|
223 |
outStream notNil ifTrue:[
|
|
224 |
outStream close.
|
|
225 |
outStream := nil
|
|
226 |
].
|
|
227 |
!
|
|
228 |
|
|
229 |
destroy
|
|
230 |
self closeDownShell.
|
|
231 |
super destroy
|
|
232 |
!
|
|
233 |
|
|
234 |
escapeSequences:codes
|
|
235 |
|tree|
|
|
236 |
|
|
237 |
tree isNil ifTrue:[tree := escapeSequenceTree := IdentityDictionary new].
|
|
238 |
|
|
239 |
codes do:[:specEntry |
|
|
240 |
|sequence function|
|
|
241 |
|
|
242 |
sequence := (specEntry at:1) withEscapes.
|
|
243 |
function := specEntry at:2.
|
|
244 |
|
|
245 |
tree := escapeSequenceTree.
|
|
246 |
|
|
247 |
sequence keysAndValuesDo:[:idx :char |
|
|
248 |
|followup|
|
|
249 |
|
|
250 |
idx == sequence size ifTrue:[
|
|
251 |
tree at:char put:function
|
|
252 |
] ifFalse:[
|
|
253 |
followup := tree at:char ifAbsent:nil.
|
|
254 |
followup isNil ifTrue:[
|
|
255 |
tree at:char put:(followup := IdentityDictionary new).
|
|
256 |
].
|
|
257 |
tree := followup
|
|
258 |
]
|
|
259 |
]
|
|
260 |
].
|
|
261 |
escapeLeadingChars := escapeSequenceTree keys asArray
|
|
262 |
!
|
|
263 |
|
|
264 |
initialize
|
|
265 |
super initialize.
|
|
266 |
|
|
267 |
showMatchingParenthesis := false.
|
|
268 |
insertMode := false.
|
|
269 |
|
|
270 |
self initializeEscapeSequences.
|
|
271 |
self initializeKeyboardSequences.
|
|
272 |
list := OrderedCollection new:24 withAll:''.
|
|
273 |
|
|
274 |
self initializeKeyboardMap.
|
|
275 |
|
|
276 |
"
|
|
277 |
VT52TerminalView openShell
|
|
278 |
"
|
|
279 |
!
|
|
280 |
|
|
281 |
initializeEscapeSequences
|
|
282 |
self subclassResponsibility.
|
|
283 |
!
|
|
284 |
|
|
285 |
initializeKeyboardMap
|
|
286 |
|ctrlKeys cmdKeys|
|
|
287 |
|
|
288 |
"/ setup my own keyboardMap, where control-keys are
|
|
289 |
"/ not translated.
|
|
290 |
kbdMap := device keyboardMap copy.
|
|
291 |
|
|
292 |
ctrlKeys := kbdMap keys select:[:key | key startsWith:'Ctrl'].
|
|
293 |
ctrlKeys do:[:key | kbdMap removeKey:key].
|
|
294 |
cmdKeys := kbdMap keys select:[:key | key startsWith:'Cmd'].
|
|
295 |
cmdKeys do:[:key | kbdMap removeKey:key].
|
|
296 |
|
|
297 |
kbdMap removeKey:#Delete ifAbsent:[].
|
|
298 |
kbdMap removeKey:#BackSpace ifAbsent:[].
|
|
299 |
|
|
300 |
"
|
|
301 |
VT52TerminalView openShell
|
|
302 |
"
|
|
303 |
!
|
|
304 |
|
|
305 |
initializeKeyboardSequences
|
|
306 |
self subclassResponsibility.
|
|
307 |
!
|
|
308 |
|
|
309 |
keyboardMap
|
|
310 |
^ kbdMap
|
|
311 |
!
|
|
312 |
|
|
313 |
startReaderProcess
|
|
314 |
readerProcess isNil ifTrue:[
|
|
315 |
readerProcess := [
|
|
316 |
[
|
|
317 |
|buffer n|
|
|
318 |
|
|
319 |
buffer := String new:1024.
|
|
320 |
[true] whileTrue:[
|
|
321 |
Transcript showCR:'wait ...'.
|
|
322 |
outStream readWait.
|
|
323 |
Transcript showCR:'... avail'.
|
|
324 |
n := outStream nextAvailableBytes:1024 into:buffer startingAt:1.
|
|
325 |
Transcript show:'got:'; showCR:n.
|
|
326 |
n > 0 ifTrue:[
|
|
327 |
self nextPutAll:(buffer copyTo:n).
|
|
328 |
] ifFalse:[
|
|
329 |
n == 0 ifTrue:[
|
|
330 |
outStream atEnd ifTrue:[
|
|
331 |
Transcript showCR:'input closed:'.
|
|
332 |
outStream close. outStream := nil.
|
|
333 |
inStream close. inStream := nil.
|
|
334 |
|
|
335 |
Processor activeProcess terminate.
|
|
336 |
]
|
|
337 |
]
|
|
338 |
]
|
|
339 |
]
|
|
340 |
] valueOnUnwindDo:[
|
|
341 |
readerProcess := nil
|
|
342 |
]
|
|
343 |
] fork.
|
|
344 |
]
|
|
345 |
|
|
346 |
"
|
|
347 |
VT52TerminalView openShell
|
|
348 |
"
|
|
349 |
!
|
|
350 |
|
|
351 |
startShell
|
|
352 |
|p slaveFD execFdArray blocked exitStatus|
|
|
353 |
|
|
354 |
p := ExternalStream makePTYPair.
|
|
355 |
p isNil ifTrue:[
|
915
|
356 |
self warn:'cannot open pty'.
|
914
|
357 |
^ self.
|
|
358 |
].
|
|
359 |
|
|
360 |
"/ p at:1 is the master;
|
|
361 |
"/ p at:2 is the slave
|
|
362 |
inStream := outStream := (p at:1).
|
|
363 |
inStream buffered:false.
|
|
364 |
|
|
365 |
"/ fork a shell process on the slave-side
|
|
366 |
slaveFD := (p at:2) fileDescriptor.
|
|
367 |
|
|
368 |
execFdArray := Array with:slaveFD with:slaveFD with:slaveFD.
|
|
369 |
|
|
370 |
blocked := OperatingSystem blockInterrupts.
|
|
371 |
|
|
372 |
shellPid := Processor
|
|
373 |
monitor:[
|
|
374 |
OperatingSystem
|
|
375 |
exec:'/bin/sh'
|
|
376 |
withArguments:#('sh')
|
|
377 |
fileDescriptors:execFdArray
|
|
378 |
closeDescriptors:#()
|
|
379 |
fork:true
|
|
380 |
newPgrp:true
|
|
381 |
inDirectory:nil.
|
|
382 |
]
|
|
383 |
action:[:status |
|
|
384 |
Transcript show:'pid:'; showCR:status pid.
|
|
385 |
Transcript show:'status:'; showCR:status status.
|
|
386 |
Transcript show:'code:'; showCR:status code.
|
|
387 |
Transcript show:'core:'; showCR:status core.
|
|
388 |
status stillAlive ifFalse:[
|
|
389 |
exitStatus := status.
|
|
390 |
OperatingSystem closePid:shellPid.
|
|
391 |
shellPid := nil.
|
|
392 |
self pushEvent:#shellTerminated
|
|
393 |
].
|
|
394 |
].
|
|
395 |
|
|
396 |
shellPid isNil ifTrue:[
|
|
397 |
self halt.
|
|
398 |
(p at:1) close.
|
|
399 |
(p at:2) close.
|
|
400 |
].
|
|
401 |
|
|
402 |
blocked ifFalse:[
|
|
403 |
OperatingSystem unblockInterrupts
|
|
404 |
].
|
|
405 |
|
|
406 |
self startReaderProcess.
|
|
407 |
|
|
408 |
"
|
|
409 |
VT52TerminalView openShell
|
|
410 |
"
|
|
411 |
! !
|
|
412 |
|
|
413 |
!TerminalView methodsFor:'menu'!
|
|
414 |
|
|
415 |
editMenu
|
|
416 |
"return the views middleButtonMenu"
|
|
417 |
|
|
418 |
<resource: #keyboard (#Copy #Paste #Print)>
|
|
419 |
<resource: #programMenu>
|
|
420 |
|
|
421 |
|items m sub shortKeys sensor|
|
|
422 |
|
|
423 |
((sensor := self sensor) notNil and:[sensor ctrlDown]) ifTrue:[
|
|
424 |
items := #(
|
|
425 |
('Interrupt' doSendInterrupt)
|
|
426 |
).
|
|
427 |
] ifFalse:[
|
|
428 |
items := #(
|
|
429 |
('copy' copySelection Copy )
|
|
430 |
('paste' pasteOrReplace Paste )
|
|
431 |
('-' )
|
|
432 |
('save as ...' save SaveAs )
|
|
433 |
('print' doPrint Print )
|
|
434 |
).
|
|
435 |
].
|
|
436 |
|
|
437 |
m := PopUpMenu itemList:items resources:resources.
|
|
438 |
|
|
439 |
self hasSelection not ifTrue:[
|
|
440 |
m disable:#copySelection.
|
|
441 |
].
|
|
442 |
^ m.
|
|
443 |
|
|
444 |
"Modified: / 21.5.1998 / 15:52:38 / cg"
|
|
445 |
|
|
446 |
|
|
447 |
! !
|
|
448 |
|
|
449 |
!TerminalView methodsFor:'misc'!
|
|
450 |
|
|
451 |
removeTrailingBlankLines
|
|
452 |
^ self
|
|
453 |
! !
|
|
454 |
|
|
455 |
!TerminalView methodsFor:'processing - input'!
|
|
456 |
|
|
457 |
basicNextPut:aCharacter
|
|
458 |
super nextPut:aCharacter
|
|
459 |
!
|
|
460 |
|
|
461 |
nextPut:aCharacter
|
|
462 |
|where next|
|
|
463 |
|
|
464 |
"/ Transcript showCR:aCharacter asciiValue.
|
|
465 |
|
|
466 |
where := currentTree ? escapeSequenceTree.
|
|
467 |
"/ Transcript showCR:'current: ' , where storeString.
|
|
468 |
|
|
469 |
next := where at:aCharacter ifAbsent:nil.
|
|
470 |
"/ Transcript showCR:'next: ' , next storeString.
|
|
471 |
next isNil ifTrue:[
|
|
472 |
"/ something collected so far ?
|
|
473 |
currentSequence notNil ifTrue:[
|
|
474 |
self halt.
|
|
475 |
].
|
|
476 |
|
|
477 |
self replaceCharAtCursor:aCharacter.
|
|
478 |
"/ self cursorRight.
|
|
479 |
|
|
480 |
currentTree := nil.
|
|
481 |
^ self.
|
|
482 |
].
|
|
483 |
|
|
484 |
next isSymbol ifTrue:[
|
|
485 |
self perform:next.
|
|
486 |
currentTree := nil.
|
|
487 |
^ self
|
|
488 |
].
|
|
489 |
currentTree := next
|
|
490 |
!
|
|
491 |
|
|
492 |
nextPutAll:aCollection
|
|
493 |
|idx|
|
|
494 |
|
|
495 |
currentTree isNil ifTrue:[
|
|
496 |
"/ initial state
|
|
497 |
Transcript showCR:'initial'.
|
|
498 |
idx := aCollection indexOfAny:escapeLeadingChars.
|
|
499 |
idx == 0 ifTrue:[
|
|
500 |
Transcript showCR:'nothing special'.
|
|
501 |
]
|
|
502 |
].
|
|
503 |
|
|
504 |
aCollection do:[:ch |
|
|
505 |
self nextPut:ch
|
|
506 |
].
|
|
507 |
! !
|
|
508 |
|
|
509 |
!TerminalView methodsFor:'queries'!
|
|
510 |
|
|
511 |
preferredExtent
|
|
512 |
^ (fontWidth * 80 + (leftMargin * 2)) @ (self heightForLines:24)
|
|
513 |
! !
|
|
514 |
|
|
515 |
!TerminalView methodsFor:'selection handling'!
|
|
516 |
|
|
517 |
paste:someText
|
|
518 |
"paste - redefined to send the chars to the shell instead
|
|
519 |
of pasting into the view"
|
|
520 |
|
|
521 |
someText asString string do:[:aChar |
|
|
522 |
inStream nextPut:aChar
|
|
523 |
]
|
|
524 |
! !
|
|
525 |
|
|
526 |
!TerminalView class methodsFor:'documentation'!
|
|
527 |
|
|
528 |
version
|
917
|
529 |
^ '$Header: /cvs/stx/stx/libwidg2/TerminalView.st,v 1.4 1998-06-09 14:06:36 cg Exp $'
|
914
|
530 |
! !
|