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