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