]> git.refcnt.org Git - colorize.git/blobdiff - colorize.c
colorize 0.58
[colorize.git] / colorize.c
index a6bd56d7e2e740f76ab5abccc15c94c238342d67..eb2be2f5080eb9acfa3a58f52fd977d29efe671f 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-2015 Steven Schubiger
+ * Copyright (c) 2011-2016 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
 
 #define ALLOC_COMPLETE_PART_LINE 8
 
-#define COLOR_SEP_CHAR '/'
+#if defined(COLOR_SEP_CHAR_COLON)
+# define COLOR_SEP_CHAR ':'
+#elif defined(COLOR_SEP_CHAR_SLASH)
+# define COLOR_SEP_CHAR '/'
+#else
+# define COLOR_SEP_CHAR '/'
+#endif
 
 #define DEBUG_FILE "debug.txt"
 
-#define VERSION "0.57"
+#define VERSION "0.58"
 
 typedef enum { false, true } bool;
 
@@ -214,10 +220,10 @@ static void process_file_arg (const char *, const char **, FILE **);
 static void skip_path_colors (const char *, const char *, const struct stat *);
 static void gather_color_names (const char *, bool *, struct color_name **);
 static void read_print_stream (bool, const struct color **, const char *, FILE *);
-static void merge_print_line (bool, const struct color **, const char *, const char *, FILE *);
+static void merge_print_line (const char *, const char *, FILE *);
 static void complete_part_line (const char *, char **, FILE *);
 static bool get_next_char (char *, const char **, FILE *, bool *);
-static void save_char (char, char **, unsigned long *, size_t *);
+static void save_char (char, char **, size_t *, size_t *);
 static void find_color_entries (struct color_name **, const struct color **);
 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);
@@ -476,6 +482,7 @@ print_version (void)
       }
     else
       printf ("Buffer size: %lu byte%s\n", (unsigned long)BUF_SIZE, BUF_SIZE > 1 ? "s" : "");
+    printf ("Color separator: '%c'\n", COLOR_SEP_CHAR);
     printf ("Debugging: %s\n", debug ? "yes" : "no");
 }
 
@@ -771,7 +778,7 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL
           {
             char *p;
             if ((clean || clean_all) && (p = strrchr (line, '\033')))
-              merge_print_line (bold, colors, line, p, stream);
+              merge_print_line (line, p, stream);
             else
               print_line (bold, colors, line, 0);
           }
@@ -779,27 +786,33 @@ read_print_stream (bool bold, const struct color **colors, const char *file, FIL
 }
 
 static void
-merge_print_line (bool bold, const struct color **colors, const char *line, const char *p, FILE *stream)
+merge_print_line (const char *line, const char *p, FILE *stream)
 {
     char *buf = NULL;
-    char *merged_part_line = NULL;
-    const char *part_line;
+    char *merged_esc = NULL;
+    const char *esc = "";
+    const char char_restore = *p;
 
     complete_part_line (p + 1, &buf, stream);
 
     if (buf)
-      part_line = merged_part_line = str_concat (line, buf);
-    else
-      part_line = line;
-    free (buf);
+      {
+        /* form escape sequence */
+        esc = merged_esc = str_concat (p, buf);
+        /* shorten partial line accordingly */
+        *(char *)p = '\0';
+        free (buf);
+      }
 
 #ifdef TEST_MERGE_PART_LINE
-    printf ("%s", part_line);
+    printf ("%s%s", line, esc);
     fflush (stdout);
     _exit (EXIT_SUCCESS);
 #else
-    print_line (bold, colors, part_line, 0);
-    free (merged_part_line);
+    print_clean (line);
+    *(char *)p = char_restore;
+    print_clean (esc);
+    free (merged_esc);
 #endif
 }
 
@@ -808,8 +821,7 @@ complete_part_line (const char *p, char **buf, FILE *stream)
 {
     bool got_next_char = false, read_from_stream;
     char ch;
-    unsigned long i = 0;
-    size_t size;
+    size_t i = 0, size;
 
     if (get_next_char (&ch, &p, stream, &read_from_stream))
       {
@@ -888,7 +900,7 @@ get_next_char (char *ch, const char **p, FILE *stream, bool *read_from_stream)
 }
 
 static void
-save_char (char ch, char **buf, unsigned long *i, size_t *size)
+save_char (char ch, char **buf, size_t *i, size_t *size)
 {
     if (!*buf)
       {