1 /* Output generating routines for GDB CLI.
3 Copyright (C) 1999, 2000, 2002, 2003, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions.
7 Written by Fernando Nasser for Cygnus.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "gdb_string.h"
28 #include "gdb_assert.h"
30 typedef struct cli_ui_out_data cli_out_data;
32 /* These are the CLI output functions */
34 static void cli_table_begin (struct ui_out *uiout, int nbrofcols,
35 int nr_rows, const char *tblid);
36 static void cli_table_body (struct ui_out *uiout);
37 static void cli_table_end (struct ui_out *uiout);
38 static void cli_table_header (struct ui_out *uiout, int width,
39 enum ui_align alig, const char *col_name,
41 static void cli_begin (struct ui_out *uiout, enum ui_out_type type,
42 int level, const char *lstid);
43 static void cli_end (struct ui_out *uiout, enum ui_out_type type, int level);
44 static void cli_field_int (struct ui_out *uiout, int fldno, int width,
45 enum ui_align alig, const char *fldname, int value);
46 static void cli_field_skip (struct ui_out *uiout, int fldno, int width,
47 enum ui_align alig, const char *fldname);
48 static void cli_field_string (struct ui_out *uiout, int fldno, int width,
49 enum ui_align alig, const char *fldname,
51 static void cli_field_fmt (struct ui_out *uiout, int fldno,
52 int width, enum ui_align align,
53 const char *fldname, const char *format,
54 va_list args) ATTR_FORMAT (printf, 6, 0);
55 static void cli_spaces (struct ui_out *uiout, int numspaces);
56 static void cli_text (struct ui_out *uiout, const char *string);
57 static void cli_message (struct ui_out *uiout, int verbosity,
58 const char *format, va_list args)
59 ATTR_FORMAT (printf, 3, 0);
60 static void cli_wrap_hint (struct ui_out *uiout, char *identstring);
61 static void cli_flush (struct ui_out *uiout);
62 static int cli_redirect (struct ui_out *uiout, struct ui_file *outstream);
64 /* This is the CLI ui-out implementation functions vector */
66 /* FIXME: This can be initialized dynamically after default is set to
67 handle initial output in main.c */
69 struct ui_out_impl cli_ui_out_impl =
87 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
90 /* Prototypes for local functions */
92 extern void _initialize_cli_out (void);
94 static void field_separator (void);
96 static void out_field_fmt (struct ui_out *uiout, int fldno,
98 const char *format,...) ATTR_FORMAT (printf, 4, 5);
100 /* Mark beginning of a table */
103 cli_table_begin (struct ui_out *uiout, int nbrofcols,
107 cli_out_data *data = ui_out_data (uiout);
109 data->suppress_output = 1;
111 /* Only the table suppresses the output and, fortunately, a table
112 is not a recursive data structure. */
113 gdb_assert (data->suppress_output == 0);
116 /* Mark beginning of a table body */
119 cli_table_body (struct ui_out *uiout)
121 cli_out_data *data = ui_out_data (uiout);
122 if (data->suppress_output)
124 /* first, close the table header line */
125 cli_text (uiout, "\n");
128 /* Mark end of a table */
131 cli_table_end (struct ui_out *uiout)
133 cli_out_data *data = ui_out_data (uiout);
134 data->suppress_output = 0;
137 /* Specify table header */
140 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
141 const char *col_name,
144 cli_out_data *data = ui_out_data (uiout);
145 if (data->suppress_output)
148 /* Always go through the function pointer (virtual function call).
149 We may have been extended. */
150 uo_field_string (uiout, 0, width, alignment, 0, colhdr);
153 /* Mark beginning of a list */
156 cli_begin (struct ui_out *uiout,
157 enum ui_out_type type,
161 cli_out_data *data = ui_out_data (uiout);
162 if (data->suppress_output)
166 /* Mark end of a list */
169 cli_end (struct ui_out *uiout,
170 enum ui_out_type type,
173 cli_out_data *data = ui_out_data (uiout);
174 if (data->suppress_output)
178 /* output an int field */
181 cli_field_int (struct ui_out *uiout, int fldno, int width,
182 enum ui_align alignment,
183 const char *fldname, int value)
185 char buffer[20]; /* FIXME: how many chars long a %d can become? */
187 cli_out_data *data = ui_out_data (uiout);
188 if (data->suppress_output)
190 sprintf (buffer, "%d", value);
192 /* Always go through the function pointer (virtual function call).
193 We may have been extended. */
194 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
197 /* used to ommit a field */
200 cli_field_skip (struct ui_out *uiout, int fldno, int width,
201 enum ui_align alignment,
204 cli_out_data *data = ui_out_data (uiout);
205 if (data->suppress_output)
208 /* Always go through the function pointer (virtual function call).
209 We may have been extended. */
210 uo_field_string (uiout, fldno, width, alignment, fldname, "");
213 /* other specific cli_field_* end up here so alignment and field
214 separators are both handled by cli_field_string */
217 cli_field_string (struct ui_out *uiout,
227 cli_out_data *data = ui_out_data (uiout);
228 if (data->suppress_output)
231 if ((align != ui_noalign) && string)
233 before = width - strlen (string);
238 if (align == ui_right)
240 else if (align == ui_left)
255 ui_out_spaces (uiout, before);
257 out_field_fmt (uiout, fldno, fldname, "%s", string);
259 ui_out_spaces (uiout, after);
261 if (align != ui_noalign)
265 /* This is the only field function that does not align. */
268 cli_field_fmt (struct ui_out *uiout, int fldno,
269 int width, enum ui_align align,
274 cli_out_data *data = ui_out_data (uiout);
275 if (data->suppress_output)
278 vfprintf_filtered (data->stream, format, args);
280 if (align != ui_noalign)
285 cli_spaces (struct ui_out *uiout, int numspaces)
287 cli_out_data *data = ui_out_data (uiout);
288 if (data->suppress_output)
290 print_spaces_filtered (numspaces, data->stream);
294 cli_text (struct ui_out *uiout, const char *string)
296 cli_out_data *data = ui_out_data (uiout);
297 if (data->suppress_output)
299 fputs_filtered (string, data->stream);
303 cli_message (struct ui_out *uiout, int verbosity,
304 const char *format, va_list args)
306 cli_out_data *data = ui_out_data (uiout);
307 if (data->suppress_output)
309 if (ui_out_get_verblvl (uiout) >= verbosity)
310 vfprintf_unfiltered (data->stream, format, args);
314 cli_wrap_hint (struct ui_out *uiout, char *identstring)
316 cli_out_data *data = ui_out_data (uiout);
317 if (data->suppress_output)
319 wrap_here (identstring);
323 cli_flush (struct ui_out *uiout)
325 cli_out_data *data = ui_out_data (uiout);
326 gdb_flush (data->stream);
330 cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
332 cli_out_data *data = ui_out_data (uiout);
333 if (outstream != NULL)
335 data->original_stream = data->stream;
336 data->stream = outstream;
338 else if (data->original_stream != NULL)
340 data->stream = data->original_stream;
341 data->original_stream = NULL;
347 /* local functions */
349 /* Like cli_field_fmt, but takes a variable number of args
350 and makes a va_list and does not insert a separator. */
354 out_field_fmt (struct ui_out *uiout, int fldno,
356 const char *format,...)
358 cli_out_data *data = ui_out_data (uiout);
361 va_start (args, format);
362 vfprintf_filtered (data->stream, format, args);
367 /* Access to ui_out format private members. */
370 field_separator (void)
372 cli_out_data *data = ui_out_data (uiout);
373 fputc_filtered (' ', data->stream);
376 /* Constructor for a `cli_out_data' object. */
379 cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
381 self->stream = stream;
382 self->original_stream = NULL;
383 self->suppress_output = 0;
386 /* Initialize private members at startup. */
389 cli_out_new (struct ui_file *stream)
391 int flags = ui_source_list;
393 cli_out_data *data = XMALLOC (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 = ui_out_data (uiout);
402 struct ui_file *old = data->stream;
403 data->stream = stream;
407 /* Standard gdb initialization hook. */
409 _initialize_cli_out (void)
411 /* nothing needs to be done */