NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
enum.c
Go to the documentation of this file.
1
22
32
33#include "config.h"
34#include <stdbool.h>
35#include <stddef.h>
36#include <stdint.h>
37#include "mutt/lib.h"
38#include "enum.h"
39#include "set.h"
40#include "types.h"
41
45static int enum_string_set(void *var, struct ConfigDef *cdef, const char *value,
46 struct Buffer *err)
47{
48 if (!value)
49 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
50
51 struct EnumDef *ed = (struct EnumDef *) cdef->data;
52 if (!ed || !ed->lookup)
53 return CSR_ERR_CODE;
54
55 int num = mutt_map_get_value(value, ed->lookup);
56 if (num < 0)
57 {
58 buf_printf(err, _("Invalid value for %s"), cdef->name);
59 buf_addch(err, '\n');
60 struct Buffer *list = buf_pool_get();
61 for (int i = 0; i < ed->count; i++)
62 {
63 if (i > 0)
64 buf_addstr(list, ", ");
65 buf_addstr(list, ed->lookup[i].name);
66 }
67 buf_add_printf(err, _("Choose from: %s"), buf_string(list));
68 buf_pool_release(&list);
70 }
71
72 if (var)
73 {
74 if (num == (*(unsigned char *) var))
76
77 if (startup_only(cdef, err))
79
80 if (cdef->validator)
81 {
82 int rc = cdef->validator(cdef, (intptr_t) num, err);
83
84 if (CSR_RESULT(rc) != CSR_SUCCESS)
85 return rc | CSR_INV_VALIDATOR;
86 }
87
88 *(unsigned char *) var = num;
89 }
90 else
91 {
92 cdef->initial = num;
93 }
94
95 return CSR_SUCCESS;
96}
97
101static int enum_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
102{
103 unsigned int value;
104
105 if (var)
106 value = *(unsigned char *) var;
107 else
108 value = (int) cdef->initial;
109
110 struct EnumDef *ed = (struct EnumDef *) cdef->data;
111 if (!ed || !ed->lookup)
112 return CSR_ERR_CODE;
113
114 const char *name = mutt_map_get_name(value, ed->lookup);
115 if (!name)
116 {
117 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value);
119 }
120
121 buf_addstr(result, name);
122 return CSR_SUCCESS;
123}
124
128static int enum_native_set(void *var, const struct ConfigDef *cdef,
129 intptr_t value, struct Buffer *err)
130{
131 struct EnumDef *ed = (struct EnumDef *) cdef->data;
132 if (!ed || !ed->lookup)
133 return CSR_ERR_CODE;
134
135 const char *name = mutt_map_get_name(value, ed->lookup);
136 if (!name)
137 {
138 buf_printf(err, _("Invalid enum value: %ld"), (long) value);
140 }
141
142 if (value == (*(unsigned char *) var))
144
145 if (startup_only(cdef, err))
147
148 if (cdef->validator)
149 {
150 int rc = cdef->validator(cdef, value, err);
151
152 if (CSR_RESULT(rc) != CSR_SUCCESS)
153 return rc | CSR_INV_VALIDATOR;
154 }
155
156 *(unsigned char *) var = value;
157 return CSR_SUCCESS;
158}
159
163static intptr_t enum_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
164{
165 return *(unsigned char *) var;
166}
167
171static bool enum_has_been_set(void *var, const struct ConfigDef *cdef)
172{
173 return (cdef->initial != (*(unsigned char *) var));
174}
175
179static int enum_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
180{
181 if (cdef->initial == (*(unsigned char *) var))
183
184 if (startup_only(cdef, err))
186
187 if (cdef->validator)
188 {
189 int rc = cdef->validator(cdef, cdef->initial, err);
190
191 if (CSR_RESULT(rc) != CSR_SUCCESS)
192 return rc | CSR_INV_VALIDATOR;
193 }
194
195 *(unsigned char *) var = cdef->initial;
196 return CSR_SUCCESS;
197}
198
202const struct ConfigSetType CstEnum = {
203 DT_ENUM,
204 "enum",
209 NULL, // string_plus_equals
210 NULL, // string_minus_equals
213 NULL, // destroy
214};
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
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
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
const struct ConfigSetType CstEnum
Config type representing an enumeration.
Definition enum.c:202
Type representing an enumeration.
static bool enum_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value?
Definition enum.c:171
static intptr_t enum_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int object from an Enumeration config item - Implements ConfigSetType::native_get() -.
Definition enum.c:163
static int enum_native_set(void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set an Enumeration config item by int - Implements ConfigSetType::native_set() -.
Definition enum.c:128
static int enum_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an Enumeration to its initial value - Implements ConfigSetType::reset() -.
Definition enum.c:179
static int enum_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get an Enumeration as a string - Implements ConfigSetType::string_get() -.
Definition enum.c:101
static int enum_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set an Enumeration by string - Implements ConfigSetType::string_set() -.
Definition enum.c:45
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
int mutt_map_get_value(const char *name, const struct Mapping *map)
Lookup the constant for a string.
Definition mapping.c:85
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition mapping.c:42
Convenience wrapper for the library headers.
#define _(a)
Definition message.h:28
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
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 data
Extra variable data.
Definition set.h:69
intptr_t initial
Initial value.
Definition set.h:68
An enumeration.
Definition enum.h:30
int count
Number of documented options.
Definition enum.h:32
const char * name
Config variable.
Definition enum.h:31
struct Mapping * lookup
Lookup table.
Definition enum.h:33
const char * name
String value.
Definition mapping.h:34
Constants for all the config types.
@ DT_ENUM
an enumeration
Definition types.h:33