]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ui-file.c
2e5c06fb43b12a93e06dfa772e1320f8fb8afefe
1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2025 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* Implement the ``struct ui_file'' object. */
23 #include "gdbsupport/gdb_obstack.h"
24 #include "gdbsupport/gdb_select.h"
25 #include "gdbsupport/filestuff.h"
27 #include "cli/cli-style.h"
30 null_file null_stream
;
39 ui_file::printf (const char *format
, ...)
43 va_start (args
, format
);
44 vprintf (format
, args
);
49 ui_file::putstr (const char *str
, int quoter
)
52 printchar (*str
++, quoter
, false);
56 ui_file::putstrn (const char *str
, int n
, int quoter
, bool async_safe
)
58 for (int i
= 0; i
< n
; i
++)
59 printchar (str
[i
], quoter
, async_safe
);
70 ui_file::vprintf (const char *format
, va_list args
)
72 ui_out_flags flags
= disallow_ui_out_field
;
73 cli_ui_out (this, flags
).vmessage (m_applied_style
, format
, args
);
79 ui_file::emit_style_escape (const ui_file_style
&style
)
81 if (can_emit_style_escape () && style
!= m_applied_style
)
83 m_applied_style
= style
;
84 this->puts (style
.to_ansi ().c_str ());
91 ui_file::printchar (int c
, int quoter
, bool async_safe
)
96 c
&= 0xFF; /* Avoid sign bit follies */
98 if (c
< 0x20 /* Low control chars */
99 || (c
>= 0x7F && c
< 0xA0) /* DEL, High controls */
100 || (sevenbit_strings
&& c
>= 0x80))
101 { /* high order bit set */
129 buf
[out
++] = '0' + ((c
>> 6) & 0x7);
130 buf
[out
++] = '0' + ((c
>> 3) & 0x7);
131 buf
[out
++] = '0' + ((c
>> 0) & 0x7);
138 if (quoter
!= 0 && (c
== '\\' || c
== quoter
))
144 this->write_async_safe (buf
, out
);
146 this->write (buf
, out
);
152 null_file::write (const char *buf
, long sizeof_buf
)
154 /* Discard the request. */
158 null_file::puts (const char *)
160 /* Discard the request. */
164 null_file::write_async_safe (const char *buf
, long sizeof_buf
)
166 /* Discard the request. */
171 string_file::~string_file ()
175 string_file::write (const char *buf
, long length_buf
)
177 m_string
.append (buf
, length_buf
);
183 string_file::term_out ()
191 string_file::can_emit_style_escape ()
193 return m_term_out
&& term_cli_styling ();
198 stdio_file::stdio_file (FILE *file
, bool close_p
)
204 stdio_file::stdio_file ()
210 stdio_file::~stdio_file ()
217 stdio_file::set_stream (FILE *file
)
220 m_fd
= fileno (file
);
224 stdio_file::open (const char *name
, const char *mode
)
226 /* Close the previous stream, if we own it. */
233 gdb_file_up f
= gdb_fopen_cloexec (name
, mode
);
238 set_stream (f
.release ());
251 stdio_file::read (char *buf
, long length_buf
)
253 /* Wait until at least one byte of data is available, or we get
254 interrupted with Control-C. */
259 FD_SET (m_fd
, &readfds
);
260 if (interruptible_select (m_fd
+ 1, &readfds
, NULL
, NULL
, NULL
) == -1)
264 return ::read (m_fd
, buf
, length_buf
);
268 stdio_file::write (const char *buf
, long length_buf
)
270 /* Calling error crashes when we are called from the exception framework. */
271 if (fwrite (buf
, length_buf
, 1, m_file
))
278 stdio_file::write_async_safe (const char *buf
, long length_buf
)
280 /* This is written the way it is to avoid a warning from gcc about not using the
281 result of write (since it can be declared with attribute warn_unused_result).
282 Alas casting to void doesn't work for this. */
283 if (::write (m_fd
, buf
, length_buf
))
290 stdio_file::puts (const char *linebuffer
)
292 /* This host-dependent function (with implementations in
293 posix-hdep.c and mingw-hdep.c) is given the opportunity to
294 process the output first in host-dependent way. If it does, it
295 should return non-zero, to avoid calling fputs below. */
296 if (gdb_console_fputs (linebuffer
, m_file
))
298 /* Calling error crashes when we are called from the exception framework. */
299 if (fputs (linebuffer
, m_file
))
306 stdio_file::isatty ()
308 return ::isatty (m_fd
);
314 stdio_file::can_emit_style_escape ()
316 return (this->isatty ()
317 && term_cli_styling ());
322 /* This is the implementation of ui_file method 'write' for stderr.
323 gdb_stdout is flushed before writing to gdb_stderr. */
326 stderr_file::write (const char *buf
, long length_buf
)
328 gdb_stdout
->flush ();
329 stdio_file::write (buf
, length_buf
);
332 /* This is the implementation of ui_file method 'puts' for stderr.
333 gdb_stdout is flushed before writing to gdb_stderr. */
336 stderr_file::puts (const char *linebuffer
)
338 gdb_stdout
->flush ();
339 stdio_file::puts (linebuffer
);
342 stderr_file::stderr_file (FILE *stream
)
343 : stdio_file (stream
)
348 tee_file::tee_file (ui_file
*one
, ui_file
*two
)
353 tee_file::~tee_file ()
365 tee_file::write (const char *buf
, long length_buf
)
367 m_one
->write (buf
, length_buf
);
368 m_two
->write (buf
, length_buf
);
372 tee_file::write_async_safe (const char *buf
, long length_buf
)
374 m_one
->write_async_safe (buf
, length_buf
);
375 m_two
->write_async_safe (buf
, length_buf
);
379 tee_file::puts (const char *linebuffer
)
381 m_one
->puts (linebuffer
);
382 m_two
->puts (linebuffer
);
388 return m_one
->isatty ();
394 tee_file::term_out ()
396 return m_one
->term_out ();
402 tee_file::can_emit_style_escape ()
404 return (m_one
->term_out ()
405 && term_cli_styling ());
411 escape_buffering_file::write (const char *buf
, long length_buf
)
413 std::string
copy (buf
, length_buf
);
414 this->puts (copy
.c_str ());
420 escape_buffering_file::puts (const char *buf
)
422 std::string local_buffer
;
423 if (!m_buffer
.empty ())
425 gdb_assert (m_buffer
[0] == '\033');
427 /* If we need to keep buffering, we'll handle that below. */
428 local_buffer
= std::move (m_buffer
);
429 buf
= local_buffer
.c_str ();
434 const char *esc
= strchr (buf
, '\033');
438 /* First, write out any prefix. */
441 do_write (buf
, esc
- buf
);
446 ansi_escape_result seen
= examine_ansi_escape (esc
, &n_read
);
447 if (seen
== ansi_escape_result::INCOMPLETE
)
449 /* Start buffering. */
453 else if (seen
== ansi_escape_result::NO_MATCH
)
455 /* Just emit the ESC . */
459 gdb_assert (seen
== ansi_escape_result::MATCHED
);
461 do_write (esc
, n_read
);
465 /* If there is any data remaining in BUF, we can flush it now. */
473 no_terminal_escape_file::do_puts (const char *buf
)
477 const char *esc
= strchr (buf
, '\033');
482 if (!skip_ansi_escape (esc
, &n_read
))
485 this->stdio_file::write (buf
, esc
- buf
);
490 this->stdio_file::write (buf
, strlen (buf
));
494 no_terminal_escape_file::do_write (const char *buf
, long len
)
496 std::string
copy (buf
, len
);
497 do_puts (copy
.c_str ());
501 timestamped_file::write (const char *buf
, long len
)
505 /* Print timestamp if previous print ended with a \n. */
506 if (m_needs_timestamp
)
508 using namespace std::chrono
;
510 steady_clock::time_point now
= steady_clock::now ();
511 seconds s
= duration_cast
<seconds
> (now
.time_since_epoch ());
512 microseconds us
= duration_cast
<microseconds
> (now
.time_since_epoch () - s
);
513 std::string timestamp
= string_printf ("%ld.%06ld ",
516 m_stream
->puts (timestamp
.c_str ());
519 /* Print the message. */
520 m_stream
->write (buf
, len
);
522 m_needs_timestamp
= (len
> 0 && buf
[len
- 1] == '\n');
525 m_stream
->write (buf
, len
);