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