1 /* UI_FILE - a generic STDIO like output stream.
3 Copyright (C) 1999-2020 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 "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
)
50 fputstr_unfiltered (str
, quoter
, this);
54 ui_file::putstrn (const char *str
, int n
, int quoter
)
56 fputstrn_unfiltered (str
, n
, quoter
, fputc_unfiltered
, this);
62 return fputc_unfiltered (c
, this);
66 ui_file::vprintf (const char *format
, va_list args
)
68 vfprintf_unfiltered (this, format
, args
);
74 null_file::write (const char *buf
, long sizeof_buf
)
76 /* Discard the request. */
80 null_file::puts (const char *)
82 /* Discard the request. */
86 null_file::write_async_safe (const char *buf
, long sizeof_buf
)
88 /* Discard the request. */
94 gdb_flush (struct ui_file
*file
)
100 ui_file_isatty (struct ui_file
*file
)
102 return file
->isatty ();
105 /* true if the gdb terminal supports styling, and styling is enabled. */
113 const char *term
= getenv ("TERM");
114 /* Windows doesn't by default define $TERM, but can support styles
117 if (term
== nullptr || !strcmp (term
, "dumb"))
120 /* But if they do define $TERM, let us behave the same as on Posix
121 platforms, for the benefit of programs which invoke GDB as their
123 if (term
&& !strcmp (term
, "dumb"))
131 ui_file_write (struct ui_file
*file
,
135 file
->write (buf
, length_buf
);
139 ui_file_write_async_safe (struct ui_file
*file
,
143 file
->write_async_safe (buf
, length_buf
);
147 ui_file_read (struct ui_file
*file
, char *buf
, long length_buf
)
149 return file
->read (buf
, length_buf
);
153 fputs_unfiltered (const char *buf
, struct ui_file
*file
)
160 string_file::~string_file ()
164 string_file::write (const char *buf
, long length_buf
)
166 m_string
.append (buf
, length_buf
);
172 string_file::term_out ()
180 string_file::can_emit_style_escape ()
182 return m_term_out
&& term_cli_styling ();
187 stdio_file::stdio_file (FILE *file
, bool close_p
)
193 stdio_file::stdio_file ()
199 stdio_file::~stdio_file ()
206 stdio_file::set_stream (FILE *file
)
209 m_fd
= fileno (file
);
213 stdio_file::open (const char *name
, const char *mode
)
215 /* Close the previous stream, if we own it. */
222 gdb_file_up f
= gdb_fopen_cloexec (name
, mode
);
227 set_stream (f
.release ());
240 stdio_file::read (char *buf
, long length_buf
)
242 /* Wait until at least one byte of data is available, or we get
243 interrupted with Control-C. */
248 FD_SET (m_fd
, &readfds
);
249 if (interruptible_select (m_fd
+ 1, &readfds
, NULL
, NULL
, NULL
) == -1)
253 return ::read (m_fd
, buf
, length_buf
);
257 stdio_file::write (const char *buf
, long length_buf
)
259 /* Calling error crashes when we are called from the exception framework. */
260 if (fwrite (buf
, length_buf
, 1, m_file
))
267 stdio_file::write_async_safe (const char *buf
, long length_buf
)
269 /* This is written the way it is to avoid a warning from gcc about not using the
270 result of write (since it can be declared with attribute warn_unused_result).
271 Alas casting to void doesn't work for this. */
272 if (::write (m_fd
, buf
, length_buf
))
279 stdio_file::puts (const char *linebuffer
)
281 /* This host-dependent function (with implementations in
282 posix-hdep.c and mingw-hdep.c) is given the opportunity to
283 process the output first in host-dependent way. If it does, it
284 should return non-zero, to avoid calling fputs below. */
285 if (gdb_console_fputs (linebuffer
, m_file
))
287 /* Calling error crashes when we are called from the exception framework. */
288 if (fputs (linebuffer
, m_file
))
295 stdio_file::isatty ()
297 return ::isatty (m_fd
);
303 stdio_file::can_emit_style_escape ()
305 return ((this == gdb_stdout
|| this == gdb_stderr
)
307 && term_cli_styling ());
312 /* This is the implementation of ui_file method 'write' for stderr.
313 gdb_stdout is flushed before writing to gdb_stderr. */
316 stderr_file::write (const char *buf
, long length_buf
)
318 gdb_flush (gdb_stdout
);
319 stdio_file::write (buf
, length_buf
);
322 /* This is the implementation of ui_file method 'puts' for stderr.
323 gdb_stdout is flushed before writing to gdb_stderr. */
326 stderr_file::puts (const char *linebuffer
)
328 gdb_flush (gdb_stdout
);
329 stdio_file::puts (linebuffer
);
332 stderr_file::stderr_file (FILE *stream
)
333 : stdio_file (stream
)
338 tee_file::tee_file (ui_file
*one
, ui_file_up
&&two
)
340 m_two (std::move (two
))
343 tee_file::~tee_file ()
355 tee_file::write (const char *buf
, long length_buf
)
357 m_one
->write (buf
, length_buf
);
358 m_two
->write (buf
, length_buf
);
362 tee_file::write_async_safe (const char *buf
, long length_buf
)
364 m_one
->write_async_safe (buf
, length_buf
);
365 m_two
->write_async_safe (buf
, length_buf
);
369 tee_file::puts (const char *linebuffer
)
371 m_one
->puts (linebuffer
);
372 m_two
->puts (linebuffer
);
378 return m_one
->isatty ();
384 tee_file::term_out ()
386 return m_one
->term_out ();
392 tee_file::can_emit_style_escape ()
394 return ((this == gdb_stdout
|| this == gdb_stderr
)
395 && m_one
->term_out ()
396 && term_cli_styling ());
402 no_terminal_escape_file::write (const char *buf
, long length_buf
)
404 std::string
copy (buf
, length_buf
);
405 this->puts (copy
.c_str ());
411 no_terminal_escape_file::puts (const char *buf
)
415 const char *esc
= strchr (buf
, '\033');
420 if (!skip_ansi_escape (esc
, &n_read
))
423 this->stdio_file::write (buf
, esc
- buf
);
428 this->stdio_file::write (buf
, strlen (buf
));
This page took 0.054494 seconds and 4 git commands to generate.