NeoMutt  2025-01-09-156-g99fdbd
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 <stdbool.h>
37#include <stddef.h>
38#include <stdint.h>
39#include "mutt/lib.h"
40#include "set.h"
41#include "types.h"
42
46static int long_string_set(void *var, struct ConfigDef *cdef, const char *value,
47 struct Buffer *err)
48{
49 if (!value || (value[0] == '\0'))
50 {
51 buf_printf(err, _("Option %s may not be empty"), cdef->name);
53 }
54
55 long num = 0;
56 if (!mutt_str_atol_full(value, &num))
57 {
58 buf_printf(err, _("Invalid long: %s"), value);
60 }
61
62 if ((num < 0) && (cdef->type & D_INTEGER_NOT_NEGATIVE))
63 {
64 buf_printf(err, _("Option %s may not be negative"), cdef->name);
66 }
67
68 if (var)
69 {
70 if (num == (*(long *) var))
72
73 if (startup_only(cdef, err))
75
76 if (cdef->validator)
77 {
78 int rc = cdef->validator(cdef, (intptr_t) num, err);
79
80 if (CSR_RESULT(rc) != CSR_SUCCESS)
81 return rc | CSR_INV_VALIDATOR;
82 }
83
84 *(long *) var = num;
85 }
86 else
87 {
88 cdef->initial = num;
89 }
90
91 return CSR_SUCCESS;
92}
93
97static int long_string_get(void *var, 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(void *var, const struct ConfigDef *cdef,
114 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(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(void *var, const struct ConfigDef *cdef, struct Buffer *err)
144{
145 return *(long *) var;
146}
147
151static int long_string_plus_equals(void *var, const struct ConfigDef *cdef,
152 const char *value, struct Buffer *err)
153{
154 long num = 0;
155 if (!mutt_str_atol_full(value, &num))
156 {
157 buf_printf(err, _("Invalid long: %s"), NONULL(value));
159 }
160
161 long result = *((long *) var) + num;
162 if ((result < 0) && (cdef->type & D_INTEGER_NOT_NEGATIVE))
163 {
164 buf_printf(err, _("Option %s may not be negative"), cdef->name);
166 }
167
168 if (result == (*(long *) var))
170
171 if (startup_only(cdef, err))
173
174 if (cdef->validator)
175 {
176 int rc = cdef->validator(cdef, (intptr_t) result, err);
177
178 if (CSR_RESULT(rc) != CSR_SUCCESS)
179 return rc | CSR_INV_VALIDATOR;
180 }
181
182 *(long *) var = result;
183 return CSR_SUCCESS;
184}
185
189static int long_string_minus_equals(void *var, const struct ConfigDef *cdef,
190 const char *value, struct Buffer *err)
191{
192 long num = 0;
193 if (!mutt_str_atol_full(value, &num))
194 {
195 buf_printf(err, _("Invalid long: %s"), NONULL(value));
197 }
198
199 long result = *((long *) var) - num;
200 if ((result < 0) && (cdef->type & D_INTEGER_NOT_NEGATIVE))
201 {
202 buf_printf(err, _("Option %s may not be negative"), cdef->name);
204 }
205
206 if (result == (*(long *) var))
208
209 if (startup_only(cdef, err))
211
212 if (cdef->validator)
213 {
214 int rc = cdef->validator(cdef, (intptr_t) result, err);
215
216 if (CSR_RESULT(rc) != CSR_SUCCESS)
217 return rc | CSR_INV_VALIDATOR;
218 }
219
220 *(long *) var = result;
221 return CSR_SUCCESS;
222}
223
227static bool long_has_been_set(void *var, const struct ConfigDef *cdef)
228{
229 return (cdef->initial != (*(long *) var));
230}
231
235static int long_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
236{
237 if (cdef->initial == (*(long *) var))
239
240 if (startup_only(cdef, err))
242
243 if (cdef->validator)
244 {
245 int rc = cdef->validator(cdef, cdef->initial, err);
246
247 if (CSR_RESULT(rc) != CSR_SUCCESS)
248 return rc | CSR_INV_VALIDATOR;
249 }
250
251 *(long *) var = cdef->initial;
252 return CSR_SUCCESS;
253}
254
258const struct ConfigSetType CstLong = {
259 DT_LONG,
260 "long",
269 NULL, // destroy
270};
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:293
#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_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:42
#define CSR_RESULT(x)
Definition: set.h:50
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:33
static bool long_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value? - Implements ConfigSetType::has_been_set() -.
Definition: long.c:227
static intptr_t long_native_get(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(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(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Long to its initial value - Implements ConfigSetType::reset() -.
Definition: long.c:235
static int long_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Long as a string - Implements ConfigSetType::string_get() -.
Definition: long.c:97
static int long_string_minus_equals(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:189
static int long_string_plus_equals(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:151
static int long_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Long by string - Implements ConfigSetType::string_set() -.
Definition: long.c:46
const struct ConfigSetType CstLong
Config type representing a long.
Definition: long.c:258
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:62
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:79
intptr_t initial
Initial value.
Definition: set.h:65
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:64
Constants for all the config types.
@ DT_LONG
a number (long)
Definition: types.h:35
#define D_INTEGER_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:100