f1b7071fd8b0dcb3076653985b0cc7c692c421be
[gnulib.git] / lib / rpmatch.c
1 /* Determine whether string value is affirmation or negative response
2    according to current locale's data.
3
4    Copyright (C) 1996, 1998, 2000, 2002, 2003, 2006, 2008 Free Software
5    Foundation, Inc.
6
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.
11
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.
16
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/>.  */
19
20 #include <config.h>
21
22 #include <stddef.h>
23 #include <stdlib.h>
24
25 #if ENABLE_NLS
26 # include <sys/types.h>
27 # include <limits.h>
28 # include <regex.h>
29 # include "gettext.h"
30 # define _(msgid) gettext (msgid)
31
32 static int
33 try (const char *response, const char *pattern, const int match,
34      const int nomatch, const char **lastp, regex_t *re)
35 {
36   if (pattern != *lastp)
37     {
38       /* The pattern has changed.  */
39       if (*lastp)
40         {
41           /* Free the old compiled pattern.  */
42           regfree (re);
43           *lastp = NULL;
44         }
45       /* Compile the pattern and cache it for future runs.  */
46       if (regcomp (re, pattern, REG_EXTENDED) != 0)
47         return -1;
48       *lastp = pattern;
49     }
50
51   /* See if the regular expression matches RESPONSE.  */
52   return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
53 }
54 #endif
55
56
57 int
58 rpmatch (const char *response)
59 {
60 #if ENABLE_NLS
61   /* Match against one of the response patterns, compiling the pattern
62      first if necessary.  */
63
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.  */
72
73   /* We cache the response patterns and compiled regexps here.  */
74   static const char *yesexpr, *noexpr;
75   static regex_t yesre, nore;
76   int result;
77
78   return ((result = try (response, _("^[yY]"), 1, 0,
79                          &yesexpr, &yesre))
80           ? result
81           : try (response, _("^[nN]"), 0, -1, &noexpr, &nore));
82 #else
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);
86 #endif
87 }