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