2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "json_script.h"
23 struct json_script_ctx *ctx;
24 struct blob_attr *vars;
30 int (*cb)(struct json_call *call, struct blob_attr *cur);
33 static int json_process_expr(struct json_call *call, struct blob_attr *cur);
34 static int json_process_cmd(struct json_call *call, struct blob_attr *cur);
36 struct json_script_file *
37 json_script_file_from_blobmsg(const char *name, void *data, int len)
39 struct json_script_file *f;
44 name_len = strlen(name) + 1;
46 f = calloc_a(sizeof(*f) + len, &new_name, name_len);
47 memcpy(f->data, data, len);
49 f->avl.key = strcpy(new_name, name);
54 static struct json_script_file *
55 json_script_get_file(struct json_script_ctx *ctx, const char *filename)
57 struct json_script_file *f;
59 f = avl_find_element(&ctx->files, filename, f, avl);
63 f = ctx->handle_file(ctx, filename);
67 avl_insert(&ctx->files, &f->avl);
71 static void __json_script_run(struct json_call *call, struct json_script_file *file,
72 struct blob_attr *context)
74 struct json_script_ctx *ctx = call->ctx;
76 if (file->seq == call->seq) {
78 ctx->handle_error(ctx, "Recursive include", context);
83 file->seq = call->seq;
85 json_process_cmd(call, file->data);
90 const char *json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *vars,
93 struct blob_attr *cur;
96 blobmsg_for_each_attr(cur, vars, rem) {
97 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
100 if (strcmp(blobmsg_name(cur), name) != 0)
103 return blobmsg_data(cur);
106 return ctx->handle_var(ctx, name, vars);
110 msg_find_var(struct json_call *call, const char *name)
112 return json_script_find_var(call->ctx, call->vars, name);
116 json_get_tuple(struct blob_attr *cur, struct blob_attr **tb, int t1, int t2)
118 static struct blobmsg_policy expr_tuple[3] = {
119 { .type = BLOBMSG_TYPE_STRING },
124 expr_tuple[1].type = t1;
125 expr_tuple[2].type = t2;
126 blobmsg_parse_array(expr_tuple, 3, tb, blobmsg_data(cur), blobmsg_data_len(cur));
129 static int handle_if(struct json_call *call, struct blob_attr *expr)
131 struct blob_attr *tb[4];
134 static const struct blobmsg_policy if_tuple[4] = {
135 { .type = BLOBMSG_TYPE_STRING },
136 { .type = BLOBMSG_TYPE_ARRAY },
137 { .type = BLOBMSG_TYPE_ARRAY },
138 { .type = BLOBMSG_TYPE_ARRAY },
141 blobmsg_parse_array(if_tuple, 4, tb, blobmsg_data(expr), blobmsg_data_len(expr));
143 if (!tb[1] || !tb[2])
146 ret = json_process_expr(call, tb[1]);
151 return json_process_cmd(call, tb[2]);
156 return json_process_cmd(call, tb[3]);
159 static int handle_case(struct json_call *call, struct blob_attr *expr)
161 struct blob_attr *tb[3], *cur;
165 json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, BLOBMSG_TYPE_TABLE);
166 if (!tb[1] || !tb[2])
169 var = msg_find_var(call, blobmsg_data(tb[1]));
173 blobmsg_for_each_attr(cur, tb[2], rem) {
174 if (!strcmp(var, blobmsg_name(cur)))
175 return json_process_cmd(call, cur);
181 static int handle_return(struct json_call *call, struct blob_attr *expr)
186 static int handle_include(struct json_call *call, struct blob_attr *expr)
188 struct blob_attr *tb[3];
189 struct json_script_file *f;
191 json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, 0);
195 f = json_script_get_file(call->ctx, blobmsg_data(tb[1]));
199 __json_script_run(call, f, expr);
203 static const struct json_handler cmd[] = {
205 { "case", handle_case },
206 { "return", handle_return },
207 { "include", handle_include },
210 static int eq_regex_cmp(const char *str, const char *pattern, bool regex)
216 return !strcmp(str, pattern);
218 if (regcomp(®, pattern, REG_EXTENDED | REG_NOSUB))
221 ret = !regexec(®, str, 0, NULL, 0);
227 static int expr_eq_regex(struct json_call *call, struct blob_attr *expr, bool regex)
229 struct json_script_ctx *ctx = call->ctx;
230 struct blob_attr *tb[3], *cur;
234 json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, 0);
235 if (!tb[1] || !tb[2])
238 var = msg_find_var(call, blobmsg_data(tb[1]));
242 switch(blobmsg_type(tb[2])) {
243 case BLOBMSG_TYPE_STRING:
244 return eq_regex_cmp(var, blobmsg_data(tb[2]), regex);
245 case BLOBMSG_TYPE_ARRAY:
246 blobmsg_for_each_attr(cur, tb[2], rem) {
247 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
248 ctx->handle_error(ctx, "Unexpected element type", cur);
252 if (eq_regex_cmp(var, blobmsg_data(cur), regex))
257 ctx->handle_error(ctx, "Unexpected element type", tb[2]);
262 static int handle_expr_eq(struct json_call *call, struct blob_attr *expr)
264 return expr_eq_regex(call, expr, false);
267 static int handle_expr_regex(struct json_call *call, struct blob_attr *expr)
269 return expr_eq_regex(call, expr, true);
272 static int handle_expr_has(struct json_call *call, struct blob_attr *expr)
274 struct json_script_ctx *ctx = call->ctx;
275 struct blob_attr *tb[3], *cur;
278 json_get_tuple(expr, tb, 0, 0);
282 switch(blobmsg_type(tb[1])) {
283 case BLOBMSG_TYPE_STRING:
284 return !!msg_find_var(call, blobmsg_data(tb[1]));
285 case BLOBMSG_TYPE_ARRAY:
286 blobmsg_for_each_attr(cur, tb[1], rem) {
287 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
288 ctx->handle_error(ctx, "Unexpected element type", cur);
292 if (msg_find_var(call, blobmsg_data(cur)))
297 ctx->handle_error(ctx, "Unexpected element type", tb[1]);
302 static int expr_and_or(struct json_call *call, struct blob_attr *expr, bool and)
304 struct blob_attr *cur;
308 blobmsg_for_each_attr(cur, expr, rem) {
312 ret = json_process_expr(call, cur);
323 static int handle_expr_and(struct json_call *call, struct blob_attr *expr)
325 return expr_and_or(call, expr, 1);
328 static int handle_expr_or(struct json_call *call, struct blob_attr *expr)
330 return expr_and_or(call, expr, 0);
333 static int handle_expr_not(struct json_call *call, struct blob_attr *expr)
335 struct blob_attr *tb[3];
337 json_get_tuple(expr, tb, BLOBMSG_TYPE_ARRAY, 0);
341 return json_process_expr(call, tb[1]);
344 static const struct json_handler expr[] = {
345 { "eq", handle_expr_eq },
346 { "regex", handle_expr_regex },
347 { "has", handle_expr_has },
348 { "and", handle_expr_and },
349 { "or", handle_expr_or },
350 { "not", handle_expr_not },
354 __json_process_type(struct json_call *call, struct blob_attr *cur,
355 const struct json_handler *h, int n, bool *found)
357 const char *name = blobmsg_data(blobmsg_data(cur));
360 for (i = 0; i < n; i++) {
361 if (strcmp(name, h[i].name) != 0)
365 return h[i].cb(call, cur);
372 static int json_process_expr(struct json_call *call, struct blob_attr *cur)
374 struct json_script_ctx *ctx = call->ctx;
378 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY ||
379 blobmsg_type(blobmsg_data(cur)) != BLOBMSG_TYPE_STRING) {
380 ctx->handle_error(ctx, "Unexpected element type", cur);
384 ret = __json_process_type(call, cur, expr, ARRAY_SIZE(expr), &found);
386 ctx->handle_error(ctx, "Unknown expression type", cur);
391 static int eval_string(struct json_call *call, struct blob_buf *buf, const char *name, const char *pattern)
393 char *dest, *next, *str;
398 dest = blobmsg_alloc_string_buffer(buf, name, 1);
399 next = alloca(strlen(pattern) + 1);
400 strcpy(next, pattern);
402 for (str = next; str; str = next) {
408 end = strchr(str, '%');
414 end = str + strlen(str);
420 cur = msg_find_var(call, str);
424 cur_len = strlen(cur);
437 dest = blobmsg_realloc_string_buffer(buf, cur_len + 1);
438 memcpy(dest + len, cur, cur_len);
443 blobmsg_add_string_buffer(buf);
451 static int cmd_add_string(struct json_call *call, const char *pattern)
453 return eval_string(call, &call->ctx->buf, NULL, pattern);
456 int json_script_eval_string(struct json_script_ctx *ctx, struct blob_attr *vars,
457 struct blob_buf *buf, const char *name,
460 struct json_call call = {
465 return eval_string(&call, buf, name, pattern);
468 static int cmd_process_strings(struct json_call *call, struct blob_attr *attr)
470 struct json_script_ctx *ctx = call->ctx;
471 struct blob_attr *cur;
476 blob_buf_init(&ctx->buf, 0);
477 c = blobmsg_open_array(&ctx->buf, NULL);
478 blobmsg_for_each_attr(cur, attr, rem) {
482 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
483 ctx->handle_error(ctx, "Invalid argument in command", attr);
487 ret = cmd_add_string(call, blobmsg_data(cur));
489 ctx->handle_error(ctx, "Unterminated variable reference in string", attr);
494 blobmsg_close_array(&ctx->buf, c);
499 static int __json_process_cmd(struct json_call *call, struct blob_attr *cur)
501 struct json_script_ctx *ctx = call->ctx;
506 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY ||
507 blobmsg_type(blobmsg_data(cur)) != BLOBMSG_TYPE_STRING) {
508 ctx->handle_error(ctx, "Unexpected element type", cur);
512 ret = __json_process_type(call, cur, cmd, ARRAY_SIZE(cmd), &found);
516 name = blobmsg_data(blobmsg_data(cur));
517 ret = cmd_process_strings(call, cur);
521 ctx->handle_command(ctx, name, blob_data(ctx->buf.head), call->vars);
526 static int json_process_cmd(struct json_call *call, struct blob_attr *block)
528 struct json_script_ctx *ctx = call->ctx;
529 struct blob_attr *cur;
534 if (blobmsg_type(block) != BLOBMSG_TYPE_ARRAY) {
535 ctx->handle_error(ctx, "Unexpected element type", block);
539 blobmsg_for_each_attr(cur, block, rem) {
540 switch(blobmsg_type(cur)) {
541 case BLOBMSG_TYPE_STRING:
543 return __json_process_cmd(call, block);
545 ret = json_process_cmd(call, cur);
556 void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file,
557 struct blob_attr *vars)
559 static unsigned int _seq = 0;
560 struct json_call call = {
570 __json_script_run(&call, file, NULL);
573 void json_script_run(struct json_script_ctx *ctx, const char *name,
574 struct blob_attr *vars)
576 struct json_script_file *file;
578 file = json_script_get_file(ctx, name);
582 json_script_run_file(ctx, file, vars);
585 static void __json_script_file_free(struct json_script_file *f)
587 struct json_script_file *next;
596 return __json_script_file_free(next);
600 json_script_free(struct json_script_ctx *ctx)
602 struct json_script_file *f, *next;
604 avl_remove_all_elements(&ctx->files, f, avl, next)
605 __json_script_file_free(f);
607 blob_buf_free(&ctx->buf);
611 __default_handle_error(struct json_script_ctx *ctx, const char *msg,
612 struct blob_attr *context)
617 __default_handle_var(struct json_script_ctx *ctx, const char *name,
618 struct blob_attr *vars)
624 __default_handle_expr(struct json_script_ctx *ctx, const char *name,
625 struct blob_attr *expr, struct blob_attr *vars)
630 static struct json_script_file *
631 __default_handle_file(struct json_script_ctx *ctx, const char *name)
636 void json_script_init(struct json_script_ctx *ctx)
638 avl_init(&ctx->files, avl_strcmp, false, NULL);
640 if (!ctx->handle_error)
641 ctx->handle_error = __default_handle_error;
643 if (!ctx->handle_var)
644 ctx->handle_var = __default_handle_var;
646 if (!ctx->handle_expr)
647 ctx->handle_expr = __default_handle_expr;
649 if (!ctx->handle_file)
650 ctx->handle_file = __default_handle_file;