]> git.draconx.ca Git - cdecl99.git/blob - src/error.c
libcdecl: Remove error code argument from cdecl__err.
[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
24 #include "cdecl.h"
25 #include "cdecl-internal.h"
26
27 #include <glthread/lock.h>
28 #include <glthread/tls.h>
29
30 #include "errmsg.h"
31
32 struct err_state {
33         struct cdecl_error err;
34         size_t nstr;
35         char str[FLEXIBLE_ARRAY_MEMBER];
36 };
37
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;
41
42 static void free_err(void *err)
43 {
44         if (err == &err_no_mem)
45                 return;
46
47         free(err);
48 }
49
50 static void set_err(unsigned code, struct cdecl_error *err)
51 {
52         static const char errmsgs[] = STRTAB_INITIALIZER;
53
54         switch (code) {
55         case CDECL__ENOMEM:
56                 err->code = CDECL_ENOMEM;
57                 break;
58         default:
59                 err->code = CDECL_ENOPARSE;
60                 break;
61         }
62
63         err->str = _(&errmsgs[code]);
64 }
65
66 static void initialize_cb(void)
67 {
68 #if ENABLE_NLS
69         bindtextdomain(PACKAGE, LOCALEDIR);
70         bindtextdomain("bison-runtime", BISON_LOCALEDIR);
71 #endif
72         set_err(CDECL__ENOMEM, &err_no_mem);
73         gl_tls_key_init(tls_key, free_err);
74 }
75
76 static void *alloc_err_state(void *old, size_t buf_size)
77 {
78         struct err_state *state;
79         void *p;
80
81         state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
82         if (state) {
83                 state->nstr = buf_size;
84         } else if (old) {
85                 /* Failed allocation, but existing state is still good */
86                 p = old;
87         } else {
88                 /* Failed allocation, no existing state */
89                 p = &err_no_mem;
90         }
91
92         gl_tls_set(tls_key, p);
93         return state;
94 }
95
96 static struct err_state *get_err_state(void)
97 {
98         void *state;
99
100         gl_once_define(static, tls_initialized)
101         gl_once(tls_initialized, initialize_cb);
102
103         state = gl_tls_get(tls_key);
104         if (state == &err_no_mem)
105                 state = NULL;
106         if (!state)
107                 return alloc_err_state(state, 100);
108         return state;
109 }
110
111
112 #if ENABLE_NLS
113 /*
114  * Initialize gettext indirectly via get_err_state.
115  */
116 void cdecl__init_i18n(void)
117 {
118         get_err_state();
119 }
120 #endif
121
122 /*
123  * Set the library error to one of the preset messages defined in errmsg.h
124  * (CDECL__Exxx).
125  */
126 void cdecl__errmsg(unsigned msg)
127 {
128         struct err_state *state;
129
130         state = get_err_state();
131         if (!state)
132                 return;
133
134         set_err(msg, &state->err);
135 }
136
137 /*
138  * In the NLS-disabled case, all format strings are of the form
139  *
140  *   "blah blah %s"
141  *
142  * so we exploit this to implement a simple snprintf workalike using the
143  * libcdecl output helpers directly.
144  *
145  * In the NLS-enabled case, we have to use snprintf as format strings may
146  * be translated.  GNU libintl ensures a suitable version is available.
147  */
148 static size_t
149 fmt_err(struct err_state *state, const char *fmt, const char *arg)
150 {
151 #if ENABLE_NLS
152         snprintf(state->str, state->nstr, fmt, arg);
153         return strlen(fmt) + strlen(arg);
154 #else
155         struct output_state dst = { state->str, state->nstr };
156         size_t rc;
157
158         rc = cdecl__strlcpy(dst.dst, fmt, dst.dstlen);
159         cdecl__advance(&dst, rc-2);
160         cdecl__emit(&dst, arg);
161
162         return dst.accum;
163 #endif
164 }
165
166 /*
167  * Sets the library error to code; fmt is a printf-style string that may use
168  * up to one %s directive, to refer to arg.
169  */
170 void cdecl__err(const char *fmt, const char *arg)
171 {
172         struct err_state *state;
173         unsigned try = 0;
174         size_t rc;
175
176         state = get_err_state();
177         if (!state)
178                 return;
179
180 retry:
181         rc = fmt_err(state, fmt, arg);
182         if (rc >= state->nstr) {
183                 assert(try++ == 0 && rc < SIZE_MAX / 4);
184
185                 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
186                 if (!state)
187                         return;
188
189                 goto retry;
190         }
191
192         state->err.code = CDECL_ENOPARSE;
193         state->err.str = state->str;
194 }
195
196 const struct cdecl_error *cdecl_get_error(void)
197 {
198         struct err_state *state = get_err_state();
199
200         return state ? &state->err : NULL;
201 }