Commit | Line | Data |
---|---|---|
8b93c638 | 1 | /* Output generating routines for GDB CLI. |
b6ba6518 | 2 | Copyright 1999, 2000 Free Software Foundation, Inc. |
8b93c638 JM |
3 | Contributed by Cygnus Solutions. |
4 | Written by Fernando Nasser for Cygnus. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
24 | #include "ui-out.h" | |
25 | #include "cli-out.h" | |
1d1358b6 | 26 | #include "gdb_string.h" |
698384cd | 27 | #include "gdb_assert.h" |
8b93c638 JM |
28 | |
29 | /* Convenience macro for allocting typesafe memory. */ | |
30 | ||
31 | #ifndef XMALLOC | |
32 | #define XMALLOC(TYPE) (TYPE*) xmalloc (sizeof (TYPE)) | |
33 | #endif | |
34 | ||
35 | struct ui_out_data | |
36 | { | |
37 | struct ui_file *stream; | |
698384cd | 38 | int suppress_output; |
8b93c638 JM |
39 | }; |
40 | ||
41 | /* These are the CLI output functions */ | |
42 | ||
e2e11a41 | 43 | static void cli_table_begin (struct ui_out *uiout, int nbrofcols, |
d63f1d40 | 44 | int nr_rows, const char *tblid); |
8b93c638 JM |
45 | static void cli_table_body (struct ui_out *uiout); |
46 | static void cli_table_end (struct ui_out *uiout); | |
47 | static void cli_table_header (struct ui_out *uiout, int width, | |
e2e11a41 AC |
48 | enum ui_align alig, |
49 | const char *colhdr); | |
631ec795 AC |
50 | static void cli_begin (struct ui_out *uiout, enum ui_out_type type, |
51 | int level, const char *lstid); | |
52 | static void cli_end (struct ui_out *uiout, enum ui_out_type type, int level); | |
8b93c638 | 53 | static void cli_field_int (struct ui_out *uiout, int fldno, int width, |
e2e11a41 | 54 | enum ui_align alig, const char *fldname, int value); |
8b93c638 | 55 | static void cli_field_skip (struct ui_out *uiout, int fldno, int width, |
e2e11a41 | 56 | enum ui_align alig, const char *fldname); |
8b93c638 | 57 | static void cli_field_string (struct ui_out *uiout, int fldno, int width, |
e2e11a41 | 58 | enum ui_align alig, const char *fldname, |
8b93c638 JM |
59 | const char *string); |
60 | static void cli_field_fmt (struct ui_out *uiout, int fldno, | |
61 | int width, enum ui_align align, | |
e2e11a41 AC |
62 | const char *fldname, const char *format, |
63 | va_list args); | |
8b93c638 | 64 | static void cli_spaces (struct ui_out *uiout, int numspaces); |
e2e11a41 AC |
65 | static void cli_text (struct ui_out *uiout, const char *string); |
66 | static void cli_message (struct ui_out *uiout, int verbosity, | |
67 | const char *format, va_list args); | |
8b93c638 JM |
68 | static void cli_wrap_hint (struct ui_out *uiout, char *identstring); |
69 | static void cli_flush (struct ui_out *uiout); | |
70 | ||
71 | /* This is the CLI ui-out implementation functions vector */ | |
72 | ||
73 | /* FIXME: This can be initialized dynamically after default is set to | |
74 | handle initial output in main.c */ | |
75 | ||
76 | static struct ui_out_impl cli_ui_out_impl = | |
77 | { | |
78 | cli_table_begin, | |
79 | cli_table_body, | |
80 | cli_table_end, | |
81 | cli_table_header, | |
631ec795 AC |
82 | cli_begin, |
83 | cli_end, | |
8b93c638 JM |
84 | cli_field_int, |
85 | cli_field_skip, | |
86 | cli_field_string, | |
87 | cli_field_fmt, | |
88 | cli_spaces, | |
89 | cli_text, | |
90 | cli_message, | |
91 | cli_wrap_hint, | |
92 | cli_flush | |
93 | }; | |
94 | ||
95 | /* Prototypes for local functions */ | |
96 | ||
a14ed312 | 97 | extern void _initialize_cli_out (void); |
8b93c638 JM |
98 | |
99 | static void field_separator (void); | |
100 | ||
e2e11a41 AC |
101 | static void out_field_fmt (struct ui_out *uiout, int fldno, |
102 | const char *fldname, | |
103 | const char *format,...); | |
8b93c638 JM |
104 | |
105 | /* local variables */ | |
106 | ||
107 | /* (none yet) */ | |
108 | ||
109 | /* Mark beginning of a table */ | |
110 | ||
111 | void | |
e2e11a41 | 112 | cli_table_begin (struct ui_out *uiout, int nbrofcols, |
d63f1d40 | 113 | int nr_rows, |
e2e11a41 | 114 | const char *tblid) |
8b93c638 | 115 | { |
698384cd AC |
116 | struct ui_out_data *data = ui_out_data (uiout); |
117 | if (nr_rows == 0) | |
118 | data->suppress_output = 1; | |
119 | else | |
120 | /* Only the table suppresses the output and, fortunatly, a table | |
121 | is not a recursive data structure. */ | |
122 | gdb_assert (data->suppress_output == 0); | |
8b93c638 JM |
123 | } |
124 | ||
125 | /* Mark beginning of a table body */ | |
126 | ||
127 | void | |
fba45db2 | 128 | cli_table_body (struct ui_out *uiout) |
8b93c638 | 129 | { |
698384cd AC |
130 | struct ui_out_data *data = ui_out_data (uiout); |
131 | if (data->suppress_output) | |
132 | return; | |
8b93c638 JM |
133 | /* first, close the table header line */ |
134 | cli_text (uiout, "\n"); | |
135 | } | |
136 | ||
137 | /* Mark end of a table */ | |
138 | ||
139 | void | |
fba45db2 | 140 | cli_table_end (struct ui_out *uiout) |
8b93c638 | 141 | { |
698384cd AC |
142 | struct ui_out_data *data = ui_out_data (uiout); |
143 | data->suppress_output = 0; | |
8b93c638 JM |
144 | } |
145 | ||
146 | /* Specify table header */ | |
147 | ||
148 | void | |
fba45db2 | 149 | cli_table_header (struct ui_out *uiout, int width, enum ui_align alignment, |
e2e11a41 | 150 | const char *colhdr) |
8b93c638 | 151 | { |
698384cd AC |
152 | struct ui_out_data *data = ui_out_data (uiout); |
153 | if (data->suppress_output) | |
154 | return; | |
8b93c638 JM |
155 | cli_field_string (uiout, 0, width, alignment, 0, colhdr); |
156 | } | |
157 | ||
158 | /* Mark beginning of a list */ | |
159 | ||
160 | void | |
631ec795 AC |
161 | cli_begin (struct ui_out *uiout, |
162 | enum ui_out_type type, | |
163 | int level, | |
164 | const char *id) | |
8b93c638 | 165 | { |
698384cd AC |
166 | struct ui_out_data *data = ui_out_data (uiout); |
167 | if (data->suppress_output) | |
168 | return; | |
8b93c638 JM |
169 | } |
170 | ||
171 | /* Mark end of a list */ | |
172 | ||
173 | void | |
631ec795 AC |
174 | cli_end (struct ui_out *uiout, |
175 | enum ui_out_type type, | |
176 | int level) | |
8b93c638 | 177 | { |
698384cd AC |
178 | struct ui_out_data *data = ui_out_data (uiout); |
179 | if (data->suppress_output) | |
180 | return; | |
8b93c638 JM |
181 | } |
182 | ||
183 | /* output an int field */ | |
184 | ||
185 | void | |
fba45db2 | 186 | cli_field_int (struct ui_out *uiout, int fldno, int width, |
e2e11a41 AC |
187 | enum ui_align alignment, |
188 | const char *fldname, int value) | |
8b93c638 JM |
189 | { |
190 | char buffer[20]; /* FIXME: how many chars long a %d can become? */ | |
191 | ||
698384cd AC |
192 | struct ui_out_data *data = ui_out_data (uiout); |
193 | if (data->suppress_output) | |
194 | return; | |
8b93c638 JM |
195 | sprintf (buffer, "%d", value); |
196 | cli_field_string (uiout, fldno, width, alignment, fldname, buffer); | |
197 | } | |
198 | ||
199 | /* used to ommit a field */ | |
200 | ||
201 | void | |
fba45db2 | 202 | cli_field_skip (struct ui_out *uiout, int fldno, int width, |
e2e11a41 AC |
203 | enum ui_align alignment, |
204 | const char *fldname) | |
8b93c638 | 205 | { |
698384cd AC |
206 | struct ui_out_data *data = ui_out_data (uiout); |
207 | if (data->suppress_output) | |
208 | return; | |
8b93c638 JM |
209 | cli_field_string (uiout, fldno, width, alignment, fldname, ""); |
210 | } | |
211 | ||
212 | /* other specific cli_field_* end up here so alignment and field | |
213 | separators are both handled by cli_field_string */ | |
214 | ||
215 | void | |
216 | cli_field_string (struct ui_out *uiout, | |
217 | int fldno, | |
218 | int width, | |
55555bbc | 219 | enum ui_align align, |
e2e11a41 | 220 | const char *fldname, |
8b93c638 JM |
221 | const char *string) |
222 | { | |
223 | int before = 0; | |
224 | int after = 0; | |
225 | ||
698384cd AC |
226 | struct ui_out_data *data = ui_out_data (uiout); |
227 | if (data->suppress_output) | |
228 | return; | |
229 | ||
8b93c638 JM |
230 | if ((align != ui_noalign) && string) |
231 | { | |
232 | before = width - strlen (string); | |
233 | if (before <= 0) | |
234 | before = 0; | |
235 | else | |
236 | { | |
237 | if (align == ui_right) | |
238 | after = 0; | |
239 | else if (align == ui_left) | |
240 | { | |
241 | after = before; | |
242 | before = 0; | |
243 | } | |
244 | else | |
245 | /* ui_center */ | |
246 | { | |
247 | after = before / 2; | |
248 | before -= after; | |
249 | } | |
250 | } | |
251 | } | |
252 | ||
253 | if (before) | |
254 | ui_out_spaces (uiout, before); | |
255 | if (string) | |
256 | out_field_fmt (uiout, fldno, fldname, "%s", string); | |
257 | if (after) | |
258 | ui_out_spaces (uiout, after); | |
259 | ||
260 | if (align != ui_noalign) | |
261 | field_separator (); | |
262 | } | |
263 | ||
264 | /* This is the only field function that does not align */ | |
265 | ||
266 | void | |
267 | cli_field_fmt (struct ui_out *uiout, int fldno, | |
268 | int width, enum ui_align align, | |
e2e11a41 AC |
269 | const char *fldname, |
270 | const char *format, | |
271 | va_list args) | |
8b93c638 JM |
272 | { |
273 | struct ui_out_data *data = ui_out_data (uiout); | |
698384cd AC |
274 | if (data->suppress_output) |
275 | return; | |
276 | ||
8b93c638 JM |
277 | vfprintf_filtered (data->stream, format, args); |
278 | ||
279 | if (align != ui_noalign) | |
280 | field_separator (); | |
281 | } | |
282 | ||
283 | void | |
fba45db2 | 284 | cli_spaces (struct ui_out *uiout, int numspaces) |
8b93c638 JM |
285 | { |
286 | struct ui_out_data *data = ui_out_data (uiout); | |
698384cd AC |
287 | if (data->suppress_output) |
288 | return; | |
8b93c638 JM |
289 | print_spaces_filtered (numspaces, data->stream); |
290 | } | |
291 | ||
292 | void | |
e2e11a41 | 293 | cli_text (struct ui_out *uiout, const char *string) |
8b93c638 JM |
294 | { |
295 | struct ui_out_data *data = ui_out_data (uiout); | |
698384cd AC |
296 | if (data->suppress_output) |
297 | return; | |
8b93c638 JM |
298 | fputs_filtered (string, data->stream); |
299 | } | |
300 | ||
301 | void | |
e2e11a41 AC |
302 | cli_message (struct ui_out *uiout, int verbosity, |
303 | const char *format, va_list args) | |
8b93c638 JM |
304 | { |
305 | struct ui_out_data *data = ui_out_data (uiout); | |
698384cd AC |
306 | if (data->suppress_output) |
307 | return; | |
8b93c638 JM |
308 | if (ui_out_get_verblvl (uiout) >= verbosity) |
309 | vfprintf_unfiltered (data->stream, format, args); | |
310 | } | |
311 | ||
312 | void | |
fba45db2 | 313 | cli_wrap_hint (struct ui_out *uiout, char *identstring) |
8b93c638 | 314 | { |
698384cd AC |
315 | struct ui_out_data *data = ui_out_data (uiout); |
316 | if (data->suppress_output) | |
317 | return; | |
8b93c638 JM |
318 | wrap_here (identstring); |
319 | } | |
320 | ||
321 | void | |
fba45db2 | 322 | cli_flush (struct ui_out *uiout) |
8b93c638 JM |
323 | { |
324 | struct ui_out_data *data = ui_out_data (uiout); | |
325 | gdb_flush (data->stream); | |
326 | } | |
327 | ||
328 | /* local functions */ | |
329 | ||
330 | /* Like cli_field_fmt, but takes a variable number of args | |
331 | and makes a va_list and does not insert a separator */ | |
332 | ||
333 | /* VARARGS */ | |
334 | static void | |
e2e11a41 AC |
335 | out_field_fmt (struct ui_out *uiout, int fldno, |
336 | const char *fldname, | |
337 | const char *format,...) | |
8b93c638 JM |
338 | { |
339 | struct ui_out_data *data = ui_out_data (uiout); | |
340 | va_list args; | |
341 | ||
342 | va_start (args, format); | |
343 | vfprintf_filtered (data->stream, format, args); | |
344 | ||
345 | va_end (args); | |
346 | } | |
347 | ||
348 | /* access to ui_out format private members */ | |
349 | ||
350 | static void | |
fba45db2 | 351 | field_separator (void) |
8b93c638 JM |
352 | { |
353 | struct ui_out_data *data = ui_out_data (uiout); | |
354 | fputc_filtered (' ', data->stream); | |
355 | } | |
356 | ||
357 | /* initalize private members at startup */ | |
358 | ||
359 | struct ui_out * | |
360 | cli_out_new (struct ui_file *stream) | |
361 | { | |
362 | int flags = ui_source_list; | |
363 | ||
364 | struct ui_out_data *data = XMALLOC (struct ui_out_data); | |
365 | data->stream = stream; | |
366 | return ui_out_new (&cli_ui_out_impl, data, flags); | |
367 | } | |
368 | ||
369 | /* standard gdb initialization hook */ | |
370 | void | |
fba45db2 | 371 | _initialize_cli_out (void) |
8b93c638 JM |
372 | { |
373 | /* nothing needs to be done */ | |
374 | } |