xref: /dflybsd-src/contrib/cvs-1.12/lib/rpmatch.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* Determine whether string value is affirmation or negative response
286d7f5d3SJohn Marino    according to current locale's data.
386d7f5d3SJohn Marino    Copyright (C) 1996, 1998, 2000, 2002, 2003 Free Software Foundation, Inc.
486d7f5d3SJohn Marino 
586d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify
686d7f5d3SJohn Marino    it under the terms of the GNU General Public License as published by
786d7f5d3SJohn Marino    the Free Software Foundation; either version 2, or (at your option)
886d7f5d3SJohn Marino    any later version.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
1186d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1286d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1386d7f5d3SJohn Marino    GNU General Public License for more details.
1486d7f5d3SJohn Marino 
1586d7f5d3SJohn Marino    You should have received a copy of the GNU General Public License
1686d7f5d3SJohn Marino    along with this program; if not, write to the Free Software Foundation,
1786d7f5d3SJohn Marino    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
1886d7f5d3SJohn Marino 
1986d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
2086d7f5d3SJohn Marino # include <config.h>
2186d7f5d3SJohn Marino #endif
2286d7f5d3SJohn Marino 
2386d7f5d3SJohn Marino #include <stddef.h>
2486d7f5d3SJohn Marino #include <stdlib.h>
2586d7f5d3SJohn Marino 
2686d7f5d3SJohn Marino #if ENABLE_NLS
2786d7f5d3SJohn Marino # include <sys/types.h>
2886d7f5d3SJohn Marino # include <limits.h>
2986d7f5d3SJohn Marino # include <regex.h>
3086d7f5d3SJohn Marino # include "gettext.h"
3186d7f5d3SJohn Marino # define _(msgid) gettext (msgid)
3286d7f5d3SJohn Marino 
3386d7f5d3SJohn Marino static int
try(const char * response,const char * pattern,const int match,const int nomatch,const char ** lastp,regex_t * re)3486d7f5d3SJohn Marino try (const char *response, const char *pattern, const int match,
3586d7f5d3SJohn Marino      const int nomatch, const char **lastp, regex_t *re)
3686d7f5d3SJohn Marino {
3786d7f5d3SJohn Marino   if (pattern != *lastp)
3886d7f5d3SJohn Marino     {
3986d7f5d3SJohn Marino       /* The pattern has changed.  */
4086d7f5d3SJohn Marino       if (*lastp)
4186d7f5d3SJohn Marino 	{
4286d7f5d3SJohn Marino 	  /* Free the old compiled pattern.  */
4386d7f5d3SJohn Marino 	  regfree (re);
4486d7f5d3SJohn Marino 	  *lastp = NULL;
4586d7f5d3SJohn Marino 	}
4686d7f5d3SJohn Marino       /* Compile the pattern and cache it for future runs.  */
4786d7f5d3SJohn Marino       if (regcomp (re, pattern, REG_EXTENDED) != 0)
4886d7f5d3SJohn Marino 	return -1;
4986d7f5d3SJohn Marino       *lastp = pattern;
5086d7f5d3SJohn Marino     }
5186d7f5d3SJohn Marino 
5286d7f5d3SJohn Marino   /* See if the regular expression matches RESPONSE.  */
5386d7f5d3SJohn Marino   return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
5486d7f5d3SJohn Marino }
5586d7f5d3SJohn Marino #endif
5686d7f5d3SJohn Marino 
5786d7f5d3SJohn Marino 
5886d7f5d3SJohn Marino int
rpmatch(const char * response)5986d7f5d3SJohn Marino rpmatch (const char *response)
6086d7f5d3SJohn Marino {
6186d7f5d3SJohn Marino #if ENABLE_NLS
6286d7f5d3SJohn Marino   /* Match against one of the response patterns, compiling the pattern
6386d7f5d3SJohn Marino      first if necessary.  */
6486d7f5d3SJohn Marino 
6586d7f5d3SJohn Marino   /* We cache the response patterns and compiled regexps here.  */
6686d7f5d3SJohn Marino   static const char *yesexpr, *noexpr;
6786d7f5d3SJohn Marino   static regex_t yesre, nore;
6886d7f5d3SJohn Marino   int result;
6986d7f5d3SJohn Marino 
7086d7f5d3SJohn Marino   return ((result = try (response, _("^[yY]"), 1, 0,
7186d7f5d3SJohn Marino 			 &yesexpr, &yesre))
7286d7f5d3SJohn Marino 	  ? result
7386d7f5d3SJohn Marino 	  : try (response, _("^[nN]"), 0, -1, &noexpr, &nore));
7486d7f5d3SJohn Marino #else
7586d7f5d3SJohn Marino   /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */
7686d7f5d3SJohn Marino   return (*response == 'y' || *response == 'Y' ? 1
7786d7f5d3SJohn Marino 	  : *response == 'n' || *response == 'N' ? 0 : -1);
7886d7f5d3SJohn Marino #endif
7986d7f5d3SJohn Marino }
80