NeoMutt  2025-01-09-81-g753ae0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
bool.c
Go to the documentation of this file.
1
35#include "config.h"
36#include <limits.h>
37#include <stdbool.h>
38#include <stddef.h>
39#include <stdint.h>
40#include "mutt/lib.h"
41#include "bool.h"
42#include "set.h"
43#include "subset.h"
44#include "types.h"
45
51const char *BoolValues[] = {
52 "no", "yes", "n", "y", "false", "true", "0", "1", "off", "on", NULL,
53};
54
58static int bool_string_set(void *var, struct ConfigDef *cdef, const char *value,
59 struct Buffer *err)
60{
61 if (!value)
62 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
63
64 int num = -1;
65 for (size_t i = 0; BoolValues[i]; i++)
66 {
67 if (mutt_istr_equal(BoolValues[i], value))
68 {
69 num = i % 2;
70 break;
71 }
72 }
73
74 if (num < 0)
75 {
76 buf_printf(err, _("Invalid boolean value: %s"), value);
78 }
79
80 if (var)
81 {
82 if (num == (*(bool *) var))
84
85 if (startup_only(cdef, err))
87
88 if (cdef->validator)
89 {
90 int rc = cdef->validator(cdef, (intptr_t) num, err);
91
92 if (CSR_RESULT(rc) != CSR_SUCCESS)
93 return rc | CSR_INV_VALIDATOR;
94 }
95
96 *(bool *) var = num;
97 }
98 else
99 {
100 cdef->initial = num;
101 }
102
103 return CSR_SUCCESS;
104}
105
109static int bool_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
110{
111 int index;
112
113 if (var)
114 index = *(bool *) var;
115 else
116 index = (int) cdef->initial;
117
118 if (index > 1)
119 return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
120
121 buf_addstr(result, BoolValues[index]);
122 return CSR_SUCCESS;
123}
124
128static int bool_native_set(void *var, const struct ConfigDef *cdef,
129 intptr_t value, struct Buffer *err)
130{
131 if ((value < 0) || (value > 1))
132 {
133 buf_printf(err, _("Invalid boolean value: %ld"), (long) value);
135 }
136
137 if (value == (*(bool *) var))
139
140 if (startup_only(cdef, err))
142
143 if (cdef->validator)
144 {
145 int rc = cdef->validator(cdef, value, err);
146
147 if (CSR_RESULT(rc) != CSR_SUCCESS)
148 return rc | CSR_INV_VALIDATOR;
149 }
150
151 *(bool *) var = value;
152 return CSR_SUCCESS;
153}
154
158static intptr_t bool_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
159{
160 return *(bool *) var;
161}
162
166static bool bool_has_been_set(void *var, const struct ConfigDef *cdef)
167{
168 return (cdef->initial != (*(bool *) var));
169}
170
174static int bool_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
175{
176 if (cdef->initial == (*(bool *) var))
178
179 if (startup_only(cdef, err))
181
182 if (cdef->validator)
183 {
184 int rc = cdef->validator(cdef, cdef->initial, err);
185
186 if (CSR_RESULT(rc) != CSR_SUCCESS)
187 return rc | CSR_INV_VALIDATOR;
188 }
189
190 *(bool *) var = cdef->initial;
191 return CSR_SUCCESS;
192}
193
201int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
202{
203 if (!sub || !he || !he->data)
204 return CSR_ERR_CODE;
205
206 struct HashElem *he_base = cs_get_base(he);
207 if (CONFIG_TYPE(he_base->type) != DT_BOOL)
208 return CSR_ERR_CODE;
209
210 intptr_t value = cs_he_native_get(sub->cs, he, err);
211 if (value == INT_MIN)
212 return CSR_ERR_CODE;
213
214 int rc = cs_he_native_set(sub->cs, he, !value, err);
215
216 if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
218
219 return rc;
220}
221
229int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
230{
231 struct HashElem *he = cs_subset_create_inheritance(sub, name);
232
233 return bool_he_toggle(sub, he, err);
234}
235
239const struct ConfigSetType CstBool = {
240 DT_BOOL,
241 "boolean",
246 NULL, // string_plus_equals
247 NULL, // string_minus_equals
250 NULL, // destroy
251};
const struct ConfigSetType CstBool
Config type representing an boolean.
Definition: bool.c:239
int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:229
int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a bool.
Definition: bool.c:201
const char * BoolValues[]
Valid strings for creating a Bool.
Definition: bool.c:51
Type representing a boolean.
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:760
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:865
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 bool bool_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value? - Implements ConfigSetType::has_been_set() -.
Definition: bool.c:166
static intptr_t bool_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a bool from a Bool config item - Implements ConfigSetType::native_get() -.
Definition: bool.c:158
static int bool_native_set(void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Bool config item by bool - Implements ConfigSetType::native_set() -.
Definition: bool.c:128
static int bool_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Bool to its initial value - Implements ConfigSetType::reset() -.
Definition: bool.c:174
static int bool_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Bool as a string - Implements ConfigSetType::string_get() -.
Definition: bool.c:109
static int bool_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Bool by string - Implements ConfigSetType::string_set() -.
Definition: bool.c:58
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.
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:64
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
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:210
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_BOOL
boolean option
Definition: types.h:32