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, 2008 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 get the patterns from a PO file. It would be possible to use
65 nl_langinfo (YESEXPR) instead of _("^[yY]"), and nl_langinfo (NOEXPR)
66 instead of _("^[nN]"), if we could assume that the system's locale
67 support is good. But this is not the case e.g. on Cygwin. The
68 localizations of gnulib.pot are of better quality in general.
69 Also, if we used locale info from non-free systems that don't have a
70 'localedef' command, we would deprive the users of the freedom to
71 localize this pattern for their preferred language. */
73 /* We cache the response patterns and compiled regexps here. */
74 static const char *yesexpr, *noexpr;
75 static regex_t yesre, nore;
78 return ((result = try (response, _("^[yY]"), 1, 0,
81 : try (response, _("^[nN]"), 0, -1, &noexpr, &nore));
83 /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */
84 return (*response == 'y' || *response == 'Y' ? 1
85 : *response == 'n' || *response == 'N' ? 0 : -1);