Rename common to gdbsupport
[deliverable/binutils-gdb.git] / gdb / ui-file.c
... / ...
CommitLineData
1/* UI_FILE - a generic STDIO like output stream.
2
3 Copyright (C) 1999-2019 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20/* Implement the ``struct ui_file'' object. */
21
22#include "defs.h"
23#include "ui-file.h"
24#include "gdb_obstack.h"
25#include "gdb_select.h"
26#include "gdbsupport/filestuff.h"
27
28null_file null_stream;
29
30ui_file::ui_file ()
31{}
32
33ui_file::~ui_file ()
34{}
35
36void
37ui_file::printf (const char *format, ...)
38{
39 va_list args;
40
41 va_start (args, format);
42 vfprintf_unfiltered (this, format, args);
43 va_end (args);
44}
45
46void
47ui_file::putstr (const char *str, int quoter)
48{
49 fputstr_unfiltered (str, quoter, this);
50}
51
52void
53ui_file::putstrn (const char *str, int n, int quoter)
54{
55 fputstrn_unfiltered (str, n, quoter, fputc_unfiltered, this);
56}
57
58int
59ui_file::putc (int c)
60{
61 return fputc_unfiltered (c, this);
62}
63
64void
65ui_file::vprintf (const char *format, va_list args)
66{
67 vfprintf_unfiltered (this, format, args);
68}
69
70\f
71
72void
73null_file::write (const char *buf, long sizeof_buf)
74{
75 /* Discard the request. */
76}
77
78void
79null_file::puts (const char *)
80{
81 /* Discard the request. */
82}
83
84void
85null_file::write_async_safe (const char *buf, long sizeof_buf)
86{
87 /* Discard the request. */
88}
89
90\f
91
92void
93gdb_flush (struct ui_file *file)
94{
95 file->flush ();
96}
97
98int
99ui_file_isatty (struct ui_file *file)
100{
101 return file->isatty ();
102}
103
104/* true if the gdb terminal supports styling, and styling is enabled. */
105
106static bool
107term_cli_styling ()
108{
109 extern int cli_styling;
110
111 if (!cli_styling)
112 return false;
113
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;
128}
129
130
131void
132ui_file_write (struct ui_file *file,
133 const char *buf,
134 long length_buf)
135{
136 file->write (buf, length_buf);
137}
138
139void
140ui_file_write_async_safe (struct ui_file *file,
141 const char *buf,
142 long length_buf)
143{
144 file->write_async_safe (buf, length_buf);
145}
146
147long
148ui_file_read (struct ui_file *file, char *buf, long length_buf)
149{
150 return file->read (buf, length_buf);
151}
152
153void
154fputs_unfiltered (const char *buf, struct ui_file *file)
155{
156 file->puts (buf);
157}
158
159\f
160
161string_file::~string_file ()
162{}
163
164void
165string_file::write (const char *buf, long length_buf)
166{
167 m_string.append (buf, length_buf);
168}
169
170/* See ui-file.h. */
171
172bool
173string_file::term_out ()
174{
175 return m_term_out;
176}
177
178/* See ui-file.h. */
179
180bool
181string_file::can_emit_style_escape ()
182{
183 return m_term_out && term_cli_styling ();
184}
185
186\f
187
188stdio_file::stdio_file (FILE *file, bool close_p)
189{
190 set_stream (file);
191 m_close_p = close_p;
192}
193
194stdio_file::stdio_file ()
195 : m_file (NULL),
196 m_fd (-1),
197 m_close_p (false)
198{}
199
200stdio_file::~stdio_file ()
201{
202 if (m_close_p)
203 fclose (m_file);
204}
205
206void
207stdio_file::set_stream (FILE *file)
208{
209 m_file = file;
210 m_fd = fileno (file);
211}
212
213bool
214stdio_file::open (const char *name, const char *mode)
215{
216 /* Close the previous stream, if we own it. */
217 if (m_close_p)
218 {
219 fclose (m_file);
220 m_close_p = false;
221 }
222
223 gdb_file_up f = gdb_fopen_cloexec (name, mode);
224
225 if (f == NULL)
226 return false;
227
228 set_stream (f.release ());
229 m_close_p = true;
230
231 return true;
232}
233
234void
235stdio_file::flush ()
236{
237 fflush (m_file);
238}
239
240long
241stdio_file::read (char *buf, long length_buf)
242{
243 /* Wait until at least one byte of data is available, or we get
244 interrupted with Control-C. */
245 {
246 fd_set readfds;
247
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;
252 }
253
254 return ::read (m_fd, buf, length_buf);
255}
256
257void
258stdio_file::write (const char *buf, long length_buf)
259{
260 /* Calling error crashes when we are called from the exception framework. */
261 if (fwrite (buf, length_buf, 1, m_file))
262 {
263 /* Nothing. */
264 }
265}
266
267void
268stdio_file::write_async_safe (const char *buf, long length_buf)
269{
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))
274 {
275 /* Nothing. */
276 }
277}
278
279void
280stdio_file::puts (const char *linebuffer)
281{
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))
290 {
291 /* Nothing. */
292 }
293}
294
295bool
296stdio_file::isatty ()
297{
298 return ::isatty (m_fd);
299}
300
301/* See ui-file.h. */
302
303bool
304stdio_file::can_emit_style_escape ()
305{
306 return (this == gdb_stdout
307 && this->isatty ()
308 && term_cli_styling ());
309}
310
311\f
312
313/* This is the implementation of ui_file method 'write' for stderr.
314 gdb_stdout is flushed before writing to gdb_stderr. */
315
316void
317stderr_file::write (const char *buf, long length_buf)
318{
319 gdb_flush (gdb_stdout);
320 stdio_file::write (buf, length_buf);
321}
322
323/* This is the implementation of ui_file method 'puts' for stderr.
324 gdb_stdout is flushed before writing to gdb_stderr. */
325
326void
327stderr_file::puts (const char *linebuffer)
328{
329 gdb_flush (gdb_stdout);
330 stdio_file::puts (linebuffer);
331}
332
333stderr_file::stderr_file (FILE *stream)
334 : stdio_file (stream)
335{}
336
337\f
338
339tee_file::tee_file (ui_file *one, ui_file_up &&two)
340 : m_one (one),
341 m_two (std::move (two))
342{}
343
344tee_file::~tee_file ()
345{
346}
347
348void
349tee_file::flush ()
350{
351 m_one->flush ();
352 m_two->flush ();
353}
354
355void
356tee_file::write (const char *buf, long length_buf)
357{
358 m_one->write (buf, length_buf);
359 m_two->write (buf, length_buf);
360}
361
362void
363tee_file::write_async_safe (const char *buf, long length_buf)
364{
365 m_one->write_async_safe (buf, length_buf);
366 m_two->write_async_safe (buf, length_buf);
367}
368
369void
370tee_file::puts (const char *linebuffer)
371{
372 m_one->puts (linebuffer);
373 m_two->puts (linebuffer);
374}
375
376bool
377tee_file::isatty ()
378{
379 return m_one->isatty ();
380}
381
382/* See ui-file.h. */
383
384bool
385tee_file::term_out ()
386{
387 return m_one->term_out ();
388}
389
390/* See ui-file.h. */
391
392bool
393tee_file::can_emit_style_escape ()
394{
395 return (this == gdb_stdout
396 && m_one->term_out ()
397 && term_cli_styling ());
398}
399
400/* See ui-file.h. */
401
402void
403no_terminal_escape_file::write (const char *buf, long length_buf)
404{
405 std::string copy (buf, length_buf);
406 this->puts (copy.c_str ());
407}
408
409/* See ui-file.h. */
410
411void
412no_terminal_escape_file::puts (const char *buf)
413{
414 while (*buf != '\0')
415 {
416 const char *esc = strchr (buf, '\033');
417 if (esc == nullptr)
418 break;
419
420 int n_read = 0;
421 if (!skip_ansi_escape (esc, &n_read))
422 ++esc;
423
424 this->stdio_file::write (buf, esc - buf);
425 buf = esc + n_read;
426 }
427
428 if (*buf != '\0')
429 this->stdio_file::write (buf, strlen (buf));
430}
This page took 0.026262 seconds and 4 git commands to generate.