1 /* MI Command Set - output generating routines.
3 Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions (a Red Hat company).
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 typedef struct ui_file
*ui_filep
;
32 int suppress_field_separator
;
35 VEC (ui_filep
) *streams
;
37 typedef struct ui_out_data mi_out_data
;
39 /* These are the MI output functions */
41 static void mi_out_data_dtor (struct ui_out
*ui_out
);
42 static void mi_table_begin (struct ui_out
*uiout
, int nbrofcols
,
43 int nr_rows
, const char *tblid
);
44 static void mi_table_body (struct ui_out
*uiout
);
45 static void mi_table_end (struct ui_out
*uiout
);
46 static void mi_table_header (struct ui_out
*uiout
, int width
,
47 enum ui_align alig
, const char *col_name
,
49 static void mi_begin (struct ui_out
*uiout
, enum ui_out_type type
,
50 int level
, const char *id
);
51 static void mi_end (struct ui_out
*uiout
, enum ui_out_type type
, int level
);
52 static void mi_field_int (struct ui_out
*uiout
, int fldno
, int width
,
53 enum ui_align alig
, const char *fldname
, int value
);
54 static void mi_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
55 enum ui_align alig
, const char *fldname
);
56 static void mi_field_string (struct ui_out
*uiout
, int fldno
, int width
,
57 enum ui_align alig
, const char *fldname
,
59 static void mi_field_fmt (struct ui_out
*uiout
, int fldno
,
60 int width
, enum ui_align align
,
61 const char *fldname
, const char *format
,
62 va_list args
) ATTRIBUTE_PRINTF (6, 0);
63 static void mi_spaces (struct ui_out
*uiout
, int numspaces
);
64 static void mi_text (struct ui_out
*uiout
, const char *string
);
65 static void mi_message (struct ui_out
*uiout
, int verbosity
,
66 const char *format
, va_list args
)
67 ATTRIBUTE_PRINTF (3, 0);
68 static void mi_wrap_hint (struct ui_out
*uiout
, char *identstring
);
69 static void mi_flush (struct ui_out
*uiout
);
70 static int mi_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
);
72 /* This is the MI ui-out implementation functions vector */
74 static const struct ui_out_impl mi_ui_out_impl
=
93 1, /* Needs MI hacks. */
96 /* Prototypes for local functions */
98 static void field_separator (struct ui_out
*uiout
);
99 static void mi_open (struct ui_out
*uiout
, const char *name
,
100 enum ui_out_type type
);
101 static void mi_close (struct ui_out
*uiout
, enum ui_out_type type
);
103 /* Mark beginning of a table. */
106 mi_table_begin (struct ui_out
*uiout
,
111 mi_open (uiout
, tblid
, ui_out_type_tuple
);
112 mi_field_int (uiout
, -1, -1, ui_left
, "nr_rows", nr_rows
);
113 mi_field_int (uiout
, -1, -1, ui_left
, "nr_cols", nr_cols
);
114 mi_open (uiout
, "hdr", ui_out_type_list
);
117 /* Mark beginning of a table body. */
120 mi_table_body (struct ui_out
*uiout
)
122 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
124 if (data
->suppress_output
)
126 /* close the table header line if there were any headers */
127 mi_close (uiout
, ui_out_type_list
);
128 mi_open (uiout
, "body", ui_out_type_list
);
131 /* Mark end of a table. */
134 mi_table_end (struct ui_out
*uiout
)
136 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
138 data
->suppress_output
= 0;
139 mi_close (uiout
, ui_out_type_list
); /* body */
140 mi_close (uiout
, ui_out_type_tuple
);
143 /* Specify table header. */
146 mi_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
147 const char *col_name
, const char *colhdr
)
149 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
151 if (data
->suppress_output
)
154 mi_open (uiout
, NULL
, ui_out_type_tuple
);
155 mi_field_int (uiout
, 0, 0, ui_center
, "width", width
);
156 mi_field_int (uiout
, 0, 0, ui_center
, "alignment", alignment
);
157 mi_field_string (uiout
, 0, 0, ui_center
, "col_name", col_name
);
158 mi_field_string (uiout
, 0, width
, alignment
, "colhdr", colhdr
);
159 mi_close (uiout
, ui_out_type_tuple
);
162 /* Mark beginning of a list. */
165 mi_begin (struct ui_out
*uiout
, enum ui_out_type type
, int level
,
168 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
170 if (data
->suppress_output
)
173 mi_open (uiout
, id
, type
);
176 /* Mark end of a list. */
179 mi_end (struct ui_out
*uiout
, enum ui_out_type type
, int level
)
181 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
183 if (data
->suppress_output
)
186 mi_close (uiout
, type
);
189 /* Output an int field. */
192 mi_field_int (struct ui_out
*uiout
, int fldno
, int width
,
193 enum ui_align alignment
, const char *fldname
, int value
)
195 char buffer
[20]; /* FIXME: how many chars long a %d can become? */
196 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
198 if (data
->suppress_output
)
201 xsnprintf (buffer
, sizeof (buffer
), "%d", value
);
202 mi_field_string (uiout
, fldno
, width
, alignment
, fldname
, buffer
);
205 /* Used to omit a field. */
208 mi_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
209 enum ui_align alignment
, const char *fldname
)
213 /* Other specific mi_field_* end up here so alignment and field
214 separators are both handled by mi_field_string. */
217 mi_field_string (struct ui_out
*uiout
, int fldno
, int width
,
218 enum ui_align align
, const char *fldname
, const char *string
)
220 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
221 struct ui_file
*stream
;
223 if (data
->suppress_output
)
226 stream
= VEC_last (ui_filep
, data
->streams
);
227 field_separator (uiout
);
229 fprintf_unfiltered (stream
, "%s=", fldname
);
230 fprintf_unfiltered (stream
, "\"");
232 fputstr_unfiltered (string
, '"', stream
);
233 fprintf_unfiltered (stream
, "\"");
236 /* This is the only field function that does not align. */
239 mi_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
240 enum ui_align align
, const char *fldname
,
241 const char *format
, va_list args
)
243 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
244 struct ui_file
*stream
;
246 if (data
->suppress_output
)
249 stream
= VEC_last (ui_filep
, data
->streams
);
250 field_separator (uiout
);
252 fprintf_unfiltered (stream
, "%s=\"", fldname
);
254 fputs_unfiltered ("\"", stream
);
255 vfprintf_unfiltered (stream
, format
, args
);
256 fputs_unfiltered ("\"", stream
);
260 mi_spaces (struct ui_out
*uiout
, int numspaces
)
265 mi_text (struct ui_out
*uiout
, const char *string
)
270 mi_message (struct ui_out
*uiout
, int verbosity
,
271 const char *format
, va_list args
)
276 mi_wrap_hint (struct ui_out
*uiout
, char *identstring
)
278 wrap_here (identstring
);
282 mi_flush (struct ui_out
*uiout
)
284 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
285 struct ui_file
*stream
= VEC_last (ui_filep
, data
->streams
);
291 mi_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
293 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
295 if (outstream
!= NULL
)
296 VEC_safe_push (ui_filep
, data
->streams
, outstream
);
298 VEC_pop (ui_filep
, data
->streams
);
303 /* local functions */
305 /* access to ui_out format private members */
308 field_separator (struct ui_out
*uiout
)
310 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
311 struct ui_file
*stream
= VEC_last (ui_filep
, data
->streams
);
313 if (data
->suppress_field_separator
)
314 data
->suppress_field_separator
= 0;
316 fputc_unfiltered (',', stream
);
320 mi_open (struct ui_out
*uiout
, const char *name
, enum ui_out_type type
)
322 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
323 struct ui_file
*stream
= VEC_last (ui_filep
, data
->streams
);
325 field_separator (uiout
);
326 data
->suppress_field_separator
= 1;
328 fprintf_unfiltered (stream
, "%s=", name
);
331 case ui_out_type_tuple
:
332 fputc_unfiltered ('{', stream
);
334 case ui_out_type_list
:
335 fputc_unfiltered ('[', stream
);
338 internal_error (__FILE__
, __LINE__
, _("bad switch"));
343 mi_close (struct ui_out
*uiout
, enum ui_out_type type
)
345 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
346 struct ui_file
*stream
= VEC_last (ui_filep
, data
->streams
);
350 case ui_out_type_tuple
:
351 fputc_unfiltered ('}', stream
);
353 case ui_out_type_list
:
354 fputc_unfiltered (']', stream
);
357 internal_error (__FILE__
, __LINE__
, _("bad switch"));
359 data
->suppress_field_separator
= 0;
362 /* Clear the buffer. */
365 mi_out_rewind (struct ui_out
*uiout
)
367 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
368 struct ui_file
*stream
= VEC_last (ui_filep
, data
->streams
);
370 ui_file_rewind (stream
);
373 /* Dump the buffer onto the specified stream. */
376 mi_out_put (struct ui_out
*uiout
, struct ui_file
*stream
)
378 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
379 struct ui_file
*outstream
= VEC_last (ui_filep
, data
->streams
);
381 ui_file_put (outstream
, ui_file_write_for_put
, stream
);
382 ui_file_rewind (outstream
);
385 /* Return the current MI version. */
388 mi_version (struct ui_out
*uiout
)
390 mi_out_data
*data
= (mi_out_data
*) ui_out_data (uiout
);
392 return data
->mi_version
;
395 /* Constructor for an `mi_out_data' object. */
398 mi_out_data_ctor (mi_out_data
*self
, int mi_version
, struct ui_file
*stream
)
400 gdb_assert (stream
!= NULL
);
402 self
->streams
= NULL
;
403 VEC_safe_push (ui_filep
, self
->streams
, stream
);
405 self
->suppress_field_separator
= 0;
406 self
->suppress_output
= 0;
407 self
->mi_version
= mi_version
;
410 /* The destructor. */
413 mi_out_data_dtor (struct ui_out
*ui_out
)
415 mi_out_data
*data
= (mi_out_data
*) ui_out_data (ui_out
);
417 VEC_free (ui_filep
, data
->streams
);
421 /* Initialize private members at startup. */
424 mi_out_new (int mi_version
)
427 mi_out_data
*data
= XNEW (mi_out_data
);
428 struct ui_file
*stream
= mem_fileopen ();
430 mi_out_data_ctor (data
, mi_version
, stream
);
431 return ui_out_new (&mi_ui_out_impl
, data
, flags
);