]> git.proxmox.com Git - mirror_iproute2.git/blame - lib/color.c
SPDX license identifiers
[mirror_iproute2.git] / lib / color.c
CommitLineData
6054c1eb 1/* SPDX-License-Identifier: GPL-2.0 */
d7bd2db5
MN
2#include <stdio.h>
3#include <stdarg.h>
54eab4c7
PV
4#include <stdlib.h>
5#include <string.h>
a1121aa1
PS
6#include <sys/socket.h>
7#include <sys/types.h>
8#include <linux/if.h>
d7bd2db5
MN
9
10#include "color.h"
11
12enum color {
13 C_RED,
14 C_GREEN,
15 C_YELLOW,
16 C_BLUE,
17 C_MAGENTA,
18 C_CYAN,
19 C_WHITE,
54eab4c7
PV
20 C_BOLD_RED,
21 C_BOLD_GREEN,
22 C_BOLD_YELLOW,
23 C_BOLD_BLUE,
24 C_BOLD_MAGENTA,
25 C_BOLD_CYAN,
26 C_BOLD_WHITE,
d7bd2db5
MN
27 C_CLEAR
28};
29
30static const char * const color_codes[] = {
31 "\e[31m",
32 "\e[32m",
33 "\e[33m",
34 "\e[34m",
35 "\e[35m",
36 "\e[36m",
37 "\e[37m",
54eab4c7
PV
38 "\e[1;31m",
39 "\e[1;32m",
40 "\e[1;33m",
41 "\e[1;34m",
42 "\e[1;35m",
43 "\e[1;36m",
44 "\e[1;37m",
d7bd2db5
MN
45 "\e[0m",
46 NULL,
47};
48
99b89c51
PV
49/* light background */
50static enum color attr_colors_light[] = {
d7bd2db5
MN
51 C_CYAN,
52 C_YELLOW,
53 C_MAGENTA,
54 C_BLUE,
55 C_GREEN,
a1121aa1 56 C_RED,
54eab4c7 57 C_CLEAR,
99b89c51 58};
54eab4c7 59
99b89c51
PV
60/* dark background */
61static enum color attr_colors_dark[] = {
54eab4c7
PV
62 C_BOLD_CYAN,
63 C_BOLD_YELLOW,
64 C_BOLD_MAGENTA,
65 C_BOLD_BLUE,
66 C_BOLD_GREEN,
67 C_BOLD_RED,
a1121aa1 68 C_CLEAR
d7bd2db5
MN
69};
70
54eab4c7 71static int is_dark_bg;
d7bd2db5
MN
72static int color_is_enabled;
73
74void enable_color(void)
75{
76 color_is_enabled = 1;
54eab4c7
PV
77 set_color_palette();
78}
79
80void set_color_palette(void)
81{
82 char *p = getenv("COLORFGBG");
83
84 /*
85 * COLORFGBG environment variable usually contains either two or three
86 * values separated by semicolons; we want the last value in either case.
87 * If this value is 0-6 or 8, background is dark.
88 */
89 if (p && (p = strrchr(p, ';')) != NULL
90 && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
91 && p[2] == '\0')
92 is_dark_bg = 1;
d7bd2db5
MN
93}
94
959f1428
JF
95void check_if_color_enabled(void)
96{
97 if (color_is_enabled) {
98 fprintf(stderr, "Option \"-json\" conflicts with \"-color\".\n");
99 exit(1);
100 }
101}
102
d7bd2db5
MN
103int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...)
104{
105 int ret = 0;
106 va_list args;
107
108 va_start(args, fmt);
109
4b73d52f 110 if (!color_is_enabled || attr == COLOR_NONE) {
d7bd2db5
MN
111 ret = vfprintf(fp, fmt, args);
112 goto end;
113 }
114
99b89c51
PV
115 ret += fprintf(fp, "%s", color_codes[is_dark_bg ?
116 attr_colors_dark[attr] : attr_colors_light[attr]]);
117
d7bd2db5
MN
118 ret += vfprintf(fp, fmt, args);
119 ret += fprintf(fp, "%s", color_codes[C_CLEAR]);
120
121end:
122 va_end(args);
123 return ret;
124}
a1121aa1
PS
125
126enum color_attr ifa_family_color(__u8 ifa_family)
127{
128 switch (ifa_family) {
129 case AF_INET:
130 return COLOR_INET;
131 case AF_INET6:
132 return COLOR_INET6;
133 default:
4b73d52f 134 return COLOR_NONE;
a1121aa1
PS
135 }
136}
137
138enum color_attr oper_state_color(__u8 state)
139{
140 switch (state) {
141 case IF_OPER_UP:
142 return COLOR_OPERSTATE_UP;
143 case IF_OPER_DOWN:
144 return COLOR_OPERSTATE_DOWN;
145 default:
4b73d52f 146 return COLOR_NONE;
a1121aa1
PS
147 }
148}