public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] nl_langinfo: Add NL_LOCALE_NAME macro
@ 2017-01-20  2:45 Eric Blake
  2017-01-20  9:34 ` Corinna Vinschen
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Blake @ 2017-01-20  2:45 UTC (permalink / raw)
  To: newlib

Provide an extension NL_LOCALE_NAME() macro, with semantics
matching glibc, which can be used as:
  nl_langinfo_l(NL_LOCALE_NAME(LC_MESSAGES), locale);
to get back the locale string that locale was originally
created with during newlocale(). This in turn allows a library
(such as gettext) to determine what thread-local locale settings
it has inherited from the main program without having to be told
what parameters were passed to newlocale(), for less overall
coupling between parts of the program.

gnulib is set up to use the extension:
https://lists.gnu.org/archive/html/bug-gnulib/2017-01/msg00129.html

* libc/include/langinfo.h (NL_LOCALE_NAME): New macro
* libc/locale/nl_langinfo.c (nl_langinfo_l): Expose locale names
of a locale_t's category components.

Signed-off-by: Eric Blake <eblake@redhat.com>
---

See this Cygwin thread for justification:
https://cygwin.com/ml/cygwin-apps/2017-01/msg00027.html

 newlib/libc/include/langinfo.h   | 10 +++++++++-
 newlib/libc/locale/nl_langinfo.c |  7 +++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/newlib/libc/include/langinfo.h b/newlib/libc/include/langinfo.h
index 0fbb2a8..193cce3 100644
--- a/newlib/libc/include/langinfo.h
+++ b/newlib/libc/include/langinfo.h
@@ -304,7 +304,7 @@ enum __nl_item
   _NL_COLLATE_CODESET,

   /* This MUST be the last entry since it's used to check for an array
-     index in nl_langinfo(). */
+     index in nl_langinfo(). It also must not exceed _NL_LOCALE_NAME_BASE. */
   _NL_LOCALE_EXTENDED_LAST_ENTRY

 #endif /* __HAVE_LOCALE_INFO_EXTENDED__ */
@@ -312,6 +312,14 @@ enum __nl_item

 };

+/* As an extension, nl_langinfo can retrive the name of a locale
+   category, with this mapping from setlocale() category (other than
+   LC_ALL) to nl_item. */
+#define _NL_LOCALE_NAME_BASE 100000
+#if __GNU_VISIBLE
+#define NL_LOCALE_NAME(category) (_NL_LOCALE_NAME_BASE + (category))
+#endif
+
 __BEGIN_DECLS
 char	*nl_langinfo (nl_item);
 #if __POSIX_VISIBLE >= 200809
diff --git a/newlib/libc/locale/nl_langinfo.c b/newlib/libc/locale/nl_langinfo.c
index 6d078b9..790ab91 100644
--- a/newlib/libc/locale/nl_langinfo.c
+++ b/newlib/libc/locale/nl_langinfo.c
@@ -368,6 +368,13 @@ do_codeset:
 		break;
 #endif
 	default:
+		/* Relies on the fact that LC_ALL is 0, and all other
+		   LC_ constants are in ascending order. */
+		if (item > NL_LOCALE_NAME(LC_ALL)
+		    && item < NL_LOCALE_NAME(_LC_LAST)) {
+			return locale->categories[item
+						  - NL_LOCALE_NAME(LC_ALL)];
+		}
 #ifdef __HAVE_LOCALE_INFO_EXTENDED__
 		if (item > _NL_LOCALE_EXTENDED_FIRST_ENTRY
 		    && item < _NL_LOCALE_EXTENDED_LAST_ENTRY) {
-- 
2.9.3

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nl_langinfo: Add NL_LOCALE_NAME macro
  2017-01-20  2:45 [PATCH] nl_langinfo: Add NL_LOCALE_NAME macro Eric Blake
@ 2017-01-20  9:34 ` Corinna Vinschen
  2017-01-20 14:06   ` Eric Blake
  0 siblings, 1 reply; 3+ messages in thread
From: Corinna Vinschen @ 2017-01-20  9:34 UTC (permalink / raw)
  To: newlib

[-- Attachment #1: Type: text/plain, Size: 1243 bytes --]

Hi Eric,

On Jan 19 20:45, Eric Blake wrote:
> Provide an extension NL_LOCALE_NAME() macro, with semantics
> matching glibc, which can be used as:
>   nl_langinfo_l(NL_LOCALE_NAME(LC_MESSAGES), locale);
> to get back the locale string that locale was originally
> created with during newlocale(). This in turn allows a library
> (such as gettext) to determine what thread-local locale settings
> it has inherited from the main program without having to be told
> what parameters were passed to newlocale(), for less overall
> coupling between parts of the program.
> 
> gnulib is set up to use the extension:
> https://lists.gnu.org/archive/html/bug-gnulib/2017-01/msg00129.html
> 
> * libc/include/langinfo.h (NL_LOCALE_NAME): New macro
> * libc/locale/nl_langinfo.c (nl_langinfo_l): Expose locale names
> of a locale_t's category components.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---

Thanks for this patch but...  did you actually try building with your
patch?  I'm getting undefined references to NL_LOCALE_NAME in
nl_langinfo.c.

The reason is a missing #define _GNU_SOURCE.

I added this to your patch and pushed it.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] nl_langinfo: Add NL_LOCALE_NAME macro
  2017-01-20  9:34 ` Corinna Vinschen
@ 2017-01-20 14:06   ` Eric Blake
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Blake @ 2017-01-20 14:06 UTC (permalink / raw)
  To: newlib


[-- Attachment #1.1: Type: text/plain, Size: 898 bytes --]

On 01/20/2017 03:33 AM, Corinna Vinschen wrote:
>>
>> * libc/include/langinfo.h (NL_LOCALE_NAME): New macro
>> * libc/locale/nl_langinfo.c (nl_langinfo_l): Expose locale names
>> of a locale_t's category components.
>>
>> Signed-off-by: Eric Blake <eblake@redhat.com>
>> ---
> 
> Thanks for this patch but...  did you actually try building with your
> patch?  I'm getting undefined references to NL_LOCALE_NAME in
> nl_langinfo.c.

Urrgh - I've got to quit changing patches after I test them. I added the
__GNU_SOURCE guard in the header at the last minute, after one last
check of whether namespace pollution was allowed by default.

> 
> The reason is a missing #define _GNU_SOURCE.
> 
> I added this to your patch and pushed it.

Thanks for fixing up after me.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-01-20 14:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-20  2:45 [PATCH] nl_langinfo: Add NL_LOCALE_NAME macro Eric Blake
2017-01-20  9:34 ` Corinna Vinschen
2017-01-20 14:06   ` Eric Blake

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).