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