examples: remove set but unused variable
[project/libubox.git] / json_script.h
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  *
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.
7  *
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.
15  */
16 #ifndef __JSON_SCRIPT_H
17 #define __JSON_SCRIPT_H
18
19 #include "avl.h"
20 #include "blob.h"
21 #include "blobmsg.h"
22 #include "utils.h"
23
24 struct json_script_file;
25
26 struct json_script_ctx {
27         struct avl_tree files;
28         struct blob_buf buf;
29
30         uint32_t run_seq;
31
32         /*
33          * handle_command: handle a command that was not recognized by the
34          * json_script core (required)
35          *
36          * @cmd: blobmsg container of the processed command
37          * @vars: blobmsg container of current run variables
38          */
39         void (*handle_command)(struct json_script_ctx *ctx, const char *name,
40                                struct blob_attr *cmd, struct blob_attr *vars);
41
42         /*
43          * handle_expr: handle an expression that was not recognized by the
44          * json_script core (optional)
45          *
46          * @expr: blobmsg container of the processed expression
47          * @vars: blobmsg container of current runtime variables
48          */
49         int (*handle_expr)(struct json_script_ctx *ctx, const char *name,
50                            struct blob_attr *expr, struct blob_attr *vars);
51
52         /*
53          * handle_var - look up a variable that's not part of the runtime
54          * variable set (optional)
55          */
56         const char *(*handle_var)(struct json_script_ctx *ctx, const char *name,
57                                   struct blob_attr *vars);
58
59         /*
60          * handle_file - load a file by filename (optional)
61          *
62          * in case of wildcards, it can return a chain of json_script files
63          * linked via the ::next pointer. Only the first json_script file is
64          * added to the tree.
65          */
66         struct json_script_file *(*handle_file)(struct json_script_ctx *ctx,
67                                                 const char *name);
68
69         /*
70          * handle_error - handle a processing error in a command or expression
71          * (optional)
72          * 
73          * @msg: error message
74          * @context: source file context of the error (blobmsg container)
75          */
76         void (*handle_error)(struct json_script_ctx *ctx, const char *msg,
77                              struct blob_attr *context);
78 };
79
80 struct json_script_file {
81         struct avl_node avl;
82         struct json_script_file *next;
83
84         unsigned int seq;
85         struct blob_attr data[];
86 };
87
88 void json_script_init(struct json_script_ctx *ctx);
89 void json_script_free(struct json_script_ctx *ctx);
90
91 /*
92  * json_script_run - run a json script with a set of runtime variables
93  *
94  * @filename: initial filename to run
95  * @vars: blobmsg container of the current runtime variables
96  */
97 void json_script_run(struct json_script_ctx *ctx, const char *filename,
98                      struct blob_attr *vars);
99
100 void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file,
101                           struct blob_attr *vars);
102 /*
103  * json_script_eval_string - evaluate a string and store the result
104  *
105  * Can be used to process variable references outside of a script
106  * in a same way that they would be interpreted in the script context.
107  */
108 int json_script_eval_string(struct json_script_ctx *ctx, struct blob_attr *vars,
109                             struct blob_buf *buf, const char *name,
110                             const char *pattern);
111
112 struct json_script_file *
113 json_script_file_from_blobmsg(const char *name, void *data, int len);
114
115 /*
116  * json_script_find_var - helper function to find a runtime variable from
117  * the list passed by json_script user.
118  * It is intended to be used by the .handle_var callback
119  */
120 const char *json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *vars,
121                                  const char *name);
122
123 #endif