]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ui-out.c
Update Copyright year range in all files maintained by GDB.
[thirdparty/binutils-gdb.git] / gdb / ui-out.c
CommitLineData
8b93c638 1/* Output generating routines for GDB.
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"
0e9f083f 24#include <string.h>
8b93c638
JM
25#include "expression.h" /* For language.h */
26#include "language.h"
27#include "ui-out.h"
80f49b30 28#include "gdb_assert.h"
8b93c638 29
8b93c638
JM
30/* table header structures */
31
32struct ui_out_hdr
33 {
34 int colno;
35 int width;
36 int alignment;
b25959ec 37 char *col_name;
8b93c638
JM
38 char *colhdr;
39 struct ui_out_hdr *next;
40 };
41
80f49b30
AC
42/* Maintain a stack so that the info applicable to the inner most list
43 is always available. Stack/nested level 0 is reserved for the
581e13c1 44 top-level result. */
80f49b30 45
dc146f7c 46enum { MAX_UI_OUT_LEVELS = 8 };
80f49b30
AC
47
48struct ui_out_level
49 {
581e13c1 50 /* Count each field; the first element is for non-list fields. */
80f49b30 51 int field_count;
581e13c1 52 /* The type of this level. */
631ec795 53 enum ui_out_type type;
80f49b30
AC
54 };
55
54eb231c
PM
56/* Define uiout->level vector types and operations. */
57typedef struct ui_out_level *ui_out_level_p;
58DEF_VEC_P (ui_out_level_p);
59
bafdd3b3
AC
60/* Tables are special. Maintain a separate structure that tracks
61 their state. At present an output can only contain a single table
62 but that restriction might eventually be lifted. */
63
64struct ui_out_table
65{
66 /* If on, a table is being generated. */
67 int flag;
68
69 /* If on, the body of a table is being generated. If off, the table
70 header is being generated. */
71 int body_flag;
72
a6c47c14
AC
73 /* The level at which each entry of the table is to be found. A row
74 (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one
75 above that of the table. */
76 int entry_level;
77
bafdd3b3
AC
78 /* Number of table columns (as specified in the table_begin call). */
79 int columns;
80
81 /* String identifying the table (as specified in the table_begin
82 call). */
83 char *id;
84
85 /* Points to the first table header (if any). */
86 struct ui_out_hdr *header_first;
87
88 /* Points to the last table header (if any). */
89 struct ui_out_hdr *header_last;
90
91 /* Points to header of NEXT column to format. */
92 struct ui_out_hdr *header_next;
93
94};
95
96
8b93c638
JM
97/* The ui_out structure */
98/* Any change here requires a corresponding one in the initialization
581e13c1 99 of the default uiout, which is statically initialized. */
8b93c638
JM
100
101struct ui_out
102 {
103 int flags;
581e13c1 104 /* Specific implementation of ui-out. */
8b93c638 105 struct ui_out_impl *impl;
0a8fce9a 106 void *data;
8b93c638 107
54eb231c 108 /* Current level. */
80f49b30 109 int level;
54eb231c
PM
110
111 /* Vector to store and track the ui-out levels. */
112 VEC (ui_out_level_p) *levels;
8b93c638 113
bafdd3b3
AC
114 /* A table, if any. At present only a single table is supported. */
115 struct ui_out_table table;
8b93c638
JM
116 };
117
581e13c1 118/* The current (inner most) level. */
80f49b30
AC
119static struct ui_out_level *
120current_level (struct ui_out *uiout)
121{
54eb231c 122 return VEC_index (ui_out_level_p, uiout->levels, uiout->level);
80f49b30
AC
123}
124
581e13c1 125/* Create a new level, of TYPE. Return the new level's index. */
80f49b30
AC
126static int
127push_level (struct ui_out *uiout,
631ec795 128 enum ui_out_type type,
80f49b30
AC
129 const char *id)
130{
131 struct ui_out_level *current;
5d502164 132
80f49b30 133 uiout->level++;
54eb231c 134 current = XMALLOC (struct ui_out_level);
80f49b30 135 current->field_count = 0;
631ec795 136 current->type = type;
54eb231c 137 VEC_safe_push (ui_out_level_p, uiout->levels, current);
80f49b30
AC
138 return uiout->level;
139}
140
141/* Discard the current level, return the discarded level's index.
581e13c1 142 TYPE is the type of the level being discarded. */
80f49b30 143static int
631ec795
AC
144pop_level (struct ui_out *uiout,
145 enum ui_out_type type)
80f49b30 146{
54eb231c
PM
147 struct ui_out_level *current;
148
581e13c1 149 /* We had better not underflow the buffer. */
54eb231c 150 gdb_assert (uiout->level > 0);
631ec795 151 gdb_assert (current_level (uiout)->type == type);
54eb231c
PM
152 current = VEC_pop (ui_out_level_p, uiout->levels);
153 xfree (current);
80f49b30
AC
154 uiout->level--;
155 return uiout->level + 1;
156}
157
158
581e13c1 159/* These are the default implementation functions. */
8b93c638
JM
160
161static void default_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 162 int nr_rows, const char *tblid);
8b93c638
JM
163static void default_table_body (struct ui_out *uiout);
164static void default_table_end (struct ui_out *uiout);
165static void default_table_header (struct ui_out *uiout, int width,
b25959ec 166 enum ui_align alig, const char *col_name,
e2e11a41 167 const char *colhdr);
631ec795
AC
168static void default_begin (struct ui_out *uiout,
169 enum ui_out_type type,
170 int level, const char *id);
171static void default_end (struct ui_out *uiout,
172 enum ui_out_type type,
173 int level);
8b93c638 174static void default_field_int (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
175 enum ui_align alig,
176 const char *fldname,
177 int value);
8b93c638 178static void default_field_skip (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
179 enum ui_align alig,
180 const char *fldname);
8b93c638 181static void default_field_string (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
182 enum ui_align align,
183 const char *fldname,
8b93c638
JM
184 const char *string);
185static void default_field_fmt (struct ui_out *uiout, int fldno,
186 int width, enum ui_align align,
e2e11a41
AC
187 const char *fldname,
188 const char *format,
a0b31db1 189 va_list args) ATTRIBUTE_PRINTF (6, 0);
8b93c638 190static void default_spaces (struct ui_out *uiout, int numspaces);
e2e11a41
AC
191static void default_text (struct ui_out *uiout, const char *string);
192static void default_message (struct ui_out *uiout, int verbosity,
193 const char *format,
a0b31db1 194 va_list args) ATTRIBUTE_PRINTF (3, 0);
8b93c638
JM
195static void default_wrap_hint (struct ui_out *uiout, char *identstring);
196static void default_flush (struct ui_out *uiout);
b65a2bd9 197static void default_data_destroy (struct ui_out *uiout);
8b93c638 198
581e13c1 199/* This is the default ui-out implementation functions vector. */
8b93c638
JM
200
201struct ui_out_impl default_ui_out_impl =
202{
203 default_table_begin,
204 default_table_body,
205 default_table_end,
206 default_table_header,
631ec795
AC
207 default_begin,
208 default_end,
8b93c638
JM
209 default_field_int,
210 default_field_skip,
211 default_field_string,
212 default_field_fmt,
213 default_spaces,
214 default_text,
215 default_message,
216 default_wrap_hint,
9dc5e2a9 217 default_flush,
0fac0b41 218 NULL,
b65a2bd9 219 default_data_destroy,
9dc5e2a9 220 0, /* Does not need MI hacks. */
8b93c638
JM
221};
222
223/* The default ui_out */
224
225struct ui_out def_uiout =
226{
227 0, /* flags */
228 &default_ui_out_impl, /* impl */
229};
230
231/* Pointer to current ui_out */
232/* FIXME: This should not be a global, but something passed down from main.c
581e13c1 233 or top.c. */
8b93c638 234
79a45e25 235struct ui_out *current_uiout = &def_uiout;
8b93c638 236
581e13c1 237/* These are the interfaces to implementation functions. */
8b93c638 238
88379baf 239static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 240 int nr_rows, const char *tblid);
8b93c638
JM
241static void uo_table_body (struct ui_out *uiout);
242static void uo_table_end (struct ui_out *uiout);
243static void uo_table_header (struct ui_out *uiout, int width,
b25959ec
AC
244 enum ui_align align, const char *col_name,
245 const char *colhdr);
631ec795
AC
246static void uo_begin (struct ui_out *uiout,
247 enum ui_out_type type,
248 int level, const char *id);
249static void uo_end (struct ui_out *uiout,
250 enum ui_out_type type,
251 int level);
8b93c638 252static void uo_field_int (struct ui_out *uiout, int fldno, int width,
88379baf 253 enum ui_align align, const char *fldname, int value);
8b93c638 254static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
88379baf 255 enum ui_align align, const char *fldname);
8b93c638 256static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
88379baf 257 enum ui_align align, const char *fldname,
bee0189a 258 const char *format, va_list args)
a0b31db1 259 ATTRIBUTE_PRINTF (6, 0);
8b93c638 260static void uo_spaces (struct ui_out *uiout, int numspaces);
88379baf 261static void uo_text (struct ui_out *uiout, const char *string);
8b93c638 262static void uo_message (struct ui_out *uiout, int verbosity,
bee0189a 263 const char *format, va_list args)
a0b31db1 264 ATTRIBUTE_PRINTF (3, 0);
8b93c638
JM
265static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
266static void uo_flush (struct ui_out *uiout);
0fac0b41 267static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream);
b65a2bd9 268static void uo_data_destroy (struct ui_out *uiout);
8b93c638
JM
269
270/* Prototypes for local functions */
271
272extern void _initialize_ui_out (void);
88379baf 273static void append_header_to_list (struct ui_out *uiout, int width,
b25959ec
AC
274 int alignment, const char *col_name,
275 const char *colhdr);
bafdd3b3 276static int get_next_header (struct ui_out *uiout, int *colno, int *width,
8b93c638
JM
277 int *alignment, char **colhdr);
278static void clear_header_list (struct ui_out *uiout);
b65a2bd9 279static void clear_table (struct ui_out *uiout);
a6c47c14
AC
280static void verify_field (struct ui_out *uiout, int *fldno, int *width,
281 int *align);
8b93c638 282
8b93c638
JM
283/* exported functions (ui_out API) */
284
581e13c1 285/* Mark beginning of a table. */
8b93c638 286
3b31d625 287static void
88379baf 288ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 289 int nr_rows,
88379baf 290 const char *tblid)
8b93c638 291{
bafdd3b3 292 if (uiout->table.flag)
8e65ff28 293 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
294 _("tables cannot be nested; table_begin found before \
295previous table_end."));
8b93c638 296
bafdd3b3
AC
297 uiout->table.flag = 1;
298 uiout->table.body_flag = 0;
a6c47c14 299 uiout->table.entry_level = uiout->level + 1;
bafdd3b3 300 uiout->table.columns = nbrofcols;
8b93c638 301 if (tblid != NULL)
bafdd3b3 302 uiout->table.id = xstrdup (tblid);
8b93c638 303 else
bafdd3b3 304 uiout->table.id = NULL;
8b93c638
JM
305 clear_header_list (uiout);
306
bafdd3b3 307 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id);
8b93c638
JM
308}
309
310void
fba45db2 311ui_out_table_body (struct ui_out *uiout)
8b93c638 312{
bafdd3b3 313 if (!uiout->table.flag)
8e65ff28 314 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
315 _("table_body outside a table is not valid; it must be \
316after a table_begin and before a table_end."));
bafdd3b3 317 if (uiout->table.body_flag)
8e65ff28 318 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
319 _("extra table_body call not allowed; there must be \
320only one table_body after a table_begin and before a table_end."));
bafdd3b3 321 if (uiout->table.header_next->colno != uiout->table.columns)
8e65ff28 322 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
323 _("number of headers differ from number of table \
324columns."));
8b93c638 325
bafdd3b3
AC
326 uiout->table.body_flag = 1;
327 uiout->table.header_next = uiout->table.header_first;
8b93c638
JM
328
329 uo_table_body (uiout);
330}
331
3b31d625 332static void
fba45db2 333ui_out_table_end (struct ui_out *uiout)
8b93c638 334{
bafdd3b3 335 if (!uiout->table.flag)
8e65ff28 336 internal_error (__FILE__, __LINE__,
e2e0b3e5 337 _("misplaced table_end or missing table_begin."));
8b93c638 338
a6c47c14 339 uiout->table.entry_level = 0;
bafdd3b3
AC
340 uiout->table.body_flag = 0;
341 uiout->table.flag = 0;
8b93c638
JM
342
343 uo_table_end (uiout);
b65a2bd9 344 clear_table (uiout);
8b93c638
JM
345}
346
347void
fba45db2 348ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
b25959ec 349 const char *col_name,
88379baf 350 const char *colhdr)
8b93c638 351{
bafdd3b3 352 if (!uiout->table.flag || uiout->table.body_flag)
8e65ff28 353 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
354 _("table header must be specified after table_begin \
355and before table_body."));
8b93c638 356
b25959ec 357 append_header_to_list (uiout, width, alignment, col_name, colhdr);
8b93c638 358
b25959ec 359 uo_table_header (uiout, width, alignment, col_name, colhdr);
8b93c638
JM
360}
361
3b31d625
EZ
362static void
363do_cleanup_table_end (void *data)
364{
365 struct ui_out *ui_out = data;
366
367 ui_out_table_end (ui_out);
368}
369
370struct cleanup *
371make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols,
372 int nr_rows, const char *tblid)
373{
374 ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid);
375 return make_cleanup (do_cleanup_table_end, ui_out);
376}
377
8b93c638 378void
631ec795
AC
379ui_out_begin (struct ui_out *uiout,
380 enum ui_out_type type,
381 const char *id)
8b93c638 382{
80f49b30 383 int new_level;
5d502164 384
bafdd3b3 385 if (uiout->table.flag && !uiout->table.body_flag)
8e65ff28 386 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
387 _("table header or table_body expected; lists must be \
388specified after table_body."));
a6c47c14
AC
389
390 /* Be careful to verify the ``field'' before the new tuple/list is
391 pushed onto the stack. That way the containing list/table/row is
392 verified and not the newly created tuple/list. This verification
393 is needed (at least) for the case where a table row entry
394 contains either a tuple/list. For that case bookkeeping such as
395 updating the column count or advancing to the next heading still
396 needs to be performed. */
397 {
398 int fldno;
399 int width;
400 int align;
5d502164 401
a6c47c14
AC
402 verify_field (uiout, &fldno, &width, &align);
403 }
404
631ec795 405 new_level = push_level (uiout, type, id);
a6c47c14
AC
406
407 /* If the push puts us at the same level as a table row entry, we've
408 got a new table row. Put the header pointer back to the start. */
409 if (uiout->table.body_flag
410 && uiout->table.entry_level == new_level)
bafdd3b3 411 uiout->table.header_next = uiout->table.header_first;
a6c47c14 412
631ec795
AC
413 uo_begin (uiout, type, new_level, id);
414}
415
631ec795
AC
416void
417ui_out_end (struct ui_out *uiout,
418 enum ui_out_type type)
419{
420 int old_level = pop_level (uiout, type);
5d502164 421
631ec795 422 uo_end (uiout, type, old_level);
8b93c638
JM
423}
424
127431f9
AC
425struct ui_out_end_cleanup_data
426{
427 struct ui_out *uiout;
428 enum ui_out_type type;
429};
430
e6e0bfab 431static void
127431f9
AC
432do_cleanup_end (void *data)
433{
434 struct ui_out_end_cleanup_data *end_cleanup_data = data;
5d502164 435
127431f9
AC
436 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
437 xfree (end_cleanup_data);
438}
439
440static struct cleanup *
441make_cleanup_ui_out_end (struct ui_out *uiout,
442 enum ui_out_type type)
443{
444 struct ui_out_end_cleanup_data *end_cleanup_data;
5d502164 445
127431f9
AC
446 end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data);
447 end_cleanup_data->uiout = uiout;
448 end_cleanup_data->type = type;
449 return make_cleanup (do_cleanup_end, end_cleanup_data);
450}
451
e6e0bfab 452struct cleanup *
666547aa
AC
453make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
454 const char *id)
455{
3b31d625 456 ui_out_begin (uiout, ui_out_type_tuple, id);
666547aa
AC
457 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
458}
459
460struct cleanup *
6b28c186
AC
461make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
462 const char *id)
e6e0bfab 463{
3b31d625 464 ui_out_begin (uiout, ui_out_type_list, id);
127431f9 465 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
e6e0bfab
MK
466}
467
8b93c638 468void
88379baf
AC
469ui_out_field_int (struct ui_out *uiout,
470 const char *fldname,
471 int value)
8b93c638
JM
472{
473 int fldno;
474 int width;
475 int align;
476
a6c47c14 477 verify_field (uiout, &fldno, &width, &align);
8b93c638
JM
478
479 uo_field_int (uiout, fldno, width, align, fldname, value);
480}
481
52c6a6ac
JJ
482void
483ui_out_field_fmt_int (struct ui_out *uiout,
484 int input_width,
485 enum ui_align input_align,
486 const char *fldname,
487 int value)
488{
489 int fldno;
490 int width;
491 int align;
52c6a6ac
JJ
492
493 verify_field (uiout, &fldno, &width, &align);
494
495 uo_field_int (uiout, fldno, input_width, input_align, fldname, value);
496}
497
15230f37
TJB
498/* Documented in ui-out.h. */
499
8b93c638 500void
88379baf
AC
501ui_out_field_core_addr (struct ui_out *uiout,
502 const char *fldname,
5af949e3 503 struct gdbarch *gdbarch,
88379baf 504 CORE_ADDR address)
8b93c638 505{
f1310107
TJB
506 ui_out_field_string (uiout, fldname,
507 print_core_address (gdbarch, address));
8b93c638
JM
508}
509
510void
88379baf
AC
511ui_out_field_stream (struct ui_out *uiout,
512 const char *fldname,
f99d8bf4 513 struct ui_file *stream)
8b93c638
JM
514{
515 long length;
f99d8bf4 516 char *buffer = ui_file_xstrdup (stream, &length);
b8c9b27d 517 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
5d502164 518
8b93c638
JM
519 if (length > 0)
520 ui_out_field_string (uiout, fldname, buffer);
521 else
522 ui_out_field_skip (uiout, fldname);
f99d8bf4 523 ui_file_rewind (stream);
8b93c638
JM
524 do_cleanups (old_cleanup);
525}
526
581e13c1 527/* Used to omit a field. */
8b93c638
JM
528
529void
88379baf
AC
530ui_out_field_skip (struct ui_out *uiout,
531 const char *fldname)
8b93c638
JM
532{
533 int fldno;
534 int width;
535 int align;
8b93c638 536
a6c47c14 537 verify_field (uiout, &fldno, &width, &align);
8b93c638
JM
538
539 uo_field_skip (uiout, fldno, width, align, fldname);
540}
541
542void
543ui_out_field_string (struct ui_out *uiout,
88379baf 544 const char *fldname,
8b93c638
JM
545 const char *string)
546{
547 int fldno;
548 int width;
549 int align;
8b93c638 550
a6c47c14 551 verify_field (uiout, &fldno, &width, &align);
8b93c638
JM
552
553 uo_field_string (uiout, fldno, width, align, fldname, string);
554}
555
556/* VARARGS */
557void
88379baf
AC
558ui_out_field_fmt (struct ui_out *uiout,
559 const char *fldname,
560 const char *format, ...)
8b93c638
JM
561{
562 va_list args;
563 int fldno;
564 int width;
565 int align;
8b93c638 566
581e13c1 567 /* Will not align, but has to call anyway. */
a6c47c14 568 verify_field (uiout, &fldno, &width, &align);
8b93c638
JM
569
570 va_start (args, format);
571
572 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
573
574 va_end (args);
575}
576
577void
fba45db2 578ui_out_spaces (struct ui_out *uiout, int numspaces)
8b93c638
JM
579{
580 uo_spaces (uiout, numspaces);
581}
582
583void
88379baf
AC
584ui_out_text (struct ui_out *uiout,
585 const char *string)
8b93c638
JM
586{
587 uo_text (uiout, string);
588}
589
590void
88379baf
AC
591ui_out_message (struct ui_out *uiout, int verbosity,
592 const char *format,...)
8b93c638
JM
593{
594 va_list args;
595
596 va_start (args, format);
8b93c638 597 uo_message (uiout, verbosity, format, args);
8b93c638
JM
598 va_end (args);
599}
600
8b93c638 601void
fba45db2 602ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
8b93c638
JM
603{
604 uo_wrap_hint (uiout, identstring);
605}
606
607void
fba45db2 608ui_out_flush (struct ui_out *uiout)
8b93c638
JM
609{
610 uo_flush (uiout);
611}
612
0fac0b41
DJ
613int
614ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
615{
616 return uo_redirect (uiout, outstream);
617}
618
581e13c1 619/* Set the flags specified by the mask given. */
8b93c638 620int
fba45db2 621ui_out_set_flags (struct ui_out *uiout, int mask)
8b93c638 622{
5bfb05ca 623 int oldflags = uiout->flags;
8b93c638 624
b8d86de3 625 uiout->flags |= mask;
8b93c638
JM
626 return oldflags;
627}
628
581e13c1 629/* Clear the flags specified by the mask given. */
8b93c638 630int
fba45db2 631ui_out_clear_flags (struct ui_out *uiout, int mask)
8b93c638 632{
5bfb05ca 633 int oldflags = uiout->flags;
8b93c638
JM
634
635 uiout->flags &= ~mask;
8b93c638
JM
636 return oldflags;
637}
638
581e13c1 639/* Test the flags against the mask given. */
8b93c638 640int
fba45db2 641ui_out_test_flags (struct ui_out *uiout, int mask)
8b93c638
JM
642{
643 return (uiout->flags & mask);
644}
645
581e13c1
MS
646/* Obtain the current verbosity level (as stablished by the
647 'set verbositylevel' command. */
8b93c638
JM
648
649int
fba45db2 650ui_out_get_verblvl (struct ui_out *uiout)
8b93c638 651{
581e13c1 652 /* FIXME: not implemented yet. */
8b93c638
JM
653 return 0;
654}
655
9dc5e2a9
AC
656int
657ui_out_is_mi_like_p (struct ui_out *uiout)
658{
659 return uiout->impl->is_mi_like_p;
660}
661
581e13c1 662/* Default gdb-out hook functions. */
8b93c638
JM
663
664static void
d63f1d40
AC
665default_table_begin (struct ui_out *uiout, int nbrofcols,
666 int nr_rows,
667 const char *tblid)
8b93c638
JM
668{
669}
670
671static void
fba45db2 672default_table_body (struct ui_out *uiout)
8b93c638
JM
673{
674}
675
676static void
fba45db2 677default_table_end (struct ui_out *uiout)
8b93c638
JM
678{
679}
680
681static void
fba45db2 682default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
b25959ec 683 const char *col_name,
e2e11a41 684 const char *colhdr)
8b93c638
JM
685{
686}
687
688static void
631ec795
AC
689default_begin (struct ui_out *uiout,
690 enum ui_out_type type,
691 int level,
692 const char *id)
8b93c638
JM
693{
694}
695
696static void
631ec795
AC
697default_end (struct ui_out *uiout,
698 enum ui_out_type type,
699 int level)
8b93c638
JM
700{
701}
702
703static void
fba45db2 704default_field_int (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
705 enum ui_align align,
706 const char *fldname, int value)
8b93c638
JM
707{
708}
709
710static void
fba45db2 711default_field_skip (struct ui_out *uiout, int fldno, int width,
e2e11a41 712 enum ui_align align, const char *fldname)
8b93c638
JM
713{
714}
715
716static void
717default_field_string (struct ui_out *uiout,
718 int fldno,
719 int width,
720 enum ui_align align,
e2e11a41 721 const char *fldname,
8b93c638
JM
722 const char *string)
723{
724}
725
726static void
fba45db2 727default_field_fmt (struct ui_out *uiout, int fldno, int width,
e2e11a41
AC
728 enum ui_align align,
729 const char *fldname,
730 const char *format,
fba45db2 731 va_list args)
8b93c638
JM
732{
733}
734
735static void
fba45db2 736default_spaces (struct ui_out *uiout, int numspaces)
8b93c638
JM
737{
738}
739
740static void
e2e11a41 741default_text (struct ui_out *uiout, const char *string)
8b93c638
JM
742{
743}
744
745static void
e2e11a41
AC
746default_message (struct ui_out *uiout, int verbosity,
747 const char *format,
fba45db2 748 va_list args)
8b93c638
JM
749{
750}
751
752static void
fba45db2 753default_wrap_hint (struct ui_out *uiout, char *identstring)
8b93c638
JM
754{
755}
756
757static void
fba45db2 758default_flush (struct ui_out *uiout)
8b93c638
JM
759{
760}
761
b65a2bd9
SCR
762static void
763default_data_destroy (struct ui_out *uiout)
764{
765}
766
581e13c1 767/* Interface to the implementation functions. */
8b93c638
JM
768
769void
88379baf 770uo_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 771 int nr_rows,
88379baf 772 const char *tblid)
8b93c638
JM
773{
774 if (!uiout->impl->table_begin)
775 return;
d63f1d40 776 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
8b93c638
JM
777}
778
779void
780uo_table_body (struct ui_out *uiout)
781{
782 if (!uiout->impl->table_body)
783 return;
784 uiout->impl->table_body (uiout);
785}
786
787void
788uo_table_end (struct ui_out *uiout)
789{
790 if (!uiout->impl->table_end)
791 return;
792 uiout->impl->table_end (uiout);
793}
794
795void
88379baf 796uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
b25959ec 797 const char *col_name,
88379baf 798 const char *colhdr)
8b93c638
JM
799{
800 if (!uiout->impl->table_header)
801 return;
b25959ec 802 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
8b93c638
JM
803}
804
b65a2bd9
SCR
805/* Clear the table associated with UIOUT. */
806
807static void
808clear_table (struct ui_out *uiout)
809{
810 if (uiout->table.id)
811 xfree (uiout->table.id);
812 clear_header_list (uiout);
813}
814
8b93c638 815void
631ec795
AC
816uo_begin (struct ui_out *uiout,
817 enum ui_out_type type,
818 int level,
819 const char *id)
8b93c638 820{
631ec795 821 if (uiout->impl->begin == NULL)
8b93c638 822 return;
631ec795 823 uiout->impl->begin (uiout, type, level, id);
8b93c638
JM
824}
825
826void
631ec795
AC
827uo_end (struct ui_out *uiout,
828 enum ui_out_type type,
829 int level)
8b93c638 830{
631ec795 831 if (uiout->impl->end == NULL)
8b93c638 832 return;
631ec795 833 uiout->impl->end (uiout, type, level);
8b93c638
JM
834}
835
836void
88379baf
AC
837uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
838 const char *fldname,
839 int value)
8b93c638
JM
840{
841 if (!uiout->impl->field_int)
842 return;
843 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
844}
845
846void
88379baf
AC
847uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
848 const char *fldname)
8b93c638
JM
849{
850 if (!uiout->impl->field_skip)
851 return;
852 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
853}
854
855void
856uo_field_string (struct ui_out *uiout, int fldno, int width,
88379baf
AC
857 enum ui_align align,
858 const char *fldname,
859 const char *string)
8b93c638
JM
860{
861 if (!uiout->impl->field_string)
862 return;
863 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
864}
865
866void
88379baf
AC
867uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align,
868 const char *fldname,
869 const char *format,
870 va_list args)
8b93c638
JM
871{
872 if (!uiout->impl->field_fmt)
873 return;
874 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
875}
876
877void
878uo_spaces (struct ui_out *uiout, int numspaces)
879{
880 if (!uiout->impl->spaces)
881 return;
882 uiout->impl->spaces (uiout, numspaces);
883}
884
885void
88379baf
AC
886uo_text (struct ui_out *uiout,
887 const char *string)
8b93c638
JM
888{
889 if (!uiout->impl->text)
890 return;
891 uiout->impl->text (uiout, string);
892}
893
894void
88379baf
AC
895uo_message (struct ui_out *uiout, int verbosity,
896 const char *format,
897 va_list args)
8b93c638
JM
898{
899 if (!uiout->impl->message)
900 return;
901 uiout->impl->message (uiout, verbosity, format, args);
902}
903
904void
905uo_wrap_hint (struct ui_out *uiout, char *identstring)
906{
907 if (!uiout->impl->wrap_hint)
908 return;
909 uiout->impl->wrap_hint (uiout, identstring);
910}
911
912void
913uo_flush (struct ui_out *uiout)
914{
915 if (!uiout->impl->flush)
916 return;
917 uiout->impl->flush (uiout);
918}
919
0fac0b41
DJ
920int
921uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
922{
923 if (!uiout->impl->redirect)
924 return -1;
925 uiout->impl->redirect (uiout, outstream);
926 return 0;
927}
928
b65a2bd9
SCR
929void
930uo_data_destroy (struct ui_out *uiout)
931{
932 if (!uiout->impl->data_destroy)
933 return;
934
935 uiout->impl->data_destroy (uiout);
936}
937
8b93c638
JM
938/* local functions */
939
581e13c1 940/* List of column headers manipulation routines. */
8b93c638
JM
941
942static void
fba45db2 943clear_header_list (struct ui_out *uiout)
8b93c638 944{
bafdd3b3 945 while (uiout->table.header_first != NULL)
8b93c638 946 {
bafdd3b3
AC
947 uiout->table.header_next = uiout->table.header_first;
948 uiout->table.header_first = uiout->table.header_first->next;
71bdabee
KS
949 xfree (uiout->table.header_next->colhdr);
950 xfree (uiout->table.header_next->col_name);
bafdd3b3 951 xfree (uiout->table.header_next);
8b93c638 952 }
bafdd3b3
AC
953 gdb_assert (uiout->table.header_first == NULL);
954 uiout->table.header_last = NULL;
955 uiout->table.header_next = NULL;
8b93c638
JM
956}
957
958static void
959append_header_to_list (struct ui_out *uiout,
960 int width,
961 int alignment,
b25959ec 962 const char *col_name,
88379baf 963 const char *colhdr)
8b93c638
JM
964{
965 struct ui_out_hdr *temphdr;
966
967 temphdr = XMALLOC (struct ui_out_hdr);
968 temphdr->width = width;
969 temphdr->alignment = alignment;
44db85f8
MS
970 /* We have to copy the column title as the original may be an
971 automatic. */
8b93c638 972 if (colhdr != NULL)
b25959ec
AC
973 temphdr->colhdr = xstrdup (colhdr);
974 else
975 temphdr->colhdr = NULL;
44db85f8 976
b25959ec 977 if (col_name != NULL)
44db85f8
MS
978 temphdr->col_name = xstrdup (col_name);
979 else if (colhdr != NULL)
b25959ec
AC
980 temphdr->col_name = xstrdup (colhdr);
981 else
44db85f8
MS
982 temphdr->col_name = NULL;
983
8b93c638 984 temphdr->next = NULL;
bafdd3b3 985 if (uiout->table.header_first == NULL)
8b93c638
JM
986 {
987 temphdr->colno = 1;
bafdd3b3
AC
988 uiout->table.header_first = temphdr;
989 uiout->table.header_last = temphdr;
8b93c638
JM
990 }
991 else
992 {
bafdd3b3
AC
993 temphdr->colno = uiout->table.header_last->colno + 1;
994 uiout->table.header_last->next = temphdr;
995 uiout->table.header_last = temphdr;
8b93c638 996 }
bafdd3b3 997 uiout->table.header_next = uiout->table.header_last;
8b93c638
JM
998}
999
7a9dd1b2 1000/* Extract the format information for the NEXT header and advance
bafdd3b3 1001 the header pointer. Return 0 if there was no next header. */
8b93c638
JM
1002
1003static int
bafdd3b3 1004get_next_header (struct ui_out *uiout,
8b93c638
JM
1005 int *colno,
1006 int *width,
1007 int *alignment,
1008 char **colhdr)
1009{
bafdd3b3
AC
1010 /* There may be no headers at all or we may have used all columns. */
1011 if (uiout->table.header_next == NULL)
8b93c638 1012 return 0;
bafdd3b3
AC
1013 *colno = uiout->table.header_next->colno;
1014 *width = uiout->table.header_next->width;
1015 *alignment = uiout->table.header_next->alignment;
1016 *colhdr = uiout->table.header_next->colhdr;
1017 /* Advance the header pointer to the next entry. */
1018 uiout->table.header_next = uiout->table.header_next->next;
8b93c638
JM
1019 return 1;
1020}
1021
a6c47c14
AC
1022
1023/* Verify that the field/tuple/list is correctly positioned. Return
1024 the field number and corresponding alignment (if
1025 available/applicable). */
8b93c638
JM
1026
1027static void
a6c47c14 1028verify_field (struct ui_out *uiout, int *fldno, int *width, int *align)
8b93c638 1029{
a6c47c14
AC
1030 struct ui_out_level *current = current_level (uiout);
1031 char *text;
1032
bafdd3b3 1033 if (uiout->table.flag)
8b93c638 1034 {
bafdd3b3 1035 if (!uiout->table.body_flag)
8e65ff28 1036 internal_error (__FILE__, __LINE__,
e2e0b3e5
AC
1037 _("table_body missing; table fields must be \
1038specified after table_body and inside a list."));
a6c47c14
AC
1039 /* NOTE: cagney/2001-12-08: There was a check here to ensure
1040 that this code was only executed when uiout->level was
1041 greater than zero. That no longer applies - this code is run
1042 before each table row tuple is started and at that point the
1043 level is zero. */
8b93c638 1044 }
8b93c638 1045
a6c47c14 1046 current->field_count += 1;
8b93c638 1047
a6c47c14
AC
1048 if (uiout->table.body_flag
1049 && uiout->table.entry_level == uiout->level
1050 && get_next_header (uiout, fldno, width, align, &text))
8b93c638 1051 {
a6c47c14 1052 if (*fldno != current->field_count)
8e65ff28 1053 internal_error (__FILE__, __LINE__,
e2e0b3e5 1054 _("ui-out internal error in handling headers."));
8b93c638
JM
1055 }
1056 else
1057 {
1058 *width = 0;
1059 *align = ui_noalign;
a6c47c14 1060 *fldno = current->field_count;
8b93c638
JM
1061 }
1062}
1063
a6c47c14 1064
581e13c1 1065/* Access to ui-out members data. */
8b93c638 1066
0a8fce9a 1067void *
8b93c638
JM
1068ui_out_data (struct ui_out *uiout)
1069{
1070 return uiout->data;
1071}
1072
170b53b2
UW
1073/* Access table field parameters. */
1074int
1075ui_out_query_field (struct ui_out *uiout, int colno,
1076 int *width, int *alignment, char **col_name)
1077{
1078 struct ui_out_hdr *hdr;
1079
1080 if (!uiout->table.flag)
1081 return 0;
1082
1083 for (hdr = uiout->table.header_first; hdr; hdr = hdr->next)
1084 if (hdr->colno == colno)
1085 {
1086 *width = hdr->width;
1087 *alignment = hdr->alignment;
1088 *col_name = hdr->col_name;
1089 return 1;
1090 }
1091
1092 return 0;
1093}
1094
581e13c1 1095/* Initalize private members at startup. */
8b93c638
JM
1096
1097struct ui_out *
0a8fce9a 1098ui_out_new (struct ui_out_impl *impl, void *data,
8b93c638
JM
1099 int flags)
1100{
1101 struct ui_out *uiout = XMALLOC (struct ui_out);
54eb231c 1102 struct ui_out_level *current = XMALLOC (struct ui_out_level);
5d502164 1103
8b93c638
JM
1104 uiout->data = data;
1105 uiout->impl = impl;
1106 uiout->flags = flags;
bafdd3b3
AC
1107 uiout->table.flag = 0;
1108 uiout->table.body_flag = 0;
80f49b30 1109 uiout->level = 0;
54eb231c
PM
1110 uiout->levels = NULL;
1111
1112 /* Create uiout->level 0, the default level. */
1113 current->type = ui_out_type_tuple;
1114 current->field_count = 0;
1115 VEC_safe_push (ui_out_level_p, uiout->levels, current);
1116
bafdd3b3
AC
1117 uiout->table.header_first = NULL;
1118 uiout->table.header_last = NULL;
1119 uiout->table.header_next = NULL;
8b93c638
JM
1120 return uiout;
1121}
1122
b65a2bd9
SCR
1123/* Free UIOUT and the memory areas it references. */
1124
1125void
1126ui_out_destroy (struct ui_out *uiout)
1127{
54eb231c
PM
1128 int i;
1129 struct ui_out_level *current;
1130
1131 /* Make sure that all levels are freed in the case where levels have
1132 been pushed, but not popped before the ui_out object is
1133 destroyed. */
1134 for (i = 0;
1135 VEC_iterate (ui_out_level_p, uiout->levels, i, current);
1136 ++i)
1137 xfree (current);
1138
1139 VEC_free (ui_out_level_p, uiout->levels);
b65a2bd9
SCR
1140 uo_data_destroy (uiout);
1141 clear_table (uiout);
1142 xfree (uiout);
1143}
1144
581e13c1 1145/* Standard gdb initialization hook. */
8b93c638
JM
1146
1147void
fba45db2 1148_initialize_ui_out (void)
8b93c638
JM
1149{
1150 /* nothing needs to be done */
1151}