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