]> git.refcnt.org Git - colorize.git/commitdiff
Clean sequences more strictly
authorSteven Schubiger <stsc@refcnt.org>
Wed, 3 Jul 2013 21:40:37 +0000 (23:40 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Wed, 3 Jul 2013 21:40:37 +0000 (23:40 +0200)
colorize.c
test.pl

index c402a0156759ab464e76345f357f6daa449b003e..02190f97a8350b23ea46cf8fc3a20f17045a362c 100644 (file)
@@ -824,13 +824,13 @@ print_clean (const char *line)
               }
             else if (clean)
               {
-                bool check_values, first = true;
-                if (!isdigit (*p))
-                  goto END;
+                bool check_values;
+                unsigned int iter = 0;
+                const char *digit;
                 do {
-                  const char *digit;
                   check_values = false;
-                  if (!first && !isdigit (*p))
+                  iter++;
+                  if (!isdigit (*p))
                     goto DISCARD;
                   digit = p;
                   while (isdigit (*p))
@@ -847,19 +847,36 @@ print_clean (const char *line)
                         val[i] = *digit++;
                       val[i] = '\0';
                       value = atoi (val);
-                      if (!((value ==  0 || value ==  1)   /* attributes        */
-                         || (value >= 30 && value <= 37)   /* foreground colors */
-                         || (value >= 40 && value <= 47)   /* background colors */
-                         || (value == 39 || value == 49))) /* default colors    */
+                      if (value == 0) /* reset */
+                        {
+                          if (iter > 1)
+                            goto DISCARD;
+                          goto END;
+                        }
+                      else if (value == 1) /* bold */
+                        {
+                          bool discard = false;
+                          if (iter > 1)
+                            discard = true;
+                          else if (*p != ';')
+                            discard = true;
+                          if (discard)
+                            goto DISCARD;
+                          p++;
+                          check_values = true;
+                        }
+                      else if ((value >= 30 && value <= 37) || value == 39) /* foreground colors */
+                        goto END;
+                      else if ((value >= 40 && value <= 47) || value == 49) /* background colors */
+                        {
+                          if (iter > 1)
+                            goto DISCARD;
+                          goto END;
+                        }
+                      else
                         goto DISCARD;
                     }
-                  if (*p == ';')
-                    {
-                      p++;
-                      check_values = true;
-                    }
-                  first = false;
-                } while (check_values);
+                } while (iter == 1 && check_values);
               }
             END: if (*p == 'm')
               {
diff --git a/test.pl b/test.pl
index 373a4bcde96d74f4ab284470406e3c63a24d4ab4..056f818d7b4d6d27c6649e9572fb1eb025baa4e1 100755 (executable)
--- a/test.pl
+++ b/test.pl
@@ -7,7 +7,7 @@ use constant true => 1;
 use File::Temp qw(tempfile tmpnam);
 use Test::More;
 
-my $tests = 21;
+my $tests = 22;
 
 my %BUF_SIZE = (
    normal => 1024,
@@ -47,6 +47,21 @@ SKIP: {
     is_deeply([split /\n/, qx(cat $infile1 | $program none/none)], [split /\n/, $text], 'text read from stdin');
     is_deeply([split /\n/, qx($program none/none $infile1)],       [split /\n/, $text], 'text read from file');
 
+    {
+        my @fg_colors = (30..37, 39);
+        my @bg_colors = (40..47, 49);
+
+        my @bold_colors = map "1;$_", @fg_colors;
+
+        my @values = (@fg_colors, @bg_colors, @bold_colors, 0);
+
+        my $ok = true;
+        foreach my $value (@values) {
+            $ok &= qx(echo -n "\e[${value}m" | $program --clean) eq '';
+        }
+        ok($ok, 'clean color sequences');
+    }
+
     my $check_clean = sub
     {
         my ($type) = @_;