3 Copyright (C) 2000-2015 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
28 #include "mi-console.h"
29 static ui_file_fputs_ftype mi_console_file_fputs;
30 static ui_file_flush_ftype mi_console_file_flush;
31 static ui_file_delete_ftype mi_console_file_delete;
33 struct mi_console_file
37 struct ui_file *buffer;
42 /* Use the address of this otherwise-unused global as a magic number
43 identifying this class of ui_file objects. */
44 static int mi_console_file_magic;
46 /* Create a console that wraps the given output stream RAW with the
47 string PREFIX and quoting it with QUOTE. */
50 mi_console_file_new (struct ui_file *raw, const char *prefix, char quote)
52 struct ui_file *ui_file = ui_file_new ();
53 struct mi_console_file *mi_console = XNEW (struct mi_console_file);
55 mi_console->magic = &mi_console_file_magic;
56 mi_console->raw = raw;
57 mi_console->buffer = mem_fileopen ();
58 mi_console->prefix = prefix;
59 mi_console->quote = quote;
60 set_ui_file_fputs (ui_file, mi_console_file_fputs);
61 set_ui_file_flush (ui_file, mi_console_file_flush);
62 set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
68 mi_console_file_delete (struct ui_file *file)
70 struct mi_console_file *mi_console
71 = (struct mi_console_file *) ui_file_data (file);
73 if (mi_console->magic != &mi_console_file_magic)
74 internal_error (__FILE__, __LINE__,
75 _("mi_console_file_delete: bad magic number"));
81 mi_console_file_fputs (const char *buf, struct ui_file *file)
83 struct mi_console_file *mi_console
84 = (struct mi_console_file *) ui_file_data (file);
86 if (mi_console->magic != &mi_console_file_magic)
87 internal_error (__FILE__, __LINE__,
88 "mi_console_file_fputs: bad magic number");
90 /* Append the text to our internal buffer. */
91 fputs_unfiltered (buf, mi_console->buffer);
92 /* Flush when an embedded newline is present anywhere in the buffer. */
93 if (strchr (buf, '\n') != NULL)
97 /* Transform a byte sequence into a console output packet. */
100 mi_console_raw_packet (void *data, const char *buf, long length_buf)
102 struct mi_console_file *mi_console = (struct mi_console_file *) data;
104 if (mi_console->magic != &mi_console_file_magic)
105 internal_error (__FILE__, __LINE__,
106 _("mi_console_raw_packet: bad magic number"));
110 fputs_unfiltered (mi_console->prefix, mi_console->raw);
111 if (mi_console->quote)
113 fputc_unfiltered (mi_console->quote, mi_console->raw);
114 fputstrn_unfiltered (buf, length_buf,
115 mi_console->quote, mi_console->raw);
116 fputc_unfiltered (mi_console->quote, mi_console->raw);
117 fputc_unfiltered ('\n', mi_console->raw);
121 fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
122 fputc_unfiltered ('\n', mi_console->raw);
124 gdb_flush (mi_console->raw);
129 mi_console_file_flush (struct ui_file *file)
131 struct mi_console_file *mi_console
132 = (struct mi_console_file *) ui_file_data (file);
134 if (mi_console->magic != &mi_console_file_magic)
135 internal_error (__FILE__, __LINE__,
136 _("mi_console_file_flush: bad magic number"));
138 ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
139 ui_file_rewind (mi_console->buffer);
143 /* Change the underlying stream of the console directly; this is
144 useful as a minimum-impact way to reflect external changes like
145 logging enable/disable. */
148 mi_console_set_raw (struct ui_file *file, struct ui_file *raw)
150 struct mi_console_file *mi_console
151 = (struct mi_console_file *) ui_file_data (file);
153 if (mi_console->magic != &mi_console_file_magic)
154 internal_error (__FILE__, __LINE__,
155 _("mi_console_file_set_raw: bad magic number"));
157 mi_console->raw = raw;