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]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
34 #include "getresponse.h"
36 /* defaults - C locale values for yesexpr/noexpr (LC_MESSAGES) */
37 #define DEFAULT_YESEXPR "^[yY]"
38 #define DEFAULT_NOEXPR "^[nN]"
44 #define SET_DEFAULT_STRS \
45 yesexpr = DEFAULT_YESEXPR; \
46 noexpr = DEFAULT_NOEXPR;
48 /* for regcomp()/regexec() yesexpr and noexpr */
49 static regex_t preg_yes
, preg_no
;
52 * This function compiles a regular expression that is used to match an
53 * affirmative response from the user, and also assigns the strings used
54 * in the prompts that request affirmative or negative responses. The
55 * locale's values for YESEXPR, NOEXPR, YESSTR and NOSTR are used.
57 * If there are any problems using the locale's YESEXPR, NOEXPR, YESSTR or NOSTR
58 * values, default values of YESEXPR, YESSTR and NOSTR will be used
59 * as a fallback. The default values are the same as the C locale values.
68 yesexpr
= strdup(nl_langinfo(YESEXPR
));
69 noexpr
= strdup(nl_langinfo(NOEXPR
));
71 if (yesexpr
== NULL
|| noexpr
== NULL
) {
77 /* if problem with locale strings, use default values */
78 if (*yesexpr
== '\0' || *noexpr
== '\0') {
83 /* Compile the yes and no expressions */
84 while (regcomp(&preg_yes
, yesexpr
, REG_EXTENDED
| REG_NOSUB
) != 0 ||
85 regcomp(&preg_no
, noexpr
, REG_EXTENDED
| REG_NOSUB
) != 0) {
87 /* The fallback yesexpr failed, so exit */
91 /* The locale's yesexpr or noexpr failed so use fallback */
100 yes_no(int (*func
)(char *))
103 char ans
[LINE_MAX
+ 1];
105 /* Get user's answer */
109 if (b
== '\n' || b
== '\0' || b
== EOF
)
116 ans
[LINE_MAX
] = '\0';
124 yes_no_check(char *ans
, regex_t
*reg1
, regex_t
*reg2
)
126 if (regexec(reg1
, ans
, 0, NULL
, 0) == 0) {
127 if (regexec(reg2
, ans
, 0, NULL
, 0) == 0) {
128 /* Both Expressions Match (reg2 conservative) */
138 * yes_check() returns 1 if the input string is matched by yesexpr and is
139 * not matched by noexpr; otherwise yes_check() returns 0.
144 return (yes_no_check(ans
, &preg_yes
, &preg_no
));
148 * no_check() returns 1 if the input string is matched by noexpr and is
149 * not matched by yesexpr; otherwise no_check() returns 0.
154 return (yes_no_check(ans
, &preg_no
, &preg_yes
));
160 return (yes_no(yes_check
));
166 return (yes_no(no_check
));