Rename field_int to field_signed
[external/binutils.git] / gdb / mi / mi-out.c
1 /* MI Command Set - output generating routines.
2
3    Copyright (C) 2000-2019 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 "mi-out.h"
24
25 #include <vector>
26
27 #include "interps.h"
28 #include "ui-out.h"
29 #include "utils.h"
30
31 /* Mark beginning of a table.  */
32
33 void
34 mi_ui_out::do_table_begin (int nr_cols, int nr_rows,
35                            const char *tblid)
36 {
37   open (tblid, ui_out_type_tuple);
38   do_field_signed (-1, -1, ui_left, "nr_rows", nr_rows);
39   do_field_signed (-1, -1, ui_left, "nr_cols", nr_cols);
40   open ("hdr", ui_out_type_list);
41 }
42
43 /* Mark beginning of a table body.  */
44
45 void
46 mi_ui_out::do_table_body ()
47 {
48   /* close the table header line if there were any headers */
49   close (ui_out_type_list);
50   open ("body", ui_out_type_list);
51 }
52
53 /* Mark end of a table.  */
54
55 void
56 mi_ui_out::do_table_end ()
57 {
58   close (ui_out_type_list); /* body */
59   close (ui_out_type_tuple);
60 }
61
62 /* Specify table header.  */
63
64 void
65 mi_ui_out::do_table_header (int width, ui_align alignment,
66                             const std::string &col_name,
67                             const std::string &col_hdr)
68 {
69   open (NULL, ui_out_type_tuple);
70   do_field_signed (0, 0, ui_center, "width", width);
71   do_field_signed (0, 0, ui_center, "alignment", alignment);
72   do_field_string (0, 0, ui_center, "col_name", col_name.c_str (),
73                    ui_out_style_kind::DEFAULT);
74   do_field_string (0, width, alignment, "colhdr", col_hdr.c_str (),
75                    ui_out_style_kind::DEFAULT);
76   close (ui_out_type_tuple);
77 }
78
79 /* Mark beginning of a list.  */
80
81 void
82 mi_ui_out::do_begin (ui_out_type type, const char *id)
83 {
84   open (id, type);
85 }
86
87 /* Mark end of a list.  */
88
89 void
90 mi_ui_out::do_end (ui_out_type type)
91 {
92   close (type);
93 }
94
95 /* Output an int field.  */
96
97 void
98 mi_ui_out::do_field_signed (int fldno, int width, ui_align alignment,
99                             const char *fldname, LONGEST value)
100 {
101   do_field_string (fldno, width, alignment, fldname, plongest (value),
102                    ui_out_style_kind::DEFAULT);
103 }
104
105 /* Output an unsigned field.  */
106
107 void
108 mi_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
109                               const char *fldname, ULONGEST value)
110 {
111   do_field_string (fldno, width, alignment, fldname, pulongest (value),
112                    ui_out_style_kind::DEFAULT);
113 }
114
115 /* Used to omit a field.  */
116
117 void
118 mi_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
119                           const char *fldname)
120 {
121 }
122
123 /* Other specific mi_field_* end up here so alignment and field
124    separators are both handled by mi_field_string. */
125
126 void
127 mi_ui_out::do_field_string (int fldno, int width, ui_align align,
128                             const char *fldname, const char *string,
129                             ui_out_style_kind style)
130 {
131   ui_file *stream = m_streams.back ();
132   field_separator ();
133
134   if (fldname)
135     fprintf_unfiltered (stream, "%s=", fldname);
136   fprintf_unfiltered (stream, "\"");
137   if (string)
138     fputstr_unfiltered (string, '"', stream);
139   fprintf_unfiltered (stream, "\"");
140 }
141
142 void
143 mi_ui_out::do_field_fmt (int fldno, int width, ui_align align,
144                          const char *fldname, const char *format,
145                          va_list args)
146 {
147   ui_file *stream = m_streams.back ();
148   field_separator ();
149
150   if (fldname)
151     fprintf_unfiltered (stream, "%s=\"", fldname);
152   else
153     fputs_unfiltered ("\"", stream);
154   vfprintf_unfiltered (stream, format, args);
155   fputs_unfiltered ("\"", stream);
156 }
157
158 void
159 mi_ui_out::do_spaces (int numspaces)
160 {
161 }
162
163 void
164 mi_ui_out::do_text (const char *string)
165 {
166 }
167
168 void
169 mi_ui_out::do_message (const char *format, va_list args)
170 {
171 }
172
173 void
174 mi_ui_out::do_wrap_hint (const char *identstring)
175 {
176   wrap_here (identstring);
177 }
178
179 void
180 mi_ui_out::do_flush ()
181 {
182
183   gdb_flush (m_streams.back ());
184 }
185
186 void
187 mi_ui_out::do_redirect (ui_file *outstream)
188 {
189   if (outstream != NULL)
190     m_streams.push_back (outstream);
191   else
192     m_streams.pop_back ();
193 }
194
195 void
196 mi_ui_out::field_separator ()
197 {
198   if (m_suppress_field_separator)
199     m_suppress_field_separator = false;
200   else
201     fputc_unfiltered (',', m_streams.back ());
202 }
203
204 void
205 mi_ui_out::open (const char *name, ui_out_type type)
206 {
207   ui_file *stream = m_streams.back ();
208
209   field_separator ();
210   m_suppress_field_separator = true;
211
212   if (name)
213     fprintf_unfiltered (stream, "%s=", name);
214
215   switch (type)
216     {
217     case ui_out_type_tuple:
218       fputc_unfiltered ('{', stream);
219       break;
220
221     case ui_out_type_list:
222       fputc_unfiltered ('[', stream);
223       break;
224
225     default:
226       internal_error (__FILE__, __LINE__, _("bad switch"));
227     }
228 }
229
230 void
231 mi_ui_out::close (ui_out_type type)
232 {
233   ui_file *stream = m_streams.back ();
234
235   switch (type)
236     {
237     case ui_out_type_tuple:
238       fputc_unfiltered ('}', stream);
239       break;
240
241     case ui_out_type_list:
242       fputc_unfiltered (']', stream);
243       break;
244
245     default:
246       internal_error (__FILE__, __LINE__, _("bad switch"));
247     }
248
249   m_suppress_field_separator = false;
250 }
251
252 string_file *
253 mi_ui_out::main_stream ()
254 {
255   gdb_assert (m_streams.size () == 1);
256
257   return (string_file *) m_streams.back ();
258 }
259
260 /* Clear the buffer.  */
261
262 void
263 mi_ui_out::rewind ()
264 {
265   main_stream ()->clear ();
266 }
267
268 /* Dump the buffer onto the specified stream.  */
269
270 void
271 mi_ui_out::put (ui_file *where)
272 {
273   string_file *mi_stream = main_stream ();
274
275   where->write (mi_stream->data (), mi_stream->size ());
276   mi_stream->clear ();
277 }
278
279 /* Return the current MI version.  */
280
281 int
282 mi_ui_out::version ()
283 {
284   return m_mi_version;
285 }
286
287 /* Constructor for an `mi_out_data' object.  */
288
289 mi_ui_out::mi_ui_out (int mi_version)
290 : ui_out (mi_version >= 3
291           ? fix_multi_location_breakpoint_output : (ui_out_flag) 0),
292   m_suppress_field_separator (false),
293   m_suppress_output (false),
294   m_mi_version (mi_version)
295 {
296   string_file *stream = new string_file ();
297   m_streams.push_back (stream);
298 }
299
300 mi_ui_out::~mi_ui_out ()
301 {
302 }
303
304 /* See mi/mi-out.h.  */
305
306 mi_ui_out *
307 mi_out_new (const char *mi_version)
308 {
309   if (streq (mi_version, INTERP_MI3) ||  streq (mi_version, INTERP_MI))
310     return new mi_ui_out (3);
311
312   if (streq (mi_version, INTERP_MI2))
313     return new mi_ui_out (2);
314
315   if (streq (mi_version, INTERP_MI1))
316     return new mi_ui_out (1);
317
318   return nullptr;
319 }
320
321 /* Helper function to return the given UIOUT as an mi_ui_out.  It is an error
322    to call this function with an ui_out that is not an MI.  */
323
324 static mi_ui_out *
325 as_mi_ui_out (ui_out *uiout)
326 {
327   mi_ui_out *mi_uiout = dynamic_cast<mi_ui_out *> (uiout);
328
329   gdb_assert (mi_uiout != NULL);
330
331   return mi_uiout;
332 }
333
334 int
335 mi_version (ui_out *uiout)
336 {
337   return as_mi_ui_out (uiout)->version ();
338 }
339
340 void
341 mi_out_put (ui_out *uiout, struct ui_file *stream)
342 {
343   return as_mi_ui_out (uiout)->put (stream);
344 }
345
346 void
347 mi_out_rewind (ui_out *uiout)
348 {
349   return as_mi_ui_out (uiout)->rewind ();
350 }