diff options
| author | 2013-10-29 20:12:42 +0100 | |
|---|---|---|
| committer | 2013-10-29 20:12:42 +0100 | |
| commit | 7f4773bba9036b8d1e01699d21fecfbaa042eb9e (patch) | |
| tree | b615d5c8bb86ab925660f54713ad1fcb6b7806c7 | |
| parent | 144136dbe30565ca78071a59e9262e9b97910867 (diff) | |
| download | colorize-7f4773bba9036b8d1e01699d21fecfbaa042eb9e.tar.gz colorize-7f4773bba9036b8d1e01699d21fecfbaa042eb9e.tar.bz2 | |
Don't fail if color string exists as file
| -rw-r--r-- | colorize.c | 28 | 
1 files changed, 25 insertions, 3 deletions
| @@ -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); \ | 
