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,
116 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
118 if (data->suppress_output)
122 /* Mark end of a list */
125 cli_end (struct ui_out *uiout,
126 enum ui_out_type type)
128 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
130 if (data->suppress_output)
134 /* output an int field */
137 cli_field_int (struct ui_out *uiout, int fldno, int width,
138 enum ui_align alignment,
139 const char *fldname, int value)
141 char buffer[20]; /* FIXME: how many chars long a %d can become? */
142 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
144 if (data->suppress_output)
146 xsnprintf (buffer, sizeof (buffer), "%d", value);
148 /* Always go through the function pointer (virtual function call).
149 We may have been extended. */
150 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
153 /* used to ommit a field */
156 cli_field_skip (struct ui_out *uiout, int fldno, int width,
157 enum ui_align alignment,
160 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
162 if (data->suppress_output)
165 /* Always go through the function pointer (virtual function call).
166 We may have been extended. */
167 uo_field_string (uiout, fldno, width, alignment, fldname, "");
170 /* other specific cli_field_* end up here so alignment and field
171 separators are both handled by cli_field_string */
174 cli_field_string (struct ui_out *uiout,
183 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
185 if (data->suppress_output)
188 if ((align != ui_noalign) && string)
190 before = width - strlen (string);
195 if (align == ui_right)
197 else if (align == ui_left)
212 ui_out_spaces (uiout, before);
214 out_field_fmt (uiout, fldno, fldname, "%s", string);
216 ui_out_spaces (uiout, after);
218 if (align != ui_noalign)
222 /* This is the only field function that does not align. */
224 static void ATTRIBUTE_PRINTF (6, 0)
225 cli_field_fmt (struct ui_out *uiout, int fldno,
226 int width, enum ui_align align,
231 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
232 struct ui_file *stream;
234 if (data->suppress_output)
237 stream = data->streams.back ();
238 vfprintf_filtered (stream, format, args);
240 if (align != ui_noalign)
245 cli_spaces (struct ui_out *uiout, int numspaces)
247 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
248 struct ui_file *stream;
250 if (data->suppress_output)
253 stream = data->streams.back ();
254 print_spaces_filtered (numspaces, stream);
258 cli_text (struct ui_out *uiout, const char *string)
260 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
261 struct ui_file *stream;
263 if (data->suppress_output)
266 stream = data->streams.back ();
267 fputs_filtered (string, stream);
270 static void ATTRIBUTE_PRINTF (2, 0)
271 cli_message (struct ui_out *uiout, const char *format, va_list args)
273 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
275 if (data->suppress_output)
278 struct ui_file *stream = data->streams.back ();
279 vfprintf_unfiltered (stream, format, args);
283 cli_wrap_hint (struct ui_out *uiout, const char *identstring)
285 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
287 if (data->suppress_output)
289 wrap_here (identstring);
293 cli_flush (struct ui_out *uiout)
295 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
296 struct ui_file *stream = data->streams.back ();
301 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
302 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
303 output stream; it is an internal error if it does not exist. */
306 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
308 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
310 if (outstream != NULL)
311 data->streams.push_back (outstream);
313 data->streams.pop_back ();
318 /* local functions */
320 /* Like cli_field_fmt, but takes a variable number of args
321 and makes a va_list and does not insert a separator. */
325 out_field_fmt (struct ui_out *uiout, int fldno,
327 const char *format,...)
329 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
330 struct ui_file *stream = data->streams.back ();
333 va_start (args, format);
334 vfprintf_filtered (stream, format, args);
339 /* Access to ui_out format private members. */
342 field_separator (void)
344 cli_out_data *data = (cli_out_data *) ui_out_data (current_uiout);
345 struct ui_file *stream = data->streams.back ();
347 fputc_filtered (' ', stream);
350 /* This is the CLI ui-out implementation functions vector */
352 const struct ui_out_impl cli_ui_out_impl =
371 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
374 /* Constructor for a `cli_out_data' object. */
377 cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
379 gdb_assert (stream != NULL);
381 self->streams.push_back (stream);
383 self->suppress_output = 0;
386 /* Initialize private members at startup. */
389 cli_out_new (struct ui_file *stream)
391 ui_out_flags flags = ui_source_list;
392 cli_out_data *data = new cli_out_data ();
394 cli_out_data_ctor (data, stream);
395 return ui_out_new (&cli_ui_out_impl, data, flags);
399 cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
401 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
404 old = data->streams.back ();
405 data->streams.back () = stream;
410 /* CLI interface to display tab-completion matches. */
412 /* CLI version of displayer.crlf. */
415 cli_mld_crlf (const struct match_list_displayer *displayer)
420 /* CLI version of displayer.putch. */
423 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
425 putc (ch, rl_outstream);
428 /* CLI version of displayer.puts. */
431 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
433 fputs (s, rl_outstream);
436 /* CLI version of displayer.flush. */
439 cli_mld_flush (const struct match_list_displayer *displayer)
441 fflush (rl_outstream);
444 EXTERN_C void _rl_erase_entire_line (void);
446 /* CLI version of displayer.erase_entire_line. */
449 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
451 _rl_erase_entire_line ();
454 /* CLI version of displayer.beep. */
457 cli_mld_beep (const struct match_list_displayer *displayer)
462 /* CLI version of displayer.read_key. */
465 cli_mld_read_key (const struct match_list_displayer *displayer)
467 return rl_read_key ();
470 /* CLI version of rl_completion_display_matches_hook.
471 See gdb_display_match_list for a description of the arguments. */
474 cli_display_match_list (char **matches, int len, int max)
476 struct match_list_displayer displayer;
478 rl_get_screen_size (&displayer.height, &displayer.width);
479 displayer.crlf = cli_mld_crlf;
480 displayer.putch = cli_mld_putch;
481 displayer.puts = cli_mld_puts;
482 displayer.flush = cli_mld_flush;
483 displayer.erase_entire_line = cli_mld_erase_entire_line;
484 displayer.beep = cli_mld_beep;
485 displayer.read_key = cli_mld_read_key;
487 gdb_display_match_list (matches, len, max, &displayer);
488 rl_forced_update_display ();