Commit | Line | Data |
---|---|---|
8b93c638 | 1 | /* Output generating routines for GDB. |
349c5d5f | 2 | |
8acc9f48 | 3 | Copyright (C) 1999-2013 Free Software Foundation, Inc. |
349c5d5f | 4 | |
8b93c638 JM |
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 | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
8b93c638 JM |
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 | |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
8b93c638 JM |
22 | |
23 | #include "defs.h" | |
24 | #include "gdb_string.h" | |
25 | #include "expression.h" /* For language.h */ | |
26 | #include "language.h" | |
27 | #include "ui-out.h" | |
80f49b30 | 28 | #include "gdb_assert.h" |
8b93c638 | 29 | |
8b93c638 JM |
30 | /* table header structures */ |
31 | ||
32 | struct ui_out_hdr | |
33 | { | |
34 | int colno; | |
35 | int width; | |
36 | int alignment; | |
b25959ec | 37 | char *col_name; |
8b93c638 JM |
38 | char *colhdr; |
39 | struct ui_out_hdr *next; | |
40 | }; | |
41 | ||
80f49b30 AC |
42 | /* Maintain a stack so that the info applicable to the inner most list |
43 | is always available. Stack/nested level 0 is reserved for the | |
581e13c1 | 44 | top-level result. */ |
80f49b30 | 45 | |
dc146f7c | 46 | enum { MAX_UI_OUT_LEVELS = 8 }; |
80f49b30 AC |
47 | |
48 | struct ui_out_level | |
49 | { | |
581e13c1 | 50 | /* Count each field; the first element is for non-list fields. */ |
80f49b30 | 51 | int field_count; |
581e13c1 | 52 | /* The type of this level. */ |
631ec795 | 53 | enum ui_out_type type; |
80f49b30 AC |
54 | }; |
55 | ||
bafdd3b3 AC |
56 | /* Tables are special. Maintain a separate structure that tracks |
57 | their state. At present an output can only contain a single table | |
58 | but that restriction might eventually be lifted. */ | |
59 | ||
60 | struct ui_out_table | |
61 | { | |
62 | /* If on, a table is being generated. */ | |
63 | int flag; | |
64 | ||
65 | /* If on, the body of a table is being generated. If off, the table | |
66 | header is being generated. */ | |
67 | int body_flag; | |
68 | ||
a6c47c14 AC |
69 | /* The level at which each entry of the table is to be found. A row |
70 | (a tuple) is made up of entries. Consequently ENTRY_LEVEL is one | |
71 | above that of the table. */ | |
72 | int entry_level; | |
73 | ||
bafdd3b3 AC |
74 | /* Number of table columns (as specified in the table_begin call). */ |
75 | int columns; | |
76 | ||
77 | /* String identifying the table (as specified in the table_begin | |
78 | call). */ | |
79 | char *id; | |
80 | ||
81 | /* Points to the first table header (if any). */ | |
82 | struct ui_out_hdr *header_first; | |
83 | ||
84 | /* Points to the last table header (if any). */ | |
85 | struct ui_out_hdr *header_last; | |
86 | ||
87 | /* Points to header of NEXT column to format. */ | |
88 | struct ui_out_hdr *header_next; | |
89 | ||
90 | }; | |
91 | ||
92 | ||
8b93c638 JM |
93 | /* The ui_out structure */ |
94 | /* Any change here requires a corresponding one in the initialization | |
581e13c1 | 95 | of the default uiout, which is statically initialized. */ |
8b93c638 JM |
96 | |
97 | struct ui_out | |
98 | { | |
99 | int flags; | |
581e13c1 | 100 | /* Specific implementation of ui-out. */ |
8b93c638 | 101 | struct ui_out_impl *impl; |
0a8fce9a | 102 | void *data; |
8b93c638 | 103 | |
bafdd3b3 | 104 | /* Sub structure tracking the ui-out depth. */ |
80f49b30 AC |
105 | int level; |
106 | struct ui_out_level levels[MAX_UI_OUT_LEVELS]; | |
8b93c638 | 107 | |
bafdd3b3 AC |
108 | /* A table, if any. At present only a single table is supported. */ |
109 | struct ui_out_table table; | |
8b93c638 JM |
110 | }; |
111 | ||
581e13c1 | 112 | /* The current (inner most) level. */ |
80f49b30 AC |
113 | static struct ui_out_level * |
114 | current_level (struct ui_out *uiout) | |
115 | { | |
116 | return &uiout->levels[uiout->level]; | |
117 | } | |
118 | ||
581e13c1 | 119 | /* Create a new level, of TYPE. Return the new level's index. */ |
80f49b30 AC |
120 | static int |
121 | push_level (struct ui_out *uiout, | |
631ec795 | 122 | enum ui_out_type type, |
80f49b30 AC |
123 | const char *id) |
124 | { | |
125 | struct ui_out_level *current; | |
5d502164 | 126 | |
581e13c1 | 127 | /* We had better not overflow the buffer. */ |
80f49b30 | 128 | uiout->level++; |
631ec795 | 129 | gdb_assert (uiout->level >= 0 && uiout->level < MAX_UI_OUT_LEVELS); |
80f49b30 AC |
130 | current = current_level (uiout); |
131 | current->field_count = 0; | |
631ec795 | 132 | current->type = type; |
80f49b30 AC |
133 | return uiout->level; |
134 | } | |
135 | ||
136 | /* Discard the current level, return the discarded level's index. | |
581e13c1 | 137 | TYPE is the type of the level being discarded. */ |
80f49b30 | 138 | static int |
631ec795 AC |
139 | pop_level (struct ui_out *uiout, |
140 | enum ui_out_type type) | |
80f49b30 | 141 | { |
581e13c1 | 142 | /* We had better not underflow the buffer. */ |
80f49b30 | 143 | gdb_assert (uiout->level > 0 && uiout->level < MAX_UI_OUT_LEVELS); |
631ec795 | 144 | gdb_assert (current_level (uiout)->type == type); |
80f49b30 AC |
145 | uiout->level--; |
146 | return uiout->level + 1; | |
147 | } | |
148 | ||
149 | ||
581e13c1 | 150 | /* These are the default implementation functions. */ |
8b93c638 JM |
151 | |
152 | static void default_table_begin (struct ui_out *uiout, int nbrofcols, | |
d63f1d40 | 153 | int nr_rows, const char *tblid); |
8b93c638 JM |
154 | static void default_table_body (struct ui_out *uiout); |
155 | static void default_table_end (struct ui_out *uiout); | |
156 | static void default_table_header (struct ui_out *uiout, int width, | |
b25959ec | 157 | enum ui_align alig, const char *col_name, |
e2e11a41 | 158 | const char *colhdr); |
631ec795 AC |
159 | static void default_begin (struct ui_out *uiout, |
160 | enum ui_out_type type, | |
161 | int level, const char *id); | |
162 | static void default_end (struct ui_out *uiout, | |
163 | enum ui_out_type type, | |
164 | int level); | |
8b93c638 | 165 | static void default_field_int (struct ui_out *uiout, int fldno, int width, |
e2e11a41 AC |
166 | enum ui_align alig, |
167 | const char *fldname, | |
168 | int value); | |
8b93c638 | 169 | static void default_field_skip (struct ui_out *uiout, int fldno, int width, |
e2e11a41 AC |
170 | enum ui_align alig, |
171 | const char *fldname); | |
8b93c638 | 172 | static void default_field_string (struct ui_out *uiout, int fldno, int width, |
e2e11a41 AC |
173 | enum ui_align align, |
174 | const char *fldname, | |
8b93c638 JM |
175 | const char *string); |
176 | static void default_field_fmt (struct ui_out *uiout, int fldno, | |
177 | int width, enum ui_align align, | |
e2e11a41 AC |
178 | const char *fldname, |
179 | const char *format, | |
a0b31db1 | 180 | va_list args) ATTRIBUTE_PRINTF (6, 0); |
8b93c638 | 181 | static void default_spaces (struct ui_out *uiout, int numspaces); |
e2e11a41 AC |
182 | static void default_text (struct ui_out *uiout, const char *string); |
183 | static void default_message (struct ui_out *uiout, int verbosity, | |
184 | const char *format, | |
a0b31db1 | 185 | va_list args) ATTRIBUTE_PRINTF (3, 0); |
8b93c638 JM |
186 | static void default_wrap_hint (struct ui_out *uiout, char *identstring); |
187 | static void default_flush (struct ui_out *uiout); | |
188 | ||
581e13c1 | 189 | /* This is the default ui-out implementation functions vector. */ |
8b93c638 JM |
190 | |
191 | struct ui_out_impl default_ui_out_impl = | |
192 | { | |
193 | default_table_begin, | |
194 | default_table_body, | |
195 | default_table_end, | |
196 | default_table_header, | |
631ec795 AC |
197 | default_begin, |
198 | default_end, | |
8b93c638 JM |
199 | default_field_int, |
200 | default_field_skip, | |
201 | default_field_string, | |
202 | default_field_fmt, | |
203 | default_spaces, | |
204 | default_text, | |
205 | default_message, | |
206 | default_wrap_hint, | |
9dc5e2a9 | 207 | default_flush, |
0fac0b41 | 208 | NULL, |
9dc5e2a9 | 209 | 0, /* Does not need MI hacks. */ |
8b93c638 JM |
210 | }; |
211 | ||
212 | /* The default ui_out */ | |
213 | ||
214 | struct ui_out def_uiout = | |
215 | { | |
216 | 0, /* flags */ | |
217 | &default_ui_out_impl, /* impl */ | |
218 | }; | |
219 | ||
220 | /* Pointer to current ui_out */ | |
221 | /* FIXME: This should not be a global, but something passed down from main.c | |
581e13c1 | 222 | or top.c. */ |
8b93c638 | 223 | |
79a45e25 | 224 | struct ui_out *current_uiout = &def_uiout; |
8b93c638 | 225 | |
581e13c1 | 226 | /* These are the interfaces to implementation functions. */ |
8b93c638 | 227 | |
88379baf | 228 | static void uo_table_begin (struct ui_out *uiout, int nbrofcols, |
d63f1d40 | 229 | int nr_rows, const char *tblid); |
8b93c638 JM |
230 | static void uo_table_body (struct ui_out *uiout); |
231 | static void uo_table_end (struct ui_out *uiout); | |
232 | static void uo_table_header (struct ui_out *uiout, int width, | |
b25959ec AC |
233 | enum ui_align align, const char *col_name, |
234 | const char *colhdr); | |
631ec795 AC |
235 | static void uo_begin (struct ui_out *uiout, |
236 | enum ui_out_type type, | |
237 | int level, const char *id); | |
238 | static void uo_end (struct ui_out *uiout, | |
239 | enum ui_out_type type, | |
240 | int level); | |
8b93c638 | 241 | static void uo_field_int (struct ui_out *uiout, int fldno, int width, |
88379baf | 242 | enum ui_align align, const char *fldname, int value); |
8b93c638 | 243 | static void uo_field_skip (struct ui_out *uiout, int fldno, int width, |
88379baf | 244 | enum ui_align align, const char *fldname); |
8b93c638 | 245 | static void uo_field_fmt (struct ui_out *uiout, int fldno, int width, |
88379baf | 246 | enum ui_align align, const char *fldname, |
bee0189a | 247 | const char *format, va_list args) |
a0b31db1 | 248 | ATTRIBUTE_PRINTF (6, 0); |
8b93c638 | 249 | static void uo_spaces (struct ui_out *uiout, int numspaces); |
88379baf | 250 | static void uo_text (struct ui_out *uiout, const char *string); |
8b93c638 | 251 | static void uo_message (struct ui_out *uiout, int verbosity, |
bee0189a | 252 | const char *format, va_list args) |
a0b31db1 | 253 | ATTRIBUTE_PRINTF (3, 0); |
8b93c638 JM |
254 | static void uo_wrap_hint (struct ui_out *uiout, char *identstring); |
255 | static void uo_flush (struct ui_out *uiout); | |
0fac0b41 | 256 | static int uo_redirect (struct ui_out *uiout, struct ui_file *outstream); |
8b93c638 JM |
257 | |
258 | /* Prototypes for local functions */ | |
259 | ||
260 | extern void _initialize_ui_out (void); | |
88379baf | 261 | static void append_header_to_list (struct ui_out *uiout, int width, |
b25959ec AC |
262 | int alignment, const char *col_name, |
263 | const char *colhdr); | |
bafdd3b3 | 264 | static int get_next_header (struct ui_out *uiout, int *colno, int *width, |
8b93c638 JM |
265 | int *alignment, char **colhdr); |
266 | static void clear_header_list (struct ui_out *uiout); | |
a6c47c14 AC |
267 | static void verify_field (struct ui_out *uiout, int *fldno, int *width, |
268 | int *align); | |
8b93c638 | 269 | |
8b93c638 JM |
270 | /* exported functions (ui_out API) */ |
271 | ||
581e13c1 | 272 | /* Mark beginning of a table. */ |
8b93c638 | 273 | |
3b31d625 | 274 | static void |
88379baf | 275 | ui_out_table_begin (struct ui_out *uiout, int nbrofcols, |
d63f1d40 | 276 | int nr_rows, |
88379baf | 277 | const char *tblid) |
8b93c638 | 278 | { |
bafdd3b3 | 279 | if (uiout->table.flag) |
8e65ff28 | 280 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 AC |
281 | _("tables cannot be nested; table_begin found before \ |
282 | previous table_end.")); | |
8b93c638 | 283 | |
bafdd3b3 AC |
284 | uiout->table.flag = 1; |
285 | uiout->table.body_flag = 0; | |
a6c47c14 | 286 | uiout->table.entry_level = uiout->level + 1; |
bafdd3b3 | 287 | uiout->table.columns = nbrofcols; |
8b93c638 | 288 | if (tblid != NULL) |
bafdd3b3 | 289 | uiout->table.id = xstrdup (tblid); |
8b93c638 | 290 | else |
bafdd3b3 | 291 | uiout->table.id = NULL; |
8b93c638 JM |
292 | clear_header_list (uiout); |
293 | ||
bafdd3b3 | 294 | uo_table_begin (uiout, nbrofcols, nr_rows, uiout->table.id); |
8b93c638 JM |
295 | } |
296 | ||
297 | void | |
fba45db2 | 298 | ui_out_table_body (struct ui_out *uiout) |
8b93c638 | 299 | { |
bafdd3b3 | 300 | if (!uiout->table.flag) |
8e65ff28 | 301 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 AC |
302 | _("table_body outside a table is not valid; it must be \ |
303 | after a table_begin and before a table_end.")); | |
bafdd3b3 | 304 | if (uiout->table.body_flag) |
8e65ff28 | 305 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 AC |
306 | _("extra table_body call not allowed; there must be \ |
307 | only one table_body after a table_begin and before a table_end.")); | |
bafdd3b3 | 308 | if (uiout->table.header_next->colno != uiout->table.columns) |
8e65ff28 | 309 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 AC |
310 | _("number of headers differ from number of table \ |
311 | columns.")); | |
8b93c638 | 312 | |
bafdd3b3 AC |
313 | uiout->table.body_flag = 1; |
314 | uiout->table.header_next = uiout->table.header_first; | |
8b93c638 JM |
315 | |
316 | uo_table_body (uiout); | |
317 | } | |
318 | ||
3b31d625 | 319 | static void |
fba45db2 | 320 | ui_out_table_end (struct ui_out *uiout) |
8b93c638 | 321 | { |
bafdd3b3 | 322 | if (!uiout->table.flag) |
8e65ff28 | 323 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 324 | _("misplaced table_end or missing table_begin.")); |
8b93c638 | 325 | |
a6c47c14 | 326 | uiout->table.entry_level = 0; |
bafdd3b3 AC |
327 | uiout->table.body_flag = 0; |
328 | uiout->table.flag = 0; | |
8b93c638 JM |
329 | |
330 | uo_table_end (uiout); | |
331 | ||
bafdd3b3 AC |
332 | if (uiout->table.id) |
333 | xfree (uiout->table.id); | |
8b93c638 JM |
334 | clear_header_list (uiout); |
335 | } | |
336 | ||
337 | void | |
fba45db2 | 338 | ui_out_table_header (struct ui_out *uiout, int width, enum ui_align alignment, |
b25959ec | 339 | const char *col_name, |
88379baf | 340 | const char *colhdr) |
8b93c638 | 341 | { |
bafdd3b3 | 342 | if (!uiout->table.flag || uiout->table.body_flag) |
8e65ff28 | 343 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 AC |
344 | _("table header must be specified after table_begin \ |
345 | and before table_body.")); | |
8b93c638 | 346 | |
b25959ec | 347 | append_header_to_list (uiout, width, alignment, col_name, colhdr); |
8b93c638 | 348 | |
b25959ec | 349 | uo_table_header (uiout, width, alignment, col_name, colhdr); |
8b93c638 JM |
350 | } |
351 | ||
3b31d625 EZ |
352 | static void |
353 | do_cleanup_table_end (void *data) | |
354 | { | |
355 | struct ui_out *ui_out = data; | |
356 | ||
357 | ui_out_table_end (ui_out); | |
358 | } | |
359 | ||
360 | struct cleanup * | |
361 | make_cleanup_ui_out_table_begin_end (struct ui_out *ui_out, int nr_cols, | |
362 | int nr_rows, const char *tblid) | |
363 | { | |
364 | ui_out_table_begin (ui_out, nr_cols, nr_rows, tblid); | |
365 | return make_cleanup (do_cleanup_table_end, ui_out); | |
366 | } | |
367 | ||
8b93c638 | 368 | void |
631ec795 AC |
369 | ui_out_begin (struct ui_out *uiout, |
370 | enum ui_out_type type, | |
371 | const char *id) | |
8b93c638 | 372 | { |
80f49b30 | 373 | int new_level; |
5d502164 | 374 | |
bafdd3b3 | 375 | if (uiout->table.flag && !uiout->table.body_flag) |
8e65ff28 | 376 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 AC |
377 | _("table header or table_body expected; lists must be \ |
378 | specified after table_body.")); | |
a6c47c14 AC |
379 | |
380 | /* Be careful to verify the ``field'' before the new tuple/list is | |
381 | pushed onto the stack. That way the containing list/table/row is | |
382 | verified and not the newly created tuple/list. This verification | |
383 | is needed (at least) for the case where a table row entry | |
384 | contains either a tuple/list. For that case bookkeeping such as | |
385 | updating the column count or advancing to the next heading still | |
386 | needs to be performed. */ | |
387 | { | |
388 | int fldno; | |
389 | int width; | |
390 | int align; | |
5d502164 | 391 | |
a6c47c14 AC |
392 | verify_field (uiout, &fldno, &width, &align); |
393 | } | |
394 | ||
631ec795 | 395 | new_level = push_level (uiout, type, id); |
a6c47c14 AC |
396 | |
397 | /* If the push puts us at the same level as a table row entry, we've | |
398 | got a new table row. Put the header pointer back to the start. */ | |
399 | if (uiout->table.body_flag | |
400 | && uiout->table.entry_level == new_level) | |
bafdd3b3 | 401 | uiout->table.header_next = uiout->table.header_first; |
a6c47c14 | 402 | |
631ec795 AC |
403 | uo_begin (uiout, type, new_level, id); |
404 | } | |
405 | ||
631ec795 AC |
406 | void |
407 | ui_out_end (struct ui_out *uiout, | |
408 | enum ui_out_type type) | |
409 | { | |
410 | int old_level = pop_level (uiout, type); | |
5d502164 | 411 | |
631ec795 | 412 | uo_end (uiout, type, old_level); |
8b93c638 JM |
413 | } |
414 | ||
127431f9 AC |
415 | struct ui_out_end_cleanup_data |
416 | { | |
417 | struct ui_out *uiout; | |
418 | enum ui_out_type type; | |
419 | }; | |
420 | ||
e6e0bfab | 421 | static void |
127431f9 AC |
422 | do_cleanup_end (void *data) |
423 | { | |
424 | struct ui_out_end_cleanup_data *end_cleanup_data = data; | |
5d502164 | 425 | |
127431f9 AC |
426 | ui_out_end (end_cleanup_data->uiout, end_cleanup_data->type); |
427 | xfree (end_cleanup_data); | |
428 | } | |
429 | ||
430 | static struct cleanup * | |
431 | make_cleanup_ui_out_end (struct ui_out *uiout, | |
432 | enum ui_out_type type) | |
433 | { | |
434 | struct ui_out_end_cleanup_data *end_cleanup_data; | |
5d502164 | 435 | |
127431f9 AC |
436 | end_cleanup_data = XMALLOC (struct ui_out_end_cleanup_data); |
437 | end_cleanup_data->uiout = uiout; | |
438 | end_cleanup_data->type = type; | |
439 | return make_cleanup (do_cleanup_end, end_cleanup_data); | |
440 | } | |
441 | ||
e6e0bfab | 442 | struct cleanup * |
666547aa AC |
443 | make_cleanup_ui_out_tuple_begin_end (struct ui_out *uiout, |
444 | const char *id) | |
445 | { | |
3b31d625 | 446 | ui_out_begin (uiout, ui_out_type_tuple, id); |
666547aa AC |
447 | return make_cleanup_ui_out_end (uiout, ui_out_type_tuple); |
448 | } | |
449 | ||
450 | struct cleanup * | |
6b28c186 AC |
451 | make_cleanup_ui_out_list_begin_end (struct ui_out *uiout, |
452 | const char *id) | |
e6e0bfab | 453 | { |
3b31d625 | 454 | ui_out_begin (uiout, ui_out_type_list, id); |
127431f9 | 455 | return make_cleanup_ui_out_end (uiout, ui_out_type_list); |
e6e0bfab MK |
456 | } |
457 | ||
8b93c638 | 458 | void |
88379baf AC |
459 | ui_out_field_int (struct ui_out *uiout, |
460 | const char *fldname, | |
461 | int value) | |
8b93c638 JM |
462 | { |
463 | int fldno; | |
464 | int width; | |
465 | int align; | |
466 | ||
a6c47c14 | 467 | verify_field (uiout, &fldno, &width, &align); |
8b93c638 JM |
468 | |
469 | uo_field_int (uiout, fldno, width, align, fldname, value); | |
470 | } | |
471 | ||
52c6a6ac JJ |
472 | void |
473 | ui_out_field_fmt_int (struct ui_out *uiout, | |
474 | int input_width, | |
475 | enum ui_align input_align, | |
476 | const char *fldname, | |
477 | int value) | |
478 | { | |
479 | int fldno; | |
480 | int width; | |
481 | int align; | |
52c6a6ac JJ |
482 | |
483 | verify_field (uiout, &fldno, &width, &align); | |
484 | ||
485 | uo_field_int (uiout, fldno, input_width, input_align, fldname, value); | |
486 | } | |
487 | ||
15230f37 TJB |
488 | /* Documented in ui-out.h. */ |
489 | ||
8b93c638 | 490 | void |
88379baf AC |
491 | ui_out_field_core_addr (struct ui_out *uiout, |
492 | const char *fldname, | |
5af949e3 | 493 | struct gdbarch *gdbarch, |
88379baf | 494 | CORE_ADDR address) |
8b93c638 | 495 | { |
f1310107 TJB |
496 | ui_out_field_string (uiout, fldname, |
497 | print_core_address (gdbarch, address)); | |
8b93c638 JM |
498 | } |
499 | ||
500 | void | |
88379baf AC |
501 | ui_out_field_stream (struct ui_out *uiout, |
502 | const char *fldname, | |
f99d8bf4 | 503 | struct ui_file *stream) |
8b93c638 JM |
504 | { |
505 | long length; | |
f99d8bf4 | 506 | char *buffer = ui_file_xstrdup (stream, &length); |
b8c9b27d | 507 | struct cleanup *old_cleanup = make_cleanup (xfree, buffer); |
5d502164 | 508 | |
8b93c638 JM |
509 | if (length > 0) |
510 | ui_out_field_string (uiout, fldname, buffer); | |
511 | else | |
512 | ui_out_field_skip (uiout, fldname); | |
f99d8bf4 | 513 | ui_file_rewind (stream); |
8b93c638 JM |
514 | do_cleanups (old_cleanup); |
515 | } | |
516 | ||
581e13c1 | 517 | /* Used to omit a field. */ |
8b93c638 JM |
518 | |
519 | void | |
88379baf AC |
520 | ui_out_field_skip (struct ui_out *uiout, |
521 | const char *fldname) | |
8b93c638 JM |
522 | { |
523 | int fldno; | |
524 | int width; | |
525 | int align; | |
8b93c638 | 526 | |
a6c47c14 | 527 | verify_field (uiout, &fldno, &width, &align); |
8b93c638 JM |
528 | |
529 | uo_field_skip (uiout, fldno, width, align, fldname); | |
530 | } | |
531 | ||
532 | void | |
533 | ui_out_field_string (struct ui_out *uiout, | |
88379baf | 534 | const char *fldname, |
8b93c638 JM |
535 | const char *string) |
536 | { | |
537 | int fldno; | |
538 | int width; | |
539 | int align; | |
8b93c638 | 540 | |
a6c47c14 | 541 | verify_field (uiout, &fldno, &width, &align); |
8b93c638 JM |
542 | |
543 | uo_field_string (uiout, fldno, width, align, fldname, string); | |
544 | } | |
545 | ||
546 | /* VARARGS */ | |
547 | void | |
88379baf AC |
548 | ui_out_field_fmt (struct ui_out *uiout, |
549 | const char *fldname, | |
550 | const char *format, ...) | |
8b93c638 JM |
551 | { |
552 | va_list args; | |
553 | int fldno; | |
554 | int width; | |
555 | int align; | |
8b93c638 | 556 | |
581e13c1 | 557 | /* Will not align, but has to call anyway. */ |
a6c47c14 | 558 | verify_field (uiout, &fldno, &width, &align); |
8b93c638 JM |
559 | |
560 | va_start (args, format); | |
561 | ||
562 | uo_field_fmt (uiout, fldno, width, align, fldname, format, args); | |
563 | ||
564 | va_end (args); | |
565 | } | |
566 | ||
567 | void | |
fba45db2 | 568 | ui_out_spaces (struct ui_out *uiout, int numspaces) |
8b93c638 JM |
569 | { |
570 | uo_spaces (uiout, numspaces); | |
571 | } | |
572 | ||
573 | void | |
88379baf AC |
574 | ui_out_text (struct ui_out *uiout, |
575 | const char *string) | |
8b93c638 JM |
576 | { |
577 | uo_text (uiout, string); | |
578 | } | |
579 | ||
580 | void | |
88379baf AC |
581 | ui_out_message (struct ui_out *uiout, int verbosity, |
582 | const char *format,...) | |
8b93c638 JM |
583 | { |
584 | va_list args; | |
585 | ||
586 | va_start (args, format); | |
8b93c638 | 587 | uo_message (uiout, verbosity, format, args); |
8b93c638 JM |
588 | va_end (args); |
589 | } | |
590 | ||
8b93c638 | 591 | void |
fba45db2 | 592 | ui_out_wrap_hint (struct ui_out *uiout, char *identstring) |
8b93c638 JM |
593 | { |
594 | uo_wrap_hint (uiout, identstring); | |
595 | } | |
596 | ||
597 | void | |
fba45db2 | 598 | ui_out_flush (struct ui_out *uiout) |
8b93c638 JM |
599 | { |
600 | uo_flush (uiout); | |
601 | } | |
602 | ||
0fac0b41 DJ |
603 | int |
604 | ui_out_redirect (struct ui_out *uiout, struct ui_file *outstream) | |
605 | { | |
606 | return uo_redirect (uiout, outstream); | |
607 | } | |
608 | ||
581e13c1 | 609 | /* Set the flags specified by the mask given. */ |
8b93c638 | 610 | int |
fba45db2 | 611 | ui_out_set_flags (struct ui_out *uiout, int mask) |
8b93c638 | 612 | { |
5bfb05ca | 613 | int oldflags = uiout->flags; |
8b93c638 | 614 | |
b8d86de3 | 615 | uiout->flags |= mask; |
8b93c638 JM |
616 | return oldflags; |
617 | } | |
618 | ||
581e13c1 | 619 | /* Clear the flags specified by the mask given. */ |
8b93c638 | 620 | int |
fba45db2 | 621 | ui_out_clear_flags (struct ui_out *uiout, int mask) |
8b93c638 | 622 | { |
5bfb05ca | 623 | int oldflags = uiout->flags; |
8b93c638 JM |
624 | |
625 | uiout->flags &= ~mask; | |
8b93c638 JM |
626 | return oldflags; |
627 | } | |
628 | ||
581e13c1 | 629 | /* Test the flags against the mask given. */ |
8b93c638 | 630 | int |
fba45db2 | 631 | ui_out_test_flags (struct ui_out *uiout, int mask) |
8b93c638 JM |
632 | { |
633 | return (uiout->flags & mask); | |
634 | } | |
635 | ||
581e13c1 MS |
636 | /* Obtain the current verbosity level (as stablished by the |
637 | 'set verbositylevel' command. */ | |
8b93c638 JM |
638 | |
639 | int | |
fba45db2 | 640 | ui_out_get_verblvl (struct ui_out *uiout) |
8b93c638 | 641 | { |
581e13c1 | 642 | /* FIXME: not implemented yet. */ |
8b93c638 JM |
643 | return 0; |
644 | } | |
645 | ||
9dc5e2a9 AC |
646 | int |
647 | ui_out_is_mi_like_p (struct ui_out *uiout) | |
648 | { | |
649 | return uiout->impl->is_mi_like_p; | |
650 | } | |
651 | ||
581e13c1 | 652 | /* Default gdb-out hook functions. */ |
8b93c638 JM |
653 | |
654 | static void | |
d63f1d40 AC |
655 | default_table_begin (struct ui_out *uiout, int nbrofcols, |
656 | int nr_rows, | |
657 | const char *tblid) | |
8b93c638 JM |
658 | { |
659 | } | |
660 | ||
661 | static void | |
fba45db2 | 662 | default_table_body (struct ui_out *uiout) |
8b93c638 JM |
663 | { |
664 | } | |
665 | ||
666 | static void | |
fba45db2 | 667 | default_table_end (struct ui_out *uiout) |
8b93c638 JM |
668 | { |
669 | } | |
670 | ||
671 | static void | |
fba45db2 | 672 | default_table_header (struct ui_out *uiout, int width, enum ui_align alignment, |
b25959ec | 673 | const char *col_name, |
e2e11a41 | 674 | const char *colhdr) |
8b93c638 JM |
675 | { |
676 | } | |
677 | ||
678 | static void | |
631ec795 AC |
679 | default_begin (struct ui_out *uiout, |
680 | enum ui_out_type type, | |
681 | int level, | |
682 | const char *id) | |
8b93c638 JM |
683 | { |
684 | } | |
685 | ||
686 | static void | |
631ec795 AC |
687 | default_end (struct ui_out *uiout, |
688 | enum ui_out_type type, | |
689 | int level) | |
8b93c638 JM |
690 | { |
691 | } | |
692 | ||
693 | static void | |
fba45db2 | 694 | default_field_int (struct ui_out *uiout, int fldno, int width, |
e2e11a41 AC |
695 | enum ui_align align, |
696 | const char *fldname, int value) | |
8b93c638 JM |
697 | { |
698 | } | |
699 | ||
700 | static void | |
fba45db2 | 701 | default_field_skip (struct ui_out *uiout, int fldno, int width, |
e2e11a41 | 702 | enum ui_align align, const char *fldname) |
8b93c638 JM |
703 | { |
704 | } | |
705 | ||
706 | static void | |
707 | default_field_string (struct ui_out *uiout, | |
708 | int fldno, | |
709 | int width, | |
710 | enum ui_align align, | |
e2e11a41 | 711 | const char *fldname, |
8b93c638 JM |
712 | const char *string) |
713 | { | |
714 | } | |
715 | ||
716 | static void | |
fba45db2 | 717 | default_field_fmt (struct ui_out *uiout, int fldno, int width, |
e2e11a41 AC |
718 | enum ui_align align, |
719 | const char *fldname, | |
720 | const char *format, | |
fba45db2 | 721 | va_list args) |
8b93c638 JM |
722 | { |
723 | } | |
724 | ||
725 | static void | |
fba45db2 | 726 | default_spaces (struct ui_out *uiout, int numspaces) |
8b93c638 JM |
727 | { |
728 | } | |
729 | ||
730 | static void | |
e2e11a41 | 731 | default_text (struct ui_out *uiout, const char *string) |
8b93c638 JM |
732 | { |
733 | } | |
734 | ||
735 | static void | |
e2e11a41 AC |
736 | default_message (struct ui_out *uiout, int verbosity, |
737 | const char *format, | |
fba45db2 | 738 | va_list args) |
8b93c638 JM |
739 | { |
740 | } | |
741 | ||
742 | static void | |
fba45db2 | 743 | default_wrap_hint (struct ui_out *uiout, char *identstring) |
8b93c638 JM |
744 | { |
745 | } | |
746 | ||
747 | static void | |
fba45db2 | 748 | default_flush (struct ui_out *uiout) |
8b93c638 JM |
749 | { |
750 | } | |
751 | ||
581e13c1 | 752 | /* Interface to the implementation functions. */ |
8b93c638 JM |
753 | |
754 | void | |
88379baf | 755 | uo_table_begin (struct ui_out *uiout, int nbrofcols, |
d63f1d40 | 756 | int nr_rows, |
88379baf | 757 | const char *tblid) |
8b93c638 JM |
758 | { |
759 | if (!uiout->impl->table_begin) | |
760 | return; | |
d63f1d40 | 761 | uiout->impl->table_begin (uiout, nbrofcols, nr_rows, tblid); |
8b93c638 JM |
762 | } |
763 | ||
764 | void | |
765 | uo_table_body (struct ui_out *uiout) | |
766 | { | |
767 | if (!uiout->impl->table_body) | |
768 | return; | |
769 | uiout->impl->table_body (uiout); | |
770 | } | |
771 | ||
772 | void | |
773 | uo_table_end (struct ui_out *uiout) | |
774 | { | |
775 | if (!uiout->impl->table_end) | |
776 | return; | |
777 | uiout->impl->table_end (uiout); | |
778 | } | |
779 | ||
780 | void | |
88379baf | 781 | uo_table_header (struct ui_out *uiout, int width, enum ui_align align, |
b25959ec | 782 | const char *col_name, |
88379baf | 783 | const char *colhdr) |
8b93c638 JM |
784 | { |
785 | if (!uiout->impl->table_header) | |
786 | return; | |
b25959ec | 787 | uiout->impl->table_header (uiout, width, align, col_name, colhdr); |
8b93c638 JM |
788 | } |
789 | ||
790 | void | |
631ec795 AC |
791 | uo_begin (struct ui_out *uiout, |
792 | enum ui_out_type type, | |
793 | int level, | |
794 | const char *id) | |
8b93c638 | 795 | { |
631ec795 | 796 | if (uiout->impl->begin == NULL) |
8b93c638 | 797 | return; |
631ec795 | 798 | uiout->impl->begin (uiout, type, level, id); |
8b93c638 JM |
799 | } |
800 | ||
801 | void | |
631ec795 AC |
802 | uo_end (struct ui_out *uiout, |
803 | enum ui_out_type type, | |
804 | int level) | |
8b93c638 | 805 | { |
631ec795 | 806 | if (uiout->impl->end == NULL) |
8b93c638 | 807 | return; |
631ec795 | 808 | uiout->impl->end (uiout, type, level); |
8b93c638 JM |
809 | } |
810 | ||
811 | void | |
88379baf AC |
812 | uo_field_int (struct ui_out *uiout, int fldno, int width, enum ui_align align, |
813 | const char *fldname, | |
814 | int value) | |
8b93c638 JM |
815 | { |
816 | if (!uiout->impl->field_int) | |
817 | return; | |
818 | uiout->impl->field_int (uiout, fldno, width, align, fldname, value); | |
819 | } | |
820 | ||
821 | void | |
88379baf AC |
822 | uo_field_skip (struct ui_out *uiout, int fldno, int width, enum ui_align align, |
823 | const char *fldname) | |
8b93c638 JM |
824 | { |
825 | if (!uiout->impl->field_skip) | |
826 | return; | |
827 | uiout->impl->field_skip (uiout, fldno, width, align, fldname); | |
828 | } | |
829 | ||
830 | void | |
831 | uo_field_string (struct ui_out *uiout, int fldno, int width, | |
88379baf AC |
832 | enum ui_align align, |
833 | const char *fldname, | |
834 | const char *string) | |
8b93c638 JM |
835 | { |
836 | if (!uiout->impl->field_string) | |
837 | return; | |
838 | uiout->impl->field_string (uiout, fldno, width, align, fldname, string); | |
839 | } | |
840 | ||
841 | void | |
88379baf AC |
842 | uo_field_fmt (struct ui_out *uiout, int fldno, int width, enum ui_align align, |
843 | const char *fldname, | |
844 | const char *format, | |
845 | va_list args) | |
8b93c638 JM |
846 | { |
847 | if (!uiout->impl->field_fmt) | |
848 | return; | |
849 | uiout->impl->field_fmt (uiout, fldno, width, align, fldname, format, args); | |
850 | } | |
851 | ||
852 | void | |
853 | uo_spaces (struct ui_out *uiout, int numspaces) | |
854 | { | |
855 | if (!uiout->impl->spaces) | |
856 | return; | |
857 | uiout->impl->spaces (uiout, numspaces); | |
858 | } | |
859 | ||
860 | void | |
88379baf AC |
861 | uo_text (struct ui_out *uiout, |
862 | const char *string) | |
8b93c638 JM |
863 | { |
864 | if (!uiout->impl->text) | |
865 | return; | |
866 | uiout->impl->text (uiout, string); | |
867 | } | |
868 | ||
869 | void | |
88379baf AC |
870 | uo_message (struct ui_out *uiout, int verbosity, |
871 | const char *format, | |
872 | va_list args) | |
8b93c638 JM |
873 | { |
874 | if (!uiout->impl->message) | |
875 | return; | |
876 | uiout->impl->message (uiout, verbosity, format, args); | |
877 | } | |
878 | ||
879 | void | |
880 | uo_wrap_hint (struct ui_out *uiout, char *identstring) | |
881 | { | |
882 | if (!uiout->impl->wrap_hint) | |
883 | return; | |
884 | uiout->impl->wrap_hint (uiout, identstring); | |
885 | } | |
886 | ||
887 | void | |
888 | uo_flush (struct ui_out *uiout) | |
889 | { | |
890 | if (!uiout->impl->flush) | |
891 | return; | |
892 | uiout->impl->flush (uiout); | |
893 | } | |
894 | ||
0fac0b41 DJ |
895 | int |
896 | uo_redirect (struct ui_out *uiout, struct ui_file *outstream) | |
897 | { | |
898 | if (!uiout->impl->redirect) | |
899 | return -1; | |
900 | uiout->impl->redirect (uiout, outstream); | |
901 | return 0; | |
902 | } | |
903 | ||
8b93c638 JM |
904 | /* local functions */ |
905 | ||
581e13c1 | 906 | /* List of column headers manipulation routines. */ |
8b93c638 JM |
907 | |
908 | static void | |
fba45db2 | 909 | clear_header_list (struct ui_out *uiout) |
8b93c638 | 910 | { |
bafdd3b3 | 911 | while (uiout->table.header_first != NULL) |
8b93c638 | 912 | { |
bafdd3b3 AC |
913 | uiout->table.header_next = uiout->table.header_first; |
914 | uiout->table.header_first = uiout->table.header_first->next; | |
71bdabee KS |
915 | xfree (uiout->table.header_next->colhdr); |
916 | xfree (uiout->table.header_next->col_name); | |
bafdd3b3 | 917 | xfree (uiout->table.header_next); |
8b93c638 | 918 | } |
bafdd3b3 AC |
919 | gdb_assert (uiout->table.header_first == NULL); |
920 | uiout->table.header_last = NULL; | |
921 | uiout->table.header_next = NULL; | |
8b93c638 JM |
922 | } |
923 | ||
924 | static void | |
925 | append_header_to_list (struct ui_out *uiout, | |
926 | int width, | |
927 | int alignment, | |
b25959ec | 928 | const char *col_name, |
88379baf | 929 | const char *colhdr) |
8b93c638 JM |
930 | { |
931 | struct ui_out_hdr *temphdr; | |
932 | ||
933 | temphdr = XMALLOC (struct ui_out_hdr); | |
934 | temphdr->width = width; | |
935 | temphdr->alignment = alignment; | |
44db85f8 MS |
936 | /* We have to copy the column title as the original may be an |
937 | automatic. */ | |
8b93c638 | 938 | if (colhdr != NULL) |
b25959ec AC |
939 | temphdr->colhdr = xstrdup (colhdr); |
940 | else | |
941 | temphdr->colhdr = NULL; | |
44db85f8 | 942 | |
b25959ec | 943 | if (col_name != NULL) |
44db85f8 MS |
944 | temphdr->col_name = xstrdup (col_name); |
945 | else if (colhdr != NULL) | |
b25959ec AC |
946 | temphdr->col_name = xstrdup (colhdr); |
947 | else | |
44db85f8 MS |
948 | temphdr->col_name = NULL; |
949 | ||
8b93c638 | 950 | temphdr->next = NULL; |
bafdd3b3 | 951 | if (uiout->table.header_first == NULL) |
8b93c638 JM |
952 | { |
953 | temphdr->colno = 1; | |
bafdd3b3 AC |
954 | uiout->table.header_first = temphdr; |
955 | uiout->table.header_last = temphdr; | |
8b93c638 JM |
956 | } |
957 | else | |
958 | { | |
bafdd3b3 AC |
959 | temphdr->colno = uiout->table.header_last->colno + 1; |
960 | uiout->table.header_last->next = temphdr; | |
961 | uiout->table.header_last = temphdr; | |
8b93c638 | 962 | } |
bafdd3b3 | 963 | uiout->table.header_next = uiout->table.header_last; |
8b93c638 JM |
964 | } |
965 | ||
7a9dd1b2 | 966 | /* Extract the format information for the NEXT header and advance |
bafdd3b3 | 967 | the header pointer. Return 0 if there was no next header. */ |
8b93c638 JM |
968 | |
969 | static int | |
bafdd3b3 | 970 | get_next_header (struct ui_out *uiout, |
8b93c638 JM |
971 | int *colno, |
972 | int *width, | |
973 | int *alignment, | |
974 | char **colhdr) | |
975 | { | |
bafdd3b3 AC |
976 | /* There may be no headers at all or we may have used all columns. */ |
977 | if (uiout->table.header_next == NULL) | |
8b93c638 | 978 | return 0; |
bafdd3b3 AC |
979 | *colno = uiout->table.header_next->colno; |
980 | *width = uiout->table.header_next->width; | |
981 | *alignment = uiout->table.header_next->alignment; | |
982 | *colhdr = uiout->table.header_next->colhdr; | |
983 | /* Advance the header pointer to the next entry. */ | |
984 | uiout->table.header_next = uiout->table.header_next->next; | |
8b93c638 JM |
985 | return 1; |
986 | } | |
987 | ||
a6c47c14 AC |
988 | |
989 | /* Verify that the field/tuple/list is correctly positioned. Return | |
990 | the field number and corresponding alignment (if | |
991 | available/applicable). */ | |
8b93c638 JM |
992 | |
993 | static void | |
a6c47c14 | 994 | verify_field (struct ui_out *uiout, int *fldno, int *width, int *align) |
8b93c638 | 995 | { |
a6c47c14 AC |
996 | struct ui_out_level *current = current_level (uiout); |
997 | char *text; | |
998 | ||
bafdd3b3 | 999 | if (uiout->table.flag) |
8b93c638 | 1000 | { |
bafdd3b3 | 1001 | if (!uiout->table.body_flag) |
8e65ff28 | 1002 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 AC |
1003 | _("table_body missing; table fields must be \ |
1004 | specified after table_body and inside a list.")); | |
a6c47c14 AC |
1005 | /* NOTE: cagney/2001-12-08: There was a check here to ensure |
1006 | that this code was only executed when uiout->level was | |
1007 | greater than zero. That no longer applies - this code is run | |
1008 | before each table row tuple is started and at that point the | |
1009 | level is zero. */ | |
8b93c638 | 1010 | } |
8b93c638 | 1011 | |
a6c47c14 | 1012 | current->field_count += 1; |
8b93c638 | 1013 | |
a6c47c14 AC |
1014 | if (uiout->table.body_flag |
1015 | && uiout->table.entry_level == uiout->level | |
1016 | && get_next_header (uiout, fldno, width, align, &text)) | |
8b93c638 | 1017 | { |
a6c47c14 | 1018 | if (*fldno != current->field_count) |
8e65ff28 | 1019 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1020 | _("ui-out internal error in handling headers.")); |
8b93c638 JM |
1021 | } |
1022 | else | |
1023 | { | |
1024 | *width = 0; | |
1025 | *align = ui_noalign; | |
a6c47c14 | 1026 | *fldno = current->field_count; |
8b93c638 JM |
1027 | } |
1028 | } | |
1029 | ||
a6c47c14 | 1030 | |
581e13c1 | 1031 | /* Access to ui-out members data. */ |
8b93c638 | 1032 | |
0a8fce9a | 1033 | void * |
8b93c638 JM |
1034 | ui_out_data (struct ui_out *uiout) |
1035 | { | |
1036 | return uiout->data; | |
1037 | } | |
1038 | ||
170b53b2 UW |
1039 | /* Access table field parameters. */ |
1040 | int | |
1041 | ui_out_query_field (struct ui_out *uiout, int colno, | |
1042 | int *width, int *alignment, char **col_name) | |
1043 | { | |
1044 | struct ui_out_hdr *hdr; | |
1045 | ||
1046 | if (!uiout->table.flag) | |
1047 | return 0; | |
1048 | ||
1049 | for (hdr = uiout->table.header_first; hdr; hdr = hdr->next) | |
1050 | if (hdr->colno == colno) | |
1051 | { | |
1052 | *width = hdr->width; | |
1053 | *alignment = hdr->alignment; | |
1054 | *col_name = hdr->col_name; | |
1055 | return 1; | |
1056 | } | |
1057 | ||
1058 | return 0; | |
1059 | } | |
1060 | ||
581e13c1 | 1061 | /* Initalize private members at startup. */ |
8b93c638 JM |
1062 | |
1063 | struct ui_out * | |
0a8fce9a | 1064 | ui_out_new (struct ui_out_impl *impl, void *data, |
8b93c638 JM |
1065 | int flags) |
1066 | { | |
1067 | struct ui_out *uiout = XMALLOC (struct ui_out); | |
5d502164 | 1068 | |
8b93c638 JM |
1069 | uiout->data = data; |
1070 | uiout->impl = impl; | |
1071 | uiout->flags = flags; | |
bafdd3b3 AC |
1072 | uiout->table.flag = 0; |
1073 | uiout->table.body_flag = 0; | |
80f49b30 AC |
1074 | uiout->level = 0; |
1075 | memset (uiout->levels, 0, sizeof (uiout->levels)); | |
bafdd3b3 AC |
1076 | uiout->table.header_first = NULL; |
1077 | uiout->table.header_last = NULL; | |
1078 | uiout->table.header_next = NULL; | |
8b93c638 JM |
1079 | return uiout; |
1080 | } | |
1081 | ||
581e13c1 | 1082 | /* Standard gdb initialization hook. */ |
8b93c638 JM |
1083 | |
1084 | void | |
fba45db2 | 1085 | _initialize_ui_out (void) |
8b93c638 JM |
1086 | { |
1087 | /* nothing needs to be done */ | |
1088 | } |