2015-03-01 18:07:28 +01:00
|
|
|
|
2015-03-11 09:06:27 +01:00
|
|
|
# getopt(s)
|
|
|
|
while [ $# -ge 1 ]; do
|
2015-03-29 16:45:55 +02:00
|
|
|
arg="$(printf '%s' "$1" | grep -E '^-+[^-]+$' | tr '[A-Z]' '[a-z]' | tr -d '-')"
|
|
|
|
case "$arg" in
|
|
|
|
'display') shift; options_display="$1";;
|
|
|
|
'noechoback') options_noEchoBack='true';;
|
|
|
|
'mask') shift; options_mask="$1";;
|
|
|
|
'keyin') options_keyIn='true';;
|
|
|
|
'encoded') options_encoded='true';;
|
2015-03-11 09:06:27 +01:00
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2015-03-29 16:45:55 +02:00
|
|
|
reset_tty() {
|
|
|
|
if [ -n "$save_tty" ]; then stty "$save_tty"; fi
|
|
|
|
}
|
|
|
|
trap 'reset_tty' EXIT
|
|
|
|
save_tty="$(stty -g)"
|
2015-03-11 09:06:27 +01:00
|
|
|
|
2015-03-29 16:45:55 +02:00
|
|
|
write_tty() { # 2nd arg: enable escape sequence
|
|
|
|
if [ -n "$2" ]; then
|
|
|
|
printf '%b' "$1" >/dev/tty
|
2015-03-01 18:07:28 +01:00
|
|
|
else
|
2015-03-29 16:45:55 +02:00
|
|
|
printf '%s' "$1" >/dev/tty
|
2015-03-01 18:07:28 +01:00
|
|
|
fi
|
2015-03-29 16:45:55 +02:00
|
|
|
is_inputline='true'
|
|
|
|
}
|
|
|
|
|
|
|
|
replace_allchars() { (
|
|
|
|
text=''
|
|
|
|
for i in $(seq 1 ${#1})
|
|
|
|
do
|
|
|
|
text="$text$2"
|
|
|
|
done
|
|
|
|
printf '%s' "$text"
|
|
|
|
) }
|
|
|
|
|
|
|
|
[ -z "$options_noEchoBack" ] && [ -z "$options_keyIn" ] && is_cooked='true'
|
|
|
|
|
|
|
|
if [ -n "$options_display" ]; then
|
|
|
|
write_tty "$options_display"
|
|
|
|
options_display=''
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$is_cooked" ]; then
|
|
|
|
stty --file=/dev/tty cooked 2>/dev/null || \
|
|
|
|
stty -F /dev/tty cooked 2>/dev/null || \
|
|
|
|
stty -f /dev/tty cooked || exit $?
|
2014-07-12 00:37:25 +02:00
|
|
|
else
|
2015-03-29 16:45:55 +02:00
|
|
|
stty --file=/dev/tty raw -echo 2>/dev/null || \
|
|
|
|
stty -F /dev/tty raw -echo 2>/dev/null || \
|
|
|
|
stty -f /dev/tty raw -echo || exit $?
|
2014-07-12 00:37:25 +02:00
|
|
|
fi
|
2015-03-29 16:45:55 +02:00
|
|
|
|
|
|
|
[ -n "$options_keyIn" ] && req_size=1
|
|
|
|
|
|
|
|
while :
|
|
|
|
do
|
|
|
|
if [ -z "$is_cooked" ]; then
|
|
|
|
chunk="$(dd if=/dev/tty bs=1 count=1 2>/dev/null)"
|
|
|
|
chunk="$(printf '%s' "$chunk" | tr -d '\r\n')"
|
|
|
|
[ -z "$chunk" ] && is_eol='true' # NL or empty-text was input
|
|
|
|
else
|
|
|
|
IFS= read -r chunk </dev/tty || exit $?
|
|
|
|
chunk="$(printf '%s' "$chunk" | tr -d '\r\n')"
|
|
|
|
is_eol='true'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# other ctrl-chars
|
|
|
|
# chunk="$(printf '%s' "$chunk" | tr -d '\00-\10\13\14\16-\37\177')"
|
|
|
|
# for System V
|
|
|
|
chunk="$(printf '%s' "$chunk" | tr -d '\00\01\02\03\04\05\06\07\10\13\14\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177')"
|
|
|
|
|
|
|
|
if [ -n "$chunk" ] && [ -z "$is_cooked" ]; then
|
|
|
|
if [ -z "$options_noEchoBack" ]; then
|
|
|
|
write_tty "$chunk"
|
|
|
|
elif [ -n "$options_mask" ]; then
|
|
|
|
write_tty "$(replace_allchars "$chunk" "$options_mask")"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
input="$input$chunk"
|
|
|
|
if [ -n "$is_eol" ] || \
|
|
|
|
( [ -n "$options_keyIn" ] && [ ${#input} -ge $req_size ] ); then break; fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ -z "$is_cooked" ] && ! ( [ -n "$options_keyIn" ] && [ -z "$is_inputline" ] ); then
|
|
|
|
write_tty '\r\n' 'true'
|
|
|
|
fi
|
|
|
|
|
|
|
|
printf "'%s'" "$input"
|
2015-03-11 09:06:27 +01:00
|
|
|
|
2014-06-27 03:08:05 +02:00
|
|
|
exit 0
|