2 * Error handling for libcdecl.
3 * Copyright © 2011-2012, 2021, 2023 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "cdecl-internal.h"
27 #include <glthread/lock.h>
28 #include <glthread/tls.h>
33 struct cdecl_error err;
35 char str[FLEXIBLE_ARRAY_MEMBER];
38 /* This pre-initialized error is reserved for dire out-of-memory conditions. */
39 static struct cdecl_error err_no_mem;
40 static gl_tls_key_t tls_key;
42 static void free_err(void *err)
44 if (err == &err_no_mem)
50 static void set_err(unsigned code, struct cdecl_error *err)
52 static const char errmsgs[] = STRTAB_INITIALIZER;
56 err->code = CDECL_ENOMEM;
59 err->code = CDECL_ENOPARSE;
63 err->str = _(&errmsgs[code]);
66 static void initialize_cb(void)
69 bindtextdomain(PACKAGE, LOCALEDIR);
70 bindtextdomain("bison-runtime", BISON_LOCALEDIR);
72 set_err(CDECL__ENOMEM, &err_no_mem);
73 gl_tls_key_init(tls_key, free_err);
76 static void *alloc_err_state(void *old, size_t buf_size)
78 struct err_state *state;
81 state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
83 state->nstr = buf_size;
85 /* Failed allocation, but existing state is still good */
88 /* Failed allocation, no existing state */
92 gl_tls_set(tls_key, p);
96 static struct err_state *get_err_state(void)
100 gl_once_define(static, tls_initialized)
101 gl_once(tls_initialized, initialize_cb);
103 state = gl_tls_get(tls_key);
104 if (state == &err_no_mem)
107 return alloc_err_state(state, 100);
114 * Initialize gettext indirectly via get_err_state.
116 void cdecl__init_i18n(void)
123 * Set the library error to one of the preset messages defined in errmsg.h
126 void cdecl__errmsg(unsigned msg)
128 struct err_state *state;
130 state = get_err_state();
134 set_err(msg, &state->err);
138 * Sets the library error to code; fmt is a printf-style string that may use
139 * up to one %s directive, to refer to arg.
141 void cdecl__err(unsigned code, const char *fmt, const char *arg)
143 struct err_state *state;
146 state = get_err_state();
151 rc = snprintf(state->str, state->nstr, fmt, arg);
152 if (rc > 0 && rc >= state->nstr) {
153 assert(try++ == 0 && rc < SIZE_MAX / 4);
154 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
161 state->err.str = state->str;
162 state->err.code = code;
165 const struct cdecl_error *cdecl_get_error(void)
167 struct err_state *state = get_err_state();
169 return state ? &state->err : NULL;