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 (struct ui_out *uiout, int nbrofcols, char *tblid)
192 if (uiout->table_flag)
193 internal_error ("gdb/ui_out.c: tables cannot be nested; table_begin found before \
194 previous table_end.");
196 uiout->table_flag = 1;
197 uiout->table_columns = nbrofcols;
199 uiout->table_id = xstrdup (tblid);
201 uiout->table_id = NULL;
202 clear_header_list (uiout);
204 uo_table_begin (uiout, nbrofcols, uiout->table_id);
208 ui_out_table_body (struct ui_out *uiout)
210 if (!uiout->table_flag)
211 internal_error ("gdb/ui_out.c: table_body outside a table is not valid; it must be \
212 after a table_begin and before a table_end.");
213 if (uiout->body_flag)
214 internal_error ("gdb/ui_out.c: extra table_body call not allowed; there must be \
215 only one table_body after a table_begin and before a table_end.");
216 if (uiout->headercurr->colno != uiout->table_columns)
217 internal_error ("gdb/ui_out.c: number of headers differ from number of table \
220 uiout->body_flag = 1;
221 uiout->headercurr = uiout->headerfirst;
223 uo_table_body (uiout);
227 ui_out_table_end (struct ui_out *uiout)
229 if (!uiout->table_flag)
230 internal_error ("gdb/ui_out.c: misplaced table_end or missing table_begin.");
232 uiout->body_flag = 0;
233 uiout->table_flag = 0;
235 uo_table_end (uiout);
238 xfree (uiout->table_id);
239 clear_header_list (uiout);
243 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
246 if (!uiout->table_flag || uiout->body_flag)
247 internal_error ("ui_out: table header must be specified after table_begin \
248 and before table_body.");
250 append_header_to_list (uiout, width, alignment, colhdr);
252 uo_table_header (uiout, width, alignment, colhdr);
256 ui_out_list_begin (struct ui_out *uiout, char *lstid)
258 if (uiout->table_flag && !uiout->body_flag)
259 internal_error ("ui_out: table header or table_body expected; lists must be \
260 specified after table_body.");
261 if (uiout->list_flag >= 4)
262 internal_error ("ui_out: list depth exceeded; only 4 levels of lists can be \
266 uiout->field_count[uiout->list_flag] = 0;
267 if (uiout->table_flag && (uiout->list_flag == 1))
268 uiout->headercurr = uiout->headerfirst;
270 uo_list_begin (uiout, uiout->list_flag, lstid);
274 ui_out_list_end (struct ui_out *uiout)
276 if (!uiout->list_flag)
277 internal_error ("ui_out: misplaced list_end; there is no list to be closed.");
279 uo_list_end (uiout, uiout->list_flag);
285 ui_out_field_int (struct ui_out *uiout, char *fldname, int value)
291 verify_field_proper_position (uiout);
293 uiout->field_count[uiout->list_flag] += 1;
294 fldno = uiout->field_count[uiout->list_flag];
296 verify_field_alignment (uiout, fldno, &width, &align);
298 uo_field_int (uiout, fldno, width, align, fldname, value);
302 ui_out_field_core_addr (struct ui_out *uiout, char *fldname, CORE_ADDR address)
306 /* FIXME-32x64: need a print_address_numeric with field width */
307 /* print_address_numeric (address, 1, local_stream); */
308 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
310 ui_out_field_string (uiout, fldname, addstr);
314 ui_out_field_stream (struct ui_out *uiout, char *fldname, struct ui_stream *buf)
317 char *buffer = ui_file_xstrdup (buf->stream, &length);
318 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
320 ui_out_field_string (uiout, fldname, buffer);
322 ui_out_field_skip (uiout, fldname);
323 ui_file_rewind (buf->stream);
324 do_cleanups (old_cleanup);
327 /* used to ommit a field */
330 ui_out_field_skip (struct ui_out *uiout, char *fldname)
336 verify_field_proper_position (uiout);
338 uiout->field_count[uiout->list_flag] += 1;
339 fldno = uiout->field_count[uiout->list_flag];
341 verify_field_alignment (uiout, fldno, &width, &align);
343 uo_field_skip (uiout, fldno, width, align, fldname);
347 ui_out_field_string (struct ui_out *uiout,
355 verify_field_proper_position (uiout);
357 uiout->field_count[uiout->list_flag] += 1;
358 fldno = uiout->field_count[uiout->list_flag];
360 verify_field_alignment (uiout, fldno, &width, &align);
362 uo_field_string (uiout, fldno, width, align, fldname, string);
367 ui_out_field_fmt (struct ui_out *uiout, char *fldname, char *format,...)
374 verify_field_proper_position (uiout);
376 uiout->field_count[uiout->list_flag] += 1;
377 fldno = uiout->field_count[uiout->list_flag];
379 /* will not align, but has to call anyway */
380 verify_field_alignment (uiout, fldno, &width, &align);
382 va_start (args, format);
384 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
390 ui_out_spaces (struct ui_out *uiout, int numspaces)
392 uo_spaces (uiout, numspaces);
396 ui_out_text (struct ui_out *uiout, char *string)
398 uo_text (uiout, string);
402 ui_out_message (struct ui_out *uiout, int verbosity, char *format,...)
406 va_start (args, format);
408 uo_message (uiout, verbosity, format, args);
414 ui_out_stream_new (struct ui_out *uiout)
416 struct ui_stream *tempbuf;
418 tempbuf = XMALLOC (struct ui_stream);
419 tempbuf->uiout = uiout;
420 tempbuf->stream = mem_fileopen ();
425 ui_out_stream_delete (struct ui_stream *buf)
427 ui_file_delete (buf->stream);
432 do_stream_delete (void *buf)
434 ui_out_stream_delete (buf);
438 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
440 return make_cleanup (do_stream_delete, buf);
445 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
447 uo_wrap_hint (uiout, identstring);
451 ui_out_flush (struct ui_out *uiout)
456 /* set the flags specified by the mask given */
458 ui_out_set_flags (struct ui_out *uiout, int mask)
460 int oldflags = uiout->flags;
462 uiout->flags |= mask;
467 /* clear the flags specified by the mask given */
469 ui_out_clear_flags (struct ui_out *uiout, int mask)
471 int oldflags = uiout->flags;
473 uiout->flags &= ~mask;
478 /* test the flags against the mask given */
480 ui_out_test_flags (struct ui_out *uiout, int mask)
482 return (uiout->flags & mask);
485 /* obtain the current verbosity level (as stablished by the
486 'set verbositylevel' command */
489 ui_out_get_verblvl (struct ui_out *uiout)
491 /* FIXME: not implemented yet */
497 ui_out_result_begin (struct ui_out *uiout, char *class)
502 ui_out_result_end (struct ui_out *uiout)
507 ui_out_info_begin (struct ui_out *uiout, char *class)
512 ui_out_info_end (struct ui_out *uiout)
517 ui_out_notify_begin (struct ui_out *uiout, char *class)
522 ui_out_notify_end (struct ui_out *uiout)
527 ui_out_error_begin (struct ui_out *uiout, char *class)
532 ui_out_error_end (struct ui_out *uiout)
539 gdb_error (ui_out * uiout, int severity, char *format,...)
545 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
550 /* default gdb-out hook functions */
553 default_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid)
558 default_table_body (struct ui_out *uiout)
563 default_table_end (struct ui_out *uiout)
568 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
574 default_list_begin (struct ui_out *uiout, int list_flag, char *lstid)
579 default_list_end (struct ui_out *uiout, int list_flag)
584 default_field_int (struct ui_out *uiout, int fldno, int width,
585 enum ui_align align, char *fldname, int value)
590 default_field_skip (struct ui_out *uiout, int fldno, int width,
591 enum ui_align align, char *fldname)
596 default_field_string (struct ui_out *uiout,
606 default_field_fmt (struct ui_out *uiout, int fldno, int width,
607 enum ui_align align, char *fldname, char *format,
613 default_spaces (struct ui_out *uiout, int numspaces)
618 default_text (struct ui_out *uiout, char *string)
623 default_message (struct ui_out *uiout, int verbosity, char *format,
629 default_wrap_hint (struct ui_out *uiout, char *identstring)
634 default_flush (struct ui_out *uiout)
638 /* Interface to the implementation functions */
641 uo_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid)
643 if (!uiout->impl->table_begin)
645 uiout->impl->table_begin (uiout, nbrofcols, tblid);
649 uo_table_body (struct ui_out *uiout)
651 if (!uiout->impl->table_body)
653 uiout->impl->table_body (uiout);
657 uo_table_end (struct ui_out *uiout)
659 if (!uiout->impl->table_end)
661 uiout->impl->table_end (uiout);
665 uo_table_header (struct ui_out *uiout, int width, enum ui_align align, char *colhdr)
667 if (!uiout->impl->table_header)
669 uiout->impl->table_header (uiout, width, align, colhdr);
673 uo_list_begin (struct ui_out *uiout, int list_flag, char *lstid)
675 if (!uiout->impl->list_begin)
677 uiout->impl->list_begin (uiout, list_flag, lstid);
681 uo_list_end (struct ui_out *uiout, int list_flag)
683 if (!uiout->impl->list_end)
685 uiout->impl->list_end (uiout, list_flag);
689 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname, int value)
691 if (!uiout->impl->field_int)
693 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
697 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname)
699 if (!uiout->impl->field_skip)
701 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
705 uo_field_string (struct ui_out *uiout, int fldno, int width,
706 enum ui_align align, char *fldname, const char *string)
708 if (!uiout->impl->field_string)
710 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
714 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname, char *format, va_list args)
716 if (!uiout->impl->field_fmt)
718 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
722 uo_spaces (struct ui_out *uiout, int numspaces)
724 if (!uiout->impl->spaces)
726 uiout->impl->spaces (uiout, numspaces);
730 uo_text (struct ui_out *uiout, char *string)
732 if (!uiout->impl->text)
734 uiout->impl->text (uiout, string);
738 uo_message (struct ui_out *uiout, int verbosity, char *format, va_list args)
740 if (!uiout->impl->message)
742 uiout->impl->message (uiout, verbosity, format, args);
746 uo_wrap_hint (struct ui_out *uiout, char *identstring)
748 if (!uiout->impl->wrap_hint)
750 uiout->impl->wrap_hint (uiout, identstring);
754 uo_flush (struct ui_out *uiout)
756 if (!uiout->impl->flush)
758 uiout->impl->flush (uiout);
761 /* local functions */
763 /* list of column headers manipulation routines */
766 clear_header_list (struct ui_out *uiout)
768 while (uiout->headerfirst != NULL)
770 uiout->headercurr = uiout->headerfirst;
771 uiout->headerfirst = uiout->headerfirst->next;
772 if (uiout->headercurr->colhdr != NULL)
773 xfree (uiout->headercurr->colhdr);
774 xfree (uiout->headercurr);
776 uiout->headerlast = NULL;
777 uiout->headercurr = NULL;
781 append_header_to_list (struct ui_out *uiout,
786 struct ui_out_hdr *temphdr;
788 temphdr = XMALLOC (struct ui_out_hdr);
789 temphdr->width = width;
790 temphdr->alignment = alignment;
791 /* we have to copy the column title as the original may be an automatic */
794 temphdr->colhdr = xmalloc (strlen (colhdr) + 1);
795 strcpy (temphdr->colhdr, colhdr);
797 temphdr->next = NULL;
798 if (uiout->headerfirst == NULL)
801 uiout->headerfirst = temphdr;
802 uiout->headerlast = temphdr;
806 temphdr->colno = uiout->headerlast->colno + 1;
807 uiout->headerlast->next = temphdr;
808 uiout->headerlast = temphdr;
810 uiout->headercurr = uiout->headerlast;
813 /* returns 0 if there is no more headers */
816 get_curr_header (struct ui_out *uiout,
822 /* There may be no headers at all or we may have used all columns */
823 if (uiout->headercurr == NULL)
825 *colno = uiout->headercurr->colno;
826 *width = uiout->headercurr->width;
827 *alignment = uiout->headercurr->alignment;
828 *colhdr = uiout->headercurr->colhdr;
829 uiout->headercurr = uiout->headercurr->next;
833 /* makes sure the field_* calls were properly placed */
836 verify_field_proper_position (struct ui_out *uiout)
838 if (uiout->table_flag)
840 if (!uiout->body_flag)
841 internal_error ("ui_out: table_body missing; table fields must be \
842 specified after table_body and inside a list.");
843 if (!uiout->list_flag)
844 internal_error ("ui_out: list_begin missing; table fields must be \
845 specified after table_body and inside a list.");
849 /* determines what is the alignment policy */
852 verify_field_alignment (struct ui_out *uiout,
860 if (uiout->table_flag
861 && get_curr_header (uiout, &colno, width, align, &text))
864 internal_error ("gdb/ui-out.c: ui-out internal error in handling headers.");
873 /* access to ui_out format private members */
876 ui_out_get_field_separator (struct ui_out *uiout)
880 /* Access to ui-out members data */
883 ui_out_data (struct ui_out *uiout)
888 /* initalize private members at startup */
891 ui_out_new (struct ui_out_impl *impl,
892 struct ui_out_data *data,
895 struct ui_out *uiout = XMALLOC (struct ui_out);
898 uiout->flags = flags;
899 uiout->table_flag = 0;
900 uiout->body_flag = 0;
901 uiout->list_flag = 0;
902 uiout->field_count[0] = 0;
903 uiout->headerfirst = NULL;
904 uiout->headerlast = NULL;
905 uiout->headercurr = NULL;
909 /* standard gdb initialization hook */
912 _initialize_ui_out (void)
914 /* nothing needs to be done */