1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999-2013 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 "gdb_string.h"
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);
43 /* These are the CLI output functions */
45 /* Mark beginning of a table */
48 cli_table_begin (struct ui_out *uiout, int nbrofcols,
52 cli_out_data *data = ui_out_data (uiout);
55 data->suppress_output = 1;
57 /* Only the table suppresses the output and, fortunately, a table
58 is not a recursive data structure. */
59 gdb_assert (data->suppress_output == 0);
62 /* Mark beginning of a table body */
65 cli_table_body (struct ui_out *uiout)
67 cli_out_data *data = ui_out_data (uiout);
69 if (data->suppress_output)
71 /* first, close the table header line */
72 cli_text (uiout, "\n");
75 /* Mark end of a table */
78 cli_table_end (struct ui_out *uiout)
80 cli_out_data *data = ui_out_data (uiout);
82 data->suppress_output = 0;
85 /* Specify table header */
88 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
92 cli_out_data *data = ui_out_data (uiout);
94 if (data->suppress_output)
97 /* Always go through the function pointer (virtual function call).
98 We may have been extended. */
99 uo_field_string (uiout, 0, width, alignment, 0, colhdr);
102 /* Mark beginning of a list */
105 cli_begin (struct ui_out *uiout,
106 enum ui_out_type type,
110 cli_out_data *data = ui_out_data (uiout);
112 if (data->suppress_output)
116 /* Mark end of a list */
119 cli_end (struct ui_out *uiout,
120 enum ui_out_type type,
123 cli_out_data *data = ui_out_data (uiout);
125 if (data->suppress_output)
129 /* output an int field */
132 cli_field_int (struct ui_out *uiout, int fldno, int width,
133 enum ui_align alignment,
134 const char *fldname, int value)
136 char buffer[20]; /* FIXME: how many chars long a %d can become? */
137 cli_out_data *data = ui_out_data (uiout);
139 if (data->suppress_output)
141 xsnprintf (buffer, sizeof (buffer), "%d", value);
143 /* Always go through the function pointer (virtual function call).
144 We may have been extended. */
145 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
148 /* used to ommit a field */
151 cli_field_skip (struct ui_out *uiout, int fldno, int width,
152 enum ui_align alignment,
155 cli_out_data *data = ui_out_data (uiout);
157 if (data->suppress_output)
160 /* Always go through the function pointer (virtual function call).
161 We may have been extended. */
162 uo_field_string (uiout, fldno, width, alignment, fldname, "");
165 /* other specific cli_field_* end up here so alignment and field
166 separators are both handled by cli_field_string */
169 cli_field_string (struct ui_out *uiout,
178 cli_out_data *data = ui_out_data (uiout);
180 if (data->suppress_output)
183 if ((align != ui_noalign) && string)
185 before = width - strlen (string);
190 if (align == ui_right)
192 else if (align == ui_left)
207 ui_out_spaces (uiout, before);
209 out_field_fmt (uiout, fldno, fldname, "%s", string);
211 ui_out_spaces (uiout, after);
213 if (align != ui_noalign)
217 /* This is the only field function that does not align. */
219 static void ATTRIBUTE_PRINTF (6, 0)
220 cli_field_fmt (struct ui_out *uiout, int fldno,
221 int width, enum ui_align align,
226 cli_out_data *data = ui_out_data (uiout);
227 struct ui_file *stream;
229 if (data->suppress_output)
232 stream = VEC_last (ui_filep, data->streams);
233 vfprintf_filtered (stream, format, args);
235 if (align != ui_noalign)
240 cli_spaces (struct ui_out *uiout, int numspaces)
242 cli_out_data *data = ui_out_data (uiout);
243 struct ui_file *stream;
245 if (data->suppress_output)
248 stream = VEC_last (ui_filep, data->streams);
249 print_spaces_filtered (numspaces, stream);
253 cli_text (struct ui_out *uiout, const char *string)
255 cli_out_data *data = ui_out_data (uiout);
256 struct ui_file *stream;
258 if (data->suppress_output)
261 stream = VEC_last (ui_filep, data->streams);
262 fputs_filtered (string, stream);
265 static void ATTRIBUTE_PRINTF (3, 0)
266 cli_message (struct ui_out *uiout, int verbosity,
267 const char *format, va_list args)
269 cli_out_data *data = ui_out_data (uiout);
271 if (data->suppress_output)
274 if (ui_out_get_verblvl (uiout) >= verbosity)
276 struct ui_file *stream = VEC_last (ui_filep, data->streams);
278 vfprintf_unfiltered (stream, format, args);
283 cli_wrap_hint (struct ui_out *uiout, char *identstring)
285 cli_out_data *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 = ui_out_data (uiout);
296 struct ui_file *stream = VEC_last (ui_filep, data->streams);
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 = ui_out_data (uiout);
310 if (outstream != NULL)
311 VEC_safe_push (ui_filep, data->streams, outstream);
313 VEC_pop (ui_filep, data->streams);
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 = ui_out_data (uiout);
330 struct ui_file *stream = VEC_last (ui_filep, data->streams);
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 = ui_out_data (current_uiout);
345 struct ui_file *stream = VEC_last (ui_filep, data->streams);
347 fputc_filtered (' ', stream);
350 /* This is the CLI ui-out implementation functions vector */
352 /* FIXME: This can be initialized dynamically after default is set to
353 handle initial output in main.c */
355 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 = NULL;
384 VEC_safe_push (ui_filep, self->streams, 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 = XMALLOC (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 = ui_out_data (uiout);
407 old = VEC_pop (ui_filep, data->streams);
408 VEC_quick_push (ui_filep, data->streams, stream);