3 Copyright (C) 2000, 2001, 2002, 2007, 2008, 2009
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);
50 mi_console->magic = &mi_console_file_magic;
51 mi_console->raw = raw;
52 mi_console->buffer = mem_fileopen ();
53 mi_console->prefix = prefix;
54 mi_console->quote = quote;
55 set_ui_file_fputs (ui_file, mi_console_file_fputs);
56 set_ui_file_flush (ui_file, mi_console_file_flush);
57 set_ui_file_data (ui_file, mi_console, mi_console_file_delete);
62 mi_console_file_delete (struct ui_file *file)
64 struct mi_console_file *mi_console = ui_file_data (file);
65 if (mi_console->magic != &mi_console_file_magic)
66 internal_error (__FILE__, __LINE__,
67 _("mi_console_file_delete: bad magic number"));
72 mi_console_file_fputs (const char *buf,
75 struct mi_console_file *mi_console = ui_file_data (file);
76 if (mi_console->magic != &mi_console_file_magic)
77 internal_error (__FILE__, __LINE__,
78 "mi_console_file_fputs: bad magic number");
79 /* Append the text to our internal buffer */
80 fputs_unfiltered (buf, mi_console->buffer);
81 /* Flush when an embedded \n */
82 if (strchr (buf, '\n') != NULL)
86 /* Transform a byte sequence into a console output packet. */
88 mi_console_raw_packet (void *data,
92 struct mi_console_file *mi_console = data;
93 if (mi_console->magic != &mi_console_file_magic)
94 internal_error (__FILE__, __LINE__,
95 _("mi_console_file_transform: bad magic number"));
99 fputs_unfiltered (mi_console->prefix, mi_console->raw);
100 if (mi_console->quote)
102 fputs_unfiltered ("\"", mi_console->raw);
103 fputstrn_unfiltered (buf, length_buf, mi_console->quote, mi_console->raw);
104 fputs_unfiltered ("\"\n", mi_console->raw);
108 fputstrn_unfiltered (buf, length_buf, 0, mi_console->raw);
109 fputs_unfiltered ("\n", mi_console->raw);
111 gdb_flush (mi_console->raw);
116 mi_console_file_flush (struct ui_file *file)
118 struct mi_console_file *mi_console = ui_file_data (file);
119 if (mi_console->magic != &mi_console_file_magic)
120 internal_error (__FILE__, __LINE__,
121 _("mi_console_file_flush: bad magic number"));
122 ui_file_put (mi_console->buffer, mi_console_raw_packet, mi_console);
123 ui_file_rewind (mi_console->buffer);