merge from gcc
[gdb/gnu.git] / gdb / ui-out.c
blob03b1240ace9882b1174c6590675eb1f27e82393f
1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
8 This file is part of GDB.
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
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "expression.h" /* For language.h */
26 #include "language.h"
27 #include "ui-out.h"
28 #include "gdb_assert.h"
30 /* table header structures */
32 struct ui_out_hdr
34 int colno;
35 int width;
36 int alignment;
37 char *col_name;
38 char *colhdr;
39 struct ui_out_hdr *next;
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
44 top-level result. */
46 enum { MAX_UI_OUT_LEVELS = 8 };
48 struct ui_out_level
50 /* Count each field; the first element is for non-list fields. */
51 int field_count;
52 /* The type of this level. */
53 enum ui_out_type type;
56 /* Define uiout->level vector types and operations. */
57 typedef struct ui_out_level *ui_out_level_p;
58 DEF_VEC_P (ui_out_level_p);
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. */
64 struct ui_out_table
66 /* If on, a table is being generated. */
67 int flag;
69 /* If on, the body of a table is being generated. If off, the table
70 header is being generated. */
71 int body_flag;
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;
78 /* Number of table columns (as specified in the table_begin call). */
79 int columns;
81 /* String identifying the table (as specified in the table_begin
82 call). */
83 char *id;
85 /* Points to the first table header (if any). */
86 struct ui_out_hdr *header_first;
88 /* Points to the last table header (if any). */
89 struct ui_out_hdr *header_last;
91 /* Points to header of NEXT column to format. */
92 struct ui_out_hdr *header_next;
97 /* The ui_out structure */
98 /* Any change here requires a corresponding one in the initialization
99 of the default uiout, which is statically initialized. */
101 struct ui_out
103 int flags;
104 /* Specific implementation of ui-out. */
105 struct ui_out_impl *impl;
106 void *data;
108 /* Current level. */
109 int level;
111 /* Vector to store and track the ui-out levels. */
112 VEC (ui_out_level_p) *levels;
114 /* A table, if any. At present only a single table is supported. */
115 struct ui_out_table table;
118 /* The current (inner most) level. */
119 static struct ui_out_level *
120 current_level (struct ui_out *uiout)
122 return VEC_index (ui_out_level_p, uiout->levels, uiout->level);
125 /* Create a new level, of TYPE. Return the new level's index. */
126 static int
127 push_level (struct ui_out *uiout,
128 enum ui_out_type type,
129 const char *id)
131 struct ui_out_level *current;
133 uiout->level++;
134 current = XMALLOC (struct ui_out_level);
135 current->field_count = 0;
136 current->type = type;
137 VEC_safe_push (ui_out_level_p, uiout->levels, current);
138 return uiout->level;
141 /* Discard the current level, return the discarded level's index.
142 TYPE is the type of the level being discarded. */
143 static int
144 pop_level (struct ui_out *uiout,
145 enum ui_out_type type)
147 struct ui_out_level *current;
149 /* We had better not underflow the buffer. */
150 gdb_assert (uiout->level > 0);
151 gdb_assert (current_level (uiout)->type == type);
152 current = VEC_pop (ui_out_level_p, uiout->levels);
153 xfree (current);
154 uiout->level--;
155 return uiout->level + 1;
159 /* These are the default implementation functions. */
161 static void default_table_begin (struct ui_out *uiout, int nbrofcols,
162 int nr_rows, const char *tblid);
163 static void default_table_body (struct ui_out *uiout);
164 static void default_table_end (struct ui_out *uiout);
165 static void default_table_header (struct ui_out *uiout, int width,
166 enum ui_align alig, const char *col_name,
167 const char *colhdr);
168 static void default_begin (struct ui_out *uiout,
169 enum ui_out_type type,
170 int level, const char *id);
171 static void default_end (struct ui_out *uiout,
172 enum ui_out_type type,
173 int level);
174 static void default_field_int (struct ui_out *uiout, int fldno, int width,
175 enum ui_align alig,
176 const char *fldname,
177 int value);
178 static void default_field_skip (struct ui_out *uiout, int fldno, int width,
179 enum ui_align alig,
180 const char *fldname);
181 static void default_field_string (struct ui_out *uiout, int fldno, int width,
182 enum ui_align align,
183 const char *fldname,
184 const char *string);
185 static void default_field_fmt (struct ui_out *uiout, int fldno,
186 int width, enum ui_align align,
187 const char *fldname,
188 const char *format,
189 va_list args) ATTRIBUTE_PRINTF (6, 0);
190 static void default_spaces (struct ui_out *uiout, int numspaces);
191 static void default_text (struct ui_out *uiout, const char *string);
192 static void default_message (struct ui_out *uiout, int verbosity,
193 const char *format,
194 va_list args) ATTRIBUTE_PRINTF (3, 0);
195 static void default_wrap_hint (struct ui_out *uiout, char *identstring);
196 static void default_flush (struct ui_out *uiout);
197 static void default_data_destroy (struct ui_out *uiout);
199 /* This is the default ui-out implementation functions vector. */
201 struct ui_out_impl default_ui_out_impl =
203 default_table_begin,
204 default_table_body,
205 default_table_end,
206 default_table_header,
207 default_begin,
208 default_end,
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,
217 default_flush,
218 NULL,
219 default_data_destroy,
220 0, /* Does not need MI hacks. */
223 /* The default ui_out */
225 struct ui_out def_uiout =
227 0, /* flags */
228 &default_ui_out_impl, /* impl */
231 /* Pointer to current ui_out */
232 /* FIXME: This should not be a global, but something passed down from main.c
233 or top.c. */
235 struct ui_out *current_uiout = &def_uiout;
237 /* These are the interfaces to implementation functions. */
239 static void uo_table_begin (struct ui_out *uiout, int nbrofcols,
240 int nr_rows, const char *tblid);
241 static void uo_table_body (struct ui_out *uiout);
242 static void uo_table_end (struct ui_out *uiout);
243 static void uo_table_header (struct ui_out *uiout, int width,
244 enum ui_align align, const char *col_name,
245 const char *colhdr);
246 static void uo_begin (struct ui_out *uiout,
247 enum ui_out_type type,
248 int level, const char *id);
249 static void uo_end (struct ui_out *uiout,
250 enum ui_out_type type,
251 int level);
252 static void uo_field_int (struct ui_out *uiout, int fldno, int width,
253 enum ui_align align, const char *fldname, int value);
254 static void uo_field_skip (struct ui_out *uiout, int fldno, int width,
255 enum ui_align align, const char *fldname);
256 static void uo_field_fmt (struct ui_out *uiout, int fldno, int width,
257 enum ui_align align, const char *fldname,
258 const char *format, va_list args)
259 ATTRIBUTE_PRINTF (6, 0);
260 static void uo_spaces (struct ui_out *uiout, int numspaces);
261 static void uo_text (struct ui_out *uiout, const char *string);
262 static void uo_message (struct ui_out *uiout, int verbosity,
263 const char *format, va_list args)
264 ATTRIBUTE_PRINTF (3, 0);
265 static void uo_wrap_hint (struct ui_out *uiout, char *identstring);
266 static void uo_flush (struct ui_out *uiout);
267 static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream);
268 static void uo_data_destroy (struct ui_out *uiout);
270 /* Prototypes for local functions */
272 extern void _initialize_ui_out (void);
273 static void append_header_to_list (struct ui_out *uiout, int width,
274 int alignment, const char *col_name,
275 const char *colhdr);
276 static int get_next_header (struct ui_out *uiout, int *colno, int *width,
277 int *alignment, char **colhdr);
278 static void clear_header_list (struct ui_out *uiout);
279 static void clear_table (struct ui_out *uiout);
280 static void verify_field (struct ui_out *uiout, int *fldno, int *width,
281 int *align);
283 /* exported functions (ui_out API) */
285 /* Mark beginning of a table. */
287 static void
288 ui_out_table_begin (struct ui_out *uiout, int nbrofcols,
289 int nr_rows,
290 const char *tblid)
292 if (uiout->table.flag)
293 internal_error (__FILE__, __LINE__,
294 _("tables cannot be nested; table_begin found before \
295 previous table_end."));
297 uiout->table.flag = 1;
298 uiout->table.body_flag = 0;
299 uiout->table.entry_level = uiout->level + 1;
300 uiout->table.columns = nbrofcols;
301 if (tblid != NULL)
302 uiout->table.id = xstrdup (tblid);
303 else
304 uiout->table.id = NULL;
305 clear_header_list (uiout);
307 uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id);
310 void
311 ui_out_table_body (struct ui_out *uiout)
313 if (!uiout->table.flag)
314 internal_error (__FILE__, __LINE__,
315 _("table_body outside a table is not valid; it must be \
316 after a table_begin and before a table_end."));
317 if (uiout->table.body_flag)
318 internal_error (__FILE__, __LINE__,
319 _("extra table_body call not allowed; there must be \
320 only one table_body after a table_begin and before a table_end."));
321 if (uiout->table.header_next->colno != uiout->table.columns)
322 internal_error (__FILE__, __LINE__,
323 _("number of headers differ from number of table \
324 columns."));
326 uiout->table.body_flag = 1;
327 uiout->table.header_next = uiout->table.header_first;
329 uo_table_body (uiout);
332 static void
333 ui_out_table_end (struct ui_out *uiout)
335 if (!uiout->table.flag)
336 internal_error (__FILE__, __LINE__,
337 _("misplaced table_end or missing table_begin."));
339 uiout->table.entry_level = 0;
340 uiout->table.body_flag = 0;
341 uiout->table.flag = 0;
343 uo_table_end (uiout);
344 clear_table (uiout);
347 void
348 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
349 const char *col_name,
350 const char *colhdr)
352 if (!uiout->table.flag || uiout->table.body_flag)
353 internal_error (__FILE__, __LINE__,
354 _("table header must be specified after table_begin \
355 and before table_body."));
357 append_header_to_list (uiout, width, alignment, col_name, colhdr);
359 uo_table_header (uiout, width, alignment, col_name, colhdr);
362 static void
363 do_cleanup_table_end (void *data)
365 struct ui_out *ui_out = data;
367 ui_out_table_end (ui_out);
370 struct cleanup *
371 make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols,
372 int nr_rows, const char *tblid)
374 ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid);
375 return make_cleanup (do_cleanup_table_end, ui_out);
378 void
379 ui_out_begin (struct ui_out *uiout,
380 enum ui_out_type type,
381 const char *id)
383 int new_level;
385 if (uiout->table.flag && !uiout->table.body_flag)
386 internal_error (__FILE__, __LINE__,
387 _("table header or table_body expected; lists must be \
388 specified after table_body."));
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. */
398 int fldno;
399 int width;
400 int align;
402 verify_field (uiout, &fldno, &width, &align);
405 new_level = push_level (uiout, type, id);
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)
411 uiout->table.header_next = uiout->table.header_first;
413 uo_begin (uiout, type, new_level, id);
416 void
417 ui_out_end (struct ui_out *uiout,
418 enum ui_out_type type)
420 int old_level = pop_level (uiout, type);
422 uo_end (uiout, type, old_level);
425 struct ui_out_end_cleanup_data
427 struct ui_out *uiout;
428 enum ui_out_type type;
431 static void
432 do_cleanup_end (void *data)
434 struct ui_out_end_cleanup_data *end_cleanup_data = data;
436 ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type);
437 xfree (end_cleanup_data);
440 static struct cleanup *
441 make_cleanup_ui_out_end (struct ui_out *uiout,
442 enum ui_out_type type)
444 struct ui_out_end_cleanup_data *end_cleanup_data;
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);
452 struct cleanup *
453 make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout,
454 const char *id)
456 ui_out_begin (uiout, ui_out_type_tuple, id);
457 return make_cleanup_ui_out_end (uiout, ui_out_type_tuple);
460 struct cleanup *
461 make_cleanup_ui_out_list_begin_end (struct ui_out *uiout,
462 const char *id)
464 ui_out_begin (uiout, ui_out_type_list, id);
465 return make_cleanup_ui_out_end (uiout, ui_out_type_list);
468 void
469 ui_out_field_int (struct ui_out *uiout,
470 const char *fldname,
471 int value)
473 int fldno;
474 int width;
475 int align;
477 verify_field (uiout, &fldno, &width, &align);
479 uo_field_int (uiout, fldno, width, align, fldname, value);
482 void
483 ui_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)
489 int fldno;
490 int width;
491 int align;
493 verify_field (uiout, &fldno, &width, &align);
495 uo_field_int (uiout, fldno, input_width, input_align, fldname, value);
498 /* Documented in ui-out.h. */
500 void
501 ui_out_field_core_addr (struct ui_out *uiout,
502 const char *fldname,
503 struct gdbarch *gdbarch,
504 CORE_ADDR address)
506 ui_out_field_string (uiout, fldname,
507 print_core_address (gdbarch, address));
510 void
511 ui_out_field_stream (struct ui_out *uiout,
512 const char *fldname,
513 struct ui_file *stream)
515 long length;
516 char *buffer = ui_file_xstrdup (stream, &length);
517 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
519 if (length > 0)
520 ui_out_field_string (uiout, fldname, buffer);
521 else
522 ui_out_field_skip (uiout, fldname);
523 ui_file_rewind (stream);
524 do_cleanups (old_cleanup);
527 /* Used to omit a field. */
529 void
530 ui_out_field_skip (struct ui_out *uiout,
531 const char *fldname)
533 int fldno;
534 int width;
535 int align;
537 verify_field (uiout, &fldno, &width, &align);
539 uo_field_skip (uiout, fldno, width, align, fldname);
542 void
543 ui_out_field_string (struct ui_out *uiout,
544 const char *fldname,
545 const char *string)
547 int fldno;
548 int width;
549 int align;
551 verify_field (uiout, &fldno, &width, &align);
553 uo_field_string (uiout, fldno, width, align, fldname, string);
556 /* VARARGS */
557 void
558 ui_out_field_fmt (struct ui_out *uiout,
559 const char *fldname,
560 const char *format, ...)
562 va_list args;
563 int fldno;
564 int width;
565 int align;
567 /* Will not align, but has to call anyway. */
568 verify_field (uiout, &fldno, &width, &align);
570 va_start (args, format);
572 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
574 va_end (args);
577 void
578 ui_out_spaces (struct ui_out *uiout, int numspaces)
580 uo_spaces (uiout, numspaces);
583 void
584 ui_out_text (struct ui_out *uiout,
585 const char *string)
587 uo_text (uiout, string);
590 void
591 ui_out_message (struct ui_out *uiout, int verbosity,
592 const char *format,...)
594 va_list args;
596 va_start (args, format);
597 uo_message (uiout, verbosity, format, args);
598 va_end (args);
601 void
602 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
604 uo_wrap_hint (uiout, identstring);
607 void
608 ui_out_flush (struct ui_out *uiout)
610 uo_flush (uiout);
614 ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream)
616 return uo_redirect (uiout, outstream);
619 /* Set the flags specified by the mask given. */
621 ui_out_set_flags (struct ui_out *uiout, int mask)
623 int oldflags = uiout->flags;
625 uiout->flags |= mask;
626 return oldflags;
629 /* Clear the flags specified by the mask given. */
631 ui_out_clear_flags (struct ui_out *uiout, int mask)
633 int oldflags = uiout->flags;
635 uiout->flags &= ~mask;
636 return oldflags;
639 /* Test the flags against the mask given. */
641 ui_out_test_flags (struct ui_out *uiout, int mask)
643 return (uiout->flags & mask);
646 /* Obtain the current verbosity level (as stablished by the
647 'set verbositylevel' command. */
650 ui_out_get_verblvl (struct ui_out *uiout)
652 /* FIXME: not implemented yet. */
653 return 0;
657 ui_out_is_mi_like_p (struct ui_out *uiout)
659 return uiout->impl->is_mi_like_p;
662 /* Default gdb-out hook functions. */
664 static void
665 default_table_begin (struct ui_out *uiout, int nbrofcols,
666 int nr_rows,
667 const char *tblid)
671 static void
672 default_table_body (struct ui_out *uiout)
676 static void
677 default_table_end (struct ui_out *uiout)
681 static void
682 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
683 const char *col_name,
684 const char *colhdr)
688 static void
689 default_begin (struct ui_out *uiout,
690 enum ui_out_type type,
691 int level,
692 const char *id)
696 static void
697 default_end (struct ui_out *uiout,
698 enum ui_out_type type,
699 int level)
703 static void
704 default_field_int (struct ui_out *uiout, int fldno, int width,
705 enum ui_align align,
706 const char *fldname, int value)
710 static void
711 default_field_skip (struct ui_out *uiout, int fldno, int width,
712 enum ui_align align, const char *fldname)
716 static void
717 default_field_string (struct ui_out *uiout,
718 int fldno,
719 int width,
720 enum ui_align align,
721 const char *fldname,
722 const char *string)
726 static void
727 default_field_fmt (struct ui_out *uiout, int fldno, int width,
728 enum ui_align align,
729 const char *fldname,
730 const char *format,
731 va_list args)
735 static void
736 default_spaces (struct ui_out *uiout, int numspaces)
740 static void
741 default_text (struct ui_out *uiout, const char *string)
745 static void
746 default_message (struct ui_out *uiout, int verbosity,
747 const char *format,
748 va_list args)
752 static void
753 default_wrap_hint (struct ui_out *uiout, char *identstring)
757 static void
758 default_flush (struct ui_out *uiout)
762 static void
763 default_data_destroy (struct ui_out *uiout)
767 /* Interface to the implementation functions. */
769 void
770 uo_table_begin (struct ui_out *uiout, int nbrofcols,
771 int nr_rows,
772 const char *tblid)
774 if (!uiout->impl->table_begin)
775 return;
776 uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid);
779 void
780 uo_table_body (struct ui_out *uiout)
782 if (!uiout->impl->table_body)
783 return;
784 uiout->impl->table_body (uiout);
787 void
788 uo_table_end (struct ui_out *uiout)
790 if (!uiout->impl->table_end)
791 return;
792 uiout->impl->table_end (uiout);
795 void
796 uo_table_header (struct ui_out *uiout, int width, enum ui_align align,
797 const char *col_name,
798 const char *colhdr)
800 if (!uiout->impl->table_header)
801 return;
802 uiout->impl->table_header (uiout, width, align, col_name, colhdr);
805 /* Clear the table associated with UIOUT. */
807 static void
808 clear_table (struct ui_out *uiout)
810 if (uiout->table.id)
811 xfree (uiout->table.id);
812 clear_header_list (uiout);
815 void
816 uo_begin (struct ui_out *uiout,
817 enum ui_out_type type,
818 int level,
819 const char *id)
821 if (uiout->impl->begin == NULL)
822 return;
823 uiout->impl->begin (uiout, type, level, id);
826 void
827 uo_end (struct ui_out *uiout,
828 enum ui_out_type type,
829 int level)
831 if (uiout->impl->end == NULL)
832 return;
833 uiout->impl->end (uiout, type, level);
836 void
837 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align,
838 const char *fldname,
839 int value)
841 if (!uiout->impl->field_int)
842 return;
843 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
846 void
847 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align,
848 const char *fldname)
850 if (!uiout->impl->field_skip)
851 return;
852 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
855 void
856 uo_field_string (struct ui_out *uiout, int fldno, int width,
857 enum ui_align align,
858 const char *fldname,
859 const char *string)
861 if (!uiout->impl->field_string)
862 return;
863 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
866 void
867 uo_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)
872 if (!uiout->impl->field_fmt)
873 return;
874 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
877 void
878 uo_spaces (struct ui_out *uiout, int numspaces)
880 if (!uiout->impl->spaces)
881 return;
882 uiout->impl->spaces (uiout, numspaces);
885 void
886 uo_text (struct ui_out *uiout,
887 const char *string)
889 if (!uiout->impl->text)
890 return;
891 uiout->impl->text (uiout, string);
894 void
895 uo_message (struct ui_out *uiout, int verbosity,
896 const char *format,
897 va_list args)
899 if (!uiout->impl->message)
900 return;
901 uiout->impl->message (uiout, verbosity, format, args);
904 void
905 uo_wrap_hint (struct ui_out *uiout, char *identstring)
907 if (!uiout->impl->wrap_hint)
908 return;
909 uiout->impl->wrap_hint (uiout, identstring);
912 void
913 uo_flush (struct ui_out *uiout)
915 if (!uiout->impl->flush)
916 return;
917 uiout->impl->flush (uiout);
921 uo_redirect (struct ui_out *uiout, struct ui_file *outstream)
923 if (!uiout->impl->redirect)
924 return -1;
925 uiout->impl->redirect (uiout, outstream);
926 return 0;
929 void
930 uo_data_destroy (struct ui_out *uiout)
932 if (!uiout->impl->data_destroy)
933 return;
935 uiout->impl->data_destroy (uiout);
938 /* local functions */
940 /* List of column headers manipulation routines. */
942 static void
943 clear_header_list (struct ui_out *uiout)
945 while (uiout->table.header_first != NULL)
947 uiout->table.header_next = uiout->table.header_first;
948 uiout->table.header_first = uiout->table.header_first->next;
949 xfree (uiout->table.header_next->colhdr);
950 xfree (uiout->table.header_next->col_name);
951 xfree (uiout->table.header_next);
953 gdb_assert (uiout->table.header_first == NULL);
954 uiout->table.header_last = NULL;
955 uiout->table.header_next = NULL;
958 static void
959 append_header_to_list (struct ui_out *uiout,
960 int width,
961 int alignment,
962 const char *col_name,
963 const char *colhdr)
965 struct ui_out_hdr *temphdr;
967 temphdr = XMALLOC (struct ui_out_hdr);
968 temphdr->width = width;
969 temphdr->alignment = alignment;
970 /* We have to copy the column title as the original may be an
971 automatic. */
972 if (colhdr != NULL)
973 temphdr->colhdr = xstrdup (colhdr);
974 else
975 temphdr->colhdr = NULL;
977 if (col_name != NULL)
978 temphdr->col_name = xstrdup (col_name);
979 else if (colhdr != NULL)
980 temphdr->col_name = xstrdup (colhdr);
981 else
982 temphdr->col_name = NULL;
984 temphdr->next = NULL;
985 if (uiout->table.header_first == NULL)
987 temphdr->colno = 1;
988 uiout->table.header_first = temphdr;
989 uiout->table.header_last = temphdr;
991 else
993 temphdr->colno = uiout->table.header_last->colno + 1;
994 uiout->table.header_last->next = temphdr;
995 uiout->table.header_last = temphdr;
997 uiout->table.header_next = uiout->table.header_last;
1000 /* Extract the format information for the NEXT header and advance
1001 the header pointer. Return 0 if there was no next header. */
1003 static int
1004 get_next_header (struct ui_out *uiout,
1005 int *colno,
1006 int *width,
1007 int *alignment,
1008 char **colhdr)
1010 /* There may be no headers at all or we may have used all columns. */
1011 if (uiout->table.header_next == NULL)
1012 return 0;
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;
1019 return 1;
1023 /* Verify that the field/tuple/list is correctly positioned. Return
1024 the field number and corresponding alignment (if
1025 available/applicable). */
1027 static void
1028 verify_field (struct ui_out *uiout, int *fldno, int *width, int *align)
1030 struct ui_out_level *current = current_level (uiout);
1031 char *text;
1033 if (uiout->table.flag)
1035 if (!uiout->table.body_flag)
1036 internal_error (__FILE__, __LINE__,
1037 _("table_body missing; table fields must be \
1038 specified after table_body and inside a list."));
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. */
1046 current->field_count += 1;
1048 if (uiout->table.body_flag
1049 && uiout->table.entry_level == uiout->level
1050 && get_next_header (uiout, fldno, width, align, &text))
1052 if (*fldno != current->field_count)
1053 internal_error (__FILE__, __LINE__,
1054 _("ui-out internal error in handling headers."));
1056 else
1058 *width = 0;
1059 *align = ui_noalign;
1060 *fldno = current->field_count;
1065 /* Access to ui-out members data. */
1067 void *
1068 ui_out_data (struct ui_out *uiout)
1070 return uiout->data;
1073 /* Access table field parameters. */
1075 ui_out_query_field (struct ui_out *uiout, int colno,
1076 int *width, int *alignment, char **col_name)
1078 struct ui_out_hdr *hdr;
1080 if (!uiout->table.flag)
1081 return 0;
1083 for (hdr = uiout->table.header_first; hdr; hdr = hdr->next)
1084 if (hdr->colno == colno)
1086 *width = hdr->width;
1087 *alignment = hdr->alignment;
1088 *col_name = hdr->col_name;
1089 return 1;
1092 return 0;
1095 /* Initalize private members at startup. */
1097 struct ui_out *
1098 ui_out_new (struct ui_out_impl *impl, void *data,
1099 int flags)
1101 struct ui_out *uiout = XMALLOC (struct ui_out);
1102 struct ui_out_level *current = XMALLOC (struct ui_out_level);
1104 uiout->data = data;
1105 uiout->impl = impl;
1106 uiout->flags = flags;
1107 uiout->table.flag = 0;
1108 uiout->table.body_flag = 0;
1109 uiout->level = 0;
1110 uiout->levels = NULL;
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);
1117 uiout->table.header_first = NULL;
1118 uiout->table.header_last = NULL;
1119 uiout->table.header_next = NULL;
1120 return uiout;
1123 /* Free UIOUT and the memory areas it references. */
1125 void
1126 ui_out_destroy (struct ui_out *uiout)
1128 int i;
1129 struct ui_out_level *current;
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);
1139 VEC_free (ui_out_level_p, uiout->levels);
1140 uo_data_destroy (uiout);
1141 clear_table (uiout);
1142 xfree (uiout);
1145 /* Standard gdb initialization hook. */
1147 void
1148 _initialize_ui_out (void)
1150 /* nothing needs to be done */