class: TerminalSession
authorClaus Gittinger <cg@exept.de>
Sat, 10 May 2014 01:50:53 +0200
changeset 3276 180b977dcbe5
parent 3275 0ca755975c5e
child 3277 06babe114ad2
class: TerminalSession added: #outputFromAction:prompt:timeout:to: #outputFromCommand:prompt:timeout:to: #startCollectingOutputTo: changed:6 methods
TerminalSession.st
--- a/TerminalSession.st	Wed May 07 16:49:15 2014 +0200
+++ b/TerminalSession.st	Sat May 10 01:50:53 2014 +0200
@@ -299,8 +299,8 @@
 
         shellPid isNil ifTrue:[
         "/ self warn:'Cannot start shell'.
-            outStream close.
-            inStream close.
+            outStream notNil ifTrue:[outStream close].
+            inStream notNil ifTrue:[inStream close].
             inStream := outStream := nil.
         ].
     ].
@@ -430,10 +430,25 @@
     "evaluate aBlock and wait for the prompt.
      return gdb output as string collection"
 
+    ^ self
+        outputFromAction:aBlock 
+        prompt:prompt 
+        timeout:seconds
+        to:nil
+!
+
+outputFromAction:aBlock prompt:prompt timeout:seconds to:aStreamOrNil
+    "evaluate aBlock and wait for the prompt.
+     return gdb output as string collection"
+
     |sema output lastSize gotPrompt|
 
     sema := Semaphore new.
-    self startCollectingOutput.
+    aStreamOrNil isNil ifTrue:[
+        self startCollectingOutput.
+    ] ifFalse:[
+        self startCollectingOutputTo:aStreamOrNil
+    ].
     self onPrompt:prompt do:[:strings | output := strings. sema signal. ].
 
     aBlock value.
@@ -467,11 +482,23 @@
 outputFromCommand:aCommand prompt:prompt timeout:seconds
     "return a command's output as string collection"
 
+    ^ self
+        outputFromCommand:aCommand 
+        prompt:prompt 
+        timeout:seconds 
+        to:nil
+!
+
+outputFromCommand:aCommand prompt:prompt timeout:seconds to:aStreamOrNil
+    "return a command's output as string collection"
+
     |output firstLine|
 
     output := self 
                 outputFromAction:[ self sendLine:aCommand ]
-                prompt:prompt timeout:seconds.
+                prompt:prompt 
+                timeout:seconds
+                to:aStreamOrNil.
     output isEmptyOrNil ifTrue:[^ output].
 
     "/ the first line of output is the echo
@@ -509,7 +536,13 @@
 startCollectingOutput
     "start collecting output in a collecting stream"
 
-    collectedOutput := '' writeStream.
+    self startCollectingOutputTo:(WriteStream on:(String new:1000)).
+!
+
+startCollectingOutputTo:aStream
+    "start collecting output into a collecting (or other) stream"
+
+    collectedOutput := aStream.
 !
 
 stopCollectingOutput
@@ -568,7 +601,7 @@
      and sends me #processInput:n: events if something arrived.
      Returns the amount of data read."
 
-    |buffer n bufferSize|
+    |buffer bufferSize n|
 
     outStream isNil ifTrue:[^ 0].   "/ already closed
 
@@ -578,14 +611,29 @@
     ExternalStream readErrorSignal handle:[:ex |
         n := 0
     ] do:[
-        n := outStream nextAvailableBytes:bufferSize into:buffer startingAt:1.
-        n > 0 ifTrue:[
-            collectedOutput notNil ifTrue:[
-                self collectOutputAndCheckForPrompt:buffer count:n
+        |line|
+
+        collectedOutput class == ActorStream ifTrue:[
+            (outStream readWaitWithTimeout:0.5) ifTrue:[
+                n := 0
+            ] ifFalse:[
+                line := outStream nextLine,Character cr.
+                n := line size.
+                self collectOutputAndCheckForPrompt:line count:n.
+                pluggableProcessInputAction notNil ifTrue:[
+                    pluggableProcessInputAction value:line value:n.
+                ]
+            ]
+        ] ifFalse:[
+            n := outStream nextAvailableBytes:bufferSize into:buffer startingAt:1.
+            n > 0 ifTrue:[
+                collectedOutput notNil ifTrue:[
+                    self collectOutputAndCheckForPrompt:buffer count:n
+                ].
+                pluggableProcessInputAction notNil ifTrue:[
+                    pluggableProcessInputAction value:buffer value:n.
+                ]
             ].
-            pluggableProcessInputAction notNil ifTrue:[
-                pluggableProcessInputAction value:buffer value:n.
-            ]
         ].
     ].
     ^ n
@@ -604,6 +652,7 @@
                 |n sensor|
 
                 readerDelay notNil ifTrue:[ Delay waitForSeconds:readerDelay].
+                outStream isNil ifTrue:[^ self].
                 outStream readWait.
 
                 (pluggableCheckBeforeReadAction isNil
@@ -673,11 +722,11 @@
 !TerminalSession class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.18 2013-11-06 16:22:04 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.19 2014-05-09 23:50:53 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.18 2013-11-06 16:22:04 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.19 2014-05-09 23:50:53 cg Exp $'
 ! !