Tue Jul 16 16:43:58 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc/history.git] / stdlib / rpmatch.c
blob4774e2cf45ebf30ece25c8f269875e207f8ab206
1 /* rpmatch - determine whether string value is affirmation or negative
2 response according to current locale's data
3 Copyright (C) 1996 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library 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 GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <langinfo.h>
23 #include <stdlib.h>
24 #include <regex.h>
27 int
28 rpmatch (response)
29 const char *response;
31 /* Match against one of the response patterns, compiling the pattern
32 first if necessary. */
33 inline int try (const int tag, const int match, const int nomatch,
34 const char **lastp, regex_t *re)
36 const char *pattern = nl_langinfo (tag);
37 if (pattern != *lastp)
39 /* The pattern has changed. */
40 if (*lastp)
42 /* Free the old compiled pattern. */
43 regfree (re);
44 *lastp = NULL;
46 /* Compile the pattern and cache it for future runs. */
47 if (regcomp (re, pattern, REG_EXTENDED) != 0)
48 return -1;
49 *lastp = pattern;
52 /* Try the pattern. */
53 return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch;
56 /* We cache the response patterns and compiled regexps here. */
57 static const char *yesexpr, *noexpr;
58 static regex_t yesre, nore;
60 return (try (YESEXPR, 1, 0, &yesexpr, &yesre) ?:
61 try (NOEXPR, 0, -1, &noexpr, &nore));