Debian Codegraph @

lib/color.c

raw
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sys/socket.h>
8 #include <sys/types.h>
9 #include <linux/if.h>
10
11 #include "color.h"
12 #include "utils.h"
13
14 static void set_color_palette(void);
15
16 enum color {
17 C_RED,
18 C_GREEN,
19 C_YELLOW,
20 C_BLUE,
21 C_MAGENTA,
22 C_CYAN,
23 C_WHITE,
24 C_BOLD_RED,
25 C_BOLD_GREEN,
26 C_BOLD_YELLOW,
27 C_BOLD_LIGHT_BLUE,
28 C_BOLD_MAGENTA,
29 C_BOLD_CYAN,
30 C_BOLD_WHITE,
31 C_CLEAR
32 };
33
34 static const char * const color_codes[] = {
35 "\e[31m",
36 "\e[32m",
37 "\e[33m",
38 "\e[34m",
39 "\e[35m",
40 "\e[36m",
41 "\e[37m",
42 "\e[1;31m",
43 "\e[1;32m",
44 "\e[1;33m",
45 "\e[1;94m",
46 "\e[1;35m",
47 "\e[1;36m",
48 "\e[1;37m",
49 "\e[0m",
50 NULL,
51 };
52
53 /* light background */
54 static enum color attr_colors_light[] = {
55 C_CYAN,
56 C_YELLOW,
57 C_MAGENTA,
58 C_BLUE,
59 C_GREEN,
60 C_RED,
61 C_CLEAR,
62 };
63
64 /* dark background */
65 static enum color attr_colors_dark[] = {
66 C_BOLD_CYAN,
67 C_BOLD_YELLOW,
68 C_BOLD_MAGENTA,
69 C_BOLD_LIGHT_BLUE,
70 C_BOLD_GREEN,
71 C_BOLD_RED,
72 C_CLEAR
73 };
74
75 /*
76 * Assume dark background until we know otherwise. The dark-background
77 * colours work better on a light background than vice versa.
78 */
79 static int is_dark_bg = 1;
80 static int color_is_enabled;
81
82 static void enable_color(void)
83 {
84 color_is_enabled = 1;
85 set_color_palette();
86 }
87
88 int default_color_opt(void)
89 {
90 const char *no_color;
91
92 /* If NO_COLOR has a non-empty value, coloured output is never wanted */
93 no_color = getenv("NO_COLOR");
94 if (no_color && *no_color)
95 return COLOR_OPT_NEVER;
96
97 return CONF_COLOR;
98 }
99
100 bool check_enable_color(int color, int json)
101 {
102 if (json || color == COLOR_OPT_NEVER)
103 return false;
104
105 if (color == COLOR_OPT_ALWAYS || isatty(fileno(stdout))) {
106 enable_color();
107 return true;
108 }
109 return false;
110 }
111
112 bool matches_color(const char *arg, int *val)
113 {
114 char *dup, *p;
115
116 if (!val)
117 return false;
118
119 dup = strdupa(arg);
120 p = strchrnul(dup, '=');
121 if (*p)
122 *(p++) = '\0';
123
124 if (matches(dup, "-color"))
125 return false;
126
127 if (*p == '\0' || !strcmp(p, "always"))
128 *val = COLOR_OPT_ALWAYS;
129 else if (!strcmp(p, "auto"))
130 *val = COLOR_OPT_AUTO;
131 else if (!strcmp(p, "never"))
132 *val = COLOR_OPT_NEVER;
133 else
134 return false;
135 return true;
136 }
137
138 static void set_color_palette(void)
139 {
140 char *p = getenv("COLORFGBG");
141
142 /*
143 * COLORFGBG environment variable usually contains either two or three
144 * values separated by semicolons; we want the last value in either case.
145 * If this value is 0-6 or 8, background is dark; otherwise it's light.
146 */
147 if (p && (p = strrchr(p, ';')) != NULL
148 && !(((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
149 && p[2] == '\0'))
150 is_dark_bg = 0;
151 }
152
153 __attribute__((format(printf, 3, 4)))
154 int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
155 {
156 int ret = 0;
157 va_list args;
158
159 if (fmt == NULL)
160 return 0;
161
162 va_start(args, fmt);
163
164 if (!color_is_enabled || attr == COLOR_NONE) {
165 ret = vfprintf(fp, fmt, args);
166 goto end;
167 }
168
169 ret += fprintf(fp, "%s", color_codes[is_dark_bg ?
170 attr_colors_dark[attr] : attr_colors_light[attr]]);
171
172 ret += vfprintf(fp, fmt, args);
173 ret += fprintf(fp, "%s", color_codes[C_CLEAR]);
174
175 end:
176 va_end(args);
177 return ret;
178 }
179
180 enum color_attr ifa_family_color(__u8 ifa_family)
181 {
182 switch (ifa_family) {
183 case AF_INET:
184 return COLOR_INET;
185 case AF_INET6:
186 return COLOR_INET6;
187 default:
188 return COLOR_NONE;
189 }
190 }
191
192 enum color_attr oper_state_color(__u8 state)
193 {
194 switch (state) {
195 case IF_OPER_UP:
196 return COLOR_OPERSTATE_UP;
197 case IF_OPER_DOWN:
198 return COLOR_OPERSTATE_DOWN;
199 default:
200 return COLOR_NONE;
201 }
202 }