Add output styles to gdb
[external/binutils.git] / gdb / mi / mi-out.c
1 /* MI Command Set - output generating routines.
2
3    Copyright (C) 2000-2018 Free Software Foundation, Inc.
4
5    Contributed by Cygnus Solutions (a Red Hat company).
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "mi-out.h"
25 #include <vector>
26
27 /* Mark beginning of a table.  */
28
29 void
30 mi_ui_out::do_table_begin (int nr_cols, int nr_rows,
31                            const char *tblid)
32 {
33   open (tblid, ui_out_type_tuple);
34   do_field_int (-1, -1, ui_left, "nr_rows", nr_rows);
35   do_field_int (-1, -1, ui_left, "nr_cols", nr_cols);
36   open ("hdr", ui_out_type_list);
37 }
38
39 /* Mark beginning of a table body.  */
40
41 void
42 mi_ui_out::do_table_body ()
43 {
44   /* close the table header line if there were any headers */
45   close (ui_out_type_list);
46   open ("body", ui_out_type_list);
47 }
48
49 /* Mark end of a table.  */
50
51 void
52 mi_ui_out::do_table_end ()
53 {
54   close (ui_out_type_list); /* body */
55   close (ui_out_type_tuple);
56 }
57
58 /* Specify table header.  */
59
60 void
61 mi_ui_out::do_table_header (int width, ui_align alignment,
62                             const std::string &col_name,
63                             const std::string &col_hdr)
64 {
65   open (NULL, ui_out_type_tuple);
66   do_field_int (0, 0, ui_center, "width", width);
67   do_field_int (0, 0, ui_center, "alignment", alignment);
68   do_field_string (0, 0, ui_center, "col_name", col_name.c_str (),
69                    ui_out_style_kind::DEFAULT);
70   do_field_string (0, width, alignment, "colhdr", col_hdr.c_str (),
71                    ui_out_style_kind::DEFAULT);
72   close (ui_out_type_tuple);
73 }
74
75 /* Mark beginning of a list.  */
76
77 void
78 mi_ui_out::do_begin (ui_out_type type, const char *id)
79 {
80   open (id, type);
81 }
82
83 /* Mark end of a list.  */
84
85 void
86 mi_ui_out::do_end (ui_out_type type)
87 {
88   close (type);
89 }
90
91 /* Output an int field.  */
92
93 void
94 mi_ui_out::do_field_int (int fldno, int width, ui_align alignment,
95                          const char *fldname, int value)
96 {
97   char buffer[20];      /* FIXME: how many chars long a %d can become? */
98
99   xsnprintf (buffer, sizeof (buffer), "%d", value);
100   do_field_string (fldno, width, alignment, fldname, buffer,
101                    ui_out_style_kind::DEFAULT);
102 }
103
104 /* Used to omit a field.  */
105
106 void
107 mi_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
108                           const char *fldname)
109 {
110 }
111
112 /* Other specific mi_field_* end up here so alignment and field
113    separators are both handled by mi_field_string. */
114
115 void
116 mi_ui_out::do_field_string (int fldno, int width, ui_align align,
117                             const char *fldname, const char *string,
118                             ui_out_style_kind style)
119 {
120   ui_file *stream = m_streams.back ();
121   field_separator ();
122
123   if (fldname)
124     fprintf_unfiltered (stream, "%s=", fldname);
125   fprintf_unfiltered (stream, "\"");
126   if (string)
127     fputstr_unfiltered (string, '"', stream);
128   fprintf_unfiltered (stream, "\"");
129 }
130
131 void
132 mi_ui_out::do_field_fmt (int fldno, int width, ui_align align,
133                          const char *fldname, const char *format,
134                          va_list args)
135 {
136   ui_file *stream = m_streams.back ();
137   field_separator ();
138
139   if (fldname)
140     fprintf_unfiltered (stream, "%s=\"", fldname);
141   else
142     fputs_unfiltered ("\"", stream);
143   vfprintf_unfiltered (stream, format, args);
144   fputs_unfiltered ("\"", stream);
145 }
146
147 void
148 mi_ui_out::do_spaces (int numspaces)
149 {
150 }
151
152 void
153 mi_ui_out::do_text (const char *string)
154 {
155 }
156
157 void
158 mi_ui_out::do_message (const char *format, va_list args)
159 {
160 }
161
162 void
163 mi_ui_out::do_wrap_hint (const char *identstring)
164 {
165   wrap_here (identstring);
166 }
167
168 void
169 mi_ui_out::do_flush ()
170 {
171
172   gdb_flush (m_streams.back ());
173 }
174
175 void
176 mi_ui_out::do_redirect (ui_file *outstream)
177 {
178   if (outstream != NULL)
179     m_streams.push_back (outstream);
180   else
181     m_streams.pop_back ();
182 }
183
184 void
185 mi_ui_out::field_separator ()
186 {
187   if (m_suppress_field_separator)
188     m_suppress_field_separator = false;
189   else
190     fputc_unfiltered (',', m_streams.back ());
191 }
192
193 void
194 mi_ui_out::open (const char *name, ui_out_type type)
195 {
196   ui_file *stream = m_streams.back ();
197
198   field_separator ();
199   m_suppress_field_separator = true;
200
201   if (name)
202     fprintf_unfiltered (stream, "%s=", name);
203
204   switch (type)
205     {
206     case ui_out_type_tuple:
207       fputc_unfiltered ('{', stream);
208       break;
209
210     case ui_out_type_list:
211       fputc_unfiltered ('[', stream);
212       break;
213
214     default:
215       internal_error (__FILE__, __LINE__, _("bad switch"));
216     }
217 }
218
219 void
220 mi_ui_out::close (ui_out_type type)
221 {
222   ui_file *stream = m_streams.back ();
223
224   switch (type)
225     {
226     case ui_out_type_tuple:
227       fputc_unfiltered ('}', stream);
228       break;
229
230     case ui_out_type_list:
231       fputc_unfiltered (']', stream);
232       break;
233
234     default:
235       internal_error (__FILE__, __LINE__, _("bad switch"));
236     }
237
238   m_suppress_field_separator = false;
239 }
240
241 string_file *
242 mi_ui_out::main_stream ()
243 {
244   gdb_assert (m_streams.size () == 1);
245
246   return (string_file *) m_streams.back ();
247 }
248
249 /* Clear the buffer.  */
250
251 void
252 mi_ui_out::rewind ()
253 {
254   main_stream ()->clear ();
255 }
256
257 /* Dump the buffer onto the specified stream.  */
258
259 void
260 mi_ui_out::put (ui_file *where)
261 {
262   string_file *mi_stream = main_stream ();
263
264   where->write (mi_stream->data (), mi_stream->size ());
265   mi_stream->clear ();
266 }
267
268 /* Return the current MI version.  */
269
270 int
271 mi_ui_out::version ()
272 {
273   return m_mi_version;
274 }
275
276 /* Constructor for an `mi_out_data' object.  */
277
278 mi_ui_out::mi_ui_out (int mi_version)
279 : m_suppress_field_separator (false),
280   m_suppress_output (false),
281   m_mi_version (mi_version)
282 {
283   string_file *stream = new string_file ();
284   m_streams.push_back (stream);
285 }
286
287 mi_ui_out::~mi_ui_out ()
288 {
289 }
290
291 /* Initialize private members at startup.  */
292
293 mi_ui_out *
294 mi_out_new (int mi_version)
295 {
296   return new mi_ui_out (mi_version);
297 }
298
299 /* Helper function to return the given UIOUT as an mi_ui_out.  It is an error
300    to call this function with an ui_out that is not an MI.  */
301
302 static mi_ui_out *
303 as_mi_ui_out (ui_out *uiout)
304 {
305   mi_ui_out *mi_uiout = dynamic_cast<mi_ui_out *> (uiout);
306
307   gdb_assert (mi_uiout != NULL);
308
309   return mi_uiout;
310 }
311
312 int
313 mi_version (ui_out *uiout)
314 {
315   return as_mi_ui_out (uiout)->version ();
316 }
317
318 void
319 mi_out_put (ui_out *uiout, struct ui_file *stream)
320 {
321   return as_mi_ui_out (uiout)->put (stream);
322 }
323
324 void
325 mi_out_rewind (ui_out *uiout)
326 {
327   return as_mi_ui_out (uiout)->rewind ();
328 }