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