NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
bool.c
Go to the documentation of this file.
1
23
34
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 value for %s"), cdef->name);
77 buf_addch(err, '\n');
78 buf_add_printf(err, _("Choose from: %s"), "no, yes");
80 }
81
82 if (var)
83 {
84 if (num == (*(bool *) var))
86
87 if (startup_only(cdef, err))
89
90 if (cdef->validator)
91 {
92 int rc = cdef->validator(cdef, (intptr_t) num, err);
93
94 if (CSR_RESULT(rc) != CSR_SUCCESS)
95 return rc | CSR_INV_VALIDATOR;
96 }
97
98 *(bool *) var = num;
99 }
100 else
101 {
102 cdef->initial = num;
103 }
104
105 return CSR_SUCCESS;
106}
107
111static int bool_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
112{
113 int index;
114
115 if (var)
116 index = *(bool *) var;
117 else
118 index = (int) cdef->initial;
119
120 if (index > 1)
121 return CSR_ERR_INVALID | CSR_INV_TYPE; /* LCOV_EXCL_LINE */
122
123 buf_addstr(result, BoolValues[index]);
124 return CSR_SUCCESS;
125}
126
130static int bool_native_set(void *var, const struct ConfigDef *cdef,
131 intptr_t value, struct Buffer *err)
132{
133 if ((value < 0) || (value > 1))
134 {
135 buf_printf(err, _("Invalid boolean value: %ld"), (long) value);
137 }
138
139 if (value == (*(bool *) var))
141
142 if (startup_only(cdef, err))
144
145 if (cdef->validator)
146 {
147 int rc = cdef->validator(cdef, value, err);
148
149 if (CSR_RESULT(rc) != CSR_SUCCESS)
150 return rc | CSR_INV_VALIDATOR;
151 }
152
153 *(bool *) var = value;
154 return CSR_SUCCESS;
155}
156
160static intptr_t bool_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
161{
162 return *(bool *) var;
163}
164
168static bool bool_has_been_set(void *var, const struct ConfigDef *cdef)
169{
170 return (cdef->initial != (*(bool *) var));
171}
172
176static int bool_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
177{
178 if (cdef->initial == (*(bool *) var))
180
181 if (startup_only(cdef, err))
183
184 if (cdef->validator)
185 {
186 int rc = cdef->validator(cdef, cdef->initial, err);
187
188 if (CSR_RESULT(rc) != CSR_SUCCESS)
189 return rc | CSR_INV_VALIDATOR;
190 }
191
192 *(bool *) var = cdef->initial;
193 return CSR_SUCCESS;
194}
195
203int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
204{
205 if (!sub || !he || !he->data)
206 return CSR_ERR_CODE;
207
208 struct HashElem *he_base = cs_get_base(he);
209 if (CONFIG_TYPE(he_base->type) != DT_BOOL)
210 return CSR_ERR_CODE;
211
212 intptr_t value = cs_he_native_get(sub->cs, he, err);
213 if (value == INT_MIN)
214 return CSR_ERR_CODE;
215
216 int rc = cs_he_native_set(sub->cs, he, !value, err);
217
218 if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
220
221 return rc;
222}
223
231int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
232{
233 struct HashElem *he = cs_subset_create_inheritance(sub, name);
234
235 return bool_he_toggle(sub, he, err);
236}
237
241const struct ConfigSetType CstBool = {
242 DT_BOOL,
243 "boolean",
248 NULL, // string_plus_equals
249 NULL, // string_minus_equals
252 NULL, // destroy
253};
const struct ConfigSetType CstBool
Config type representing an boolean.
Definition bool.c:241
int bool_str_toggle(struct ConfigSubset *sub, const char *name, struct Buffer *err)
Toggle the value of a bool.
Definition bool.c:231
int bool_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a bool.
Definition bool.c:203
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
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
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: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:296
#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_INV_WARNING
Report as a warning, not an error.
Definition set.h:48
#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:53
#define CSR_SUCCESS
Action completed successfully.
Definition set.h:33
static bool bool_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value?
Definition bool.c:168
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:160
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:130
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:176
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:111
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:677
String manipulation buffer.
Definition buffer.h:36
const char * name
User-visible name.
Definition set.h:66
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition set.h:82
intptr_t initial
Initial value.
Definition set.h:68
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:214
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_BOOL
boolean option
Definition types.h:32