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