s/pxeldr/pxeboot/
[dragonfly.git] / contrib / top / getans
1 #!/bin/sh
2 # getans prompt type default  results_filename
3 #  type is one of 
4 #   number  
5 #   integer
6 #   neginteger
7 #   file    default=default filename
8 #   path        
9 #   yesno   default=0,1 corres yes or no 
10 #   string (default)
11
12 RAWPMPT=$1
13 TYP=$2
14 DFLT=$3
15 OFNM=$4
16
17 ny0="no"; ny1="yes"
18 if [ ${TYP} = "yesno" ]; then
19     eval ny=\$ny${DFLT}
20     pmpt="${RAWPMPT} [$ny]: "
21 else
22     if [ -z "${DFLT}" ]; then
23         pmpt="${RAWPMPT}"
24     else
25         pmpt="${RAWPMPT} [${DFLT}]: "
26     fi
27 fi
28 if [ x"`echo -n`" = x-n ]
29 then
30     c=\\c
31 else
32     n=-n
33 fi
34
35 while :
36 do
37     echo $n "$pmpt"$c
38     read input
39     case "$TYP" in
40     number)
41         tmp=`echo $input | tr -d 0123456789.`
42         if [ -n "$tmp" ]; then
43             echo "Invalid number.  Please try again."
44             continue
45         fi
46         ;;
47
48     integer)
49         tmp=`echo $input | tr -d 0123456789`
50         if [ -n "$tmp" ]; then
51             echo "Invalid integer.  Please try again."
52             continue
53         fi
54         ;;
55
56     neginteger)
57         if [ "x$input" != "x-1" ]; then
58             tmp=`echo $input | tr -d 0123456789`
59             if [ -n "$tmp" ]; then
60                 echo "Invalid integer.  Please try again."
61                 continue
62             fi
63         fi
64         ;;
65
66     file)
67         if [ -z "$input" ]; then
68             input=${DFLT}
69         fi
70         if [ ! -f "$input"  -a ! -d "$input" ]; then
71             echo "The file $input does not exist.  Please try again."
72             continue
73         fi
74         ;;
75
76     path)
77         if [ -z "$input" ];  then
78             input="${DFLT}"
79         fi
80         if [ ! -f "$input" ]; then
81             path=`echo $PATH | sed -e s'/::/ . /g' -e 's/:/ /g'`
82             x=
83             for elt in $path;  do
84                 if [ -f "$elt/$input" ]; then  x=1; break; fi
85             done
86             if [ -z "$x" ] ;then 
87                 echo "The command $input was not found.  Please try again."
88                 continue
89             fi
90         fi
91         ;;
92
93     yesno)
94         if [ -z "$input" ];  then  
95             input="${DFLT}"
96         else
97             case $input in 
98             y | yes)
99                 input=1 ;;
100             n | no)
101                 input=0 ;;
102             *)
103                 echo 'Please answer "yes" or "no".'
104                 continue ;;
105             esac
106         fi
107         ;;
108
109     *)  ;;
110     esac
111     break
112 done
113
114 if [ -z "$input" ]; then
115     input="${DFLT}"
116 fi
117
118 echo $input > ${OFNM}