]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - lib/color.c
ll_map: Add function to remove link cache entry by index
[mirror_iproute2.git] / lib / color.c
index da1f516cb2492f1c4515274c741218951b969a63..59976847295c5927816d525fefe6efdfbd4bc2de 100644 (file)
@@ -3,11 +3,15 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <linux/if.h>
 
 #include "color.h"
+#include "utils.h"
+
+static void set_color_palette(void);
 
 enum color {
        C_RED,
@@ -71,13 +75,51 @@ static enum color attr_colors_dark[] = {
 static int is_dark_bg;
 static int color_is_enabled;
 
-void enable_color(void)
+static void enable_color(void)
 {
        color_is_enabled = 1;
        set_color_palette();
 }
 
-void set_color_palette(void)
+bool check_enable_color(int color, int json)
+{
+       if (json || color == COLOR_OPT_NEVER)
+               return false;
+
+       if (color == COLOR_OPT_ALWAYS || isatty(fileno(stdout))) {
+               enable_color();
+               return true;
+       }
+       return false;
+}
+
+bool matches_color(const char *arg, int *val)
+{
+       char *dup, *p;
+
+       if (!val)
+               return false;
+
+       dup = strdupa(arg);
+       p = strchrnul(dup, '=');
+       if (*p)
+               *(p++) = '\0';
+
+       if (matches(dup, "-color"))
+               return false;
+
+       if (*p == '\0' || !strcmp(p, "always"))
+               *val = COLOR_OPT_ALWAYS;
+       else if (!strcmp(p, "auto"))
+               *val = COLOR_OPT_AUTO;
+       else if (!strcmp(p, "never"))
+               *val = COLOR_OPT_NEVER;
+       else
+               return false;
+       return true;
+}
+
+static void set_color_palette(void)
 {
        char *p = getenv("COLORFGBG");
 
@@ -92,6 +134,7 @@ void set_color_palette(void)
                is_dark_bg = 1;
 }
 
+__attribute__((format(printf, 3, 4)))
 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
 {
        int ret = 0;