]>
Commit | Line | Data |
---|---|---|
d7bd2db5 MN |
1 | #include <stdio.h> |
2 | #include <stdarg.h> | |
a1121aa1 PS |
3 | #include <sys/socket.h> |
4 | #include <sys/types.h> | |
5 | #include <linux/if.h> | |
d7bd2db5 MN |
6 | |
7 | #include "color.h" | |
8 | ||
9 | enum color { | |
10 | C_RED, | |
11 | C_GREEN, | |
12 | C_YELLOW, | |
13 | C_BLUE, | |
14 | C_MAGENTA, | |
15 | C_CYAN, | |
16 | C_WHITE, | |
17 | C_CLEAR | |
18 | }; | |
19 | ||
20 | static const char * const color_codes[] = { | |
21 | "\e[31m", | |
22 | "\e[32m", | |
23 | "\e[33m", | |
24 | "\e[34m", | |
25 | "\e[35m", | |
26 | "\e[36m", | |
27 | "\e[37m", | |
28 | "\e[0m", | |
29 | NULL, | |
30 | }; | |
31 | ||
32 | static enum color attr_colors[] = { | |
33 | C_CYAN, | |
34 | C_YELLOW, | |
35 | C_MAGENTA, | |
36 | C_BLUE, | |
37 | C_GREEN, | |
a1121aa1 PS |
38 | C_RED, |
39 | C_CLEAR | |
d7bd2db5 MN |
40 | }; |
41 | ||
42 | static int color_is_enabled; | |
43 | ||
44 | void enable_color(void) | |
45 | { | |
46 | color_is_enabled = 1; | |
47 | } | |
48 | ||
49 | int color_fprintf(FILE *fp, enum color_attr attr, const char *fmt, ...) | |
50 | { | |
51 | int ret = 0; | |
52 | va_list args; | |
53 | ||
54 | va_start(args, fmt); | |
55 | ||
56 | if (!color_is_enabled) { | |
57 | ret = vfprintf(fp, fmt, args); | |
58 | goto end; | |
59 | } | |
60 | ||
61 | ret += fprintf(fp, "%s", color_codes[attr_colors[attr]]); | |
62 | ret += vfprintf(fp, fmt, args); | |
63 | ret += fprintf(fp, "%s", color_codes[C_CLEAR]); | |
64 | ||
65 | end: | |
66 | va_end(args); | |
67 | return ret; | |
68 | } | |
a1121aa1 PS |
69 | |
70 | enum color_attr ifa_family_color(__u8 ifa_family) | |
71 | { | |
72 | switch (ifa_family) { | |
73 | case AF_INET: | |
74 | return COLOR_INET; | |
75 | case AF_INET6: | |
76 | return COLOR_INET6; | |
77 | default: | |
78 | return COLOR_CLEAR; | |
79 | } | |
80 | } | |
81 | ||
82 | enum color_attr oper_state_color(__u8 state) | |
83 | { | |
84 | switch (state) { | |
85 | case IF_OPER_UP: | |
86 | return COLOR_OPERSTATE_UP; | |
87 | case IF_OPER_DOWN: | |
88 | return COLOR_OPERSTATE_DOWN; | |
89 | default: | |
90 | return COLOR_CLEAR; | |
91 | } | |
92 | } |