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