]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Combine error and i18n init.
authorNick Bowler <nbowler@draconx.ca>
Fri, 7 Jul 2023 00:50:22 +0000 (20:50 -0400)
committerNick Bowler <nbowler@draconx.ca>
Fri, 7 Jul 2023 00:50:22 +0000 (20:50 -0400)
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.

Makefile.am
src/error.c
src/i18n.c [deleted file]
t/cdeclerr.c

index 1673167297516428a03083512201b7e50f8dd638..507fc67cf1bd56b6287c93a8afd22a8d4695911b 100644 (file)
@@ -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
index 36e63666774d6a34cbf0ad99c0d14d06044adf85..cf4411e0e89b8da7dfbe82fcbda7ddf0c18c871b 100644 (file)
@@ -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 (file)
index 753cf27..0000000
+++ /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 <http://www.gnu.org/licenses/>.
- */
-
-#include <config.h>
-#include <stdio.h>
-#include <assert.h>
-
-#include "cdecl-internal.h"
-
-#include <glthread/lock.h>
-
-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);
-}
index b8a9a741210cee508aef27bf494a9839779da643..b69f7e60971a8088731889df2cfa4f13ccf078d5 100644 (file)
 #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;