Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / mi / mi-console.c
CommitLineData
5796c8dc
SS
1/* MI Console code.
2
ef5ccd6c 3 Copyright (C) 2000-2013 Free Software Foundation, Inc.
5796c8dc
SS
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
ef5ccd6c
JM
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
5796c8dc
SS
27#include "defs.h"
28#include "mi-console.h"
29#include "gdb_string.h"
30
5796c8dc
SS
31static ui_file_fputs_ftype mi_console_file_fputs;
32static ui_file_flush_ftype mi_console_file_flush;
33static ui_file_delete_ftype mi_console_file_delete;
34
35struct mi_console_file
36 {
37 int *magic;
38 struct ui_file *raw;
39 struct ui_file *buffer;
40 const char *prefix;
41 char quote;
42 };
43
ef5ccd6c
JM
44/* Use the address of this otherwise-unused global as a magic number
45 identifying this class of ui_file objects. */
46static int mi_console_file_magic;
47
48/* Create a console that wraps the given output stream RAW with the
49 string PREFIX and quoting it with QUOTE. */
5796c8dc
SS
50
51struct ui_file *
ef5ccd6c 52mi_console_file_new (struct ui_file *raw, const char *prefix, char quote)
5796c8dc
SS
53{
54 struct ui_file *ui_file = ui_file_new ();
55 struct mi_console_file *mi_console = XMALLOC (struct mi_console_file);
cf7f2e2d 56
5796c8dc
SS
57 mi_console->magic = &mi_console_file_magic;
58 mi_console->raw = raw;
59 mi_console->buffer = mem_fileopen ();
60 mi_console->prefix = prefix;
61 mi_console->quote = quote;
62 set_ui_file_fputs (ui_file, mi_console_file_fputs);
63 set_ui_file_flush (ui_file, mi_console_file_flush);
64 set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
ef5ccd6c 65
5796c8dc
SS
66 return ui_file;
67}
68
69static void
70mi_console_file_delete (struct ui_file *file)
71{
72 struct mi_console_file *mi_console = ui_file_data (file);
cf7f2e2d 73
5796c8dc
SS
74 if (mi_console->magic != &mi_console_file_magic)
75 internal_error (__FILE__, __LINE__,
76 _("mi_console_file_delete: bad magic number"));
ef5ccd6c 77
5796c8dc
SS
78 xfree (mi_console);
79}
80
81static void
ef5ccd6c 82mi_console_file_fputs (const char *buf, struct ui_file *file)
5796c8dc
SS
83{
84 struct mi_console_file *mi_console = ui_file_data (file);
cf7f2e2d 85
5796c8dc
SS
86 if (mi_console->magic != &mi_console_file_magic)
87 internal_error (__FILE__, __LINE__,
88 "mi_console_file_fputs: bad magic number");
ef5ccd6c
JM
89
90 /* Append the text to our internal buffer. */
5796c8dc 91 fputs_unfiltered (buf, mi_console->buffer);
ef5ccd6c 92 /* Flush when an embedded newline is present anywhere in the buffer. */
5796c8dc
SS
93 if (strchr (buf, '\n') != NULL)
94 gdb_flush (file);
95}
96
ef5ccd6c
JM
97/* Transform a byte sequence into a console output packet. */
98
5796c8dc 99static void
ef5ccd6c 100mi_console_raw_packet (void *data, const char *buf, long length_buf)
5796c8dc
SS
101{
102 struct mi_console_file *mi_console = data;
cf7f2e2d 103
5796c8dc
SS
104 if (mi_console->magic != &mi_console_file_magic)
105 internal_error (__FILE__, __LINE__,
ef5ccd6c 106 _("mi_console_raw_packet: bad magic number"));
5796c8dc
SS
107
108 if (length_buf > 0)
109 {
110 fputs_unfiltered (mi_console->prefix, mi_console->raw);
111 if (mi_console->quote)
112 {
113 fputs_unfiltered ("\"", mi_console->raw);
c50c785c
JM
114 fputstrn_unfiltered (buf, length_buf,
115 mi_console->quote, mi_console->raw);
5796c8dc
SS
116 fputs_unfiltered ("\"\n", mi_console->raw);
117 }
118 else
119 {
120 fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
121 fputs_unfiltered ("\n", mi_console->raw);
122 }
123 gdb_flush (mi_console->raw);
124 }
125}
126
127static void
128mi_console_file_flush (struct ui_file *file)
129{
130 struct mi_console_file *mi_console = ui_file_data (file);
cf7f2e2d 131
5796c8dc
SS
132 if (mi_console->magic != &mi_console_file_magic)
133 internal_error (__FILE__, __LINE__,
134 _("mi_console_file_flush: bad magic number"));
ef5ccd6c 135
5796c8dc
SS
136 ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
137 ui_file_rewind (mi_console->buffer);
ef5ccd6c
JM
138
139}
140
141/* Change the underlying stream of the console directly; this is
142 useful as a minimum-impact way to reflect external changes like
143 logging enable/disable. */
144
145void
146mi_console_set_raw (struct ui_file *file, struct ui_file *raw)
147{
148 struct mi_console_file *mi_console = ui_file_data (file);
149
150 if (mi_console->magic != &mi_console_file_magic)
151 internal_error (__FILE__, __LINE__,
152 _("mi_console_file_set_raw: bad magic number"));
153
154 mi_console->raw = raw;
5796c8dc 155}