gdb: Use string_printf to format int fields instead of a fixed size buffer
[deliverable/binutils-gdb.git] / gdb / cli-out.c
1 /* Output generating routines for GDB CLI.
2
3 Copyright (C) 1999-2018 Free Software Foundation, Inc.
4
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 3 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, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24 #include "ui-out.h"
25 #include "cli-out.h"
26 #include "completer.h"
27 #include "readline/readline.h"
28
29 /* These are the CLI output functions */
30
31 /* Mark beginning of a table */
32
33 void
34 cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
35 {
36 if (nr_rows == 0)
37 m_suppress_output = true;
38 else
39 /* Only the table suppresses the output and, fortunately, a table
40 is not a recursive data structure. */
41 gdb_assert (!m_suppress_output);
42 }
43
44 /* Mark beginning of a table body */
45
46 void
47 cli_ui_out::do_table_body ()
48 {
49 if (m_suppress_output)
50 return;
51
52 /* first, close the table header line */
53 text ("\n");
54 }
55
56 /* Mark end of a table */
57
58 void
59 cli_ui_out::do_table_end ()
60 {
61 m_suppress_output = false;
62 }
63
64 /* Specify table header */
65
66 void
67 cli_ui_out::do_table_header (int width, ui_align alignment,
68 const std::string &col_name,
69 const std::string &col_hdr)
70 {
71 if (m_suppress_output)
72 return;
73
74 do_field_string (0, width, alignment, 0, col_hdr.c_str ());
75 }
76
77 /* Mark beginning of a list */
78
79 void
80 cli_ui_out::do_begin (ui_out_type type, const char *id)
81 {
82 }
83
84 /* Mark end of a list */
85
86 void
87 cli_ui_out::do_end (ui_out_type type)
88 {
89 }
90
91 /* output an int field */
92
93 void
94 cli_ui_out::do_field_int (int fldno, int width, ui_align alignment,
95 const char *fldname, int value)
96 {
97 if (m_suppress_output)
98 return;
99
100 std::string str = string_printf ("%d", value);
101
102 do_field_string (fldno, width, alignment, fldname, str.c_str ());
103 }
104
105 /* used to omit a field */
106
107 void
108 cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
109 const char *fldname)
110 {
111 if (m_suppress_output)
112 return;
113
114 do_field_string (fldno, width, alignment, fldname, "");
115 }
116
117 /* other specific cli_field_* end up here so alignment and field
118 separators are both handled by cli_field_string */
119
120 void
121 cli_ui_out::do_field_string (int fldno, int width, ui_align align,
122 const char *fldname, const char *string)
123 {
124 int before = 0;
125 int after = 0;
126
127 if (m_suppress_output)
128 return;
129
130 if ((align != ui_noalign) && string)
131 {
132 before = width - strlen (string);
133 if (before <= 0)
134 before = 0;
135 else
136 {
137 if (align == ui_right)
138 after = 0;
139 else if (align == ui_left)
140 {
141 after = before;
142 before = 0;
143 }
144 else
145 /* ui_center */
146 {
147 after = before / 2;
148 before -= after;
149 }
150 }
151 }
152
153 if (before)
154 spaces (before);
155
156 if (string)
157 fputs_filtered (string, m_streams.back ());
158
159 if (after)
160 spaces (after);
161
162 if (align != ui_noalign)
163 field_separator ();
164 }
165
166 /* Output field containing ARGS using printf formatting in FORMAT. */
167
168 void
169 cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
170 const char *fldname, const char *format,
171 va_list args)
172 {
173 if (m_suppress_output)
174 return;
175
176 std::string str = string_vprintf (format, args);
177
178 do_field_string (fldno, width, align, fldname, str.c_str ());
179 }
180
181 void
182 cli_ui_out::do_spaces (int numspaces)
183 {
184 if (m_suppress_output)
185 return;
186
187 print_spaces_filtered (numspaces, m_streams.back ());
188 }
189
190 void
191 cli_ui_out::do_text (const char *string)
192 {
193 if (m_suppress_output)
194 return;
195
196 fputs_filtered (string, m_streams.back ());
197 }
198
199 void
200 cli_ui_out::do_message (const char *format, va_list args)
201 {
202 if (m_suppress_output)
203 return;
204
205 vfprintf_unfiltered (m_streams.back (), format, args);
206 }
207
208 void
209 cli_ui_out::do_wrap_hint (const char *identstring)
210 {
211 if (m_suppress_output)
212 return;
213
214 wrap_here (identstring);
215 }
216
217 void
218 cli_ui_out::do_flush ()
219 {
220 gdb_flush (m_streams.back ());
221 }
222
223 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
224 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
225 output stream; it is an internal error if it does not exist. */
226
227 void
228 cli_ui_out::do_redirect (ui_file *outstream)
229 {
230 if (outstream != NULL)
231 m_streams.push_back (outstream);
232 else
233 m_streams.pop_back ();
234 }
235
236 /* local functions */
237
238 void
239 cli_ui_out::field_separator ()
240 {
241 fputc_filtered (' ', m_streams.back ());
242 }
243
244 /* Constructor for cli_ui_out. */
245
246 cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags)
247 : ui_out (flags),
248 m_suppress_output (false)
249 {
250 gdb_assert (stream != NULL);
251
252 m_streams.push_back (stream);
253 }
254
255 cli_ui_out::~cli_ui_out ()
256 {
257 }
258
259 /* Initialize private members at startup. */
260
261 cli_ui_out *
262 cli_out_new (struct ui_file *stream)
263 {
264 return new cli_ui_out (stream, ui_source_list);
265 }
266
267 ui_file *
268 cli_ui_out::set_stream (struct ui_file *stream)
269 {
270 ui_file *old;
271
272 old = m_streams.back ();
273 m_streams.back () = stream;
274
275 return old;
276 }
277
278 /* CLI interface to display tab-completion matches. */
279
280 /* CLI version of displayer.crlf. */
281
282 static void
283 cli_mld_crlf (const struct match_list_displayer *displayer)
284 {
285 rl_crlf ();
286 }
287
288 /* CLI version of displayer.putch. */
289
290 static void
291 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
292 {
293 putc (ch, rl_outstream);
294 }
295
296 /* CLI version of displayer.puts. */
297
298 static void
299 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
300 {
301 fputs (s, rl_outstream);
302 }
303
304 /* CLI version of displayer.flush. */
305
306 static void
307 cli_mld_flush (const struct match_list_displayer *displayer)
308 {
309 fflush (rl_outstream);
310 }
311
312 EXTERN_C void _rl_erase_entire_line (void);
313
314 /* CLI version of displayer.erase_entire_line. */
315
316 static void
317 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
318 {
319 _rl_erase_entire_line ();
320 }
321
322 /* CLI version of displayer.beep. */
323
324 static void
325 cli_mld_beep (const struct match_list_displayer *displayer)
326 {
327 rl_ding ();
328 }
329
330 /* CLI version of displayer.read_key. */
331
332 static int
333 cli_mld_read_key (const struct match_list_displayer *displayer)
334 {
335 return rl_read_key ();
336 }
337
338 /* CLI version of rl_completion_display_matches_hook.
339 See gdb_display_match_list for a description of the arguments. */
340
341 void
342 cli_display_match_list (char **matches, int len, int max)
343 {
344 struct match_list_displayer displayer;
345
346 rl_get_screen_size (&displayer.height, &displayer.width);
347 displayer.crlf = cli_mld_crlf;
348 displayer.putch = cli_mld_putch;
349 displayer.puts = cli_mld_puts;
350 displayer.flush = cli_mld_flush;
351 displayer.erase_entire_line = cli_mld_erase_entire_line;
352 displayer.beep = cli_mld_beep;
353 displayer.read_key = cli_mld_read_key;
354
355 gdb_display_match_list (matches, len, max, &displayer);
356 rl_forced_update_display ();
357 }
This page took 0.0371 seconds and 5 git commands to generate.