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