xref: /original-bsd/games/trek/getpar.c (revision a95f03a8)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)getpar.c	4.8 (Berkeley) 06/01/90";
10 #endif /* not lint */
11 
12 # include	<stdio.h>
13 # include	"getpar.h"
14 
15 /**
16  **	get integer parameter
17  **/
18 
19 getintpar(s)
20 char	*s;
21 {
22 	register int	i;
23 	int		n;
24 
25 	while (1)
26 	{
27 		if (testnl() && s)
28 			printf("%s: ", s);
29 		i = scanf("%d", &n);
30 		if (i < 0)
31 			exit(1);
32 		if (i > 0 && testterm())
33 			return (n);
34 		printf("invalid input; please enter an integer\n");
35 		skiptonl(0);
36 	}
37 }
38 
39 /**
40  **	get floating parameter
41  **/
42 
43 double getfltpar(s)
44 char	*s;
45 {
46 	register int		i;
47 	double			d;
48 
49 	while (1)
50 	{
51 		if (testnl() && s)
52 			printf("%s: ", s);
53 		i = scanf("%lf", &d);
54 		if (i < 0)
55 			exit(1);
56 		if (i > 0 && testterm())
57 			return (d);
58 		printf("invalid input; please enter a double\n");
59 		skiptonl(0);
60 	}
61 }
62 
63 /**
64  **	get yes/no parameter
65  **/
66 
67 struct cvntab	Yntab[] =
68 {
69 	"y",	"es",	(int (*)())1,	0,
70 	"n",	"o",	(int (*)())0,	0,
71 	0
72 };
73 
74 getynpar(s)
75 char	*s;
76 {
77 	struct cvntab		*r;
78 
79 	r = getcodpar(s, Yntab);
80 	return ((int) r->value);
81 }
82 
83 
84 /**
85  **	get coded parameter
86  **/
87 
88 struct cvntab *getcodpar(s, tab)
89 char		*s;
90 struct cvntab	tab[];
91 {
92 	char				input[100];
93 	register struct cvntab		*r;
94 	int				flag;
95 	register char			*p, *q;
96 	int				c;
97 	int				f;
98 
99 	flag = 0;
100 	while (1)
101 	{
102 		flag |= (f = testnl());
103 		if (flag)
104 			printf("%s: ", s);
105 		if (f)
106 			cgetc(0);		/* throw out the newline */
107 		scanf("%*[ \t;]");
108 		if ((c = scanf("%[^ \t;\n]", input)) < 0)
109 			exit(1);
110 		if (c == 0)
111 			continue;
112 		flag = 1;
113 
114 		/* if command list, print four per line */
115 		if (input[0] == '?' && input[1] == 0)
116 		{
117 			c = 4;
118 			for (r = tab; r->abrev; r++)
119 			{
120 				concat(r->abrev, r->full, input);
121 				printf("%14.14s", input);
122 				if (--c > 0)
123 					continue;
124 				c = 4;
125 				printf("\n");
126 			}
127 			if (c != 4)
128 				printf("\n");
129 			continue;
130 		}
131 
132 		/* search for in table */
133 		for (r = tab; r->abrev; r++)
134 		{
135 			p = input;
136 			for (q = r->abrev; *q; q++)
137 				if (*p++ != *q)
138 					break;
139 			if (!*q)
140 			{
141 				for (q = r->full; *p && *q; q++, p++)
142 					if (*p != *q)
143 						break;
144 				if (!*p || !*q)
145 					break;
146 			}
147 		}
148 
149 		/* check for not found */
150 		if (!r->abrev)
151 		{
152 			printf("invalid input; ? for valid inputs\n");
153 			skiptonl(0);
154 		}
155 		else
156 			return (r);
157 	}
158 }
159 
160 
161 /**
162  **	get string parameter
163  **/
164 
165 getstrpar(s, r, l, t)
166 char	*s;
167 char	*r;
168 int	l;
169 char	*t;
170 {
171 	register int	i;
172 	char		format[20];
173 	register int	f;
174 
175 	if (t == 0)
176 		t = " \t\n;";
177 	(void)sprintf(format, "%%%d[^%s]", l, t);
178 	while (1)
179 	{
180 		if ((f = testnl()) && s)
181 			printf("%s: ", s);
182 		if (f)
183 			cgetc(0);
184 		scanf("%*[\t ;]");
185 		i = scanf(format, r);
186 		if (i < 0)
187 			exit(1);
188 		if (i != 0)
189 			return;
190 	}
191 }
192 
193 
194 /**
195  **	test if newline is next valid character
196  **/
197 
198 testnl()
199 {
200 	register char		c;
201 
202 	while ((c = cgetc(0)) != '\n')
203 		if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
204 				(c >= 'A' && c <= 'Z') ||
205 				(c >= 'a' && c <= 'z') || c == '-')
206 		{
207 			ungetc(c, stdin);
208 			return(0);
209 		}
210 	ungetc(c, stdin);
211 	return (1);
212 }
213 
214 
215 /**
216  **	scan for newline
217  **/
218 
219 skiptonl(c)
220 char	c;
221 {
222 	while (c != '\n')
223 		if (!(c = cgetc(0)))
224 			return;
225 	ungetc('\n', stdin);
226 	return;
227 }
228 
229 
230 /**
231  **	test for valid terminator
232  **/
233 
234 testterm()
235 {
236 	register char		c;
237 
238 	if (!(c = cgetc(0)))
239 		return (1);
240 	if (c == '.')
241 		return (0);
242 	if (c == '\n' || c == ';')
243 		ungetc(c, stdin);
244 	return (1);
245 }
246 
247 
248 /*
249 **  TEST FOR SPECIFIED DELIMETER
250 **
251 **	The standard input is scanned for the parameter.  If found,
252 **	it is thrown away and non-zero is returned.  If not found,
253 **	zero is returned.
254 */
255 
256 readdelim(d)
257 char	d;
258 {
259 	register char	c;
260 
261 	while (c = cgetc(0))
262 	{
263 		if (c == d)
264 			return (1);
265 		if (c == ' ')
266 			continue;
267 		ungetc(c, stdin);
268 		break;
269 	}
270 	return (0);
271 }
272