NeoMutt  2020-06-26-89-g172cd3
Teaching an old dog new tricks
DOXYGEN
subset.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <limits.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include "mutt/lib.h"
35 #include "subset.h"
36 #include "set.h"
37 
38 struct Notify;
39 
48 int elem_list_sort(const void *a, const void *b)
49 {
50  if (!a || !b)
51  return 0;
52 
53  const struct HashElem *hea = *(struct HashElem const *const *) a;
54  const struct HashElem *heb = *(struct HashElem const *const *) b;
55 
56  return mutt_istr_cmp(hea->key.strkey, heb->key.strkey);
57 }
58 
64 struct HashElem **get_elem_list(struct ConfigSet *cs)
65 {
66  if (!cs)
67  return NULL;
68 
69  struct HashElem **list = mutt_mem_calloc(1024, sizeof(struct HashElem *));
70  size_t index = 0;
71 
72  struct HashWalkState walk = { 0 };
73  struct HashElem *he = NULL;
74 
75  while ((he = mutt_hash_walk(cs->hash, &walk)))
76  {
77  list[index++] = he;
78  if (index == 1022)
79  break; /* LCOV_EXCL_LINE */
80  }
81 
82  qsort(list, index, sizeof(struct HashElem *), elem_list_sort);
83 
84  return list;
85 }
86 
93 void cs_subset_free(struct ConfigSubset **ptr)
94 {
95  if (!ptr || !*ptr)
96  return;
97 
98  struct ConfigSubset *sub = *ptr;
99 
100  if (sub->cs && sub->name)
101  {
102  char scope[256];
103  snprintf(scope, sizeof(scope), "%s:", sub->name);
104 
105  // We don't know if any config items have been set,
106  // so search for anything with a matching scope.
107  struct HashElem **list = get_elem_list(sub->cs);
108  for (size_t i = 0; list[i]; i++)
109  {
110  const char *item = list[i]->key.strkey;
111  if (mutt_str_startswith(item, scope) != 0)
112  {
113  cs_uninherit_variable(sub->cs, item);
114  }
115  }
116  FREE(&list);
117  }
118 
119  notify_free(&sub->notify);
120  FREE(&sub->name);
121  FREE(ptr);
122 }
123 
133 struct ConfigSubset *cs_subset_new(const char *name, struct ConfigSubset *sub_parent,
134  struct Notify *not_parent)
135 {
136  struct ConfigSubset *sub = mutt_mem_calloc(1, sizeof(*sub));
137 
138  if (sub_parent)
139  {
140  sub->parent = sub_parent;
141  sub->cs = sub_parent->cs;
142  }
143 
144  if (name)
145  {
146  char scope[256];
147 
148  if (sub_parent && sub_parent->name)
149  snprintf(scope, sizeof(scope), "%s:%s", sub_parent->name, name);
150  else
151  mutt_str_copy(scope, name, sizeof(scope));
152 
153  sub->name = mutt_str_dup(scope);
154  }
155 
156  sub->notify = notify_new();
157  notify_set_parent(sub->notify, not_parent);
158 
159  return sub;
160 }
161 
168 struct HashElem *cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
169 {
170  if (!sub || !name)
171  return NULL;
172 
173  char scope[256];
174  if (sub->name)
175  snprintf(scope, sizeof(scope), "%s:%s", sub->name, name);
176  else
177  mutt_str_copy(scope, name, sizeof(scope));
178 
179  return cs_get_elem(sub->cs, scope);
180 }
181 
189 struct HashElem *cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
190 {
191  if (!sub)
192  return NULL;
193 
194  struct HashElem *he = cs_subset_lookup(sub, name);
195  if (he)
196  return he;
197 
198  if (sub->parent)
199  {
200  // Create parent before creating name
201  he = cs_subset_create_inheritance(sub->parent, name);
202  }
203 
204  if (!he)
205  return NULL;
206 
207  char scope[256];
208  snprintf(scope, sizeof(scope), "%s:%s", sub->name, name);
209  return cs_inherit_variable(sub->cs, he, scope);
210 }
211 
219  struct HashElem *he, enum NotifyConfig ev)
220 {
221  if (!sub || !he)
222  return;
223 
224  struct HashElem *he_base = cs_get_base(he);
225  struct EventConfig ec = { sub, he_base->key.strkey, he };
226  notify_send(sub->notify, NT_CONFIG, ev, &ec);
227 }
228 
237 intptr_t cs_subset_he_native_get(const struct ConfigSubset *sub,
238  struct HashElem *he, struct Buffer *err)
239 {
240  if (!sub)
241  return INT_MIN;
242 
243  return cs_he_native_get(sub->cs, he, err);
244 }
245 
255  const char *name, struct Buffer *err)
256 {
257  struct HashElem *he = cs_subset_create_inheritance(sub, name);
258 
259  return cs_subset_he_native_get(sub, he, err);
260 }
261 
270 int cs_subset_he_native_set(const struct ConfigSubset *sub, struct HashElem *he,
271  intptr_t value, struct Buffer *err)
272 {
273  if (!sub)
274  return CSR_ERR_CODE;
275 
276  int rc = cs_he_native_set(sub->cs, he, value, err);
277 
278  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
280 
281  return rc;
282 }
283 
292 int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name,
293  intptr_t value, struct Buffer *err)
294 {
295  struct HashElem *he = cs_subset_create_inheritance(sub, name);
296 
297  return cs_subset_he_native_set(sub, he, value, err);
298 }
299 
307 int cs_subset_he_reset(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
308 {
309  if (!sub)
310  return CSR_ERR_CODE;
311 
312  int rc = cs_he_reset(sub->cs, he, err);
313 
314  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
316 
317  return rc;
318 }
319 
327 int cs_subset_str_reset(const struct ConfigSubset *sub, const char *name, struct Buffer *err)
328 {
329  struct HashElem *he = cs_subset_create_inheritance(sub, name);
330 
331  return cs_subset_he_reset(sub, he, err);
332 }
333 
341 int cs_subset_he_string_get(const struct ConfigSubset *sub, struct HashElem *he,
342  struct Buffer *result)
343 {
344  if (!sub)
345  return CSR_ERR_CODE;
346 
347  return cs_he_string_get(sub->cs, he, result);
348 }
349 
357 int cs_subset_str_string_get(const struct ConfigSubset *sub, const char *name,
358  struct Buffer *result)
359 {
360  struct HashElem *he = cs_subset_create_inheritance(sub, name);
361 
362  return cs_subset_he_string_get(sub, he, result);
363 }
364 
373 int cs_subset_he_string_set(const struct ConfigSubset *sub, struct HashElem *he,
374  const char *value, struct Buffer *err)
375 {
376  if (!sub)
377  return CSR_ERR_CODE;
378 
379  int rc = cs_he_string_set(sub->cs, he, value, err);
380 
381  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
383 
384  return rc;
385 }
386 
395 int cs_subset_str_string_set(const struct ConfigSubset *sub, const char *name,
396  const char *value, struct Buffer *err)
397 {
398  struct HashElem *he = cs_subset_create_inheritance(sub, name);
399 
400  return cs_subset_he_string_set(sub, he, value, err);
401 }
402 
411 int cs_subset_he_string_plus_equals(const struct ConfigSubset *sub, struct HashElem *he,
412  const char *value, struct Buffer *err)
413 {
414  if (!sub)
415  return CSR_ERR_CODE;
416 
417  int rc = cs_he_string_plus_equals(sub->cs, he, value, err);
418 
419  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
421 
422  return rc;
423 }
424 
433 int cs_subset_str_string_plus_equals(const struct ConfigSubset *sub, const char *name,
434  const char *value, struct Buffer *err)
435 {
436  struct HashElem *he = cs_subset_create_inheritance(sub, name);
437 
438  return cs_subset_he_string_plus_equals(sub, he, value, err);
439 }
440 
449 int cs_subset_he_string_minus_equals(const struct ConfigSubset *sub, struct HashElem *he,
450  const char *value, struct Buffer *err)
451 {
452  if (!sub)
453  return CSR_ERR_CODE;
454 
455  int rc = cs_he_string_minus_equals(sub->cs, he, value, err);
456 
457  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
459 
460  return rc;
461 }
462 
471 int cs_subset_str_string_minus_equals(const struct ConfigSubset *sub, const char *name,
472  const char *value, struct Buffer *err)
473 {
474  struct HashElem *he = cs_subset_create_inheritance(sub, name);
475 
476  return cs_subset_he_string_minus_equals(sub, he, value, err);
477 }
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:585
union HashKey key
Key representing the data.
Definition: hash.h:46
Container for lots of config items.
Definition: set.h:227
NotifyConfig
Config notification types.
Definition: subset.h:60
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
int cs_he_string_minus_equals(const struct ConfigSet *cs, struct HashElem *he, const char *value, struct Buffer *err)
Remove from a config item by string.
Definition: set.c:943
struct ConfigSubset * parent
Parent Subset.
Definition: subset.h:50
#define CSR_RESULT(x)
Definition: set.h:52
struct HashElem ** get_elem_list(struct ConfigSet *cs)
Create a sorted list of all config items.
Definition: subset.c:64
struct ConfigSubset * cs_subset_new(const char *name, struct ConfigSubset *sub_parent, struct Notify *not_parent)
Create a new Config Subset.
Definition: subset.c:133
intptr_t cs_subset_str_native_get(const struct ConfigSubset *sub, const char *name, struct Buffer *err)
Natively get the value of a string config item.
Definition: subset.c:254
int cs_subset_str_string_plus_equals(const struct ConfigSubset *sub, const char *name, const char *value, struct Buffer *err)
Add to a config item by string.
Definition: subset.c:433
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:62
const char * name
Scope name of Subset.
Definition: subset.h:48
Cursor to iterate through a Hash Table.
Definition: hash.h:119
void cs_uninherit_variable(const struct ConfigSet *cs, const char *name)
Remove an inherited config item.
Definition: set.c:333
struct HashElem * cs_inherit_variable(const struct ConfigSet *cs, struct HashElem *parent, const char *name)
Create in inherited config item.
Definition: set.c:308
A config-change event.
Definition: subset.h:70
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:189
String manipulation buffer.
Definition: buffer.h:33
void cs_subset_notify_observers(const struct ConfigSubset *sub, struct HashElem *he, enum NotifyConfig ev)
Notify all observers of an event.
Definition: subset.c:218
int cs_subset_he_string_get(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: subset.c:341
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:191
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:375
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:93
struct HashElem * cs_subset_lookup(const struct ConfigSubset *sub, const char *name)
Find an inherited config item.
Definition: subset.c:168
int cs_subset_str_string_set(const struct ConfigSubset *sub, const char *name, const char *value, struct Buffer *err)
Set a config item by string.
Definition: subset.c:395
int cs_subset_he_string_plus_equals(const struct ConfigSubset *sub, struct HashElem *he, const char *value, struct Buffer *err)
Add to a config item by string.
Definition: subset.c:411
int cs_subset_str_string_get(const struct ConfigSubset *sub, const char *name, struct Buffer *result)
Get a config item as a string.
Definition: subset.c:357
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:292
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
struct HashElem * he
Config item that changed.
Definition: subset.h:74
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:237
int cs_he_string_plus_equals(const struct ConfigSet *cs, struct HashElem *he, const char *value, struct Buffer *err)
Add to a config item by string.
Definition: set.c:860
int cs_subset_he_string_set(const struct ConfigSubset *sub, struct HashElem *he, const char *value, struct Buffer *err)
Set a config item by string.
Definition: subset.c:373
int cs_he_reset(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *err)
Reset a config item to its initial value.
Definition: set.c:348
int cs_he_native_set(const struct ConfigSet *cs, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition: set.c:687
struct HashElem * cs_get_elem(const struct ConfigSet *cs, const char *name)
Get the HashElem representing a config item.
Definition: set.c:206
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
struct HashTable * hash
HashTable storing the config items.
Definition: set.h:229
int cs_subset_he_string_minus_equals(const struct ConfigSubset *sub, struct HashElem *he, const char *value, struct Buffer *err)
Remove from a config item by string.
Definition: subset.c:449
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:49
A set of inherited config items.
Definition: subset.h:46
struct Notify * notify
Notifications system.
Definition: subset.h:52
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:165
A collection of config items.
Notification API.
Definition: notify.c:39
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition: subset.h:49
Subset of Config Items.
Config item has been set.
Definition: subset.h:62
int cs_subset_str_string_minus_equals(const struct ConfigSubset *sub, const char *name, const char *value, struct Buffer *err)
Remove from a config item by string.
Definition: subset.c:471
int elem_list_sort(const void *a, const void *b)
Sort two HashElem pointers to config.
Definition: subset.c:48
int cs_he_string_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: set.c:620
Config has changed, NotifyConfig, EventConfig.
Definition: notify_type.h:36
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:721
const char * strkey
String key.
Definition: hash.h:36
#define FREE(x)
Definition: memory.h:40
struct HashElem * mutt_hash_walk(const struct HashTable *table, struct HashWalkState *state)
Iterate through all the HashElem&#39;s in a Hash Table.
Definition: hash.c:479
Config item has been reset to initial, or parent, value.
Definition: subset.h:63
The item stored in a Hash Table.
Definition: hash.h:43
void notify_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition: notify.c:82
Convenience wrapper for the library headers.
int cs_subset_he_native_set(const struct ConfigSubset *sub, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition: subset.c:270
const struct ConfigSubset * sub
Config Subset.
Definition: subset.h:72
intptr_t cs_he_native_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *err)
Natively get the value of a HashElem config item.
Definition: set.c:792
int cs_subset_he_reset(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Reset a config item to its initial value.
Definition: subset.c:307
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
const char * name
Name of config item that changed.
Definition: subset.h:73
int cs_subset_str_reset(const struct ConfigSubset *sub, const char *name, struct Buffer *err)
Reset a config item to its initial value.
Definition: subset.c:327
int cs_he_string_set(const struct ConfigSet *cs, struct HashElem *he, const char *value, struct Buffer *err)
Set a config item by string.
Definition: set.c:545
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:137