Fix for PR mi/15863
[deliverable/binutils-gdb.git] / gdb / mi / mi-out.c
1 /* MI Command Set - output generating routines.
2
3 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Solutions (a Red Hat company).
6
7 This file is part of GDB.
8
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 3 of the License, or
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "ui-out.h"
24 #include "mi-out.h"
25
26 struct ui_out_data
27 {
28 int suppress_field_separator;
29 int suppress_output;
30 int mi_version;
31 struct ui_file *buffer;
32 struct ui_file *original_buffer;
33 };
34 typedef struct ui_out_data mi_out_data;
35
36 /* These are the MI output functions */
37
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,
44 const char *colhdr);
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,
54 const char *string);
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,
58 va_list args) ATTRIBUTE_PRINTF (6, 0);
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 ATTRIBUTE_PRINTF (3, 0);
64 static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
65 static void mi_flush (struct ui_out *uiout);
66 static int mi_redirect (struct ui_out *uiout, struct ui_file *outstream);
67
68 /* This is the MI ui-out implementation functions vector */
69
70 static const struct ui_out_impl mi_ui_out_impl =
71 {
72 mi_table_begin,
73 mi_table_body,
74 mi_table_end,
75 mi_table_header,
76 mi_begin,
77 mi_end,
78 mi_field_int,
79 mi_field_skip,
80 mi_field_string,
81 mi_field_fmt,
82 mi_spaces,
83 mi_text,
84 mi_message,
85 mi_wrap_hint,
86 mi_flush,
87 mi_redirect,
88 0,
89 1, /* Needs MI hacks. */
90 };
91
92 /* Prototypes for local functions */
93
94 extern void _initialize_mi_out (void);
95 static void field_separator (struct ui_out *uiout);
96 static void mi_open (struct ui_out *uiout, const char *name,
97 enum ui_out_type type);
98 static void mi_close (struct ui_out *uiout, enum ui_out_type type);
99
100 /* Mark beginning of a table. */
101
102 void
103 mi_table_begin (struct ui_out *uiout,
104 int nr_cols,
105 int nr_rows,
106 const char *tblid)
107 {
108 mi_open (uiout, tblid, ui_out_type_tuple);
109 mi_field_int (uiout, -1, -1, -1, "nr_rows", nr_rows);
110 mi_field_int (uiout, -1, -1, -1, "nr_cols", nr_cols);
111 mi_open (uiout, "hdr", ui_out_type_list);
112 }
113
114 /* Mark beginning of a table body. */
115
116 void
117 mi_table_body (struct ui_out *uiout)
118 {
119 mi_out_data *data = ui_out_data (uiout);
120
121 if (data->suppress_output)
122 return;
123 /* close the table header line if there were any headers */
124 mi_close (uiout, ui_out_type_list);
125 mi_open (uiout, "body", ui_out_type_list);
126 }
127
128 /* Mark end of a table. */
129
130 void
131 mi_table_end (struct ui_out *uiout)
132 {
133 mi_out_data *data = ui_out_data (uiout);
134
135 data->suppress_output = 0;
136 mi_close (uiout, ui_out_type_list); /* body */
137 mi_close (uiout, ui_out_type_tuple);
138 }
139
140 /* Specify table header. */
141
142 void
143 mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
144 const char *col_name, const char *colhdr)
145 {
146 mi_out_data *data = ui_out_data (uiout);
147
148 if (data->suppress_output)
149 return;
150
151 mi_open (uiout, NULL, ui_out_type_tuple);
152 mi_field_int (uiout, 0, 0, 0, "width", width);
153 mi_field_int (uiout, 0, 0, 0, "alignment", alignment);
154 mi_field_string (uiout, 0, 0, 0, "col_name", col_name);
155 mi_field_string (uiout, 0, width, alignment, "colhdr", colhdr);
156 mi_close (uiout, ui_out_type_tuple);
157 }
158
159 /* Mark beginning of a list. */
160
161 void
162 mi_begin (struct ui_out *uiout, enum ui_out_type type, int level,
163 const char *id)
164 {
165 mi_out_data *data = ui_out_data (uiout);
166
167 if (data->suppress_output)
168 return;
169
170 mi_open (uiout, id, type);
171 }
172
173 /* Mark end of a list. */
174
175 void
176 mi_end (struct ui_out *uiout, enum ui_out_type type, int level)
177 {
178 mi_out_data *data = ui_out_data (uiout);
179
180 if (data->suppress_output)
181 return;
182
183 mi_close (uiout, type);
184 }
185
186 /* Output an int field. */
187
188 static void
189 mi_field_int (struct ui_out *uiout, int fldno, int width,
190 enum ui_align alignment, const char *fldname, int value)
191 {
192 char buffer[20]; /* FIXME: how many chars long a %d can become? */
193 mi_out_data *data = ui_out_data (uiout);
194
195 if (data->suppress_output)
196 return;
197
198 xsnprintf (buffer, sizeof (buffer), "%d", value);
199 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
200 }
201
202 /* Used to omit a field. */
203
204 void
205 mi_field_skip (struct ui_out *uiout, int fldno, int width,
206 enum ui_align alignment, const char *fldname)
207 {
208 }
209
210 /* Other specific mi_field_* end up here so alignment and field
211 separators are both handled by mi_field_string. */
212
213 void
214 mi_field_string (struct ui_out *uiout, int fldno, int width,
215 enum ui_align align, const char *fldname, const char *string)
216 {
217 mi_out_data *data = ui_out_data (uiout);
218
219 if (data->suppress_output)
220 return;
221
222 field_separator (uiout);
223 if (fldname)
224 fprintf_unfiltered (data->buffer, "%s=", fldname);
225 fprintf_unfiltered (data->buffer, "\"");
226 if (string)
227 fputstr_unfiltered (string, '"', data->buffer);
228 fprintf_unfiltered (data->buffer, "\"");
229 }
230
231 /* This is the only field function that does not align. */
232
233 void
234 mi_field_fmt (struct ui_out *uiout, int fldno, int width,
235 enum ui_align align, const char *fldname,
236 const char *format, va_list args)
237 {
238 mi_out_data *data = ui_out_data (uiout);
239
240 if (data->suppress_output)
241 return;
242
243 field_separator (uiout);
244 if (fldname)
245 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
246 else
247 fputs_unfiltered ("\"", data->buffer);
248 vfprintf_unfiltered (data->buffer, format, args);
249 fputs_unfiltered ("\"", data->buffer);
250 }
251
252 void
253 mi_spaces (struct ui_out *uiout, int numspaces)
254 {
255 }
256
257 void
258 mi_text (struct ui_out *uiout, const char *string)
259 {
260 }
261
262 void
263 mi_message (struct ui_out *uiout, int verbosity,
264 const char *format, va_list args)
265 {
266 }
267
268 void
269 mi_wrap_hint (struct ui_out *uiout, char *identstring)
270 {
271 wrap_here (identstring);
272 }
273
274 void
275 mi_flush (struct ui_out *uiout)
276 {
277 mi_out_data *data = ui_out_data (uiout);
278
279 gdb_flush (data->buffer);
280 }
281
282 int
283 mi_redirect (struct ui_out *uiout, struct ui_file *outstream)
284 {
285 mi_out_data *data = ui_out_data (uiout);
286
287 if (outstream != NULL)
288 {
289 data->original_buffer = data->buffer;
290 data->buffer = outstream;
291 }
292 else if (data->original_buffer != NULL)
293 {
294 data->buffer = data->original_buffer;
295 data->original_buffer = NULL;
296 }
297
298 return 0;
299 }
300
301 /* local functions */
302
303 /* access to ui_out format private members */
304
305 static void
306 field_separator (struct ui_out *uiout)
307 {
308 mi_out_data *data = ui_out_data (uiout);
309
310 if (data->suppress_field_separator)
311 data->suppress_field_separator = 0;
312 else
313 fputc_unfiltered (',', data->buffer);
314 }
315
316 static void
317 mi_open (struct ui_out *uiout, const char *name, enum ui_out_type type)
318 {
319 mi_out_data *data = ui_out_data (uiout);
320
321 field_separator (uiout);
322 data->suppress_field_separator = 1;
323 if (name)
324 fprintf_unfiltered (data->buffer, "%s=", name);
325 switch (type)
326 {
327 case ui_out_type_tuple:
328 fputc_unfiltered ('{', data->buffer);
329 break;
330 case ui_out_type_list:
331 fputc_unfiltered ('[', data->buffer);
332 break;
333 default:
334 internal_error (__FILE__, __LINE__, _("bad switch"));
335 }
336 }
337
338 static void
339 mi_close (struct ui_out *uiout, enum ui_out_type type)
340 {
341 mi_out_data *data = ui_out_data (uiout);
342
343 switch (type)
344 {
345 case ui_out_type_tuple:
346 fputc_unfiltered ('}', data->buffer);
347 break;
348 case ui_out_type_list:
349 fputc_unfiltered (']', data->buffer);
350 break;
351 default:
352 internal_error (__FILE__, __LINE__, _("bad switch"));
353 }
354 data->suppress_field_separator = 0;
355 }
356
357 /* Add a string to the buffer. */
358
359 void
360 mi_out_buffered (struct ui_out *uiout, char *string)
361 {
362 mi_out_data *data = ui_out_data (uiout);
363
364 fprintf_unfiltered (data->buffer, "%s", string);
365 }
366
367 /* Clear the buffer. */
368
369 void
370 mi_out_rewind (struct ui_out *uiout)
371 {
372 mi_out_data *data = ui_out_data (uiout);
373
374 ui_file_rewind (data->buffer);
375 }
376
377 /* Dump the buffer onto the specified stream. */
378
379 static void
380 do_write (void *data, const char *buffer, long length_buffer)
381 {
382 ui_file_write (data, buffer, length_buffer);
383 }
384
385 void
386 mi_out_put (struct ui_out *uiout, struct ui_file *stream)
387 {
388 mi_out_data *data = ui_out_data (uiout);
389
390 ui_file_put (data->buffer, do_write, stream);
391 ui_file_rewind (data->buffer);
392 }
393
394 /* Return the current MI version. */
395
396 int
397 mi_version (struct ui_out *uiout)
398 {
399 mi_out_data *data = ui_out_data (uiout);
400
401 return data->mi_version;
402 }
403
404 /* Initialize private members at startup. */
405
406 struct ui_out *
407 mi_out_new (int mi_version)
408 {
409 int flags = 0;
410
411 mi_out_data *data = XNEW (mi_out_data);
412 data->suppress_field_separator = 0;
413 data->suppress_output = 0;
414 data->mi_version = mi_version;
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 }
This page took 0.060832 seconds and 4 git commands to generate.