]> git.draconx.ca Git - cdecl99.git/blob - src/error.c
Minor portability improvements.
[cdecl99.git] / src / error.c
1 /*
2  *  Error handling for libcdecl.
3  *  Copyright © 2011-2012, 2021 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 <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23
24 #include <glthread/lock.h>
25 #include <glthread/tls.h>
26
27 #include "cdecl.h"
28 #include "cdecl-internal.h"
29
30 gl_once_define(static, tls_initialized);
31 static gl_tls_key_t tls_key;
32
33 struct err_state {
34         struct cdecl_error err;
35         size_t nstr;
36         char str[FLEXIBLE_ARRAY_MEMBER];
37 };
38
39 /* This error is reserved for extremely dire out-of-memory conditions. */
40 static struct err_state err_no_mem = {
41         .err = {
42                 .code = CDECL_ENOMEM,
43                 .str  = NULL,
44         },
45 };
46
47 const char *cdecl__strerror(unsigned code)
48 {
49 #include "errtab.h"
50
51         switch (code) {
52         case CDECL_ENOMEM: return gettext(strtab+err_enomem);
53         case CDECL_ENOPARSE: return gettext(strtab+err_enoparse);
54         }
55
56         assert(0);
57 }
58
59 static void free_err(void *err)
60 {
61         if (err == &err_no_mem)
62                 return;
63
64         free(err);
65 }
66
67 static void set_error(const struct cdecl_error *err)
68 {
69         struct err_state *state;
70         size_t need_len = 0;
71
72         if (err->str) {
73                 need_len = strlen(err->str) + 1;
74         }
75
76         state = gl_tls_get(tls_key);
77         if (state == &err_no_mem)
78                 state = NULL;
79         if (!state || state->nstr < need_len) {
80                 struct err_state *tmp;
81                 
82                 tmp = realloc(state, sizeof *state + need_len);
83                 if (!tmp) {
84                         /* Re-use the existing state buffer, if any. */
85                         if (state)
86                                 state->err = err_no_mem.err;
87                         else
88                                 state = &err_no_mem;
89                         
90                         gl_tls_set(tls_key, state);
91                         return;
92                 }
93
94                 state = tmp;
95                 state->nstr = need_len;
96                 gl_tls_set(tls_key, state);
97         }
98
99         state->err = *err;
100         if (err->str) {
101                 memcpy(state->str, err->str, need_len);
102                 state->err.str = state->str;
103         } else {
104                 state->err.str = cdecl__strerror(state->err.code);
105         }
106 }
107
108 static void initialize(void)
109 {
110         cdecl__init_i18n();
111         err_no_mem.err.str = cdecl__strerror(CDECL_ENOMEM);
112
113         gl_tls_key_init(tls_key, free_err);
114
115         /*
116          * This default error message is a stop-gap measure until all library
117          * error conditions use the new interface.
118          */
119         set_error(&(const struct cdecl_error){ .code = CDECL_ENOPARSE });
120 }
121
122 const struct cdecl_error *cdecl_get_error(void)
123 {
124         struct err_state *state;
125
126         gl_once(tls_initialized, initialize);
127
128         state = gl_tls_get(tls_key);
129         assert(state);
130
131         return &state->err;
132 }
133
134 void cdecl__set_error(const struct cdecl_error *err)
135 {
136         gl_once(tls_initialized, initialize);
137
138         set_error(err);
139 }