]> git.refcnt.org Git - colorize.git/commitdiff
Fix using color from config with hyphen
authorSteven Schubiger <stsc@refcnt.org>
Sat, 21 Sep 2019 20:25:53 +0000 (22:25 +0200)
committerSteven Schubiger <stsc@refcnt.org>
Sat, 21 Sep 2019 20:25:53 +0000 (22:25 +0200)
colorize.c
t/conf/color.t

index adb240893190121bbe2ad51152064c86cba82721..ac4ee9a4b3b84c4d6d4ebdd81783898346563354 100644 (file)
@@ -956,7 +956,7 @@ free_conf (struct conf *config)
 static void
 process_args (unsigned int arg_cnt, char **arg_strings, char *attr, const struct color **colors, const char **file, FILE **stream, struct conf *config)
 {
 static void
 process_args (unsigned int arg_cnt, char **arg_strings, char *attr, const struct color **colors, const char **file, FILE **stream, struct conf *config)
 {
-    bool use_conf_color;
+    bool has_hyphen, use_conf_color;
     int ret;
     char *p;
     struct stat sb;
     int ret;
     char *p;
     struct stat sb;
@@ -971,23 +971,27 @@ process_args (unsigned int arg_cnt, char **arg_strings, char *attr, const struct
 
     assert (color_string != NULL);
 
 
     assert (color_string != NULL);
 
-    if (streq (color_string, "-"))
+    has_hyphen = streq (color_string, "-");
+
+    if (has_hyphen)
       {
         if (file_string)
           vfprintf_fail (formats[FMT_GENERIC], "hyphen cannot be used as color string");
       {
         if (file_string)
           vfprintf_fail (formats[FMT_GENERIC], "hyphen cannot be used as color string");
-        else
+        else if (!config->color)
           vfprintf_fail (formats[FMT_GENERIC], "hyphen must be preceded by color string");
       }
 
           vfprintf_fail (formats[FMT_GENERIC], "hyphen must be preceded by color string");
       }
 
-    if ((ret = lstat (color_string, &sb)) == 0) /* exists */
+    if (!has_hyphen && (ret = lstat (color_string, &sb)) == 0) /* exists */
       /* Ensure that we don't fail if there's a file with one or more
          color names in its path.  */
       use_conf_color = skip_path_colors (color_string, file_string, &sb, !!config->color);
       /* Ensure that we don't fail if there's a file with one or more
          color names in its path.  */
       use_conf_color = skip_path_colors (color_string, file_string, &sb, !!config->color);
+    else if (has_hyphen)
+      use_conf_color = true;
+    else
+      use_conf_color = false;
 
     /* Use color from config file.  */
 
     /* Use color from config file.  */
-    if (arg_cnt == 1
-     && (access (color_string, F_OK) != -1)
-     && use_conf_color)
+    if (arg_cnt == 1 && use_conf_color)
       {
         file_string = color_string;
         color_string = config->color;
       {
         file_string = color_string;
         color_string = config->color;
index 3adfd5b60ebacab9c774786d1506337869a402f0..fc3ab71fbed023e6d6c5a2f9817249d7c0d05dc6 100755 (executable)
@@ -8,7 +8,7 @@ use Colorize::Common qw(:defaults $write_to_tmpfile);
 use File::Temp qw(tmpnam);
 use Test::More;
 
 use File::Temp qw(tmpnam);
 use Test::More;
 
-my $tests = 3;
+my $tests = 4;
 
 plan tests => $tests;
 
 
 plan tests => $tests;
 
@@ -24,7 +24,8 @@ SKIP: {
     print {$fh} "color=green\n";
     close($fh);
 
     print {$fh} "color=green\n";
     close($fh);
 
-    is(qx($program $infile), "\e[32mfoo\e[0m", 'color from config');
+    is(qx(printf %s "foo" | $program -), "\e[32mfoo\e[0m", 'color from config (stdin)');
+    is(qx($program $infile), "\e[32mfoo\e[0m", 'color from config (file)');
     is(qx(printf %s "foo" | $program none/none), 'foo', 'read from stdin');
     is(qx($program none/none $infile), 'foo', 'read from file');
 
     is(qx(printf %s "foo" | $program none/none), 'foo', 'read from stdin');
     is(qx($program none/none $infile), 'foo', 'read from file');