GDB (xrefs)
Loading...
Searching...
No Matches
cli-style.c
Go to the documentation of this file.
1/* CLI colorizing
2
3 Copyright (C) 2018-2023 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 "defs.h"
21#include "cli/cli-cmds.h"
22#include "cli/cli-decode.h"
23#include "cli/cli-setshow.h"
24#include "cli/cli-style.h"
25#include "source-cache.h"
26#include "observable.h"
27
28/* True if styling is enabled. */
29
30#if defined (__MSDOS__)
31bool cli_styling = false;
32#else
33bool cli_styling = true;
34#endif
35
36/* True if source styling is enabled. Note that this is only
37 consulted when cli_styling is true. */
38
39bool source_styling = true;
40
41/* True if disassembler styling is enabled. Note that this is only
42 consulted when cli_styling is true. */
43
45
46/* Name of colors; must correspond to ui_file_style::basic_color. */
47static const char * const cli_colors[] = {
48 "none",
49 "black",
50 "red",
51 "green",
52 "yellow",
53 "blue",
54 "magenta",
55 "cyan",
56 "white",
57 nullptr
58};
59
60/* Names of intensities; must correspond to
61 ui_file_style::intensity. */
62static const char * const cli_intensities[] = {
63 "normal",
64 "bold",
65 "dim",
66 nullptr
67};
68
69/* See cli-style.h. */
70
72
73/* See cli-style.h. */
74
76
77/* See cli-style.h. */
78
80
81/* See cli-style.h. */
82
84
85/* See cli-style.h. */
86
88
89/* See cli-style.h. */
90
92
93/* See cli-style.h. */
94
96
97/* See cli-style.h. */
98
101
102/* See cli-style.h. */
103
105
106/* See cli-style.h. */
107
110
111/* See cli-style.h. */
112
114
115/* See cli-style.h. */
116
118
119/* See cli-style.h. */
120
122
123/* See cli-style.h. */
124
127
128/* See cli-style.h. */
129
132 ui_file_style::intensity intensity)
133 : changed (name),
134 m_name (name),
135 m_foreground (cli_colors[fg - ui_file_style::NONE]),
136 m_background (cli_colors[0]),
137 m_intensity (cli_intensities[intensity])
138{
139}
140
141/* See cli-style.h. */
142
145 : changed (name),
146 m_name (name),
147 m_foreground (cli_colors[0]),
148 m_background (cli_colors[0]),
149 m_intensity (cli_intensities[i])
150{
151}
152
153/* Return the color number corresponding to COLOR. */
154
155static int
156color_number (const char *color)
157{
158 for (int i = 0; i < ARRAY_SIZE (cli_colors); ++i)
159 {
160 if (color == cli_colors[i])
161 return i - 1;
162 }
163 gdb_assert_not_reached ("color not found");
164}
165
166/* See cli-style.h. */
167
170{
171 int fg = color_number (m_foreground);
172 int bg = color_number (m_background);
174
175 for (int i = 0; i < ARRAY_SIZE (cli_intensities); ++i)
176 {
178 {
179 intensity = (ui_file_style::intensity) i;
180 break;
181 }
182 }
183
184 return ui_file_style (fg, bg, intensity);
185}
186
187/* See cli-style.h. */
188
189void
190cli_style_option::do_set_value (const char *ignore, int from_tty,
191 struct cmd_list_element *cmd)
192{
193 cli_style_option *cso = (cli_style_option *) cmd->context ();
194 cso->changed.notify ();
195}
196
197/* Implements the cli_style_option::do_show_* functions.
198 WHAT and VALUE are the property and value to show.
199 The style for which WHAT is shown is retrieved from CMD context. */
200
201static void
202do_show (const char *what, struct ui_file *file,
203 struct cmd_list_element *cmd,
204 const char *value)
205{
206 cli_style_option *cso = (cli_style_option *) cmd->context ();
207 gdb_puts (_("The "), file);
208 fprintf_styled (file, cso->style (), _("\"%s\" style"), cso->name ());
209 gdb_printf (file, _(" %s is: %s\n"), what, value);
210}
211
212/* See cli-style.h. */
213
214void
216 struct cmd_list_element *cmd,
217 const char *value)
218{
219 do_show (_("foreground color"), file, cmd, value);
220}
221
222/* See cli-style.h. */
223
224void
226 struct cmd_list_element *cmd,
227 const char *value)
228{
229 do_show (_("background color"), file, cmd, value);
230}
231
232/* See cli-style.h. */
233
234void
236 struct cmd_list_element *cmd,
237 const char *value)
238{
239 do_show (_("display intensity"), file, cmd, value);
240}
241
242/* See cli-style.h. */
243
246 const char *prefix_doc,
247 struct cmd_list_element **set_list,
248 struct cmd_list_element **show_list,
249 bool skip_intensity)
250{
251 set_show_commands prefix_cmds
252 = add_setshow_prefix_cmd (m_name, theclass, prefix_doc, prefix_doc,
254
255 set_show_commands commands;
256
257 commands = add_setshow_enum_cmd
258 ("foreground", theclass, cli_colors,
260 _("Set the foreground color for this property."),
261 _("Show the foreground color for this property."),
262 nullptr,
266 commands.set->set_context (this);
267 commands.show->set_context (this);
268
269 commands = add_setshow_enum_cmd
270 ("background", theclass, cli_colors,
272 _("Set the background color for this property."),
273 _("Show the background color for this property."),
274 nullptr,
278 commands.set->set_context (this);
279 commands.show->set_context (this);
280
281 if (!skip_intensity)
282 {
283 commands = add_setshow_enum_cmd
284 ("intensity", theclass, cli_intensities,
286 _("Set the display intensity for this property."),
287 _("Show the display intensity for this property."),
288 nullptr,
292 commands.set->set_context (this);
293 commands.show->set_context (this);
294 }
295
296 return prefix_cmds;
297}
298
301
302/* The command list for 'set style disassembler'. */
303
305
306/* The command list for 'show style disassembler'. */
307
309
310static void
311set_style_enabled (const char *args, int from_tty, struct cmd_list_element *c)
312{
315}
316
317static void
318show_style_enabled (struct ui_file *file, int from_tty,
319 struct cmd_list_element *c, const char *value)
320{
321 if (cli_styling)
322 gdb_printf (file, _("CLI output styling is enabled.\n"));
323 else
324 gdb_printf (file, _("CLI output styling is disabled.\n"));
325}
326
327static void
328show_style_sources (struct ui_file *file, int from_tty,
329 struct cmd_list_element *c, const char *value)
330{
331 if (source_styling)
332 gdb_printf (file, _("Source code styling is enabled.\n"));
333 else
334 gdb_printf (file, _("Source code styling is disabled.\n"));
335}
336
337/* Implement 'show style disassembler'. */
338
339static void
340show_style_disassembler (struct ui_file *file, int from_tty,
341 struct cmd_list_element *c, const char *value)
342{
344 gdb_printf (file, _("Disassembler output styling is enabled.\n"));
345 else
346 gdb_printf (file, _("Disassembler output styling is disabled.\n"));
347}
348
350void
352{
354 _("\
355Style-specific settings.\n\
356Configure various style-related variables, such as colors"),
357 _("\
358Style-specific settings.\n\
359Configure various style-related variables, such as colors"),
361 &setlist, &showlist);
362
364Set whether CLI styling is enabled."), _("\
365Show whether CLI is enabled."), _("\
366If enabled, output to the terminal is styled."),
369
371Set whether source code styling is enabled."), _("\
372Show whether source code styling is enabled."), _("\
373If enabled, source code is styled.\n"
374#ifdef HAVE_SOURCE_HIGHLIGHT
375"Note that source styling only works if styling in general is enabled,\n\
376see \"show style enabled\"."
377#else
378"Source highlighting may be disabled in this installation of gdb, because\n\
379it was not linked against GNU Source Highlight. However, it might still be\n\
380available if the appropriate extension is available at runtime."
381#endif
384
385 add_setshow_prefix_cmd ("disassembler", no_class,
386 _("\
387Style-specific settings for the disassembler.\n\
388Configure various disassembler style-related variables."),
389 _("\
390Style-specific settings for the disassembler.\n\
391Configure various disassembler style-related variables."),
394
396Set whether disassembler output styling is enabled."), _("\
397Show whether disassembler output styling is enabled."), _("\
398If enabled, disassembler output is styled. Disassembler highlighting\n\
399requires the Python Pygments library, if this library is not available\n\
400then disassembler highlighting will not be possible."
403
405Filename display styling.\n\
406Configure filename colors and display intensity."),
408 false);
409
410 set_show_commands function_prefix_cmds
412Function name display styling.\n\
413Configure function name colors and display intensity"),
416 false);
417
419Variable name display styling.\n\
420Configure variable name colors and display intensity"),
422 false);
423
424 set_show_commands address_prefix_cmds
426Address display styling.\n\
427Configure address colors and display intensity"),
429 false);
430
432Title display styling.\n\
433Configure title colors and display intensity\n\
434Some commands (such as \"apropos -v REGEXP\") use the title style to improve\n\
435readability."),
437 false);
438
440Highlight display styling.\n\
441Configure highlight colors and display intensity\n\
442Some commands use the highlight style to draw the attention to a part\n\
443of their output."),
445 false);
446
448Metadata display styling.\n\
449Configure metadata colors and display intensity\n\
450The \"metadata\" style is used when GDB displays information about\n\
451your data, for example \"<unavailable>\""),
453 false);
454
456TUI border display styling.\n\
457Configure TUI border colors\n\
458The \"tui-border\" style is used when GDB displays the border of a\n\
459TUI window that does not have the focus."),
461 true);
462
464TUI active border display styling.\n\
465Configure TUI active border colors\n\
466The \"tui-active-border\" style is used when GDB displays the border of a\n\
467TUI window that does have the focus."),
470 true);
471
473Version string display styling.\n\
474Configure colors used to display the GDB version string."),
476 false);
477
479Disassembler mnemonic display styling.\n\
480Configure the colors and display intensity for instruction mnemonics\n\
481in the disassembler output. The \"disassembler mnemonic\" style is\n\
482used to display instruction mnemonics as well as any assembler\n\
483directives, e.g. \".byte\", \".word\", etc.\n\
484\n\
485This style will only be used for targets that support libopcodes based\n\
486disassembler styling. When Python Pygments based styling is used\n\
487then this style has no effect."),
490 false);
491
493Disassembler register display styling.\n\
494Configure the colors and display intensity for registers in the\n\
495disassembler output.\n\
496\n\
497This style will only be used for targets that support libopcodes based\n\
498disassembler styling. When Python Pygments based styling is used\n\
499then this style has no effect."),
502 false);
503
505Disassembler immediate display styling.\n\
506Configure the colors and display intensity for immediates in the\n\
507disassembler output. The \"disassembler immediate\" style is used for\n\
508any number that is not an address, this includes constants in arithmetic\n\
509instructions, as well as address offsets in memory access instructions.\n\
510\n\
511This style will only be used for targets that support libopcodes based\n\
512disassembler styling. When Python Pygments based styling is used\n\
513then this style has no effect."),
516 false);
517
519Disassembler comment display styling.\n\
520Configure the colors and display intensity for comments in the\n\
521disassembler output. The \"disassembler comment\" style is used for\n\
522the comment character, and everything after the comment character up to\n\
523the end of the line. The comment style overrides any other styling,\n\
524e.g. a register name in a comment will use the comment styling.\n\
525\n\
526This style will only be used for targets that support libopcodes based\n\
527disassembler styling. When Python Pygments based styling is used\n\
528then this style has no effect."),
531 false);
532
533 /* Setup 'disassembler address' style and 'disassembler symbol' style,
534 these are aliases for 'address' and 'function' styles respectively. */
535 add_alias_cmd ("address", address_prefix_cmds.set, no_class, 0,
537 add_alias_cmd ("address", address_prefix_cmds.show, no_class, 0,
539 add_alias_cmd ("symbol", function_prefix_cmds.set, no_class, 0,
541 add_alias_cmd ("symbol", function_prefix_cmds.show, no_class, 0,
543}
const char *const name
const char * m_name
Definition cli-style.h:67
static void do_show_intensity(struct ui_file *file, int from_tty, struct cmd_list_element *cmd, const char *value)
Definition cli-style.c:235
gdb::observers::observable changed
Definition cli-style.h:62
const char * m_intensity
Definition cli-style.h:74
ui_file_style style() const
Definition cli-style.c:169
struct cmd_list_element * m_set_list
Definition cli-style.h:78
set_show_commands add_setshow_commands(enum command_class theclass, const char *prefix_doc, struct cmd_list_element **set_list, struct cmd_list_element **show_list, bool skip_intensity)
Definition cli-style.c:245
struct cmd_list_element * set_list()
Definition cli-style.h:56
struct cmd_list_element * m_show_list
Definition cli-style.h:79
static void do_show_background(struct ui_file *file, int from_tty, struct cmd_list_element *cmd, const char *value)
Definition cli-style.c:225
static void do_set_value(const char *ignore, int from_tty, struct cmd_list_element *cmd)
Definition cli-style.c:190
static void do_show_foreground(struct ui_file *file, int from_tty, struct cmd_list_element *cmd, const char *value)
Definition cli-style.c:215
const char * m_background
Definition cli-style.h:72
cli_style_option(const char *name, ui_file_style::basic_color fg, ui_file_style::intensity=ui_file_style::NORMAL)
Definition cli-style.c:130
const char * m_foreground
Definition cli-style.h:70
struct cmd_list_element * show_list()
Definition cli-style.h:59
const char * name()
Definition cli-style.h:44
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * setlist
Definition cli-cmds.c:119
struct cmd_list_element * add_alias_cmd(const char *name, cmd_list_element *target, enum command_class theclass, int abbrev_flag, struct cmd_list_element **list)
Definition cli-decode.c:294
set_show_commands add_setshow_enum_cmd(const char *name, enum command_class theclass, const char *const *enumlist, const char **var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:688
set_show_commands add_setshow_prefix_cmd(const char *name, command_class theclass, const char *set_doc, const char *show_doc, cmd_list_element **set_subcommands_list, cmd_list_element **show_subcommands_list, cmd_list_element **set_list, cmd_list_element **show_list)
Definition cli-decode.c:428
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
bool source_styling
Definition cli-style.c:39
cmd_list_element * style_show_list
Definition cli-style.c:300
cmd_list_element * style_set_list
Definition cli-style.c:299
static void show_style_enabled(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition cli-style.c:318
static void show_style_sources(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition cli-style.c:328
bool disassembler_styling
Definition cli-style.c:44
static const char *const cli_intensities[]
Definition cli-style.c:62
static int color_number(const char *color)
Definition cli-style.c:156
static const char *const cli_colors[]
Definition cli-style.c:47
void _initialize_cli_style()
Definition cli-style.c:351
static cmd_list_element * style_disasm_show_list
Definition cli-style.c:308
bool cli_styling
Definition cli-style.c:33
static void show_style_disassembler(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition cli-style.c:340
static void set_style_enabled(const char *args, int from_tty, struct cmd_list_element *c)
Definition cli-style.c:311
static void do_show(const char *what, struct ui_file *file, struct cmd_list_element *cmd, const char *value)
Definition cli-style.c:202
static cmd_list_element * style_disasm_set_list
Definition cli-style.c:304
cli_style_option address_style
cli_style_option tui_active_border_style
cli_style_option highlight_style
cli_style_option function_name_style
cli_style_option disasm_mnemonic_style
cli_style_option disasm_register_style
cli_style_option variable_name_style
cli_style_option title_style
cli_style_option tui_border_style
cli_style_option file_name_style
cli_style_option metadata_style
cli_style_option disasm_immediate_style
cli_style_option disasm_comment_style
cli_style_option version_style
command_class
Definition command.h:43
@ no_class
Definition command.h:53
observable styling_changed
source_cache g_source_cache
void * context() const
Definition cli-decode.h:109
void set_context(void *context)
Definition cli-decode.h:103
cmd_list_element * set
Definition command.h:422
cmd_list_element * show
Definition command.h:422
Definition value.h:130
void fprintf_styled(struct ui_file *stream, const ui_file_style &style, const char *format,...)
Definition utils.c:1898
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
void gdb_puts(const char *linebuffer, struct ui_file *stream)
Definition utils.c:1809