1 /* Output generating routines for GDB.
2 Copyright 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
4 Written by Fernando Nasser for Cygnus.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
25 #include "expression.h" /* For language.h */
29 /* Convenience macro for allocting typesafe memory. */
32 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
34 /* table header structures */
42 struct ui_out_hdr
*next
;
45 /* The ui_out structure */
46 /* Any change here requires a corresponding one in the initialization
47 of the default uiout, which is statically initialized */
52 /* specific implementation of ui-out */
53 struct ui_out_impl
*impl
;
54 struct ui_out_data
*data
;
56 /* if on, a table is being generated */
59 /* if on, the body of a table is being generated */
62 /* number of table columns (as specified in the table_begin call) */
65 /* strinf identifying the table (as specified in the table_begin call) */
68 /* if on, a list is being generated. The value is the level of nesting */
71 /* we count each field; the first element is for non-list fields */
74 /* points to the first header (if any) */
75 struct ui_out_hdr
*headerfirst
;
77 /* points to the last header (if any) */
78 struct ui_out_hdr
*headerlast
;
80 /* points to header of next column to format */
81 struct ui_out_hdr
*headercurr
;
85 /* These are the default implementation functions */
87 static void default_table_begin (struct ui_out
*uiout
, int nbrofcols
,
89 static void default_table_body (struct ui_out
*uiout
);
90 static void default_table_end (struct ui_out
*uiout
);
91 static void default_table_header (struct ui_out
*uiout
, int width
,
92 enum ui_align alig
, char *colhdr
);
93 static void default_list_begin (struct ui_out
*uiout
, int list_flag
,
95 static void default_list_end (struct ui_out
*uiout
, int list_flag
);
96 static void default_field_int (struct ui_out
*uiout
, int fldno
, int width
,
97 enum ui_align alig
, char *fldname
, int value
);
98 static void default_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
99 enum ui_align alig
, char *fldname
);
100 static void default_field_string (struct ui_out
*uiout
, int fldno
, int width
,
101 enum ui_align align
, char *fldname
,
103 static void default_field_fmt (struct ui_out
*uiout
, int fldno
,
104 int width
, enum ui_align align
,
105 char *fldname
, char *format
, va_list args
);
106 static void default_spaces (struct ui_out
*uiout
, int numspaces
);
107 static void default_text (struct ui_out
*uiout
, char *string
);
108 static void default_message (struct ui_out
*uiout
, int verbosity
, char *format
,
110 static void default_wrap_hint (struct ui_out
*uiout
, char *identstring
);
111 static void default_flush (struct ui_out
*uiout
);
113 /* This is the default ui-out implementation functions vector */
115 struct ui_out_impl default_ui_out_impl
=
120 default_table_header
,
125 default_field_string
,
134 /* The default ui_out */
136 struct ui_out def_uiout
=
139 &default_ui_out_impl
, /* impl */
142 /* Pointer to current ui_out */
143 /* FIXME: This should not be a global, but something passed down from main.c
146 struct ui_out
*uiout
= &def_uiout
;
148 /* These are the interfaces to implementation functions */
150 static void uo_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
);
151 static void uo_table_body (struct ui_out
*uiout
);
152 static void uo_table_end (struct ui_out
*uiout
);
153 static void uo_table_header (struct ui_out
*uiout
, int width
,
154 enum ui_align align
, char *colhdr
);
155 static void uo_list_begin (struct ui_out
*uiout
, int list_flag
, char *lstid
);
156 static void uo_list_end (struct ui_out
*uiout
, int list_flag
);
157 static void uo_field_int (struct ui_out
*uiout
, int fldno
, int width
,
158 enum ui_align align
, char *fldname
, int value
);
159 static void uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
160 enum ui_align align
, char *fldname
);
161 static void uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
162 enum ui_align align
, char *fldname
, const char *string
);
163 static void uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
164 enum ui_align align
, char *fldname
,
165 char *format
, va_list args
);
166 static void uo_spaces (struct ui_out
*uiout
, int numspaces
);
167 static void uo_text (struct ui_out
*uiout
, char *string
);
168 static void uo_message (struct ui_out
*uiout
, int verbosity
,
169 char *format
, va_list args
);
170 static void uo_wrap_hint (struct ui_out
*uiout
, char *identstring
);
171 static void uo_flush (struct ui_out
*uiout
);
173 /* Prototypes for local functions */
175 extern void _initialize_ui_out (void);
176 static void append_header_to_list (struct ui_out
*uiout
, int width
, int alignment
, char *colhdr
);
177 static int get_curr_header (struct ui_out
*uiout
, int *colno
, int *width
,
178 int *alignment
, char **colhdr
);
179 static void clear_header_list (struct ui_out
*uiout
);
180 static void verify_field_proper_position (struct ui_out
*uiout
);
181 static void verify_field_alignment (struct ui_out
*uiout
, int fldno
, int *width
, int *alignment
);
183 static void init_ui_out_state (struct ui_out
*uiout
);
185 /* exported functions (ui_out API) */
187 /* Mark beginning of a table */
190 ui_out_table_begin (uiout
, nbrofcols
, tblid
)
191 struct ui_out
*uiout
;
195 if (uiout
->table_flag
)
196 internal_error ("gdb/ui_out.c: tables cannot be nested; table_begin found before \
197 previous table_end.");
199 uiout
->table_flag
= 1;
200 uiout
->table_columns
= nbrofcols
;
202 uiout
->table_id
= xstrdup (tblid
);
204 uiout
->table_id
= NULL
;
205 clear_header_list (uiout
);
207 uo_table_begin (uiout
, nbrofcols
, uiout
->table_id
);
211 ui_out_table_body (uiout
)
212 struct ui_out
*uiout
;
214 if (!uiout
->table_flag
)
215 internal_error ("gdb/ui_out.c: table_body outside a table is not valid; it must be \
216 after a table_begin and before a table_end.");
217 if (uiout
->body_flag
)
218 internal_error ("gdb/ui_out.c: extra table_body call not allowed; there must be \
219 only one table_body after a table_begin and before a table_end.");
220 if (uiout
->headercurr
->colno
!= uiout
->table_columns
)
221 internal_error ("gdb/ui_out.c: number of headers differ from number of table \
224 uiout
->body_flag
= 1;
225 uiout
->headercurr
= uiout
->headerfirst
;
227 uo_table_body (uiout
);
231 ui_out_table_end (uiout
)
232 struct ui_out
*uiout
;
234 if (!uiout
->table_flag
)
235 internal_error ("gdb/ui_out.c: misplaced table_end or missing table_begin.");
237 uiout
->body_flag
= 0;
238 uiout
->table_flag
= 0;
240 uo_table_end (uiout
);
243 free (uiout
->table_id
);
244 clear_header_list (uiout
);
248 ui_out_table_header (uiout
, width
, alignment
, colhdr
)
249 struct ui_out
*uiout
;
251 enum ui_align alignment
;
254 if (!uiout
->table_flag
|| uiout
->body_flag
)
255 internal_error ("ui_out: table header must be specified after table_begin \
256 and before table_body.");
258 append_header_to_list (uiout
, width
, alignment
, colhdr
);
260 uo_table_header (uiout
, width
, alignment
, colhdr
);
264 ui_out_list_begin (uiout
, lstid
)
265 struct ui_out
*uiout
;
268 if (uiout
->table_flag
&& !uiout
->body_flag
)
269 internal_error ("ui_out: table header or table_body expected; lists must be \
270 specified after table_body.");
271 if (uiout
->list_flag
>= 4)
272 internal_error ("ui_out: list depth exceeded; only 4 levels of lists can be \
276 uiout
->field_count
[uiout
->list_flag
] = 0;
277 if (uiout
->table_flag
&& (uiout
->list_flag
== 1))
278 uiout
->headercurr
= uiout
->headerfirst
;
280 uo_list_begin (uiout
, uiout
->list_flag
, lstid
);
284 ui_out_list_end (uiout
)
285 struct ui_out
*uiout
;
287 if (!uiout
->list_flag
)
288 internal_error ("ui_out: misplaced list_end; there is no list to be closed.");
290 uo_list_end (uiout
, uiout
->list_flag
);
296 ui_out_field_int (uiout
, fldname
, value
)
297 struct ui_out
*uiout
;
305 verify_field_proper_position (uiout
);
307 uiout
->field_count
[uiout
->list_flag
] += 1;
308 fldno
= uiout
->field_count
[uiout
->list_flag
];
310 verify_field_alignment (uiout
, fldno
, &width
, &align
);
312 uo_field_int (uiout
, fldno
, width
, align
, fldname
, value
);
316 ui_out_field_core_addr (uiout
, fldname
, address
)
317 struct ui_out
*uiout
;
323 /* FIXME-32x64: need a print_address_numeric with field width */
324 /* print_address_numeric (address, 1, local_stream); */
325 strcpy (addstr
, local_hex_string_custom ((unsigned long) address
, "08l"));
327 ui_out_field_string (uiout
, fldname
, addstr
);
331 ui_out_field_stream (uiout
, fldname
, buf
)
332 struct ui_out
*uiout
;
334 struct ui_stream
*buf
;
337 char *buffer
= ui_file_xstrdup (buf
->stream
, &length
);
338 struct cleanup
*old_cleanup
= make_cleanup (free
, buffer
);
340 ui_out_field_string (uiout
, fldname
, buffer
);
342 ui_out_field_skip (uiout
, fldname
);
343 ui_file_rewind (buf
->stream
);
344 do_cleanups (old_cleanup
);
347 /* used to ommit a field */
350 ui_out_field_skip (uiout
, fldname
)
351 struct ui_out
*uiout
;
358 verify_field_proper_position (uiout
);
360 uiout
->field_count
[uiout
->list_flag
] += 1;
361 fldno
= uiout
->field_count
[uiout
->list_flag
];
363 verify_field_alignment (uiout
, fldno
, &width
, &align
);
365 uo_field_skip (uiout
, fldno
, width
, align
, fldname
);
369 ui_out_field_string (struct ui_out
*uiout
,
377 verify_field_proper_position (uiout
);
379 uiout
->field_count
[uiout
->list_flag
] += 1;
380 fldno
= uiout
->field_count
[uiout
->list_flag
];
382 verify_field_alignment (uiout
, fldno
, &width
, &align
);
384 uo_field_string (uiout
, fldno
, width
, align
, fldname
, string
);
389 ui_out_field_fmt (struct ui_out
*uiout
, char *fldname
, char *format
,...)
396 verify_field_proper_position (uiout
);
398 uiout
->field_count
[uiout
->list_flag
] += 1;
399 fldno
= uiout
->field_count
[uiout
->list_flag
];
401 /* will not align, but has to call anyway */
402 verify_field_alignment (uiout
, fldno
, &width
, &align
);
404 va_start (args
, format
);
406 uo_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
412 ui_out_spaces (uiout
, numspaces
)
413 struct ui_out
*uiout
;
416 uo_spaces (uiout
, numspaces
);
420 ui_out_text (uiout
, string
)
421 struct ui_out
*uiout
;
424 uo_text (uiout
, string
);
428 ui_out_message (struct ui_out
*uiout
, int verbosity
, char *format
,...)
432 va_start (args
, format
);
434 uo_message (uiout
, verbosity
, format
, args
);
440 ui_out_stream_new (uiout
)
441 struct ui_out
*uiout
;
443 struct ui_stream
*tempbuf
;
445 tempbuf
= XMALLOC (struct ui_stream
);
446 tempbuf
->uiout
= uiout
;
447 tempbuf
->stream
= mem_fileopen ();
452 ui_out_stream_delete (buf
)
453 struct ui_stream
*buf
;
455 ui_file_delete (buf
->stream
);
460 do_stream_delete (void *buf
)
462 ui_out_stream_delete (buf
);
466 make_cleanup_ui_out_stream_delete (struct ui_stream
*buf
)
468 return make_cleanup (do_stream_delete
, buf
);
473 ui_out_wrap_hint (uiout
, identstring
)
474 struct ui_out
*uiout
;
477 uo_wrap_hint (uiout
, identstring
);
482 struct ui_out
*uiout
;
487 /* set the flags specified by the mask given */
489 ui_out_set_flags (uiout
, mask
)
490 struct ui_out
*uiout
;
495 uiout
->flags
|= mask
;
500 /* clear the flags specified by the mask given */
502 ui_out_clear_flags (uiout
, mask
)
503 struct ui_out
*uiout
;
508 uiout
->flags
&= ~mask
;
513 /* test the flags against the mask given */
515 ui_out_test_flags (uiout
, mask
)
516 struct ui_out
*uiout
;
519 return (uiout
->flags
& mask
);
522 /* obtain the current verbosity level (as stablished by the
523 'set verbositylevel' command */
526 ui_out_get_verblvl (uiout
)
527 struct ui_out
*uiout
;
529 /* FIXME: not implemented yet */
535 ui_out_result_begin (uiout
, class)
536 struct ui_out
*uiout
;
542 ui_out_result_end (uiout
)
543 struct ui_out
*uiout
;
548 ui_out_info_begin (uiout
, class)
549 struct ui_out
*uiout
;
555 ui_out_info_end (uiout
)
556 struct ui_out
*uiout
;
561 ui_out_notify_begin (uiout
, class)
562 struct ui_out
*uiout
;
568 ui_out_notify_end (uiout
)
569 struct ui_out
*uiout
;
574 ui_out_error_begin (uiout
, class)
575 struct ui_out
*uiout
;
581 ui_out_error_end (uiout
)
582 struct ui_out
*uiout
;
589 gdb_error (ui_out
* uiout
, int severity
, char *format
,...)
595 gdb_query (uiout
, qflags
, qprompt
)
596 struct ui_out
*uiout
;
603 /* default gdb-out hook functions */
606 default_table_begin (uiout
, nbrofcols
, tblid
)
607 struct ui_out
*uiout
;
614 default_table_body (uiout
)
615 struct ui_out
*uiout
;
620 default_table_end (uiout
)
621 struct ui_out
*uiout
;
626 default_table_header (uiout
, width
, alignment
, colhdr
)
627 struct ui_out
*uiout
;
629 enum ui_align alignment
;
635 default_list_begin (uiout
, list_flag
, lstid
)
636 struct ui_out
*uiout
;
643 default_list_end (uiout
, list_flag
)
644 struct ui_out
*uiout
;
650 default_field_int (uiout
, fldno
, width
, align
, fldname
, value
)
651 struct ui_out
*uiout
;
661 default_field_skip (uiout
, fldno
, width
, align
, fldname
)
662 struct ui_out
*uiout
;
671 default_field_string (struct ui_out
*uiout
,
681 default_field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
)
682 struct ui_out
*uiout
;
693 default_spaces (uiout
, numspaces
)
694 struct ui_out
*uiout
;
700 default_text (uiout
, string
)
701 struct ui_out
*uiout
;
707 default_message (uiout
, verbosity
, format
, args
)
708 struct ui_out
*uiout
;
716 default_wrap_hint (uiout
, identstring
)
717 struct ui_out
*uiout
;
723 default_flush (uiout
)
724 struct ui_out
*uiout
;
728 /* Interface to the implementation functions */
731 uo_table_begin (struct ui_out
*uiout
, int nbrofcols
, char *tblid
)
733 if (!uiout
->impl
->table_begin
)
735 uiout
->impl
->table_begin (uiout
, nbrofcols
, tblid
);
739 uo_table_body (struct ui_out
*uiout
)
741 if (!uiout
->impl
->table_body
)
743 uiout
->impl
->table_body (uiout
);
747 uo_table_end (struct ui_out
*uiout
)
749 if (!uiout
->impl
->table_end
)
751 uiout
->impl
->table_end (uiout
);
755 uo_table_header (struct ui_out
*uiout
, int width
, enum ui_align align
, char *colhdr
)
757 if (!uiout
->impl
->table_header
)
759 uiout
->impl
->table_header (uiout
, width
, align
, colhdr
);
763 uo_list_begin (struct ui_out
*uiout
, int list_flag
, char *lstid
)
765 if (!uiout
->impl
->list_begin
)
767 uiout
->impl
->list_begin (uiout
, list_flag
, lstid
);
771 uo_list_end (struct ui_out
*uiout
, int list_flag
)
773 if (!uiout
->impl
->list_end
)
775 uiout
->impl
->list_end (uiout
, list_flag
);
779 uo_field_int (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
, int value
)
781 if (!uiout
->impl
->field_int
)
783 uiout
->impl
->field_int (uiout
, fldno
, width
, align
, fldname
, value
);
787 uo_field_skip (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
)
789 if (!uiout
->impl
->field_skip
)
791 uiout
->impl
->field_skip (uiout
, fldno
, width
, align
, fldname
);
795 uo_field_string (struct ui_out
*uiout
, int fldno
, int width
,
796 enum ui_align align
, char *fldname
, const char *string
)
798 if (!uiout
->impl
->field_string
)
800 uiout
->impl
->field_string (uiout
, fldno
, width
, align
, fldname
, string
);
804 uo_field_fmt (struct ui_out
*uiout
, int fldno
, int width
, enum ui_align align
, char *fldname
, char *format
, va_list args
)
806 if (!uiout
->impl
->field_fmt
)
808 uiout
->impl
->field_fmt (uiout
, fldno
, width
, align
, fldname
, format
, args
);
812 uo_spaces (struct ui_out
*uiout
, int numspaces
)
814 if (!uiout
->impl
->spaces
)
816 uiout
->impl
->spaces (uiout
, numspaces
);
820 uo_text (struct ui_out
*uiout
, char *string
)
822 if (!uiout
->impl
->text
)
824 uiout
->impl
->text (uiout
, string
);
828 uo_message (struct ui_out
*uiout
, int verbosity
, char *format
, va_list args
)
830 if (!uiout
->impl
->message
)
832 uiout
->impl
->message (uiout
, verbosity
, format
, args
);
836 uo_wrap_hint (struct ui_out
*uiout
, char *identstring
)
838 if (!uiout
->impl
->wrap_hint
)
840 uiout
->impl
->wrap_hint (uiout
, identstring
);
844 uo_flush (struct ui_out
*uiout
)
846 if (!uiout
->impl
->flush
)
848 uiout
->impl
->flush (uiout
);
851 /* local functions */
853 /* list of column headers manipulation routines */
856 clear_header_list (uiout
)
857 struct ui_out
*uiout
;
859 while (uiout
->headerfirst
!= NULL
)
861 uiout
->headercurr
= uiout
->headerfirst
;
862 uiout
->headerfirst
= uiout
->headerfirst
->next
;
863 if (uiout
->headercurr
->colhdr
!= NULL
)
864 free (uiout
->headercurr
->colhdr
);
865 free (uiout
->headercurr
);
867 uiout
->headerlast
= NULL
;
868 uiout
->headercurr
= NULL
;
872 append_header_to_list (struct ui_out
*uiout
,
877 struct ui_out_hdr
*temphdr
;
879 temphdr
= XMALLOC (struct ui_out_hdr
);
880 temphdr
->width
= width
;
881 temphdr
->alignment
= alignment
;
882 /* we have to copy the column title as the original may be an automatic */
885 temphdr
->colhdr
= xmalloc (strlen (colhdr
) + 1);
886 strcpy (temphdr
->colhdr
, colhdr
);
888 temphdr
->next
= NULL
;
889 if (uiout
->headerfirst
== NULL
)
892 uiout
->headerfirst
= temphdr
;
893 uiout
->headerlast
= temphdr
;
897 temphdr
->colno
= uiout
->headerlast
->colno
+ 1;
898 uiout
->headerlast
->next
= temphdr
;
899 uiout
->headerlast
= temphdr
;
901 uiout
->headercurr
= uiout
->headerlast
;
904 /* returns 0 if there is no more headers */
907 get_curr_header (struct ui_out
*uiout
,
913 /* There may be no headers at all or we may have used all columns */
914 if (uiout
->headercurr
== NULL
)
916 *colno
= uiout
->headercurr
->colno
;
917 *width
= uiout
->headercurr
->width
;
918 *alignment
= uiout
->headercurr
->alignment
;
919 *colhdr
= uiout
->headercurr
->colhdr
;
920 uiout
->headercurr
= uiout
->headercurr
->next
;
924 /* makes sure the field_* calls were properly placed */
927 verify_field_proper_position (struct ui_out
*uiout
)
929 if (uiout
->table_flag
)
931 if (!uiout
->body_flag
)
932 internal_error ("ui_out: table_body missing; table fields must be \
933 specified after table_body and inside a list.");
934 if (!uiout
->list_flag
)
935 internal_error ("ui_out: list_begin missing; table fields must be \
936 specified after table_body and inside a list.");
940 /* determines what is the alignment policy */
943 verify_field_alignment (struct ui_out
*uiout
,
951 if (uiout
->table_flag
952 && get_curr_header (uiout
, &colno
, width
, align
, &text
))
955 internal_error ("gdb/ui-out.c: ui-out internal error in handling headers.");
964 /* access to ui_out format private members */
967 ui_out_get_field_separator (uiout
)
968 struct ui_out
*uiout
;
972 /* Access to ui-out members data */
975 ui_out_data (struct ui_out
*uiout
)
980 /* initalize private members at startup */
983 ui_out_new (struct ui_out_impl
*impl
,
984 struct ui_out_data
*data
,
987 struct ui_out
*uiout
= XMALLOC (struct ui_out
);
990 uiout
->flags
= flags
;
991 uiout
->table_flag
= 0;
992 uiout
->body_flag
= 0;
993 uiout
->list_flag
= 0;
994 uiout
->field_count
[0] = 0;
995 uiout
->headerfirst
= NULL
;
996 uiout
->headerlast
= NULL
;
997 uiout
->headercurr
= NULL
;
1001 /* standard gdb initialization hook */
1004 _initialize_ui_out ()
1006 /* nothing needs to be done */