4271
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2013 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 |
"
|
3028
|
12 |
"{ Package: 'stx:libbasic2' }"
|
|
13 |
|
3595
|
14 |
"{ NameSpace: Smalltalk }"
|
|
15 |
|
3028
|
16 |
Object subclass:#TerminalSession
|
|
17 |
instanceVariableNames:'inStream outStream errStream readerProcess shellPid shellCommand
|
|
18 |
shellDirectory readerDelay pluggableCheckBeforeReadAction
|
3038
|
19 |
pluggableProcessInputAction execFDArray stxToStdinPipe
|
3087
|
20 |
stdOutToStxPipe pty ptyName terminatedAction collectedOutput
|
3095
|
21 |
promptActions'
|
3028
|
22 |
classVariableNames:'Debug'
|
|
23 |
poolDictionaries:''
|
|
24 |
category:'Views-TerminalViews'
|
|
25 |
!
|
|
26 |
|
|
27 |
!TerminalSession class methodsFor:'documentation'!
|
|
28 |
|
4271
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 2013 by eXept Software AG
|
|
32 |
All Rights Reserved
|
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
|
|
41 |
"
|
|
42 |
!
|
|
43 |
|
3028
|
44 |
documentation
|
|
45 |
"
|
4639
|
46 |
This is basically a TerminalView without a view.
|
|
47 |
|
3028
|
48 |
This keeps the state and API to interact with another program
|
|
49 |
via a terminal session. Under Unix, a pseudo-tty connection
|
|
50 |
is used; other operating systems might use other mechanisms.
|
4027
|
51 |
|
3315
|
52 |
This is (currently) used by the GDBApplication, to interact
|
3981
|
53 |
with gdb, cscope and the program.
|
|
54 |
It can be used wherever more control is needed than a simple pipe
|
3028
|
55 |
offers (such as terminal emulation, window size, CTRL-c support etc.)
|
|
56 |
|
4027
|
57 |
A major feature is a mechanism to catch certain prompt strings of the output
|
|
58 |
and get a notification.
|
|
59 |
This is very useful, if a program's output is to be shown both in a terminalview,
|
|
60 |
and also to be analyzed for robot actions.
|
|
61 |
Applications for this are telnet like interaction, interactions with a debugger
|
|
62 |
or monitor (gdb, android-adb) etc.,
|
|
63 |
|
|
64 |
A lot of code has been extracted from TerminalView,
|
|
65 |
which will be refactored, once this is stable.
|
3981
|
66 |
For now, there is some code duplication (as of Summer 2014).
|
3315
|
67 |
|
|
68 |
outStream - the controlled program's output (a pty-half)
|
|
69 |
inStream - the controlled program's input (a pty-half)
|
|
70 |
errStream - the controlled program's output (a pty-half)
|
3028
|
71 |
"
|
4639
|
72 |
!
|
|
73 |
|
|
74 |
examples
|
|
75 |
"
|
|
76 |
|session|
|
|
77 |
|
|
78 |
session := TerminalSession new.
|
|
79 |
session startReaderProcess.
|
|
80 |
session
|
|
81 |
pluggableProcessInputAction:[:buffer :n |
|
|
82 |
Transcript show:(buffer copyTo:n).
|
|
83 |
Transcript endEntry.
|
|
84 |
].
|
|
85 |
session
|
|
86 |
startCommand:'ls -l' in:'~'
|
|
87 |
environment:nil
|
|
88 |
setupTerminalWith:[]
|
|
89 |
terminatedAction:[ Transcript showCR:'finished' ].
|
|
90 |
|
|
91 |
session
|
|
92 |
startCommand:'(cd ~/work/cg/schemeNew ; make)' in:'~'
|
|
93 |
environment:nil
|
|
94 |
setupTerminalWith:[]
|
|
95 |
terminatedAction:[ Transcript showCR:'finished' ].
|
|
96 |
|
|
97 |
session
|
|
98 |
startCommand:'(ls ~/work/cg/schemeNew)' in:'~'
|
|
99 |
environment:nil
|
|
100 |
setupTerminalWith:[]
|
|
101 |
terminatedAction:[ Transcript showCR:'finished' ].
|
|
102 |
"
|
3028
|
103 |
! !
|
|
104 |
|
|
105 |
!TerminalSession class methodsFor:'initialization'!
|
|
106 |
|
|
107 |
initialize
|
|
108 |
|
|
109 |
Debug := false.
|
|
110 |
|
|
111 |
"
|
|
112 |
self initialize
|
4639
|
113 |
Debug := true.
|
3028
|
114 |
"
|
|
115 |
! !
|
|
116 |
|
|
117 |
!TerminalSession methodsFor:'accessing'!
|
|
118 |
|
|
119 |
errStream
|
4550
|
120 |
"the stdErrToStx stream.
|
|
121 |
Stuff written by the process' to its stderr will appear here"
|
|
122 |
|
3028
|
123 |
^ errStream
|
|
124 |
!
|
|
125 |
|
|
126 |
inStream
|
4550
|
127 |
"the stxTostdIn stream.
|
|
128 |
Stuff written to this stream will appear at the process' stdin"
|
|
129 |
|
3028
|
130 |
^ inStream
|
|
131 |
!
|
|
132 |
|
|
133 |
outStream
|
4550
|
134 |
"the process' stdOutToStx stream.
|
|
135 |
Stuff written by the process to its stdout will appear here"
|
|
136 |
|
3028
|
137 |
^ outStream
|
|
138 |
!
|
|
139 |
|
4550
|
140 |
pluggableCheckBeforeReadAction:aBlockOrNil
|
|
141 |
pluggableCheckBeforeReadAction := aBlockOrNil.
|
3028
|
142 |
!
|
|
143 |
|
4550
|
144 |
pluggableProcessInputAction:aBlockOrNil
|
|
145 |
pluggableProcessInputAction := aBlockOrNil.
|
3028
|
146 |
!
|
|
147 |
|
3039
|
148 |
pty
|
|
149 |
^ pty
|
|
150 |
!
|
|
151 |
|
3040
|
152 |
ptyName
|
|
153 |
^ ptyName
|
|
154 |
!
|
|
155 |
|
3028
|
156 |
shellCommand
|
|
157 |
^ shellCommand
|
|
158 |
!
|
|
159 |
|
|
160 |
shellDirectory
|
|
161 |
^ shellDirectory
|
|
162 |
!
|
|
163 |
|
|
164 |
shellPid
|
|
165 |
^ shellPid
|
3084
|
166 |
!
|
|
167 |
|
4301
|
168 |
terminatedAction:aBlock
|
4550
|
169 |
"hook to be called when terminated"
|
4301
|
170 |
|
|
171 |
terminatedAction := aBlock.
|
|
172 |
|
|
173 |
"Modified (comment): / 07-02-2017 / 16:05:55 / cg"
|
3028
|
174 |
! !
|
|
175 |
|
|
176 |
!TerminalSession methodsFor:'initialization & release'!
|
|
177 |
|
|
178 |
closeDownShell
|
4553
|
179 |
"shut down my shell process"
|
3028
|
180 |
|
4639
|
181 |
self closeDownShell:1
|
|
182 |
!
|
|
183 |
|
|
184 |
closeDownShell:waitTimeInSeconds
|
|
185 |
"shut down my shell process"
|
|
186 |
|
|
187 |
|pid waitTime|
|
3028
|
188 |
|
|
189 |
(pid := shellPid) notNil ifTrue:[
|
4639
|
190 |
Logger info:'killing shell pid=%1' with:shellPid.
|
3028
|
191 |
Debug ifTrue:[
|
4639
|
192 |
Logger info:'killing shell pid=%1' with:pid.
|
3028
|
193 |
].
|
4639
|
194 |
OperatingSystem terminateProcessGroup:pid.
|
3028
|
195 |
OperatingSystem terminateProcess:pid.
|
4639
|
196 |
|
|
197 |
waitTime := 0.
|
|
198 |
[shellPid notNil and:[waitTime < waitTimeInSeconds]] whileTrue:[
|
|
199 |
Delay waitForSeconds:0.1.
|
|
200 |
waitTime := waitTime + 0.1
|
|
201 |
].
|
|
202 |
Logger info:'shell pid after SIGTERM=%1' with:shellPid.
|
|
203 |
|
|
204 |
"/ still not dead?
|
3028
|
205 |
shellPid notNil ifTrue:[
|
4643
|
206 |
Logger info:'still not dead after %1' with:waitTimeInSeconds.
|
3028
|
207 |
OperatingSystem isMSWINDOWSlike ifFalse:[
|
|
208 |
OperatingSystem killProcessGroup:pid.
|
|
209 |
].
|
|
210 |
OperatingSystem killProcess:pid.
|
|
211 |
shellPid := nil.
|
|
212 |
].
|
|
213 |
OperatingSystem closePid:pid.
|
|
214 |
].
|
|
215 |
|
|
216 |
"Modified: / 5.5.1999 / 18:43:02 / cg"
|
|
217 |
!
|
|
218 |
|
4553
|
219 |
closeDownShellAndStopReaderProcess
|
4625
|
220 |
"shut down my shell process, stop the background reader thread
|
|
221 |
and close the streams"
|
4553
|
222 |
|
|
223 |
self closeDownShell.
|
|
224 |
self stopReaderProcess.
|
|
225 |
self closeStreams
|
|
226 |
!
|
|
227 |
|
3028
|
228 |
closeStreams
|
4554
|
229 |
|s|
|
|
230 |
|
3037
|
231 |
self stopReaderProcess.
|
|
232 |
|
4554
|
233 |
(s := inStream) notNil ifTrue:[
|
|
234 |
inStream := nil.
|
|
235 |
s isStream ifTrue:[s close].
|
3028
|
236 |
].
|
4554
|
237 |
(s := outStream) notNil ifTrue:[
|
|
238 |
outStream := nil.
|
|
239 |
s close.
|
3028
|
240 |
].
|
4554
|
241 |
(s := errStream) notNil ifTrue:[
|
|
242 |
errStream := nil.
|
|
243 |
s close.
|
3028
|
244 |
].
|
|
245 |
!
|
|
246 |
|
3038
|
247 |
createTerminalConnectionAndSetupWith:setupBlock
|
4550
|
248 |
"create a terminal connection (pseudo terminal or pipe)"
|
3038
|
249 |
|
3040
|
250 |
|slaveFD master slave ptyTriple|
|
3038
|
251 |
|
|
252 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
4550
|
253 |
"use two pipes (eg. to COMMAND.COM)"
|
5198
|
254 |
stxToStdinPipe := PipeStream makePipe.
|
3038
|
255 |
stxToStdinPipe isNil ifTrue:[
|
|
256 |
self error:(self class classResources string:'Could not create pipe to COMMAND.COM.') mayProceed:true.
|
|
257 |
^ self.
|
|
258 |
].
|
|
259 |
|
5198
|
260 |
stdOutToStxPipe := PipeStream makePipe.
|
3038
|
261 |
stdOutToStxPipe isNil ifTrue:[
|
|
262 |
self error:(self class classResources classResources string:'Could not create pipe from COMMAND.COM.') mayProceed:true.
|
|
263 |
^ self.
|
|
264 |
].
|
|
265 |
|
|
266 |
"/ pipe readSide is p at:1;
|
|
267 |
"/ writeSide is p at:2
|
|
268 |
|
|
269 |
slaveFD := (stdOutToStxPipe at:2) fileDescriptor.
|
|
270 |
execFDArray := Array
|
|
271 |
with:(stxToStdinPipe at:1) fileDescriptor "stdin"
|
|
272 |
with:slaveFD "stdout"
|
|
273 |
with:slaveFD. "stderr"
|
|
274 |
|
|
275 |
outStream := stdOutToStxPipe at:1.
|
|
276 |
inStream := stxToStdinPipe at:2.
|
|
277 |
] ifFalse:[
|
|
278 |
"Use a pseudo-tty"
|
3040
|
279 |
ptyTriple := OperatingSystem makePTY.
|
|
280 |
ptyTriple isNil ifTrue:[
|
3038
|
281 |
self warn:'Cannot open pty.'.
|
|
282 |
^ self.
|
|
283 |
].
|
|
284 |
|
5365
|
285 |
"/ pty at:1 is the masterFD;
|
|
286 |
"/ pty at:2 is the slaveFD
|
4550
|
287 |
"/ pty at:3 is the name of the pty
|
5198
|
288 |
ptyName := ptyTriple at:3 ifAbsent:'pty?'.
|
3039
|
289 |
|
5198
|
290 |
master := UnixPTYStream forReadWriteToFileDescriptor:(ptyTriple at:1).
|
3039
|
291 |
master buffered:false.
|
5198
|
292 |
master setCommandString:ptyName.
|
3039
|
293 |
|
5198
|
294 |
slave := UnixPTYStream forReadWriteToFileDescriptor:(ptyTriple at:2).
|
3040
|
295 |
slave buffered:false.
|
5198
|
296 |
slave setCommandString:ptyName.
|
3040
|
297 |
|
|
298 |
pty := { master . slave }.
|
|
299 |
|
3039
|
300 |
inStream := outStream := master.
|
3038
|
301 |
setupBlock value.
|
|
302 |
"/ fork a shell process on the slave-side
|
3040
|
303 |
slaveFD := (ptyTriple at:2).
|
3038
|
304 |
execFDArray := Array with:slaveFD with:slaveFD with:slaveFD.
|
|
305 |
].
|
|
306 |
!
|
|
307 |
|
3028
|
308 |
reinitialize
|
|
309 |
shellPid := nil.
|
|
310 |
inStream := outStream := errStream := nil.
|
|
311 |
!
|
|
312 |
|
3084
|
313 |
startCommand:aCommand in:aDirectory environment:envIn setupTerminalWith:setupBlock terminatedAction:terminatedActionArg
|
4550
|
314 |
"start a command on a pseudo terminal.
|
|
315 |
If the command arg is nil, a shell is started.
|
|
316 |
If aDirectory is nil, the command is executed in the current directory.
|
|
317 |
Also fork a reader process, to read the shell's output
|
|
318 |
and tell me, whenever something arrives"
|
3028
|
319 |
|
4550
|
320 |
|exitStatus
|
3088
|
321 |
cmd shell args env shellAndArgs didOpenTerminal|
|
3028
|
322 |
|
|
323 |
shellCommand := aCommand.
|
|
324 |
shellDirectory := aDirectory.
|
3084
|
325 |
terminatedAction := terminatedActionArg.
|
3088
|
326 |
didOpenTerminal := false.
|
3028
|
327 |
|
4550
|
328 |
env := Dictionary new.
|
|
329 |
env declareAllFrom:envIn.
|
|
330 |
|
3085
|
331 |
(inStream isNil or:[outStream isNil]) ifTrue:[
|
|
332 |
self createTerminalConnectionAndSetupWith:setupBlock.
|
3088
|
333 |
didOpenTerminal := true.
|
3085
|
334 |
].
|
3028
|
335 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
336 |
shellAndArgs := OperatingSystem commandAndArgsForOSCommand:aCommand.
|
|
337 |
shell := shellAndArgs at:1.
|
|
338 |
args := (shellAndArgs at:2) ? ''.
|
|
339 |
] ifFalse:[
|
|
340 |
aCommand isNil ifTrue:[
|
|
341 |
shell := OperatingSystem getEnvironment:'SHELL'.
|
|
342 |
shell size == 0 ifTrue:[
|
|
343 |
shell := '/bin/sh'.
|
|
344 |
].
|
|
345 |
cmd := shell asFilename baseName.
|
|
346 |
args := (Array with:cmd).
|
|
347 |
] ifFalse:[
|
|
348 |
shell := '/bin/sh'.
|
|
349 |
args := (Array with:'sh' with:'-c' with:aCommand).
|
|
350 |
].
|
|
351 |
env at:'SHELL' put:shell.
|
|
352 |
].
|
|
353 |
|
|
354 |
shellPid := Processor
|
|
355 |
monitor:[
|
4550
|
356 |
Debug ifTrue:[
|
4639
|
357 |
Logger info:'exec: "%1" args: "%2"' with:shell with:args.
|
4550
|
358 |
].
|
3028
|
359 |
OperatingSystem
|
|
360 |
exec:shell
|
|
361 |
withArguments:args
|
|
362 |
environment:env
|
3038
|
363 |
fileDescriptors:execFDArray
|
3028
|
364 |
fork:true
|
|
365 |
newPgrp:true
|
4060
|
366 |
inDirectory:aDirectory
|
|
367 |
showWindow:false.
|
3028
|
368 |
]
|
|
369 |
action:[:status |
|
|
370 |
Debug ifTrue:[
|
4639
|
371 |
Logger info:'pid: %1' with:status pid.
|
|
372 |
Logger info:'status %1:' with:status status.
|
|
373 |
Logger info:'code: %1' with:status code.
|
|
374 |
Logger info:'core %1:' with:status core.
|
3028
|
375 |
].
|
|
376 |
status stillAlive ifFalse:[
|
|
377 |
exitStatus := status.
|
|
378 |
OperatingSystem closePid:shellPid.
|
|
379 |
shellPid := nil.
|
3135
|
380 |
terminatedAction valueWithOptionalArgument:status
|
3028
|
381 |
].
|
|
382 |
].
|
|
383 |
|
4639
|
384 |
Logger info:'started shell pid: %1' with:shellPid.
|
|
385 |
|
3028
|
386 |
"close the slave side of the pty/pipes (only used by the child)"
|
|
387 |
pty notNil ifTrue:[
|
|
388 |
(pty at:2) close.
|
|
389 |
].
|
|
390 |
|
3088
|
391 |
didOpenTerminal ifTrue:[
|
|
392 |
stdOutToStxPipe notNil ifTrue:[
|
|
393 |
(stdOutToStxPipe at:2) close.
|
|
394 |
(stxToStdinPipe at:1) close.
|
|
395 |
].
|
3028
|
396 |
|
3088
|
397 |
shellPid isNil ifTrue:[
|
3069
|
398 |
"/ self warn:'Cannot start shell'.
|
3276
|
399 |
outStream notNil ifTrue:[outStream close].
|
|
400 |
inStream notNil ifTrue:[inStream close].
|
3088
|
401 |
inStream := outStream := nil.
|
|
402 |
].
|
3028
|
403 |
].
|
3069
|
404 |
^ shellPid
|
3028
|
405 |
|
3069
|
406 |
"Created: / 20-07-1998 / 18:19:32 / cg"
|
|
407 |
"Modified: / 01-08-2013 / 20:38:37 / cg"
|
3028
|
408 |
! !
|
|
409 |
|
|
410 |
!TerminalSession methodsFor:'input / output'!
|
|
411 |
|
|
412 |
paste:someText
|
|
413 |
"paste - redefined to send the chars to the shell instead
|
|
414 |
of pasting into the view"
|
|
415 |
|
|
416 |
|s nLines|
|
|
417 |
|
|
418 |
s := someText.
|
|
419 |
s isString ifTrue:[
|
|
420 |
s := s asStringCollection
|
|
421 |
] ifFalse:[
|
|
422 |
(s isKindOf:StringCollection) ifFalse:[
|
4643
|
423 |
self warn:'selection (' , s class name , ') is not convertable to text'.
|
3028
|
424 |
^ self
|
|
425 |
]
|
|
426 |
].
|
|
427 |
(nLines := s size) == 0 ifTrue:[^ self].
|
|
428 |
(nLines == 1 and:[(s at:1) size == 0]) ifTrue:[^ self].
|
|
429 |
s keysAndValuesDo:[:idx :line |
|
|
430 |
line notNil ifTrue:[inStream nextPutAll:line].
|
|
431 |
idx ~~ nLines ifTrue:[
|
|
432 |
self sendLineEnd.
|
|
433 |
]
|
|
434 |
].
|
|
435 |
|
|
436 |
"Modified: / 12.6.1998 / 22:12:47 / cg"
|
|
437 |
!
|
|
438 |
|
4676
|
439 |
send:aString
|
|
440 |
inStream nextPutAll:aString.
|
|
441 |
|
|
442 |
"Created: / 26-05-2018 / 11:32:04 / Claus Gittinger"
|
|
443 |
!
|
|
444 |
|
3112
|
445 |
sendCharacter:aCharacter
|
|
446 |
inStream nextPut:aCharacter.
|
|
447 |
!
|
|
448 |
|
3028
|
449 |
sendLine:aString
|
|
450 |
inStream nextPutAll:aString.
|
|
451 |
self sendLineEnd
|
|
452 |
!
|
|
453 |
|
|
454 |
sendLineEnd
|
|
455 |
OperatingSystem isMSDOSlike ifTrue:[
|
|
456 |
inStream nextPut:Character return.
|
|
457 |
inStream nextPut:Character linefeed.
|
|
458 |
] ifFalse:[
|
|
459 |
inStream nextPut:Character return.
|
|
460 |
].
|
|
461 |
! !
|
|
462 |
|
|
463 |
!TerminalSession methodsFor:'misc'!
|
|
464 |
|
3087
|
465 |
collectedOutput
|
3097
|
466 |
"return any collected output, so far"
|
|
467 |
|
|
468 |
collectedOutput isNil ifTrue:[^ nil].
|
|
469 |
^ collectedOutput contents
|
3087
|
470 |
!
|
|
471 |
|
3028
|
472 |
defineWindowSizeLines:numberOfLines columns:numberOfColumns
|
|
473 |
"/ any idea, how to do this under windows ?
|
|
474 |
OperatingSystem isUNIXlike ifTrue:[
|
|
475 |
"/
|
|
476 |
"/ tell the pty;
|
|
477 |
"/ tell the shell;
|
|
478 |
"/
|
|
479 |
(inStream notNil
|
|
480 |
and:[inStream isExternalStream
|
|
481 |
and:[inStream isOpen]]) ifTrue:[
|
|
482 |
Debug ifTrue:[
|
4639
|
483 |
Logger info:'TerminalSession [info]: changed len to %1' with:numberOfLines.
|
3028
|
484 |
].
|
|
485 |
(OperatingSystem
|
|
486 |
setWindowSizeOnFileDescriptor:inStream fileDescriptor
|
|
487 |
width:numberOfColumns
|
|
488 |
height:numberOfLines) ifFalse:[
|
|
489 |
Debug ifTrue:[
|
4639
|
490 |
Logger info:'TerminalSession [warning]: cannot change windowSize'.
|
3028
|
491 |
].
|
|
492 |
].
|
|
493 |
|
|
494 |
].
|
|
495 |
shellPid notNil ifTrue:[
|
|
496 |
OperatingSystem sendSignal:OperatingSystem sigWINCH to:shellPid
|
|
497 |
]
|
|
498 |
].
|
|
499 |
|
|
500 |
"Created: / 11.6.1998 / 22:51:39 / cg"
|
|
501 |
"Modified: / 5.5.1999 / 19:45:09 / cg"
|
|
502 |
!
|
|
503 |
|
3110
|
504 |
forgetPrompt:aString
|
4027
|
505 |
"/ Transcript show:'forget prompt: '; showCR:aString.
|
|
506 |
promptActions removeKey:aString ifAbsent:[].
|
|
507 |
"/ Transcript show:'prompts now: '; showCR:promptActions.
|
3110
|
508 |
!
|
|
509 |
|
3087
|
510 |
onPrompt:aString do:aBlock
|
|
511 |
"remember what to do, when a prompt arrives;
|
|
512 |
notice: will only start checking for prompt, when startCollectingOutput
|
|
513 |
has been called."
|
|
514 |
|
4027
|
515 |
"/ Transcript show:'add prompt: '; showCR:aString.
|
|
516 |
promptActions isNil ifTrue:[promptActions := Dictionary new].
|
|
517 |
promptActions at:aString put:aBlock
|
3095
|
518 |
!
|
|
519 |
|
|
520 |
onPrompt:string1 do:block1 onPrompt:string2 do:block2
|
|
521 |
"remember what to do, when a prompt arrives;
|
|
522 |
notice: will only start checking for prompt, when startCollectingOutput
|
|
523 |
has been called."
|
|
524 |
|
4027
|
525 |
"/ Transcript show:'add prompt: '; showCR:string1.
|
|
526 |
"/ Transcript show:'add prompt: '; showCR:string2.
|
|
527 |
promptActions isNil ifTrue:[promptActions := Dictionary new].
|
|
528 |
promptActions
|
|
529 |
at:string1 put:block1;
|
|
530 |
at:string2 put:block2.
|
3087
|
531 |
!
|
|
532 |
|
|
533 |
outputFromAction:aBlock prompt:prompt timeout:seconds
|
|
534 |
"evaluate aBlock and wait for the prompt.
|
|
535 |
return gdb output as string collection"
|
|
536 |
|
3276
|
537 |
^ self
|
|
538 |
outputFromAction:aBlock
|
|
539 |
prompt:prompt
|
|
540 |
timeout:seconds
|
|
541 |
to:nil
|
|
542 |
!
|
|
543 |
|
|
544 |
outputFromAction:aBlock prompt:prompt timeout:seconds to:aStreamOrNil
|
|
545 |
"evaluate aBlock and wait for the prompt.
|
|
546 |
return gdb output as string collection"
|
|
547 |
|
3112
|
548 |
|sema output lastSize gotPrompt|
|
3087
|
549 |
|
|
550 |
sema := Semaphore new.
|
3276
|
551 |
aStreamOrNil isNil ifTrue:[
|
|
552 |
self startCollectingOutput.
|
|
553 |
] ifFalse:[
|
|
554 |
self startCollectingOutputTo:aStreamOrNil
|
|
555 |
].
|
4551
|
556 |
self onPrompt:prompt do:[:strings | output := strings. sema signalOnce. ].
|
3087
|
557 |
|
|
558 |
aBlock value.
|
|
559 |
|
3112
|
560 |
lastSize := 0.
|
|
561 |
[
|
|
562 |
|newSize|
|
|
563 |
|
|
564 |
(gotPrompt := (sema waitWithTimeout:seconds) notNil) ifFalse:[
|
|
565 |
newSize := collectedOutput size.
|
4551
|
566 |
self debuggingCodeFor:#cg is:[
|
4639
|
567 |
Logger info:'timeout - output size is: %1' with:newSize.
|
4551
|
568 |
(newSize between:1 and:1000) ifTrue:[
|
4639
|
569 |
Logger info:'output is: "%1"' with:collectedOutput contents.
|
4551
|
570 |
].
|
|
571 |
].
|
|
572 |
|
3595
|
573 |
newSize > 100000 ifTrue:[
|
|
574 |
self stopCollectingOutput.
|
|
575 |
self onPrompt:nil do:nil.
|
|
576 |
collectedOutput := nil.
|
5365
|
577 |
TimeoutError raiseRequestErrorString:'command output too big'.
|
3595
|
578 |
].
|
3112
|
579 |
newSize = lastSize ifTrue:[
|
|
580 |
"/ self information:'Error: command timeout.'.
|
|
581 |
self stopCollectingOutput.
|
|
582 |
self onPrompt:nil do:nil.
|
5365
|
583 |
TimeoutError raiseRequestErrorString:'command timeout'.
|
3112
|
584 |
].
|
|
585 |
lastSize := newSize.
|
|
586 |
].
|
4551
|
587 |
] doUntil:[ gotPrompt ].
|
3112
|
588 |
|
3111
|
589 |
output notEmptyOrNil ifTrue:[
|
|
590 |
output first isEmpty ifTrue:[
|
|
591 |
"/ self halt.
|
|
592 |
output := output copyFrom:2
|
|
593 |
].
|
3087
|
594 |
].
|
|
595 |
^ output
|
|
596 |
!
|
|
597 |
|
|
598 |
outputFromCommand:aCommand prompt:prompt timeout:seconds
|
|
599 |
"return a command's output as string collection"
|
|
600 |
|
3276
|
601 |
^ self
|
|
602 |
outputFromCommand:aCommand
|
|
603 |
prompt:prompt
|
|
604 |
timeout:seconds
|
|
605 |
to:nil
|
|
606 |
!
|
|
607 |
|
|
608 |
outputFromCommand:aCommand prompt:prompt timeout:seconds to:aStreamOrNil
|
|
609 |
"return a command's output as string collection"
|
|
610 |
|
3087
|
611 |
|output firstLine|
|
|
612 |
|
|
613 |
output := self
|
|
614 |
outputFromAction:[ self sendLine:aCommand ]
|
3276
|
615 |
prompt:prompt
|
|
616 |
timeout:seconds
|
|
617 |
to:aStreamOrNil.
|
3111
|
618 |
output isEmptyOrNil ifTrue:[^ output].
|
3087
|
619 |
|
|
620 |
"/ the first line of output is the echo
|
|
621 |
firstLine := output first withoutLeadingSeparators.
|
|
622 |
firstLine ~= aCommand ifTrue:[
|
3135
|
623 |
(aCommand startsWith:firstLine) ifTrue:[
|
3087
|
624 |
"/ sigh - it is sometimes truncated (to be investigated)
|
4028
|
625 |
self breakPoint:#cg.
|
3135
|
626 |
^ output.
|
3087
|
627 |
].
|
3135
|
628 |
"/ self halt.
|
3087
|
629 |
^ output.
|
|
630 |
].
|
|
631 |
^ output copyFrom:2
|
|
632 |
!
|
|
633 |
|
3028
|
634 |
sendInterruptSignal
|
|
635 |
"send an INT-signal to the shell (UNIX only)"
|
|
636 |
|
|
637 |
shellPid notNil ifTrue:[
|
4551
|
638 |
OperatingSystem isUNIXlike ifTrue:[
|
|
639 |
OperatingSystem interruptProcessGroup:shellPid.
|
|
640 |
OperatingSystem interruptProcess:shellPid.
|
|
641 |
] ifFalse:[
|
5391
|
642 |
OperatingSystem sendCtrlCToProcess:shellPid.
|
|
643 |
"/ 'TerminalSession [info]: IRQ unimplemented for DOS' infoPrintCR.
|
4551
|
644 |
].
|
|
645 |
] ifFalse:[
|
|
646 |
'TerminalSession [info]: no shell' infoPrintCR.
|
3028
|
647 |
].
|
|
648 |
|
|
649 |
"Modified: / 10.6.1998 / 17:49:49 / cg"
|
|
650 |
!
|
|
651 |
|
|
652 |
sendKillSignal
|
|
653 |
"send a KILL-signal to the shell (UNIX only)"
|
|
654 |
|
|
655 |
shellPid notNil ifTrue:[
|
3722
|
656 |
OperatingSystem killProcessGroup:shellPid.
|
|
657 |
OperatingSystem killProcess:shellPid.
|
|
658 |
OperatingSystem childProcessWait:false pid:shellPid.
|
3028
|
659 |
]
|
3087
|
660 |
!
|
|
661 |
|
|
662 |
startCollectingOutput
|
3097
|
663 |
"start collecting output in a collecting stream"
|
|
664 |
|
3276
|
665 |
self startCollectingOutputTo:(WriteStream on:(String new:1000)).
|
|
666 |
!
|
|
667 |
|
|
668 |
startCollectingOutputTo:aStream
|
|
669 |
"start collecting output into a collecting (or other) stream"
|
|
670 |
|
|
671 |
collectedOutput := aStream.
|
3087
|
672 |
!
|
|
673 |
|
|
674 |
stopCollectingOutput
|
3097
|
675 |
"start collecting output in a collecting stream"
|
|
676 |
|
3087
|
677 |
collectedOutput := nil.
|
3028
|
678 |
! !
|
|
679 |
|
4552
|
680 |
!TerminalSession methodsFor:'queries'!
|
|
681 |
|
|
682 |
isOpen
|
|
683 |
(inStream notNil and:[inStream isOpen]) ifTrue:[^ true].
|
|
684 |
(outStream notNil and:[outStream isOpen]) ifTrue:[^ true].
|
|
685 |
(errStream notNil and:[errStream isOpen]) ifTrue:[^ true].
|
|
686 |
^ false
|
|
687 |
! !
|
|
688 |
|
3028
|
689 |
!TerminalSession methodsFor:'reader process'!
|
|
690 |
|
3087
|
691 |
collectOutputAndCheckForPrompt:buffer count:n
|
3110
|
692 |
|string collectedString collectedLines i i2 lastLine|
|
3087
|
693 |
|
|
694 |
collectedOutput isNil ifTrue:[^ self].
|
|
695 |
|
|
696 |
string := buffer copyTo:n.
|
|
697 |
collectedOutput nextPutAll:string.
|
|
698 |
|
4027
|
699 |
"/ Transcript showCR:'prompts: '; showCR:promptActions.
|
3095
|
700 |
promptActions notNil ifTrue:[
|
3087
|
701 |
collectedString := collectedOutput contents.
|
4643
|
702 |
i := collectedString lastIndexOf:(Character lf).
|
3713
|
703 |
i ~~ 0 ifTrue:[
|
3087
|
704 |
lastLine := (collectedString copyFrom:i+1) withoutTrailingSeparators.
|
3110
|
705 |
lastLine isEmpty ifTrue:[
|
4643
|
706 |
i2 := collectedString lastIndexOf:(Character lf) startingAt:(i-1).
|
3713
|
707 |
i2 ~~ 0 ifTrue:[
|
3110
|
708 |
lastLine := (collectedString copyFrom:i2+1 to:i-1) withoutTrailingSeparators.
|
|
709 |
].
|
|
710 |
].
|
4027
|
711 |
"/ Transcript show:' got: <'; show:lastLine; showCR:'>'. "/ ; showCR:lastLine asByteArray.
|
3095
|
712 |
promptActions keysAndValuesDo:[:expectedPrompt :promptAction |
|
4027
|
713 |
"/ Transcript show:' looking for: '; showCR:expectedPrompt.
|
3095
|
714 |
((lastLine endsWith:expectedPrompt)
|
|
715 |
or:[ (lastLine startsWith:expectedPrompt) ]) ifTrue:[
|
|
716 |
"/ ('found prompt; call ',promptAction printString) printCR.
|
3087
|
717 |
|
3095
|
718 |
"/ perform the promptaction
|
|
719 |
collectedLines := collectedString asStringCollection
|
|
720 |
collect:[:each |
|
|
721 |
(each endsWith:String crlf)
|
|
722 |
ifTrue:[ each copyButLast:2 ]
|
|
723 |
ifFalse:[
|
|
724 |
(each endsWith:Character return)
|
3317
|
725 |
ifTrue:[ each copyButLast ]
|
3095
|
726 |
ifFalse:[ each ]]].
|
|
727 |
collectedLines removeLast. "/ the prompt itself
|
|
728 |
promptAction value: collectedLines.
|
4027
|
729 |
].
|
3095
|
730 |
].
|
3087
|
731 |
].
|
|
732 |
].
|
|
733 |
!
|
|
734 |
|
3028
|
735 |
readAnyAvailableData
|
|
736 |
"read data from the stream,
|
|
737 |
and sends me #processInput:n: events if something arrived.
|
|
738 |
Returns the amount of data read."
|
|
739 |
|
3276
|
740 |
|buffer bufferSize n|
|
3028
|
741 |
|
|
742 |
outStream isNil ifTrue:[^ 0]. "/ already closed
|
|
743 |
|
3132
|
744 |
bufferSize := 1024.
|
3028
|
745 |
buffer := String new:bufferSize.
|
|
746 |
|
4186
|
747 |
StreamError handle:[:ex |
|
5365
|
748 |
self debuggingCodeFor:#cg is:[
|
|
749 |
Transcript showCR:'stream error: ',ex description.
|
|
750 |
].
|
|
751 |
n := 0.
|
3028
|
752 |
] do:[
|
3276
|
753 |
|line|
|
|
754 |
|
|
755 |
collectedOutput class == ActorStream ifTrue:[
|
|
756 |
(outStream readWaitWithTimeout:0.5) ifTrue:[
|
|
757 |
n := 0
|
|
758 |
] ifFalse:[
|
|
759 |
line := outStream nextLine,Character cr.
|
5365
|
760 |
self debuggingCodeFor:#cg is:[
|
|
761 |
Transcript showCR:('line: "',line,'"').
|
|
762 |
].
|
3276
|
763 |
n := line size.
|
|
764 |
pluggableProcessInputAction notNil ifTrue:[
|
|
765 |
pluggableProcessInputAction value:line value:n.
|
3285
|
766 |
].
|
|
767 |
collectedOutput notNil ifTrue:[
|
|
768 |
self collectOutputAndCheckForPrompt:line count:n.
|
3276
|
769 |
]
|
|
770 |
]
|
|
771 |
] ifFalse:[
|
|
772 |
n := outStream nextAvailableBytes:bufferSize into:buffer startingAt:1.
|
|
773 |
n > 0 ifTrue:[
|
5365
|
774 |
self debuggingCodeFor:#cg is:[
|
|
775 |
Transcript showCR:('buffer: "',(buffer copyTo:n),'"').
|
|
776 |
].
|
3285
|
777 |
pluggableProcessInputAction notNil ifTrue:[
|
|
778 |
pluggableProcessInputAction value:buffer value:n.
|
|
779 |
].
|
3276
|
780 |
collectedOutput notNil ifTrue:[
|
|
781 |
self collectOutputAndCheckForPrompt:buffer count:n
|
|
782 |
].
|
3087
|
783 |
].
|
3028
|
784 |
].
|
|
785 |
].
|
|
786 |
^ n
|
|
787 |
!
|
|
788 |
|
|
789 |
readerProcessLoop
|
|
790 |
"look for the session's output"
|
|
791 |
|
|
792 |
StreamError handle:[:ex |
|
|
793 |
Transcript show:'Terminal(PTY-reader) [error]: '; showCR:ex description.
|
|
794 |
] do:[
|
4641
|
795 |
|outStreamWasNonNil|
|
|
796 |
|
|
797 |
outStreamWasNonNil := false.
|
3028
|
798 |
[true] whileTrue:[
|
|
799 |
AbortOperationRequest handle:[:ex |
|
3096
|
800 |
^ self
|
3028
|
801 |
] do:[
|
|
802 |
|n sensor|
|
|
803 |
|
|
804 |
readerDelay notNil ifTrue:[ Delay waitForSeconds:readerDelay].
|
4639
|
805 |
outStream isNil ifTrue:[
|
4641
|
806 |
outStreamWasNonNil ifTrue:[^ self].
|
4639
|
807 |
Delay waitForSeconds:0.1
|
|
808 |
] ifFalse:[
|
4641
|
809 |
outStreamWasNonNil := true.
|
4639
|
810 |
outStream readWait.
|
3028
|
811 |
|
4639
|
812 |
(pluggableCheckBeforeReadAction isNil
|
|
813 |
or:[pluggableCheckBeforeReadAction value]) ifTrue:[
|
|
814 |
n := self readAnyAvailableData.
|
|
815 |
n == 0 ifTrue:[
|
|
816 |
"/ Windows IPC has a bug - it always
|
|
817 |
"/ returns 0 (when the command is idle)
|
|
818 |
"/ and says it's at the end (sigh)
|
|
819 |
OperatingSystem isMSWINDOWSlike ifTrue:[
|
|
820 |
Delay waitForSeconds:0.1
|
3028
|
821 |
] ifFalse:[
|
4639
|
822 |
outStream atEnd ifTrue:[
|
|
823 |
outStream close. outStream := nil.
|
|
824 |
inStream close. inStream := nil.
|
|
825 |
Processor activeProcess terminate.
|
|
826 |
] ifFalse:[
|
|
827 |
"/ this should not happen.
|
|
828 |
Delay waitForSeconds:0.1
|
|
829 |
]
|
|
830 |
].
|
|
831 |
]
|
3028
|
832 |
]
|
|
833 |
]
|
|
834 |
]
|
|
835 |
]
|
|
836 |
]
|
|
837 |
!
|
|
838 |
|
|
839 |
startReaderProcess
|
|
840 |
"Start a reader process, which looks for the commands output,
|
|
841 |
and sends me #processInput:n: events whenever something arrives."
|
|
842 |
|
4639
|
843 |
Logger info:'start reader'.
|
3028
|
844 |
readerProcess isNil ifTrue:[
|
|
845 |
readerProcess := [
|
|
846 |
[
|
|
847 |
self readerProcessLoop.
|
|
848 |
] ifCurtailed:[
|
4642
|
849 |
"/ read any remaining data
|
|
850 |
(self readAnyAvailableData > 0) ifTrue:[
|
5072
|
851 |
"/ self halt:'to check if this solves the make problem'.
|
4642
|
852 |
].
|
|
853 |
readerProcess := nil.
|
3028
|
854 |
]
|
|
855 |
] fork. "/ forkAt:9.
|
|
856 |
readerProcess name:'pty reader'.
|
|
857 |
]
|
|
858 |
|
|
859 |
"
|
|
860 |
VT100TerminalView openShell
|
|
861 |
"
|
|
862 |
|
|
863 |
"Modified: / 5.5.1999 / 17:58:02 / cg"
|
|
864 |
"Modified: / 28.1.2002 / 21:10:13 / micha"
|
|
865 |
!
|
|
866 |
|
|
867 |
stopReaderProcess
|
|
868 |
"stop the background reader thread"
|
|
869 |
|
3037
|
870 |
|p|
|
|
871 |
|
4639
|
872 |
Logger info:'stop reader'.
|
3037
|
873 |
(p := readerProcess) notNil ifTrue:[
|
|
874 |
readerProcess := nil.
|
|
875 |
p terminate.
|
3028
|
876 |
"/ give it a chance to really terminate
|
|
877 |
Processor yield.
|
4642
|
878 |
[p isDead] whileFalse:[
|
|
879 |
Delay waitForSeconds:0.05
|
4641
|
880 |
].
|
3028
|
881 |
].
|
|
882 |
! !
|
|
883 |
|
|
884 |
!TerminalSession class methodsFor:'documentation'!
|
|
885 |
|
|
886 |
version
|
3595
|
887 |
^ '$Header$'
|
3028
|
888 |
!
|
|
889 |
|
|
890 |
version_CVS
|
3595
|
891 |
^ '$Header$'
|
3028
|
892 |
! !
|
|
893 |
|
|
894 |
|
|
895 |
TerminalSession initialize!
|