]>
Commit | Line | Data |
---|---|---|
7cc27f44 UD |
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. | |
9c7ff11a | 4 | Copyright (C) 1996, 1997, 2000, 2003 Free Software Foundation, Inc. |
857fa1b8 | 5 | |
7cc27f44 | 6 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
857fa1b8 | 10 | |
7cc27f44 UD |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
857fa1b8 | 15 | |
41bdb6e2 AJ |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. */ | |
857fa1b8 RM |
20 | |
21 | #include <langinfo.h> | |
22 | #include <stdlib.h> | |
23 | #include <regex.h> | |
24 | ||
25 | ||
26 | int | |
27 | rpmatch (response) | |
28 | const char *response; | |
29 | { | |
30 | /* Match against one of the response patterns, compiling the pattern | |
31 | first if necessary. */ | |
9c7ff11a UD |
32 | auto int try (const int tag, const int match, const int nomatch, |
33 | const char **lastp, regex_t *re); | |
938c669e | 34 | |
9c7ff11a UD |
35 | int try (const int tag, const int match, const int nomatch, |
36 | const char **lastp, regex_t *re) | |
857fa1b8 RM |
37 | { |
38 | const char *pattern = nl_langinfo (tag); | |
39 | if (pattern != *lastp) | |
40 | { | |
41 | /* The pattern has changed. */ | |
42 | if (*lastp) | |
43 | { | |
44 | /* Free the old compiled pattern. */ | |
4aebaa6b | 45 | __regfree (re); |
857fa1b8 RM |
46 | *lastp = NULL; |
47 | } | |
48 | /* Compile the pattern and cache it for future runs. */ | |
4aebaa6b | 49 | if (__regcomp (re, pattern, REG_EXTENDED) != 0) |
857fa1b8 RM |
50 | return -1; |
51 | *lastp = pattern; | |
52 | } | |
53 | ||
54 | /* Try the pattern. */ | |
4aebaa6b | 55 | return __regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch; |
857fa1b8 RM |
56 | } |
57 | ||
58 | /* We cache the response patterns and compiled regexps here. */ | |
59 | static const char *yesexpr, *noexpr; | |
60 | static regex_t yesre, nore; | |
61 | ||
a641835a RM |
62 | return (try (YESEXPR, 1, 0, &yesexpr, &yesre) ?: |
63 | try (NOEXPR, 0, -1, &noexpr, &nore)); | |
857fa1b8 | 64 | } |