forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyvar.c
218 lines (180 loc) · 5.49 KB
/
myvar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @file
* Type representing a user-defined variable "my_var"
*
* @authors
* Copyright (C) 2023 Richard Russon <[email protected]>
* Copyright (C) 2023 Rayford Shireman
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page config_myvar Type: MyVar
*
* Config type representing a user-defined variable "my_var".
*
* - Backed by `char *`
* - Empty variable is stored as `NULL`
* - Data is freed when `ConfigSet` is freed
* - Implementation: #CstMyVar
*/
#include "config.h"
#include <stddef.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "set.h"
#include "types.h"
/**
* myvar_destroy - Destroy a MyVar - Implements ConfigSetType::destroy() - @ingroup cfg_type_destroy
*/
static void myvar_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
{
const char **str = (const char **) var;
if (!*str)
return;
FREE(var);
}
/**
* myvar_string_set - Set a MyVar by string - Implements ConfigSetType::string_set() - @ingroup cfg_type_string_set
*/
static int myvar_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
const char *value, struct Buffer *err)
{
/* Store empty myvars as NULL */
if (value && (value[0] == '\0'))
value = NULL;
int rc = CSR_SUCCESS;
if (var)
{
if (mutt_str_equal(value, (*(char **) var)))
return CSR_SUCCESS | CSR_SUC_NO_CHANGE;
myvar_destroy(cs, var, cdef);
const char *str = mutt_str_dup(value);
if (!str)
rc |= CSR_SUC_EMPTY;
*(const char **) var = str;
}
else
{
if (cdef->type & D_INTERNAL_INITIAL_SET)
FREE(&cdef->initial);
cdef->type |= D_INTERNAL_INITIAL_SET;
cdef->initial = (intptr_t) mutt_str_dup(value);
}
return rc;
}
/**
* myvar_string_get - Get a MyVar as a string - Implements ConfigSetType::string_get() - @ingroup cfg_type_string_get
*/
static int myvar_string_get(const struct ConfigSet *cs, void *var,
const struct ConfigDef *cdef, struct Buffer *result)
{
const char *str = NULL;
if (var)
str = *(const char **) var;
else
str = (char *) cdef->initial;
if (!str)
return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty myvar */
buf_addstr(result, str);
return CSR_SUCCESS;
}
/**
* myvar_native_set - Set a MyVar config item by string - Implements ConfigSetType::native_set() - @ingroup cfg_type_native_set
*/
static int myvar_native_set(const struct ConfigSet *cs, void *var,
const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
{
const char *str = (const char *) value;
/* Store empty myvars as NULL */
if (str && (str[0] == '\0'))
value = 0;
if (mutt_str_equal((const char *) value, (*(char **) var)))
return CSR_SUCCESS | CSR_SUC_NO_CHANGE;
int rc;
myvar_destroy(cs, var, cdef);
str = mutt_str_dup(str);
rc = CSR_SUCCESS;
if (!str)
rc |= CSR_SUC_EMPTY;
*(const char **) var = str;
return rc;
}
/**
* myvar_native_get - Get a string from a MyVar config item - Implements ConfigSetType::native_get() - @ingroup cfg_type_native_get
*/
static intptr_t myvar_native_get(const struct ConfigSet *cs, void *var,
const struct ConfigDef *cdef, struct Buffer *err)
{
const char *str = *(const char **) var;
return (intptr_t) str;
}
/**
* myvar_string_plus_equals - Add to a MyVar by string - Implements ConfigSetType::string_plus_equals() - @ingroup cfg_type_string_plus_equals
*/
static int myvar_string_plus_equals(const struct ConfigSet *cs, void *var,
const struct ConfigDef *cdef,
const char *value, struct Buffer *err)
{
/* Skip if the value is missing or empty string*/
if (!value || (value[0] == '\0'))
return CSR_SUCCESS | CSR_SUC_NO_CHANGE;
int rc = CSR_SUCCESS;
char *str = NULL;
const char **var_str = (const char **) var;
if (*var_str)
mutt_str_asprintf(&str, "%s%s", *var_str, value);
else
str = mutt_str_dup(value);
myvar_destroy(cs, var, cdef);
*var_str = str;
return rc;
}
/**
* myvar_reset - Reset a MyVar to its initial value - Implements ConfigSetType::reset() - @ingroup cfg_type_reset
*/
static int myvar_reset(const struct ConfigSet *cs, void *var,
const struct ConfigDef *cdef, struct Buffer *err)
{
int rc = CSR_SUCCESS;
const char *str = mutt_str_dup((const char *) cdef->initial);
if (!str)
rc |= CSR_SUC_EMPTY;
if (mutt_str_equal(str, (*(char **) var)))
{
FREE(&str);
return rc | CSR_SUC_NO_CHANGE;
}
myvar_destroy(cs, var, cdef);
if (!str)
rc |= CSR_SUC_EMPTY;
*(const char **) var = str;
return rc;
}
/**
* CstMyVar - Config type representing a MyVar
*/
const struct ConfigSetType CstMyVar = {
DT_MYVAR,
"myvar",
myvar_string_set,
myvar_string_get,
myvar_native_set,
myvar_native_get,
myvar_string_plus_equals,
NULL, // string_minus_equals
myvar_reset,
myvar_destroy,
};