OSDN Git Service

2aac5289829ae6ed05d6bab5e1b89fc67b104726
[pf3gnuchains/sourceware.git] / gdb / mi / mi-out.c
1 /* MI Command Set - output generating routines.
2
3    Copyright 2000, 2002, 2003 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 typedef struct ui_out_data mi_out_data;
36
37 /* These are the MI output functions */
38
39 static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
40                             int nr_rows, const char *tblid);
41 static void mi_table_body (struct ui_out *uiout);
42 static void mi_table_end (struct ui_out *uiout);
43 static void mi_table_header (struct ui_out *uiout, int width,
44                              enum ui_align alig, const char *col_name,
45                              const char *colhdr);
46 static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
47                       int level, const char *id);
48 static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
49 static void mi_field_int (struct ui_out *uiout, int fldno, int width,
50                           enum ui_align alig, const char *fldname, int value);
51 static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
52                            enum ui_align alig, const char *fldname);
53 static void mi_field_string (struct ui_out *uiout, int fldno, int width,
54                              enum ui_align alig, const char *fldname,
55                              const char *string);
56 static void mi_field_fmt (struct ui_out *uiout, int fldno,
57                           int width, enum ui_align align,
58                           const char *fldname, const char *format,
59                           va_list args);
60 static void mi_spaces (struct ui_out *uiout, int numspaces);
61 static void mi_text (struct ui_out *uiout, const char *string);
62 static void mi_message (struct ui_out *uiout, int verbosity,
63                         const char *format, va_list args);
64 static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
65 static 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
72 struct ui_out_impl mi_ui_out_impl =
73 {
74   mi_table_begin,
75   mi_table_body,
76   mi_table_end,
77   mi_table_header,
78   mi_begin,
79   mi_end,
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,
88   mi_flush,
89   1, /* Needs MI hacks.  */
90 };
91
92 /* Prototypes for local functions */
93
94 extern void _initialize_mi_out (void);
95 static void field_separator (struct ui_out *uiout);
96 static void mi_open (struct ui_out *uiout, const char *name,
97                      enum ui_out_type type);
98 static void mi_close (struct ui_out *uiout, enum ui_out_type type);
99
100 static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
101                            char *format,...);
102
103 /* Mark beginning of a table */
104
105 void
106 mi_table_begin (struct ui_out *uiout,
107                 int nr_cols,
108                 int nr_rows,
109                 const char *tblid)
110 {
111   mi_out_data *data = ui_out_data (uiout);
112   mi_open (uiout, tblid, ui_out_type_tuple);
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);
118 }
119
120 /* Mark beginning of a table body */
121
122 void
123 mi_table_body (struct ui_out *uiout)
124 {
125   mi_out_data *data = ui_out_data (uiout);
126   if (data->suppress_output)
127     return;
128   /* close the table header line if there were any headers */
129   mi_close (uiout, ui_out_type_list);
130   mi_open (uiout, "body", ui_out_type_list);
131 }
132
133 /* Mark end of a table */
134
135 void
136 mi_table_end (struct ui_out *uiout)
137 {
138   mi_out_data *data = ui_out_data (uiout);
139   data->suppress_output = 0;
140   mi_close (uiout, ui_out_type_list); /* body */
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, enum ui_align alignment,
148                  const char *col_name,
149                  const char *colhdr)
150 {
151   mi_out_data *data = ui_out_data (uiout);
152   if (data->suppress_output)
153     return;
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);
160 }
161
162 /* Mark beginning of a list */
163
164 void
165 mi_begin (struct ui_out *uiout,
166           enum ui_out_type type,
167           int level,
168           const char *id)
169 {
170   mi_out_data *data = ui_out_data (uiout);
171   if (data->suppress_output)
172     return;
173   mi_open (uiout, id, type);
174 }
175
176 /* Mark end of a list */
177
178 void
179 mi_end (struct ui_out *uiout,
180         enum ui_out_type type,
181         int level)
182 {
183   mi_out_data *data = ui_out_data (uiout);
184   if (data->suppress_output)
185     return;
186   mi_close (uiout, type);
187 }
188
189 /* output an int field */
190
191 void
192 mi_field_int (struct ui_out *uiout, int fldno, int width,
193               enum ui_align alignment, const char *fldname, int value)
194 {
195   char buffer[20];              /* FIXME: how many chars long a %d can become? */
196   mi_out_data *data = ui_out_data (uiout);
197   if (data->suppress_output)
198     return;
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
206 void
207 mi_field_skip (struct ui_out *uiout, int fldno, int width,
208                enum ui_align alignment, const char *fldname)
209 {
210   mi_out_data *data = ui_out_data (uiout);
211   if (data->suppress_output)
212     return;
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
219 void
220 mi_field_string (struct ui_out *uiout,
221                  int fldno,
222                  int width,
223                  enum ui_align align,
224                  const char *fldname,
225                  const char *string)
226 {
227   mi_out_data *data = ui_out_data (uiout);
228   if (data->suppress_output)
229     return;
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
241 void
242 mi_field_fmt (struct ui_out *uiout, int fldno,
243               int width, enum ui_align align,
244               const char *fldname,
245               const char *format,
246               va_list args)
247 {
248   mi_out_data *data = ui_out_data (uiout);
249   if (data->suppress_output)
250     return;
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
260 void
261 mi_spaces (struct ui_out *uiout, int numspaces)
262 {
263 }
264
265 void
266 mi_text (struct ui_out *uiout, const char *string)
267 {
268 }
269
270 void
271 mi_message (struct ui_out *uiout, int verbosity,
272             const char *format,
273             va_list args)
274 {
275 }
276
277 void
278 mi_wrap_hint (struct ui_out *uiout, char *identstring)
279 {
280   wrap_here (identstring);
281 }
282
283 void
284 mi_flush (struct ui_out *uiout)
285 {
286   mi_out_data *data = ui_out_data (uiout);
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 */
296 static void
297 out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
298                char *format,...)
299 {
300   mi_out_data *data = ui_out_data (uiout);
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
319 static void
320 field_separator (struct ui_out *uiout)
321 {
322   mi_out_data *data = ui_out_data (uiout);
323   if (data->suppress_field_separator)
324     data->suppress_field_separator = 0;
325   else
326     fputc_unfiltered (',', data->buffer);
327 }
328
329 static void
330 mi_open (struct ui_out *uiout,
331          const char *name,
332          enum ui_out_type type)
333 {
334   mi_out_data *data = ui_out_data (uiout);
335   field_separator (uiout);
336   data->suppress_field_separator = 1;
337   if (name)
338     fprintf_unfiltered (data->buffer, "%s=", name);
339   switch (type)
340     {
341     case ui_out_type_tuple:
342       fputc_unfiltered ('{', data->buffer);
343       break;
344     case ui_out_type_list:
345       fputc_unfiltered ('[', data->buffer);
346       break;
347     default:
348       internal_error (__FILE__, __LINE__, "bad switch");
349     }
350 }
351
352 static void
353 mi_close (struct ui_out *uiout,
354           enum ui_out_type type)
355 {
356   mi_out_data *data = ui_out_data (uiout);
357   switch (type)
358     {
359     case ui_out_type_tuple:
360       fputc_unfiltered ('}', data->buffer);
361       break;
362     case ui_out_type_list:
363       fputc_unfiltered (']', data->buffer);
364       break;
365     default:
366       internal_error (__FILE__, __LINE__, "bad switch");
367     }
368   data->suppress_field_separator = 0;
369 }
370
371 /* add a string to the buffer */
372
373 void
374 mi_out_buffered (struct ui_out *uiout, char *string)
375 {
376   mi_out_data *data = ui_out_data (uiout);
377   fprintf_unfiltered (data->buffer, "%s", string);
378 }
379
380 /* clear the buffer */
381
382 void
383 mi_out_rewind (struct ui_out *uiout)
384 {
385   mi_out_data *data = ui_out_data (uiout);
386   ui_file_rewind (data->buffer);
387 }
388
389 /* dump the buffer onto the specified stream */
390
391 static void
392 do_write (void *data, const char *buffer, long length_buffer)
393 {
394   ui_file_write (data, buffer, length_buffer);
395 }
396
397 void
398 mi_out_put (struct ui_out *uiout,
399             struct ui_file *stream)
400 {
401   mi_out_data *data = ui_out_data (uiout);
402   ui_file_put (data->buffer, do_write, stream);
403   ui_file_rewind (data->buffer);
404 }
405
406 /* Current MI version.  */
407
408 int
409 mi_version (struct ui_out *uiout)
410 {
411   mi_out_data *data = ui_out_data (uiout);
412   return data->mi_version;
413 }
414
415 /* initalize private members at startup */
416
417 struct ui_out *
418 mi_out_new (int mi_version)
419 {
420   int flags = 0;
421   mi_out_data *data = XMALLOC (mi_out_data);
422   data->suppress_field_separator = 0;
423   data->suppress_output = 0;
424   data->mi_version = mi_version;
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 */
432 void
433 _initialize_mi_out (void)
434 {
435   /* nothing happens here */
436 }