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