1 /* MI Command Set - output generating routines.
3 Copyright (C) 2000-2019 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions (a Red Hat company).
7 This file is part of GDB.
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.
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.
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/>. */
31 /* Mark beginning of a table. */
34 mi_ui_out::do_table_begin (int nr_cols
, int nr_rows
,
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
);
43 /* Mark beginning of a table body. */
46 mi_ui_out::do_table_body ()
48 /* close the table header line if there were any headers */
49 close (ui_out_type_list
);
50 open ("body", ui_out_type_list
);
53 /* Mark end of a table. */
56 mi_ui_out::do_table_end ()
58 close (ui_out_type_list
); /* body */
59 close (ui_out_type_tuple
);
62 /* Specify table header. */
65 mi_ui_out::do_table_header (int width
, ui_align alignment
,
66 const std::string
&col_name
,
67 const std::string
&col_hdr
)
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
);
79 /* Mark beginning of a list. */
82 mi_ui_out::do_begin (ui_out_type type
, const char *id
)
87 /* Mark end of a list. */
90 mi_ui_out::do_end (ui_out_type type
)
95 /* Output an int field. */
98 mi_ui_out::do_field_int (int fldno
, int width
, ui_align alignment
,
99 const char *fldname
, int value
)
101 char buffer
[20]; /* FIXME: how many chars long a %d can become? */
103 xsnprintf (buffer
, sizeof (buffer
), "%d", value
);
104 do_field_string (fldno
, width
, alignment
, fldname
, buffer
,
105 ui_out_style_kind::DEFAULT
);
108 /* Used to omit a field. */
111 mi_ui_out::do_field_skip (int fldno
, int width
, ui_align alignment
,
116 /* Other specific mi_field_* end up here so alignment and field
117 separators are both handled by mi_field_string. */
120 mi_ui_out::do_field_string (int fldno
, int width
, ui_align align
,
121 const char *fldname
, const char *string
,
122 ui_out_style_kind style
)
124 ui_file
*stream
= m_streams
.back ();
128 fprintf_unfiltered (stream
, "%s=", fldname
);
129 fprintf_unfiltered (stream
, "\"");
131 fputstr_unfiltered (string
, '"', stream
);
132 fprintf_unfiltered (stream
, "\"");
136 mi_ui_out::do_field_fmt (int fldno
, int width
, ui_align align
,
137 const char *fldname
, const char *format
,
140 ui_file
*stream
= m_streams
.back ();
144 fprintf_unfiltered (stream
, "%s=\"", fldname
);
146 fputs_unfiltered ("\"", stream
);
147 vfprintf_unfiltered (stream
, format
, args
);
148 fputs_unfiltered ("\"", stream
);
152 mi_ui_out::do_spaces (int numspaces
)
157 mi_ui_out::do_text (const char *string
)
162 mi_ui_out::do_message (const char *format
, va_list args
)
167 mi_ui_out::do_wrap_hint (const char *identstring
)
169 wrap_here (identstring
);
173 mi_ui_out::do_flush ()
176 gdb_flush (m_streams
.back ());
180 mi_ui_out::do_redirect (ui_file
*outstream
)
182 if (outstream
!= NULL
)
183 m_streams
.push_back (outstream
);
185 m_streams
.pop_back ();
189 mi_ui_out::field_separator ()
191 if (m_suppress_field_separator
)
192 m_suppress_field_separator
= false;
194 fputc_unfiltered (',', m_streams
.back ());
198 mi_ui_out::open (const char *name
, ui_out_type type
)
200 ui_file
*stream
= m_streams
.back ();
203 m_suppress_field_separator
= true;
206 fprintf_unfiltered (stream
, "%s=", name
);
210 case ui_out_type_tuple
:
211 fputc_unfiltered ('{', stream
);
214 case ui_out_type_list
:
215 fputc_unfiltered ('[', stream
);
219 internal_error (__FILE__
, __LINE__
, _("bad switch"));
224 mi_ui_out::close (ui_out_type type
)
226 ui_file
*stream
= m_streams
.back ();
230 case ui_out_type_tuple
:
231 fputc_unfiltered ('}', stream
);
234 case ui_out_type_list
:
235 fputc_unfiltered (']', stream
);
239 internal_error (__FILE__
, __LINE__
, _("bad switch"));
242 m_suppress_field_separator
= false;
246 mi_ui_out::main_stream ()
248 gdb_assert (m_streams
.size () == 1);
250 return (string_file
*) m_streams
.back ();
253 /* Clear the buffer. */
258 main_stream ()->clear ();
261 /* Dump the buffer onto the specified stream. */
264 mi_ui_out::put (ui_file
*where
)
266 string_file
*mi_stream
= main_stream ();
268 where
->write (mi_stream
->data (), mi_stream
->size ());
272 /* Return the current MI version. */
275 mi_ui_out::version ()
280 /* Constructor for an `mi_out_data' object. */
282 mi_ui_out::mi_ui_out (int mi_version
)
283 : m_suppress_field_separator (false),
284 m_suppress_output (false),
285 m_mi_version (mi_version
)
287 string_file
*stream
= new string_file ();
288 m_streams
.push_back (stream
);
291 mi_ui_out::~mi_ui_out ()
295 /* See mi/mi-out.h. */
298 mi_out_new (const char *mi_version
)
300 if (streq (mi_version
, INTERP_MI3
) || streq (mi_version
, INTERP_MI
))
301 return new mi_ui_out (3);
303 if (streq (mi_version
, INTERP_MI2
))
304 return new mi_ui_out (2);
306 if (streq (mi_version
, INTERP_MI1
))
307 return new mi_ui_out (1);
312 /* Helper function to return the given UIOUT as an mi_ui_out. It is an error
313 to call this function with an ui_out that is not an MI. */
316 as_mi_ui_out (ui_out
*uiout
)
318 mi_ui_out
*mi_uiout
= dynamic_cast<mi_ui_out
*> (uiout
);
320 gdb_assert (mi_uiout
!= NULL
);
326 mi_version (ui_out
*uiout
)
328 return as_mi_ui_out (uiout
)->version ();
332 mi_out_put (ui_out
*uiout
, struct ui_file
*stream
)
334 return as_mi_ui_out (uiout
)->put (stream
);
338 mi_out_rewind (ui_out
*uiout
)
340 return as_mi_ui_out (uiout
)->rewind ();