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