GDB (xrefs)
/tmp/gdb-8.1/gdb/ui-file.c
Go to the documentation of this file.
1 /* UI_FILE - a generic STDIO like output stream.
2 
3  Copyright (C) 1999-2018 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 "filestuff.h"
27 
29 
31 {}
32 
34 {}
35 
36 void
37 ui_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 
46 void
47 ui_file::putstr (const char *str, int quoter)
48 {
49  fputstr_unfiltered (str, quoter, this);
50 }
51 
52 void
53 ui_file::putstrn (const char *str, int n, int quoter)
54 {
55  fputstrn_unfiltered (str, n, quoter, this);
56 }
57 
58 int
60 {
61  return fputc_unfiltered (c, this);
62 }
63 
64 void
65 ui_file::vprintf (const char *format, va_list args)
66 {
67  vfprintf_unfiltered (this, format, args);
68 }
69 
70 
71 
72 void
73 null_file::write (const char *buf, long sizeof_buf)
74 {
75  /* Discard the request. */
76 }
77 
78 void
79 null_file::puts (const char *)
80 {
81  /* Discard the request. */
82 }
83 
84 void
85 null_file::write_async_safe (const char *buf, long sizeof_buf)
86 {
87  /* Discard the request. */
88 }
89 
90 
91 
92 void
93 gdb_flush (struct ui_file *file)
94 {
95  file->flush ();
96 }
97 
98 int
99 ui_file_isatty (struct ui_file *file)
100 {
101  return file->isatty ();
102 }
103 
104 void
105 ui_file_write (struct ui_file *file,
106  const char *buf,
107  long length_buf)
108 {
109  file->write (buf, length_buf);
110 }
111 
112 void
114  const char *buf,
115  long length_buf)
116 {
117  file->write_async_safe (buf, length_buf);
118 }
119 
120 long
121 ui_file_read (struct ui_file *file, char *buf, long length_buf)
122 {
123  return file->read (buf, length_buf);
124 }
125 
126 void
127 fputs_unfiltered (const char *buf, struct ui_file *file)
128 {
129  file->puts (buf);
130 }
131 
132 
133 
135 {}
136 
137 void
138 string_file::write (const char *buf, long length_buf)
139 {
140  m_string.append (buf, length_buf);
141 }
142 
143 
144 
145 stdio_file::stdio_file (FILE *file, bool close_p)
146 {
147  set_stream (file);
148  m_close_p = close_p;
149 }
150 
152  : m_file (NULL),
153  m_fd (-1),
154  m_close_p (false)
155 {}
156 
158 {
159  if (m_close_p)
160  fclose (m_file);
161 }
162 
163 void
165 {
166  m_file = file;
167  m_fd = fileno (file);
168 }
169 
170 bool
171 stdio_file::open (const char *name, const char *mode)
172 {
173  /* Close the previous stream, if we own it. */
174  if (m_close_p)
175  {
176  fclose (m_file);
177  m_close_p = false;
178  }
179 
180  gdb_file_up f = gdb_fopen_cloexec (name, mode);
181 
182  if (f == NULL)
183  return false;
184 
185  set_stream (f.release ());
186  m_close_p = true;
187 
188  return true;
189 }
190 
191 void
193 {
194  fflush (m_file);
195 }
196 
197 long
198 stdio_file::read (char *buf, long length_buf)
199 {
200  /* Wait until at least one byte of data is available, or we get
201  interrupted with Control-C. */
202  {
203  fd_set readfds;
204 
205  FD_ZERO (&readfds);
206  FD_SET (m_fd, &readfds);
207  if (interruptible_select (m_fd + 1, &readfds, NULL, NULL, NULL) == -1)
208  return -1;
209  }
210 
211  return ::read (m_fd, buf, length_buf);
212 }
213 
214 void
215 stdio_file::write (const char *buf, long length_buf)
216 {
217  /* Calling error crashes when we are called from the exception framework. */
218  if (fwrite (buf, length_buf, 1, m_file))
219  {
220  /* Nothing. */
221  }
222 }
223 
224 void
225 stdio_file::write_async_safe (const char *buf, long length_buf)
226 {
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))
231  {
232  /* Nothing. */
233  }
234 }
235 
236 void
237 stdio_file::puts (const char *linebuffer)
238 {
239  /* Calling error crashes when we are called from the exception framework. */
240  if (fputs (linebuffer, m_file))
241  {
242  /* Nothing. */
243  }
244 }
245 
246 bool
248 {
249  return ::isatty (m_fd);
250 }
251 
252 
253 
254 /* This is the implementation of ui_file method 'write' for stderr.
255  gdb_stdout is flushed before writing to gdb_stderr. */
256 
257 void
258 stderr_file::write (const char *buf, long length_buf)
259 {
261  stdio_file::write (buf, length_buf);
262 }
263 
264 /* This is the implementation of ui_file method 'puts' for stderr.
265  gdb_stdout is flushed before writing to gdb_stderr. */
266 
267 void
268 stderr_file::puts (const char *linebuffer)
269 {
271  stdio_file::puts (linebuffer);
272 }
273 
275  : stdio_file (stream)
276 {}
277 
278 
279 
280 tee_file::tee_file (ui_file *one, bool close_one,
281  ui_file *two, bool close_two)
282  : m_one (one),
283  m_two (two),
284  m_close_one (close_one),
285  m_close_two (close_two)
286 {}
287 
289 {
290  if (m_close_one)
291  delete m_one;
292  if (m_close_two)
293  delete m_two;
294 }
295 
296 void
298 {
299  m_one->flush ();
300  m_two->flush ();
301 }
302 
303 void
304 tee_file::write (const char *buf, long length_buf)
305 {
306  m_one->write (buf, length_buf);
307  m_two->write (buf, length_buf);
308 }
309 
310 void
311 tee_file::write_async_safe (const char *buf, long length_buf)
312 {
313  m_one->write_async_safe (buf, length_buf);
314  m_two->write_async_safe (buf, length_buf);
315 }
316 
317 void
318 tee_file::puts (const char *linebuffer)
319 {
320  m_one->puts (linebuffer);
321  m_two->puts (linebuffer);
322 }
323 
324 bool
326 {
327  return m_one->isatty ();
328 }
void virtual void write(const char *buf, long length_buf)=0
ssize_t read(int fd, void *buf, size_t count)
Definition: expect-read1.c:26
bool m_close_p
Definition: ui-file.h:197
void fputs_unfiltered(const char *buf, struct ui_file *file)
Definition: ui-file.c:127
~tee_file() override
Definition: ui-file.c:288
int ui_file_isatty(struct ui_file *file)
Definition: ui-file.c:99
void flush() override
Definition: ui-file.c:297
stderr_file(FILE *stream)
Definition: ui-file.c:274
void puts(const char *linebuffer) override
Definition: ui-file.c:268
virtual bool isatty()
Definition: ui-file.h:69
void write(const char *buf, long length_buf) override
Definition: ui-file.c:138
bool m_close_two
Definition: ui-file.h:261
void write_async_safe(const char *buf, long sizeof_buf) override
Definition: ui-file.c:85
void flush() override
Definition: ui-file.c:192
stdio_file()
Definition: ui-file.c:151
const char *const name
Definition: aarch64-tdep.c:76
void fputstr_unfiltered(const char *str, int quoter, struct ui_file *stream)
Definition: utils.c:1261
ui_file * m_two
Definition: ui-file.h:260
std::unique_ptr< FILE, gdb_file_deleter > gdb_file_up
Definition: filestuff.h:59
ui_file * m_one
Definition: ui-file.h:260
null_file null_stream
Definition: ui-file.c:28
int fputc_unfiltered(int c, struct ui_file *stream)
Definition: utils.c:1835
void ui_file_write_async_safe(struct ui_file *file, const char *buf, long length_buf)
Definition: ui-file.c:113
void void putstr(const char *str, int quoter)
Definition: ui-file.c:47
void vfprintf_unfiltered(struct ui_file *stream, const char *format, va_list args)
Definition: utils.c:1969
void fputstrn_unfiltered(const char *str, int n, int quoter, struct ui_file *stream)
Definition: utils.c:1278
void write(const char *buf, long length_buf) override
Definition: ui-file.c:215
long read(char *buf, long length_buf) override
Definition: ui-file.c:198
bool isatty() override
Definition: ui-file.c:247
gdb_file_up gdb_fopen_cloexec(const char *filename, const char *opentype)
Definition: filestuff.c:296
virtual void puts(const char *str)
Definition: ui-file.h:63
int m_fd
Definition: ui-file.h:194
void write(const char *buf, long length_buf) override
Definition: ui-file.c:258
ui_file()
Definition: ui-file.c:30
virtual long read(char *buf, long length_buf)
Definition: ui-file.h:66
void printf(const char *,...) ATTRIBUTE_PRINTF(2
Definition: ui-file.c:37
virtual void write_async_safe(const char *buf, long length_buf)
Definition: ui-file.h:58
bool open(const char *name, const char *mode)
Definition: ui-file.c:171
~stdio_file() override
Definition: ui-file.c:157
void ui_file_write(struct ui_file *file, const char *buf, long length_buf)
Definition: ui-file.c:105
long ui_file_read(struct ui_file *file, char *buf, long length_buf)
Definition: ui-file.c:121
virtual void flush()
Definition: ui-file.h:72
~string_file() override
Definition: ui-file.c:134
void write_async_safe(const char *buf, long length_buf) override
Definition: ui-file.c:311
bool m_close_one
Definition: ui-file.h:261
void puts(const char *str) override
Definition: ui-file.c:79
void write_async_safe(const char *buf, long length_buf) override
Definition: ui-file.c:225
void vprintf(const char *, va_list) ATTRIBUTE_PRINTF(2
Definition: ui-file.c:65
void puts(const char *) override
Definition: ui-file.c:237
void write(const char *buf, long length_buf) override
Definition: ui-file.c:73
int putc(int c)
Definition: ui-file.c:59
virtual ~ui_file()=0
Definition: ui-file.c:33
int interruptible_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
Definition: event-top.c:985
void gdb_flush(struct ui_file *file)
Definition: ui-file.c:93
std::string m_string
Definition: ui-file.h:144
void set_stream(FILE *file)
Definition: ui-file.c:164
void putstrn(const char *str, int n, int quoter)
Definition: ui-file.c:53
void puts(const char *) override
Definition: ui-file.c:318
tee_file(ui_file *one, bool close_one, ui_file *two, bool close_two)
Definition: ui-file.c:280
void write(const char *buf, long length_buf) override
Definition: ui-file.c:304
bool isatty() override
Definition: ui-file.c:325
FILE * m_file
Definition: ui-file.h:189
#define gdb_stdout
Definition: utils.h:340