NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
string.c
Go to the documentation of this file.
1
37#include "config.h"
38#include <stdbool.h>
39#include <stddef.h>
40#include <stdint.h>
41#include "mutt/lib.h"
42#include "set.h"
43#include "types.h"
44
48static void string_destroy(void *var, const struct ConfigDef *cdef)
49{
50 const char **str = (const char **) var;
51 if (!*str)
52 return;
53
54 FREE(var);
55}
56
60static int string_string_set(void *var, struct ConfigDef *cdef,
61 const char *value, struct Buffer *err)
62{
63 /* Store empty strings as NULL */
64 if (value && (value[0] == '\0'))
65 value = NULL;
66
67 if (!value && (cdef->type & D_NOT_EMPTY))
68 {
69 buf_printf(err, _("Option %s may not be empty"), cdef->name);
71 }
72
73 int rc = CSR_SUCCESS;
74
75 if (var)
76 {
77 if (mutt_str_equal(value, (*(char **) var)))
79
80 if (startup_only(cdef, err))
82
83 if (cdef->validator)
84 {
85 rc = cdef->validator(cdef, (intptr_t) value, err);
86
87 if (CSR_RESULT(rc) != CSR_SUCCESS)
88 return rc | CSR_INV_VALIDATOR;
89 }
90
91 string_destroy(var, cdef);
92
93 const char *str = mutt_str_dup(value);
94 if (!str)
95 rc |= CSR_SUC_EMPTY;
96
97 *(const char **) var = str;
98 }
99 else
100 {
101 if (cdef->type & D_INTERNAL_INITIAL_SET)
102 FREE(&cdef->initial);
103
105 cdef->initial = (intptr_t) mutt_str_dup(value);
106 }
107
108 return rc;
109}
110
114static int string_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
115{
116 const char *str = NULL;
117
118 if (var)
119 str = *(const char **) var;
120 else
121 str = (char *) cdef->initial;
122
123 if (!str)
124 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty string */
125
126 buf_addstr(result, str);
127 return CSR_SUCCESS;
128}
129
133static int string_native_set(void *var, const struct ConfigDef *cdef,
134 intptr_t value, struct Buffer *err)
135{
136 const char *str = (const char *) value;
137
138 /* Store empty strings as NULL */
139 if (str && (str[0] == '\0'))
140 value = 0;
141
142 if ((value == 0) && (cdef->type & D_NOT_EMPTY))
143 {
144 buf_printf(err, _("Option %s may not be empty"), cdef->name);
146 }
147
148 if (mutt_str_equal((const char *) value, (*(char **) var)))
150
151 int rc;
152
153 if (startup_only(cdef, err))
155
156 if (cdef->validator)
157 {
158 rc = cdef->validator(cdef, value, err);
159
160 if (CSR_RESULT(rc) != CSR_SUCCESS)
161 return rc | CSR_INV_VALIDATOR;
162 }
163
164 string_destroy(var, cdef);
165
166 str = mutt_str_dup(str);
167 rc = CSR_SUCCESS;
168 if (!str)
169 rc |= CSR_SUC_EMPTY;
170
171 *(const char **) var = str;
172 return rc;
173}
174
178static intptr_t string_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
179{
180 const char *str = *(const char **) var;
181
182 return (intptr_t) str;
183}
184
188static int string_string_plus_equals(void *var, const struct ConfigDef *cdef,
189 const char *value, struct Buffer *err)
190{
191 /* Skip if the value is missing or empty string*/
192 if (!value || (value[0] == '\0'))
194
195 if (value && startup_only(cdef, err))
197
198 int rc = CSR_SUCCESS;
199
200 char *str = NULL;
201 const char **var_str = (const char **) var;
202
203 if (*var_str)
204 mutt_str_asprintf(&str, "%s%s", *var_str, value);
205 else
206 str = mutt_str_dup(value);
207
208 if (cdef->validator)
209 {
210 rc = cdef->validator(cdef, (intptr_t) str, err);
211
212 if (CSR_RESULT(rc) != CSR_SUCCESS)
213 {
214 FREE(&str);
215 return rc | CSR_INV_VALIDATOR;
216 }
217 }
218
219 string_destroy(var, cdef);
220 *var_str = str;
221
222 return rc;
223}
224
228static bool string_has_been_set(void *var, const struct ConfigDef *cdef)
229{
230 const char *initial = (const char *) cdef->initial;
231 const char *value = *(const char **) var;
232
233 return !mutt_str_equal(initial, value);
234}
235
239static int string_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
240{
241 int rc = CSR_SUCCESS;
242
243 const char *str = mutt_str_dup((const char *) cdef->initial);
244 if (!str)
245 rc |= CSR_SUC_EMPTY;
246
247 if (mutt_str_equal(str, (*(char **) var)))
248 {
249 FREE(&str);
250 return rc | CSR_SUC_NO_CHANGE;
251 }
252
253 if (startup_only(cdef, err))
254 {
255 FREE(&str);
257 }
258
259 if (cdef->validator)
260 {
261 rc = cdef->validator(cdef, cdef->initial, err);
262
263 if (CSR_RESULT(rc) != CSR_SUCCESS)
264 {
265 FREE(&str);
266 return rc | CSR_INV_VALIDATOR;
267 }
268 }
269
270 string_destroy(var, cdef);
271
272 if (!str)
273 rc |= CSR_SUC_EMPTY;
274
275 *(const char **) var = str;
276 return rc;
277}
278
282const struct ConfigSetType CstString = {
283 DT_STRING,
284 "string",
290 NULL, // string_minus_equals
294};
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:293
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:36
#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_SUC_EMPTY
Value is empty/unset.
Definition: set.h:40
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:33
const struct ConfigSetType CstString
Config type representing a string.
Definition: string.c:282
static void string_destroy(void *var, const struct ConfigDef *cdef)
Destroy a String - Implements ConfigSetType::destroy() -.
Definition: string.c:48
static bool string_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value? - Implements ConfigSetType::has_been_set() -.
Definition: string.c:228
static intptr_t string_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a String config item - Implements ConfigSetType::native_get() -.
Definition: string.c:178
static int string_native_set(void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a String config item by string - Implements ConfigSetType::native_set() -.
Definition: string.c:133
static int string_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a String to its initial value - Implements ConfigSetType::reset() -.
Definition: string.c:239
static int string_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a String as a string - Implements ConfigSetType::string_get() -.
Definition: string.c:114
static int string_string_plus_equals(void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to a String by string - Implements ConfigSetType::string_plus_equals() -.
Definition: string.c:188
static int string_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a String by string - Implements ConfigSetType::string_set() -.
Definition: string.c:60
#define FREE(x)
Definition: memory.h:55
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:254
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:804
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:661
Parse the 'set' command.
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.
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:89
@ DT_STRING
a string
Definition: types.h:44
#define D_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:79