netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Sutter <phil@nwl.cc>
To: Stephen Hemminger <shemming@brocade.com>
Cc: netdev@vger.kernel.org
Subject: [iproute PATCH 1/7] color: introduce color helpers and COLOR_CLEAR
Date: Tue, 22 Mar 2016 19:35:13 +0100	[thread overview]
Message-ID: <1458671719-14361-2-git-send-email-phil@nwl.cc> (raw)
In-Reply-To: <1458671719-14361-1-git-send-email-phil@nwl.cc>

This adds two helper functions which map a given data field to a color,
so color_fprintf() statements don't have to be duplicated with only a
different color value depending on that data field's value. In order for
this to work in a generic way, COLOR_CLEAR has been added to serve as a
fallback default of uncolored output.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 include/color.h |  5 ++++-
 ip/ipaddress.c  | 47 +++++++++++++----------------------------------
 lib/color.c     | 30 +++++++++++++++++++++++++++++-
 3 files changed, 46 insertions(+), 36 deletions(-)

diff --git a/include/color.h b/include/color.h
index b85003aed19f8..c1c29831159af 100644
--- a/include/color.h
+++ b/include/color.h
@@ -7,10 +7,13 @@ enum color_attr {
 	COLOR_INET,
 	COLOR_INET6,
 	COLOR_OPERSTATE_UP,
-	COLOR_OPERSTATE_DOWN
+	COLOR_OPERSTATE_DOWN,
+	COLOR_CLEAR
 };
 
 void enable_color(void);
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
+enum color_attr ifa_family_color(__u8 ifa_family);
+enum color_attr oper_state_color(__u8 state);
 
 #endif
diff --git a/ip/ipaddress.c b/ip/ipaddress.c
index f8c5029400949..7aab8e781eae8 100644
--- a/ip/ipaddress.c
+++ b/ip/ipaddress.c
@@ -135,25 +135,15 @@ static const char *oper_states[] = {
 
 static void print_operstate(FILE *f, __u8 state)
 {
-	if (state >= ARRAY_SIZE(oper_states))
+	if (state >= ARRAY_SIZE(oper_states)) {
 		fprintf(f, "state %#x ", state);
-	else {
-		if (brief) {
-			if (strcmp(oper_states[state], "UP") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_UP, "%-14s ", oper_states[state]);
-			else if (strcmp(oper_states[state], "DOWN") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_DOWN, "%-14s ", oper_states[state]);
-			else
-				fprintf(f, "%-14s ", oper_states[state]);
-		} else {
-			fprintf(f, "state ");
-			if (strcmp(oper_states[state], "UP") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_UP, "%s ", oper_states[state]);
-			else if (strcmp(oper_states[state], "DOWN") == 0)
-				color_fprintf(f, COLOR_OPERSTATE_DOWN, "%s ", oper_states[state]);
-			else
-				fprintf(f, "%s ", oper_states[state]);
-		}
+	} else if (brief) {
+		color_fprintf(f, oper_state_color(state),
+		              "%-14s ", oper_states[state]);
+	} else {
+		fprintf(f, "state ");
+		color_fprintf(f, oper_state_color(state),
+		              "%s ", oper_states[state]);
 	}
 }
 
@@ -1067,22 +1057,11 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
 	}
 
 	if (rta_tb[IFA_LOCAL]) {
-		if (ifa->ifa_family == AF_INET)
-			color_fprintf(fp, COLOR_INET, "%s", format_host(ifa->ifa_family,
-						RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-						RTA_DATA(rta_tb[IFA_LOCAL]),
-						abuf, sizeof(abuf)));
-		else if (ifa->ifa_family == AF_INET6)
-			color_fprintf(fp, COLOR_INET6, "%s", format_host(ifa->ifa_family,
-						RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-						RTA_DATA(rta_tb[IFA_LOCAL]),
-						abuf, sizeof(abuf)));
-		else
-			fprintf(fp, "%s", format_host(ifa->ifa_family,
-						RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-						RTA_DATA(rta_tb[IFA_LOCAL]),
-						abuf, sizeof(abuf)));
-
+		color_fprintf(fp, ifa_family_color(ifa->ifa_family), "%s",
+		              format_host(ifa->ifa_family,
+		                          RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
+		                          RTA_DATA(rta_tb[IFA_LOCAL]),
+		                          abuf, sizeof(abuf)));
 		if (rta_tb[IFA_ADDRESS] == NULL ||
 		    memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]),
 			   ifa->ifa_family == AF_INET ? 4 : 16) == 0) {
diff --git a/lib/color.c b/lib/color.c
index 8c9a48ba702bf..95596be236a05 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -1,5 +1,8 @@
 #include <stdio.h>
 #include <stdarg.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <linux/if.h>
 
 #include "color.h"
 
@@ -32,7 +35,8 @@ static enum color attr_colors[] = {
 	C_MAGENTA,
 	C_BLUE,
 	C_GREEN,
-	C_RED
+	C_RED,
+	C_CLEAR
 };
 
 static int color_is_enabled;
@@ -62,3 +66,27 @@ end:
 	va_end(args);
 	return ret;
 }
+
+enum color_attr ifa_family_color(__u8 ifa_family)
+{
+	switch (ifa_family) {
+	case AF_INET:
+		return COLOR_INET;
+	case AF_INET6:
+		return COLOR_INET6;
+	default:
+		return COLOR_CLEAR;
+	}
+}
+
+enum color_attr oper_state_color(__u8 state)
+{
+	switch (state) {
+	case IF_OPER_UP:
+		return COLOR_OPERSTATE_UP;
+	case IF_OPER_DOWN:
+		return COLOR_OPERSTATE_DOWN;
+	default:
+		return COLOR_CLEAR;
+	}
+}
-- 
2.7.2

  reply	other threads:[~2016-03-22 18:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-22 18:35 [iproute PATCH 0/7] Refactor some internal library functions Phil Sutter
2016-03-22 18:35 ` Phil Sutter [this message]
2016-03-22 18:35 ` [iproute PATCH 2/7] ipaddress: colorize peer, broadcast and anycast addresses as well Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 3/7] make format_host non-reentrant by default Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 4/7] utils: make rt_addr_n2a() " Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 5/7] lib/utils: introduce format_host_rta() Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 6/7] lib/utils: introduce rt_addr_n2a_rta() Phil Sutter
2016-03-22 18:35 ` [iproute PATCH 7/7] lib/ll_addr: improve ll_addr_n2a() a bit Phil Sutter
2016-03-27 17:38 ` [iproute PATCH 0/7] Refactor some internal library functions 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=1458671719-14361-2-git-send-email-phil@nwl.cc \
    --to=phil@nwl.cc \
    --cc=netdev@vger.kernel.org \
    --cc=shemming@brocade.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
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;
as well as URLs for NNTP newsgroup(s).