#include "utils.h"
#include "ll_map.h"
#include "ip_common.h"
+#include "color.h"
enum {
IPADD_LIST,
{
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)
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]) : "<nil>");
if (tb[IFLA_LINK]) {
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)
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]),
--- /dev/null
+#include <stdio.h>
+#include <stdarg.h>
+
+#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_is_enabled;
+
+void enable_color(void)
+{
+ color_is_enabled = 1;
+}
+
+int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
+{
+ int ret = 0;
+ va_list args;
+
+ va_start(args, fmt);
+
+ if (!color_is_enabled) {
+ ret = vfprintf(fp, fmt, args);
+ goto end;
+ }
+
+ ret += fprintf(fp, "%s", color_codes[attr_colors[attr]]);
+ ret += vfprintf(fp, fmt, args);
+ ret += fprintf(fp, "%s", color_codes[C_CLEAR]);
+
+end:
+ va_end(args);
+ return ret;
+}