1 /* MI Command Set - output generating routines.
3 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007
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 2 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, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
31 int suppress_field_separator;
34 struct ui_file *buffer;
36 typedef struct ui_out_data mi_out_data;
38 /* These are the MI output functions */
40 static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
41 int nr_rows, const char *tblid);
42 static void mi_table_body (struct ui_out *uiout);
43 static void mi_table_end (struct ui_out *uiout);
44 static void mi_table_header (struct ui_out *uiout, int width,
45 enum ui_align alig, const char *col_name,
47 static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
48 int level, const char *id);
49 static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
50 static void mi_field_int (struct ui_out *uiout, int fldno, int width,
51 enum ui_align alig, const char *fldname, int value);
52 static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
53 enum ui_align alig, const char *fldname);
54 static void mi_field_string (struct ui_out *uiout, int fldno, int width,
55 enum ui_align alig, const char *fldname,
57 static void mi_field_fmt (struct ui_out *uiout, int fldno,
58 int width, enum ui_align align,
59 const char *fldname, const char *format,
60 va_list args) ATTR_FORMAT (printf, 6, 0);
61 static void mi_spaces (struct ui_out *uiout, int numspaces);
62 static void mi_text (struct ui_out *uiout, const char *string);
63 static void mi_message (struct ui_out *uiout, int verbosity,
64 const char *format, va_list args)
65 ATTR_FORMAT (printf, 3, 0);
66 static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
67 static void mi_flush (struct ui_out *uiout);
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_out_data *data = ui_out_data (uiout);
112 mi_open (uiout, tblid, ui_out_type_tuple);
113 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
115 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
117 mi_open (uiout, "hdr", ui_out_type_list);
120 /* Mark beginning of a table body */
123 mi_table_body (struct ui_out *uiout)
125 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);
139 data->suppress_output = 0;
140 mi_close (uiout, ui_out_type_list); /* body */
141 mi_close (uiout, ui_out_type_tuple);
144 /* Specify table header */
147 mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
148 const char *col_name,
151 mi_out_data *data = ui_out_data (uiout);
152 if (data->suppress_output)
154 mi_open (uiout, NULL, ui_out_type_tuple);
155 mi_field_int (uiout, 0, 0, 0, "width", width);
156 mi_field_int (uiout, 0, 0, 0, "alignment", alignment);
157 mi_field_string (uiout, 0, 0, 0, "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,
166 enum ui_out_type type,
170 mi_out_data *data = ui_out_data (uiout);
171 if (data->suppress_output)
173 mi_open (uiout, id, type);
176 /* Mark end of a list */
179 mi_end (struct ui_out *uiout,
180 enum ui_out_type type,
183 mi_out_data *data = ui_out_data (uiout);
184 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 = ui_out_data (uiout);
197 if (data->suppress_output)
200 sprintf (buffer, "%d", value);
201 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
204 /* used to ommit a field */
207 mi_field_skip (struct ui_out *uiout, int fldno, int width,
208 enum ui_align alignment, const char *fldname)
210 mi_out_data *data = ui_out_data (uiout);
211 if (data->suppress_output)
213 mi_field_string (uiout, fldno, width, alignment, fldname, "");
216 /* other specific mi_field_* end up here so alignment and field
217 separators are both handled by mi_field_string */
220 mi_field_string (struct ui_out *uiout,
227 mi_out_data *data = ui_out_data (uiout);
228 if (data->suppress_output)
230 field_separator (uiout);
232 fprintf_unfiltered (data->buffer, "%s=", fldname);
233 fprintf_unfiltered (data->buffer, "\"");
235 fputstr_unfiltered (string, '"', data->buffer);
236 fprintf_unfiltered (data->buffer, "\"");
239 /* This is the only field function that does not align */
242 mi_field_fmt (struct ui_out *uiout, int fldno,
243 int width, enum ui_align align,
248 mi_out_data *data = ui_out_data (uiout);
249 if (data->suppress_output)
251 field_separator (uiout);
253 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
255 fputs_unfiltered ("\"", data->buffer);
256 vfprintf_unfiltered (data->buffer, format, args);
257 fputs_unfiltered ("\"", data->buffer);
261 mi_spaces (struct ui_out *uiout, int numspaces)
266 mi_text (struct ui_out *uiout, const char *string)
271 mi_message (struct ui_out *uiout, int verbosity,
278 mi_wrap_hint (struct ui_out *uiout, char *identstring)
280 wrap_here (identstring);
284 mi_flush (struct ui_out *uiout)
286 mi_out_data *data = ui_out_data (uiout);
287 gdb_flush (data->buffer);
290 /* local functions */
292 /* access to ui_out format private members */
295 field_separator (struct ui_out *uiout)
297 mi_out_data *data = ui_out_data (uiout);
298 if (data->suppress_field_separator)
299 data->suppress_field_separator = 0;
301 fputc_unfiltered (',', data->buffer);
305 mi_open (struct ui_out *uiout,
307 enum ui_out_type type)
309 mi_out_data *data = ui_out_data (uiout);
310 field_separator (uiout);
311 data->suppress_field_separator = 1;
313 fprintf_unfiltered (data->buffer, "%s=", name);
316 case ui_out_type_tuple:
317 fputc_unfiltered ('{', data->buffer);
319 case ui_out_type_list:
320 fputc_unfiltered ('[', data->buffer);
323 internal_error (__FILE__, __LINE__, _("bad switch"));
328 mi_close (struct ui_out *uiout,
329 enum ui_out_type type)
331 mi_out_data *data = ui_out_data (uiout);
334 case ui_out_type_tuple:
335 fputc_unfiltered ('}', data->buffer);
337 case ui_out_type_list:
338 fputc_unfiltered (']', data->buffer);
341 internal_error (__FILE__, __LINE__, _("bad switch"));
343 data->suppress_field_separator = 0;
346 /* add a string to the buffer */
349 mi_out_buffered (struct ui_out *uiout, char *string)
351 mi_out_data *data = ui_out_data (uiout);
352 fprintf_unfiltered (data->buffer, "%s", string);
355 /* clear the buffer */
358 mi_out_rewind (struct ui_out *uiout)
360 mi_out_data *data = ui_out_data (uiout);
361 ui_file_rewind (data->buffer);
364 /* dump the buffer onto the specified stream */
367 do_write (void *data, const char *buffer, long length_buffer)
369 ui_file_write (data, buffer, length_buffer);
373 mi_out_put (struct ui_out *uiout,
374 struct ui_file *stream)
376 mi_out_data *data = ui_out_data (uiout);
377 ui_file_put (data->buffer, do_write, stream);
378 ui_file_rewind (data->buffer);
381 /* Current MI version. */
384 mi_version (struct ui_out *uiout)
386 mi_out_data *data = ui_out_data (uiout);
387 return data->mi_version;
390 /* initalize private members at startup */
393 mi_out_new (int mi_version)
396 mi_out_data *data = XMALLOC (mi_out_data);
397 data->suppress_field_separator = 0;
398 data->suppress_output = 0;
399 data->mi_version = mi_version;
400 /* FIXME: This code should be using a ``string_file'' and not the
402 data->buffer = mem_fileopen ();
403 return ui_out_new (&mi_ui_out_impl, data, flags);
406 /* standard gdb initialization hook */
408 _initialize_mi_out (void)
410 /* nothing happens here */