]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
color: Simplify color error handling.
authorAndre Noll <maan@systemlinux.org>
Sun, 21 Nov 2010 20:48:42 +0000 (21:48 +0100)
committerAndre Noll <maan@systemlinux.org>
Sun, 5 Dec 2010 17:52:40 +0000 (18:52 +0100)
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
color.c
color.h
daemon.c
daemon.h
server.c

index 89b160db527ae9397a2bd2b216b304d6c4c8920f..039a688639a770ee40af0fd63c065d50cb324482 100644 (file)
--- 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 ca5a32d3bc6ccec7d4e93c2f8f6b285a53b22dcb..0e2beeb0cac4f703668a666db4b57ab954c27c0e 100644 (file)
--- 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 12972b3a2f1b7181305a6f34949a4bcfa08ad1b6..167c929470827cf5e3e8556b3e052dc6dbf82957 100644 (file)
--- 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);
index eef504f1b972f20b9a8b5aa22d0efc0ae80f1396..08231cb6212d1aa543b39216743db1c287f63dd7 100644 (file)
--- 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);
 }
 
 /**
index 3705b9da27cc9db1892fb06c5b64cdcd273702da..4e803bdc4aa18df8bf124e53d1c024ed65cf2fef 100644 (file)
--- 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 {
index 48ef55e84e7afdbbf13b4f0cd0383c1c837d60fc..473233599b9844310f1b77e8a0c196c8afe007f9 100644 (file)
--- 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]);
 }
 
 /*