2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* A regular expression that is used for matching ANSI terminal escape
25 static const char *ansi_regex_text =
29 /* Capture parameter and intermediate bytes. */
31 /* Parameter bytes. */
33 /* Intermediate bytes. */
35 /* End the first capture. */
38 #define FINAL_SUBEXP 2
41 /* The number of subexpressions to allocate space for, including the
42 "0th" whole match subexpression. */
43 #define NUM_SUBEXPRESSIONS 3
45 /* The compiled form of ansi_regex_text. */
47 static regex_t ansi_regex;
49 /* This maps bright colors to RGB triples. The index is the bright
50 color index, starting with bright black. The values come from
53 static const uint8_t bright_colors[][3] = {
54 { 127, 127, 127 }, /* Black. */
55 { 255, 0, 0 }, /* Red. */
56 { 0, 255, 0 }, /* Green. */
57 { 255, 255, 0 }, /* Yellow. */
58 { 92, 92, 255 }, /* Blue. */
59 { 255, 0, 255 }, /* Magenta. */
60 { 0, 255, 255 }, /* Cyan. */
61 { 255, 255, 255 } /* White. */
67 ui_file_style::color::append_ansi (bool is_fg, std::string *str) const
71 if (m_value >= BLACK && m_value <= WHITE)
72 str->append (std::to_string (m_value + (is_fg ? 30 : 40)));
73 else if (m_value > WHITE && m_value <= WHITE + 8)
74 str->append (std::to_string (m_value - WHITE + (is_fg ? 90 : 100)));
75 else if (m_value != -1)
77 str->append (is_fg ? "38;5;" : "48;5;");
78 str->append (std::to_string (m_value));
85 str->append (is_fg ? "38;2;" : "48;2;");
86 str->append (std::to_string (m_red)
87 + ";" + std::to_string (m_green)
88 + ";" + std::to_string (m_blue));
96 ui_file_style::color::get_rgb (uint8_t *rgb) const
100 /* Can't call this for a basic color or NONE -- those will end
101 up in the assert below. */
102 if (m_value >= 8 && m_value <= 15)
103 memcpy (rgb, bright_colors[m_value - 8], 3 * sizeof (uint8_t));
104 else if (m_value >= 16 && m_value <= 231)
108 /* This obscure formula seems to be what terminals actually
110 int component = value / 36;
111 rgb[0] = component == 0 ? 0 : (55 + component * 40);
113 component = value / 6;
114 rgb[1] = component == 0 ? 0 : (55 + component * 40);
116 rgb[2] = value == 0 ? 0 : (55 + value * 40);
118 else if (m_value >= 232)
120 uint8_t v = (m_value - 232) * 10 + 8;
126 gdb_assert_not_reached ("get_rgb called on invalid color");
136 /* See ui-style.h. */
139 ui_file_style::to_ansi () const
141 std::string result ("\033[");
142 bool need_semi = m_foreground.append_ansi (true, &result);
143 if (!m_background.is_none ())
146 result.push_back (';');
147 m_background.append_ansi (false, &result);
150 if (m_intensity != NORMAL)
153 result.push_back (';');
154 result.append (std::to_string (m_intensity));
160 result.push_back (';');
161 result.push_back ('7');
163 result.push_back ('m');
167 /* Read a ";" and a number from STRING. Return the number of
168 characters read and put the number into *NUM. */
171 read_semi_number (const char *string, int *idx, long *num)
173 if (string[*idx] != ';')
176 if (string[*idx] < '0' || string[*idx] > '9')
179 *num = strtol (string + *idx, &tail, 10);
180 *idx = tail - string;
184 /* A helper for ui_file_style::parse that reads an extended color
185 sequence; that is, and 8- or 24- bit color. */
188 extended_color (const char *str, int *idx, ui_file_style::color *color)
192 if (!read_semi_number (str, idx, &value))
198 if (!read_semi_number (str, idx, &value))
201 if (value >= 0 && value <= 255)
202 *color = ui_file_style::color (value);
210 if (!read_semi_number (str, idx, &r)
212 || !read_semi_number (str, idx, &g)
214 || !read_semi_number (str, idx, &b)
217 *color = ui_file_style::color (r, g, b);
221 /* Unrecognized sequence. */
228 /* See ui-style.h. */
231 ui_file_style::parse (const char *buf, size_t *n_read)
233 regmatch_t subexps[NUM_SUBEXPRESSIONS];
235 int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0);
236 if (match == REG_NOMATCH)
241 /* Other failures mean the regexp is broken. */
242 gdb_assert (match == 0);
243 /* The regexp is anchored. */
244 gdb_assert (subexps[0].rm_so == 0);
245 /* The final character exists. */
246 gdb_assert (subexps[FINAL_SUBEXP].rm_eo - subexps[FINAL_SUBEXP].rm_so == 1);
248 if (buf[subexps[FINAL_SUBEXP].rm_so] != 'm')
250 /* We don't handle this sequence, so just drop it. */
251 *n_read = subexps[0].rm_eo;
255 /* Examine each setting in the match and apply it to the result.
256 See the Select Graphic Rendition section of
257 https://en.wikipedia.org/wiki/ANSI_escape_code. In essence each
258 code is just a number, separated by ";"; there are some more
259 wrinkles but we don't support them all.. */
261 /* "\033[m" means the same thing as "\033[0m", so handle that
263 if (subexps[DATA_SUBEXP].rm_so == subexps[DATA_SUBEXP].rm_eo)
264 *this = ui_file_style ();
266 for (regoff_t i = subexps[DATA_SUBEXP].rm_so;
267 i < subexps[DATA_SUBEXP].rm_eo;
274 else if (buf[i] >= '0' && buf[i] <= '9')
277 long value = strtol (buf + i, &tail, 10);
284 *this = ui_file_style ();
299 m_intensity = NORMAL;
303 m_intensity = NORMAL;
320 m_foreground = color (value - 30);
333 m_background = color (value - 40);
344 m_foreground = color (value - 90 + 8);
355 m_background = color (value - 100 + 8);
359 /* If we can't parse the extended color, fail. */
360 if (!extended_color (buf, &i, &m_foreground))
362 *n_read = subexps[0].rm_eo;
368 /* If we can't parse the extended color, fail. */
369 if (!extended_color (buf, &i, &m_background))
371 *n_read = subexps[0].rm_eo;
377 /* Ignore everything else. */
383 /* Unknown, let's just ignore. */
387 *n_read = subexps[0].rm_eo;
391 /* See ui-style.h. */
394 skip_ansi_escape (const char *buf, int *n_read)
396 regmatch_t subexps[NUM_SUBEXPRESSIONS];
398 int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0);
399 if (match == REG_NOMATCH || buf[subexps[FINAL_SUBEXP].rm_so] != 'm')
402 *n_read = subexps[FINAL_SUBEXP].rm_eo;
407 _initialize_ui_style ()
409 int code = regcomp (&ansi_regex, ansi_regex_text, REG_EXTENDED);
410 /* If the regular expression was incorrect, it was a programming
412 gdb_assert (code == 0);