Debian Codegraph @

src/basic/ansi-color.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4
5 #include "ansi-color.h"
6 #include "process-util.h"
7 #include "string-table.h"
8 #include "string-util.h"
9 #include "strv.h"
10 #include "terminal-util.h"
11
12 static volatile int cached_color_mode = _COLOR_MODE_INVALID;
13 static volatile int cached_underline_enabled = -1;
14
15 bool underline_enabled(void) {
16
17 if (cached_underline_enabled < 0) {
18
19 /* The Linux console doesn't support underlining, turn it off, but only there. */
20
21 if (colors_enabled())
22 cached_underline_enabled = !streq_ptr(getenv("TERM"), "linux");
23 else
24 cached_underline_enabled = false;
25 }
26
27 return cached_underline_enabled;
28 }
29
30 void reset_ansi_feature_caches(void) {
31 cached_color_mode = _COLOR_MODE_INVALID;
32 cached_underline_enabled = -1;
33 }
34
35 ColorMode parse_systemd_colors(void) {
36 const char *e;
37
38 /* Note: do not log in this function, to avoid infinite recursion issues, as the log functions call
39 * this when deciding whether to color the output. */
40
41 e = getenv("SYSTEMD_COLORS");
42 if (!e)
43 return _COLOR_MODE_INVALID;
44
45 return color_mode_from_string(e);
46 }
47
48 static ColorMode get_color_mode_impl(void) {
49 /* Note: do not log in this function, to avoid infinite recursion issues, as the log functions call
50 * this when deciding whether to color the output. */
51
52 /* Returns the mode used to choose output colors. The possible modes are COLOR_OFF for no colors,
53 * COLOR_16 for only the base 16 ANSI colors, COLOR_256 for more colors, and COLOR_24BIT for
54 * unrestricted color output. */
55
56 /* First, we check $SYSTEMD_COLORS, which is the explicit way to change the mode. */
57 ColorMode m = parse_systemd_colors();
58 if (m >= 0 && m < _COLOR_MODE_FIXED_MAX)
59 return m;
60
61 /* If SYSTEMD_COLORS=true was set explicitly, skip the environment checks below — the user
62 * explicitly requested colors, so honor it even when stdout is piped or $NO_COLOR is set. */
63 if (m != COLOR_TRUE) {
64 /* Check for the presence of $NO_COLOR; value is ignored. */
65 if (getenv("NO_COLOR"))
66 return COLOR_OFF;
67
68 /* Turn colors off unless we are on a TTY. And if we are on a TTY we turn it off if $TERM
69 * is set to "dumb". There's one special tweak though: if we are PID 1 then we do not check
70 * whether we are connected to a TTY, because we don't keep /dev/console open continuously
71 * due to fear of SAK, and hence things are a bit weird. */
72 if (getpid_cached() == 1 ? getenv_terminal_is_dumb() : terminal_is_dumb())
73 return COLOR_OFF;
74 }
75
76 /* We failed to figure out any reason to *disable* colors. Let's see how many colors we shall use. */
77 if (m == COLOR_AUTO_16)
78 return COLOR_16;
79 if (m == COLOR_AUTO_256)
80 return COLOR_256;
81 if (m == COLOR_AUTO_24BIT)
82 return COLOR_24BIT;
83
84 if (STRPTR_IN_SET(getenv("COLORTERM"),
85 "truecolor",
86 "24bit"))
87 return COLOR_24BIT;
88
89 /* Note that the Linux console can only display 16 colors. We still enable 256 color mode
90 * even for PID1 output though (which typically goes to the Linux console), since the Linux
91 * console is able to parse the 256 color sequences and automatically map them to the closest
92 * color in the 16 color palette (since kernel 3.16). Doing 256 colors is nice for people who
93 * invoke systemd in a container or via a serial link or such, and use a true 256 color
94 * terminal to do so. */
95 return COLOR_256;
96 }
97
98 ColorMode get_color_mode(void) {
99 if (cached_color_mode < 0) {
100 cached_color_mode = get_color_mode_impl();
101 assert(cached_color_mode >= 0 && cached_color_mode < _COLOR_MODE_FIXED_MAX);
102 }
103
104 return cached_color_mode;
105 }
106
107 static const char* const color_mode_table[_COLOR_MODE_MAX] = {
108 [COLOR_OFF] = "off",
109 [COLOR_16] = "16",
110 [COLOR_256] = "256",
111 [COLOR_24BIT] = "24bit",
112 [COLOR_AUTO_16] = "auto-16",
113 [COLOR_AUTO_256] = "auto-256",
114 [COLOR_AUTO_24BIT] = "auto-24bit",
115 [COLOR_TRUE] = "true",
116 };
117
118 DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_TRUE);
119
120 /*
121 * Check that the string is formatted like an ANSI color code, i.e. that it consists of one or more
122 * sequences of ASCII digits separated by semicolons. This is equivalent to matching the regex:
123 * ^[0-9]+(;[0-9]+)*$
124 * This can be used to partially validate escape codes of the form "\x1B[%sm", accepting all valid
125 * ANSI color codes while rejecting anything that would result in garbled output (such as injecting
126 * text or changing the type of escape code).
127 */
128 bool looks_like_ansi_color_code(const char *str) {
129 assert(str);
130
131 bool prev_char_was_digit = false;
132
133 for (char c = *str; c != '\0'; c = *(++str)) {
134 if (ascii_isdigit(c))
135 prev_char_was_digit = true;
136 else if (prev_char_was_digit && c == ';')
137 prev_char_was_digit = false;
138 else
139 return false;
140 }
141
142 return prev_char_was_digit;
143 }