freebsd-nq/contrib/top/getans

119 lines
2.1 KiB
Plaintext
Raw Normal View History

2002-01-24 17:24:16 +00:00
#!/bin/sh
# getans prompt type default results_filename
# type is one of
# number
# integer
# neginteger
# file default=default filename
# path
# yesno default=0,1 corres yes or no
# string (default)
RAWPMPT=$1
TYP=$2
DFLT=$3
OFNM=$4
ny0="no"; ny1="yes"
if [ ${TYP} = "yesno" ]; then
eval ny=\$ny${DFLT}
pmpt="${RAWPMPT} [$ny]: "
else
2002-01-24 17:24:16 +00:00
if [ -z "${DFLT}" ]; then
pmpt="${RAWPMPT}"
else
2002-01-24 17:24:16 +00:00
pmpt="${RAWPMPT} [${DFLT}]: "
fi
fi
if [ x"`echo -n`" = x-n ]
then
c=\\c
else
n=-n
fi
while :
do
echo $n "$pmpt"$c
read input
case "$TYP" in
number)
tmp=`echo $input | tr -d 0123456789.`
if [ -n "$tmp" ]; then
echo "Invalid number. Please try again."
2002-01-24 17:24:16 +00:00
continue
fi
;;
2002-01-24 17:24:16 +00:00
integer)
tmp=`echo $input | tr -d 0123456789`
if [ -n "$tmp" ]; then
echo "Invalid integer. Please try again."
2002-01-24 17:24:16 +00:00
continue
fi
;;
2002-01-24 17:24:16 +00:00
neginteger)
if [ "x$input" != "x-1" ]; then
tmp=`echo $input | tr -d 0123456789`
if [ -n "$tmp" ]; then
echo "Invalid integer. Please try again."
2002-01-24 17:24:16 +00:00
continue
fi
fi
;;
2002-01-24 17:24:16 +00:00
file)
if [ -z "$input" ]; then
input=${DFLT}
fi
if [ ! -f "$input" -a ! -d "$input" ]; then
echo "The file $input does not exist. Please try again."
continue
fi
;;
2002-01-24 17:24:16 +00:00
path)
if [ -z "$input" ]; then
input="${DFLT}"
fi
if [ ! -f "$input" ]; then
path=`echo $PATH | sed -e s'/::/ . /g' -e 's/:/ /g'`
x=
for elt in $path; do
if [ -f "$elt/$input" ]; then x=1; break; fi
done
if [ -z "$x" ] ;then
echo "The command $input was not found. Please try again."
continue
fi
fi
;;
2002-01-24 17:24:16 +00:00
yesno)
if [ -z "$input" ]; then
input="${DFLT}"
else
case $input in
y | yes)
input=1 ;;
n | no)
input=0 ;;
*)
echo 'Please answer "yes" or "no".'
continue ;;
esac
fi
;;
2002-01-24 17:24:16 +00:00
*) ;;
esac
break
done
2002-01-24 17:24:16 +00:00
if [ -z "$input" ]; then
input="${DFLT}"
fi
2002-01-24 17:24:16 +00:00
echo $input > ${OFNM}