Remove vfprintf_styled_no_gdbfmt
[binutils-gdb.git] / gdb / cli-out.c
1 /* Output generating routines for GDB CLI.
2
3 Copyright (C) 1999-2022 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 #include "cli/cli-style.h"
29
30 /* These are the CLI output functions */
31
32 /* Mark beginning of a table */
33
34 void
35 cli_ui_out::do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
36 {
37 if (nr_rows == 0)
38 m_suppress_output = true;
39 else
40 /* Only the table suppresses the output and, fortunately, a table
41 is not a recursive data structure. */
42 gdb_assert (!m_suppress_output);
43 }
44
45 /* Mark beginning of a table body */
46
47 void
48 cli_ui_out::do_table_body ()
49 {
50 if (m_suppress_output)
51 return;
52
53 /* first, close the table header line */
54 text ("\n");
55 }
56
57 /* Mark end of a table */
58
59 void
60 cli_ui_out::do_table_end ()
61 {
62 m_suppress_output = false;
63 }
64
65 /* Specify table header */
66
67 void
68 cli_ui_out::do_table_header (int width, ui_align alignment,
69 const std::string &col_name,
70 const std::string &col_hdr)
71 {
72 if (m_suppress_output)
73 return;
74
75 do_field_string (0, width, alignment, 0, col_hdr.c_str (),
76 ui_file_style ());
77 }
78
79 /* Mark beginning of a list */
80
81 void
82 cli_ui_out::do_begin (ui_out_type type, const char *id)
83 {
84 }
85
86 /* Mark end of a list */
87
88 void
89 cli_ui_out::do_end (ui_out_type type)
90 {
91 }
92
93 /* output an int field */
94
95 void
96 cli_ui_out::do_field_signed (int fldno, int width, ui_align alignment,
97 const char *fldname, LONGEST value)
98 {
99 if (m_suppress_output)
100 return;
101
102 do_field_string (fldno, width, alignment, fldname, plongest (value),
103 ui_file_style ());
104 }
105
106 /* output an unsigned field */
107
108 void
109 cli_ui_out::do_field_unsigned (int fldno, int width, ui_align alignment,
110 const char *fldname, ULONGEST value)
111 {
112 if (m_suppress_output)
113 return;
114
115 do_field_string (fldno, width, alignment, fldname, pulongest (value),
116 ui_file_style ());
117 }
118
119 /* used to omit a field */
120
121 void
122 cli_ui_out::do_field_skip (int fldno, int width, ui_align alignment,
123 const char *fldname)
124 {
125 if (m_suppress_output)
126 return;
127
128 do_field_string (fldno, width, alignment, fldname, "",
129 ui_file_style ());
130 }
131
132 /* other specific cli_field_* end up here so alignment and field
133 separators are both handled by cli_field_string */
134
135 void
136 cli_ui_out::do_field_string (int fldno, int width, ui_align align,
137 const char *fldname, const char *string,
138 const ui_file_style &style)
139 {
140 int before = 0;
141 int after = 0;
142
143 if (m_suppress_output)
144 return;
145
146 if ((align != ui_noalign) && string)
147 {
148 before = width - strlen (string);
149 if (before <= 0)
150 before = 0;
151 else
152 {
153 if (align == ui_right)
154 after = 0;
155 else if (align == ui_left)
156 {
157 after = before;
158 before = 0;
159 }
160 else
161 /* ui_center */
162 {
163 after = before / 2;
164 before -= after;
165 }
166 }
167 }
168
169 if (before)
170 spaces (before);
171
172 if (string)
173 {
174 if (test_flags (unfiltered_output))
175 fputs_styled_unfiltered (string, style, m_streams.back ());
176 else
177 fputs_styled (string, style, m_streams.back ());
178 }
179
180 if (after)
181 spaces (after);
182
183 if (align != ui_noalign)
184 field_separator ();
185 }
186
187 /* Output field containing ARGS using printf formatting in FORMAT. */
188
189 void
190 cli_ui_out::do_field_fmt (int fldno, int width, ui_align align,
191 const char *fldname, const ui_file_style &style,
192 const char *format, va_list args)
193 {
194 if (m_suppress_output)
195 return;
196
197 std::string str = string_vprintf (format, args);
198
199 do_field_string (fldno, width, align, fldname, str.c_str (), style);
200 }
201
202 void
203 cli_ui_out::do_spaces (int numspaces)
204 {
205 if (m_suppress_output)
206 return;
207
208 if (test_flags (unfiltered_output))
209 fprintf_unfiltered (m_streams.back (), "%*s", numspaces, "");
210 else
211 print_spaces_filtered (numspaces, m_streams.back ());
212 }
213
214 void
215 cli_ui_out::do_text (const char *string)
216 {
217 if (m_suppress_output)
218 return;
219
220 if (test_flags (unfiltered_output))
221 fputs_unfiltered (string, m_streams.back ());
222 else
223 fputs_filtered (string, m_streams.back ());
224 }
225
226 void
227 cli_ui_out::do_message (const ui_file_style &style,
228 const char *format, va_list args)
229 {
230 if (m_suppress_output)
231 return;
232
233 std::string str = string_vprintf (format, args);
234 if (!str.empty ())
235 {
236 if (test_flags (unfiltered_output))
237 fputs_styled_unfiltered (str.c_str (), style, m_streams.back ());
238 else
239 fputs_styled (str.c_str (), style, m_streams.back ());
240 }
241 }
242
243 void
244 cli_ui_out::do_wrap_hint (int indent)
245 {
246 if (m_suppress_output)
247 return;
248
249 m_streams.back ()->wrap_here (indent);
250 }
251
252 void
253 cli_ui_out::do_flush ()
254 {
255 gdb_flush (m_streams.back ());
256 }
257
258 /* OUTSTREAM as non-NULL will push OUTSTREAM on the stack of output streams
259 and make it therefore active. OUTSTREAM as NULL will pop the last pushed
260 output stream; it is an internal error if it does not exist. */
261
262 void
263 cli_ui_out::do_redirect (ui_file *outstream)
264 {
265 if (outstream != NULL)
266 m_streams.push_back (outstream);
267 else
268 m_streams.pop_back ();
269 }
270
271 /* The cli_ui_out::do_progress_* functions result in the following:
272 - printed for tty, SHOULD_PRINT == true:
273 <NAME
274 [##### ]\r>
275 - printed for tty, SHOULD_PRINT == false:
276 <>
277 - printed for not-a-tty:
278 <NAME...
279 >
280 */
281
282 void
283 cli_ui_out::do_progress_start (const std::string &name, bool should_print)
284 {
285 struct ui_file *stream = m_streams.back ();
286 cli_progress_info meter;
287
288 meter.last_value = 0;
289 meter.name = name;
290 if (!stream->isatty ())
291 {
292 fprintf_unfiltered (stream, "%s...", meter.name.c_str ());
293 gdb_flush (stream);
294 meter.printing = WORKING;
295 }
296 else
297 {
298 /* Don't actually emit anything until the first call notifies us
299 of progress. This makes it so a second progress message can
300 be started before the first one has been notified, without
301 messy output. */
302 meter.printing = should_print ? START : NO_PRINT;
303 }
304
305 m_meters.push_back (std::move (meter));
306 }
307
308 void
309 cli_ui_out::do_progress_notify (double howmuch)
310 {
311 struct ui_file *stream = m_streams.back ();
312 cli_progress_info &meter (m_meters.back ());
313
314 if (meter.printing == NO_PRINT)
315 return;
316
317 if (meter.printing == START)
318 {
319 fprintf_unfiltered (stream, "%s\n", meter.name.c_str ());
320 gdb_flush (stream);
321 meter.printing = WORKING;
322 }
323
324 if (meter.printing == WORKING && howmuch >= 1.0)
325 return;
326
327 if (!stream->isatty ())
328 return;
329
330 int chars_per_line = get_chars_per_line ();
331 if (chars_per_line > 0)
332 {
333 int i, max;
334 int width = chars_per_line - 3;
335
336 max = width * howmuch;
337 fprintf_unfiltered (stream, "\r[");
338 for (i = 0; i < width; ++i)
339 fprintf_unfiltered (stream, i < max ? "#" : " ");
340 fprintf_unfiltered (stream, "]");
341 gdb_flush (stream);
342 meter.printing = PROGRESS;
343 }
344 }
345
346 void
347 cli_ui_out::do_progress_end ()
348 {
349 struct ui_file *stream = m_streams.back ();
350 cli_progress_info &meter = m_meters.back ();
351
352 if (!stream->isatty ())
353 {
354 fprintf_unfiltered (stream, "\n");
355 gdb_flush (stream);
356 }
357 else if (meter.printing == PROGRESS)
358 {
359 int i;
360 int width = get_chars_per_line () - 3;
361
362 fprintf_unfiltered (stream, "\r");
363 for (i = 0; i < width + 2; ++i)
364 fprintf_unfiltered (stream, " ");
365 fprintf_unfiltered (stream, "\r");
366 gdb_flush (stream);
367 }
368
369 m_meters.pop_back ();
370 }
371
372 /* local functions */
373
374 void
375 cli_ui_out::field_separator ()
376 {
377 if (test_flags (unfiltered_output))
378 fputc_unfiltered (' ', m_streams.back ());
379 else
380 fputc_filtered (' ', m_streams.back ());
381 }
382
383 /* Constructor for cli_ui_out. */
384
385 cli_ui_out::cli_ui_out (ui_file *stream, ui_out_flags flags)
386 : ui_out (flags),
387 m_suppress_output (false)
388 {
389 gdb_assert (stream != NULL);
390
391 m_streams.push_back (stream);
392 }
393
394 cli_ui_out::~cli_ui_out ()
395 {
396 }
397
398 /* Initialize private members at startup. */
399
400 cli_ui_out *
401 cli_out_new (struct ui_file *stream)
402 {
403 return new cli_ui_out (stream, ui_source_list);
404 }
405
406 ui_file *
407 cli_ui_out::set_stream (struct ui_file *stream)
408 {
409 ui_file *old;
410
411 old = m_streams.back ();
412 m_streams.back () = stream;
413
414 return old;
415 }
416
417 bool
418 cli_ui_out::can_emit_style_escape () const
419 {
420 return m_streams.back ()->can_emit_style_escape ();
421 }
422
423 /* CLI interface to display tab-completion matches. */
424
425 /* CLI version of displayer.crlf. */
426
427 static void
428 cli_mld_crlf (const struct match_list_displayer *displayer)
429 {
430 rl_crlf ();
431 }
432
433 /* CLI version of displayer.putch. */
434
435 static void
436 cli_mld_putch (const struct match_list_displayer *displayer, int ch)
437 {
438 putc (ch, rl_outstream);
439 }
440
441 /* CLI version of displayer.puts. */
442
443 static void
444 cli_mld_puts (const struct match_list_displayer *displayer, const char *s)
445 {
446 fputs (s, rl_outstream);
447 }
448
449 /* CLI version of displayer.flush. */
450
451 static void
452 cli_mld_flush (const struct match_list_displayer *displayer)
453 {
454 fflush (rl_outstream);
455 }
456
457 EXTERN_C void _rl_erase_entire_line (void);
458
459 /* CLI version of displayer.erase_entire_line. */
460
461 static void
462 cli_mld_erase_entire_line (const struct match_list_displayer *displayer)
463 {
464 _rl_erase_entire_line ();
465 }
466
467 /* CLI version of displayer.beep. */
468
469 static void
470 cli_mld_beep (const struct match_list_displayer *displayer)
471 {
472 rl_ding ();
473 }
474
475 /* CLI version of displayer.read_key. */
476
477 static int
478 cli_mld_read_key (const struct match_list_displayer *displayer)
479 {
480 return rl_read_key ();
481 }
482
483 /* CLI version of rl_completion_display_matches_hook.
484 See gdb_display_match_list for a description of the arguments. */
485
486 void
487 cli_display_match_list (char **matches, int len, int max)
488 {
489 struct match_list_displayer displayer;
490
491 rl_get_screen_size (&displayer.height, &displayer.width);
492 displayer.crlf = cli_mld_crlf;
493 displayer.putch = cli_mld_putch;
494 displayer.puts = cli_mld_puts;
495 displayer.flush = cli_mld_flush;
496 displayer.erase_entire_line = cli_mld_erase_entire_line;
497 displayer.beep = cli_mld_beep;
498 displayer.read_key = cli_mld_read_key;
499
500 gdb_display_match_list (matches, len, max, &displayer);
501 rl_forced_update_display ();
502 }