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"
29 /* This pre-initialized error is reserved for dire out-of-memory conditions. */
30 static struct cdecl_error err_no_mem;
32 static void set_err(unsigned code, struct cdecl_error *err)
34 static const char _Alignas(1) errmsgs[] = STRTAB_INITIALIZER;
38 err->code = CDECL_ENOMEM;
41 err->code = CDECL_ENOPARSE;
45 err->str = _(&errmsgs[code]);
48 static void init_once_cb(void)
51 bindtextdomain(PACKAGE, LOCALEDIR);
52 bindtextdomain("bison-runtime", BISON_LOCALEDIR);
54 set_err(CDECL__ENOMEM, &err_no_mem);
58 # include "thread-posix.h"
59 #elif USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS
60 # include "thread-stdc.h"
61 #elif USE_WINDOWS_THREADS
62 # include "thread-w32.h"
65 enum { tls_key_valid = 1 };
67 #define tls_get() tls_key
68 #define tls_set(a) ((tls_key = (a)), 1)
70 static int init_once(void)
80 struct cdecl_error err;
82 char str[FLEXIBLE_ARRAY_MEMBER];
85 static void *alloc_err_state(void *old, size_t buf_size)
87 struct err_state *state;
90 state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
92 state->nstr = buf_size;
93 if (!tls_set(state)) {
95 * We have to presume that pthread_setspecific etc.
96 * cannot fail after the key has been successfully
97 * assigned once, because there seems to be no
98 * reasonable recovery from such a scenario.
104 /* Failed allocation, but existing state is still good */
111 static struct err_state *get_err_state(void)
118 if (!(state = tls_get()))
119 return alloc_err_state(state, 100);
126 * Initialize gettext indirectly via get_err_state.
128 void cdecl__init_i18n(void)
135 * Set the library error to one of the preset messages defined in errmsg.h
138 void cdecl__errmsg(unsigned msg)
140 struct err_state *state;
142 state = get_err_state();
146 set_err(msg, &state->err);
150 * In the NLS-disabled case, all format strings are of the form
154 * so we exploit this to implement a simple snprintf workalike using the
155 * libcdecl output helpers directly.
157 * In the NLS-enabled case, we have to use snprintf as format strings may
158 * be translated. GNU libintl ensures a suitable version is available.
161 fmt_err(struct err_state *state, const char *fmt, const char *arg)
164 snprintf(state->str, state->nstr, fmt, arg);
165 return strlen(fmt) + strlen(arg);
167 struct output_state dst = { state->str, state->nstr };
170 rc = cdecl__strlcpy(dst.dst, fmt, dst.dstlen);
171 cdecl__advance(&dst, rc-2);
172 cdecl__emit(&dst, arg);
179 * Sets the library error to code; fmt is a printf-style string that may use
180 * up to one %s directive, to refer to arg.
182 void cdecl__err(const char *fmt, const char *arg)
184 struct err_state *state;
188 state = get_err_state();
193 rc = fmt_err(state, fmt, arg);
194 if (rc >= state->nstr) {
195 assert(try++ == 0 && rc < (size_t)-1 / 4);
197 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
204 state->err.code = CDECL_ENOPARSE;
205 state->err.str = state->str;
208 const struct cdecl_error *cdecl_get_error(void)
210 struct err_state *state = get_err_state();
212 return state ? &state->err : &err_no_mem;