]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
colorize 0.54
[colorize.git] / colorize.c
index 6c998ab6c5778a3a10eba3d74ddd3a2799fa40c8..26eaa2f5ee9092ac7d11862cf3e9b4ae741af604 100644 (file)
 
 #define streq(s1, s2) (strcmp (s1, s2) == 0)
 
-#if DEBUG
-# define xmalloc(size)        malloc_wrap_debug(size,        __FILE__, __LINE__)
-# define xcalloc(nmemb, size) calloc_wrap_debug(nmemb, size, __FILE__, __LINE__)
-# define xrealloc(ptr, size)  realloc_wrap_debug(ptr, size,  __FILE__, __LINE__)
-#else
+#if !DEBUG
 # define xmalloc(size)        malloc_wrap(size)
 # define xcalloc(nmemb, size) calloc_wrap(nmemb, size)
 # define xrealloc(ptr, size)  realloc_wrap(ptr, size)
+#else
+# define xmalloc(size)        malloc_wrap_debug(size,        __FILE__, __LINE__)
+# define xcalloc(nmemb, size) calloc_wrap_debug(nmemb, size, __FILE__, __LINE__)
+# define xrealloc(ptr, size)  realloc_wrap_debug(ptr, size,  __FILE__, __LINE__)
 #endif
 
 #define free_null(ptr) free_wrap((void **)&ptr)
     release_var (vars_list, stacked_vars, (void **)&ptr); \
 } while (false)
 
-#define MEM_ALLOC_FAIL_DEBUG(file, line) do {                                               \
-    fprintf (stderr, "Memory allocation failure in source file %s, line %u\n", file, line); \
-    exit (2);                                                                               \
-} while (false)
-#define MEM_ALLOC_FAIL() do {                                          \
+#if !DEBUG
+# define MEM_ALLOC_FAIL() do {                                         \
     fprintf (stderr, "%s: memory allocation failure\n", program_name); \
     exit (2);                                                          \
 } while (false)
+#else
+# define MEM_ALLOC_FAIL_DEBUG(file, line) do {                                              \
+    fprintf (stderr, "Memory allocation failure in source file %s, line %u\n", file, line); \
+    exit (2);                                                                               \
+} while (false)
+#endif
 
 #define ABORT_TRACE()                                                              \
     fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \
 
 #define COLOR_SEP_CHAR '/'
 
-#define VERSION "0.53"
+#define VERSION "0.54"
 
 typedef enum { false, true } bool;
 
@@ -142,6 +145,7 @@ static const struct color bg_colors[] = {
 
 enum fmts {
     FMT_GENERIC,
+    FMT_STRING,
     FMT_QUOTE,
     FMT_COLOR,
     FMT_RANDOM,
@@ -151,6 +155,7 @@ enum fmts {
 };
 static const char *formats[] = {
     "%s",                    /* generic */
+    "%s '%s'",               /* string  */
     "%s `%s' %s",            /* quote   */
     "%s color '%s' %s",      /* color   */
     "%s color '%s' %s '%s'", /* random  */
@@ -195,12 +200,15 @@ static void find_color_entry (const struct color_name *, unsigned int, const str
 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);
+#if !DEBUG
 static void *malloc_wrap (size_t);
 static void *calloc_wrap (size_t, size_t);
 static void *realloc_wrap (void *, size_t);
+#else
 static void *malloc_wrap_debug (size_t, const char *, unsigned int);
 static void *calloc_wrap_debug (size_t, size_t, const char *, unsigned int);
 static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int);
+#endif
 static void free_wrap (void **);
 static char *strdup_wrap (const char *);
 static char *str_concat (const char *, const char *);
@@ -499,14 +507,15 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct
 
         if (have_file)
           {
+            const char *file_exists = color_string;
             if (file_string)
-              vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), color_string, "cannot be used as color string");
+              vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "cannot be used as color string");
             else
               {
                 if (VALID_FILE_TYPE (mode))
-                  vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), color_string, "must be preceeded by color string");
+                  vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "must be preceeded by color string");
                 else
-                  vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), color_string, "is not a valid file type");
+                  vfprintf_fail (formats[FMT_QUOTE], get_file_type (mode), file_exists, "is not a valid file type");
               }
           }
       }
@@ -514,11 +523,11 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct
     if ((p = strchr (color_string, COLOR_SEP_CHAR)))
       {
         if (p == color_string)
-          vfprintf_fail (formats[FMT_GENERIC], "foreground color missing");
+          vfprintf_fail (formats[FMT_STRING], "foreground color missing in string", color_string);
         else if (p == color_string + strlen (color_string) - 1)
-          vfprintf_fail (formats[FMT_GENERIC], "background color missing");
+          vfprintf_fail (formats[FMT_STRING], "background color missing in string", color_string);
         else if (strchr (++p, COLOR_SEP_CHAR))
-          vfprintf_fail (formats[FMT_GENERIC], "one color pair allowed only");
+          vfprintf_fail (formats[FMT_STRING], "one color pair allowed only for string", color_string);
       }
 
     str = xstrdup (color_string);
@@ -617,7 +626,7 @@ process_file_arg (const char *file_string, const char **file, FILE **stream)
             FILE *s;
             const char *file = file_string;
             struct stat sb;
-            int errno, ret;
+            int ret;
 
             errno = 0;
             ret = lstat (file, &sb);
@@ -939,6 +948,7 @@ print_free_offsets (const char *line, char ***offsets, unsigned int count)
     free_null (offsets);
 }
 
+#if !DEBUG
 static void *
 malloc_wrap (size_t size)
 {
@@ -965,7 +975,7 @@ realloc_wrap (void *ptr, size_t size)
       MEM_ALLOC_FAIL ();
     return p;
 }
-
+#else
 static void *
 malloc_wrap_debug (size_t size, const char *file, unsigned int line)
 {
@@ -992,6 +1002,7 @@ realloc_wrap_debug (void *ptr, size_t size, const char *file, unsigned int line)
       MEM_ALLOC_FAIL_DEBUG (file, line);
     return p;
 }
+#endif
 
 static void
 free_wrap (void **ptr)