From patchwork Tue Jun 12 02:32:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hoang Huu Le X-Patchwork-Id: 928043 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=dektech.com.au Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=dektech.com.au header.i=@dektech.com.au header.b="E4I0GaXs"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 414Ynt281mz9s0w for ; Tue, 12 Jun 2018 12:32:50 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935098AbeFLCcm (ORCPT ); Mon, 11 Jun 2018 22:32:42 -0400 Received: from f0-dek.dektech.com.au ([210.10.221.142]:32878 "EHLO mail.dektech.com.au" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S935077AbeFLCcl (ORCPT ); Mon, 11 Jun 2018 22:32:41 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.dektech.com.au (Postfix) with ESMTP id 372EFF2B94; Tue, 12 Jun 2018 12:32:38 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=dektech.com.au; h=x-mailer:message-id:date:date:subject:subject:from:from :received:received:received; s=mail_dkim; t=1528770758; bh=Rjuma kTRfQoRRlNr8s783gpLvTSqZtR7xF5D1tC+uQI=; b=E4I0GaXspfz6I2/ejKTRm tRzoJw/pzteLUfOXxUlYE5/UeQ1ssabwinYwnBzVJVpsOxSpqtLXBh/ARAYJgOQe xRyUueWh30hGWwVmBhTeKs4uUcNP8RVaspDR0Z5OuOOaXl0ik8IDa6DcbcOpXmBb aDD9/pSsnz9OC5wOCw5BnQ= X-Virus-Scanned: amavisd-new at dektech.com.au Received: from mail.dektech.com.au ([127.0.0.1]) by localhost (mail2.dektech.com.au [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id uA121OyXEf1d; Tue, 12 Jun 2018 12:32:38 +1000 (AEST) Received: from mail.dektech.com.au (localhost [127.0.0.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.dektech.com.au (Postfix) with ESMTPS id E71BEF2B93; Tue, 12 Jun 2018 12:32:37 +1000 (AEST) Received: from build.dek-tpc.internal (unknown [14.161.14.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) by mail.dektech.com.au (Postfix) with ESMTPSA id 1E52CF2B92; Tue, 12 Jun 2018 12:32:36 +1000 (AEST) From: Hoang Le To: netdev@vger.kernel.org, tipc-discussion@lists.sourceforge.net Subject: [iproute2-next v3 1/2] tipc: JSON support for showing nametable Date: Tue, 12 Jun 2018 09:32:28 +0700 Message-Id: <1528770749-10415-1-git-send-email-hoang.h.le@dektech.com.au> X-Mailer: git-send-email 2.7.4 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add json output support for nametable show Example output: $tipc -j -p nametable show [ { "type": 0, "lower": 16781313, "upper": 16781313, "scope": "zone", "port": 0, "node": "" },{ "type": 0, "lower": 16781416, "upper": 16781416, "scope": "cluster", "port": 0, "node": "" } ] v2: Replace variable 'json_flag' by 'json' declared in include/utils.h Add new parameter '-pretty' to support pretty output v3: Update manual page Acked-by: Jon Maloy Signed-off-by: Hoang Le --- man/man8/tipc-nametable.8 | 10 ++++++++++ man/man8/tipc.8 | 9 +++++++++ tipc/nametable.c | 31 ++++++++++++++++++++++--------- tipc/tipc.c | 20 +++++++++++++++++++- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/man/man8/tipc-nametable.8 b/man/man8/tipc-nametable.8 index 4bcefe47f6b1..b187d25e1e1f 100644 --- a/man/man8/tipc-nametable.8 +++ b/man/man8/tipc-nametable.8 @@ -18,6 +18,16 @@ tipc-nametable \- show TIPC nametable Options (flags) that can be passed anywhere in the command chain. .TP .BR "\-h" , " --help" + +.TP +.BR "\-j", " \-json" +Output results in JavaScript Object Notation (JSON). + +.TP +.BR "\-p", " \-pretty" +The default JSON format is compact and more efficient to parse but hard for most users to read. +This flag adds indentation for readability. + Show help about last valid command. For example .B tipc nametable --help will show nametable help and diff --git a/man/man8/tipc.8 b/man/man8/tipc.8 index 32943fa50b23..6706cca12427 100644 --- a/man/man8/tipc.8 +++ b/man/man8/tipc.8 @@ -40,6 +40,15 @@ will show bearer help and .B tipc --help will show general help. The position of the option in the string is irrelevant. +.TP +.BR "\-j", " \-json" +Output results in JavaScript Object Notation (JSON). + +.TP +.BR "\-p", " \-pretty" +The default JSON format is compact and more efficient to parse but hard for most users to read. +This flag adds indentation for readability. + .SH COMMANDS .TP diff --git a/tipc/nametable.c b/tipc/nametable.c index ae73dfa5f8b9..eb4bd0bda835 100644 --- a/tipc/nametable.c +++ b/tipc/nametable.c @@ -21,6 +21,7 @@ #include "msg.h" #include "nametable.h" #include "misc.h" +#include "utils.h" #define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */ @@ -46,7 +47,7 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data) if (!publ[TIPC_NLA_NAME_TABLE_PUBL]) return MNL_CB_ERROR; - if (!*iteration) + if (!*iteration && !is_json_context()) printf("%-10s %-10s %-10s %-8s %-10s %-33s\n", "Type", "Lower", "Upper", "Scope", "Port", "Node"); @@ -54,13 +55,20 @@ static int nametable_show_cb(const struct nlmsghdr *nlh, void *data) hash2nodestr(mnl_attr_get_u32(publ[TIPC_NLA_PUBL_NODE]), str); - printf("%-10u %-10u %-10u %-8s %-10u %s\n", - mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE]), - mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER]), - mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER]), - scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])], - mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF]), - str); + open_json_object(NULL); + print_uint(PRINT_ANY, "type", "%-10u", + mnl_attr_get_u32(publ[TIPC_NLA_PUBL_TYPE])); + print_uint(PRINT_ANY, "lower", "%-10u", + mnl_attr_get_u32(publ[TIPC_NLA_PUBL_LOWER])); + print_uint(PRINT_ANY, "upper", "%-10u", + mnl_attr_get_u32(publ[TIPC_NLA_PUBL_UPPER])); + print_string(PRINT_ANY, "scope", "%-8s", + scope[mnl_attr_get_u32(publ[TIPC_NLA_PUBL_SCOPE])]); + print_uint(PRINT_ANY, "port", "%-10u", + mnl_attr_get_u32(publ[TIPC_NLA_PUBL_REF])); + print_string(PRINT_ANY, "node", "%s", str); + print_string(PRINT_FP, NULL, "\n", ""); + close_json_object(); return MNL_CB_OK; } @@ -70,6 +78,7 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd, { int iteration = 0; char buf[MNL_SOCKET_BUFFER_SIZE]; + int rc = 0; if (help_flag) { fprintf(stderr, "Usage: %s nametable show\n", cmdl->argv[0]); @@ -81,7 +90,11 @@ static int cmd_nametable_show(struct nlmsghdr *nlh, const struct cmd *cmd, return -1; } - return msg_dumpit(nlh, nametable_show_cb, &iteration); + new_json_obj(json); + rc = msg_dumpit(nlh, nametable_show_cb, &iteration); + delete_json_obj(); + + return rc; } void cmd_nametable_help(struct cmdl *cmdl) diff --git a/tipc/tipc.c b/tipc/tipc.c index 600d5e2a160f..f85ddee0e278 100644 --- a/tipc/tipc.c +++ b/tipc/tipc.c @@ -24,6 +24,8 @@ #include "cmdl.h" int help_flag; +int json; +int pretty; static void about(struct cmdl *cmdl) { @@ -33,6 +35,8 @@ static void about(struct cmdl *cmdl) "\n" "Options:\n" " -h, --help \t\tPrint help for last given command\n" + " -j, --json \t\tJson format printouts\n" + " -p, --pretty \t\tpretty print\n" "\n" "Commands:\n" " bearer - Show or modify bearers\n" @@ -53,6 +57,8 @@ int main(int argc, char *argv[]) const struct cmd cmd = {"tipc", NULL, about}; struct option long_options[] = { {"help", no_argument, 0, 'h'}, + {"json", no_argument, 0, 'j'}, + {"pretty", no_argument, 0, 'p'}, {0, 0, 0, 0} }; const struct cmd cmds[] = { @@ -69,7 +75,7 @@ int main(int argc, char *argv[]) do { int option_index = 0; - i = getopt_long(argc, argv, "h", long_options, &option_index); + i = getopt_long(argc, argv, "hjp", long_options, &option_index); switch (i) { case 'h': @@ -79,6 +85,18 @@ int main(int argc, char *argv[]) */ help_flag = 1; break; + case 'j': + /* + * Enable json format printouts + */ + json = 1; + break; + case 'p': + /* + * Enable json pretty output + */ + pretty = 1; + break; case -1: /* End of options */ break;