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