]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
Exchange color codes for cyan/magenta
[colorize.git] / colorize.c
index 4303eb7f2b220ef3855987a3a9a3d908ab347f0c..ab4e786232ec84a0c12ac0d625fe358565a47512 100644 (file)
 #define free_null(ptr) free_wrap((void **)&ptr)
 #define xstrdup(str)   strdup_wrap(str)
 
-#if !defined BUF_SIZE || BUF_SIZE <= 0
+#if BUF_SIZE <= 0
 # undef BUF_SIZE
-# define BUF_SIZE 4096 + 1
+#endif
+#ifndef BUF_SIZE
+# define BUF_SIZE 4096
 #endif
 
 #define LF 0x01
 
 #define STACK_VAR(ptr) do {                                   \
     stack_var (&vars_list, &stacked_vars, stacked_vars, ptr); \
-} while (false);
+} while (false)
 
 #define RELEASE_VAR(ptr) do {                             \
     release_var (vars_list, stacked_vars, (void **)&ptr); \
-} while (false);
+} 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);
+} while (false)
 #define MEM_ALLOC_FAIL() do {                                          \
     fprintf (stderr, "%s: memory allocation failure\n", program_name); \
     exit (2);                                                          \
-} while (false);
+} while (false)
 
 #define ABORT_TRACE()                                                              \
     fprintf (stderr, "Aborting in source file %s, line %u\n", __FILE__, __LINE__); \
@@ -119,8 +121,8 @@ static const struct color fg_colors[] = {
     { "green",   "32m" },
     { "yellow",  "33m" },
     { "blue",    "34m" },
-    { "cyan",    "35m" },
-    { "magenta", "36m" },
+    { "magenta", "35m" },
+    { "cyan",    "36m" },
     { "white",   "37m" },
     { "default", "39m" },
 };
@@ -131,8 +133,8 @@ static const struct color bg_colors[] = {
     { "green",   "42m" },
     { "yellow",  "43m" },
     { "blue",    "44m" },
-    { "cyan",    "45m" },
-    { "magenta", "46m" },
+    { "magenta", "45m" },
+    { "cyan",    "46m" },
     { "white",   "47m" },
     { "default", "49m" },
 };
@@ -373,14 +375,21 @@ static void
 print_version (void)
 {
     const char *c_flags;
-    printf ("%s v%s (compiled at %s, %s)\n", "colorize", VERSION, __DATE__, __TIME__);
+    bool debug;
 #ifdef CFLAGS
     c_flags = to_str (CFLAGS);
 #else
     c_flags = "unknown";
 #endif
+#if DEBUG
+    debug = true;
+#else
+    debug = false;
+#endif
+    printf ("%s v%s (compiled at %s, %s)\n", "colorize", VERSION, __DATE__, __TIME__);
     printf ("Compiler flags: %s\n", c_flags);
-    printf ("Buffer size: %u bytes\n", BUF_SIZE - 1);
+    printf ("Buffer size: %u bytes\n", BUF_SIZE);
+    printf ("Debugging: %s\n", debug ? "yes" : "no");
 }
 
 static void
@@ -628,12 +637,12 @@ process_file_arg (const char *file_string, const char **file, FILE **stream)
     if (!check_eof || *current_line != '\0')                     \
       print_line (bold, colors, current_line, flags);            \
     free (merged_line);                                          \
-} while (false);
+} while (false)
 
 static void
 read_print_stream (bool bold, const struct color **colors, const char *file, FILE *stream)
 {
-    char buf[BUF_SIZE], *part_line = NULL;
+    char buf[BUF_SIZE + 1], *part_line = NULL;
     unsigned int flags = 0;
 
     while (!feof (stream))
@@ -641,10 +650,10 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL
         size_t bytes_read;
         char *eol;
         const char *line;
-        memset (buf, '\0', BUF_SIZE);
-        bytes_read = fread (buf, 1, BUF_SIZE - 1, stream);
-        if (bytes_read != (BUF_SIZE - 1) && ferror (stream))
-          vfprintf_fail (formats[FMT_ERROR], BUF_SIZE - 1, "read");
+        memset (buf, '\0', BUF_SIZE + 1);
+        bytes_read = fread (buf, 1, BUF_SIZE, stream);
+        if (bytes_read != BUF_SIZE && ferror (stream))
+          vfprintf_fail (formats[FMT_ERROR], BUF_SIZE, "read");
         line = buf;
         while ((eol = strpbrk (line, "\n\r")))
           {