strip ANSI color markers in non-interactive mode
authorChristophe Varoqui <christophe.varoqui@free.fr>
Thu, 20 Nov 2008 22:16:01 +0000 (23:16 +0100)
committerChristophe Varoqui <christophe.varoqui@free.fr>
Thu, 20 Nov 2008 22:16:01 +0000 (23:16 +0100)
Tested with piped and redirected stdout, using either "multipath -l"
or "multipathd show topo". Seems there are no more blatant regression.
Let me know if there are, because I'm tempted to go a little further
with those ANSI codes by highlighting degrated/failure situation.

libmultipath/print.c
multipathd/uxclnt.c

index 31af478..595067b 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdarg.h>
 #include <sys/stat.h>
 #include <dirent.h>
+#include <unistd.h>
 
 #include "checkers.h"
 #include "vector.h"
@@ -759,7 +760,8 @@ snprint_multipath_topology (char * buff, int len, struct multipath * mpp,
        if (verbosity == 1)
                return snprint_multipath(buff, len, "%n", mpp);
 
-       c += sprintf(c, "%c[%dm", 0x1B, 1); /* bold on */
+       if(isatty(1))
+               c += sprintf(c, "%c[%dm", 0x1B, 1); /* bold on */
 
        if (verbosity > 1 &&
            mpp->action != ACT_NOTHING &&
@@ -772,7 +774,8 @@ snprint_multipath_topology (char * buff, int len, struct multipath * mpp,
                c += sprintf(c, " (%%w)");
 
        c += sprintf(c, " %%d %%s");
-       c += sprintf(c, "%c[%dm", 0x1B, 0); /* bold off */
+       if(isatty(1))
+               c += sprintf(c, "%c[%dm", 0x1B, 0); /* bold off */
 
        fwd += snprint_multipath(buff + fwd, len - fwd, style, mpp);
        if (fwd > len)
index bc89294..6de496b 100644 (file)
 #include <vector.h>
 #include "cli.h"
 
+static void print_reply(char *s)
+{
+       if (isatty(1)) {
+               printf("%s", s);
+               return;
+       }
+       /* strip ANSI color markers */
+       while (*s != '\0') {
+               if ((*s == 0x1b) && (*(s+1) == '['))
+                       while ((*s++ != 'm') && (*s != '\0')) {}; 
+               putchar(*s++);
+       }
+}
 /*
  * process the client 
  */
@@ -51,7 +64,7 @@ static void process(int fd)
                if (send_packet(fd, line, llen + 1) != 0) break;
                if (recv_packet(fd, &reply, &len) != 0) break;
 
-               printf("%s", reply);
+               print_reply(reply);
 
                if (line && *line)
                        add_history(line);
@@ -69,7 +82,7 @@ static void process_req(int fd, char * inbuf)
        send_packet(fd, inbuf, strlen(inbuf) + 1);
        recv_packet(fd, &reply, &len);
 
-       printf("%s", reply);
+       print_reply(reply);
        FREE(reply);
 }