From ccf5026c5cbe90cf3b3a07531b0946aa19cdd611 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 21 Nov 2010 21:48:42 +0100 Subject: [PATCH] color: Simplify color error handling. We exit on errors anyway, so get rid of the return value of color_parse() and daemon_set_log_color() and abort immediately rather than returning -1. Add the familiar "_or_die" suffix to these functions to make it clear that no error handling is necessary in the caller. --- audiod.c | 9 +++------ color.c | 12 +++++------- color.h | 2 +- daemon.c | 19 +++++++------------ daemon.h | 2 +- server.c | 9 +++------ 6 files changed, 20 insertions(+), 33 deletions(-) diff --git a/audiod.c b/audiod.c index 89b160db..039a6886 100644 --- a/audiod.c +++ b/audiod.c @@ -1298,17 +1298,14 @@ __noreturn static void print_help_and_die(void) static void init_colors_or_die(void) { - int ret, i; + int i; if (!want_colors()) return; daemon_set_default_log_colors(); daemon_set_flag(DF_COLOR_LOG); - for (i = 0; i < conf.log_color_given; i++) { - ret = daemon_set_log_color(conf.log_color_arg[i]); - if (ret < 0) - exit(EXIT_FAILURE); - } + for (i = 0; i < conf.log_color_given; i++) + daemon_set_log_color_or_die(conf.log_color_arg[i]); } /** diff --git a/color.c b/color.c index ca5a32d3..0e2beeb0 100644 --- a/color.c +++ b/color.c @@ -47,11 +47,9 @@ static int parse_attr(const char *name, int len) * \param value Human-readable color spec. * \param dst Result pointer for the escape sequence. * - * \return -1 on errors, 1 on success. - * * Format of \a value: [fg [bg]] [attr]. */ -int color_parse(const char *value, char *dst) +void color_parse_or_die(const char *value, char *dst) { const char *ptr = value; int attr = -1; @@ -60,7 +58,7 @@ int color_parse(const char *value, char *dst) if (!strcasecmp(value, "reset")) { strcpy(dst, COLOR_RESET); - return 1; + return; } /* [fg [bg]] [attr] */ @@ -125,8 +123,8 @@ int color_parse(const char *value, char *dst) *dst++ = 'm'; } *dst = 0; - return 1; + return; bad: - PARA_ERROR_LOG("bad color value '%s'\n", value); - return -1; + PARA_EMERG_LOG("bad color value '%s'\n", value); + exit(EXIT_FAILURE); } diff --git a/color.h b/color.h index 12972b3a..167c9294 100644 --- a/color.h +++ b/color.h @@ -7,4 +7,4 @@ /** Switch back to normal colors. */ #define COLOR_RESET "\033[m" -int color_parse(const char *value, char *dst); +void color_parse_or_die(const char *value, char *dst); diff --git a/daemon.c b/daemon.c index eef504f1..08231cb6 100644 --- a/daemon.c +++ b/daemon.c @@ -56,10 +56,8 @@ void daemon_set_default_log_colors(void) [LL_CRIT] = "magenta bold", [LL_EMERG] = "red bold", }; - for (i = 0; i < NUM_LOGLEVELS; i++) { - int ret = color_parse(default_log_colors[i], me->log_colors[i]); - assert(ret >= 0); - } + for (i = 0; i < NUM_LOGLEVELS; i++) + color_parse_or_die(default_log_colors[i], me->log_colors[i]); } /** @@ -71,7 +69,7 @@ void daemon_set_default_log_colors(void) * * \return 1 On success, -1 on errors. */ -int daemon_set_log_color(char const *arg) +void daemon_set_log_color_or_die(char const *arg) { char *p = strchr(arg, ':'); int ret, ll; @@ -83,14 +81,11 @@ int daemon_set_log_color(char const *arg) goto err; ll = ret; p++; - ret = color_parse(p, me->log_colors[ll]); - if (ret < 0) - goto err; - return 1; + color_parse_or_die(p, me->log_colors[ll]); + return; err: - PARA_ERROR_LOG("%s: color syntax error\n", arg); - return -1; - + PARA_EMERG_LOG("%s: color syntax error\n", arg); + exit(EXIT_FAILURE); } /** diff --git a/daemon.h b/daemon.h index 3705b9da..4e803bdc 100644 --- a/daemon.h +++ b/daemon.h @@ -15,7 +15,7 @@ void daemon_set_flag(unsigned flag); void daemon_clear_flag(unsigned flag); void daemon_set_loglevel(char *loglevel); void daemon_set_default_log_colors(void); -int daemon_set_log_color(char const *arg); +void daemon_set_log_color_or_die(char const *arg); /** Daemon log configuration flags. */ enum daemon_flags { diff --git a/server.c b/server.c index 48ef55e8..47323359 100644 --- a/server.c +++ b/server.c @@ -146,17 +146,14 @@ static int want_colors(void) static void init_colors_or_die(void) { - int ret, i; + int i; if (!want_colors()) return; daemon_set_flag(DF_COLOR_LOG); daemon_set_default_log_colors(); - for (i = 0; i < conf.log_color_given; i++) { - ret = daemon_set_log_color(conf.log_color_arg[i]); - if (ret < 0) - exit(EXIT_FAILURE); - } + for (i = 0; i < conf.log_color_given; i++) + daemon_set_log_color_or_die(conf.log_color_arg[i]); } /* -- 2.30.2