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