>From c53ceb6e42e7a90764a11c263b904de82f8c8192 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sat, 12 Sep 2026 20:41:12 -0400 Subject: [PATCH 04/18] add common __loc_lookup backend for new locale this function is to be shared by all langinfo and error string lookup functions. it takes pointers to the built-in C locale data and the corresponding subtable of the locale table, along with a path to the data requested and a fallback path if the requested key does not exist. if the requested path is not found in the C locale, it is replaced with the fallback, allowing a default message/value such as "unknown error" to be obtained, and the lookup is restarted. if the requested path is found in the C locale, it is also searched in the locale table, and replaces the value from the C locale if found. --- src/internal/locale_impl.h | 4 ++++ src/locale/__loc_lookup.c | 12 ++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/locale/__loc_lookup.c diff --git a/src/internal/locale_impl.h b/src/internal/locale_impl.h index 4431a92e..33e54347 100644 --- a/src/internal/locale_impl.h +++ b/src/internal/locale_impl.h @@ -22,6 +22,7 @@ extern hidden const struct __locale_struct __c_locale; extern hidden const struct __locale_struct __c_dot_utf8_locale; hidden const struct __locale_map *__get_locale(int, const char *); +hidden const char *__loc_lookup(const unsigned char *, const unsigned char *, const int *, const int *); hidden const char *__mo_lookup(const void *, size_t, const char *); hidden const char *__lctrans(const char *, const struct __locale_map *); hidden const char *__lctrans_cur(const char *); @@ -31,6 +32,9 @@ hidden char *__gettextdomain(void); #define LOC_MAP_FAILED ((const struct __locale_map *)-1) +#define LMEMB(l,c,m) ((l) && (l)->cat[(c)] ? (l)->cat[(c)]->m : 0) +#define LPATH(...) { sizeof((int[]){__VA_ARGS__})/sizeof(int), __VA_ARGS__ } + #define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)]) #define LCTRANS_CUR(msg) __lctrans_cur(msg) diff --git a/src/locale/__loc_lookup.c b/src/locale/__loc_lookup.c new file mode 100644 index 00000000..7bc4d6ae --- /dev/null +++ b/src/locale/__loc_lookup.c @@ -0,0 +1,12 @@ +#include "locale_impl.h" +#include "ikmlt.h" + +const char *__loc_lookup(const unsigned char *ptable, const unsigned char *ltable, const int *path, const int *fallback) +{ + if (!path) return 0; + const char *pstr = ikmlt_lookup_path(ptable, path); + if (!pstr) return __loc_lookup(ptable, ltable, fallback, 0); + if (!ltable) return (char *)pstr; + const char *lstr = ikmlt_lookup_path(ltable, path); + return lstr ? (char *)lstr : (char *)pstr; +} -- 2.21.0