NeoMutt  2021-02-05-666-ge300cd
Teaching an old dog new tricks
DOXYGEN
bool.c
Go to the documentation of this file.
1 
34 #include "config.h"
35 #include <stddef.h>
36 #include <limits.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include "mutt/lib.h"
40 #include "bool.h"
41 #include "set.h"
42 #include "subset.h"
43 #include "types.h"
44 
50 const char *BoolValues[] = {
51  "no", "yes", "n", "y", "false", "true", "0", "1", "off", "on", NULL,
52 };
53 
57 static int bool_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
58  const char *value, struct Buffer *err)
59 {
60  if (!value)
61  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
62 
63  int num = -1;
64  for (size_t i = 0; BoolValues[i]; i++)
65  {
66  if (mutt_istr_equal(BoolValues[i], value))
67  {
68  num = i % 2;
69  break;
70  }
71  }
72 
73  if (num < 0)
74  {
75  mutt_buffer_printf(err, _("Invalid boolean value: %s"), value);
77  }
78 
79  if (var)
80  {
81  if (num == (*(bool *) var))
83 
84  if (cdef->validator)
85  {
86  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
87 
88  if (CSR_RESULT(rc) != CSR_SUCCESS)
89  return rc | CSR_INV_VALIDATOR;
90  }
91 
92  *(bool *) var = num;
93  }
94  else
95  {
96  cdef->initial = num;
97  }
98 
99  return CSR_SUCCESS;
100 }
101 
105 static int bool_string_get(const struct ConfigSet *cs, void *var,
106  const struct ConfigDef *cdef, struct Buffer *result)
107 {
108  int index;
109 
110  if (var)
111  index = *(bool *) var;
112  else
113  index = (int) cdef->initial;
114 
115  if (index > 1)
116  return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
117 
118  mutt_buffer_addstr(result, BoolValues[index]);
119  return CSR_SUCCESS;
120 }
121 
125 static int bool_native_set(const struct ConfigSet *cs, void *var,
126  const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
127 {
128  if ((value < 0) || (value > 1))
129  {
130  mutt_buffer_printf(err, _("Invalid boolean value: %ld"), value);
131  return CSR_ERR_INVALID | CSR_INV_TYPE;
132  }
133 
134  if (value == (*(bool *) var))
136 
137  if (cdef->validator)
138  {
139  int rc = cdef->validator(cs, cdef, value, err);
140 
141  if (CSR_RESULT(rc) != CSR_SUCCESS)
142  return rc | CSR_INV_VALIDATOR;
143  }
144 
145  *(bool *) var = value;
146  return CSR_SUCCESS;
147 }
148 
152 static intptr_t bool_native_get(const struct ConfigSet *cs, void *var,
153  const struct ConfigDef *cdef, struct Buffer *err)
154 {
155  return *(bool *) var;
156 }
157 
161 static int bool_reset(const struct ConfigSet *cs, void *var,
162  const struct ConfigDef *cdef, struct Buffer *err)
163 {
164  if (cdef->initial == (*(bool *) var))
166 
167  if (cdef->validator)
168  {
169  int rc = cdef->validator(cs, cdef, cdef->initial, err);
170 
171  if (CSR_RESULT(rc) != CSR_SUCCESS)
172  return rc | CSR_INV_VALIDATOR;
173  }
174 
175  *(bool *) var = cdef->initial;
176  return CSR_SUCCESS;
177 }
178 
186 int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
187 {
188  if (!sub || !he || !he->data)
189  return CSR_ERR_CODE;
190 
191  struct HashElem *he_base = cs_get_base(he);
192  if (DTYPE(he_base->type) != DT_BOOL)
193  return CSR_ERR_CODE;
194 
195  intptr_t value = cs_he_native_get(sub->cs, he, err);
196  if (value == INT_MIN)
197  return CSR_ERR_CODE;
198 
199  int rc = cs_he_native_set(sub->cs, he, !value, err);
200 
201  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
203 
204  return rc;
205 }
206 
214 int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
215 {
216  struct HashElem *he = cs_subset_create_inheritance(sub, name);
217 
218  return bool_he_toggle(sub, he, err);
219 }
220 
224 const struct ConfigSetType cst_bool = {
225  DT_BOOL,
226  "boolean",
231  NULL, // string_plus_equals
232  NULL, // string_minus_equals
233  bool_reset,
234  NULL, // destroy
235 };
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
Container for lots of config items.
Definition: set.h:259
#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:50
int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:186
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:161
A collection of config items.
intptr_t initial
Initial value.
Definition: set.h:67
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:57
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:229
#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
Definition: set.h:63
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:200
#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:790
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:214
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
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:105
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:916
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)
Definition: set.h:82
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:125
Subset of Config Items.
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:189
Config item has been set.
Definition: subset.h:62
void * data
User-supplied data.
Definition: hash.h:47
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:152
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:685
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
Convenience wrapper for the library headers.
#define DT_BOOL
boolean option
Definition: types.h:30
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48