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