@@ -11,13 +11,11 @@ import (
1111 "log"
1212 "net"
1313 "os"
14- "os/signal"
1514 "path/filepath"
1615 "runtime"
1716 "strings"
1817 "sync"
1918 "sync/atomic"
20- "syscall"
2119
2220 "github.com/golang-jwt/jwt/v5"
2321 "github.com/wavetermdev/waveterm/pkg/baseds"
@@ -28,7 +26,6 @@ import (
2826 "github.com/wavetermdev/waveterm/pkg/wavebase"
2927 "github.com/wavetermdev/waveterm/pkg/wavejwt"
3028 "github.com/wavetermdev/waveterm/pkg/wshrpc"
31- "golang.org/x/term"
3229)
3330
3431// these should both be 5 characters
@@ -126,102 +123,17 @@ func EncodeWaveOSCMessageEx(oscNum string, msg *RpcMessage) ([]byte, error) {
126123 return EncodeWaveOSCBytes (oscNum , barr )
127124}
128125
129- var termModeLock = sync.Mutex {}
130- var termIsRaw bool
131- var origTermState * term.State
132- var shutdownSignalHandlersInstalled bool
133126var shutdownOnce sync.Once
134- var extraShutdownFunc atomic.Pointer [func ()]
135127
136128func DoShutdown (reason string , exitCode int , quiet bool ) {
137129 shutdownOnce .Do (func () {
138130 defer os .Exit (exitCode )
139- RestoreTermState ()
140- extraFn := extraShutdownFunc .Load ()
141- if extraFn != nil {
142- (* extraFn )()
143- }
144131 if ! quiet && reason != "" {
145- log .Printf ("shutting down: %s\r \ n " , reason )
132+ log .Printf ("shutting down: %s\n " , reason )
146133 }
147134 })
148135}
149136
150- func installShutdownSignalHandlers (quiet bool ) {
151- termModeLock .Lock ()
152- defer termModeLock .Unlock ()
153- if shutdownSignalHandlersInstalled {
154- return
155- }
156- sigCh := make (chan os.Signal , 1 )
157- signal .Notify (sigCh , syscall .SIGHUP , syscall .SIGTERM , syscall .SIGINT )
158- go func () {
159- defer func () {
160- panichandler .PanicHandlerNoTelemetry ("installShutdownSignalHandlers" , recover ())
161- }()
162- for sig := range sigCh {
163- DoShutdown (fmt .Sprintf ("got signal %v" , sig ), 1 , quiet )
164- break
165- }
166- }()
167- }
168-
169- func SetTermRawModeAndInstallShutdownHandlers (quietShutdown bool ) {
170- SetTermRawMode ()
171- installShutdownSignalHandlers (quietShutdown )
172- }
173-
174- func SetExtraShutdownFunc (fn func ()) {
175- extraShutdownFunc .Store (& fn )
176- }
177-
178- func SetTermRawMode () {
179- termModeLock .Lock ()
180- defer termModeLock .Unlock ()
181- if termIsRaw {
182- return
183- }
184- origState , err := term .MakeRaw (int (os .Stdin .Fd ()))
185- if err != nil {
186- fmt .Fprintf (os .Stderr , "Error setting raw mode: %v\n " , err )
187- return
188- }
189- origTermState = origState
190- termIsRaw = true
191- }
192-
193- func RestoreTermState () {
194- termModeLock .Lock ()
195- defer termModeLock .Unlock ()
196- if ! termIsRaw || origTermState == nil {
197- return
198- }
199- term .Restore (int (os .Stdin .Fd ()), origTermState )
200- termIsRaw = false
201- }
202-
203- // returns (wshRpc, wrappedStdin)
204- func SetupTerminalRpcClient (serverImpl ServerImpl , debugStr string ) (* WshRpc , io.Reader ) {
205- messageCh := make (chan baseds.RpcInputChType , DefaultInputChSize )
206- outputCh := make (chan []byte , DefaultOutputChSize )
207- ptyBuf := MakePtyBuffer (WaveServerOSCPrefix , os .Stdin , messageCh )
208- rpcClient := MakeWshRpcWithChannels (messageCh , outputCh , wshrpc.RpcContext {}, serverImpl , debugStr )
209- go func () {
210- defer func () {
211- panichandler .PanicHandler ("SetupTerminalRpcClient" , recover ())
212- }()
213- for msg := range outputCh {
214- barr , err := EncodeWaveOSCBytes (WaveOSC , msg )
215- if err != nil {
216- fmt .Fprintf (os .Stderr , "Error encoding OSC message: %v\n " , err )
217- continue
218- }
219- os .Stdout .Write (barr )
220- }
221- }()
222- return rpcClient , ptyBuf
223- }
224-
225137func SetupPacketRpcClient (input io.Reader , output io.Writer , serverImpl ServerImpl , debugStr string ) (* WshRpc , chan []byte ) {
226138 messageCh := make (chan baseds.RpcInputChType , DefaultInputChSize )
227139 outputCh := make (chan []byte , DefaultOutputChSize )
0 commit comments