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.
19 #include <libubox/avl-cmp.h>
21 #include "json_script.h"
24 struct json_script_ctx
*ctx
;
25 struct blob_attr
*vars
;
31 int (*cb
)(struct json_call
*call
, struct blob_attr
*cur
);
34 static int json_process_expr(struct json_call
*call
, struct blob_attr
*cur
);
35 static int json_process_cmd(struct json_call
*call
, struct blob_attr
*cur
);
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];
338 json_get_tuple(expr
, tb
, BLOBMSG_TYPE_ARRAY
, 0);
342 return json_process_expr(call
, tb
[1]);
345 static const struct json_handler expr
[] = {
346 { "eq", handle_expr_eq
},
347 { "regex", handle_expr_regex
},
348 { "has", handle_expr_has
},
349 { "and", handle_expr_and
},
350 { "or", handle_expr_or
},
351 { "not", handle_expr_not
},
355 __json_process_type(struct json_call
*call
, struct blob_attr
*cur
,
356 const struct json_handler
*h
, int n
, bool *found
)
358 const char *name
= blobmsg_data(blobmsg_data(cur
));
361 for (i
= 0; i
< n
; i
++) {
362 if (strcmp(name
, h
[i
].name
) != 0)
366 return h
[i
].cb(call
, cur
);
373 static int json_process_expr(struct json_call
*call
, struct blob_attr
*cur
)
375 struct json_script_ctx
*ctx
= call
->ctx
;
379 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
||
380 blobmsg_type(blobmsg_data(cur
)) != BLOBMSG_TYPE_STRING
) {
381 ctx
->handle_error(ctx
, "Unexpected element type", cur
);
385 ret
= __json_process_type(call
, cur
, expr
, ARRAY_SIZE(expr
), &found
);
387 ctx
->handle_error(ctx
, "Unknown expression type", cur
);
392 static int cmd_add_string(struct json_call
*call
, const char *pattern
)
394 struct json_script_ctx
*ctx
= call
->ctx
;
395 char *dest
, *next
, *str
;
400 dest
= blobmsg_alloc_string_buffer(&ctx
->buf
, NULL
, 1);
401 next
= alloca(strlen(pattern
) + 1);
402 strcpy(next
, pattern
);
404 for (str
= next
; str
; str
= next
) {
410 end
= strchr(str
, '%');
416 end
= str
+ strlen(str
);
422 cur
= msg_find_var(call
, str
);
426 cur_len
= strlen(cur
);
439 dest
= blobmsg_realloc_string_buffer(&ctx
->buf
, cur_len
+ 1);
440 memcpy(dest
+ len
, cur
, cur_len
);
448 blobmsg_add_string_buffer(&ctx
->buf
);
452 static int cmd_process_strings(struct json_call
*call
, struct blob_attr
*attr
)
454 struct json_script_ctx
*ctx
= call
->ctx
;
455 struct blob_attr
*cur
;
460 blob_buf_init(&ctx
->buf
, 0);
461 c
= blobmsg_open_array(&ctx
->buf
, NULL
);
462 blobmsg_for_each_attr(cur
, attr
, rem
) {
466 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
) {
467 ctx
->handle_error(ctx
, "Invalid argument in command", attr
);
471 ret
= cmd_add_string(call
, blobmsg_data(cur
));
473 ctx
->handle_error(ctx
, "Unterminated variable reference in string", attr
);
478 blobmsg_close_array(&ctx
->buf
, c
);
483 static int __json_process_cmd(struct json_call
*call
, struct blob_attr
*cur
)
485 struct json_script_ctx
*ctx
= call
->ctx
;
490 if (blobmsg_type(cur
) != BLOBMSG_TYPE_ARRAY
||
491 blobmsg_type(blobmsg_data(cur
)) != BLOBMSG_TYPE_STRING
) {
492 ctx
->handle_error(ctx
, "Unexpected element type", cur
);
496 ret
= __json_process_type(call
, cur
, cmd
, ARRAY_SIZE(cmd
), &found
);
500 name
= blobmsg_data(blobmsg_data(cur
));
501 ret
= cmd_process_strings(call
, cur
);
505 ctx
->handle_command(ctx
, name
, blob_data(ctx
->buf
.head
), call
->vars
);
510 static int json_process_cmd(struct json_call
*call
, struct blob_attr
*block
)
512 struct json_script_ctx
*ctx
= call
->ctx
;
513 struct blob_attr
*cur
;
518 if (blobmsg_type(block
) != BLOBMSG_TYPE_ARRAY
) {
519 ctx
->handle_error(ctx
, "Unexpected element type", block
);
523 blobmsg_for_each_attr(cur
, block
, rem
) {
524 switch(blobmsg_type(cur
)) {
525 case BLOBMSG_TYPE_STRING
:
527 return __json_process_cmd(call
, block
);
529 ret
= json_process_cmd(call
, cur
);
540 void json_script_run(struct json_script_ctx
*ctx
, const char *name
,
541 struct blob_attr
*vars
)
543 struct json_script_file
*file
;
544 static unsigned int _seq
= 0;
545 struct json_call call
= {
555 file
= json_script_get_file(ctx
, name
);
559 __json_script_run(&call
, file
, NULL
);
562 static void __json_script_file_free(struct json_script_ctx
*ctx
, struct json_script_file
*f
)
564 struct json_script_file
*next
;
566 for (next
= f
->next
; f
; f
= next
, next
= f
->next
)
571 json_script_free(struct json_script_ctx
*ctx
)
573 struct json_script_file
*f
, *next
;
575 avl_remove_all_elements(&ctx
->files
, f
, avl
, next
)
576 __json_script_file_free(ctx
, f
);
578 blob_buf_free(&ctx
->buf
);
582 __default_handle_error(struct json_script_ctx
*ctx
, const char *msg
,
583 struct blob_attr
*context
)
588 __default_handle_var(struct json_script_ctx
*ctx
, const char *name
,
589 struct blob_attr
*vars
)
595 __default_handle_expr(struct json_script_ctx
*ctx
, const char *name
,
596 struct blob_attr
*expr
, struct blob_attr
*vars
)
601 static struct json_script_file
*
602 __default_handle_file(struct json_script_ctx
*ctx
, const char *name
)
607 void json_script_init(struct json_script_ctx
*ctx
)
609 avl_init(&ctx
->files
, avl_strcmp
, false, NULL
);
611 if (!ctx
->handle_error
)
612 ctx
->handle_error
= __default_handle_error
;
614 if (!ctx
->handle_var
)
615 ctx
->handle_var
= __default_handle_var
;
617 if (!ctx
->handle_expr
)
618 ctx
->handle_expr
= __default_handle_expr
;
620 if (!ctx
->handle_file
)
621 ctx
->handle_file
= __default_handle_file
;