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