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