3 Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "mi-console.h"
25 #include "gdb_string.h"
27 /* MI-console: send output to std-out but correcty encapsulated */
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 int mi_console_file_magic;
45 mi_console_file_new (struct ui_file *raw,
46 const char *prefix, char quote)
48 struct ui_file *ui_file = ui_file_new ();
49 struct mi_console_file *mi_console = XMALLOC (struct mi_console_file);
51 mi_console->magic = &mi_console_file_magic;
52 mi_console->raw = raw;
53 mi_console->buffer = mem_fileopen ();
54 mi_console->prefix = prefix;
55 mi_console->quote = quote;
56 set_ui_file_fputs (ui_file, mi_console_file_fputs);
57 set_ui_file_flush (ui_file, mi_console_file_flush);
58 set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
63 mi_console_file_delete (struct ui_file *file)
65 struct mi_console_file *mi_console = ui_file_data (file);
67 if (mi_console->magic != &mi_console_file_magic)
68 internal_error (__FILE__, __LINE__,
69 _("mi_console_file_delete: bad magic number"));
74 mi_console_file_fputs (const char *buf,
77 struct mi_console_file *mi_console = ui_file_data (file);
79 if (mi_console->magic != &mi_console_file_magic)
80 internal_error (__FILE__, __LINE__,
81 "mi_console_file_fputs: bad magic number");
82 /* Append the text to our internal buffer */
83 fputs_unfiltered (buf, mi_console->buffer);
84 /* Flush when an embedded \n */
85 if (strchr (buf, '\n') != NULL)
89 /* Transform a byte sequence into a console output packet. */
91 mi_console_raw_packet (void *data,
95 struct mi_console_file *mi_console = data;
97 if (mi_console->magic != &mi_console_file_magic)
98 internal_error (__FILE__, __LINE__,
99 _("mi_console_file_transform: bad magic number"));
103 fputs_unfiltered (mi_console->prefix, mi_console->raw);
104 if (mi_console->quote)
106 fputs_unfiltered ("\"", mi_console->raw);
107 fputstrn_unfiltered (buf, length_buf, mi_console->quote, mi_console->raw);
108 fputs_unfiltered ("\"\n", mi_console->raw);
112 fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
113 fputs_unfiltered ("\n", mi_console->raw);
115 gdb_flush (mi_console->raw);
120 mi_console_file_flush (struct ui_file *file)
122 struct mi_console_file *mi_console = ui_file_data (file);
124 if (mi_console->magic != &mi_console_file_magic)
125 internal_error (__FILE__, __LINE__,
126 _("mi_console_file_flush: bad magic number"));
127 ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
128 ui_file_rewind (mi_console->buffer);