NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
quad.c
Go to the documentation of this file.
1
38#include "config.h"
39#include <limits.h>
40#include <stddef.h>
41#include <stdint.h>
42#include "mutt/lib.h"
43#include "quad.h"
44#include "set.h"
45#include "subset.h"
46#include "types.h"
47
53const char *QuadValues[] = {
54 "no", "yes", "ask-no", "ask-yes", NULL,
55};
56
60static int quad_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
61 const char *value, struct Buffer *err)
62{
63 if (!value)
64 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
65
66 int num = -1;
67 for (size_t i = 0; QuadValues[i]; i++)
68 {
69 if (mutt_istr_equal(QuadValues[i], value))
70 {
71 num = i;
72 break;
73 }
74 }
75
76 if (num < 0)
77 {
78 buf_printf(err, _("Invalid quad value: %s"), value);
80 }
81
82 if (var)
83 {
84 if (num == (*(char *) var))
86
87 if (startup_only(cdef, err))
89
90 if (cdef->validator)
91 {
92 int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
93
94 if (CSR_RESULT(rc) != CSR_SUCCESS)
95 return rc | CSR_INV_VALIDATOR;
96 }
97
98 *(char *) var = num;
99 }
100 else
101 {
102 cdef->initial = num;
103 }
104
105 return CSR_SUCCESS;
106}
107
111static int quad_string_get(const struct ConfigSet *cs, void *var,
112 const struct ConfigDef *cdef, struct Buffer *result)
113{
114 unsigned int value;
115
116 if (var)
117 value = *(char *) var;
118 else
119 value = (int) cdef->initial;
120
121 if (value >= (mutt_array_size(QuadValues) - 1))
122 {
123 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value); /* LCOV_EXCL_LINE */
124 return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
125 }
126
127 buf_addstr(result, QuadValues[value]);
128 return CSR_SUCCESS;
129}
130
134static int quad_native_set(const struct ConfigSet *cs, void *var,
135 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
136{
137 if ((value < 0) || (value >= (mutt_array_size(QuadValues) - 1)))
138 {
139 buf_printf(err, _("Invalid quad value: %ld"), (long) value);
141 }
142
143 if (value == (*(char *) var))
145
146 if (startup_only(cdef, err))
148
149 if (cdef->validator)
150 {
151 int rc = cdef->validator(cs, cdef, value, err);
152
153 if (CSR_RESULT(rc) != CSR_SUCCESS)
154 return rc | CSR_INV_VALIDATOR;
155 }
156
157 *(char *) var = value;
158 return CSR_SUCCESS;
159}
160
164static intptr_t quad_native_get(const struct ConfigSet *cs, void *var,
165 const struct ConfigDef *cdef, struct Buffer *err)
166{
167 return *(char *) var;
168}
169
173static int quad_reset(const struct ConfigSet *cs, void *var,
174 const struct ConfigDef *cdef, struct Buffer *err)
175{
176 if (cdef->initial == (*(char *) var))
178
179 if (startup_only(cdef, err))
181
182 if (cdef->validator)
183 {
184 int rc = cdef->validator(cs, cdef, cdef->initial, err);
185
186 if (CSR_RESULT(rc) != CSR_SUCCESS)
187 return rc | CSR_INV_VALIDATOR;
188 }
189
190 *(char *) var = cdef->initial;
191 return CSR_SUCCESS;
192}
193
203static int quad_toggle(int opt)
204{
205 return opt ^ 1;
206}
207
217int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
218{
219 if (!sub || !he || !he->data)
220 return CSR_ERR_CODE;
221
222 struct HashElem *he_base = cs_get_base(he);
223 if (DTYPE(he_base->type) != DT_QUAD)
224 return CSR_ERR_CODE;
225
226 intptr_t value = cs_he_native_get(sub->cs, he, err);
227 if (value == INT_MIN)
228 return CSR_ERR_CODE;
229
230 value = quad_toggle(value);
231 int rc = cs_he_native_set(sub->cs, he, value, err);
232
233 if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
235
236 return rc;
237}
238
242const struct ConfigSetType CstQuad = {
243 DT_QUAD,
244 "quad",
249 NULL, // string_plus_equals
250 NULL, // string_minus_equals
252 NULL, // destroy
253};
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:160
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:708
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:813
static bool startup_only(const struct ConfigDef *cdef, struct Buffer *err)
Validator function for D_ON_STARTUP.
Definition: set.h:296
#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
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:164
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:134
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:173
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:111
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:60
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
#define mutt_array_size(x)
Definition: memory.h:38
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:672
Parse the 'set' command.
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:217
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition: quad.c:203
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:53
const struct ConfigSetType CstQuad
Config type representing a quad-option.
Definition: quad.c:242
Type representing a quad-option.
String manipulation buffer.
Definition: buffer.h:36
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:252
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:43
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:44
void * data
User-supplied data.
Definition: hash.h:46
void cs_subset_notify_observers(const struct ConfigSubset *sub, struct HashElem *he, enum NotifyConfig ev)
Notify all observers of an event.
Definition: subset.c:237
Subset of Config Items.
@ NT_CONFIG_SET
Config item has been set.
Definition: subset.h:62
Constants for all the config types.
#define DTYPE(t)
Definition: types.h:50
@ DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:41