forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyvar.c
143 lines (127 loc) · 3.1 KB
/
myvar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @file
* Handling of personal config ('my' variables)
*
* @authors
* Copyright (C) 2018 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page myvar Handling of personal config ('my' variables)
*
* Handling of personal config ('my' variables)
*/
#include "config.h"
#include <stddef.h>
#include "mutt/mutt.h"
#include "myvar.h"
struct MyVarList MyVars = TAILQ_HEAD_INITIALIZER(MyVars);
/**
* myvar_new - Create a new MyVar
* @param name Variable name
* @param value Variable value
* @retval ptr New MyVar
*
* @note The name and value will be copied.
*/
static struct MyVar *myvar_new(const char *name, const char *value)
{
struct MyVar *myv = mutt_mem_calloc(1, sizeof(struct MyVar));
myv->name = mutt_str_strdup(name);
myv->value = mutt_str_strdup(value);
return myv;
}
/**
* myvar_free - Free a MyVar
* @param ptr MyVar to free
*/
static void myvar_free(struct MyVar **ptr)
{
if (!ptr || !*ptr)
return;
struct MyVar *myv = *ptr;
FREE(&myv->name);
FREE(&myv->value);
FREE(ptr);
}
/**
* myvar_get - Get the value of a "my_" variable
* @param var Variable name
* @retval ptr Success, value of variable
* @retval NULL Error, variable doesn't exist
*/
const char *myvar_get(const char *var)
{
struct MyVar *myv = NULL;
TAILQ_FOREACH(myv, &MyVars, entries)
{
if (mutt_str_strcmp(myv->name, var) == 0)
return NONULL(myv->value);
}
return NULL;
}
/**
* myvar_set - Set the value of a "my_" variable
* @param var Variable name
* @param val Value to set
*/
void myvar_set(const char *var, const char *val)
{
struct MyVar *myv = NULL;
TAILQ_FOREACH(myv, &MyVars, entries)
{
if (mutt_str_strcmp(myv->name, var) == 0)
{
mutt_str_replace(&myv->value, val);
return;
}
}
myv = myvar_new(var, val);
TAILQ_INSERT_TAIL(&MyVars, myv, entries);
}
/**
* myvar_del - Unset the value of a "my_" variable
* @param var Variable name
*/
void myvar_del(const char *var)
{
struct MyVar *myv = NULL;
TAILQ_FOREACH(myv, &MyVars, entries)
{
if (mutt_str_strcmp(myv->name, var) == 0)
{
TAILQ_REMOVE(&MyVars, myv, entries);
myvar_free(&myv);
return;
}
}
}
/**
* myvarlist_free - Free a List of MyVars
* @param list List of MyVars
*/
void myvarlist_free(struct MyVarList *list)
{
if (!list)
return;
struct MyVar *myv = NULL;
struct MyVar *tmp = NULL;
TAILQ_FOREACH_SAFE(myv, list, entries, tmp)
{
TAILQ_REMOVE(list, myv, entries);
myvar_free(&myv);
}
}