NeoMutt  2020-11-20
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 
188 static int quad_toggle(int opt)
189 {
190  return opt ^= 1;
191 }
192 
202 int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
203 {
204  if (!sub || !he || !he->data)
205  return CSR_ERR_CODE;
206 
207  struct HashElem *he_base = cs_get_base(he);
208  if (DTYPE(he_base->type) != DT_QUAD)
209  return CSR_ERR_CODE;
210 
211  intptr_t value = cs_he_native_get(sub->cs, he, err);
212  if (value == INT_MIN)
213  return CSR_ERR_CODE;
214 
215  value = quad_toggle(value);
216  int rc = cs_he_native_set(sub->cs, he, value, err);
217 
218  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
220 
221  return rc;
222 }
223 
233 int quad_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 quad_he_toggle(sub, he, err);
238 }
239 
243 const struct ConfigSetType cst_quad = {
244  DT_QUAD,
245  "quad",
250  NULL, // string_plus_equals
251  NULL, // string_minus_equals
252  quad_reset,
253  NULL, // destroy
254 };
#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
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
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
Constants for all the config types.
A collection of config items.
intptr_t initial
Initial value.
Definition: set.h:66
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:202
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 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
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
#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
#define mutt_array_size(x)
Definition: memory.h:33
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
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
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
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition: quad.c:188
Type representing a quad-option.
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
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
void * data
User-supplied data.
Definition: hash.h:47
Log at debug level 1.
Definition: logging.h:40
int quad_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:233
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
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
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
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
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
Convenience wrapper for the library headers.
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48