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