[PATCH 12/57][Arm][GAS] Add support for MVE instructions: vaddlv and vaddv
[binutils-gdb.git] / gdb / ui-file.c
blob4139b5deffc47933e317700edb289d4360ca7a63
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. */
22 #include "defs.h"
23 #include "ui-file.h"
24 #include "gdb_obstack.h"
25 #include "gdb_select.h"
26 #include "common/filestuff.h"
28 null_file null_stream;
30 ui_file::ui_file ()
33 ui_file::~ui_file ()
36 void
37 ui_file::printf (const char *format, ...)
39 va_list args;
41 va_start (args, format);
42 vfprintf_unfiltered (this, format, args);
43 va_end (args);
46 void
47 ui_file::putstr (const char *str, int quoter)
49 fputstr_unfiltered (str, quoter, this);
52 void
53 ui_file::putstrn (const char *str, int n, int quoter)
55 fputstrn_unfiltered (str, n, quoter, fputc_unfiltered, this);
58 int
59 ui_file::putc (int c)
61 return fputc_unfiltered (c, this);
64 void
65 ui_file::vprintf (const char *format, va_list args)
67 vfprintf_unfiltered (this, format, args);
72 void
73 null_file::write (const char *buf, long sizeof_buf)
75 /* Discard the request. */
78 void
79 null_file::puts (const char *)
81 /* Discard the request. */
84 void
85 null_file::write_async_safe (const char *buf, long sizeof_buf)
87 /* Discard the request. */
92 void
93 gdb_flush (struct ui_file *file)
95 file->flush ();
98 int
99 ui_file_isatty (struct ui_file *file)
101 return file->isatty ();
104 /* true if the gdb terminal supports styling, and styling is enabled. */
106 static bool
107 term_cli_styling ()
109 extern int cli_styling;
111 if (!cli_styling)
112 return false;
114 const char *term = getenv ("TERM");
115 /* Windows doesn't by default define $TERM, but can support styles
116 regardless. */
117 #ifndef _WIN32
118 if (term == nullptr || !strcmp (term, "dumb"))
119 return false;
120 #else
121 /* But if they do define $TERM, let us behave the same as on Posix
122 platforms, for the benefit of programs which invoke GDB as their
123 back-end. */
124 if (term && !strcmp (term, "dumb"))
125 return false;
126 #endif
127 return true;
131 void
132 ui_file_write (struct ui_file *file,
133 const char *buf,
134 long length_buf)
136 file->write (buf, length_buf);
139 void
140 ui_file_write_async_safe (struct ui_file *file,
141 const char *buf,
142 long length_buf)
144 file->write_async_safe (buf, length_buf);
147 long
148 ui_file_read (struct ui_file *file, char *buf, long length_buf)
150 return file->read (buf, length_buf);
153 void
154 fputs_unfiltered (const char *buf, struct ui_file *file)
156 file->puts (buf);
161 string_file::~string_file ()
164 void
165 string_file::write (const char *buf, long length_buf)
167 m_string.append (buf, length_buf);
170 /* See ui-file.h. */
172 bool
173 string_file::term_out ()
175 return m_term_out;
178 /* See ui-file.h. */
180 bool
181 string_file::can_emit_style_escape ()
183 return m_term_out && term_cli_styling ();
188 stdio_file::stdio_file (FILE *file, bool close_p)
190 set_stream (file);
191 m_close_p = close_p;
194 stdio_file::stdio_file ()
195 : m_file (NULL),
196 m_fd (-1),
197 m_close_p (false)
200 stdio_file::~stdio_file ()
202 if (m_close_p)
203 fclose (m_file);
206 void
207 stdio_file::set_stream (FILE *file)
209 m_file = file;
210 m_fd = fileno (file);
213 bool
214 stdio_file::open (const char *name, const char *mode)
216 /* Close the previous stream, if we own it. */
217 if (m_close_p)
219 fclose (m_file);
220 m_close_p = false;
223 gdb_file_up f = gdb_fopen_cloexec (name, mode);
225 if (f == NULL)
226 return false;
228 set_stream (f.release ());
229 m_close_p = true;
231 return true;
234 void
235 stdio_file::flush ()
237 fflush (m_file);
240 long
241 stdio_file::read (char *buf, long length_buf)
243 /* Wait until at least one byte of data is available, or we get
244 interrupted with Control-C. */
246 fd_set readfds;
248 FD_ZERO (&readfds);
249 FD_SET (m_fd, &readfds);
250 if (interruptible_select (m_fd + 1, &readfds, NULL, NULL, NULL) == -1)
251 return -1;
254 return ::read (m_fd, buf, length_buf);
257 void
258 stdio_file::write (const char *buf, long length_buf)
260 /* Calling error crashes when we are called from the exception framework. */
261 if (fwrite (buf, length_buf, 1, m_file))
263 /* Nothing. */
267 void
268 stdio_file::write_async_safe (const char *buf, long length_buf)
270 /* This is written the way it is to avoid a warning from gcc about not using the
271 result of write (since it can be declared with attribute warn_unused_result).
272 Alas casting to void doesn't work for this. */
273 if (::write (m_fd, buf, length_buf))
275 /* Nothing. */
279 void
280 stdio_file::puts (const char *linebuffer)
282 /* This host-dependent function (with implementations in
283 posix-hdep.c and mingw-hdep.c) is given the opportunity to
284 process the output first in host-dependent way. If it does, it
285 should return non-zero, to avoid calling fputs below. */
286 if (gdb_console_fputs (linebuffer, m_file))
287 return;
288 /* Calling error crashes when we are called from the exception framework. */
289 if (fputs (linebuffer, m_file))
291 /* Nothing. */
295 bool
296 stdio_file::isatty ()
298 return ::isatty (m_fd);
301 /* See ui-file.h. */
303 bool
304 stdio_file::can_emit_style_escape ()
306 return (this == gdb_stdout
307 && this->isatty ()
308 && term_cli_styling ());
313 /* This is the implementation of ui_file method 'write' for stderr.
314 gdb_stdout is flushed before writing to gdb_stderr. */
316 void
317 stderr_file::write (const char *buf, long length_buf)
319 gdb_flush (gdb_stdout);
320 stdio_file::write (buf, length_buf);
323 /* This is the implementation of ui_file method 'puts' for stderr.
324 gdb_stdout is flushed before writing to gdb_stderr. */
326 void
327 stderr_file::puts (const char *linebuffer)
329 gdb_flush (gdb_stdout);
330 stdio_file::puts (linebuffer);
333 stderr_file::stderr_file (FILE *stream)
334 : stdio_file (stream)
339 tee_file::tee_file (ui_file *one, bool close_one,
340 ui_file *two, bool close_two)
341 : m_one (one),
342 m_two (two),
343 m_close_one (close_one),
344 m_close_two (close_two)
347 tee_file::~tee_file ()
349 if (m_close_one)
350 delete m_one;
351 if (m_close_two)
352 delete m_two;
355 void
356 tee_file::flush ()
358 m_one->flush ();
359 m_two->flush ();
362 void
363 tee_file::write (const char *buf, long length_buf)
365 m_one->write (buf, length_buf);
366 m_two->write (buf, length_buf);
369 void
370 tee_file::write_async_safe (const char *buf, long length_buf)
372 m_one->write_async_safe (buf, length_buf);
373 m_two->write_async_safe (buf, length_buf);
376 void
377 tee_file::puts (const char *linebuffer)
379 m_one->puts (linebuffer);
380 m_two->puts (linebuffer);
383 bool
384 tee_file::isatty ()
386 return m_one->isatty ();
389 /* See ui-file.h. */
391 bool
392 tee_file::term_out ()
394 return m_one->term_out ();
397 /* See ui-file.h. */
399 bool
400 tee_file::can_emit_style_escape ()
402 return (this == gdb_stdout
403 && m_one->term_out ()
404 && term_cli_styling ());