]>
Commit | Line | Data |
---|---|---|
857fa1b8 RM |
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. | |
4 | ||
5 | This file is part of the GNU C Library. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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. */ | |
21 | ||
22 | #include <langinfo.h> | |
23 | #include <stdlib.h> | |
24 | #include <regex.h> | |
25 | ||
26 | ||
27 | int | |
28 | rpmatch (response) | |
29 | const char *response; | |
30 | { | |
31 | /* Match against one of the response patterns, compiling the pattern | |
32 | first if necessary. */ | |
a641835a | 33 | inline int try (const int tag, const int match, const int nomatch, |
857fa1b8 RM |
34 | const char **lastp, regex_t *re) |
35 | { | |
36 | const char *pattern = nl_langinfo (tag); | |
37 | if (pattern != *lastp) | |
38 | { | |
39 | /* The pattern has changed. */ | |
40 | if (*lastp) | |
41 | { | |
42 | /* Free the old compiled pattern. */ | |
43 | regfree (re); | |
44 | *lastp = NULL; | |
45 | } | |
46 | /* Compile the pattern and cache it for future runs. */ | |
47 | if (regcomp (re, pattern, REG_EXTENDED) != 0) | |
48 | return -1; | |
49 | *lastp = pattern; | |
50 | } | |
51 | ||
52 | /* Try the pattern. */ | |
a641835a | 53 | return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch; |
857fa1b8 RM |
54 | } |
55 | ||
56 | /* We cache the response patterns and compiled regexps here. */ | |
57 | static const char *yesexpr, *noexpr; | |
58 | static regex_t yesre, nore; | |
59 | ||
a641835a RM |
60 | return (try (YESEXPR, 1, 0, &yesexpr, &yesre) ?: |
61 | try (NOEXPR, 0, -1, &noexpr, &nore)); | |
857fa1b8 | 62 | } |