Signed-off-by: Hoang Le <[email protected]>
---
tipc/nametable.c | 33 ++++++++++++++++++++++++---------
tipc/tipc.c | 11 ++++++++++-
2 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/tipc/nametable.c b/tipc/nametable.c
index ae73dfa5f8b9..3d1621709954 100644
--- a/tipc/nametable.c
+++ b/tipc/nametable.c
@@ -21,6 +21,9 @@
#include "msg.h"
#include "nametable.h"
#include "misc.h"
+#include "json_print.h"
+
+extern int json_flag;
#define PORTID_STR_LEN 45 /* Four u32 and five delimiter chars */
@@ -46,7 +49,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 +57,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 +80,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 +92,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_flag);
+ 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..ed8457a464b5 100644
--- a/tipc/tipc.c
+++ b/tipc/tipc.c
@@ -24,6 +24,7 @@
#include "cmdl.h"
int help_flag;
+int json_flag;
static void about(struct cmdl *cmdl)
{
@@ -33,6 +34,7 @@ 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"
"\n"
"Commands:\n"
" bearer - Show or modify bearers\n"
@@ -53,6 +55,7 @@ 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'},
{0, 0, 0, 0}
};
const struct cmd cmds[] = {
@@ -69,7 +72,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, "hj", long_options, &option_index);
switch (i) {
case 'h':
@@ -79,6 +82,12 @@ int main(int argc, char *argv[])
*/
help_flag = 1;
break;
+ case 'j':
+ /*
+ * Enable json format printouts
+ */
+ json_flag = 1;
+ break;
case -1:
/* End of options */
break;
--
2.7.4
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
tipc-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tipc-discussion