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