2 * Error handling for libcdecl.
3 * Copyright © 2011-2012, 2021, 2023-2024 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/>.
26 #include "cdecl-internal.h"
28 #include <glthread/lock.h>
33 # include "thread-posix.h"
34 #elif USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
35 # include "thread-stdc.h"
36 #elif USE_WINDOWS_THREADS
37 # include "thread-w32.h"
40 enum { tls_key_valid = 1 };
42 #define tls_key_init() ((void)0)
43 #define tls_get() tls_key
44 #define tls_set(a) (tls_key = (a), 1)
48 struct cdecl_error err;
50 char str[FLEXIBLE_ARRAY_MEMBER];
53 /* This pre-initialized error is reserved for dire out-of-memory conditions. */
54 static struct cdecl_error err_no_mem;
56 static void set_err(unsigned code, struct cdecl_error *err)
58 static const char errmsgs[] = STRTAB_INITIALIZER;
62 err->code = CDECL_ENOMEM;
65 err->code = CDECL_ENOPARSE;
69 err->str = _(&errmsgs[code]);
72 static void initialize_cb(void)
75 bindtextdomain(PACKAGE, LOCALEDIR);
76 bindtextdomain("bison-runtime", BISON_LOCALEDIR);
78 set_err(CDECL__ENOMEM, &err_no_mem);
82 static void *alloc_err_state(void *old, size_t buf_size)
84 struct err_state *state;
87 state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
89 state->nstr = buf_size;
90 if (!tls_set(state)) {
92 * We have to presume that pthread_setspecific etc.
93 * cannot fail after the key has been successfully
94 * assigned once, because there seems to be no
95 * reasonable recovery from such a scenario.
101 /* Failed allocation, but existing state is still good */
108 static struct err_state *get_err_state(void)
112 gl_once_define(static, tls_initialized)
113 if (glthread_once(&tls_initialized, initialize_cb) || !tls_key_valid)
116 if (!(state = tls_get()))
117 return alloc_err_state(state, 100);
124 * Initialize gettext indirectly via get_err_state.
126 void cdecl__init_i18n(void)
133 * Set the library error to one of the preset messages defined in errmsg.h
136 void cdecl__errmsg(unsigned msg)
138 struct err_state *state;
140 state = get_err_state();
144 set_err(msg, &state->err);
148 * In the NLS-disabled case, all format strings are of the form
152 * so we exploit this to implement a simple snprintf workalike using the
153 * libcdecl output helpers directly.
155 * In the NLS-enabled case, we have to use snprintf as format strings may
156 * be translated. GNU libintl ensures a suitable version is available.
159 fmt_err(struct err_state *state, const char *fmt, const char *arg)
162 snprintf(state->str, state->nstr, fmt, arg);
163 return strlen(fmt) + strlen(arg);
165 struct output_state dst = { state->str, state->nstr };
168 rc = cdecl__strlcpy(dst.dst, fmt, dst.dstlen);
169 cdecl__advance(&dst, rc-2);
170 cdecl__emit(&dst, arg);
177 * Sets the library error to code; fmt is a printf-style string that may use
178 * up to one %s directive, to refer to arg.
180 void cdecl__err(const char *fmt, const char *arg)
182 struct err_state *state;
186 state = get_err_state();
191 rc = fmt_err(state, fmt, arg);
192 if (rc >= state->nstr) {
193 assert(try++ == 0 && rc < (size_t)-1 / 4);
195 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
202 state->err.code = CDECL_ENOPARSE;
203 state->err.str = state->str;
206 const struct cdecl_error *cdecl_get_error(void)
208 struct err_state *state = get_err_state();
210 return state ? &state->err : &err_no_mem;