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