1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-05-20 10:53:23 -07:00

fix leaky reads and add argc checking

This commit is contained in:
Connor Olding 2021-09-28 12:34:56 -07:00
parent 9bac51b1a6
commit f15ff1c965
2 changed files with 13 additions and 6 deletions

View File

@ -29,22 +29,24 @@
if [ -n "$ZSH_VERSION" ]; then
confirm() {
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
read -q '?Continue? [y/N] '
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
local c=
read -q '?Continue? [y/N] ' c
ret=$?
echo >/dev/tty
return $ret
}
elif [ -n "$BASH_VERSION" ]; then
confirm() {
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
local c=
read -n1 -p "Continue? [y/N] " c 2>&1
echo
[ "$c" = y ] || [ "$c" = Y ]
} 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
else
confirm() (
[ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
old="$(stty -g)"
trap 'stty "$old"' INT EXIT
printf 'Continue? [y/N] '

View File

@ -15,14 +15,19 @@
if [ -n "$ZSH_VERSION" ]; then
pause() {
read -sk $'?Press any key to continue\n'
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
local c=
read -sk $'?Press any key to continue\n' c
}
elif [ -n "$BASH_VERSION" ]; then
pause() {
read -n1 -sp $'Press any key to continue\n' 2>&1
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
local c=
read -n1 -sp $'Press any key to continue\n' c 2>&1
} 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
else
pause() (
[ $# -eq 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 2; }
old="$(stty -g)"
trap 'stty "$old"' INT EXIT
printf 'Press any key to continue\n'