Add translations for various sub-directories
[binutils-gdb.git] / gdb / ui-style.c
blobf0a8e81d6371d3a1b3a532b6dd57b0affbbacbe5
1 /* Styling for ui_file
2 Copyright (C) 2018-2024 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/>. */
19 #include "ui-style.h"
20 #include "gdb_curses.h"
21 #include "gdbsupport/gdb_regex.h"
23 /* A regular expression that is used for matching ANSI terminal escape
24 sequences. */
26 static const char ansi_regex_text[] =
27 /* Introduction. */
28 "^\033\\["
29 #define DATA_SUBEXP 1
30 /* Capture parameter and intermediate bytes. */
31 "("
32 /* Parameter bytes. */
33 "[\x30-\x3f]*"
34 /* Intermediate bytes. */
35 "[\x20-\x2f]*"
36 /* End the first capture. */
37 ")"
38 /* The final byte. */
39 #define FINAL_SUBEXP 2
40 "([\x40-\x7e])";
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 8-color palette to RGB triples. The values come from
51 plain linux terminal. */
53 static const uint8_t palette_8colors[][3] = {
54 { 1, 1, 1 }, /* Black. */
55 { 222, 56, 43 }, /* Red. */
56 { 57, 181, 74 }, /* Green. */
57 { 255, 199, 6 }, /* Yellow. */
58 { 0, 111, 184 }, /* Blue. */
59 { 118, 38, 113 }, /* Magenta. */
60 { 44, 181, 233 }, /* Cyan. */
61 { 204, 204, 204 }, /* White. */
64 /* This maps 16-color palette to RGB triples. The values come from xterm. */
66 static const uint8_t palette_16colors[][3] = {
67 { 0, 0, 0 }, /* Black. */
68 { 205, 0, 0 }, /* Red. */
69 { 0, 205, 0 }, /* Green. */
70 { 205, 205, 0 }, /* Yellow. */
71 { 0, 0, 238 }, /* Blue. */
72 { 205, 0, 205 }, /* Magenta. */
73 { 0, 205, 205 }, /* Cyan. */
74 { 229, 229, 229 }, /* White. */
75 { 127, 127, 127 }, /* Bright Black. */
76 { 255, 0, 0 }, /* Bright Red. */
77 { 0, 255, 0 }, /* Bright Green. */
78 { 255, 255, 0 }, /* Bright Yellow. */
79 { 92, 92, 255 }, /* Bright Blue. */
80 { 255, 0, 255 }, /* Bright Magenta. */
81 { 0, 255, 255 }, /* Bright Cyan. */
82 { 255, 255, 255 } /* Bright White. */
85 /* See ui-style.h. */
86 /* Must correspond to ui_file_style::basic_color. */
87 const std::vector<const char *> ui_file_style::basic_color_enums = {
88 "none",
89 "black",
90 "red",
91 "green",
92 "yellow",
93 "blue",
94 "magenta",
95 "cyan",
96 "white",
97 nullptr
100 /* Returns text representation of a basic COLOR. */
102 static const char *
103 basic_color_name (int color)
105 int pos = color - ui_file_style::NONE;
106 if (0 <= pos && pos < ui_file_style::basic_color_enums.size ())
107 if (const char *s = ui_file_style::basic_color_enums[pos])
108 return s;
109 error (_("Basic color %d has no name."), color);
112 /* See ui-style.h. */
114 void
115 ui_file_style::color::append_ansi (bool is_fg, std::string *str) const
117 if (m_color_space == color_space::MONOCHROME)
118 str->append (is_fg ? "39" : "49");
119 else if (is_basic ())
120 str->append (std::to_string (m_value + (is_fg ? 30 : 40)));
121 else if (m_color_space == color_space::AIXTERM_16COLOR)
122 str->append (std::to_string (m_value - WHITE - 1 + (is_fg ? 90 : 100)));
123 else if (m_color_space == color_space::XTERM_256COLOR)
124 str->append (is_fg ? "38;5;" : "48;5;").append (std::to_string (m_value));
125 else if (m_color_space == color_space::RGB_24BIT)
127 // See ISO/IEC 8613-6 (or ITU T.416) 13.1.8 Select Graphic Rendition (SGR)
128 str->append (is_fg ? "38;2;" : "48;2;");
129 str->append (std::to_string (m_red)
130 + ";" + std::to_string (m_green)
131 + ";" + std::to_string (m_blue));
133 else
134 gdb_assert_not_reached ("no valid ansi representation of the color");
137 /* See ui-style.h. */
138 std::string
139 ui_file_style::color::to_ansi (bool is_fg) const
141 std::string s = "\033[";
142 append_ansi (is_fg, &s);
143 s.push_back ('m');
144 return s;
147 /* See ui-style.h. */
149 std::string
150 ui_file_style::color::to_string () const
152 if (m_color_space == color_space::RGB_24BIT)
154 char s[64];
155 snprintf (s, sizeof s, "#%02X%02X%02X", m_red, m_green, m_blue);
156 return s;
158 else if (is_none () || is_basic ())
159 return basic_color_name (m_value);
160 else
161 return std::to_string (get_value ());
164 /* See ui-style.h. */
166 void
167 ui_file_style::color::get_rgb (uint8_t *rgb) const
169 if (m_color_space == color_space::RGB_24BIT)
171 rgb[0] = m_red;
172 rgb[1] = m_green;
173 rgb[2] = m_blue;
175 else if (m_color_space == color_space::ANSI_8COLOR
176 && 0 <= m_value && m_value <= 7)
177 memcpy (rgb, palette_8colors[m_value], 3 * sizeof (uint8_t));
178 else if (m_color_space == color_space::AIXTERM_16COLOR
179 && 0 <= m_value && m_value <= 15)
180 memcpy (rgb, palette_16colors[m_value], 3 * sizeof (uint8_t));
181 else if (m_color_space != color_space::XTERM_256COLOR)
182 gdb_assert_not_reached ("get_rgb called on invalid color");
183 else if (0 <= m_value && m_value <= 15)
184 memcpy (rgb, palette_16colors[m_value], 3 * sizeof (uint8_t));
185 else if (m_value >= 16 && m_value <= 231)
187 int value = m_value;
188 value -= 16;
189 /* This obscure formula seems to be what terminals actually
190 do. */
191 int component = value / 36;
192 rgb[0] = component == 0 ? 0 : (55 + component * 40);
193 value %= 36;
194 component = value / 6;
195 rgb[1] = component == 0 ? 0 : (55 + component * 40);
196 value %= 6;
197 rgb[2] = value == 0 ? 0 : (55 + value * 40);
199 else if (232 <= m_value && m_value <= 255)
201 uint8_t v = (m_value - 232) * 10 + 8;
202 rgb[0] = v;
203 rgb[1] = v;
204 rgb[2] = v;
206 else
207 gdb_assert_not_reached ("get_rgb called on invalid color");
210 /* See ui-style.h. */
212 ui_file_style::color
213 ui_file_style::color::approximate (const std::vector<color_space> &spaces) const
215 if (spaces.empty () || is_none ())
216 return NONE;
218 color_space target_space = color_space::MONOCHROME;
219 for (color_space sp : spaces)
220 if (sp == m_color_space)
221 return *this;
222 else if (sp > target_space)
223 target_space = sp;
225 if (target_space == color_space::RGB_24BIT)
227 uint8_t rgb[3];
228 get_rgb (rgb);
229 return color (rgb[0], rgb[1], rgb[2]);
232 int target_size = 0;
233 switch (target_space)
235 case color_space::ANSI_8COLOR:
236 target_size = 8;
237 break;
238 case color_space::AIXTERM_16COLOR:
239 target_size = 16;
240 break;
241 case color_space::XTERM_256COLOR:
242 target_size = 256;
243 break;
246 if (is_simple() && m_value < target_size)
247 return color (target_space, m_value);
249 color result = NONE;
250 int best_distance = std::numeric_limits<int>::max ();
251 uint8_t rgb[3];
252 get_rgb (rgb);
254 for (int i = 0; i < target_size; ++i)
256 uint8_t c_rgb[3];
257 color c (target_space, i);
258 c.get_rgb (c_rgb);
259 int d_red = std::abs (rgb[0] - c_rgb[0]);
260 int d_green = std::abs (rgb[1] - c_rgb[1]);
261 int d_blue = std::abs (rgb[2] - c_rgb[2]);
262 int dist = d_red * d_red + d_green * d_green + d_blue * d_blue;
263 if (dist < best_distance)
265 best_distance = dist;
266 result = c;
270 return result;
273 /* See ui-style.h. */
275 std::string
276 ui_file_style::to_ansi () const
278 std::string result ("\033[");
279 if (!is_default ())
281 m_foreground.append_ansi (true, &result);
282 result.push_back (';');
283 m_background.append_ansi (false, &result);
284 result.push_back (';');
285 if (m_intensity == NORMAL)
286 result.append ("22");
287 else
288 result.append (std::to_string (m_intensity));
289 result.push_back (';');
290 if (m_reverse)
291 result.push_back ('7');
292 else
293 result.append ("27");
295 result.push_back ('m');
296 return result;
299 /* Read a ";" and a number from STRING. Return the number of
300 characters read and put the number into *NUM. */
302 static bool
303 read_semi_number (const char *string, regoff_t *idx, long *num)
305 if (string[*idx] != ';')
306 return false;
307 ++*idx;
308 if (string[*idx] < '0' || string[*idx] > '9')
309 return false;
310 char *tail;
311 *num = strtol (string + *idx, &tail, 10);
312 *idx = tail - string;
313 return true;
316 /* A helper for ui_file_style::parse that reads an extended color
317 sequence; that is, and 8- or 24- bit color. */
319 static bool
320 extended_color (const char *str, regoff_t *idx, ui_file_style::color *color)
322 long value;
324 if (!read_semi_number (str, idx, &value))
325 return false;
327 if (value == 5)
329 /* 8-bit color. */
330 if (!read_semi_number (str, idx, &value))
331 return false;
333 if (value >= 0 && value <= 255)
334 *color = ui_file_style::color (value);
335 else
336 return false;
338 else if (value == 2)
340 /* 24-bit color. */
341 long r, g, b;
342 if (!read_semi_number (str, idx, &r)
343 || r > 255
344 || !read_semi_number (str, idx, &g)
345 || g > 255
346 || !read_semi_number (str, idx, &b)
347 || b > 255)
348 return false;
349 *color = ui_file_style::color (r, g, b);
351 else
353 /* Unrecognized sequence. */
354 return false;
357 return true;
360 /* See ui-style.h. */
362 bool
363 ui_file_style::parse (const char *buf, size_t *n_read)
365 regmatch_t subexps[NUM_SUBEXPRESSIONS];
367 int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0);
368 if (match == REG_NOMATCH)
370 *n_read = 0;
371 return false;
373 /* Other failures mean the regexp is broken. */
374 gdb_assert (match == 0);
375 /* The regexp is anchored. */
376 gdb_assert (subexps[0].rm_so == 0);
377 /* The final character exists. */
378 gdb_assert (subexps[FINAL_SUBEXP].rm_eo - subexps[FINAL_SUBEXP].rm_so == 1);
380 if (buf[subexps[FINAL_SUBEXP].rm_so] != 'm')
382 /* We don't handle this sequence, so just drop it. */
383 *n_read = subexps[0].rm_eo;
384 return false;
387 /* Examine each setting in the match and apply it to the result.
388 See the Select Graphic Rendition section of
389 https://en.wikipedia.org/wiki/ANSI_escape_code. In essence each
390 code is just a number, separated by ";"; there are some more
391 wrinkles but we don't support them all.. */
393 /* "\033[m" means the same thing as "\033[0m", so handle that
394 specially here. */
395 if (subexps[DATA_SUBEXP].rm_so == subexps[DATA_SUBEXP].rm_eo)
396 *this = ui_file_style ();
398 for (regoff_t i = subexps[DATA_SUBEXP].rm_so;
399 i < subexps[DATA_SUBEXP].rm_eo;
400 ++i)
402 if (buf[i] == ';')
404 /* Skip. */
406 else if (buf[i] >= '0' && buf[i] <= '9')
408 char *tail;
409 long value = strtol (buf + i, &tail, 10);
410 i = tail - buf;
412 switch (value)
414 case 0:
415 /* Reset. */
416 *this = ui_file_style ();
417 break;
418 case 1:
419 /* Bold. */
420 m_intensity = BOLD;
421 break;
422 case 2:
423 /* Dim. */
424 m_intensity = DIM;
425 break;
426 case 7:
427 /* Reverse. */
428 m_reverse = true;
429 break;
430 case 21:
431 m_intensity = NORMAL;
432 break;
433 case 22:
434 /* Normal. */
435 m_intensity = NORMAL;
436 break;
437 case 27:
438 /* Inverse off. */
439 m_reverse = false;
440 break;
442 case 30:
443 case 31:
444 case 32:
445 case 33:
446 case 34:
447 case 35:
448 case 36:
449 case 37:
450 m_foreground = color (value - 30);
451 break;
452 /* Note: not 38. */
453 case 39:
454 m_foreground = NONE;
455 break;
457 case 40:
458 case 41:
459 case 42:
460 case 43:
461 case 44:
462 case 45:
463 case 46:
464 case 47:
465 m_background = color (value - 40);
466 break;
467 /* Note: not 48. */
468 case 49:
469 m_background = NONE;
470 break;
472 case 90:
473 case 91:
474 case 92:
475 case 93:
476 case 94:
477 case 95:
478 case 96:
479 case 97:
480 m_foreground = color (value - 90 + 8);
481 break;
483 case 100:
484 case 101:
485 case 102:
486 case 103:
487 case 104:
488 case 105:
489 case 106:
490 case 107:
491 m_background = color (value - 100 + 8);
492 break;
494 case 38:
495 /* If we can't parse the extended color, fail. */
496 if (!extended_color (buf, &i, &m_foreground))
498 *n_read = subexps[0].rm_eo;
499 return false;
501 break;
503 case 48:
504 /* If we can't parse the extended color, fail. */
505 if (!extended_color (buf, &i, &m_background))
507 *n_read = subexps[0].rm_eo;
508 return false;
510 break;
512 default:
513 /* Ignore everything else. */
514 break;
517 else
519 /* Unknown, let's just ignore. */
523 *n_read = subexps[0].rm_eo;
524 return true;
527 /* See ui-style.h. */
529 bool
530 skip_ansi_escape (const char *buf, int *n_read)
532 regmatch_t subexps[NUM_SUBEXPRESSIONS];
534 int match = regexec (&ansi_regex, buf, ARRAY_SIZE (subexps), subexps, 0);
535 if (match == REG_NOMATCH || buf[subexps[FINAL_SUBEXP].rm_so] != 'm')
536 return false;
538 *n_read = subexps[FINAL_SUBEXP].rm_eo;
539 return true;
542 void _initialize_ui_style ();
543 void
544 _initialize_ui_style ()
546 int code = regcomp (&ansi_regex, ansi_regex_text, REG_EXTENDED);
547 /* If the regular expression was incorrect, it was a programming
548 error. */
549 gdb_assert (code == 0);
552 /* See ui-style.h. */
554 const std::vector<color_space> &
555 colorsupport ()
557 static const std::vector<color_space> value = []
559 std::vector<color_space> result = {color_space::MONOCHROME};
561 int colors = tgetnum ("Co");
562 if (colors >= 8)
563 result.push_back (color_space::ANSI_8COLOR);
564 if (colors >= 16)
565 result.push_back (color_space::AIXTERM_16COLOR);
566 if (colors >= 256)
567 result.push_back (color_space::XTERM_256COLOR);
569 const char *colorterm = getenv ("COLORTERM");
570 if (colorterm != nullptr && (!strcmp (colorterm, "truecolor")
571 || !strcmp (colorterm, "24bit")))
572 result.push_back (color_space::RGB_24BIT);
574 return result;
575 } ();
576 return value;
579 const char *
580 color_space_name (color_space c)
582 switch (c)
584 case color_space::MONOCHROME: return "monochrome";
585 case color_space::ANSI_8COLOR: return "ansi_8color";
586 case color_space::AIXTERM_16COLOR: return "aixterm_16color";
587 case color_space::XTERM_256COLOR: return "xterm_256color";
588 case color_space::RGB_24BIT: return "rgb_24bit";
590 gdb_assert_not_reached ("color_space_name called on invalid color");
593 bool
594 color_space_safe_cast (color_space *result, long c)
596 switch (static_cast<color_space>(c))
598 case color_space::MONOCHROME:
599 case color_space::ANSI_8COLOR:
600 case color_space::AIXTERM_16COLOR:
601 case color_space::XTERM_256COLOR:
602 case color_space::RGB_24BIT:
603 *result = static_cast<color_space>(c);
604 return true;
606 return false;