NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
myvar.c
Go to the documentation of this file.
1
35#include "config.h"
36#include <stddef.h>
37#include <stdint.h>
38#include "mutt/lib.h"
39#include "set.h"
40#include "types.h"
41
45static void myvar_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
46{
47 const char **str = (const char **) var;
48 if (!*str)
49 return;
50
51 FREE(var);
52}
53
57static int myvar_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
58 const char *value, struct Buffer *err)
59{
60 /* Store empty myvars as NULL */
61 if (value && (value[0] == '\0'))
62 value = NULL;
63
64 int rc = CSR_SUCCESS;
65
66 if (var)
67 {
68 if (mutt_str_equal(value, (*(char **) var)))
70
71 myvar_destroy(cs, var, cdef);
72
73 const char *str = mutt_str_dup(value);
74 if (!str)
75 rc |= CSR_SUC_EMPTY;
76
77 *(const char **) var = str;
78 }
79 else
80 {
81 if (cdef->type & D_INTERNAL_INITIAL_SET)
82 FREE(&cdef->initial);
83
85 cdef->initial = (intptr_t) mutt_str_dup(value);
86 }
87
88 return rc;
89}
90
94static int myvar_string_get(const struct ConfigSet *cs, void *var,
95 const struct ConfigDef *cdef, struct Buffer *result)
96{
97 const char *str = NULL;
98
99 if (var)
100 str = *(const char **) var;
101 else
102 str = (char *) cdef->initial;
103
104 if (!str)
105 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty myvar */
106
107 buf_addstr(result, str);
108 return CSR_SUCCESS;
109}
110
114static int myvar_native_set(const struct ConfigSet *cs, void *var,
115 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
116{
117 const char *str = (const char *) value;
118
119 /* Store empty myvars as NULL */
120 if (str && (str[0] == '\0'))
121 value = 0;
122
123 if (mutt_str_equal((const char *) value, (*(char **) var)))
125
126 int rc;
127
128 myvar_destroy(cs, var, cdef);
129
130 str = mutt_str_dup(str);
131 rc = CSR_SUCCESS;
132 if (!str)
133 rc |= CSR_SUC_EMPTY;
134
135 *(const char **) var = str;
136 return rc;
137}
138
142static intptr_t myvar_native_get(const struct ConfigSet *cs, void *var,
143 const struct ConfigDef *cdef, struct Buffer *err)
144{
145 const char *str = *(const char **) var;
146
147 return (intptr_t) str;
148}
149
153static int myvar_string_plus_equals(const struct ConfigSet *cs, void *var,
154 const struct ConfigDef *cdef,
155 const char *value, struct Buffer *err)
156{
157 /* Skip if the value is missing or empty string*/
158 if (!value || (value[0] == '\0'))
160
161 int rc = CSR_SUCCESS;
162
163 char *str = NULL;
164 const char **var_str = (const char **) var;
165
166 if (*var_str)
167 mutt_str_asprintf(&str, "%s%s", *var_str, value);
168 else
169 str = mutt_str_dup(value);
170
171 myvar_destroy(cs, var, cdef);
172 *var_str = str;
173
174 return rc;
175}
176
180static int myvar_reset(const struct ConfigSet *cs, void *var,
181 const struct ConfigDef *cdef, struct Buffer *err)
182{
183 int rc = CSR_SUCCESS;
184
185 const char *str = mutt_str_dup((const char *) cdef->initial);
186 if (!str)
187 rc |= CSR_SUC_EMPTY;
188
189 if (mutt_str_equal(str, (*(char **) var)))
190 {
191 FREE(&str);
192 return rc | CSR_SUC_NO_CHANGE;
193 }
194
195 myvar_destroy(cs, var, cdef);
196
197 if (!str)
198 rc |= CSR_SUC_EMPTY;
199
200 *(const char **) var = str;
201 return rc;
202}
203
207const struct ConfigSetType CstMyVar = {
208 DT_MYVAR,
209 "myvar",
215 NULL, // string_minus_equals
218};
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:44
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
static void myvar_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a MyVar - Implements ConfigSetType::destroy() -.
Definition: myvar.c:45
static intptr_t myvar_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a MyVar config item - Implements ConfigSetType::native_get() -.
Definition: myvar.c:142
static int myvar_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a MyVar config item by string - Implements ConfigSetType::native_set() -.
Definition: myvar.c:114
static int myvar_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a MyVar to its initial value - Implements ConfigSetType::reset() -.
Definition: myvar.c:180
static int myvar_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a MyVar as a string - Implements ConfigSetType::string_get() -.
Definition: myvar.c:94
static int myvar_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Add to a MyVar by string - Implements ConfigSetType::string_plus_equals() -.
Definition: myvar.c:153
static int myvar_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a MyVar by string - Implements ConfigSetType::string_set() -.
Definition: myvar.c:57
#define FREE(x)
Definition: memory.h:45
Convenience wrapper for the library headers.
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:852
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:709
const struct ConfigSetType CstMyVar
Config type representing a MyVar.
Definition: myvar.c:207
Parse the 'set' command.
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:64
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_MYVAR
a user-defined variable (my_foo)
Definition: types.h:38