NeoMutt  2019-11-11
Teaching an old dog new tricks
DOXYGEN
common.c
Go to the documentation of this file.
1 
23 #define TEST_NO_MAIN
24 #include "acutest.h"
25 #include "config.h"
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "mutt/mutt.h"
32 #include "config/lib.h"
33 #include "core/lib.h"
34 #include "common.h"
35 
36 const char *line = "----------------------------------------"
37  "----------------------------------------";
38 
39 bool dont_fail = false;
40 
41 int validator_fail(const struct ConfigSet *cs, const struct ConfigDef *cdef,
42  intptr_t value, struct Buffer *result)
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 }
53 
54 int validator_warn(const struct ConfigSet *cs, const struct ConfigDef *cdef,
55  intptr_t value, struct Buffer *result)
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 }
63 
64 int validator_succeed(const struct ConfigSet *cs, const struct ConfigDef *cdef,
65  intptr_t value, struct Buffer *result)
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 }
73 
74 void log_line(const char *fn)
75 {
76  int len = 44 - mutt_str_strlen(fn);
77  TEST_MSG("\033[36m---- %s %.*s\033[m\n", fn, len, line);
78 }
79 
80 void short_line(void)
81 {
82  TEST_MSG("%s\n", line + 40);
83 }
84 
86 {
87  if (!nc)
88  return -1;
89 
90  struct EventConfig *ec = (struct EventConfig *) nc->event;
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->cs, ec->he, &result);
103  else
104  cs_he_initial_get(ec->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 }
112 
113 void set_list(const struct ConfigSet *cs)
114 {
115  log_line(__func__);
116  cs_dump_set(cs);
117  log_line(__func__);
118 }
119 
120 int sort_list_cb(const void *a, const void *b)
121 {
122  const char *stra = *(char const *const *) a;
123  const char *strb = *(char const *const *) b;
124  return strcmp(stra, strb);
125 }
126 
127 void cs_dump_set(const struct ConfigSet *cs)
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_strdup(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_strdup(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 }
int sort_list_cb(const void *a, const void *b)
Definition: common.c:120
union HashKey key
Definition: hash.h:45
Type definition for a config item.
Definition: set.h:168
void log_line(const char *fn)
Definition: common.c:74
Container for lots of config items.
Definition: set.h:187
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct Hash * hash
HashTable storing the config items.
Definition: set.h:189
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
#define CSR_RESULT(x)
Definition: set.h:62
Data passed to a notification function.
Definition: observer.h:40
int validator_fail(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
Definition: common.c:41
Shared Testing Code.
Cursor to iterate through a Hash Table.
Definition: hash.h:96
A config-change event.
Definition: set.h:199
void set_list(const struct ConfigSet *cs)
Definition: common.c:113
String manipulation buffer.
Definition: buffer.h:33
int log_observer(struct NotifyCallback *nc)
Definition: common.c:85
#define TEST_MSG(...)
Definition: acutest.h:177
cst_string_get string_get
Initialise a variable from a string.
Definition: set.h:172
intptr_t event
Send: event data.
Definition: observer.h:46
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
const char * name
Name of this config item.
Definition: inheritance.h:34
bool dont_fail
Definition: common.c:39
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
Config item definition.
Definition: set.h:153
Convenience wrapper for the config headers.
An inherited config item.
Definition: inheritance.h:31
int event_subtype
Send: event subtype.
Definition: observer.h:45
size_t dsize
Length of data.
Definition: buffer.h:37
struct HashElem * he
Config item that changed.
Definition: set.h:202
void short_line(void)
Definition: common.c:80
const struct ConfigSet * cs
Config set.
Definition: set.h:201
const char * name
Definition: pgpmicalg.c:45
Convenience wrapper for the core headers.
const char * name
User-visible name.
Definition: set.h:155
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:46
const char * line
Definition: common.c:36
#define CSR_ERR_INVALID
Value hasn&#39;t been set.
Definition: set.h:49
char * data
Pointer to data.
Definition: buffer.h:35
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:513
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:650
void * data
Definition: hash.h:46
void * var
Pointer to the global variable.
Definition: set.h:157
struct HashElem * mutt_hash_walk(const struct Hash *table, struct HashWalkState *state)
Iterate through all the HashElem&#39;s in a Hash table.
Definition: hash.c:503
#define DT_SYNONYM
synonym for another variable
Definition: types.h:41
void cs_dump_set(const struct ConfigSet *cs)
Definition: common.c:127
const struct ConfigSetType * cs_get_type_def(const struct ConfigSet *cs, unsigned int type)
Get the definition for a type.
Definition: set.c:239
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
const char * name
Name of the type, e.g.
Definition: set.h:170
const char * strkey
Definition: hash.h:35
int validator_warn(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
Definition: common.c:54
int type
Definition: hash.h:44
#define CSR_SUC_WARNING
Notify the user of a warning.
Definition: set.h:54
#define FREE(x)
Definition: memory.h:40
The item stored in a Hash Table.
Definition: hash.h:42
int validator_succeed(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *result)
Definition: common.c:64
Config item&#39;s initial value has been set.
Definition: set.h:42
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
const char * name
Name of config item that changed.
Definition: set.h:203
#define DT_INHERITED
Config item is inherited.
Definition: types.h:78