NeoMutt  2020-08-07-1-gab41a1
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 
182 void bool_init(struct ConfigSet *cs)
183 {
184  const struct ConfigSetType cst_bool = {
185  "boolean",
190  NULL, // string_plus_equals
191  NULL, // string_minus_equals
192  bool_reset,
193  NULL, // destroy
194  };
195  cs_register_type(cs, DT_BOOL, &cst_bool);
196 }
197 
205 int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
206 {
207  if (!sub || !he || !he->data)
208  return CSR_ERR_CODE;
209 
210  struct HashElem *he_base = cs_get_base(he);
211  if (DTYPE(he_base->type) != DT_BOOL)
212  return CSR_ERR_CODE;
213 
214  intptr_t value = cs_he_native_get(sub->cs, he, err);
215  if (value == INT_MIN)
216  return CSR_ERR_CODE;
217 
218  int rc = cs_he_native_set(sub->cs, he, !value, err);
219 
220  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
222 
223  return rc;
224 }
225 
233 int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
234 {
235  struct HashElem *he = cs_subset_create_inheritance(sub, name);
236 
237  return bool_he_toggle(sub, he, err);
238 }
Type definition for a config item.
Definition: set.h:88
Container for lots of config items.
Definition: set.h:227
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
void bool_init(struct ConfigSet *cs)
Register the Bool config type.
Definition: bool.c:182
Constants for all the config types.
if(!test_colorize_)
Definition: acutest.h:499
#define CSR_RESULT(x)
Definition: set.h:52
intptr_t initial
Initial value.
Definition: set.h:66
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
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:199
#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
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
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:707
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
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:888
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
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
A collection of config items.
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
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_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:233
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
Subset of Config Items.
Config item has been set.
Definition: subset.h:62
void * data
User-supplied data.
Definition: hash.h:47
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
Type representing a boolean.
int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:205
const char * BoolValues[]
Valid strings for creating a Bool.
Definition: bool.c:49
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
Convenience wrapper for the library headers.
bool cs_register_type(struct ConfigSet *cs, unsigned int type, const struct ConfigSetType *cst)
Register a type of config item.
Definition: set.c:259
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:818
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
#define DT_BOOL
boolean option
Definition: types.h:30
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_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