NeoMutt  2020-06-26-89-g172cd3
Teaching an old dog new tricks
DOXYGEN
common.h File Reference

Shared Testing Code. More...

#include <stdbool.h>
#include <stdint.h>
#include "config/lib.h"
+ Include dependency graph for common.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int validator_succeed (const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
 
int validator_warn (const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
 
int validator_fail (const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
 
void log_line (const char *fn)
 
void short_line (void)
 
int log_observer (struct NotifyCallback *nc)
 
void set_list (const struct ConfigSet *cs)
 
void cs_dump_set (const struct ConfigSet *cs)
 

Variables

const char * line
 
bool dont_fail
 

Detailed Description

Shared Testing Code.

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 common.h.

Function Documentation

◆ validator_succeed()

int validator_succeed ( const struct ConfigSet cs,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer result 
)

Definition at line 64 of file common.c.

66 {
67  if (value > 1000000)
68  mutt_buffer_printf(result, "%s: %s, (ptr)", __func__, cdef->name);
69  else
70  mutt_buffer_printf(result, "%s: %s, %ld", __func__, cdef->name, value);
71  return CSR_SUCCESS;
72 }
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * name
User-visible name.
Definition: set.h:65
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
+ Here is the call graph for this function:

◆ validator_warn()

int validator_warn ( const struct ConfigSet cs,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer result 
)

Definition at line 54 of file common.c.

56 {
57  if (value > 1000000)
58  mutt_buffer_printf(result, "%s: %s, (ptr)", __func__, cdef->name);
59  else
60  mutt_buffer_printf(result, "%s: %s, %ld", __func__, cdef->name, value);
62 }
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * name
User-visible name.
Definition: set.h:65
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define CSR_SUC_WARNING
Notify the user of a warning.
Definition: set.h:43
+ Here is the call graph for this function:

◆ validator_fail()

int validator_fail ( const struct ConfigSet cs,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer result 
)

Definition at line 41 of file common.c.

43 {
44  if (dont_fail)
45  return CSR_SUCCESS;
46 
47  if (value > 1000000)
48  mutt_buffer_printf(result, "%s: %s, (ptr)", __func__, cdef->name);
49  else
50  mutt_buffer_printf(result, "%s: %s, %ld", __func__, cdef->name, value);
51  return CSR_ERR_INVALID;
52 }
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
const char * name
User-visible name.
Definition: set.h:65
bool dont_fail
Definition: common.c:39
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:38
+ Here is the call graph for this function:

◆ log_line()

void log_line ( const char *  fn)

Definition at line 74 of file common.c.

75 {
76  int len = 44 - mutt_str_len(fn);
77  TEST_MSG("\033[36m---- %s %.*s\033[m\n", fn, len, line);
78 }
#define TEST_MSG(...)
Definition: acutest.h:214
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:636
const char * line
Definition: common.c:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ short_line()

void short_line ( void  )

Definition at line 80 of file common.c.

81 {
82  TEST_MSG("%s\n", line + 40);
83 }
#define TEST_MSG(...)
Definition: acutest.h:214
const char * line
Definition: common.c:36
+ Here is the caller graph for this function:

◆ log_observer()

int log_observer ( struct NotifyCallback nc)

Definition at line 85 of file common.c.

86 {
87  if (!nc)
88  return -1;
89 
90  struct EventConfig *ec = nc->event_data;
91 
92  struct Buffer result;
93  mutt_buffer_init(&result);
94  result.dsize = 256;
95  result.data = mutt_mem_calloc(1, result.dsize);
96 
97  const char *events[] = { "set", "reset", "initial-set" };
98 
99  mutt_buffer_reset(&result);
100 
102  cs_he_string_get(ec->sub->cs, ec->he, &result);
103  else
104  cs_he_initial_get(ec->sub->cs, ec->he, &result);
105 
106  TEST_MSG("Event: %s has been %s to '%s'\n", ec->name,
107  events[nc->event_subtype - 1], result.data);
108 
109  FREE(&result.data);
110  return true;
111 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
Config item&#39;s initial value has been set.
Definition: subset.h:64
A config-change event.
Definition: subset.h:70
String manipulation buffer.
Definition: buffer.h:33
#define TEST_MSG(...)
Definition: acutest.h:214
int event_subtype
Send: Event subtype, e.g. NT_ACCOUNT_ADD.
Definition: observer.h:42
struct ConfigSet * cs
Parent ConfigSet.
Definition: subset.h:51
struct HashElem * he
Config item that changed.
Definition: subset.h:74
int cs_he_initial_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get the initial, or parent, value of a config item.
Definition: set.c:486
void * event_data
Data from notify_send()
Definition: observer.h:43
int cs_he_string_get(const struct ConfigSet *cs, struct HashElem *he, struct Buffer *result)
Get a config item as a string.
Definition: set.c:620
#define FREE(x)
Definition: memory.h:40
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
const struct ConfigSubset * sub
Config Subset.
Definition: subset.h:72
const char * name
Name of config item that changed.
Definition: subset.h:73
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ set_list()

void set_list ( const struct ConfigSet cs)

Definition at line 113 of file common.c.

114 {
115  log_line(__func__);
116  cs_dump_set(cs);
117  log_line(__func__);
118 }
void cs_dump_set(const struct ConfigSet *cs)
Definition: common.c:127
void log_line(const char *fn)
Definition: common.c:74
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cs_dump_set()

void cs_dump_set ( const struct ConfigSet cs)

Definition at line 127 of file common.c.

128 {
129  if (!cs)
130  return;
131 
132  struct HashElem *he = NULL;
133  struct HashWalkState state;
134  memset(&state, 0, sizeof(state));
135 
136  struct Buffer result;
137  mutt_buffer_init(&result);
138  result.dsize = 256;
139  result.data = mutt_mem_calloc(1, result.dsize);
140 
141  char tmp[128];
142  char *list[26] = { 0 };
143  size_t index = 0;
144  size_t i = 0;
145 
146  for (i = 0; (he = mutt_hash_walk(cs->hash, &state)); i++)
147  {
148  if (he->type == DT_SYNONYM)
149  continue;
150 
151  const char *name = NULL;
152 
153  if (he->type & DT_INHERITED)
154  {
155  struct Inheritance *inh = he->data;
156  he = inh->parent;
157  name = inh->name;
158  }
159  else
160  {
161  name = he->key.strkey;
162  }
163 
164  const struct ConfigSetType *cst = cs_get_type_def(cs, he->type);
165  if (!cst)
166  {
167  snprintf(tmp, sizeof(tmp), "Unknown type: %d", he->type);
168  list[index] = mutt_str_dup(tmp);
169  index++;
170  continue;
171  }
172 
173  mutt_buffer_reset(&result);
174  const struct ConfigDef *cdef = he->data;
175 
176  int rc = cst->string_get(cs, cdef->var, cdef, &result);
177  if (CSR_RESULT(rc) == CSR_SUCCESS)
178  snprintf(tmp, sizeof(tmp), "%s %s = %s", cst->name, name, result.data);
179  else
180  snprintf(tmp, sizeof(tmp), "%s %s: ERROR: %s", cst->name, name, result.data);
181  list[index] = mutt_str_dup(tmp);
182  index++;
183  }
184 
185  qsort(list, index, sizeof(list[0]), sort_list_cb);
186  for (i = 0; list[i]; i++)
187  {
188  TEST_MSG("%s\n", list[i]);
189  FREE(&list[i]);
190  }
191 
192  FREE(&result.data);
193 }
union HashKey key
Key representing the data.
Definition: hash.h:46
Type definition for a config item.
Definition: set.h:88
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
#define CSR_RESULT(x)
Definition: set.h:52
int(* string_get)(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get a config item as a string.
Definition: set.h:124
Cursor to iterate through a Hash Table.
Definition: hash.h:119
String manipulation buffer.
Definition: buffer.h:33
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:375
#define TEST_MSG(...)
Definition: acutest.h:214
int sort_list_cb(const void *a, const void *b)
Definition: common.c:120
const char * name
Name of this config item.
Definition: inheritance.h:34
Config item definition.
Definition: set.h:63
An inherited config item.
Definition: inheritance.h:31
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
struct HashTable * hash
HashTable storing the config items.
Definition: set.h:229
void * data
User-supplied data.
Definition: hash.h:47
void * var
Pointer to the global variable.
Definition: set.h:67
#define DT_SYNONYM
synonym for another variable
Definition: types.h:42
const struct ConfigSetType * cs_get_type_def(const struct ConfigSet *cs, unsigned int type)
Get the definition for a type.
Definition: set.c:229
const char * name
Name of the type, e.g. "String".
Definition: set.h:90
const char * strkey
String key.
Definition: hash.h:36
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
#define FREE(x)
Definition: memory.h:40
struct HashElem * mutt_hash_walk(const struct HashTable *table, struct HashWalkState *state)
Iterate through all the HashElem&#39;s in a Hash Table.
Definition: hash.c:479
The item stored in a Hash Table.
Definition: hash.h:43
struct HashElem * parent
HashElem of parent config item.
Definition: inheritance.h:33
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:46
#define DT_INHERITED
Config item is inherited.
Definition: types.h:80
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ line

const char* line

Definition at line 617 of file acutest.h.

◆ dont_fail

bool dont_fail

Definition at line 39 of file common.c.