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