From 7f4773bba9036b8d1e01699d21fecfbaa042eb9e Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Tue, 29 Oct 2013 20:12:42 +0100 Subject: Don't fail if color string exists as file --- colorize.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/colorize.c b/colorize.c index 35d14dc..67a4861 100644 --- a/colorize.c +++ b/colorize.c @@ -196,6 +196,7 @@ static void *realloc_wrap_debug (void *, size_t, const char *, unsigned int); static void free_wrap (void **); static char *strdup_wrap (const char *); static char *str_concat (const char *, const char *); +static bool has_color_name (const char *, const char *); static void vfprintf_diag (const char *, ...); static void vfprintf_fail (const char *, ...); static void stack_var (void ***, unsigned int *, unsigned int, void *); @@ -443,14 +444,18 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct for (i = 0; i < tables[FOREGROUND].count; i++) { const struct color *entry = &tables[FOREGROUND].entries[i]; - char *p; - if ((p = strstr (color, entry->name)) && p == color) + if (has_color_name (color, entry->name)) { - color = p + strlen (entry->name); + color += strlen (entry->name); matched = true; break; } } + if (!matched && has_color_name (color, "random")) + { + color += strlen ("random"); + matched = true; + } if (matched && *color == COLOR_SEP_CHAR && *(color + 1)) color++; else @@ -982,6 +987,23 @@ str_concat (const char *str1, const char *str2) return str; } +static bool +has_color_name (const char *str, const char *name) +{ + char *p; + + assert (strlen (str)); + assert (strlen (name)); + + if (!(*str == *name || *str == toupper (*name))) + return false; + else if (*(name + 1) != '\0' + && !((p = strstr (str + 1, name + 1)) && p == str + 1)) + return false; + + return true; +} + #define DO_VFPRINTF(fmt) \ va_list ap; \ fprintf (stderr, "%s: ", program_name); \ -- cgit v1.2.3