]> git.draconx.ca Git - cdecl99.git/blob - src/error.c
8b9f04e55cd4f0de103283d6f707362366ca4483
[cdecl99.git] / src / error.c
1 /*
2  *  Error handling for libcdecl.
3  *  Copyright © 2011-2012, 2021, 2023 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 <string.h>
23 #include <assert.h>
24 #include <stdarg.h>
25
26 #include "cdecl.h"
27 #include "cdecl-internal.h"
28
29 #include <glthread/lock.h>
30 #include <glthread/tls.h>
31
32 gl_once_define(static, tls_initialized);
33 static gl_tls_key_t tls_key;
34
35 struct err_state {
36         struct cdecl_error err;
37         size_t nstr;
38         char str[FLEXIBLE_ARRAY_MEMBER];
39 };
40
41 /* This pre-initialized error is reserved for dire out-of-memory conditions. */
42 static struct cdecl_error err_no_mem;
43
44 static void free_err(void *err)
45 {
46         if (err == &err_no_mem)
47                 return;
48
49         free(err);
50 }
51
52 static void initialize(void)
53 {
54         cdecl__init_i18n();
55         err_no_mem.str = _("failed to allocate memory");
56         gl_tls_key_init(tls_key, free_err);
57 }
58
59 static void *alloc_err_state(void *old, size_t buf_size)
60 {
61         struct err_state *state;
62         void *p;
63
64         state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
65         if (state) {
66                 state->nstr = buf_size;
67         } else if (old) {
68                 /* Failed allocation, but existing state is still good */
69                 p = old;
70         } else {
71                 /* Failed allocation, no existing state */
72                 p = &err_no_mem;
73         }
74
75         gl_tls_set(tls_key, p);
76         return state;
77 }
78
79 static struct err_state *get_err_state(void)
80 {
81         void *state;
82
83         gl_once(tls_initialized, initialize);
84
85         state = gl_tls_get(tls_key);
86         if (state == &err_no_mem)
87                 state = NULL;
88         if (!state)
89                 return alloc_err_state(state, 100);
90         return state;
91 }
92
93 /*
94  * cdecl__err(CDECL_ENOMEM);
95  * cdecl__err(code, fmt, ...);
96  *
97  * Sets the library error to code, with a printf-style error string.
98  */
99 void cdecl__err(unsigned code, ...)
100 {
101         const char *fmt;
102         struct err_state *state;
103         int rc, try = 0;
104         va_list ap;
105
106         state = get_err_state();
107         if (!state)
108                 return;
109
110         if (code == CDECL_ENOMEM) {
111                 state->err.code = code;
112                 state->err.str = err_no_mem.str;
113                 return;
114         }
115 retry:
116         va_start(ap, code);
117         fmt = va_arg(ap, const char *);
118         rc = vsnprintf(state->str, state->nstr, fmt, ap);
119         va_end(ap);
120
121         if (rc > 0 && rc >= state->nstr) {
122                 assert(try++ == 0 && rc < SIZE_MAX / 4);
123                 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
124                 if (!state)
125                         return;
126
127                 goto retry;
128         }
129
130         state->err.str = state->str;
131         state->err.code = code;
132 }
133
134 const struct cdecl_error *cdecl_get_error(void)
135 {
136         struct err_state *state = get_err_state();
137
138         return state ? &state->err : NULL;
139 }