rework ssh 'init' command

This commit is contained in:
Mikaël Cluseau
2024-01-21 15:26:00 +01:00
parent 2df68d3fca
commit 4a12e1ba8f
9 changed files with 93 additions and 65 deletions

25
tty.go
View File

@ -1,29 +1,25 @@
package main
import (
"os"
"golang.org/x/sys/unix"
)
var (
stdinTTY = &tty{int(os.Stdin.Fd()), nil}
)
type tty struct {
fd int
termios *unix.Termios
}
func newTTY(fd uintptr) *tty {
termios, _ := unix.IoctlGetTermios(int(fd), unix.TCGETS)
return &tty{int(fd), termios}
}
func (t *tty) EchoOff() {
termios, err := unix.IoctlGetTermios(t.fd, unix.TCGETS)
if err != nil {
if t.termios == nil {
return
}
t.termios = termios
newState := *termios
newState := *t.termios
newState.Lflag &^= unix.ECHO
newState.Lflag |= unix.ICANON | unix.ISIG
newState.Iflag |= unix.ICRNL
@ -31,8 +27,9 @@ func (t *tty) EchoOff() {
}
func (t *tty) Restore() {
if t.termios != nil {
unix.IoctlSetTermios(t.fd, unix.TCSETS, t.termios)
t.termios = nil
if t.termios == nil {
return
}
unix.IoctlSetTermios(t.fd, unix.TCSETS, t.termios)
}