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