NeoMutt  2024-11-14-34-g5aaf0d
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 <stddef.h>
39#include <stdint.h>
40#include "mutt/lib.h"
41#include "set.h"
42#include "types.h"
43
47static void string_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
48{
49 const char **str = (const char **) var;
50 if (!*str)
51 return;
52
53 FREE(var);
54}
55
59static int string_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
60 const char *value, struct Buffer *err)
61{
62 /* Store empty strings as NULL */
63 if (value && (value[0] == '\0'))
64 value = NULL;
65
66 if (!value && (cdef->type & D_NOT_EMPTY))
67 {
68 buf_printf(err, _("Option %s may not be empty"), cdef->name);
70 }
71
72 int rc = CSR_SUCCESS;
73
74 if (var)
75 {
76 if (mutt_str_equal(value, (*(char **) var)))
78
79 if (startup_only(cdef, err))
81
82 if (cdef->validator)
83 {
84 rc = cdef->validator(cs, cdef, (intptr_t) value, err);
85
86 if (CSR_RESULT(rc) != CSR_SUCCESS)
87 return rc | CSR_INV_VALIDATOR;
88 }
89
90 string_destroy(cs, var, cdef);
91
92 const char *str = mutt_str_dup(value);
93 if (!str)
94 rc |= CSR_SUC_EMPTY;
95
96 *(const char **) var = str;
97 }
98 else
99 {
100 if (cdef->type & D_INTERNAL_INITIAL_SET)
101 FREE(&cdef->initial);
102
104 cdef->initial = (intptr_t) mutt_str_dup(value);
105 }
106
107 return rc;
108}
109
113static int string_string_get(const struct ConfigSet *cs, void *var,
114 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(const struct ConfigSet *cs, void *var,
134 const struct ConfigDef *cdef, intptr_t value,
135 struct Buffer *err)
136{
137 const char *str = (const char *) value;
138
139 /* Store empty strings as NULL */
140 if (str && (str[0] == '\0'))
141 value = 0;
142
143 if ((value == 0) && (cdef->type & D_NOT_EMPTY))
144 {
145 buf_printf(err, _("Option %s may not be empty"), cdef->name);
147 }
148
149 if (mutt_str_equal((const char *) value, (*(char **) var)))
151
152 int rc;
153
154 if (startup_only(cdef, err))
156
157 if (cdef->validator)
158 {
159 rc = cdef->validator(cs, cdef, value, err);
160
161 if (CSR_RESULT(rc) != CSR_SUCCESS)
162 return rc | CSR_INV_VALIDATOR;
163 }
164
165 string_destroy(cs, var, cdef);
166
167 str = mutt_str_dup(str);
168 rc = CSR_SUCCESS;
169 if (!str)
170 rc |= CSR_SUC_EMPTY;
171
172 *(const char **) var = str;
173 return rc;
174}
175
179static intptr_t string_native_get(const struct ConfigSet *cs, void *var,
180 const struct ConfigDef *cdef, struct Buffer *err)
181{
182 const char *str = *(const char **) var;
183
184 return (intptr_t) str;
185}
186
190static int string_string_plus_equals(const struct ConfigSet *cs, void *var,
191 const struct ConfigDef *cdef,
192 const char *value, struct Buffer *err)
193{
194 /* Skip if the value is missing or empty string*/
195 if (!value || (value[0] == '\0'))
197
198 if (value && startup_only(cdef, err))
200
201 int rc = CSR_SUCCESS;
202
203 char *str = NULL;
204 const char **var_str = (const char **) var;
205
206 if (*var_str)
207 mutt_str_asprintf(&str, "%s%s", *var_str, value);
208 else
209 str = mutt_str_dup(value);
210
211 if (cdef->validator)
212 {
213 rc = cdef->validator(cs, cdef, (intptr_t) str, err);
214
215 if (CSR_RESULT(rc) != CSR_SUCCESS)
216 {
217 FREE(&str);
218 return rc | CSR_INV_VALIDATOR;
219 }
220 }
221
222 string_destroy(cs, var, cdef);
223 *var_str = str;
224
225 return rc;
226}
227
231static int string_reset(const struct ConfigSet *cs, void *var,
232 const struct ConfigDef *cdef, struct Buffer *err)
233{
234 int rc = CSR_SUCCESS;
235
236 const char *str = mutt_str_dup((const char *) cdef->initial);
237 if (!str)
238 rc |= CSR_SUC_EMPTY;
239
240 if (mutt_str_equal(str, (*(char **) var)))
241 {
242 FREE(&str);
243 return rc | CSR_SUC_NO_CHANGE;
244 }
245
246 if (startup_only(cdef, err))
247 {
248 FREE(&str);
250 }
251
252 if (cdef->validator)
253 {
254 rc = cdef->validator(cs, cdef, cdef->initial, err);
255
256 if (CSR_RESULT(rc) != CSR_SUCCESS)
257 {
258 FREE(&str);
259 return rc | CSR_INV_VALIDATOR;
260 }
261 }
262
263 string_destroy(cs, var, cdef);
264
265 if (!str)
266 rc |= CSR_SUC_EMPTY;
267
268 *(const char **) var = str;
269 return rc;
270}
271
275const struct ConfigSetType CstString = {
276 DT_STRING,
277 "string",
283 NULL, // string_minus_equals
286};
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_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_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
const struct ConfigSetType CstString
Config type representing a string.
Definition: string.c:275
static void string_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a String - Implements ConfigSetType::destroy() -.
Definition: string.c:47
static intptr_t string_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a String config item - Implements ConfigSetType::native_get() -.
Definition: string.c:179
static int string_native_set(const struct ConfigSet *cs, 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(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a String to its initial value - Implements ConfigSetType::reset() -.
Definition: string.c:231
static int string_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a String as a string - Implements ConfigSetType::string_get() -.
Definition: string.c:113
static int string_string_plus_equals(const struct ConfigSet *cs, 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:190
static int string_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a String by string - Implements ConfigSetType::string_set() -.
Definition: string.c:59
#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:253
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:803
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
Parse the 'set' command.
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.
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:90
@ DT_STRING
a string
Definition: types.h:45
#define D_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:80