* 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;
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);
}
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");
}
{
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);
}
}
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);
- free (merged_part_line);
- exit (EXIT_SUCCESS);
+ 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
}
{
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))
{
}
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)
{