NeoMutt  2020-08-07-1-gab41a1
Teaching an old dog new tricks
DOXYGEN
quad.c File Reference

Type representing a quad-option. More...

#include "config.h"
#include <stddef.h>
#include <limits.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "quad.h"
#include "set.h"
#include "subset.h"
#include "types.h"
+ Include dependency graph for quad.c:

Go to the source code of this file.

Functions

static int quad_string_set (const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
 Set a Quad-option by string - Implements ConfigSetType::string_set() More...
 
static int quad_string_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
 Get a Quad-option as a string - Implements ConfigSetType::string_get() 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 intptr_t quad_native_get (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Get an int object from a Quad-option config item - Implements ConfigSetType::native_get() More...
 
static int quad_reset (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
 Reset a Quad-option to its initial value - Implements ConfigSetType::reset() More...
 
void quad_init (struct ConfigSet *cs)
 Register the Quad-option config type. More...
 
static int quad_toggle (int opt)
 Toggle (invert) the value of a quad option. More...
 
int quad_he_toggle (struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
 Toggle the value of a quad. More...
 
int quad_str_toggle (struct ConfigSubset *sub, const char *name, struct Buffer *err)
 Toggle the value of a quad. More...
 

Variables

const char * QuadValues []
 Valid strings for creating a QuadValue. More...
 

Detailed Description

Type representing a quad-option.

Authors
  • Richard Russon

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/.

Definition in file quad.c.

Function Documentation

◆ quad_string_set()

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

Set a Quad-option by string - Implements ConfigSetType::string_set()

Definition at line 55 of file quad.c.

57 {
58  if (!value)
59  return CSR_ERR_CODE; /* LCOV_EXCL_LINE */
60 
61  int num = -1;
62  for (size_t i = 0; QuadValues[i]; i++)
63  {
64  if (mutt_istr_equal(QuadValues[i], value))
65  {
66  num = i;
67  break;
68  }
69  }
70 
71  if (num < 0)
72  {
73  mutt_buffer_printf(err, _("Invalid quad value: %s"), value);
75  }
76 
77  if (var)
78  {
79  if (num == (*(char *) var))
81 
82  if (cdef->validator)
83  {
84  int rc = cdef->validator(cs, cdef, (intptr_t) num, err);
85 
86  if (CSR_RESULT(rc) != CSR_SUCCESS)
87  return rc | CSR_INV_VALIDATOR;
88  }
89 
90  *(char *) var = num;
91  }
92  else
93  {
94  cdef->initial = num;
95  }
96 
97  return CSR_SUCCESS;
98 }
#define CSR_RESULT(x)
Definition: set.h:52
intptr_t initial
Initial value.
Definition: set.h:66
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:888
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quad_string_get()

static int quad_string_get ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer result 
)
static

Get a Quad-option as a string - Implements ConfigSetType::string_get()

Definition at line 103 of file quad.c.

105 {
106  unsigned int value;
107 
108  if (var)
109  value = *(char *) var;
110  else
111  value = (int) cdef->initial;
112 
113  if (value >= (mutt_array_size(QuadValues) - 1))
114  {
115  mutt_debug(LL_DEBUG1, "Variable has an invalid value: %d\n", value);
116  return CSR_ERR_INVALID | CSR_INV_TYPE;
117  }
118 
119  mutt_buffer_addstr(result, QuadValues[value]);
120  return CSR_SUCCESS;
121 }
if(!test_colorize_)
Definition: acutest.h:499
intptr_t initial
Initial value.
Definition: set.h:66
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
#define mutt_array_size(x)
Definition: memory.h:33
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
Log at debug level 1.
Definition: logging.h:40
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
+ Here is the call graph for this function:
+ Here is the caller 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 126 of file quad.c.

128 {
129  if ((value < 0) || (value >= (mutt_array_size(QuadValues) - 1)))
130  {
131  mutt_buffer_printf(err, _("Invalid quad value: %ld"), value);
132  return CSR_ERR_INVALID | CSR_INV_TYPE;
133  }
134 
135  if (value == (*(char *) var))
137 
138  if (cdef->validator)
139  {
140  int rc = cdef->validator(cs, cdef, value, err);
141 
142  if (CSR_RESULT(rc) != CSR_SUCCESS)
143  return rc | CSR_INV_VALIDATOR;
144  }
145 
146  *(char *) var = value;
147  return CSR_SUCCESS;
148 }
#define CSR_RESULT(x)
Definition: set.h:52
#define _(a)
Definition: message.h:28
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * QuadValues[]
Valid strings for creating a QuadValue.
Definition: quad.c:48
#define mutt_array_size(x)
Definition: memory.h:33
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
#define CSR_INV_TYPE
Value is not valid for the type.
Definition: set.h:47
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quad_native_get()

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

Get an int object from a Quad-option config item - Implements ConfigSetType::native_get()

Definition at line 153 of file quad.c.

155 {
156  return *(char *) var;
157 }
+ Here is the caller graph for this function:

◆ quad_reset()

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

Reset a Quad-option to its initial value - Implements ConfigSetType::reset()

Definition at line 162 of file quad.c.

164 {
165  if (cdef->initial == (*(char *) var))
167 
168  if (cdef->validator)
169  {
170  int rc = cdef->validator(cs, cdef, cdef->initial, err);
171 
172  if (CSR_RESULT(rc) != CSR_SUCCESS)
173  return rc | CSR_INV_VALIDATOR;
174  }
175 
176  *(char *) var = cdef->initial;
177  return CSR_SUCCESS;
178 }
#define CSR_RESULT(x)
Definition: set.h:52
intptr_t initial
Initial value.
Definition: set.h:66
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
+ Here is the caller graph for this function:

◆ quad_init()

void quad_init ( struct ConfigSet cs)

Register the Quad-option config type.

Parameters
csConfig items

Definition at line 184 of file quad.c.

185 {
186  const struct ConfigSetType cst_quad = {
187  "quad",
192  NULL, // string_plus_equals
193  NULL, // string_minus_equals
194  quad_reset,
195  NULL, // destroy
196  };
197  cs_register_type(cs, DT_QUAD, &cst_quad);
198 }
Type definition for a config item.
Definition: set.h:88
static int quad_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a Quad-option as a string - Implements ConfigSetType::string_get()
Definition: quad.c:103
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37
static int quad_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set a Quad-option by string - Implements ConfigSetType::string_set()
Definition: quad.c:55
static int quad_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset a Quad-option to its initial value - Implements ConfigSetType::reset()
Definition: quad.c:162
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()
Definition: quad.c:126
bool cs_register_type(struct ConfigSet *cs, unsigned int type, const struct ConfigSetType *cst)
Register a type of config item.
Definition: set.c:259
static intptr_t quad_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an int object from a Quad-option config item - Implements ConfigSetType::native_get() ...
Definition: quad.c:153
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quad_toggle()

static int quad_toggle ( int  opt)
static

Toggle (invert) the value of a quad option.

Parameters
optValue to toggle

By toggling the low bit, the following are swapped:

Definition at line 208 of file quad.c.

209 {
210  return opt ^= 1;
211 }
+ Here is the caller graph for this function:

◆ quad_he_toggle()

int quad_he_toggle ( struct ConfigSubset sub,
struct HashElem he,
struct Buffer err 
)

Toggle the value of a quad.

Parameters
subConfig subset
heHashElem representing config item
errBuffer for error messages
Return values
numResult, e.g. CSR_SUCCESS
See also
quad_toggle()

Definition at line 222 of file quad.c.

223 {
224  if (!sub || !he || !he->data)
225  return CSR_ERR_CODE;
226 
227  struct HashElem *he_base = cs_get_base(he);
228  if (DTYPE(he_base->type) != DT_QUAD)
229  return CSR_ERR_CODE;
230 
231  intptr_t value = cs_he_native_get(sub->cs, he, err);
232  if (value == INT_MIN)
233  return CSR_ERR_CODE;
234 
235  value = quad_toggle(value);
236  int rc = cs_he_native_set(sub->cs, he, value, err);
237 
238  if ((CSR_RESULT(rc) == CSR_SUCCESS) && !(rc & CSR_SUC_NO_CHANGE))
240 
241  return rc;
242 }
static int quad_toggle(int opt)
Toggle (invert) the value of a quad option.
Definition: quad.c:208
#define CSR_RESULT(x)
Definition: set.h:52
void cs_subset_notify_observers(const struct ConfigSubset *sub, struct HashElem *he, enum NotifyConfig ev)
Notify all observers of an event.
Definition: subset.c:218
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:199
#define DT_QUAD
quad-option (no/yes/ask-no/ask-yes)
Definition: types.h:37
#define DTYPE(x)
Mask for the Data Type.
Definition: types.h:44
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
int cs_he_native_set(const struct ConfigSet *cs, struct HashElem *he, intptr_t value, struct Buffer *err)
Natively set the value of a HashElem config item.
Definition: set.c:707
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
Config item has been set.
Definition: subset.h:62
void * data
User-supplied data.
Definition: hash.h:47
#define CSR_ERR_CODE
Problem with the code.
Definition: set.h:36
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
The item stored in a Hash Table.
Definition: hash.h:43
intptr_t cs_he_native_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *err)
Natively get the value of a HashElem config item.
Definition: set.c:818
#define CSR_SUC_NO_CHANGE
The value hasn&#39;t changed.
Definition: set.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ quad_str_toggle()

int quad_str_toggle ( struct ConfigSubset sub,
const char *  name,
struct Buffer err 
)

Toggle the value of a quad.

Parameters
subConfig subset
nameHashElem representing config item
errBuffer for error messages
Return values
numResult, e.g. CSR_SUCCESS
See also
quad_toggle()

Definition at line 253 of file quad.c.

254 {
255  struct HashElem *he = cs_subset_create_inheritance(sub, name);
256 
257  return quad_he_toggle(sub, he, err);
258 }
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:189
int quad_he_toggle(struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Toggle the value of a quad.
Definition: quad.c:222
The item stored in a Hash Table.
Definition: hash.h:43
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ QuadValues

const char* QuadValues[]
Initial value:
= {
"no", "yes", "ask-no", "ask-yes", NULL,
}

Valid strings for creating a QuadValue.

These strings are case-insensitive.

Definition at line 48 of file quad.c.