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