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