NeoMutt  2024-02-01-23-g345d7b
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
slist.c
Go to the documentation of this file.
1
36#include "config.h"
37#include <stddef.h>
38#include <limits.h>
39#include <stdint.h>
40#include "mutt/lib.h"
41#include "set.h"
42#include "types.h"
43
47static void slist_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
48{
49 if (!cs || !var || !cdef)
50 return; /* LCOV_EXCL_LINE */
51
52 struct Slist **l = (struct Slist **) var;
53 if (!*l)
54 return;
55
56 slist_free(l);
57}
58
62static int slist_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
63 const char *value, struct Buffer *err)
64{
65 if (!cs || !cdef)
66 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
67
68 /* Store empty string list as NULL */
69 if (value && (value[0] == '\0'))
70 value = NULL;
71
72 struct Slist *list = NULL;
73
74 int rc = CSR_SUCCESS;
75
76 if (var)
77 {
78 list = slist_parse(value, cdef->type);
79
80 if (slist_equal(list, *(struct Slist **) var))
81 {
82 slist_free(&list);
84 }
85
86 if (startup_only(cdef, err))
87 {
88 slist_free(&list);
90 }
91
92 if (cdef->validator)
93 {
94 rc = cdef->validator(cs, cdef, (intptr_t) list, err);
95
96 if (CSR_RESULT(rc) != CSR_SUCCESS)
97 {
98 slist_free(&list);
99 return rc | CSR_INV_VALIDATOR;
100 }
101 }
102
103 slist_destroy(cs, var, cdef);
104
105 *(struct Slist **) var = list;
106
107 if (!list)
108 rc |= CSR_SUC_EMPTY;
109 }
110 else
111 {
112 if (cdef->type & D_INTERNAL_INITIAL_SET)
113 FREE(&cdef->initial);
114
116 cdef->initial = (intptr_t) mutt_str_dup(value);
117 }
118
119 return rc;
120}
121
125static int slist_string_get(const struct ConfigSet *cs, void *var,
126 const struct ConfigDef *cdef, struct Buffer *result)
127{
128 if (!cs || !cdef)
129 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
130
131 if (var)
132 {
133 struct Slist *list = *(struct Slist **) var;
134 if (!list)
135 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty string */
136
137 slist_to_buffer(list, result);
138 }
139 else
140 {
141 buf_addstr(result, (char *) cdef->initial);
142 }
143
144 int rc = CSR_SUCCESS;
145 if (buf_is_empty(result))
146 rc |= CSR_SUC_EMPTY;
147
148 return rc;
149}
150
154static int slist_native_set(const struct ConfigSet *cs, void *var,
155 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
156{
157 if (!cs || !var || !cdef)
158 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
159
160 int rc;
161
162 if (slist_equal((struct Slist *) value, *(struct Slist **) var))
164
165 if (startup_only(cdef, err))
167
168 if (cdef->validator)
169 {
170 rc = cdef->validator(cs, cdef, value, err);
171
172 if (CSR_RESULT(rc) != CSR_SUCCESS)
173 return rc | CSR_INV_VALIDATOR;
174 }
175
176 slist_free(var);
177
178 struct Slist *list = slist_dup((struct Slist *) value);
179
180 rc = CSR_SUCCESS;
181 if (!list)
182 rc |= CSR_SUC_EMPTY;
183
184 *(struct Slist **) var = list;
185 return rc;
186}
187
191static intptr_t slist_native_get(const struct ConfigSet *cs, void *var,
192 const struct ConfigDef *cdef, struct Buffer *err)
193{
194 if (!cs || !var || !cdef)
195 return INT_MIN; /* LCOV_EXCL_LINE */
196
197 struct Slist *list = *(struct Slist **) var;
198
199 return (intptr_t) list;
200}
201
205static int slist_string_plus_equals(const struct ConfigSet *cs, void *var,
206 const struct ConfigDef *cdef,
207 const char *value, struct Buffer *err)
208{
209 if (!cs || !cdef)
210 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
211
212 int rc = CSR_SUCCESS;
213
214 /* Store empty strings as NULL */
215 if (value && (value[0] == '\0'))
216 value = NULL;
217
218 if (!value)
219 return rc | CSR_SUC_NO_CHANGE;
220
221 if (startup_only(cdef, err))
223
224 struct Slist *orig = *(struct Slist **) var;
225 if (slist_is_member(orig, value))
226 return rc | CSR_SUC_NO_CHANGE;
227
228 struct Slist *copy = slist_dup(orig);
229 if (!copy)
230 copy = slist_new(cdef->type & D_SLIST_SEP_MASK);
231
232 slist_add_string(copy, value);
233
234 if (cdef->validator)
235 {
236 rc = cdef->validator(cs, cdef, (intptr_t) copy, err);
237 if (CSR_RESULT(rc) != CSR_SUCCESS)
238 {
239 slist_free(&copy);
240 return rc | CSR_INV_VALIDATOR;
241 }
242 }
243
244 slist_free(&orig);
245 *(struct Slist **) var = copy;
246
247 return rc;
248}
249
253static int slist_string_minus_equals(const struct ConfigSet *cs, void *var,
254 const struct ConfigDef *cdef,
255 const char *value, struct Buffer *err)
256{
257 if (!cs || !cdef)
258 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
259
260 int rc = CSR_SUCCESS;
261
262 /* Store empty strings as NULL */
263 if (value && (value[0] == '\0'))
264 value = NULL;
265
266 if (!value)
267 return rc | CSR_SUC_NO_CHANGE;
268
269 if (startup_only(cdef, err))
271
272 struct Slist *orig = *(struct Slist **) var;
273 if (!slist_is_member(orig, value))
274 return rc | CSR_SUC_NO_CHANGE;
275
276 struct Slist *copy = slist_dup(orig);
277 slist_remove_string(copy, value);
278
279 if (cdef->validator)
280 {
281 rc = cdef->validator(cs, cdef, (intptr_t) copy, err);
282 if (CSR_RESULT(rc) != CSR_SUCCESS)
283 {
284 slist_free(&copy);
285 return rc | CSR_INV_VALIDATOR;
286 }
287 }
288
289 slist_free(&orig);
290 *(struct Slist **) var = copy;
291
292 return rc;
293}
294
298static int slist_reset(const struct ConfigSet *cs, void *var,
299 const struct ConfigDef *cdef, struct Buffer *err)
300{
301 if (!cs || !var || !cdef)
302 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
303
304 struct Slist *list = NULL;
305 const char *initial = (const char *) cdef->initial;
306
307 if (initial)
308 list = slist_parse(initial, cdef->type);
309
310 if (slist_equal(list, *(struct Slist **) var))
311 {
312 slist_free(&list);
314 }
315
316 if (startup_only(cdef, err))
317 {
318 slist_free(&list);
320 }
321
322 int rc = CSR_SUCCESS;
323
324 if (cdef->validator)
325 {
326 rc = cdef->validator(cs, cdef, (intptr_t) list, err);
327
328 if (CSR_RESULT(rc) != CSR_SUCCESS)
329 {
330 slist_destroy(cs, &list, cdef);
331 return rc | CSR_INV_VALIDATOR;
332 }
333 }
334
335 if (!list)
336 rc |= CSR_SUC_EMPTY;
337
338 slist_destroy(cs, var, cdef);
339
340 *(struct Slist **) var = list;
341 return rc;
342}
343
347const struct ConfigSetType CstSlist = {
348 DT_SLIST,
349 "slist",
358};
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:308
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:243
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 CstSlist
Config type representing a list of strings.
Definition: slist.c:347
static void slist_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an Slist object - Implements ConfigSetType::destroy() -.
Definition: slist.c:47
static intptr_t slist_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a Slist from a Slist config item - Implements ConfigSetType::native_get() -.
Definition: slist.c:191
static int slist_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Slist config item by Slist - Implements ConfigSetType::native_set() -.
Definition: slist.c:154
static int slist_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Slist to its initial value - Implements ConfigSetType::reset() -.
Definition: slist.c:298
static int slist_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Slist as a string - Implements ConfigSetType::string_get() -.
Definition: slist.c:125
static int slist_string_minus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Remove from a Slist by string - Implements ConfigSetType::string_minus_equals() -.
Definition: slist.c:253
static int slist_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to a Slist by string - Implements ConfigSetType::string_plus_equals() -.
Definition: slist.c:205
static int slist_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Slist by string - Implements ConfigSetType::string_set() -.
Definition: slist.c:62
#define FREE(x)
Definition: memory.h:45
Convenience wrapper for the library headers.
struct Slist * slist_remove_string(struct Slist *list, const char *str)
Remove a string from a list.
Definition: slist.c:237
struct Slist * slist_parse(const char *str, uint32_t flags)
Parse a list of strings into a list.
Definition: slist.c:179
void slist_free(struct Slist **ptr)
Free an Slist object.
Definition: slist.c:126
bool slist_equal(const struct Slist *a, const struct Slist *b)
Compare two string lists.
Definition: slist.c:89
struct Slist * slist_add_string(struct Slist *list, const char *str)
Add a string to a list.
Definition: slist.c:66
bool slist_is_member(const struct Slist *list, const char *str)
Is a string a member of a list?
Definition: slist.c:156
struct Slist * slist_dup(const struct Slist *list)
Create a copy of an Slist object.
Definition: slist.c:106
int slist_to_buffer(const struct Slist *list, struct Buffer *buf)
Export an Slist to a Buffer.
Definition: slist.c:271
struct Slist * slist_new(uint32_t flags)
Create a new string list.
Definition: slist.c:51
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
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
String list.
Definition: slist.h:37
Constants for all the config types.
#define D_SLIST_SEP_MASK
Definition: types.h:110
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:87
@ DT_SLIST
a list of strings
Definition: types.h:42