1 /* MI Command Set - output generating routines.
3 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
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
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 int suppress_field_separator;
32 struct ui_file *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);
67 /* This is the MI ui-out implementation functions vector */
69 /* FIXME: This can be initialized dynamically after default is set to
70 handle initial output in main.c */
72 struct ui_out_impl mi_ui_out_impl =
90 1, /* Needs MI hacks. */
93 /* Prototypes for local functions */
95 extern void _initialize_mi_out (void);
96 static void field_separator (struct ui_out *uiout);
97 static void mi_open (struct ui_out *uiout, const char *name,
98 enum ui_out_type type);
99 static void mi_close (struct ui_out *uiout, enum ui_out_type type);
101 /* Mark beginning of a table */
104 mi_table_begin (struct ui_out *uiout,
109 mi_open (uiout, tblid, ui_out_type_tuple);
110 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
112 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
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 = ui_out_data (uiout);
123 if (data->suppress_output)
125 /* close the table header line if there were any headers */
126 mi_close (uiout, ui_out_type_list);
127 mi_open (uiout, "body", ui_out_type_list);
130 /* Mark end of a table */
133 mi_table_end (struct ui_out *uiout)
135 mi_out_data *data = ui_out_data (uiout);
136 data->suppress_output = 0;
137 mi_close (uiout, ui_out_type_list); /* body */
138 mi_close (uiout, ui_out_type_tuple);
141 /* Specify table header */
144 mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
145 const char *col_name,
148 mi_out_data *data = ui_out_data (uiout);
149 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,
163 enum ui_out_type type,
167 mi_out_data *data = ui_out_data (uiout);
168 if (data->suppress_output)
170 mi_open (uiout, id, type);
173 /* Mark end of a list */
176 mi_end (struct ui_out *uiout,
177 enum ui_out_type type,
180 mi_out_data *data = ui_out_data (uiout);
181 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);
194 if (data->suppress_output)
197 sprintf (buffer, "%d", value);
198 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
201 /* used to ommit a field */
204 mi_field_skip (struct ui_out *uiout, int fldno, int width,
205 enum ui_align alignment, const char *fldname)
207 mi_out_data *data = ui_out_data (uiout);
208 if (data->suppress_output)
210 mi_field_string (uiout, fldno, width, alignment, 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,
224 mi_out_data *data = ui_out_data (uiout);
225 if (data->suppress_output)
227 field_separator (uiout);
229 fprintf_unfiltered (data->buffer, "%s=", fldname);
230 fprintf_unfiltered (data->buffer, "\"");
232 fputstr_unfiltered (string, '"', data->buffer);
233 fprintf_unfiltered (data->buffer, "\"");
236 /* This is the only field function that does not align */
239 mi_field_fmt (struct ui_out *uiout, int fldno,
240 int width, enum ui_align align,
245 mi_out_data *data = ui_out_data (uiout);
246 if (data->suppress_output)
248 field_separator (uiout);
250 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
252 fputs_unfiltered ("\"", data->buffer);
253 vfprintf_unfiltered (data->buffer, format, args);
254 fputs_unfiltered ("\"", data->buffer);
258 mi_spaces (struct ui_out *uiout, int numspaces)
263 mi_text (struct ui_out *uiout, const char *string)
268 mi_message (struct ui_out *uiout, int verbosity,
275 mi_wrap_hint (struct ui_out *uiout, char *identstring)
277 wrap_here (identstring);
281 mi_flush (struct ui_out *uiout)
283 mi_out_data *data = ui_out_data (uiout);
284 gdb_flush (data->buffer);
287 /* local functions */
289 /* access to ui_out format private members */
292 field_separator (struct ui_out *uiout)
294 mi_out_data *data = ui_out_data (uiout);
295 if (data->suppress_field_separator)
296 data->suppress_field_separator = 0;
298 fputc_unfiltered (',', data->buffer);
302 mi_open (struct ui_out *uiout,
304 enum ui_out_type type)
306 mi_out_data *data = ui_out_data (uiout);
307 field_separator (uiout);
308 data->suppress_field_separator = 1;
310 fprintf_unfiltered (data->buffer, "%s=", name);
313 case ui_out_type_tuple:
314 fputc_unfiltered ('{', data->buffer);
316 case ui_out_type_list:
317 fputc_unfiltered ('[', data->buffer);
320 internal_error (__FILE__, __LINE__, _("bad switch"));
325 mi_close (struct ui_out *uiout,
326 enum ui_out_type type)
328 mi_out_data *data = ui_out_data (uiout);
331 case ui_out_type_tuple:
332 fputc_unfiltered ('}', data->buffer);
334 case ui_out_type_list:
335 fputc_unfiltered (']', data->buffer);
338 internal_error (__FILE__, __LINE__, _("bad switch"));
340 data->suppress_field_separator = 0;
343 /* add a string to the buffer */
346 mi_out_buffered (struct ui_out *uiout, char *string)
348 mi_out_data *data = ui_out_data (uiout);
349 fprintf_unfiltered (data->buffer, "%s", string);
352 /* clear the buffer */
355 mi_out_rewind (struct ui_out *uiout)
357 mi_out_data *data = ui_out_data (uiout);
358 ui_file_rewind (data->buffer);
361 /* dump the buffer onto the specified stream */
364 do_write (void *data, const char *buffer, long length_buffer)
366 ui_file_write (data, buffer, length_buffer);
370 mi_out_put (struct ui_out *uiout,
371 struct ui_file *stream)
373 mi_out_data *data = ui_out_data (uiout);
374 ui_file_put (data->buffer, do_write, stream);
375 ui_file_rewind (data->buffer);
378 /* Current MI version. */
381 mi_version (struct ui_out *uiout)
383 mi_out_data *data = ui_out_data (uiout);
384 return data->mi_version;
387 /* initalize private members at startup */
390 mi_out_new (int mi_version)
393 mi_out_data *data = XMALLOC (mi_out_data);
394 data->suppress_field_separator = 0;
395 data->suppress_output = 0;
396 data->mi_version = mi_version;
397 /* FIXME: This code should be using a ``string_file'' and not the
399 data->buffer = mem_fileopen ();
400 return ui_out_new (&mi_ui_out_impl, data, flags);
403 /* standard gdb initialization hook */
405 _initialize_mi_out (void)
407 /* nothing happens here */