From patchwork Tue Dec 16 21:15:37 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousong Zhou X-Patchwork-Id: 422799 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 9202D1400A0 for ; Fri, 19 Dec 2014 19:17:55 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 3AD4428168B; Fri, 19 Dec 2014 09:12:27 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 68D7B284E8A for ; Fri, 19 Dec 2014 09:11:31 +0100 (CET) X-policyd-weight: using cached result; rate:hard: -8.5 Received: from mail-pd0-f176.google.com (mail-pd0-f176.google.com [209.85.192.176]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Fri, 19 Dec 2014 09:11:29 +0100 (CET) Received: by mail-pd0-f176.google.com with SMTP id r10so725935pdi.35 for ; Fri, 19 Dec 2014 00:13:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=tU1YxWXzHTV+Wlhu89iOynbh2mnds4lr400MSqTvavU=; b=0/1S/lPFspiMXoWp35J8/q6rTNeOUPdduE9U7Yex8BPpQK7oBqMuHaeq/EHivYMPN9 sXvQhsPIyVWYnFldmZ2SuOgy5jYSvqobj05mz3pki7VdP0wU7dtOfCoPOq71zz4sVobN e37OxXyBX/URXqPpGDaS1T2fm+S+oLMMkqUfA7FOcLoS6ywbI944M8F2qUQ5EBDiDKax fqLIZ6AfIBM54qMpLcqhm3OX0sjOiXV4w7RmhQB4LQUc2H4SGJdsS3bXJdcfM4BQCbO9 a8/bYao9Vy6MxicOc2APNLhGecszH44KtS33IA+DS5zMHD0sZsMGPsvaaEWvpYxGRi9z n3fg== X-Received: by 10.66.235.37 with SMTP id uj5mr10663572pac.72.1418976800792; Fri, 19 Dec 2014 00:13:20 -0800 (PST) Received: from debian.lan ([103.29.140.56]) by mx.google.com with ESMTPSA id nv7sm8816709pbc.29.2014.12.19.00.13.17 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Dec 2014 00:13:19 -0800 (PST) From: Yousong Zhou To: nbd@openwrt.org Date: Wed, 17 Dec 2014 05:15:37 +0800 Message-Id: <1418764537-36787-4-git-send-email-yszhou4tech@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1418764537-36787-1-git-send-email-yszhou4tech@gmail.com> References: <1418764537-36787-1-git-send-email-yszhou4tech@gmail.com> Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH 3/3] examples: add example code for json_script. X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" Signed-off-by: Yousong Zhou --- examples/CMakeLists.txt | 2 + examples/json_script-example.c | 84 +++++++++++++++++++++++++++++++++++++ examples/json_script-example.json | 33 +++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 examples/json_script-example.c create mode 100644 examples/json_script-example.json 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 +#include + +#include +#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] \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" ] +]