NeoMutt  2024-04-25-85-g27bab4
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
config_type.c
Go to the documentation of this file.
1
36#include "config.h"
37#include <limits.h>
38#include <stddef.h>
39#include <stdint.h>
40#include <stdio.h>
41#include "mutt/lib.h"
42#include "config/lib.h"
43#include "expando.h"
44
48static void expando_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
49{
50 struct Expando **a = var;
51 if (!*a)
52 return;
53
54 expando_free(a);
55}
56
60static int expando_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
61 const char *value, struct Buffer *err)
62{
63 /* Store empty string list as NULL */
64 if (value && (value[0] == '\0'))
65 value = NULL;
66
67 if (!value && (cdef->type & D_NOT_EMPTY))
68 {
69 buf_printf(err, _("Option %s may not be empty"), cdef->name);
71 }
72
73 int rc = CSR_SUCCESS;
74
75 if (var)
76 {
77 const struct ExpandoDefinition *defs = (const struct ExpandoDefinition *)
78 cdef->data;
79 struct Expando *exp = expando_parse(value, defs, err);
80 if (!exp && !buf_is_empty(err))
81 {
82 char opt[128] = { 0 };
83 // L10N: e.g. "Option index_format:" plus an error message
84 snprintf(opt, sizeof(opt), _("Option %s: "), cdef->name);
85 buf_insert(err, 0, opt);
86 return CSR_ERR_INVALID;
87 }
88
89 if (expando_equal(exp, *(struct Expando **) var))
90 {
91 expando_free(&exp);
93 }
94
95 if (startup_only(cdef, err))
96 {
97 expando_free(&exp);
99 }
100
101 if (cdef->validator)
102 {
103 rc = cdef->validator(cs, cdef, (intptr_t) exp, err);
104
105 if (CSR_RESULT(rc) != CSR_SUCCESS)
106 {
107 expando_free(&exp);
108 return rc | CSR_INV_VALIDATOR;
109 }
110 }
111
112 expando_destroy(cs, var, cdef);
113
114 *(struct Expando **) var = exp;
115
116 if (!exp)
117 rc |= CSR_SUC_EMPTY;
118 }
119 else
120 {
121 if (cdef->type & D_INTERNAL_INITIAL_SET)
122 FREE(&cdef->initial);
123
125 cdef->initial = (intptr_t) mutt_str_dup(value);
126 }
127
128 return rc;
129}
130
134static int expando_string_get(const struct ConfigSet *cs, void *var,
135 const struct ConfigDef *cdef, struct Buffer *result)
136{
137 const char *str = NULL;
138
139 if (var)
140 {
141 struct Expando *exp = *(struct Expando **) var;
142 if (exp)
143 str = exp->string;
144 }
145 else
146 {
147 str = (char *) cdef->initial;
148 }
149
150 if (!str)
151 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty string */
152
153 buf_addstr(result, str);
154 return CSR_SUCCESS;
155}
156
160static int expando_native_set(const struct ConfigSet *cs, void *var,
161 const struct ConfigDef *cdef, intptr_t value,
162 struct Buffer *err)
163{
164 int rc;
165
166 struct Expando *exp_value = (struct Expando *) value;
167 if (!exp_value && (cdef->type & D_NOT_EMPTY))
168 {
169 buf_printf(err, _("Option %s may not be empty"), cdef->name);
171 }
172
173 struct Expando *exp_old = *(struct Expando **) var;
174 if (expando_equal(exp_value, exp_old))
176
177 if (startup_only(cdef, err))
179
180 if (cdef->validator)
181 {
182 rc = cdef->validator(cs, cdef, value, err);
183
184 if (CSR_RESULT(rc) != CSR_SUCCESS)
185 return rc | CSR_INV_VALIDATOR;
186 }
187
188 expando_free(var);
189
190 struct Expando *exp_copy = NULL;
191
192 if (exp_value)
193 {
194 const struct ExpandoDefinition *defs = (const struct ExpandoDefinition *)
195 cdef->data;
196 exp_copy = expando_parse(exp_value->string, defs, err);
197 }
198
199 rc = CSR_SUCCESS;
200 if (!exp_copy)
201 rc |= CSR_SUC_EMPTY;
202
203 *(struct Expando **) var = exp_copy;
204 return rc;
205}
206
210static intptr_t expando_native_get(const struct ConfigSet *cs, void *var,
211 const struct ConfigDef *cdef, struct Buffer *err)
212{
213 if (!cs || !var || !cdef)
214 return INT_MIN; /* LCOV_EXCL_LINE */
215
216 struct Expando *exp = *(struct Expando **) var;
217
218 return (intptr_t) exp;
219}
220
224static int expando_string_plus_equals(const struct ConfigSet *cs, void *var,
225 const struct ConfigDef *cdef,
226 const char *value, struct Buffer *err)
227{
228 /* Skip if the value is missing or empty string*/
229 if (!value || (value[0] == '\0'))
231
232 if (value && startup_only(cdef, err))
234
235 int rc = CSR_SUCCESS;
236
237 char *str = NULL;
238 struct Expando *exp_old = *(struct Expando **) var;
239
240 if (exp_old && exp_old->string)
241 mutt_str_asprintf(&str, "%s%s", exp_old->string, value);
242 else
243 str = mutt_str_dup(value);
244
245 const struct ExpandoDefinition *defs = (const struct ExpandoDefinition *) cdef->data;
246 struct Expando *exp_new = expando_parse(str, defs, err);
247 FREE(&str);
248
249 if (!exp_new && !buf_is_empty(err))
250 {
251 char opt[128] = { 0 };
252 // L10N: e.g. "Option index_format:" plus an error message
253 snprintf(opt, sizeof(opt), _("Option %s: "), cdef->name);
254 buf_insert(err, 0, opt);
255 return CSR_ERR_INVALID;
256 }
257
258 if (expando_equal(exp_new, exp_old))
259 {
260 expando_free(&exp_new); // LCOV_EXCL_LINE
261 return CSR_SUCCESS | CSR_SUC_NO_CHANGE; // LCOV_EXCL_LINE
262 }
263
264 if (cdef->validator)
265 {
266 rc = cdef->validator(cs, cdef, (intptr_t) exp_new, err);
267
268 if (CSR_RESULT(rc) != CSR_SUCCESS)
269 {
270 expando_free(&exp_new);
271 return rc | CSR_INV_VALIDATOR;
272 }
273 }
274
275 expando_destroy(cs, var, cdef);
276 *(struct Expando **) var = exp_new;
277
278 return rc;
279}
280
284static int expando_reset(const struct ConfigSet *cs, void *var,
285 const struct ConfigDef *cdef, struct Buffer *err)
286{
287 if (!cs || !var || !cdef)
288 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
289
290 struct Expando *exp = NULL;
291 const char *initial = (const char *) cdef->initial;
292
293 if (initial)
294 {
295 const struct ExpandoDefinition *defs = (const struct ExpandoDefinition *)
296 cdef->data;
297 exp = expando_parse(initial, defs, err);
298 }
299
300 if (expando_equal(exp, *(struct Expando **) var))
301 {
302 expando_free(&exp);
304 }
305
306 if (startup_only(cdef, err))
307 {
308 expando_free(&exp);
310 }
311
312 int rc = CSR_SUCCESS;
313
314 if (cdef->validator)
315 {
316 rc = cdef->validator(cs, cdef, (intptr_t) exp, err);
317
318 if (CSR_RESULT(rc) != CSR_SUCCESS)
319 {
320 expando_destroy(cs, &exp, cdef);
321 return rc | CSR_INV_VALIDATOR;
322 }
323 }
324
325 if (!exp)
326 rc |= CSR_SUC_EMPTY;
327
328 expando_destroy(cs, var, cdef);
329
330 *(struct Expando **) var = exp;
331 return rc;
332}
333
337const struct ConfigSetType CstExpando = {
339 "expando",
345 NULL, // string_minus_equals
348};
349
357const struct Expando *cs_subset_expando(const struct ConfigSubset *sub, const char *name)
358{
359 ASSERT(sub && name);
360
361 struct HashElem *he = cs_subset_create_inheritance(sub, name);
362 ASSERT(he);
363
364#ifndef NDEBUG
365 struct HashElem *he_base = cs_get_base(he);
366 ASSERT(DTYPE(he_base->type) == DT_EXPANDO);
367#endif
368
369 intptr_t value = cs_subset_he_native_get(sub, he, NULL);
370 ASSERT(value != INT_MIN);
371
372 return (const struct Expando *) value;
373}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
size_t buf_insert(struct Buffer *buf, size_t offset, const char *s)
Add a string in the middle of a buffer.
Definition: buffer.c:256
Convenience wrapper for the config headers.
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:160
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_ERR_CODE
Problem with the code.
Definition: set.h:36
#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
const struct ConfigSetType CstExpando
Config type representing an Expando.
Definition: config_type.c:337
const struct Expando * cs_subset_expando(const struct ConfigSubset *sub, const char *name)
Get an Expando config item by name.
Definition: config_type.c:357
struct Expando * expando_parse(const char *str, const struct ExpandoDefinition *defs, struct Buffer *err)
Parse an Expando string.
Definition: expando.c:74
void expando_free(struct Expando **ptr)
Free an Expando object.
Definition: expando.c:54
bool expando_equal(const struct Expando *a, const struct Expando *b)
Compare two expandos.
Definition: expando.c:128
Parsed Expando.
static void expando_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an Expando object - Implements ConfigSetType::destroy() -.
Definition: config_type.c:48
static int expando_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set an Expando object from an Expando config item - Implements ConfigSetType::native_get() -.
Definition: config_type.c:160
static intptr_t expando_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an Expando object from an Expando config item - Implements ConfigSetType::native_get() -.
Definition: config_type.c:210
static int expando_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an Expando to its initial value - Implements ConfigSetType::reset() -.
Definition: config_type.c:284
static int expando_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get an Expando as a string - Implements ConfigSetType::string_get() -.
Definition: config_type.c:134
static int expando_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to an Expando by string - Implements ConfigSetType::string_plus_equals() -.
Definition: config_type.c:224
static int expando_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set an Expando by string - Implements ConfigSetType::string_set() -.
Definition: config_type.c:60
#define FREE(x)
Definition: memory.h:45
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:803
#define ASSERT(COND)
Definition: signal2.h:58
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t data
Extra variable data.
Definition: set.h:68
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
A set of inherited config items.
Definition: subset.h:47
Definition of a format string.
Definition: definition.h:52
Parsed Expando trees.
Definition: expando.h:41
const char * string
Pointer to the parsed string.
Definition: expando.h:42
The item stored in a Hash Table.
Definition: hash.h:43
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:44
intptr_t cs_subset_he_native_get(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Natively get the value of a HashElem config item.
Definition: subset.c:258
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:208
#define DTYPE(t)
Definition: types.h:50
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:90
@ DT_EXPANDO
an expando
Definition: types.h:34
#define D_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:80