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