/* UI_FILE - a generic STDIO like output stream.
- Copyright (C) 1999-2019 Free Software Foundation, Inc.
+ Copyright (C) 1999-2020 Free Software Foundation, Inc.
This file is part of GDB.
#include "defs.h"
#include "ui-file.h"
#include "gdb_obstack.h"
-#include "gdb_select.h"
-#include "common/filestuff.h"
+#include "gdbsupport/gdb_select.h"
+#include "gdbsupport/filestuff.h"
+#include "cli/cli-style.h"
null_file null_stream;
\f
-void
-gdb_flush (struct ui_file *file)
-{
- file->flush ();
-}
+/* true if the gdb terminal supports styling, and styling is enabled. */
-int
-ui_file_isatty (struct ui_file *file)
+static bool
+term_cli_styling ()
{
- return file->isatty ();
-}
+ if (!cli_styling)
+ return false;
-void
-ui_file_write (struct ui_file *file,
- const char *buf,
- long length_buf)
-{
- file->write (buf, length_buf);
+ const char *term = getenv ("TERM");
+ /* Windows doesn't by default define $TERM, but can support styles
+ regardless. */
+#ifndef _WIN32
+ if (term == nullptr || !strcmp (term, "dumb"))
+ return false;
+#else
+ /* But if they do define $TERM, let us behave the same as on Posix
+ platforms, for the benefit of programs which invoke GDB as their
+ back-end. */
+ if (term && !strcmp (term, "dumb"))
+ return false;
+#endif
+ return true;
}
+\f
+
+string_file::~string_file ()
+{}
+
void
-ui_file_write_async_safe (struct ui_file *file,
- const char *buf,
- long length_buf)
+string_file::write (const char *buf, long length_buf)
{
- file->write_async_safe (buf, length_buf);
+ m_string.append (buf, length_buf);
}
-long
-ui_file_read (struct ui_file *file, char *buf, long length_buf)
-{
- return file->read (buf, length_buf);
-}
+/* See ui-file.h. */
-void
-fputs_unfiltered (const char *buf, struct ui_file *file)
+bool
+string_file::term_out ()
{
- file->puts (buf);
+ return m_term_out;
}
-\f
+/* See ui-file.h. */
-string_file::~string_file ()
-{}
-
-void
-string_file::write (const char *buf, long length_buf)
+bool
+string_file::can_emit_style_escape ()
{
- m_string.append (buf, length_buf);
+ return m_term_out && term_cli_styling ();
}
\f
return ::isatty (m_fd);
}
+/* See ui-file.h. */
+
+bool
+stdio_file::can_emit_style_escape ()
+{
+ return ((this == gdb_stdout || this == gdb_stderr)
+ && this->isatty ()
+ && term_cli_styling ());
+}
+
\f
/* This is the implementation of ui_file method 'write' for stderr.
void
stderr_file::write (const char *buf, long length_buf)
{
- gdb_flush (gdb_stdout);
+ gdb_stdout->flush ();
stdio_file::write (buf, length_buf);
}
void
stderr_file::puts (const char *linebuffer)
{
- gdb_flush (gdb_stdout);
+ gdb_stdout->flush ();
stdio_file::puts (linebuffer);
}
\f
-tee_file::tee_file (ui_file *one, bool close_one,
- ui_file *two, bool close_two)
+tee_file::tee_file (ui_file *one, ui_file_up &&two)
: m_one (one),
- m_two (two),
- m_close_one (close_one),
- m_close_two (close_two)
+ m_two (std::move (two))
{}
tee_file::~tee_file ()
{
- if (m_close_one)
- delete m_one;
- if (m_close_two)
- delete m_two;
}
void
{
return m_one->isatty ();
}
+
+/* See ui-file.h. */
+
+bool
+tee_file::term_out ()
+{
+ return m_one->term_out ();
+}
+
+/* See ui-file.h. */
+
+bool
+tee_file::can_emit_style_escape ()
+{
+ return ((this == gdb_stdout || this == gdb_stderr)
+ && m_one->term_out ()
+ && term_cli_styling ());
+}
+
+/* See ui-file.h. */
+
+void
+no_terminal_escape_file::write (const char *buf, long length_buf)
+{
+ std::string copy (buf, length_buf);
+ this->puts (copy.c_str ());
+}
+
+/* See ui-file.h. */
+
+void
+no_terminal_escape_file::puts (const char *buf)
+{
+ while (*buf != '\0')
+ {
+ const char *esc = strchr (buf, '\033');
+ if (esc == nullptr)
+ break;
+
+ int n_read = 0;
+ if (!skip_ansi_escape (esc, &n_read))
+ ++esc;
+
+ this->stdio_file::write (buf, esc - buf);
+ buf = esc + n_read;
+ }
+
+ if (*buf != '\0')
+ this->stdio_file::write (buf, strlen (buf));
+}