TerminalSession.st
author Claus Gittinger <cg@exept.de>
Sun, 07 Jul 2013 09:39:35 +0200
changeset 3037 47f022308e7d
parent 3028 e49cf8755fd6
child 3038 c418ee215311
permissions -rw-r--r--
class: TerminalSession changed: #closeStreams #stopReaderProcess
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3028
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:libbasic2' }"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
Object subclass:#TerminalSession
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
	instanceVariableNames:'inStream outStream errStream readerProcess shellPid shellCommand
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
		shellDirectory readerDelay pluggableCheckBeforeReadAction
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
		pluggableProcessInputAction'
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	classVariableNames:'Debug'
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
	poolDictionaries:''
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
	category:'Views-TerminalViews'
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
!TerminalSession class methodsFor:'documentation'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
documentation
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
    This keeps the state and API to interact with another program
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
    via a terminal session. Under Unix, a pseudo-tty connection
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
    is used; other operating systems might use other mechanisms.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
    This is (currently) mostly used by the TerminalView application,
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
    but can be used whereever more control is needed than a simple pipe
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
    offers (such as terminal emulation, window size, CTRL-c support etc.)
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
    Extracted from TerminalView, which is refactored, once this is stable.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
!TerminalSession class methodsFor:'initialization'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
initialize
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
    Debug := false.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
    "
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
     self initialize
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
    "
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
!TerminalSession methodsFor:'accessing'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
errStream
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
    ^ errStream
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
inStream
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
    ^ inStream
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
outStream
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
    ^ outStream
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
pluggableCheckBeforeReadAction:something
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
    pluggableCheckBeforeReadAction := something.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
pluggableProcessInputAction:something
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    pluggableProcessInputAction := something.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
shellCommand
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
    ^ shellCommand
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
shellDirectory
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    ^ shellDirectory
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
shellPid
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    ^ shellPid
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
!TerminalSession methodsFor:'initialization & release'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
closeDownShell
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
    "shut down my shell process and stop the background reader thread."
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
    |pid|
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
    (pid := shellPid) notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
        Debug ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
            Transcript print:'killing shell pid='; showCR:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
        OperatingSystem terminateProcessGroup:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
        OperatingSystem terminateProcess:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
        Delay waitForSeconds:1.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
        shellPid notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
            OperatingSystem isMSWINDOWSlike ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
                OperatingSystem killProcessGroup:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
            ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
            OperatingSystem killProcess:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
            shellPid := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
        OperatingSystem closePid:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
    "Modified: / 5.5.1999 / 18:43:02 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
closeStreams
3037
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   101
    self stopReaderProcess.
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   102
3028
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
    inStream notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
        inStream isStream ifTrue:[inStream close].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
        inStream := nil
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
    outStream notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
        outStream close.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
        outStream := nil
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
    errStream notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
        errStream close.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
        errStream := nil
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
killShell
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
    "shut down my shell process and stop the background reader thread."
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
    |pid|
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
    (pid := shellPid) notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
        Debug ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
            Transcript show:'killing shell pid='; showCR:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
        OperatingSystem terminateProcessGroup:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
        OperatingSystem terminateProcess:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
        Delay waitForSeconds:1.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
        shellPid notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
            OperatingSystem isMSWINDOWSlike ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
                OperatingSystem killProcessGroup:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
            ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
            OperatingSystem killProcess:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
            shellPid := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
        OperatingSystem closePid:pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
reinitialize
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
    shellPid := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
    inStream := outStream := errStream := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
startCommand:aCommand in:aDirectory environment:envIn setupTerminalWith:setupBlock terminatedAction:terminatedAction
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
    "start a command on a pseudo terminal. If the command arg is nil,
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
     a shell is started. If aDirectory is not nil, the command is
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
     executed in that directory.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
     Also fork a reader process, to read the shell's output and
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
     tell me, whenever something arrives"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
    |pty slaveFD execFdArray blocked exitStatus 
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
     stxToStdinPipe stdOutToStxPipe cmd shell args env shellAndArgs|
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
    shellCommand := aCommand.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
    shellDirectory := aDirectory.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
    OperatingSystem isMSWINDOWSlike ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
        "use two pipes to COMMAND.COM"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
        stxToStdinPipe := NonPositionableExternalStream makePipe.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
        stxToStdinPipe isNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
            self warn:(self class classResources string:'Could not create pipe to COMMAND.COM.'). 
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
            ^ self.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
        stdOutToStxPipe := NonPositionableExternalStream makePipe.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
        stdOutToStxPipe isNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
            self warn:(self class classResources classResources string:'Could not create pipe from COMMAND.COM.'). 
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
            ^ self.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
        "/ pipe readSide is p at:1;
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
        "/      writeSide is p at:2
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
        slaveFD := (stdOutToStxPipe at:2) fileDescriptor.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
        execFdArray := Array 
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
                         with:(stxToStdinPipe at:1) fileDescriptor        "stdin"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
                         with:slaveFD                                       "stdout"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
                         with:slaveFD.                                      "stderr"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
        outStream := stdOutToStxPipe at:1.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
        inStream  := stxToStdinPipe at:2.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
        shellAndArgs := OperatingSystem commandAndArgsForOSCommand:aCommand.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
        shell := shellAndArgs at:1.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
        args  := (shellAndArgs at:2) ? ''.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
    ] ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
        "Use a pseudo-tty"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
        pty := NonPositionableExternalStream makePTYPair.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
        pty isNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
            self warn:'Cannot open pty.'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
            ^ self.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
        "/ pty at:1 is the master;
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
        "/ pty at:2 is the slave
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
        inStream := outStream := (pty at:1).
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
        setupBlock value.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
        "/ fork a shell process on the slave-side
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
        slaveFD := (pty at:2) fileDescriptor.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
        execFdArray := Array with:slaveFD with:slaveFD with:slaveFD.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
        aCommand isNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
            shell := OperatingSystem getEnvironment:'SHELL'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
            shell size == 0 ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
                shell := '/bin/sh'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
            ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
            cmd := shell asFilename baseName.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
            args := (Array with:cmd).
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
        ] ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
            shell := '/bin/sh'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
            args := (Array with:'sh' with:'-c' with:aCommand).
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
        env := Dictionary new.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
        env declareAllFrom:envIn.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
        env at:'SHELL' put:shell.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
    blocked := OperatingSystem blockInterrupts.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
    shellPid := Processor
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
               monitor:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
                  OperatingSystem
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
                      exec:shell
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
                      withArguments:args
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
                      environment:env
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
                      fileDescriptors:execFdArray
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
                      fork:true
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
                      newPgrp:true
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
                      inDirectory:aDirectory.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
               ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
               action:[:status |
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
                    Debug ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
                        Transcript show:'pid:'; showCR:status pid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
                        Transcript show:'status:'; showCR:status status.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
                        Transcript show:'code:'; showCR:status code.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
                        Transcript show:'core:'; showCR:status core.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
                    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
                    status stillAlive ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
                        exitStatus := status.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
                        OperatingSystem closePid:shellPid.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
                        shellPid := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
                        terminatedAction value
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
                    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
               ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
    blocked ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
        OperatingSystem unblockInterrupts
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
    "close the slave side of the pty/pipes (only used by the child)"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
    pty notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
        (pty at:2) close.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
    stdOutToStxPipe notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
        (stdOutToStxPipe at:2) close.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
        (stxToStdinPipe at:1) close.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
    shellPid isNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
        self warn:'Cannot start shell'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
        outStream close.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
        inStream close.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
        inStream := outStream := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
        ^ self.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
    "Created: / 20.7.1998 / 18:19:32 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
    "Modified: / 5.5.1999 / 17:28:37 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
!TerminalSession methodsFor:'input / output'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
paste:someText
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
    "paste - redefined to send the chars to the shell instead
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
     of pasting into the view"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
    |s nLines|
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
    s := someText.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
    s isString ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
        s := s asStringCollection
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
    ] ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
        (s isKindOf:StringCollection) ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
            self warn:'selection (' , s class name , ') is not convertable to Text'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
            ^ self
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
        ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
    (nLines := s size) == 0 ifTrue:[^ self].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
    (nLines == 1 and:[(s at:1) size == 0]) ifTrue:[^ self].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
    s keysAndValuesDo:[:idx :line |
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
        line notNil ifTrue:[inStream nextPutAll:line].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
        idx ~~ nLines ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
            self sendLineEnd.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
        ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
    "Modified: / 12.6.1998 / 22:12:47 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
sendLine:aString
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
    inStream nextPutAll:aString.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
    self sendLineEnd
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
sendLineEnd
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
    OperatingSystem isMSDOSlike ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
        inStream nextPut:Character return.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
        inStream nextPut:Character linefeed.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
    ] ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
        inStream nextPut:Character return.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
!TerminalSession methodsFor:'misc'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
defineWindowSizeLines:numberOfLines columns:numberOfColumns
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
    | delta prevNumCols prevNumLines|
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
    "/ any idea, how to do this under windows ?
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
    OperatingSystem isUNIXlike ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
        "/
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
        "/ tell the pty;
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
        "/ tell the shell;
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
        "/
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
        (inStream notNil 
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
        and:[inStream isExternalStream
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
        and:[inStream isOpen]]) ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
            Debug ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
                Transcript showCR:'TerminalSession [info]: changed len to ', numberOfLines printString.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
            ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
            (OperatingSystem 
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
                setWindowSizeOnFileDescriptor:inStream fileDescriptor
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
                width:numberOfColumns
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
                height:numberOfLines) ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
                Debug ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
                    Transcript showCR:'TerminalSession [warning]: cannot change windowSize'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
                ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
            ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
        shellPid notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
            OperatingSystem sendSignal:OperatingSystem sigWINCH to:shellPid
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   346
        ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   349
    "Created: / 11.6.1998 / 22:51:39 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
    "Modified: / 5.5.1999 / 19:45:09 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
sendInterruptSignal
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
    "send an INT-signal to the shell (UNIX only)"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
    shellPid notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
        OperatingSystem sendSignal:(OperatingSystem sigINT) to:shellPid negated.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
    "Modified: / 10.6.1998 / 17:49:49 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
sendKillSignal
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
    "send a KILL-signal to the shell (UNIX only)"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
    shellPid notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
        OperatingSystem sendSignal:(OperatingSystem sigKILL) to:shellPid negated.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
    ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
!TerminalSession methodsFor:'reader process'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
readAnyAvailableData
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
    "read data from the stream,
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
     and sends me #processInput:n: events if something arrived.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
     Returns the amount of data read."
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
    |buffer n bufferSize|
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
    outStream isNil ifTrue:[^ 0].   "/ already closed
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
    bufferSize := 256.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
    buffer := String new:bufferSize.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
    ExternalStream readErrorSignal handle:[:ex |
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
        n := 0
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
    ] do:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
        n := outStream nextAvailableBytes:bufferSize into:buffer startingAt:1.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
        n > 0 ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
            pluggableProcessInputAction notNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
                pluggableProcessInputAction value:buffer value:n.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
            ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
    ^ n
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
readerProcessLoop
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
    "look for the session's output"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
    StreamError handle:[:ex |
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
        Transcript show:'Terminal(PTY-reader) [error]: '; showCR:ex description.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
    ] do:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
        [true] whileTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
            AbortOperationRequest handle:[:ex |
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
            ] do:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
                |n sensor|
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
                readerDelay notNil ifTrue:[ Delay waitForSeconds:readerDelay].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
                outStream readWait.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
                (pluggableCheckBeforeReadAction isNil
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
                or:[pluggableCheckBeforeReadAction value]) ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
                    n := self readAnyAvailableData.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
                    n == 0 ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
                        "/ Windows IPC has a bug - it always
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
                        "/ returns 0 (when the command is idle)
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
                        "/ and says it's at the end (sigh)
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
                        OperatingSystem isMSWINDOWSlike ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
                            Delay waitForSeconds:0.1
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
                        ] ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   423
                            outStream atEnd ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
                                outStream close. outStream := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
                                inStream close.  inStream := nil.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
                                Processor activeProcess terminate.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
                            ] ifFalse:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
                                "/ this should not happen.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
                                Delay waitForSeconds:0.1
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   431
                            ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   432
                        ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   433
                    ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   434
                ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   435
            ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   436
        ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   437
    ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   438
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
startReaderProcess
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
    "Start a reader process, which looks for the commands output,
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
     and sends me #processInput:n: events whenever something arrives."
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   444
    readerProcess isNil ifTrue:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
        readerProcess := [
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
            [
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
                self readerProcessLoop.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
            ] ifCurtailed:[
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
                readerProcess := nil    
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
            ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
        ] fork. "/ forkAt:9.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
        readerProcess name:'pty reader'.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
    ]
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   454
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
    "
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   456
     VT100TerminalView openShell
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
    "
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
    "Modified: / 5.5.1999 / 17:58:02 / cg"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
    "Modified: / 28.1.2002 / 21:10:13 / micha"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   463
stopReaderProcess
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
    "stop the background reader thread"
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
3037
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   466
    |p|
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   467
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   468
    (p := readerProcess) notNil ifTrue:[
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   469
        readerProcess := nil.
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   470
        p terminate.
3028
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
        "/ give it a chance to really terminate
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
        Processor yield.
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
    ].
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
!TerminalSession class methodsFor:'documentation'!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
version
3037
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   479
    ^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.2 2013-07-07 07:39:35 cg Exp $'
3028
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
!
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
version_CVS
3037
47f022308e7d class: TerminalSession
Claus Gittinger <cg@exept.de>
parents: 3028
diff changeset
   483
    ^ '$Header: /cvs/stx/stx/libbasic2/TerminalSession.st,v 1.2 2013-07-07 07:39:35 cg Exp $'
3028
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
! !
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
e49cf8755fd6 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
TerminalSession initialize!