+class string_file : public ui_file
+{
+public:
+ /* Construct a string_file to collect 'raw' output, i.e. without
+ 'terminal' behaviour such as cli_styling. */
+ string_file () : m_term_out (false) {};
+ /* If TERM_OUT, construct a string_file with terminal output behaviour
+ such as cli_styling)
+ else collect 'raw' output like the previous constructor. */
+ explicit string_file (bool term_out) : m_term_out (term_out) {};
+ ~string_file () override;
+
+ /* Override ui_file methods. */
+
+ void write (const char *buf, long length_buf) override;
+
+ long read (char *buf, long length_buf) override
+ { gdb_assert_not_reached ("a string_file is not readable"); }
+
+ bool term_out () override;
+ bool can_emit_style_escape () override;
+
+ /* string_file-specific public API. */
+
+ /* Accesses the std::string containing the entire output collected
+ so far.
+
+ Returns a non-const reference so that it's easy to move the
+ string contents out of the string_file. E.g.:
+
+ string_file buf;
+ buf.printf (....);
+ buf.printf (....);
+ return std::move (buf.string ());
+ */
+ std::string &string () { return m_string; }
+
+ /* Provide a few convenience methods with the same API as the
+ underlying std::string. */
+ const char *data () const { return m_string.data (); }
+ const char *c_str () const { return m_string.c_str (); }
+ size_t size () const { return m_string.size (); }
+ bool empty () const { return m_string.empty (); }
+ void clear () { return m_string.clear (); }
+
+private:
+ /* The internal buffer. */
+ std::string m_string;
+
+ bool m_term_out;
+};
+
+/* A ui_file implementation that maps directly onto <stdio.h>'s FILE.
+ A stdio_file can either own its underlying file, or not. If it
+ owns the file, then destroying the stdio_file closes the underlying
+ file, otherwise it is left open. */
+
+class stdio_file : public ui_file
+{
+public:
+ /* Create a ui_file from a previously opened FILE. CLOSE_P
+ indicates whether the underlying file should be closed when the
+ stdio_file is destroyed. */
+ explicit stdio_file (FILE *file, bool close_p = false);
+
+ /* Create an stdio_file that is not managing any file yet. Call
+ open to actually open something. */
+ stdio_file ();
+
+ ~stdio_file () override;
+
+ /* Open NAME in mode MODE, and own the resulting file. Returns true
+ on success, false otherwise. If the stdio_file previously owned
+ a file, it is closed. */
+ bool open (const char *name, const char *mode);
+
+ void flush () override;
+
+ void write (const char *buf, long length_buf) override;
+
+ void write_async_safe (const char *buf, long length_buf) override;
+
+ void puts (const char *) override;
+
+ long read (char *buf, long length_buf) override;
+
+ bool isatty () override;
+
+ bool can_emit_style_escape () override;
+
+private:
+ /* Sets the internal stream to FILE, and saves the FILE's file
+ descriptor in M_FD. */
+ void set_stream (FILE *file);
+
+ /* The file. */
+ FILE *m_file;
+
+ /* The associated file descriptor is extracted ahead of time for
+ stdio_file::write_async_safe's benefit, in case fileno isn't
+ async-safe. */
+ int m_fd;
+
+ /* If true, M_FILE is closed on destruction. */
+ bool m_close_p;
+};
+
+typedef std::unique_ptr<stdio_file> stdio_file_up;
+
+/* Like stdio_file, but specifically for stderr.
+
+ This exists because there is no real line-buffering on Windows, see
+ <http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx>
+ so the stdout is either fully-buffered or non-buffered. We can't
+ make stdout non-buffered, because of two concerns:
+
+ 1. Non-buffering hurts performance.
+ 2. Non-buffering may change GDB's behavior when it is interacting
+ with a front-end, such as Emacs.
+
+ We leave stdout as fully buffered, but flush it first when
+ something is written to stderr.
+
+ Note that the 'write_async_safe' method is not overridden, because
+ there's no way to flush a stream in an async-safe manner.
+ Fortunately, it doesn't really matter, because:
+
+ 1. That method is only used for printing internal debug output
+ from signal handlers.
+
+ 2. Windows hosts don't have a concept of async-safeness. Signal
+ handlers run in a separate thread, so they can call the regular
+ non-async-safe output routines freely.
+*/
+class stderr_file : public stdio_file
+{
+public:
+ explicit stderr_file (FILE *stream);
+
+ /* Override the output routines to flush gdb_stdout before deferring
+ to stdio_file for the actual outputting. */
+ void write (const char *buf, long length_buf) override;
+ void puts (const char *linebuffer) override;
+};