Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a6a9c7b18e |
@@ -21,9 +21,8 @@ import (
|
|||||||
// of input and goes on waiting.
|
// of input and goes on waiting.
|
||||||
//
|
//
|
||||||
// Meanwhile SIGINT, SIGTERM and SIGHUP are caught, as signals.Notify
|
// Meanwhile SIGINT, SIGTERM and SIGHUP are caught, as signals.Notify
|
||||||
// catches them. One that has arrived by the time the line is read puts
|
// catches them. One that arrives puts the terminal back as it was and
|
||||||
// the terminal back as it was and then ends the tool, as it would have
|
// then ends the tool, as it would have ended it if it were not caught.
|
||||||
// ended it if it were not caught.
|
|
||||||
func readWithoutEcho() (string, error) {
|
func readWithoutEcho() (string, error) {
|
||||||
fd := int(os.Stdin.Fd())
|
fd := int(os.Stdin.Fd())
|
||||||
|
|
||||||
@@ -49,6 +48,11 @@ func readWithoutEcho() (string, error) {
|
|||||||
return "", fmt.Errorf("turning echo off: %w", err)
|
return "", fmt.Errorf("turning echo off: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
restore := func() {
|
||||||
|
_ = unix.IoctlSetTermios(fd, ioctlWriteTermios, settings)
|
||||||
|
}
|
||||||
|
defer restore()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
typed string
|
typed string
|
||||||
failed error
|
failed error
|
||||||
@@ -64,26 +68,12 @@ func readWithoutEcho() (string, error) {
|
|||||||
close(read)
|
close(read)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var ending os.Signal
|
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-read:
|
case <-read:
|
||||||
case ending = <-received:
|
case ending := <-received:
|
||||||
}
|
restore()
|
||||||
|
|
||||||
// The terminal is put back while the signals are still caught, so
|
|
||||||
// that none can end the tool with echo off. Stop returns only once
|
|
||||||
// every signal the tool has received has been handed over, so one
|
|
||||||
// that came as the line was read is in received by then.
|
|
||||||
_ = unix.IoctlSetTermios(fd, ioctlWriteTermios, settings)
|
|
||||||
|
|
||||||
signal.Stop(received)
|
signal.Stop(received)
|
||||||
|
|
||||||
if len(received) > 0 {
|
|
||||||
ending = <-received
|
|
||||||
}
|
|
||||||
|
|
||||||
if ending != nil {
|
|
||||||
return "", endBy(ending)
|
return "", endBy(ending)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user