1 /* MI Command Set - output generating routines.
3 Copyright (C) 2000, 2002-2005, 2007-2012 Free Software Foundation,
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;
33 struct ui_file *original_buffer;
35 typedef struct ui_out_data mi_out_data;
37 /* These are the MI output functions */
39 static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
40 int nr_rows, const char *tblid);
41 static void mi_table_body (struct ui_out *uiout);
42 static void mi_table_end (struct ui_out *uiout);
43 static void mi_table_header (struct ui_out *uiout, int width,
44 enum ui_align alig, const char *col_name,
46 static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
47 int level, const char *id);
48 static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
49 static void mi_field_int (struct ui_out *uiout, int fldno, int width,
50 enum ui_align alig, const char *fldname, int value);
51 static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
52 enum ui_align alig, const char *fldname);
53 static void mi_field_string (struct ui_out *uiout, int fldno, int width,
54 enum ui_align alig, const char *fldname,
56 static void mi_field_fmt (struct ui_out *uiout, int fldno,
57 int width, enum ui_align align,
58 const char *fldname, const char *format,
59 va_list args) ATTRIBUTE_PRINTF (6, 0);
60 static void mi_spaces (struct ui_out *uiout, int numspaces);
61 static void mi_text (struct ui_out *uiout, const char *string);
62 static void mi_message (struct ui_out *uiout, int verbosity,
63 const char *format, va_list args)
64 ATTRIBUTE_PRINTF (3, 0);
65 static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
66 static void mi_flush (struct ui_out *uiout);
67 static int mi_redirect (struct ui_out *uiout, struct ui_file *outstream);
69 /* This is the MI ui-out implementation functions vector */
71 /* FIXME: This can be initialized dynamically after default is set to
72 handle initial output in main.c */
74 struct ui_out_impl mi_ui_out_impl =
92 1, /* Needs MI hacks. */
95 /* Prototypes for local functions */
97 extern void _initialize_mi_out (void);
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/*fldno*/, -1/*width*/, -1/*alin*/,
114 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
116 mi_open (uiout, "hdr", ui_out_type_list);
119 /* Mark beginning of a table body */
122 mi_table_body (struct ui_out *uiout)
124 mi_out_data *data = ui_out_data (uiout);
126 if (data->suppress_output)
128 /* close the table header line if there were any headers */
129 mi_close (uiout, ui_out_type_list);
130 mi_open (uiout, "body", ui_out_type_list);
133 /* Mark end of a table */
136 mi_table_end (struct ui_out *uiout)
138 mi_out_data *data = ui_out_data (uiout);
140 data->suppress_output = 0;
141 mi_close (uiout, ui_out_type_list); /* body */
142 mi_close (uiout, ui_out_type_tuple);
145 /* Specify table header */
148 mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
149 const char *col_name,
152 mi_out_data *data = ui_out_data (uiout);
154 if (data->suppress_output)
156 mi_open (uiout, NULL, ui_out_type_tuple);
157 mi_field_int (uiout, 0, 0, 0, "width", width);
158 mi_field_int (uiout, 0, 0, 0, "alignment", alignment);
159 mi_field_string (uiout, 0, 0, 0, "col_name", col_name);
160 mi_field_string (uiout, 0, width, alignment, "colhdr", colhdr);
161 mi_close (uiout, ui_out_type_tuple);
164 /* Mark beginning of a list */
167 mi_begin (struct ui_out *uiout,
168 enum ui_out_type type,
172 mi_out_data *data = ui_out_data (uiout);
174 if (data->suppress_output)
176 mi_open (uiout, id, type);
179 /* Mark end of a list */
182 mi_end (struct ui_out *uiout,
183 enum ui_out_type type,
186 mi_out_data *data = ui_out_data (uiout);
188 if (data->suppress_output)
190 mi_close (uiout, type);
193 /* output an int field */
196 mi_field_int (struct ui_out *uiout, int fldno, int width,
197 enum ui_align alignment, const char *fldname, int value)
199 char buffer[20]; /* FIXME: how many chars long a %d can become? */
200 mi_out_data *data = ui_out_data (uiout);
202 if (data->suppress_output)
205 sprintf (buffer, "%d", value);
206 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
209 /* used to ommit a field */
212 mi_field_skip (struct ui_out *uiout, int fldno, int width,
213 enum ui_align alignment, const char *fldname)
217 /* other specific mi_field_* end up here so alignment and field
218 separators are both handled by mi_field_string */
221 mi_field_string (struct ui_out *uiout,
228 mi_out_data *data = ui_out_data (uiout);
230 if (data->suppress_output)
232 field_separator (uiout);
234 fprintf_unfiltered (data->buffer, "%s=", fldname);
235 fprintf_unfiltered (data->buffer, "\"");
237 fputstr_unfiltered (string, '"', data->buffer);
238 fprintf_unfiltered (data->buffer, "\"");
241 /* This is the only field function that does not align */
244 mi_field_fmt (struct ui_out *uiout, int fldno,
245 int width, enum ui_align align,
250 mi_out_data *data = ui_out_data (uiout);
252 if (data->suppress_output)
254 field_separator (uiout);
256 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
258 fputs_unfiltered ("\"", data->buffer);
259 vfprintf_unfiltered (data->buffer, format, args);
260 fputs_unfiltered ("\"", data->buffer);
264 mi_spaces (struct ui_out *uiout, int numspaces)
269 mi_text (struct ui_out *uiout, const char *string)
274 mi_message (struct ui_out *uiout, int verbosity,
281 mi_wrap_hint (struct ui_out *uiout, char *identstring)
283 wrap_here (identstring);
287 mi_flush (struct ui_out *uiout)
289 mi_out_data *data = ui_out_data (uiout);
291 gdb_flush (data->buffer);
295 mi_redirect (struct ui_out *uiout, struct ui_file *outstream)
297 mi_out_data *data = ui_out_data (uiout);
299 if (outstream != NULL)
301 data->original_buffer = data->buffer;
302 data->buffer = outstream;
304 else if (data->original_buffer != NULL)
306 data->buffer = data->original_buffer;
307 data->original_buffer = NULL;
313 /* local functions */
315 /* access to ui_out format private members */
318 field_separator (struct ui_out *uiout)
320 mi_out_data *data = ui_out_data (uiout);
322 if (data->suppress_field_separator)
323 data->suppress_field_separator = 0;
325 fputc_unfiltered (',', data->buffer);
329 mi_open (struct ui_out *uiout,
331 enum ui_out_type type)
333 mi_out_data *data = ui_out_data (uiout);
335 field_separator (uiout);
336 data->suppress_field_separator = 1;
338 fprintf_unfiltered (data->buffer, "%s=", name);
341 case ui_out_type_tuple:
342 fputc_unfiltered ('{', data->buffer);
344 case ui_out_type_list:
345 fputc_unfiltered ('[', data->buffer);
348 internal_error (__FILE__, __LINE__, _("bad switch"));
353 mi_close (struct ui_out *uiout,
354 enum ui_out_type type)
356 mi_out_data *data = ui_out_data (uiout);
360 case ui_out_type_tuple:
361 fputc_unfiltered ('}', data->buffer);
363 case ui_out_type_list:
364 fputc_unfiltered (']', data->buffer);
367 internal_error (__FILE__, __LINE__, _("bad switch"));
369 data->suppress_field_separator = 0;
372 /* add a string to the buffer */
375 mi_out_buffered (struct ui_out *uiout, char *string)
377 mi_out_data *data = ui_out_data (uiout);
379 fprintf_unfiltered (data->buffer, "%s", string);
382 /* clear the buffer */
385 mi_out_rewind (struct ui_out *uiout)
387 mi_out_data *data = ui_out_data (uiout);
389 ui_file_rewind (data->buffer);
392 /* dump the buffer onto the specified stream */
395 do_write (void *data, const char *buffer, long length_buffer)
397 ui_file_write (data, buffer, length_buffer);
401 mi_out_put (struct ui_out *uiout,
402 struct ui_file *stream)
404 mi_out_data *data = ui_out_data (uiout);
406 ui_file_put (data->buffer, do_write, stream);
407 ui_file_rewind (data->buffer);
410 /* Current MI version. */
413 mi_version (struct ui_out *uiout)
415 mi_out_data *data = ui_out_data (uiout);
417 return data->mi_version;
420 /* initalize private members at startup */
423 mi_out_new (int mi_version)
427 mi_out_data *data = XMALLOC (mi_out_data);
428 data->suppress_field_separator = 0;
429 data->suppress_output = 0;
430 data->mi_version = mi_version;
431 /* FIXME: This code should be using a ``string_file'' and not the
433 data->buffer = mem_fileopen ();
434 return ui_out_new (&mi_ui_out_impl, data, flags);
437 /* standard gdb initialization hook */
439 _initialize_mi_out (void)
441 /* nothing happens here */