NeoMutt  2025-01-09-117-gace867
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 <stdbool.h>
41#include <stddef.h>
42#include <stdint.h>
43#include "mutt/lib.h"
44#include "quad.h"
45#include "set.h"
46#include "subset.h"
47#include "types.h"
48
54const char *QuadValues[] = {
55 "no", "yes", "ask-no", "ask-yes", NULL,
56};
57
61static int quad_string_set(void *var, struct ConfigDef *cdef, const char *value,
62 struct Buffer *err)
63{
64 if (!value)
65 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
66
67 int num = -1;
68 for (size_t i = 0; QuadValues[i]; i++)
69 {
70 if (mutt_istr_equal(QuadValues[i], value))
71 {
72 num = i;
73 break;
74 }
75 }
76
77 if (num < 0)
78 {
79 buf_printf(err, _("Invalid quad value: %s"), value);
81 }
82
83 if (var)
84 {
85 if (num == (*(char *) var))
87
88 if (startup_only(cdef, err))
90
91 if (cdef->validator)
92 {
93 int rc = cdef->validator(cdef, (intptr_t) num, err);
94
95 if (CSR_RESULT(rc) != CSR_SUCCESS)
96 return rc | CSR_INV_VALIDATOR;
97 }
98
99 *(char *) var = num;
100 }
101 else
102 {
103 cdef->initial = num;
104 }
105
106 return CSR_SUCCESS;
107}
108
112static int quad_string_get(void *var, 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(void *var, const struct ConfigDef *cdef,
135 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(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(void *var, const struct ConfigDef *cdef, struct Buffer *err)
165{
166 return *(char *) var;
167}
168
172static bool quad_has_been_set(void *var, const struct ConfigDef *cdef)
173{
174 return (cdef->initial != (*(char *) var));
175}
176
180static int quad_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
181{
182 if (cdef->initial == (*(char *) var))
184
185 if (startup_only(cdef, err))
187
188 if (cdef->validator)
189 {
190 int rc = cdef->validator(cdef, cdef->initial, err);
191
192 if (CSR_RESULT(rc) != CSR_SUCCESS)
193 return rc | CSR_INV_VALIDATOR;
194 }
195
196 *(char *) var = cdef->initial;
197 return CSR_SUCCESS;
198}
199
209static int quad_toggle(int opt)
210{
211 return opt ^ 1;
212}
213
223int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
224{
225 if (!sub || !he || !he->data)
226 return CSR_ERR_CODE;
227
228 struct HashElem *he_base = cs_get_base(he);
229 if (CONFIG_TYPE(he_base->type) != DT_QUAD)
230 return CSR_ERR_CODE;
231
232 intptr_t value = cs_he_native_get(sub->cs, he, err);
233 if (value == INT_MIN)
234 return CSR_ERR_CODE;
235
236 value = quad_toggle(value);
237 int rc = cs_he_native_set(sub->cs, he, value, err);
238
239 if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
241
242 return rc;
243}
244
248const struct ConfigSetType CstQuad = {
249 DT_QUAD,
250 "quad",
255 NULL, // string_plus_equals
256 NULL, // string_minus_equals
259 NULL, // destroy
260};
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:736
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:841
static bool startup_only(const struct ConfigDef *cdef, struct Buffer *err)
Validator function for D_ON_STARTUP.
Definition: set.h:293
#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)
Definition: set.h:50
#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? - Implements ConfigSetType::has_been_set() -.
Definition: quad.c:172
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:164
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:134
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:180
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:112
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:61
#define mutt_debug(LEVEL,...)
Definition: logging2.h:90
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:44
#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:673
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:223
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition: quad.c:209
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:54
const struct ConfigSetType CstQuad
Config type representing a quad-option.
Definition: quad.c:248
Type representing a quad-option.
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:62
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:79
intptr_t initial
Initial value.
Definition: set.h:65
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:239
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)
Definition: types.h:49
@ DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:40