From: Karel Zak Date: Tue, 7 May 2013 07:09:35 +0000 (+0200) Subject: lib/colors: add colormode_or_err() X-Git-Tag: v2.24-rc1~567 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7faf99128c6dd5c01e58de6227fd5ab42f5602c;p=thirdparty%2Futil-linux.git lib/colors: add colormode_or_err() ... to make the code easy to use in utils. Signed-off-by: Karel Zak --- diff --git a/include/colors.h b/include/colors.h index f9e36fd575..b6c99171a8 100644 --- a/include/colors.h +++ b/include/colors.h @@ -48,6 +48,7 @@ enum colortmode { }; extern int colormode_from_string(const char *str); +extern int colormode_or_err(const char *str, const char *errmsg); /* Initialize the global variable OUT_IS_TERM */ extern int colors_init(int mode); diff --git a/lib/colors.c b/lib/colors.c index 57ecae93dd..c8075cc1eb 100644 --- a/lib/colors.c +++ b/lib/colors.c @@ -61,10 +61,21 @@ int colormode_from_string(const char *str) return -EINVAL; } +int colormode_or_err(const char *str, const char *errmsg) +{ + const char *p = str && *str == '=' ? str + 1 : str; + int colormode; + + colormode = colormode_from_string(p); + if (colormode < 0) + errx(EXIT_FAILURE, "%s: '%s'", errmsg, p); + + return colormode; +} + + #ifdef TEST_PROGRAM # include -# include - int main(int argc, char *argv[]) { static const struct option longopts[] = { @@ -77,13 +88,8 @@ int main(int argc, char *argv[]) switch (c) { case 'c': mode = UL_COLORMODE_AUTO; - if (optarg) { - char *p = *optarg == '=' ? optarg + 1 : optarg; - - mode = colormode_from_string(p); - if (mode < 0) - errx(EXIT_FAILURE, "'%s' unsupported color mode", p); - } + if (optarg) + mode = colormode_or_err(optarg, "unsupported color mode"); break; } } diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index 5d0453300b..ae8e93807b 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -1241,12 +1241,9 @@ int main(int argc, char *argv[]) break; case 'L': colormode = UL_COLORMODE_AUTO; - if (optarg) { - char *p = *optarg == '=' ? optarg + 1 : optarg; - colormode = colormode_from_string(p); - if (colormode < 0) - errx(EXIT_FAILURE, _("unsupported color mode: '%s'"), p); - } + if (optarg) + colormode = colormode_or_err(optarg, + _("unsupported color mode")); break; case 'l': ctl.fltr_lev= 1;