NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
long.c
Go to the documentation of this file.
1
35#include "config.h"
36#include <stddef.h>
37#include <stdint.h>
38#include "mutt/lib.h"
39#include "set.h"
40#include "types.h"
41
45static int long_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
46 const char *value, struct Buffer *err)
47{
48 if (!value || (value[0] == '\0'))
49 {
50 buf_printf(err, _("Option %s may not be empty"), cdef->name);
52 }
53
54 long num = 0;
55 if (!mutt_str_atol_full(value, &num))
56 {
57 buf_printf(err, _("Invalid long: %s"), value);
59 }
60
61 if ((num < 0) && (cdef->type & D_INTEGER_NOT_NEGATIVE))
62 {
63 buf_printf(err, _("Option %s may not be negative"), cdef->name);
65 }
66
67 if (var)
68 {
69 if (num == (*(long *) var))
71
72 if (startup_only(cdef, err))
74
75 if (cdef->validator)
76 {
77 int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
78
79 if (CSR_RESULT(rc) != CSR_SUCCESS)
80 return rc | CSR_INV_VALIDATOR;
81 }
82
83 *(long *) var = num;
84 }
85 else
86 {
87 cdef->initial = num;
88 }
89
90 return CSR_SUCCESS;
91}
92
96static int long_string_get(const struct ConfigSet *cs, void *var,
97 const struct ConfigDef *cdef, struct Buffer *result)
98{
99 int value;
100
101 if (var)
102 value = *(long *) var;
103 else
104 value = (int) cdef->initial;
105
106 buf_printf(result, "%d", value);
107 return CSR_SUCCESS;
108}
109
113static int long_native_set(const struct ConfigSet *cs, void *var,
114 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
115{
116 if ((value < 0) && (cdef->type & D_INTEGER_NOT_NEGATIVE))
117 {
118 buf_printf(err, _("Option %s may not be negative"), cdef->name);
120 }
121
122 if (value == (*(long *) var))
124
125 if (startup_only(cdef, err))
127
128 if (cdef->validator)
129 {
130 int rc = cdef->validator(cs, cdef, value, err);
131
132 if (CSR_RESULT(rc) != CSR_SUCCESS)
133 return rc | CSR_INV_VALIDATOR;
134 }
135
136 *(long *) var = value;
137 return CSR_SUCCESS;
138}
139
143static intptr_t long_native_get(const struct ConfigSet *cs, void *var,
144 const struct ConfigDef *cdef, struct Buffer *err)
145{
146 return *(long *) var;
147}
148
152static int long_string_plus_equals(const struct ConfigSet *cs, void *var,
153 const struct ConfigDef *cdef,
154 const char *value, struct Buffer *err)
155{
156 long num = 0;
157 if (!mutt_str_atol_full(value, &num))
158 {
159 buf_printf(err, _("Invalid long: %s"), NONULL(value));
161 }
162
163 long result = *((long *) var) + num;
164 if ((result < 0) && (cdef->type & D_INTEGER_NOT_NEGATIVE))
165 {
166 buf_printf(err, _("Option %s may not be negative"), cdef->name);
168 }
169
170 if (result == (*(long *) var))
172
173 if (startup_only(cdef, err))
175
176 if (cdef->validator)
177 {
178 int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
179
180 if (CSR_RESULT(rc) != CSR_SUCCESS)
181 return rc | CSR_INV_VALIDATOR;
182 }
183
184 *(long *) var = result;
185 return CSR_SUCCESS;
186}
187
191static int long_string_minus_equals(const struct ConfigSet *cs, void *var,
192 const struct ConfigDef *cdef,
193 const char *value, struct Buffer *err)
194{
195 long num = 0;
196 if (!mutt_str_atol_full(value, &num))
197 {
198 buf_printf(err, _("Invalid long: %s"), NONULL(value));
200 }
201
202 long result = *((long *) var) - num;
203 if ((result < 0) && (cdef->type & D_INTEGER_NOT_NEGATIVE))
204 {
205 buf_printf(err, _("Option %s may not be negative"), cdef->name);
207 }
208
209 if (result == (*(long *) var))
211
212 if (startup_only(cdef, err))
214
215 if (cdef->validator)
216 {
217 int rc = cdef->validator(cs, cdef, (intptr_t) result, err);
218
219 if (CSR_RESULT(rc) != CSR_SUCCESS)
220 return rc | CSR_INV_VALIDATOR;
221 }
222
223 *(long *) var = result;
224 return CSR_SUCCESS;
225}
226
230static int long_reset(const struct ConfigSet *cs, void *var,
231 const struct ConfigDef *cdef, struct Buffer *err)
232{
233 if (cdef->initial == (*(long *) var))
235
236 if (startup_only(cdef, err))
238
239 if (cdef->validator)
240 {
241 int rc = cdef->validator(cs, cdef, cdef->initial, err);
242
243 if (CSR_RESULT(rc) != CSR_SUCCESS)
244 return rc | CSR_INV_VALIDATOR;
245 }
246
247 *(long *) var = cdef->initial;
248 return CSR_SUCCESS;
249}
250
254const struct ConfigSetType CstLong = {
255 DT_LONG,
256 "long",
264 NULL, // destroy
265};
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
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_RESULT(x)
Definition: set.h:52
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
static intptr_t long_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a long from a Long config item - Implements ConfigSetType::native_get() -.
Definition: long.c:143
static int long_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Long config item by long - Implements ConfigSetType::native_set() -.
Definition: long.c:113
static int long_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Long to its initial value - Implements ConfigSetType::reset() -.
Definition: long.c:230
static int long_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Long as a string - Implements ConfigSetType::string_get() -.
Definition: long.c:96
static int long_string_minus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Subtract from a Long by string - Implements ConfigSetType::string_minus_equals() -.
Definition: long.c:191
static int long_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to a Long by string - Implements ConfigSetType::string_plus_equals() -.
Definition: long.c:152
static int long_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Long by string - Implements ConfigSetType::string_set() -.
Definition: long.c:45
const struct ConfigSetType CstLong
Config type representing a long.
Definition: long.c:254
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
Parse the 'set' command.
#define NONULL(x)
Definition: string2.h:37
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:64
const char * name
User-visible name.
Definition: set.h:65
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t initial
Initial value.
Definition: set.h:67
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
Container for lots of config items.
Definition: set.h:252
Constants for all the config types.
@ DT_LONG
a number (long)
Definition: types.h:36
#define D_INTEGER_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:101