Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYousong Zhou <yszhou4tech@gmail.com>2014-12-17 00:15:37 +0300
committerFelix Fietkau <nbd@openwrt.org>2014-12-22 17:46:35 +0300
commitfecaf2f5f66f8cb5598e55d493e4721554c5dcd5 (patch)
tree93a936f99931a57046e474a265c97d9626df7ff0 /examples
parent13b5c1d4ca488575ee3dd4726b669f768fad8ffa (diff)
examples: add example code for json_script.
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt2
-rw-r--r--examples/json_script-example.c84
-rw-r--r--examples/json_script-example.json33
3 files changed, 119 insertions, 0 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index a466dd6..6399c7a 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -17,3 +17,5 @@ TARGET_LINK_LIBRARIES(ustream-example ubox)
ADD_EXECUTABLE(runqueue-example runqueue-example.c)
TARGET_LINK_LIBRARIES(runqueue-example ubox)
+ADD_EXECUTABLE(json_script-example json_script-example.c)
+TARGET_LINK_LIBRARIES(json_script-example ubox blobmsg_json json_script ${json})
diff --git a/examples/json_script-example.c b/examples/json_script-example.c
new file mode 100644
index 0000000..e3305de
--- /dev/null
+++ b/examples/json_script-example.c
@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <json.h>
+#include "blobmsg.h"
+#include "blobmsg_json.h"
+#include "json_script.h"
+
+struct json_script_ctx jctx;
+struct blob_buf b_vars;
+struct blob_buf b_script;
+
+static void handle_command(struct json_script_ctx *ctx, const char *name,
+ struct blob_attr *data, struct blob_attr *vars)
+{
+ struct blob_attr *cur;
+ int rem;
+
+ fprintf(stderr, "Command: %s", name);
+ blobmsg_for_each_attr(cur, data, rem)
+ fprintf(stderr, " %s", (char *) blobmsg_data(cur));
+ fprintf(stderr, "\n");
+}
+
+static struct json_script_file *
+handle_file(struct json_script_ctx *ctx, const char *filename)
+{
+ json_object *obj;
+
+ obj = json_object_from_file(filename);
+ if (!obj) {
+ fprintf(stderr, "load JSON data from %s failed.\n", filename);
+ return NULL;
+ }
+
+ blob_buf_init(&b_script, 0);
+ blobmsg_add_json_element(&b_script, "", obj);
+ json_object_put(obj);
+
+ return json_script_file_from_blobmsg(filename,
+ blob_data(b_script.head), blob_len(b_script.head));
+}
+
+static void usage(const char *prog, int exit_code)
+{
+ fprintf(stderr, "Usage: %s [VARNAME=value] <filename_json_script>\n", prog);
+ exit(exit_code);
+}
+
+int main(int argc, char *argv[])
+{
+ int i;
+ char *file = NULL;
+ const char *prog = argv[0];
+
+ blobmsg_buf_init(&b_vars);
+ blobmsg_buf_init(&b_script);
+
+ json_script_init(&jctx);
+ jctx.handle_command = handle_command;
+ jctx.handle_file = handle_file;
+
+ for (i = 1; i < argc; i++) {
+ char *sep = strchr(argv[i], '=');
+ if (sep) {
+ *sep = '\0';
+ blobmsg_add_string(&b_vars, argv[i], sep + 1);
+ } else if (!file) {
+ file = argv[i];
+ } else {
+ usage(prog, -1);
+ }
+ }
+ if (i < argc || !file)
+ usage(prog, -2);
+
+ json_script_run(&jctx, file, b_vars.head);
+
+ json_script_free(&jctx);
+ blob_buf_free(&b_script);
+ blob_buf_free(&b_vars);
+
+ return 0;
+}
diff --git a/examples/json_script-example.json b/examples/json_script-example.json
new file mode 100644
index 0000000..45636b7
--- /dev/null
+++ b/examples/json_script-example.json
@@ -0,0 +1,33 @@
+[
+ [ "exec", "%EXECVAR%", "/%%/" ],
+ [ "if",
+ [ "eq", "EQVAR", "eqval" ],
+ [ "exec_if", "%VAR%", "%%", "jk" ]
+ ],
+ [ "case", "CASEVAR", {
+ "caseval0": ["cmd_case_0", "cmd_case_arg0", "case_cmd_arg1"],
+ "caseval1": ["cmd_case_1", "cmd_case_arg0", "case_cmd_arg1"]
+ } ],
+
+ [ "if",
+ [ "and", [ "eq", "EQVAR", "eqval" ],
+ [ "has", "HASVAR" ],
+ [ "regex", "REGEXVAR0", "regexval" ],
+ [ "regex", "REGEXVAR1", [ "regexval10", "regexval11" ] ],
+ [ "not", [ "eq", "NOTEQVAR", "noteqval" ] ] ],
+ [ "exec_if_and", "%ANDVAR%" ]
+ ],
+
+ [ "if",
+ [ "or", [ "eq", "EQVAR", "eqval" ],
+ [ "has", "HASVAR" ],
+ [ "regex", "REGEXVAR0", "regexval" ],
+ [ "regex", "REGEXVAR1", [ "regexval10", "regexval11" ] ],
+ [ "not", [ "eq", "NOTEQVAR", "noteqval" ] ] ],
+ [ "exec_if_or", "%ORVAR%" ]
+ ],
+
+ [ "return", "foobar" ],
+
+ [ "exec_non_reachable", "Arghhh" ]
+]