NeoMutt  2021-10-29-43-g6b8931
Teaching an old dog new tricks
DOXYGEN
native_set()

Set a config item by string. More...

+ Collaboration diagram for native_set():

Functions

static int address_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set an Address config item by Address object - Implements ConfigSetType::native_set() -. More...
 
static int bool_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Bool config item by bool - Implements ConfigSetType::native_set() -. More...
 
static int enum_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set an Enumeration config item by int - Implements ConfigSetType::native_set() -. More...
 
static int long_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Long config item by long - Implements ConfigSetType::native_set() -. More...
 
static int mbtable_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set an MbTable config item by MbTable object - Implements ConfigSetType::native_set() -. More...
 
static int number_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Number config item by int - Implements ConfigSetType::native_set() -. More...
 
static int path_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Path config item by string - Implements ConfigSetType::native_set() -. More...
 
static int quad_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Quad-option config item by int - Implements ConfigSetType::native_set() -. More...
 
static int regex_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Regex config item by Regex object - Implements ConfigSetType::native_set() -. More...
 
static int slist_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Slist config item by Slist - Implements ConfigSetType::native_set() -. More...
 
static int sort_native_set (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
 Set a Sort config item by int - Implements ConfigSetType::native_set() -. More...
 
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() -. More...
 

Detailed Description

Set a config item by string.

Parameters
csConfig items
varVariable to set
cdefVariable definition
valueNative pointer/value to set
errBuffer for error messages (may be NULL)
Return values
numResult, e.g. CSR_SUCCESS

Contract

Function Documentation

◆ address_native_set()

static int address_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set an Address config item by Address object - Implements ConfigSetType::native_set() -.

Definition at line 161 of file address.c.

164 {
165  int rc;
166 
167  if (cdef->validator)
168  {
169  rc = cdef->validator(cs, cdef, value, err);
170 
171  if (CSR_RESULT(rc) != CSR_SUCCESS)
172  return rc | CSR_INV_VALIDATOR;
173  }
174 
175  address_free(var);
176 
177  struct Address *addr = address_dup((struct Address *) value);
178 
179  rc = CSR_SUCCESS;
180  if (!addr)
181  rc |= CSR_SUC_EMPTY;
182 
183  *(struct Address **) var = addr;
184  return rc;
185 }
void address_free(struct Address **addr)
Free an Address object.
Definition: address.c:249
static struct Address * address_dup(struct Address *addr)
Create a copy of an Address object.
Definition: address.c:147
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#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
An email address.
Definition: address.h:36
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
+ Here is the call graph for this function:

◆ bool_native_set()

static int bool_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Bool config item by bool - Implements ConfigSetType::native_set() -.

Definition at line 125 of file bool.c.

127 {
128  if ((value < 0) || (value > 1))
129  {
130  mutt_buffer_printf(err, _("Invalid boolean value: %ld"), value);
131  return CSR_ERR_INVALID | CSR_INV_TYPE;
132  }
133 
134  if (value == (*(bool *) var))
136 
137  if (cdef->validator)
138  {
139  int rc = cdef->validator(cs, cdef, value, err);
140 
141  if (CSR_RESULT(rc) != CSR_SUCCESS)
142  return rc | CSR_INV_VALIDATOR;
143  }
144 
145  *(bool *) var = value;
146  return CSR_SUCCESS;
147 }
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define _(a)
Definition: message.h:28
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:38
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define CSR_SUC_NO_CHANGE
The value hasn't changed.
Definition: set.h:44
+ Here is the call graph for this function:

◆ enum_native_set()

static int enum_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set an Enumeration config item by int - Implements ConfigSetType::native_set() -.

Definition at line 119 of file enum.c.

121 {
122  if (!cs || !var || !cdef)
123  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
124 
125  struct EnumDef *ed = (struct EnumDef *) cdef->data;
126  if (!ed || !ed->lookup)
127  return CSR_ERR_CODE;
128 
129  const char *name = mutt_map_get_name(value, ed->lookup);
130  if (!name)
131  {
132  mutt_buffer_printf(err, _("Invalid enum value: %ld"), value);
133  return (CSR_ERR_INVALID | CSR_INV_TYPE);
134  }
135 
136  if (value == (*(unsigned char *) var))
137  return (CSR_SUCCESS | CSR_SUC_NO_CHANGE);
138 
139  if (cdef->validator)
140  {
141  int rc = cdef->validator(cs, cdef, value, err);
142 
143  if (CSR_RESULT(rc) != CSR_SUCCESS)
144  return (rc | CSR_INV_VALIDATOR);
145  }
146 
147  *(unsigned char *) var = value;
148  return CSR_SUCCESS;
149 }
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
intptr_t data
Extra variable data.
Definition: set.h:68
An enumeration.
Definition: enum.h:30
const char * name
Config variable.
Definition: enum.h:31
struct Mapping * lookup
Lookup table.
Definition: enum.h:33
+ Here is the call graph for this function:

◆ long_native_set()

static int long_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Long config item by long - Implements ConfigSetType::native_set() -.

Definition at line 108 of file long.c.

110 {
111  if ((value < 0) && (cdef->type & DT_NOT_NEGATIVE))
112  {
113  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
115  }
116 
117  if (value == (*(long *) var))
119 
120  if (cdef->validator)
121  {
122  int rc = cdef->validator(cs, cdef, value, err);
123 
124  if (CSR_RESULT(rc) != CSR_SUCCESS)
125  return rc | CSR_INV_VALIDATOR;
126  }
127 
128  *(long *) var = value;
129  return CSR_SUCCESS;
130 }
const char * name
User-visible name.
Definition: set.h:65
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
#define DT_NOT_NEGATIVE
Negative numbers are not allowed.
Definition: types.h:47
+ Here is the call graph for this function:

◆ mbtable_native_set()

static int mbtable_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set an MbTable config item by MbTable object - Implements ConfigSetType::native_set() -.

Definition at line 198 of file mbtable.c.

201 {
202  int rc;
203 
204  if (cdef->validator)
205  {
206  rc = cdef->validator(cs, cdef, value, err);
207 
208  if (CSR_RESULT(rc) != CSR_SUCCESS)
209  return rc | CSR_INV_VALIDATOR;
210  }
211 
212  mbtable_free(var);
213 
214  struct MbTable *table = mbtable_dup((struct MbTable *) value);
215 
216  rc = CSR_SUCCESS;
217  if (!table)
218  rc |= CSR_SUC_EMPTY;
219 
220  *(struct MbTable **) var = table;
221  return rc;
222 }
void mbtable_free(struct MbTable **table)
Free an MbTable object.
Definition: mbtable.c:281
static struct MbTable * mbtable_dup(struct MbTable *table)
Create a copy of an MbTable object.
Definition: mbtable.c:185
Multibyte character table.
Definition: mbtable.h:34
+ Here is the call graph for this function:

◆ number_native_set()

static int number_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Number config item by int - Implements ConfigSetType::native_set() -.

Definition at line 115 of file number.c.

118 {
119  if ((value < SHRT_MIN) || (value > SHRT_MAX))
120  {
121  mutt_buffer_printf(err, _("Invalid number: %ld"), value);
122  return CSR_ERR_INVALID | CSR_INV_TYPE;
123  }
124 
125  if ((value < 0) && (cdef->type & DT_NOT_NEGATIVE))
126  {
127  mutt_buffer_printf(err, _("Option %s may not be negative"), cdef->name);
129  }
130 
131  if (value == (*(short *) var))
133 
134  if (cdef->validator)
135  {
136  int rc = cdef->validator(cs, cdef, value, err);
137 
138  if (CSR_RESULT(rc) != CSR_SUCCESS)
139  return rc | CSR_INV_VALIDATOR;
140  }
141 
142  *(short *) var = value;
143  return CSR_SUCCESS;
144 }
+ Here is the call graph for this function:

◆ path_native_set()

static int path_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Path config item by string - Implements ConfigSetType::native_set() -.

Definition at line 156 of file path.c.

158 {
159  const char *str = (const char *) value;
160 
161  /* Store empty paths as NULL */
162  if (str && (str[0] == '\0'))
163  value = 0;
164 
165  if ((value == 0) && (cdef->type & DT_NOT_EMPTY))
166  {
167  mutt_buffer_printf(err, _("Option %s may not be empty"), cdef->name);
169  }
170 
171  if (mutt_str_equal((const char *) value, (*(char **) var)))
173 
174  int rc;
175 
176  if (cdef->validator)
177  {
178  rc = cdef->validator(cs, cdef, value, err);
179 
180  if (CSR_RESULT(rc) != CSR_SUCCESS)
181  return rc | CSR_INV_VALIDATOR;
182  }
183 
184  path_destroy(cs, var, cdef);
185 
186  str = path_tidy(str, cdef->type & DT_PATH_DIR);
187  rc = CSR_SUCCESS;
188  if (!str)
189  rc |= CSR_SUC_EMPTY;
190 
191  *(const char **) var = str;
192  return rc;
193 }
static char * path_tidy(const char *path, bool is_dir)
Tidy a path for storage.
Definition: path.c:56
static void path_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a Path - Implements ConfigSetType::destroy() -.
Definition: path.c:73
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:715
#define DT_PATH_DIR
Path is a directory.
Definition: types.h:53
#define DT_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:46
+ Here is the call graph for this function:

◆ quad_native_set()

static int quad_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Quad-option config item by int - Implements ConfigSetType::native_set() -.

Definition at line 127 of file quad.c.

129 {
130  if ((value < 0) || (value >= (mutt_array_size(QuadValues) - 1)))
131  {
132  mutt_buffer_printf(err, _("Invalid quad value: %ld"), value);
133  return CSR_ERR_INVALID | CSR_INV_TYPE;
134  }
135 
136  if (value == (*(char *) var))
138 
139  if (cdef->validator)
140  {
141  int rc = cdef->validator(cs, cdef, value, err);
142 
143  if (CSR_RESULT(rc) != CSR_SUCCESS)
144  return rc | CSR_INV_VALIDATOR;
145  }
146 
147  *(char *) var = value;
148  return CSR_SUCCESS;
149 }
#define mutt_array_size(x)
Definition: memory.h:33
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:49
+ Here is the call graph for this function:

◆ regex_native_set()

static int regex_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Regex config item by Regex object - Implements ConfigSetType::native_set() -.

Definition at line 203 of file regex.c.

205 {
206  int rc;
207 
208  if (cdef->validator)
209  {
210  rc = cdef->validator(cs, cdef, value, err);
211 
212  if (CSR_RESULT(rc) != CSR_SUCCESS)
213  return rc | CSR_INV_VALIDATOR;
214  }
215 
216  rc = CSR_SUCCESS;
217  struct Regex *orig = (struct Regex *) value;
218  struct Regex *r = NULL;
219 
220  if (orig && orig->pattern)
221  {
222  const uint32_t flags = orig->pat_not ? DT_REGEX_ALLOW_NOT : 0;
223  r = regex_new(orig->pattern, flags, err);
224  if (!r)
225  rc = CSR_ERR_INVALID;
226  }
227  else
228  {
229  rc |= CSR_SUC_EMPTY;
230  }
231 
232  if (CSR_RESULT(rc) == CSR_SUCCESS)
233  {
234  regex_free(var);
235  *(struct Regex **) var = r;
236  }
237 
238  return rc;
239 }
struct Regex * regex_new(const char *str, uint32_t flags, struct Buffer *err)
Create an Regex from a string.
Definition: regex.c:80
void regex_free(struct Regex **r)
Free a Regex object.
Definition: regex.c:48
#define DT_REGEX_ALLOW_NOT
Regex can begin with '!'.
Definition: regex3.h:37
Cached regular expression.
Definition: regex3.h:90
char * pattern
printable version
Definition: regex3.h:91
bool pat_not
do not match
Definition: regex3.h:93
+ Here is the call graph for this function:

◆ slist_native_set()

static int slist_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Slist config item by Slist - Implements ConfigSetType::native_set() -.

Definition at line 141 of file slist.c.

143 {
144  if (!cs || !var || !cdef)
145  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
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  slist_free(var);
158 
159  struct Slist *list = slist_dup((struct Slist *) value);
160 
161  rc = CSR_SUCCESS;
162  if (!list)
163  rc |= CSR_SUC_EMPTY;
164 
165  *(struct Slist **) var = list;
166  return rc;
167 }
struct Slist * slist_dup(const struct Slist *list)
Create a copy of an Slist object.
Definition: slist.c:120
void slist_free(struct Slist **list)
Free an Slist object.
Definition: slist.c:162
String list.
Definition: slist.h:47
+ Here is the call graph for this function:

◆ sort_native_set()

static int sort_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a Sort config item by int - Implements ConfigSetType::native_set() -.

Definition at line 152 of file sort.c.

154 {
155  const char *str = NULL;
156 
157  str = mutt_map_get_name((value & SORT_MASK), (struct Mapping *) cdef->data);
158 
159  if (!str)
160  {
161  mutt_buffer_printf(err, _("Invalid sort type: %ld"), value);
162  return CSR_ERR_INVALID | CSR_INV_TYPE;
163  }
164 
165  if (value == (*(short *) var))
167 
168  if (cdef->validator)
169  {
170  int rc = cdef->validator(cs, cdef, value, err);
171 
172  if (CSR_RESULT(rc) != CSR_SUCCESS)
173  return rc | CSR_INV_VALIDATOR;
174  }
175 
176  *(short *) var = value;
177  return CSR_SUCCESS;
178 }
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78
Mapping between user-readable string and a constant.
Definition: mapping.h:32
+ Here is the call graph for this function:

◆ string_native_set()

static int string_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set a String config item by string - Implements ConfigSetType::native_set() -.

Definition at line 128 of file string.c.

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 }
static void string_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy a String - Implements ConfigSetType::destroy() -.
Definition: string.c:45
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:181
+ Here is the call graph for this function: