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