]> git.refcnt.org Git - colorize.git/commitdiff
Validate color provided to exclude-random switch
authorSteven Schubiger <stsc@refcnt.org>
Sat, 16 Nov 2013 22:19:06 +0000 (23:19 +0100)
committerSteven Schubiger <stsc@refcnt.org>
Sat, 16 Nov 2013 22:19:06 +0000 (23:19 +0100)
colorize.c
test.pl

index 319c799020a32b1e428d16e8024224d31caa2ce0..4303eb7f2b220ef3855987a3a9a3d908ab347f0c 100644 (file)
@@ -262,13 +262,21 @@ main (int argc, char **argv)
                     clean_all = true;
                     break;
                   case OPT_EXCLUDE_RANDOM: {
-                    char *p;
+                    bool valid = false;
+                    unsigned int i;
                     exclude = xstrdup (optarg);
                     STACK_VAR (exclude);
-                    for (p = exclude; *p; p++)
-                      *p = tolower (*p);
-                    if (streq (exclude, "random"))
-                      vfprintf_fail (formats[FMT_GENERIC], "--exclude-random switch must be provided a color");
+                    for (i = 1; i < tables[FOREGROUND].count - 1; i++) /* skip color none and default */
+                      {
+                        const struct color *entry = &tables[FOREGROUND].entries[i];
+                        if (streq (exclude, entry->name))
+                          {
+                            valid = true;
+                            break;
+                          }
+                      }
+                    if (!valid)
+                      vfprintf_fail (formats[FMT_GENERIC], "--exclude-random switch must be provided a plain color");
                     break;
                   }
                   case OPT_HELP:
diff --git a/test.pl b/test.pl
index 7968097ea66426183c0db47f87a80ffa835c8434..49a0cf38fae6cce73c46f4f00b5a97673137ad01 100755 (executable)
--- a/test.pl
+++ b/test.pl
@@ -61,7 +61,7 @@ SKIP: {
 
         my $file = $write_to_tmpfile->('abc');
 
-        $ok &= $run_program_fail->($program, '--exclude-random=random', 'must be provided a color');
+        $ok &= $run_program_fail->($program, '--exclude-random=random', 'must be provided a plain color');
         $ok &= $run_program_fail->($program, '--clean --clean-all',     'mutually exclusive');
         $ok &= $run_program_fail->($program, '--clean file1 file2',     'more than one file');
         $ok &= $run_program_fail->($program, '--clean-all file1 file2', 'more than one file');