NeoMutt  2019-12-07
Teaching an old dog new tricks
DOXYGEN
myvar.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stddef.h>
31 #include "mutt/mutt.h"
32 #include "myvar.h"
33 
34 struct MyVarList MyVars = TAILQ_HEAD_INITIALIZER(MyVars);
35 
44 static struct MyVar *myvar_new(const char *name, const char *value)
45 {
46  struct MyVar *myv = mutt_mem_calloc(1, sizeof(struct MyVar));
47  myv->name = mutt_str_strdup(name);
48  myv->value = mutt_str_strdup(value);
49  return myv;
50 }
51 
56 static void myvar_free(struct MyVar **ptr)
57 {
58  if (!ptr || !*ptr)
59  return;
60 
61  struct MyVar *myv = *ptr;
62  FREE(&myv->name);
63  FREE(&myv->value);
64  FREE(ptr);
65 }
66 
73 const char *myvar_get(const char *var)
74 {
75  struct MyVar *myv = NULL;
76 
77  TAILQ_FOREACH(myv, &MyVars, entries)
78  {
79  if (mutt_str_strcmp(myv->name, var) == 0)
80  return NONULL(myv->value);
81  }
82 
83  return NULL;
84 }
85 
91 void myvar_set(const char *var, const char *val)
92 {
93  struct MyVar *myv = NULL;
94 
95  TAILQ_FOREACH(myv, &MyVars, entries)
96  {
97  if (mutt_str_strcmp(myv->name, var) == 0)
98  {
99  mutt_str_replace(&myv->value, val);
100  return;
101  }
102  }
103 
104  myv = myvar_new(var, val);
105  TAILQ_INSERT_TAIL(&MyVars, myv, entries);
106 }
107 
112 void myvar_del(const char *var)
113 {
114  struct MyVar *myv = NULL;
115 
116  TAILQ_FOREACH(myv, &MyVars, entries)
117  {
118  if (mutt_str_strcmp(myv->name, var) == 0)
119  {
120  TAILQ_REMOVE(&MyVars, myv, entries);
121  myvar_free(&myv);
122  return;
123  }
124  }
125 }
126 
131 void myvarlist_free(struct MyVarList *list)
132 {
133  if (!list)
134  return;
135 
136  struct MyVar *myv = NULL;
137  struct MyVar *tmp = NULL;
138  TAILQ_FOREACH_SAFE(myv, list, entries, tmp)
139  {
140  TAILQ_REMOVE(list, myv, entries);
141  myvar_free(&myv);
142  }
143 }
static void myvar_free(struct MyVar **ptr)
Free a MyVar.
Definition: myvar.c:56
#define NONULL(x)
Definition: string2.h:37
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:719
static struct MyVar * myvar_new(const char *name, const char *value)
Create a new MyVar.
Definition: myvar.c:44
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:729
char * name
Name of user variable.
Definition: myvar.h:33
const char * name
Definition: pgpmicalg.c:46
A user-set variable.
Definition: myvar.h:31
struct MyVarList MyVars
List of all the user&#39;s custom config variables.
Definition: myvar.c:34
char * value
Value of user variable.
Definition: myvar.h:34
void myvar_del(const char *var)
Unset the value of a "my_" variable.
Definition: myvar.c:112
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:821
void myvarlist_free(struct MyVarList *list)
Free a List of MyVars.
Definition: myvar.c:131
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:803
void myvar_set(const char *var, const char *val)
Set the value of a "my_" variable.
Definition: myvar.c:91
void mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:453
const char * myvar_get(const char *var)
Get the value of a "my_" variable.
Definition: myvar.c:73
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:380
#define FREE(x)
Definition: memory.h:40
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:631
Handling of personal config (&#39;my&#39; variables)
int mutt_str_strcmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:615