2011-08-04 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / cli-out.c
CommitLineData
8b93c638 1/* Output generating routines for GDB CLI.
349c5d5f 2
7b6bb8da 3 Copyright (C) 1999, 2000, 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011
bee0189a 4 Free Software Foundation, Inc.
349c5d5f 5
8b93c638
JM
6 Contributed by Cygnus Solutions.
7 Written by Fernando Nasser for Cygnus.
8
9 This file is part of GDB.
10
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
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
8b93c638
JM
14 (at your option) any later version.
15
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.
20
21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b93c638
JM
23
24#include "defs.h"
25#include "ui-out.h"
26#include "cli-out.h"
1d1358b6 27#include "gdb_string.h"
698384cd 28#include "gdb_assert.h"
14dba4b4 29#include "vec.h"
8b93c638 30
0a8fce9a 31typedef struct cli_ui_out_data cli_out_data;
8b93c638 32
8b93c638
JM
33
34/* Prototypes for local functions */
35
02a45ac0 36static void cli_text (struct ui_out *uiout, const char *string);
8b93c638
JM
37
38static void field_separator (void);
39
e2e11a41
AC
40static void out_field_fmt (struct ui_out *uiout, int fldno,
41 const char *fldname,
a0b31db1 42 const char *format,...) ATTRIBUTE_PRINTF (4, 5);
8b93c638 43
02a45ac0
PA
44/* These are the CLI output functions */
45
8b93c638
JM
46/* Mark beginning of a table */
47
02a45ac0 48static void
e2e11a41 49cli_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 50 int nr_rows,
e2e11a41 51 const char *tblid)
8b93c638 52{
1248ede2 53 cli_out_data *data = ui_out_data (uiout);
c5504eaf 54
698384cd
AC
55 if (nr_rows == 0)
56 data->suppress_output = 1;
57 else
ce2826aa 58 /* Only the table suppresses the output and, fortunately, a table
30fdc99f 59 is not a recursive data structure. */
698384cd 60 gdb_assert (data->suppress_output == 0);
8b93c638
JM
61}
62
63/* Mark beginning of a table body */
64
02a45ac0 65static void
fba45db2 66cli_table_body (struct ui_out *uiout)
8b93c638 67{
1248ede2 68 cli_out_data *data = ui_out_data (uiout);
c5504eaf 69
698384cd
AC
70 if (data->suppress_output)
71 return;
8b93c638
JM
72 /* first, close the table header line */
73 cli_text (uiout, "\n");
74}
75
76/* Mark end of a table */
77
02a45ac0 78static void
fba45db2 79cli_table_end (struct ui_out *uiout)
8b93c638 80{
1248ede2 81 cli_out_data *data = ui_out_data (uiout);
c5504eaf 82
698384cd 83 data->suppress_output = 0;
8b93c638
JM
84}
85
86/* Specify table header */
87
02a45ac0 88static void
fba45db2 89cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
b25959ec 90 const char *col_name,
e2e11a41 91 const char *colhdr)
8b93c638 92{
1248ede2 93 cli_out_data *data = ui_out_data (uiout);
c5504eaf 94
698384cd
AC
95 if (data->suppress_output)
96 return;
0a8fce9a
PA
97
98 /* Always go through the function pointer (virtual function call).
99 We may have been extended. */
100 uo_field_string (uiout, 0, width, alignment, 0, colhdr);
8b93c638
JM
101}
102
103/* Mark beginning of a list */
104
02a45ac0 105static void
631ec795
AC
106cli_begin (struct ui_out *uiout,
107 enum ui_out_type type,
108 int level,
109 const char *id)
8b93c638 110{
1248ede2 111 cli_out_data *data = ui_out_data (uiout);
c5504eaf 112
698384cd
AC
113 if (data->suppress_output)
114 return;
8b93c638
JM
115}
116
117/* Mark end of a list */
118
02a45ac0 119static void
631ec795
AC
120cli_end (struct ui_out *uiout,
121 enum ui_out_type type,
122 int level)
8b93c638 123{
1248ede2 124 cli_out_data *data = ui_out_data (uiout);
c5504eaf 125
698384cd
AC
126 if (data->suppress_output)
127 return;
8b93c638
JM
128}
129
130/* output an int field */
131
02a45ac0 132static void
fba45db2 133cli_field_int (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
134 enum ui_align alignment,
135 const char *fldname, int value)
8b93c638 136{
c5504eaf 137 char buffer[20]; /* FIXME: how many chars long a %d can become? */
1248ede2 138 cli_out_data *data = ui_out_data (uiout);
c5504eaf 139
698384cd
AC
140 if (data->suppress_output)
141 return;
8b93c638 142 sprintf (buffer, "%d", value);
0a8fce9a
PA
143
144 /* Always go through the function pointer (virtual function call).
145 We may have been extended. */
146 uo_field_string (uiout, fldno, width, alignment, fldname, buffer);
8b93c638
JM
147}
148
149/* used to ommit a field */
150
02a45ac0 151static void
fba45db2 152cli_field_skip (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
153 enum ui_align alignment,
154 const char *fldname)
8b93c638 155{
1248ede2 156 cli_out_data *data = ui_out_data (uiout);
c5504eaf 157
698384cd
AC
158 if (data->suppress_output)
159 return;
0a8fce9a
PA
160
161 /* Always go through the function pointer (virtual function call).
162 We may have been extended. */
163 uo_field_string (uiout, fldno, width, alignment, fldname, "");
8b93c638
JM
164}
165
166/* other specific cli_field_* end up here so alignment and field
167 separators are both handled by cli_field_string */
168
02a45ac0 169static void
8b93c638
JM
170cli_field_string (struct ui_out *uiout,
171 int fldno,
172 int width,
55555bbc 173 enum ui_align align,
e2e11a41 174 const char *fldname,
8b93c638
JM
175 const char *string)
176{
177 int before = 0;
178 int after = 0;
1248ede2 179 cli_out_data *data = ui_out_data (uiout);
c5504eaf 180
698384cd
AC
181 if (data->suppress_output)
182 return;
183
8b93c638
JM
184 if ((align != ui_noalign) && string)
185 {
186 before = width - strlen (string);
187 if (before <= 0)
188 before = 0;
189 else
190 {
191 if (align == ui_right)
192 after = 0;
193 else if (align == ui_left)
194 {
195 after = before;
196 before = 0;
197 }
198 else
199 /* ui_center */
200 {
201 after = before / 2;
202 before -= after;
203 }
204 }
205 }
206
207 if (before)
208 ui_out_spaces (uiout, before);
209 if (string)
210 out_field_fmt (uiout, fldno, fldname, "%s", string);
211 if (after)
212 ui_out_spaces (uiout, after);
213
214 if (align != ui_noalign)
215 field_separator ();
216}
217
30fdc99f 218/* This is the only field function that does not align. */
8b93c638 219
a0b31db1 220static void ATTRIBUTE_PRINTF (6, 0)
8b93c638
JM
221cli_field_fmt (struct ui_out *uiout, int fldno,
222 int width, enum ui_align align,
e2e11a41
AC
223 const char *fldname,
224 const char *format,
225 va_list args)
8b93c638 226{
1248ede2 227 cli_out_data *data = ui_out_data (uiout);
14dba4b4 228 struct ui_file *stream;
c5504eaf 229
698384cd
AC
230 if (data->suppress_output)
231 return;
232
14dba4b4
JK
233 stream = VEC_last (ui_filep, data->streams);
234 vfprintf_filtered (stream, format, args);
8b93c638
JM
235
236 if (align != ui_noalign)
237 field_separator ();
238}
239
02a45ac0 240static void
fba45db2 241cli_spaces (struct ui_out *uiout, int numspaces)
8b93c638 242{
1248ede2 243 cli_out_data *data = ui_out_data (uiout);
14dba4b4 244 struct ui_file *stream;
c5504eaf 245
698384cd
AC
246 if (data->suppress_output)
247 return;
14dba4b4
JK
248
249 stream = VEC_last (ui_filep, data->streams);
250 print_spaces_filtered (numspaces, stream);
8b93c638
JM
251}
252
02a45ac0 253static void
e2e11a41 254cli_text (struct ui_out *uiout, const char *string)
8b93c638 255{
1248ede2 256 cli_out_data *data = ui_out_data (uiout);
14dba4b4 257 struct ui_file *stream;
c5504eaf 258
698384cd
AC
259 if (data->suppress_output)
260 return;
14dba4b4
JK
261
262 stream = VEC_last (ui_filep, data->streams);
263 fputs_filtered (string, stream);
8b93c638
JM
264}
265
a0b31db1 266static void ATTRIBUTE_PRINTF (3, 0)
e2e11a41
AC
267cli_message (struct ui_out *uiout, int verbosity,
268 const char *format, va_list args)
8b93c638 269{
1248ede2 270 cli_out_data *data = ui_out_data (uiout);
c5504eaf 271
698384cd
AC
272 if (data->suppress_output)
273 return;
14dba4b4 274
8b93c638 275 if (ui_out_get_verblvl (uiout) >= verbosity)
14dba4b4
JK
276 {
277 struct ui_file *stream = VEC_last (ui_filep, data->streams);
278
279 vfprintf_unfiltered (stream, format, args);
280 }
8b93c638
JM
281}
282
02a45ac0 283static void
fba45db2 284cli_wrap_hint (struct ui_out *uiout, char *identstring)
8b93c638 285{
1248ede2 286 cli_out_data *data = ui_out_data (uiout);
c5504eaf 287
698384cd
AC
288 if (data->suppress_output)
289 return;
8b93c638
JM
290 wrap_here (identstring);
291}
292
02a45ac0 293static void
fba45db2 294cli_flush (struct ui_out *uiout)
8b93c638 295{
1248ede2 296 cli_out_data *data = ui_out_data (uiout);
14dba4b4 297 struct ui_file *stream = VEC_last (ui_filep, data->streams);
c5504eaf 298
14dba4b4 299 gdb_flush (stream);
8b93c638
JM
300}
301
14dba4b4
JK
302/* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
303 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
304 output stream; it is an internal error if it does not exist. */
305
02a45ac0 306static int
0fac0b41
DJ
307cli_redirect (struct ui_out *uiout, struct ui_file *outstream)
308{
0a8fce9a 309 cli_out_data *data = ui_out_data (uiout);
c5504eaf 310
0fac0b41 311 if (outstream != NULL)
14dba4b4
JK
312 VEC_safe_push (ui_filep, data->streams, outstream);
313 else
314 VEC_pop (ui_filep, data->streams);
0fac0b41
DJ
315
316 return 0;
317}
318
8b93c638
JM
319/* local functions */
320
321/* Like cli_field_fmt, but takes a variable number of args
30fdc99f 322 and makes a va_list and does not insert a separator. */
8b93c638
JM
323
324/* VARARGS */
325static void
e2e11a41
AC
326out_field_fmt (struct ui_out *uiout, int fldno,
327 const char *fldname,
328 const char *format,...)
8b93c638 329{
1248ede2 330 cli_out_data *data = ui_out_data (uiout);
14dba4b4 331 struct ui_file *stream = VEC_last (ui_filep, data->streams);
8b93c638
JM
332 va_list args;
333
334 va_start (args, format);
14dba4b4 335 vfprintf_filtered (stream, format, args);
8b93c638
JM
336
337 va_end (args);
338}
339
30fdc99f 340/* Access to ui_out format private members. */
8b93c638
JM
341
342static void
fba45db2 343field_separator (void)
8b93c638 344{
79a45e25 345 cli_out_data *data = ui_out_data (current_uiout);
14dba4b4 346 struct ui_file *stream = VEC_last (ui_filep, data->streams);
c5504eaf 347
14dba4b4 348 fputc_filtered (' ', stream);
8b93c638
JM
349}
350
02a45ac0
PA
351/* This is the CLI ui-out implementation functions vector */
352
353/* FIXME: This can be initialized dynamically after default is set to
354 handle initial output in main.c */
355
356struct ui_out_impl cli_ui_out_impl =
357{
358 cli_table_begin,
359 cli_table_body,
360 cli_table_end,
361 cli_table_header,
362 cli_begin,
363 cli_end,
364 cli_field_int,
365 cli_field_skip,
366 cli_field_string,
367 cli_field_fmt,
368 cli_spaces,
369 cli_text,
370 cli_message,
371 cli_wrap_hint,
372 cli_flush,
373 cli_redirect,
374 0, /* Does not need MI hacks (i.e. needs CLI hacks). */
375};
376
0a8fce9a
PA
377/* Constructor for a `cli_out_data' object. */
378
379void
380cli_out_data_ctor (cli_out_data *self, struct ui_file *stream)
381{
14dba4b4
JK
382 gdb_assert (stream != NULL);
383
384 self->streams = NULL;
385 VEC_safe_push (ui_filep, self->streams, stream);
386
0a8fce9a
PA
387 self->suppress_output = 0;
388}
389
390/* Initialize private members at startup. */
8b93c638
JM
391
392struct ui_out *
393cli_out_new (struct ui_file *stream)
394{
395 int flags = ui_source_list;
1248ede2 396 cli_out_data *data = XMALLOC (cli_out_data);
c5504eaf 397
0a8fce9a 398 cli_out_data_ctor (data, stream);
8b93c638
JM
399 return ui_out_new (&cli_ui_out_impl, data, flags);
400}
401
4389a95a
AC
402struct ui_file *
403cli_out_set_stream (struct ui_out *uiout, struct ui_file *stream)
404{
1248ede2 405 cli_out_data *data = ui_out_data (uiout);
14dba4b4
JK
406 struct ui_file *old;
407
408 old = VEC_pop (ui_filep, data->streams);
409 VEC_quick_push (ui_filep, data->streams, stream);
c5504eaf 410
4389a95a
AC
411 return old;
412}
This page took 0.9153 seconds and 4 git commands to generate.