xref: /original-bsd/games/monop/getinp.c (revision 4092c5cc)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)getinp.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 # include	<stdio.h>
13 # include	<ctype.h>
14 
15 # define	reg	register
16 
17 # define	LINE	70
18 
19 static char	buf[257];
20 
21 getinp(prompt, list)
22 char	*prompt, *list[]; {
23 
24 	reg int	i, n_match, match;
25 	char	*sp;
26 	int	plen;
27 	static int comp();
28 
29 	for (;;) {
30 inter:
31 		printf(prompt);
32 		for (sp = buf; (*sp=getchar()) != '\n'; )
33 			if (*sp == -1)	/* check for interupted system call */
34 				goto inter;
35 			else if (sp != buf || *sp != ' ')
36 				sp++;
37 		if (buf[0] == '?' && buf[1] == '\n') {
38 			printf("Valid inputs are: ");
39 			for (i = 0, match = 18; list[i]; i++) {
40 				if ((match+=(n_match=strlen(list[i]))) > LINE) {
41 					printf("\n\t");
42 					match = n_match + 8;
43 				}
44 				if (*list[i] == '\0') {
45 					match += 8;
46 					printf("<RETURN>");
47 				}
48 				else
49 					printf(list[i]);
50 				if (list[i+1])
51 					printf(", ");
52 				else
53 					putchar('\n');
54 				match += 2;
55 			}
56 			continue;
57 		}
58 		*sp = '\0';
59 		for (sp = buf; *sp; sp++)
60 			if (isupper(*sp))
61 				*sp = tolower(*sp);
62 		for (i = n_match = 0; list[i]; i++)
63 			if (comp(list[i])) {
64 				n_match++;
65 				match = i;
66 			}
67 		if (n_match == 1)
68 			return match;
69 		else if (buf[0] != '\0')
70 			printf("Illegal response: \"%s\".  Use '?' to get list of valid answers\n", buf);
71 	}
72 }
73 
74 static
75 comp(s1)
76 char	*s1; {
77 
78 	reg char	*sp, *tsp, c;
79 
80 	if (buf[0] != '\0')
81 		for (sp = buf, tsp = s1; *sp; ) {
82 			c = isupper(*tsp) ? tolower(*tsp) : *tsp;
83 			tsp++;
84 			if (c != *sp++)
85 				return 0;
86 		}
87 	else if (*s1 != '\0')
88 		return 0;
89 	return 1;
90 }
91