1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2022 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. */
24 #include "gdbsupport/gdb_obstack.h"
25 #include "gdbsupport/gdb_select.h"
26 #include "gdbsupport/filestuff.h"
28 #include "cli/cli-style.h"
31 null_file null_stream
;
40 ui_file::printf (const char *format
, ...)
44 va_start (args
, format
);
45 vprintf (format
, args
);
50 ui_file::putstr (const char *str
, int quoter
)
53 printchar (*str
++, quoter
, false);
57 ui_file::putstrn (const char *str
, int n
, int quoter
, bool async_safe
)
59 for (int i
= 0; i
< n
; i
++)
60 printchar (str
[i
], quoter
, async_safe
);
72 ui_file::vprintf (const char *format
, va_list args
)
74 ui_out_flags flags
= disallow_ui_out_field
;
75 cli_ui_out (this, flags
).vmessage (m_applied_style
, format
, args
);
81 ui_file::emit_style_escape (const ui_file_style
&style
)
83 if (can_emit_style_escape () && style
!= m_applied_style
)
85 m_applied_style
= style
;
86 this->puts (style
.to_ansi ().c_str ());
93 ui_file::reset_style ()
95 if (can_emit_style_escape ())
97 m_applied_style
= ui_file_style ();
98 this->puts (m_applied_style
.to_ansi ().c_str ());
105 ui_file::printchar (int c
, int quoter
, bool async_safe
)
110 c
&= 0xFF; /* Avoid sign bit follies */
112 if (c
< 0x20 /* Low control chars */
113 || (c
>= 0x7F && c
< 0xA0) /* DEL, High controls */
114 || (sevenbit_strings
&& c
>= 0x80))
115 { /* high order bit set */
143 buf
[out
++] = '0' + ((c
>> 6) & 0x7);
144 buf
[out
++] = '0' + ((c
>> 3) & 0x7);
145 buf
[out
++] = '0' + ((c
>> 0) & 0x7);
152 if (quoter
!= 0 && (c
== '\\' || c
== quoter
))
158 this->write_async_safe (buf
, out
);
160 this->write (buf
, out
);
166 null_file::write (const char *buf
, long sizeof_buf
)
168 /* Discard the request. */
172 null_file::puts (const char *)
174 /* Discard the request. */
178 null_file::write_async_safe (const char *buf
, long sizeof_buf
)
180 /* Discard the request. */
185 /* true if the gdb terminal supports styling, and styling is enabled. */
193 const char *term
= getenv ("TERM");
194 /* Windows doesn't by default define $TERM, but can support styles
197 if (term
== nullptr || !strcmp (term
, "dumb"))
200 /* But if they do define $TERM, let us behave the same as on Posix
201 platforms, for the benefit of programs which invoke GDB as their
203 if (term
&& !strcmp (term
, "dumb"))
211 string_file::~string_file ()
215 string_file::write (const char *buf
, long length_buf
)
217 m_string
.append (buf
, length_buf
);
223 string_file::term_out ()
231 string_file::can_emit_style_escape ()
233 return m_term_out
&& term_cli_styling ();
238 stdio_file::stdio_file (FILE *file
, bool close_p
)
244 stdio_file::stdio_file ()
250 stdio_file::~stdio_file ()
257 stdio_file::set_stream (FILE *file
)
260 m_fd
= fileno (file
);
264 stdio_file::open (const char *name
, const char *mode
)
266 /* Close the previous stream, if we own it. */
273 gdb_file_up f
= gdb_fopen_cloexec (name
, mode
);
278 set_stream (f
.release ());
291 stdio_file::read (char *buf
, long length_buf
)
293 /* Wait until at least one byte of data is available, or we get
294 interrupted with Control-C. */
299 FD_SET (m_fd
, &readfds
);
300 if (interruptible_select (m_fd
+ 1, &readfds
, NULL
, NULL
, NULL
) == -1)
304 return ::read (m_fd
, buf
, length_buf
);
308 stdio_file::write (const char *buf
, long length_buf
)
310 /* Calling error crashes when we are called from the exception framework. */
311 if (fwrite (buf
, length_buf
, 1, m_file
))
318 stdio_file::write_async_safe (const char *buf
, long length_buf
)
320 /* This is written the way it is to avoid a warning from gcc about not using the
321 result of write (since it can be declared with attribute warn_unused_result).
322 Alas casting to void doesn't work for this. */
323 if (::write (m_fd
, buf
, length_buf
))
330 stdio_file::puts (const char *linebuffer
)
332 /* This host-dependent function (with implementations in
333 posix-hdep.c and mingw-hdep.c) is given the opportunity to
334 process the output first in host-dependent way. If it does, it
335 should return non-zero, to avoid calling fputs below. */
336 if (gdb_console_fputs (linebuffer
, m_file
))
338 /* Calling error crashes when we are called from the exception framework. */
339 if (fputs (linebuffer
, m_file
))
346 stdio_file::isatty ()
348 return ::isatty (m_fd
);
354 stdio_file::can_emit_style_escape ()
356 return (this->isatty ()
357 && term_cli_styling ());
362 /* This is the implementation of ui_file method 'write' for stderr.
363 gdb_stdout is flushed before writing to gdb_stderr. */
366 stderr_file::write (const char *buf
, long length_buf
)
368 gdb_stdout
->flush ();
369 stdio_file::write (buf
, length_buf
);
372 /* This is the implementation of ui_file method 'puts' for stderr.
373 gdb_stdout is flushed before writing to gdb_stderr. */
376 stderr_file::puts (const char *linebuffer
)
378 gdb_stdout
->flush ();
379 stdio_file::puts (linebuffer
);
382 stderr_file::stderr_file (FILE *stream
)
383 : stdio_file (stream
)
388 tee_file::tee_file (ui_file
*one
, ui_file_up
&&two
)
390 m_two (std::move (two
))
393 tee_file::~tee_file ()
405 tee_file::write (const char *buf
, long length_buf
)
407 m_one
->write (buf
, length_buf
);
408 m_two
->write (buf
, length_buf
);
412 tee_file::write_async_safe (const char *buf
, long length_buf
)
414 m_one
->write_async_safe (buf
, length_buf
);
415 m_two
->write_async_safe (buf
, length_buf
);
419 tee_file::puts (const char *linebuffer
)
421 m_one
->puts (linebuffer
);
422 m_two
->puts (linebuffer
);
428 return m_one
->isatty ();
434 tee_file::term_out ()
436 return m_one
->term_out ();
442 tee_file::can_emit_style_escape ()
444 return (m_one
->term_out ()
445 && term_cli_styling ());
451 no_terminal_escape_file::write (const char *buf
, long length_buf
)
453 std::string
copy (buf
, length_buf
);
454 this->puts (copy
.c_str ());
460 no_terminal_escape_file::puts (const char *buf
)
464 const char *esc
= strchr (buf
, '\033');
469 if (!skip_ansi_escape (esc
, &n_read
))
472 this->stdio_file::write (buf
, esc
- buf
);
477 this->stdio_file::write (buf
, strlen (buf
));
481 timestamped_file::write (const char *buf
, long len
)
485 /* Print timestamp if previous print ended with a \n. */
486 if (m_needs_timestamp
)
488 using namespace std::chrono
;
490 steady_clock::time_point now
= steady_clock::now ();
491 seconds s
= duration_cast
<seconds
> (now
.time_since_epoch ());
492 microseconds us
= duration_cast
<microseconds
> (now
.time_since_epoch () - s
);
493 std::string timestamp
= string_printf ("%ld.%06ld ",
496 m_stream
->puts (timestamp
.c_str ());
499 /* Print the message. */
500 m_stream
->write (buf
, len
);
502 m_needs_timestamp
= (len
> 0 && buf
[len
- 1] == '\n');
505 m_stream
->write (buf
, len
);