Skip to content

Commit

Permalink
Fix wrong termios save code
Browse files Browse the repository at this point in the history
This bug is introduced by 20e0658.
Saving a *unix.Termios and then modifying it in SetRaw() makes the
saved state useless.

By restoring saveTermios type from *unix.Termios to unix.Termios,
SetRaw() no longer influences the saved termios.

This should fix c-bata#228 and c-bata#233 .

Signed-off-by: Xiami <i@f2light.com>
  • Loading branch information
Xiami authored and Xiami2012 committed Jun 17, 2021
1 parent 82a9122 commit b1024da
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/term/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ func SetRaw(fd int) error {
n.Cc[syscall.VMIN] = 1
n.Cc[syscall.VTIME] = 0

return termios.Tcsetattr(uintptr(fd), termios.TCSANOW, (*unix.Termios)(n))
return termios.Tcsetattr(uintptr(fd), termios.TCSANOW, (*unix.Termios)(&n))
}
10 changes: 6 additions & 4 deletions internal/term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
)

var (
saveTermios *unix.Termios
saveTermios unix.Termios
saveTermiosFD int
saveTermiosOnce sync.Once
)

func getOriginalTermios(fd int) (*unix.Termios, error) {
func getOriginalTermios(fd int) (unix.Termios, error) {
var err error
saveTermiosOnce.Do(func() {
saveTermiosFD = fd
saveTermios, err = termios.Tcgetattr(uintptr(fd))
var saveTermiosPtr *unix.Termios
saveTermiosPtr, err = termios.Tcgetattr(uintptr(fd))
saveTermios = *saveTermiosPtr
})
return saveTermios, err
}
Expand All @@ -30,5 +32,5 @@ func Restore() error {
if err != nil {
return err
}
return termios.Tcsetattr(uintptr(saveTermiosFD), termios.TCSANOW, o)
return termios.Tcsetattr(uintptr(saveTermiosFD), termios.TCSANOW, &o)
}

0 comments on commit b1024da

Please sign in to comment.