1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2019 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 "gdb_obstack.h"
25 #include "gdb_select.h"
26 #include "common/filestuff.h"
28 null_file null_stream;
37 ui_file::printf (const char *format, ...)
41 va_start (args, format);
42 vfprintf_unfiltered (this, format, args);
47 ui_file::putstr (const char *str, int quoter)
49 fputstr_unfiltered (str, quoter, this);
53 ui_file::putstrn (const char *str, int n, int quoter)
55 fputstrn_unfiltered (str, n, quoter, fputc_unfiltered, this);
61 return fputc_unfiltered (c, this);
65 ui_file::vprintf (const char *format, va_list args)
67 vfprintf_unfiltered (this, format, args);
73 null_file::write (const char *buf, long sizeof_buf)
75 /* Discard the request. */
79 null_file::puts (const char *)
81 /* Discard the request. */
85 null_file::write_async_safe (const char *buf, long sizeof_buf)
87 /* Discard the request. */
93 gdb_flush (struct ui_file *file)
99 ui_file_isatty (struct ui_file *file)
101 return file->isatty ();
105 ui_file_write (struct ui_file *file,
109 file->write (buf, length_buf);
113 ui_file_write_async_safe (struct ui_file *file,
117 file->write_async_safe (buf, length_buf);
121 ui_file_read (struct ui_file *file, char *buf, long length_buf)
123 return file->read (buf, length_buf);
127 fputs_unfiltered (const char *buf, struct ui_file *file)
134 string_file::~string_file ()
138 string_file::write (const char *buf, long length_buf)
140 m_string.append (buf, length_buf);
145 stdio_file::stdio_file (FILE *file, bool close_p)
151 stdio_file::stdio_file ()
157 stdio_file::~stdio_file ()
164 stdio_file::set_stream (FILE *file)
167 m_fd = fileno (file);
171 stdio_file::open (const char *name, const char *mode)
173 /* Close the previous stream, if we own it. */
180 gdb_file_up f = gdb_fopen_cloexec (name, mode);
185 set_stream (f.release ());
198 stdio_file::read (char *buf, long length_buf)
200 /* Wait until at least one byte of data is available, or we get
201 interrupted with Control-C. */
206 FD_SET (m_fd, &readfds);
207 if (interruptible_select (m_fd + 1, &readfds, NULL, NULL, NULL) == -1)
211 return ::read (m_fd, buf, length_buf);
215 stdio_file::write (const char *buf, long length_buf)
217 /* Calling error crashes when we are called from the exception framework. */
218 if (fwrite (buf, length_buf, 1, m_file))
225 stdio_file::write_async_safe (const char *buf, long length_buf)
227 /* This is written the way it is to avoid a warning from gcc about not using the
228 result of write (since it can be declared with attribute warn_unused_result).
229 Alas casting to void doesn't work for this. */
230 if (::write (m_fd, buf, length_buf))
237 stdio_file::puts (const char *linebuffer)
239 /* This host-dependent function (with implementations in
240 posix-hdep.c and mingw-hdep.c) is given the opportunity to
241 process the output first in host-dependent way. If it does, it
242 should return non-zero, to avoid calling fputs below. */
243 if (gdb_console_fputs (linebuffer, m_file))
245 /* Calling error crashes when we are called from the exception framework. */
246 if (fputs (linebuffer, m_file))
253 stdio_file::isatty ()
255 return ::isatty (m_fd);
260 /* This is the implementation of ui_file method 'write' for stderr.
261 gdb_stdout is flushed before writing to gdb_stderr. */
264 stderr_file::write (const char *buf, long length_buf)
266 gdb_flush (gdb_stdout);
267 stdio_file::write (buf, length_buf);
270 /* This is the implementation of ui_file method 'puts' for stderr.
271 gdb_stdout is flushed before writing to gdb_stderr. */
274 stderr_file::puts (const char *linebuffer)
276 gdb_flush (gdb_stdout);
277 stdio_file::puts (linebuffer);
280 stderr_file::stderr_file (FILE *stream)
281 : stdio_file (stream)
286 tee_file::tee_file (ui_file *one, bool close_one,
287 ui_file *two, bool close_two)
290 m_close_one (close_one),
291 m_close_two (close_two)
294 tee_file::~tee_file ()
310 tee_file::write (const char *buf, long length_buf)
312 m_one->write (buf, length_buf);
313 m_two->write (buf, length_buf);
317 tee_file::write_async_safe (const char *buf, long length_buf)
319 m_one->write_async_safe (buf, length_buf);
320 m_two->write_async_safe (buf, length_buf);
324 tee_file::puts (const char *linebuffer)
326 m_one->puts (linebuffer);
327 m_two->puts (linebuffer);
333 return m_one->isatty ();