NeoMutt  2024-04-25-127-g771158
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(const struct ConfigSet *cs, 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(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
91 const char *value, 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(cs, cdef, (intptr_t) value, err);
116
117 if (CSR_RESULT(rc) != CSR_SUCCESS)
118 return rc | CSR_INV_VALIDATOR;
119 }
120
121 path_destroy(cs, 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(const struct ConfigSet *cs, void *var,
145 const struct ConfigDef *cdef, struct Buffer *result)
146{
147 const char *str = NULL;
148
149 if (var)
150 str = *(const char **) var;
151 else
152 str = (char *) cdef->initial;
153
154 if (!str)
155 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty path */
156
157 buf_addstr(result, str);
158 return CSR_SUCCESS;
159}
160
164static int path_native_set(const struct ConfigSet *cs, void *var,
165 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
166{
167 const char *str = (const char *) value;
168
169 /* Store empty paths as NULL */
170 if (str && (str[0] == '\0'))
171 value = 0;
172
173 if ((value == 0) && (cdef->type & D_NOT_EMPTY))
174 {
175 buf_printf(err, _("Option %s may not be empty"), cdef->name);
177 }
178
179 if (mutt_str_equal((const char *) value, (*(char **) var)))
181
182 int rc;
183
184 if (startup_only(cdef, err))
186
187 if (cdef->validator)
188 {
189 rc = cdef->validator(cs, cdef, value, err);
190
191 if (CSR_RESULT(rc) != CSR_SUCCESS)
192 return rc | CSR_INV_VALIDATOR;
193 }
194
195 path_destroy(cs, var, cdef);
196
197 str = path_tidy(str, cdef->type & D_PATH_DIR);
198 rc = CSR_SUCCESS;
199 if (!str)
200 rc |= CSR_SUC_EMPTY;
201
202 *(const char **) var = str;
203 return rc;
204}
205
209static intptr_t path_native_get(const struct ConfigSet *cs, void *var,
210 const struct ConfigDef *cdef, struct Buffer *err)
211{
212 const char *str = *(const char **) var;
213
214 return (intptr_t) str;
215}
216
220static int path_reset(const struct ConfigSet *cs, void *var,
221 const struct ConfigDef *cdef, struct Buffer *err)
222{
223 int rc = CSR_SUCCESS;
224
225 const char *str = path_tidy((const char *) cdef->initial, cdef->type & D_PATH_DIR);
226 if (!str)
227 rc |= CSR_SUC_EMPTY;
228
229 if (mutt_str_equal(str, (*(char **) var)))
230 {
231 FREE(&str);
232 return rc | CSR_SUC_NO_CHANGE;
233 }
234
235 if (startup_only(cdef, err))
236 {
237 FREE(&str);
239 }
240
241 if (cdef->validator)
242 {
243 rc = cdef->validator(cs, cdef, cdef->initial, err);
244
245 if (CSR_RESULT(rc) != CSR_SUCCESS)
246 {
247 FREE(&str);
248 return rc | CSR_INV_VALIDATOR;
249 }
250 }
251
252 path_destroy(cs, var, cdef);
253
254 if (!str)
255 rc |= CSR_SUC_EMPTY;
256
257 *(const char **) var = str;
258 return rc;
259}
260
264const struct ConfigSetType CstPath = {
265 DT_PATH,
266 "path",
271 NULL, // string_plus_equals
272 NULL, // string_minus_equals
275};
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:264
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:38
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(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy() -.
Definition: path.c:78
static intptr_t path_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a Path config item - Implements ConfigSetType::native_get() -.
Definition: path.c:209
static int path_native_set(const struct ConfigSet *cs, 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:164
static int path_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Path to its initial value - Implements ConfigSetType::reset() -.
Definition: path.c:220
static int path_string_get(const struct ConfigSet *cs, 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(const struct ConfigSet *cs, 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:45
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:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
Parse the 'set' command.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
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_PATH_DIR
Path is a directory.
Definition: types.h:103
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:90
@ DT_PATH
a path to a file/directory
Definition: types.h:40
#define D_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:80