1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999-2014 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/>. */
27 #include "gdb_assert.h"
30 typedef struct cli_ui_out_data cli_out_data;
33 /* Prototypes for local functions */
35 static void cli_text (struct ui_out *uiout, const char *string);
37 static void field_separator (void);
39 static void out_field_fmt (struct ui_out *uiout, int fldno,
41 const char *format,...) ATTRIBUTE_PRINTF (4, 5);
46 cli_uiout_dtor (struct ui_out *ui_out)
48 cli_out_data *data = ui_out_data (ui_out);
50 VEC_free (ui_filep, data->streams);
54 /* These are the CLI output functions */
56 /* Mark beginning of a table */
59 cli_table_begin (struct ui_out *uiout, int nbrofcols,
63 cli_out_data *data = ui_out_data (uiout);
66 data->suppress_output = 1;
68 /* Only the table suppresses the output and, fortunately, a table
69 is not a recursive data structure. */
70 gdb_assert (data->suppress_output == 0);
73 /* Mark beginning of a table body */
76 cli_table_body (struct ui_out *uiout)
78 cli_out_data *data = ui_out_data (uiout);
80 if (data->suppress_output)
82 /* first, close the table header line */
83 cli_text (uiout, "\n");
86 /* Mark end of a table */
89 cli_table_end (struct ui_out *uiout)
91 cli_out_data *data = ui_out_data (uiout);
93 data->suppress_output = 0;
96 /* Specify table header */
99 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
100 const char *col_name,
103 cli_out_data *data = ui_out_data (uiout);
105 if (data->suppress_output)
108 /* Always go through the function pointer (virtual function call).
109 We may have been extended. */
110 uo_field_string (uiout, 0, width, alignment, 0, colhdr);
113 /* Mark beginning of a list */
116 cli_begin (struct ui_out *uiout,
117 enum ui_out_type type,
121 cli_out_data *data = ui_out_data (uiout);
123 if (data->suppress_output)
127 /* Mark end of a list */
130 cli_end (struct ui_out *uiout,
131 enum ui_out_type type,
134 cli_out_data *data = ui_out_data (uiout);
136 if (data->suppress_output)
140 /* output an int field */
143 cli_field_int (struct ui_out *uiout, int fldno, int width,
144 enum ui_align alignment,
145 const char *fldname, int value)
147 char buffer[20]; /* FIXME: how many chars long a %d can become? */
148 cli_out_data *data = ui_out_data (uiout);
150 if (data->suppress_output)
152 xsnprintf (buffer, sizeof (buffer), "%d", value);
154 /* Always go through the function pointer (virtual function call).
155 We may have been extended. */
156 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
159 /* used to ommit a field */
162 cli_field_skip (struct ui_out *uiout, int fldno, int width,
163 enum ui_align alignment,
166 cli_out_data *data = ui_out_data (uiout);
168 if (data->suppress_output)
171 /* Always go through the function pointer (virtual function call).
172 We may have been extended. */
173 uo_field_string (uiout, fldno, width, alignment, fldname, "");
176 /* other specific cli_field_* end up here so alignment and field
177 separators are both handled by cli_field_string */
180 cli_field_string (struct ui_out *uiout,
189 cli_out_data *data = ui_out_data (uiout);
191 if (data->suppress_output)
194 if ((align != ui_noalign) && string)
196 before = width - strlen (string);
201 if (align == ui_right)
203 else if (align == ui_left)
218 ui_out_spaces (uiout, before);
220 out_field_fmt (uiout, fldno, fldname, "%s", string);
222 ui_out_spaces (uiout, after);
224 if (align != ui_noalign)
228 /* This is the only field function that does not align. */
230 static void ATTRIBUTE_PRINTF (6, 0)
231 cli_field_fmt (struct ui_out *uiout, int fldno,
232 int width, enum ui_align align,
237 cli_out_data *data = ui_out_data (uiout);
238 struct ui_file *stream;
240 if (data->suppress_output)
243 stream = VEC_last (ui_filep, data->streams);
244 vfprintf_filtered (stream, format, args);
246 if (align != ui_noalign)
251 cli_spaces (struct ui_out *uiout, int numspaces)
253 cli_out_data *data = ui_out_data (uiout);
254 struct ui_file *stream;
256 if (data->suppress_output)
259 stream = VEC_last (ui_filep, data->streams);
260 print_spaces_filtered (numspaces, stream);
264 cli_text (struct ui_out *uiout, const char *string)
266 cli_out_data *data = ui_out_data (uiout);
267 struct ui_file *stream;
269 if (data->suppress_output)
272 stream = VEC_last (ui_filep, data->streams);
273 fputs_filtered (string, stream);
276 static void ATTRIBUTE_PRINTF (3, 0)
277 cli_message (struct ui_out *uiout, int verbosity,
278 const char *format, va_list args)
280 cli_out_data *data = ui_out_data (uiout);
282 if (data->suppress_output)
285 if (ui_out_get_verblvl (uiout) >= verbosity)
287 struct ui_file *stream = VEC_last (ui_filep, data->streams);
289 vfprintf_unfiltered (stream, format, args);
294 cli_wrap_hint (struct ui_out *uiout, char *identstring)
296 cli_out_data *data = ui_out_data (uiout);
298 if (data->suppress_output)
300 wrap_here (identstring);
304 cli_flush (struct ui_out *uiout)
306 cli_out_data *data = ui_out_data (uiout);
307 struct ui_file *stream = VEC_last (ui_filep, data->streams);
312 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
313 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
314 output stream; it is an internal error if it does not exist. */
317 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
319 cli_out_data *data = ui_out_data (uiout);
321 if (outstream != NULL)
322 VEC_safe_push (ui_filep, data->streams, outstream);
324 VEC_pop (ui_filep, data->streams);
329 /* local functions */
331 /* Like cli_field_fmt, but takes a variable number of args
332 and makes a va_list and does not insert a separator. */
336 out_field_fmt (struct ui_out *uiout, int fldno,
338 const char *format,...)
340 cli_out_data *data = ui_out_data (uiout);
341 struct ui_file *stream = VEC_last (ui_filep, data->streams);
344 va_start (args, format);
345 vfprintf_filtered (stream, format, args);
350 /* Access to ui_out format private members. */
353 field_separator (void)
355 cli_out_data *data = ui_out_data (current_uiout);
356 struct ui_file *stream = VEC_last (ui_filep, data->streams);
358 fputc_filtered (' ', stream);
361 /* This is the CLI ui-out implementation functions vector */
363 const struct ui_out_impl cli_ui_out_impl =
382 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
385 /* Constructor for a `cli_out_data' object. */
388 cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
390 gdb_assert (stream != NULL);
392 self->streams = NULL;
393 VEC_safe_push (ui_filep, self->streams, stream);
395 self->suppress_output = 0;
398 /* Initialize private members at startup. */
401 cli_out_new (struct ui_file *stream)
403 int flags = ui_source_list;
404 cli_out_data *data = XNEW (cli_out_data);
406 cli_out_data_ctor (data, stream);
407 return ui_out_new (&cli_ui_out_impl, data, flags);
411 cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
413 cli_out_data *data = ui_out_data (uiout);
416 old = VEC_pop (ui_filep, data->streams);
417 VEC_quick_push (ui_filep, data->streams, stream);