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