xref: /csrg-svn/games/monop/getinp.c (revision 60815)
133207Sbostic /*
2*60815Sbostic  * Copyright (c) 1980, 1993
3*60815Sbostic  *	The Regents of the University of California.  All rights reserved.
433207Sbostic  *
542583Sbostic  * %sccs.include.redist.c%
633207Sbostic  */
733207Sbostic 
833207Sbostic #ifndef lint
9*60815Sbostic static char sccsid[] = "@(#)getinp.c	8.1 (Berkeley) 05/31/93";
1033207Sbostic #endif /* not lint */
1133207Sbostic 
1233207Sbostic # include	<stdio.h>
1333207Sbostic # include	<ctype.h>
1433207Sbostic 
1533207Sbostic # define	reg	register
1633207Sbostic 
1733207Sbostic # define	LINE	70
1833207Sbostic 
1933207Sbostic static char	buf[257];
2033207Sbostic 
getinp(prompt,list)2133207Sbostic getinp(prompt, list)
2233207Sbostic char	*prompt, *list[]; {
2333207Sbostic 
2433207Sbostic 	reg int	i, n_match, match;
2533207Sbostic 	char	*sp;
2633207Sbostic 	int	plen;
2746751Sbostic 	static int comp();
2833207Sbostic 
2933207Sbostic 	for (;;) {
3033207Sbostic inter:
3133207Sbostic 		printf(prompt);
3233207Sbostic 		for (sp = buf; (*sp=getchar()) != '\n'; )
3333207Sbostic 			if (*sp == -1)	/* check for interupted system call */
3433207Sbostic 				goto inter;
3533207Sbostic 			else if (sp != buf || *sp != ' ')
3633207Sbostic 				sp++;
3733207Sbostic 		if (buf[0] == '?' && buf[1] == '\n') {
3833207Sbostic 			printf("Valid inputs are: ");
3933207Sbostic 			for (i = 0, match = 18; list[i]; i++) {
4033207Sbostic 				if ((match+=(n_match=strlen(list[i]))) > LINE) {
4133207Sbostic 					printf("\n\t");
4233207Sbostic 					match = n_match + 8;
4333207Sbostic 				}
4433207Sbostic 				if (*list[i] == '\0') {
4533207Sbostic 					match += 8;
4633207Sbostic 					printf("<RETURN>");
4733207Sbostic 				}
4833207Sbostic 				else
4933207Sbostic 					printf(list[i]);
5033207Sbostic 				if (list[i+1])
5133207Sbostic 					printf(", ");
5233207Sbostic 				else
5333207Sbostic 					putchar('\n');
5433207Sbostic 				match += 2;
5533207Sbostic 			}
5633207Sbostic 			continue;
5733207Sbostic 		}
5833207Sbostic 		*sp = '\0';
5933207Sbostic 		for (sp = buf; *sp; sp++)
6033207Sbostic 			if (isupper(*sp))
6133207Sbostic 				*sp = tolower(*sp);
6233207Sbostic 		for (i = n_match = 0; list[i]; i++)
6333207Sbostic 			if (comp(list[i])) {
6433207Sbostic 				n_match++;
6533207Sbostic 				match = i;
6633207Sbostic 			}
6733207Sbostic 		if (n_match == 1)
6833207Sbostic 			return match;
6933207Sbostic 		else if (buf[0] != '\0')
7033207Sbostic 			printf("Illegal response: \"%s\".  Use '?' to get list of valid answers\n", buf);
7133207Sbostic 	}
7233207Sbostic }
7333207Sbostic 
7433207Sbostic static
comp(s1)7533207Sbostic comp(s1)
7633207Sbostic char	*s1; {
7733207Sbostic 
7833207Sbostic 	reg char	*sp, *tsp, c;
7933207Sbostic 
8033207Sbostic 	if (buf[0] != '\0')
8133207Sbostic 		for (sp = buf, tsp = s1; *sp; ) {
8233207Sbostic 			c = isupper(*tsp) ? tolower(*tsp) : *tsp;
8333207Sbostic 			tsp++;
8433207Sbostic 			if (c != *sp++)
8533207Sbostic 				return 0;
8633207Sbostic 		}
8733207Sbostic 	else if (*s1 != '\0')
8833207Sbostic 		return 0;
8933207Sbostic 	return 1;
9033207Sbostic }
91