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