Mercurial > gnulib
annotate lib/rpmatch.c @ 9989:2bff2d33e74d
Add some comments.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 27 Apr 2008 23:30:18 +0200 |
parents | bbbbbf4cd1c5 |
children | 8095c13f76df |
rev | line source |
---|---|
882 | 1 /* Determine whether string value is affirmation or negative response |
2 according to current locale's data. | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
3 |
9989 | 4 Copyright (C) 1996, 1998, 2000, 2002, 2003, 2006, 2008 Free Software |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
5 Foundation, Inc. |
662 | 6 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
7 This program is free software: you can redistribute it and/or modify |
882 | 8 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
9 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
10 (at your option) any later version. |
662 | 11 |
882 | 12 This program 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 | |
15 GNU General Public License for more details. | |
662 | 16 |
882 | 17 You should have received a copy of the GNU General Public License |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
662 | 19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
20 #include <config.h> |
662 | 21 |
4333 | 22 #include <stddef.h> |
4674 | 23 #include <stdlib.h> |
665
724b81f1f93a
[STDC_HEADERS]: Guard inclusion of stdlib.h.
Jim Meyering <jim@meyering.net>
parents:
662
diff
changeset
|
24 |
681
c952aa1385fd
(ENABLE_NLS): Include libintl.h and define _ to gettext.
Jim Meyering <jim@meyering.net>
parents:
667
diff
changeset
|
25 #if ENABLE_NLS |
1558
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
26 # include <sys/types.h> |
4333 | 27 # include <limits.h> |
1558
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
28 # include <regex.h> |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
2444
diff
changeset
|
29 # include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
2444
diff
changeset
|
30 # define _(msgid) gettext (msgid) |
662 | 31 |
32 static int | |
1579 | 33 try (const char *response, const char *pattern, const int match, |
34 const int nomatch, const char **lastp, regex_t *re) | |
662 | 35 { |
36 if (pattern != *lastp) | |
37 { | |
38 /* The pattern has changed. */ | |
39 if (*lastp) | |
40 { | |
41 /* Free the old compiled pattern. */ | |
42 regfree (re); | |
43 *lastp = NULL; | |
44 } | |
45 /* Compile the pattern and cache it for future runs. */ | |
46 if (regcomp (re, pattern, REG_EXTENDED) != 0) | |
47 return -1; | |
48 *lastp = pattern; | |
49 } | |
50 | |
665
724b81f1f93a
[STDC_HEADERS]: Guard inclusion of stdlib.h.
Jim Meyering <jim@meyering.net>
parents:
662
diff
changeset
|
51 /* See if the regular expression matches RESPONSE. */ |
662 | 52 return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch; |
53 } | |
1558
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
54 #endif |
662 | 55 |
56 | |
57 int | |
1557 | 58 rpmatch (const char *response) |
662 | 59 { |
1558
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
60 #if ENABLE_NLS |
662 | 61 /* Match against one of the response patterns, compiling the pattern |
62 first if necessary. */ | |
63 | |
9989 | 64 /* We get the patterns from a PO file. It would be possible to use |
65 nl_langinfo (YESEXPR) instead of _("^[yY]"), and nl_langinfo (NOEXPR) | |
66 instead of _("^[nN]"), if we could assume that the system's locale | |
67 support is good. But this is not the case e.g. on Cygwin. The | |
68 localizations of gnulib.pot are of better quality in general. | |
69 Also, if we used locale info from non-free systems that don't have a | |
70 'localedef' command, we would deprive the users of the freedom to | |
71 localize this pattern for their preferred language. */ | |
72 | |
662 | 73 /* We cache the response patterns and compiled regexps here. */ |
74 static const char *yesexpr, *noexpr; | |
75 static regex_t yesre, nore; | |
76 int result; | |
77 | |
682
8356321f3b4c
this conforms to POSIX.2. From Paul Eggert <eggert@twinsun.com>.
Jim Meyering <jim@meyering.net>
parents:
681
diff
changeset
|
78 return ((result = try (response, _("^[yY]"), 1, 0, |
665
724b81f1f93a
[STDC_HEADERS]: Guard inclusion of stdlib.h.
Jim Meyering <jim@meyering.net>
parents:
662
diff
changeset
|
79 &yesexpr, &yesre)) |
662 | 80 ? result |
682
8356321f3b4c
this conforms to POSIX.2. From Paul Eggert <eggert@twinsun.com>.
Jim Meyering <jim@meyering.net>
parents:
681
diff
changeset
|
81 : try (response, _("^[nN]"), 0, -1, &noexpr, &nore)); |
1558
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
82 #else |
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
83 /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */ |
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
84 return (*response == 'y' || *response == 'Y' ? 1 |
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
85 : *response == 'n' || *response == 'N' ? 0 : -1); |
db3dc1c514e1
(rpmatch) [!ENABLE_NLS]: Hard-code tests to use `^[yY]' and `^[nN]'.
Jim Meyering <jim@meyering.net>
parents:
1557
diff
changeset
|
86 #endif |
662 | 87 } |