From patchwork Fri Apr 17 13:02:02 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mathias Nyman X-Patchwork-Id: 462037 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id D4B6D1402D5 for ; Fri, 17 Apr 2015 23:02:10 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932082AbbDQNCG (ORCPT ); Fri, 17 Apr 2015 09:02:06 -0400 Received: from mail.kapsi.fi ([217.30.184.167]:43905 "EHLO mail.kapsi.fi" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753865AbbDQNCF (ORCPT ); Fri, 17 Apr 2015 09:02:05 -0400 Received: from lakka.kapsi.fi ([2001:1bc8:1004::1] ident=Debian-exim) by mail.kapsi.fi with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1Yj5uE-00085s-IX; Fri, 17 Apr 2015 16:02:02 +0300 Received: from lem by lakka.kapsi.fi with local (Exim 4.80) (envelope-from ) id 1Yj5uE-0005c1-Cn; Fri, 17 Apr 2015 16:02:02 +0300 Date: Fri, 17 Apr 2015 16:02:02 +0300 From: Mathias Nyman To: netdev@vger.kernel.org Cc: aleksi.aalto@iki.fi Subject: [PATCH net-next] ip: Add color output option Message-ID: <20150417130202.GA29596@iki.fi> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-SA-Exim-Connect-IP: 2001:1bc8:1004::1 X-SA-Exim-Mail-From: lem@kapsi.fi X-SA-Exim-Scanned: No (on mail.kapsi.fi); SAEximRunCond expanded to false Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org It is hard to quickly find what you are looking for in the output of the ip command. Color helps. This patch adds a '-c' flag to highlight these with individual colors: - interface name - ip addresse - mac addresse - up/down state --- include/color.h | 17 ++++++++++++++++ ip/Makefile | 2 +- ip/color.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ip/ip.c | 5 ++++- ip/ipaddress.c | 43 +++++++++++++++++++++++++++++---------- man/man8/ip.8 | 7 ++++++- 6 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 include/color.h create mode 100644 ip/color.c diff --git a/include/color.h b/include/color.h new file mode 100644 index 0000000..1662c62 --- /dev/null +++ b/include/color.h @@ -0,0 +1,17 @@ +#ifndef __COLOR_H__ +#define __COLOR_H__ 1 + +enum color_attr +{ + COLOR_IFNAME, + COLOR_MAC, + COLOR_INET, + COLOR_INET6, + COLOR_OPERSTATE_UP, + COLOR_OPERSTATE_DOWN +}; + +void enable_color(void); +int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...); + +#endif diff --git a/ip/Makefile b/ip/Makefile index 5637bcf..ac35131 100644 --- a/ip/Makefile +++ b/ip/Makefile @@ -6,7 +6,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o ipnetns.o \ iplink_macvlan.o iplink_macvtap.o ipl2tp.o link_vti.o link_vti6.o \ iplink_vxlan.o tcp_metrics.o iplink_ipoib.o ipnetconf.o link_ip6tnl.o \ link_iptnl.o link_gre6.o iplink_bond.o iplink_bond_slave.o iplink_hsr.o \ - iplink_bridge.o iplink_bridge_slave.o ipfou.o iplink_ipvlan.o + iplink_bridge.o iplink_bridge_slave.o ipfou.o iplink_ipvlan.o color.o RTMONOBJ=rtmon.o diff --git a/ip/color.c b/ip/color.c new file mode 100644 index 0000000..2e73fcb --- /dev/null +++ b/ip/color.c @@ -0,0 +1,63 @@ +#include +#include + +#include "color.h" + +enum color +{ + C_RED, + C_GREEN, + C_YELLOW, + C_BLUE, + C_MAGENTA, + C_CYAN, + C_WHITE, + C_CLEAR +}; + +static const char * const color_codes[] = +{ + "\e[31m", + "\e[32m", + "\e[33m", + "\e[34m", + "\e[35m", + "\e[36m", + "\e[37m", + "\e[0m", + NULL, +}; + +static enum color attr_colors[] = +{ + C_CYAN, + C_YELLOW, + C_MAGENTA, + C_BLUE, + C_GREEN, + C_RED +}; + +static int color_enable = 0; + +void enable_color(void) +{ + ++color_enable; +} + +int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...) +{ + int ret = 0; + va_list args; + va_start(args, fmt); + + if (!color_enable) + return vfprintf(fp, fmt, args); + + ret += fprintf(fp, "%s", color_codes[attr_colors[attr]]); + ret += vfprintf(fp, fmt, args); + ret += fprintf(fp, "%s", color_codes[C_CLEAR]); + + va_end(args); + return ret; +} diff --git a/ip/ip.c b/ip/ip.c index f7f214b..c23de74 100644 --- a/ip/ip.c +++ b/ip/ip.c @@ -23,6 +23,7 @@ #include "utils.h" #include "ip_common.h" #include "namespace.h" +#include "color.h" int preferred_family = AF_UNSPEC; int human_readable = 0; @@ -56,7 +57,7 @@ static void usage(void) " -4 | -6 | -I | -D | -B | -0 |\n" " -l[oops] { maximum-addr-flush-attempts } |\n" " -o[neline] | -t[imestamp] | -ts[hort] | -b[atch] [filename] |\n" -" -rc[vbuf] [size] | -n[etns] name | -a[ll] }\n"); +" -rc[vbuf] [size] | -n[etns] name | -a[ll] | -c[olor]}\n"); exit(-1); } @@ -257,6 +258,8 @@ int main(int argc, char **argv) exit(-1); } rcvbuf = size; + } else if (matches(opt, "-color") == 0) { + enable_color(); } else if (matches(opt, "-help") == 0) { usage(); } else if (matches(opt, "-netns") == 0) { diff --git a/ip/ipaddress.c b/ip/ipaddress.c index e582da0..92afa49 100644 --- a/ip/ipaddress.c +++ b/ip/ipaddress.c @@ -34,6 +34,7 @@ #include "utils.h" #include "ll_map.h" #include "ip_common.h" +#include "color.h" enum { IPADD_LIST, @@ -136,8 +137,15 @@ static void print_operstate(FILE *f, __u8 state) { if (state >= sizeof(oper_states)/sizeof(oper_states[0])) fprintf(f, "state %#x ", state); - else - fprintf(f, "state %s ", 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]); + } } int get_operstate(const char *name) @@ -606,7 +614,8 @@ int print_linkinfo(const struct sockaddr_nl *who, if (n->nlmsg_type == RTM_DELLINK) fprintf(fp, "Deleted "); - fprintf(fp, "%d: %s", ifi->ifi_index, + fprintf(fp, "%d: ", ifi->ifi_index); + color_fprintf(fp, COLOR_IFNAME, "%s", tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : ""); if (tb[IFLA_LINK]) { @@ -666,10 +675,11 @@ int print_linkinfo(const struct sockaddr_nl *who, fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); if (tb[IFLA_ADDRESS]) { - fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), - RTA_PAYLOAD(tb[IFLA_ADDRESS]), - ifi->ifi_type, - b1, sizeof(b1))); + color_fprintf(fp, COLOR_MAC, "%s", + ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), + RTA_PAYLOAD(tb[IFLA_ADDRESS]), + ifi->ifi_type, + b1, sizeof(b1))); } if (tb[IFLA_BROADCAST]) { if (ifi->ifi_flags&IFF_POINTOPOINT) @@ -849,10 +859,21 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, fprintf(fp, " family %d ", ifa->ifa_family); if (rta_tb[IFA_LOCAL]) { - fprintf(fp, "%s", format_host(ifa->ifa_family, - RTA_PAYLOAD(rta_tb[IFA_LOCAL]), - RTA_DATA(rta_tb[IFA_LOCAL]), - abuf, sizeof(abuf))); + 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))); if (rta_tb[IFA_ADDRESS] == NULL || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), diff --git a/man/man8/ip.8 b/man/man8/ip.8 index 44d1ee6..43ddac9 100644 --- a/man/man8/ip.8 +++ b/man/man8/ip.8 @@ -33,7 +33,8 @@ ip \- show / manipulate routing, devices, policy routing and tunnels .BR inet " | " inet6 " | " ipx " | " dnet " | " link " } | " \fB\-o\fR[\fIneline\fR] | \fB\-n\fR[\fIetns\fR] name | -\fB\-a\fR[\fIll\fR] } +\fB\-a\fR[\fIll\fR] | +\fB\-c\fR[\fIolor\fR] } .SH OPTIONS @@ -165,6 +166,10 @@ to .BR "\-a" , " \-all" executes specified command over all objects, it depends if command supports this option. +.TP +.BR "\-c" , " -color" +Use color output. + .SH IP - COMMAND SYNTAX .SS