1 /* Determine whether string value is affirmation or negative response
2 according to current locale's data.
4 Copyright (C) 1996, 1998, 2000, 2002, 2003, 2006 Free Software
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 # include <sys/types.h>
30 # define _(msgid) gettext (msgid)
33 try (const char *response, const char *pattern, const int match,
34 const int nomatch, const char **lastp, regex_t *re)
36 if (pattern != *lastp)
38 /* The pattern has changed. */
41 /* Free the old compiled pattern. */
45 /* Compile the pattern and cache it for future runs. */
46 if (regcomp (re, pattern, REG_EXTENDED) != 0)
51 /* See if the regular expression matches RESPONSE. */
52 return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
58 rpmatch (const char *response)
61 /* Match against one of the response patterns, compiling the pattern
62 first if necessary. */
64 /* We cache the response patterns and compiled regexps here. */
65 static const char *yesexpr, *noexpr;
66 static regex_t yesre, nore;
69 return ((result = try (response, _("^[yY]"), 1, 0,
72 : try (response, _("^[nN]"), 0, -1, &noexpr, &nore));
74 /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */
75 return (*response == 'y' || *response == 'Y' ? 1
76 : *response == 'n' || *response == 'N' ? 0 : -1);