Back to index

glibc  2.9
rpmatch.c
Go to the documentation of this file.
00001 /* Determine whether string value is affirmation or negative response
00002    according to current locale's data.
00003    This file is part of the GNU C Library.
00004    Copyright (C) 1996, 1997, 2000, 2003 Free Software Foundation, Inc.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <langinfo.h>
00022 #include <stdlib.h>
00023 #include <regex.h>
00024 
00025 
00026 int
00027 rpmatch (response)
00028      const char *response;
00029 {
00030   /* Match against one of the response patterns, compiling the pattern
00031      first if necessary.  */
00032   auto int try (const int tag, const int match, const int nomatch,
00033               const char **lastp, regex_t *re);
00034 
00035   int try (const int tag, const int match, const int nomatch,
00036           const char **lastp, regex_t *re)
00037     {
00038       const char *pattern = nl_langinfo (tag);
00039       if (pattern != *lastp)
00040        {
00041          /* The pattern has changed.  */
00042          if (*lastp)
00043            {
00044              /* Free the old compiled pattern.  */
00045              __regfree (re);
00046              *lastp = NULL;
00047            }
00048          /* Compile the pattern and cache it for future runs.  */
00049          if (__regcomp (re, pattern, REG_EXTENDED) != 0)
00050            return -1;
00051          *lastp = pattern;
00052        }
00053 
00054       /* Try the pattern.  */
00055       return __regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
00056     }
00057 
00058   /* We cache the response patterns and compiled regexps here.  */
00059   static const char *yesexpr, *noexpr;
00060   static regex_t yesre, nore;
00061 
00062   return (try (YESEXPR, 1, 0, &yesexpr, &yesre) ?:
00063          try (NOEXPR, 0, -1, &noexpr, &nore));
00064 }