NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
string.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 
45 static void string_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 
57 static int string_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
58  const char *value, struct Buffer *err)
59 {
60  /* Store empty strings as NULL */
61  if (value && (value[0] == '\0'))
62  value = NULL;
63 
64  if (!value && (cdef->type & DT_NOT_EMPTY))
65  {
66  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
68  }
69 
70  int rc = CSR_SUCCESS;
71 
72  if (var)
73  {
74  if (mutt_str_equal(value, (*(char **) var)))
76 
77  if (cdef->validator)
78  {
79  rc = cdef->validator(cs, cdef, (intptr_t) value, err);
80 
81  if (CSR_RESULT(rc) != CSR_SUCCESS)
82  return rc | CSR_INV_VALIDATOR;
83  }
84 
85  string_destroy(cs, var, cdef);
86 
87  const char *str = mutt_str_dup(value);
88  if (!str)
89  rc |= CSR_SUC_EMPTY;
90 
91  *(const char **) var = str;
92  }
93  else
94  {
95  if (cdef->type & DT_INITIAL_SET)
96  FREE(&cdef->initial);
97 
98  cdef->type |= DT_INITIAL_SET;
99  cdef->initial = (intptr_t) mutt_str_dup(value);
100  }
101 
102  return rc;
103 }
104 
108 static int string_string_get(const struct ConfigSet *cs, void *var,
109  const struct ConfigDef *cdef, struct Buffer *result)
110 {
111  const char *str = NULL;
112 
113  if (var)
114  str = *(const char **) var;
115  else
116  str = (char *) cdef->initial;
117 
118  if (!str)
119  return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty string */
120 
121  mutt_buffer_addstr(result, str);
122  return CSR_SUCCESS;
123 }
124 
128 static int string_native_set(const struct ConfigSet *cs, void *var,
129  const struct ConfigDef *cdef, intptr_t value,
130  struct Buffer *err)
131 {
132  const char *str = (const char *) value;
133 
134  /* Store empty strings as NULL */
135  if (str && (str[0] == '\0'))
136  value = 0;
137 
138  if ((value == 0) && (cdef->type & DT_NOT_EMPTY))
139  {
140  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
142  }
143 
144  if (mutt_str_equal((const char *) value, (*(char **) var)))
146 
147  int rc;
148 
149  if (cdef->validator)
150  {
151  rc = cdef->validator(cs, cdef, value, err);
152 
153  if (CSR_RESULT(rc) != CSR_SUCCESS)
154  return rc | CSR_INV_VALIDATOR;
155  }
156 
157  string_destroy(cs, var, cdef);
158 
159  str = mutt_str_dup(str);
160  rc = CSR_SUCCESS;
161  if (!str)
162  rc |= CSR_SUC_EMPTY;
163 
164  *(const char **) var = str;
165  return rc;
166 }
167 
171 static intptr_t string_native_get(const struct ConfigSet *cs, void *var,
172  const struct ConfigDef *cdef, struct Buffer *err)
173 {
174  const char *str = *(const char **) var;
175 
176  return (intptr_t) str;
177 }
178 
182 static int string_string_plus_equals(const struct ConfigSet *cs, void *var,
183  const struct ConfigDef *cdef,
184  const char *value, struct Buffer *err)
185 {
186  /* Skip if the value is missing or empty string*/
187  if (!value || (value && (value[0] == '\0')))
189 
190  int rc = CSR_SUCCESS;
191 
192  char *str = NULL;
193  const char **var_str = (const char **) var;
194 
195  if (*var_str)
196  mutt_str_asprintf(&str, "%s%s", *var_str, value);
197  else
198  str = mutt_str_dup(value);
199 
200  if (cdef->validator)
201  {
202  rc = cdef->validator(cs, cdef, (intptr_t) str, err);
203 
204  if (CSR_RESULT(rc) != CSR_SUCCESS)
205  {
206  FREE(&str);
207  return rc | CSR_INV_VALIDATOR;
208  }
209  }
210 
211  string_destroy(cs, var, cdef);
212  *var_str = str;
213 
214  return rc;
215 }
216 
220 static int string_reset(const struct ConfigSet *cs, void *var,
221  const struct ConfigDef *cdef, struct Buffer *err)
222 {
223  int rc = CSR_SUCCESS;
224 
225  const char *str = mutt_str_dup((const char *) cdef->initial);
226  if (!str)
227  rc |= CSR_SUC_EMPTY;
228 
229  if (mutt_str_equal(str, (*(char **) var)))
230  {
231  FREE(&str);
232  return rc | CSR_SUC_NO_CHANGE;
233  }
234 
235  if (cdef->validator)
236  {
237  rc = cdef->validator(cs, cdef, cdef->initial, err);
238 
239  if (CSR_RESULT(rc) != CSR_SUCCESS)
240  {
241  FREE(&str);
242  return rc | CSR_INV_VALIDATOR;
243  }
244  }
245 
246  string_destroy(cs, var, cdef);
247 
248  if (!str)
249  rc |= CSR_SUC_EMPTY;
250 
251  *(const char **) var = str;
252  return rc;
253 }
254 
258 const struct ConfigSetType CstString = {
259  DT_STRING,
260  "string",
266  NULL, // string_minus_equals
267  string_reset,
269 };
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:223
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:158
const struct ConfigSetType CstString
Config type representing a string.
Definition: string.c:258
static void string_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a String - Implements ConfigSetType::destroy() -.
Definition: string.c:45
static intptr_t string_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get a string from a String config item - Implements ConfigSetType::native_get() -.
Definition: string.c:171
static int string_native_set(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set a String config item by string - Implements ConfigSetType::native_set() -.
Definition: string.c:128
static int string_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a String to its initial value - Implements ConfigSetType::reset() -.
Definition: string.c:220
static int string_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a String as a string - Implements ConfigSetType::string_get() -.
Definition: string.c:108
static int string_string_plus_equals(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, const char *value, struct Buffer *err)
Concat String to a string - Implements ConfigSetType::string_plus_equals() -.
Definition: string.c:182
static int string_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a String by string - Implements ConfigSetType::string_set() -.
Definition: string.c:57
#define FREE(x)
Definition: memory.h:43
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition: string.c:1008
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:784
A collection of config items.
#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
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:260
Constants for all the config types.
#define DT_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:79
#define DT_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:46
#define DT_STRING
a string
Definition: types.h:41