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