2011-03-02 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / mi / mi-out.c
CommitLineData
fb40c209 1/* MI Command Set - output generating routines.
349c5d5f 2
7b6bb8da 3 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
6aba47ca 4 Free Software Foundation, Inc.
349c5d5f 5
ab91fdd5 6 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
7
8 This file is part of GDB.
9
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
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
fb40c209
AC
13 (at your option) any later version.
14
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.
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fb40c209
AC
22
23#include "defs.h"
24#include "ui-out.h"
25#include "mi-out.h"
26
fb40c209
AC
27struct ui_out_data
28 {
59807497 29 int suppress_field_separator;
76fe6b98 30 int suppress_output;
b30bf9ee 31 int mi_version;
fb40c209
AC
32 struct ui_file *buffer;
33 };
1248ede2 34typedef struct ui_out_data mi_out_data;
fb40c209
AC
35
36/* These are the MI output functions */
37
e2e11a41 38static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
d63f1d40 39 int nr_rows, const char *tblid);
fb40c209
AC
40static void mi_table_body (struct ui_out *uiout);
41static void mi_table_end (struct ui_out *uiout);
42static void mi_table_header (struct ui_out *uiout, int width,
b25959ec 43 enum ui_align alig, const char *col_name,
e2e11a41 44 const char *colhdr);
631ec795
AC
45static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
46 int level, const char *id);
47static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
fb40c209 48static void mi_field_int (struct ui_out *uiout, int fldno, int width,
e2e11a41 49 enum ui_align alig, const char *fldname, int value);
fb40c209 50static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
e2e11a41 51 enum ui_align alig, const char *fldname);
fb40c209 52static void mi_field_string (struct ui_out *uiout, int fldno, int width,
e2e11a41 53 enum ui_align alig, const char *fldname,
fb40c209
AC
54 const char *string);
55static void mi_field_fmt (struct ui_out *uiout, int fldno,
56 int width, enum ui_align align,
e2e11a41 57 const char *fldname, const char *format,
a0b31db1 58 va_list args) ATTRIBUTE_PRINTF (6, 0);
fb40c209 59static void mi_spaces (struct ui_out *uiout, int numspaces);
e2e11a41
AC
60static void mi_text (struct ui_out *uiout, const char *string);
61static void mi_message (struct ui_out *uiout, int verbosity,
bee0189a 62 const char *format, va_list args)
a0b31db1 63 ATTRIBUTE_PRINTF (3, 0);
fb40c209
AC
64static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
65static void mi_flush (struct ui_out *uiout);
66
67/* This is the MI ui-out implementation functions vector */
68
69/* FIXME: This can be initialized dynamically after default is set to
70 handle initial output in main.c */
71
72struct ui_out_impl mi_ui_out_impl =
73{
74 mi_table_begin,
75 mi_table_body,
76 mi_table_end,
77 mi_table_header,
631ec795
AC
78 mi_begin,
79 mi_end,
fb40c209
AC
80 mi_field_int,
81 mi_field_skip,
82 mi_field_string,
83 mi_field_fmt,
84 mi_spaces,
85 mi_text,
86 mi_message,
87 mi_wrap_hint,
9dc5e2a9 88 mi_flush,
0fac0b41 89 NULL,
9dc5e2a9 90 1, /* Needs MI hacks. */
fb40c209
AC
91};
92
93/* Prototypes for local functions */
94
a14ed312 95extern void _initialize_mi_out (void);
fb40c209 96static void field_separator (struct ui_out *uiout);
d5e8ba62
AC
97static void mi_open (struct ui_out *uiout, const char *name,
98 enum ui_out_type type);
9a0f0643 99static void mi_close (struct ui_out *uiout, enum ui_out_type type);
fb40c209 100
fb40c209
AC
101/* Mark beginning of a table */
102
103void
cff22675
AC
104mi_table_begin (struct ui_out *uiout,
105 int nr_cols,
d63f1d40 106 int nr_rows,
e2e11a41 107 const char *tblid)
fb40c209 108{
d5e8ba62 109 mi_open (uiout, tblid, ui_out_type_tuple);
cff22675
AC
110 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
111 "nr_rows", nr_rows);
112 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
113 "nr_cols", nr_cols);
114 mi_open (uiout, "hdr", ui_out_type_list);
fb40c209
AC
115}
116
117/* Mark beginning of a table body */
118
119void
fba45db2 120mi_table_body (struct ui_out *uiout)
fb40c209 121{
1248ede2 122 mi_out_data *data = ui_out_data (uiout);
102040f0 123
76fe6b98
AC
124 if (data->suppress_output)
125 return;
cff22675
AC
126 /* close the table header line if there were any headers */
127 mi_close (uiout, ui_out_type_list);
cff22675 128 mi_open (uiout, "body", ui_out_type_list);
fb40c209
AC
129}
130
131/* Mark end of a table */
132
133void
fba45db2 134mi_table_end (struct ui_out *uiout)
fb40c209 135{
1248ede2 136 mi_out_data *data = ui_out_data (uiout);
102040f0 137
76fe6b98 138 data->suppress_output = 0;
cff22675 139 mi_close (uiout, ui_out_type_list); /* body */
666547aa 140 mi_close (uiout, ui_out_type_tuple);
fb40c209
AC
141}
142
143/* Specify table header */
144
145void
46712191 146mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
b25959ec 147 const char *col_name,
e2e11a41 148 const char *colhdr)
fb40c209 149{
1248ede2 150 mi_out_data *data = ui_out_data (uiout);
102040f0 151
76fe6b98
AC
152 if (data->suppress_output)
153 return;
cff22675
AC
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);
fb40c209
AC
160}
161
162/* Mark beginning of a list */
163
164void
631ec795
AC
165mi_begin (struct ui_out *uiout,
166 enum ui_out_type type,
167 int level,
9a0f0643 168 const char *id)
fb40c209 169{
1248ede2 170 mi_out_data *data = ui_out_data (uiout);
102040f0 171
76fe6b98
AC
172 if (data->suppress_output)
173 return;
d5e8ba62 174 mi_open (uiout, id, type);
fb40c209
AC
175}
176
177/* Mark end of a list */
178
179void
631ec795
AC
180mi_end (struct ui_out *uiout,
181 enum ui_out_type type,
182 int level)
fb40c209 183{
1248ede2 184 mi_out_data *data = ui_out_data (uiout);
102040f0 185
76fe6b98
AC
186 if (data->suppress_output)
187 return;
9a0f0643 188 mi_close (uiout, type);
fb40c209
AC
189}
190
191/* output an int field */
192
193void
46712191
KB
194mi_field_int (struct ui_out *uiout, int fldno, int width,
195 enum ui_align alignment, const char *fldname, int value)
fb40c209 196{
102040f0 197 char buffer[20]; /* FIXME: how many chars long a %d can become? */
1248ede2 198 mi_out_data *data = ui_out_data (uiout);
102040f0 199
76fe6b98
AC
200 if (data->suppress_output)
201 return;
fb40c209
AC
202
203 sprintf (buffer, "%d", value);
204 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
205}
206
207/* used to ommit a field */
208
209void
46712191
KB
210mi_field_skip (struct ui_out *uiout, int fldno, int width,
211 enum ui_align alignment, const char *fldname)
fb40c209 212{
1248ede2 213 mi_out_data *data = ui_out_data (uiout);
102040f0 214
76fe6b98
AC
215 if (data->suppress_output)
216 return;
fb40c209
AC
217 mi_field_string (uiout, fldno, width, alignment, fldname, "");
218}
219
220/* other specific mi_field_* end up here so alignment and field
221 separators are both handled by mi_field_string */
222
223void
224mi_field_string (struct ui_out *uiout,
225 int fldno,
226 int width,
46712191 227 enum ui_align align,
e2e11a41 228 const char *fldname,
fb40c209
AC
229 const char *string)
230{
1248ede2 231 mi_out_data *data = ui_out_data (uiout);
102040f0 232
76fe6b98
AC
233 if (data->suppress_output)
234 return;
fb40c209
AC
235 field_separator (uiout);
236 if (fldname)
237 fprintf_unfiltered (data->buffer, "%s=", fldname);
238 fprintf_unfiltered (data->buffer, "\"");
239 if (string)
240 fputstr_unfiltered (string, '"', data->buffer);
241 fprintf_unfiltered (data->buffer, "\"");
242}
243
244/* This is the only field function that does not align */
245
246void
247mi_field_fmt (struct ui_out *uiout, int fldno,
248 int width, enum ui_align align,
e2e11a41
AC
249 const char *fldname,
250 const char *format,
251 va_list args)
fb40c209 252{
1248ede2 253 mi_out_data *data = ui_out_data (uiout);
102040f0 254
76fe6b98
AC
255 if (data->suppress_output)
256 return;
fb40c209
AC
257 field_separator (uiout);
258 if (fldname)
259 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
260 else
261 fputs_unfiltered ("\"", data->buffer);
262 vfprintf_unfiltered (data->buffer, format, args);
263 fputs_unfiltered ("\"", data->buffer);
264}
265
266void
fba45db2 267mi_spaces (struct ui_out *uiout, int numspaces)
fb40c209
AC
268{
269}
270
271void
e2e11a41 272mi_text (struct ui_out *uiout, const char *string)
fb40c209
AC
273{
274}
275
276void
e2e11a41
AC
277mi_message (struct ui_out *uiout, int verbosity,
278 const char *format,
279 va_list args)
fb40c209
AC
280{
281}
282
283void
fba45db2 284mi_wrap_hint (struct ui_out *uiout, char *identstring)
fb40c209
AC
285{
286 wrap_here (identstring);
287}
288
289void
fba45db2 290mi_flush (struct ui_out *uiout)
fb40c209 291{
1248ede2 292 mi_out_data *data = ui_out_data (uiout);
102040f0 293
fb40c209
AC
294 gdb_flush (data->buffer);
295}
296
297/* local functions */
298
fb40c209
AC
299/* access to ui_out format private members */
300
301static void
302field_separator (struct ui_out *uiout)
303{
1248ede2 304 mi_out_data *data = ui_out_data (uiout);
102040f0 305
59807497
AC
306 if (data->suppress_field_separator)
307 data->suppress_field_separator = 0;
fb40c209
AC
308 else
309 fputc_unfiltered (',', data->buffer);
310}
311
312static void
9a0f0643 313mi_open (struct ui_out *uiout,
d5e8ba62 314 const char *name,
9a0f0643 315 enum ui_out_type type)
fb40c209 316{
1248ede2 317 mi_out_data *data = ui_out_data (uiout);
102040f0 318
d5e8ba62 319 field_separator (uiout);
59807497 320 data->suppress_field_separator = 1;
d5e8ba62
AC
321 if (name)
322 fprintf_unfiltered (data->buffer, "%s=", name);
5a9aa5dc
AC
323 switch (type)
324 {
325 case ui_out_type_tuple:
326 fputc_unfiltered ('{', data->buffer);
327 break;
328 case ui_out_type_list:
da0f9dcd 329 fputc_unfiltered ('[', data->buffer);
5a9aa5dc
AC
330 break;
331 default:
e2e0b3e5 332 internal_error (__FILE__, __LINE__, _("bad switch"));
5a9aa5dc 333 }
fb40c209
AC
334}
335
336static void
9a0f0643
AC
337mi_close (struct ui_out *uiout,
338 enum ui_out_type type)
fb40c209 339{
1248ede2 340 mi_out_data *data = ui_out_data (uiout);
102040f0 341
5a9aa5dc
AC
342 switch (type)
343 {
344 case ui_out_type_tuple:
345 fputc_unfiltered ('}', data->buffer);
346 break;
347 case ui_out_type_list:
da0f9dcd 348 fputc_unfiltered (']', data->buffer);
5a9aa5dc
AC
349 break;
350 default:
e2e0b3e5 351 internal_error (__FILE__, __LINE__, _("bad switch"));
5a9aa5dc 352 }
59807497 353 data->suppress_field_separator = 0;
fb40c209
AC
354}
355
356/* add a string to the buffer */
357
358void
359mi_out_buffered (struct ui_out *uiout, char *string)
360{
1248ede2 361 mi_out_data *data = ui_out_data (uiout);
102040f0 362
fb40c209
AC
363 fprintf_unfiltered (data->buffer, "%s", string);
364}
365
366/* clear the buffer */
367
368void
369mi_out_rewind (struct ui_out *uiout)
370{
1248ede2 371 mi_out_data *data = ui_out_data (uiout);
102040f0 372
fb40c209
AC
373 ui_file_rewind (data->buffer);
374}
375
376/* dump the buffer onto the specified stream */
377
378static void
379do_write (void *data, const char *buffer, long length_buffer)
380{
381 ui_file_write (data, buffer, length_buffer);
382}
383
384void
385mi_out_put (struct ui_out *uiout,
386 struct ui_file *stream)
387{
1248ede2 388 mi_out_data *data = ui_out_data (uiout);
102040f0 389
fb40c209
AC
390 ui_file_put (data->buffer, do_write, stream);
391 ui_file_rewind (data->buffer);
392}
393
c7ec4050
AC
394/* Current MI version. */
395
396int
397mi_version (struct ui_out *uiout)
398{
1248ede2 399 mi_out_data *data = ui_out_data (uiout);
102040f0 400
c7ec4050
AC
401 return data->mi_version;
402}
403
fb40c209
AC
404/* initalize private members at startup */
405
406struct ui_out *
b30bf9ee 407mi_out_new (int mi_version)
fb40c209
AC
408{
409 int flags = 0;
102040f0 410
1248ede2 411 mi_out_data *data = XMALLOC (mi_out_data);
59807497 412 data->suppress_field_separator = 0;
bf702e7e 413 data->suppress_output = 0;
b30bf9ee 414 data->mi_version = mi_version;
fb40c209
AC
415 /* FIXME: This code should be using a ``string_file'' and not the
416 TUI buffer hack. */
417 data->buffer = mem_fileopen ();
418 return ui_out_new (&mi_ui_out_impl, data, flags);
419}
420
421/* standard gdb initialization hook */
422void
fba45db2 423_initialize_mi_out (void)
fb40c209
AC
424{
425 /* nothing happens here */
426}
This page took 0.880845 seconds and 4 git commands to generate.