forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.c
1014 lines (858 loc) · 25.2 KB
/
set.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* A collection of config items
*
* @authors
* Copyright (C) 2017-2018 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_set Config Set
*
* A collection of config items.
*/
#include "config.h"
#include <limits.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "set.h"
#include "inheritance.h"
#include "types.h"
struct ConfigSetType RegisteredTypes[18] = {
{ 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
};
/**
* cs_hashelem_free - Callback function for the Hash Table - Implements ::hash_hdata_free_t - @ingroup hash_hdata_free_api
* @param type Object type, e.g. #DT_STRING
* @param obj Object to destroy
* @param data ConfigSet associated with the object
*/
static void cs_hashelem_free(int type, void *obj, intptr_t data)
{
if (data == 0)
return; /* LCOV_EXCL_LINE */
struct ConfigSet *cs = (struct ConfigSet *) data;
const struct ConfigSetType *cst = NULL;
if (type & DT_INHERITED)
{
struct Inheritance *i = obj;
struct HashElem *he_base = cs_get_base(i->parent);
struct ConfigDef *cdef = he_base->data;
if (!cdef)
return; // LCOV_EXCL_LINE
cst = cs_get_type_def(cs, he_base->type);
if (cst && cst->destroy)
cst->destroy(cs, (void **) &i->var, cdef);
FREE(&i->name);
FREE(&i);
}
else
{
struct ConfigDef *cdef = obj;
cst = cs_get_type_def(cs, type);
if (cst && cst->destroy)
cst->destroy(cs, &cdef->var, cdef);
/* If we allocated the initial value, clean it up */
if (cdef->type & DT_INITIAL_SET)
FREE(&cdef->initial);
}
}
/**
* create_synonym - Create an alternative name for a config item
* @param cs Config items
* @param cdef Variable definition
* @param err Buffer for error messages
* @retval ptr New HashElem representing the config item synonym
*/
static struct HashElem *create_synonym(const struct ConfigSet *cs,
struct ConfigDef *cdef, struct Buffer *err)
{
if (!cs || !cdef)
return NULL; /* LCOV_EXCL_LINE */
const char *name = (const char *) cdef->initial;
struct HashElem *parent = cs_get_elem(cs, name);
if (!parent)
{
mutt_buffer_printf(err, _("No such variable: %s"), name);
return NULL;
}
struct HashElem *child =
mutt_hash_typed_insert(cs->hash, cdef->name, cdef->type, (void *) cdef);
if (!child)
return NULL; /* LCOV_EXCL_LINE */
cdef->var = (intptr_t) parent;
return child;
}
/**
* reg_one_var - Register one config item
* @param cs Config items
* @param cdef Variable definition
* @param err Buffer for error messages
* @retval ptr New HashElem representing the config item
*/
static struct HashElem *reg_one_var(const struct ConfigSet *cs,
struct ConfigDef *cdef, struct Buffer *err)
{
if (!cs || !cdef)
return NULL; /* LCOV_EXCL_LINE */
if (DTYPE(cdef->type) == DT_SYNONYM)
return create_synonym(cs, cdef, err);
const struct ConfigSetType *cst = cs_get_type_def(cs, cdef->type);
if (!cst)
{
mutt_buffer_printf(err, _("Variable '%s' has an invalid type %d"),
cdef->name, cdef->type);
return NULL;
}
struct HashElem *he = mutt_hash_typed_insert(cs->hash, cdef->name, cdef->type, cdef);
if (!he)
return NULL; /* LCOV_EXCL_LINE */
if (cst && cst->reset)
cst->reset(cs, &cdef->var, cdef, err);
return he;
}
/**
* cs_new - Create a new Config Set
* @param size Number of expected config items
* @retval ptr New ConfigSet object
*/
struct ConfigSet *cs_new(size_t size)
{
struct ConfigSet *cs = mutt_mem_calloc(1, sizeof(*cs));
cs->hash = mutt_hash_new(size, MUTT_HASH_NO_FLAGS);
mutt_hash_set_destructor(cs->hash, cs_hashelem_free, (intptr_t) cs);
return cs;
}
/**
* cs_free - Free a Config Set
* @param[out] ptr Config items
*/
void cs_free(struct ConfigSet **ptr)
{
if (!ptr || !*ptr)
return;
struct ConfigSet *cs = *ptr;
mutt_hash_free(&cs->hash);
FREE(ptr);
}
/**
* cs_get_base - Find the root Config Item
* @param he Config Item to examine
* @retval ptr Root Config Item
*
* Given an inherited HashElem, find the HashElem representing the original
* Config Item.
*/
struct HashElem *cs_get_base(struct HashElem *he)
{
if (!(he->type & DT_INHERITED))
return he;
struct Inheritance *i = he->data;
return cs_get_base(i->parent);
}
/**
* cs_get_elem - Get the HashElem representing a config item
* @param cs Config items
* @param name Name of config item
* @retval ptr HashElem representing the config item
*/
struct HashElem *cs_get_elem(const struct ConfigSet *cs, const char *name)
{
if (!cs || !name)
return NULL;
struct HashElem *he = mutt_hash_find_elem(cs->hash, name);
if (!he)
return NULL;
if (DTYPE(he->type) != DT_SYNONYM)
return he;
const struct ConfigDef *cdef = he->data;
return (struct HashElem *) cdef->var;
}
/**
* cs_get_type_def - Get the definition for a type
* @param cs Config items
* @param type Type to lookup, e.g. #DT_NUMBER
* @retval ptr ConfigSetType representing the type
*/
const struct ConfigSetType *cs_get_type_def(const struct ConfigSet *cs, unsigned int type)
{
if (!cs)
return NULL;
type = DTYPE(type);
if ((type < 1) || (type >= mutt_array_size(cs->types)))
return NULL;
if (!cs->types[type].name)
return NULL;
return &cs->types[type];
}
/**
* cs_register_type - Register a type of config item
* @param cs Config items
* @param cst Structure defining the type
* @retval true Type was registered successfully
*/
bool cs_register_type(struct ConfigSet *cs, const struct ConfigSetType *cst)
{
if (!cs || !cst)
return false;
if (!cst->name || !cst->string_set || !cst->string_get || !cst->reset ||
!cst->native_set || !cst->native_get)
{
return false;
}
if (cst->type >= mutt_array_size(cs->types))
return false;
if (cs->types[cst->type].name)
return false; /* already registered */
cs->types[cst->type] = *cst;
return true;
}
/**
* cs_register_variables - Register a set of config items
* @param cs Config items
* @param vars Variable definition
* @param flags Flags, e.g. #DT_DEPRECATED
* @retval true All variables were registered successfully
*/
bool cs_register_variables(const struct ConfigSet *cs, struct ConfigDef vars[], uint32_t flags)
{
if (!cs || !vars)
return false;
struct Buffer err = mutt_buffer_make(0);
bool rc = true;
for (size_t i = 0; vars[i].name; i++)
{
vars[i].type |= flags;
if (!reg_one_var(cs, &vars[i], &err))
{
mutt_debug(LL_DEBUG1, "%s\n", mutt_buffer_string(&err));
rc = false;
}
}
mutt_buffer_dealloc(&err);
return rc;
}
/**
* cs_inherit_variable - Create in inherited config item
* @param cs Config items
* @param parent HashElem of parent config item
* @param name Name of account-specific config item
* @retval ptr New HashElem representing the inherited config item
*/
struct HashElem *cs_inherit_variable(const struct ConfigSet *cs,
struct HashElem *parent, const char *name)
{
if (!cs || !parent)
return NULL;
struct Inheritance *i = mutt_mem_calloc(1, sizeof(*i));
i->parent = parent;
i->name = mutt_str_dup(name);
struct HashElem *he = mutt_hash_typed_insert(cs->hash, i->name, DT_INHERITED, i);
if (!he)
{
FREE(&i->name);
FREE(&i);
}
return he;
}
/**
* cs_uninherit_variable - Remove an inherited config item
* @param cs Config items
* @param name Name of config item to remove
*/
void cs_uninherit_variable(const struct ConfigSet *cs, const char *name)
{
if (!cs || !name)
return;
mutt_hash_delete(cs->hash, name, NULL);
}
/**
* cs_he_reset - Reset a config item to its initial value
* @param cs Config items
* @param he HashElem representing config item
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_he_reset(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *err)
{
if (!cs || !he)
return CSR_ERR_CODE;
/* An inherited var that's already pointing to its parent.
* Return 'success', but don't send a notification. */
if ((he->type & DT_INHERITED) && (DTYPE(he->type) == 0))
return CSR_SUCCESS;
int rc = CSR_SUCCESS;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
struct HashElem *he_base = cs_get_base(he);
struct ConfigDef *cdef = he_base->data;
if (!cdef)
return CSR_ERR_CODE; // LCOV_EXCL_LINE
const struct ConfigSetType *cst = cs_get_type_def(cs, he_base->type);
if (cst && cst->destroy)
cst->destroy(cs, (void **) &i->var, cdef);
he->type = DT_INHERITED;
}
else
{
struct ConfigDef *cdef = he->data;
if (!cdef)
return CSR_ERR_CODE; // LCOV_EXCL_LINE
const struct ConfigSetType *cst = cs_get_type_def(cs, he->type);
if (cst)
rc = cst->reset(cs, &cdef->var, cdef, err);
}
return rc;
}
/**
* cs_str_reset - Reset a config item to its initial value
* @param cs Config items
* @param name Name of config item
* @param err Buffer for error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_str_reset(const struct ConfigSet *cs, const char *name, struct Buffer *err)
{
if (!cs || !name)
return CSR_ERR_CODE;
struct HashElem *he = cs_get_elem(cs, name);
if (!he)
{
mutt_buffer_printf(err, _("Unknown variable '%s'"), name);
return CSR_ERR_UNKNOWN;
}
return cs_he_reset(cs, he, err);
}
/**
* cs_he_initial_set - Set the initial value of a config item
* @param cs Config items
* @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_he_initial_set(const struct ConfigSet *cs, struct HashElem *he,
const char *value, struct Buffer *err)
{
if (!cs || !he)
return CSR_ERR_CODE;
struct ConfigDef *cdef = NULL;
if (he->type & DT_INHERITED)
{
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
mutt_debug(LL_DEBUG1, "Variable '%s' is inherited type\n", cdef->name);
return CSR_ERR_CODE;
}
cdef = he->data;
if (!cdef)
return CSR_ERR_CODE; // LCOV_EXCL_LINE
const struct ConfigSetType *cst = cs_get_type_def(cs, he->type);
if (!cst)
{
mutt_debug(LL_DEBUG1, "Variable '%s' has an invalid type %d\n", cdef->name, he->type);
return CSR_ERR_CODE;
}
int rc = cst->string_set(cs, NULL, cdef, value, err);
if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc;
return CSR_SUCCESS;
}
/**
* cs_str_initial_set - Set the initial value of a config item
* @param cs Config items
* @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_str_initial_set(const struct ConfigSet *cs, const char *name,
const char *value, struct Buffer *err)
{
if (!cs || !name)
return CSR_ERR_CODE;
struct HashElem *he = cs_get_elem(cs, name);
if (!he)
{
mutt_buffer_printf(err, _("Unknown variable '%s'"), name);
return CSR_ERR_UNKNOWN;
}
return cs_he_initial_set(cs, he, value, err);
}
/**
* cs_he_initial_get - Get the initial, or parent, value of a config item
* @param cs Config items
* @param he HashElem representing config item
* @param result Buffer for results or error messages
* @retval num Result, e.g. #CSR_SUCCESS
*
* If a config item is inherited from another, then this will get the parent's
* value. Otherwise, it will get the config item's initial value.
*/
int cs_he_initial_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
{
if (!cs || !he || !result)
return CSR_ERR_CODE;
const struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
if (he->type & DT_INHERITED)
{
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
}
else
{
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
}
if (!cst)
return CSR_ERR_CODE; // LCOV_EXCL_LINE
return cst->string_get(cs, NULL, cdef, result);
}
/**
* cs_str_initial_get - Get the initial, or parent, value of a config item
* @param cs Config items
* @param name Name of config item
* @param result Buffer for results or error messages
* @retval num Result, e.g. #CSR_SUCCESS
*
* If a config item is inherited from another, then this will get the parent's
* value. Otherwise, it will get the config item's initial value.
*/
int cs_str_initial_get(const struct ConfigSet *cs, const char *name, struct Buffer *result)
{
if (!cs || !name)
return CSR_ERR_CODE;
struct HashElem *he = cs_get_elem(cs, name);
if (!he)
{
mutt_buffer_printf(result, _("Unknown variable '%s'"), name);
return CSR_ERR_UNKNOWN;
}
return cs_he_initial_get(cs, he, result);
}
/**
* cs_he_string_set - Set a config item by string
* @param cs Config items
* @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_he_string_set(const struct ConfigSet *cs, struct HashElem *he,
const char *value, struct Buffer *err)
{
if (!cs || !he)
return CSR_ERR_CODE;
struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
void *var = NULL;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
var = &i->var;
}
else
{
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
var = &cdef->var;
}
if (!cdef)
return CSR_ERR_CODE; // LCOV_EXCL_LINE
if (!cst)
{
mutt_debug(LL_DEBUG1, "Variable '%s' has an invalid type %d\n", cdef->name, he->type);
return CSR_ERR_CODE;
}
int rc = cst->string_set(cs, var, cdef, value, err);
if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc;
if (he->type & DT_INHERITED)
he->type = cdef->type | DT_INHERITED;
return rc;
}
/**
* cs_str_string_set - Set a config item by string
* @param cs Config items
* @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_str_string_set(const struct ConfigSet *cs, const char *name,
const char *value, struct Buffer *err)
{
if (!cs || !name)
return CSR_ERR_CODE;
struct HashElem *he = cs_get_elem(cs, name);
if (!he)
{
mutt_buffer_printf(err, _("Unknown variable '%s'"), name);
return CSR_ERR_UNKNOWN;
}
return cs_he_string_set(cs, he, value, err);
}
/**
* cs_he_string_get - Get a config item as a string
* @param cs Config items
* @param he HashElem representing config item
* @param result Buffer for results or error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_he_string_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
{
if (!cs || !he || !result)
return CSR_ERR_CODE;
struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
void *var = NULL;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
// inherited, value not set
if (DTYPE(he->type) == 0)
return cs_he_string_get(cs, i->parent, result);
// inherited, value set
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
var = &i->var;
}
else
{
// not inherited
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
var = &cdef->var;
}
if (!cdef || !cst)
return CSR_ERR_CODE; // LCOV_EXCL_LINE
return cst->string_get(cs, var, cdef, result);
}
/**
* cs_str_string_get - Get a config item as a string
* @param cs Config items
* @param name Name of config item
* @param result Buffer for results or error messages
* @retval num Result, e.g. #CSR_SUCCESS
*/
int cs_str_string_get(const struct ConfigSet *cs, const char *name, struct Buffer *result)
{
if (!cs || !name)
return CSR_ERR_CODE;
struct HashElem *he = cs_get_elem(cs, name);
if (!he)
{
mutt_buffer_printf(result, _("Unknown variable '%s'"), name);
return CSR_ERR_UNKNOWN;
}
return cs_he_string_get(cs, he, result);
}
/**
* cs_he_native_set - Natively set the value of a HashElem config item
* @param cs Config items
* @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_he_native_set(const struct ConfigSet *cs, struct HashElem *he,
intptr_t value, struct Buffer *err)
{
if (!cs || !he)
return CSR_ERR_CODE;
struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
void *var = NULL;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
var = &i->var;
}
else
{
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
var = &cdef->var;
}
if (!cst)
{
mutt_debug(LL_DEBUG1, "Variable '%s' has an invalid type %d\n", cdef->name, he->type);
return CSR_ERR_CODE;
}
if (!var || !cdef)
return CSR_ERR_CODE; // LCOV_EXCL_LINE
int rc = cst->native_set(cs, var, cdef, value, err);
if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc;
if (he->type & DT_INHERITED)
he->type = cdef->type | DT_INHERITED;
return rc;
}
/**
* cs_str_native_set - Natively set the value of a string config item
* @param cs Config items
* @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_str_native_set(const struct ConfigSet *cs, const char *name,
intptr_t value, struct Buffer *err)
{
if (!cs || !name)
return CSR_ERR_CODE;
struct HashElem *he = cs_get_elem(cs, name);
if (!he)
{
mutt_buffer_printf(err, _("Unknown variable '%s'"), name);
return CSR_ERR_UNKNOWN;
}
struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
void *var = NULL;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
var = &i->var;
}
else
{
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
var = &cdef->var;
}
if (!cst || !var || !cdef)
return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
int rc = cst->native_set(cs, var, cdef, value, err);
if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc;
if (he->type & DT_INHERITED)
he->type = cdef->type | DT_INHERITED;
return rc;
}
/**
* cs_he_native_get - Natively get the value of a HashElem config item
* @param cs Config items
* @param he HashElem representing config item
* @param err Buffer for results or error messages
* @retval intptr_t Native pointer/value
* @retval INT_MIN Error
*/
intptr_t cs_he_native_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *err)
{
if (!cs || !he)
return INT_MIN;
struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
void *var = NULL;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
// inherited, value not set
if (DTYPE(he->type) == 0)
return cs_he_native_get(cs, i->parent, err);
// inherited, value set
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
var = &i->var;
}
else
{
// not inherited
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
var = &cdef->var;
}
if (!var || !cdef)
return INT_MIN; // LCOV_EXCL_LINE
if (!cst)
{
mutt_buffer_printf(err, _("Variable '%s' has an invalid type %d"), cdef->name, he->type);
return INT_MIN;
}
return cst->native_get(cs, var, cdef, err);
}
/**
* cs_str_native_get - Natively get the value of a string config item
* @param cs Config items
* @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_str_native_get(const struct ConfigSet *cs, const char *name, struct Buffer *err)
{
if (!cs || !name)
return INT_MIN;
struct HashElem *he = cs_get_elem(cs, name);
return cs_he_native_get(cs, he, err);
}
/**
* cs_he_string_plus_equals - Add to a config item by string
* @param cs Config items
* @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_he_string_plus_equals(const struct ConfigSet *cs, struct HashElem *he,
const char *value, struct Buffer *err)
{
if (!cs || !he)
return CSR_ERR_CODE;
struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
void *var = NULL;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
var = &i->var;
}
else
{
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
var = &cdef->var;
}
if (!var || !cdef)
return INT_MIN; // LCOV_EXCL_LINE
if (!cst)
{
mutt_debug(LL_DEBUG1, "Variable '%s' has an invalid type %d\n", cdef->name, he->type);
return CSR_ERR_CODE;
}
if (!cst->string_plus_equals)
{
// L10N: e.g. Type 'boolean' doesn't support operation '+='
mutt_buffer_printf(err, _("Type '%s' doesn't support operation '%s'"), cst->name, "+=");
return CSR_ERR_INVALID | CSV_INV_NOT_IMPL;
}
int rc = cst->string_plus_equals(cs, var, cdef, value, err);
if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc;
if (he->type & DT_INHERITED)
he->type = cdef->type | DT_INHERITED;
return rc;
}
/**
* cs_str_string_plus_equals - Add to a config item by string
* @param cs Config items
* @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_str_string_plus_equals(const struct ConfigSet *cs, const char *name,
const char *value, struct Buffer *err)
{
if (!cs || !name)
return CSR_ERR_CODE;
struct HashElem *he = cs_get_elem(cs, name);
if (!he)
{
mutt_buffer_printf(err, _("Unknown variable '%s'"), name);
return CSR_ERR_UNKNOWN;
}
return cs_he_string_plus_equals(cs, he, value, err);
}
/**
* cs_he_string_minus_equals - Remove from a config item by string
* @param cs Config items
* @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_he_string_minus_equals(const struct ConfigSet *cs, struct HashElem *he,
const char *value, struct Buffer *err)
{
if (!cs || !he)
return CSR_ERR_CODE;
struct ConfigDef *cdef = NULL;
const struct ConfigSetType *cst = NULL;
void *var = NULL;
if (he->type & DT_INHERITED)
{
struct Inheritance *i = he->data;
struct HashElem *he_base = cs_get_base(he);
cdef = he_base->data;
cst = cs_get_type_def(cs, he_base->type);
var = &i->var;
}
else
{
cdef = he->data;
cst = cs_get_type_def(cs, he->type);
var = &cdef->var;
}
if (!var || !cdef)
return INT_MIN; // LCOV_EXCL_LINE
if (!cst)
{
mutt_debug(LL_DEBUG1, "Variable '%s' has an invalid type %d\n", cdef->name, he->type);
return CSR_ERR_CODE;
}
if (!cst->string_minus_equals)
{
// L10N: e.g. Type 'boolean' doesn't support operation '+='
mutt_buffer_printf(err, _("Type '%s' doesn't support operation '%s'"), cst->name, "-=");
return CSR_ERR_INVALID | CSV_INV_NOT_IMPL;
}
int rc = cst->string_minus_equals(cs, var, cdef, value, err);
if (CSR_RESULT(rc) != CSR_SUCCESS)
return rc;
if (he->type & DT_INHERITED)
he->type = cdef->type | DT_INHERITED;
return rc;
}
/**
* cs_str_string_minus_equals - Remove from a config item by string
* @param cs Config items
* @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_str_string_minus_equals(const struct ConfigSet *cs, const char *name,