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
3    Copyright 1999, 2000, 2002 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 "ui-out.h"
27 #include "cli-out.h"
28 #include "gdb_string.h"
29 #include "gdb_assert.h"
30
31 struct ui_out_data
32   {
33     struct ui_file *stream;
34     int suppress_output;
35   };
36
37 /* These are the CLI output functions */
38
39 static void cli_table_begin (struct ui_out *uiout, int nbrofcols,
40                              int nr_rows, const 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, const char *col_name,
45                               const char *colhdr);
46 static void cli_begin (struct ui_out *uiout, enum ui_out_type type,
47                        int level, const char *lstid);
48 static void cli_end (struct ui_out *uiout, enum ui_out_type type, int level);
49 static void cli_field_int (struct ui_out *uiout, int fldno, int width,
50                            enum ui_align alig, const char *fldname, int value);
51 static void cli_field_skip (struct ui_out *uiout, int fldno, int width,
52                             enum ui_align alig, const char *fldname);
53 static void cli_field_string (struct ui_out *uiout, int fldno, int width,
54                               enum ui_align alig, const char *fldname,
55                               const char *string);
56 static void cli_field_fmt (struct ui_out *uiout, int fldno,
57                            int width, enum ui_align align,
58                            const char *fldname, const char *format,
59                            va_list args);
60 static void cli_spaces (struct ui_out *uiout, int numspaces);
61 static void cli_text (struct ui_out *uiout, const char *string);
62 static void cli_message (struct ui_out *uiout, int verbosity,
63                          const char *format, va_list args);
64 static void cli_wrap_hint (struct ui_out *uiout, char *identstring);
65 static void cli_flush (struct ui_out *uiout);
66
67 /* This is the CLI ui-out implementation functions vector */
68
69 /* FIXME: This can be initialized dynamically after default is set to
70    handle initial output in main.c */
71
72 static struct ui_out_impl cli_ui_out_impl =
73 {
74   cli_table_begin,
75   cli_table_body,
76   cli_table_end,
77   cli_table_header,
78   cli_begin,
79   cli_end,
80   cli_field_int,
81   cli_field_skip,
82   cli_field_string,
83   cli_field_fmt,
84   cli_spaces,
85   cli_text,
86   cli_message,
87   cli_wrap_hint,
88   cli_flush,
89   0, /* Does not need MI hacks (i.e. needs CLI hacks).  */
90 };
91
92 /* Prototypes for local functions */
93
94 extern void _initialize_cli_out (void);
95
96 static void field_separator (void);
97
98 static void out_field_fmt (struct ui_out *uiout, int fldno,
99                            const char *fldname,
100                            const char *format,...);
101
102 /* local variables */
103
104 /* (none yet) */
105
106 /* Mark beginning of a table */
107
108 void
109 cli_table_begin (struct ui_out *uiout, int nbrofcols,
110                  int nr_rows,
111                  const char *tblid)
112 {
113   struct ui_out_data *data = ui_out_data (uiout);
114   if (nr_rows == 0)
115     data->suppress_output = 1;
116   else
117     /* Only the table suppresses the output and, fortunatly, a table
118        is not a recursive data structure. */
119     gdb_assert (data->suppress_output == 0);
120 }
121
122 /* Mark beginning of a table body */
123
124 void
125 cli_table_body (struct ui_out *uiout)
126 {
127   struct ui_out_data *data = ui_out_data (uiout);
128   if (data->suppress_output)
129     return;
130   /* first, close the table header line */
131   cli_text (uiout, "\n");
132 }
133
134 /* Mark end of a table */
135
136 void
137 cli_table_end (struct ui_out *uiout)
138 {
139   struct ui_out_data *data = ui_out_data (uiout);
140   data->suppress_output = 0;
141 }
142
143 /* Specify table header */
144
145 void
146 cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
147                   const char *col_name,
148                   const char *colhdr)
149 {
150   struct ui_out_data *data = ui_out_data (uiout);
151   if (data->suppress_output)
152     return;
153   cli_field_string (uiout, 0, width, alignment, 0, colhdr);
154 }
155
156 /* Mark beginning of a list */
157
158 void
159 cli_begin (struct ui_out *uiout,
160            enum ui_out_type type,
161            int level,
162            const char *id)
163 {
164   struct ui_out_data *data = ui_out_data (uiout);
165   if (data->suppress_output)
166     return;
167 }
168
169 /* Mark end of a list */
170
171 void
172 cli_end (struct ui_out *uiout,
173          enum ui_out_type type,
174          int level)
175 {
176   struct ui_out_data *data = ui_out_data (uiout);
177   if (data->suppress_output)
178     return;
179 }
180
181 /* output an int field */
182
183 void
184 cli_field_int (struct ui_out *uiout, int fldno, int width,
185                enum ui_align alignment,
186                const char *fldname, int value)
187 {
188   char buffer[20];              /* FIXME: how many chars long a %d can become? */
189
190   struct ui_out_data *data = ui_out_data (uiout);
191   if (data->suppress_output)
192     return;
193   sprintf (buffer, "%d", value);
194   cli_field_string (uiout, fldno, width, alignment, fldname, buffer);
195 }
196
197 /* used to ommit a field */
198
199 void
200 cli_field_skip (struct ui_out *uiout, int fldno, int width,
201                 enum ui_align alignment,
202                 const char *fldname)
203 {
204   struct ui_out_data *data = ui_out_data (uiout);
205   if (data->suppress_output)
206     return;
207   cli_field_string (uiout, fldno, width, alignment, fldname, "");
208 }
209
210 /* other specific cli_field_* end up here so alignment and field
211    separators are both handled by cli_field_string */
212
213 void
214 cli_field_string (struct ui_out *uiout,
215                   int fldno,
216                   int width,
217                   enum ui_align align,
218                   const char *fldname,
219                   const char *string)
220 {
221   int before = 0;
222   int after = 0;
223
224   struct ui_out_data *data = ui_out_data (uiout);
225   if (data->suppress_output)
226     return;
227
228   if ((align != ui_noalign) && string)
229     {
230       before = width - strlen (string);
231       if (before <= 0)
232         before = 0;
233       else
234         {
235           if (align == ui_right)
236             after = 0;
237           else if (align == ui_left)
238             {
239               after = before;
240               before = 0;
241             }
242           else
243             /* ui_center */
244             {
245               after = before / 2;
246               before -= after;
247             }
248         }
249     }
250
251   if (before)
252     ui_out_spaces (uiout, before);
253   if (string)
254     out_field_fmt (uiout, fldno, fldname, "%s", string);
255   if (after)
256     ui_out_spaces (uiout, after);
257
258   if (align != ui_noalign)
259     field_separator ();
260 }
261
262 /* This is the only field function that does not align */
263
264 void
265 cli_field_fmt (struct ui_out *uiout, int fldno,
266                int width, enum ui_align align,
267                const char *fldname,
268                const char *format,
269                va_list args)
270 {
271   struct ui_out_data *data = ui_out_data (uiout);
272   if (data->suppress_output)
273     return;
274
275   vfprintf_filtered (data->stream, format, args);
276
277   if (align != ui_noalign)
278     field_separator ();
279 }
280
281 void
282 cli_spaces (struct ui_out *uiout, int numspaces)
283 {
284   struct ui_out_data *data = ui_out_data (uiout);
285   if (data->suppress_output)
286     return;
287   print_spaces_filtered (numspaces, data->stream);
288 }
289
290 void
291 cli_text (struct ui_out *uiout, const char *string)
292 {
293   struct ui_out_data *data = ui_out_data (uiout);
294   if (data->suppress_output)
295     return;
296   fputs_filtered (string, data->stream);
297 }
298
299 void
300 cli_message (struct ui_out *uiout, int verbosity,
301              const char *format, va_list args)
302 {
303   struct ui_out_data *data = ui_out_data (uiout);
304   if (data->suppress_output)
305     return;
306   if (ui_out_get_verblvl (uiout) >= verbosity)
307     vfprintf_unfiltered (data->stream, format, args);
308 }
309
310 void
311 cli_wrap_hint (struct ui_out *uiout, char *identstring)
312 {
313   struct ui_out_data *data = ui_out_data (uiout);
314   if (data->suppress_output)
315     return;
316   wrap_here (identstring);
317 }
318
319 void
320 cli_flush (struct ui_out *uiout)
321 {
322   struct ui_out_data *data = ui_out_data (uiout);
323   gdb_flush (data->stream);
324 }
325
326 /* local functions */
327
328 /* Like cli_field_fmt, but takes a variable number of args
329    and makes a va_list and does not insert a separator */
330
331 /* VARARGS */
332 static void
333 out_field_fmt (struct ui_out *uiout, int fldno,
334                const char *fldname,
335                const char *format,...)
336 {
337   struct ui_out_data *data = ui_out_data (uiout);
338   va_list args;
339
340   va_start (args, format);
341   vfprintf_filtered (data->stream, format, args);
342
343   va_end (args);
344 }
345
346 /* access to ui_out format private members */
347
348 static void
349 field_separator (void)
350 {
351   struct ui_out_data *data = ui_out_data (uiout);
352   fputc_filtered (' ', data->stream);
353 }
354
355 /* initalize private members at startup */
356
357 struct ui_out *
358 cli_out_new (struct ui_file *stream)
359 {
360   int flags = ui_source_list;
361
362   struct ui_out_data *data = XMALLOC (struct ui_out_data);
363   data->stream = stream;
364   data->suppress_output = 0;
365   return ui_out_new (&cli_ui_out_impl, data, flags);
366 }
367
368 /* standard gdb initialization hook */
369 void
370 _initialize_cli_out (void)
371 {
372   /* nothing needs to be done */
373 }