#UI_ENHANCEMENT by cg
class: TerminalSession
added: #isOpen
comment/format in:
#createTerminalConnectionAndSetupWith:
#errStream
#inStream
#outStream
#pluggableCheckBeforeReadAction:
#pluggableProcessInputAction:
#terminatedAction:
changed: #startCommand:in:environment:setupTerminalWith:terminatedAction:
--- a/TerminalSession.st Fri Dec 22 15:36:27 2017 +0100
+++ b/TerminalSession.st Sat Dec 23 13:34:32 2017 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
"
COPYRIGHT (c) 2013 by eXept Software AG
All Rights Reserved
@@ -83,23 +85,32 @@
!TerminalSession methodsFor:'accessing'!
errStream
+ "the stdErrToStx stream.
+ Stuff written by the process' to its stderr will appear here"
+
^ errStream
!
inStream
+ "the stxTostdIn stream.
+ Stuff written to this stream will appear at the process' stdin"
+
^ inStream
!
outStream
+ "the process' stdOutToStx stream.
+ Stuff written by the process to its stdout will appear here"
+
^ outStream
!
-pluggableCheckBeforeReadAction:something
- pluggableCheckBeforeReadAction := something.
+pluggableCheckBeforeReadAction:aBlockOrNil
+ pluggableCheckBeforeReadAction := aBlockOrNil.
!
-pluggableProcessInputAction:something
- pluggableProcessInputAction := something.
+pluggableProcessInputAction:aBlockOrNil
+ pluggableProcessInputAction := aBlockOrNil.
!
pty
@@ -123,7 +134,7 @@
!
terminatedAction:aBlock
- "hook to callback when terminated"
+ "hook to be called when terminated"
terminatedAction := aBlock.
@@ -178,12 +189,12 @@
!
createTerminalConnectionAndSetupWith:setupBlock
- "create a terminal conenction (pseudo terminal or pipe)"
+ "create a terminal connection (pseudo terminal or pipe)"
|slaveFD master slave ptyTriple|
OperatingSystem isMSWINDOWSlike ifTrue:[
- "use two pipes to COMMAND.COM"
+ "use two pipes (eg. to COMMAND.COM)"
stxToStdinPipe := NonPositionableExternalStream makePipe.
stxToStdinPipe isNil ifTrue:[
self error:(self class classResources string:'Could not create pipe to COMMAND.COM.') mayProceed:true.
@@ -215,10 +226,11 @@
^ self.
].
+ "/ pty at:1 is the master;
+ "/ pty at:2 is the slave
+ "/ pty at:3 is the name of the pty
ptyName := ptyTriple at:3.
- "/ pty at:1 is the master;
- "/ pty at:2 is the slave
master := NonPositionableExternalStream forReadWriteToFileDescriptor:(ptyTriple at:1).
master buffered:false.
@@ -235,6 +247,13 @@
].
!
+isOpen
+ (inStream notNil and:[inStream isOpen]) ifTrue:[^ true].
+ (outStream notNil and:[outStream isOpen]) ifTrue:[^ true].
+ (errStream notNil and:[errStream isOpen]) ifTrue:[^ true].
+ ^ false
+!
+
killShell
"shut down my shell process and stop the background reader thread."
@@ -264,13 +283,13 @@
!
startCommand:aCommand in:aDirectory environment:envIn setupTerminalWith:setupBlock terminatedAction:terminatedActionArg
- "start a command on a pseudo terminal. If the command arg is nil,
- a shell is started. If aDirectory is not nil, the command is
- executed in that directory.
- Also fork a reader process, to read the shell's output and
- tell me, whenever something arrives"
+ "start a command on a pseudo terminal.
+ If the command arg is nil, a shell is started.
+ If aDirectory is nil, the command is executed in the current directory.
+ Also fork a reader process, to read the shell's output
+ and tell me, whenever something arrives"
- |blocked exitStatus
+ |exitStatus
cmd shell args env shellAndArgs didOpenTerminal|
shellCommand := aCommand.
@@ -278,6 +297,9 @@
terminatedAction := terminatedActionArg.
didOpenTerminal := false.
+ env := Dictionary new.
+ env declareAllFrom:envIn.
+
(inStream isNil or:[outStream isNil]) ifTrue:[
self createTerminalConnectionAndSetupWith:setupBlock.
didOpenTerminal := true.
@@ -298,15 +320,15 @@
shell := '/bin/sh'.
args := (Array with:'sh' with:'-c' with:aCommand).
].
- env := Dictionary new.
- env declareAllFrom:envIn.
env at:'SHELL' put:shell.
].
- blocked := OperatingSystem blockInterrupts.
-
shellPid := Processor
monitor:[
+ Debug ifTrue:[
+ Transcript show:'exec:'; show:shell.
+ Transcript show:' '; showCR:args.
+ ].
OperatingSystem
exec:shell
withArguments:args
@@ -332,10 +354,6 @@
].
].
- blocked ifFalse:[
- OperatingSystem unblockInterrupts
- ].
-
"close the slave side of the pty/pipes (only used by the child)"
pty notNil ifTrue:[
(pty at:2) close.