NeoMutt  2023-05-17-16-g61469c
Teaching an old dog new tricks
DOXYGEN
path.c
Go to the documentation of this file.
1
35#include "config.h"
36#include <stddef.h>
37#include <limits.h>
38#include <stdbool.h>
39#include <stdint.h>
40#include "mutt/lib.h"
41#include "set.h"
42#include "types.h"
43
44extern char *HomeDir;
45
56static char *path_tidy(const char *path, bool is_dir)
57{
58 if (!path || (*path == '\0'))
59 return NULL;
60
61 char buf[PATH_MAX] = { 0 };
62 mutt_str_copy(buf, path, sizeof(buf));
63
64 mutt_path_tilde(buf, sizeof(buf), HomeDir);
65 mutt_path_tidy(buf, is_dir);
66
67 return mutt_str_dup(buf);
68}
69
73static void path_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
74{
75 const char **str = (const char **) var;
76 if (!*str)
77 return;
78
79 FREE(var);
80}
81
85static int path_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
86 const char *value, struct Buffer *err)
87{
88 /* Store empty paths as NULL */
89 if (value && (value[0] == '\0'))
90 value = NULL;
91
92 if (!value && (cdef->type & DT_NOT_EMPTY))
93 {
94 buf_printf(err, _("Option %s may not be empty"), cdef->name);
96 }
97
98 int rc = CSR_SUCCESS;
99
100 if (var)
101 {
102 if (mutt_str_equal(value, (*(char **) var)))
104
105 if (cdef->validator)
106 {
107 rc = cdef->validator(cs, cdef, (intptr_t) value, err);
108
109 if (CSR_RESULT(rc) != CSR_SUCCESS)
110 return rc | CSR_INV_VALIDATOR;
111 }
112
113 path_destroy(cs, var, cdef);
114
115 const char *str = path_tidy(value, cdef->type & DT_PATH_DIR);
116 if (!str)
117 rc |= CSR_SUC_EMPTY;
118
119 *(const char **) var = str;
120 }
121 else
122 {
123 if (cdef->type & DT_INITIAL_SET)
124 FREE(&cdef->initial);
125
126 cdef->type |= DT_INITIAL_SET;
127 cdef->initial = (intptr_t) mutt_str_dup(value);
128 }
129
130 return rc;
131}
132
136static int path_string_get(const struct ConfigSet *cs, void *var,
137 const struct ConfigDef *cdef, struct Buffer *result)
138{
139 const char *str = NULL;
140
141 if (var)
142 str = *(const char **) var;
143 else
144 str = (char *) cdef->initial;
145
146 if (!str)
147 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty path */
148
149 buf_addstr(result, str);
150 return CSR_SUCCESS;
151}
152
156static int path_native_set(const struct ConfigSet *cs, void *var,
157 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
158{
159 const char *str = (const char *) value;
160
161 /* Store empty paths as NULL */
162 if (str && (str[0] == '\0'))
163 value = 0;
164
165 if ((value == 0) && (cdef->type & DT_NOT_EMPTY))
166 {
167 buf_printf(err, _("Option %s may not be empty"), cdef->name);
169 }
170
171 if (mutt_str_equal((const char *) value, (*(char **) var)))
173
174 int rc;
175
176 if (cdef->validator)
177 {
178 rc = cdef->validator(cs, cdef, value, err);
179
180 if (CSR_RESULT(rc) != CSR_SUCCESS)
181 return rc | CSR_INV_VALIDATOR;
182 }
183
184 path_destroy(cs, var, cdef);
185
186 str = path_tidy(str, cdef->type & DT_PATH_DIR);
187 rc = CSR_SUCCESS;
188 if (!str)
189 rc |= CSR_SUC_EMPTY;
190
191 *(const char **) var = str;
192 return rc;
193}
194
198static intptr_t path_native_get(const struct ConfigSet *cs, void *var,
199 const struct ConfigDef *cdef, struct Buffer *err)
200{
201 const char *str = *(const char **) var;
202
203 return (intptr_t) str;
204}
205
209static int path_reset(const struct ConfigSet *cs, void *var,
210 const struct ConfigDef *cdef, struct Buffer *err)
211{
212 int rc = CSR_SUCCESS;
213
214 const char *str = path_tidy((const char *) cdef->initial, cdef->type & DT_PATH_DIR);
215 if (!str)
216 rc |= CSR_SUC_EMPTY;
217
218 if (mutt_str_equal(str, (*(char **) var)))
219 {
220 FREE(&str);
221 return rc | CSR_SUC_NO_CHANGE;
222 }
223
224 if (cdef->validator)
225 {
226 rc = cdef->validator(cs, cdef, cdef->initial, err);
227
228 if (CSR_RESULT(rc) != CSR_SUCCESS)
229 {
230 FREE(&str);
231 return rc | CSR_INV_VALIDATOR;
232 }
233 }
234
235 path_destroy(cs, var, cdef);
236
237 if (!str)
238 rc |= CSR_SUC_EMPTY;
239
240 *(const char **) var = str;
241 return rc;
242}
243
247const struct ConfigSetType CstPath = {
248 DT_PATH,
249 "path",
254 NULL, // string_plus_equals
255 NULL, // string_minus_equals
258};
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:171
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:236
const struct ConfigSetType CstPath
Config type representing a path.
Definition: path.c:247
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:56
char * HomeDir
User's home directory.
Definition: globals.c:39
#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:73
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:198
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:156
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:209
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:136
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:85
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_path_tidy(char *buf, bool is_dir)
Remove unnecessary parts of a path.
Definition: path.c:165
bool mutt_path_tilde(char *buf, size_t buflen, const char *homedir)
Expand '~' in a path.
Definition: path.c:223
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:653
#define PATH_MAX
Definition: mutt.h:41
Parse the 'set' command.
String manipulation buffer.
Definition: buffer.h:34
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 DT_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:79
#define DT_PATH_DIR
Path is a directory.
Definition: types.h:56
#define DT_PATH
a path to a file/directory
Definition: types.h:36
#define DT_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:49