]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
Merge common code for conditionally colored output
authorPhil Sutter <phil@nwl.cc>
Wed, 15 Aug 2018 16:21:26 +0000 (18:21 +0200)
committerDavid Ahern <dsahern@gmail.com>
Wed, 15 Aug 2018 16:55:27 +0000 (09:55 -0700)
Instead of calling enable_color() conditionally with identical check in
three places, introduce check_enable_color() which does it in one place.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: David Ahern <dsahern@gmail.com>
bridge/bridge.c
include/color.h
ip/ip.c
lib/color.c
tc/tc.c

index 289a157d37f0385102125f3a980130f15b68114f..451d684e0bcfd291aaf6780d64ba86600c17fb87 100644 (file)
@@ -200,8 +200,7 @@ main(int argc, char **argv)
 
        _SL_ = oneline ? "\\" : "\n";
 
-       if (color && !json)
-               enable_color();
+       check_enable_color(color, json);
 
        if (batch_file)
                return batch(batch_file);
index c80359d3e2e95d6fd5ebe9cd4674e5e3847d04f6..4f2c918db7e4351698e14d664e5d5fef035c6b94 100644 (file)
@@ -13,6 +13,7 @@ enum color_attr {
 };
 
 void enable_color(void);
+int check_enable_color(int color, int json);
 void set_color_palette(void);
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...);
 enum color_attr ifa_family_color(__u8 ifa_family);
diff --git a/ip/ip.c b/ip/ip.c
index 71d5170c0cc23ee901fb104aaae5c35e640effad..38eac5ec1e17db458edd607a883aa907fe2a09c5 100644 (file)
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -304,8 +304,7 @@ int main(int argc, char **argv)
 
        _SL_ = oneline ? "\\" : "\n";
 
-       if (color && !json)
-               enable_color();
+       check_enable_color(color, json);
 
        if (batch_file)
                return batch(batch_file);
index da1f516cb2492f1c4515274c741218951b969a63..edf96e5c6ecd7e6dca27b922a8e39a63eb6beaf3 100644 (file)
@@ -77,6 +77,15 @@ void enable_color(void)
        set_color_palette();
 }
 
+int check_enable_color(int color, int json)
+{
+       if (color && !json) {
+               enable_color();
+               return 0;
+       }
+       return 1;
+}
+
 void set_color_palette(void)
 {
        char *p = getenv("COLORFGBG");
diff --git a/tc/tc.c b/tc/tc.c
index 8d94ce3f704b460297bbdfcb79de01e648384fde..4c7a128c8103e289bd0c3bb189ab9bc707c8f7e6 100644 (file)
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -518,8 +518,7 @@ int main(int argc, char **argv)
 
        _SL_ = oneline ? "\\" : "\n";
 
-       if (color && !json)
-               enable_color();
+       check_enable_color(color, json);
 
        if (batch_file)
                return batch(batch_file);