Introduce field_unsigned
[deliverable/binutils-gdb.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_int (-1, -1, ui_left, "nr_rows", nr_rows);
39 do_field_int (-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_int (0, 0, ui_center, "width", width);
71 do_field_int (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_int (int fldno, int width, ui_align alignment,
99 const char *fldname, int value)
100 {
101 char buffer[20]; /* FIXME: how many chars long a %d can become? */
102
103 xsnprintf (buffer, sizeof (buffer), "%d", value);
104 do_field_string (fldno, width, alignment, fldname, buffer,
105 ui_out_style_kind::DEFAULT);
106 }
107
108 /* Output an unsigned field. */
109
110 void
111 mi_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
112 const char *fldname, ULONGEST value)
113 {
114 do_field_string (fldno, width, alignment, fldname, pulongest (value),
115 ui_out_style_kind::DEFAULT);
116 }
117
118 /* Used to omit a field. */
119
120 void
121 mi_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
122 const char *fldname)
123 {
124 }
125
126 /* Other specific mi_field_* end up here so alignment and field
127 separators are both handled by mi_field_string. */
128
129 void
130 mi_ui_out::do_field_string (int fldno, int width, ui_align align,
131 const char *fldname, const char *string,
132 ui_out_style_kind style)
133 {
134 ui_file *stream = m_streams.back ();
135 field_separator ();
136
137 if (fldname)
138 fprintf_unfiltered (stream, "%s=", fldname);
139 fprintf_unfiltered (stream, "\"");
140 if (string)
141 fputstr_unfiltered (string, '"', stream);
142 fprintf_unfiltered (stream, "\"");
143 }
144
145 void
146 mi_ui_out::do_field_fmt (int fldno, int width, ui_align align,
147 const char *fldname, const char *format,
148 va_list args)
149 {
150 ui_file *stream = m_streams.back ();
151 field_separator ();
152
153 if (fldname)
154 fprintf_unfiltered (stream, "%s=\"", fldname);
155 else
156 fputs_unfiltered ("\"", stream);
157 vfprintf_unfiltered (stream, format, args);
158 fputs_unfiltered ("\"", stream);
159 }
160
161 void
162 mi_ui_out::do_spaces (int numspaces)
163 {
164 }
165
166 void
167 mi_ui_out::do_text (const char *string)
168 {
169 }
170
171 void
172 mi_ui_out::do_message (const char *format, va_list args)
173 {
174 }
175
176 void
177 mi_ui_out::do_wrap_hint (const char *identstring)
178 {
179 wrap_here (identstring);
180 }
181
182 void
183 mi_ui_out::do_flush ()
184 {
185
186 gdb_flush (m_streams.back ());
187 }
188
189 void
190 mi_ui_out::do_redirect (ui_file *outstream)
191 {
192 if (outstream != NULL)
193 m_streams.push_back (outstream);
194 else
195 m_streams.pop_back ();
196 }
197
198 void
199 mi_ui_out::field_separator ()
200 {
201 if (m_suppress_field_separator)
202 m_suppress_field_separator = false;
203 else
204 fputc_unfiltered (',', m_streams.back ());
205 }
206
207 void
208 mi_ui_out::open (const char *name, ui_out_type type)
209 {
210 ui_file *stream = m_streams.back ();
211
212 field_separator ();
213 m_suppress_field_separator = true;
214
215 if (name)
216 fprintf_unfiltered (stream, "%s=", name);
217
218 switch (type)
219 {
220 case ui_out_type_tuple:
221 fputc_unfiltered ('{', stream);
222 break;
223
224 case ui_out_type_list:
225 fputc_unfiltered ('[', stream);
226 break;
227
228 default:
229 internal_error (__FILE__, __LINE__, _("bad switch"));
230 }
231 }
232
233 void
234 mi_ui_out::close (ui_out_type type)
235 {
236 ui_file *stream = m_streams.back ();
237
238 switch (type)
239 {
240 case ui_out_type_tuple:
241 fputc_unfiltered ('}', stream);
242 break;
243
244 case ui_out_type_list:
245 fputc_unfiltered (']', stream);
246 break;
247
248 default:
249 internal_error (__FILE__, __LINE__, _("bad switch"));
250 }
251
252 m_suppress_field_separator = false;
253 }
254
255 string_file *
256 mi_ui_out::main_stream ()
257 {
258 gdb_assert (m_streams.size () == 1);
259
260 return (string_file *) m_streams.back ();
261 }
262
263 /* Clear the buffer. */
264
265 void
266 mi_ui_out::rewind ()
267 {
268 main_stream ()->clear ();
269 }
270
271 /* Dump the buffer onto the specified stream. */
272
273 void
274 mi_ui_out::put (ui_file *where)
275 {
276 string_file *mi_stream = main_stream ();
277
278 where->write (mi_stream->data (), mi_stream->size ());
279 mi_stream->clear ();
280 }
281
282 /* Return the current MI version. */
283
284 int
285 mi_ui_out::version ()
286 {
287 return m_mi_version;
288 }
289
290 /* Constructor for an `mi_out_data' object. */
291
292 mi_ui_out::mi_ui_out (int mi_version)
293 : ui_out (mi_version >= 3
294 ? fix_multi_location_breakpoint_output : (ui_out_flag) 0),
295 m_suppress_field_separator (false),
296 m_suppress_output (false),
297 m_mi_version (mi_version)
298 {
299 string_file *stream = new string_file ();
300 m_streams.push_back (stream);
301 }
302
303 mi_ui_out::~mi_ui_out ()
304 {
305 }
306
307 /* See mi/mi-out.h. */
308
309 mi_ui_out *
310 mi_out_new (const char *mi_version)
311 {
312 if (streq (mi_version, INTERP_MI3) || streq (mi_version, INTERP_MI))
313 return new mi_ui_out (3);
314
315 if (streq (mi_version, INTERP_MI2))
316 return new mi_ui_out (2);
317
318 if (streq (mi_version, INTERP_MI1))
319 return new mi_ui_out (1);
320
321 return nullptr;
322 }
323
324 /* Helper function to return the given UIOUT as an mi_ui_out. It is an error
325 to call this function with an ui_out that is not an MI. */
326
327 static mi_ui_out *
328 as_mi_ui_out (ui_out *uiout)
329 {
330 mi_ui_out *mi_uiout = dynamic_cast<mi_ui_out *> (uiout);
331
332 gdb_assert (mi_uiout != NULL);
333
334 return mi_uiout;
335 }
336
337 int
338 mi_version (ui_out *uiout)
339 {
340 return as_mi_ui_out (uiout)->version ();
341 }
342
343 void
344 mi_out_put (ui_out *uiout, struct ui_file *stream)
345 {
346 return as_mi_ui_out (uiout)->put (stream);
347 }
348
349 void
350 mi_out_rewind (ui_out *uiout)
351 {
352 return as_mi_ui_out (uiout)->rewind ();
353 }
This page took 0.037153 seconds and 5 git commands to generate.