NeoMutt  2025-01-09-81-g753ae0
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(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(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(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(void *var, const struct ConfigDef *cdef, struct Buffer *result)
95{
96 const char *str = NULL;
97
98 if (var)
99 str = *(const char **) var;
100 else
101 str = (char *) cdef->initial;
102
103 if (!str)
104 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty myvar */
105
106 buf_addstr(result, str);
107 return CSR_SUCCESS;
108}
109
113static int myvar_native_set(void *var, const struct ConfigDef *cdef,
114 intptr_t value, struct Buffer *err)
115{
116 const char *str = (const char *) value;
117
118 /* Store empty myvars as NULL */
119 if (str && (str[0] == '\0'))
120 value = 0;
121
122 if (mutt_str_equal((const char *) value, (*(char **) var)))
124
125 int rc;
126
127 myvar_destroy(var, cdef);
128
129 str = mutt_str_dup(str);
130 rc = CSR_SUCCESS;
131 if (!str)
132 rc |= CSR_SUC_EMPTY;
133
134 *(const char **) var = str;
135 return rc;
136}
137
141static intptr_t myvar_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
142{
143 const char *str = *(const char **) var;
144
145 return (intptr_t) str;
146}
147
151static int myvar_string_plus_equals(void *var, const struct ConfigDef *cdef,
152 const char *value, struct Buffer *err)
153{
154 /* Skip if the value is missing or empty string*/
155 if (!value || (value[0] == '\0'))
157
158 int rc = CSR_SUCCESS;
159
160 char *str = NULL;
161 const char **var_str = (const char **) var;
162
163 if (*var_str)
164 mutt_str_asprintf(&str, "%s%s", *var_str, value);
165 else
166 str = mutt_str_dup(value);
167
168 myvar_destroy(var, cdef);
169 *var_str = str;
170
171 return rc;
172}
173
177static int myvar_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
178{
179 int rc = CSR_SUCCESS;
180
181 const char *str = mutt_str_dup((const char *) cdef->initial);
182 if (!str)
183 rc |= CSR_SUC_EMPTY;
184
185 if (mutt_str_equal(str, (*(char **) var)))
186 {
187 FREE(&str);
188 return rc | CSR_SUC_NO_CHANGE;
189 }
190
191 myvar_destroy(var, cdef);
192
193 if (!str)
194 rc |= CSR_SUC_EMPTY;
195
196 *(const char **) var = str;
197 return rc;
198}
199
203const struct ConfigSetType CstMyVar = {
204 DT_MYVAR,
205 "myvar",
211 NULL, // string_minus_equals
212 NULL, // has_been_set
215};
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
#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(void *var, const struct ConfigDef *cdef)
Destroy a MyVar - Implements ConfigSetType::destroy() -.
Definition: myvar.c:45
static intptr_t myvar_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a MyVar config item - Implements ConfigSetType::native_get() -.
Definition: myvar.c:141
static int myvar_native_set(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:113
static int myvar_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a MyVar to its initial value - Implements ConfigSetType::reset() -.
Definition: myvar.c:177
static int myvar_string_get(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(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:151
static int myvar_string_set(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:55
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:254
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:804
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:661
const struct ConfigSetType CstMyVar
Config type representing a MyVar.
Definition: myvar.c:203
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
Constants for all the config types.
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:89
@ DT_MYVAR
a user-defined variable (my_foo)
Definition: types.h:37