Initial import from FreeBSD RELENG_4:
[games.git] / games / trek / getpar.c
1 /*
2  * Copyright (c) 1980, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)getpar.c    8.1 (Berkeley) 5/31/93";
37 #endif
38 static const char rcsid[] =
39  "$FreeBSD: src/games/trek/getpar.c,v 1.5 1999/11/30 03:49:48 billf Exp $";
40 #endif /* not lint */
41
42 # include       <stdio.h>
43 # include       "getpar.h"
44
45 /**
46  **     get integer parameter
47  **/
48
49 getintpar(s)
50 char    *s;
51 {
52         int     i;
53         int             n;
54
55         while (1)
56         {
57                 if (testnl() && s)
58                         printf("%s: ", s);
59                 i = scanf("%d", &n);
60                 if (i < 0)
61                         exit(1);
62                 if (i > 0 && testterm())
63                         return (n);
64                 printf("invalid input; please enter an integer\n");
65                 skiptonl(0);
66         }
67 }
68
69 /**
70  **     get floating parameter
71  **/
72
73 double getfltpar(s)
74 char    *s;
75 {
76         int             i;
77         double                  d;
78
79         while (1)
80         {
81                 if (testnl() && s)
82                         printf("%s: ", s);
83                 i = scanf("%lf", &d);
84                 if (i < 0)
85                         exit(1);
86                 if (i > 0 && testterm())
87                         return (d);
88                 printf("invalid input; please enter a double\n");
89                 skiptonl(0);
90         }
91 }
92
93 /**
94  **     get yes/no parameter
95  **/
96
97 struct cvntab   Yntab[] =
98 {
99         "y",    "es",   (int (*)())1,   0,
100         "n",    "o",    (int (*)())0,   0,
101         0
102 };
103
104 getynpar(s)
105 char    *s;
106 {
107         struct cvntab           *r;
108
109         r = getcodpar(s, Yntab);
110         return ((long) r->value);
111 }
112
113
114 /**
115  **     get coded parameter
116  **/
117
118 struct cvntab *getcodpar(s, tab)
119 char            *s;
120 struct cvntab   tab[];
121 {
122         char                            input[100];
123         struct cvntab           *r;
124         int                             flag;
125         char                    *p, *q;
126         int                             c;
127         int                             f;
128
129         flag = 0;
130         while (1)
131         {
132                 flag |= (f = testnl());
133                 if (flag)
134                         printf("%s: ", s);
135                 if (f)
136                         cgetc(0);               /* throw out the newline */
137                 scanf("%*[ \t;]");
138                 if ((c = scanf("%[^ \t;\n]", input)) < 0)
139                         exit(1);
140                 if (c == 0)
141                         continue;
142                 flag = 1;
143
144                 /* if command list, print four per line */
145                 if (input[0] == '?' && input[1] == 0)
146                 {
147                         c = 4;
148                         for (r = tab; r->abrev; r++)
149                         {
150                                 concat(r->abrev, r->full, input);
151                                 printf("%14.14s", input);
152                                 if (--c > 0)
153                                         continue;
154                                 c = 4;
155                                 printf("\n");
156                         }
157                         if (c != 4)
158                                 printf("\n");
159                         continue;
160                 }
161
162                 /* search for in table */
163                 for (r = tab; r->abrev; r++)
164                 {
165                         p = input;
166                         for (q = r->abrev; *q; q++)
167                                 if (*p++ != *q)
168                                         break;
169                         if (!*q)
170                         {
171                                 for (q = r->full; *p && *q; q++, p++)
172                                         if (*p != *q)
173                                                 break;
174                                 if (!*p || !*q)
175                                         break;
176                         }
177                 }
178
179                 /* check for not found */
180                 if (!r->abrev)
181                 {
182                         printf("invalid input; ? for valid inputs\n");
183                         skiptonl(0);
184                 }
185                 else
186                         return (r);
187         }
188 }
189
190
191 /**
192  **     get string parameter
193  **/
194
195 getstrpar(s, r, l, t)
196 char    *s;
197 char    *r;
198 int     l;
199 char    *t;
200 {
201         int     i;
202         char            format[20];
203         int     f;
204
205         if (t == 0)
206                 t = " \t\n;";
207         (void)sprintf(format, "%%%d[^%s]", l, t);
208         while (1)
209         {
210                 if ((f = testnl()) && s)
211                         printf("%s: ", s);
212                 if (f)
213                         cgetc(0);
214                 scanf("%*[\t ;]");
215                 i = scanf(format, r);
216                 if (i < 0)
217                         exit(1);
218                 if (i != 0)
219                         return;
220         }
221 }
222
223
224 /**
225  **     test if newline is next valid character
226  **/
227
228 testnl()
229 {
230         char            c;
231
232         while ((c = cgetc(0)) != '\n')
233                 if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
234                                 (c >= 'A' && c <= 'Z') ||
235                                 (c >= 'a' && c <= 'z') || c == '-')
236                 {
237                         ungetc(c, stdin);
238                         return(0);
239                 }
240         ungetc(c, stdin);
241         return (1);
242 }
243
244
245 /**
246  **     scan for newline
247  **/
248
249 skiptonl(c)
250 char    c;
251 {
252         while (c != '\n')
253                 if (!(c = cgetc(0)))
254                         return;
255         ungetc('\n', stdin);
256         return;
257 }
258
259
260 /**
261  **     test for valid terminator
262  **/
263
264 testterm()
265 {
266         char            c;
267
268         if (!(c = cgetc(0)))
269                 return (1);
270         if (c == '.')
271                 return (0);
272         if (c == '\n' || c == ';')
273                 ungetc(c, stdin);
274         return (1);
275 }
276
277
278 /*
279 **  TEST FOR SPECIFIED DELIMETER
280 **
281 **      The standard input is scanned for the parameter.  If found,
282 **      it is thrown away and non-zero is returned.  If not found,
283 **      zero is returned.
284 */
285
286 readdelim(d)
287 char    d;
288 {
289         char    c;
290
291         while (c = cgetc(0))
292         {
293                 if (c == d)
294                         return (1);
295                 if (c == ' ')
296                         continue;
297                 ungetc(c, stdin);
298                 break;
299         }
300         return (0);
301 }