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
|
3087
|
7 |
stdOutToStxPipe pty ptyName terminatedAction collectedOutput
|
3095
|
8 |
promptActions'
|
3028
|
9 |
classVariableNames:'Debug'
|
|
10 |
poolDictionaries:''
|
|
11 |
category:'Views-TerminalViews'
|
|
12 |
!
|
|
13 |
|
|
14 |
!TerminalSession class methodsFor:'documentation'!
|
|
15 |
|
|
16 |
documentation
|
|
17 |
"
|
|
18 |
This keeps the state and API to interact with another program
|
|
19 |
via a terminal session. Under Unix, a pseudo-tty connection
|
|
20 |
is used; other operating systems might use other mechanisms.
|
|
21 |
This is (currently) mostly used by the TerminalView application,
|
|
22 |
but can be used whereever more control is needed than a simple pipe
|
|
23 |
offers (such as terminal emulation, window size, CTRL-c support etc.)
|
|
24 |
|
|
25 |
Extracted from TerminalView, which is refactored, once this is stable.
|
|
26 |
|
|
27 |
"
|
|
28 |
! !
|
|
29 |
|
|
30 |
!TerminalSession class methodsFor:'initialization'!
|
|
31 |
|
|
32 |
initialize
|
|
33 |
|
|
34 |
Debug := false.
|
|
35 |
|
|
36 |
"
|
|
37 |
self initialize
|
|
38 |
"
|
|
39 |
! !
|
|
40 |
|
|
41 |
!TerminalSession methodsFor:'accessing'!
|
|
42 |
|
|
43 |
errStream
|
|
44 |
^ errStream
|
|
45 |
!
|
|
46 |
|
|
47 |
inStream
|
|
48 |
^ inStream
|
|
49 |
!
|
|
50 |
|
|
51 |
outStream
|
|
52 |
^ outStream
|
|
53 |
!
|
|
54 |
|
|
55 |
pluggableCheckBeforeReadAction:something
|
|
56 |
pluggableCheckBeforeReadAction := something.
|
|
57 |
!
|
|
58 |
|
|
59 |
pluggableProcessInputAction:something
|
|
60 |
pluggableProcessInputAction := something.
|
|
61 |
!
|
|
62 |
|
3039
|
63 |
pty
|
|
64 |
^ pty
|
|
65 |
!
|
|
66 |
|
3040
|
67 |
ptyName
|
|
68 |
^ ptyName
|
|
69 |
!
|
|
70 |
|
3028
|
71 |
shellCommand
|
|
72 |
^ shellCommand
|
|
73 |
!
|
|
74 |
|
|
75 |
shellDirectory
|
|
76 |
^ shellDirectory
|
|
77 |
!
|
|
78 |
|
|
79 |
shellPid
|
|
80 |
^ shellPid
|
3084
|
81 |
!
|
|
82 |
|
|
83 |
terminatedAction:something
|
|
84 |
terminatedAction := something.
|
3028
|
85 |
! !
|
|
86 |
|
|
87 |
!TerminalSession methodsFor:'initialization & release'!
|
|
88 |
|
|
89 |
closeDownShell
|
|
90 |
"shut down my shell process and stop the background reader thread."
|
|
91 |
|
|
92 |
|pid|
|
|
93 |
|
|
94 |
(pid := shellPid) notNil ifTrue:[
|
|
95 |
Debug ifTrue:[
|
|
96 |
Transcript print:'killing shell pid='; showCR:pid.
|
|
97 |
].
|
|
98 |
OperatingSystem terminateProcessGroup:pid.
|
|
99 |
OperatingSystem terminateProcess:pid.
|
|
100 |
Delay waitForSeconds:1.
|
|
101 |
shellPid notNil ifTrue:[
|
|
102 |
OperatingSystem isMSWINDOWSlike ifFalse:[
|
|
103 |
OperatingSystem killProcessGroup:pid.
|
|
104 |
].
|
|
105 |
OperatingSystem killProcess:pid.
|
|
106 |
shellPid := nil.
|
|
107 |
].
|
|
108 |
OperatingSystem closePid:pid.
|
|
109 |
].
|
|
110 |
|
|
111 |
"Modified: / 5.5.1999 / 18:43:02 / cg"
|
|
112 |
!
|
|
113 |
|
|
114 |
closeStreams
|
3037
|
115 |
self stopReaderProcess.
|
|
116 |
|
3028
|
117 |
inStream notNil ifTrue:[
|
|
118 |
inStream isStream ifTrue:[inStream close].
|
|
119 |
inStream := nil
|
|
120 |
].
|
|
121 |
outStream notNil ifTrue:[
|
|
122 |
outStream close.
|
|
123 |
outStream := nil
|
|
124 |
].
|
|
125 |
errStream notNil ifTrue:[
|
|
126 |
errStream close.
|
|
127 |
errStream := nil
|
|
128 |
].
|
|
129 |
!
|
|
130 |
|
3038
|
131 |
createTerminalConnectionAndSetupWith:setupBlock
|
|
132 |
"create a terminal conenction (pseudo terminal or pipe)"
|
|
133 |
|
3040
|
134 |
|slaveFD master slave ptyTriple|
|
3038
|
135 |
|
|
136 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
137 |
"use two pipes to COMMAND.COM"
|
|
138 |
stxToStdinPipe := NonPositionableExternalStream makePipe.
|
|
139 |
stxToStdinPipe isNil ifTrue:[
|
|
140 |
self error:(self class classResources string:'Could not create pipe to COMMAND.COM.') mayProceed:true.
|
|
141 |
^ self.
|
|
142 |
].
|
|
143 |
|
|
144 |
stdOutToStxPipe := NonPositionableExternalStream makePipe.
|
|
145 |
stdOutToStxPipe isNil ifTrue:[
|
|
146 |
self error:(self class classResources classResources string:'Could not create pipe from COMMAND.COM.') mayProceed:true.
|
|
147 |
^ self.
|
|
148 |
].
|
|
149 |
|
|
150 |
"/ pipe readSide is p at:1;
|
|
151 |
"/ writeSide is p at:2
|
|
152 |
|
|
153 |
slaveFD := (stdOutToStxPipe at:2) fileDescriptor.
|
|
154 |
execFDArray := Array
|
|
155 |
with:(stxToStdinPipe at:1) fileDescriptor "stdin"
|
|
156 |
with:slaveFD "stdout"
|
|
157 |
with:slaveFD. "stderr"
|
|
158 |
|
|
159 |
outStream := stdOutToStxPipe at:1.
|
|
160 |
inStream := stxToStdinPipe at:2.
|
|
161 |
] ifFalse:[
|
|
162 |
"Use a pseudo-tty"
|
3040
|
163 |
ptyTriple := OperatingSystem makePTY.
|
|
164 |
ptyTriple isNil ifTrue:[
|
3038
|
165 |
self warn:'Cannot open pty.'.
|
|
166 |
^ self.
|
|
167 |
].
|
|
168 |
|
3040
|
169 |
ptyName := ptyTriple at:3.
|
3039
|
170 |
|
3038
|
171 |
"/ pty at:1 is the master;
|
|
172 |
"/ pty at:2 is the slave
|
3040
|
173 |
master := NonPositionableExternalStream forReadWriteToFileDescriptor:(ptyTriple at:1).
|
3039
|
174 |
master buffered:false.
|
|
175 |
|
3040
|
176 |
slave := NonPositionableExternalStream forReadWriteToFileDescriptor:(ptyTriple at:2).
|
|
177 |
slave buffered:false.
|
|
178 |
|
|
179 |
pty := { master . slave }.
|
|
180 |
|
3039
|
181 |
inStream := outStream := master.
|
3038
|
182 |
setupBlock value.
|
|
183 |
"/ fork a shell process on the slave-side
|
3040
|
184 |
slaveFD := (ptyTriple at:2).
|
3038
|
185 |
execFDArray := Array with:slaveFD with:slaveFD with:slaveFD.
|
|
186 |
].
|
|
187 |
!
|
|
188 |
|
3028
|
189 |
killShell
|
|
190 |
"shut down my shell process and stop the background reader thread."
|
|
191 |
|
|
192 |
|pid|
|
|
193 |
|
|
194 |
(pid := shellPid) notNil ifTrue:[
|
|
195 |
Debug ifTrue:[
|
|
196 |
Transcript show:'killing shell pid='; showCR:pid.
|
|
197 |
].
|
|
198 |
OperatingSystem terminateProcessGroup:pid.
|
|
199 |
OperatingSystem terminateProcess:pid.
|
|
200 |
Delay waitForSeconds:1.
|
|
201 |
shellPid notNil ifTrue:[
|
|
202 |
OperatingSystem isMSWINDOWSlike ifFalse:[
|
|
203 |
OperatingSystem killProcessGroup:pid.
|
|
204 |
].
|
|
205 |
OperatingSystem killProcess:pid.
|
|
206 |
shellPid := nil.
|
|
207 |
].
|
|
208 |
OperatingSystem closePid:pid.
|
|
209 |
].
|
|
210 |
!
|
|
211 |
|
|
212 |
reinitialize
|
|
213 |
shellPid := nil.
|
|
214 |
inStream := outStream := errStream := nil.
|
|
215 |
!
|
|
216 |
|
3084
|
217 |
startCommand:aCommand in:aDirectory environment:envIn setupTerminalWith:setupBlock terminatedAction:terminatedActionArg
|
3028
|
218 |
"start a command on a pseudo terminal. If the command arg is nil,
|
|
219 |
a shell is started. If aDirectory is not nil, the command is
|
|
220 |
executed in that directory.
|
|
221 |
Also fork a reader process, to read the shell's output and
|
|
222 |
tell me, whenever something arrives"
|
|
223 |
|
3038
|
224 |
|blocked exitStatus
|
3088
|
225 |
cmd shell args env shellAndArgs didOpenTerminal|
|
3028
|
226 |
|
|
227 |
shellCommand := aCommand.
|
|
228 |
shellDirectory := aDirectory.
|
3084
|
229 |
terminatedAction := terminatedActionArg.
|
3088
|
230 |
didOpenTerminal := false.
|
3028
|
231 |
|
3085
|
232 |
(inStream isNil or:[outStream isNil]) ifTrue:[
|
|
233 |
self createTerminalConnectionAndSetupWith:setupBlock.
|
3088
|
234 |
didOpenTerminal := true.
|
3085
|
235 |
].
|
3028
|
236 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
237 |
shellAndArgs := OperatingSystem commandAndArgsForOSCommand:aCommand.
|
|
238 |
shell := shellAndArgs at:1.
|
|
239 |
args := (shellAndArgs at:2) ? ''.
|
|
240 |
] ifFalse:[
|
|
241 |
aCommand isNil ifTrue:[
|
|
242 |
shell := OperatingSystem getEnvironment:'SHELL'.
|
|
243 |
shell size == 0 ifTrue:[
|
|
244 |
shell := '/bin/sh'.
|
|
245 |
].
|
|
246 |
cmd := shell asFilename baseName.
|
|
247 |
args := (Array with:cmd).
|
|
248 |
] ifFalse:[
|
|
249 |
shell := '/bin/sh'.
|
|
250 |
args := (Array with:'sh' with:'-c' with:aCommand).
|
|
251 |
].
|
|
252 |
env := Dictionary new.
|
|
253 |
env declareAllFrom:envIn.
|
|
254 |
env at:'SHELL' put:shell.
|
|
255 |
].
|
|
256 |
|
|
257 |
blocked := OperatingSystem blockInterrupts.
|
|
258 |
|
|
259 |
shellPid := Processor
|
|
260 |
monitor:[
|
|
261 |
OperatingSystem
|
|
262 |
exec:shell
|
|
263 |
withArguments:args
|
|
264 |
environment:env
|
3038
|
265 |
fileDescriptors:execFDArray
|
3028
|
266 |
fork:true
|
|
267 |
newPgrp:true
|
|
268 |
inDirectory:aDirectory.
|
|
269 |
]
|
|
270 |
action:[:status |
|
|
271 |
Debug ifTrue:[
|
|
272 |
Transcript show:'pid:'; showCR:status pid.
|
|
273 |
Transcript show:'status:'; showCR:status status.
|
|
274 |
Transcript show:'code:'; showCR:status code.
|
|
275 |
Transcript show:'core:'; showCR:status core.
|
|
276 |
].
|
|
277 |
status stillAlive ifFalse:[
|
|
278 |
exitStatus := status.
|
|
279 |
OperatingSystem closePid:shellPid.
|
|
280 |
shellPid := nil.
|
|
281 |
terminatedAction value
|
|
282 |
].
|
|
283 |
].
|
|
284 |
|
|
285 |
blocked ifFalse:[
|
|
286 |
OperatingSystem unblockInterrupts
|
|
287 |
].
|
|
288 |
|
|
289 |
"close the slave side of the pty/pipes (only used by the child)"
|
|
290 |
pty notNil ifTrue:[
|
|
291 |
(pty at:2) close.
|
|
292 |
].
|
|
293 |
|
3088
|
294 |
didOpenTerminal ifTrue:[
|
|
295 |
stdOutToStxPipe notNil ifTrue:[
|
|
296 |
(stdOutToStxPipe at:2) close.
|
|
297 |
(stxToStdinPipe at:1) close.
|
|
298 |
].
|
3028
|
299 |
|
3088
|
300 |
shellPid isNil ifTrue:[
|
3069
|
301 |
"/ self warn:'Cannot start shell'.
|
3088
|
302 |
outStream close.
|
|
303 |
inStream close.
|
|
304 |
inStream := outStream := nil.
|
|
305 |
].
|
3028
|
306 |
].
|
3069
|
307 |
^ shellPid
|
3028
|
308 |
|
3069
|
309 |
"Created: / 20-07-1998 / 18:19:32 / cg"
|
|
310 |
"Modified: / 01-08-2013 / 20:38:37 / cg"
|
3028
|
311 |
! !
|
|
312 |
|
|
313 |
!TerminalSession methodsFor:'input / output'!
|
|
314 |
|
|
315 |
paste:someText
|
|
316 |
"paste - redefined to send the chars to the shell instead
|
|
317 |
of pasting into the view"
|
|
318 |
|
|
319 |
|s nLines|
|
|
320 |
|
|
321 |
s := someText.
|
|
322 |
s isString ifTrue:[
|
|
323 |
s := s asStringCollection
|
|
324 |
] ifFalse:[
|
|
325 |
(s isKindOf:StringCollection) ifFalse:[
|
|
326 |
self warn:'selection (' , s class name , ') is not convertable to Text'.
|
|
327 |
^ self
|
|
328 |
]
|
|
329 |
].
|
|
330 |
(nLines := s size) == 0 ifTrue:[^ self].
|
|
331 |
(nLines == 1 and:[(s at:1) size == 0]) ifTrue:[^ self].
|
|
332 |
s keysAndValuesDo:[:idx :line |
|
|
333 |
line notNil ifTrue:[inStream nextPutAll:line].
|
|
334 |
idx ~~ nLines ifTrue:[
|
|
335 |
self sendLineEnd.
|
|
336 |
]
|
|
337 |
].
|
|
338 |
|
|
339 |
"Modified: / 12.6.1998 / 22:12:47 / cg"
|
|
340 |
!
|
|
341 |
|
|
342 |
sendLine:aString
|
|
343 |
inStream nextPutAll:aString.
|
|
344 |
self sendLineEnd
|
|
345 |
!
|
|
346 |
|
|
347 |
sendLineEnd
|
|
348 |
OperatingSystem isMSDOSlike ifTrue:[
|
|
349 |
inStream nextPut:Character return.
|
|
350 |
inStream nextPut:Character linefeed.
|
|
351 |
] ifFalse:[
|
|
352 |
inStream nextPut:Character return.
|
|
353 |
].
|
|
354 |
! !
|
|
355 |
|
|
356 |
!TerminalSession methodsFor:'misc'!
|
|
357 |
|
3087
|
358 |
collectedOutput
|
3097
|
359 |
"return any collected output, so far"
|
|
360 |
|
|
361 |
collectedOutput isNil ifTrue:[^ nil].
|
|
362 |
^ collectedOutput contents
|
3087
|
363 |
!
|
|
364 |
|
3028
|
365 |
defineWindowSizeLines:numberOfLines columns:numberOfColumns
|
|
366 |
| delta prevNumCols prevNumLines|
|
|
367 |
|
|
368 |
"/ any idea, how to do this under windows ?
|
|
369 |
|
|
370 |
OperatingSystem isUNIXlike ifTrue:[
|
|
371 |
"/
|
|
372 |
"/ tell the pty;
|
|
373 |
"/ tell the shell;
|
|
374 |
"/
|
|
375 |
(inStream notNil
|
|
376 |
and:[inStream isExternalStream
|
|
377 |
and:[inStream isOpen]]) ifTrue:[
|
|
378 |
Debug ifTrue:[
|
|
379 |
Transcript showCR:'TerminalSession [info]: changed len to ', numberOfLines printString.
|
|
380 |
].
|
|
381 |
(OperatingSystem
|
|
382 |
setWindowSizeOnFileDescriptor:inStream fileDescriptor
|
|
383 |
width:numberOfColumns
|
|
384 |
height:numberOfLines) ifFalse:[
|
|
385 |
Debug ifTrue:[
|
|
386 |
Transcript showCR:'TerminalSession [warning]: cannot change windowSize'.
|
|
387 |
].
|
|
388 |
].
|
|
389 |
|
|
390 |
].
|
|
391 |
shellPid notNil ifTrue:[
|
|
392 |
OperatingSystem sendSignal:OperatingSystem sigWINCH to:shellPid
|
|
393 |
]
|
|
394 |
].
|
|
395 |
|
|
396 |
"Created: / 11.6.1998 / 22:51:39 / cg"
|
|
397 |
"Modified: / 5.5.1999 / 19:45:09 / cg"
|
|
398 |
!
|
|
399 |
|
3110
|
400 |
forgetPrompt:aString
|
|
401 |
promptActions removeKey:aString
|
|
402 |
!
|
|
403 |
|
3087
|
404 |
onPrompt:aString do:aBlock
|
|
405 |
"remember what to do, when a prompt arrives;
|
|
406 |
notice: will only start checking for prompt, when startCollectingOutput
|
|
407 |
has been called."
|
|
408 |
|
3095
|
409 |
promptActions := Dictionary new
|
|
410 |
at:aString put:aBlock;
|
|
411 |
yourself
|
|
412 |
!
|
|
413 |
|
|
414 |
onPrompt:string1 do:block1 onPrompt:string2 do:block2
|
|
415 |
"remember what to do, when a prompt arrives;
|
|
416 |
notice: will only start checking for prompt, when startCollectingOutput
|
|
417 |
has been called."
|
|
418 |
|
|
419 |
promptActions := Dictionary new
|
|
420 |
at:string1 put:block1;
|
|
421 |
at:string2 put:block2;
|
|
422 |
yourself
|
3087
|
423 |
!
|
|
424 |
|
|
425 |
outputFromAction:aBlock prompt:prompt timeout:seconds
|
|
426 |
"evaluate aBlock and wait for the prompt.
|
|
427 |
return gdb output as string collection"
|
|
428 |
|
|
429 |
|sema output|
|
|
430 |
|
|
431 |
sema := Semaphore new.
|
|
432 |
self startCollectingOutput.
|
|
433 |
self onPrompt:prompt do:[:strings | output := strings. sema signal. ].
|
|
434 |
|
|
435 |
aBlock value.
|
|
436 |
|
|
437 |
(sema waitWithTimeout:seconds) isNil ifTrue:[
|
|
438 |
self information:'Error: command timeout.'.
|
|
439 |
self stopCollectingOutput.
|
|
440 |
self onPrompt:nil do:nil.
|
|
441 |
AbortOperationRequest raise.
|
|
442 |
].
|
3111
|
443 |
output notEmptyOrNil ifTrue:[
|
|
444 |
output first isEmpty ifTrue:[
|
|
445 |
"/ self halt.
|
|
446 |
output := output copyFrom:2
|
|
447 |
].
|
3087
|
448 |
].
|
|
449 |
^ output
|
|
450 |
!
|
|
451 |
|
|
452 |
outputFromCommand:aCommand prompt:prompt timeout:seconds
|
|
453 |
"return a command's output as string collection"
|
|
454 |
|
|
455 |
|output firstLine|
|
|
456 |
|
|
457 |
output := self
|
|
458 |
outputFromAction:[ self sendLine:aCommand ]
|
|
459 |
prompt:prompt timeout:seconds.
|
3111
|
460 |
output isEmptyOrNil ifTrue:[^ output].
|
3087
|
461 |
|
|
462 |
"/ the first line of output is the echo
|
|
463 |
firstLine := output first withoutLeadingSeparators.
|
|
464 |
firstLine ~= aCommand ifTrue:[
|
|
465 |
OperatingSystem isMSWINDOWSlike ifFalse:[
|
|
466 |
"/ sigh - it is sometimes truncated (to be investigated)
|
|
467 |
(aCommand startsWith:firstLine) ifTrue:[
|
|
468 |
self halt.
|
|
469 |
^ output.
|
|
470 |
].
|
|
471 |
self halt.
|
|
472 |
].
|
|
473 |
^ output.
|
|
474 |
].
|
|
475 |
^ output copyFrom:2
|
|
476 |
!
|
|
477 |
|
3028
|
478 |
sendInterruptSignal
|
|
479 |
"send an INT-signal to the shell (UNIX only)"
|
|
480 |
|
|
481 |
shellPid notNil ifTrue:[
|
|
482 |
OperatingSystem sendSignal:(OperatingSystem sigINT) to:shellPid negated.
|
|
483 |
].
|
|
484 |
|
|
485 |
"Modified: / 10.6.1998 / 17:49:49 / cg"
|
|
486 |
!
|
|
487 |
|
|
488 |
sendKillSignal
|
|
489 |
"send a KILL-signal to the shell (UNIX only)"
|
|
490 |
|
|
491 |
shellPid notNil ifTrue:[
|
|
492 |
OperatingSystem sendSignal:(OperatingSystem sigKILL) to:shellPid negated.
|
|
493 |
]
|
3087
|
494 |
!
|
|
495 |
|
|
496 |
startCollectingOutput
|
3097
|
497 |
"start collecting output in a collecting stream"
|
|
498 |
|
3087
|
499 |
collectedOutput := '' writeStream.
|
|
500 |
!
|
|
501 |
|
|
502 |
stopCollectingOutput
|
3097
|
503 |
"start collecting output in a collecting stream"
|
|
504 |
|
3087
|
505 |
collectedOutput := nil.
|
3028
|
506 |
! !
|
|
507 |
|
|
508 |
!TerminalSession methodsFor:'reader process'!
|
|
509 |
|
3087
|
510 |
collectOutputAndCheckForPrompt:buffer count:n
|
3110
|
511 |
|string collectedString collectedLines i i2 lastLine|
|
3087
|
512 |
|
|
513 |
collectedOutput isNil ifTrue:[^ self].
|
|
514 |
|
|
515 |
string := buffer copyTo:n.
|
|
516 |
collectedOutput nextPutAll:string.
|
|
517 |
|
3095
|
518 |
promptActions notNil ifTrue:[
|
3087
|
519 |
collectedString := collectedOutput contents.
|
|
520 |
i := collectedString lastIndexOf:Character lf.
|
|
521 |
i ~= 0 ifTrue:[
|
|
522 |
lastLine := (collectedString copyFrom:i+1) withoutTrailingSeparators.
|
3110
|
523 |
lastLine isEmpty ifTrue:[
|
|
524 |
i2 := collectedString lastIndexOf:Character lf startingAt:(i-1).
|
|
525 |
i2 ~= 0 ifTrue:[
|
|
526 |
lastLine := (collectedString copyFrom:i2+1 to:i-1) withoutTrailingSeparators.
|
|
527 |
].
|
|
528 |
].
|
|
529 |
"/ Transcript show:lastLine; showCR:lastLine asByteArray.
|
3095
|
530 |
promptActions keysAndValuesDo:[:expectedPrompt :promptAction |
|
|
531 |
|
|
532 |
((lastLine endsWith:expectedPrompt)
|
|
533 |
or:[ (lastLine startsWith:expectedPrompt) ]) ifTrue:[
|
|
534 |
"/ ('found prompt; call ',promptAction printString) printCR.
|
3087
|
535 |
|
3095
|
536 |
"/ perform the promptaction
|
|
537 |
collectedLines := collectedString asStringCollection
|
|
538 |
collect:[:each |
|
|
539 |
(each endsWith:String crlf)
|
|
540 |
ifTrue:[ each copyButLast:2 ]
|
|
541 |
ifFalse:[
|
|
542 |
(each endsWith:Character return)
|
|
543 |
ifTrue:[ each copyButLast:1 ]
|
|
544 |
ifFalse:[ each ]]].
|
|
545 |
collectedLines removeLast. "/ the prompt itself
|
|
546 |
promptAction value: collectedLines.
|
|
547 |
]
|
|
548 |
].
|
3087
|
549 |
].
|
|
550 |
].
|
|
551 |
!
|
|
552 |
|
3028
|
553 |
readAnyAvailableData
|
|
554 |
"read data from the stream,
|
|
555 |
and sends me #processInput:n: events if something arrived.
|
|
556 |
Returns the amount of data read."
|
|
557 |
|
|
558 |
|buffer n bufferSize|
|
|
559 |
|
|
560 |
outStream isNil ifTrue:[^ 0]. "/ already closed
|
|
561 |
|
|
562 |
bufferSize := 256.
|
|
563 |
buffer := String new:bufferSize.
|
|
564 |
|
|
565 |
ExternalStream readErrorSignal handle:[:ex |
|
|
566 |
n := 0
|
|
567 |
] do:[
|
|
568 |
n := outStream nextAvailableBytes:bufferSize into:buffer startingAt:1.
|
|
569 |
n > 0 ifTrue:[
|
3087
|
570 |
collectedOutput notNil ifTrue:[
|
|
571 |
self collectOutputAndCheckForPrompt:buffer count:n
|
|
572 |
].
|
3028
|
573 |
pluggableProcessInputAction notNil ifTrue:[
|
|
574 |
pluggableProcessInputAction value:buffer value:n.
|
|
575 |
]
|
|
576 |
].
|
|
577 |
].
|
|
578 |
^ n
|
|
579 |
!
|
|
580 |
|
|
581 |
readerProcessLoop
|
|
582 |
"look for the session's output"
|
|
583 |
|
|
584 |
StreamError handle:[:ex |
|
|
585 |
Transcript show:'Terminal(PTY-reader) [error]: '; showCR:ex description.
|
|
586 |
] do:[
|
|
587 |
[true] whileTrue:[
|
|
588 |
AbortOperationRequest handle:[:ex |
|
3096
|
589 |
^ self
|
3028
|
590 |
] do:[
|
|
591 |
|n sensor|
|
|
592 |
|
|
593 |
readerDelay notNil ifTrue:[ Delay waitForSeconds:readerDelay].
|
|
594 |
outStream readWait.
|
|
595 |
|
|
596 |
(pluggableCheckBeforeReadAction isNil
|
|
597 |
or:[pluggableCheckBeforeReadAction value]) ifTrue:[
|
|
598 |
n := self readAnyAvailableData.
|
|
599 |
n == 0 ifTrue:[
|
|
600 |
"/ Windows IPC has a bug - it always
|
|
601 |
"/ returns 0 (when the command is idle)
|
|
602 |
"/ and says it's at the end (sigh)
|
|
603 |
|
|
604 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
605 |
Delay waitForSeconds:0.1
|
|
606 |
] ifFalse:[
|
|
607 |
outStream atEnd ifTrue:[
|
|
608 |
outStream close. outStream := nil.
|
|
609 |
inStream close. inStream := nil.
|
|
610 |
Processor activeProcess terminate.
|
|
611 |
] ifFalse:[
|
|
612 |
"/ this should not happen.
|
|
613 |
|
|
614 |
Delay waitForSeconds:0.1
|
|
615 |
]
|
|
616 |
].
|
|
617 |
]
|
|
618 |
]
|
|
619 |
]
|
|
620 |
]
|
|
621 |
]
|
|
622 |
!
|
|
623 |
|
|
624 |
startReaderProcess
|
|
625 |
"Start a reader process, which looks for the commands output,
|
|
626 |
and sends me #processInput:n: events whenever something arrives."
|
|
627 |
|
|
628 |
readerProcess isNil ifTrue:[
|
|
629 |
readerProcess := [
|
|
630 |
[
|
|
631 |
self readerProcessLoop.
|
|
632 |
] ifCurtailed:[
|
|
633 |
readerProcess := nil
|
|
634 |
]
|
|
635 |
] fork. "/ forkAt:9.
|
|
636 |
readerProcess name:'pty reader'.
|
|
637 |
]
|
|
638 |
|
|
639 |
"
|
|
640 |
VT100TerminalView openShell
|
|
641 |
"
|
|
642 |
|
|
643 |
"Modified: / 5.5.1999 / 17:58:02 / cg"
|
|
644 |
"Modified: / 28.1.2002 / 21:10:13 / micha"
|
|
645 |
!
|
|
646 |
|
|
647 |
stopReaderProcess
|
|
648 |
"stop the background reader thread"
|
|
649 |
|
3037
|
650 |
|p|
|
|
651 |
|
|
652 |
(p := readerProcess) notNil ifTrue:[
|
|
653 |
readerProcess := nil.
|
|
654 |
p terminate.
|
3028
|
655 |
"/ give it a chance to really terminate
|
|
656 |
Processor yield.
|
|
657 |
].
|
|
658 |
! !
|
|
659 |
|
|
660 |
!TerminalSession class methodsFor:'documentation'!
|
|
661 |
|
|
662 |
version
|
3111
|
663 |
^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.15 2013-08-23 12:02:58 cg Exp $'
|
3028
|
664 |
!
|
|
665 |
|
|
666 |
version_CVS
|
3111
|
667 |
^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.15 2013-08-23 12:02:58 cg Exp $'
|
3028
|
668 |
! !
|
|
669 |
|
|
670 |
|
|
671 |
TerminalSession initialize!
|