From 12024b1c2e5bb1acc8f21d78504f7a473fbf5429 Mon Sep 17 00:00:00 2001 From: Kerin Millar Date: Sun, 26 Jun 2022 15:20:23 +0100 Subject: nlmsg: Only print ECMA-48 colour sequences to terminals Check isatty() to skip colors for non-terminals. Add mnl_fprintf_attr_color() and mnl_fprintf_attr_raw() helper function. Joint work with Pablo. Signed-off-by: Kerin Millar Signed-off-by: Pablo Neira Ayuso --- src/nlmsg.c | 76 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 23 deletions(-) (limited to 'src/nlmsg.c') diff --git a/src/nlmsg.c b/src/nlmsg.c index ce37cbc..e7014bd 100644 --- a/src/nlmsg.c +++ b/src/nlmsg.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "internal.h" @@ -244,11 +245,57 @@ static void mnl_nlmsg_fprintf_header(FILE *fd, const struct nlmsghdr *nlh) fprintf(fd, "----------------\t------------------\n"); } +static void mnl_fprintf_attr_color(FILE *fd, const struct nlattr *attr) +{ + fprintf(fd, "|%c[%d;%dm" + "%.5u" + "%c[%dm" + "|" + "%c[%d;%dm" + "%c%c" + "%c[%dm" + "|" + "%c[%d;%dm" + "%.5u" + "%c[%dm|\t", + 27, 1, 31, + attr->nla_len, + 27, 0, + 27, 1, 32, + attr->nla_type & NLA_F_NESTED ? 'N' : '-', + attr->nla_type & NLA_F_NET_BYTEORDER ? 'B' : '-', + 27, 0, + 27, 1, 34, + attr->nla_type & NLA_TYPE_MASK, + 27, 0); +} + +static void mnl_fprintf_attr_raw(FILE *fd, const struct nlattr *attr) +{ + fprintf(fd, "|" + "%.5u" + "|" + "%c%c" + "|" + "%.5u" + "|\t", + attr->nla_len, + attr->nla_type & NLA_F_NESTED ? 'N' : '-', + attr->nla_type & NLA_F_NET_BYTEORDER ? 'B' : '-', + attr->nla_type & NLA_TYPE_MASK); +} + static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh, size_t extra_header_size) { - int rem = 0; + int colorize = 0; unsigned int i; + int rem = 0; + int fdnum; + + fdnum = fileno(fd); + if (fdnum != -1) + colorize = isatty(fdnum); for (i=sizeof(struct nlmsghdr); inlmsg_len; i+=4) { char *b = (char *) nlh; @@ -269,28 +316,11 @@ static void mnl_nlmsg_fprintf_payload(FILE *fd, const struct nlmsghdr *nlh, fprintf(fd, "| extra header |\n"); /* this seems like an attribute header. */ } else if (rem == 0 && (attr->nla_type & NLA_TYPE_MASK) != 0) { - fprintf(fd, "|%c[%d;%dm" - "%.5u" - "%c[%dm" - "|" - "%c[%d;%dm" - "%c%c" - "%c[%dm" - "|" - "%c[%d;%dm" - "%.5u" - "%c[%dm|\t", - 27, 1, 31, - attr->nla_len, - 27, 0, - 27, 1, 32, - attr->nla_type & NLA_F_NESTED ? 'N' : '-', - attr->nla_type & - NLA_F_NET_BYTEORDER ? 'B' : '-', - 27, 0, - 27, 1, 34, - attr->nla_type & NLA_TYPE_MASK, - 27, 0); + if (colorize) { + mnl_fprintf_attr_color(fd, attr); + } else { + mnl_fprintf_attr_raw(fd, attr); + } fprintf(fd, "|len |flags| type|\n"); if (!(attr->nla_type & NLA_F_NESTED)) { -- cgit v1.2.3