Use std::string for ui_out_hdr's text fields
[deliverable/binutils-gdb.git] / gdb / mi / mi-out.c
1 /* MI Command Set - output generating routines.
2
3 Copyright (C) 2000-2016 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 #include <vector>
26
27 struct mi_ui_out_data
28 {
29 int suppress_field_separator;
30 int suppress_output;
31 int mi_version;
32 std::vector<ui_file *> streams;
33 };
34 typedef struct mi_ui_out_data mi_out_data;
35
36 /* These are the MI output functions */
37
38 static void mi_out_data_dtor (struct ui_out *ui_out);
39 static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
40 int nr_rows, const char *tblid);
41 static void mi_table_body (struct ui_out *uiout);
42 static void mi_table_end (struct ui_out *uiout);
43 static void mi_table_header (struct ui_out *uiout, int width,
44 enum ui_align alignment,
45 const std::string &col_name,
46 const std::string &col_hdr);
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,
56 const char *string);
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) ATTRIBUTE_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, const char *format, va_list args)
64 ATTRIBUTE_PRINTF (2, 0);
65 static void mi_wrap_hint (struct ui_out *uiout, const char *identstring);
66 static void mi_flush (struct ui_out *uiout);
67 static int mi_redirect (struct ui_out *uiout, struct ui_file *outstream);
68
69 /* This is the MI ui-out implementation functions vector */
70
71 static const struct ui_out_impl mi_ui_out_impl =
72 {
73 mi_table_begin,
74 mi_table_body,
75 mi_table_end,
76 mi_table_header,
77 mi_begin,
78 mi_end,
79 mi_field_int,
80 mi_field_skip,
81 mi_field_string,
82 mi_field_fmt,
83 mi_spaces,
84 mi_text,
85 mi_message,
86 mi_wrap_hint,
87 mi_flush,
88 mi_redirect,
89 mi_out_data_dtor,
90 1, /* Needs MI hacks. */
91 };
92
93 /* Prototypes for local functions */
94
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, ui_left, "nr_rows", nr_rows);
110 mi_field_int (uiout, -1, -1, ui_left, "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 = (mi_out_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 = (mi_out_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 std::string &col_name, const std::string &col_hdr)
145 {
146 mi_out_data *data = (mi_out_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, ui_center, "width", width);
153 mi_field_int (uiout, 0, 0, ui_center, "alignment", alignment);
154 mi_field_string (uiout, 0, 0, ui_center, "col_name", col_name.c_str ());
155 mi_field_string (uiout, 0, width, alignment, "colhdr", col_hdr.c_str ());
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 = (mi_out_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 = (mi_out_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 = (mi_out_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 = (mi_out_data *) ui_out_data (uiout);
218 struct ui_file *stream;
219
220 if (data->suppress_output)
221 return;
222
223 stream = data->streams.back ();
224 field_separator (uiout);
225 if (fldname)
226 fprintf_unfiltered (stream, "%s=", fldname);
227 fprintf_unfiltered (stream, "\"");
228 if (string)
229 fputstr_unfiltered (string, '"', stream);
230 fprintf_unfiltered (stream, "\"");
231 }
232
233 /* This is the only field function that does not align. */
234
235 void
236 mi_field_fmt (struct ui_out *uiout, int fldno, int width,
237 enum ui_align align, const char *fldname,
238 const char *format, va_list args)
239 {
240 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
241 struct ui_file *stream;
242
243 if (data->suppress_output)
244 return;
245
246 stream = data->streams.back ();
247 field_separator (uiout);
248 if (fldname)
249 fprintf_unfiltered (stream, "%s=\"", fldname);
250 else
251 fputs_unfiltered ("\"", stream);
252 vfprintf_unfiltered (stream, format, args);
253 fputs_unfiltered ("\"", stream);
254 }
255
256 void
257 mi_spaces (struct ui_out *uiout, int numspaces)
258 {
259 }
260
261 void
262 mi_text (struct ui_out *uiout, const char *string)
263 {
264 }
265
266 void
267 mi_message (struct ui_out *uiout, const char *format, va_list args)
268 {
269 }
270
271 void
272 mi_wrap_hint (struct ui_out *uiout, const char *identstring)
273 {
274 wrap_here (identstring);
275 }
276
277 void
278 mi_flush (struct ui_out *uiout)
279 {
280 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
281 struct ui_file *stream = data->streams.back ();
282
283 gdb_flush (stream);
284 }
285
286 int
287 mi_redirect (struct ui_out *uiout, struct ui_file *outstream)
288 {
289 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
290
291 if (outstream != NULL)
292 data->streams.push_back (outstream);
293 else
294 data->streams.pop_back ();
295
296 return 0;
297 }
298
299 /* local functions */
300
301 /* access to ui_out format private members */
302
303 static void
304 field_separator (struct ui_out *uiout)
305 {
306 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
307 ui_file *stream = data->streams.back ();
308
309 if (data->suppress_field_separator)
310 data->suppress_field_separator = 0;
311 else
312 fputc_unfiltered (',', stream);
313 }
314
315 static void
316 mi_open (struct ui_out *uiout, const char *name, enum ui_out_type type)
317 {
318 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
319 ui_file *stream = data->streams.back ();
320
321 field_separator (uiout);
322 data->suppress_field_separator = 1;
323 if (name)
324 fprintf_unfiltered (stream, "%s=", name);
325 switch (type)
326 {
327 case ui_out_type_tuple:
328 fputc_unfiltered ('{', stream);
329 break;
330 case ui_out_type_list:
331 fputc_unfiltered ('[', stream);
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 = (mi_out_data *) ui_out_data (uiout);
342 ui_file *stream = data->streams.back ();
343
344 switch (type)
345 {
346 case ui_out_type_tuple:
347 fputc_unfiltered ('}', stream);
348 break;
349 case ui_out_type_list:
350 fputc_unfiltered (']', stream);
351 break;
352 default:
353 internal_error (__FILE__, __LINE__, _("bad switch"));
354 }
355 data->suppress_field_separator = 0;
356 }
357
358 /* Clear the buffer. */
359
360 void
361 mi_out_rewind (struct ui_out *uiout)
362 {
363 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
364 ui_file *stream = data->streams.back ();
365
366 ui_file_rewind (stream);
367 }
368
369 /* Dump the buffer onto the specified stream. */
370
371 void
372 mi_out_put (struct ui_out *uiout, struct ui_file *stream)
373 {
374 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
375 ui_file *outstream = data->streams.back ();
376
377 ui_file_put (outstream, ui_file_write_for_put, stream);
378 ui_file_rewind (outstream);
379 }
380
381 /* Return the current MI version. */
382
383 int
384 mi_version (struct ui_out *uiout)
385 {
386 mi_out_data *data = (mi_out_data *) ui_out_data (uiout);
387
388 return data->mi_version;
389 }
390
391 /* Constructor for an `mi_out_data' object. */
392
393 static void
394 mi_out_data_ctor (mi_out_data *self, int mi_version, struct ui_file *stream)
395 {
396 gdb_assert (stream != NULL);
397
398 self->streams.push_back (stream);
399
400 self->suppress_field_separator = 0;
401 self->suppress_output = 0;
402 self->mi_version = mi_version;
403 }
404
405 /* The destructor. */
406
407 static void
408 mi_out_data_dtor (struct ui_out *ui_out)
409 {
410 mi_out_data *data = (mi_out_data *) ui_out_data (ui_out);
411
412 delete data;
413 }
414
415 /* Initialize private members at startup. */
416
417 struct ui_out *
418 mi_out_new (int mi_version)
419 {
420 int flags = 0;
421 mi_out_data *data = new mi_out_data ();
422 struct ui_file *stream = mem_fileopen ();
423
424 mi_out_data_ctor (data, mi_version, stream);
425 return ui_out_new (&mi_ui_out_impl, data, flags);
426 }
This page took 0.042755 seconds and 4 git commands to generate.