NeoMutt  2025-01-09-81-g753ae0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
enum.c
Go to the documentation of this file.
1
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 enum value: %s"), value);
60 }
61
62 if (var)
63 {
64 if (num == (*(unsigned char *) var))
66
67 if (startup_only(cdef, err))
69
70 if (cdef->validator)
71 {
72 int rc = cdef->validator(cdef, (intptr_t) num, err);
73
74 if (CSR_RESULT(rc) != CSR_SUCCESS)
75 return rc | CSR_INV_VALIDATOR;
76 }
77
78 *(unsigned char *) var = num;
79 }
80 else
81 {
82 cdef->initial = num;
83 }
84
85 return CSR_SUCCESS;
86}
87
91static int enum_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
92{
93 unsigned int value;
94
95 if (var)
96 value = *(unsigned char *) var;
97 else
98 value = (int) cdef->initial;
99
100 struct EnumDef *ed = (struct EnumDef *) cdef->data;
101 if (!ed || !ed->lookup)
102 return CSR_ERR_CODE;
103
104 const char *name = mutt_map_get_name(value, ed->lookup);
105 if (!name)
106 {
107 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value);
109 }
110
111 buf_addstr(result, name);
112 return CSR_SUCCESS;
113}
114
118static int enum_native_set(void *var, const struct ConfigDef *cdef,
119 intptr_t value, struct Buffer *err)
120{
121 struct EnumDef *ed = (struct EnumDef *) cdef->data;
122 if (!ed || !ed->lookup)
123 return CSR_ERR_CODE;
124
125 const char *name = mutt_map_get_name(value, ed->lookup);
126 if (!name)
127 {
128 buf_printf(err, _("Invalid enum value: %ld"), (long) value);
130 }
131
132 if (value == (*(unsigned char *) var))
134
135 if (startup_only(cdef, err))
137
138 if (cdef->validator)
139 {
140 int rc = cdef->validator(cdef, value, err);
141
142 if (CSR_RESULT(rc) != CSR_SUCCESS)
143 return rc | CSR_INV_VALIDATOR;
144 }
145
146 *(unsigned char *) var = value;
147 return CSR_SUCCESS;
148}
149
153static intptr_t enum_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
154{
155 return *(unsigned char *) var;
156}
157
161static bool enum_has_been_set(void *var, const struct ConfigDef *cdef)
162{
163 return (cdef->initial != (*(unsigned char *) var));
164}
165
169static int enum_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
170{
171 if (cdef->initial == (*(unsigned char *) var))
173
174 if (startup_only(cdef, err))
176
177 if (cdef->validator)
178 {
179 int rc = cdef->validator(cdef, cdef->initial, err);
180
181 if (CSR_RESULT(rc) != CSR_SUCCESS)
182 return rc | CSR_INV_VALIDATOR;
183 }
184
185 *(unsigned char *) var = cdef->initial;
186 return CSR_SUCCESS;
187}
188
192const struct ConfigSetType CstEnum = {
193 DT_ENUM,
194 "enum",
199 NULL, // string_plus_equals
200 NULL, // string_minus_equals
203 NULL, // destroy
204};
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
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
const struct ConfigSetType CstEnum
Config type representing an enumeration.
Definition: enum.c:192
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? - Implements ConfigSetType::has_been_set() -.
Definition: enum.c:161
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:153
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:118
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:169
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:91
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:89
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
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
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 data
Extra variable data.
Definition: set.h:68
intptr_t initial
Initial value.
Definition: set.h:67
An enumeration.
Definition: enum.h:30
const char * name
Config variable.
Definition: enum.h:31
struct Mapping * lookup
Lookup table.
Definition: enum.h:33
Constants for all the config types.
@ DT_ENUM
an enumeration
Definition: types.h:33