1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 02:49:02 -08: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 if [ -n "$ZSH_VERSION" ]; then
confirm() { 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; }
read -q '?Continue? [y/N] ' local c=
read -q '?Continue? [y/N] ' c
ret=$? ret=$?
echo >/dev/tty echo >/dev/tty
return $ret return $ret
} }
elif [ -n "$BASH_VERSION" ]; then elif [ -n "$BASH_VERSION" ]; then
confirm() { 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 read -n1 -p "Continue? [y/N] " c 2>&1
echo echo
[ "$c" = y ] || [ "$c" = Y ] [ "$c" = y ] || [ "$c" = Y ]
} 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe } 1<>/dev/tty <&1 # ensure this interacts with a terminal instead of a pipe
else else
confirm() ( 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)" old="$(stty -g)"
trap 'stty "$old"' INT EXIT trap 'stty "$old"' INT EXIT
printf 'Continue? [y/N] ' printf 'Continue? [y/N] '

View file

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