1 /* MI Command Set - output generating routines.
3 Copyright (C) 2000-2015 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/>. */
28 int suppress_field_separator
;
31 struct ui_file
*buffer
;
32 struct ui_file
*original_buffer
;
34 typedef struct ui_out_data mi_out_data
;
36 /* These are the MI output functions */
38 static void mi_table_begin (struct ui_out
*uiout
, int nbrofcols
,
39 int nr_rows
, const char *tblid
);
40 static void mi_table_body (struct ui_out
*uiout
);
41 static void mi_table_end (struct ui_out
*uiout
);
42 static void mi_table_header (struct ui_out
*uiout
, int width
,
43 enum ui_align alig
, const char *col_name
,
45 static void mi_begin (struct ui_out
*uiout
, enum ui_out_type type
,
46 int level
, const char *id
);
47 static void mi_end (struct ui_out
*uiout
, enum ui_out_type type
, int level
);
48 static void mi_field_int (struct ui_out
*uiout
, int fldno
, int width
,
49 enum ui_align alig
, const char *fldname
, int value
);
50 static void mi_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
51 enum ui_align alig
, const char *fldname
);
52 static void mi_field_string (struct ui_out
*uiout
, int fldno
, int width
,
53 enum ui_align alig
, const char *fldname
,
55 static void mi_field_fmt (struct ui_out
*uiout
, int fldno
,
56 int width
, enum ui_align align
,
57 const char *fldname
, const char *format
,
58 va_list args
) ATTRIBUTE_PRINTF (6, 0);
59 static void mi_spaces (struct ui_out
*uiout
, int numspaces
);
60 static void mi_text (struct ui_out
*uiout
, const char *string
);
61 static void mi_message (struct ui_out
*uiout
, int verbosity
,
62 const char *format
, va_list args
)
63 ATTRIBUTE_PRINTF (3, 0);
64 static void mi_wrap_hint (struct ui_out
*uiout
, char *identstring
);
65 static void mi_flush (struct ui_out
*uiout
);
66 static int mi_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
);
68 /* This is the MI ui-out implementation functions vector */
70 static const struct ui_out_impl mi_ui_out_impl
=
89 1, /* Needs MI hacks. */
92 /* Prototypes for local functions */
94 extern void _initialize_mi_out (void);
95 static void field_separator (struct ui_out
*uiout
);
96 static void mi_open (struct ui_out
*uiout
, const char *name
,
97 enum ui_out_type type
);
98 static void mi_close (struct ui_out
*uiout
, enum ui_out_type type
);
100 /* Mark beginning of a table. */
103 mi_table_begin (struct ui_out
*uiout
,
108 mi_open (uiout
, tblid
, ui_out_type_tuple
);
109 mi_field_int (uiout
, -1, -1, -1, "nr_rows", nr_rows
);
110 mi_field_int (uiout
, -1, -1, -1, "nr_cols", nr_cols
);
111 mi_open (uiout
, "hdr", ui_out_type_list
);
114 /* Mark beginning of a table body. */
117 mi_table_body (struct ui_out
*uiout
)
119 mi_out_data
*data
= ui_out_data (uiout
);
121 if (data
->suppress_output
)
123 /* close the table header line if there were any headers */
124 mi_close (uiout
, ui_out_type_list
);
125 mi_open (uiout
, "body", ui_out_type_list
);
128 /* Mark end of a table. */
131 mi_table_end (struct ui_out
*uiout
)
133 mi_out_data
*data
= ui_out_data (uiout
);
135 data
->suppress_output
= 0;
136 mi_close (uiout
, ui_out_type_list
); /* body */
137 mi_close (uiout
, ui_out_type_tuple
);
140 /* Specify table header. */
143 mi_table_header (struct ui_out
*uiout
, int width
, enum ui_align alignment
,
144 const char *col_name
, const char *colhdr
)
146 mi_out_data
*data
= ui_out_data (uiout
);
148 if (data
->suppress_output
)
151 mi_open (uiout
, NULL
, ui_out_type_tuple
);
152 mi_field_int (uiout
, 0, 0, 0, "width", width
);
153 mi_field_int (uiout
, 0, 0, 0, "alignment", alignment
);
154 mi_field_string (uiout
, 0, 0, 0, "col_name", col_name
);
155 mi_field_string (uiout
, 0, width
, alignment
, "colhdr", colhdr
);
156 mi_close (uiout
, ui_out_type_tuple
);
159 /* Mark beginning of a list. */
162 mi_begin (struct ui_out
*uiout
, enum ui_out_type type
, int level
,
165 mi_out_data
*data
= ui_out_data (uiout
);
167 if (data
->suppress_output
)
170 mi_open (uiout
, id
, type
);
173 /* Mark end of a list. */
176 mi_end (struct ui_out
*uiout
, enum ui_out_type type
, int level
)
178 mi_out_data
*data
= ui_out_data (uiout
);
180 if (data
->suppress_output
)
183 mi_close (uiout
, type
);
186 /* Output an int field. */
189 mi_field_int (struct ui_out
*uiout
, int fldno
, int width
,
190 enum ui_align alignment
, const char *fldname
, int value
)
192 char buffer
[20]; /* FIXME: how many chars long a %d can become? */
193 mi_out_data
*data
= ui_out_data (uiout
);
195 if (data
->suppress_output
)
198 xsnprintf (buffer
, sizeof (buffer
), "%d", value
);
199 mi_field_string (uiout
, fldno
, width
, alignment
, fldname
, buffer
);
202 /* Used to omit a field. */
205 mi_field_skip (struct ui_out
*uiout
, int fldno
, int width
,
206 enum ui_align alignment
, const char *fldname
)
210 /* Other specific mi_field_* end up here so alignment and field
211 separators are both handled by mi_field_string. */
214 mi_field_string (struct ui_out
*uiout
, int fldno
, int width
,
215 enum ui_align align
, const char *fldname
, const char *string
)
217 mi_out_data
*data
= ui_out_data (uiout
);
219 if (data
->suppress_output
)
222 field_separator (uiout
);
224 fprintf_unfiltered (data
->buffer
, "%s=", fldname
);
225 fprintf_unfiltered (data
->buffer
, "\"");
227 fputstr_unfiltered (string
, '"', data
->buffer
);
228 fprintf_unfiltered (data
->buffer
, "\"");
231 /* This is the only field function that does not align. */
234 mi_field_fmt (struct ui_out
*uiout
, int fldno
, int width
,
235 enum ui_align align
, const char *fldname
,
236 const char *format
, va_list args
)
238 mi_out_data
*data
= ui_out_data (uiout
);
240 if (data
->suppress_output
)
243 field_separator (uiout
);
245 fprintf_unfiltered (data
->buffer
, "%s=\"", fldname
);
247 fputs_unfiltered ("\"", data
->buffer
);
248 vfprintf_unfiltered (data
->buffer
, format
, args
);
249 fputs_unfiltered ("\"", data
->buffer
);
253 mi_spaces (struct ui_out
*uiout
, int numspaces
)
258 mi_text (struct ui_out
*uiout
, const char *string
)
263 mi_message (struct ui_out
*uiout
, int verbosity
,
264 const char *format
, va_list args
)
269 mi_wrap_hint (struct ui_out
*uiout
, char *identstring
)
271 wrap_here (identstring
);
275 mi_flush (struct ui_out
*uiout
)
277 mi_out_data
*data
= ui_out_data (uiout
);
279 gdb_flush (data
->buffer
);
283 mi_redirect (struct ui_out
*uiout
, struct ui_file
*outstream
)
285 mi_out_data
*data
= ui_out_data (uiout
);
287 if (outstream
!= NULL
)
289 data
->original_buffer
= data
->buffer
;
290 data
->buffer
= outstream
;
292 else if (data
->original_buffer
!= NULL
)
294 data
->buffer
= data
->original_buffer
;
295 data
->original_buffer
= NULL
;
301 /* local functions */
303 /* access to ui_out format private members */
306 field_separator (struct ui_out
*uiout
)
308 mi_out_data
*data
= ui_out_data (uiout
);
310 if (data
->suppress_field_separator
)
311 data
->suppress_field_separator
= 0;
313 fputc_unfiltered (',', data
->buffer
);
317 mi_open (struct ui_out
*uiout
, const char *name
, enum ui_out_type type
)
319 mi_out_data
*data
= ui_out_data (uiout
);
321 field_separator (uiout
);
322 data
->suppress_field_separator
= 1;
324 fprintf_unfiltered (data
->buffer
, "%s=", name
);
327 case ui_out_type_tuple
:
328 fputc_unfiltered ('{', data
->buffer
);
330 case ui_out_type_list
:
331 fputc_unfiltered ('[', data
->buffer
);
334 internal_error (__FILE__
, __LINE__
, _("bad switch"));
339 mi_close (struct ui_out
*uiout
, enum ui_out_type type
)
341 mi_out_data
*data
= ui_out_data (uiout
);
345 case ui_out_type_tuple
:
346 fputc_unfiltered ('}', data
->buffer
);
348 case ui_out_type_list
:
349 fputc_unfiltered (']', data
->buffer
);
352 internal_error (__FILE__
, __LINE__
, _("bad switch"));
354 data
->suppress_field_separator
= 0;
357 /* Add a string to the buffer. */
360 mi_out_buffered (struct ui_out
*uiout
, char *string
)
362 mi_out_data
*data
= ui_out_data (uiout
);
364 fprintf_unfiltered (data
->buffer
, "%s", string
);
367 /* Clear the buffer. */
370 mi_out_rewind (struct ui_out
*uiout
)
372 mi_out_data
*data
= ui_out_data (uiout
);
374 ui_file_rewind (data
->buffer
);
377 /* Dump the buffer onto the specified stream. */
380 mi_out_put (struct ui_out
*uiout
, struct ui_file
*stream
)
382 mi_out_data
*data
= ui_out_data (uiout
);
384 ui_file_put (data
->buffer
, ui_file_write_for_put
, stream
);
385 ui_file_rewind (data
->buffer
);
388 /* Return the current MI version. */
391 mi_version (struct ui_out
*uiout
)
393 mi_out_data
*data
= ui_out_data (uiout
);
395 return data
->mi_version
;
398 /* Initialize private members at startup. */
401 mi_out_new (int mi_version
)
405 mi_out_data
*data
= XNEW (mi_out_data
);
406 data
->suppress_field_separator
= 0;
407 data
->suppress_output
= 0;
408 data
->mi_version
= mi_version
;
409 /* FIXME: This code should be using a ``string_file'' and not the
411 data
->buffer
= mem_fileopen ();
412 return ui_out_new (&mi_ui_out_impl
, data
, flags
);