From 480ad419cc2a751eda210b100f4315848f705469 Mon Sep 17 00:00:00 2001 From: Joseph Cumines Date: Fri, 11 Aug 2023 07:55:21 +1000 Subject: [PATCH 1/2] Fix getOriginalTermios error not persisting --- internal/term/term.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/term/term.go b/internal/term/term.go index 3f3a53c0..3d9ef774 100644 --- a/internal/term/term.go +++ b/internal/term/term.go @@ -11,17 +11,17 @@ import ( var ( saveTermios *unix.Termios + saveTermiosErr error saveTermiosFD int saveTermiosOnce sync.Once ) func getOriginalTermios(fd int) (*unix.Termios, error) { - var err error saveTermiosOnce.Do(func() { saveTermiosFD = fd - saveTermios, err = termios.Tcgetattr(uintptr(fd)) + saveTermios, saveTermiosErr = termios.Tcgetattr(uintptr(fd)) }) - return saveTermios, err + return saveTermios, saveTermiosErr } // Restore terminal's mode. From 645e1d124d016b35d84f0ef5f6fec29c0b2a4cf6 Mon Sep 17 00:00:00 2001 From: Joseph Cumines Date: Fri, 11 Aug 2023 08:17:52 +1000 Subject: [PATCH 2/2] Fix #266 correctly restore terminal parameters --- input_posix.go | 3 +-- internal/term/term.go | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/input_posix.go b/input_posix.go index f1043031..bf594b9f 100644 --- a/input_posix.go +++ b/input_posix.go @@ -13,8 +13,7 @@ const maxReadBytes = 1024 // PosixParser is a ConsoleParser implementation for POSIX environment. type PosixParser struct { - fd int - origTermios syscall.Termios + fd int } // Setup should be called before starting input diff --git a/internal/term/term.go b/internal/term/term.go index 3d9ef774..701c210f 100644 --- a/internal/term/term.go +++ b/internal/term/term.go @@ -10,7 +10,7 @@ import ( ) var ( - saveTermios *unix.Termios + saveTermios unix.Termios saveTermiosErr error saveTermiosFD int saveTermiosOnce sync.Once @@ -19,9 +19,19 @@ var ( func getOriginalTermios(fd int) (*unix.Termios, error) { saveTermiosOnce.Do(func() { saveTermiosFD = fd - saveTermios, saveTermiosErr = termios.Tcgetattr(uintptr(fd)) + var v *unix.Termios + v, saveTermiosErr = termios.Tcgetattr(uintptr(fd)) + if saveTermiosErr == nil { + // save a copy + saveTermios = *v + } }) - return saveTermios, saveTermiosErr + if saveTermiosErr != nil { + return nil, saveTermiosErr + } + // return a copy + v := saveTermios + return &v, nil } // Restore terminal's mode.