Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5df9dc098d |
@@ -21,8 +21,9 @@ 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 arrives puts the terminal back as it was and
|
// catches them. One that has arrived by the time the line is read puts
|
||||||
// then ends the tool, as it would have ended it if it were not caught.
|
// 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) {
|
func readWithoutEcho() (string, error) {
|
||||||
fd := int(os.Stdin.Fd())
|
fd := int(os.Stdin.Fd())
|
||||||
|
|
||||||
@@ -48,11 +49,6 @@ 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
|
||||||
@@ -68,12 +64,26 @@ 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()
|
}
|
||||||
signal.Stop(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)
|
return "", endBy(ending)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user