Automatic date update in version.in
[binutils-gdb.git] / gdb / cli-out.c
blobfdbed6f5e91feb2d1950d38880ef6744e93ad55b
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/>. */
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "cli-out.h"
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 */
34 void
35 cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
37 if (nr_rows == 0)
38 m_suppress_output = true;
39 else
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 */
47 void
48 cli_ui_out::do_table_body ()
50 if (m_suppress_output)
51 return;
53 /* first, close the table header line */
54 text ("\n");
57 /* Mark end of a table */
59 void
60 cli_ui_out::do_table_end ()
62 m_suppress_output = false;
65 /* Specify table header */
67 void
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)
73 return;
75 do_field_string (0, width, alignment, 0, col_hdr.c_str (),
76 ui_file_style ());
79 /* Mark beginning of a list */
81 void
82 cli_ui_out::do_begin (ui_out_type type, const char *id)
86 /* Mark end of a list */
88 void
89 cli_ui_out::do_end (ui_out_type type)
93 /* output an int field */
95 void
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)
100 return;
102 do_field_string (fldno, width, alignment, fldname, plongest (value),
103 ui_file_style ());
106 /* output an unsigned field */
108 void
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)
113 return;
115 do_field_string (fldno, width, alignment, fldname, pulongest (value),
116 ui_file_style ());
119 /* used to omit a field */
121 void
122 cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
123 const char *fldname)
125 if (m_suppress_output)
126 return;
128 do_field_string (fldno, width, alignment, fldname, "",
129 ui_file_style ());
132 /* other specific cli_field_* end up here so alignment and field
133 separators are both handled by cli_field_string */
135 void
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)
140 int before = 0;
141 int after = 0;
143 if (m_suppress_output)
144 return;
146 if ((align != ui_noalign) && string)
148 before = width - strlen (string);
149 if (before <= 0)
150 before = 0;
151 else
153 if (align == ui_right)
154 after = 0;
155 else if (align == ui_left)
157 after = before;
158 before = 0;
160 else
161 /* ui_center */
163 after = before / 2;
164 before -= after;
169 if (before)
170 spaces (before);
172 if (string)
174 ui_file *stream = m_streams.back ();
175 stream->emit_style_escape (style);
176 stream->puts (string);
177 stream->emit_style_escape (ui_file_style ());
180 if (after)
181 spaces (after);
183 if (align != ui_noalign)
184 field_separator ();
187 /* Output field containing ARGS using printf formatting in FORMAT. */
189 void
190 cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
191 const char *fldname, const ui_file_style &style,
192 const char *format, va_list args)
194 if (m_suppress_output)
195 return;
197 std::string str = string_vprintf (format, args);
199 do_field_string (fldno, width, align, fldname, str.c_str (), style);
202 void
203 cli_ui_out::do_spaces (int numspaces)
205 if (m_suppress_output)
206 return;
208 print_spaces (numspaces, m_streams.back ());
211 void
212 cli_ui_out::do_text (const char *string)
214 if (m_suppress_output)
215 return;
217 gdb_puts (string, m_streams.back ());
220 void
221 cli_ui_out::do_message (const ui_file_style &style,
222 const char *format, va_list args)
224 if (m_suppress_output)
225 return;
227 std::string str = string_vprintf (format, args);
228 if (!str.empty ())
230 ui_file *stream = m_streams.back ();
231 stream->emit_style_escape (style);
232 stream->puts (str.c_str ());
233 stream->emit_style_escape (ui_file_style ());
237 void
238 cli_ui_out::do_wrap_hint (int indent)
240 if (m_suppress_output)
241 return;
243 m_streams.back ()->wrap_here (indent);
246 void
247 cli_ui_out::do_flush ()
249 gdb_flush (m_streams.back ());
252 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
253 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
254 output stream; it is an internal error if it does not exist. */
256 void
257 cli_ui_out::do_redirect (ui_file *outstream)
259 if (outstream != NULL)
260 m_streams.push_back (outstream);
261 else
262 m_streams.pop_back ();
265 /* The cli_ui_out::do_progress_* functions result in the following:
266 - printed for tty, SHOULD_PRINT == true:
267 <NAME
268 [##### ]\r>
269 - printed for tty, SHOULD_PRINT == false:
271 - printed for not-a-tty:
272 <NAME...
276 void
277 cli_ui_out::do_progress_start (const std::string &name, bool should_print)
279 struct ui_file *stream = m_streams.back ();
280 cli_progress_info meter;
282 meter.last_value = 0;
283 meter.name = name;
284 if (!stream->isatty ())
286 gdb_printf (stream, "%s...", meter.name.c_str ());
287 gdb_flush (stream);
288 meter.printing = WORKING;
290 else
292 /* Don't actually emit anything until the first call notifies us
293 of progress. This makes it so a second progress message can
294 be started before the first one has been notified, without
295 messy output. */
296 meter.printing = should_print ? START : NO_PRINT;
299 m_meters.push_back (std::move (meter));
302 void
303 cli_ui_out::do_progress_notify (double howmuch)
305 struct ui_file *stream = m_streams.back ();
306 cli_progress_info &meter (m_meters.back ());
308 if (meter.printing == NO_PRINT)
309 return;
311 if (meter.printing == START)
313 gdb_printf (stream, "%s\n", meter.name.c_str ());
314 gdb_flush (stream);
315 meter.printing = WORKING;
318 if (meter.printing == WORKING && howmuch >= 1.0)
319 return;
321 if (!stream->isatty ())
322 return;
324 int chars_per_line = get_chars_per_line ();
325 if (chars_per_line > 0)
327 int i, max;
328 int width = chars_per_line - 3;
330 max = width * howmuch;
331 gdb_printf (stream, "\r[");
332 for (i = 0; i < width; ++i)
333 gdb_printf (stream, i < max ? "#" : " ");
334 gdb_printf (stream, "]");
335 gdb_flush (stream);
336 meter.printing = PROGRESS;
340 void
341 cli_ui_out::do_progress_end ()
343 struct ui_file *stream = m_streams.back ();
344 cli_progress_info &meter = m_meters.back ();
346 if (!stream->isatty ())
348 gdb_printf (stream, "\n");
349 gdb_flush (stream);
351 else if (meter.printing == PROGRESS)
353 int i;
354 int width = get_chars_per_line () - 3;
356 gdb_printf (stream, "\r");
357 for (i = 0; i < width + 2; ++i)
358 gdb_printf (stream, " ");
359 gdb_printf (stream, "\r");
360 gdb_flush (stream);
363 m_meters.pop_back ();
366 /* local functions */
368 void
369 cli_ui_out::field_separator ()
371 gdb_putc (' ', m_streams.back ());
374 /* Constructor for cli_ui_out. */
376 cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags)
377 : ui_out (flags),
378 m_suppress_output (false)
380 gdb_assert (stream != NULL);
382 m_streams.push_back (stream);
385 cli_ui_out::~cli_ui_out ()
389 ui_file *
390 cli_ui_out::set_stream (struct ui_file *stream)
392 ui_file *old;
394 old = m_streams.back ();
395 m_streams.back () = stream;
397 return old;
400 bool
401 cli_ui_out::can_emit_style_escape () const
403 return m_streams.back ()->can_emit_style_escape ();
406 /* CLI interface to display tab-completion matches. */
408 /* CLI version of displayer.crlf. */
410 static void
411 cli_mld_crlf (const struct match_list_displayer *displayer)
413 rl_crlf ();
416 /* CLI version of displayer.putch. */
418 static void
419 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
421 putc (ch, rl_outstream);
424 /* CLI version of displayer.puts. */
426 static void
427 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
429 fputs (s, rl_outstream);
432 /* CLI version of displayer.flush. */
434 static void
435 cli_mld_flush (const struct match_list_displayer *displayer)
437 fflush (rl_outstream);
440 EXTERN_C void _rl_erase_entire_line (void);
442 /* CLI version of displayer.erase_entire_line. */
444 static void
445 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
447 _rl_erase_entire_line ();
450 /* CLI version of displayer.beep. */
452 static void
453 cli_mld_beep (const struct match_list_displayer *displayer)
455 rl_ding ();
458 /* CLI version of displayer.read_key. */
460 static int
461 cli_mld_read_key (const struct match_list_displayer *displayer)
463 return rl_read_key ();
466 /* CLI version of rl_completion_display_matches_hook.
467 See gdb_display_match_list for a description of the arguments. */
469 void
470 cli_display_match_list (char **matches, int len, int max)
472 struct match_list_displayer displayer;
474 rl_get_screen_size (&displayer.height, &displayer.width);
475 displayer.crlf = cli_mld_crlf;
476 displayer.putch = cli_mld_putch;
477 displayer.puts = cli_mld_puts;
478 displayer.flush = cli_mld_flush;
479 displayer.erase_entire_line = cli_mld_erase_entire_line;
480 displayer.beep = cli_mld_beep;
481 displayer.read_key = cli_mld_read_key;
483 gdb_display_match_list (matches, len, max, &displayer);
484 rl_forced_update_display ();