NeoMutt  2024-11-14-34-g5aaf0d
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 <limits.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(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
46 const char *value, struct Buffer *err)
47{
48 if (!cs || !cdef || !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(cs, 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(const struct ConfigSet *cs, void *var,
92 const struct ConfigDef *cdef, struct Buffer *result)
93{
94 if (!cs || !cdef)
95 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
96
97 unsigned int value;
98
99 if (var)
100 value = *(unsigned char *) var;
101 else
102 value = (int) cdef->initial;
103
104 struct EnumDef *ed = (struct EnumDef *) cdef->data;
105 if (!ed || !ed->lookup)
106 return CSR_ERR_CODE;
107
108 const char *name = mutt_map_get_name(value, ed->lookup);
109 if (!name)
110 {
111 mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value);
113 }
114
115 buf_addstr(result, name);
116 return CSR_SUCCESS;
117}
118
122static int enum_native_set(const struct ConfigSet *cs, void *var,
123 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
124{
125 if (!cs || !var || !cdef)
126 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
127
128 struct EnumDef *ed = (struct EnumDef *) cdef->data;
129 if (!ed || !ed->lookup)
130 return CSR_ERR_CODE;
131
132 const char *name = mutt_map_get_name(value, ed->lookup);
133 if (!name)
134 {
135 buf_printf(err, _("Invalid enum value: %ld"), (long) value);
137 }
138
139 if (value == (*(unsigned char *) var))
141
142 if (startup_only(cdef, err))
144
145 if (cdef->validator)
146 {
147 int rc = cdef->validator(cs, cdef, value, err);
148
149 if (CSR_RESULT(rc) != CSR_SUCCESS)
150 return rc | CSR_INV_VALIDATOR;
151 }
152
153 *(unsigned char *) var = value;
154 return CSR_SUCCESS;
155}
156
160static intptr_t enum_native_get(const struct ConfigSet *cs, void *var,
161 const struct ConfigDef *cdef, struct Buffer *err)
162{
163 if (!cs || !var || !cdef)
164 return INT_MIN; /* LCOV_EXCL_LINE */
165
166 return *(unsigned char *) var;
167}
168
172static int enum_reset(const struct ConfigSet *cs, void *var,
173 const struct ConfigDef *cdef, struct Buffer *err)
174{
175 if (!cs || !var || !cdef)
176 return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
177
178 if (cdef->initial == (*(unsigned char *) var))
180
181 if (startup_only(cdef, err))
183
184 if (cdef->validator)
185 {
186 int rc = cdef->validator(cs, cdef, cdef->initial, err);
187
188 if (CSR_RESULT(rc) != CSR_SUCCESS)
189 return rc | CSR_INV_VALIDATOR;
190 }
191
192 *(unsigned char *) var = cdef->initial;
193 return CSR_SUCCESS;
194}
195
199const struct ConfigSetType CstEnum = {
200 DT_ENUM,
201 "enum",
206 NULL, // string_plus_equals
207 NULL, // string_minus_equals
209 NULL, // destroy
210};
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:199
Type representing an enumeration.
static intptr_t enum_native_get(const struct ConfigSet *cs, 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:160
static int enum_native_set(const struct ConfigSet *cs, 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:122
static int enum_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an Enumeration to its initial value - Implements ConfigSetType::reset() -.
Definition: enum.c:172
static int enum_string_get(const struct ConfigSet *cs, 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(const struct ConfigSet *cs, 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 ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t data
Extra variable data.
Definition: set.h:68
intptr_t initial
Initial value.
Definition: set.h:67
Container for lots of config items.
Definition: set.h:252
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