This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / cli-out.c
1 /* Output generating routines for GDB CLI.
2    Copyright 1999-2000 Free Software Foundation, Inc.
3    Contributed by Cygnus Solutions.
4    Written by Fernando Nasser for Cygnus.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "cli-out.h"
26
27 /* Convenience macro for allocting typesafe memory. */
28
29 #ifndef XMALLOC
30 #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
31 #endif
32
33 struct ui_out_data
34   {
35     struct ui_file *stream;
36   };
37
38 /* These are the CLI output functions */
39
40 static void cli_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid);
41 static void cli_table_body (struct ui_out *uiout);
42 static void cli_table_end (struct ui_out *uiout);
43 static void cli_table_header (struct ui_out *uiout, int width,
44                               enum ui_align alig, char *colhdr);
45 static void cli_list_begin (struct ui_out *uiout, int list_flag, char *lstid);
46 static void cli_list_end (struct ui_out *uiout, int list_flag);
47 static void cli_field_int (struct ui_out *uiout, int fldno, int width,
48                            enum ui_align alig, char *fldname, int value);
49 static void cli_field_skip (struct ui_out *uiout, int fldno, int width,
50                             enum ui_align alig, char *fldname);
51 static void cli_field_string (struct ui_out *uiout, int fldno, int width,
52                            enum ui_align alig, char *fldname,
53                               const char *string);
54 static void cli_field_fmt (struct ui_out *uiout, int fldno,
55                            int width, enum ui_align align,
56                            char *fldname, char *format, va_list args);
57 static void cli_spaces (struct ui_out *uiout, int numspaces);
58 static void cli_text (struct ui_out *uiout, char *string);
59 static void cli_message (struct ui_out *uiout, int verbosity, char *format,
60                          va_list args);
61 static void cli_wrap_hint (struct ui_out *uiout, char *identstring);
62 static void cli_flush (struct ui_out *uiout);
63
64 /* This is the CLI ui-out implementation functions vector */
65
66 /* FIXME: This can be initialized dynamically after default is set to
67    handle initial output in main.c */
68
69 static struct ui_out_impl cli_ui_out_impl =
70 {
71   cli_table_begin,
72   cli_table_body,
73   cli_table_end,
74   cli_table_header,
75   cli_list_begin,
76   cli_list_end,
77   cli_field_int,
78   cli_field_skip,
79   cli_field_string,
80   cli_field_fmt,
81   cli_spaces,
82   cli_text,
83   cli_message,
84   cli_wrap_hint,
85   cli_flush
86 };
87
88 /* Prototypes for local functions */
89
90 extern void _initialize_cli_out PARAMS ((void));
91
92 static void field_separator (void);
93
94 static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
95                            char *format,...);
96
97 /* local variables */
98
99 /* (none yet) */
100
101 /* Mark beginning of a table */
102
103 void
104 cli_table_begin (uiout, nbrofcols, tblid)
105      struct ui_out *uiout;
106      int nbrofcols;
107      char *tblid;
108 {
109 }
110
111 /* Mark beginning of a table body */
112
113 void
114 cli_table_body (uiout)
115      struct ui_out *uiout;
116 {
117   /* first, close the table header line */
118   cli_text (uiout, "\n");
119 }
120
121 /* Mark end of a table */
122
123 void
124 cli_table_end (uiout)
125      struct ui_out *uiout;
126 {
127 }
128
129 /* Specify table header */
130
131 void
132 cli_table_header (uiout, width, alignment, colhdr)
133      struct ui_out *uiout;
134      int width;
135      enum ui_align alignment;
136      char *colhdr;
137 {
138   cli_field_string (uiout, 0, width, alignment, 0, colhdr);
139 }
140
141 /* Mark beginning of a list */
142
143 void
144 cli_list_begin (uiout, list_flag, lstid)
145      struct ui_out *uiout;
146      int list_flag;
147      char *lstid;
148 {
149 }
150
151 /* Mark end of a list */
152
153 void
154 cli_list_end (uiout, list_flag)
155      struct ui_out *uiout;
156      int list_flag;
157 {
158 }
159
160 /* output an int field */
161
162 void
163 cli_field_int (uiout, fldno, width, alignment, fldname, value)
164      struct ui_out *uiout;
165      int fldno;
166      int width;
167      enum ui_align alignment;
168      char *fldname;
169      int value;
170 {
171   char buffer[20];              /* FIXME: how many chars long a %d can become? */
172
173   sprintf (buffer, "%d", value);
174   cli_field_string (uiout, fldno, width, alignment, fldname, buffer);
175 }
176
177 /* used to ommit a field */
178
179 void
180 cli_field_skip (uiout, fldno, width, alignment, fldname)
181      struct ui_out *uiout;
182      int fldno;
183      int width;
184      enum ui_align alignment;
185      char *fldname;
186 {
187   cli_field_string (uiout, fldno, width, alignment, fldname, "");
188 }
189
190 /* other specific cli_field_* end up here so alignment and field
191    separators are both handled by cli_field_string */
192
193 void
194 cli_field_string (struct ui_out *uiout,
195                   int fldno,
196                   int width,
197                   enum ui_align align,
198                   char *fldname,
199                   const char *string)
200 {
201   int before = 0;
202   int after = 0;
203
204   if ((align != ui_noalign) && string)
205     {
206       before = width - strlen (string);
207       if (before <= 0)
208         before = 0;
209       else
210         {
211           if (align == ui_right)
212             after = 0;
213           else if (align == ui_left)
214             {
215               after = before;
216               before = 0;
217             }
218           else
219             /* ui_center */
220             {
221               after = before / 2;
222               before -= after;
223             }
224         }
225     }
226
227   if (before)
228     ui_out_spaces (uiout, before);
229   if (string)
230     out_field_fmt (uiout, fldno, fldname, "%s", string);
231   if (after)
232     ui_out_spaces (uiout, after);
233
234   if (align != ui_noalign)
235     field_separator ();
236 }
237
238 /* This is the only field function that does not align */
239
240 void
241 cli_field_fmt (struct ui_out *uiout, int fldno,
242                int width, enum ui_align align,
243                char *fldname, char *format, va_list args)
244 {
245   struct ui_out_data *data = ui_out_data (uiout);
246   vfprintf_filtered (data->stream, format, args);
247
248   if (align != ui_noalign)
249     field_separator ();
250 }
251
252 void
253 cli_spaces (uiout, numspaces)
254      struct ui_out *uiout;
255      int numspaces;
256 {
257   struct ui_out_data *data = ui_out_data (uiout);
258   print_spaces_filtered (numspaces, data->stream);
259 }
260
261 void
262 cli_text (uiout, string)
263      struct ui_out *uiout;
264      char *string;
265 {
266   struct ui_out_data *data = ui_out_data (uiout);
267   fputs_filtered (string, data->stream);
268 }
269
270 void
271 cli_message (struct ui_out *uiout, int verbosity, char *format, va_list args)
272 {
273   struct ui_out_data *data = ui_out_data (uiout);
274   if (ui_out_get_verblvl (uiout) >= verbosity)
275     vfprintf_unfiltered (data->stream, format, args);
276 }
277
278 void
279 cli_wrap_hint (uiout, identstring)
280      struct ui_out *uiout;
281      char *identstring;
282 {
283   wrap_here (identstring);
284 }
285
286 void
287 cli_flush (uiout)
288      struct ui_out *uiout;
289 {
290   struct ui_out_data *data = ui_out_data (uiout);
291   gdb_flush (data->stream);
292 }
293
294 /* local functions */
295
296 /* Like cli_field_fmt, but takes a variable number of args
297    and makes a va_list and does not insert a separator */
298
299 /* VARARGS */
300 static void
301 out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
302                char *format,...)
303 {
304   struct ui_out_data *data = ui_out_data (uiout);
305   va_list args;
306
307   va_start (args, format);
308   vfprintf_filtered (data->stream, format, args);
309
310   va_end (args);
311 }
312
313 /* access to ui_out format private members */
314
315 static void
316 field_separator ()
317 {
318   struct ui_out_data *data = ui_out_data (uiout);
319   fputc_filtered (' ', data->stream);
320 }
321
322 /* initalize private members at startup */
323
324 struct ui_out *
325 cli_out_new (struct ui_file *stream)
326 {
327   int flags = ui_source_list;
328
329   struct ui_out_data *data = XMALLOC (struct ui_out_data);
330   data->stream = stream;
331   return ui_out_new (&cli_ui_out_impl, data, flags);
332 }
333
334 /* standard gdb initialization hook */
335 void
336 _initialize_cli_out ()
337 {
338   /* nothing needs to be done */
339 }