summaryrefslogtreecommitdiff
path: root/gdb/mi/mi-console.c
blob: 7c1c2acc37bdbc2d0aec79c2f4756582ff1b4b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* MI Console code.

   Copyright (C) 2000-2017 Free Software Foundation, Inc.

   Contributed by Cygnus Solutions (a Red Hat company).

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* An MI console is a kind of ui_file stream that sends output to
   stdout, but encapsulated and prefixed with a distinctive string;
   for instance, error output is normally identified by a leading
   "&".  */

#include "defs.h"
#include "mi-console.h"

/* Create a console that wraps the given output stream RAW with the
   string PREFIX and quoting it with QUOTE.  */

mi_console_file::mi_console_file (ui_file *raw, const char *prefix, char quote)
  : m_raw (raw),
    m_prefix (prefix),
    m_quote (quote)
{}

void
mi_console_file::write (const char *buf, long length_buf)
{
  size_t prev_size = m_buffer.size ();
  /* Append the text to our internal buffer.  */
  m_buffer.write (buf, length_buf);
  /* Flush when an embedded newline is present anywhere in the
     buffer.  */
  if (strchr (m_buffer.c_str () + prev_size, '\n') != NULL)
    this->flush ();
}

void
mi_console_file::flush ()
{
  const std::string &str = m_buffer.string ();

  /* Transform a byte sequence into a console output packet.  */
  if (!str.empty ())
    {
      size_t length_buf = str.size ();
      const char *buf = str.data ();

      fputs_unfiltered (m_prefix, m_raw);
      if (m_quote)
	{
	  fputc_unfiltered (m_quote, m_raw);
	  fputstrn_unfiltered (buf, length_buf, m_quote, m_raw);
	  fputc_unfiltered (m_quote, m_raw);
	  fputc_unfiltered ('\n', m_raw);
	}
      else
	{
	  fputstrn_unfiltered (buf, length_buf, 0, m_raw);
	  fputc_unfiltered ('\n', m_raw);
	}
      gdb_flush (m_raw);
    }

  m_buffer.clear ();
}

/* Change the underlying stream of the console directly; this is
   useful as a minimum-impact way to reflect external changes like
   logging enable/disable.  */

void
mi_console_file::set_raw (ui_file *raw)
{
  m_raw = raw;
}