NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
quad.c
Go to the documentation of this file.
1
34#include "config.h"
35#include <stddef.h>
36#include <limits.h>
37#include <stdint.h>
38#include "mutt/lib.h"
39#include "quad.h"
40#include "set.h"
41#include "subset.h"
42#include "types.h"
43
49const char *QuadValues[] = {
50 "no", "yes", "ask-no", "ask-yes", NULL,
51};
52
56static int quad_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; QuadValues[i]; i++)
64 {
65 if (mutt_istr_equal(QuadValues[i], value))
66 {
67 num = i;
68 break;
69 }
70 }
71
72 if (num < 0)
73 {
74 mutt_buffer_printf(err, _("Invalid quad value: %s"), value);
76 }
77
78 if (var)
79 {
80 if (num == (*(char *) 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 *(char *) var = num;
92 }
93 else
94 {
95 cdef->initial = num;
96 }
97
98 return CSR_SUCCESS;
99}
100
104static int quad_string_get(const struct ConfigSet *cs, void *var,
105 const struct ConfigDef *cdef, struct Buffer *result)
106{
107 unsigned int value;
108
109 if (var)
110 value = *(char *) var;
111 else
112 value = (int) cdef->initial;
113
114 if (value >= (mutt_array_size(QuadValues) - 1))
115 {
116 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value); /* LCOV_EXCL_LINE */
117 return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
118 }
119
120 mutt_buffer_addstr(result, QuadValues[value]);
121 return CSR_SUCCESS;
122}
123
127static int quad_native_set(const struct ConfigSet *cs, void *var,
128 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
129{
130 if ((value < 0) || (value >= (mutt_array_size(QuadValues) - 1)))
131 {
132 mutt_buffer_printf(err, _("Invalid quad value: %ld"), value);
134 }
135
136 if (value == (*(char *) var))
138
139 if (cdef->validator)
140 {
141 int rc = cdef->validator(cs, cdef, value, err);
142
143 if (CSR_RESULT(rc) != CSR_SUCCESS)
144 return rc | CSR_INV_VALIDATOR;
145 }
146
147 *(char *) var = value;
148 return CSR_SUCCESS;
149}
150
154static intptr_t quad_native_get(const struct ConfigSet *cs, void *var,
155 const struct ConfigDef *cdef, struct Buffer *err)
156{
157 return *(char *) var;
158}
159
163static int quad_reset(const struct ConfigSet *cs, void *var,
164 const struct ConfigDef *cdef, struct Buffer *err)
165{
166 if (cdef->initial == (*(char *) var))
168
169 if (cdef->validator)
170 {
171 int rc = cdef->validator(cs, cdef, cdef->initial, err);
172
173 if (CSR_RESULT(rc) != CSR_SUCCESS)
174 return rc | CSR_INV_VALIDATOR;
175 }
176
177 *(char *) var = cdef->initial;
178 return CSR_SUCCESS;
179}
180
190static int quad_toggle(int opt)
191{
192 return opt ^= 1;
193}
194
204int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
205{
206 if (!sub || !he || !he->data)
207 return CSR_ERR_CODE;
208
209 struct HashElem *he_base = cs_get_base(he);
210 if (DTYPE(he_base->type) != DT_QUAD)
211 return CSR_ERR_CODE;
212
213 intptr_t value = cs_he_native_get(sub->cs, he, err);
214 if (value == INT_MIN)
215 return CSR_ERR_CODE;
216
217 value = quad_toggle(value);
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
235int quad_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
236{
237 struct HashElem *he = cs_subset_create_inheritance(sub, name);
238
239 return quad_he_toggle(sub, he, err);
240}
241
245const struct ConfigSetType CstQuad = {
246 DT_QUAD,
247 "quad",
252 NULL, // string_plus_equals
253 NULL, // string_minus_equals
255 NULL, // destroy
256};
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:233
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:168
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:154
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:127
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:163
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:104
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:56
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
#define mutt_array_size(x)
Definition: memory.h:36
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:819
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:204
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition: quad.c:190
int quad_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:235
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:49
const struct ConfigSetType CstQuad
Config type representing a quad-option.
Definition: quad.c:245
Type representing a quad-option.
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:189
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
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
A collection of config items.
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#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
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:44
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
String manipulation buffer.
Definition: buffer.h:34
Definition: set.h:64
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t initial
Initial value.
Definition: set.h:67
Container for lots of config items.
Definition: set.h:260
A set of inherited config items.
Definition: subset.h:47
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
The item stored in a Hash Table.
Definition: hash.h:44
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
void * data
User-supplied data.
Definition: hash.h:47
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:200
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
Subset of Config Items.
@ NT_CONFIG_SET
Config item has been set.
Definition: subset.h:62
Constants for all the config types.
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37