1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999-2022 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "completer.h"
27 #include "readline/readline.h"
28 #include "cli/cli-style.h"
30 /* These are the CLI output functions */
32 /* Mark beginning of a table */
35 cli_ui_out::do_table_begin (int nbrofcols
, int nr_rows
, const char *tblid
)
38 m_suppress_output
= true;
40 /* Only the table suppresses the output and, fortunately, a table
41 is not a recursive data structure. */
42 gdb_assert (!m_suppress_output
);
45 /* Mark beginning of a table body */
48 cli_ui_out::do_table_body ()
50 if (m_suppress_output
)
53 /* first, close the table header line */
57 /* Mark end of a table */
60 cli_ui_out::do_table_end ()
62 m_suppress_output
= false;
65 /* Specify table header */
68 cli_ui_out::do_table_header (int width
, ui_align alignment
,
69 const std::string
&col_name
,
70 const std::string
&col_hdr
)
72 if (m_suppress_output
)
75 do_field_string (0, width
, alignment
, 0, col_hdr
.c_str (),
79 /* Mark beginning of a list */
82 cli_ui_out::do_begin (ui_out_type type
, const char *id
)
86 /* Mark end of a list */
89 cli_ui_out::do_end (ui_out_type type
)
93 /* output an int field */
96 cli_ui_out::do_field_signed (int fldno
, int width
, ui_align alignment
,
97 const char *fldname
, LONGEST value
)
99 if (m_suppress_output
)
102 do_field_string (fldno
, width
, alignment
, fldname
, plongest (value
),
106 /* output an unsigned field */
109 cli_ui_out::do_field_unsigned (int fldno
, int width
, ui_align alignment
,
110 const char *fldname
, ULONGEST value
)
112 if (m_suppress_output
)
115 do_field_string (fldno
, width
, alignment
, fldname
, pulongest (value
),
119 /* used to omit a field */
122 cli_ui_out::do_field_skip (int fldno
, int width
, ui_align alignment
,
125 if (m_suppress_output
)
128 do_field_string (fldno
, width
, alignment
, fldname
, "",
132 /* other specific cli_field_* end up here so alignment and field
133 separators are both handled by cli_field_string */
136 cli_ui_out::do_field_string (int fldno
, int width
, ui_align align
,
137 const char *fldname
, const char *string
,
138 const ui_file_style
&style
)
143 if (m_suppress_output
)
146 if ((align
!= ui_noalign
) && string
)
148 before
= width
- strlen (string
);
153 if (align
== ui_right
)
155 else if (align
== ui_left
)
174 ui_file
*stream
= m_streams
.back ();
175 stream
->emit_style_escape (style
);
176 if (test_flags (unfiltered_output
))
177 stream
->puts_unfiltered (string
);
179 stream
->puts (string
);
180 stream
->emit_style_escape (ui_file_style ());
186 if (align
!= ui_noalign
)
190 /* Output field containing ARGS using printf formatting in FORMAT. */
193 cli_ui_out::do_field_fmt (int fldno
, int width
, ui_align align
,
194 const char *fldname
, const ui_file_style
&style
,
195 const char *format
, va_list args
)
197 if (m_suppress_output
)
200 std::string str
= string_vprintf (format
, args
);
202 do_field_string (fldno
, width
, align
, fldname
, str
.c_str (), style
);
206 cli_ui_out::do_spaces (int numspaces
)
208 if (m_suppress_output
)
211 if (test_flags (unfiltered_output
))
212 fprintf_unfiltered (m_streams
.back (), "%*s", numspaces
, "");
214 print_spaces_filtered (numspaces
, m_streams
.back ());
218 cli_ui_out::do_text (const char *string
)
220 if (m_suppress_output
)
223 if (test_flags (unfiltered_output
))
224 gdb_puts (string
, m_streams
.back ());
226 gdb_puts (string
, m_streams
.back ());
230 cli_ui_out::do_message (const ui_file_style
&style
,
231 const char *format
, va_list args
)
233 if (m_suppress_output
)
236 std::string str
= string_vprintf (format
, args
);
239 ui_file
*stream
= m_streams
.back ();
240 stream
->emit_style_escape (style
);
241 if (test_flags (unfiltered_output
))
242 stream
->puts_unfiltered (str
.c_str ());
244 stream
->puts (str
.c_str ());
245 stream
->emit_style_escape (ui_file_style ());
250 cli_ui_out::do_wrap_hint (int indent
)
252 if (m_suppress_output
)
255 m_streams
.back ()->wrap_here (indent
);
259 cli_ui_out::do_flush ()
261 gdb_flush (m_streams
.back ());
264 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
265 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
266 output stream; it is an internal error if it does not exist. */
269 cli_ui_out::do_redirect (ui_file
*outstream
)
271 if (outstream
!= NULL
)
272 m_streams
.push_back (outstream
);
274 m_streams
.pop_back ();
277 /* The cli_ui_out::do_progress_* functions result in the following:
278 - printed for tty, SHOULD_PRINT == true:
281 - printed for tty, SHOULD_PRINT == false:
283 - printed for not-a-tty:
289 cli_ui_out::do_progress_start (const std::string
&name
, bool should_print
)
291 struct ui_file
*stream
= m_streams
.back ();
292 cli_progress_info meter
;
294 meter
.last_value
= 0;
296 if (!stream
->isatty ())
298 fprintf_unfiltered (stream
, "%s...", meter
.name
.c_str ());
300 meter
.printing
= WORKING
;
304 /* Don't actually emit anything until the first call notifies us
305 of progress. This makes it so a second progress message can
306 be started before the first one has been notified, without
308 meter
.printing
= should_print
? START
: NO_PRINT
;
311 m_meters
.push_back (std::move (meter
));
315 cli_ui_out::do_progress_notify (double howmuch
)
317 struct ui_file
*stream
= m_streams
.back ();
318 cli_progress_info
&meter (m_meters
.back ());
320 if (meter
.printing
== NO_PRINT
)
323 if (meter
.printing
== START
)
325 fprintf_unfiltered (stream
, "%s\n", meter
.name
.c_str ());
327 meter
.printing
= WORKING
;
330 if (meter
.printing
== WORKING
&& howmuch
>= 1.0)
333 if (!stream
->isatty ())
336 int chars_per_line
= get_chars_per_line ();
337 if (chars_per_line
> 0)
340 int width
= chars_per_line
- 3;
342 max
= width
* howmuch
;
343 fprintf_unfiltered (stream
, "\r[");
344 for (i
= 0; i
< width
; ++i
)
345 fprintf_unfiltered (stream
, i
< max
? "#" : " ");
346 fprintf_unfiltered (stream
, "]");
348 meter
.printing
= PROGRESS
;
353 cli_ui_out::do_progress_end ()
355 struct ui_file
*stream
= m_streams
.back ();
356 cli_progress_info
&meter
= m_meters
.back ();
358 if (!stream
->isatty ())
360 fprintf_unfiltered (stream
, "\n");
363 else if (meter
.printing
== PROGRESS
)
366 int width
= get_chars_per_line () - 3;
368 fprintf_unfiltered (stream
, "\r");
369 for (i
= 0; i
< width
+ 2; ++i
)
370 fprintf_unfiltered (stream
, " ");
371 fprintf_unfiltered (stream
, "\r");
375 m_meters
.pop_back ();
378 /* local functions */
381 cli_ui_out::field_separator ()
383 if (test_flags (unfiltered_output
))
384 fputc_unfiltered (' ', m_streams
.back ());
386 fputc_filtered (' ', m_streams
.back ());
389 /* Constructor for cli_ui_out. */
391 cli_ui_out::cli_ui_out (ui_file
*stream
, ui_out_flags flags
)
393 m_suppress_output (false)
395 gdb_assert (stream
!= NULL
);
397 m_streams
.push_back (stream
);
400 cli_ui_out::~cli_ui_out ()
404 /* Initialize private members at startup. */
407 cli_out_new (struct ui_file
*stream
)
409 return new cli_ui_out (stream
, ui_source_list
);
413 cli_ui_out::set_stream (struct ui_file
*stream
)
417 old
= m_streams
.back ();
418 m_streams
.back () = stream
;
424 cli_ui_out::can_emit_style_escape () const
426 return m_streams
.back ()->can_emit_style_escape ();
429 /* CLI interface to display tab-completion matches. */
431 /* CLI version of displayer.crlf. */
434 cli_mld_crlf (const struct match_list_displayer
*displayer
)
439 /* CLI version of displayer.putch. */
442 cli_mld_putch (const struct match_list_displayer
*displayer
, int ch
)
444 putc (ch
, rl_outstream
);
447 /* CLI version of displayer.puts. */
450 cli_mld_puts (const struct match_list_displayer
*displayer
, const char *s
)
452 fputs (s
, rl_outstream
);
455 /* CLI version of displayer.flush. */
458 cli_mld_flush (const struct match_list_displayer
*displayer
)
460 fflush (rl_outstream
);
463 EXTERN_C
void _rl_erase_entire_line (void);
465 /* CLI version of displayer.erase_entire_line. */
468 cli_mld_erase_entire_line (const struct match_list_displayer
*displayer
)
470 _rl_erase_entire_line ();
473 /* CLI version of displayer.beep. */
476 cli_mld_beep (const struct match_list_displayer
*displayer
)
481 /* CLI version of displayer.read_key. */
484 cli_mld_read_key (const struct match_list_displayer
*displayer
)
486 return rl_read_key ();
489 /* CLI version of rl_completion_display_matches_hook.
490 See gdb_display_match_list for a description of the arguments. */
493 cli_display_match_list (char **matches
, int len
, int max
)
495 struct match_list_displayer displayer
;
497 rl_get_screen_size (&displayer
.height
, &displayer
.width
);
498 displayer
.crlf
= cli_mld_crlf
;
499 displayer
.putch
= cli_mld_putch
;
500 displayer
.puts
= cli_mld_puts
;
501 displayer
.flush
= cli_mld_flush
;
502 displayer
.erase_entire_line
= cli_mld_erase_entire_line
;
503 displayer
.beep
= cli_mld_beep
;
504 displayer
.read_key
= cli_mld_read_key
;
506 gdb_display_match_list (matches
, len
, max
, &displayer
);
507 rl_forced_update_display ();