s/supress/suppress/
[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 struct ui_file *buffer;
37 };
38
39 /* These are the MI output functions */
40
41 static void mi_table_begin (struct ui_out *uiout, int nbrofcols,
42 const char *tblid);
43 static void mi_table_body (struct ui_out *uiout);
44 static void mi_table_end (struct ui_out *uiout);
45 static void mi_table_header (struct ui_out *uiout, int width,
46 enum ui_align alig,
47 const char *colhdr);
48 static void mi_begin (struct ui_out *uiout, enum ui_out_type type,
49 int level, const char *id);
50 static void mi_end (struct ui_out *uiout, enum ui_out_type type, int level);
51 static void mi_field_int (struct ui_out *uiout, int fldno, int width,
52 enum ui_align alig, const char *fldname, int value);
53 static void mi_field_skip (struct ui_out *uiout, int fldno, int width,
54 enum ui_align alig, const char *fldname);
55 static void mi_field_string (struct ui_out *uiout, int fldno, int width,
56 enum ui_align alig, const char *fldname,
57 const char *string);
58 static void mi_field_fmt (struct ui_out *uiout, int fldno,
59 int width, enum ui_align align,
60 const char *fldname, const char *format,
61 va_list args);
62 static void mi_spaces (struct ui_out *uiout, int numspaces);
63 static void mi_text (struct ui_out *uiout, const char *string);
64 static void mi_message (struct ui_out *uiout, int verbosity,
65 const char *format, va_list args);
66 static void mi_wrap_hint (struct ui_out *uiout, char *identstring);
67 static 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
74 struct ui_out_impl mi_ui_out_impl =
75 {
76 mi_table_begin,
77 mi_table_body,
78 mi_table_end,
79 mi_table_header,
80 mi_begin,
81 mi_end,
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
95 extern void _initialize_mi_out (void);
96 static void field_separator (struct ui_out *uiout);
97 static void mi_open (struct ui_out *uiout, const char *name,
98 enum ui_out_type type);
99 static void mi_close (struct ui_out *uiout, enum ui_out_type type);
100
101 static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname,
102 char *format,...);
103
104 /* Mark beginning of a table */
105
106 void
107 mi_table_begin (struct ui_out *uiout, int nbrofcols,
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 data->first_header = 0;
113 }
114
115 /* Mark beginning of a table body */
116
117 void
118 mi_table_body (struct ui_out *uiout)
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)
123 mi_close (uiout, ui_out_type_tuple);
124 }
125
126 /* Mark end of a table */
127
128 void
129 mi_table_end (struct ui_out *uiout)
130 {
131 struct ui_out_data *data = ui_out_data (uiout);
132 mi_close (uiout, ui_out_type_tuple);
133 }
134
135 /* Specify table header */
136
137 void
138 mi_table_header (struct ui_out *uiout, int width, int alignment,
139 const char *colhdr)
140 {
141 struct ui_out_data *data = ui_out_data (uiout);
142 if (!data->first_header++)
143 {
144 mi_open (uiout, "hdr", ui_out_type_tuple);
145 }
146 mi_field_string (uiout, 0, width, alignment, 0, colhdr);
147 }
148
149 /* Mark beginning of a list */
150
151 void
152 mi_begin (struct ui_out *uiout,
153 enum ui_out_type type,
154 int level,
155 const char *id)
156 {
157 struct ui_out_data *data = ui_out_data (uiout);
158 mi_open (uiout, id, type);
159 }
160
161 /* Mark end of a list */
162
163 void
164 mi_end (struct ui_out *uiout,
165 enum ui_out_type type,
166 int level)
167 {
168 struct ui_out_data *data = ui_out_data (uiout);
169 mi_close (uiout, type);
170 }
171
172 /* output an int field */
173
174 void
175 mi_field_int (struct ui_out *uiout, int fldno, int width, int alignment,
176 const char *fldname, int value)
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
186 void
187 mi_field_skip (struct ui_out *uiout, int fldno, int width, int alignment,
188 const char *fldname)
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
196 void
197 mi_field_string (struct ui_out *uiout,
198 int fldno,
199 int width,
200 int align,
201 const char *fldname,
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
216 void
217 mi_field_fmt (struct ui_out *uiout, int fldno,
218 int width, enum ui_align align,
219 const char *fldname,
220 const char *format,
221 va_list args)
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
233 void
234 mi_spaces (struct ui_out *uiout, int numspaces)
235 {
236 }
237
238 void
239 mi_text (struct ui_out *uiout, const char *string)
240 {
241 }
242
243 void
244 mi_message (struct ui_out *uiout, int verbosity,
245 const char *format,
246 va_list args)
247 {
248 }
249
250 void
251 mi_wrap_hint (struct ui_out *uiout, char *identstring)
252 {
253 wrap_here (identstring);
254 }
255
256 void
257 mi_flush (struct ui_out *uiout)
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 */
269 static void
270 out_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
292 static void
293 field_separator (struct ui_out *uiout)
294 {
295 struct ui_out_data *data = ui_out_data (uiout);
296 if (data->suppress_field_separator)
297 data->suppress_field_separator = 0;
298 else
299 fputc_unfiltered (',', data->buffer);
300 }
301
302 static void
303 mi_open (struct ui_out *uiout,
304 const char *name,
305 enum ui_out_type type)
306 {
307 struct ui_out_data *data = ui_out_data (uiout);
308 field_separator (uiout);
309 data->suppress_field_separator = 1;
310 if (name)
311 fprintf_unfiltered (data->buffer, "%s=", name);
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 }
323 }
324
325 static void
326 mi_close (struct ui_out *uiout,
327 enum ui_out_type type)
328 {
329 struct ui_out_data *data = ui_out_data (uiout);
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 }
341 data->suppress_field_separator = 0;
342 }
343
344 /* add a string to the buffer */
345
346 void
347 mi_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
355 void
356 mi_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
364 static void
365 do_write (void *data, const char *buffer, long length_buffer)
366 {
367 ui_file_write (data, buffer, length_buffer);
368 }
369
370 void
371 mi_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
381 struct ui_out *
382 mi_out_new (void)
383 {
384 int flags = 0;
385 struct ui_out_data *data = XMALLOC (struct ui_out_data);
386 data->suppress_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 */
394 void
395 _initialize_mi_out (void)
396 {
397 /* nothing happens here */
398 }
This page took 0.037662 seconds and 5 git commands to generate.