]> git.draconx.ca Git - cdecl99.git/blob - src/error.c
libcdecl: Accumulate output length in structure.
[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 #include "errmsg.h"
33
34 gl_once_define(static, tls_initialized)
35 static gl_tls_key_t tls_key;
36
37 struct err_state {
38         struct cdecl_error err;
39         size_t nstr;
40         char str[FLEXIBLE_ARRAY_MEMBER];
41 };
42
43 /* This pre-initialized error is reserved for dire out-of-memory conditions. */
44 static struct cdecl_error err_no_mem;
45
46 static void free_err(void *err)
47 {
48         if (err == &err_no_mem)
49                 return;
50
51         free(err);
52 }
53
54 static void set_err(unsigned code, struct cdecl_error *err)
55 {
56         static const char errmsgs[] = STRTAB_INITIALIZER;
57
58         switch (code) {
59         case CDECL__ENOMEM:
60                 err->code = CDECL_ENOMEM;
61                 break;
62         default:
63                 err->code = CDECL_ENOPARSE;
64                 break;
65         }
66
67         err->str = _(&errmsgs[code]);
68 }
69
70 static void initialize(void)
71 {
72         cdecl__init_i18n();
73         set_err(CDECL__ENOMEM, &err_no_mem);
74         gl_tls_key_init(tls_key, free_err);
75 }
76
77 static void *alloc_err_state(void *old, size_t buf_size)
78 {
79         struct err_state *state;
80         void *p;
81
82         state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
83         if (state) {
84                 state->nstr = buf_size;
85         } else if (old) {
86                 /* Failed allocation, but existing state is still good */
87                 p = old;
88         } else {
89                 /* Failed allocation, no existing state */
90                 p = &err_no_mem;
91         }
92
93         gl_tls_set(tls_key, p);
94         return state;
95 }
96
97 static struct err_state *get_err_state(void)
98 {
99         void *state;
100
101         gl_once(tls_initialized, initialize);
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  * Set the library error to one of the preset messages defined in errmsg.h
113  * (CDECL__Exxx).
114  */
115 void cdecl__errmsg(unsigned msg)
116 {
117         struct err_state *state;
118
119         state = get_err_state();
120         if (!state)
121                 return;
122
123         set_err(msg, &state->err);
124 }
125
126 /*
127  * Sets the library error to code, with a printf-style error string.
128  */
129 void cdecl__err(unsigned code, const char *fmt, ...)
130 {
131         struct err_state *state;
132         int rc, try = 0;
133         va_list ap;
134
135         state = get_err_state();
136         if (!state)
137                 return;
138
139 retry:
140         va_start(ap, fmt);
141         rc = vsnprintf(state->str, state->nstr, fmt, ap);
142         va_end(ap);
143
144         if (rc > 0 && rc >= state->nstr) {
145                 assert(try++ == 0 && rc < SIZE_MAX / 4);
146                 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
147                 if (!state)
148                         return;
149
150                 goto retry;
151         }
152
153         state->err.str = state->str;
154         state->err.code = code;
155 }
156
157 const struct cdecl_error *cdecl_get_error(void)
158 {
159         struct err_state *state = get_err_state();
160
161         return state ? &state->err : NULL;
162 }