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