ui-file.h revision 1.1.1.7 1 1.1 christos /* UI_FILE - a generic STDIO like output stream.
2 1.1.1.7 christos Copyright (C) 1999-2020 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos This file is part of GDB.
5 1.1 christos
6 1.1 christos This program is free software; you can redistribute it and/or modify
7 1.1 christos it under the terms of the GNU General Public License as published by
8 1.1 christos the Free Software Foundation; either version 3 of the License, or
9 1.1 christos (at your option) any later version.
10 1.1 christos
11 1.1 christos This program is distributed in the hope that it will be useful,
12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 christos GNU General Public License for more details.
15 1.1 christos
16 1.1 christos You should have received a copy of the GNU General Public License
17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 1.1 christos
19 1.1 christos #ifndef UI_FILE_H
20 1.1 christos #define UI_FILE_H
21 1.1 christos
22 1.1.1.5 christos #include <string>
23 1.1.1.6 christos #include "ui-style.h"
24 1.1 christos
25 1.1.1.5 christos /* The abstract ui_file base class. */
26 1.1 christos
27 1.1.1.5 christos class ui_file
28 1.1.1.5 christos {
29 1.1.1.5 christos public:
30 1.1.1.5 christos ui_file ();
31 1.1.1.5 christos virtual ~ui_file () = 0;
32 1.1 christos
33 1.1.1.5 christos /* Public non-virtual API. */
34 1.1 christos
35 1.1.1.5 christos void printf (const char *, ...) ATTRIBUTE_PRINTF (2, 3);
36 1.1 christos
37 1.1.1.5 christos /* Print a string whose delimiter is QUOTER. Note that these
38 1.1.1.5 christos routines should only be called for printing things which are
39 1.1.1.5 christos independent of the language of the program being debugged. */
40 1.1.1.5 christos void putstr (const char *str, int quoter);
41 1.1 christos
42 1.1.1.5 christos void putstrn (const char *str, int n, int quoter);
43 1.1 christos
44 1.1.1.5 christos int putc (int c);
45 1.1 christos
46 1.1.1.5 christos void vprintf (const char *, va_list) ATTRIBUTE_PRINTF (2, 0);
47 1.1 christos
48 1.1.1.5 christos /* Methods below are both public, and overridable by ui_file
49 1.1.1.5 christos subclasses. */
50 1.1 christos
51 1.1.1.5 christos virtual void write (const char *buf, long length_buf) = 0;
52 1.1 christos
53 1.1.1.5 christos /* This version of "write" is safe for use in signal handlers. It's
54 1.1.1.5 christos not guaranteed that all existing output will have been flushed
55 1.1.1.5 christos first. Implementations are also free to ignore some or all of
56 1.1.1.5 christos the request. puts_async is not provided as the async versions
57 1.1.1.5 christos are rarely used, no point in having both for a rarely used
58 1.1.1.5 christos interface. */
59 1.1.1.5 christos virtual void write_async_safe (const char *buf, long length_buf)
60 1.1.1.5 christos { gdb_assert_not_reached ("write_async_safe"); }
61 1.1.1.2 christos
62 1.1.1.5 christos /* Some ui_files override this to provide a efficient implementation
63 1.1.1.5 christos that avoids a strlen. */
64 1.1.1.5 christos virtual void puts (const char *str)
65 1.1.1.5 christos { this->write (str, strlen (str)); }
66 1.1.1.2 christos
67 1.1.1.5 christos virtual long read (char *buf, long length_buf)
68 1.1.1.5 christos { gdb_assert_not_reached ("can't read from this file type"); }
69 1.1 christos
70 1.1.1.5 christos virtual bool isatty ()
71 1.1.1.5 christos { return false; }
72 1.1 christos
73 1.1.1.7 christos /* true indicates terminal output behaviour such as cli_styling.
74 1.1.1.7 christos This default implementation indicates to do terminal output
75 1.1.1.7 christos behaviour if the UI_FILE is a tty. A derived class can override
76 1.1.1.7 christos TERM_OUT to have cli_styling behaviour without being a tty. */
77 1.1.1.7 christos virtual bool term_out ()
78 1.1.1.7 christos { return isatty (); }
79 1.1.1.7 christos
80 1.1.1.7 christos /* true if ANSI escapes can be used on STREAM. */
81 1.1.1.7 christos virtual bool can_emit_style_escape ()
82 1.1.1.7 christos { return false; }
83 1.1.1.7 christos
84 1.1.1.5 christos virtual void flush ()
85 1.1.1.5 christos {}
86 1.1.1.5 christos };
87 1.1 christos
88 1.1.1.5 christos typedef std::unique_ptr<ui_file> ui_file_up;
89 1.1 christos
90 1.1.1.5 christos /* A ui_file that writes to nowhere. */
91 1.1 christos
92 1.1.1.5 christos class null_file : public ui_file
93 1.1.1.5 christos {
94 1.1.1.5 christos public:
95 1.1.1.5 christos void write (const char *buf, long length_buf) override;
96 1.1.1.5 christos void write_async_safe (const char *buf, long sizeof_buf) override;
97 1.1.1.5 christos void puts (const char *str) override;
98 1.1.1.5 christos };
99 1.1 christos
100 1.1.1.5 christos /* A preallocated null_file stream. */
101 1.1.1.5 christos extern null_file null_stream;
102 1.1 christos
103 1.1.1.6 christos extern int gdb_console_fputs (const char *, FILE *);
104 1.1.1.6 christos
105 1.1.1.5 christos /* A std::string-based ui_file. Can be used as a scratch buffer for
106 1.1.1.5 christos collecting output. */
107 1.1 christos
108 1.1.1.5 christos class string_file : public ui_file
109 1.1.1.5 christos {
110 1.1.1.5 christos public:
111 1.1.1.7 christos /* Construct a string_file to collect 'raw' output, i.e. without
112 1.1.1.7 christos 'terminal' behaviour such as cli_styling. */
113 1.1.1.7 christos string_file () : m_term_out (false) {};
114 1.1.1.7 christos /* If TERM_OUT, construct a string_file with terminal output behaviour
115 1.1.1.7 christos such as cli_styling)
116 1.1.1.7 christos else collect 'raw' output like the previous constructor. */
117 1.1.1.7 christos explicit string_file (bool term_out) : m_term_out (term_out) {};
118 1.1.1.5 christos ~string_file () override;
119 1.1.1.5 christos
120 1.1.1.5 christos /* Override ui_file methods. */
121 1.1.1.5 christos
122 1.1.1.5 christos void write (const char *buf, long length_buf) override;
123 1.1.1.5 christos
124 1.1.1.5 christos long read (char *buf, long length_buf) override
125 1.1.1.5 christos { gdb_assert_not_reached ("a string_file is not readable"); }
126 1.1.1.5 christos
127 1.1.1.7 christos bool term_out () override;
128 1.1.1.7 christos bool can_emit_style_escape () override;
129 1.1.1.7 christos
130 1.1.1.5 christos /* string_file-specific public API. */
131 1.1.1.5 christos
132 1.1.1.5 christos /* Accesses the std::string containing the entire output collected
133 1.1.1.5 christos so far.
134 1.1.1.5 christos
135 1.1.1.5 christos Returns a non-const reference so that it's easy to move the
136 1.1.1.5 christos string contents out of the string_file. E.g.:
137 1.1.1.5 christos
138 1.1.1.5 christos string_file buf;
139 1.1.1.5 christos buf.printf (....);
140 1.1.1.5 christos buf.printf (....);
141 1.1.1.5 christos return std::move (buf.string ());
142 1.1.1.5 christos */
143 1.1.1.5 christos std::string &string () { return m_string; }
144 1.1.1.5 christos
145 1.1.1.5 christos /* Provide a few convenience methods with the same API as the
146 1.1.1.5 christos underlying std::string. */
147 1.1.1.5 christos const char *data () const { return m_string.data (); }
148 1.1.1.5 christos const char *c_str () const { return m_string.c_str (); }
149 1.1.1.5 christos size_t size () const { return m_string.size (); }
150 1.1.1.5 christos bool empty () const { return m_string.empty (); }
151 1.1.1.5 christos void clear () { return m_string.clear (); }
152 1.1.1.5 christos
153 1.1.1.5 christos private:
154 1.1.1.5 christos /* The internal buffer. */
155 1.1.1.5 christos std::string m_string;
156 1.1.1.7 christos
157 1.1.1.7 christos bool m_term_out;
158 1.1.1.5 christos };
159 1.1.1.5 christos
160 1.1.1.5 christos /* A ui_file implementation that maps directly onto <stdio.h>'s FILE.
161 1.1.1.5 christos A stdio_file can either own its underlying file, or not. If it
162 1.1.1.5 christos owns the file, then destroying the stdio_file closes the underlying
163 1.1.1.5 christos file, otherwise it is left open. */
164 1.1.1.5 christos
165 1.1.1.5 christos class stdio_file : public ui_file
166 1.1.1.5 christos {
167 1.1.1.5 christos public:
168 1.1.1.5 christos /* Create a ui_file from a previously opened FILE. CLOSE_P
169 1.1.1.5 christos indicates whether the underlying file should be closed when the
170 1.1.1.5 christos stdio_file is destroyed. */
171 1.1.1.5 christos explicit stdio_file (FILE *file, bool close_p = false);
172 1.1.1.5 christos
173 1.1.1.5 christos /* Create an stdio_file that is not managing any file yet. Call
174 1.1.1.5 christos open to actually open something. */
175 1.1.1.5 christos stdio_file ();
176 1.1.1.5 christos
177 1.1.1.5 christos ~stdio_file () override;
178 1.1.1.5 christos
179 1.1.1.5 christos /* Open NAME in mode MODE, and own the resulting file. Returns true
180 1.1.1.5 christos on success, false otherwise. If the stdio_file previously owned
181 1.1.1.5 christos a file, it is closed. */
182 1.1.1.5 christos bool open (const char *name, const char *mode);
183 1.1.1.5 christos
184 1.1.1.5 christos void flush () override;
185 1.1.1.5 christos
186 1.1.1.5 christos void write (const char *buf, long length_buf) override;
187 1.1.1.5 christos
188 1.1.1.5 christos void write_async_safe (const char *buf, long length_buf) override;
189 1.1.1.5 christos
190 1.1.1.5 christos void puts (const char *) override;
191 1.1.1.5 christos
192 1.1.1.5 christos long read (char *buf, long length_buf) override;
193 1.1.1.5 christos
194 1.1.1.5 christos bool isatty () override;
195 1.1.1.5 christos
196 1.1.1.7 christos bool can_emit_style_escape () override;
197 1.1.1.7 christos
198 1.1.1.5 christos private:
199 1.1.1.5 christos /* Sets the internal stream to FILE, and saves the FILE's file
200 1.1.1.5 christos descriptor in M_FD. */
201 1.1.1.5 christos void set_stream (FILE *file);
202 1.1.1.5 christos
203 1.1.1.5 christos /* The file. */
204 1.1.1.5 christos FILE *m_file;
205 1.1.1.5 christos
206 1.1.1.5 christos /* The associated file descriptor is extracted ahead of time for
207 1.1.1.5 christos stdio_file::write_async_safe's benefit, in case fileno isn't
208 1.1.1.5 christos async-safe. */
209 1.1.1.5 christos int m_fd;
210 1.1.1.5 christos
211 1.1.1.5 christos /* If true, M_FILE is closed on destruction. */
212 1.1.1.5 christos bool m_close_p;
213 1.1.1.5 christos };
214 1.1.1.5 christos
215 1.1.1.5 christos typedef std::unique_ptr<stdio_file> stdio_file_up;
216 1.1.1.5 christos
217 1.1.1.5 christos /* Like stdio_file, but specifically for stderr.
218 1.1.1.5 christos
219 1.1.1.5 christos This exists because there is no real line-buffering on Windows, see
220 1.1.1.5 christos <http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx>
221 1.1.1.5 christos so the stdout is either fully-buffered or non-buffered. We can't
222 1.1.1.5 christos make stdout non-buffered, because of two concerns:
223 1.1.1.5 christos
224 1.1.1.5 christos 1. Non-buffering hurts performance.
225 1.1.1.5 christos 2. Non-buffering may change GDB's behavior when it is interacting
226 1.1.1.5 christos with a front-end, such as Emacs.
227 1.1.1.5 christos
228 1.1.1.5 christos We leave stdout as fully buffered, but flush it first when
229 1.1.1.5 christos something is written to stderr.
230 1.1.1.5 christos
231 1.1.1.5 christos Note that the 'write_async_safe' method is not overridden, because
232 1.1.1.5 christos there's no way to flush a stream in an async-safe manner.
233 1.1.1.5 christos Fortunately, it doesn't really matter, because:
234 1.1.1.5 christos
235 1.1.1.5 christos 1. That method is only used for printing internal debug output
236 1.1.1.5 christos from signal handlers.
237 1.1.1.5 christos
238 1.1.1.5 christos 2. Windows hosts don't have a concept of async-safeness. Signal
239 1.1.1.5 christos handlers run in a separate thread, so they can call the regular
240 1.1.1.5 christos non-async-safe output routines freely.
241 1.1.1.5 christos */
242 1.1.1.5 christos class stderr_file : public stdio_file
243 1.1.1.5 christos {
244 1.1.1.5 christos public:
245 1.1.1.5 christos explicit stderr_file (FILE *stream);
246 1.1.1.5 christos
247 1.1.1.5 christos /* Override the output routines to flush gdb_stdout before deferring
248 1.1.1.5 christos to stdio_file for the actual outputting. */
249 1.1.1.5 christos void write (const char *buf, long length_buf) override;
250 1.1.1.5 christos void puts (const char *linebuffer) override;
251 1.1.1.5 christos };
252 1.1.1.5 christos
253 1.1.1.5 christos /* A ui_file implementation that maps onto two ui-file objects. */
254 1.1.1.5 christos
255 1.1.1.5 christos class tee_file : public ui_file
256 1.1.1.5 christos {
257 1.1.1.5 christos public:
258 1.1.1.7 christos /* Create a file which writes to both ONE and TWO. ONE will remain
259 1.1.1.7 christos open when this object is destroyed; but TWO will be closed. */
260 1.1.1.7 christos tee_file (ui_file *one, ui_file_up &&two);
261 1.1.1.5 christos ~tee_file () override;
262 1.1.1.5 christos
263 1.1.1.5 christos void write (const char *buf, long length_buf) override;
264 1.1.1.5 christos void write_async_safe (const char *buf, long length_buf) override;
265 1.1.1.5 christos void puts (const char *) override;
266 1.1.1.5 christos
267 1.1.1.5 christos bool isatty () override;
268 1.1.1.7 christos bool term_out () override;
269 1.1.1.7 christos bool can_emit_style_escape () override;
270 1.1.1.5 christos void flush () override;
271 1.1.1.5 christos
272 1.1.1.5 christos private:
273 1.1.1.7 christos /* The two underlying ui_files. */
274 1.1.1.7 christos ui_file *m_one;
275 1.1.1.7 christos ui_file_up m_two;
276 1.1.1.7 christos };
277 1.1.1.7 christos
278 1.1.1.7 christos /* A ui_file implementation that filters out terminal escape
279 1.1.1.7 christos sequences. */
280 1.1.1.7 christos
281 1.1.1.7 christos class no_terminal_escape_file : public stdio_file
282 1.1.1.7 christos {
283 1.1.1.7 christos public:
284 1.1.1.7 christos no_terminal_escape_file ()
285 1.1.1.7 christos {
286 1.1.1.7 christos }
287 1.1.1.7 christos
288 1.1.1.7 christos /* Like the stdio_file methods, but these filter out terminal escape
289 1.1.1.7 christos sequences. */
290 1.1.1.7 christos void write (const char *buf, long length_buf) override;
291 1.1.1.7 christos void puts (const char *linebuffer) override;
292 1.1.1.5 christos };
293 1.1 christos
294 1.1 christos #endif
295