xref: /netbsd-src/games/monop/getinp.c (revision c34afa686bf074a6dcc7f17157faae72111dde9a)
1*c34afa68Sdholland /*	$NetBSD: getinp.c,v 1.19 2012/06/19 05:35:32 dholland Exp $	*/
242fb1b9dScgd 
361f28255Scgd /*
442fb1b9dScgd  * Copyright (c) 1980, 1993
542fb1b9dScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32b118ad22Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3442fb1b9dScgd #if 0
3542fb1b9dScgd static char sccsid[] = "@(#)getinp.c	8.1 (Berkeley) 5/31/93";
3642fb1b9dScgd #else
37*c34afa68Sdholland __RCSID("$NetBSD: getinp.c,v 1.19 2012/06/19 05:35:32 dholland Exp $");
3842fb1b9dScgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include <stdio.h>
42c4816c32Scgd #include <string.h>
4361f28255Scgd #include <ctype.h>
44b51259daSdholland 
45b51259daSdholland #include "monop.h"
4661f28255Scgd 
4761f28255Scgd #define	LINE	70
4861f28255Scgd 
4961f28255Scgd static char	buf[257];
5061f28255Scgd 
51cb5fd834Sjsm static int comp(const char *);
5261f28255Scgd 
53b118ad22Schristos int
getinp(const char * prompt,const char * const lst[])54*c34afa68Sdholland getinp(const char *prompt, const char *const lst [])
55b118ad22Schristos {
56b118ad22Schristos 	int i, n_match, match = 0;
5761f28255Scgd 	char *sp;
5861f28255Scgd 
5961f28255Scgd 	for (;;) {
60d3039275Sitojun 		printf("%s", prompt);
61ff1acb64Sdholland 		fgets(buf, sizeof(buf), stdin);
62ff1acb64Sdholland 		if (feof(stdin)) {
63271a4190Selad 			return 0;
64365ece4eSthorpej 		}
6561f28255Scgd 		if (buf[0] == '?' && buf[1] == '\n') {
6661f28255Scgd 			printf("Valid inputs are: ");
674e5cdff5Sdholland 			for (i = 0, match = 18; lst[i]; i++) {
684e5cdff5Sdholland 				if ((match+=(n_match=strlen(lst[i]))) > LINE) {
6961f28255Scgd 					printf("\n\t");
7061f28255Scgd 					match = n_match + 8;
7161f28255Scgd 				}
724e5cdff5Sdholland 				if (*lst[i] == '\0') {
7361f28255Scgd 					match += 8;
7461f28255Scgd 					printf("<RETURN>");
7561f28255Scgd 				}
7661f28255Scgd 				else
774e5cdff5Sdholland 					printf("%s", lst[i]);
784e5cdff5Sdholland 				if (lst[i+1])
7961f28255Scgd 					printf(", ");
8061f28255Scgd 				else
8161f28255Scgd 					putchar('\n');
8261f28255Scgd 				match += 2;
8361f28255Scgd 			}
8461f28255Scgd 			continue;
8561f28255Scgd 		}
86ff1acb64Sdholland 		if ((sp = strchr(buf, '\n')) != NULL)
8761f28255Scgd 			*sp = '\0';
8861f28255Scgd 		for (sp = buf; *sp; sp++)
8949f7d8a9Sdsl 			*sp = tolower((unsigned char)*sp);
904e5cdff5Sdholland 		for (i = n_match = 0; lst[i]; i++)
914e5cdff5Sdholland 			if (comp(lst[i])) {
9261f28255Scgd 				n_match++;
9361f28255Scgd 				match = i;
9461f28255Scgd 			}
9561f28255Scgd 		if (n_match == 1)
9661f28255Scgd 			return match;
9761f28255Scgd 		else if (buf[0] != '\0')
98e42b2048Ssimonb 			printf("Illegal response: \"%s\".  "
99e42b2048Ssimonb 			    "Use '?' to get list of valid answers\n", buf);
10061f28255Scgd 	}
10161f28255Scgd }
10261f28255Scgd 
103b118ad22Schristos static int
comp(const char * s1)104*c34afa68Sdholland comp(const char *s1)
105b118ad22Schristos {
106092d3130Sjsm 	const char *sp, *tsp;
107092d3130Sjsm 	char c;
10861f28255Scgd 
10961f28255Scgd 	if (buf[0] != '\0')
11061f28255Scgd 		for (sp = buf, tsp = s1; *sp; ) {
11149f7d8a9Sdsl 			c = tolower((unsigned char)*tsp);
11261f28255Scgd 			tsp++;
11361f28255Scgd 			if (c != *sp++)
11461f28255Scgd 				return 0;
11561f28255Scgd 		}
11661f28255Scgd 	else if (*s1 != '\0')
11761f28255Scgd 		return 0;
11861f28255Scgd 	return 1;
11961f28255Scgd }
120