Convert to C90
[deliverable/binutils-gdb.git] / gdb / mi / mi-out.c
CommitLineData
fb40c209 1/* MI Command Set - output generating routines.
349c5d5f 2
1248ede2 3 Copyright 2000, 2002, 2003 Free Software Foundation, Inc.
349c5d5f 4
ab91fdd5 5 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
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 2 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, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24#include "defs.h"
25#include "ui-out.h"
26#include "mi-out.h"
27
fb40c209
AC
28struct ui_out_data
29 {
59807497 30 int suppress_field_separator;
76fe6b98 31 int suppress_output;
b30bf9ee 32 int mi_version;
fb40c209
AC
33 struct ui_file *buffer;
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
AC
58 const char *fldname, const char *format,
59 va_list args);
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,
63 const char *format, va_list args);
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
AC
88 mi_flush,
89 1, /* Needs MI hacks. */
fb40c209
AC
90};
91
92/* Prototypes for local functions */
93
a14ed312 94extern void _initialize_mi_out (void);
fb40c209 95static void field_separator (struct ui_out *uiout);
d5e8ba62
AC
96static void mi_open (struct ui_out *uiout, const char *name,
97 enum ui_out_type type);
9a0f0643 98static void mi_close (struct ui_out *uiout, enum ui_out_type type);
fb40c209
AC
99
100static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
101 char *format,...);
102
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{
1248ede2 111 mi_out_data *data = ui_out_data (uiout);
d5e8ba62 112 mi_open (uiout, tblid, ui_out_type_tuple);
cff22675
AC
113 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
114 "nr_rows", nr_rows);
115 mi_field_int (uiout, -1/*fldno*/, -1/*width*/, -1/*alin*/,
116 "nr_cols", nr_cols);
117 mi_open (uiout, "hdr", ui_out_type_list);
fb40c209
AC
118}
119
120/* Mark beginning of a table body */
121
122void
fba45db2 123mi_table_body (struct ui_out *uiout)
fb40c209 124{
1248ede2 125 mi_out_data *data = ui_out_data (uiout);
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);
76fe6b98 139 data->suppress_output = 0;
cff22675 140 mi_close (uiout, ui_out_type_list); /* body */
666547aa 141 mi_close (uiout, ui_out_type_tuple);
fb40c209
AC
142}
143
144/* Specify table header */
145
146void
46712191 147mi_table_header (struct ui_out *uiout, int width, enum ui_align alignment,
b25959ec 148 const char *col_name,
e2e11a41 149 const char *colhdr)
fb40c209 150{
1248ede2 151 mi_out_data *data = ui_out_data (uiout);
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);
76fe6b98
AC
171 if (data->suppress_output)
172 return;
d5e8ba62 173 mi_open (uiout, id, type);
fb40c209
AC
174}
175
176/* Mark end of a list */
177
178void
631ec795
AC
179mi_end (struct ui_out *uiout,
180 enum ui_out_type type,
181 int level)
fb40c209 182{
1248ede2 183 mi_out_data *data = ui_out_data (uiout);
76fe6b98
AC
184 if (data->suppress_output)
185 return;
9a0f0643 186 mi_close (uiout, type);
fb40c209
AC
187}
188
189/* output an int field */
190
191void
46712191
KB
192mi_field_int (struct ui_out *uiout, int fldno, int width,
193 enum ui_align alignment, const char *fldname, int value)
fb40c209
AC
194{
195 char buffer[20]; /* FIXME: how many chars long a %d can become? */
1248ede2 196 mi_out_data *data = ui_out_data (uiout);
76fe6b98
AC
197 if (data->suppress_output)
198 return;
fb40c209
AC
199
200 sprintf (buffer, "%d", value);
201 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
202}
203
204/* used to ommit a field */
205
206void
46712191
KB
207mi_field_skip (struct ui_out *uiout, int fldno, int width,
208 enum ui_align alignment, const char *fldname)
fb40c209 209{
1248ede2 210 mi_out_data *data = ui_out_data (uiout);
76fe6b98
AC
211 if (data->suppress_output)
212 return;
fb40c209
AC
213 mi_field_string (uiout, fldno, width, alignment, fldname, "");
214}
215
216/* other specific mi_field_* end up here so alignment and field
217 separators are both handled by mi_field_string */
218
219void
220mi_field_string (struct ui_out *uiout,
221 int fldno,
222 int width,
46712191 223 enum ui_align align,
e2e11a41 224 const char *fldname,
fb40c209
AC
225 const char *string)
226{
1248ede2 227 mi_out_data *data = ui_out_data (uiout);
76fe6b98
AC
228 if (data->suppress_output)
229 return;
fb40c209
AC
230 field_separator (uiout);
231 if (fldname)
232 fprintf_unfiltered (data->buffer, "%s=", fldname);
233 fprintf_unfiltered (data->buffer, "\"");
234 if (string)
235 fputstr_unfiltered (string, '"', data->buffer);
236 fprintf_unfiltered (data->buffer, "\"");
237}
238
239/* This is the only field function that does not align */
240
241void
242mi_field_fmt (struct ui_out *uiout, int fldno,
243 int width, enum ui_align align,
e2e11a41
AC
244 const char *fldname,
245 const char *format,
246 va_list args)
fb40c209 247{
1248ede2 248 mi_out_data *data = ui_out_data (uiout);
76fe6b98
AC
249 if (data->suppress_output)
250 return;
fb40c209
AC
251 field_separator (uiout);
252 if (fldname)
253 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
254 else
255 fputs_unfiltered ("\"", data->buffer);
256 vfprintf_unfiltered (data->buffer, format, args);
257 fputs_unfiltered ("\"", data->buffer);
258}
259
260void
fba45db2 261mi_spaces (struct ui_out *uiout, int numspaces)
fb40c209
AC
262{
263}
264
265void
e2e11a41 266mi_text (struct ui_out *uiout, const char *string)
fb40c209
AC
267{
268}
269
270void
e2e11a41
AC
271mi_message (struct ui_out *uiout, int verbosity,
272 const char *format,
273 va_list args)
fb40c209
AC
274{
275}
276
277void
fba45db2 278mi_wrap_hint (struct ui_out *uiout, char *identstring)
fb40c209
AC
279{
280 wrap_here (identstring);
281}
282
283void
fba45db2 284mi_flush (struct ui_out *uiout)
fb40c209 285{
1248ede2 286 mi_out_data *data = ui_out_data (uiout);
fb40c209
AC
287 gdb_flush (data->buffer);
288}
289
290/* local functions */
291
292/* Like mi_field_fmt, but takes a variable number of args
293 and makes a va_list and does not insert a separator */
294
295/* VARARGS */
296static void
297out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
298 char *format,...)
299{
1248ede2 300 mi_out_data *data = ui_out_data (uiout);
fb40c209
AC
301 va_list args;
302
303 field_separator (uiout);
304 if (fldname)
305 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
306 else
307 fputs_unfiltered ("\"", data->buffer);
308
309 va_start (args, format);
310 vfprintf_unfiltered (data->buffer, format, args);
311
312 fputs_unfiltered ("\"", data->buffer);
313
314 va_end (args);
315}
316
317/* access to ui_out format private members */
318
319static void
320field_separator (struct ui_out *uiout)
321{
1248ede2 322 mi_out_data *data = ui_out_data (uiout);
59807497
AC
323 if (data->suppress_field_separator)
324 data->suppress_field_separator = 0;
fb40c209
AC
325 else
326 fputc_unfiltered (',', data->buffer);
327}
328
329static void
9a0f0643 330mi_open (struct ui_out *uiout,
d5e8ba62 331 const char *name,
9a0f0643 332 enum ui_out_type type)
fb40c209 333{
1248ede2 334 mi_out_data *data = ui_out_data (uiout);
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:
348 internal_error (__FILE__, __LINE__, "bad switch");
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);
5a9aa5dc
AC
357 switch (type)
358 {
359 case ui_out_type_tuple:
360 fputc_unfiltered ('}', data->buffer);
361 break;
362 case ui_out_type_list:
da0f9dcd 363 fputc_unfiltered (']', data->buffer);
5a9aa5dc
AC
364 break;
365 default:
366 internal_error (__FILE__, __LINE__, "bad switch");
367 }
59807497 368 data->suppress_field_separator = 0;
fb40c209
AC
369}
370
371/* add a string to the buffer */
372
373void
374mi_out_buffered (struct ui_out *uiout, char *string)
375{
1248ede2 376 mi_out_data *data = ui_out_data (uiout);
fb40c209
AC
377 fprintf_unfiltered (data->buffer, "%s", string);
378}
379
380/* clear the buffer */
381
382void
383mi_out_rewind (struct ui_out *uiout)
384{
1248ede2 385 mi_out_data *data = ui_out_data (uiout);
fb40c209
AC
386 ui_file_rewind (data->buffer);
387}
388
389/* dump the buffer onto the specified stream */
390
391static void
392do_write (void *data, const char *buffer, long length_buffer)
393{
394 ui_file_write (data, buffer, length_buffer);
395}
396
397void
398mi_out_put (struct ui_out *uiout,
399 struct ui_file *stream)
400{
1248ede2 401 mi_out_data *data = ui_out_data (uiout);
fb40c209
AC
402 ui_file_put (data->buffer, do_write, stream);
403 ui_file_rewind (data->buffer);
404}
405
c7ec4050
AC
406/* Current MI version. */
407
408int
409mi_version (struct ui_out *uiout)
410{
1248ede2 411 mi_out_data *data = ui_out_data (uiout);
c7ec4050
AC
412 return data->mi_version;
413}
414
fb40c209
AC
415/* initalize private members at startup */
416
417struct ui_out *
b30bf9ee 418mi_out_new (int mi_version)
fb40c209
AC
419{
420 int flags = 0;
1248ede2 421 mi_out_data *data = XMALLOC (mi_out_data);
59807497 422 data->suppress_field_separator = 0;
bf702e7e 423 data->suppress_output = 0;
b30bf9ee 424 data->mi_version = mi_version;
fb40c209
AC
425 /* FIXME: This code should be using a ``string_file'' and not the
426 TUI buffer hack. */
427 data->buffer = mem_fileopen ();
428 return ui_out_new (&mi_ui_out_impl, data, flags);
429}
430
431/* standard gdb initialization hook */
432void
fba45db2 433_initialize_mi_out (void)
fb40c209
AC
434{
435 /* nothing happens here */
436}
This page took 0.293456 seconds and 4 git commands to generate.