xref: /netbsd-src/external/gpl3/gdb/dist/gdb/cli/cli-style.c (revision 51c551b6b1a184a955f1d3368897d004bf667c68)
1 /* CLI colorizing
2 
3    Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "cli/cli-cmds.h"
21 #include "cli/cli-decode.h"
22 #include "cli/cli-setshow.h"
23 #include "cli/cli-style.h"
24 #include "source-cache.h"
25 #include "observable.h"
26 
27 /* True if styling is enabled.  */
28 
29 #if defined (__MSDOS__)
30 bool cli_styling = false;
31 #else
32 bool cli_styling = true;
33 #endif
34 
35 /* True if source styling is enabled.  Note that this is only
36    consulted when cli_styling is true.  */
37 
38 bool source_styling = true;
39 
40 /* True if disassembler styling is enabled.  Note that this is only
41    consulted when cli_styling is true.  */
42 
43 bool disassembler_styling = true;
44 
45 /* Name of colors; must correspond to ui_file_style::basic_color.  */
46 static const char * const cli_colors[] = {
47   "none",
48   "black",
49   "red",
50   "green",
51   "yellow",
52   "blue",
53   "magenta",
54   "cyan",
55   "white",
56   nullptr
57 };
58 
59 /* Names of intensities; must correspond to
60    ui_file_style::intensity.  */
61 static const char * const cli_intensities[] = {
62   "normal",
63   "bold",
64   "dim",
65   nullptr
66 };
67 
68 /* See cli-style.h.  */
69 
70 cli_style_option file_name_style ("filename", ui_file_style::GREEN);
71 
72 /* See cli-style.h.  */
73 
74 cli_style_option function_name_style ("function", ui_file_style::YELLOW);
75 
76 /* See cli-style.h.  */
77 
78 cli_style_option variable_name_style ("variable", ui_file_style::CYAN);
79 
80 /* See cli-style.h.  */
81 
82 cli_style_option address_style ("address", ui_file_style::BLUE);
83 
84 /* See cli-style.h.  */
85 
86 cli_style_option highlight_style ("highlight", ui_file_style::RED);
87 
88 /* See cli-style.h.  */
89 
90 cli_style_option title_style ("title", ui_file_style::BOLD);
91 
92 /* See cli-style.h.  */
93 
94 cli_style_option tui_border_style ("tui-border", ui_file_style::CYAN);
95 
96 /* See cli-style.h.  */
97 
98 cli_style_option tui_active_border_style ("tui-active-border",
99 					  ui_file_style::CYAN);
100 
101 /* See cli-style.h.  */
102 
103 cli_style_option metadata_style ("metadata", ui_file_style::DIM);
104 
105 /* See cli-style.h.  */
106 
107 cli_style_option version_style ("version", ui_file_style::MAGENTA,
108 				ui_file_style::BOLD);
109 
110 /* See cli-style.h.  */
111 
112 cli_style_option disasm_mnemonic_style ("mnemonic", ui_file_style::GREEN);
113 
114 /* See cli-style.h.  */
115 
116 cli_style_option disasm_register_style ("register", ui_file_style::RED);
117 
118 /* See cli-style.h.  */
119 
120 cli_style_option disasm_immediate_style ("immediate", ui_file_style::BLUE);
121 
122 /* See cli-style.h.  */
123 
124 cli_style_option disasm_comment_style ("comment", ui_file_style::WHITE,
125 				       ui_file_style::DIM);
126 
127 /* See cli-style.h.  */
128 
129 cli_style_option::cli_style_option (const char *name,
130 				    ui_file_style::basic_color fg,
131 				    ui_file_style::intensity intensity)
132   : changed (name),
133     m_name (name),
134     m_foreground (cli_colors[fg - ui_file_style::NONE]),
135     m_background (cli_colors[0]),
136     m_intensity (cli_intensities[intensity])
137 {
138 }
139 
140 /* See cli-style.h.  */
141 
142 cli_style_option::cli_style_option (const char *name,
143 				    ui_file_style::intensity i)
144   : changed (name),
145     m_name (name),
146     m_foreground (cli_colors[0]),
147     m_background (cli_colors[0]),
148     m_intensity (cli_intensities[i])
149 {
150 }
151 
152 /* Return the color number corresponding to COLOR.  */
153 
154 static int
155 color_number (const char *color)
156 {
157   for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
158     {
159       if (color == cli_colors[i])
160 	return i - 1;
161     }
162   gdb_assert_not_reached ("color not found");
163 }
164 
165 /* See cli-style.h.  */
166 
167 ui_file_style
168 cli_style_option::style () const
169 {
170   int fg = color_number (m_foreground);
171   int bg = color_number (m_background);
172   ui_file_style::intensity intensity = ui_file_style::NORMAL;
173 
174   for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
175     {
176       if (m_intensity == cli_intensities[i])
177 	{
178 	  intensity = (ui_file_style::intensity) i;
179 	  break;
180 	}
181     }
182 
183   return ui_file_style (fg, bg, intensity);
184 }
185 
186 /* See cli-style.h.  */
187 
188 void
189 cli_style_option::do_set_value (const char *ignore, int from_tty,
190 				struct cmd_list_element *cmd)
191 {
192   cli_style_option *cso = (cli_style_option *) cmd->context ();
193   cso->changed.notify ();
194 }
195 
196 /* Implements the cli_style_option::do_show_* functions.
197    WHAT and VALUE are the property and value to show.
198    The style for which WHAT is shown is retrieved from CMD context.  */
199 
200 static void
201 do_show (const char *what, struct ui_file *file,
202 	 struct cmd_list_element *cmd,
203 	 const char *value)
204 {
205   cli_style_option *cso = (cli_style_option *) cmd->context ();
206   gdb_puts (_("The "), file);
207   fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ());
208   gdb_printf (file, _(" %s is: %s\n"), what, value);
209 }
210 
211 /* See cli-style.h.  */
212 
213 void
214 cli_style_option::do_show_foreground (struct ui_file *file, int from_tty,
215 				      struct cmd_list_element *cmd,
216 				      const char *value)
217 {
218   do_show (_("foreground color"), file, cmd, value);
219 }
220 
221 /* See cli-style.h.  */
222 
223 void
224 cli_style_option::do_show_background (struct ui_file *file, int from_tty,
225 				      struct cmd_list_element *cmd,
226 				      const char *value)
227 {
228   do_show (_("background color"), file, cmd, value);
229 }
230 
231 /* See cli-style.h.  */
232 
233 void
234 cli_style_option::do_show_intensity (struct ui_file *file, int from_tty,
235 				     struct cmd_list_element *cmd,
236 				     const char *value)
237 {
238   do_show (_("display intensity"), file, cmd, value);
239 }
240 
241 /* See cli-style.h.  */
242 
243 set_show_commands
244 cli_style_option::add_setshow_commands (enum command_class theclass,
245 					const char *prefix_doc,
246 					struct cmd_list_element **set_list,
247 					struct cmd_list_element **show_list,
248 					bool skip_intensity)
249 {
250   set_show_commands prefix_cmds
251     = add_setshow_prefix_cmd (m_name, theclass, prefix_doc, prefix_doc,
252 			      &m_set_list, &m_show_list, set_list, show_list);
253 
254   set_show_commands commands;
255 
256   commands = add_setshow_enum_cmd
257     ("foreground", theclass, cli_colors,
258      &m_foreground,
259      _("Set the foreground color for this property."),
260      _("Show the foreground color for this property."),
261      nullptr,
262      do_set_value,
263      do_show_foreground,
264      &m_set_list, &m_show_list);
265   commands.set->set_context (this);
266   commands.show->set_context (this);
267 
268   commands = add_setshow_enum_cmd
269     ("background", theclass, cli_colors,
270      &m_background,
271      _("Set the background color for this property."),
272      _("Show the background color for this property."),
273      nullptr,
274      do_set_value,
275      do_show_background,
276      &m_set_list, &m_show_list);
277   commands.set->set_context (this);
278   commands.show->set_context (this);
279 
280   if (!skip_intensity)
281     {
282       commands = add_setshow_enum_cmd
283 	("intensity", theclass, cli_intensities,
284 	 &m_intensity,
285 	 _("Set the display intensity for this property."),
286 	 _("Show the display intensity for this property."),
287 	 nullptr,
288 	 do_set_value,
289 	 do_show_intensity,
290 	 &m_set_list, &m_show_list);
291       commands.set->set_context (this);
292       commands.show->set_context (this);
293     }
294 
295   return prefix_cmds;
296 }
297 
298 cmd_list_element *style_set_list;
299 cmd_list_element *style_show_list;
300 
301 /* The command list for 'set style disassembler'.  */
302 
303 static cmd_list_element *style_disasm_set_list;
304 
305 /* The command list for 'show style disassembler'.  */
306 
307 static cmd_list_element *style_disasm_show_list;
308 
309 static void
310 set_style_enabled  (const char *args, int from_tty, struct cmd_list_element *c)
311 {
312   g_source_cache.clear ();
313   gdb::observers::styling_changed.notify ();
314 }
315 
316 static void
317 show_style_enabled (struct ui_file *file, int from_tty,
318 		    struct cmd_list_element *c, const char *value)
319 {
320   if (cli_styling)
321     gdb_printf (file, _("CLI output styling is enabled.\n"));
322   else
323     gdb_printf (file, _("CLI output styling is disabled.\n"));
324 }
325 
326 static void
327 show_style_sources (struct ui_file *file, int from_tty,
328 		    struct cmd_list_element *c, const char *value)
329 {
330   if (source_styling)
331     gdb_printf (file, _("Source code styling is enabled.\n"));
332   else
333     gdb_printf (file, _("Source code styling is disabled.\n"));
334 }
335 
336 /* Implement 'show style disassembler'.  */
337 
338 static void
339 show_style_disassembler (struct ui_file *file, int from_tty,
340 			 struct cmd_list_element *c, const char *value)
341 {
342   if (disassembler_styling)
343     gdb_printf (file, _("Disassembler output styling is enabled.\n"));
344   else
345     gdb_printf (file, _("Disassembler output styling is disabled.\n"));
346 }
347 
348 void _initialize_cli_style ();
349 void
350 _initialize_cli_style ()
351 {
352   add_setshow_prefix_cmd ("style", no_class,
353 			  _("\
354 Style-specific settings.\n\
355 Configure various style-related variables, such as colors"),
356 			  _("\
357 Style-specific settings.\n\
358 Configure various style-related variables, such as colors"),
359 			  &style_set_list, &style_show_list,
360 			  &setlist, &showlist);
361 
362   add_setshow_boolean_cmd ("enabled", no_class, &cli_styling, _("\
363 Set whether CLI styling is enabled."), _("\
364 Show whether CLI is enabled."), _("\
365 If enabled, output to the terminal is styled."),
366 			   set_style_enabled, show_style_enabled,
367 			   &style_set_list, &style_show_list);
368 
369   add_setshow_boolean_cmd ("sources", no_class, &source_styling, _("\
370 Set whether source code styling is enabled."), _("\
371 Show whether source code styling is enabled."), _("\
372 If enabled, source code is styled.\n"
373 #ifdef HAVE_SOURCE_HIGHLIGHT
374 "Note that source styling only works if styling in general is enabled,\n\
375 see \"show style enabled\"."
376 #else
377 "Source highlighting may be disabled in this installation of gdb, because\n\
378 it was not linked against GNU Source Highlight.  However, it might still be\n\
379 available if the appropriate extension is available at runtime."
380 #endif
381 			   ), set_style_enabled, show_style_sources,
382 			   &style_set_list, &style_show_list);
383 
384   add_setshow_prefix_cmd ("disassembler", no_class,
385 			  _("\
386 Style-specific settings for the disassembler.\n\
387 Configure various disassembler style-related variables."),
388 			  _("\
389 Style-specific settings for the disassembler.\n\
390 Configure various disassembler style-related variables."),
391 			  &style_disasm_set_list, &style_disasm_show_list,
392 			  &style_set_list, &style_show_list);
393 
394   add_setshow_boolean_cmd ("enabled", no_class, &disassembler_styling, _("\
395 Set whether disassembler output styling is enabled."), _("\
396 Show whether disassembler output styling is enabled."), _("\
397 If enabled, disassembler output is styled.  Disassembler highlighting\n\
398 requires the Python Pygments library, if this library is not available\n\
399 then disassembler highlighting will not be possible."
400 			   ), set_style_enabled, show_style_disassembler,
401 			   &style_disasm_set_list, &style_disasm_show_list);
402 
403   file_name_style.add_setshow_commands (no_class, _("\
404 Filename display styling.\n\
405 Configure filename colors and display intensity."),
406 					&style_set_list, &style_show_list,
407 					false);
408 
409   set_show_commands function_prefix_cmds
410     = function_name_style.add_setshow_commands (no_class, _("\
411 Function name display styling.\n\
412 Configure function name colors and display intensity"),
413 						&style_set_list,
414 						&style_show_list,
415 						false);
416 
417   variable_name_style.add_setshow_commands (no_class, _("\
418 Variable name display styling.\n\
419 Configure variable name colors and display intensity"),
420 					    &style_set_list, &style_show_list,
421 					    false);
422 
423   set_show_commands address_prefix_cmds
424     = address_style.add_setshow_commands (no_class, _("\
425 Address display styling.\n\
426 Configure address colors and display intensity"),
427 					  &style_set_list, &style_show_list,
428 					  false);
429 
430   title_style.add_setshow_commands (no_class, _("\
431 Title display styling.\n\
432 Configure title colors and display intensity\n\
433 Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
434 readability."),
435 				    &style_set_list, &style_show_list,
436 				    false);
437 
438   highlight_style.add_setshow_commands (no_class, _("\
439 Highlight display styling.\n\
440 Configure highlight colors and display intensity\n\
441 Some commands use the highlight style to draw the attention to a part\n\
442 of their output."),
443 					&style_set_list, &style_show_list,
444 					false);
445 
446   metadata_style.add_setshow_commands (no_class, _("\
447 Metadata display styling.\n\
448 Configure metadata colors and display intensity\n\
449 The \"metadata\" style is used when GDB displays information about\n\
450 your data, for example \"<unavailable>\""),
451 				       &style_set_list, &style_show_list,
452 				       false);
453 
454   tui_border_style.add_setshow_commands (no_class, _("\
455 TUI border display styling.\n\
456 Configure TUI border colors\n\
457 The \"tui-border\" style is used when GDB displays the border of a\n\
458 TUI window that does not have the focus."),
459 					 &style_set_list, &style_show_list,
460 					 true);
461 
462   tui_active_border_style.add_setshow_commands (no_class, _("\
463 TUI active border display styling.\n\
464 Configure TUI active border colors\n\
465 The \"tui-active-border\" style is used when GDB displays the border of a\n\
466 TUI window that does have the focus."),
467 						&style_set_list,
468 						&style_show_list,
469 						true);
470 
471   version_style.add_setshow_commands (no_class, _("\
472 Version string display styling.\n\
473 Configure colors used to display the GDB version string."),
474 				      &style_set_list, &style_show_list,
475 				      false);
476 
477   disasm_mnemonic_style.add_setshow_commands (no_class, _("\
478 Disassembler mnemonic display styling.\n\
479 Configure the colors and display intensity for instruction mnemonics\n\
480 in the disassembler output.  The \"disassembler mnemonic\" style is\n\
481 used to display instruction mnemonics as well as any assembler\n\
482 directives, e.g. \".byte\", \".word\", etc.\n\
483 \n\
484 This style will only be used for targets that support libopcodes based\n\
485 disassembler styling.  When Python Pygments based styling is used\n\
486 then this style has no effect."),
487 					      &style_disasm_set_list,
488 					      &style_disasm_show_list,
489 					      false);
490 
491   disasm_register_style.add_setshow_commands (no_class, _("\
492 Disassembler register display styling.\n\
493 Configure the colors and display intensity for registers in the\n\
494 disassembler output.\n\
495 \n\
496 This style will only be used for targets that support libopcodes based\n\
497 disassembler styling.  When Python Pygments based styling is used\n\
498 then this style has no effect."),
499 					      &style_disasm_set_list,
500 					      &style_disasm_show_list,
501 					      false);
502 
503   disasm_immediate_style.add_setshow_commands (no_class, _("\
504 Disassembler immediate display styling.\n\
505 Configure the colors and display intensity for immediates in the\n\
506 disassembler output.  The \"disassembler immediate\" style is used for\n\
507 any number that is not an address, this includes constants in arithmetic\n\
508 instructions, as well as address offsets in memory access instructions.\n\
509 \n\
510 This style will only be used for targets that support libopcodes based\n\
511 disassembler styling.  When Python Pygments based styling is used\n\
512 then this style has no effect."),
513 					       &style_disasm_set_list,
514 					       &style_disasm_show_list,
515 					       false);
516 
517   disasm_comment_style.add_setshow_commands (no_class, _("\
518 Disassembler comment display styling.\n\
519 Configure the colors and display intensity for comments in the\n\
520 disassembler output.  The \"disassembler comment\" style is used for\n\
521 the comment character, and everything after the comment character up to\n\
522 the end of the line.  The comment style overrides any other styling,\n\
523 e.g. a register name in a comment will use the comment styling.\n\
524 \n\
525 This style will only be used for targets that support libopcodes based\n\
526 disassembler styling.  When Python Pygments based styling is used\n\
527 then this style has no effect."),
528 					     &style_disasm_set_list,
529 					     &style_disasm_show_list,
530 					     false);
531 
532   /* Setup 'disassembler address' style and 'disassembler symbol' style,
533      these are aliases for 'address' and 'function' styles respectively.  */
534   add_alias_cmd ("address", address_prefix_cmds.set, no_class, 0,
535 		 &style_disasm_set_list);
536   add_alias_cmd ("address", address_prefix_cmds.show, no_class, 0,
537 		 &style_disasm_show_list);
538   add_alias_cmd ("symbol", function_prefix_cmds.set, no_class, 0,
539 		 &style_disasm_set_list);
540   add_alias_cmd ("symbol", function_prefix_cmds.show, no_class, 0,
541 		 &style_disasm_show_list);
542 }
543