| 1 | /* Output generating routines for GDB. |
| 2 | |
| 3 | Copyright (C) 1999-2016 Free Software Foundation, Inc. |
| 4 | |
| 5 | Contributed by Cygnus Solutions. |
| 6 | Written by Fernando Nasser for Cygnus. |
| 7 | |
| 8 | This file is part of GDB. |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation; either version 3 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 22 | |
| 23 | #include "defs.h" |
| 24 | #include "expression.h" /* For language.h */ |
| 25 | #include "language.h" |
| 26 | #include "ui-out.h" |
| 27 | |
| 28 | #include <vector> |
| 29 | #include <memory> |
| 30 | |
| 31 | /* table header structures */ |
| 32 | |
| 33 | struct ui_out_hdr |
| 34 | { |
| 35 | int colno; |
| 36 | int width; |
| 37 | enum ui_align alignment; |
| 38 | char *col_name; |
| 39 | char *colhdr; |
| 40 | struct ui_out_hdr *next; |
| 41 | }; |
| 42 | |
| 43 | struct ui_out_level |
| 44 | { |
| 45 | /* Count each field; the first element is for non-list fields. */ |
| 46 | int field_count; |
| 47 | /* The type of this level. */ |
| 48 | enum ui_out_type type; |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | /* Tables are special. Maintain a separate structure that tracks |
| 53 | their state. At present an output can only contain a single table |
| 54 | but that restriction might eventually be lifted. */ |
| 55 | |
| 56 | struct ui_out_table |
| 57 | { |
| 58 | /* If on, a table is being generated. */ |
| 59 | int flag; |
| 60 | |
| 61 | /* If on, the body of a table is being generated. If off, the table |
| 62 | header is being generated. */ |
| 63 | int body_flag; |
| 64 | |
| 65 | /* The level at which each entry of the table is to be found. A row |
| 66 | (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one |
| 67 | above that of the table. */ |
| 68 | int entry_level; |
| 69 | |
| 70 | /* Number of table columns (as specified in the table_begin call). */ |
| 71 | int columns; |
| 72 | |
| 73 | /* String identifying the table (as specified in the table_begin |
| 74 | call). */ |
| 75 | char *id; |
| 76 | |
| 77 | /* Points to the first table header (if any). */ |
| 78 | struct ui_out_hdr *header_first; |
| 79 | |
| 80 | /* Points to the last table header (if any). */ |
| 81 | struct ui_out_hdr *header_last; |
| 82 | |
| 83 | /* Points to header of NEXT column to format. */ |
| 84 | struct ui_out_hdr *header_next; |
| 85 | |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | /* The ui_out structure */ |
| 90 | |
| 91 | struct ui_out |
| 92 | { |
| 93 | int flags; |
| 94 | /* Specific implementation of ui-out. */ |
| 95 | const struct ui_out_impl *impl; |
| 96 | void *data; |
| 97 | |
| 98 | /* Current level. */ |
| 99 | int level; |
| 100 | |
| 101 | /* Vector to store and track the ui-out levels. */ |
| 102 | std::vector<std::unique_ptr<ui_out_level>> levels; |
| 103 | |
| 104 | /* A table, if any. At present only a single table is supported. */ |
| 105 | struct ui_out_table table; |
| 106 | }; |
| 107 | |
| 108 | /* The current (inner most) level. */ |
| 109 | static struct ui_out_level * |
| 110 | current_level (struct ui_out *uiout) |
| 111 | { |
| 112 | return uiout->levels[uiout->level].get (); |
| 113 | } |
| 114 | |
| 115 | /* Create a new level, of TYPE. Return the new level's index. */ |
| 116 | static int |
| 117 | push_level (struct ui_out *uiout, |
| 118 | enum ui_out_type type) |
| 119 | { |
| 120 | std::unique_ptr<ui_out_level> current (new ui_out_level ()); |
| 121 | |
| 122 | current->field_count = 0; |
| 123 | current->type = type; |
| 124 | |
| 125 | uiout->level++; |
| 126 | uiout->levels.push_back (std::move (current)); |
| 127 | |
| 128 | return uiout->level; |
| 129 | } |
| 130 | |
| 131 | /* Discard the current level, return the discarded level's index. |
| 132 | TYPE is the type of the level being discarded. */ |
| 133 | static int |
| 134 | pop_level (struct ui_out *uiout, |
| 135 | enum ui_out_type type) |
| 136 | { |
| 137 | /* We had better not underflow the buffer. */ |
| 138 | gdb_assert (uiout->level > 0); |
| 139 | gdb_assert (current_level (uiout)->type == type); |
| 140 | |
| 141 | uiout->levels.pop_back (); |
| 142 | uiout->level--; |
| 143 | |
| 144 | return uiout->level + 1; |
| 145 | } |
| 146 | |
| 147 | /* These are the interfaces to implementation functions. */ |
| 148 | |
| 149 | static void uo_table_begin (struct ui_out *uiout, int nbrofcols, |
| 150 | int nr_rows, const char *tblid); |
| 151 | static void uo_table_body (struct ui_out *uiout); |
| 152 | static void uo_table_end (struct ui_out *uiout); |
| 153 | static void uo_table_header (struct ui_out *uiout, int width, |
| 154 | enum ui_align align, const char *col_name, |
| 155 | const char *colhdr); |
| 156 | static void uo_begin (struct ui_out *uiout, |
| 157 | enum ui_out_type type, |
| 158 | int level, const char *id); |
| 159 | static void uo_end (struct ui_out *uiout, |
| 160 | enum ui_out_type type, |
| 161 | int level); |
| 162 | static void uo_field_int (struct ui_out *uiout, int fldno, int width, |
| 163 | enum ui_align align, const char *fldname, int value); |
| 164 | static void uo_field_skip (struct ui_out *uiout, int fldno, int width, |
| 165 | enum ui_align align, const char *fldname); |
| 166 | static void uo_field_fmt (struct ui_out *uiout, int fldno, int width, |
| 167 | enum ui_align align, const char *fldname, |
| 168 | const char *format, va_list args) |
| 169 | ATTRIBUTE_PRINTF (6, 0); |
| 170 | static void uo_spaces (struct ui_out *uiout, int numspaces); |
| 171 | static void uo_text (struct ui_out *uiout, const char *string); |
| 172 | static void uo_message (struct ui_out *uiout, |
| 173 | const char *format, va_list args) |
| 174 | ATTRIBUTE_PRINTF (2, 0); |
| 175 | static void uo_wrap_hint (struct ui_out *uiout, const char *identstring); |
| 176 | static void uo_flush (struct ui_out *uiout); |
| 177 | static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream); |
| 178 | |
| 179 | /* Prototypes for local functions */ |
| 180 | |
| 181 | static void append_header_to_list (struct ui_out *uiout, int width, |
| 182 | enum ui_align alignment, const char *col_name, |
| 183 | const char *colhdr); |
| 184 | static int get_next_header (struct ui_out *uiout, int *colno, int *width, |
| 185 | enum ui_align *alignment, char **colhdr); |
| 186 | static void clear_header_list (struct ui_out *uiout); |
| 187 | static void clear_table (struct ui_out *uiout); |
| 188 | static void verify_field (struct ui_out *uiout, int *fldno, int *width, |
| 189 | enum ui_align *align); |
| 190 | |
| 191 | /* exported functions (ui_out API) */ |
| 192 | |
| 193 | /* Mark beginning of a table. */ |
| 194 | |
| 195 | static void |
| 196 | ui_out_table_begin (struct ui_out *uiout, int nbrofcols, |
| 197 | int nr_rows, |
| 198 | const char *tblid) |
| 199 | { |
| 200 | if (uiout->table.flag) |
| 201 | internal_error (__FILE__, __LINE__, |
| 202 | _("tables cannot be nested; table_begin found before \ |
| 203 | previous table_end.")); |
| 204 | |
| 205 | uiout->table.flag = 1; |
| 206 | uiout->table.body_flag = 0; |
| 207 | uiout->table.entry_level = uiout->level + 1; |
| 208 | uiout->table.columns = nbrofcols; |
| 209 | if (tblid != NULL) |
| 210 | uiout->table.id = xstrdup (tblid); |
| 211 | else |
| 212 | uiout->table.id = NULL; |
| 213 | clear_header_list (uiout); |
| 214 | |
| 215 | uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id); |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | ui_out_table_body (struct ui_out *uiout) |
| 220 | { |
| 221 | if (!uiout->table.flag) |
| 222 | internal_error (__FILE__, __LINE__, |
| 223 | _("table_body outside a table is not valid; it must be \ |
| 224 | after a table_begin and before a table_end.")); |
| 225 | if (uiout->table.body_flag) |
| 226 | internal_error (__FILE__, __LINE__, |
| 227 | _("extra table_body call not allowed; there must be \ |
| 228 | only one table_body after a table_begin and before a table_end.")); |
| 229 | if (uiout->table.header_next->colno != uiout->table.columns) |
| 230 | internal_error (__FILE__, __LINE__, |
| 231 | _("number of headers differ from number of table \ |
| 232 | columns.")); |
| 233 | |
| 234 | uiout->table.body_flag = 1; |
| 235 | uiout->table.header_next = uiout->table.header_first; |
| 236 | |
| 237 | uo_table_body (uiout); |
| 238 | } |
| 239 | |
| 240 | static void |
| 241 | ui_out_table_end (struct ui_out *uiout) |
| 242 | { |
| 243 | if (!uiout->table.flag) |
| 244 | internal_error (__FILE__, __LINE__, |
| 245 | _("misplaced table_end or missing table_begin.")); |
| 246 | |
| 247 | uiout->table.entry_level = 0; |
| 248 | uiout->table.body_flag = 0; |
| 249 | uiout->table.flag = 0; |
| 250 | |
| 251 | uo_table_end (uiout); |
| 252 | clear_table (uiout); |
| 253 | } |
| 254 | |
| 255 | void |
| 256 | ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment, |
| 257 | const char *col_name, |
| 258 | const char *colhdr) |
| 259 | { |
| 260 | if (!uiout->table.flag || uiout->table.body_flag) |
| 261 | internal_error (__FILE__, __LINE__, |
| 262 | _("table header must be specified after table_begin \ |
| 263 | and before table_body.")); |
| 264 | |
| 265 | append_header_to_list (uiout, width, alignment, col_name, colhdr); |
| 266 | |
| 267 | uo_table_header (uiout, width, alignment, col_name, colhdr); |
| 268 | } |
| 269 | |
| 270 | static void |
| 271 | do_cleanup_table_end (void *data) |
| 272 | { |
| 273 | struct ui_out *ui_out = (struct ui_out *) data; |
| 274 | |
| 275 | ui_out_table_end (ui_out); |
| 276 | } |
| 277 | |
| 278 | struct cleanup * |
| 279 | make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols, |
| 280 | int nr_rows, const char *tblid) |
| 281 | { |
| 282 | ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid); |
| 283 | return make_cleanup (do_cleanup_table_end, ui_out); |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | ui_out_begin (struct ui_out *uiout, |
| 288 | enum ui_out_type type, |
| 289 | const char *id) |
| 290 | { |
| 291 | int new_level; |
| 292 | |
| 293 | if (uiout->table.flag && !uiout->table.body_flag) |
| 294 | internal_error (__FILE__, __LINE__, |
| 295 | _("table header or table_body expected; lists must be \ |
| 296 | specified after table_body.")); |
| 297 | |
| 298 | /* Be careful to verify the ``field'' before the new tuple/list is |
| 299 | pushed onto the stack. That way the containing list/table/row is |
| 300 | verified and not the newly created tuple/list. This verification |
| 301 | is needed (at least) for the case where a table row entry |
| 302 | contains either a tuple/list. For that case bookkeeping such as |
| 303 | updating the column count or advancing to the next heading still |
| 304 | needs to be performed. */ |
| 305 | { |
| 306 | int fldno; |
| 307 | int width; |
| 308 | enum ui_align align; |
| 309 | |
| 310 | verify_field (uiout, &fldno, &width, &align); |
| 311 | } |
| 312 | |
| 313 | new_level = push_level (uiout, type); |
| 314 | |
| 315 | /* If the push puts us at the same level as a table row entry, we've |
| 316 | got a new table row. Put the header pointer back to the start. */ |
| 317 | if (uiout->table.body_flag |
| 318 | && uiout->table.entry_level == new_level) |
| 319 | uiout->table.header_next = uiout->table.header_first; |
| 320 | |
| 321 | uo_begin (uiout, type, new_level, id); |
| 322 | } |
| 323 | |
| 324 | void |
| 325 | ui_out_end (struct ui_out *uiout, |
| 326 | enum ui_out_type type) |
| 327 | { |
| 328 | int old_level = pop_level (uiout, type); |
| 329 | |
| 330 | uo_end (uiout, type, old_level); |
| 331 | } |
| 332 | |
| 333 | struct ui_out_end_cleanup_data |
| 334 | { |
| 335 | struct ui_out *uiout; |
| 336 | enum ui_out_type type; |
| 337 | }; |
| 338 | |
| 339 | static void |
| 340 | do_cleanup_end (void *data) |
| 341 | { |
| 342 | struct ui_out_end_cleanup_data *end_cleanup_data |
| 343 | = (struct ui_out_end_cleanup_data *) data; |
| 344 | |
| 345 | ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type); |
| 346 | xfree (end_cleanup_data); |
| 347 | } |
| 348 | |
| 349 | static struct cleanup * |
| 350 | make_cleanup_ui_out_end (struct ui_out *uiout, |
| 351 | enum ui_out_type type) |
| 352 | { |
| 353 | struct ui_out_end_cleanup_data *end_cleanup_data; |
| 354 | |
| 355 | end_cleanup_data = XNEW (struct ui_out_end_cleanup_data); |
| 356 | end_cleanup_data->uiout = uiout; |
| 357 | end_cleanup_data->type = type; |
| 358 | return make_cleanup (do_cleanup_end, end_cleanup_data); |
| 359 | } |
| 360 | |
| 361 | struct cleanup * |
| 362 | make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout, |
| 363 | const char *id) |
| 364 | { |
| 365 | ui_out_begin (uiout, ui_out_type_tuple, id); |
| 366 | return make_cleanup_ui_out_end (uiout, ui_out_type_tuple); |
| 367 | } |
| 368 | |
| 369 | struct cleanup * |
| 370 | make_cleanup_ui_out_list_begin_end (struct ui_out *uiout, |
| 371 | const char *id) |
| 372 | { |
| 373 | ui_out_begin (uiout, ui_out_type_list, id); |
| 374 | return make_cleanup_ui_out_end (uiout, ui_out_type_list); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | ui_out_field_int (struct ui_out *uiout, |
| 379 | const char *fldname, |
| 380 | int value) |
| 381 | { |
| 382 | int fldno; |
| 383 | int width; |
| 384 | enum ui_align align; |
| 385 | |
| 386 | verify_field (uiout, &fldno, &width, &align); |
| 387 | |
| 388 | uo_field_int (uiout, fldno, width, align, fldname, value); |
| 389 | } |
| 390 | |
| 391 | void |
| 392 | ui_out_field_fmt_int (struct ui_out *uiout, |
| 393 | int input_width, |
| 394 | enum ui_align input_align, |
| 395 | const char *fldname, |
| 396 | int value) |
| 397 | { |
| 398 | int fldno; |
| 399 | int width; |
| 400 | enum ui_align align; |
| 401 | |
| 402 | verify_field (uiout, &fldno, &width, &align); |
| 403 | |
| 404 | uo_field_int (uiout, fldno, input_width, input_align, fldname, value); |
| 405 | } |
| 406 | |
| 407 | /* Documented in ui-out.h. */ |
| 408 | |
| 409 | void |
| 410 | ui_out_field_core_addr (struct ui_out *uiout, |
| 411 | const char *fldname, |
| 412 | struct gdbarch *gdbarch, |
| 413 | CORE_ADDR address) |
| 414 | { |
| 415 | ui_out_field_string (uiout, fldname, |
| 416 | print_core_address (gdbarch, address)); |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | ui_out_field_stream (struct ui_out *uiout, |
| 421 | const char *fldname, |
| 422 | struct ui_file *stream) |
| 423 | { |
| 424 | std::string buffer = ui_file_as_string (stream); |
| 425 | |
| 426 | if (!buffer.empty ()) |
| 427 | ui_out_field_string (uiout, fldname, buffer.c_str ()); |
| 428 | else |
| 429 | ui_out_field_skip (uiout, fldname); |
| 430 | ui_file_rewind (stream); |
| 431 | } |
| 432 | |
| 433 | /* Used to omit a field. */ |
| 434 | |
| 435 | void |
| 436 | ui_out_field_skip (struct ui_out *uiout, |
| 437 | const char *fldname) |
| 438 | { |
| 439 | int fldno; |
| 440 | int width; |
| 441 | enum ui_align align; |
| 442 | |
| 443 | verify_field (uiout, &fldno, &width, &align); |
| 444 | |
| 445 | uo_field_skip (uiout, fldno, width, align, fldname); |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | ui_out_field_string (struct ui_out *uiout, |
| 450 | const char *fldname, |
| 451 | const char *string) |
| 452 | { |
| 453 | int fldno; |
| 454 | int width; |
| 455 | enum ui_align align; |
| 456 | |
| 457 | verify_field (uiout, &fldno, &width, &align); |
| 458 | |
| 459 | uo_field_string (uiout, fldno, width, align, fldname, string); |
| 460 | } |
| 461 | |
| 462 | /* VARARGS */ |
| 463 | void |
| 464 | ui_out_field_fmt (struct ui_out *uiout, |
| 465 | const char *fldname, |
| 466 | const char *format, ...) |
| 467 | { |
| 468 | va_list args; |
| 469 | int fldno; |
| 470 | int width; |
| 471 | enum ui_align align; |
| 472 | |
| 473 | /* Will not align, but has to call anyway. */ |
| 474 | verify_field (uiout, &fldno, &width, &align); |
| 475 | |
| 476 | va_start (args, format); |
| 477 | |
| 478 | uo_field_fmt (uiout, fldno, width, align, fldname, format, args); |
| 479 | |
| 480 | va_end (args); |
| 481 | } |
| 482 | |
| 483 | void |
| 484 | ui_out_spaces (struct ui_out *uiout, int numspaces) |
| 485 | { |
| 486 | uo_spaces (uiout, numspaces); |
| 487 | } |
| 488 | |
| 489 | void |
| 490 | ui_out_text (struct ui_out *uiout, |
| 491 | const char *string) |
| 492 | { |
| 493 | uo_text (uiout, string); |
| 494 | } |
| 495 | |
| 496 | void |
| 497 | ui_out_message (struct ui_out *uiout, const char *format, ...) |
| 498 | { |
| 499 | va_list args; |
| 500 | |
| 501 | va_start (args, format); |
| 502 | uo_message (uiout, format, args); |
| 503 | va_end (args); |
| 504 | } |
| 505 | |
| 506 | void |
| 507 | ui_out_wrap_hint (struct ui_out *uiout, const char *identstring) |
| 508 | { |
| 509 | uo_wrap_hint (uiout, identstring); |
| 510 | } |
| 511 | |
| 512 | void |
| 513 | ui_out_flush (struct ui_out *uiout) |
| 514 | { |
| 515 | uo_flush (uiout); |
| 516 | } |
| 517 | |
| 518 | int |
| 519 | ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream) |
| 520 | { |
| 521 | return uo_redirect (uiout, outstream); |
| 522 | } |
| 523 | |
| 524 | /* Test the flags against the mask given. */ |
| 525 | int |
| 526 | ui_out_test_flags (struct ui_out *uiout, int mask) |
| 527 | { |
| 528 | return (uiout->flags & mask); |
| 529 | } |
| 530 | |
| 531 | int |
| 532 | ui_out_is_mi_like_p (struct ui_out *uiout) |
| 533 | { |
| 534 | return uiout->impl->is_mi_like_p; |
| 535 | } |
| 536 | |
| 537 | /* Interface to the implementation functions. */ |
| 538 | |
| 539 | void |
| 540 | uo_table_begin (struct ui_out *uiout, int nbrofcols, |
| 541 | int nr_rows, |
| 542 | const char *tblid) |
| 543 | { |
| 544 | if (!uiout->impl->table_begin) |
| 545 | return; |
| 546 | uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid); |
| 547 | } |
| 548 | |
| 549 | void |
| 550 | uo_table_body (struct ui_out *uiout) |
| 551 | { |
| 552 | if (!uiout->impl->table_body) |
| 553 | return; |
| 554 | uiout->impl->table_body (uiout); |
| 555 | } |
| 556 | |
| 557 | void |
| 558 | uo_table_end (struct ui_out *uiout) |
| 559 | { |
| 560 | if (!uiout->impl->table_end) |
| 561 | return; |
| 562 | uiout->impl->table_end (uiout); |
| 563 | } |
| 564 | |
| 565 | void |
| 566 | uo_table_header (struct ui_out *uiout, int width, enum ui_align align, |
| 567 | const char *col_name, |
| 568 | const char *colhdr) |
| 569 | { |
| 570 | if (!uiout->impl->table_header) |
| 571 | return; |
| 572 | uiout->impl->table_header (uiout, width, align, col_name, colhdr); |
| 573 | } |
| 574 | |
| 575 | /* Clear the table associated with UIOUT. */ |
| 576 | |
| 577 | static void |
| 578 | clear_table (struct ui_out *uiout) |
| 579 | { |
| 580 | xfree (uiout->table.id); |
| 581 | uiout->table.id = NULL; |
| 582 | clear_header_list (uiout); |
| 583 | } |
| 584 | |
| 585 | void |
| 586 | uo_begin (struct ui_out *uiout, |
| 587 | enum ui_out_type type, |
| 588 | int level, |
| 589 | const char *id) |
| 590 | { |
| 591 | if (uiout->impl->begin == NULL) |
| 592 | return; |
| 593 | uiout->impl->begin (uiout, type, level, id); |
| 594 | } |
| 595 | |
| 596 | void |
| 597 | uo_end (struct ui_out *uiout, |
| 598 | enum ui_out_type type, |
| 599 | int level) |
| 600 | { |
| 601 | if (uiout->impl->end == NULL) |
| 602 | return; |
| 603 | uiout->impl->end (uiout, type, level); |
| 604 | } |
| 605 | |
| 606 | void |
| 607 | uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align, |
| 608 | const char *fldname, |
| 609 | int value) |
| 610 | { |
| 611 | if (!uiout->impl->field_int) |
| 612 | return; |
| 613 | uiout->impl->field_int (uiout, fldno, width, align, fldname, value); |
| 614 | } |
| 615 | |
| 616 | void |
| 617 | uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align, |
| 618 | const char *fldname) |
| 619 | { |
| 620 | if (!uiout->impl->field_skip) |
| 621 | return; |
| 622 | uiout->impl->field_skip (uiout, fldno, width, align, fldname); |
| 623 | } |
| 624 | |
| 625 | void |
| 626 | uo_field_string (struct ui_out *uiout, int fldno, int width, |
| 627 | enum ui_align align, |
| 628 | const char *fldname, |
| 629 | const char *string) |
| 630 | { |
| 631 | if (!uiout->impl->field_string) |
| 632 | return; |
| 633 | uiout->impl->field_string (uiout, fldno, width, align, fldname, string); |
| 634 | } |
| 635 | |
| 636 | void |
| 637 | uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align, |
| 638 | const char *fldname, |
| 639 | const char *format, |
| 640 | va_list args) |
| 641 | { |
| 642 | if (!uiout->impl->field_fmt) |
| 643 | return; |
| 644 | uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args); |
| 645 | } |
| 646 | |
| 647 | void |
| 648 | uo_spaces (struct ui_out *uiout, int numspaces) |
| 649 | { |
| 650 | if (!uiout->impl->spaces) |
| 651 | return; |
| 652 | uiout->impl->spaces (uiout, numspaces); |
| 653 | } |
| 654 | |
| 655 | void |
| 656 | uo_text (struct ui_out *uiout, |
| 657 | const char *string) |
| 658 | { |
| 659 | if (!uiout->impl->text) |
| 660 | return; |
| 661 | uiout->impl->text (uiout, string); |
| 662 | } |
| 663 | |
| 664 | void |
| 665 | uo_message (struct ui_out *uiout, |
| 666 | const char *format, |
| 667 | va_list args) |
| 668 | { |
| 669 | if (!uiout->impl->message) |
| 670 | return; |
| 671 | uiout->impl->message (uiout, format, args); |
| 672 | } |
| 673 | |
| 674 | void |
| 675 | uo_wrap_hint (struct ui_out *uiout, const char *identstring) |
| 676 | { |
| 677 | if (!uiout->impl->wrap_hint) |
| 678 | return; |
| 679 | uiout->impl->wrap_hint (uiout, identstring); |
| 680 | } |
| 681 | |
| 682 | void |
| 683 | uo_flush (struct ui_out *uiout) |
| 684 | { |
| 685 | if (!uiout->impl->flush) |
| 686 | return; |
| 687 | uiout->impl->flush (uiout); |
| 688 | } |
| 689 | |
| 690 | int |
| 691 | uo_redirect (struct ui_out *uiout, struct ui_file *outstream) |
| 692 | { |
| 693 | if (!uiout->impl->redirect) |
| 694 | return -1; |
| 695 | return uiout->impl->redirect (uiout, outstream); |
| 696 | } |
| 697 | |
| 698 | /* local functions */ |
| 699 | |
| 700 | /* List of column headers manipulation routines. */ |
| 701 | |
| 702 | static void |
| 703 | clear_header_list (struct ui_out *uiout) |
| 704 | { |
| 705 | while (uiout->table.header_first != NULL) |
| 706 | { |
| 707 | uiout->table.header_next = uiout->table.header_first; |
| 708 | uiout->table.header_first = uiout->table.header_first->next; |
| 709 | xfree (uiout->table.header_next->colhdr); |
| 710 | xfree (uiout->table.header_next->col_name); |
| 711 | delete uiout->table.header_next; |
| 712 | } |
| 713 | |
| 714 | gdb_assert (uiout->table.header_first == NULL); |
| 715 | uiout->table.header_last = NULL; |
| 716 | uiout->table.header_next = NULL; |
| 717 | } |
| 718 | |
| 719 | static void |
| 720 | append_header_to_list (struct ui_out *uiout, |
| 721 | int width, |
| 722 | enum ui_align alignment, |
| 723 | const char *col_name, |
| 724 | const char *colhdr) |
| 725 | { |
| 726 | struct ui_out_hdr *temphdr; |
| 727 | |
| 728 | temphdr = new ui_out_hdr (); |
| 729 | temphdr->width = width; |
| 730 | temphdr->alignment = alignment; |
| 731 | /* We have to copy the column title as the original may be an |
| 732 | automatic. */ |
| 733 | if (colhdr != NULL) |
| 734 | temphdr->colhdr = xstrdup (colhdr); |
| 735 | else |
| 736 | temphdr->colhdr = NULL; |
| 737 | |
| 738 | if (col_name != NULL) |
| 739 | temphdr->col_name = xstrdup (col_name); |
| 740 | else if (colhdr != NULL) |
| 741 | temphdr->col_name = xstrdup (colhdr); |
| 742 | else |
| 743 | temphdr->col_name = NULL; |
| 744 | |
| 745 | temphdr->next = NULL; |
| 746 | if (uiout->table.header_first == NULL) |
| 747 | { |
| 748 | temphdr->colno = 1; |
| 749 | uiout->table.header_first = temphdr; |
| 750 | uiout->table.header_last = temphdr; |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | temphdr->colno = uiout->table.header_last->colno + 1; |
| 755 | uiout->table.header_last->next = temphdr; |
| 756 | uiout->table.header_last = temphdr; |
| 757 | } |
| 758 | uiout->table.header_next = uiout->table.header_last; |
| 759 | } |
| 760 | |
| 761 | /* Extract the format information for the NEXT header and advance |
| 762 | the header pointer. Return 0 if there was no next header. */ |
| 763 | |
| 764 | static int |
| 765 | get_next_header (struct ui_out *uiout, |
| 766 | int *colno, |
| 767 | int *width, |
| 768 | enum ui_align *alignment, |
| 769 | char **colhdr) |
| 770 | { |
| 771 | /* There may be no headers at all or we may have used all columns. */ |
| 772 | if (uiout->table.header_next == NULL) |
| 773 | return 0; |
| 774 | *colno = uiout->table.header_next->colno; |
| 775 | *width = uiout->table.header_next->width; |
| 776 | *alignment = uiout->table.header_next->alignment; |
| 777 | *colhdr = uiout->table.header_next->colhdr; |
| 778 | /* Advance the header pointer to the next entry. */ |
| 779 | uiout->table.header_next = uiout->table.header_next->next; |
| 780 | return 1; |
| 781 | } |
| 782 | |
| 783 | |
| 784 | /* Verify that the field/tuple/list is correctly positioned. Return |
| 785 | the field number and corresponding alignment (if |
| 786 | available/applicable). */ |
| 787 | |
| 788 | static void |
| 789 | verify_field (struct ui_out *uiout, int *fldno, int *width, |
| 790 | enum ui_align *align) |
| 791 | { |
| 792 | struct ui_out_level *current = current_level (uiout); |
| 793 | char *text; |
| 794 | |
| 795 | if (uiout->table.flag) |
| 796 | { |
| 797 | if (!uiout->table.body_flag) |
| 798 | internal_error (__FILE__, __LINE__, |
| 799 | _("table_body missing; table fields must be \ |
| 800 | specified after table_body and inside a list.")); |
| 801 | /* NOTE: cagney/2001-12-08: There was a check here to ensure |
| 802 | that this code was only executed when uiout->level was |
| 803 | greater than zero. That no longer applies - this code is run |
| 804 | before each table row tuple is started and at that point the |
| 805 | level is zero. */ |
| 806 | } |
| 807 | |
| 808 | current->field_count += 1; |
| 809 | |
| 810 | if (uiout->table.body_flag |
| 811 | && uiout->table.entry_level == uiout->level |
| 812 | && get_next_header (uiout, fldno, width, align, &text)) |
| 813 | { |
| 814 | if (*fldno != current->field_count) |
| 815 | internal_error (__FILE__, __LINE__, |
| 816 | _("ui-out internal error in handling headers.")); |
| 817 | } |
| 818 | else |
| 819 | { |
| 820 | *width = 0; |
| 821 | *align = ui_noalign; |
| 822 | *fldno = current->field_count; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | |
| 827 | /* Access to ui-out members data. */ |
| 828 | |
| 829 | void * |
| 830 | ui_out_data (struct ui_out *uiout) |
| 831 | { |
| 832 | return uiout->data; |
| 833 | } |
| 834 | |
| 835 | /* Access table field parameters. */ |
| 836 | int |
| 837 | ui_out_query_field (struct ui_out *uiout, int colno, |
| 838 | int *width, int *alignment, char **col_name) |
| 839 | { |
| 840 | struct ui_out_hdr *hdr; |
| 841 | |
| 842 | if (!uiout->table.flag) |
| 843 | return 0; |
| 844 | |
| 845 | for (hdr = uiout->table.header_first; hdr; hdr = hdr->next) |
| 846 | if (hdr->colno == colno) |
| 847 | { |
| 848 | *width = hdr->width; |
| 849 | *alignment = hdr->alignment; |
| 850 | *col_name = hdr->col_name; |
| 851 | return 1; |
| 852 | } |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | /* Initialize private members at startup. */ |
| 858 | |
| 859 | struct ui_out * |
| 860 | ui_out_new (const struct ui_out_impl *impl, void *data, |
| 861 | int flags) |
| 862 | { |
| 863 | struct ui_out *uiout = new ui_out (); |
| 864 | std::unique_ptr<ui_out_level> current (new ui_out_level ()); |
| 865 | |
| 866 | uiout->data = data; |
| 867 | uiout->impl = impl; |
| 868 | uiout->flags = flags; |
| 869 | uiout->table.flag = 0; |
| 870 | uiout->table.body_flag = 0; |
| 871 | uiout->level = 0; |
| 872 | |
| 873 | /* Create uiout->level 0, the default level. */ |
| 874 | current->type = ui_out_type_tuple; |
| 875 | current->field_count = 0; |
| 876 | uiout->levels.push_back (std::move (current)); |
| 877 | |
| 878 | uiout->table.id = NULL; |
| 879 | uiout->table.header_first = NULL; |
| 880 | uiout->table.header_last = NULL; |
| 881 | uiout->table.header_next = NULL; |
| 882 | return uiout; |
| 883 | } |