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