1 /* @(#)getpar.c 8.1 (Berkeley) 5/31/93 */
2 /* $NetBSD: getpar.c,v 1.18 2009/08/12 08:54:54 dholland Exp $ */
5 * Copyright (c) 1980, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static int testterm(void);
42 ** get integer parameter
46 getintpar(const char *s)
57 if (i > 0 && testterm())
59 printf("invalid input; please enter an integer\n");
65 ** get floating parameter
69 getfltpar(const char *s)
80 if (i > 0 && testterm())
82 printf("invalid input; please enter a double\n");
88 ** get yes/no parameter
91 static const struct cvntab Yntab[] = {
92 { "y", "es", (cmdfun)1, 1 },
93 { "n", "o", (cmdfun)0, 0 },
94 { NULL, NULL, NULL, 0 }
98 getynpar(const char *s)
100 const struct cvntab *r;
102 r = getcodpar(s, Yntab);
108 ** get coded parameter
111 const struct cvntab *
112 getcodpar(const char *s, const struct cvntab tab[])
115 const struct cvntab *r;
123 flag |= (f = testnl());
127 /* throw out the newline */
131 if ((c = scanf("%99[^ \t;\n]", input)) < 0)
137 /* if command list, print four per line */
138 if (input[0] == '?' && input[1] == 0) {
140 for (r = tab; r->abbrev; r++) {
141 strcpy(input, r->abbrev);
142 strcat(input, r->full);
143 printf("%14.14s", input);
154 /* search for in table */
155 for (r = tab; r->abbrev; r++) {
157 for (q = r->abbrev; *q; q++)
161 for (q = r->full; *p && *q; q++, p++)
169 /* check for not found */
171 printf("invalid input; ? for valid inputs\n");
180 ** get string parameter
184 getstrpar(const char *s, char *r, int l, const char *t)
192 (void)snprintf(format, sizeof(format), "%%%d[^%s]", l, t);
194 if ((f = testnl()) && s)
199 i = scanf(format, r);
209 ** test if newline is next valid character
217 while ((c = getchar()) != '\n') {
221 if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
222 (c >= 'A' && c <= 'Z') ||
223 (c >= 'a' && c <= 'z') || c == '-') {
252 ** test for valid terminator
266 if (c == '\n' || c == ';')
273 ** TEST FOR SPECIFIED DELIMITER
275 ** The standard input is scanned for the parameter. If found,
276 ** it is thrown away and non-zero is returned. If not found,
285 while ((c = getchar()) != EOF) {