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