NeoMutt  2025-01-09-81-g753ae0
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 "set.h"
44#include "types.h"
45
46extern char *HomeDir;
47
58static char *path_tidy(const char *path, bool is_dir)
59{
60 if (!path || (*path == '\0'))
61 return NULL;
62
63 struct Buffer *buf = buf_pool_get();
64 buf_strcpy(buf, path);
65
67 mutt_path_tidy(buf, is_dir);
68
69 char *tidy_path = buf_strdup(buf);
70 buf_pool_release(&buf);
71
72 return tidy_path;
73}
74
78static void path_destroy(void *var, const struct ConfigDef *cdef)
79{
80 const char **str = (const char **) var;
81 if (!*str)
82 return;
83
84 FREE(var);
85}
86
90static int path_string_set(void *var, struct ConfigDef *cdef, const char *value,
91 struct Buffer *err)
92{
93 /* Store empty paths as NULL */
94 if (value && (value[0] == '\0'))
95 value = NULL;
96
97 if (!value && (cdef->type & D_NOT_EMPTY))
98 {
99 buf_printf(err, _("Option %s may not be empty"), cdef->name);
101 }
102
103 int rc = CSR_SUCCESS;
104
105 if (var)
106 {
107 if (mutt_str_equal(value, (*(char **) var)))
109
110 if (startup_only(cdef, err))
112
113 if (cdef->validator)
114 {
115 rc = cdef->validator(cdef, (intptr_t) value, err);
116
117 if (CSR_RESULT(rc) != CSR_SUCCESS)
118 return rc | CSR_INV_VALIDATOR;
119 }
120
121 path_destroy(var, cdef);
122
123 const char *str = path_tidy(value, cdef->type & D_PATH_DIR);
124 if (!str)
125 rc |= CSR_SUC_EMPTY;
126
127 *(const char **) var = str;
128 }
129 else
130 {
131 if (cdef->type & D_INTERNAL_INITIAL_SET)
132 FREE(&cdef->initial);
133
135 cdef->initial = (intptr_t) mutt_str_dup(value);
136 }
137
138 return rc;
139}
140
144static int path_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
145{
146 const char *str = NULL;
147
148 if (var)
149 str = *(const char **) var;
150 else
151 str = (char *) cdef->initial;
152
153 if (!str)
154 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty path */
155
156 buf_addstr(result, str);
157 return CSR_SUCCESS;
158}
159
163static int path_native_set(void *var, const struct ConfigDef *cdef,
164 intptr_t value, struct Buffer *err)
165{
166 const char *str = (const char *) value;
167
168 /* Store empty paths as NULL */
169 if (str && (str[0] == '\0'))
170 value = 0;
171
172 if ((value == 0) && (cdef->type & D_NOT_EMPTY))
173 {
174 buf_printf(err, _("Option %s may not be empty"), cdef->name);
176 }
177
178 if (mutt_str_equal((const char *) value, (*(char **) var)))
180
181 int rc;
182
183 if (startup_only(cdef, err))
185
186 if (cdef->validator)
187 {
188 rc = cdef->validator(cdef, value, err);
189
190 if (CSR_RESULT(rc) != CSR_SUCCESS)
191 return rc | CSR_INV_VALIDATOR;
192 }
193
194 path_destroy(var, cdef);
195
196 str = path_tidy(str, cdef->type & D_PATH_DIR);
197 rc = CSR_SUCCESS;
198 if (!str)
199 rc |= CSR_SUC_EMPTY;
200
201 *(const char **) var = str;
202 return rc;
203}
204
208static intptr_t path_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
209{
210 const char *str = *(const char **) var;
211
212 return (intptr_t) str;
213}
214
218static bool path_has_been_set(void *var, const struct ConfigDef *cdef)
219{
220 const char *initial = path_tidy((const char *) cdef->initial, cdef->type & D_PATH_DIR);
221 const char *value = *(const char **) var;
222
223 bool rc = !mutt_str_equal(initial, value);
224 FREE(&initial);
225 return rc;
226}
227
231static int path_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
232{
233 int rc = CSR_SUCCESS;
234
235 const char *str = path_tidy((const char *) cdef->initial, cdef->type & D_PATH_DIR);
236 if (!str)
237 rc |= CSR_SUC_EMPTY;
238
239 if (mutt_str_equal(str, (*(char **) var)))
240 {
241 FREE(&str);
242 return rc | CSR_SUC_NO_CHANGE;
243 }
244
245 if (startup_only(cdef, err))
246 {
247 FREE(&str);
249 }
250
251 if (cdef->validator)
252 {
253 rc = cdef->validator(cdef, cdef->initial, err);
254
255 if (CSR_RESULT(rc) != CSR_SUCCESS)
256 {
257 FREE(&str);
258 return rc | CSR_INV_VALIDATOR;
259 }
260 }
261
262 path_destroy(var, cdef);
263
264 if (!str)
265 rc |= CSR_SUC_EMPTY;
266
267 *(const char **) var = str;
268 return rc;
269}
270
274const struct ConfigSetType CstPath = {
275 DT_PATH,
276 "path",
281 NULL, // string_plus_equals
282 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
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:274
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:58
char * HomeDir
User's home directory.
Definition: globals.c:37
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
static void path_destroy(void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy() -.
Definition: path.c:78
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:218
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:208
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:163
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:231
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:144
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:90
#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:64
const char * name
User-visible name.
Definition: set.h:65
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:81
intptr_t initial
Initial value.
Definition: set.h:67
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
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