1 /* Output generating routines for GDB.
2 Copyright 1999, 2000, 2001 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 (__FILE__, __LINE__,
194 "tables cannot be nested; table_begin found before \
195 previous table_end.");
197 uiout->table_flag = 1;
198 uiout->table_columns = nbrofcols;
200 uiout->table_id = xstrdup (tblid);
202 uiout->table_id = NULL;
203 clear_header_list (uiout);
205 uo_table_begin (uiout, nbrofcols, uiout->table_id);
209 ui_out_table_body (struct ui_out *uiout)
211 if (!uiout->table_flag)
212 internal_error (__FILE__, __LINE__,
213 "table_body outside a table is not valid; it must be \
214 after a table_begin and before a table_end.");
215 if (uiout->body_flag)
216 internal_error (__FILE__, __LINE__,
217 "extra table_body call not allowed; there must be \
218 only one table_body after a table_begin and before a table_end.");
219 if (uiout->headercurr->colno != uiout->table_columns)
220 internal_error (__FILE__, __LINE__,
221 "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 (struct ui_out *uiout)
233 if (!uiout->table_flag)
234 internal_error (__FILE__, __LINE__,
235 "misplaced table_end or missing table_begin.");
237 uiout->body_flag = 0;
238 uiout->table_flag = 0;
240 uo_table_end (uiout);
243 xfree (uiout->table_id);
244 clear_header_list (uiout);
248 ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
251 if (!uiout->table_flag || uiout->body_flag)
252 internal_error (__FILE__, __LINE__,
253 "table header must be specified after table_begin \
254 and before table_body.");
256 append_header_to_list (uiout, width, alignment, colhdr);
258 uo_table_header (uiout, width, alignment, colhdr);
262 ui_out_list_begin (struct ui_out *uiout, char *lstid)
264 if (uiout->table_flag && !uiout->body_flag)
265 internal_error (__FILE__, __LINE__,
266 "table header or table_body expected; lists must be \
267 specified after table_body.");
268 if (uiout->list_flag >= 4)
269 internal_error (__FILE__, __LINE__,
270 "list depth exceeded; only 4 levels of lists can be \
274 uiout->field_count[uiout->list_flag] = 0;
275 if (uiout->table_flag && (uiout->list_flag == 1))
276 uiout->headercurr = uiout->headerfirst;
278 uo_list_begin (uiout, uiout->list_flag, lstid);
282 ui_out_list_end (struct ui_out *uiout)
284 if (!uiout->list_flag)
285 internal_error (__FILE__, __LINE__,
286 "misplaced list_end; there is no list to be closed.");
288 uo_list_end (uiout, uiout->list_flag);
294 do_list_end (void *uiout)
296 ui_out_list_end (uiout);
300 make_cleanup_ui_out_list_end (struct ui_out *uiout)
302 return make_cleanup (do_list_end, uiout);
306 ui_out_field_int (struct ui_out *uiout, char *fldname, int value)
312 verify_field_proper_position (uiout);
314 uiout->field_count[uiout->list_flag] += 1;
315 fldno = uiout->field_count[uiout->list_flag];
317 verify_field_alignment (uiout, fldno, &width, &align);
319 uo_field_int (uiout, fldno, width, align, fldname, value);
323 ui_out_field_core_addr (struct ui_out *uiout, char *fldname, CORE_ADDR address)
327 /* FIXME-32x64: need a print_address_numeric with field width */
328 /* print_address_numeric (address, 1, local_stream); */
329 strcpy (addstr, local_hex_string_custom ((unsigned long) address, "08l"));
331 ui_out_field_string (uiout, fldname, addstr);
335 ui_out_field_stream (struct ui_out *uiout, char *fldname, struct ui_stream *buf)
338 char *buffer = ui_file_xstrdup (buf->stream, &length);
339 struct cleanup *old_cleanup = make_cleanup (xfree, buffer);
341 ui_out_field_string (uiout, fldname, buffer);
343 ui_out_field_skip (uiout, fldname);
344 ui_file_rewind (buf->stream);
345 do_cleanups (old_cleanup);
348 /* used to ommit a field */
351 ui_out_field_skip (struct ui_out *uiout, char *fldname)
357 verify_field_proper_position (uiout);
359 uiout->field_count[uiout->list_flag] += 1;
360 fldno = uiout->field_count[uiout->list_flag];
362 verify_field_alignment (uiout, fldno, &width, &align);
364 uo_field_skip (uiout, fldno, width, align, fldname);
368 ui_out_field_string (struct ui_out *uiout,
376 verify_field_proper_position (uiout);
378 uiout->field_count[uiout->list_flag] += 1;
379 fldno = uiout->field_count[uiout->list_flag];
381 verify_field_alignment (uiout, fldno, &width, &align);
383 uo_field_string (uiout, fldno, width, align, fldname, string);
388 ui_out_field_fmt (struct ui_out *uiout, char *fldname, char *format,...)
395 verify_field_proper_position (uiout);
397 uiout->field_count[uiout->list_flag] += 1;
398 fldno = uiout->field_count[uiout->list_flag];
400 /* will not align, but has to call anyway */
401 verify_field_alignment (uiout, fldno, &width, &align);
403 va_start (args, format);
405 uo_field_fmt (uiout, fldno, width, align, fldname, format, args);
411 ui_out_spaces (struct ui_out *uiout, int numspaces)
413 uo_spaces (uiout, numspaces);
417 ui_out_text (struct ui_out *uiout, char *string)
419 uo_text (uiout, string);
423 ui_out_message (struct ui_out *uiout, int verbosity, char *format,...)
427 va_start (args, format);
429 uo_message (uiout, verbosity, format, args);
435 ui_out_stream_new (struct ui_out *uiout)
437 struct ui_stream *tempbuf;
439 tempbuf = XMALLOC (struct ui_stream);
440 tempbuf->uiout = uiout;
441 tempbuf->stream = mem_fileopen ();
446 ui_out_stream_delete (struct ui_stream *buf)
448 ui_file_delete (buf->stream);
453 do_stream_delete (void *buf)
455 ui_out_stream_delete (buf);
459 make_cleanup_ui_out_stream_delete (struct ui_stream *buf)
461 return make_cleanup (do_stream_delete, buf);
466 ui_out_wrap_hint (struct ui_out *uiout, char *identstring)
468 uo_wrap_hint (uiout, identstring);
472 ui_out_flush (struct ui_out *uiout)
477 /* set the flags specified by the mask given */
479 ui_out_set_flags (struct ui_out *uiout, int mask)
481 int oldflags = uiout->flags;
483 uiout->flags |= mask;
488 /* clear the flags specified by the mask given */
490 ui_out_clear_flags (struct ui_out *uiout, int mask)
492 int oldflags = uiout->flags;
494 uiout->flags &= ~mask;
499 /* test the flags against the mask given */
501 ui_out_test_flags (struct ui_out *uiout, int mask)
503 return (uiout->flags & mask);
506 /* obtain the current verbosity level (as stablished by the
507 'set verbositylevel' command */
510 ui_out_get_verblvl (struct ui_out *uiout)
512 /* FIXME: not implemented yet */
518 ui_out_result_begin (struct ui_out *uiout, char *class)
523 ui_out_result_end (struct ui_out *uiout)
528 ui_out_info_begin (struct ui_out *uiout, char *class)
533 ui_out_info_end (struct ui_out *uiout)
538 ui_out_notify_begin (struct ui_out *uiout, char *class)
543 ui_out_notify_end (struct ui_out *uiout)
548 ui_out_error_begin (struct ui_out *uiout, char *class)
553 ui_out_error_end (struct ui_out *uiout)
560 gdb_error (ui_out * uiout, int severity, char *format,...)
566 gdb_query (struct ui_out *uiout, int qflags, char *qprompt)
571 /* default gdb-out hook functions */
574 default_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid)
579 default_table_body (struct ui_out *uiout)
584 default_table_end (struct ui_out *uiout)
589 default_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
595 default_list_begin (struct ui_out *uiout, int list_flag, char *lstid)
600 default_list_end (struct ui_out *uiout, int list_flag)
605 default_field_int (struct ui_out *uiout, int fldno, int width,
606 enum ui_align align, char *fldname, int value)
611 default_field_skip (struct ui_out *uiout, int fldno, int width,
612 enum ui_align align, char *fldname)
617 default_field_string (struct ui_out *uiout,
627 default_field_fmt (struct ui_out *uiout, int fldno, int width,
628 enum ui_align align, char *fldname, char *format,
634 default_spaces (struct ui_out *uiout, int numspaces)
639 default_text (struct ui_out *uiout, char *string)
644 default_message (struct ui_out *uiout, int verbosity, char *format,
650 default_wrap_hint (struct ui_out *uiout, char *identstring)
655 default_flush (struct ui_out *uiout)
659 /* Interface to the implementation functions */
662 uo_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid)
664 if (!uiout->impl->table_begin)
666 uiout->impl->table_begin (uiout, nbrofcols, tblid);
670 uo_table_body (struct ui_out *uiout)
672 if (!uiout->impl->table_body)
674 uiout->impl->table_body (uiout);
678 uo_table_end (struct ui_out *uiout)
680 if (!uiout->impl->table_end)
682 uiout->impl->table_end (uiout);
686 uo_table_header (struct ui_out *uiout, int width, enum ui_align align, char *colhdr)
688 if (!uiout->impl->table_header)
690 uiout->impl->table_header (uiout, width, align, colhdr);
694 uo_list_begin (struct ui_out *uiout, int list_flag, char *lstid)
696 if (!uiout->impl->list_begin)
698 uiout->impl->list_begin (uiout, list_flag, lstid);
702 uo_list_end (struct ui_out *uiout, int list_flag)
704 if (!uiout->impl->list_end)
706 uiout->impl->list_end (uiout, list_flag);
710 uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname, int value)
712 if (!uiout->impl->field_int)
714 uiout->impl->field_int (uiout, fldno, width, align, fldname, value);
718 uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname)
720 if (!uiout->impl->field_skip)
722 uiout->impl->field_skip (uiout, fldno, width, align, fldname);
726 uo_field_string (struct ui_out *uiout, int fldno, int width,
727 enum ui_align align, char *fldname, const char *string)
729 if (!uiout->impl->field_string)
731 uiout->impl->field_string (uiout, fldno, width, align, fldname, string);
735 uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align, char *fldname, char *format, va_list args)
737 if (!uiout->impl->field_fmt)
739 uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args);
743 uo_spaces (struct ui_out *uiout, int numspaces)
745 if (!uiout->impl->spaces)
747 uiout->impl->spaces (uiout, numspaces);
751 uo_text (struct ui_out *uiout, char *string)
753 if (!uiout->impl->text)
755 uiout->impl->text (uiout, string);
759 uo_message (struct ui_out *uiout, int verbosity, char *format, va_list args)
761 if (!uiout->impl->message)
763 uiout->impl->message (uiout, verbosity, format, args);
767 uo_wrap_hint (struct ui_out *uiout, char *identstring)
769 if (!uiout->impl->wrap_hint)
771 uiout->impl->wrap_hint (uiout, identstring);
775 uo_flush (struct ui_out *uiout)
777 if (!uiout->impl->flush)
779 uiout->impl->flush (uiout);
782 /* local functions */
784 /* list of column headers manipulation routines */
787 clear_header_list (struct ui_out *uiout)
789 while (uiout->headerfirst != NULL)
791 uiout->headercurr = uiout->headerfirst;
792 uiout->headerfirst = uiout->headerfirst->next;
793 if (uiout->headercurr->colhdr != NULL)
794 xfree (uiout->headercurr->colhdr);
795 xfree (uiout->headercurr);
797 uiout->headerlast = NULL;
798 uiout->headercurr = NULL;
802 append_header_to_list (struct ui_out *uiout,
807 struct ui_out_hdr *temphdr;
809 temphdr = XMALLOC (struct ui_out_hdr);
810 temphdr->width = width;
811 temphdr->alignment = alignment;
812 /* we have to copy the column title as the original may be an automatic */
815 temphdr->colhdr = xmalloc (strlen (colhdr) + 1);
816 strcpy (temphdr->colhdr, colhdr);
818 temphdr->next = NULL;
819 if (uiout->headerfirst == NULL)
822 uiout->headerfirst = temphdr;
823 uiout->headerlast = temphdr;
827 temphdr->colno = uiout->headerlast->colno + 1;
828 uiout->headerlast->next = temphdr;
829 uiout->headerlast = temphdr;
831 uiout->headercurr = uiout->headerlast;
834 /* returns 0 if there is no more headers */
837 get_curr_header (struct ui_out *uiout,
843 /* There may be no headers at all or we may have used all columns */
844 if (uiout->headercurr == NULL)
846 *colno = uiout->headercurr->colno;
847 *width = uiout->headercurr->width;
848 *alignment = uiout->headercurr->alignment;
849 *colhdr = uiout->headercurr->colhdr;
850 uiout->headercurr = uiout->headercurr->next;
854 /* makes sure the field_* calls were properly placed */
857 verify_field_proper_position (struct ui_out *uiout)
859 if (uiout->table_flag)
861 if (!uiout->body_flag)
862 internal_error (__FILE__, __LINE__,
863 "table_body missing; table fields must be \
864 specified after table_body and inside a list.");
865 if (!uiout->list_flag)
866 internal_error (__FILE__, __LINE__,
867 "list_begin missing; table fields must be \
868 specified after table_body and inside a list.");
872 /* determines what is the alignment policy */
875 verify_field_alignment (struct ui_out *uiout,
883 if (uiout->table_flag
884 && get_curr_header (uiout, &colno, width, align, &text))
887 internal_error (__FILE__, __LINE__,
888 "ui-out internal error in handling headers.");
897 /* access to ui_out format private members */
900 ui_out_get_field_separator (struct ui_out *uiout)
904 /* Access to ui-out members data */
907 ui_out_data (struct ui_out *uiout)
912 /* initalize private members at startup */
915 ui_out_new (struct ui_out_impl *impl,
916 struct ui_out_data *data,
919 struct ui_out *uiout = XMALLOC (struct ui_out);
922 uiout->flags = flags;
923 uiout->table_flag = 0;
924 uiout->body_flag = 0;
925 uiout->list_flag = 0;
926 uiout->field_count[0] = 0;
927 uiout->headerfirst = NULL;
928 uiout->headerlast = NULL;
929 uiout->headercurr = NULL;
933 /* standard gdb initialization hook */
936 _initialize_ui_out (void)
938 /* nothing needs to be done */