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,
100 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
102 if (data->suppress_output)
105 /* Always go through the function pointer (virtual function call).
106 We may have been extended. */
107 uo_field_string (uiout, 0, width, alignment, 0, colhdr);
110 /* Mark beginning of a list */
113 cli_begin (struct ui_out *uiout,
114 enum ui_out_type type,
118 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
120 if (data->suppress_output)
124 /* Mark end of a list */
127 cli_end (struct ui_out *uiout,
128 enum ui_out_type type,
131 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
133 if (data->suppress_output)
137 /* output an int field */
140 cli_field_int (struct ui_out *uiout, int fldno, int width,
141 enum ui_align alignment,
142 const char *fldname, int value)
144 char buffer[20]; /* FIXME: how many chars long a %d can become? */
145 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
147 if (data->suppress_output)
149 xsnprintf (buffer, sizeof (buffer), "%d", value);
151 /* Always go through the function pointer (virtual function call).
152 We may have been extended. */
153 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
156 /* used to ommit a field */
159 cli_field_skip (struct ui_out *uiout, int fldno, int width,
160 enum ui_align alignment,
163 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
165 if (data->suppress_output)
168 /* Always go through the function pointer (virtual function call).
169 We may have been extended. */
170 uo_field_string (uiout, fldno, width, alignment, fldname, "");
173 /* other specific cli_field_* end up here so alignment and field
174 separators are both handled by cli_field_string */
177 cli_field_string (struct ui_out *uiout,
186 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
188 if (data->suppress_output)
191 if ((align != ui_noalign) && string)
193 before = width - strlen (string);
198 if (align == ui_right)
200 else if (align == ui_left)
215 ui_out_spaces (uiout, before);
217 out_field_fmt (uiout, fldno, fldname, "%s", string);
219 ui_out_spaces (uiout, after);
221 if (align != ui_noalign)
225 /* This is the only field function that does not align. */
227 static void ATTRIBUTE_PRINTF (6, 0)
228 cli_field_fmt (struct ui_out *uiout, int fldno,
229 int width, enum ui_align align,
234 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
235 struct ui_file *stream;
237 if (data->suppress_output)
240 stream = data->streams.back ();
241 vfprintf_filtered (stream, format, args);
243 if (align != ui_noalign)
248 cli_spaces (struct ui_out *uiout, int numspaces)
250 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
251 struct ui_file *stream;
253 if (data->suppress_output)
256 stream = data->streams.back ();
257 print_spaces_filtered (numspaces, stream);
261 cli_text (struct ui_out *uiout, const char *string)
263 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
264 struct ui_file *stream;
266 if (data->suppress_output)
269 stream = data->streams.back ();
270 fputs_filtered (string, stream);
273 static void ATTRIBUTE_PRINTF (2, 0)
274 cli_message (struct ui_out *uiout, const char *format, va_list args)
276 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
278 if (data->suppress_output)
281 struct ui_file *stream = data->streams.back ();
282 vfprintf_unfiltered (stream, format, args);
286 cli_wrap_hint (struct ui_out *uiout, const char *identstring)
288 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
290 if (data->suppress_output)
292 wrap_here (identstring);
296 cli_flush (struct ui_out *uiout)
298 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
299 struct ui_file *stream = data->streams.back ();
304 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
305 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
306 output stream; it is an internal error if it does not exist. */
309 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
311 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
313 if (outstream != NULL)
314 data->streams.push_back (outstream);
316 data->streams.pop_back ();
321 /* local functions */
323 /* Like cli_field_fmt, but takes a variable number of args
324 and makes a va_list and does not insert a separator. */
328 out_field_fmt (struct ui_out *uiout, int fldno,
330 const char *format,...)
332 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
333 struct ui_file *stream = data->streams.back ();
336 va_start (args, format);
337 vfprintf_filtered (stream, format, args);
342 /* Access to ui_out format private members. */
345 field_separator (void)
347 cli_out_data *data = (cli_out_data *) ui_out_data (current_uiout);
348 struct ui_file *stream = data->streams.back ();
350 fputc_filtered (' ', stream);
353 /* This is the CLI ui-out implementation functions vector */
355 const struct ui_out_impl cli_ui_out_impl =
374 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
377 /* Constructor for a `cli_out_data' object. */
380 cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
382 gdb_assert (stream != NULL);
384 self->streams.push_back (stream);
386 self->suppress_output = 0;
389 /* Initialize private members at startup. */
392 cli_out_new (struct ui_file *stream)
394 int flags = ui_source_list;
395 cli_out_data *data = new cli_out_data ();
397 cli_out_data_ctor (data, stream);
398 return ui_out_new (&cli_ui_out_impl, data, flags);
402 cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
404 cli_out_data *data = (cli_out_data *) ui_out_data (uiout);
407 old = data->streams.back ();
408 data->streams.back () = stream;
413 /* CLI interface to display tab-completion matches. */
415 /* CLI version of displayer.crlf. */
418 cli_mld_crlf (const struct match_list_displayer *displayer)
423 /* CLI version of displayer.putch. */
426 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
428 putc (ch, rl_outstream);
431 /* CLI version of displayer.puts. */
434 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
436 fputs (s, rl_outstream);
439 /* CLI version of displayer.flush. */
442 cli_mld_flush (const struct match_list_displayer *displayer)
444 fflush (rl_outstream);
447 EXTERN_C void _rl_erase_entire_line (void);
449 /* CLI version of displayer.erase_entire_line. */
452 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
454 _rl_erase_entire_line ();
457 /* CLI version of displayer.beep. */
460 cli_mld_beep (const struct match_list_displayer *displayer)
465 /* CLI version of displayer.read_key. */
468 cli_mld_read_key (const struct match_list_displayer *displayer)
470 return rl_read_key ();
473 /* CLI version of rl_completion_display_matches_hook.
474 See gdb_display_match_list for a description of the arguments. */
477 cli_display_match_list (char **matches, int len, int max)
479 struct match_list_displayer displayer;
481 rl_get_screen_size (&displayer.height, &displayer.width);
482 displayer.crlf = cli_mld_crlf;
483 displayer.putch = cli_mld_putch;
484 displayer.puts = cli_mld_puts;
485 displayer.flush = cli_mld_flush;
486 displayer.erase_entire_line = cli_mld_erase_entire_line;
487 displayer.beep = cli_mld_beep;
488 displayer.read_key = cli_mld_read_key;
490 gdb_display_match_list (matches, len, max, &displayer);
491 rl_forced_update_display ();