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