]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
*alloc_wrap_debug(): improve aligning of debug output
[colorize.git] / colorize.c
index 63237a5240d4cf7a4e44942b9503a7db9357d5c9..f91af67ce30ad1cce06f2553c0d0588969f75274 100644 (file)
@@ -2,7 +2,7 @@
  * colorize - Read text from standard input stream or file and print
  *            it colorized through use of ANSI escape sequences
  *
- * Copyright (c) 2011-2017 Steven Schubiger
+ * Copyright (c) 2011-2018 Steven Schubiger
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -73,6 +73,8 @@
 #define LF 0x01
 #define CR 0x02
 
+#define COUNT_OF(obj, type) (sizeof (obj) / sizeof (type))
+
 #define SKIP_LINE_ENDINGS(flags) ((flags) == (CR|LF) ? 2 : 1)
 
 #define VALID_FILE_TYPE(mode) (S_ISREG (mode) || S_ISLNK (mode) || S_ISFIFO (mode))
 
 #define PROGRAM_NAME "colorize"
 
-#define VERSION "0.63"
+#define VERSION "0.64"
 
 typedef enum { false, true } bool;
 
@@ -196,8 +198,8 @@ static const struct {
     unsigned int count;
     const char *desc;
 } tables[] = {
-    { fg_colors, sizeof (fg_colors) / sizeof (struct color), "foreground" },
-    { bg_colors, sizeof (bg_colors) / sizeof (struct color), "background" },
+    { fg_colors, COUNT_OF (fg_colors, struct color), "foreground" },
+    { bg_colors, COUNT_OF (bg_colors, struct color), "background" },
 };
 
 enum {
@@ -339,8 +341,8 @@ main (int argc, char **argv)
           vfprintf_fail (formats[FMT_GENERIC], "--clean and --clean-all switch are mutually exclusive");
         if (arg_cnt > 1)
           {
-            const char *format = "%s %s";
-            const char *message = "switch cannot be used with more than one file";
+            const char *const format = "%s %s";
+            const char *const message = "switch cannot be used with more than one file";
             if (clean)
               vfprintf_fail (format, "--clean", message);
             else if (clean_all)
@@ -467,7 +469,7 @@ process_opt_attr (const char *p)
           {
             bool valid_attr = false;
             unsigned int i;
-            for (i = 0; i < sizeof (attrs) / sizeof (struct attr); i++)
+            for (i = 0; i < COUNT_OF (attrs, struct attr); i++)
               {
                 const size_t name_len = strlen (attrs[i].name);
                 if ((size_t)(p - s) == name_len && strneq (s, attrs[i].name, name_len))
@@ -551,7 +553,7 @@ print_help (void)
       {
         const struct opt_data *opt_data = NULL;
         unsigned int i;
-        for (i = 0; i < sizeof (opts_data) / sizeof (struct opt_data); i++)
+        for (i = 0; i < COUNT_OF (opts_data, struct opt_data); i++)
           if (streq (opt->name, opts_data[i].name))
             {
               opt_data = &opts_data[i];
@@ -576,7 +578,7 @@ print_version (void)
 #ifdef HAVE_VERSION
 # include "version.h"
 #else
-    const char *version = NULL;
+    const char *const version = NULL;
 #endif
     const char *version_prefix, *version_string;
     const char *c_flags, *ld_flags, *cpp_flags;
@@ -661,7 +663,11 @@ process_args (unsigned int arg_cnt, char **arg_strings, char *attr, const struct
     int ret;
     char *p;
     struct stat sb;
-    struct color_name *color_names[3] = { NULL, NULL, NULL };
+    struct color_name *color_names[3] = {
+        NULL, /* foreground */
+        NULL, /* background */
+        NULL, /* sentinel value */
+    };
 
     const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL;
     const char *file_string  = arg_cnt == 2 ? arg_strings[1] : NULL;
@@ -1346,13 +1352,14 @@ realloc_wrap (void *ptr, size_t size)
     return p;
 }
 #else
+static const char *const format_debug = "%s: %10s %7lu bytes [source file %s, line %5u]\n";
 static void *
 malloc_wrap_debug (size_t size, const char *file, unsigned int line)
 {
     void *p = malloc (size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
-    fprintf (log, "%s: malloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
+    fprintf (log, format_debug, program_name, "malloc'ed", (unsigned long)size, file, line);
     return p;
 }
 
@@ -1362,7 +1369,7 @@ calloc_wrap_debug (size_t nmemb, size_t size, const char *file, unsigned int lin
     void *p = calloc (nmemb, size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
-    fprintf (log, "%s: calloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)(nmemb * size), file, line);
+    fprintf (log, format_debug, program_name, "calloc'ed", (unsigned long)(nmemb * size), file, line);
     return p;
 }
 
@@ -1372,7 +1379,7 @@ realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
     void *p = realloc (ptr, size);
     if (!p)
       MEM_ALLOC_FAIL_DEBUG (file, line);
-    fprintf (log, "%s: realloc'ed %lu bytes [source file %s, line %u]\n", program_name, (unsigned long)size, file, line);
+    fprintf (log, format_debug, program_name, "realloc'ed", (unsigned long)size, file, line);
     return p;
 }
 #endif /* !DEBUG */