GDB (xrefs)
mi-console.c
Go to the documentation of this file.
1 /* MI Console code.
2 
3  Copyright (C) 2000-2018 Free Software Foundation, Inc.
4 
5  Contributed by Cygnus Solutions (a Red Hat company).
6 
7  This file is part of GDB.
8 
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.
13 
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.
18 
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/>. */
21 
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  "&". */
26 
27 #include "defs.h"
28 #include "mi-console.h"
29 
30 /* Create a console that wraps the given output stream RAW with the
31  string PREFIX and quoting it with QUOTE. */
32 
33 mi_console_file::mi_console_file (ui_file *raw, const char *prefix, char quote)
34  : m_raw (raw),
35  m_prefix (prefix),
36  m_quote (quote)
37 {}
38 
39 void
40 mi_console_file::write (const char *buf, long length_buf)
41 {
42  size_t prev_size = m_buffer.size ();
43  /* Append the text to our internal buffer. */
44  m_buffer.write (buf, length_buf);
45  /* Flush when an embedded newline is present anywhere in the
46  buffer. */
47  if (strchr (m_buffer.c_str () + prev_size, '\n') != NULL)
48  this->flush ();
49 }
50 
51 void
53 {
54  const std::string &str = m_buffer.string ();
55 
56  /* Transform a byte sequence into a console output packet. */
57  if (!str.empty ())
58  {
59  size_t length_buf = str.size ();
60  const char *buf = str.data ();
61 
63  if (m_quote)
64  {
66  fputstrn_unfiltered (buf, length_buf, m_quote, m_raw);
68  fputc_unfiltered ('\n', m_raw);
69  }
70  else
71  {
72  fputstrn_unfiltered (buf, length_buf, 0, m_raw);
73  fputc_unfiltered ('\n', m_raw);
74  }
75  gdb_flush (m_raw);
76  }
77 
78  m_buffer.clear ();
79 }
80 
81 /* Change the underlying stream of the console directly; this is
82  useful as a minimum-impact way to reflect external changes like
83  logging enable/disable. */
84 
85 void
87 {
88  m_raw = raw;
89 }
const char * string
Definition: signals.c:50
void fputs_unfiltered(const char *buf, struct ui_file *file)
Definition: ui-file.c:127
void write(const char *buf, long length_buf) override
Definition: ui-file.c:138
void set_raw(ui_file *raw)
Definition: mi-console.c:86
void flush() override
Definition: mi-console.c:52
ui_file * m_raw
Definition: mi-console.h:44
int fputc_unfiltered(int c, struct ui_file *stream)
Definition: utils.c:1835
void fputstrn_unfiltered(const char *str, int n, int quoter, struct ui_file *stream)
Definition: utils.c:1278
void clear()
Definition: ui-file.h:140
size_t size() const
Definition: ui-file.h:138
mi_console_file(ui_file *raw, const char *prefix, char quote)
Definition: mi-console.c:33
string_file m_buffer
Definition: mi-console.h:47
std::string & string()
Definition: ui-file.h:132
const char * c_str() const
Definition: ui-file.h:137
const char * m_prefix
Definition: mi-console.h:50
void gdb_flush(struct ui_file *file)
Definition: ui-file.c:93
void write(const char *buf, long length_buf) override
Definition: mi-console.c:40