6787b528267909807e1f5b4ce1f973ac7a5865d0
[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 suppress_output;
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 int nr_rows, 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, const char *col_name,
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 int nr_rows,
110 const char *tblid)
111 {
112 struct ui_out_data *data = ui_out_data (uiout);
113 mi_open (uiout, tblid, ui_out_type_tuple);
114 if (nr_rows == 0)
115 {
116 data->suppress_output = 1;
117 return;
118 }
119 mi_open (uiout, "hdr", ui_out_type_tuple);
120 }
121
122 /* Mark beginning of a table body */
123
124 void
125 mi_table_body (struct ui_out *uiout)
126 {
127 struct ui_out_data *data = ui_out_data (uiout);
128 /* close the table header line if there were any headers */
129 if (data->suppress_output)
130 return;
131 mi_close (uiout, ui_out_type_tuple);
132 }
133
134 /* Mark end of a table */
135
136 void
137 mi_table_end (struct ui_out *uiout)
138 {
139 struct ui_out_data *data = ui_out_data (uiout);
140 data->suppress_output = 0;
141 mi_close (uiout, ui_out_type_tuple);
142 }
143
144 /* Specify table header */
145
146 void
147 mi_table_header (struct ui_out *uiout, int width, int alignment,
148 const char *col_name,
149 const char *colhdr)
150 {
151 struct ui_out_data *data = ui_out_data (uiout);
152 if (data->suppress_output)
153 return;
154 mi_field_string (uiout, 0, width, alignment, 0, colhdr);
155 }
156
157 /* Mark beginning of a list */
158
159 void
160 mi_begin (struct ui_out *uiout,
161 enum ui_out_type type,
162 int level,
163 const char *id)
164 {
165 struct ui_out_data *data = ui_out_data (uiout);
166 if (data->suppress_output)
167 return;
168 mi_open (uiout, id, type);
169 }
170
171 /* Mark end of a list */
172
173 void
174 mi_end (struct ui_out *uiout,
175 enum ui_out_type type,
176 int level)
177 {
178 struct ui_out_data *data = ui_out_data (uiout);
179 if (data->suppress_output)
180 return;
181 mi_close (uiout, type);
182 }
183
184 /* output an int field */
185
186 void
187 mi_field_int (struct ui_out *uiout, int fldno, int width, int alignment,
188 const char *fldname, int value)
189 {
190 char buffer[20]; /* FIXME: how many chars long a %d can become? */
191 struct ui_out_data *data = ui_out_data (uiout);
192 if (data->suppress_output)
193 return;
194
195 sprintf (buffer, "%d", value);
196 mi_field_string (uiout, fldno, width, alignment, fldname, buffer);
197 }
198
199 /* used to ommit a field */
200
201 void
202 mi_field_skip (struct ui_out *uiout, int fldno, int width, int alignment,
203 const char *fldname)
204 {
205 struct ui_out_data *data = ui_out_data (uiout);
206 if (data->suppress_output)
207 return;
208 mi_field_string (uiout, fldno, width, alignment, fldname, "");
209 }
210
211 /* other specific mi_field_* end up here so alignment and field
212 separators are both handled by mi_field_string */
213
214 void
215 mi_field_string (struct ui_out *uiout,
216 int fldno,
217 int width,
218 int align,
219 const char *fldname,
220 const char *string)
221 {
222 struct ui_out_data *data = ui_out_data (uiout);
223 if (data->suppress_output)
224 return;
225 field_separator (uiout);
226 if (fldname)
227 fprintf_unfiltered (data->buffer, "%s=", fldname);
228 fprintf_unfiltered (data->buffer, "\"");
229 if (string)
230 fputstr_unfiltered (string, '"', data->buffer);
231 fprintf_unfiltered (data->buffer, "\"");
232 }
233
234 /* This is the only field function that does not align */
235
236 void
237 mi_field_fmt (struct ui_out *uiout, int fldno,
238 int width, enum ui_align align,
239 const char *fldname,
240 const char *format,
241 va_list args)
242 {
243 struct ui_out_data *data = ui_out_data (uiout);
244 if (data->suppress_output)
245 return;
246 field_separator (uiout);
247 if (fldname)
248 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
249 else
250 fputs_unfiltered ("\"", data->buffer);
251 vfprintf_unfiltered (data->buffer, format, args);
252 fputs_unfiltered ("\"", data->buffer);
253 }
254
255 void
256 mi_spaces (struct ui_out *uiout, int numspaces)
257 {
258 }
259
260 void
261 mi_text (struct ui_out *uiout, const char *string)
262 {
263 }
264
265 void
266 mi_message (struct ui_out *uiout, int verbosity,
267 const char *format,
268 va_list args)
269 {
270 }
271
272 void
273 mi_wrap_hint (struct ui_out *uiout, char *identstring)
274 {
275 wrap_here (identstring);
276 }
277
278 void
279 mi_flush (struct ui_out *uiout)
280 {
281 struct ui_out_data *data = ui_out_data (uiout);
282 gdb_flush (data->buffer);
283 }
284
285 /* local functions */
286
287 /* Like mi_field_fmt, but takes a variable number of args
288 and makes a va_list and does not insert a separator */
289
290 /* VARARGS */
291 static void
292 out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
293 char *format,...)
294 {
295 struct ui_out_data *data = ui_out_data (uiout);
296 va_list args;
297
298 field_separator (uiout);
299 if (fldname)
300 fprintf_unfiltered (data->buffer, "%s=\"", fldname);
301 else
302 fputs_unfiltered ("\"", data->buffer);
303
304 va_start (args, format);
305 vfprintf_unfiltered (data->buffer, format, args);
306
307 fputs_unfiltered ("\"", data->buffer);
308
309 va_end (args);
310 }
311
312 /* access to ui_out format private members */
313
314 static void
315 field_separator (struct ui_out *uiout)
316 {
317 struct ui_out_data *data = ui_out_data (uiout);
318 if (data->suppress_field_separator)
319 data->suppress_field_separator = 0;
320 else
321 fputc_unfiltered (',', data->buffer);
322 }
323
324 static void
325 mi_open (struct ui_out *uiout,
326 const char *name,
327 enum ui_out_type type)
328 {
329 struct ui_out_data *data = ui_out_data (uiout);
330 field_separator (uiout);
331 data->suppress_field_separator = 1;
332 if (name)
333 fprintf_unfiltered (data->buffer, "%s=", name);
334 switch (type)
335 {
336 case ui_out_type_tuple:
337 fputc_unfiltered ('{', data->buffer);
338 break;
339 case ui_out_type_list:
340 if (data->mi_version > 0)
341 fputc_unfiltered ('[', data->buffer);
342 else
343 fputc_unfiltered ('{', data->buffer);
344 break;
345 default:
346 internal_error (__FILE__, __LINE__, "bad switch");
347 }
348 }
349
350 static void
351 mi_close (struct ui_out *uiout,
352 enum ui_out_type type)
353 {
354 struct ui_out_data *data = ui_out_data (uiout);
355 switch (type)
356 {
357 case ui_out_type_tuple:
358 fputc_unfiltered ('}', data->buffer);
359 break;
360 case ui_out_type_list:
361 if (data->mi_version > 0)
362 fputc_unfiltered (']', data->buffer);
363 else
364 fputc_unfiltered ('}', data->buffer);
365 break;
366 default:
367 internal_error (__FILE__, __LINE__, "bad switch");
368 }
369 data->suppress_field_separator = 0;
370 }
371
372 /* add a string to the buffer */
373
374 void
375 mi_out_buffered (struct ui_out *uiout, char *string)
376 {
377 struct ui_out_data *data = ui_out_data (uiout);
378 fprintf_unfiltered (data->buffer, "%s", string);
379 }
380
381 /* clear the buffer */
382
383 void
384 mi_out_rewind (struct ui_out *uiout)
385 {
386 struct ui_out_data *data = ui_out_data (uiout);
387 ui_file_rewind (data->buffer);
388 }
389
390 /* dump the buffer onto the specified stream */
391
392 static void
393 do_write (void *data, const char *buffer, long length_buffer)
394 {
395 ui_file_write (data, buffer, length_buffer);
396 }
397
398 void
399 mi_out_put (struct ui_out *uiout,
400 struct ui_file *stream)
401 {
402 struct ui_out_data *data = ui_out_data (uiout);
403 ui_file_put (data->buffer, do_write, stream);
404 ui_file_rewind (data->buffer);
405 }
406
407 /* initalize private members at startup */
408
409 struct ui_out *
410 mi_out_new (int mi_version)
411 {
412 int flags = 0;
413 struct ui_out_data *data = XMALLOC (struct ui_out_data);
414 data->suppress_field_separator = 0;
415 data->mi_version = mi_version;
416 /* FIXME: This code should be using a ``string_file'' and not the
417 TUI buffer hack. */
418 data->buffer = mem_fileopen ();
419 return ui_out_new (&mi_ui_out_impl, data, flags);
420 }
421
422 /* standard gdb initialization hook */
423 void
424 _initialize_mi_out (void)
425 {
426 /* nothing happens here */
427 }
This page took 0.049528 seconds and 4 git commands to generate.