public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber•org>
To: dsahern@gmail•com
Cc: netdev@vger•kernel.org,
	Stephen Hemminger <sthemmin@microsoft•com>,
	Stephen Hemminger <stephen@networkplumber•org>
Subject: [PATCH iproute2-next 1/3] ipmaddr: json and color support
Date: Tue,  6 Mar 2018 17:03:53 -0800	[thread overview]
Message-ID: <20180307010355.5011-2-stephen@networkplumber.org> (raw)
In-Reply-To: <20180307010355.5011-1-stephen@networkplumber.org>

From: Stephen Hemminger <sthemmin@microsoft•com>

Support printing mulitcast addresses in json and color mode.
Output format is unchanged for normal use.

Signed-off-by: Stephen Hemminger <stephen@networkplumber•org>
---
 ip/ipmaddr.c | 69 +++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 43 insertions(+), 26 deletions(-)

diff --git a/ip/ipmaddr.c b/ip/ipmaddr.c
index d7bf1f99f67e..a48499029e17 100644
--- a/ip/ipmaddr.c
+++ b/ip/ipmaddr.c
@@ -28,6 +28,7 @@
 #include "rt_names.h"
 #include "utils.h"
 #include "ip_common.h"
+#include "json_print.h"
 
 static struct {
 	char *dev;
@@ -193,50 +194,66 @@ static void read_igmp6(struct ma_info **result_p)
 
 static void print_maddr(FILE *fp, struct ma_info *list)
 {
-	fprintf(fp, "\t");
+	print_string(PRINT_FP, NULL, "\t", NULL);
 
+	open_json_object(NULL);
 	if (list->addr.family == AF_PACKET) {
 		SPRINT_BUF(b1);
-		fprintf(fp, "link  %s", ll_addr_n2a((unsigned char *)list->addr.data,
-						    list->addr.bytelen, 0,
-						    b1, sizeof(b1)));
+
+		print_string(PRINT_FP, NULL, "link  ", NULL);
+		print_color_string(PRINT_ANY, COLOR_MAC, "link", "%s",
+				   ll_addr_n2a((void *)list->addr.data, list->addr.bytelen,
+					       0, b1, sizeof(b1)));
 	} else {
-		switch (list->addr.family) {
-		case AF_INET:
-			fprintf(fp, "inet  ");
-			break;
-		case AF_INET6:
-			fprintf(fp, "inet6 ");
-			break;
-		default:
-			fprintf(fp, "family %d ", list->addr.family);
-			break;
-		}
-		fprintf(fp, "%s",
-			format_host(list->addr.family,
-				    -1, list->addr.data));
+		print_string(PRINT_ANY, "family", "%-5s ",
+			     family_name(list->addr.family));
+		print_color_string(PRINT_ANY, ifa_family_color(list->addr.family),
+				   "address", "%s",
+				   format_host(list->addr.family,
+					       -1, list->addr.data));
 	}
+
 	if (list->users != 1)
-		fprintf(fp, " users %d", list->users);
+		print_uint(PRINT_ANY, "users", " users %u", list->users);
+
 	if (list->features)
-		fprintf(fp, " %s", list->features);
-	fprintf(fp, "\n");
+		print_string(PRINT_ANY, "features", " %s", list->features);
+
+	print_string(PRINT_FP, NULL, "\n", NULL);
+	close_json_object();
 }
 
 static void print_mlist(FILE *fp, struct ma_info *list)
 {
 	int cur_index = 0;
 
+	new_json_obj(json);
 	for (; list; list = list->next) {
-		if (oneline) {
-			cur_index = list->index;
-			fprintf(fp, "%d:\t%s%s", cur_index, list->name, _SL_);
-		} else if (cur_index != list->index) {
+
+		if (list->index != cur_index || oneline) {
+			if (cur_index) {
+				close_json_array(PRINT_JSON, NULL);
+				close_json_object();
+			}
+			open_json_object(NULL);
+
+			print_uint(PRINT_ANY, "ifindex", "%d:", list->index);
+			print_color_string(PRINT_ANY, COLOR_IFNAME,
+					   "ifname", "\t%s", list->name);
+			print_string(PRINT_FP, NULL, "%s", _SL_);
 			cur_index = list->index;
-			fprintf(fp, "%d:\t%s\n", cur_index, list->name);
+
+			open_json_array(PRINT_JSON, "maddr");
 		}
+
 		print_maddr(fp, list);
 	}
+	if (cur_index) {
+		close_json_array(PRINT_JSON, NULL);
+		close_json_object();
+	}
+
+	delete_json_obj();
 }
 
 static int multiaddr_list(int argc, char **argv)
-- 
2.16.1

  reply	other threads:[~2018-03-07  1:03 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-07  1:03 [PATCH iproute2-next 0/3] ip multicast command JSON support Stephen Hemminger
2018-03-07  1:03 ` Stephen Hemminger [this message]
2018-03-07  1:03 ` [PATCH iproute2-next 2/3] ipmroute: don't complain about unicast routes Stephen Hemminger
2018-03-07  8:43   ` Sergei Shtylyov
2018-03-07 15:54     ` Stephen Hemminger
2018-03-07 15:56       ` Sergei Shtylyov
2018-03-07 16:03   ` Stephen Hemminger
2018-03-07 16:47     ` David Ahern
2018-03-07 16:51   ` Stephen Hemminger
2018-03-08 17:12     ` David Ahern
2018-03-07  1:03 ` [PATCH iproute2-next 3/3] ipmroute: convert to output JSON Stephen Hemminger
  -- strict thread matches above, loose matches on Subject: below --
2018-03-09  2:02 [PATCH iproute2-next 0/3] ip: multicast commands JSON Stephen Hemminger
2018-03-09  2:02 ` [PATCH iproute2-next 1/3] ipmaddr: json and color support Stephen Hemminger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180307010355.5011-2-stephen@networkplumber.org \
    --to=stephen@networkplumber$(echo .)org \
    --cc=dsahern@gmail$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=sthemmin@microsoft$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox