1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32: #include <sys/cdefs.h>
33: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)getpar.c 8.1 (Berkeley) 5/31/93";
36: #else
37: __RCSID("$NetBSD: getpar.c,v 1.12 2004/01/27 20:30:31 jsm Exp $");
38: #endif
39: #endif
40:
41: #include <stdio.h>
42: #include <stdlib.h>
43: #include <string.h>
44: #include "getpar.h"
45: #include "trek.h"
46:
47: static int testterm(void);
48:
49:
50:
51:
52:
53: int
54: getintpar(s)
55: const char *s;
56: {
57: int i;
58: int n;
59:
60: while (1)
61: {
62: if (testnl() && s)
63: printf("%s: ", s);
64: i = scanf("%d", &n);
65: if (i < 0)
66: exit(1);
67: if (i > 0 && testterm())
68: return (n);
69: printf("invalid input; please enter an integer\n");
70: skiptonl(0);
71: }
72: }
73:
74:
75:
76:
77:
78: double getfltpar(s)
79: const char *s;
80: {
81: int i;
82: double d;
83:
84: while (1)
85: {
86: if (testnl() && s)
87: printf("%s: ", s);
88: i = scanf("%lf", &d);
89: if (i < 0)
90: exit(1);
91: if (i > 0 && testterm())
92: return (d);
93: printf("invalid input; please enter a double\n");
94: skiptonl(0);
95: }
96: }
97:
98:
99:
100:
101:
102: const struct cvntab Yntab[] =
103: {
104: { "y", "es", (cmdfun)1, 1 },
105: { "n", "o", (cmdfun)0, 0 },
106: { NULL, NULL, NULL, 0 }
107: };
108:
109: int
110: getynpar(s)
111: const char *s;
112: {
113: const struct cvntab *r;
114:
115: r = getcodpar(s, Yntab);
116: return r->value2;
117: }
118:
119:
120:
121:
122:
123:
124: const struct cvntab *getcodpar(s, tab)
125: const char *s;
126: const struct cvntab tab[];
127: {
128: char input[100];
129: const struct cvntab *r;
130: int flag;
131: const char *p, *q;
132: int c;
133: int f;
134:
135: flag = 0;
136: while (1)
137: {
138: flag |= (f = testnl());
139: if (flag)
140: printf("%s: ", s);
141: if (f)
142: cgetc(0);
143: scanf("%*[ \t;]");
144: if ((c = scanf("%99[^ \t;\n]", input)) < 0)
145: exit(1);
146: if (c == 0)
147: continue;
148: flag = 1;
149:
150:
151: if (input[0] == '?' && input[1] == 0)
152: {
153: c = 4;
154: for (r = tab; r->abrev; r++)
155: {
156: strcpy(input, r->abrev);
157: strcat(input, r->full);
158: printf("%14.14s", input);
159: if (--c > 0)
160: continue;
161: c = 4;
162: printf("\n");
163: }
164: if (c != 4)
165: printf("\n");
166: continue;
167: }
168:
169:
170: for (r = tab; r->abrev; r++)
171: {
172: p = input;
173: for (q = r->abrev; *q; q++)
174: if (*p++ != *q)
175: break;
176: if (!*q)
177: {
178: for (q = r->full; *p && *q; q++, p++)
179: if (*p != *q)
180: break;
181: if (!*p || !*q)
182: break;
183: }
184: }
185:
186:
187: if (!r->abrev)
188: {
189: printf("invalid input; ? for valid inputs\n");
190: skiptonl(0);
191: }
192: else
193: return (r);
194: }
195: }
196:
197:
198:
199:
200:
201:
202: void
203: getstrpar(s, r, l, t)
204: const char *s;
205: char *r;
206: int l;
207: const char *t;
208: {
209: int i;
210: char format[20];
211: int f;
212:
213: if (t == 0)
214: t = " \t\n;";
215: (void)sprintf(format, "%%%d[^%s]", l, t);
216: while (1)
217: {
218: if ((f = testnl()) && s)
219: printf("%s: ", s);
220: if (f)
221: cgetc(0);
222: scanf("%*[\t ;]");
223: i = scanf(format, r);
224: if (i < 0)
225: exit(1);
226: if (i != 0)
227: return;
228: }
229: }
230:
231:
232:
233:
234:
235:
236: int
237: testnl()
238: {
239: char c;
240:
241: while ((c = cgetc(0)) != '\n')
242: if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
243: (c >= 'A' && c <= 'Z') ||
244: (c >= 'a' && c <= 'z') || c == '-')
245: {
246: ungetc(c, stdin);
247: return(0);
248: }
249: ungetc(c, stdin);
250: return (1);
251: }
252:
253:
254:
255:
256:
257:
258: void
259: skiptonl(c)
260: int c;
261: {
262: while (c != '\n')
263: if (!(c = cgetc(0)))
264: return;
265: ungetc('\n', stdin);
266: return;
267: }
268:
269:
270:
271:
272:
273:
274: static int
275: testterm()
276: {
277: char c;
278:
279: if (!(c = cgetc(0)))
280: return (1);
281: if (c == '.')
282: return (0);
283: if (c == '\n' || c == ';')
284: ungetc(c, stdin);
285: return (1);
286: }
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297: int
298: readdelim(d)
299: char d;
300: {
301: char c;
302:
303: while ((c = cgetc(0)) != '\0')
304: {
305: if (c == d)
306: return (1);
307: if (c == ' ')
308: continue;
309: ungetc(c, stdin);
310: break;
311: }
312: return (0);
313: }