Eric Blake wrote: > The yesno module also has a potential POSIX-compliance problem > on all platforms. Inside rpmatch, the code uses _("^[yY]") to > get the locale-specific regex to use as the yesexpr. However, > this is not necessarily the same as using nl_langinfo(YESEXPR).
Why is this a POSIX compliance problem? POSIX does not specify the yesno() function nor the rpmatch() function. > Either gettext should special-case the strings "^[yY]" and "^[nN]", > or rpmatch should be fixed to use nl_langinfo instead of gettext. Remember that we support many platforms which have deficient locale support. For example, what is nl_langinfo(YESEXPR) in a German or French locale on Cygwin? Just the same string as in English. That's why we put "^[yY]" into gnulib's message catalog. But I have no problem with using nl_langinfo(YESEXPR) when it has a locale dependent information and gnulib's message catalog doesn't. > If nothing else, we should add a TRANSLATOR comment before > both regex strings in rpmatch.c, to make it obvious that these strings > should be consistent with the locale's choice of yesexpr and noexpr, > rather than the usual use of _() to designate translated output. Consistency is not a must, in my opinion. But a translator comment cannot hurt. How about this patch? OK to commit? 2007-08-18 Bruno Haible <[EMAIL PROTECTED]> * lib/rpmatch.c: Include langinfo.h. (N_): New macro. (localized_pattern): New function/macro. (rpmatch): Use it. Add translator comments. * m4/rpmatch.m4 (gl_PREREQ_RPMATCH): Test for nl_langinfo and YESEXPR. Suggested by Eric Blake. *** lib/rpmatch.c 13 Sep 2006 22:38:14 -0000 1.20 --- lib/rpmatch.c 18 Aug 2007 15:37:52 -0000 *************** *** 1,7 **** /* Determine whether string value is affirmation or negative response according to current locale's data. ! Copyright (C) 1996, 1998, 2000, 2002, 2003, 2006 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify --- 1,7 ---- /* Determine whether string value is affirmation or negative response according to current locale's data. ! Copyright (C) 1996, 1998, 2000, 2002-2003, 2006-2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify *************** *** 27,34 **** --- 27,38 ---- # include <sys/types.h> # include <limits.h> # include <regex.h> + # if HAVE_LANGINFO_YESEXPR + # include <langinfo.h> + # endif # include "gettext.h" # define _(msgid) gettext (msgid) + # define N_(msgid) gettext_noop (msgid) static int try (const char *response, const char *pattern, const int match, *************** *** 52,60 **** --- 56,88 ---- /* See if the regular expression matches RESPONSE. */ return regexec (re, response, 0, NULL, 0) == 0 ? match : nomatch; } + + # if HAVE_LANGINFO_YESEXPR + static const char * + localized_pattern (const char *english_pattern, nl_item nl_index) + { + /* First, look in the gnulib message catalog. */ + const char *translated_pattern = _(english_pattern); + if (translated_pattern == english_pattern) + { + /* The gnulib message catalog provides no translation. + Try the system's message catalog. */ + translated_pattern = nl_langinfo (nl_index); + if (translated_pattern == NULL || translated_pattern[0] == '\0') + /* Broken system. */ + translated_pattern = english_pattern; + } + return translated_pattern; + } + # else + # define localized_pattern(english_pattern,nl_index) _(english_pattern) + # endif + #endif + /* Test a user response to a question. + Return 1 if it is affirmative, 0 if it is negative, or -1 if not clear. */ int rpmatch (const char *response) { *************** *** 67,76 **** static regex_t yesre, nore; int result; ! return ((result = try (response, _("^[yY]"), 1, 0, ! &yesexpr, &yesre)) ? result ! : try (response, _("^[nN]"), 0, -1, &noexpr, &nore)); #else /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */ return (*response == 'y' || *response == 'Y' ? 1 --- 95,121 ---- static regex_t yesre, nore; int result; ! return ((result = try (response, ! /* TRANSLATORS: A regular expression testing for an ! affirmative answer (english: "yes"). Testing the ! first character may be sufficient. Take care to ! consider upper and lower case. ! To enquire the regular expression that your system ! uses for this purpose, you can use the command ! locale -k LC_MESSAGES | grep '^yesexpr=' */ ! localized_pattern (N_("^[yY]"), YESEXPR), ! 1, 0, &yesexpr, &yesre)) ? result ! : try (response, ! /* TRANSLATORS: A regular expression testing for a ! negative answer (english: "no"). Testing the ! first character may be sufficient. Take care to ! consider upper and lower case. ! To enquire the regular expression that your system ! uses for this purpose, you can use the command ! locale -k LC_MESSAGES | grep '^noexpr=' */ ! localized_pattern (N_("^[nN]"), NOEXPR), ! 0, -1, &noexpr, &nore)); #else /* Test against "^[yY]" and "^[nN]", hardcoded to avoid requiring regex */ return (*response == 'y' || *response == 'Y' ? 1 *** m4/rpmatch.m4 23 Jan 2005 08:06:57 -0000 1.5 --- m4/rpmatch.m4 18 Aug 2007 15:37:52 -0000 *************** *** 1,5 **** ! # rpmatch.m4 serial 5 ! dnl Copyright (C) 2002, 2003 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. --- 1,5 ---- ! # rpmatch.m4 serial 6 ! dnl Copyright (C) 2002, 2003, 2007 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. *************** *** 13,16 **** ]) # Prerequisites of lib/rpmatch.c. ! AC_DEFUN([gl_PREREQ_RPMATCH], [:]) --- 13,27 ---- ]) # Prerequisites of lib/rpmatch.c. ! AC_DEFUN([gl_PREREQ_RPMATCH], [ ! AC_CACHE_CHECK([for nl_langinfo and YESEXPR], gl_cv_langinfo_yesexpr, ! [AC_TRY_LINK([#include <langinfo.h>], ! [char* cs = nl_langinfo(YESEXPR); return !cs;], ! gl_cv_langinfo_yesexpr=yes, ! gl_cv_langinfo_yesexpr=no) ! ]) ! if test $gl_cv_langinfo_yesexpr = yes; then ! AC_DEFINE([HAVE_LANGINFO_YESEXPR], 1, ! [Define if you have <langinfo.h> and nl_langinfo(YESEXPR).]) ! fi ! ])