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);
35 static int eval_string(struct json_call *call, struct blob_buf *buf, const char *name, const char *pattern);
37 struct json_script_file *
38 json_script_file_from_blobmsg(const char *name, void *data, int len)
40 struct json_script_file *f;
45 name_len = strlen(name) + 1;
47 f = calloc_a(sizeof(*f) + len, &new_name, name_len);
48 memcpy(f->data, data, len);
50 f->avl.key = strcpy(new_name, name);
55 static struct json_script_file *
56 json_script_get_file(struct json_script_ctx *ctx, const char *filename)
58 struct json_script_file *f;
60 f = avl_find_element(&ctx->files, filename, f, avl);
64 f = ctx->handle_file(ctx, filename);
68 avl_insert(&ctx->files, &f->avl);
72 static void __json_script_run(struct json_call *call, struct json_script_file *file,
73 struct blob_attr *context)
75 struct json_script_ctx *ctx = call->ctx;
77 if (file->seq == call->seq) {
79 ctx->handle_error(ctx, "Recursive include", context);
84 file->seq = call->seq;
86 json_process_cmd(call, file->data);
91 const char *json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *vars,
94 struct blob_attr *cur;
97 blobmsg_for_each_attr(cur, vars, rem) {
98 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
101 if (strcmp(blobmsg_name(cur), name) != 0)
104 return blobmsg_data(cur);
107 return ctx->handle_var(ctx, name, vars);
111 msg_find_var(struct json_call *call, const char *name)
113 return json_script_find_var(call->ctx, call->vars, name);
117 json_get_tuple(struct blob_attr *cur, struct blob_attr **tb, int t1, int t2)
119 static struct blobmsg_policy expr_tuple[3] = {
120 { .type = BLOBMSG_TYPE_STRING },
125 expr_tuple[1].type = t1;
126 expr_tuple[2].type = t2;
127 blobmsg_parse_array(expr_tuple, 3, tb, blobmsg_data(cur), blobmsg_data_len(cur));
130 static int handle_if(struct json_call *call, struct blob_attr *expr)
132 struct blob_attr *tb[4];
135 static const struct blobmsg_policy if_tuple[4] = {
136 { .type = BLOBMSG_TYPE_STRING },
137 { .type = BLOBMSG_TYPE_ARRAY },
138 { .type = BLOBMSG_TYPE_ARRAY },
139 { .type = BLOBMSG_TYPE_ARRAY },
142 blobmsg_parse_array(if_tuple, 4, tb, blobmsg_data(expr), blobmsg_data_len(expr));
144 if (!tb[1] || !tb[2])
147 ret = json_process_expr(call, tb[1]);
152 return json_process_cmd(call, tb[2]);
157 return json_process_cmd(call, tb[3]);
160 static int handle_case(struct json_call *call, struct blob_attr *expr)
162 struct blob_attr *tb[3], *cur;
166 json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, BLOBMSG_TYPE_TABLE);
167 if (!tb[1] || !tb[2])
170 var = msg_find_var(call, blobmsg_data(tb[1]));
174 blobmsg_for_each_attr(cur, tb[2], rem) {
175 if (!strcmp(var, blobmsg_name(cur)))
176 return json_process_cmd(call, cur);
182 static int handle_return(struct json_call *call, struct blob_attr *expr)
187 static int handle_include(struct json_call *call, struct blob_attr *expr)
189 struct blob_attr *tb[3];
190 struct json_script_file *f;
192 json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, 0);
196 f = json_script_get_file(call->ctx, blobmsg_data(tb[1]));
200 __json_script_run(call, f, expr);
204 static const struct json_handler cmd[] = {
206 { "case", handle_case },
207 { "return", handle_return },
208 { "include", handle_include },
211 static int eq_regex_cmp(const char *str, const char *pattern, bool regex)
217 return !strcmp(str, pattern);
219 if (regcomp(®, pattern, REG_EXTENDED | REG_NOSUB))
222 ret = !regexec(®, str, 0, NULL, 0);
228 static int expr_eq_regex(struct json_call *call, struct blob_attr *expr, bool regex)
230 struct json_script_ctx *ctx = call->ctx;
231 struct blob_attr *tb[3], *cur;
235 json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, 0);
236 if (!tb[1] || !tb[2])
239 var = msg_find_var(call, blobmsg_data(tb[1]));
243 switch(blobmsg_type(tb[2])) {
244 case BLOBMSG_TYPE_STRING:
245 return eq_regex_cmp(var, blobmsg_data(tb[2]), regex);
246 case BLOBMSG_TYPE_ARRAY:
247 blobmsg_for_each_attr(cur, tb[2], rem) {
248 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
249 ctx->handle_error(ctx, "Unexpected element type", cur);
253 if (eq_regex_cmp(var, blobmsg_data(cur), regex))
258 ctx->handle_error(ctx, "Unexpected element type", tb[2]);
263 static int handle_expr_eq(struct json_call *call, struct blob_attr *expr)
265 return expr_eq_regex(call, expr, false);
268 static int handle_expr_regex(struct json_call *call, struct blob_attr *expr)
270 return expr_eq_regex(call, expr, true);
273 static int handle_expr_has(struct json_call *call, struct blob_attr *expr)
275 struct json_script_ctx *ctx = call->ctx;
276 struct blob_attr *tb[3], *cur;
279 json_get_tuple(expr, tb, 0, 0);
283 switch(blobmsg_type(tb[1])) {
284 case BLOBMSG_TYPE_STRING:
285 return !!msg_find_var(call, blobmsg_data(tb[1]));
286 case BLOBMSG_TYPE_ARRAY:
287 blobmsg_for_each_attr(cur, tb[1], rem) {
288 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
289 ctx->handle_error(ctx, "Unexpected element type", cur);
293 if (msg_find_var(call, blobmsg_data(cur)))
298 ctx->handle_error(ctx, "Unexpected element type", tb[1]);
303 static int expr_and_or(struct json_call *call, struct blob_attr *expr, bool and)
305 struct blob_attr *cur;
309 blobmsg_for_each_attr(cur, expr, rem) {
313 ret = json_process_expr(call, cur);
324 static int handle_expr_and(struct json_call *call, struct blob_attr *expr)
326 return expr_and_or(call, expr, 1);
329 static int handle_expr_or(struct json_call *call, struct blob_attr *expr)
331 return expr_and_or(call, expr, 0);
334 static int handle_expr_not(struct json_call *call, struct blob_attr *expr)
336 struct blob_attr *tb[3];
339 json_get_tuple(expr, tb, BLOBMSG_TYPE_ARRAY, 0);
343 ret = json_process_expr(call, tb[1]);
349 static int handle_expr_isdir(struct json_call *call, struct blob_attr *expr)
351 static struct blob_buf b;
352 struct blob_attr *tb[3];
353 const char *pattern, *path;
357 json_get_tuple(expr, tb, BLOBMSG_TYPE_STRING, 0);
358 if (!tb[1] || blobmsg_type(tb[1]) != BLOBMSG_TYPE_STRING)
360 pattern = blobmsg_data(tb[1]);
362 blob_buf_init(&b, 0);
363 ret = eval_string(call, &b, NULL, pattern);
366 path = blobmsg_data(blob_data(b.head));
367 ret = stat(path, &s);
370 return S_ISDIR(s.st_mode);
373 static const struct json_handler expr[] = {
374 { "eq", handle_expr_eq },
375 { "regex", handle_expr_regex },
376 { "has", handle_expr_has },
377 { "and", handle_expr_and },
378 { "or", handle_expr_or },
379 { "not", handle_expr_not },
380 { "isdir", handle_expr_isdir },
384 __json_process_type(struct json_call *call, struct blob_attr *cur,
385 const struct json_handler *h, int n, bool *found)
387 const char *name = blobmsg_data(blobmsg_data(cur));
390 for (i = 0; i < n; i++) {
391 if (strcmp(name, h[i].name) != 0)
395 return h[i].cb(call, cur);
402 static int json_process_expr(struct json_call *call, struct blob_attr *cur)
404 struct json_script_ctx *ctx = call->ctx;
408 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY ||
409 blobmsg_type(blobmsg_data(cur)) != BLOBMSG_TYPE_STRING) {
410 ctx->handle_error(ctx, "Unexpected element type", cur);
414 ret = __json_process_type(call, cur, expr, ARRAY_SIZE(expr), &found);
416 ctx->handle_error(ctx, "Unknown expression type", cur);
421 static int eval_string(struct json_call *call, struct blob_buf *buf, const char *name, const char *pattern)
423 char *dest, *next, *str;
428 dest = blobmsg_alloc_string_buffer(buf, name, 1);
429 next = alloca(strlen(pattern) + 1);
430 strcpy(next, pattern);
432 for (str = next; str; str = next) {
438 end = strchr(str, '%');
444 end = str + strlen(str);
450 cur = msg_find_var(call, str);
454 cur_len = strlen(cur);
467 dest = blobmsg_realloc_string_buffer(buf, len + cur_len + 1);
468 memcpy(dest + len, cur, cur_len);
473 blobmsg_add_string_buffer(buf);
481 static int cmd_add_string(struct json_call *call, const char *pattern)
483 return eval_string(call, &call->ctx->buf, NULL, pattern);
486 int json_script_eval_string(struct json_script_ctx *ctx, struct blob_attr *vars,
487 struct blob_buf *buf, const char *name,
490 struct json_call call = {
495 return eval_string(&call, buf, name, pattern);
498 static int cmd_process_strings(struct json_call *call, struct blob_attr *attr)
500 struct json_script_ctx *ctx = call->ctx;
501 struct blob_attr *cur;
506 blob_buf_init(&ctx->buf, 0);
507 c = blobmsg_open_array(&ctx->buf, NULL);
508 blobmsg_for_each_attr(cur, attr, rem) {
512 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
513 blobmsg_add_blob(&ctx->buf, cur);
517 ret = cmd_add_string(call, blobmsg_data(cur));
519 ctx->handle_error(ctx, "Unterminated variable reference in string", attr);
524 blobmsg_close_array(&ctx->buf, c);
529 static int __json_process_cmd(struct json_call *call, struct blob_attr *cur)
531 struct json_script_ctx *ctx = call->ctx;
536 if (blobmsg_type(cur) != BLOBMSG_TYPE_ARRAY ||
537 blobmsg_type(blobmsg_data(cur)) != BLOBMSG_TYPE_STRING) {
538 ctx->handle_error(ctx, "Unexpected element type", cur);
542 ret = __json_process_type(call, cur, cmd, ARRAY_SIZE(cmd), &found);
546 name = blobmsg_data(blobmsg_data(cur));
547 ret = cmd_process_strings(call, cur);
551 ctx->handle_command(ctx, name, blob_data(ctx->buf.head), call->vars);
556 static int json_process_cmd(struct json_call *call, struct blob_attr *block)
558 struct json_script_ctx *ctx = call->ctx;
559 struct blob_attr *cur;
564 if (blobmsg_type(block) != BLOBMSG_TYPE_ARRAY) {
565 ctx->handle_error(ctx, "Unexpected element type", block);
569 blobmsg_for_each_attr(cur, block, rem) {
573 switch(blobmsg_type(cur)) {
574 case BLOBMSG_TYPE_STRING:
576 return __json_process_cmd(call, block);
578 ret = json_process_cmd(call, cur);
589 void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file,
590 struct blob_attr *vars)
592 static unsigned int _seq = 0;
593 struct json_call call = {
605 __json_script_run(&call, file, NULL);
608 void json_script_run(struct json_script_ctx *ctx, const char *name,
609 struct blob_attr *vars)
611 struct json_script_file *file;
613 file = json_script_get_file(ctx, name);
617 json_script_run_file(ctx, file, vars);
620 static void __json_script_file_free(struct json_script_file *f)
622 struct json_script_file *next;
630 __json_script_file_free(next);
634 json_script_free(struct json_script_ctx *ctx)
636 struct json_script_file *f, *next;
638 avl_remove_all_elements(&ctx->files, f, avl, next)
639 __json_script_file_free(f);
641 blob_buf_free(&ctx->buf);
645 __default_handle_error(struct json_script_ctx *ctx, const char *msg,
646 struct blob_attr *context)
651 __default_handle_var(struct json_script_ctx *ctx, const char *name,
652 struct blob_attr *vars)
658 __default_handle_expr(struct json_script_ctx *ctx, const char *name,
659 struct blob_attr *expr, struct blob_attr *vars)
664 static struct json_script_file *
665 __default_handle_file(struct json_script_ctx *ctx, const char *name)
670 void json_script_init(struct json_script_ctx *ctx)
672 avl_init(&ctx->files, avl_strcmp, false, NULL);
674 if (!ctx->handle_error)
675 ctx->handle_error = __default_handle_error;
677 if (!ctx->handle_var)
678 ctx->handle_var = __default_handle_var;
680 if (!ctx->handle_expr)
681 ctx->handle_expr = __default_handle_expr;
683 if (!ctx->handle_file)
684 ctx->handle_file = __default_handle_file;