Use std::string for ui_out_hdr's text fields
[deliverable/binutils-gdb.git] / gdb / mi / mi-out.c
CommitLineData
fb40c209 1/* MI Command Set - output generating routines.
349c5d5f 2
618f726f 3 Copyright (C) 2000-2016 Free Software Foundation, Inc.
349c5d5f 4
ab91fdd5 5 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
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
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
21
22#include "defs.h"
23#include "ui-out.h"
24#include "mi-out.h"
4a9d4ea5 25#include <vector>
fb40c209 26
0abe66b5 27struct mi_ui_out_data
fb40c209 28 {
59807497 29 int suppress_field_separator;
76fe6b98 30 int suppress_output;
b30bf9ee 31 int mi_version;
4a9d4ea5 32 std::vector<ui_file *> streams;
fb40c209 33 };
0abe66b5 34typedef struct mi_ui_out_data mi_out_data;
fb40c209
AC
35
36/* These are the MI output functions */
37
4d6cceb4 38static void mi_out_data_dtor (struct ui_out *ui_out);
e2e11a41 39static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 40 int nr_rows, const char *tblid);
fb40c209
AC
41static void mi_table_body (struct ui_out *uiout);
42static void mi_table_end (struct ui_out *uiout);
43static void mi_table_header (struct ui_out *uiout, int width,
c5209615
SM
44 enum ui_align alignment,
45 const std::string &col_name,
46 const std::string &col_hdr);
631ec795
AC
47static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
48 int level, const char *id);
49static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
fb40c209 50static void mi_field_int (struct ui_out *uiout, int fldno, int width,
e2e11a41 51 enum ui_align alig, const char *fldname, int value);
fb40c209 52static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
e2e11a41 53 enum ui_align alig, const char *fldname);
fb40c209 54static void mi_field_string (struct ui_out *uiout, int fldno, int width,
e2e11a41 55 enum ui_align alig, const char *fldname,
fb40c209
AC
56 const char *string);
57static void mi_field_fmt (struct ui_out *uiout, int fldno,
58 int width, enum ui_align align,
e2e11a41 59 const char *fldname, const char *format,
a0b31db1 60 va_list args) ATTRIBUTE_PRINTF (6, 0);
fb40c209 61static void mi_spaces (struct ui_out *uiout, int numspaces);
e2e11a41 62static void mi_text (struct ui_out *uiout, const char *string);
7fb048a2
SM
63static void mi_message (struct ui_out *uiout, const char *format, va_list args)
64 ATTRIBUTE_PRINTF (2, 0);
d2c0eef4 65static void mi_wrap_hint (struct ui_out *uiout, const char *identstring);
fb40c209 66static void mi_flush (struct ui_out *uiout);
8d3788bd 67static int mi_redirect (struct ui_out *uiout, struct ui_file *outstream);
fb40c209
AC
68
69/* This is the MI ui-out implementation functions vector */
70
89de4da4 71static const struct ui_out_impl mi_ui_out_impl =
fb40c209
AC
72{
73 mi_table_begin,
74 mi_table_body,
75 mi_table_end,
76 mi_table_header,
631ec795
AC
77 mi_begin,
78 mi_end,
fb40c209
AC
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,
9dc5e2a9 87 mi_flush,
8d3788bd 88 mi_redirect,
4d6cceb4 89 mi_out_data_dtor,
9dc5e2a9 90 1, /* Needs MI hacks. */
fb40c209
AC
91};
92
93/* Prototypes for local functions */
94
fb40c209 95static void field_separator (struct ui_out *uiout);
d5e8ba62
AC
96static void mi_open (struct ui_out *uiout, const char *name,
97 enum ui_out_type type);
9a0f0643 98static void mi_close (struct ui_out *uiout, enum ui_out_type type);
fb40c209 99
2b03b41d 100/* Mark beginning of a table. */
fb40c209
AC
101
102void
cff22675
AC
103mi_table_begin (struct ui_out *uiout,
104 int nr_cols,
d63f1d40 105 int nr_rows,
e2e11a41 106 const char *tblid)
fb40c209 107{
d5e8ba62 108 mi_open (uiout, tblid, ui_out_type_tuple);
f486487f
SM
109 mi_field_int (uiout, -1, -1, ui_left, "nr_rows", nr_rows);
110 mi_field_int (uiout, -1, -1, ui_left, "nr_cols", nr_cols);
cff22675 111 mi_open (uiout, "hdr", ui_out_type_list);
fb40c209
AC
112}
113
2b03b41d 114/* Mark beginning of a table body. */
fb40c209
AC
115
116void
fba45db2 117mi_table_body (struct ui_out *uiout)
fb40c209 118{
19ba03f4 119 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
102040f0 120
76fe6b98
AC
121 if (data->suppress_output)
122 return;
cff22675
AC
123 /* close the table header line if there were any headers */
124 mi_close (uiout, ui_out_type_list);
cff22675 125 mi_open (uiout, "body", ui_out_type_list);
fb40c209
AC
126}
127
2b03b41d 128/* Mark end of a table. */
fb40c209
AC
129
130void
fba45db2 131mi_table_end (struct ui_out *uiout)
fb40c209 132{
19ba03f4 133 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
102040f0 134
76fe6b98 135 data->suppress_output = 0;
cff22675 136 mi_close (uiout, ui_out_type_list); /* body */
666547aa 137 mi_close (uiout, ui_out_type_tuple);
fb40c209
AC
138}
139
2b03b41d 140/* Specify table header. */
fb40c209
AC
141
142void
46712191 143mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
c5209615 144 const std::string &col_name, const std::string &col_hdr)
fb40c209 145{
19ba03f4 146 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
102040f0 147
76fe6b98
AC
148 if (data->suppress_output)
149 return;
2b03b41d 150
cff22675 151 mi_open (uiout, NULL, ui_out_type_tuple);
f486487f
SM
152 mi_field_int (uiout, 0, 0, ui_center, "width", width);
153 mi_field_int (uiout, 0, 0, ui_center, "alignment", alignment);
c5209615
SM
154 mi_field_string (uiout, 0, 0, ui_center, "col_name", col_name.c_str ());
155 mi_field_string (uiout, 0, width, alignment, "colhdr", col_hdr.c_str ());
cff22675 156 mi_close (uiout, ui_out_type_tuple);
fb40c209
AC
157}
158
2b03b41d 159/* Mark beginning of a list. */
fb40c209
AC
160
161void
2b03b41d 162mi_begin (struct ui_out *uiout, enum ui_out_type type, int level,
9a0f0643 163 const char *id)
fb40c209 164{
19ba03f4 165 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
102040f0 166
76fe6b98
AC
167 if (data->suppress_output)
168 return;
2b03b41d 169
d5e8ba62 170 mi_open (uiout, id, type);
fb40c209
AC
171}
172
2b03b41d 173/* Mark end of a list. */
fb40c209
AC
174
175void
2b03b41d 176mi_end (struct ui_out *uiout, enum ui_out_type type, int level)
fb40c209 177{
19ba03f4 178 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
102040f0 179
76fe6b98
AC
180 if (data->suppress_output)
181 return;
2b03b41d 182
9a0f0643 183 mi_close (uiout, type);
fb40c209
AC
184}
185
2b03b41d 186/* Output an int field. */
fb40c209 187
2b03b41d 188static void
46712191
KB
189mi_field_int (struct ui_out *uiout, int fldno, int width,
190 enum ui_align alignment, const char *fldname, int value)
fb40c209 191{
102040f0 192 char buffer[20]; /* FIXME: how many chars long a %d can become? */
19ba03f4 193 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
102040f0 194
76fe6b98
AC
195 if (data->suppress_output)
196 return;
fb40c209 197
08850b56 198 xsnprintf (buffer, sizeof (buffer), "%d", value);
fb40c209
AC
199 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
200}
201
2b03b41d 202/* Used to omit a field. */
fb40c209
AC
203
204void
46712191
KB
205mi_field_skip (struct ui_out *uiout, int fldno, int width,
206 enum ui_align alignment, const char *fldname)
fb40c209 207{
fb40c209
AC
208}
209
2b03b41d
SS
210/* Other specific mi_field_* end up here so alignment and field
211 separators are both handled by mi_field_string. */
fb40c209
AC
212
213void
2b03b41d
SS
214mi_field_string (struct ui_out *uiout, int fldno, int width,
215 enum ui_align align, const char *fldname, const char *string)
fb40c209 216{
19ba03f4 217 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4d6cceb4 218 struct ui_file *stream;
102040f0 219
76fe6b98
AC
220 if (data->suppress_output)
221 return;
2b03b41d 222
4a9d4ea5 223 stream = data->streams.back ();
fb40c209
AC
224 field_separator (uiout);
225 if (fldname)
4d6cceb4
DE
226 fprintf_unfiltered (stream, "%s=", fldname);
227 fprintf_unfiltered (stream, "\"");
fb40c209 228 if (string)
4d6cceb4
DE
229 fputstr_unfiltered (string, '"', stream);
230 fprintf_unfiltered (stream, "\"");
fb40c209
AC
231}
232
2b03b41d 233/* This is the only field function that does not align. */
fb40c209
AC
234
235void
2b03b41d
SS
236mi_field_fmt (struct ui_out *uiout, int fldno, int width,
237 enum ui_align align, const char *fldname,
238 const char *format, va_list args)
fb40c209 239{
19ba03f4 240 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4d6cceb4 241 struct ui_file *stream;
102040f0 242
76fe6b98
AC
243 if (data->suppress_output)
244 return;
2b03b41d 245
4a9d4ea5 246 stream = data->streams.back ();
fb40c209
AC
247 field_separator (uiout);
248 if (fldname)
4d6cceb4 249 fprintf_unfiltered (stream, "%s=\"", fldname);
fb40c209 250 else
4d6cceb4
DE
251 fputs_unfiltered ("\"", stream);
252 vfprintf_unfiltered (stream, format, args);
253 fputs_unfiltered ("\"", stream);
fb40c209
AC
254}
255
256void
fba45db2 257mi_spaces (struct ui_out *uiout, int numspaces)
fb40c209
AC
258{
259}
260
261void
e2e11a41 262mi_text (struct ui_out *uiout, const char *string)
fb40c209
AC
263{
264}
265
266void
7fb048a2 267mi_message (struct ui_out *uiout, const char *format, va_list args)
fb40c209
AC
268{
269}
270
271void
d2c0eef4 272mi_wrap_hint (struct ui_out *uiout, const char *identstring)
fb40c209
AC
273{
274 wrap_here (identstring);
275}
276
277void
fba45db2 278mi_flush (struct ui_out *uiout)
fb40c209 279{
19ba03f4 280 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4a9d4ea5 281 struct ui_file *stream = data->streams.back ();
102040f0 282
4d6cceb4 283 gdb_flush (stream);
fb40c209
AC
284}
285
8d3788bd
VP
286int
287mi_redirect (struct ui_out *uiout, struct ui_file *outstream)
288{
19ba03f4 289 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
8d3788bd
VP
290
291 if (outstream != NULL)
4a9d4ea5 292 data->streams.push_back (outstream);
4d6cceb4 293 else
4a9d4ea5 294 data->streams.pop_back ();
8d3788bd
VP
295
296 return 0;
297}
298
fb40c209
AC
299/* local functions */
300
fb40c209
AC
301/* access to ui_out format private members */
302
303static void
304field_separator (struct ui_out *uiout)
305{
19ba03f4 306 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4a9d4ea5 307 ui_file *stream = data->streams.back ();
102040f0 308
59807497
AC
309 if (data->suppress_field_separator)
310 data->suppress_field_separator = 0;
fb40c209 311 else
4d6cceb4 312 fputc_unfiltered (',', stream);
fb40c209
AC
313}
314
315static void
2b03b41d 316mi_open (struct ui_out *uiout, const char *name, enum ui_out_type type)
fb40c209 317{
19ba03f4 318 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4a9d4ea5 319 ui_file *stream = data->streams.back ();
102040f0 320
d5e8ba62 321 field_separator (uiout);
59807497 322 data->suppress_field_separator = 1;
d5e8ba62 323 if (name)
4d6cceb4 324 fprintf_unfiltered (stream, "%s=", name);
5a9aa5dc
AC
325 switch (type)
326 {
327 case ui_out_type_tuple:
4d6cceb4 328 fputc_unfiltered ('{', stream);
5a9aa5dc
AC
329 break;
330 case ui_out_type_list:
4d6cceb4 331 fputc_unfiltered ('[', stream);
5a9aa5dc
AC
332 break;
333 default:
e2e0b3e5 334 internal_error (__FILE__, __LINE__, _("bad switch"));
5a9aa5dc 335 }
fb40c209
AC
336}
337
338static void
2b03b41d 339mi_close (struct ui_out *uiout, enum ui_out_type type)
fb40c209 340{
19ba03f4 341 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4a9d4ea5 342 ui_file *stream = data->streams.back ();
102040f0 343
5a9aa5dc
AC
344 switch (type)
345 {
346 case ui_out_type_tuple:
4d6cceb4 347 fputc_unfiltered ('}', stream);
5a9aa5dc
AC
348 break;
349 case ui_out_type_list:
4d6cceb4 350 fputc_unfiltered (']', stream);
5a9aa5dc
AC
351 break;
352 default:
e2e0b3e5 353 internal_error (__FILE__, __LINE__, _("bad switch"));
5a9aa5dc 354 }
59807497 355 data->suppress_field_separator = 0;
fb40c209
AC
356}
357
2b03b41d 358/* Clear the buffer. */
fb40c209
AC
359
360void
361mi_out_rewind (struct ui_out *uiout)
362{
19ba03f4 363 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4a9d4ea5 364 ui_file *stream = data->streams.back ();
102040f0 365
4d6cceb4 366 ui_file_rewind (stream);
fb40c209
AC
367}
368
2b03b41d 369/* Dump the buffer onto the specified stream. */
fb40c209 370
fb40c209 371void
2b03b41d 372mi_out_put (struct ui_out *uiout, struct ui_file *stream)
fb40c209 373{
19ba03f4 374 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
4a9d4ea5 375 ui_file *outstream = data->streams.back ();
102040f0 376
4d6cceb4
DE
377 ui_file_put (outstream, ui_file_write_for_put, stream);
378 ui_file_rewind (outstream);
fb40c209
AC
379}
380
2b03b41d 381/* Return the current MI version. */
c7ec4050
AC
382
383int
384mi_version (struct ui_out *uiout)
385{
19ba03f4 386 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
102040f0 387
c7ec4050
AC
388 return data->mi_version;
389}
390
4d6cceb4
DE
391/* Constructor for an `mi_out_data' object. */
392
393static void
394mi_out_data_ctor (mi_out_data *self, int mi_version, struct ui_file *stream)
395{
396 gdb_assert (stream != NULL);
397
4a9d4ea5 398 self->streams.push_back (stream);
4d6cceb4
DE
399
400 self->suppress_field_separator = 0;
401 self->suppress_output = 0;
402 self->mi_version = mi_version;
403}
404
405/* The destructor. */
406
407static void
408mi_out_data_dtor (struct ui_out *ui_out)
409{
19ba03f4 410 mi_out_data *data = (mi_out_data *) ui_out_data (ui_out);
4d6cceb4 411
5486f164 412 delete data;
4d6cceb4
DE
413}
414
2b03b41d 415/* Initialize private members at startup. */
fb40c209
AC
416
417struct ui_out *
b30bf9ee 418mi_out_new (int mi_version)
fb40c209
AC
419{
420 int flags = 0;
5486f164 421 mi_out_data *data = new mi_out_data ();
4d6cceb4
DE
422 struct ui_file *stream = mem_fileopen ();
423
424 mi_out_data_ctor (data, mi_version, stream);
fb40c209
AC
425 return ui_out_new (&mi_ui_out_impl, data, flags);
426}
This page took 1.461028 seconds and 4 git commands to generate.