Commit | Line | Data |
---|---|---|
fb40c209 AC |
1 | /* MI Command Set - output generating routines. |
2 | Copyright (C) 2000, Free Software Foundation, Inc. | |
ab91fdd5 | 3 | Contributed by Cygnus Solutions (a Red Hat company). |
fb40c209 AC |
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 supress_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, char *tblid); | |
42 | static void mi_table_body (struct ui_out *uiout); | |
43 | static void mi_table_end (struct ui_out *uiout); | |
44 | static void mi_table_header (struct ui_out *uiout, int width, | |
45 | enum ui_align alig, char *colhdr); | |
46 | static void mi_list_begin (struct ui_out *uiout, int list_flag, char *lstid); | |
47 | static void mi_list_end (struct ui_out *uiout, int list_flag); | |
48 | static void mi_field_int (struct ui_out *uiout, int fldno, int width, | |
49 | enum ui_align alig, char *fldname, int value); | |
50 | static void mi_field_skip (struct ui_out *uiout, int fldno, int width, | |
51 | enum ui_align alig, char *fldname); | |
52 | static void mi_field_string (struct ui_out *uiout, int fldno, int width, | |
53 | enum ui_align alig, 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 | char *fldname, char *format, va_list args); | |
58 | static void mi_spaces (struct ui_out *uiout, int numspaces); | |
59 | static void mi_text (struct ui_out *uiout, char *string); | |
60 | static void mi_message (struct ui_out *uiout, int verbosity, char *format, | |
61 | va_list args); | |
62 | static void mi_wrap_hint (struct ui_out *uiout, char *identstring); | |
63 | static void mi_flush (struct ui_out *uiout); | |
64 | ||
65 | /* This is the MI ui-out implementation functions vector */ | |
66 | ||
67 | /* FIXME: This can be initialized dynamically after default is set to | |
68 | handle initial output in main.c */ | |
69 | ||
70 | struct ui_out_impl mi_ui_out_impl = | |
71 | { | |
72 | mi_table_begin, | |
73 | mi_table_body, | |
74 | mi_table_end, | |
75 | mi_table_header, | |
76 | mi_list_begin, | |
77 | mi_list_end, | |
78 | mi_field_int, | |
79 | mi_field_skip, | |
80 | mi_field_string, | |
81 | mi_field_fmt, | |
82 | mi_spaces, | |
83 | mi_text, | |
84 | mi_message, | |
85 | mi_wrap_hint, | |
86 | mi_flush | |
87 | }; | |
88 | ||
89 | /* Prototypes for local functions */ | |
90 | ||
a14ed312 | 91 | extern void _initialize_mi_out (void); |
fb40c209 AC |
92 | static void field_separator (struct ui_out *uiout); |
93 | static void list_open (struct ui_out *uiout); | |
94 | static void list_close (struct ui_out *uiout); | |
95 | ||
96 | static void out_field_fmt (struct ui_out *uiout, int fldno, char *fldname, | |
97 | char *format,...); | |
98 | ||
99 | /* Mark beginning of a table */ | |
100 | ||
101 | void | |
fba45db2 | 102 | mi_table_begin (struct ui_out *uiout, int nbrofcols, char *tblid) |
fb40c209 AC |
103 | { |
104 | struct ui_out_data *data = ui_out_data (uiout); | |
105 | field_separator (uiout); | |
106 | if (tblid) | |
107 | fprintf_unfiltered (data->buffer, "%s=", tblid); | |
108 | list_open (uiout); | |
109 | data->first_header = 0; | |
110 | data->supress_field_separator = 1; | |
111 | } | |
112 | ||
113 | /* Mark beginning of a table body */ | |
114 | ||
115 | void | |
fba45db2 | 116 | mi_table_body (struct ui_out *uiout) |
fb40c209 AC |
117 | { |
118 | struct ui_out_data *data = ui_out_data (uiout); | |
119 | /* close the table header line if there were any headers */ | |
120 | if (data->first_header) | |
121 | list_close (uiout); | |
122 | } | |
123 | ||
124 | /* Mark end of a table */ | |
125 | ||
126 | void | |
fba45db2 | 127 | mi_table_end (struct ui_out *uiout) |
fb40c209 AC |
128 | { |
129 | struct ui_out_data *data = ui_out_data (uiout); | |
130 | list_close (uiout); | |
131 | /* If table was empty this flag did not get reset yet */ | |
132 | data->supress_field_separator = 0; | |
133 | } | |
134 | ||
135 | /* Specify table header */ | |
136 | ||
137 | void | |
fba45db2 | 138 | mi_table_header (struct ui_out *uiout, int width, int alignment, char *colhdr) |
fb40c209 AC |
139 | { |
140 | struct ui_out_data *data = ui_out_data (uiout); | |
141 | if (!data->first_header++) | |
142 | { | |
143 | fputs_unfiltered ("hdr=", data->buffer); | |
144 | list_open (uiout); | |
145 | } | |
146 | mi_field_string (uiout, 0, width, alignment, 0, colhdr); | |
147 | } | |
148 | ||
149 | /* Mark beginning of a list */ | |
150 | ||
151 | void | |
fba45db2 | 152 | mi_list_begin (struct ui_out *uiout, int list_flag, char *lstid) |
fb40c209 AC |
153 | { |
154 | struct ui_out_data *data = ui_out_data (uiout); | |
155 | field_separator (uiout); | |
156 | data->supress_field_separator = 1; | |
157 | if (lstid) | |
158 | fprintf_unfiltered (data->buffer, "%s=", lstid); | |
159 | list_open (uiout); | |
160 | } | |
161 | ||
162 | /* Mark end of a list */ | |
163 | ||
164 | void | |
fba45db2 | 165 | mi_list_end (struct ui_out *uiout, int list_flag) |
fb40c209 AC |
166 | { |
167 | struct ui_out_data *data = ui_out_data (uiout); | |
168 | list_close (uiout); | |
169 | /* If list was empty this flag did not get reset yet */ | |
170 | data->supress_field_separator = 0; | |
171 | } | |
172 | ||
173 | /* output an int field */ | |
174 | ||
175 | void | |
fba45db2 KB |
176 | mi_field_int (struct ui_out *uiout, int fldno, int width, int alignment, |
177 | char *fldname, int value) | |
fb40c209 AC |
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 | |
fba45db2 KB |
188 | mi_field_skip (struct ui_out *uiout, int fldno, int width, int alignment, |
189 | char *fldname) | |
fb40c209 AC |
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 | 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 | char *fldname, char *format, va_list args) | |
221 | { | |
222 | struct ui_out_data *data = ui_out_data (uiout); | |
223 | field_separator (uiout); | |
224 | if (fldname) | |
225 | fprintf_unfiltered (data->buffer, "%s=\"", fldname); | |
226 | else | |
227 | fputs_unfiltered ("\"", data->buffer); | |
228 | vfprintf_unfiltered (data->buffer, format, args); | |
229 | fputs_unfiltered ("\"", data->buffer); | |
230 | } | |
231 | ||
232 | void | |
fba45db2 | 233 | mi_spaces (struct ui_out *uiout, int numspaces) |
fb40c209 AC |
234 | { |
235 | } | |
236 | ||
237 | void | |
fba45db2 | 238 | mi_text (struct ui_out *uiout, char *string) |
fb40c209 AC |
239 | { |
240 | } | |
241 | ||
242 | void | |
243 | mi_message (struct ui_out *uiout, int verbosity, char *format, va_list args) | |
244 | { | |
245 | } | |
246 | ||
247 | void | |
fba45db2 | 248 | mi_wrap_hint (struct ui_out *uiout, char *identstring) |
fb40c209 AC |
249 | { |
250 | wrap_here (identstring); | |
251 | } | |
252 | ||
253 | void | |
fba45db2 | 254 | mi_flush (struct ui_out *uiout) |
fb40c209 AC |
255 | { |
256 | struct ui_out_data *data = ui_out_data (uiout); | |
257 | gdb_flush (data->buffer); | |
258 | } | |
259 | ||
260 | /* local functions */ | |
261 | ||
262 | /* Like mi_field_fmt, but takes a variable number of args | |
263 | and makes a va_list and does not insert a separator */ | |
264 | ||
265 | /* VARARGS */ | |
266 | static void | |
267 | out_field_fmt (struct ui_out *uiout, int fldno, char *fldname, | |
268 | char *format,...) | |
269 | { | |
270 | struct ui_out_data *data = ui_out_data (uiout); | |
271 | va_list args; | |
272 | ||
273 | field_separator (uiout); | |
274 | if (fldname) | |
275 | fprintf_unfiltered (data->buffer, "%s=\"", fldname); | |
276 | else | |
277 | fputs_unfiltered ("\"", data->buffer); | |
278 | ||
279 | va_start (args, format); | |
280 | vfprintf_unfiltered (data->buffer, format, args); | |
281 | ||
282 | fputs_unfiltered ("\"", data->buffer); | |
283 | ||
284 | va_end (args); | |
285 | } | |
286 | ||
287 | /* access to ui_out format private members */ | |
288 | ||
289 | static void | |
290 | field_separator (struct ui_out *uiout) | |
291 | { | |
292 | struct ui_out_data *data = ui_out_data (uiout); | |
293 | if (data->supress_field_separator) | |
294 | data->supress_field_separator = 0; | |
295 | else | |
296 | fputc_unfiltered (',', data->buffer); | |
297 | } | |
298 | ||
299 | static void | |
300 | list_open (struct ui_out *uiout) | |
301 | { | |
302 | struct ui_out_data *data = ui_out_data (uiout); | |
303 | fputc_unfiltered ('{', data->buffer); | |
304 | } | |
305 | ||
306 | static void | |
307 | list_close (struct ui_out *uiout) | |
308 | { | |
309 | struct ui_out_data *data = ui_out_data (uiout); | |
310 | fputc_unfiltered ('}', data->buffer); | |
311 | } | |
312 | ||
313 | /* add a string to the buffer */ | |
314 | ||
315 | void | |
316 | mi_out_buffered (struct ui_out *uiout, char *string) | |
317 | { | |
318 | struct ui_out_data *data = ui_out_data (uiout); | |
319 | fprintf_unfiltered (data->buffer, "%s", string); | |
320 | } | |
321 | ||
322 | /* clear the buffer */ | |
323 | ||
324 | void | |
325 | mi_out_rewind (struct ui_out *uiout) | |
326 | { | |
327 | struct ui_out_data *data = ui_out_data (uiout); | |
328 | ui_file_rewind (data->buffer); | |
329 | } | |
330 | ||
331 | /* dump the buffer onto the specified stream */ | |
332 | ||
333 | static void | |
334 | do_write (void *data, const char *buffer, long length_buffer) | |
335 | { | |
336 | ui_file_write (data, buffer, length_buffer); | |
337 | } | |
338 | ||
339 | void | |
340 | mi_out_put (struct ui_out *uiout, | |
341 | struct ui_file *stream) | |
342 | { | |
343 | struct ui_out_data *data = ui_out_data (uiout); | |
344 | ui_file_put (data->buffer, do_write, stream); | |
345 | ui_file_rewind (data->buffer); | |
346 | } | |
347 | ||
348 | /* initalize private members at startup */ | |
349 | ||
350 | struct ui_out * | |
351 | mi_out_new (void) | |
352 | { | |
353 | int flags = 0; | |
354 | struct ui_out_data *data = XMALLOC (struct ui_out_data); | |
355 | data->supress_field_separator = 0; | |
356 | /* FIXME: This code should be using a ``string_file'' and not the | |
357 | TUI buffer hack. */ | |
358 | data->buffer = mem_fileopen (); | |
359 | return ui_out_new (&mi_ui_out_impl, data, flags); | |
360 | } | |
361 | ||
362 | /* standard gdb initialization hook */ | |
363 | void | |
fba45db2 | 364 | _initialize_mi_out (void) |
fb40c209 AC |
365 | { |
366 | /* nothing happens here */ | |
367 | } |