From 2d50fa4a1e8a43a9d7a67bf347a899e48f1bf763 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Tue, 27 Oct 2015 16:17:07 +0100 Subject: Move color names code to a function --- colorize.c | 119 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/colorize.c b/colorize.c index 07a409e..2dff23e 100644 --- a/colorize.c +++ b/colorize.c @@ -211,6 +211,7 @@ static void cleanup (void); static void free_color_names (struct color_name **); static void process_args (unsigned int, char **, bool *, const struct color **, const char **, FILE **); static void process_file_arg (const char *, const char **, FILE **); +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 complete_part_line (const char *, char **, FILE *); @@ -514,8 +515,7 @@ static void process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct color **colors, const char **file, FILE **stream) { int ret; - unsigned int index; - char *color, *p, *str; + char *p; struct stat sb; const char *color_string = arg_cnt >= 1 ? arg_strings[0] : NULL; @@ -594,59 +594,7 @@ process_args (unsigned int arg_cnt, char **arg_strings, bool *bold, const struct vfprintf_fail (formats[FMT_STRING], "one color pair allowed only for string", color_string); } - str = xstrdup (color_string); - STACK_VAR (str); - - for (index = 0, color = str; *color; index++, color = p) - { - char *ch, *sep; - - p = NULL; - if ((sep = strchr (color, COLOR_SEP_CHAR))) - { - *sep = '\0'; - p = sep + 1; - } - else - p = color + strlen (color); - assert (p); - - for (ch = color; *ch; ch++) - if (!isalpha (*ch)) - vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be made of non-alphabetic characters"); - - for (ch = color + 1; *ch; ch++) - if (!islower (*ch)) - vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be in mixed lower/upper case"); - - if (streq (color, "None")) - vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be bold"); - - if (isupper (*color)) - { - switch (index) - { - case FOREGROUND: - *bold = true; - break; - case BACKGROUND: - vfprintf_fail (formats[FMT_COLOR], tables[BACKGROUND].desc, color, "cannot be bold"); - default: /* never reached */ - ABORT_TRACE (); - } - } - - color_names[index] = xcalloc (1, sizeof (struct color_name)); - - color_names[index]->orig = xstrdup (color); - - for (ch = color; *ch; ch++) - *ch = tolower (*ch); - - color_names[index]->name = xstrdup (color); - } - - RELEASE_VAR (str); + gather_color_names (color_string, bold, color_names); assert (color_names[FOREGROUND]); @@ -713,6 +661,67 @@ process_file_arg (const char *file_string, const char **file, FILE **stream) assert (*file); } +static void +gather_color_names (const char *color_string, bool *bold, struct color_name **color_names) +{ + unsigned int index; + char *color, *p, *str; + + str = xstrdup (color_string); + STACK_VAR (str); + + for (index = 0, color = str; *color; index++, color = p) + { + char *ch, *sep; + + p = NULL; + if ((sep = strchr (color, COLOR_SEP_CHAR))) + { + *sep = '\0'; + p = sep + 1; + } + else + p = color + strlen (color); + assert (p); + + for (ch = color; *ch; ch++) + if (!isalpha (*ch)) + vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be made of non-alphabetic characters"); + + for (ch = color + 1; *ch; ch++) + if (!islower (*ch)) + vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be in mixed lower/upper case"); + + if (streq (color, "None")) + vfprintf_fail (formats[FMT_COLOR], tables[index].desc, color, "cannot be bold"); + + if (isupper (*color)) + { + switch (index) + { + case FOREGROUND: + *bold = true; + break; + case BACKGROUND: + vfprintf_fail (formats[FMT_COLOR], tables[BACKGROUND].desc, color, "cannot be bold"); + default: /* never reached */ + ABORT_TRACE (); + } + } + + color_names[index] = xcalloc (1, sizeof (struct color_name)); + + color_names[index]->orig = xstrdup (color); + + for (ch = color; *ch; ch++) + *ch = tolower (*ch); + + color_names[index]->name = xstrdup (color); + } + + RELEASE_VAR (str); +} + static void read_print_stream (bool bold, const struct color **colors, const char *file, FILE *stream) { -- cgit v1.2.3