NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
quad.c
Go to the documentation of this file.
1
24
38
39#include "config.h"
40#include <limits.h>
41#include <stdbool.h>
42#include <stddef.h>
43#include <stdint.h>
44#include "mutt/lib.h"
45#include "quad.h"
46#include "set.h"
47#include "subset.h"
48#include "types.h"
49
55const char *QuadValues[] = {
56 "no", "yes", "ask-no", "ask-yes", NULL,
57};
58
62static int quad_string_set(void *var, struct ConfigDef *cdef, const char *value,
63 struct Buffer *err)
64{
65 if (!value)
66 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
67
68 int num = -1;
69 for (size_t i = 0; QuadValues[i]; i++)
70 {
71 if (mutt_istr_equal(QuadValues[i], value))
72 {
73 num = i;
74 break;
75 }
76 }
77
78 if (num < 0)
79 {
80 buf_printf(err, _("Invalid quad value: %s"), value);
82 }
83
84 if (var)
85 {
86 if (num == (*(char *) var))
88
89 if (startup_only(cdef, err))
91
92 if (cdef->validator)
93 {
94 int rc = cdef->validator(cdef, (intptr_t) num, err);
95
96 if (CSR_RESULT(rc) != CSR_SUCCESS)
97 return rc | CSR_INV_VALIDATOR;
98 }
99
100 *(char *) var = num;
101 }
102 else
103 {
104 cdef->initial = num;
105 }
106
107 return CSR_SUCCESS;
108}
109
113static int quad_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
114{
115 unsigned int value;
116
117 if (var)
118 value = *(char *) var;
119 else
120 value = (int) cdef->initial;
121
122 if (value >= (countof(QuadValues) - 1))
123 {
124 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value); /* LCOV_EXCL_LINE */
125 return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
126 }
127
128 buf_addstr(result, QuadValues[value]);
129 return CSR_SUCCESS;
130}
131
135static int quad_native_set(void *var, const struct ConfigDef *cdef,
136 intptr_t value, struct Buffer *err)
137{
138 if ((value < 0) || (value >= (countof(QuadValues) - 1)))
139 {
140 buf_printf(err, _("Invalid quad value: %ld"), (long) value);
142 }
143
144 if (value == (*(char *) var))
146
147 if (startup_only(cdef, err))
149
150 if (cdef->validator)
151 {
152 int rc = cdef->validator(cdef, value, err);
153
154 if (CSR_RESULT(rc) != CSR_SUCCESS)
155 return rc | CSR_INV_VALIDATOR;
156 }
157
158 *(char *) var = value;
159 return CSR_SUCCESS;
160}
161
165static intptr_t quad_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
166{
167 return *(char *) var;
168}
169
173static bool quad_has_been_set(void *var, const struct ConfigDef *cdef)
174{
175 return (cdef->initial != (*(char *) var));
176}
177
181static int quad_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
182{
183 if (cdef->initial == (*(char *) var))
185
186 if (startup_only(cdef, err))
188
189 if (cdef->validator)
190 {
191 int rc = cdef->validator(cdef, cdef->initial, err);
192
193 if (CSR_RESULT(rc) != CSR_SUCCESS)
194 return rc | CSR_INV_VALIDATOR;
195 }
196
197 *(char *) var = cdef->initial;
198 return CSR_SUCCESS;
199}
200
210static int quad_toggle(int opt)
211{
212 return opt ^ 1;
213}
214
224int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
225{
226 if (!sub || !he || !he->data)
227 return CSR_ERR_CODE;
228
229 struct HashElem *he_base = cs_get_base(he);
230 if (CONFIG_TYPE(he_base->type) != DT_QUAD)
231 return CSR_ERR_CODE;
232
233 intptr_t value = cs_he_native_get(sub->cs, he, err);
234 if (value == INT_MIN)
235 return CSR_ERR_CODE;
236
237 value = quad_toggle(value);
238 int rc = cs_he_native_set(sub->cs, he, value, err);
239
240 if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
242
243 return rc;
244}
245
249const struct ConfigSetType CstQuad = {
250 DT_QUAD,
251 "quad",
256 NULL, // string_plus_equals
257 NULL, // string_minus_equals
260 NULL, // destroy
261};
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
const struct ConfigSetType CstQuad
Config type representing a quad-option.
Definition quad.c:249
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition set.c:161
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:737
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:842
A collection of config items.
static bool startup_only(const struct ConfigDef *cdef, struct Buffer *err)
Validator function for D_ON_STARTUP.
Definition set.h:295
#define CSR_ERR_INVALID
Value hasn't been set.
Definition set.h:36
#define CSR_INV_TYPE
Value is not valid for the type.
Definition set.h:45
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition set.h:46
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition set.h:42
#define CSR_ERR_CODE
Problem with the code.
Definition set.h:34
#define CSR_RESULT(x)
Extract the result code from CSR_* flags.
Definition set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition set.h:33
static bool quad_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value?
Definition quad.c:173
static intptr_t quad_native_get(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:165
static int quad_native_set(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:135
static int quad_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Quad-option to its initial value - Implements ConfigSetType::reset() -.
Definition quad.c:181
static int quad_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Quad-option as a string - Implements ConfigSetType::string_get() -.
Definition quad.c:113
static int quad_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Quad-option by string - Implements ConfigSetType::string_set() -.
Definition quad.c:62
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
#define countof(x)
Definition memory.h:49
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:674
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition quad.c:224
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition quad.c:210
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition quad.c:55
Type representing a quad-option.
String manipulation buffer.
Definition buffer.h:36
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition set.h:81
intptr_t initial
Initial value.
Definition set.h:67
A set of inherited config items.
Definition subset.h:46
struct ConfigSet * cs
Parent ConfigSet.
Definition subset.h:50
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
void cs_subset_notify_observers(const struct ConfigSubset *sub, struct HashElem *he, enum NotifyConfig ev)
Notify all observers of an event.
Definition subset.c:243
Subset of Config Items.
@ NT_CONFIG_SET
Config item has been set.
Definition subset.h:61
Constants for all the config types.
#define CONFIG_TYPE(t)
Extract the type from the flags.
Definition types.h:50
@ DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition types.h:40