]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/colors: add colormode_or_err()
authorKarel Zak <kzak@redhat.com>
Tue, 7 May 2013 07:09:35 +0000 (09:09 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 7 May 2013 07:09:35 +0000 (09:09 +0200)
... to make the code easy to use in utils.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/colors.h
lib/colors.c
sys-utils/dmesg.c

index f9e36fd5751f1be43685b47619f4f38a7b58288e..b6c99171a8994dca08cbd9ce6798ca2380daa40d 100644 (file)
@@ -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);
index 57ecae93dd34ddbd97da3c43366506bc54c8dacd..c8075cc1eb6dbdce2636efbd1965d12c06287c60 100644 (file)
@@ -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 <getopt.h>
-# include <err.h>
-
 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;
                }
        }
index 5d0453300b5a14a10520fd3e079ce28efd911176..ae8e93807b2c3fa58f35b6885e6e11b51b95f139 100644 (file)
@@ -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;