1 /* MI Command Set - output generating routines.
3 Copyright 2000, 2002 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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
30 int suppress_field_separator;
33 struct ui_file *buffer;
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,
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 static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
64 static void mi_flush (struct ui_out *uiout);
66 /* This is the MI ui-out implementation functions vector */
68 /* FIXME: This can be initialized dynamically after default is set to
69 handle initial output in main.c */
71 struct ui_out_impl mi_ui_out_impl =
88 1, /* Needs MI hacks. */
91 /* Prototypes for local functions */
93 extern void _initialize_mi_out (void);
94 static void field_separator (struct ui_out *uiout);
95 static void mi_open (struct ui_out *uiout, const char *name,
96 enum ui_out_type type);
97 static void mi_close (struct ui_out *uiout, enum ui_out_type type);
99 static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
102 /* Mark beginning of a table */
105 mi_table_begin (struct ui_out *uiout,
110 struct ui_out_data *data = ui_out_data (uiout);
111 mi_open (uiout, tblid, ui_out_type_tuple);
112 if (data->mi_version == 0)
115 data->suppress_output = 1;
117 mi_open (uiout, "hdr", ui_out_type_list);
120 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
122 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
124 mi_open (uiout, "hdr", ui_out_type_list);
127 /* Mark beginning of a table body */
130 mi_table_body (struct ui_out *uiout)
132 struct ui_out_data *data = ui_out_data (uiout);
133 if (data->suppress_output)
135 /* close the table header line if there were any headers */
136 mi_close (uiout, ui_out_type_list);
137 if (data->mi_version == 0)
139 mi_open (uiout, "body", ui_out_type_list);
142 /* Mark end of a table */
145 mi_table_end (struct ui_out *uiout)
147 struct ui_out_data *data = ui_out_data (uiout);
148 data->suppress_output = 0;
149 if (data->mi_version == 0)
151 mi_close (uiout, ui_out_type_tuple);
154 mi_close (uiout, ui_out_type_list); /* body */
155 mi_close (uiout, ui_out_type_tuple);
158 /* Specify table header */
161 mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
162 const char *col_name,
165 struct ui_out_data *data = ui_out_data (uiout);
166 if (data->suppress_output)
168 if (data->mi_version == 0)
170 mi_field_string (uiout, 0, width, alignment, 0, colhdr);
173 mi_open (uiout, NULL, ui_out_type_tuple);
174 mi_field_int (uiout, 0, 0, 0, "width", width);
175 mi_field_int (uiout, 0, 0, 0, "alignment", alignment);
176 mi_field_string (uiout, 0, 0, 0, "col_name", col_name);
177 mi_field_string (uiout, 0, width, alignment, "colhdr", colhdr);
178 mi_close (uiout, ui_out_type_tuple);
181 /* Mark beginning of a list */
184 mi_begin (struct ui_out *uiout,
185 enum ui_out_type type,
189 struct ui_out_data *data = ui_out_data (uiout);
190 if (data->suppress_output)
192 mi_open (uiout, id, type);
195 /* Mark end of a list */
198 mi_end (struct ui_out *uiout,
199 enum ui_out_type type,
202 struct ui_out_data *data = ui_out_data (uiout);
203 if (data->suppress_output)
205 mi_close (uiout, type);
208 /* output an int field */
211 mi_field_int (struct ui_out *uiout, int fldno, int width,
212 enum ui_align alignment, const char *fldname, int value)
214 char buffer[20]; /* FIXME: how many chars long a %d can become? */
215 struct ui_out_data *data = ui_out_data (uiout);
216 if (data->suppress_output)
219 sprintf (buffer, "%d", value);
220 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
223 /* used to ommit a field */
226 mi_field_skip (struct ui_out *uiout, int fldno, int width,
227 enum ui_align alignment, const char *fldname)
229 struct ui_out_data *data = ui_out_data (uiout);
230 if (data->suppress_output)
232 mi_field_string (uiout, fldno, width, alignment, fldname, "");
235 /* other specific mi_field_* end up here so alignment and field
236 separators are both handled by mi_field_string */
239 mi_field_string (struct ui_out *uiout,
246 struct ui_out_data *data = ui_out_data (uiout);
247 if (data->suppress_output)
249 field_separator (uiout);
251 fprintf_unfiltered (data->buffer, "%s=", fldname);
252 fprintf_unfiltered (data->buffer, "\"");
254 fputstr_unfiltered (string, '"', data->buffer);
255 fprintf_unfiltered (data->buffer, "\"");
258 /* This is the only field function that does not align */
261 mi_field_fmt (struct ui_out *uiout, int fldno,
262 int width, enum ui_align align,
267 struct ui_out_data *data = ui_out_data (uiout);
268 if (data->suppress_output)
270 field_separator (uiout);
272 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
274 fputs_unfiltered ("\"", data->buffer);
275 vfprintf_unfiltered (data->buffer, format, args);
276 fputs_unfiltered ("\"", data->buffer);
280 mi_spaces (struct ui_out *uiout, int numspaces)
285 mi_text (struct ui_out *uiout, const char *string)
290 mi_message (struct ui_out *uiout, int verbosity,
297 mi_wrap_hint (struct ui_out *uiout, char *identstring)
299 wrap_here (identstring);
303 mi_flush (struct ui_out *uiout)
305 struct ui_out_data *data = ui_out_data (uiout);
306 gdb_flush (data->buffer);
309 /* local functions */
311 /* Like mi_field_fmt, but takes a variable number of args
312 and makes a va_list and does not insert a separator */
316 out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
319 struct ui_out_data *data = ui_out_data (uiout);
322 field_separator (uiout);
324 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
326 fputs_unfiltered ("\"", data->buffer);
328 va_start (args, format);
329 vfprintf_unfiltered (data->buffer, format, args);
331 fputs_unfiltered ("\"", data->buffer);
336 /* access to ui_out format private members */
339 field_separator (struct ui_out *uiout)
341 struct ui_out_data *data = ui_out_data (uiout);
342 if (data->suppress_field_separator)
343 data->suppress_field_separator = 0;
345 fputc_unfiltered (',', data->buffer);
349 mi_open (struct ui_out *uiout,
351 enum ui_out_type type)
353 struct ui_out_data *data = ui_out_data (uiout);
354 field_separator (uiout);
355 data->suppress_field_separator = 1;
357 fprintf_unfiltered (data->buffer, "%s=", name);
360 case ui_out_type_tuple:
361 fputc_unfiltered ('{', data->buffer);
363 case ui_out_type_list:
364 if (data->mi_version == 0)
365 fputc_unfiltered ('{', data->buffer);
367 fputc_unfiltered ('[', data->buffer);
370 internal_error (__FILE__, __LINE__, "bad switch");
375 mi_close (struct ui_out *uiout,
376 enum ui_out_type type)
378 struct ui_out_data *data = ui_out_data (uiout);
381 case ui_out_type_tuple:
382 fputc_unfiltered ('}', data->buffer);
384 case ui_out_type_list:
385 if (data->mi_version == 0)
386 fputc_unfiltered ('}', data->buffer);
388 fputc_unfiltered (']', data->buffer);
391 internal_error (__FILE__, __LINE__, "bad switch");
393 data->suppress_field_separator = 0;
396 /* add a string to the buffer */
399 mi_out_buffered (struct ui_out *uiout, char *string)
401 struct ui_out_data *data = ui_out_data (uiout);
402 fprintf_unfiltered (data->buffer, "%s", string);
405 /* clear the buffer */
408 mi_out_rewind (struct ui_out *uiout)
410 struct ui_out_data *data = ui_out_data (uiout);
411 ui_file_rewind (data->buffer);
414 /* dump the buffer onto the specified stream */
417 do_write (void *data, const char *buffer, long length_buffer)
419 ui_file_write (data, buffer, length_buffer);
423 mi_out_put (struct ui_out *uiout,
424 struct ui_file *stream)
426 struct ui_out_data *data = ui_out_data (uiout);
427 ui_file_put (data->buffer, do_write, stream);
428 ui_file_rewind (data->buffer);
431 /* initalize private members at startup */
434 mi_out_new (int mi_version)
437 struct ui_out_data *data = XMALLOC (struct ui_out_data);
438 data->suppress_field_separator = 0;
439 data->suppress_output = 0;
440 data->mi_version = mi_version;
441 /* FIXME: This code should be using a ``string_file'' and not the
443 data->buffer = mem_fileopen ();
444 return ui_out_new (&mi_ui_out_impl, data, flags);
447 /* standard gdb initialization hook */
449 _initialize_mi_out (void)
451 /* nothing happens here */