| 1 | /* MI Command Set - output generating routines. |
| 2 | |
| 3 | Copyright (C) 2000, 2002, 2003, 2004, 2005 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 2 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, write to the Free Software |
| 21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 22 | Boston, MA 02110-1301, USA. */ |
| 23 | |
| 24 | #include "defs.h" |
| 25 | #include "ui-out.h" |
| 26 | #include "mi-out.h" |
| 27 | |
| 28 | struct ui_out_data |
| 29 | { |
| 30 | int suppress_field_separator; |
| 31 | int suppress_output; |
| 32 | int mi_version; |
| 33 | struct ui_file *buffer; |
| 34 | }; |
| 35 | typedef struct ui_out_data mi_out_data; |
| 36 | |
| 37 | /* These are the MI output functions */ |
| 38 | |
| 39 | static void mi_table_begin (struct ui_out *uiout, int nbrofcols, |
| 40 | int nr_rows, const char *tblid); |
| 41 | static void mi_table_body (struct ui_out *uiout); |
| 42 | static void mi_table_end (struct ui_out *uiout); |
| 43 | static void mi_table_header (struct ui_out *uiout, int width, |
| 44 | enum ui_align alig, const char *col_name, |
| 45 | const char *colhdr); |
| 46 | static void mi_begin (struct ui_out *uiout, enum ui_out_type type, |
| 47 | int level, const char *id); |
| 48 | static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level); |
| 49 | static void mi_field_int (struct ui_out *uiout, int fldno, int width, |
| 50 | enum ui_align alig, const char *fldname, int value); |
| 51 | static void mi_field_skip (struct ui_out *uiout, int fldno, int width, |
| 52 | enum ui_align alig, const char *fldname); |
| 53 | static void mi_field_string (struct ui_out *uiout, int fldno, int width, |
| 54 | enum ui_align alig, const char *fldname, |
| 55 | const char *string); |
| 56 | static void mi_field_fmt (struct ui_out *uiout, int fldno, |
| 57 | int width, enum ui_align align, |
| 58 | const char *fldname, const char *format, |
| 59 | va_list args) ATTR_FORMAT (printf, 6, 0); |
| 60 | static void mi_spaces (struct ui_out *uiout, int numspaces); |
| 61 | static void mi_text (struct ui_out *uiout, const char *string); |
| 62 | static void mi_message (struct ui_out *uiout, int verbosity, |
| 63 | const char *format, va_list args) |
| 64 | ATTR_FORMAT (printf, 3, 0); |
| 65 | static void mi_wrap_hint (struct ui_out *uiout, char *identstring); |
| 66 | static void mi_flush (struct ui_out *uiout); |
| 67 | |
| 68 | /* This is the MI ui-out implementation functions vector */ |
| 69 | |
| 70 | /* FIXME: This can be initialized dynamically after default is set to |
| 71 | handle initial output in main.c */ |
| 72 | |
| 73 | struct ui_out_impl mi_ui_out_impl = |
| 74 | { |
| 75 | mi_table_begin, |
| 76 | mi_table_body, |
| 77 | mi_table_end, |
| 78 | mi_table_header, |
| 79 | mi_begin, |
| 80 | mi_end, |
| 81 | mi_field_int, |
| 82 | mi_field_skip, |
| 83 | mi_field_string, |
| 84 | mi_field_fmt, |
| 85 | mi_spaces, |
| 86 | mi_text, |
| 87 | mi_message, |
| 88 | mi_wrap_hint, |
| 89 | mi_flush, |
| 90 | NULL, |
| 91 | 1, /* Needs MI hacks. */ |
| 92 | }; |
| 93 | |
| 94 | /* Prototypes for local functions */ |
| 95 | |
| 96 | extern void _initialize_mi_out (void); |
| 97 | static void field_separator (struct ui_out *uiout); |
| 98 | static void mi_open (struct ui_out *uiout, const char *name, |
| 99 | enum ui_out_type type); |
| 100 | static void mi_close (struct ui_out *uiout, enum ui_out_type type); |
| 101 | |
| 102 | /* Mark beginning of a table */ |
| 103 | |
| 104 | void |
| 105 | mi_table_begin (struct ui_out *uiout, |
| 106 | int nr_cols, |
| 107 | int nr_rows, |
| 108 | const char *tblid) |
| 109 | { |
| 110 | mi_out_data *data = ui_out_data (uiout); |
| 111 | mi_open (uiout, tblid, ui_out_type_tuple); |
| 112 | mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/, |
| 113 | "nr_rows", nr_rows); |
| 114 | mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/, |
| 115 | "nr_cols", nr_cols); |
| 116 | mi_open (uiout, "hdr", ui_out_type_list); |
| 117 | } |
| 118 | |
| 119 | /* Mark beginning of a table body */ |
| 120 | |
| 121 | void |
| 122 | mi_table_body (struct ui_out *uiout) |
| 123 | { |
| 124 | mi_out_data *data = ui_out_data (uiout); |
| 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 = ui_out_data (uiout); |
| 138 | data->suppress_output = 0; |
| 139 | mi_close (uiout, ui_out_type_list); /* body */ |
| 140 | mi_close (uiout, ui_out_type_tuple); |
| 141 | } |
| 142 | |
| 143 | /* Specify table header */ |
| 144 | |
| 145 | void |
| 146 | mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment, |
| 147 | const char *col_name, |
| 148 | const char *colhdr) |
| 149 | { |
| 150 | mi_out_data *data = ui_out_data (uiout); |
| 151 | if (data->suppress_output) |
| 152 | return; |
| 153 | mi_open (uiout, NULL, ui_out_type_tuple); |
| 154 | mi_field_int (uiout, 0, 0, 0, "width", width); |
| 155 | mi_field_int (uiout, 0, 0, 0, "alignment", alignment); |
| 156 | mi_field_string (uiout, 0, 0, 0, "col_name", col_name); |
| 157 | mi_field_string (uiout, 0, width, alignment, "colhdr", colhdr); |
| 158 | mi_close (uiout, ui_out_type_tuple); |
| 159 | } |
| 160 | |
| 161 | /* Mark beginning of a list */ |
| 162 | |
| 163 | void |
| 164 | mi_begin (struct ui_out *uiout, |
| 165 | enum ui_out_type type, |
| 166 | int level, |
| 167 | const char *id) |
| 168 | { |
| 169 | mi_out_data *data = ui_out_data (uiout); |
| 170 | if (data->suppress_output) |
| 171 | return; |
| 172 | mi_open (uiout, id, type); |
| 173 | } |
| 174 | |
| 175 | /* Mark end of a list */ |
| 176 | |
| 177 | void |
| 178 | mi_end (struct ui_out *uiout, |
| 179 | enum ui_out_type type, |
| 180 | int level) |
| 181 | { |
| 182 | mi_out_data *data = ui_out_data (uiout); |
| 183 | if (data->suppress_output) |
| 184 | return; |
| 185 | mi_close (uiout, type); |
| 186 | } |
| 187 | |
| 188 | /* output an int field */ |
| 189 | |
| 190 | void |
| 191 | mi_field_int (struct ui_out *uiout, int fldno, int width, |
| 192 | enum ui_align alignment, const char *fldname, int value) |
| 193 | { |
| 194 | char buffer[20]; /* FIXME: how many chars long a %d can become? */ |
| 195 | mi_out_data *data = ui_out_data (uiout); |
| 196 | if (data->suppress_output) |
| 197 | return; |
| 198 | |
| 199 | sprintf (buffer, "%d", value); |
| 200 | mi_field_string (uiout, fldno, width, alignment, fldname, buffer); |
| 201 | } |
| 202 | |
| 203 | /* used to ommit a field */ |
| 204 | |
| 205 | void |
| 206 | mi_field_skip (struct ui_out *uiout, int fldno, int width, |
| 207 | enum ui_align alignment, const char *fldname) |
| 208 | { |
| 209 | mi_out_data *data = ui_out_data (uiout); |
| 210 | if (data->suppress_output) |
| 211 | return; |
| 212 | mi_field_string (uiout, fldno, width, alignment, fldname, ""); |
| 213 | } |
| 214 | |
| 215 | /* other specific mi_field_* end up here so alignment and field |
| 216 | separators are both handled by mi_field_string */ |
| 217 | |
| 218 | void |
| 219 | mi_field_string (struct ui_out *uiout, |
| 220 | int fldno, |
| 221 | int width, |
| 222 | enum ui_align align, |
| 223 | const char *fldname, |
| 224 | const char *string) |
| 225 | { |
| 226 | mi_out_data *data = ui_out_data (uiout); |
| 227 | if (data->suppress_output) |
| 228 | return; |
| 229 | field_separator (uiout); |
| 230 | if (fldname) |
| 231 | fprintf_unfiltered (data->buffer, "%s=", fldname); |
| 232 | fprintf_unfiltered (data->buffer, "\""); |
| 233 | if (string) |
| 234 | fputstr_unfiltered (string, '"', data->buffer); |
| 235 | fprintf_unfiltered (data->buffer, "\""); |
| 236 | } |
| 237 | |
| 238 | /* This is the only field function that does not align */ |
| 239 | |
| 240 | void |
| 241 | mi_field_fmt (struct ui_out *uiout, int fldno, |
| 242 | int width, enum ui_align align, |
| 243 | const char *fldname, |
| 244 | const char *format, |
| 245 | va_list args) |
| 246 | { |
| 247 | mi_out_data *data = ui_out_data (uiout); |
| 248 | if (data->suppress_output) |
| 249 | return; |
| 250 | field_separator (uiout); |
| 251 | if (fldname) |
| 252 | fprintf_unfiltered (data->buffer, "%s=\"", fldname); |
| 253 | else |
| 254 | fputs_unfiltered ("\"", data->buffer); |
| 255 | vfprintf_unfiltered (data->buffer, format, args); |
| 256 | fputs_unfiltered ("\"", data->buffer); |
| 257 | } |
| 258 | |
| 259 | void |
| 260 | mi_spaces (struct ui_out *uiout, int numspaces) |
| 261 | { |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | mi_text (struct ui_out *uiout, const char *string) |
| 266 | { |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | mi_message (struct ui_out *uiout, int verbosity, |
| 271 | const char *format, |
| 272 | 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 = ui_out_data (uiout); |
| 286 | gdb_flush (data->buffer); |
| 287 | } |
| 288 | |
| 289 | /* local functions */ |
| 290 | |
| 291 | /* access to ui_out format private members */ |
| 292 | |
| 293 | static void |
| 294 | field_separator (struct ui_out *uiout) |
| 295 | { |
| 296 | mi_out_data *data = ui_out_data (uiout); |
| 297 | if (data->suppress_field_separator) |
| 298 | data->suppress_field_separator = 0; |
| 299 | else |
| 300 | fputc_unfiltered (',', data->buffer); |
| 301 | } |
| 302 | |
| 303 | static void |
| 304 | mi_open (struct ui_out *uiout, |
| 305 | const char *name, |
| 306 | enum ui_out_type type) |
| 307 | { |
| 308 | mi_out_data *data = ui_out_data (uiout); |
| 309 | field_separator (uiout); |
| 310 | data->suppress_field_separator = 1; |
| 311 | if (name) |
| 312 | fprintf_unfiltered (data->buffer, "%s=", name); |
| 313 | switch (type) |
| 314 | { |
| 315 | case ui_out_type_tuple: |
| 316 | fputc_unfiltered ('{', data->buffer); |
| 317 | break; |
| 318 | case ui_out_type_list: |
| 319 | fputc_unfiltered ('[', data->buffer); |
| 320 | break; |
| 321 | default: |
| 322 | internal_error (__FILE__, __LINE__, _("bad switch")); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static void |
| 327 | mi_close (struct ui_out *uiout, |
| 328 | enum ui_out_type type) |
| 329 | { |
| 330 | mi_out_data *data = ui_out_data (uiout); |
| 331 | switch (type) |
| 332 | { |
| 333 | case ui_out_type_tuple: |
| 334 | fputc_unfiltered ('}', data->buffer); |
| 335 | break; |
| 336 | case ui_out_type_list: |
| 337 | fputc_unfiltered (']', data->buffer); |
| 338 | break; |
| 339 | default: |
| 340 | internal_error (__FILE__, __LINE__, _("bad switch")); |
| 341 | } |
| 342 | data->suppress_field_separator = 0; |
| 343 | } |
| 344 | |
| 345 | /* add a string to the buffer */ |
| 346 | |
| 347 | void |
| 348 | mi_out_buffered (struct ui_out *uiout, char *string) |
| 349 | { |
| 350 | mi_out_data *data = ui_out_data (uiout); |
| 351 | fprintf_unfiltered (data->buffer, "%s", string); |
| 352 | } |
| 353 | |
| 354 | /* clear the buffer */ |
| 355 | |
| 356 | void |
| 357 | mi_out_rewind (struct ui_out *uiout) |
| 358 | { |
| 359 | mi_out_data *data = ui_out_data (uiout); |
| 360 | ui_file_rewind (data->buffer); |
| 361 | } |
| 362 | |
| 363 | /* dump the buffer onto the specified stream */ |
| 364 | |
| 365 | static void |
| 366 | do_write (void *data, const char *buffer, long length_buffer) |
| 367 | { |
| 368 | ui_file_write (data, buffer, length_buffer); |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | mi_out_put (struct ui_out *uiout, |
| 373 | struct ui_file *stream) |
| 374 | { |
| 375 | mi_out_data *data = ui_out_data (uiout); |
| 376 | ui_file_put (data->buffer, do_write, stream); |
| 377 | ui_file_rewind (data->buffer); |
| 378 | } |
| 379 | |
| 380 | /* Current MI version. */ |
| 381 | |
| 382 | int |
| 383 | mi_version (struct ui_out *uiout) |
| 384 | { |
| 385 | mi_out_data *data = ui_out_data (uiout); |
| 386 | return data->mi_version; |
| 387 | } |
| 388 | |
| 389 | /* initalize private members at startup */ |
| 390 | |
| 391 | struct ui_out * |
| 392 | mi_out_new (int mi_version) |
| 393 | { |
| 394 | int flags = 0; |
| 395 | mi_out_data *data = XMALLOC (mi_out_data); |
| 396 | data->suppress_field_separator = 0; |
| 397 | data->suppress_output = 0; |
| 398 | data->mi_version = mi_version; |
| 399 | /* FIXME: This code should be using a ``string_file'' and not the |
| 400 | TUI buffer hack. */ |
| 401 | data->buffer = mem_fileopen (); |
| 402 | return ui_out_new (&mi_ui_out_impl, data, flags); |
| 403 | } |
| 404 | |
| 405 | /* standard gdb initialization hook */ |
| 406 | void |
| 407 | _initialize_mi_out (void) |
| 408 | { |
| 409 | /* nothing happens here */ |
| 410 | } |