NeoMutt  2024-10-02-7-gd3e66a
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mbtable.c
Go to the documentation of this file.
1
36#include "config.h"
37#include <stdint.h>
38#include <string.h>
39#include <wchar.h>
40#include "mutt/lib.h"
41#include "mbtable.h"
42#include "set.h"
43#include "types.h"
44
51bool mbtable_equal(const struct MbTable *a, const struct MbTable *b)
52{
53 if (!a && !b) /* both empty */
54 return true;
55 if (!a ^ !b) /* one is empty, but not the other */
56 return false;
57
58 return mutt_str_equal(a->orig_str, b->orig_str);
59}
60
66struct MbTable *mbtable_parse(const char *s)
67{
68 struct MbTable *t = NULL;
69 size_t slen, k;
70 mbstate_t mbstate = { 0 };
71 char *d = NULL;
72
73 slen = mutt_str_len(s);
74 if (!slen)
75 return NULL;
76
77 t = mutt_mem_calloc(1, sizeof(struct MbTable));
78
79 t->orig_str = mutt_str_dup(s);
80 /* This could be more space efficient. However, being used on tiny
81 * strings (`$to_chars` and `$status_chars`), the overhead is not great. */
82 t->chars = mutt_mem_calloc(slen, sizeof(char *));
83 t->segmented_str = mutt_mem_calloc(slen * 2, sizeof(char));
84 d = t->segmented_str;
85
86 while (slen && (k = mbrtowc(NULL, s, slen, &mbstate)))
87 {
88 if ((k == ICONV_ILLEGAL_SEQ) || (k == ICONV_BUF_TOO_SMALL))
89 {
90 /* XXX put message in err buffer; fail? warning? */
91 mutt_debug(LL_DEBUG1, "mbrtowc returned %zd converting %s in %s\n", k, s, t->orig_str);
92 if (k == ICONV_ILLEGAL_SEQ)
93 memset(&mbstate, 0, sizeof(mbstate));
94 k = (k == ICONV_ILLEGAL_SEQ) ? 1 : slen;
95 }
96
97 slen -= k;
98 t->chars[t->len++] = d;
99 while (k--)
100 *d++ = *s++;
101 *d++ = '\0';
102 }
103
104 return t;
105}
106
110static void mbtable_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
111{
112 struct MbTable **m = var;
113 if (!*m)
114 return;
115
116 mbtable_free(m);
117}
118
122static int mbtable_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
123 const char *value, struct Buffer *err)
124{
125 /* Store empty mbtables as NULL */
126 if (value && (value[0] == '\0'))
127 value = NULL;
128
129 struct MbTable *table = NULL;
130
131 int rc = CSR_SUCCESS;
132
133 if (var)
134 {
135 struct MbTable *curval = *(struct MbTable **) var;
136 if (curval && mutt_str_equal(value, curval->orig_str))
138
139 if (startup_only(cdef, err))
141
142 table = mbtable_parse(value);
143
144 if (cdef->validator)
145 {
146 rc = cdef->validator(cs, cdef, (intptr_t) table, err);
147
148 if (CSR_RESULT(rc) != CSR_SUCCESS)
149 {
150 mbtable_free(&table);
151 return rc | CSR_INV_VALIDATOR;
152 }
153 }
154
155 mbtable_destroy(cs, var, cdef);
156
157 *(struct MbTable **) var = table;
158
159 if (!table)
160 rc |= CSR_SUC_EMPTY;
161 }
162 else
163 {
164 if (cdef->type & D_INTERNAL_INITIAL_SET)
165 FREE(&cdef->initial);
166
168 cdef->initial = (intptr_t) mutt_str_dup(value);
169 }
170
171 return rc;
172}
173
177static int mbtable_string_get(const struct ConfigSet *cs, void *var,
178 const struct ConfigDef *cdef, struct Buffer *result)
179{
180 const char *str = NULL;
181
182 if (var)
183 {
184 struct MbTable *table = *(struct MbTable **) var;
185 if (!table || !table->orig_str)
186 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty string */
187 str = table->orig_str;
188 }
189 else
190 {
191 str = (char *) cdef->initial;
192 }
193
194 buf_addstr(result, str);
195 return CSR_SUCCESS;
196}
197
203static struct MbTable *mbtable_dup(struct MbTable *table)
204{
205 if (!table)
206 return NULL; /* LCOV_EXCL_LINE */
207
208 struct MbTable *m = mutt_mem_calloc(1, sizeof(*m));
209 m->orig_str = mutt_str_dup(table->orig_str);
210 return m;
211}
212
216static int mbtable_native_set(const struct ConfigSet *cs, void *var,
217 const struct ConfigDef *cdef, intptr_t value,
218 struct Buffer *err)
219{
220 int rc;
221
222 if (mbtable_equal(*(struct MbTable **) var, (struct MbTable *) value))
224
225 if (startup_only(cdef, err))
227
228 if (cdef->validator)
229 {
230 rc = cdef->validator(cs, cdef, value, err);
231
232 if (CSR_RESULT(rc) != CSR_SUCCESS)
233 return rc | CSR_INV_VALIDATOR;
234 }
235
236 mbtable_free(var);
237
238 struct MbTable *table = mbtable_dup((struct MbTable *) value);
239
240 rc = CSR_SUCCESS;
241 if (!table)
242 rc |= CSR_SUC_EMPTY;
243
244 *(struct MbTable **) var = table;
245 return rc;
246}
247
251static intptr_t mbtable_native_get(const struct ConfigSet *cs, void *var,
252 const struct ConfigDef *cdef, struct Buffer *err)
253{
254 struct MbTable *table = *(struct MbTable **) var;
255
256 return (intptr_t) table;
257}
258
262static int mbtable_reset(const struct ConfigSet *cs, void *var,
263 const struct ConfigDef *cdef, struct Buffer *err)
264{
265 struct MbTable *table = NULL;
266 const char *initial = (const char *) cdef->initial;
267
268 struct MbTable *curtable = *(struct MbTable **) var;
269 const char *curval = curtable ? curtable->orig_str : NULL;
270
271 int rc = CSR_SUCCESS;
272 if (!curtable)
273 rc |= CSR_SUC_EMPTY;
274
275 if (mutt_str_equal(initial, curval))
276 return rc | CSR_SUC_NO_CHANGE;
277
278 if (startup_only(cdef, err))
280
281 if (initial)
282 table = mbtable_parse(initial);
283
284 if (cdef->validator)
285 {
286 rc = cdef->validator(cs, cdef, (intptr_t) table, err);
287
288 if (CSR_RESULT(rc) != CSR_SUCCESS)
289 {
290 mbtable_destroy(cs, &table, cdef);
291 return rc | CSR_INV_VALIDATOR;
292 }
293 }
294
295 if (!table)
296 rc |= CSR_SUC_EMPTY;
297
298 mbtable_destroy(cs, var, cdef);
299
300 *(struct MbTable **) var = table;
301 return rc;
302}
303
308void mbtable_free(struct MbTable **ptr)
309{
310 if (!ptr || !*ptr)
311 return;
312
313 struct MbTable *table = *ptr;
314 FREE(&table->orig_str);
315 FREE(&table->chars);
316 FREE(&table->segmented_str);
317
318 FREE(ptr);
319}
320
331const char *mbtable_get_nth_wchar(const struct MbTable *table, int index)
332{
333 if (!table || !table->chars || (index < 0) || (index >= table->len))
334 return " ";
335
336 if (table->chars[index][0] == '\r')
337 return "";
338
339 return table->chars[index];
340}
341
345const struct ConfigSetType CstMbtable = {
347 "mbtable",
352 NULL, // string_plus_equals
353 NULL, // string_minus_equals
356};
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
static bool startup_only(const struct ConfigDef *cdef, struct Buffer *err)
Validator function for D_ON_STARTUP.
Definition: set.h:296
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:44
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
static void mbtable_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an MbTable object - Implements ConfigSetType::destroy() -.
Definition: mbtable.c:110
static intptr_t mbtable_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an MbTable object from a MbTable config item - Implements ConfigSetType::native_get() -.
Definition: mbtable.c:251
static int mbtable_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set an MbTable config item by MbTable object - Implements ConfigSetType::native_set() -.
Definition: mbtable.c:216
static int mbtable_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an MbTable to its initial value - Implements ConfigSetType::reset() -.
Definition: mbtable.c:262
static int mbtable_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a MbTable as a string - Implements ConfigSetType::string_get() -.
Definition: mbtable.c:177
static int mbtable_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set an MbTable by string - Implements ConfigSetType::string_set() -.
Definition: mbtable.c:122
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
bool mbtable_equal(const struct MbTable *a, const struct MbTable *b)
Compare two MbTables.
Definition: mbtable.c:51
static struct MbTable * mbtable_dup(struct MbTable *table)
Create a copy of an MbTable object.
Definition: mbtable.c:203
struct MbTable * mbtable_parse(const char *s)
Parse a multibyte string into a table.
Definition: mbtable.c:66
const char * mbtable_get_nth_wchar(const struct MbTable *table, int index)
Extract one char from a multi-byte table.
Definition: mbtable.c:331
void mbtable_free(struct MbTable **ptr)
Free an MbTable object.
Definition: mbtable.c:308
const struct ConfigSetType CstMbtable
Config type representing a multi-byte table.
Definition: mbtable.c:345
Type representing a multibyte character table.
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
#define FREE(x)
Definition: memory.h:45
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition: charset.h:107
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition: charset.h:105
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
Parse the 'set' command.
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:64
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t initial
Initial value.
Definition: set.h:67
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
Container for lots of config items.
Definition: set.h:252
Multibyte character table.
Definition: mbtable.h:36
char * orig_str
Original string used to generate this object.
Definition: mbtable.h:37
int len
Number of characters.
Definition: mbtable.h:38
char ** chars
The array of multibyte character strings.
Definition: mbtable.h:39
char * segmented_str
Each chars entry points inside this string.
Definition: mbtable.h:40
Constants for all the config types.
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:90
@ DT_MBTABLE
multibyte char table
Definition: types.h:37