From: Nick Bowler Date: Fri, 7 Jul 2023 00:50:22 +0000 (-0400) Subject: libcdecl: Combine error and i18n init. X-Git-Tag: v1.3~128 X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/commitdiff_plain/ffc2c34e83af80b4e1cbcbbbbdb7693a338f43a6 libcdecl: Combine error and i18n init. We can just do the gettext initialization at the same time as error initialization, which lets us use a single "glthread_once" invocation for everything. --- diff --git a/Makefile.am b/Makefile.am index 1673167..507fc67 100644 --- a/Makefile.am +++ b/Makefile.am @@ -48,10 +48,6 @@ libcdecl_la_LIBADD = $(shared_gl_objects) $(LTLIBINTL) $(LIBTHREAD) EXTRA_libcdecl_la_DEPENDENCIES = $(shared_gl_objects) $(libcdecl_la_OBJECTS): $(gnulib_headers) -if USE_NLS -libcdecl_la_SOURCES += src/i18n.c -endif - EXTRA_LIBRARIES = libgnu.a libgnu_a_SOURCES = libgnu_a_SHORTNAME = s diff --git a/src/error.c b/src/error.c index 36e6366..cf4411e 100644 --- a/src/error.c +++ b/src/error.c @@ -29,9 +29,6 @@ #include "errmsg.h" -gl_once_define(static, tls_initialized) -static gl_tls_key_t tls_key; - struct err_state { struct cdecl_error err; size_t nstr; @@ -40,6 +37,7 @@ struct err_state { /* This pre-initialized error is reserved for dire out-of-memory conditions. */ static struct cdecl_error err_no_mem; +static gl_tls_key_t tls_key; static void free_err(void *err) { @@ -65,9 +63,12 @@ static void set_err(unsigned code, struct cdecl_error *err) err->str = _(&errmsgs[code]); } -static void initialize(void) +static void initialize_cb(void) { - cdecl__init_i18n(); +#if ENABLE_NLS + bindtextdomain(PACKAGE, LOCALEDIR); + bindtextdomain("bison-runtime", BISON_LOCALEDIR); +#endif set_err(CDECL__ENOMEM, &err_no_mem); gl_tls_key_init(tls_key, free_err); } @@ -96,7 +97,8 @@ static struct err_state *get_err_state(void) { void *state; - gl_once(tls_initialized, initialize); + gl_once_define(static, tls_initialized) + gl_once(tls_initialized, initialize_cb); state = gl_tls_get(tls_key); if (state == &err_no_mem) @@ -106,6 +108,17 @@ static struct err_state *get_err_state(void) return state; } + +#if ENABLE_NLS +/* + * Initialize gettext indirectly via get_err_state. + */ +void cdecl__init_i18n(void) +{ + get_err_state(); +} +#endif + /* * Set the library error to one of the preset messages defined in errmsg.h * (CDECL__Exxx). diff --git a/src/i18n.c b/src/i18n.c deleted file mode 100644 index 753cf27..0000000 --- a/src/i18n.c +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Library initialization. - * Copyright © 2011, 2021 Nick Bowler - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include - -#include "cdecl-internal.h" - -#include - -static void do_init_i18n(void) -{ - bindtextdomain(PACKAGE, LOCALEDIR); - bindtextdomain("bison-runtime", BISON_LOCALEDIR); -} - -void cdecl__init_i18n(void) -{ - gl_once_define(static, initialized); - - int err = glthread_once(&initialized, do_init_i18n); - assert(err == 0); -} diff --git a/t/cdeclerr.c b/t/cdeclerr.c index b8a9a74..b69f7e6 100644 --- a/t/cdeclerr.c +++ b/t/cdeclerr.c @@ -27,10 +27,6 @@ #include "errmsg.h" #include "tap.h" -#if ENABLE_NLS -void cdecl__init_i18n(void) { } -#endif - static char *fmt_char(int c, char *buf) { int escape = 0;