guix-install.sh: Restore compatibility with "yes" invocation.

Commit 6a2e303d3a had modified prompt_yes_no to only read a single character,
aiming to ease the user experience.  This was, in retrospect, a bad idea, as
it makes user input error more likely and introduces complexity.

This commit reverts to line-oriented input, while preserving the default yes
value so that a user can simply hit 'Enter' at the prompt in place of typing
"yes".

* etc/guix-install.sh (_flush): Delete function.
(prompt_yes_no): Restore line-oriented read.  Remove loop.  Make anything else
than yes means no.  Use Bash features to streamline definition.

Reported-by: Lars-Dominik Braun <lars@6xq.net> and others.
This commit is contained in:
Maxim Cournoyer 2022-10-11 11:49:37 -04:00
parent 68d79a8b60
commit e46bb5fd5a
No known key found for this signature in database
GPG key ID: 1260E46482E63562

View file

@ -92,33 +92,20 @@ _debug()
fi fi
} }
_flush()
{
while read -t0; do
read -N1
done
}
die() die()
{ {
_err "${ERR}$*" _err "${ERR}$*"
exit 1 exit 1
} }
# Return true if user answered yes, false otherwise. It defaults to "yes" # Return true if user answered yes, false otherwise. The prompt is
# when a single newline character is input. # yes-biased, that is, when the user simply enter newline, it is equivalent to
# answering "yes".
# $1: The prompt question. # $1: The prompt question.
prompt_yes_no() { prompt_yes_no() {
while true; do local -l yn
_flush read -rp "$1 [Y/n]" yn
read -N1 -rsp "$1 [Y/n]" yn [[ ! $yn || $yn = y || $yn = yes ]] || return 1
case $yn in
$'\n') echo && return 0;;
[Yy]*) echo && return 0;;
[Nn]*) echo && return 1;;
*) echo && _msg "Please answer yes or no."
esac
done
} }
chk_require() chk_require()