diff options
Diffstat (limited to 'contrib/top/getans')
-rwxr-xr-x | contrib/top/getans | 178 |
1 files changed, 105 insertions, 73 deletions
diff --git a/contrib/top/getans b/contrib/top/getans index 7c94c37..1b741f7 100755 --- a/contrib/top/getans +++ b/contrib/top/getans @@ -1,86 +1,118 @@ -#!/bin/csh -f -set ny = (no yes) -if ($2 == "yesno") then - @ i = $3 + 1 - set pmpt = "$1 [$ny[$i]]: " +#!/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 - if ("$3" == "") then - set pmpt = "${1}" + if [ -z "${DFLT}" ]; then + pmpt="${RAWPMPT}" else - set pmpt = "$1 [$3]: " - endif -endif -rpt: -echo -n "$pmpt" -set input = $< -switch ($2) - case number: - set tmp = `echo $input | tr -d 0123456789.` - if ("x$tmp" != x) then + 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." - goto rpt - endif - breaksw + continue + fi + ;; - case integer: - set tmp = `echo $input | tr -d 0123456789` - if ("x$tmp" != x) then + integer) + tmp=`echo $input | tr -d 0123456789` + if [ -n "$tmp" ]; then echo "Invalid integer. Please try again." - goto rpt - endif - breaksw + continue + fi + ;; - case neginteger: - if ("x$input" != x-1) then - set tmp = `echo $input | tr -d 0123456789` - if ("x$tmp" != x) then + neginteger) + if [ "x$input" != "x-1" ]; then + tmp=`echo $input | tr -d 0123456789` + if [ -n "$tmp" ]; then echo "Invalid integer. Please try again." - goto rpt - endif - endif - breaksw - - case file: - if ("x$input" == "x") then - set input = $3 - endif - if (! -e "$input") then - echo The file $input "does not exist. Please try again." - goto rpt - endif - breaksw + continue + fi + fi + ;; - case path: - if ("x$input" == "x") then - set input = "$3" - endif - if (! -e "$input") then - foreach elt ($path) - if (-e "$elt/$input") breaksw - end - echo The command $input "was not found. Please try again." - goto rpt - endif - breaksw + 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 + ;; - case yesno: - if ("x$input" == xy || "x$input" == xyes) then - set input = 1 - else if ("x$input" == xn || "x$input" == xno) then - set input = 0 - else if ("x$input" != x) then - echo 'Please answer "yes" or "no".' - goto rpt - endif - breaksw + 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 + ;; - default: - breaksw + 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 + ;; -endsw + *) ;; + esac + break +done -if ("x$input" == x) then - set input = "$3" -endif +if [ -z "$input" ]; then + input="${DFLT}" +fi -echo $input > $4 +echo $input > ${OFNM} |