Update libressl README.DRAGONFLY for v2.4.4
[dragonfly.git] / games / trek / getpar.c
blob731fc954a9f45527e7cfedfd8a75182ed83ebd42
1 /*-
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)getpar.c 8.1 (Berkeley) 5/31/93
30 * $FreeBSD: src/games/trek/getpar.c,v 1.5 1999/11/30 03:49:48 billf Exp $
33 #include "getpar.h"
34 #include "trek.h"
36 static bool testterm(void);
38 /**
39 ** get integer parameter
40 **/
42 int
43 getintpar(const char *s)
45 int i;
46 int n;
48 while (1) {
49 if (testnl() && s)
50 printf("%s: ", s);
51 i = scanf("%d", &n);
52 if (i < 0)
53 exit(1);
54 if (i > 0 && testterm())
55 return (n);
56 printf("invalid input; please enter an integer\n");
57 skiptonl(0);
61 /**
62 ** get floating parameter
63 **/
65 double
66 getfltpar(const char *s)
68 int i;
69 double d;
71 while (1) {
72 if (testnl() && s)
73 printf("%s: ", s);
74 i = scanf("%lf", &d);
75 if (i < 0)
76 exit(1);
77 if (i > 0 && testterm())
78 return (d);
79 printf("invalid input; please enter a double\n");
80 skiptonl(0);
84 /**
85 ** get yes/no parameter
86 **/
88 struct cvntab Yntab[] = {
89 { "y", "es", (cmdfun)1, 0 },
90 { "n", "o", (cmdfun)0, 0 },
91 { NULL, NULL, NULL, 0 }
94 long
95 getynpar(const char *s)
97 struct cvntab *r;
99 r = getcodpar(s, Yntab);
100 return ((long) r->value);
105 ** get coded parameter
108 struct cvntab *
109 getcodpar(const char *s, struct cvntab tab[])
111 char input[100];
112 struct cvntab *r;
113 int flag;
114 char *p;
115 const char *q;
116 int c;
117 int f;
119 flag = 0;
120 while (1) {
121 flag |= (f = testnl());
122 if (flag)
123 printf("%s: ", s);
124 if (f)
125 cgetc(0); /* throw out the newline */
126 scanf("%*[ \t;]");
127 if ((c = scanf("%[^ \t;\n]", input)) < 0)
128 exit(1);
129 if (c == 0)
130 continue;
131 flag = 1;
133 /* if command list, print four per line */
134 if (input[0] == '?' && input[1] == 0) {
135 c = 4;
136 for (r = tab; r->abrev; r++) {
137 strcpy(input, r->abrev);
138 strcat(input, r->full);
139 printf("%14.14s", input);
140 if (--c > 0)
141 continue;
142 c = 4;
143 printf("\n");
145 if (c != 4)
146 printf("\n");
147 continue;
150 /* search for in table */
151 for (r = tab; r->abrev; r++) {
152 p = input;
153 for (q = r->abrev; *q; q++)
154 if (*p++ != *q)
155 break;
156 if (!*q) {
157 for (q = r->full; *p && *q; q++, p++)
158 if (*p != *q)
159 break;
160 if (!*p || !*q)
161 break;
165 /* check for not found */
166 if (!r->abrev) {
167 printf("invalid input; ? for valid inputs\n");
168 skiptonl(0);
170 else
171 return (r);
177 ** get string parameter
180 void
181 getstrpar(const char *s, char *r, int l, const char *t)
183 int i;
184 char format[20];
185 int f;
187 if (t == NULL)
188 t = " \t\n;";
189 sprintf(format, "%%%d[^%s]", l, t);
190 while (1) {
191 if ((f = testnl()) && s)
192 printf("%s: ", s);
193 if (f)
194 cgetc(0);
195 scanf("%*[\t ;]");
196 i = scanf(format, r);
197 if (i < 0)
198 exit(1);
199 if (i != 0)
200 return;
206 ** test if newline is next valid character
209 bool
210 testnl(void)
212 char c;
214 while ((c = cgetc(0)) != '\n') {
215 if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
216 (c >= 'A' && c <= 'Z') ||
217 (c >= 'a' && c <= 'z') || c == '-') {
218 ungetc(c, stdin);
219 return(0);
222 ungetc(c, stdin);
223 return (1);
228 ** scan for newline
231 void
232 skiptonl(char c)
234 while (c != '\n') {
235 if (!(c = cgetc(0)))
236 return;
238 ungetc('\n', stdin);
239 return;
244 ** test for valid terminator
247 static bool
248 testterm(void)
250 char c;
252 if (!(c = cgetc(0)))
253 return (1);
254 if (c == '.')
255 return (0);
256 if (c == '\n' || c == ';')
257 ungetc(c, stdin);
258 return (1);
263 ** TEST FOR SPECIFIED DELIMITER
265 ** The standard input is scanned for the parameter. If found,
266 ** it is thrown away and non-zero is returned. If not found,
267 ** zero is returned.
270 bool
271 readdelim(char d)
273 char c;
275 while ((c = cgetc(0))) {
276 if (c == d)
277 return (1);
278 if (c == ' ')
279 continue;
280 ungetc(c, stdin);
281 break;
283 return (0);