]> git.draconx.ca Git - cdecl99.git/blob - src/error.c
0ff31ee0a4f6871e5eaadac892aed76c4b2b7878
[cdecl99.git] / src / error.c
1 /*
2  *  Error handling for libcdecl.
3  *  Copyright © 2011-2012, 2021, 2023-2024 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <stdbool.h>
24
25 #include "cdecl.h"
26 #include "cdecl-internal.h"
27
28 #include <glthread/lock.h>
29
30 #include "errmsg.h"
31
32 #if USE_POSIX_THREADS
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"
38 #else
39 static void *tls_key;
40 enum { tls_key_valid = 1 };
41
42 #define tls_key_init() ((void)0)
43 #define tls_get() tls_key
44 #define tls_set(a) (tls_key = (a), 1)
45 #endif
46
47 struct err_state {
48         struct cdecl_error err;
49         size_t nstr;
50         char str[FLEXIBLE_ARRAY_MEMBER];
51 };
52
53 /* This pre-initialized error is reserved for dire out-of-memory conditions. */
54 static struct cdecl_error err_no_mem;
55
56 static void set_err(unsigned code, struct cdecl_error *err)
57 {
58         static const char errmsgs[] = STRTAB_INITIALIZER;
59
60         switch (code) {
61         case CDECL__ENOMEM:
62                 err->code = CDECL_ENOMEM;
63                 break;
64         default:
65                 err->code = CDECL_ENOPARSE;
66                 break;
67         }
68
69         err->str = _(&errmsgs[code]);
70 }
71
72 static void initialize_cb(void)
73 {
74 #if ENABLE_NLS
75         bindtextdomain(PACKAGE, LOCALEDIR);
76         bindtextdomain("bison-runtime", BISON_LOCALEDIR);
77 #endif
78         set_err(CDECL__ENOMEM, &err_no_mem);
79         tls_key_init();
80 }
81
82 static void *alloc_err_state(void *old, size_t buf_size)
83 {
84         struct err_state *state;
85         void *p;
86
87         state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
88         if (state) {
89                 state->nstr = buf_size;
90                 if (!tls_set(state)) {
91                         /*
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.
96                          */
97                         free(p);
98                         p = NULL;
99                 }
100         } else if (old) {
101                 /* Failed allocation, but existing state is still good */
102                 p = old;
103         }
104
105         return p;
106 }
107
108 static struct err_state *get_err_state(void)
109 {
110         void *state;
111
112         gl_once_define(static, tls_initialized)
113         if (glthread_once(&tls_initialized, initialize_cb) || !tls_key_valid)
114                 return NULL;
115
116         if (!(state = tls_get()))
117                 return alloc_err_state(state, 100);
118         return state;
119 }
120
121
122 #if ENABLE_NLS
123 /*
124  * Initialize gettext indirectly via get_err_state.
125  */
126 void cdecl__init_i18n(void)
127 {
128         get_err_state();
129 }
130 #endif
131
132 /*
133  * Set the library error to one of the preset messages defined in errmsg.h
134  * (CDECL__Exxx).
135  */
136 void cdecl__errmsg(unsigned msg)
137 {
138         struct err_state *state;
139
140         state = get_err_state();
141         if (!state)
142                 return;
143
144         set_err(msg, &state->err);
145 }
146
147 /*
148  * In the NLS-disabled case, all format strings are of the form
149  *
150  *   "blah blah %s"
151  *
152  * so we exploit this to implement a simple snprintf workalike using the
153  * libcdecl output helpers directly.
154  *
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.
157  */
158 static size_t
159 fmt_err(struct err_state *state, const char *fmt, const char *arg)
160 {
161 #if ENABLE_NLS
162         snprintf(state->str, state->nstr, fmt, arg);
163         return strlen(fmt) + strlen(arg);
164 #else
165         struct output_state dst = { state->str, state->nstr };
166         size_t rc;
167
168         rc = cdecl__strlcpy(dst.dst, fmt, dst.dstlen);
169         cdecl__advance(&dst, rc-2);
170         cdecl__emit(&dst, arg);
171
172         return dst.accum;
173 #endif
174 }
175
176 /*
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.
179  */
180 void cdecl__err(const char *fmt, const char *arg)
181 {
182         struct err_state *state;
183         unsigned try = 0;
184         size_t rc;
185
186         state = get_err_state();
187         if (!state)
188                 return;
189
190 retry:
191         rc = fmt_err(state, fmt, arg);
192         if (rc >= state->nstr) {
193                 assert(try++ == 0 && rc < (size_t)-1 / 4);
194
195                 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
196                 if (!state)
197                         return;
198
199                 goto retry;
200         }
201
202         state->err.code = CDECL_ENOPARSE;
203         state->err.str = state->str;
204 }
205
206 const struct cdecl_error *cdecl_get_error(void)
207 {
208         struct err_state *state = get_err_state();
209
210         return state ? &state->err : &err_no_mem;
211 }