]> granicus.if.org Git - procps-ng/commitdiff
watch: Fix handling of ANSI color escapes for -c
authorSean Silva <chisophugis@gmail.com>
Fri, 18 Oct 2013 06:04:15 +0000 (02:04 -0400)
committerJaromir Capik <jcapik@redhat.com>
Wed, 20 Aug 2014 17:13:20 +0000 (19:13 +0200)
The previous code assumed that there would be 1 or 2 attributes to
apply. In fact, there can in general be any number (but typically
between 1 and 3). This commit generalizes the existing code to read
arbitrarily many attributes from the escape sequence.

watch.c

diff --git a/watch.c b/watch.c
index f0a3ec39ecfb33d5b75d4adde21dcb3a738bc867..37fd4ea0edb1cca56f4c1fce778c233a97b406f5 100644 (file)
--- a/watch.c
+++ b/watch.c
@@ -138,9 +138,9 @@ static void set_ansi_attribute(const int attrib)
 
 static void process_ansi(FILE * fp)
 {
-       int i, c, num1, num2;
+       int i, c;
        char buf[MAX_ANSIBUF];
-       char *nextnum;
+       char *numstart, *endptr;
 
        c = getc(fp);
        if (c != '[') {
@@ -161,13 +161,15 @@ static void process_ansi(FILE * fp)
                }
                buf[i] = (char)c;
        }
-       num1 = strtol(buf, &nextnum, 10);
-       if (nextnum != buf && nextnum[0] != '\0')
-               num2 = strtol(nextnum + 1, NULL, 10);
-       else
-               num2 = -1;
-       set_ansi_attribute(num1);
-       set_ansi_attribute(num2);
+       /*
+        * buf now contains a semicolon-separated list of decimal integers,
+        * each indicating an attribute to apply.
+        * For example, buf might contain "0;1;31", derived from the color
+        * escape sequence "<ESC>[0;1;31m". There can be 1 or more
+        * attributes to apply, but typically there are between 1 and 3.
+        */
+       for (numstart = buf; *endptr != '\0'; numstart = endptr + 1)
+               set_ansi_attribute(strtol(numstart, &endptr, 10));
 }
 
 static void __attribute__ ((__noreturn__)) do_exit(int status)