1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999-2016 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 typedef struct cli_ui_out_data cli_out_data;
31 /* Prototypes for local functions */
33 static void cli_text (struct ui_out *uiout, const char *string);
35 static void field_separator (void);
37 static void out_field_fmt (struct ui_out *uiout, int fldno,
39 const char *format,...) ATTRIBUTE_PRINTF (4, 5);
44 cli_uiout_dtor (struct ui_out *ui_out)
46 cli_out_data *data = (cli_out_data *) ui_out_data (ui_out);
51 /* These are the CLI output functions */
53 /* Mark beginning of a table */
56 cli_table_begin (struct ui_out *uiout, int nbrofcols,
60 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
63 data->suppress_output = 1;
65 /* Only the table suppresses the output and, fortunately, a table
66 is not a recursive data structure. */
67 gdb_assert (data->suppress_output == 0);
70 /* Mark beginning of a table body */
73 cli_table_body (struct ui_out *uiout)
75 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
77 if (data->suppress_output)
79 /* first, close the table header line */
80 cli_text (uiout, "\n");
83 /* Mark end of a table */
86 cli_table_end (struct ui_out *uiout)
88 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
90 data->suppress_output = 0;
93 /* Specify table header */
96 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
97 const std::string &col_name, const std::string &col_hdr)
99 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
101 if (data->suppress_output)
104 /* Always go through the function pointer (virtual function call).
105 We may have been extended. */
106 uo_field_string (uiout, 0, width, alignment, 0, col_hdr.c_str ());
109 /* Mark beginning of a list */
112 cli_begin (struct ui_out *uiout,
113 enum ui_out_type type,
117 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
119 if (data->suppress_output)
123 /* Mark end of a list */
126 cli_end (struct ui_out *uiout,
127 enum ui_out_type type,
130 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
132 if (data->suppress_output)
136 /* output an int field */
139 cli_field_int (struct ui_out *uiout, int fldno, int width,
140 enum ui_align alignment,
141 const char *fldname, int value)
143 char buffer[20]; /* FIXME: how many chars long a %d can become? */
144 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
146 if (data->suppress_output)
148 xsnprintf (buffer, sizeof (buffer), "%d", value);
150 /* Always go through the function pointer (virtual function call).
151 We may have been extended. */
152 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
155 /* used to ommit a field */
158 cli_field_skip (struct ui_out *uiout, int fldno, int width,
159 enum ui_align alignment,
162 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
164 if (data->suppress_output)
167 /* Always go through the function pointer (virtual function call).
168 We may have been extended. */
169 uo_field_string (uiout, fldno, width, alignment, fldname, "");
172 /* other specific cli_field_* end up here so alignment and field
173 separators are both handled by cli_field_string */
176 cli_field_string (struct ui_out *uiout,
185 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
187 if (data->suppress_output)
190 if ((align != ui_noalign) && string)
192 before = width - strlen (string);
197 if (align == ui_right)
199 else if (align == ui_left)
214 ui_out_spaces (uiout, before);
216 out_field_fmt (uiout, fldno, fldname, "%s", string);
218 ui_out_spaces (uiout, after);
220 if (align != ui_noalign)
224 /* This is the only field function that does not align. */
226 static void ATTRIBUTE_PRINTF (6, 0)
227 cli_field_fmt (struct ui_out *uiout, int fldno,
228 int width, enum ui_align align,
233 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
234 struct ui_file *stream;
236 if (data->suppress_output)
239 stream = data->streams.back ();
240 vfprintf_filtered (stream, format, args);
242 if (align != ui_noalign)
247 cli_spaces (struct ui_out *uiout, int numspaces)
249 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
250 struct ui_file *stream;
252 if (data->suppress_output)
255 stream = data->streams.back ();
256 print_spaces_filtered (numspaces, stream);
260 cli_text (struct ui_out *uiout, const char *string)
262 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
263 struct ui_file *stream;
265 if (data->suppress_output)
268 stream = data->streams.back ();
269 fputs_filtered (string, stream);
272 static void ATTRIBUTE_PRINTF (2, 0)
273 cli_message (struct ui_out *uiout, const char *format, va_list args)
275 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
277 if (data->suppress_output)
280 struct ui_file *stream = data->streams.back ();
281 vfprintf_unfiltered (stream, format, args);
285 cli_wrap_hint (struct ui_out *uiout, const char *identstring)
287 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
289 if (data->suppress_output)
291 wrap_here (identstring);
295 cli_flush (struct ui_out *uiout)
297 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
298 struct ui_file *stream = data->streams.back ();
303 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
304 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
305 output stream; it is an internal error if it does not exist. */
308 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
310 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
312 if (outstream != NULL)
313 data->streams.push_back (outstream);
315 data->streams.pop_back ();
320 /* local functions */
322 /* Like cli_field_fmt, but takes a variable number of args
323 and makes a va_list and does not insert a separator. */
327 out_field_fmt (struct ui_out *uiout, int fldno,
329 const char *format,...)
331 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
332 struct ui_file *stream = data->streams.back ();
335 va_start (args, format);
336 vfprintf_filtered (stream, format, args);
341 /* Access to ui_out format private members. */
344 field_separator (void)
346 cli_out_data *data = (cli_out_data *) ui_out_data (current_uiout);
347 struct ui_file *stream = data->streams.back ();
349 fputc_filtered (' ', stream);
352 /* This is the CLI ui-out implementation functions vector */
354 const struct ui_out_impl cli_ui_out_impl =
373 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
376 /* Constructor for a `cli_out_data' object. */
379 cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
381 gdb_assert (stream != NULL);
383 self->streams.push_back (stream);
385 self->suppress_output = 0;
388 /* Initialize private members at startup. */
391 cli_out_new (struct ui_file *stream)
393 int flags = ui_source_list;
394 cli_out_data *data = new cli_out_data ();
396 cli_out_data_ctor (data, stream);
397 return ui_out_new (&cli_ui_out_impl, data, flags);
401 cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
403 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
406 old = data->streams.back ();
407 data->streams.back () = stream;
412 /* CLI interface to display tab-completion matches. */
414 /* CLI version of displayer.crlf. */
417 cli_mld_crlf (const struct match_list_displayer *displayer)
422 /* CLI version of displayer.putch. */
425 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
427 putc (ch, rl_outstream);
430 /* CLI version of displayer.puts. */
433 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
435 fputs (s, rl_outstream);
438 /* CLI version of displayer.flush. */
441 cli_mld_flush (const struct match_list_displayer *displayer)
443 fflush (rl_outstream);
446 EXTERN_C void _rl_erase_entire_line (void);
448 /* CLI version of displayer.erase_entire_line. */
451 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
453 _rl_erase_entire_line ();
456 /* CLI version of displayer.beep. */
459 cli_mld_beep (const struct match_list_displayer *displayer)
464 /* CLI version of displayer.read_key. */
467 cli_mld_read_key (const struct match_list_displayer *displayer)
469 return rl_read_key ();
472 /* CLI version of rl_completion_display_matches_hook.
473 See gdb_display_match_list for a description of the arguments. */
476 cli_display_match_list (char **matches, int len, int max)
478 struct match_list_displayer displayer;
480 rl_get_screen_size (&displayer.height, &displayer.width);
481 displayer.crlf = cli_mld_crlf;
482 displayer.putch = cli_mld_putch;
483 displayer.puts = cli_mld_puts;
484 displayer.flush = cli_mld_flush;
485 displayer.erase_entire_line = cli_mld_erase_entire_line;
486 displayer.beep = cli_mld_beep;
487 displayer.read_key = cli_mld_read_key;
489 gdb_display_match_list (matches, len, max, &displayer);
490 rl_forced_update_display ();