From patchwork Tue Jul 28 18:48:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaymin Patel X-Patchwork-Id: 1337964 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=dc82XLKy; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=TE1Mpc4l; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4BGQk93PV5z9sRN for ; Wed, 29 Jul 2020 04:51:13 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Owner; bh=g/5DNKfDfEdkphRjDQMIUIkPyx1r/r3TW2w6qZa+/WY=; b=dc82XLKytc8bNAnuEJr1CHzzeA 54yc4uJGd8m0MerfABuD+6/MrNEx1G2rCx41ILPXG8Hn9p6neTsMcHhr0PQrOZMBOhQq2Sxmofog6 fawdyez+6qA+mHXC0RlsulnNeY98gCmHz+q6W7quS2zyjbfmmy5WkhDyGbJOsEb/bhO5WOkZJtM0Y gN2u6onogP65TEql3IuBNJhIz8Yp+d0XzqEMTq2Z2CA6goTOEfJ7u39mTuCM7c35UHqPSs0Re5PU1 unw9LW9RHnJnmTyD6egBMBC7gF/xwh31pkJtU33uPhR6MwoagRpUW9WB+Jz/ef6WEJELU02+lvCF2 BuxyJNbg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k0Ufo-0001GC-FL; Tue, 28 Jul 2020 18:50:00 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k0Ufl-0001EW-6m for openwrt-devel@lists.openwrt.org; Tue, 28 Jul 2020 18:49:58 +0000 Received: by mail-pg1-x52c.google.com with SMTP id d4so12518688pgk.4 for ; Tue, 28 Jul 2020 11:49:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=sMzEm2onsMq/OS9wfFuW7QEfxHeCJF1kB4Qmrq8QUjw=; b=TE1Mpc4l//+8K7DzoEAsFCTB2ibDVRJqunZ7xS3Hkwbj0myPw5jb8KtIsmyYXdWtat S2pxEtAclABA6mFUt8epKQsg5RMYk/cXQzHncTBPN/29GhmcFJuwr10FWmzRNLMGq3E/ KCQlaYgWIJvRNdsuWDixx0vUzhMNeS3GFx15HxzMDXpXVGgpva6Fi3s20v651Su779JR BuShxst7OSYl1CbQ6PjnKZnNDAMQJVtprQlJOaeIkd1ABe4//el6kcnxx4D//0MniYsr 4ZaLaNozNbHmHl9ts7Rf5eUUyaMk+cGmr9drrGWo5cMzz7LicRk4c3ByPA/sr9CzQ8e6 kSSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=sMzEm2onsMq/OS9wfFuW7QEfxHeCJF1kB4Qmrq8QUjw=; b=NkiZBTllwLl4Nzf+c2Y5NHbNIuvBUDGlnPVwq6cXEyDUU0nQHFi4yGa3j2IlJwwNga 3KOo1rSiS6AU/+hx8eh0W2nRkcF4/8nfI6XJ4ZvQLWfc0+DDy8eUQxb5A+PeNJlMOpVy iWZkmBwDMI6jRk7lZ94FgVxmY9CiP6dr7lgbOue4obpm1WXw0xMWW/FIaXELZFDYVNJ2 UFQgsGQNzAncTfiRxbWpB3wV9LBNehh5miwNm+rDmfEpH5/u34wkZHZKSZlkZ+gx8GCH jVvIQxW6Wp+PzQ+4Ae/1xXGNqm0hVw+3I9eGAeGb38TGE9IfO0E9qfMnCIPll+l5BI9q r3Dw== X-Gm-Message-State: AOAM531c98NtZ/0sNEQk44f2c+YUMRmGImazQ0/eV05PjPkJ+lusQO2S F4mGNLB8x4BleLIRkug27915QU9M5vY= X-Google-Smtp-Source: ABdhPJzeczlShFSo8SuJGmrqWrP+topYPgBXVLYCrkE1m5udTABEluCEH7c1J68m20a3EeDVK6YkdQ== X-Received: by 2002:a63:a843:: with SMTP id i3mr26050582pgp.190.1595962193396; Tue, 28 Jul 2020 11:49:53 -0700 (PDT) Received: from in-gn-31611ln.inf.redpiranha.net ([103.250.136.216]) by smtp.googlemail.com with ESMTPSA id g8sm19186341pgr.70.2020.07.28.11.49.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Jul 2020 11:49:50 -0700 (PDT) From: jem.patel@gmail.com To: openwrt-devel@lists.openwrt.org Subject: [PATCH] jshn: add argument to append json using jshn.sh Date: Wed, 29 Jul 2020 00:18:18 +0530 Message-Id: <1595962098-11901-1-git-send-email-jem.patel@gmail.com> X-Mailer: git-send-email 2.7.4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200728_144957_326962_3866C884 X-CRM114-Status: GOOD ( 15.55 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:52c listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [jem.patel[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jaymin Patel MIME-Version: 1.0 Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org From: Jaymin Patel argument of -a with jshn is useful to avoid initialization of a new json object and provides an option to append json into existing json_object Signed-off-by: Jaymin Patel --- jshn.c | 22 ++++++++++++++-------- sh/jshn.sh | 8 ++++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/jshn.c b/jshn.c index 1b685e5..971246f 100644 --- a/jshn.c +++ b/jshn.c @@ -172,7 +172,7 @@ static int add_json_element(const char *key, json_object *obj) return 0; } -static int jshn_parse(const char *str) +static int jshn_parse(const char *str, bool append) { json_object *obj; @@ -183,7 +183,9 @@ static int jshn_parse(const char *str) fprintf(stderr, "Failed to parse message data\n"); return 1; } - fprintf(stdout, "json_init;\n"); + if (!append) { + fprintf(stdout, "json_init;\n"); + } add_json_object(obj); fflush(stdout); json_object_put(obj); @@ -313,7 +315,7 @@ out: static int usage(const char *progname) { - fprintf(stderr, "Usage: %s [-n] [-i] -r |-R |-o |-p |-w\n", progname); + fprintf(stderr, "Usage: %s [-n] [-i] [-a] -r |-R |-o |-p |-w\n", progname); return 2; } @@ -338,7 +340,7 @@ static int avl_strcmp_var(const void *k1, const void *k2, void *ptr) return c1 - c2; } -static int jshn_parse_file(const char *path) +static int jshn_parse_file(const char *path, bool append) { struct stat sb; int ret = 0; @@ -369,7 +371,7 @@ static int jshn_parse_file(const char *path) return 3; } - ret = jshn_parse(fbuf); + ret = jshn_parse(fbuf, append); free(fbuf); close(fd); @@ -397,6 +399,7 @@ int main(int argc, char **argv) { extern char **environ; bool no_newline = false; + bool append_object = false; bool indent = false; struct env_var *vars; int i; @@ -423,17 +426,17 @@ int main(int argc, char **argv) avl_insert(&env_vars, &vars[i].avl); } - while ((ch = getopt(argc, argv, "p:nir:R:o:w")) != -1) { + while ((ch = getopt(argc, argv, "p:anir:R:o:w")) != -1) { switch(ch) { case 'p': var_prefix = optarg; var_prefix_len = strlen(var_prefix); break; case 'r': - ret = jshn_parse(optarg); + ret = jshn_parse(optarg, append_object); goto exit; case 'R': - ret = jshn_parse_file(optarg); + ret = jshn_parse_file(optarg, append_object); goto exit; case 'w': ret = jshn_format(no_newline, indent, stdout); @@ -441,6 +444,9 @@ int main(int argc, char **argv) case 'o': ret = jshn_format_file(optarg, no_newline, indent); goto exit; + case 'a': + append_object = true; + break; case 'n': no_newline = true; break; diff --git a/sh/jshn.sh b/sh/jshn.sh index 7b0155d..ba3f1f3 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -180,6 +180,14 @@ json_load() { eval "`jshn -r "$1"`" } +json_append() { + eval "`jshn -a -r "$1"`" +} + +json_append_file() { + eval "`jshn -a -R "$1"`" +} + json_load_file() { eval "`jshn -R "$1"`" }