1*86d7f5d3SJohn Marino /* Determine whether string value is affirmation or negative response
2*86d7f5d3SJohn Marino according to current locale's data.
3*86d7f5d3SJohn Marino Copyright (C) 1996, 1998, 2000, 2002, 2003 Free Software Foundation, Inc.
4*86d7f5d3SJohn Marino
5*86d7f5d3SJohn Marino This program is free software; you can redistribute it and/or modify
6*86d7f5d3SJohn Marino it under the terms of the GNU General Public License as published by
7*86d7f5d3SJohn Marino the Free Software Foundation; either version 2, or (at your option)
8*86d7f5d3SJohn Marino any later version.
9*86d7f5d3SJohn Marino
10*86d7f5d3SJohn Marino This program is distributed in the hope that it will be useful,
11*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*86d7f5d3SJohn Marino GNU General Public License for more details.
14*86d7f5d3SJohn Marino
15*86d7f5d3SJohn Marino You should have received a copy of the GNU General Public License
16*86d7f5d3SJohn Marino along with this program; if not, write to the Free Software Foundation,
17*86d7f5d3SJohn Marino Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18*86d7f5d3SJohn Marino
19*86d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
20*86d7f5d3SJohn Marino # include <config.h>
21*86d7f5d3SJohn Marino #endif
22*86d7f5d3SJohn Marino
23*86d7f5d3SJohn Marino #include <stddef.h>
24*86d7f5d3SJohn Marino #include <stdlib.h>
25*86d7f5d3SJohn Marino
26*86d7f5d3SJohn Marino #if ENABLE_NLS
27*86d7f5d3SJohn Marino # include <sys/types.h>
28*86d7f5d3SJohn Marino # include <limits.h>
29*86d7f5d3SJohn Marino # include <regex.h>
30*86d7f5d3SJohn Marino # include "gettext.h"
31*86d7f5d3SJohn Marino # define _(msgid) gettext (msgid)
32*86d7f5d3SJohn Marino
33*86d7f5d3SJohn Marino static int
try(const char * response,const char * pattern,const int match,const int nomatch,const char ** lastp,regex_t * re)34*86d7f5d3SJohn Marino try (const char *response, const char *pattern, const int match,
35*86d7f5d3SJohn Marino const int nomatch, const char **lastp, regex_t *re)
36*86d7f5d3SJohn Marino {
37*86d7f5d3SJohn Marino if (pattern != *lastp)
38*86d7f5d3SJohn Marino {
39*86d7f5d3SJohn Marino /* The pattern has changed. */
40*86d7f5d3SJohn Marino if (*lastp)
41*86d7f5d3SJohn Marino {
42*86d7f5d3SJohn Marino /* Free the old compiled pattern. */
43*86d7f5d3SJohn Marino regfree (re);
44*86d7f5d3SJohn Marino *lastp = NULL;
45*86d7f5d3SJohn Marino }
46*86d7f5d3SJohn Marino /* Compile the pattern and cache it for future runs. */
47*86d7f5d3SJohn Marino if (regcomp (re, pattern, REG_EXTENDED) != 0)
48*86d7f5d3SJohn Marino return -1;
49*86d7f5d3SJohn Marino *lastp = pattern;
50*86d7f5d3SJohn Marino }
51*86d7f5d3SJohn Marino
52*86d7f5d3SJohn Marino /* See if the regular expression matches RESPONSE. */
53*86d7f5d3SJohn Marino return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
54*86d7f5d3SJohn Marino }
55*86d7f5d3SJohn Marino #endif
56*86d7f5d3SJohn Marino
57*86d7f5d3SJohn Marino
58*86d7f5d3SJohn Marino int
rpmatch(const char * response)59*86d7f5d3SJohn Marino rpmatch (const char *response)
60*86d7f5d3SJohn Marino {
61*86d7f5d3SJohn Marino #if ENABLE_NLS
62*86d7f5d3SJohn Marino /* Match against one of the response patterns, compiling the pattern
63*86d7f5d3SJohn Marino first if necessary. */
64*86d7f5d3SJohn Marino
65*86d7f5d3SJohn Marino /* We cache the response patterns and compiled regexps here. */
66*86d7f5d3SJohn Marino static const char *yesexpr, *noexpr;
67*86d7f5d3SJohn Marino static regex_t yesre, nore;
68*86d7f5d3SJohn Marino int result;
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino return ((result = try (response, _("^[yY]"), 1, 0,
71*86d7f5d3SJohn Marino &yesexpr, &yesre))
72*86d7f5d3SJohn Marino ? result
73*86d7f5d3SJohn Marino : try (response, _("^[nN]"), 0, -1, &noexpr, &nore));
74*86d7f5d3SJohn Marino #else
75*86d7f5d3SJohn Marino /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */
76*86d7f5d3SJohn Marino return (*response == 'y' || *response == 'Y' ? 1
77*86d7f5d3SJohn Marino : *response == 'n' || *response == 'N' ? 0 : -1);
78*86d7f5d3SJohn Marino #endif
79*86d7f5d3SJohn Marino }
80