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