NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
path.c
Go to the documentation of this file.
1
38#include "config.h"
39#include <stdbool.h>
40#include <stddef.h>
41#include <stdint.h>
42#include "mutt/lib.h"
43#include "core/lib.h"
44#include "set.h"
45#include "types.h"
46
57static char *path_tidy(const char *path, bool is_dir)
58{
59 if (!path || (*path == '\0'))
60 return NULL;
61
62 struct Buffer *buf = buf_pool_get();
63 buf_strcpy(buf, path);
64
66 mutt_path_tidy(buf, is_dir);
67
68 char *tidy_path = buf_strdup(buf);
69 buf_pool_release(&buf);
70
71 return tidy_path;
72}
73
77static void path_destroy(void *var, const struct ConfigDef *cdef)
78{
79 const char **str = (const char **) var;
80 if (!*str)
81 return;
82
83 FREE(var);
84}
85
89static int path_string_set(void *var, struct ConfigDef *cdef, const char *value,
90 struct Buffer *err)
91{
92 /* Store empty paths as NULL */
93 if (value && (value[0] == '\0'))
94 value = NULL;
95
96 if (!value && (cdef->type & D_NOT_EMPTY))
97 {
98 buf_printf(err, _("Option %s may not be empty"), cdef->name);
100 }
101
102 int rc = CSR_SUCCESS;
103
104 if (var)
105 {
106 if (mutt_str_equal(value, (*(char **) var)))
108
109 if (startup_only(cdef, err))
111
112 if (cdef->validator)
113 {
114 rc = cdef->validator(cdef, (intptr_t) value, err);
115
116 if (CSR_RESULT(rc) != CSR_SUCCESS)
117 return rc | CSR_INV_VALIDATOR;
118 }
119
120 path_destroy(var, cdef);
121
122 const char *str = path_tidy(value, cdef->type & D_PATH_DIR);
123 if (!str)
124 rc |= CSR_SUC_EMPTY;
125
126 *(const char **) var = str;
127 }
128 else
129 {
130 if (cdef->type & D_INTERNAL_INITIAL_SET)
131 FREE(&cdef->initial);
132
134 cdef->initial = (intptr_t) mutt_str_dup(value);
135 }
136
137 return rc;
138}
139
143static int path_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
144{
145 const char *str = NULL;
146
147 if (var)
148 str = *(const char **) var;
149 else
150 str = (char *) cdef->initial;
151
152 if (!str)
153 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty path */
154
155 buf_addstr(result, str);
156 return CSR_SUCCESS;
157}
158
162static int path_native_set(void *var, const struct ConfigDef *cdef,
163 intptr_t value, struct Buffer *err)
164{
165 const char *str = (const char *) value;
166
167 /* Store empty paths as NULL */
168 if (str && (str[0] == '\0'))
169 value = 0;
170
171 if ((value == 0) && (cdef->type & D_NOT_EMPTY))
172 {
173 buf_printf(err, _("Option %s may not be empty"), cdef->name);
175 }
176
177 if (mutt_str_equal((const char *) value, (*(char **) var)))
179
180 int rc;
181
182 if (startup_only(cdef, err))
184
185 if (cdef->validator)
186 {
187 rc = cdef->validator(cdef, value, err);
188
189 if (CSR_RESULT(rc) != CSR_SUCCESS)
190 return rc | CSR_INV_VALIDATOR;
191 }
192
193 path_destroy(var, cdef);
194
195 str = path_tidy(str, cdef->type & D_PATH_DIR);
196 rc = CSR_SUCCESS;
197 if (!str)
198 rc |= CSR_SUC_EMPTY;
199
200 *(const char **) var = str;
201 return rc;
202}
203
207static intptr_t path_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
208{
209 const char *str = *(const char **) var;
210
211 return (intptr_t) str;
212}
213
217static bool path_has_been_set(void *var, const struct ConfigDef *cdef)
218{
219 const char *initial = path_tidy((const char *) cdef->initial, cdef->type & D_PATH_DIR);
220 const char *value = *(const char **) var;
221
222 bool rc = !mutt_str_equal(initial, value);
223 FREE(&initial);
224 return rc;
225}
226
230static int path_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
231{
232 int rc = CSR_SUCCESS;
233
234 const char *str = path_tidy((const char *) cdef->initial, cdef->type & D_PATH_DIR);
235 if (!str)
236 rc |= CSR_SUC_EMPTY;
237
238 if (mutt_str_equal(str, (*(char **) var)))
239 {
240 FREE(&str);
241 return rc | CSR_SUC_NO_CHANGE;
242 }
243
244 if (startup_only(cdef, err))
245 {
246 FREE(&str);
248 }
249
250 if (cdef->validator)
251 {
252 rc = cdef->validator(cdef, cdef->initial, err);
253
254 if (CSR_RESULT(rc) != CSR_SUCCESS)
255 {
256 FREE(&str);
257 return rc | CSR_INV_VALIDATOR;
258 }
259 }
260
261 path_destroy(var, cdef);
262
263 if (!str)
264 rc |= CSR_SUC_EMPTY;
265
266 *(const char **) var = str;
267 return rc;
268}
269
273const struct ConfigSetType CstPath = {
274 DT_PATH,
275 "path",
280 NULL, // string_plus_equals
281 NULL, // string_minus_equals
285};
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
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:395
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:571
const struct ConfigSetType CstPath
Config type representing a path.
Definition: path.c:273
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:57
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
Convenience wrapper for the core headers.
static void path_destroy(void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy() -.
Definition: path.c:77
static bool path_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value? - Implements ConfigSetType::has_been_set() -.
Definition: path.c:217
static intptr_t path_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a Path config item - Implements ConfigSetType::native_get() -.
Definition: path.c:207
static int path_native_set(void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a Path config item by string - Implements ConfigSetType::native_set() -.
Definition: path.c:162
static int path_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Path to its initial value - Implements ConfigSetType::reset() -.
Definition: path.c:230
static int path_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Path as a string - Implements ConfigSetType::string_get() -.
Definition: path.c:143
static int path_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Path by string - Implements ConfigSetType::string_set() -.
Definition: path.c:89
#define FREE(x)
Definition: memory.h:55
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_path_tidy(struct Buffer *path, bool is_dir)
Remove unnecessary parts of a path.
Definition: path.c:169
bool mutt_path_tilde(struct Buffer *path, const char *homedir)
Expand '~' in a path.
Definition: path.c:194
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:254
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:661
Parse the 'set' command.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
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
Container for Accounts, Notifications.
Definition: neomutt.h:43
char * home_dir
User's home directory.
Definition: neomutt.h:53
Constants for all the config types.
#define D_PATH_DIR
Path is a directory.
Definition: types.h:102
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:89
@ DT_PATH
a path to a file/directory
Definition: types.h:39
#define D_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:79