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