NeoMutt  2020-08-07-1-gab41a1
Teaching an old dog new tricks
DOXYGEN
quad.c
Go to the documentation of this file.
1 
33 #include "config.h"
34 #include <stddef.h>
35 #include <limits.h>
36 #include <stdint.h>
37 #include "mutt/lib.h"
38 #include "quad.h"
39 #include "set.h"
40 #include "subset.h"
41 #include "types.h"
42 
48 const char *QuadValues[] = {
49  "no", "yes", "ask-no", "ask-yes", NULL,
50 };
51 
55 static int quad_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
56  const char *value, struct Buffer *err)
57 {
58  if (!value)
59  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
60 
61  int num = -1;
62  for (size_t i = 0; QuadValues[i]; i++)
63  {
64  if (mutt_istr_equal(QuadValues[i], value))
65  {
66  num = i;
67  break;
68  }
69  }
70 
71  if (num < 0)
72  {
73  mutt_buffer_printf(err, _("Invalid quad value: %s"), value);
75  }
76 
77  if (var)
78  {
79  if (num == (*(char *) var))
81 
82  if (cdef->validator)
83  {
84  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
85 
86  if (CSR_RESULT(rc) != CSR_SUCCESS)
87  return rc | CSR_INV_VALIDATOR;
88  }
89 
90  *(char *) var = num;
91  }
92  else
93  {
94  cdef->initial = num;
95  }
96 
97  return CSR_SUCCESS;
98 }
99 
103 static int quad_string_get(const struct ConfigSet *cs, void *var,
104  const struct ConfigDef *cdef, struct Buffer *result)
105 {
106  unsigned int value;
107 
108  if (var)
109  value = *(char *) var;
110  else
111  value = (int) cdef->initial;
112 
113  if (value >= (mutt_array_size(QuadValues) - 1))
114  {
115  mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value);
116  return CSR_ERR_INVALID | CSR_INV_TYPE;
117  }
118 
119  mutt_buffer_addstr(result, QuadValues[value]);
120  return CSR_SUCCESS;
121 }
122 
126 static int quad_native_set(const struct ConfigSet *cs, void *var,
127  const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
128 {
129  if ((value < 0) || (value >= (mutt_array_size(QuadValues) - 1)))
130  {
131  mutt_buffer_printf(err, _("Invalid quad value: %ld"), value);
132  return CSR_ERR_INVALID | CSR_INV_TYPE;
133  }
134 
135  if (value == (*(char *) var))
137 
138  if (cdef->validator)
139  {
140  int rc = cdef->validator(cs, cdef, value, err);
141 
142  if (CSR_RESULT(rc) != CSR_SUCCESS)
143  return rc | CSR_INV_VALIDATOR;
144  }
145 
146  *(char *) var = value;
147  return CSR_SUCCESS;
148 }
149 
153 static intptr_t quad_native_get(const struct ConfigSet *cs, void *var,
154  const struct ConfigDef *cdef, struct Buffer *err)
155 {
156  return *(char *) var;
157 }
158 
162 static int quad_reset(const struct ConfigSet *cs, void *var,
163  const struct ConfigDef *cdef, struct Buffer *err)
164 {
165  if (cdef->initial == (*(char *) var))
167 
168  if (cdef->validator)
169  {
170  int rc = cdef->validator(cs, cdef, cdef->initial, err);
171 
172  if (CSR_RESULT(rc) != CSR_SUCCESS)
173  return rc | CSR_INV_VALIDATOR;
174  }
175 
176  *(char *) var = cdef->initial;
177  return CSR_SUCCESS;
178 }
179 
184 void quad_init(struct ConfigSet *cs)
185 {
186  const struct ConfigSetType cst_quad = {
187  "quad",
192  NULL, // string_plus_equals
193  NULL, // string_minus_equals
194  quad_reset,
195  NULL, // destroy
196  };
197  cs_register_type(cs, DT_QUAD, &cst_quad);
198 }
199 
208 static int quad_toggle(int opt)
209 {
210  return opt ^= 1;
211 }
212 
222 int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
223 {
224  if (!sub || !he || !he->data)
225  return CSR_ERR_CODE;
226 
227  struct HashElem *he_base = cs_get_base(he);
228  if (DTYPE(he_base->type) != DT_QUAD)
229  return CSR_ERR_CODE;
230 
231  intptr_t value = cs_he_native_get(sub->cs, he, err);
232  if (value == INT_MIN)
233  return CSR_ERR_CODE;
234 
235  value = quad_toggle(value);
236  int rc = cs_he_native_set(sub->cs, he, value, err);
237 
238  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
240 
241  return rc;
242 }
243 
253 int quad_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
254 {
255  struct HashElem *he = cs_subset_create_inheritance(sub, name);
256 
257  return quad_he_toggle(sub, he, err);
258 }
Type definition for a config item.
Definition: set.h:88
Container for lots of config items.
Definition: set.h:227
void quad_init(struct ConfigSet *cs)
Register the Quad-option config type.
Definition: quad.c:184
Constants for all the config types.
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition: quad.c:208
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
static int quad_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Quad-option as a string - Implements ConfigSetType::string_get()
Definition: quad.c:103
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
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
#define mutt_array_size(x)
Definition: memory.h:33
Type representing a quad-option.
static int quad_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Quad-option by string - Implements ConfigSetType::string_set()
Definition: quad.c:55
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
static int quad_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Quad-option to its initial value - Implements ConfigSetType::reset()
Definition: quad.c:162
static int quad_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Quad-option config item by int - Implements ConfigSetType::native_set()
Definition: quad.c:126
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
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
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
int quad_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:253
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
Log at debug level 1.
Definition: logging.h:40
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:222
The item stored in a Hash Table.
Definition: hash.h:43
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
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
static intptr_t quad_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int object from a Quad-option config item - Implements ConfigSetType::native_get() ...
Definition: quad.c:153