Update.
[glibc.git] / stdlib / rpmatch.c
blob7475a7769c4c4593976a87e339b375c16b662694
1 /* Determine whether string value is affirmation or negative response
2 according to current locale's data.
3 This file is part of the GNU C Library.
4 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <langinfo.h>
22 #include <stdlib.h>
23 #include <regex.h>
26 int
27 rpmatch (response)
28 const char *response;
30 /* Match against one of the response patterns, compiling the pattern
31 first if necessary. */
32 inline int try (const int tag, const int match, const int nomatch,
33 const char **lastp, regex_t *re)
35 const char *pattern = nl_langinfo (tag);
36 if (pattern != *lastp)
38 /* The pattern has changed. */
39 if (*lastp)
41 /* Free the old compiled pattern. */
42 regfree (re);
43 *lastp = NULL;
45 /* Compile the pattern and cache it for future runs. */
46 if (regcomp (re, pattern, REG_EXTENDED) != 0)
47 return -1;
48 *lastp = pattern;
51 /* Try the pattern. */
52 return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
55 /* We cache the response patterns and compiled regexps here. */
56 static const char *yesexpr, *noexpr;
57 static regex_t yesre, nore;
59 return (try (YESEXPR, 1, 0, &yesexpr, &yesre) ?:
60 try (NOEXPR, 0, -1, &noexpr, &nore));