NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
bool.c
Go to the documentation of this file.
1 
33 #include "config.h"
34 #include <stddef.h>
35 #include <limits.h>
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include "mutt/lib.h"
39 #include "bool.h"
40 #include "set.h"
41 #include "subset.h"
42 #include "types.h"
43 
49 const char *BoolValues[] = {
50  "no", "yes", "n", "y", "false", "true", "0", "1", "off", "on", NULL,
51 };
52 
56 static int bool_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
57  const char *value, struct Buffer *err)
58 {
59  if (!value)
60  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
61 
62  int num = -1;
63  for (size_t i = 0; BoolValues[i]; i++)
64  {
65  if (mutt_istr_equal(BoolValues[i], value))
66  {
67  num = i % 2;
68  break;
69  }
70  }
71 
72  if (num < 0)
73  {
74  mutt_buffer_printf(err, _("Invalid boolean value: %s"), value);
76  }
77 
78  if (var)
79  {
80  if (num == (*(bool *) var))
82 
83  if (cdef->validator)
84  {
85  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
86 
87  if (CSR_RESULT(rc) != CSR_SUCCESS)
88  return rc | CSR_INV_VALIDATOR;
89  }
90 
91  *(bool *) var = num;
92  }
93  else
94  {
95  cdef->initial = num;
96  }
97 
98  return CSR_SUCCESS;
99 }
100 
104 static int bool_string_get(const struct ConfigSet *cs, void *var,
105  const struct ConfigDef *cdef, struct Buffer *result)
106 {
107  int index;
108 
109  if (var)
110  index = *(bool *) var;
111  else
112  index = (int) cdef->initial;
113 
114  if (index > 1)
115  return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
116 
117  mutt_buffer_addstr(result, BoolValues[index]);
118  return CSR_SUCCESS;
119 }
120 
124 static int bool_native_set(const struct ConfigSet *cs, void *var,
125  const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
126 {
127  if ((value < 0) || (value > 1))
128  {
129  mutt_buffer_printf(err, _("Invalid boolean value: %ld"), value);
130  return CSR_ERR_INVALID | CSR_INV_TYPE;
131  }
132 
133  if (value == (*(bool *) var))
135 
136  if (cdef->validator)
137  {
138  int rc = cdef->validator(cs, cdef, value, err);
139 
140  if (CSR_RESULT(rc) != CSR_SUCCESS)
141  return rc | CSR_INV_VALIDATOR;
142  }
143 
144  *(bool *) var = value;
145  return CSR_SUCCESS;
146 }
147 
151 static intptr_t bool_native_get(const struct ConfigSet *cs, void *var,
152  const struct ConfigDef *cdef, struct Buffer *err)
153 {
154  return *(bool *) var;
155 }
156 
160 static int bool_reset(const struct ConfigSet *cs, void *var,
161  const struct ConfigDef *cdef, struct Buffer *err)
162 {
163  if (cdef->initial == (*(bool *) var))
165 
166  if (cdef->validator)
167  {
168  int rc = cdef->validator(cs, cdef, cdef->initial, err);
169 
170  if (CSR_RESULT(rc) != CSR_SUCCESS)
171  return rc | CSR_INV_VALIDATOR;
172  }
173 
174  *(bool *) var = cdef->initial;
175  return CSR_SUCCESS;
176 }
177 
185 int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
186 {
187  if (!sub || !he || !he->data)
188  return CSR_ERR_CODE;
189 
190  struct HashElem *he_base = cs_get_base(he);
191  if (DTYPE(he_base->type) != DT_BOOL)
192  return CSR_ERR_CODE;
193 
194  intptr_t value = cs_he_native_get(sub->cs, he, err);
195  if (value == INT_MIN)
196  return CSR_ERR_CODE;
197 
198  int rc = cs_he_native_set(sub->cs, he, !value, err);
199 
200  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
202 
203  return rc;
204 }
205 
213 int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
214 {
215  struct HashElem *he = cs_subset_create_inheritance(sub, name);
216 
217  return bool_he_toggle(sub, he, err);
218 }
219 
223 const struct ConfigSetType cst_bool = {
224  DT_BOOL,
225  "boolean",
230  NULL, // string_plus_equals
231  NULL, // string_minus_equals
232  bool_reset,
233  NULL, // destroy
234 };
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
Type definition for a config item.
Definition: set.h:88
Container for lots of config items.
Definition: set.h:228
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
Constants for all the config types.
const char * BoolValues[]
Valid strings for creating a Bool.
Definition: bool.c:49
static int bool_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Bool config item by bool - Implements ConfigSetType::native_set()
Definition: bool.c:124
int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:185
static int bool_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Bool as a string - Implements ConfigSetType::string_get()
Definition: bool.c:104
static int bool_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Bool by string - Implements ConfigSetType::string_set()
Definition: bool.c:56
A collection of config items.
intptr_t initial
Initial value.
Definition: set.h:66
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
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
Config item definition.
Definition: set.h:61
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:189
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
#define CSR_RESULT(x)
Definition: set.h:52
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:817
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:213
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:883
static intptr_t bool_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a bool from a Bool config item - Implements ConfigSetType::native_get()
Definition: bool.c:151
A set of inherited config items.
Definition: subset.h:46
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
Subset of Config Items.
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:199
Config item has been set.
Definition: subset.h:62
void * data
User-supplied data.
Definition: hash.h:47
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
The item stored in a Hash Table.
Definition: hash.h:43
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
Type representing a boolean.
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:706
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
Convenience wrapper for the library headers.
static int bool_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Bool to its initial value - Implements ConfigSetType::reset()
Definition: bool.c:160
#define DT_BOOL
boolean option
Definition: types.h:30
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48