9128 cw(1onbld) should be able to run multiple shadows
[unleashed.git] / usr / src / common / util / getresponse.c
blob6bde237a146a3314c01b01fae6d6a4d792cbd561
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <regex.h>
30 #include <locale.h>
31 #include <langinfo.h>
32 #include <limits.h>
33 #include <errno.h>
34 #include "getresponse.h"
36 /* defaults - C locale values for yesstr, nostr, yesexpr (LC_MESSAGES) */
37 #define DEFAULT_YESSTR "yes"
38 #define DEFAULT_NOSTR "no"
39 #define DEFAULT_YESEXPR "^[yY]"
40 #define DEFAULT_NOEXPR "^[nN]"
42 #define FREE_MEM \
43 if (yesstr) \
44 free(yesstr); \
45 if (nostr) \
46 free(nostr); \
47 if (yesexpr) \
48 free(yesexpr); \
49 if (noexpr) \
50 free(noexpr)
52 #define SET_DEFAULT_STRS \
53 yesstr = DEFAULT_YESSTR; \
54 nostr = DEFAULT_NOSTR; \
55 yesexpr = DEFAULT_YESEXPR; \
56 noexpr = DEFAULT_NOEXPR;
58 /* variables used by getresponse functions */
59 char *yesstr = NULL;
60 char *nostr = NULL;
62 /* for regcomp()/regexec() yesexpr and noexpr */
63 static regex_t preg_yes, preg_no;
66 * This function compiles a regular expression that is used to match an
67 * affirmative response from the user, and also assigns the strings used
68 * in the prompts that request affirmative or negative responses. The
69 * locale's values for YESEXPR, NOEXPR, YESSTR and NOSTR are used.
71 * If there are any problems using the locale's YESEXPR, NOEXPR, YESSTR or NOSTR
72 * values, default values of YESEXPR, YESSTR and NOSTR will be used
73 * as a fallback. The default values are the same as the C locale values.
75 int
76 init_yes(void)
78 int fallback = 0;
79 char *yesexpr;
80 char *noexpr;
82 /* get yes expression and strings for yes/no prompts */
83 yesstr = strdup(nl_langinfo(YESSTR));
84 nostr = strdup(nl_langinfo(NOSTR));
85 yesexpr = strdup(nl_langinfo(YESEXPR));
86 noexpr = strdup(nl_langinfo(NOEXPR));
88 if (yesstr == NULL || nostr == NULL ||
89 yesexpr == NULL || noexpr == NULL) {
90 FREE_MEM;
91 errno = ENOMEM;
92 return (-1);
95 /* if problem with locale strings, use default values */
96 if (*yesstr == '\0' || *nostr == '\0' ||
97 *yesexpr == '\0' || *noexpr == '\0') {
98 FREE_MEM;
99 SET_DEFAULT_STRS;
100 fallback = 1;
102 /* Compile the yes and no expressions */
103 while (regcomp(&preg_yes, yesexpr, REG_EXTENDED | REG_NOSUB) != 0 ||
104 regcomp(&preg_no, noexpr, REG_EXTENDED | REG_NOSUB) != 0) {
105 if (fallback == 1) {
106 /* The fallback yesexpr failed, so exit */
107 errno = EINVAL;
108 return (-1);
110 /* The locale's yesexpr or noexpr failed so use fallback */
111 FREE_MEM;
112 SET_DEFAULT_STRS;
113 fallback = 1;
115 return (0);
118 static int
119 yes_no(int (*func)(char *))
121 int i, b;
122 char ans[LINE_MAX + 1];
124 /* Get user's answer */
125 i = 0;
126 for (;;) {
127 b = getchar();
128 if (b == '\n' || b == '\0' || b == EOF)
129 break;
130 if (i < LINE_MAX)
131 ans[i] = b;
132 i++;
134 if (i >= LINE_MAX)
135 ans[LINE_MAX] = '\0';
136 else
137 ans[i] = '\0';
139 return (func(ans));
142 static int
143 yes_no_check(char *ans, regex_t *reg1, regex_t *reg2)
145 if (regexec(reg1, ans, 0, NULL, 0) == 0) {
146 if (regexec(reg2, ans, 0, NULL, 0) == 0) {
147 /* Both Expressions Match (reg2 conservative) */
148 return (0);
150 /* Match */
151 return (1);
153 return (0);
157 * yes_check() returns 1 if the input string is matched by yesexpr and is
158 * not matched by noexpr; otherwise yes_check() returns 0.
161 yes_check(char *ans)
163 return (yes_no_check(ans, &preg_yes, &preg_no));
167 * no_check() returns 1 if the input string is matched by noexpr and is
168 * not matched by yesexpr; otherwise no_check() returns 0.
171 no_check(char *ans)
173 return (yes_no_check(ans, &preg_no, &preg_yes));
177 yes(void)
179 return (yes_no(yes_check));
183 no(void)
185 return (yes_no(no_check));