NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
myvar.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <stddef.h>
33#include <stdint.h>
34#include "mutt/lib.h"
35#include "set.h"
36#include "types.h"
37
41static void myvar_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
42{
43 const char **str = (const char **) var;
44 if (!*str)
45 return;
46
47 FREE(var);
48}
49
53static int myvar_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
54 const char *value, struct Buffer *err)
55{
56 /* Store empty myvars as NULL */
57 if (value && (value[0] == '\0'))
58 value = NULL;
59
60 int rc = CSR_SUCCESS;
61
62 if (var)
63 {
64 if (mutt_str_equal(value, (*(char **) var)))
66
67 myvar_destroy(cs, var, cdef);
68
69 const char *str = mutt_str_dup(value);
70 if (!str)
71 rc |= CSR_SUC_EMPTY;
72
73 *(const char **) var = str;
74 }
75 else
76 {
77 if (cdef->type & DT_INITIAL_SET)
78 FREE(&cdef->initial);
79
80 cdef->type |= DT_INITIAL_SET;
81 cdef->initial = (intptr_t) mutt_str_dup(value);
82 }
83
84 return rc;
85}
86
90static int myvar_string_get(const struct ConfigSet *cs, void *var,
91 const struct ConfigDef *cdef, struct Buffer *result)
92{
93 const char *str = NULL;
94
95 if (var)
96 str = *(const char **) var;
97 else
98 str = (char *) cdef->initial;
99
100 if (!str)
101 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty myvar */
102
103 buf_addstr(result, str);
104 return CSR_SUCCESS;
105}
106
110static int myvar_native_set(const struct ConfigSet *cs, void *var,
111 const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
112{
113 const char *str = (const char *) value;
114
115 /* Store empty myvars as NULL */
116 if (str && (str[0] == '\0'))
117 value = 0;
118
119 if (mutt_str_equal((const char *) value, (*(char **) var)))
121
122 int rc;
123
124 myvar_destroy(cs, var, cdef);
125
126 str = mutt_str_dup(str);
127 rc = CSR_SUCCESS;
128 if (!str)
129 rc |= CSR_SUC_EMPTY;
130
131 *(const char **) var = str;
132 return rc;
133}
134
138static intptr_t myvar_native_get(const struct ConfigSet *cs, void *var,
139 const struct ConfigDef *cdef, struct Buffer *err)
140{
141 const char *str = *(const char **) var;
142
143 return (intptr_t) str;
144}
145
149static int myvar_string_plus_equals(const struct ConfigSet *cs, void *var,
150 const struct ConfigDef *cdef,
151 const char *value, struct Buffer *err)
152{
153 /* Skip if the value is missing or empty string*/
154 if (!value || (value[0] == '\0'))
156
157 int rc = CSR_SUCCESS;
158
159 char *str = NULL;
160 const char **var_str = (const char **) var;
161
162 if (*var_str)
163 mutt_str_asprintf(&str, "%s%s", *var_str, value);
164 else
165 str = mutt_str_dup(value);
166
167 myvar_destroy(cs, var, cdef);
168 *var_str = str;
169
170 return rc;
171}
172
176static int myvar_reset(const struct ConfigSet *cs, void *var,
177 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(cs, 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
214};
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:238
#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:41
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:138
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:110
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:176
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:90
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:149
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:53
#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:251
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:1022
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
const struct ConfigSetType CstMyVar
Config type representing a MyVar.
Definition: myvar.c:203
Parse the 'set' command.
String manipulation buffer.
Definition: buffer.h:34
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 DT_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:80
#define DT_MYVAR
a user-defined variable (my_foo)
Definition: types.h:43