]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
colorize 0.52
[colorize.git] / colorize.c
index 1efc7d917794303de0132dd26b131bd0a30c4c06..319c799020a32b1e428d16e8024224d31caa2ce0 100644 (file)
@@ -94,7 +94,7 @@
 
 #define COLOR_SEP_CHAR '/'
 
-#define VERSION "0.51"
+#define VERSION "0.52"
 
 typedef unsigned short bool;
 
@@ -196,6 +196,7 @@ static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
 static void free_wrap (void **);
 static char *strdup_wrap (const char *);
 static char *str_concat (const char *, const char *);
+static bool has_color_name (const char *, const char *);
 static void vfprintf_diag (const char *, ...);
 static void vfprintf_fail (const char *, ...);
 static void stack_var (void ***, unsigned int *, unsigned int, void *);
@@ -229,7 +230,7 @@ main (int argc, char **argv)
         { "exclude-random", required_argument, &opt_type, OPT_EXCLUDE_RANDOM },
         { "help",           no_argument,       &opt_type, OPT_HELP           },
         { "version",        no_argument,       &opt_type, OPT_VERSION        },
-        {  0,               0,                 0,         0                  },
+        {  NULL,            0,                 NULL,      0                  },
     };
 
     bool bold = false;
@@ -443,14 +444,18 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct
             for (i = 0; i < tables[FOREGROUND].count; i++)
               {
                 const struct color *entry = &tables[FOREGROUND].entries[i];
-                char *p;
-                if ((p = strstr (color, entry->name)) && p == color)
+                if (has_color_name (color, entry->name))
                   {
-                    color = p + strlen (entry->name);
+                    color += strlen (entry->name);
                     matched = true;
                     break;
                   }
               }
+            if (!matched && has_color_name (color, "random"))
+              {
+                color += strlen ("random");
+                matched = true;
+              }
             if (matched && *color == COLOR_SEP_CHAR && *(color + 1))
               color++;
             else
@@ -484,6 +489,8 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct
     for (index = 0, color = str; *color; index++, color = p)
       {
         char *ch, *sep;
+
+        p = NULL;
         if ((sep = strchr (color, COLOR_SEP_CHAR)))
           {
             *sep = '\0';
@@ -491,6 +498,7 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct
           }
         else
           p = color + strlen (color);
+        assert (p);
 
         for (ch = color; *ch; ch++)
           if (!isalpha (*ch))
@@ -979,6 +987,23 @@ str_concat (const char *str1, const char *str2)
     return str;
 }
 
+static bool
+has_color_name (const char *str, const char *name)
+{
+    char *p;
+
+    assert (strlen (str));
+    assert (strlen (name));
+
+    if (!(*str == *name || *str == toupper (*name)))
+      return false;
+    else if (*(name + 1) != '\0'
+     && !((p = strstr (str + 1, name + 1)) && p == str + 1))
+      return false;
+
+    return true;
+}
+
 #define DO_VFPRINTF(fmt)                    \
     va_list ap;                             \
     fprintf (stderr, "%s: ", program_name); \