From f15ff1c96555a554fc32bb8e8eb6b3887eed6abc Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 28 Sep 2021 12:34:56 -0700 Subject: [PATCH] fix leaky reads and add argc checking --- sh/confirm | 10 ++++++---- sh/pause | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/sh/confirm b/sh/confirm index d966bfc..78e9940 100755 --- a/sh/confirm +++ b/sh/confirm @@ -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] ' diff --git a/sh/pause b/sh/pause index 232bb4d..26a4c6c 100755 --- a/sh/pause +++ b/sh/pause @@ -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'