Home | History | Annotate | Line # | Download | only in trek
getpar.c revision 1.3
      1 /*	$NetBSD: getpar.c,v 1.3 1995/04/22 10:58:57 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)getpar.c	8.1 (Berkeley) 5/31/93";
     39 #else
     40 static char rcsid[] = "$NetBSD: getpar.c,v 1.3 1995/04/22 10:58:57 cgd Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 # include	<stdio.h>
     45 # include	"getpar.h"
     46 
     47 /**
     48  **	get integer parameter
     49  **/
     50 
     51 getintpar(s)
     52 char	*s;
     53 {
     54 	register int	i;
     55 	int		n;
     56 
     57 	while (1)
     58 	{
     59 		if (testnl() && s)
     60 			printf("%s: ", s);
     61 		i = scanf("%d", &n);
     62 		if (i < 0)
     63 			exit(1);
     64 		if (i > 0 && testterm())
     65 			return (n);
     66 		printf("invalid input; please enter an integer\n");
     67 		skiptonl(0);
     68 	}
     69 }
     70 
     71 /**
     72  **	get floating parameter
     73  **/
     74 
     75 double getfltpar(s)
     76 char	*s;
     77 {
     78 	register int		i;
     79 	double			d;
     80 
     81 	while (1)
     82 	{
     83 		if (testnl() && s)
     84 			printf("%s: ", s);
     85 		i = scanf("%lf", &d);
     86 		if (i < 0)
     87 			exit(1);
     88 		if (i > 0 && testterm())
     89 			return (d);
     90 		printf("invalid input; please enter a double\n");
     91 		skiptonl(0);
     92 	}
     93 }
     94 
     95 /**
     96  **	get yes/no parameter
     97  **/
     98 
     99 struct cvntab	Yntab[] =
    100 {
    101 	"y",	"es",	(int (*)())1,	0,
    102 	"n",	"o",	(int (*)())0,	0,
    103 	0
    104 };
    105 
    106 getynpar(s)
    107 char	*s;
    108 {
    109 	struct cvntab		*r;
    110 
    111 	r = getcodpar(s, Yntab);
    112 	return ((int) r->value);
    113 }
    114 
    115 
    116 /**
    117  **	get coded parameter
    118  **/
    119 
    120 struct cvntab *getcodpar(s, tab)
    121 char		*s;
    122 struct cvntab	tab[];
    123 {
    124 	char				input[100];
    125 	register struct cvntab		*r;
    126 	int				flag;
    127 	register char			*p, *q;
    128 	int				c;
    129 	int				f;
    130 
    131 	flag = 0;
    132 	while (1)
    133 	{
    134 		flag |= (f = testnl());
    135 		if (flag)
    136 			printf("%s: ", s);
    137 		if (f)
    138 			cgetc(0);		/* throw out the newline */
    139 		scanf("%*[ \t;]");
    140 		if ((c = scanf("%[^ \t;\n]", input)) < 0)
    141 			exit(1);
    142 		if (c == 0)
    143 			continue;
    144 		flag = 1;
    145 
    146 		/* if command list, print four per line */
    147 		if (input[0] == '?' && input[1] == 0)
    148 		{
    149 			c = 4;
    150 			for (r = tab; r->abrev; r++)
    151 			{
    152 				concat(r->abrev, r->full, input);
    153 				printf("%14.14s", input);
    154 				if (--c > 0)
    155 					continue;
    156 				c = 4;
    157 				printf("\n");
    158 			}
    159 			if (c != 4)
    160 				printf("\n");
    161 			continue;
    162 		}
    163 
    164 		/* search for in table */
    165 		for (r = tab; r->abrev; r++)
    166 		{
    167 			p = input;
    168 			for (q = r->abrev; *q; q++)
    169 				if (*p++ != *q)
    170 					break;
    171 			if (!*q)
    172 			{
    173 				for (q = r->full; *p && *q; q++, p++)
    174 					if (*p != *q)
    175 						break;
    176 				if (!*p || !*q)
    177 					break;
    178 			}
    179 		}
    180 
    181 		/* check for not found */
    182 		if (!r->abrev)
    183 		{
    184 			printf("invalid input; ? for valid inputs\n");
    185 			skiptonl(0);
    186 		}
    187 		else
    188 			return (r);
    189 	}
    190 }
    191 
    192 
    193 /**
    194  **	get string parameter
    195  **/
    196 
    197 getstrpar(s, r, l, t)
    198 char	*s;
    199 char	*r;
    200 int	l;
    201 char	*t;
    202 {
    203 	register int	i;
    204 	char		format[20];
    205 	register int	f;
    206 
    207 	if (t == 0)
    208 		t = " \t\n;";
    209 	(void)sprintf(format, "%%%d[^%s]", l, t);
    210 	while (1)
    211 	{
    212 		if ((f = testnl()) && s)
    213 			printf("%s: ", s);
    214 		if (f)
    215 			cgetc(0);
    216 		scanf("%*[\t ;]");
    217 		i = scanf(format, r);
    218 		if (i < 0)
    219 			exit(1);
    220 		if (i != 0)
    221 			return;
    222 	}
    223 }
    224 
    225 
    226 /**
    227  **	test if newline is next valid character
    228  **/
    229 
    230 testnl()
    231 {
    232 	register char		c;
    233 
    234 	while ((c = cgetc(0)) != '\n')
    235 		if ((c >= '0' && c <= '9') || c == '.' || c == '!' ||
    236 				(c >= 'A' && c <= 'Z') ||
    237 				(c >= 'a' && c <= 'z') || c == '-')
    238 		{
    239 			ungetc(c, stdin);
    240 			return(0);
    241 		}
    242 	ungetc(c, stdin);
    243 	return (1);
    244 }
    245 
    246 
    247 /**
    248  **	scan for newline
    249  **/
    250 
    251 skiptonl(c)
    252 char	c;
    253 {
    254 	while (c != '\n')
    255 		if (!(c = cgetc(0)))
    256 			return;
    257 	ungetc('\n', stdin);
    258 	return;
    259 }
    260 
    261 
    262 /**
    263  **	test for valid terminator
    264  **/
    265 
    266 testterm()
    267 {
    268 	register char		c;
    269 
    270 	if (!(c = cgetc(0)))
    271 		return (1);
    272 	if (c == '.')
    273 		return (0);
    274 	if (c == '\n' || c == ';')
    275 		ungetc(c, stdin);
    276 	return (1);
    277 }
    278 
    279 
    280 /*
    281 **  TEST FOR SPECIFIED DELIMETER
    282 **
    283 **	The standard input is scanned for the parameter.  If found,
    284 **	it is thrown away and non-zero is returned.  If not found,
    285 **	zero is returned.
    286 */
    287 
    288 readdelim(d)
    289 char	d;
    290 {
    291 	register char	c;
    292 
    293 	while (c = cgetc(0))
    294 	{
    295 		if (c == d)
    296 			return (1);
    297 		if (c == ' ')
    298 			continue;
    299 		ungetc(c, stdin);
    300 		break;
    301 	}
    302 	return (0);
    303 }
    304