forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
param.c
347 lines (305 loc) · 8.12 KB
/
param.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include <ccan/asort/asort.h>
#include <ccan/tal/str/str.h>
#include <common/json_command.h>
#include <common/jsonrpc_errors.h>
#include <common/param.h>
#include <common/utils.h>
struct param {
const char *name;
bool is_set;
bool required;
param_cbx cbx;
void *arg;
};
static bool param_add(struct param **params,
const char *name, bool required,
param_cbx cbx, void *arg)
{
#if DEVELOPER
if (!(name && cbx && arg))
return false;
#endif
struct param last;
last.is_set = false;
last.name = name;
last.required = required;
last.cbx = cbx;
last.arg = arg;
tal_arr_expand(params, last);
return true;
}
static struct command_result *make_callback(struct command *cmd,
struct param *def,
const char *buffer,
const jsmntok_t *tok)
{
def->is_set = true;
return def->cbx(cmd, def->name, buffer, tok, def->arg);
}
static struct command_result *post_check(struct command *cmd,
struct param *params)
{
struct param *first = params;
struct param *last = first + tal_count(params);
/* Make sure required params were provided. */
while (first != last && first->required) {
if (!first->is_set) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"missing required parameter: %s",
first->name);
}
first++;
}
return NULL;
}
static struct command_result *parse_by_position(struct command *cmd,
struct param *params,
const char *buffer,
const jsmntok_t tokens[],
bool allow_extra)
{
struct command_result *res;
const jsmntok_t *tok;
size_t i;
json_for_each_arr(i, tok, tokens) {
/* check for unexpected trailing params */
if (i == tal_count(params)) {
if (!allow_extra) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"too many parameters:"
" got %u, expected %zu",
tokens->size,
tal_count(params));
}
break;
}
if (!json_tok_is_null(buffer, tok)) {
res = make_callback(cmd, params+i, buffer, tok);
if (res)
return res;
}
}
return post_check(cmd, params);
}
static struct param *find_param(struct param *params, const char *start,
size_t n)
{
struct param *first = params;
struct param *last = first + tal_count(params);
while (first != last) {
if (strncmp(first->name, start, n) == 0)
if (strlen(first->name) == n)
return first;
first++;
}
return NULL;
}
static struct command_result *parse_by_name(struct command *cmd,
struct param *params,
const char *buffer,
const jsmntok_t tokens[],
bool allow_extra)
{
size_t i;
const jsmntok_t *t;
json_for_each_obj(i, t, tokens) {
struct param *p = find_param(params, buffer + t->start,
t->end - t->start);
if (!p) {
if (!allow_extra) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"unknown parameter: %.*s",
t->end - t->start,
buffer + t->start);
}
} else {
struct command_result *res;
if (p->is_set) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"duplicate json names: %s",
p->name);
}
res = make_callback(cmd, p, buffer, t + 1);
if (res)
return res;
}
}
return post_check(cmd, params);
}
#if DEVELOPER
static int comp_by_name(const struct param *a, const struct param *b,
void *unused)
{
return strcmp(a->name, b->name);
}
static int comp_by_arg(const struct param *a, const struct param *b,
void *unused)
{
/* size_t could be larger than int: don't turn a 4bn difference into 0 */
if (a->arg > b->arg)
return 1;
else if (a->arg < b->arg)
return -1;
return 0;
}
/* This comparator is a bit different, but works well.
* Return 0 if @a is optional and @b is required. Otherwise return 1.
*/
static int comp_req_order(const struct param *a, const struct param *b,
void *unused)
{
if (!a->required && b->required)
return 0;
return 1;
}
/*
* Make sure 2 sequential items in @params are not equal (based on
* provided comparator).
*/
static bool check_distinct(struct param *params,
int (*compar) (const struct param *a,
const struct param *b, void *unused))
{
struct param *first = params;
struct param *last = first + tal_count(params);
first++;
while (first != last) {
if (compar(first - 1, first, NULL) == 0)
return false;
first++;
}
return true;
}
static bool check_unique(struct param *copy,
int (*compar) (const struct param *a,
const struct param *b, void *unused))
{
asort(copy, tal_count(copy), compar, NULL);
return check_distinct(copy, compar);
}
/*
* Verify consistent internal state.
*/
static bool check_params(struct param *params)
{
if (tal_count(params) < 2)
return true;
/* make sure there are no required params following optional */
if (!check_distinct(params, comp_req_order))
return false;
/* duplicate so we can sort */
struct param *copy = tal_dup_arr(params, struct param,
params, tal_count(params), 0);
/* check for repeated names and args */
if (!check_unique(copy, comp_by_name))
return false;
if (!check_unique(copy, comp_by_arg))
return false;
tal_free(copy);
return true;
}
#endif
static char *param_usage(const tal_t *ctx,
const struct param *params)
{
char *usage = tal_strdup(ctx, "");
for (size_t i = 0; i < tal_count(params); i++) {
if (i != 0)
tal_append_fmt(&usage, " ");
if (params[i].required)
tal_append_fmt(&usage, "%s", params[i].name);
else
tal_append_fmt(&usage, "[%s]", params[i].name);
}
return usage;
}
static struct command_result *param_arr(struct command *cmd, const char *buffer,
const jsmntok_t tokens[],
struct param *params,
bool allow_extra)
{
#if DEVELOPER
if (!check_params(params)) {
return command_fail(cmd, PARAM_DEV_ERROR,
"developer error: check_params");
}
#endif
if (tokens->type == JSMN_ARRAY)
return parse_by_position(cmd, params, buffer, tokens, allow_extra);
else if (tokens->type == JSMN_OBJECT)
return parse_by_name(cmd, params, buffer, tokens, allow_extra);
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Expected array or object for params");
}
#include <stdio.h>
const char *param_subcommand(struct command *cmd, const char *buffer,
const jsmntok_t tokens[],
const char *name, ...)
{
va_list ap;
struct param *params = tal_arr(cmd, struct param, 0);
const char *arg, **names = tal_arr(tmpctx, const char *, 1);
const char *subcmd;
param_add(¶ms, "subcommand", true, (void *)param_string, &subcmd);
names[0] = name;
va_start(ap, name);
while ((arg = va_arg(ap, const char *)) != NULL)
tal_arr_expand(&names, arg);
va_end(ap);
if (command_usage_only(cmd)) {
char *usage = tal_strdup(cmd, "subcommand");
for (size_t i = 0; i < tal_count(names); i++)
tal_append_fmt(&usage, "%c%s",
i == 0 ? '=' : '|', names[i]);
command_set_usage(cmd, usage);
return NULL;
}
/* Check it's valid */
if (param_arr(cmd, buffer, tokens, params, true) != NULL) {
return NULL;
}
/* Check it's one of the known ones. */
for (size_t i = 0; i < tal_count(names); i++)
if (streq(subcmd, names[i]))
return subcmd;
/* We really do ignore this. */
if (command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Unknown subcommand '%s'", subcmd))
;
return NULL;
}
bool param(struct command *cmd, const char *buffer,
const jsmntok_t tokens[], ...)
{
struct param *params = tal_arr(cmd, struct param, 0);
const char *name;
va_list ap;
bool allow_extra = false;
va_start(ap, tokens);
while ((name = va_arg(ap, const char *)) != NULL) {
bool required = va_arg(ap, int);
param_cbx cbx = va_arg(ap, param_cbx);
void *arg = va_arg(ap, void *);
if (streq(name, "")) {
allow_extra = true;
continue;
}
if (!param_add(¶ms, name, required, cbx, arg)) {
/* We really do ignore this return! */
if (command_fail(cmd, PARAM_DEV_ERROR,
"developer error: param_add %s", name))
;
va_end(ap);
return false;
}
}
va_end(ap);
if (command_usage_only(cmd)) {
command_set_usage(cmd, param_usage(cmd, params));
return false;
}
/* Always return false if we're simply checking command parameters;
* normally this returns true if all parameters are valid. */
return param_arr(cmd, buffer, tokens, params, allow_extra) == NULL
&& !command_check_only(cmd);
}