2001-06-11 H.J. Lu <hjl@gnu.org>
[deliverable/binutils-gdb.git] / gdb / mi / mi-out.c
CommitLineData
fb40c209 1/* MI Command Set - output generating routines.
b6ba6518 2 Copyright 2000 Free Software Foundation, Inc.
ab91fdd5 3 Contributed by Cygnus Solutions (a Red Hat company).
fb40c209
AC
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#include "defs.h"
23#include "ui-out.h"
24#include "mi-out.h"
25
26/* Convenience macro for allocting typesafe memory. */
27
28#ifndef XMALLOC
29#define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE))
30#endif
31
32struct ui_out_data
33 {
34 int supress_field_separator;
35 int first_header;
36 struct ui_file *buffer;
37 };
38
39/* These are the MI output functions */
40
e2e11a41
AC
41static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
42 const char *tblid);
fb40c209
AC
43static void mi_table_body (struct ui_out *uiout);
44static void mi_table_end (struct ui_out *uiout);
45static void mi_table_header (struct ui_out *uiout, int width,
e2e11a41
AC
46 enum ui_align alig,
47 const char *colhdr);
631ec795
AC
48static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
49 int level, const char *id);
50static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
fb40c209 51static void mi_field_int (struct ui_out *uiout, int fldno, int width,
e2e11a41 52 enum ui_align alig, const char *fldname, int value);
fb40c209 53static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
e2e11a41 54 enum ui_align alig, const char *fldname);
fb40c209 55static void mi_field_string (struct ui_out *uiout, int fldno, int width,
e2e11a41 56 enum ui_align alig, const char *fldname,
fb40c209
AC
57 const char *string);
58static void mi_field_fmt (struct ui_out *uiout, int fldno,
59 int width, enum ui_align align,
e2e11a41
AC
60 const char *fldname, const char *format,
61 va_list args);
fb40c209 62static void mi_spaces (struct ui_out *uiout, int numspaces);
e2e11a41
AC
63static void mi_text (struct ui_out *uiout, const char *string);
64static void mi_message (struct ui_out *uiout, int verbosity,
65 const char *format, va_list args);
fb40c209
AC
66static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
67static void mi_flush (struct ui_out *uiout);
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,
90 mi_flush
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
AC
100
101static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
102 char *format,...);
103
104/* Mark beginning of a table */
105
106void
e2e11a41
AC
107mi_table_begin (struct ui_out *uiout, int nbrofcols,
108 const char *tblid)
fb40c209
AC
109{
110 struct ui_out_data *data = ui_out_data (uiout);
d5e8ba62 111 mi_open (uiout, tblid, ui_out_type_tuple);
fb40c209 112 data->first_header = 0;
fb40c209
AC
113}
114
115/* Mark beginning of a table body */
116
117void
fba45db2 118mi_table_body (struct ui_out *uiout)
fb40c209
AC
119{
120 struct ui_out_data *data = ui_out_data (uiout);
121 /* close the table header line if there were any headers */
122 if (data->first_header)
666547aa 123 mi_close (uiout, ui_out_type_tuple);
fb40c209
AC
124}
125
126/* Mark end of a table */
127
128void
fba45db2 129mi_table_end (struct ui_out *uiout)
fb40c209
AC
130{
131 struct ui_out_data *data = ui_out_data (uiout);
666547aa 132 mi_close (uiout, ui_out_type_tuple);
fb40c209
AC
133}
134
135/* Specify table header */
136
137void
e2e11a41
AC
138mi_table_header (struct ui_out *uiout, int width, int alignment,
139 const char *colhdr)
fb40c209
AC
140{
141 struct ui_out_data *data = ui_out_data (uiout);
142 if (!data->first_header++)
143 {
d5e8ba62 144 mi_open (uiout, "hdr", ui_out_type_tuple);
fb40c209
AC
145 }
146 mi_field_string (uiout, 0, width, alignment, 0, colhdr);
147}
148
149/* Mark beginning of a list */
150
151void
631ec795
AC
152mi_begin (struct ui_out *uiout,
153 enum ui_out_type type,
154 int level,
9a0f0643 155 const char *id)
fb40c209
AC
156{
157 struct ui_out_data *data = ui_out_data (uiout);
d5e8ba62 158 mi_open (uiout, id, type);
fb40c209
AC
159}
160
161/* Mark end of a list */
162
163void
631ec795
AC
164mi_end (struct ui_out *uiout,
165 enum ui_out_type type,
166 int level)
fb40c209
AC
167{
168 struct ui_out_data *data = ui_out_data (uiout);
9a0f0643 169 mi_close (uiout, type);
fb40c209
AC
170}
171
172/* output an int field */
173
174void
fba45db2 175mi_field_int (struct ui_out *uiout, int fldno, int width, int alignment,
e2e11a41 176 const char *fldname, int value)
fb40c209
AC
177{
178 char buffer[20]; /* FIXME: how many chars long a %d can become? */
179
180 sprintf (buffer, "%d", value);
181 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
182}
183
184/* used to ommit a field */
185
186void
fba45db2 187mi_field_skip (struct ui_out *uiout, int fldno, int width, int alignment,
e2e11a41 188 const char *fldname)
fb40c209
AC
189{
190 mi_field_string (uiout, fldno, width, alignment, fldname, "");
191}
192
193/* other specific mi_field_* end up here so alignment and field
194 separators are both handled by mi_field_string */
195
196void
197mi_field_string (struct ui_out *uiout,
198 int fldno,
199 int width,
200 int align,
e2e11a41 201 const char *fldname,
fb40c209
AC
202 const char *string)
203{
204 struct ui_out_data *data = ui_out_data (uiout);
205 field_separator (uiout);
206 if (fldname)
207 fprintf_unfiltered (data->buffer, "%s=", fldname);
208 fprintf_unfiltered (data->buffer, "\"");
209 if (string)
210 fputstr_unfiltered (string, '"', data->buffer);
211 fprintf_unfiltered (data->buffer, "\"");
212}
213
214/* This is the only field function that does not align */
215
216void
217mi_field_fmt (struct ui_out *uiout, int fldno,
218 int width, enum ui_align align,
e2e11a41
AC
219 const char *fldname,
220 const char *format,
221 va_list args)
fb40c209
AC
222{
223 struct ui_out_data *data = ui_out_data (uiout);
224 field_separator (uiout);
225 if (fldname)
226 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
227 else
228 fputs_unfiltered ("\"", data->buffer);
229 vfprintf_unfiltered (data->buffer, format, args);
230 fputs_unfiltered ("\"", data->buffer);
231}
232
233void
fba45db2 234mi_spaces (struct ui_out *uiout, int numspaces)
fb40c209
AC
235{
236}
237
238void
e2e11a41 239mi_text (struct ui_out *uiout, const char *string)
fb40c209
AC
240{
241}
242
243void
e2e11a41
AC
244mi_message (struct ui_out *uiout, int verbosity,
245 const char *format,
246 va_list args)
fb40c209
AC
247{
248}
249
250void
fba45db2 251mi_wrap_hint (struct ui_out *uiout, char *identstring)
fb40c209
AC
252{
253 wrap_here (identstring);
254}
255
256void
fba45db2 257mi_flush (struct ui_out *uiout)
fb40c209
AC
258{
259 struct ui_out_data *data = ui_out_data (uiout);
260 gdb_flush (data->buffer);
261}
262
263/* local functions */
264
265/* Like mi_field_fmt, but takes a variable number of args
266 and makes a va_list and does not insert a separator */
267
268/* VARARGS */
269static void
270out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
271 char *format,...)
272{
273 struct ui_out_data *data = ui_out_data (uiout);
274 va_list args;
275
276 field_separator (uiout);
277 if (fldname)
278 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
279 else
280 fputs_unfiltered ("\"", data->buffer);
281
282 va_start (args, format);
283 vfprintf_unfiltered (data->buffer, format, args);
284
285 fputs_unfiltered ("\"", data->buffer);
286
287 va_end (args);
288}
289
290/* access to ui_out format private members */
291
292static void
293field_separator (struct ui_out *uiout)
294{
295 struct ui_out_data *data = ui_out_data (uiout);
296 if (data->supress_field_separator)
297 data->supress_field_separator = 0;
298 else
299 fputc_unfiltered (',', data->buffer);
300}
301
302static void
9a0f0643 303mi_open (struct ui_out *uiout,
d5e8ba62 304 const char *name,
9a0f0643 305 enum ui_out_type type)
fb40c209
AC
306{
307 struct ui_out_data *data = ui_out_data (uiout);
d5e8ba62
AC
308 field_separator (uiout);
309 data->supress_field_separator = 1;
310 if (name)
311 fprintf_unfiltered (data->buffer, "%s=", name);
5a9aa5dc
AC
312 switch (type)
313 {
314 case ui_out_type_tuple:
315 fputc_unfiltered ('{', data->buffer);
316 break;
317 case ui_out_type_list:
318 fputc_unfiltered ('[', data->buffer);
319 break;
320 default:
321 internal_error (__FILE__, __LINE__, "bad switch");
322 }
fb40c209
AC
323}
324
325static void
9a0f0643
AC
326mi_close (struct ui_out *uiout,
327 enum ui_out_type type)
fb40c209
AC
328{
329 struct ui_out_data *data = ui_out_data (uiout);
5a9aa5dc
AC
330 switch (type)
331 {
332 case ui_out_type_tuple:
333 fputc_unfiltered ('}', data->buffer);
334 break;
335 case ui_out_type_list:
336 fputc_unfiltered (']', data->buffer);
337 break;
338 default:
339 internal_error (__FILE__, __LINE__, "bad switch");
340 }
d5e8ba62 341 data->supress_field_separator = 0;
fb40c209
AC
342}
343
344/* add a string to the buffer */
345
346void
347mi_out_buffered (struct ui_out *uiout, char *string)
348{
349 struct ui_out_data *data = ui_out_data (uiout);
350 fprintf_unfiltered (data->buffer, "%s", string);
351}
352
353/* clear the buffer */
354
355void
356mi_out_rewind (struct ui_out *uiout)
357{
358 struct ui_out_data *data = ui_out_data (uiout);
359 ui_file_rewind (data->buffer);
360}
361
362/* dump the buffer onto the specified stream */
363
364static void
365do_write (void *data, const char *buffer, long length_buffer)
366{
367 ui_file_write (data, buffer, length_buffer);
368}
369
370void
371mi_out_put (struct ui_out *uiout,
372 struct ui_file *stream)
373{
374 struct ui_out_data *data = ui_out_data (uiout);
375 ui_file_put (data->buffer, do_write, stream);
376 ui_file_rewind (data->buffer);
377}
378
379/* initalize private members at startup */
380
381struct ui_out *
382mi_out_new (void)
383{
384 int flags = 0;
385 struct ui_out_data *data = XMALLOC (struct ui_out_data);
386 data->supress_field_separator = 0;
387 /* FIXME: This code should be using a ``string_file'' and not the
388 TUI buffer hack. */
389 data->buffer = mem_fileopen ();
390 return ui_out_new (&mi_ui_out_impl, data, flags);
391}
392
393/* standard gdb initialization hook */
394void
fba45db2 395_initialize_mi_out (void)
fb40c209
AC
396{
397 /* nothing happens here */
398}
This page took 0.11398 seconds and 4 git commands to generate.