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