| 1 | /* MI Command Set - output generating routines. |
| 2 | Copyright 2000 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Solutions (a Red Hat company). |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 | Boston, MA 02111-1307, USA. */ |
| 21 | |
| 22 | #include "defs.h" |
| 23 | #include "ui-out.h" |
| 24 | #include "mi-out.h" |
| 25 | |
| 26 | /* Convenience macro for allocting typesafe memory. */ |
| 27 | |
| 28 | #ifndef XMALLOC |
| 29 | #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE)) |
| 30 | #endif |
| 31 | |
| 32 | struct ui_out_data |
| 33 | { |
| 34 | int supress_field_separator; |
| 35 | int first_header; |
| 36 | struct ui_file *buffer; |
| 37 | }; |
| 38 | |
| 39 | /* These are the MI output functions */ |
| 40 | |
| 41 | static void mi_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid); |
| 42 | static void mi_table_body (struct ui_out *uiout); |
| 43 | static void mi_table_end (struct ui_out *uiout); |
| 44 | static void mi_table_header (struct ui_out *uiout, int width, |
| 45 | enum ui_align alig, 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, char *fldname, int value); |
| 51 | static void mi_field_skip (struct ui_out *uiout, int fldno, int width, |
| 52 | enum ui_align alig, char *fldname); |
| 53 | static void mi_field_string (struct ui_out *uiout, int fldno, int width, |
| 54 | enum ui_align alig, 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 | char *fldname, char *format, va_list args); |
| 59 | static void mi_spaces (struct ui_out *uiout, int numspaces); |
| 60 | static void mi_text (struct ui_out *uiout, char *string); |
| 61 | static void mi_message (struct ui_out *uiout, int verbosity, char *format, |
| 62 | va_list args); |
| 63 | static void mi_wrap_hint (struct ui_out *uiout, char *identstring); |
| 64 | static void mi_flush (struct ui_out *uiout); |
| 65 | |
| 66 | /* This is the MI ui-out implementation functions vector */ |
| 67 | |
| 68 | /* FIXME: This can be initialized dynamically after default is set to |
| 69 | handle initial output in main.c */ |
| 70 | |
| 71 | struct ui_out_impl mi_ui_out_impl = |
| 72 | { |
| 73 | mi_table_begin, |
| 74 | mi_table_body, |
| 75 | mi_table_end, |
| 76 | mi_table_header, |
| 77 | mi_begin, |
| 78 | mi_end, |
| 79 | mi_field_int, |
| 80 | mi_field_skip, |
| 81 | mi_field_string, |
| 82 | mi_field_fmt, |
| 83 | mi_spaces, |
| 84 | mi_text, |
| 85 | mi_message, |
| 86 | mi_wrap_hint, |
| 87 | mi_flush |
| 88 | }; |
| 89 | |
| 90 | /* Prototypes for local functions */ |
| 91 | |
| 92 | extern void _initialize_mi_out (void); |
| 93 | static void field_separator (struct ui_out *uiout); |
| 94 | static void mi_open (struct ui_out *uiout, enum ui_out_type type); |
| 95 | static void mi_close (struct ui_out *uiout, enum ui_out_type type); |
| 96 | |
| 97 | static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname, |
| 98 | char *format,...); |
| 99 | |
| 100 | /* Mark beginning of a table */ |
| 101 | |
| 102 | void |
| 103 | mi_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid) |
| 104 | { |
| 105 | struct ui_out_data *data = ui_out_data (uiout); |
| 106 | field_separator (uiout); |
| 107 | if (tblid) |
| 108 | fprintf_unfiltered (data->buffer, "%s=", tblid); |
| 109 | mi_open (uiout, ui_out_type_tupple); |
| 110 | data->first_header = 0; |
| 111 | data->supress_field_separator = 1; |
| 112 | } |
| 113 | |
| 114 | /* Mark beginning of a table body */ |
| 115 | |
| 116 | void |
| 117 | mi_table_body (struct ui_out *uiout) |
| 118 | { |
| 119 | struct ui_out_data *data = ui_out_data (uiout); |
| 120 | /* close the table header line if there were any headers */ |
| 121 | if (data->first_header) |
| 122 | mi_close (uiout, ui_out_type_tupple); |
| 123 | } |
| 124 | |
| 125 | /* Mark end of a table */ |
| 126 | |
| 127 | void |
| 128 | mi_table_end (struct ui_out *uiout) |
| 129 | { |
| 130 | struct ui_out_data *data = ui_out_data (uiout); |
| 131 | mi_close (uiout, ui_out_type_tupple); |
| 132 | /* If table was empty this flag did not get reset yet */ |
| 133 | data->supress_field_separator = 0; |
| 134 | } |
| 135 | |
| 136 | /* Specify table header */ |
| 137 | |
| 138 | void |
| 139 | mi_table_header (struct ui_out *uiout, int width, int alignment, char *colhdr) |
| 140 | { |
| 141 | struct ui_out_data *data = ui_out_data (uiout); |
| 142 | if (!data->first_header++) |
| 143 | { |
| 144 | fputs_unfiltered ("hdr=", data->buffer); |
| 145 | mi_open (uiout, ui_out_type_tupple); |
| 146 | } |
| 147 | mi_field_string (uiout, 0, width, alignment, 0, colhdr); |
| 148 | } |
| 149 | |
| 150 | /* Mark beginning of a list */ |
| 151 | |
| 152 | void |
| 153 | mi_begin (struct ui_out *uiout, |
| 154 | enum ui_out_type type, |
| 155 | int level, |
| 156 | const char *id) |
| 157 | { |
| 158 | struct ui_out_data *data = ui_out_data (uiout); |
| 159 | field_separator (uiout); |
| 160 | data->supress_field_separator = 1; |
| 161 | if (id) |
| 162 | fprintf_unfiltered (data->buffer, "%s=", id); |
| 163 | mi_open (uiout, type); |
| 164 | } |
| 165 | |
| 166 | /* Mark end of a list */ |
| 167 | |
| 168 | void |
| 169 | mi_end (struct ui_out *uiout, |
| 170 | enum ui_out_type type, |
| 171 | int level) |
| 172 | { |
| 173 | struct ui_out_data *data = ui_out_data (uiout); |
| 174 | mi_close (uiout, type); |
| 175 | /* If list was empty this flag did not get reset yet */ |
| 176 | data->supress_field_separator = 0; |
| 177 | } |
| 178 | |
| 179 | /* output an int field */ |
| 180 | |
| 181 | void |
| 182 | mi_field_int (struct ui_out *uiout, int fldno, int width, int alignment, |
| 183 | char *fldname, int value) |
| 184 | { |
| 185 | char buffer[20]; /* FIXME: how many chars long a %d can become? */ |
| 186 | |
| 187 | sprintf (buffer, "%d", value); |
| 188 | mi_field_string (uiout, fldno, width, alignment, fldname, buffer); |
| 189 | } |
| 190 | |
| 191 | /* used to ommit a field */ |
| 192 | |
| 193 | void |
| 194 | mi_field_skip (struct ui_out *uiout, int fldno, int width, int alignment, |
| 195 | char *fldname) |
| 196 | { |
| 197 | mi_field_string (uiout, fldno, width, alignment, fldname, ""); |
| 198 | } |
| 199 | |
| 200 | /* other specific mi_field_* end up here so alignment and field |
| 201 | separators are both handled by mi_field_string */ |
| 202 | |
| 203 | void |
| 204 | mi_field_string (struct ui_out *uiout, |
| 205 | int fldno, |
| 206 | int width, |
| 207 | int align, |
| 208 | char *fldname, |
| 209 | const char *string) |
| 210 | { |
| 211 | struct ui_out_data *data = ui_out_data (uiout); |
| 212 | field_separator (uiout); |
| 213 | if (fldname) |
| 214 | fprintf_unfiltered (data->buffer, "%s=", fldname); |
| 215 | fprintf_unfiltered (data->buffer, "\""); |
| 216 | if (string) |
| 217 | fputstr_unfiltered (string, '"', data->buffer); |
| 218 | fprintf_unfiltered (data->buffer, "\""); |
| 219 | } |
| 220 | |
| 221 | /* This is the only field function that does not align */ |
| 222 | |
| 223 | void |
| 224 | mi_field_fmt (struct ui_out *uiout, int fldno, |
| 225 | int width, enum ui_align align, |
| 226 | char *fldname, char *format, va_list args) |
| 227 | { |
| 228 | struct ui_out_data *data = ui_out_data (uiout); |
| 229 | field_separator (uiout); |
| 230 | if (fldname) |
| 231 | fprintf_unfiltered (data->buffer, "%s=\"", fldname); |
| 232 | else |
| 233 | fputs_unfiltered ("\"", data->buffer); |
| 234 | vfprintf_unfiltered (data->buffer, format, args); |
| 235 | fputs_unfiltered ("\"", data->buffer); |
| 236 | } |
| 237 | |
| 238 | void |
| 239 | mi_spaces (struct ui_out *uiout, int numspaces) |
| 240 | { |
| 241 | } |
| 242 | |
| 243 | void |
| 244 | mi_text (struct ui_out *uiout, char *string) |
| 245 | { |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | mi_message (struct ui_out *uiout, int verbosity, char *format, va_list args) |
| 250 | { |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | mi_wrap_hint (struct ui_out *uiout, char *identstring) |
| 255 | { |
| 256 | wrap_here (identstring); |
| 257 | } |
| 258 | |
| 259 | void |
| 260 | mi_flush (struct ui_out *uiout) |
| 261 | { |
| 262 | struct ui_out_data *data = ui_out_data (uiout); |
| 263 | gdb_flush (data->buffer); |
| 264 | } |
| 265 | |
| 266 | /* local functions */ |
| 267 | |
| 268 | /* Like mi_field_fmt, but takes a variable number of args |
| 269 | and makes a va_list and does not insert a separator */ |
| 270 | |
| 271 | /* VARARGS */ |
| 272 | static void |
| 273 | out_field_fmt (struct ui_out *uiout, int fldno, char *fldname, |
| 274 | char *format,...) |
| 275 | { |
| 276 | struct ui_out_data *data = ui_out_data (uiout); |
| 277 | va_list args; |
| 278 | |
| 279 | field_separator (uiout); |
| 280 | if (fldname) |
| 281 | fprintf_unfiltered (data->buffer, "%s=\"", fldname); |
| 282 | else |
| 283 | fputs_unfiltered ("\"", data->buffer); |
| 284 | |
| 285 | va_start (args, format); |
| 286 | vfprintf_unfiltered (data->buffer, format, args); |
| 287 | |
| 288 | fputs_unfiltered ("\"", data->buffer); |
| 289 | |
| 290 | va_end (args); |
| 291 | } |
| 292 | |
| 293 | /* access to ui_out format private members */ |
| 294 | |
| 295 | static void |
| 296 | field_separator (struct ui_out *uiout) |
| 297 | { |
| 298 | struct ui_out_data *data = ui_out_data (uiout); |
| 299 | if (data->supress_field_separator) |
| 300 | data->supress_field_separator = 0; |
| 301 | else |
| 302 | fputc_unfiltered (',', data->buffer); |
| 303 | } |
| 304 | |
| 305 | static void |
| 306 | mi_open (struct ui_out *uiout, |
| 307 | enum ui_out_type type) |
| 308 | { |
| 309 | struct ui_out_data *data = ui_out_data (uiout); |
| 310 | fputc_unfiltered ('{', data->buffer); |
| 311 | } |
| 312 | |
| 313 | static void |
| 314 | mi_close (struct ui_out *uiout, |
| 315 | enum ui_out_type type) |
| 316 | { |
| 317 | struct ui_out_data *data = ui_out_data (uiout); |
| 318 | fputc_unfiltered ('}', data->buffer); |
| 319 | } |
| 320 | |
| 321 | /* add a string to the buffer */ |
| 322 | |
| 323 | void |
| 324 | mi_out_buffered (struct ui_out *uiout, char *string) |
| 325 | { |
| 326 | struct ui_out_data *data = ui_out_data (uiout); |
| 327 | fprintf_unfiltered (data->buffer, "%s", string); |
| 328 | } |
| 329 | |
| 330 | /* clear the buffer */ |
| 331 | |
| 332 | void |
| 333 | mi_out_rewind (struct ui_out *uiout) |
| 334 | { |
| 335 | struct ui_out_data *data = ui_out_data (uiout); |
| 336 | ui_file_rewind (data->buffer); |
| 337 | } |
| 338 | |
| 339 | /* dump the buffer onto the specified stream */ |
| 340 | |
| 341 | static void |
| 342 | do_write (void *data, const char *buffer, long length_buffer) |
| 343 | { |
| 344 | ui_file_write (data, buffer, length_buffer); |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | mi_out_put (struct ui_out *uiout, |
| 349 | struct ui_file *stream) |
| 350 | { |
| 351 | struct ui_out_data *data = ui_out_data (uiout); |
| 352 | ui_file_put (data->buffer, do_write, stream); |
| 353 | ui_file_rewind (data->buffer); |
| 354 | } |
| 355 | |
| 356 | /* initalize private members at startup */ |
| 357 | |
| 358 | struct ui_out * |
| 359 | mi_out_new (void) |
| 360 | { |
| 361 | int flags = 0; |
| 362 | struct ui_out_data *data = XMALLOC (struct ui_out_data); |
| 363 | data->supress_field_separator = 0; |
| 364 | /* FIXME: This code should be using a ``string_file'' and not the |
| 365 | TUI buffer hack. */ |
| 366 | data->buffer = mem_fileopen (); |
| 367 | return ui_out_new (&mi_ui_out_impl, data, flags); |
| 368 | } |
| 369 | |
| 370 | /* standard gdb initialization hook */ |
| 371 | void |
| 372 | _initialize_mi_out (void) |
| 373 | { |
| 374 | /* nothing happens here */ |
| 375 | } |