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