Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - output generating routines. |
349c5d5f | 2 | |
618f726f | 3 | Copyright (C) 2000-2016 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" | |
23 | #include "ui-out.h" | |
24 | #include "mi-out.h" | |
4d6cceb4 DE |
25 | #include "vec.h" |
26 | ||
27 | typedef struct ui_file *ui_filep; | |
28 | DEF_VEC_P (ui_filep); | |
fb40c209 | 29 | |
fb40c209 AC |
30 | struct ui_out_data |
31 | { | |
59807497 | 32 | int suppress_field_separator; |
76fe6b98 | 33 | int suppress_output; |
b30bf9ee | 34 | int mi_version; |
4d6cceb4 | 35 | VEC (ui_filep) *streams; |
fb40c209 | 36 | }; |
1248ede2 | 37 | typedef struct ui_out_data mi_out_data; |
fb40c209 AC |
38 | |
39 | /* These are the MI output functions */ | |
40 | ||
4d6cceb4 | 41 | static void mi_out_data_dtor (struct ui_out *ui_out); |
e2e11a41 | 42 | static void mi_table_begin (struct ui_out *uiout, int nbrofcols, |
d63f1d40 | 43 | int nr_rows, const char *tblid); |
fb40c209 AC |
44 | static void mi_table_body (struct ui_out *uiout); |
45 | static void mi_table_end (struct ui_out *uiout); | |
46 | static void mi_table_header (struct ui_out *uiout, int width, | |
b25959ec | 47 | enum ui_align alig, const char *col_name, |
e2e11a41 | 48 | const char *colhdr); |
631ec795 AC |
49 | static void mi_begin (struct ui_out *uiout, enum ui_out_type type, |
50 | int level, const char *id); | |
51 | static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level); | |
fb40c209 | 52 | static void mi_field_int (struct ui_out *uiout, int fldno, int width, |
e2e11a41 | 53 | enum ui_align alig, const char *fldname, int value); |
fb40c209 | 54 | static void mi_field_skip (struct ui_out *uiout, int fldno, int width, |
e2e11a41 | 55 | enum ui_align alig, const char *fldname); |
fb40c209 | 56 | static void mi_field_string (struct ui_out *uiout, int fldno, int width, |
e2e11a41 | 57 | enum ui_align alig, const char *fldname, |
fb40c209 AC |
58 | const char *string); |
59 | static void mi_field_fmt (struct ui_out *uiout, int fldno, | |
60 | int width, enum ui_align align, | |
e2e11a41 | 61 | const char *fldname, const char *format, |
a0b31db1 | 62 | va_list args) ATTRIBUTE_PRINTF (6, 0); |
fb40c209 | 63 | static void mi_spaces (struct ui_out *uiout, int numspaces); |
e2e11a41 AC |
64 | static void mi_text (struct ui_out *uiout, const char *string); |
65 | static void mi_message (struct ui_out *uiout, int verbosity, | |
bee0189a | 66 | const char *format, va_list args) |
a0b31db1 | 67 | ATTRIBUTE_PRINTF (3, 0); |
fb40c209 AC |
68 | static void mi_wrap_hint (struct ui_out *uiout, char *identstring); |
69 | static void mi_flush (struct ui_out *uiout); | |
8d3788bd | 70 | static int mi_redirect (struct ui_out *uiout, struct ui_file *outstream); |
fb40c209 AC |
71 | |
72 | /* This is the MI ui-out implementation functions vector */ | |
73 | ||
89de4da4 | 74 | static const struct ui_out_impl mi_ui_out_impl = |
fb40c209 AC |
75 | { |
76 | mi_table_begin, | |
77 | mi_table_body, | |
78 | mi_table_end, | |
79 | mi_table_header, | |
631ec795 AC |
80 | mi_begin, |
81 | mi_end, | |
fb40c209 AC |
82 | mi_field_int, |
83 | mi_field_skip, | |
84 | mi_field_string, | |
85 | mi_field_fmt, | |
86 | mi_spaces, | |
87 | mi_text, | |
88 | mi_message, | |
89 | mi_wrap_hint, | |
9dc5e2a9 | 90 | mi_flush, |
8d3788bd | 91 | mi_redirect, |
4d6cceb4 | 92 | mi_out_data_dtor, |
9dc5e2a9 | 93 | 1, /* Needs MI hacks. */ |
fb40c209 AC |
94 | }; |
95 | ||
96 | /* Prototypes for local functions */ | |
97 | ||
a14ed312 | 98 | extern void _initialize_mi_out (void); |
fb40c209 | 99 | static void field_separator (struct ui_out *uiout); |
d5e8ba62 AC |
100 | static void mi_open (struct ui_out *uiout, const char *name, |
101 | enum ui_out_type type); | |
9a0f0643 | 102 | static void mi_close (struct ui_out *uiout, enum ui_out_type type); |
fb40c209 | 103 | |
2b03b41d | 104 | /* Mark beginning of a table. */ |
fb40c209 AC |
105 | |
106 | void | |
cff22675 AC |
107 | mi_table_begin (struct ui_out *uiout, |
108 | int nr_cols, | |
d63f1d40 | 109 | int nr_rows, |
e2e11a41 | 110 | const char *tblid) |
fb40c209 | 111 | { |
d5e8ba62 | 112 | mi_open (uiout, tblid, ui_out_type_tuple); |
f486487f SM |
113 | mi_field_int (uiout, -1, -1, ui_left, "nr_rows", nr_rows); |
114 | mi_field_int (uiout, -1, -1, ui_left, "nr_cols", nr_cols); | |
cff22675 | 115 | mi_open (uiout, "hdr", ui_out_type_list); |
fb40c209 AC |
116 | } |
117 | ||
2b03b41d | 118 | /* Mark beginning of a table body. */ |
fb40c209 AC |
119 | |
120 | void | |
fba45db2 | 121 | mi_table_body (struct ui_out *uiout) |
fb40c209 | 122 | { |
19ba03f4 | 123 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
102040f0 | 124 | |
76fe6b98 AC |
125 | if (data->suppress_output) |
126 | return; | |
cff22675 AC |
127 | /* close the table header line if there were any headers */ |
128 | mi_close (uiout, ui_out_type_list); | |
cff22675 | 129 | mi_open (uiout, "body", ui_out_type_list); |
fb40c209 AC |
130 | } |
131 | ||
2b03b41d | 132 | /* Mark end of a table. */ |
fb40c209 AC |
133 | |
134 | void | |
fba45db2 | 135 | mi_table_end (struct ui_out *uiout) |
fb40c209 | 136 | { |
19ba03f4 | 137 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
102040f0 | 138 | |
76fe6b98 | 139 | data->suppress_output = 0; |
cff22675 | 140 | mi_close (uiout, ui_out_type_list); /* body */ |
666547aa | 141 | mi_close (uiout, ui_out_type_tuple); |
fb40c209 AC |
142 | } |
143 | ||
2b03b41d | 144 | /* Specify table header. */ |
fb40c209 AC |
145 | |
146 | void | |
46712191 | 147 | mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment, |
2b03b41d | 148 | const char *col_name, const char *colhdr) |
fb40c209 | 149 | { |
19ba03f4 | 150 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
102040f0 | 151 | |
76fe6b98 AC |
152 | if (data->suppress_output) |
153 | return; | |
2b03b41d | 154 | |
cff22675 | 155 | mi_open (uiout, NULL, ui_out_type_tuple); |
f486487f SM |
156 | mi_field_int (uiout, 0, 0, ui_center, "width", width); |
157 | mi_field_int (uiout, 0, 0, ui_center, "alignment", alignment); | |
158 | mi_field_string (uiout, 0, 0, ui_center, "col_name", col_name); | |
cff22675 AC |
159 | mi_field_string (uiout, 0, width, alignment, "colhdr", colhdr); |
160 | mi_close (uiout, ui_out_type_tuple); | |
fb40c209 AC |
161 | } |
162 | ||
2b03b41d | 163 | /* Mark beginning of a list. */ |
fb40c209 AC |
164 | |
165 | void | |
2b03b41d | 166 | mi_begin (struct ui_out *uiout, enum ui_out_type type, int level, |
9a0f0643 | 167 | const char *id) |
fb40c209 | 168 | { |
19ba03f4 | 169 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
102040f0 | 170 | |
76fe6b98 AC |
171 | if (data->suppress_output) |
172 | return; | |
2b03b41d | 173 | |
d5e8ba62 | 174 | mi_open (uiout, id, type); |
fb40c209 AC |
175 | } |
176 | ||
2b03b41d | 177 | /* Mark end of a list. */ |
fb40c209 AC |
178 | |
179 | void | |
2b03b41d | 180 | mi_end (struct ui_out *uiout, enum ui_out_type type, int level) |
fb40c209 | 181 | { |
19ba03f4 | 182 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
102040f0 | 183 | |
76fe6b98 AC |
184 | if (data->suppress_output) |
185 | return; | |
2b03b41d | 186 | |
9a0f0643 | 187 | mi_close (uiout, type); |
fb40c209 AC |
188 | } |
189 | ||
2b03b41d | 190 | /* Output an int field. */ |
fb40c209 | 191 | |
2b03b41d | 192 | static void |
46712191 KB |
193 | mi_field_int (struct ui_out *uiout, int fldno, int width, |
194 | enum ui_align alignment, const char *fldname, int value) | |
fb40c209 | 195 | { |
102040f0 | 196 | char buffer[20]; /* FIXME: how many chars long a %d can become? */ |
19ba03f4 | 197 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
102040f0 | 198 | |
76fe6b98 AC |
199 | if (data->suppress_output) |
200 | return; | |
fb40c209 | 201 | |
08850b56 | 202 | xsnprintf (buffer, sizeof (buffer), "%d", value); |
fb40c209 AC |
203 | mi_field_string (uiout, fldno, width, alignment, fldname, buffer); |
204 | } | |
205 | ||
2b03b41d | 206 | /* Used to omit a field. */ |
fb40c209 AC |
207 | |
208 | void | |
46712191 KB |
209 | mi_field_skip (struct ui_out *uiout, int fldno, int width, |
210 | enum ui_align alignment, const char *fldname) | |
fb40c209 | 211 | { |
fb40c209 AC |
212 | } |
213 | ||
2b03b41d SS |
214 | /* Other specific mi_field_* end up here so alignment and field |
215 | separators are both handled by mi_field_string. */ | |
fb40c209 AC |
216 | |
217 | void | |
2b03b41d SS |
218 | mi_field_string (struct ui_out *uiout, int fldno, int width, |
219 | enum ui_align align, const char *fldname, const char *string) | |
fb40c209 | 220 | { |
19ba03f4 | 221 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 222 | struct ui_file *stream; |
102040f0 | 223 | |
76fe6b98 AC |
224 | if (data->suppress_output) |
225 | return; | |
2b03b41d | 226 | |
4d6cceb4 | 227 | stream = VEC_last (ui_filep, data->streams); |
fb40c209 AC |
228 | field_separator (uiout); |
229 | if (fldname) | |
4d6cceb4 DE |
230 | fprintf_unfiltered (stream, "%s=", fldname); |
231 | fprintf_unfiltered (stream, "\""); | |
fb40c209 | 232 | if (string) |
4d6cceb4 DE |
233 | fputstr_unfiltered (string, '"', stream); |
234 | fprintf_unfiltered (stream, "\""); | |
fb40c209 AC |
235 | } |
236 | ||
2b03b41d | 237 | /* This is the only field function that does not align. */ |
fb40c209 AC |
238 | |
239 | void | |
2b03b41d SS |
240 | mi_field_fmt (struct ui_out *uiout, int fldno, int width, |
241 | enum ui_align align, const char *fldname, | |
242 | const char *format, va_list args) | |
fb40c209 | 243 | { |
19ba03f4 | 244 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 245 | struct ui_file *stream; |
102040f0 | 246 | |
76fe6b98 AC |
247 | if (data->suppress_output) |
248 | return; | |
2b03b41d | 249 | |
4d6cceb4 | 250 | stream = VEC_last (ui_filep, data->streams); |
fb40c209 AC |
251 | field_separator (uiout); |
252 | if (fldname) | |
4d6cceb4 | 253 | fprintf_unfiltered (stream, "%s=\"", fldname); |
fb40c209 | 254 | else |
4d6cceb4 DE |
255 | fputs_unfiltered ("\"", stream); |
256 | vfprintf_unfiltered (stream, format, args); | |
257 | fputs_unfiltered ("\"", stream); | |
fb40c209 AC |
258 | } |
259 | ||
260 | void | |
fba45db2 | 261 | mi_spaces (struct ui_out *uiout, int numspaces) |
fb40c209 AC |
262 | { |
263 | } | |
264 | ||
265 | void | |
e2e11a41 | 266 | mi_text (struct ui_out *uiout, const char *string) |
fb40c209 AC |
267 | { |
268 | } | |
269 | ||
270 | void | |
e2e11a41 | 271 | mi_message (struct ui_out *uiout, int verbosity, |
2b03b41d | 272 | const char *format, va_list args) |
fb40c209 AC |
273 | { |
274 | } | |
275 | ||
276 | void | |
fba45db2 | 277 | mi_wrap_hint (struct ui_out *uiout, char *identstring) |
fb40c209 AC |
278 | { |
279 | wrap_here (identstring); | |
280 | } | |
281 | ||
282 | void | |
fba45db2 | 283 | mi_flush (struct ui_out *uiout) |
fb40c209 | 284 | { |
19ba03f4 | 285 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 286 | struct ui_file *stream = VEC_last (ui_filep, data->streams); |
102040f0 | 287 | |
4d6cceb4 | 288 | gdb_flush (stream); |
fb40c209 AC |
289 | } |
290 | ||
8d3788bd VP |
291 | int |
292 | mi_redirect (struct ui_out *uiout, struct ui_file *outstream) | |
293 | { | |
19ba03f4 | 294 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
8d3788bd VP |
295 | |
296 | if (outstream != NULL) | |
4d6cceb4 DE |
297 | VEC_safe_push (ui_filep, data->streams, outstream); |
298 | else | |
299 | VEC_pop (ui_filep, data->streams); | |
8d3788bd VP |
300 | |
301 | return 0; | |
302 | } | |
303 | ||
fb40c209 AC |
304 | /* local functions */ |
305 | ||
fb40c209 AC |
306 | /* access to ui_out format private members */ |
307 | ||
308 | static void | |
309 | field_separator (struct ui_out *uiout) | |
310 | { | |
19ba03f4 | 311 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 312 | struct ui_file *stream = VEC_last (ui_filep, data->streams); |
102040f0 | 313 | |
59807497 AC |
314 | if (data->suppress_field_separator) |
315 | data->suppress_field_separator = 0; | |
fb40c209 | 316 | else |
4d6cceb4 | 317 | fputc_unfiltered (',', stream); |
fb40c209 AC |
318 | } |
319 | ||
320 | static void | |
2b03b41d | 321 | mi_open (struct ui_out *uiout, const char *name, enum ui_out_type type) |
fb40c209 | 322 | { |
19ba03f4 | 323 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 324 | struct ui_file *stream = VEC_last (ui_filep, data->streams); |
102040f0 | 325 | |
d5e8ba62 | 326 | field_separator (uiout); |
59807497 | 327 | data->suppress_field_separator = 1; |
d5e8ba62 | 328 | if (name) |
4d6cceb4 | 329 | fprintf_unfiltered (stream, "%s=", name); |
5a9aa5dc AC |
330 | switch (type) |
331 | { | |
332 | case ui_out_type_tuple: | |
4d6cceb4 | 333 | fputc_unfiltered ('{', stream); |
5a9aa5dc AC |
334 | break; |
335 | case ui_out_type_list: | |
4d6cceb4 | 336 | fputc_unfiltered ('[', stream); |
5a9aa5dc AC |
337 | break; |
338 | default: | |
e2e0b3e5 | 339 | internal_error (__FILE__, __LINE__, _("bad switch")); |
5a9aa5dc | 340 | } |
fb40c209 AC |
341 | } |
342 | ||
343 | static void | |
2b03b41d | 344 | mi_close (struct ui_out *uiout, enum ui_out_type type) |
fb40c209 | 345 | { |
19ba03f4 | 346 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 347 | struct ui_file *stream = VEC_last (ui_filep, data->streams); |
102040f0 | 348 | |
5a9aa5dc AC |
349 | switch (type) |
350 | { | |
351 | case ui_out_type_tuple: | |
4d6cceb4 | 352 | fputc_unfiltered ('}', stream); |
5a9aa5dc AC |
353 | break; |
354 | case ui_out_type_list: | |
4d6cceb4 | 355 | fputc_unfiltered (']', stream); |
5a9aa5dc AC |
356 | break; |
357 | default: | |
e2e0b3e5 | 358 | internal_error (__FILE__, __LINE__, _("bad switch")); |
5a9aa5dc | 359 | } |
59807497 | 360 | data->suppress_field_separator = 0; |
fb40c209 AC |
361 | } |
362 | ||
2b03b41d | 363 | /* Add a string to the buffer. */ |
fb40c209 AC |
364 | |
365 | void | |
366 | mi_out_buffered (struct ui_out *uiout, char *string) | |
367 | { | |
19ba03f4 | 368 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 369 | struct ui_file *stream = VEC_last (ui_filep, data->streams); |
102040f0 | 370 | |
4d6cceb4 | 371 | fprintf_unfiltered (stream, "%s", string); |
fb40c209 AC |
372 | } |
373 | ||
2b03b41d | 374 | /* Clear the buffer. */ |
fb40c209 AC |
375 | |
376 | void | |
377 | mi_out_rewind (struct ui_out *uiout) | |
378 | { | |
19ba03f4 | 379 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 380 | struct ui_file *stream = VEC_last (ui_filep, data->streams); |
102040f0 | 381 | |
4d6cceb4 | 382 | ui_file_rewind (stream); |
fb40c209 AC |
383 | } |
384 | ||
2b03b41d | 385 | /* Dump the buffer onto the specified stream. */ |
fb40c209 | 386 | |
fb40c209 | 387 | void |
2b03b41d | 388 | mi_out_put (struct ui_out *uiout, struct ui_file *stream) |
fb40c209 | 389 | { |
19ba03f4 | 390 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
4d6cceb4 | 391 | struct ui_file *outstream = VEC_last (ui_filep, data->streams); |
102040f0 | 392 | |
4d6cceb4 DE |
393 | ui_file_put (outstream, ui_file_write_for_put, stream); |
394 | ui_file_rewind (outstream); | |
fb40c209 AC |
395 | } |
396 | ||
2b03b41d | 397 | /* Return the current MI version. */ |
c7ec4050 AC |
398 | |
399 | int | |
400 | mi_version (struct ui_out *uiout) | |
401 | { | |
19ba03f4 | 402 | mi_out_data *data = (mi_out_data *) ui_out_data (uiout); |
102040f0 | 403 | |
c7ec4050 AC |
404 | return data->mi_version; |
405 | } | |
406 | ||
4d6cceb4 DE |
407 | /* Constructor for an `mi_out_data' object. */ |
408 | ||
409 | static void | |
410 | mi_out_data_ctor (mi_out_data *self, int mi_version, struct ui_file *stream) | |
411 | { | |
412 | gdb_assert (stream != NULL); | |
413 | ||
414 | self->streams = NULL; | |
415 | VEC_safe_push (ui_filep, self->streams, stream); | |
416 | ||
417 | self->suppress_field_separator = 0; | |
418 | self->suppress_output = 0; | |
419 | self->mi_version = mi_version; | |
420 | } | |
421 | ||
422 | /* The destructor. */ | |
423 | ||
424 | static void | |
425 | mi_out_data_dtor (struct ui_out *ui_out) | |
426 | { | |
19ba03f4 | 427 | mi_out_data *data = (mi_out_data *) ui_out_data (ui_out); |
4d6cceb4 DE |
428 | |
429 | VEC_free (ui_filep, data->streams); | |
430 | xfree (data); | |
431 | } | |
432 | ||
2b03b41d | 433 | /* Initialize private members at startup. */ |
fb40c209 AC |
434 | |
435 | struct ui_out * | |
b30bf9ee | 436 | mi_out_new (int mi_version) |
fb40c209 AC |
437 | { |
438 | int flags = 0; | |
70ba0933 | 439 | mi_out_data *data = XNEW (mi_out_data); |
4d6cceb4 DE |
440 | struct ui_file *stream = mem_fileopen (); |
441 | ||
442 | mi_out_data_ctor (data, mi_version, stream); | |
fb40c209 AC |
443 | return ui_out_new (&mi_ui_out_impl, data, flags); |
444 | } |