xref: /illumos-gate/usr/src/lib/libadm/common/ckyorn.c (revision 543a8da84d26ec70222ecd1638b72c1e6c763275)
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 /*LINTLIBRARY*/
32 
33 #include <stdio.h>
34 #include <ctype.h>
35 #include <string.h>
36 #include <limits.h>
37 #include <sys/types.h>
38 #include "libadm.h"
39 
40 static char *choices[] = { "y", "n", NULL };
41 static char *vchoices[] = { "y", "n", "yes", "no", NULL };
42 
43 #define	TMPSIZ	7
44 #define	REQMSG	"Input is required."
45 #define	ERRMSG	"Please enter yes or no."
46 #define	HLPMSG	\
47 	"To respond in the affirmative, enter y, yes, Y, or YES. \
48 	To respond in the negative, enter n, no, N, or NO."
49 
50 int
51 ckyorn_val(char *str)
52 {
53 	int	i;
54 	char	*pt,
55 		temp[TMPSIZ+1];
56 
57 	for (pt = temp, i = 0; *str && i < TMPSIZ; i++) {
58 		*pt++ = tolower((unsigned char)*str++);
59 	}
60 	*pt = '\0';
61 	for (i = 0; vchoices[i]; ) {
62 		if (strcmp(temp, vchoices[i++]) == 0)
63 			return (0);
64 	}
65 	return (-1);
66 }
67 
68 void
69 ckyorn_err(char *error)
70 {
71 	puterror(stdout, ERRMSG, error);
72 }
73 
74 void
75 ckyorn_hlp(char *help)
76 {
77 	puthelp(stdout, HLPMSG, help);
78 }
79 
80 int
81 ckyorn(char *yorn, char *defstr, char *error, char *help, char *prompt)
82 {
83 	int	n;
84 	char	input[MAX_INPUT];
85 
86 	if (!prompt)
87 		prompt = "Yes or No";
88 start:
89 	putprmpt(stderr, prompt, choices, defstr);
90 	if (getinput(input))
91 		return (1);
92 
93 	n = (int)strlen(input);
94 	if (n == 0) {
95 		if (defstr) {
96 			(void) strcpy(yorn, defstr);
97 			return (0);
98 		}
99 		puterror(stderr, REQMSG, error);
100 		goto start;
101 	}
102 	if (strcmp(input, "?") == 0) {
103 		puthelp(stderr, HLPMSG, help);
104 		goto start;
105 	}
106 	if (ckquit && (strcmp(input, "q") == 0))
107 		return (3);
108 
109 	if (ckyorn_val(input)) {
110 		puterror(stderr, ERRMSG, error);
111 		goto start;
112 	}
113 	(void) strcpy(yorn, input);
114 	return (0);
115 }
116