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