]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
colorize 0.52
[colorize.git] / colorize.c
index 3b80c9e9ab5382a5c7164f8f31216d277f12991c..319c799020a32b1e428d16e8024224d31caa2ce0 100644 (file)
@@ -19,7 +19,7 @@
  *
  */
 
-#define _POSIX_SOURCE
+#define _POSIX_C_SOURCE 200809L
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
@@ -94,7 +94,7 @@
 
 #define COLOR_SEP_CHAR '/'
 
-#define VERSION "0.50"
+#define VERSION "0.52"
 
 typedef unsigned short bool;
 
@@ -179,12 +179,12 @@ static void print_help (void);
 static void print_version (void);
 static void cleanup (void);
 static void free_color_names (struct color_name **);
-static void process_options (unsigned int, char **, bool *, const struct color **, const char **, FILE **);
-static void process_file_option (const char *, const char **, FILE **);
+static void process_args (unsigned int, char **, bool *, const struct color **, const char **, FILE **);
+static void process_file_arg (const char *, const char **, FILE **);
 static void read_print_stream (bool, const struct color **, const char *, FILE *);
 static void find_color_entries (struct color_name **, const struct color **);
-static void find_color_entry (const char *const, unsigned int, const struct color **);
-static void print_line (const struct color **, bool, const char * const, unsigned int);
+static void find_color_entry (const struct color_name *, unsigned int, const struct color **);
+static void print_line (bool, const struct color **, const char * const, unsigned int);
 static void print_clean (const char *);
 static void print_free_offsets (const char *, char ***, unsigned int);
 static void *malloc_wrap (size_t);
@@ -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;
@@ -239,7 +240,7 @@ main (int argc, char **argv)
         NULL, /* background */
     };
 
-    const char *file;
+    const char *file = NULL;
 
     program_name = argv[0];
     atexit (cleanup);
@@ -312,15 +313,16 @@ main (int argc, char **argv)
       {
         if (arg_cnt == 0 || arg_cnt > 2)
           {
+            vfprintf_diag ("%u arguments provided, expected 1-2 arguments or option", arg_cnt);
             print_help ();
             exit (EXIT_FAILURE);
           }
       }
 
     if (clean || clean_all)
-      process_file_option (argv[optind], &file, &stream);
+      process_file_arg (argv[optind], &file, &stream);
     else
-      process_options (arg_cnt, &argv[optind], &bold, colors, &file, &stream);
+      process_args (arg_cnt, &argv[optind], &bold, colors, &file, &stream);
     read_print_stream (bold, colors, file, stream);
 
     RELEASE_VAR (exclude);
@@ -405,15 +407,15 @@ free_color_names (struct color_name **color_names)
 }
 
 static void
-process_options (unsigned int arg_cnt, char **option_strings, bool *bold, const struct color **colors, const char **file, FILE **stream)
+process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct color **colors, const char **file, FILE **stream)
 {
     int ret;
     unsigned int index;
     char *color, *p, *str;
     struct stat sb;
 
-    const char *color_string = arg_cnt >= 1 ? option_strings[0] : NULL;
-    const char *file_string  = arg_cnt == 2 ? option_strings[1] : NULL;
+    const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL;
+    const char *file_string  = arg_cnt == 2 ? arg_strings[1] : NULL;
 
     assert (color_string);
 
@@ -442,14 +444,18 @@ process_options (unsigned int arg_cnt, char **option_strings, bool *bold, const
             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
@@ -483,6 +489,8 @@ process_options (unsigned int arg_cnt, char **option_strings, bool *bold, const
     for (index = 0, color = str; *color; index++, color = p)
       {
         char *ch, *sep;
+
+        p = NULL;
         if ((sep = strchr (color, COLOR_SEP_CHAR)))
           {
             *sep = '\0';
@@ -490,6 +498,7 @@ process_options (unsigned int arg_cnt, char **option_strings, bool *bold, const
           }
         else
           p = color + strlen (color);
+        assert (p);
 
         for (ch = color; *ch; ch++)
           if (!isalpha (*ch))
@@ -548,13 +557,18 @@ process_options (unsigned int arg_cnt, char **option_strings, bool *bold, const
     free_color_names (color_names);
 
     if (!colors[FOREGROUND]->code && colors[BACKGROUND] && colors[BACKGROUND]->code)
-      find_color_entry ("default", FOREGROUND, colors);
+      {
+        struct color_name color_name;
+        color_name.name = color_name.orig = "default";
+
+        find_color_entry (&color_name, FOREGROUND, colors);
+      }
 
-    process_file_option (file_string, file, stream);
+    process_file_arg (file_string, file, stream);
 }
 
 static void
-process_file_option (const char *file_string, const char **file, FILE **stream)
+process_file_arg (const char *file_string, const char **file, FILE **stream)
 {
     if (file_string)
       {
@@ -592,6 +606,7 @@ process_file_option (const char *file_string, const char **file, FILE **stream)
       }
 
     assert (*stream);
+    assert (*file);
 }
 
 #define MERGE_PRINT_LINE(part_line, line, flags, check_eof) do { \
@@ -603,7 +618,7 @@ process_file_option (const char *file_string, const char **file, FILE **stream)
       }                                                          \
     current_line = merged_line ? merged_line : (char *)line;     \
     if (!check_eof || *current_line != '\0')                     \
-      print_line (colors, bold, current_line, flags);            \
+      print_line (bold, colors, current_line, flags);            \
     free (merged_line);                                          \
 } while (false);
 
@@ -648,7 +663,7 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL
         else if (*line != '\0')
           {
             if (!clean && !clean_all) /* efficiency */
-              print_line (colors, bold, line, 0);
+              print_line (bold, colors, line, 0);
             else if (!part_line)
               part_line = xstrdup (line);
             else
@@ -705,12 +720,12 @@ find_color_entries (struct color_name **color_names, const struct color **colors
             colors[index] = (struct color *)&color_entries[i];
           }
         else
-          find_color_entry (color_name, index, colors);
+          find_color_entry (color_names[index], index, colors);
       }
 }
 
 static void
-find_color_entry (const char *const color_name, unsigned int index, const struct color **colors)
+find_color_entry (const struct color_name *color_name, unsigned int index, const struct color **colors)
 {
     bool found = false;
     unsigned int i;
@@ -719,18 +734,18 @@ find_color_entry (const char *const color_name, unsigned int index, const struct
     const struct color *const color_entries = tables[index].entries;
 
     for (i = 0; i < count; i++)
-      if (streq (color_name, color_entries[i].name))
+      if (streq (color_name->name, color_entries[i].name))
         {
           colors[index] = (struct color *)&color_entries[i];
           found = true;
           break;
         }
     if (!found)
-      vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color_name, "not recognized");
+      vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color_name->orig, "not recognized");
 }
 
 static void
-print_line (const struct color **colors, bool bold, const char *const line, unsigned int flags)
+print_line (bool bold, const struct color **colors, const char *const line, unsigned int flags)
 {
     /* --clean[-all] */
     if (clean || clean_all)
@@ -972,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); \