]>
Commit | Line | Data |
---|---|---|
d9fcf2fb | 1 | /* UI_FILE - a generic STDIO like output stream. |
d01e8234 | 2 | Copyright (C) 1999-2025 Free Software Foundation, Inc. |
d9fcf2fb JM |
3 | |
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
d9fcf2fb JM |
9 | (at your option) any later version. |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d9fcf2fb | 18 | |
cc709640 TT |
19 | #ifndef GDB_UI_FILE_H |
20 | #define GDB_UI_FILE_H | |
d9fcf2fb | 21 | |
8de00631 | 22 | #include <string> |
eedeedd2 | 23 | #include "ui-style.h" |
8de00631 | 24 | |
d7e74731 PA |
25 | /* The abstract ui_file base class. */ |
26 | ||
27 | class ui_file | |
28 | { | |
29 | public: | |
30 | ui_file (); | |
31 | virtual ~ui_file () = 0; | |
d9fcf2fb | 32 | |
34f997c8 AB |
33 | ui_file (ui_file &&other) = default; |
34 | ||
d7e74731 | 35 | /* Public non-virtual API. */ |
d9fcf2fb | 36 | |
d7e74731 | 37 | void printf (const char *, ...) ATTRIBUTE_PRINTF (2, 3); |
d9fcf2fb | 38 | |
d53fd721 TT |
39 | /* Print a NUL-terminated string whose delimiter is QUOTER. Note |
40 | that these routines should only be called for printing things | |
41 | which are independent of the language of the program being | |
42 | debugged. | |
43 | ||
44 | This will normally escape backslashes and instances of QUOTER. | |
45 | If QUOTER is 0, it won't escape backslashes or any quoting | |
46 | character. As a side effect, if you pass the backslash character | |
47 | as the QUOTER, this will escape backslashes as usual, but not any | |
48 | other quoting character. */ | |
d7e74731 | 49 | void putstr (const char *str, int quoter); |
d9fcf2fb | 50 | |
d53fd721 TT |
51 | /* Like putstr, but only print the first N characters of STR. If |
52 | ASYNC_SAFE is true, then the output is done via the | |
53 | write_async_safe method. */ | |
54 | void putstrn (const char *str, int n, int quoter, bool async_safe = false); | |
3e43a32a | 55 | |
4311246b | 56 | void putc (int c); |
3e43a32a | 57 | |
d7e74731 | 58 | void vprintf (const char *, va_list) ATTRIBUTE_PRINTF (2, 0); |
01124a23 | 59 | |
d7e74731 PA |
60 | /* Methods below are both public, and overridable by ui_file |
61 | subclasses. */ | |
3e43a32a | 62 | |
d7e74731 | 63 | virtual void write (const char *buf, long length_buf) = 0; |
3e43a32a | 64 | |
d7e74731 PA |
65 | /* This version of "write" is safe for use in signal handlers. It's |
66 | not guaranteed that all existing output will have been flushed | |
67 | first. Implementations are also free to ignore some or all of | |
68 | the request. puts_async is not provided as the async versions | |
69 | are rarely used, no point in having both for a rarely used | |
70 | interface. */ | |
71 | virtual void write_async_safe (const char *buf, long length_buf) | |
72 | { gdb_assert_not_reached ("write_async_safe"); } | |
3e43a32a | 73 | |
d7e74731 PA |
74 | /* Some ui_files override this to provide a efficient implementation |
75 | that avoids a strlen. */ | |
76 | virtual void puts (const char *str) | |
77 | { this->write (str, strlen (str)); } | |
d9fcf2fb | 78 | |
d7e74731 PA |
79 | virtual long read (char *buf, long length_buf) |
80 | { gdb_assert_not_reached ("can't read from this file type"); } | |
d9fcf2fb | 81 | |
d7e74731 PA |
82 | virtual bool isatty () |
83 | { return false; } | |
2a9d5ccf | 84 | |
ac51afb5 | 85 | /* true indicates terminal output behavior such as cli_styling. |
8a522c6c | 86 | This default implementation indicates to do terminal output |
ac51afb5 TV |
87 | behavior if the UI_FILE is a tty. A derived class can override |
88 | TERM_OUT to have cli_styling behavior without being a tty. */ | |
8a522c6c PW |
89 | virtual bool term_out () |
90 | { return isatty (); } | |
91 | ||
92 | /* true if ANSI escapes can be used on STREAM. */ | |
93 | virtual bool can_emit_style_escape () | |
94 | { return false; } | |
95 | ||
d7e74731 PA |
96 | virtual void flush () |
97 | {} | |
6aa4f97c AB |
98 | |
99 | /* If this object has an underlying file descriptor, then return it. | |
100 | Otherwise, return -1. */ | |
101 | virtual int fd () const | |
102 | { return -1; } | |
84b33497 | 103 | |
7016a382 TT |
104 | /* Indicate that if the next sequence of characters overflows the |
105 | line, a newline should be inserted here rather than when it hits | |
106 | the end. If INDENT is non-zero, it is a number of spaces to be | |
107 | printed to indent the wrapped part on the next line. | |
108 | ||
109 | If the line is already overfull, we immediately print a newline and | |
110 | the indentation, and disable further wrapping. | |
111 | ||
112 | If we don't know the width of lines, but we know the page height, | |
113 | we must not wrap words, but should still keep track of newlines | |
114 | that were explicitly printed. | |
115 | ||
116 | This routine is guaranteed to force out any output which has been | |
117 | squirreled away in the wrap_buffer, so wrap_here (0) can be | |
118 | used to force out output from the wrap_buffer. */ | |
3cd52293 TT |
119 | virtual void wrap_here (int indent) |
120 | { | |
121 | } | |
7016a382 | 122 | |
c8d74a7b TT |
123 | /* Emit an ANSI style escape for STYLE. */ |
124 | virtual void emit_style_escape (const ui_file_style &style); | |
125 | ||
9b716718 TT |
126 | /* Print STR, bypassing any paging that might be done by this |
127 | ui_file. Note that nearly no code should call this -- it's | |
6cb06a8c | 128 | intended for use by gdb_printf, but nothing else. */ |
9b716718 TT |
129 | virtual void puts_unfiltered (const char *str) |
130 | { | |
131 | this->puts (str); | |
132 | } | |
133 | ||
c8d74a7b TT |
134 | protected: |
135 | ||
136 | /* The currently applied style. */ | |
137 | ui_file_style m_applied_style; | |
138 | ||
d53fd721 TT |
139 | private: |
140 | ||
141 | /* Helper function for putstr and putstrn. Print the character C on | |
142 | this stream as part of the contents of a literal string whose | |
143 | delimiter is QUOTER. */ | |
144 | void printchar (int c, int quoter, bool async_safe); | |
d7e74731 | 145 | }; |
d9fcf2fb | 146 | |
d7e74731 | 147 | typedef std::unique_ptr<ui_file> ui_file_up; |
d9fcf2fb | 148 | |
d7e74731 | 149 | /* A ui_file that writes to nowhere. */ |
d9fcf2fb | 150 | |
d7e74731 PA |
151 | class null_file : public ui_file |
152 | { | |
153 | public: | |
154 | void write (const char *buf, long length_buf) override; | |
155 | void write_async_safe (const char *buf, long sizeof_buf) override; | |
156 | void puts (const char *str) override; | |
157 | }; | |
d9fcf2fb | 158 | |
d7e74731 PA |
159 | /* A preallocated null_file stream. */ |
160 | extern null_file null_stream; | |
161 | ||
e4adb939 EZ |
162 | extern int gdb_console_fputs (const char *, FILE *); |
163 | ||
d7e74731 PA |
164 | /* A std::string-based ui_file. Can be used as a scratch buffer for |
165 | collecting output. */ | |
d9fcf2fb | 166 | |
d7e74731 PA |
167 | class string_file : public ui_file |
168 | { | |
169 | public: | |
8a522c6c | 170 | /* Construct a string_file to collect 'raw' output, i.e. without |
ac51afb5 | 171 | 'terminal' behavior such as cli_styling. */ |
8a522c6c | 172 | string_file () : m_term_out (false) {}; |
ac51afb5 | 173 | /* If TERM_OUT, construct a string_file with terminal output behavior |
8a522c6c PW |
174 | such as cli_styling) |
175 | else collect 'raw' output like the previous constructor. */ | |
176 | explicit string_file (bool term_out) : m_term_out (term_out) {}; | |
d7e74731 | 177 | ~string_file () override; |
d9fcf2fb | 178 | |
34f997c8 AB |
179 | string_file (string_file &&other) = default; |
180 | ||
d7e74731 | 181 | /* Override ui_file methods. */ |
8de00631 | 182 | |
d7e74731 | 183 | void write (const char *buf, long length_buf) override; |
d9fcf2fb | 184 | |
d7e74731 PA |
185 | long read (char *buf, long length_buf) override |
186 | { gdb_assert_not_reached ("a string_file is not readable"); } | |
187 | ||
8a522c6c PW |
188 | bool term_out () override; |
189 | bool can_emit_style_escape () override; | |
190 | ||
d7e74731 PA |
191 | /* string_file-specific public API. */ |
192 | ||
193 | /* Accesses the std::string containing the entire output collected | |
5d10a204 SM |
194 | so far. */ |
195 | const std::string &string () { return m_string; } | |
d7e74731 | 196 | |
5d10a204 | 197 | /* Return an std::string containing the entire output collected so far. |
d7e74731 | 198 | |
5d10a204 SM |
199 | The internal buffer is cleared, such that it's ready to build a new |
200 | string. */ | |
201 | std::string release () | |
202 | { | |
203 | std::string ret = std::move (m_string); | |
204 | m_string.clear (); | |
205 | return ret; | |
206 | } | |
d7e74731 | 207 | |
e867795e AB |
208 | /* Set the internal buffer contents to STR. Any existing contents are |
209 | discarded. */ | |
210 | string_file &operator= (std::string &&str) | |
211 | { | |
212 | m_string = std::move (str); | |
213 | return *this; | |
214 | } | |
215 | ||
d7e74731 PA |
216 | /* Provide a few convenience methods with the same API as the |
217 | underlying std::string. */ | |
218 | const char *data () const { return m_string.data (); } | |
219 | const char *c_str () const { return m_string.c_str (); } | |
220 | size_t size () const { return m_string.size (); } | |
221 | bool empty () const { return m_string.empty (); } | |
222 | void clear () { return m_string.clear (); } | |
223 | ||
224 | private: | |
225 | /* The internal buffer. */ | |
226 | std::string m_string; | |
8a522c6c PW |
227 | |
228 | bool m_term_out; | |
d7e74731 PA |
229 | }; |
230 | ||
231 | /* A ui_file implementation that maps directly onto <stdio.h>'s FILE. | |
232 | A stdio_file can either own its underlying file, or not. If it | |
233 | owns the file, then destroying the stdio_file closes the underlying | |
234 | file, otherwise it is left open. */ | |
235 | ||
236 | class stdio_file : public ui_file | |
237 | { | |
238 | public: | |
239 | /* Create a ui_file from a previously opened FILE. CLOSE_P | |
240 | indicates whether the underlying file should be closed when the | |
241 | stdio_file is destroyed. */ | |
242 | explicit stdio_file (FILE *file, bool close_p = false); | |
243 | ||
244 | /* Create an stdio_file that is not managing any file yet. Call | |
245 | open to actually open something. */ | |
246 | stdio_file (); | |
247 | ||
248 | ~stdio_file () override; | |
249 | ||
250 | /* Open NAME in mode MODE, and own the resulting file. Returns true | |
251 | on success, false otherwise. If the stdio_file previously owned | |
252 | a file, it is closed. */ | |
253 | bool open (const char *name, const char *mode); | |
254 | ||
255 | void flush () override; | |
256 | ||
257 | void write (const char *buf, long length_buf) override; | |
258 | ||
259 | void write_async_safe (const char *buf, long length_buf) override; | |
260 | ||
261 | void puts (const char *) override; | |
262 | ||
263 | long read (char *buf, long length_buf) override; | |
264 | ||
265 | bool isatty () override; | |
266 | ||
8a522c6c PW |
267 | bool can_emit_style_escape () override; |
268 | ||
6aa4f97c AB |
269 | /* Return the underlying file descriptor. */ |
270 | int fd () const override | |
271 | { return m_fd; } | |
272 | ||
d7e74731 PA |
273 | private: |
274 | /* Sets the internal stream to FILE, and saves the FILE's file | |
275 | descriptor in M_FD. */ | |
276 | void set_stream (FILE *file); | |
277 | ||
278 | /* The file. */ | |
279 | FILE *m_file; | |
280 | ||
281 | /* The associated file descriptor is extracted ahead of time for | |
282 | stdio_file::write_async_safe's benefit, in case fileno isn't | |
283 | async-safe. */ | |
284 | int m_fd; | |
285 | ||
286 | /* If true, M_FILE is closed on destruction. */ | |
287 | bool m_close_p; | |
288 | }; | |
289 | ||
290 | typedef std::unique_ptr<stdio_file> stdio_file_up; | |
291 | ||
292 | /* Like stdio_file, but specifically for stderr. | |
293 | ||
294 | This exists because there is no real line-buffering on Windows, see | |
295 | <http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx> | |
296 | so the stdout is either fully-buffered or non-buffered. We can't | |
297 | make stdout non-buffered, because of two concerns: | |
449092f6 | 298 | |
d7e74731 PA |
299 | 1. Non-buffering hurts performance. |
300 | 2. Non-buffering may change GDB's behavior when it is interacting | |
301 | with a front-end, such as Emacs. | |
2a9d5ccf | 302 | |
d7e74731 PA |
303 | We leave stdout as fully buffered, but flush it first when |
304 | something is written to stderr. | |
d9fcf2fb | 305 | |
d7e74731 PA |
306 | Note that the 'write_async_safe' method is not overridden, because |
307 | there's no way to flush a stream in an async-safe manner. | |
308 | Fortunately, it doesn't really matter, because: | |
309 | ||
310 | 1. That method is only used for printing internal debug output | |
311 | from signal handlers. | |
312 | ||
313 | 2. Windows hosts don't have a concept of async-safeness. Signal | |
314 | handlers run in a separate thread, so they can call the regular | |
315 | non-async-safe output routines freely. | |
316 | */ | |
317 | class stderr_file : public stdio_file | |
318 | { | |
319 | public: | |
320 | explicit stderr_file (FILE *stream); | |
321 | ||
322 | /* Override the output routines to flush gdb_stdout before deferring | |
323 | to stdio_file for the actual outputting. */ | |
324 | void write (const char *buf, long length_buf) override; | |
325 | void puts (const char *linebuffer) override; | |
326 | }; | |
d9fcf2fb | 327 | |
d7e74731 | 328 | /* A ui_file implementation that maps onto two ui-file objects. */ |
d9fcf2fb | 329 | |
d7e74731 PA |
330 | class tee_file : public ui_file |
331 | { | |
332 | public: | |
2b141965 TT |
333 | /* Create a file which writes to both ONE and TWO. Ownership of |
334 | both files is up to the user. */ | |
335 | tee_file (ui_file *one, ui_file *two); | |
d7e74731 | 336 | ~tee_file () override; |
d9fcf2fb | 337 | |
d7e74731 PA |
338 | void write (const char *buf, long length_buf) override; |
339 | void write_async_safe (const char *buf, long length_buf) override; | |
340 | void puts (const char *) override; | |
ffa4ac95 | 341 | |
d7e74731 | 342 | bool isatty () override; |
8a522c6c PW |
343 | bool term_out () override; |
344 | bool can_emit_style_escape () override; | |
d7e74731 | 345 | void flush () override; |
ffa4ac95 | 346 | |
3cd52293 TT |
347 | void emit_style_escape (const ui_file_style &style) override |
348 | { | |
349 | m_one->emit_style_escape (style); | |
350 | m_two->emit_style_escape (style); | |
351 | } | |
352 | ||
9b716718 TT |
353 | void puts_unfiltered (const char *str) override |
354 | { | |
355 | m_one->puts_unfiltered (str); | |
356 | m_two->puts_unfiltered (str); | |
357 | } | |
358 | ||
d7e74731 | 359 | private: |
f3a09c80 AH |
360 | /* The two underlying ui_files. */ |
361 | ui_file *m_one; | |
2b141965 | 362 | ui_file *m_two; |
d7e74731 | 363 | }; |
d9fcf2fb | 364 | |
7b18593a TT |
365 | /* A ui_file implementation that buffers terminal escape sequences. |
366 | Note that this does not buffer in general -- it only buffers when | |
367 | an incomplete but potentially recognizable escape sequence is | |
368 | started. */ | |
369 | ||
370 | class escape_buffering_file : public stdio_file | |
371 | { | |
372 | public: | |
373 | using stdio_file::stdio_file; | |
374 | ||
375 | /* Like the stdio_file methods but these forward to do_write and | |
376 | do_puts, respectively. */ | |
377 | void write (const char *buf, long length_buf) override final; | |
378 | void puts (const char *linebuffer) override final; | |
379 | ||
380 | /* This class does not override 'flush'. While it does have an | |
381 | internal buffer, it does not really make sense to flush the | |
382 | buffer until an escape sequence has been fully processed. */ | |
383 | ||
384 | protected: | |
385 | ||
386 | /* Called to output some text. If the text contains a recognizable | |
387 | terminal escape sequence, then it is guaranteed to be complete. | |
388 | "Recognizable" here means that examine_ansi_escape did not return | |
389 | INCOMPLETE. */ | |
390 | virtual void do_puts (const char *buf) = 0; | |
391 | virtual void do_write (const char *buf, long len) = 0; | |
392 | ||
393 | private: | |
394 | ||
395 | /* Buffer used only for incomplete escape sequences. */ | |
396 | std::string m_buffer; | |
397 | }; | |
398 | ||
0735b091 TT |
399 | /* A ui_file implementation that filters out terminal escape |
400 | sequences. */ | |
401 | ||
7b18593a | 402 | class no_terminal_escape_file : public escape_buffering_file |
0735b091 TT |
403 | { |
404 | public: | |
405 | no_terminal_escape_file () | |
406 | { | |
407 | } | |
408 | ||
c8d74a7b TT |
409 | void emit_style_escape (const ui_file_style &style) override |
410 | { | |
411 | } | |
7b18593a TT |
412 | |
413 | protected: | |
414 | ||
415 | void do_puts (const char *linebuffer) override; | |
416 | void do_write (const char *buf, long len) override; | |
0735b091 TT |
417 | }; |
418 | ||
c269d90a | 419 | /* A base class for ui_file types that wrap another ui_file. */ |
3c6c449e | 420 | |
c269d90a | 421 | class wrapped_file : public ui_file |
3c6c449e TT |
422 | { |
423 | public: | |
c269d90a TT |
424 | |
425 | bool isatty () override | |
426 | { return m_stream->isatty (); } | |
427 | ||
428 | bool term_out () override | |
429 | { return m_stream->term_out (); } | |
3c6c449e | 430 | |
962937b1 SM |
431 | bool can_emit_style_escape () override |
432 | { return m_stream->can_emit_style_escape (); } | |
433 | ||
c269d90a TT |
434 | void flush () override |
435 | { m_stream->flush (); } | |
436 | ||
437 | void wrap_here (int indent) override | |
438 | { m_stream->wrap_here (indent); } | |
439 | ||
440 | void emit_style_escape (const ui_file_style &style) override | |
441 | { m_stream->emit_style_escape (style); } | |
442 | ||
c269d90a TT |
443 | int fd () const override |
444 | { return m_stream->fd (); } | |
445 | ||
446 | void puts_unfiltered (const char *str) override | |
447 | { m_stream->puts_unfiltered (str); } | |
448 | ||
ad62fd45 SM |
449 | void write_async_safe (const char *buf, long length_buf) override |
450 | { return m_stream->write_async_safe (buf, length_buf); } | |
451 | ||
c269d90a TT |
452 | protected: |
453 | ||
454 | /* Note that this class does not assume ownership of the stream. | |
455 | However, a subclass may choose to, by adding a 'delete' to its | |
456 | destructor. */ | |
457 | explicit wrapped_file (ui_file *stream) | |
458 | : m_stream (stream) | |
459 | { | |
460 | } | |
461 | ||
462 | /* The underlying stream. */ | |
463 | ui_file *m_stream; | |
464 | }; | |
465 | ||
466 | /* A ui_file that optionally puts a timestamp at the start of each | |
467 | line of output. */ | |
468 | ||
469 | class timestamped_file : public wrapped_file | |
470 | { | |
471 | public: | |
472 | explicit timestamped_file (ui_file *stream) | |
473 | : wrapped_file (stream) | |
474 | { | |
475 | } | |
476 | ||
3c6c449e TT |
477 | DISABLE_COPY_AND_ASSIGN (timestamped_file); |
478 | ||
479 | void write (const char *buf, long len) override; | |
480 | ||
481 | private: | |
482 | ||
3c6c449e TT |
483 | /* True if the next output should be timestamped. */ |
484 | bool m_needs_timestamp = true; | |
485 | }; | |
486 | ||
cc709640 | 487 | #endif /* GDB_UI_FILE_H */ |