]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
colorize 0.51
[colorize.git] / colorize.c
index 542e400053a1b19c098d3bd62089a77c3ffb4c6a..827de87850bbae88b82202a9eb6bd08ce0302677 100644 (file)
@@ -94,7 +94,7 @@
 
 #define COLOR_SEP_CHAR '/'
 
-#define VERSION "0.50"
+#define VERSION "0.51"
 
 typedef unsigned short bool;
 
@@ -183,7 +183,7 @@ static void process_options (unsigned int, char **, bool *, const struct color *
 static void process_file_option (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 find_color_entry (const struct color_name *, unsigned int, const struct color **);
 static void print_line (const struct color **, bool, const char * const, unsigned int);
 static void print_clean (const char *);
 static void print_free_offsets (const char *, char ***, unsigned int);
@@ -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 void vfprintf_diag (const char *, ...);
 static void vfprintf_fail (const char *, ...);
 static void stack_var (void ***, unsigned int *, unsigned int, void *);
 static void release_var (void **, unsigned int, void **);
@@ -311,6 +312,7 @@ 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);
           }
@@ -547,7 +549,12 @@ 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);
 }
@@ -644,7 +651,7 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL
         if (feof (stream)) {
           MERGE_PRINT_LINE (part_line, line, 0, true);
         }
-        else
+        else if (*line != '\0')
           {
             if (!clean && !clean_all) /* efficiency */
               print_line (colors, bold, line, 0);
@@ -704,12 +711,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;
@@ -718,14 +725,14 @@ 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
@@ -971,15 +978,24 @@ str_concat (const char *str1, const char *str2)
     return str;
 }
 
+#define DO_VFPRINTF(fmt)                    \
+    va_list ap;                             \
+    fprintf (stderr, "%s: ", program_name); \
+    va_start (ap, fmt);                     \
+    vfprintf (stderr, fmt, ap);             \
+    va_end (ap);                            \
+    fprintf (stderr, "\n");                 \
+
+static void
+vfprintf_diag (const char *fmt, ...)
+{
+    DO_VFPRINTF (fmt);
+}
+
 static void
 vfprintf_fail (const char *fmt, ...)
 {
-    va_list ap;
-    fprintf (stderr, "%s: ", program_name);
-    va_start (ap, fmt);
-    vfprintf (stderr, fmt, ap);
-    va_end (ap);
-    fprintf (stderr, "\n");
+    DO_VFPRINTF (fmt);
     exit (EXIT_FAILURE);
 }