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