]> Git Repo - binutils.git/blob - gdb/ui-file.c
gdb: remove SYMBOL_CLASS macro, add getter
[binutils.git] / gdb / ui-file.c
1 /* UI_FILE - a generic STDIO like output stream.
2
3    Copyright (C) 1999-2022 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 "gdbsupport/gdb_obstack.h"
25 #include "gdbsupport/gdb_select.h"
26 #include "gdbsupport/filestuff.h"
27 #include "cli/cli-style.h"
28
29 null_file null_stream;
30
31 ui_file::ui_file ()
32 {}
33
34 ui_file::~ui_file ()
35 {}
36
37 void
38 ui_file::printf (const char *format, ...)
39 {
40   va_list args;
41
42   va_start (args, format);
43   vfprintf_unfiltered (this, format, args);
44   va_end (args);
45 }
46
47 void
48 ui_file::putstr (const char *str, int quoter)
49 {
50   while (*str)
51     printchar (*str++, quoter, false);
52 }
53
54 void
55 ui_file::putstrn (const char *str, int n, int quoter, bool async_safe)
56 {
57   for (int i = 0; i < n; i++)
58     printchar (str[i], quoter, async_safe);
59 }
60
61 int
62 ui_file::putc (int c)
63 {
64   return fputc_unfiltered (c, this);
65 }
66
67 void
68 ui_file::vprintf (const char *format, va_list args)
69 {
70   vfprintf_unfiltered (this, format, args);
71 }
72
73 /* See ui-file.h.  */
74
75 void
76 ui_file::printchar (int c, int quoter, bool async_safe)
77 {
78   char buf[4];
79   int out = 0;
80
81   c &= 0xFF;                    /* Avoid sign bit follies */
82
83   if (c < 0x20                   /* Low control chars */
84       || (c >= 0x7F && c < 0xA0) /* DEL, High controls */
85       || (sevenbit_strings && c >= 0x80))
86     {                           /* high order bit set */
87       buf[out++] = '\\';
88
89       switch (c)
90         {
91         case '\n':
92           buf[out++] = 'n';
93           break;
94         case '\b':
95           buf[out++] = 'b';
96           break;
97         case '\t':
98           buf[out++] = 't';
99           break;
100         case '\f':
101           buf[out++] = 'f';
102           break;
103         case '\r':
104           buf[out++] = 'r';
105           break;
106         case '\033':
107           buf[out++] = 'e';
108           break;
109         case '\007':
110           buf[out++] = 'a';
111           break;
112         default:
113           {
114             buf[out++] = '0' + ((c >> 6) & 0x7);
115             buf[out++] = '0' + ((c >> 3) & 0x7);
116             buf[out++] = '0' + ((c >> 0) & 0x7);
117             break;
118           }
119         }
120     }
121   else
122     {
123       if (quoter != 0 && (c == '\\' || c == quoter))
124         buf[out++] = '\\';
125       buf[out++] = c;
126     }
127
128   if (async_safe)
129     this->write_async_safe (buf, out);
130   else
131     this->write (buf, out);
132 }
133
134 \f
135
136 void
137 null_file::write (const char *buf, long sizeof_buf)
138 {
139   /* Discard the request.  */
140 }
141
142 void
143 null_file::puts (const char *)
144 {
145   /* Discard the request.  */
146 }
147
148 void
149 null_file::write_async_safe (const char *buf, long sizeof_buf)
150 {
151   /* Discard the request.  */
152 }
153
154 \f
155
156 /* true if the gdb terminal supports styling, and styling is enabled.  */
157
158 static bool
159 term_cli_styling ()
160 {
161   if (!cli_styling)
162     return false;
163
164   const char *term = getenv ("TERM");
165   /* Windows doesn't by default define $TERM, but can support styles
166      regardless.  */
167 #ifndef _WIN32
168   if (term == nullptr || !strcmp (term, "dumb"))
169     return false;
170 #else
171   /* But if they do define $TERM, let us behave the same as on Posix
172      platforms, for the benefit of programs which invoke GDB as their
173      back-end.  */
174   if (term && !strcmp (term, "dumb"))
175     return false;
176 #endif
177   return true;
178 }
179
180 \f
181
182 string_file::~string_file ()
183 {}
184
185 void
186 string_file::write (const char *buf, long length_buf)
187 {
188   m_string.append (buf, length_buf);
189 }
190
191 /* See ui-file.h.  */
192
193 bool
194 string_file::term_out ()
195 {
196   return m_term_out;
197 }
198
199 /* See ui-file.h.  */
200
201 bool
202 string_file::can_emit_style_escape ()
203 {
204   return m_term_out && term_cli_styling ();
205 }
206
207 \f
208
209 stdio_file::stdio_file (FILE *file, bool close_p)
210 {
211   set_stream (file);
212   m_close_p = close_p;
213 }
214
215 stdio_file::stdio_file ()
216   : m_file (NULL),
217     m_fd (-1),
218     m_close_p (false)
219 {}
220
221 stdio_file::~stdio_file ()
222 {
223   if (m_close_p)
224     fclose (m_file);
225 }
226
227 void
228 stdio_file::set_stream (FILE *file)
229 {
230   m_file = file;
231   m_fd = fileno (file);
232 }
233
234 bool
235 stdio_file::open (const char *name, const char *mode)
236 {
237   /* Close the previous stream, if we own it.  */
238   if (m_close_p)
239     {
240       fclose (m_file);
241       m_close_p = false;
242     }
243
244   gdb_file_up f = gdb_fopen_cloexec (name, mode);
245
246   if (f == NULL)
247     return false;
248
249   set_stream (f.release ());
250   m_close_p = true;
251
252   return true;
253 }
254
255 void
256 stdio_file::flush ()
257 {
258   fflush (m_file);
259 }
260
261 long
262 stdio_file::read (char *buf, long length_buf)
263 {
264   /* Wait until at least one byte of data is available, or we get
265      interrupted with Control-C.  */
266   {
267     fd_set readfds;
268
269     FD_ZERO (&readfds);
270     FD_SET (m_fd, &readfds);
271     if (interruptible_select (m_fd + 1, &readfds, NULL, NULL, NULL) == -1)
272       return -1;
273   }
274
275   return ::read (m_fd, buf, length_buf);
276 }
277
278 void
279 stdio_file::write (const char *buf, long length_buf)
280 {
281   /* Calling error crashes when we are called from the exception framework.  */
282   if (fwrite (buf, length_buf, 1, m_file))
283     {
284       /* Nothing.  */
285     }
286 }
287
288 void
289 stdio_file::write_async_safe (const char *buf, long length_buf)
290 {
291   /* This is written the way it is to avoid a warning from gcc about not using the
292      result of write (since it can be declared with attribute warn_unused_result).
293      Alas casting to void doesn't work for this.  */
294   if (::write (m_fd, buf, length_buf))
295     {
296       /* Nothing.  */
297     }
298 }
299
300 void
301 stdio_file::puts (const char *linebuffer)
302 {
303   /* This host-dependent function (with implementations in
304      posix-hdep.c and mingw-hdep.c) is given the opportunity to
305      process the output first in host-dependent way.  If it does, it
306      should return non-zero, to avoid calling fputs below.  */
307   if (gdb_console_fputs (linebuffer, m_file))
308     return;
309   /* Calling error crashes when we are called from the exception framework.  */
310   if (fputs (linebuffer, m_file))
311     {
312       /* Nothing.  */
313     }
314 }
315
316 bool
317 stdio_file::isatty ()
318 {
319   return ::isatty (m_fd);
320 }
321
322 /* See ui-file.h.  */
323
324 bool
325 stdio_file::can_emit_style_escape ()
326 {
327   return ((this == gdb_stdout || this == gdb_stderr)
328           && this->isatty ()
329           && term_cli_styling ());
330 }
331
332 \f
333
334 /* This is the implementation of ui_file method 'write' for stderr.
335    gdb_stdout is flushed before writing to gdb_stderr.  */
336
337 void
338 stderr_file::write (const char *buf, long length_buf)
339 {
340   gdb_stdout->flush ();
341   stdio_file::write (buf, length_buf);
342 }
343
344 /* This is the implementation of ui_file method 'puts' for stderr.
345    gdb_stdout is flushed before writing to gdb_stderr.  */
346
347 void
348 stderr_file::puts (const char *linebuffer)
349 {
350   gdb_stdout->flush ();
351   stdio_file::puts (linebuffer);
352 }
353
354 stderr_file::stderr_file (FILE *stream)
355   : stdio_file (stream)
356 {}
357
358 \f
359
360 tee_file::tee_file (ui_file *one, ui_file_up &&two)
361   : m_one (one),
362     m_two (std::move (two))
363 {}
364
365 tee_file::~tee_file ()
366 {
367 }
368
369 void
370 tee_file::flush ()
371 {
372   m_one->flush ();
373   m_two->flush ();
374 }
375
376 void
377 tee_file::write (const char *buf, long length_buf)
378 {
379   m_one->write (buf, length_buf);
380   m_two->write (buf, length_buf);
381 }
382
383 void
384 tee_file::write_async_safe (const char *buf, long length_buf)
385 {
386   m_one->write_async_safe (buf, length_buf);
387   m_two->write_async_safe (buf, length_buf);
388 }
389
390 void
391 tee_file::puts (const char *linebuffer)
392 {
393   m_one->puts (linebuffer);
394   m_two->puts (linebuffer);
395 }
396
397 bool
398 tee_file::isatty ()
399 {
400   return m_one->isatty ();
401 }
402
403 /* See ui-file.h.  */
404
405 bool
406 tee_file::term_out ()
407 {
408   return m_one->term_out ();
409 }
410
411 /* See ui-file.h.  */
412
413 bool
414 tee_file::can_emit_style_escape ()
415 {
416   return ((this == gdb_stdout || this == gdb_stderr)
417           && m_one->term_out ()
418           && term_cli_styling ());
419 }
420
421 /* See ui-file.h.  */
422
423 void
424 no_terminal_escape_file::write (const char *buf, long length_buf)
425 {
426   std::string copy (buf, length_buf);
427   this->puts (copy.c_str ());
428 }
429
430 /* See ui-file.h.  */
431
432 void
433 no_terminal_escape_file::puts (const char *buf)
434 {
435   while (*buf != '\0')
436     {
437       const char *esc = strchr (buf, '\033');
438       if (esc == nullptr)
439         break;
440
441       int n_read = 0;
442       if (!skip_ansi_escape (esc, &n_read))
443         ++esc;
444
445       this->stdio_file::write (buf, esc - buf);
446       buf = esc + n_read;
447     }
448
449   if (*buf != '\0')
450     this->stdio_file::write (buf, strlen (buf));
451 }
This page took 0.048528 seconds and 4 git commands to generate.