]> git.draconx.ca Git - cdecl99.git/blob - src/error.c
cdecl99: Don't check blank lines if no history support.
[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 <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 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 set_err(unsigned code, struct cdecl_error *err)
53 {
54         static const char errmsgs[] = STRTAB_INITIALIZER;
55
56         switch (code) {
57         case CDECL__ENOMEM:
58                 err->code = CDECL_ENOMEM;
59                 break;
60         default:
61                 err->code = CDECL_ENOPARSE;
62                 break;
63         }
64
65         err->str = _(&errmsgs[code]);
66 }
67
68 static void initialize(void)
69 {
70         cdecl__init_i18n();
71         set_err(CDECL__ENOMEM, &err_no_mem);
72         gl_tls_key_init(tls_key, free_err);
73 }
74
75 static void *alloc_err_state(void *old, size_t buf_size)
76 {
77         struct err_state *state;
78         void *p;
79
80         state = p = realloc(old, offsetof(struct err_state, str) + buf_size);
81         if (state) {
82                 state->nstr = buf_size;
83         } else if (old) {
84                 /* Failed allocation, but existing state is still good */
85                 p = old;
86         } else {
87                 /* Failed allocation, no existing state */
88                 p = &err_no_mem;
89         }
90
91         gl_tls_set(tls_key, p);
92         return state;
93 }
94
95 static struct err_state *get_err_state(void)
96 {
97         void *state;
98
99         gl_once(tls_initialized, initialize);
100
101         state = gl_tls_get(tls_key);
102         if (state == &err_no_mem)
103                 state = NULL;
104         if (!state)
105                 return alloc_err_state(state, 100);
106         return state;
107 }
108
109 /*
110  * Set the library error to one of the preset messages defined in errmsg.h
111  * (CDECL__Exxx).
112  */
113 void cdecl__errmsg(unsigned msg)
114 {
115         struct err_state *state;
116
117         state = get_err_state();
118         if (!state)
119                 return;
120
121         set_err(msg, &state->err);
122 }
123
124 /*
125  * Sets the library error to code; fmt is a printf-style string that may use
126  * up to one %s directive, to refer to arg.
127  */
128 void cdecl__err(unsigned code, const char *fmt, const char *arg)
129 {
130         struct err_state *state;
131         int rc, try = 0;
132
133         state = get_err_state();
134         if (!state)
135                 return;
136
137 retry:
138         rc = snprintf(state->str, state->nstr, fmt, arg);
139         if (rc > 0 && rc >= state->nstr) {
140                 assert(try++ == 0 && rc < SIZE_MAX / 4);
141                 state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
142                 if (!state)
143                         return;
144
145                 goto retry;
146         }
147
148         state->err.str = state->str;
149         state->err.code = code;
150 }
151
152 const struct cdecl_error *cdecl_get_error(void)
153 {
154         struct err_state *state = get_err_state();
155
156         return state ? &state->err : NULL;
157 }