aboutsummaryrefslogtreecommitdiffstats
path: root/getans
blob: 1b741f7cb7c268c99abeee389d044aaec73d6b2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/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 [ -z "${DFLT}" ]; then
        pmpt="${RAWPMPT}"
    else
        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."
	    continue
	fi
	;;

    integer)
	tmp=`echo $input | tr -d 0123456789`
	if [ -n "$tmp" ]; then
	    echo "Invalid integer.  Please try again."
	    continue
	fi
	;;

    neginteger)
	if [ "x$input" != "x-1" ]; then
	    tmp=`echo $input | tr -d 0123456789`
	    if [ -n "$tmp" ]; then
	        echo "Invalid integer.  Please try again."
	        continue
	    fi
	fi
        ;;

    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
	;;

    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
	;;

    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
        ;;

    *)	;;
    esac
    break
done

if [ -z "$input" ]; then
    input="${DFLT}"
fi

echo $input > ${OFNM}