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