ignore invalid DOF provider sections
[binutils-gdb.git] / gdb / cli-out.c
blob2a83169154684012f5ca7e7570540be33f271cf7
1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999-2015 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 "vec.h"
28 #include "readline/readline.h"
30 typedef struct cli_ui_out_data cli_out_data;
32 /* Prototypes for local functions */
34 static void cli_text (struct ui_out *uiout, const char *string);
36 static void field_separator (void);
38 static void out_field_fmt (struct ui_out *uiout, int fldno,
39 const char *fldname,
40 const char *format,...) ATTRIBUTE_PRINTF (4, 5);
42 /* The destructor. */
44 static void
45 cli_uiout_dtor (struct ui_out *ui_out)
47 cli_out_data *data = ui_out_data (ui_out);
49 VEC_free (ui_filep, data->streams);
50 xfree (data);
53 /* These are the CLI output functions */
55 /* Mark beginning of a table */
57 static void
58 cli_table_begin (struct ui_out *uiout, int nbrofcols,
59 int nr_rows,
60 const char *tblid)
62 cli_out_data *data = ui_out_data (uiout);
64 if (nr_rows == 0)
65 data->suppress_output = 1;
66 else
67 /* Only the table suppresses the output and, fortunately, a table
68 is not a recursive data structure. */
69 gdb_assert (data->suppress_output == 0);
72 /* Mark beginning of a table body */
74 static void
75 cli_table_body (struct ui_out *uiout)
77 cli_out_data *data = ui_out_data (uiout);
79 if (data->suppress_output)
80 return;
81 /* first, close the table header line */
82 cli_text (uiout, "\n");
85 /* Mark end of a table */
87 static void
88 cli_table_end (struct ui_out *uiout)
90 cli_out_data *data = ui_out_data (uiout);
92 data->suppress_output = 0;
95 /* Specify table header */
97 static void
98 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
99 const char *col_name,
100 const char *colhdr)
102 cli_out_data *data = ui_out_data (uiout);
104 if (data->suppress_output)
105 return;
107 /* Always go through the function pointer (virtual function call).
108 We may have been extended. */
109 uo_field_string (uiout, 0, width, alignment, 0, colhdr);
112 /* Mark beginning of a list */
114 static void
115 cli_begin (struct ui_out *uiout,
116 enum ui_out_type type,
117 int level,
118 const char *id)
120 cli_out_data *data = ui_out_data (uiout);
122 if (data->suppress_output)
123 return;
126 /* Mark end of a list */
128 static void
129 cli_end (struct ui_out *uiout,
130 enum ui_out_type type,
131 int level)
133 cli_out_data *data = ui_out_data (uiout);
135 if (data->suppress_output)
136 return;
139 /* output an int field */
141 static void
142 cli_field_int (struct ui_out *uiout, int fldno, int width,
143 enum ui_align alignment,
144 const char *fldname, int value)
146 char buffer[20]; /* FIXME: how many chars long a %d can become? */
147 cli_out_data *data = ui_out_data (uiout);
149 if (data->suppress_output)
150 return;
151 xsnprintf (buffer, sizeof (buffer), "%d", value);
153 /* Always go through the function pointer (virtual function call).
154 We may have been extended. */
155 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
158 /* used to ommit a field */
160 static void
161 cli_field_skip (struct ui_out *uiout, int fldno, int width,
162 enum ui_align alignment,
163 const char *fldname)
165 cli_out_data *data = ui_out_data (uiout);
167 if (data->suppress_output)
168 return;
170 /* Always go through the function pointer (virtual function call).
171 We may have been extended. */
172 uo_field_string (uiout, fldno, width, alignment, fldname, "");
175 /* other specific cli_field_* end up here so alignment and field
176 separators are both handled by cli_field_string */
178 static void
179 cli_field_string (struct ui_out *uiout,
180 int fldno,
181 int width,
182 enum ui_align align,
183 const char *fldname,
184 const char *string)
186 int before = 0;
187 int after = 0;
188 cli_out_data *data = ui_out_data (uiout);
190 if (data->suppress_output)
191 return;
193 if ((align != ui_noalign) && string)
195 before = width - strlen (string);
196 if (before <= 0)
197 before = 0;
198 else
200 if (align == ui_right)
201 after = 0;
202 else if (align == ui_left)
204 after = before;
205 before = 0;
207 else
208 /* ui_center */
210 after = before / 2;
211 before -= after;
216 if (before)
217 ui_out_spaces (uiout, before);
218 if (string)
219 out_field_fmt (uiout, fldno, fldname, "%s", string);
220 if (after)
221 ui_out_spaces (uiout, after);
223 if (align != ui_noalign)
224 field_separator ();
227 /* This is the only field function that does not align. */
229 static void ATTRIBUTE_PRINTF (6, 0)
230 cli_field_fmt (struct ui_out *uiout, int fldno,
231 int width, enum ui_align align,
232 const char *fldname,
233 const char *format,
234 va_list args)
236 cli_out_data *data = ui_out_data (uiout);
237 struct ui_file *stream;
239 if (data->suppress_output)
240 return;
242 stream = VEC_last (ui_filep, data->streams);
243 vfprintf_filtered (stream, format, args);
245 if (align != ui_noalign)
246 field_separator ();
249 static void
250 cli_spaces (struct ui_out *uiout, int numspaces)
252 cli_out_data *data = ui_out_data (uiout);
253 struct ui_file *stream;
255 if (data->suppress_output)
256 return;
258 stream = VEC_last (ui_filep, data->streams);
259 print_spaces_filtered (numspaces, stream);
262 static void
263 cli_text (struct ui_out *uiout, const char *string)
265 cli_out_data *data = ui_out_data (uiout);
266 struct ui_file *stream;
268 if (data->suppress_output)
269 return;
271 stream = VEC_last (ui_filep, data->streams);
272 fputs_filtered (string, stream);
275 static void ATTRIBUTE_PRINTF (3, 0)
276 cli_message (struct ui_out *uiout, int verbosity,
277 const char *format, va_list args)
279 cli_out_data *data = ui_out_data (uiout);
281 if (data->suppress_output)
282 return;
284 if (ui_out_get_verblvl (uiout) >= verbosity)
286 struct ui_file *stream = VEC_last (ui_filep, data->streams);
288 vfprintf_unfiltered (stream, format, args);
292 static void
293 cli_wrap_hint (struct ui_out *uiout, char *identstring)
295 cli_out_data *data = ui_out_data (uiout);
297 if (data->suppress_output)
298 return;
299 wrap_here (identstring);
302 static void
303 cli_flush (struct ui_out *uiout)
305 cli_out_data *data = ui_out_data (uiout);
306 struct ui_file *stream = VEC_last (ui_filep, data->streams);
308 gdb_flush (stream);
311 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
312 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
313 output stream; it is an internal error if it does not exist. */
315 static int
316 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
318 cli_out_data *data = ui_out_data (uiout);
320 if (outstream != NULL)
321 VEC_safe_push (ui_filep, data->streams, outstream);
322 else
323 VEC_pop (ui_filep, data->streams);
325 return 0;
328 /* local functions */
330 /* Like cli_field_fmt, but takes a variable number of args
331 and makes a va_list and does not insert a separator. */
333 /* VARARGS */
334 static void
335 out_field_fmt (struct ui_out *uiout, int fldno,
336 const char *fldname,
337 const char *format,...)
339 cli_out_data *data = ui_out_data (uiout);
340 struct ui_file *stream = VEC_last (ui_filep, data->streams);
341 va_list args;
343 va_start (args, format);
344 vfprintf_filtered (stream, format, args);
346 va_end (args);
349 /* Access to ui_out format private members. */
351 static void
352 field_separator (void)
354 cli_out_data *data = ui_out_data (current_uiout);
355 struct ui_file *stream = VEC_last (ui_filep, data->streams);
357 fputc_filtered (' ', stream);
360 /* This is the CLI ui-out implementation functions vector */
362 const struct ui_out_impl cli_ui_out_impl =
364 cli_table_begin,
365 cli_table_body,
366 cli_table_end,
367 cli_table_header,
368 cli_begin,
369 cli_end,
370 cli_field_int,
371 cli_field_skip,
372 cli_field_string,
373 cli_field_fmt,
374 cli_spaces,
375 cli_text,
376 cli_message,
377 cli_wrap_hint,
378 cli_flush,
379 cli_redirect,
380 cli_uiout_dtor,
381 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
384 /* Constructor for a `cli_out_data' object. */
386 void
387 cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
389 gdb_assert (stream != NULL);
391 self->streams = NULL;
392 VEC_safe_push (ui_filep, self->streams, stream);
394 self->suppress_output = 0;
397 /* Initialize private members at startup. */
399 struct ui_out *
400 cli_out_new (struct ui_file *stream)
402 int flags = ui_source_list;
403 cli_out_data *data = XNEW (cli_out_data);
405 cli_out_data_ctor (data, stream);
406 return ui_out_new (&cli_ui_out_impl, data, flags);
409 struct ui_file *
410 cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
412 cli_out_data *data = ui_out_data (uiout);
413 struct ui_file *old;
415 old = VEC_pop (ui_filep, data->streams);
416 VEC_quick_push (ui_filep, data->streams, stream);
418 return old;
421 /* CLI interface to display tab-completion matches. */
423 /* CLI version of displayer.crlf. */
425 static void
426 cli_mld_crlf (const struct match_list_displayer *displayer)
428 rl_crlf ();
431 /* CLI version of displayer.putch. */
433 static void
434 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
436 putc (ch, rl_outstream);
439 /* CLI version of displayer.puts. */
441 static void
442 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
444 fputs (s, rl_outstream);
447 /* CLI version of displayer.flush. */
449 static void
450 cli_mld_flush (const struct match_list_displayer *displayer)
452 fflush (rl_outstream);
455 EXTERN_C void _rl_erase_entire_line (void);
457 /* CLI version of displayer.erase_entire_line. */
459 static void
460 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
462 _rl_erase_entire_line ();
465 /* CLI version of displayer.beep. */
467 static void
468 cli_mld_beep (const struct match_list_displayer *displayer)
470 rl_ding ();
473 /* CLI version of displayer.read_key. */
475 static int
476 cli_mld_read_key (const struct match_list_displayer *displayer)
478 return rl_read_key ();
481 /* CLI version of rl_completion_display_matches_hook.
482 See gdb_display_match_list for a description of the arguments. */
484 void
485 cli_display_match_list (char **matches, int len, int max)
487 struct match_list_displayer displayer;
489 rl_get_screen_size (&displayer.height, &displayer.width);
490 displayer.crlf = cli_mld_crlf;
491 displayer.putch = cli_mld_putch;
492 displayer.puts = cli_mld_puts;
493 displayer.flush = cli_mld_flush;
494 displayer.erase_entire_line = cli_mld_erase_entire_line;
495 displayer.beep = cli_mld_beep;
496 displayer.read_key = cli_mld_read_key;
498 gdb_display_match_list (matches, len, max, &displayer);
499 rl_forced_update_display ();