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"
27 #include "cli/cli-style.h"
29 null_file null_stream;
38 ui_file::printf (const char *format, ...)
42 va_start (args, format);
43 vfprintf_unfiltered (this, format, args);
48 ui_file::putstr (const char *str, int quoter)
51 printchar (*str++, quoter, false);
55 ui_file::putstrn (const char *str, int n, int quoter, bool async_safe)
57 for (int i = 0; i < n; i++)
58 printchar (str[i], quoter, async_safe);
64 return fputc_unfiltered (c, this);
68 ui_file::vprintf (const char *format, va_list args)
70 vfprintf_unfiltered (this, format, args);
76 ui_file::printchar (int c, int quoter, bool async_safe)
81 c &= 0xFF; /* Avoid sign bit follies */
83 if (c < 0x20 /* Low control chars */
84 || (c >= 0x7F && c < 0xA0) /* DEL, High controls */
85 || (sevenbit_strings && c >= 0x80))
86 { /* high order bit set */
114 buf[out++] = '0' + ((c >> 6) & 0x7);
115 buf[out++] = '0' + ((c >> 3) & 0x7);
116 buf[out++] = '0' + ((c >> 0) & 0x7);
123 if (quoter != 0 && (c == '\\' || c == quoter))
129 this->write_async_safe (buf, out);
131 this->write (buf, out);
137 null_file::write (const char *buf, long sizeof_buf)
139 /* Discard the request. */
143 null_file::puts (const char *)
145 /* Discard the request. */
149 null_file::write_async_safe (const char *buf, long sizeof_buf)
151 /* Discard the request. */
156 /* true if the gdb terminal supports styling, and styling is enabled. */
164 const char *term = getenv ("TERM");
165 /* Windows doesn't by default define $TERM, but can support styles
168 if (term == nullptr || !strcmp (term, "dumb"))
171 /* But if they do define $TERM, let us behave the same as on Posix
172 platforms, for the benefit of programs which invoke GDB as their
174 if (term && !strcmp (term, "dumb"))
182 string_file::~string_file ()
186 string_file::write (const char *buf, long length_buf)
188 m_string.append (buf, length_buf);
194 string_file::term_out ()
202 string_file::can_emit_style_escape ()
204 return m_term_out && term_cli_styling ();
209 stdio_file::stdio_file (FILE *file, bool close_p)
215 stdio_file::stdio_file ()
221 stdio_file::~stdio_file ()
228 stdio_file::set_stream (FILE *file)
231 m_fd = fileno (file);
235 stdio_file::open (const char *name, const char *mode)
237 /* Close the previous stream, if we own it. */
244 gdb_file_up f = gdb_fopen_cloexec (name, mode);
249 set_stream (f.release ());
262 stdio_file::read (char *buf, long length_buf)
264 /* Wait until at least one byte of data is available, or we get
265 interrupted with Control-C. */
270 FD_SET (m_fd, &readfds);
271 if (interruptible_select (m_fd + 1, &readfds, NULL, NULL, NULL) == -1)
275 return ::read (m_fd, buf, length_buf);
279 stdio_file::write (const char *buf, long length_buf)
281 /* Calling error crashes when we are called from the exception framework. */
282 if (fwrite (buf, length_buf, 1, m_file))
289 stdio_file::write_async_safe (const char *buf, long length_buf)
291 /* This is written the way it is to avoid a warning from gcc about not using the
292 result of write (since it can be declared with attribute warn_unused_result).
293 Alas casting to void doesn't work for this. */
294 if (::write (m_fd, buf, length_buf))
301 stdio_file::puts (const char *linebuffer)
303 /* This host-dependent function (with implementations in
304 posix-hdep.c and mingw-hdep.c) is given the opportunity to
305 process the output first in host-dependent way. If it does, it
306 should return non-zero, to avoid calling fputs below. */
307 if (gdb_console_fputs (linebuffer, m_file))
309 /* Calling error crashes when we are called from the exception framework. */
310 if (fputs (linebuffer, m_file))
317 stdio_file::isatty ()
319 return ::isatty (m_fd);
325 stdio_file::can_emit_style_escape ()
327 return ((this == gdb_stdout || this == gdb_stderr)
329 && term_cli_styling ());
334 /* This is the implementation of ui_file method 'write' for stderr.
335 gdb_stdout is flushed before writing to gdb_stderr. */
338 stderr_file::write (const char *buf, long length_buf)
340 gdb_stdout->flush ();
341 stdio_file::write (buf, length_buf);
344 /* This is the implementation of ui_file method 'puts' for stderr.
345 gdb_stdout is flushed before writing to gdb_stderr. */
348 stderr_file::puts (const char *linebuffer)
350 gdb_stdout->flush ();
351 stdio_file::puts (linebuffer);
354 stderr_file::stderr_file (FILE *stream)
355 : stdio_file (stream)
360 tee_file::tee_file (ui_file *one, ui_file_up &&two)
362 m_two (std::move (two))
365 tee_file::~tee_file ()
377 tee_file::write (const char *buf, long length_buf)
379 m_one->write (buf, length_buf);
380 m_two->write (buf, length_buf);
384 tee_file::write_async_safe (const char *buf, long length_buf)
386 m_one->write_async_safe (buf, length_buf);
387 m_two->write_async_safe (buf, length_buf);
391 tee_file::puts (const char *linebuffer)
393 m_one->puts (linebuffer);
394 m_two->puts (linebuffer);
400 return m_one->isatty ();
406 tee_file::term_out ()
408 return m_one->term_out ();
414 tee_file::can_emit_style_escape ()
416 return ((this == gdb_stdout || this == gdb_stderr)
417 && m_one->term_out ()
418 && term_cli_styling ());
424 no_terminal_escape_file::write (const char *buf, long length_buf)
426 std::string copy (buf, length_buf);
427 this->puts (copy.c_str ());
433 no_terminal_escape_file::puts (const char *buf)
437 const char *esc = strchr (buf, '\033');
442 if (!skip_ansi_escape (esc, &n_read))
445 this->stdio_file::write (buf, esc - buf);
450 this->stdio_file::write (buf, strlen (buf));