mi-console.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* MI Console code.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions (a Red Hat company).
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* An MI console is a kind of ui_file stream that sends output to
  16. stdout, but encapsulated and prefixed with a distinctive string;
  17. for instance, error output is normally identified by a leading
  18. "&". */
  19. #include "defs.h"
  20. #include "mi-console.h"
  21. /* Create a console that wraps the given output stream RAW with the
  22. string PREFIX and quoting it with QUOTE. */
  23. mi_console_file::mi_console_file (ui_file *raw, const char *prefix, char quote)
  24. : m_raw (raw),
  25. m_prefix (prefix),
  26. m_quote (quote)
  27. {}
  28. void
  29. mi_console_file::write (const char *buf, long length_buf)
  30. {
  31. size_t prev_size = m_buffer.size ();
  32. /* Append the text to our internal buffer. */
  33. m_buffer.write (buf, length_buf);
  34. /* Flush when an embedded newline is present anywhere in the
  35. buffer. */
  36. if (strchr (m_buffer.c_str () + prev_size, '\n') != NULL)
  37. this->flush ();
  38. }
  39. void
  40. mi_console_file::write_async_safe (const char *buf, long length_buf)
  41. {
  42. m_raw->write_async_safe (m_prefix, strlen (m_prefix));
  43. if (m_quote)
  44. {
  45. m_raw->write_async_safe (&m_quote, 1);
  46. m_raw->putstrn (buf, length_buf, m_quote, true);
  47. m_raw->write_async_safe (&m_quote, 1);
  48. }
  49. else
  50. m_raw->putstrn (buf, length_buf, 0, true);
  51. char nl = '\n';
  52. m_raw->write_async_safe (&nl, 1);
  53. }
  54. void
  55. mi_console_file::flush ()
  56. {
  57. const std::string &str = m_buffer.string ();
  58. /* Transform a byte sequence into a console output packet. */
  59. if (!str.empty ())
  60. {
  61. size_t length_buf = str.size ();
  62. const char *buf = str.data ();
  63. gdb_puts (m_prefix, m_raw);
  64. if (m_quote)
  65. {
  66. gdb_putc (m_quote, m_raw);
  67. m_raw->putstrn (buf, length_buf, m_quote);
  68. gdb_putc (m_quote, m_raw);
  69. gdb_putc ('\n', m_raw);
  70. }
  71. else
  72. {
  73. m_raw->putstrn (buf, length_buf, 0);
  74. gdb_putc ('\n', m_raw);
  75. }
  76. gdb_flush (m_raw);
  77. }
  78. m_buffer.clear ();
  79. }
  80. /* Change the underlying stream of the console directly; this is
  81. useful as a minimum-impact way to reflect external changes like
  82. logging enable/disable. */
  83. void
  84. mi_console_file::set_raw (ui_file *raw)
  85. {
  86. m_raw = raw;
  87. }