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/>. */
21 #include "gdb_regex.h"
23 /* A regular expression that is used for matching ANSI terminal escape
26 static const char *ansi_regex_text
=
30 /* Capture parameter and intermediate bytes. */
32 /* Parameter bytes. */
34 /* Intermediate bytes. */
36 /* End the first capture. */
39 #define FINAL_SUBEXP 2
42 /* The number of subexpressions to allocate space for, including the
43 "0th" whole match subexpression. */
44 #define NUM_SUBEXPRESSIONS 3
46 /* The compiled form of ansi_regex_text. */
48 static regex_t ansi_regex
;
50 /* This maps bright colors to RGB triples. The index is the bright
51 color index, starting with bright black. The values come from
54 static const uint8_t bright_colors
[][3] = {
55 { 127, 127, 127 }, /* Black. */
56 { 255, 0, 0 }, /* Red. */
57 { 0, 255, 0 }, /* Green. */
58 { 255, 255, 0 }, /* Yellow. */
59 { 92, 92, 255 }, /* Blue. */
60 { 255, 0, 255 }, /* Magenta. */
61 { 0, 255, 255 }, /* Cyan. */
62 { 255, 255, 255 } /* White. */
68 ui_file_style::color::append_ansi (bool is_fg
, std::string
*str
) const
72 if (m_value
>= BLACK
&& m_value
<= WHITE
)
73 str
->append (std::to_string (m_value
+ (is_fg
? 30 : 40)));
74 else if (m_value
> WHITE
&& m_value
<= WHITE
+ 8)
75 str
->append (std::to_string (m_value
- WHITE
+ (is_fg
? 90 : 100)));
76 else if (m_value
!= -1)
78 str
->append (is_fg
? "38;5;" : "48;5;");
79 str
->append (std::to_string (m_value
));
86 str
->append (is_fg
? "38;2;" : "48;2;");
87 str
->append (std::to_string (m_red
)
88 + ";" + std::to_string (m_green
)
89 + ";" + std::to_string (m_blue
));
97 ui_file_style::color::get_rgb (uint8_t *rgb
) const
101 /* Can't call this for a basic color or NONE -- those will end
102 up in the assert below. */
103 if (m_value
>= 8 && m_value
<= 15)
104 memcpy (rgb
, bright_colors
[m_value
- 8], 3 * sizeof (uint8_t));
105 else if (m_value
>= 16 && m_value
<= 231)
109 /* This obscure formula seems to be what terminals actually
111 int component
= value
/ 36;
112 rgb
[0] = component
== 0 ? 0 : (55 + component
* 40);
114 component
= value
/ 6;
115 rgb
[1] = component
== 0 ? 0 : (55 + component
* 40);
117 rgb
[2] = value
== 0 ? 0 : (55 + value
* 40);
119 else if (m_value
>= 232)
121 uint8_t v
= (m_value
- 232) * 10 + 8;
127 gdb_assert_not_reached ("get_rgb called on invalid color");
137 /* See ui-style.h. */
140 ui_file_style::to_ansi () const
142 std::string
result ("\033[");
143 bool need_semi
= m_foreground
.append_ansi (true, &result
);
144 if (!m_background
.is_none ())
147 result
.push_back (';');
148 m_background
.append_ansi (false, &result
);
151 if (m_intensity
!= NORMAL
)
154 result
.push_back (';');
155 result
.append (std::to_string (m_intensity
));
161 result
.push_back (';');
162 result
.push_back ('7');
164 result
.push_back ('m');
168 /* Read a ";" and a number from STRING. Return the number of
169 characters read and put the number into *NUM. */
172 read_semi_number (const char *string
, int *idx
, long *num
)
174 if (string
[*idx
] != ';')
177 if (string
[*idx
] < '0' || string
[*idx
] > '9')
180 *num
= strtol (string
+ *idx
, &tail
, 10);
181 *idx
= tail
- string
;
185 /* A helper for ui_file_style::parse that reads an extended color
186 sequence; that is, and 8- or 24- bit color. */
189 extended_color (const char *str
, int *idx
, ui_file_style::color
*color
)
193 if (!read_semi_number (str
, idx
, &value
))
199 if (!read_semi_number (str
, idx
, &value
))
202 if (value
>= 0 && value
<= 255)
203 *color
= ui_file_style::color (value
);
211 if (!read_semi_number (str
, idx
, &r
)
213 || !read_semi_number (str
, idx
, &g
)
215 || !read_semi_number (str
, idx
, &b
)
218 *color
= ui_file_style::color (r
, g
, b
);
222 /* Unrecognized sequence. */
229 /* See ui-style.h. */
232 ui_file_style::parse (const char *buf
, size_t *n_read
)
234 regmatch_t subexps
[NUM_SUBEXPRESSIONS
];
236 int match
= regexec (&ansi_regex
, buf
, ARRAY_SIZE (subexps
), subexps
, 0);
237 if (match
== REG_NOMATCH
)
242 /* Other failures mean the regexp is broken. */
243 gdb_assert (match
== 0);
244 /* The regexp is anchored. */
245 gdb_assert (subexps
[0].rm_so
== 0);
246 /* The final character exists. */
247 gdb_assert (subexps
[FINAL_SUBEXP
].rm_eo
- subexps
[FINAL_SUBEXP
].rm_so
== 1);
249 if (buf
[subexps
[FINAL_SUBEXP
].rm_so
] != 'm')
251 /* We don't handle this sequence, so just drop it. */
252 *n_read
= subexps
[0].rm_eo
;
256 /* Examine each setting in the match and apply it to the result.
257 See the Select Graphic Rendition section of
258 https://en.wikipedia.org/wiki/ANSI_escape_code. In essence each
259 code is just a number, separated by ";"; there are some more
260 wrinkles but we don't support them all.. */
262 /* "\033[m" means the same thing as "\033[0m", so handle that
264 if (subexps
[DATA_SUBEXP
].rm_so
== subexps
[DATA_SUBEXP
].rm_eo
)
265 *this = ui_file_style ();
267 for (regoff_t i
= subexps
[DATA_SUBEXP
].rm_so
;
268 i
< subexps
[DATA_SUBEXP
].rm_eo
;
275 else if (buf
[i
] >= '0' && buf
[i
] <= '9')
278 long value
= strtol (buf
+ i
, &tail
, 10);
285 *this = ui_file_style ();
300 m_intensity
= NORMAL
;
304 m_intensity
= NORMAL
;
321 m_foreground
= color (value
- 30);
334 m_background
= color (value
- 40);
345 m_foreground
= color (value
- 90 + 8);
356 m_background
= color (value
- 100 + 8);
360 /* If we can't parse the extended color, fail. */
361 if (!extended_color (buf
, &i
, &m_foreground
))
363 *n_read
= subexps
[0].rm_eo
;
369 /* If we can't parse the extended color, fail. */
370 if (!extended_color (buf
, &i
, &m_background
))
372 *n_read
= subexps
[0].rm_eo
;
378 /* Ignore everything else. */
384 /* Unknown, let's just ignore. */
388 *n_read
= subexps
[0].rm_eo
;
392 /* See ui-style.h. */
395 skip_ansi_escape (const char *buf
, int *n_read
)
397 regmatch_t subexps
[NUM_SUBEXPRESSIONS
];
399 int match
= regexec (&ansi_regex
, buf
, ARRAY_SIZE (subexps
), subexps
, 0);
400 if (match
== REG_NOMATCH
|| buf
[subexps
[FINAL_SUBEXP
].rm_so
] != 'm')
403 *n_read
= subexps
[FINAL_SUBEXP
].rm_eo
;
408 _initialize_ui_style ()
410 int code
= regcomp (&ansi_regex
, ansi_regex_text
, REG_EXTENDED
);
411 /* If the regular expression was incorrect, it was a programming
413 gdb_assert (code
== 0);
This page took 0.046856 seconds and 4 git commands to generate.