forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset.c
477 lines (405 loc) · 13.1 KB
/
subset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
* @file
* Subset of config items
*
* @authors
* Copyright (C) 2019 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page config_subset Subset of config items
*
* Subset of config items
*/
#include "config.h"
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "mutt/lib.h"
#include "subset.h"
#include "set.h"
struct Notify;
/**
* elem_list_sort - Sort two HashElem pointers to config
* @param a First HashElem
* @param b Second HashElem
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
int elem_list_sort(const void *a, const void *b)
{
if (!a || !b)
return 0;
const struct HashElem *hea = *(struct HashElem const *const *) a;
const struct HashElem *heb = *(struct HashElem const *const *) b;
return mutt_istr_cmp(hea->key.strkey, heb->key.strkey);
}
/**
* get_elem_list - Create a sorted list of all config items
* @param cs ConfigSet to read
* @retval ptr Null-terminated array of HashElem
*/
struct HashElem **get_elem_list(struct ConfigSet *cs)
{
if (!cs)
return NULL;
struct HashElem **list = mutt_mem_calloc(1024, sizeof(struct HashElem *));
size_t index = 0;
struct HashWalkState walk = { 0 };
struct HashElem *he = NULL;
while ((he = mutt_hash_walk(cs->hash, &walk)))
{
list[index++] = he;
if (index == 1022)
break; /* LCOV_EXCL_LINE */
}
qsort(list, index, sizeof(struct HashElem *), elem_list_sort);
return list;
}
/**
* cs_subset_free - Free a Config Subset
* @param ptr Subset to free
*
* @note Config items matching this Subset will be freed
*/
void cs_subset_free(struct ConfigSubset **ptr)
{
if (!ptr || !*ptr)
return;
struct ConfigSubset *sub = *ptr;
if (sub->cs && sub->name)
{
char scope[256];
snprintf(scope, sizeof(scope), "%s:", sub->name);
// We don't know if any config items have been set,
// so search for anything with a matching scope.
struct HashElem **list = get_elem_list(sub->cs);
for (size_t i = 0; list[i]; i++)
{
const char *item = list[i]->key.strkey;
if (mutt_str_startswith(item, scope) != 0)
{
cs_uninherit_variable(sub->cs, item);
}
}
FREE(&list);
}
notify_free(&sub->notify);
FREE(&sub->name);
FREE(ptr);
}
/**
* cs_subset_new - Create a new Config Subset
* @param name Name for this Subset
* @param sub_parent Parent Subset
* @param not_parent Parent Notification
* @retval ptr New Subset
*
* @note The name will be combined with the parents' names
*/
struct ConfigSubset *cs_subset_new(const char *name, struct ConfigSubset *sub_parent,
struct Notify *not_parent)
{
struct ConfigSubset *sub = mutt_mem_calloc(1, sizeof(*sub));
if (sub_parent)
{
sub->parent = sub_parent;
sub->cs = sub_parent->cs;
}
if (name)
{
char scope[256];
if (sub_parent && sub_parent->name)
snprintf(scope, sizeof(scope), "%s:%s", sub_parent->name, name);
else
mutt_str_copy(scope, name, sizeof(scope));
sub->name = mutt_str_dup(scope);
}
sub->notify = notify_new();
notify_set_parent(sub->notify, not_parent);
return sub;
}
/**
* cs_subset_lookup - Find an inherited config item
* @param sub Subset to search
* @param name Name of Config item to find
* @retval ptr HashElem of the config item
*/
struct HashElem *cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
{
if (!sub || !name)
return NULL;
char scope[256];
if (sub->name)
snprintf(scope, sizeof(scope), "%s:%s", sub->name, name);
else
mutt_str_copy(scope, name, sizeof(scope));
return cs_get_elem(sub->cs, scope);
}
/**
* cs_subset_create_inheritance - Create a Subset config item (inherited)
* @param sub Config Subset
* @param name Name of config item
* @retval ptr HashElem of the config item
* @retval NULL Error
*/
struct HashElem *cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
{
if (!sub)
return NULL;
struct HashElem *he = cs_subset_lookup(sub, name);
if (he)
return he;
if (sub->parent)
{
// Create parent before creating name
he = cs_subset_create_inheritance(sub->parent, name);
}
if (!he)
return NULL;
char scope[256];
snprintf(scope, sizeof(scope), "%s:%s", sub->name, name);
return cs_inherit_variable(sub->cs, he, scope);
}
/**
* cs_subset_notify_observers - Notify all observers of an event
* @param sub Config Subset
* @param he HashElem representing config item
* @param ev Type of event
*/
void cs_subset_notify_observers(const struct ConfigSubset *sub,
struct HashElem *he, enum NotifyConfig ev)
{
if (!sub || !he)
return;
struct HashElem *he_base = cs_get_base(he);
struct EventConfig ec = { sub, he_base->key.strkey, he };
notify_send(sub->notify, NT_CONFIG, ev, &ec);
}
/**
* cs_subset_he_native_get - Natively get the value of a HashElem config item
* @param sub Config Subset
* @param he HashElem representing config item
* @param err Buffer for error messages
* @retval intptr_t Native pointer/value
* @retval INT_MIN Error
*/
intptr_t cs_subset_he_native_get(const struct ConfigSubset *sub,
struct HashElem *he, struct Buffer *err)
{
if (!sub)
return INT_MIN;
return cs_he_native_get(sub->cs, he, err);
}
/**
* cs_subset_str_native_get - Natively get the value of a string config item
* @param sub Config Subset
* @param name Name of config item
* @param err Buffer for error messages
* @retval intptr_t Native pointer/value
* @retval INT_MIN Error
*/
intptr_t cs_subset_str_native_get(const struct ConfigSubset *sub,
const char *name, struct Buffer *err)
{
struct HashElem *he = cs_subset_create_inheritance(sub, name);
return cs_subset_he_native_get(sub, he, err);
}
/**
* cs_subset_he_native_set - Natively set the value of a HashElem config item
* @param sub Config Subset
* @param he HashElem representing config item
* @param value Native pointer/value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_he_native_set(const struct ConfigSubset *sub, struct HashElem *he,
intptr_t value, struct Buffer *err)
{
if (!sub)
return CSR_ERR_CODE;
int rc = cs_he_native_set(sub->cs, he, value, err);
if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
cs_subset_notify_observers(sub, he, NT_CONFIG_SET);
return rc;
}
/**
* cs_subset_str_native_set - Natively set the value of a string config item
* @param sub Config Subset
* @param name Name of config item
* @param value Native pointer/value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name,
intptr_t value, struct Buffer *err)
{
struct HashElem *he = cs_subset_create_inheritance(sub, name);
return cs_subset_he_native_set(sub, he, value, err);
}
/**
* cs_subset_he_reset - Reset a config item to its initial value
* @param sub Config Subset
* @param he HashElem representing config item
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_he_reset(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
{
if (!sub)
return CSR_ERR_CODE;
int rc = cs_he_reset(sub->cs, he, err);
if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
cs_subset_notify_observers(sub, he, NT_CONFIG_RESET);
return rc;
}
/**
* cs_subset_str_reset - Reset a config item to its initial value
* @param sub Config Subset
* @param name Name of config item
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_str_reset(const struct ConfigSubset *sub, const char *name, struct Buffer *err)
{
struct HashElem *he = cs_subset_create_inheritance(sub, name);
return cs_subset_he_reset(sub, he, err);
}
/**
* cs_subset_he_string_get - Get a config item as a string
* @param sub Config Subset
* @param he HashElem representing config item
* @param result Buffer for results or error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_he_string_get(const struct ConfigSubset *sub, struct HashElem *he,
struct Buffer *result)
{
if (!sub)
return CSR_ERR_CODE;
return cs_he_string_get(sub->cs, he, result);
}
/**
* cs_subset_str_string_get - Get a config item as a string
* @param sub Config Subset
* @param name Name of config item
* @param result Buffer for results or error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_str_string_get(const struct ConfigSubset *sub, const char *name,
struct Buffer *result)
{
struct HashElem *he = cs_subset_create_inheritance(sub, name);
return cs_subset_he_string_get(sub, he, result);
}
/**
* cs_subset_he_string_set - Set a config item by string
* @param sub Config Subset
* @param he HashElem representing config item
* @param value Value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_he_string_set(const struct ConfigSubset *sub, struct HashElem *he,
const char *value, struct Buffer *err)
{
if (!sub)
return CSR_ERR_CODE;
int rc = cs_he_string_set(sub->cs, he, value, err);
if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
cs_subset_notify_observers(sub, he, NT_CONFIG_SET);
return rc;
}
/**
* cs_subset_str_string_set - Set a config item by string
* @param sub Config Subset
* @param name Name of config item
* @param value Value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_str_string_set(const struct ConfigSubset *sub, const char *name,
const char *value, struct Buffer *err)
{
struct HashElem *he = cs_subset_create_inheritance(sub, name);
return cs_subset_he_string_set(sub, he, value, err);
}
/**
* cs_subset_he_string_plus_equals - Add to a config item by string
* @param sub Config Subset
* @param he HashElem representing config item
* @param value Value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_he_string_plus_equals(const struct ConfigSubset *sub, struct HashElem *he,
const char *value, struct Buffer *err)
{
if (!sub)
return CSR_ERR_CODE;
int rc = cs_he_string_plus_equals(sub->cs, he, value, err);
if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
cs_subset_notify_observers(sub, he, NT_CONFIG_SET);
return rc;
}
/**
* cs_subset_str_string_plus_equals - Add to a config item by string
* @param sub Config Subset
* @param name Name of config item
* @param value Value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_str_string_plus_equals(const struct ConfigSubset *sub, const char *name,
const char *value, struct Buffer *err)
{
struct HashElem *he = cs_subset_create_inheritance(sub, name);
return cs_subset_he_string_plus_equals(sub, he, value, err);
}
/**
* cs_subset_he_string_minus_equals - Remove from a config item by string
* @param sub Config Subset
* @param he HashElem representing config item
* @param value Value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_he_string_minus_equals(const struct ConfigSubset *sub, struct HashElem *he,
const char *value, struct Buffer *err)
{
if (!sub)
return CSR_ERR_CODE;
int rc = cs_he_string_minus_equals(sub->cs, he, value, err);
if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
cs_subset_notify_observers(sub, he, NT_CONFIG_SET);
return rc;
}
/**
* cs_subset_str_string_minus_equals - Remove from a config item by string
* @param sub Config Subset
* @param name Name of config item
* @param value Value to set
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_subset_str_string_minus_equals(const struct ConfigSubset *sub, const char *name,
const char *value, struct Buffer *err)
{
struct HashElem *he = cs_subset_create_inheritance(sub, name);
return cs_subset_he_string_minus_equals(sub, he, value, err);
}