1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999-2017 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"
29 /* These are the CLI output functions */
31 /* Mark beginning of a table */
34 cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
37 m_suppress_output = true;
39 /* Only the table suppresses the output and, fortunately, a table
40 is not a recursive data structure. */
41 gdb_assert (!m_suppress_output);
44 /* Mark beginning of a table body */
47 cli_ui_out::do_table_body ()
49 if (m_suppress_output)
52 /* first, close the table header line */
56 /* Mark end of a table */
59 cli_ui_out::do_table_end ()
61 m_suppress_output = false;
64 /* Specify table header */
67 cli_ui_out::do_table_header (int width, ui_align alignment,
68 const std::string &col_name,
69 const std::string &col_hdr)
71 if (m_suppress_output)
74 do_field_string (0, width, alignment, 0, col_hdr.c_str ());
77 /* Mark beginning of a list */
80 cli_ui_out::do_begin (ui_out_type type, const char *id)
84 /* Mark end of a list */
87 cli_ui_out::do_end (ui_out_type type)
91 /* output an int field */
94 cli_ui_out::do_field_int (int fldno, int width, ui_align alignment,
95 const char *fldname, int value)
97 char buffer[20]; /* FIXME: how many chars long a %d can become? */
99 if (m_suppress_output)
102 xsnprintf (buffer, sizeof (buffer), "%d", value);
104 do_field_string (fldno, width, alignment, fldname, buffer);
107 /* used to omit a field */
110 cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
113 if (m_suppress_output)
116 do_field_string (fldno, width, alignment, fldname, "");
119 /* other specific cli_field_* end up here so alignment and field
120 separators are both handled by cli_field_string */
123 cli_ui_out::do_field_string (int fldno, int width, ui_align align,
124 const char *fldname, const char *string)
129 if (m_suppress_output)
132 if ((align != ui_noalign) && string)
134 before = width - strlen (string);
139 if (align == ui_right)
141 else if (align == ui_left)
159 out_field_fmt (fldno, fldname, "%s", string);
164 if (align != ui_noalign)
168 /* This is the only field function that does not align. */
171 cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
172 const char *fldname, const char *format,
175 if (m_suppress_output)
178 vfprintf_filtered (m_streams.back (), format, args);
180 if (align != ui_noalign)
185 cli_ui_out::do_spaces (int numspaces)
187 if (m_suppress_output)
190 print_spaces_filtered (numspaces, m_streams.back ());
194 cli_ui_out::do_text (const char *string)
196 if (m_suppress_output)
199 fputs_filtered (string, m_streams.back ());
203 cli_ui_out::do_message (const char *format, va_list args)
205 if (m_suppress_output)
208 vfprintf_unfiltered (m_streams.back (), format, args);
212 cli_ui_out::do_wrap_hint (const char *identstring)
214 if (m_suppress_output)
217 wrap_here (identstring);
221 cli_ui_out::do_flush ()
223 gdb_flush (m_streams.back ());
226 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
227 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
228 output stream; it is an internal error if it does not exist. */
231 cli_ui_out::do_redirect (ui_file *outstream)
233 if (outstream != NULL)
234 m_streams.push_back (outstream);
236 m_streams.pop_back ();
239 /* local functions */
241 /* Like cli_ui_out::do_field_fmt, but takes a variable number of args
242 and makes a va_list and does not insert a separator. */
246 cli_ui_out::out_field_fmt (int fldno, const char *fldname,
247 const char *format, ...)
251 va_start (args, format);
252 vfprintf_filtered (m_streams.back (), format, args);
258 cli_ui_out::field_separator ()
260 fputc_filtered (' ', m_streams.back ());
263 /* Constructor for cli_ui_out. */
265 cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags)
267 m_suppress_output (false)
269 gdb_assert (stream != NULL);
271 m_streams.push_back (stream);
274 cli_ui_out::~cli_ui_out ()
278 /* Initialize private members at startup. */
281 cli_out_new (struct ui_file *stream)
283 return new cli_ui_out (stream, ui_source_list);
287 cli_ui_out::set_stream (struct ui_file *stream)
291 old = m_streams.back ();
292 m_streams.back () = stream;
297 /* CLI interface to display tab-completion matches. */
299 /* CLI version of displayer.crlf. */
302 cli_mld_crlf (const struct match_list_displayer *displayer)
307 /* CLI version of displayer.putch. */
310 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
312 putc (ch, rl_outstream);
315 /* CLI version of displayer.puts. */
318 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
320 fputs (s, rl_outstream);
323 /* CLI version of displayer.flush. */
326 cli_mld_flush (const struct match_list_displayer *displayer)
328 fflush (rl_outstream);
331 EXTERN_C void _rl_erase_entire_line (void);
333 /* CLI version of displayer.erase_entire_line. */
336 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
338 _rl_erase_entire_line ();
341 /* CLI version of displayer.beep. */
344 cli_mld_beep (const struct match_list_displayer *displayer)
349 /* CLI version of displayer.read_key. */
352 cli_mld_read_key (const struct match_list_displayer *displayer)
354 return rl_read_key ();
357 /* CLI version of rl_completion_display_matches_hook.
358 See gdb_display_match_list for a description of the arguments. */
361 cli_display_match_list (char **matches, int len, int max)
363 struct match_list_displayer displayer;
365 rl_get_screen_size (&displayer.height, &displayer.width);
366 displayer.crlf = cli_mld_crlf;
367 displayer.putch = cli_mld_putch;
368 displayer.puts = cli_mld_puts;
369 displayer.flush = cli_mld_flush;
370 displayer.erase_entire_line = cli_mld_erase_entire_line;
371 displayer.beep = cli_mld_beep;
372 displayer.read_key = cli_mld_read_key;
374 gdb_display_match_list (matches, len, max, &displayer);
375 rl_forced_update_display ();