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