forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigvar.c
240 lines (214 loc) · 5.91 KB
/
configvar.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "config.h"
#include <assert.h>
#include <ccan/cast/cast.h>
#include <ccan/tal/str/str.h>
#include <common/configvar.h>
#include <common/utils.h>
#include <unistd.h>
struct configvar *configvar_new(const tal_t *ctx,
enum configvar_src src,
const char *file,
size_t linenum,
const char *configline)
{
struct configvar *cv = tal(ctx, struct configvar);
cv->file = tal_strdup_or_null(cv, file);
cv->src = src;
cv->linenum = linenum;
cv->configline = tal_strdup(cv, configline);
cv->overridden = false;
cv->optvar = NULL;
/* We fill in cv->optvar and cv->optarg when parsing! */
return cv;
}
const struct opt_table *configvar_unparsed(struct configvar *cv)
{
const struct opt_table *ot;
if (cv->src == CONFIGVAR_CMDLINE_SHORT) {
ot = opt_find_short(cv->configline[0]);
cv->optarg = NULL;
} else {
ot = opt_find_long(cv->configline, cast_const2(const char **, &cv->optarg));
}
if (!ot)
return NULL;
/* We get called multiple times, but we're expected to always
* finish the cv vars, even if they're added at the last minute
* on the cmdline, so we check this is only done once, and we
* do it even if we're not going to use it now. */
if (!cv->optvar) {
/* optvar is up to the = (i.e. one char before optarg) */
if (!cv->optarg)
cv->optvar = cv->configline;
else
cv->optvar = tal_strndup(cv, cv->configline,
cv->optarg - cv->configline - 1);
}
return ot;
}
static void trim_whitespace(char *s)
{
size_t len = strlen(s);
while (len > 0 && cisspace(s[len - 1]))
len--;
s[len] = '\0';
}
const char *configvar_parse(struct configvar *cv,
bool early,
bool full_knowledge,
bool developer)
{
const struct opt_table *ot;
ot = configvar_unparsed(cv);
if (!ot) {
/* Do we ignore unknown entries? */
if (!full_knowledge)
return NULL;
return "unknown option";
}
if ((ot->type & OPT_DEV) && !developer)
return "requires --developer";
/* If we're early and we want late, or vv, ignore. */
if (!!(ot->type & OPT_EARLY) != early)
return NULL;
if (ot->type & OPT_NOARG) {
/* MULTI doesn't make sense with single args */
assert(!(ot->type & OPT_MULTI));
if (cv->optarg)
return "doesn't allow an argument";
return ot->cb(ot->u.arg);
} else {
if (!cv->optarg)
return "requires an argument";
if (!(ot->type & OPT_KEEP_WHITESPACE))
trim_whitespace(cv->optarg);
return ot->cb_arg(cv->optarg, ot->u.arg);
}
}
/* This is O(N^2) but nobody cares */
void configvar_finalize_overrides(struct configvar **cvs)
{
/* Map to options: two different names can be the same option,
* given aliases! */
const struct opt_table **opts;
opts = tal_arr(tmpctx, const struct opt_table *, tal_count(cvs));
for (size_t i = 0; i < tal_count(cvs); i++) {
opts[i] = opt_find_long(cvs[i]->optvar, NULL);
/* If you're allowed multiple, they don't override */
if (opts[i]->type & OPT_MULTI)
continue;
for (size_t j = 0; j < i; j++) {
if (opts[j] == opts[i])
cvs[j]->overridden = true;
}
}
}
void configvar_remove(struct configvar ***cvs,
const char *name,
enum configvar_src src,
const char *optarg)
{
/* We remove all from this source, potentially restoring an overridden */
ssize_t prev = -1;
bool removed;
removed = false;
for (size_t i = 0; i < tal_count(*cvs); i++) {
/* This can happen if plugin fails during startup! */
if ((*cvs)[i]->optvar == NULL)
continue;
if (!streq((*cvs)[i]->optvar, name))
continue;
if (optarg && !streq((*cvs)[i]->optarg, optarg))
continue;
if ((*cvs)[i]->src == src) {
tal_free((*cvs)[i]);
tal_arr_remove(cvs, i);
i--;
removed = true;
continue;
}
/* Wrong type, correct name. */
prev = i;
}
/* Unmark prev if we removed overriding ones. If it's multi,
* this is a noop. */
if (removed && prev != -1)
(*cvs)[prev]->overridden = false;
}
struct configvar *configvar_dup(const tal_t *ctx, const struct configvar *cv)
{
struct configvar *ret;
if (taken(cv))
return tal_steal(ctx, cast_const(struct configvar *, cv));
ret = tal_dup(ctx, struct configvar, cv);
if (ret->file)
ret->file = tal_strdup(ret, ret->file);
if (ret->configline)
ret->configline = tal_strdup(ret, ret->configline);
if (ret->optvar) {
ret->optvar = tal_strdup(ret, ret->optvar);
/* Optarg, if non-NULL, points into cmdline! */
if (ret->optarg) {
size_t off = cv->optarg - cv->configline;
assert(off < strlen(cv->configline));
ret->optarg = ret->configline + off;
}
}
return ret;
}
struct configvar **configvar_join(const tal_t *ctx,
struct configvar **first,
struct configvar **second)
{
struct configvar **cvs;
size_t n = tal_count(first);
if (taken(first)) {
cvs = tal_steal(ctx, first);
tal_resize(&cvs, n + tal_count(second));
} else {
cvs = tal_arr(ctx, struct configvar *, n + tal_count(second));
for (size_t i = 0; i < n; i++) {
cvs[i] = configvar_dup(cvs, first[i]);
}
}
if (taken(second)) {
for (size_t i = 0; i < tal_count(second); i++)
cvs[n + i] = tal_steal(cvs, second[i]);
tal_free(second);
} else {
for (size_t i = 0; i < tal_count(second); i++)
cvs[n + i] = configvar_dup(cvs, second[i]);
}
return cvs;
}
static struct configvar *configvar_iter(struct configvar **cvs,
const char **names,
const struct configvar *firstcv)
{
for (size_t i = 0; i < tal_count(cvs); i++) {
/* Wait until we reach firstcv, if any */
if (firstcv) {
if (cvs[i] == firstcv)
firstcv = NULL;
continue;
}
for (size_t j = 0; j < tal_count(names); j++) {
/* In case we iterate before initialization! */
if (!cvs[i]->optvar)
continue;
if (streq(cvs[i]->optvar, names[j]) && !cvs[i]->overridden)
return cvs[i];
}
}
return NULL;
}
struct configvar *configvar_first(struct configvar **cvs, const char **names)
{
return configvar_iter(cvs, names, NULL);
}
struct configvar *configvar_next(struct configvar **cvs,
const struct configvar *cv,
const char **names)
{
return configvar_iter(cvs, names, cv);
}