xref: /titanic_52/usr/src/lib/libadm/common/ckyorn.c (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1 */
32 /*LINTLIBRARY*/
33 
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <limits.h>
38 #include <sys/types.h>
39 #include "libadm.h"
40 
41 static char *choices[] = { "y", "n", NULL };
42 static char *vchoices[] = { "y", "n", "yes", "no", NULL };
43 
44 #define	TMPSIZ	7
45 #define	REQMSG	"Input is required."
46 #define	ERRMSG	"Please enter yes or no."
47 #define	HLPMSG	\
48 	"To respond in the affirmative, enter y, yes, Y, or YES. \
49 	To respond in the negative, enter n, no, N, or NO."
50 
51 int
52 ckyorn_val(char *str)
53 {
54 	int	i;
55 	char	*pt,
56 		temp[TMPSIZ+1];
57 
58 	for (pt = temp, i = 0; *str && i < TMPSIZ; i++) {
59 		*pt++ = tolower((unsigned char)*str++);
60 	}
61 	*pt = '\0';
62 	for (i = 0; vchoices[i]; ) {
63 		if (strcmp(temp, vchoices[i++]) == 0)
64 			return (0);
65 	}
66 	return (-1);
67 }
68 
69 void
70 ckyorn_err(char *error)
71 {
72 	puterror(stdout, ERRMSG, error);
73 }
74 
75 void
76 ckyorn_hlp(char *help)
77 {
78 	puthelp(stdout, HLPMSG, help);
79 }
80 
81 int
82 ckyorn(char *yorn, char *defstr, char *error, char *help, char *prompt)
83 {
84 	int	n;
85 	char	input[MAX_INPUT];
86 
87 	if (!prompt)
88 		prompt = "Yes or No";
89 start:
90 	putprmpt(stderr, prompt, choices, defstr);
91 	if (getinput(input))
92 		return (1);
93 
94 	n = (int)strlen(input);
95 	if (n == 0) {
96 		if (defstr) {
97 			(void) strcpy(yorn, defstr);
98 			return (0);
99 		}
100 		puterror(stderr, REQMSG, error);
101 		goto start;
102 	}
103 	if (strcmp(input, "?") == 0) {
104 		puthelp(stderr, HLPMSG, help);
105 		goto start;
106 	}
107 	if (ckquit && (strcmp(input, "q") == 0))
108 		return (3);
109 
110 	if (ckyorn_val(input)) {
111 		puterror(stderr, ERRMSG, error);
112 		goto start;
113 	}
114 	(void) strcpy(yorn, input);
115 	return (0);
116 }
117